Improve consistency of tag types; streamline CLAMP macro

This commit is contained in:
Gerry Hernandez 2026-03-10 12:20:29 -07:00
parent 923c7add5b
commit 686bd7f6ec
5 changed files with 36 additions and 31 deletions

View file

@ -46,7 +46,7 @@ static int32_t Aflag;
static uint32_t occ, seltags, total_clients, urg;
static char *output_name;
static int32_t tagcount;
static uint32_t tag_count;
static char *tagset;
static char *layout_name;
static int32_t layoutcount, layout_idx;
@ -85,10 +85,11 @@ static void noop_scale(void *data, struct wl_output *wl_output,
static void noop_description(void *data, struct wl_output *wl_output,
const char *description) {}
// Convert n to an N-bit binary string, store result in buf (minimum length bits+1)
void bin_str_nbits(char *buf, uint32_t n, int32_t bits) {
for (int32_t i = bits - 1; i >= 0; i--) {
*buf++ = ((n >> i) & 1) ? '1' : '0';
// Convert num to an N-bit binary string, store result in buf (minimum length
// nbits+1)
void bin_str_nbits(char *buf, uint32_t num, uint32_t nbits) {
for (int32_t i = nbits - 1; i >= 0; i--) {
*buf++ = ((num >> i) & 1) ? '1' : '0';
}
*buf = '\0'; // 字符串结尾
}
@ -96,9 +97,9 @@ void bin_str_nbits(char *buf, uint32_t n, int32_t bits) {
static void dwl_ipc_tags(void *data,
struct zdwl_ipc_manager_v2 *dwl_ipc_manager,
uint32_t count) {
tagcount = count;
tag_count = count;
if (Tflag && mode & GET)
printf("%d\n", tagcount);
printf("%u\n", tag_count);
}
static void dwl_ipc_layout(void *data,
@ -324,7 +325,7 @@ static void dwl_ipc_output_frame(void *data,
if (tflag) {
uint32_t mask = seltags;
char *t = tagset;
int32_t i = 0;
uint32_t i = 0;
for (; *t && *t >= '0' && *t <= '9'; t++)
i = *t - '0' + i * 10;
@ -346,7 +347,7 @@ static void dwl_ipc_output_frame(void *data,
}
}
if ((i - 1) > tagcount)
if ((i - 1) > tag_count)
die("bad tagset %s", tagset);
zdwl_ipc_output_v2_set_tags(dwl_ipc_output, mask, 0);
@ -354,7 +355,7 @@ static void dwl_ipc_output_frame(void *data,
if (cflag) {
uint32_t and = ~0, xor = 0;
char *t = client_tags;
int32_t i = 0;
uint32_t i = 0;
for (; *t && *t >= '0' && *t <= '9'; t++)
i = *t - '0' + i * 10;
@ -376,7 +377,7 @@ static void dwl_ipc_output_frame(void *data,
break;
}
}
if ((i - 1) > tagcount)
if ((i - 1) > tag_count)
die("bad client tagset %s", client_tags);
zdwl_ipc_output_v2_set_client_tags(dwl_ipc_output, and, xor);
@ -395,11 +396,11 @@ static void dwl_ipc_output_frame(void *data,
printf("%s clients %u\n", output_name, total_clients);
char occ_str[tagcount + 1], seltags_str[tagcount + 1], urg_str[tagcount + 1];
char occ_str[tag_count + 1], seltags_str[tag_count + 1], urg_str[tag_count + 1];
bin_str_nbits(occ_str, occ, tagcount);
bin_str_nbits(seltags_str, seltags, tagcount);
bin_str_nbits(urg_str, urg, tagcount);
bin_str_nbits(occ_str, occ, tag_count);
bin_str_nbits(seltags_str, seltags, tag_count);
bin_str_nbits(urg_str, urg, tag_count);
printf("%s tags %u %u %u\n", output_name, occ, seltags, urg);
printf("%s tags %s %s %s\n", output_name, occ_str, seltags_str,
urg_str);

View file

@ -8,15 +8,19 @@
#define SYSCONFDIR "/etc"
#endif
// Clamps value in range while preserving numeric type
#define CLAMP(x, min, max) \
((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)))
// 整数版本 - 截断小数部分
// Deprecated: use CLAMP or CLAMP with explicit casts instead
#define CLAMP_INT(x, min, max) \
((int32_t)(x) < (int32_t)(min) \
? (int32_t)(min) \
: ((int32_t)(x) > (int32_t)(max) ? (int32_t)(max) : (int32_t)(x)))
CLAMP((int32_t)(x), (int32_t)(min), (int32_t)(max))
// 浮点数版本 - 保留小数部分
// Deprecated: use CLAMP instead
#define CLAMP_FLOAT(x, min, max) \
((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)))
CLAMP(x, min, max)
enum { NUM_TYPE_MINUS, NUM_TYPE_PLUS, NUM_TYPE_DEFAULT };
@ -159,7 +163,7 @@ typedef struct {
} GestureBinding;
typedef struct {
int32_t id;
uint32_t id;
char *layout_name;
char *monitor_name;
char *monitor_make;
@ -314,7 +318,7 @@ typedef struct {
char autostart[3][256];
int32_t tag_count;
uint32_t tag_count;
ConfigTagRule *tag_rules; // 动态数组
int32_t tag_rules_count; // 数量
@ -1586,7 +1590,7 @@ bool parse_option(Config *config, char *key, char *value) {
} else if (strcmp(key, "default_nmaster") == 0) {
config->default_nmaster = atoi(value);
} else if (strcmp(key, "tag_count") == 0) {
config->tag_count = CLAMP_INT(atoi(value), 1, 32);
config->tag_count = CLAMP(atoi(value), 1, 32);
tag_count = config->tag_count;
} else if (strcmp(key, "center_master_overspread") == 0) {
config->center_master_overspread = atoi(value);
@ -3707,7 +3711,7 @@ void reapply_pointer(void) {
void reapply_master(void) {
int32_t i;
uint32_t i;
Monitor *m = NULL;
for (i = 0; i <= tag_count; i++) {
wl_list_for_each(m, &mons, link) {
@ -3725,7 +3729,7 @@ void reapply_master(void) {
}
void parse_tagrule(Monitor *m) {
int32_t i, jk;
uint32_t i, jk;
ConfigTagRule tr;
Client *c = NULL;
bool match_rule = false;

View file

@ -207,7 +207,7 @@ enum libinput_config_tap_button_map button_map = LIBINPUT_CONFIG_TAP_MAP_LRM;
/* If you want to use the windows key for MODKEY, use WLR_MODIFIER_LOGO */
#define MODKEY WLR_MODIFIER_ALT
int32_t tag_count = 9;
uint32_t tag_count = 9;
float focused_opacity = 1.0;
float unfocused_opacity = 1.0;

View file

@ -112,15 +112,15 @@ void dwl_ipc_output_printstatus_to(DwlIpcOutput *ipc_output) {
Client *c = NULL, *focused = NULL;
struct wlr_keyboard *keyboard;
xkb_layout_index_t current;
int32_t tagmask, state, numclients, focused_client, tag;
uint32_t tagmask, state, numclients, focused_client, tag_idx;
const char *title, *appid, *symbol;
char kb_layout[32];
focused = focustop(monitor);
zdwl_ipc_output_v2_send_active(ipc_output->resource, monitor == selmon);
for (tag = 0; tag < tag_count; tag++) {
for (tag_idx = 0; tag_idx < tag_count; tag_idx++) {
numclients = state = focused_client = 0;
tagmask = 1 << tag;
tagmask = 1 << tag_idx;
if ((tagmask & monitor->tagset[monitor->seltags]) != 0)
state |= ZDWL_IPC_OUTPUT_V2_TAG_STATE_ACTIVE;
wl_list_for_each(c, &clients, link) {
@ -134,7 +134,7 @@ void dwl_ipc_output_printstatus_to(DwlIpcOutput *ipc_output) {
state |= ZDWL_IPC_OUTPUT_V2_TAG_STATE_URGENT;
numclients++;
}
zdwl_ipc_output_v2_send_tag(ipc_output->resource, tag, state,
zdwl_ipc_output_v2_send_tag(ipc_output->resource, tag_idx, state,
numclients, focused_client);
}

View file

@ -74,7 +74,7 @@ static void handle_ext_workspace_deactivate(struct wl_listener *listener,
static void get_name_from_tag_number(char *dst_buf, size_t dst_len,
uint32_t tag_number) {
assert(tag_number <= (uint32_t)tag_count);
assert(tag_number <= tag_count);
if (tag_number == 0)
snprintf(dst_buf, dst_len, "overview");
else
@ -169,7 +169,7 @@ void dwl_ext_workspace_printstatus(Monitor *m) {
}
void refresh_monitors_workspaces_status(Monitor *m) {
int32_t i;
uint32_t i;
if (m->isoverview) {
for (i = 1; i <= tag_count; i++) {