fix: miss free kb_layout string

This commit is contained in:
DreamMaoMao 2025-11-01 22:54:53 +08:00
parent 3243de5db8
commit bc9accd08d
3 changed files with 31 additions and 35 deletions

View file

@ -113,7 +113,8 @@ void dwl_ipc_output_printstatus_to(DwlIpcOutput *ipc_output) {
struct wlr_keyboard *keyboard; struct wlr_keyboard *keyboard;
xkb_layout_index_t current; xkb_layout_index_t current;
int tagmask, state, numclients, focused_client, tag; int tagmask, state, numclients, focused_client, tag;
const char *title, *appid, *symbol, *kb_layout; const char *title, *appid, *symbol;
char kb_layout[32];
focused = focustop(monitor); focused = focustop(monitor);
zdwl_ipc_output_v2_send_active(ipc_output->resource, monitor == selmon); zdwl_ipc_output_v2_send_active(ipc_output->resource, monitor == selmon);
@ -151,8 +152,8 @@ void dwl_ipc_output_printstatus_to(DwlIpcOutput *ipc_output) {
keyboard = &kb_group->wlr_group->keyboard; keyboard = &kb_group->wlr_group->keyboard;
current = xkb_state_serialize_layout(keyboard->xkb_state, current = xkb_state_serialize_layout(keyboard->xkb_state,
XKB_STATE_LAYOUT_EFFECTIVE); XKB_STATE_LAYOUT_EFFECTIVE);
kb_layout = get_layout_abbr(kb_layout,
get_layout_abbr(xkb_keymap_layout_get_name(keyboard->keymap, current)); xkb_keymap_layout_get_name(keyboard->keymap, current));
zdwl_ipc_output_v2_send_layout( zdwl_ipc_output_v2_send_layout(
ipc_output->resource, ipc_output->resource,

View file

@ -44,11 +44,15 @@ char *get_autostart_path(char *autostart_path, unsigned int buf_size) {
return autostart_path; return autostart_path;
} }
const char *get_layout_abbr(const char *full_name) { void get_layout_abbr(char *abbr, const char *full_name) {
// 清空输出缓冲区
abbr[0] = '\0';
// 1. 尝试在映射表中查找 // 1. 尝试在映射表中查找
for (int i = 0; layout_mappings[i].full_name != NULL; i++) { for (int i = 0; layout_mappings[i].full_name != NULL; i++) {
if (strcmp(full_name, layout_mappings[i].full_name) == 0) { if (strcmp(full_name, layout_mappings[i].full_name) == 0) {
return layout_mappings[i].abbr; strcpy(abbr, layout_mappings[i].abbr);
return;
} }
} }
@ -58,21 +62,16 @@ const char *get_layout_abbr(const char *full_name) {
if (open && close && close > open) { if (open && close && close > open) {
unsigned int len = close - open - 1; unsigned int len = close - open - 1;
if (len > 0 && len <= 4) { if (len > 0 && len <= 4) {
char *abbr = malloc(len + 1);
if (abbr) {
// 提取并转换为小写 // 提取并转换为小写
for (unsigned int j = 0; j < len; j++) { for (unsigned int j = 0; j < len; j++) {
abbr[j] = tolower(open[j + 1]); abbr[j] = tolower(open[j + 1]);
} }
abbr[len] = '\0'; abbr[len] = '\0';
return abbr; return;
}
} }
} }
// 3. 提取前2-3个字母并转换为小写 // 3. 提取前2-3个字母并转换为小写
char *abbr = malloc(4);
if (abbr) {
unsigned int j = 0; unsigned int j = 0;
for (unsigned int i = 0; full_name[i] != '\0' && j < 3; i++) { for (unsigned int i = 0; full_name[i] != '\0' && j < 3; i++) {
if (isalpha(full_name[i])) { if (isalpha(full_name[i])) {
@ -82,22 +81,18 @@ const char *get_layout_abbr(const char *full_name) {
abbr[j] = '\0'; abbr[j] = '\0';
// 确保至少2个字符 // 确保至少2个字符
if (j >= 2) if (j >= 2) {
return abbr; return;
free(abbr);
} }
// 4. 回退方案:使用首字母小写 // 4. 回退方案:使用首字母小写
char *fallback = malloc(3); if (j == 1) {
if (fallback) { abbr[1] = full_name[1] ? tolower(full_name[1]) : '\0';
fallback[0] = tolower(full_name[0]); abbr[2] = '\0';
fallback[1] = full_name[1] ? tolower(full_name[1]) : '\0'; } else {
fallback[2] = '\0';
return fallback;
}
// 5. 最终回退:返回 "xx" // 5. 最终回退:返回 "xx"
return strdup("xx"); strcpy(abbr, "xx");
}
} }
void xytonode(double x, double y, struct wlr_surface **psurface, Client **pc, void xytonode(double x, double y, struct wlr_surface **psurface, Client **pc,

View file

@ -693,7 +693,7 @@ wlr_scene_tree_snapshot(struct wlr_scene_node *node,
struct wlr_scene_tree *parent); struct wlr_scene_tree *parent);
static bool is_scroller_layout(Monitor *m); static bool is_scroller_layout(Monitor *m);
static void create_output(struct wlr_backend *backend, void *data); static void create_output(struct wlr_backend *backend, void *data);
static const char *get_layout_abbr(const char *full_name); static void get_layout_abbr(char *abbr, const char *full_name);
static void apply_named_scratchpad(Client *target_client); static void apply_named_scratchpad(Client *target_client);
static Client *get_client_by_id_or_title(const char *arg_id, static Client *get_client_by_id_or_title(const char *arg_id,
const char *arg_title); const char *arg_title);