replaced strcmp for command strings with a pointer cmd to where the command should be in sockbuf, and repeated ADVANCE_IF_LAST_RET() to set wordbeg to the xdg_output_name

This commit is contained in:
samjjacko 2025-07-24 18:06:18 +08:00
parent 48dbe00bdb
commit 5221e192f3

270
dwlb.c
View File

@ -1447,149 +1447,151 @@ copy_customtext(CustomText *from, CustomText *to)
static void static void
read_socket(void) read_socket(void)
{ {
int cli_fd; int cli_fd;
if ((cli_fd = accept(sock_fd, NULL, 0)) == -1) if ((cli_fd = accept(sock_fd, NULL, 0)) == -1)
EDIE("accept"); EDIE("accept");
ssize_t len = recv(cli_fd, sockbuf, sizeof sockbuf - 1, 0); ssize_t len = recv(cli_fd, sockbuf, sizeof sockbuf - 1, 0);
if (len == -1) if (len == -1)
EDIE("recv"); EDIE("recv");
close(cli_fd); close(cli_fd);
if (len == 0) if (len == 0)
return; return;
sockbuf[len] = '\0'; sockbuf[len] = '\0';
char *wordbeg, *wordend; char *wordbeg, *wordend;
wordend = (char *)&sockbuf; wordend = (char *)&sockbuf;
ADVANCE_IF_LAST_RET(); ADVANCE_IF_LAST_RET();
Bar *bar = NULL, *it; char *cmd = wordbeg;
bool all = false;
if (!strcmp(wordbeg, "all")) { ADVANCE_IF_LAST_RET();
all = true;
} else if (!strcmp(wordbeg, "selected")) {
wl_list_for_each(it, &bar_list, link) {
if (it->sel) {
bar = it;
break;
}
}
} else {
wl_list_for_each(it, &bar_list, link) {
if (it->xdg_output_name && !strcmp(wordbeg, it->xdg_output_name)) {
bar = it;
break;
}
}
}
if (!all && !bar) Bar *bar = NULL, *it;
return; bool all = false;
ADVANCE(); if (!strcmp(wordbeg, "all")) {
all = true;
} else if (!strcmp(wordbeg, "selected")) {
wl_list_for_each(it, &bar_list, link) {
if (it->sel) {
bar = it;
break;
}
}
} else {
wl_list_for_each(it, &bar_list, link) {
if (it->xdg_output_name && !strcmp(wordbeg, it->xdg_output_name)) {
bar = it;
break;
}
}
}
if (!strcmp(wordbeg, "status")) { if (!all && !bar)
if (!*wordend) return;
return;
if (all) {
Bar *first = NULL;
wl_list_for_each(bar, &bar_list, link) {
if (first) {
copy_customtext(&first->status, &bar->status);
} else {
parse_into_customtext(&bar->status, wordend);
first = bar;
}
bar->redraw = true;
}
} else {
parse_into_customtext(&bar->status, wordend);
bar->redraw = true;
}
} else if (!strcmp(wordbeg, "title")) {
if (!custom_title || !*wordend)
return;
if (all) {
Bar *first = NULL;
wl_list_for_each(bar, &bar_list, link) {
if (first) {
copy_customtext(&first->title, &bar->title);
} else {
parse_into_customtext(&bar->title, wordend);
first = bar;
}
bar->redraw = true;
}
} else {
parse_into_customtext(&bar->title, wordend);
bar->redraw = true;
}
} else if (!strcmp(wordbeg, "show")) {
if (all) {
wl_list_for_each(bar, &bar_list, link)
if (bar->hidden)
show_bar(bar);
} else {
if (bar->hidden)
show_bar(bar);
}
} else if (!strcmp(wordbeg, "hide")) {
if (all) {
wl_list_for_each(bar, &bar_list, link)
if (!bar->hidden)
hide_bar(bar);
} else {
if (!bar->hidden)
hide_bar(bar);
}
} else if (!strcmp(wordbeg, "toggle-visibility")) {
if (all) {
wl_list_for_each(bar, &bar_list, link)
if (bar->hidden)
show_bar(bar);
else
hide_bar(bar);
} else {
if (bar->hidden)
show_bar(bar);
else
hide_bar(bar);
}
} else if (!strcmp(wordbeg, "set-top")) {
if (all) {
wl_list_for_each(bar, &bar_list, link)
if (bar->bottom)
set_top(bar);
} else { ADVANCE();
if (bar->bottom)
set_top(bar);
}
} else if (!strcmp(wordbeg, "set-bottom")) {
if (all) {
wl_list_for_each(bar, &bar_list, link)
if (!bar->bottom)
set_bottom(bar);
} else { if (!strcmp(cmd, "status")) {
if (!bar->bottom) if (!*wordend)
set_bottom(bar); return;
} if (all) {
} else if (!strcmp(wordbeg, "toggle-location")) { Bar *first = NULL;
if (all) { wl_list_for_each(bar, &bar_list, link) {
wl_list_for_each(bar, &bar_list, link) if (first) {
if (bar->bottom) copy_customtext(&first->status, &bar->status);
set_top(bar); } else {
else parse_into_customtext(&bar->status, wordend);
set_bottom(bar); first = bar;
} else { }
if (bar->bottom) bar->redraw = true;
set_top(bar); }
else } else {
set_bottom(bar); parse_into_customtext(&bar->status, wordend);
} bar->redraw = true;
} }
} else if (!strcmp(cmd, "title")) {
if (!custom_title || !*wordend)
return;
if (all) {
Bar *first = NULL;
wl_list_for_each(bar, &bar_list, link) {
if (first) {
copy_customtext(&first->title, &bar->title);
} else {
parse_into_customtext(&bar->title, wordend);
first = bar;
}
bar->redraw = true;
}
} else {
parse_into_customtext(&bar->title, wordend);
bar->redraw = true;
}
} else if (!strcmp(cmd, "show")) {
if (all) {
wl_list_for_each(bar, &bar_list, link)
if (bar->hidden)
show_bar(bar);
} else {
if (bar->hidden)
show_bar(bar);
}
} else if (!strcmp(cmd, "hide")) {
if (all) {
wl_list_for_each(bar, &bar_list, link)
if (!bar->hidden)
hide_bar(bar);
} else {
if (!bar->hidden)
hide_bar(bar);
}
} else if (!strcmp(cmd, "toggle-visibility")) {
if (all) {
wl_list_for_each(bar, &bar_list, link)
if (bar->hidden)
show_bar(bar);
else
hide_bar(bar);
} else {
if (bar->hidden)
show_bar(bar);
else
hide_bar(bar);
}
} else if (!strcmp(cmd, "set-top")) {
if (all) {
wl_list_for_each(bar, &bar_list, link)
if (bar->bottom)
set_top(bar);
} else {
if (bar->bottom)
set_top(bar);
}
} else if (!strcmp(cmd, "set-bottom")) {
if (all) {
wl_list_for_each(bar, &bar_list, link)
if (!bar->bottom)
set_bottom(bar);
} else {
if (!bar->bottom)
set_bottom(bar);
}
} else if (!strcmp(cmd, "toggle-location")) {
if (all) {
wl_list_for_each(bar, &bar_list, link)
if (bar->bottom)
set_top(bar);
else
set_bottom(bar);
} else {
if (bar->bottom)
set_top(bar);
else
set_bottom(bar);
}
}
} }
static void static void