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

282
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;
bool all = false;
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 (!all && !bar)
return;
ADVANCE();
if (!strcmp(wordbeg, "status")) { char *cmd = wordbeg;
if (!*wordend)
return; ADVANCE_IF_LAST_RET();
if (all) {
Bar *first = NULL; Bar *bar = NULL, *it;
wl_list_for_each(bar, &bar_list, link) { bool all = false;
if (first) {
copy_customtext(&first->status, &bar->status); if (!strcmp(wordbeg, "all")) {
} else { all = true;
parse_into_customtext(&bar->status, wordend); } else if (!strcmp(wordbeg, "selected")) {
first = bar; wl_list_for_each(it, &bar_list, link) {
} if (it->sel) {
bar->redraw = true; bar = it;
} break;
} else { }
parse_into_customtext(&bar->status, wordend); }
bar->redraw = true; } else {
} wl_list_for_each(it, &bar_list, link) {
} else if (!strcmp(wordbeg, "title")) { if (it->xdg_output_name && !strcmp(wordbeg, it->xdg_output_name)) {
if (!custom_title || !*wordend) bar = it;
return; break;
if (all) { }
Bar *first = NULL; }
wl_list_for_each(bar, &bar_list, link) { }
if (first) {
copy_customtext(&first->title, &bar->title); if (!all && !bar)
} else { return;
parse_into_customtext(&bar->title, wordend);
first = bar; ADVANCE();
}
bar->redraw = true; if (!strcmp(cmd, "status")) {
} if (!*wordend)
} else { return;
parse_into_customtext(&bar->title, wordend); if (all) {
bar->redraw = true; Bar *first = NULL;
} wl_list_for_each(bar, &bar_list, link) {
} else if (!strcmp(wordbeg, "show")) { if (first) {
if (all) { copy_customtext(&first->status, &bar->status);
wl_list_for_each(bar, &bar_list, link) } else {
if (bar->hidden) parse_into_customtext(&bar->status, wordend);
show_bar(bar); first = bar;
} else { }
if (bar->hidden) bar->redraw = true;
show_bar(bar); }
} } else {
} else if (!strcmp(wordbeg, "hide")) { parse_into_customtext(&bar->status, wordend);
if (all) { bar->redraw = true;
wl_list_for_each(bar, &bar_list, link) }
if (!bar->hidden) } else if (!strcmp(cmd, "title")) {
hide_bar(bar); if (!custom_title || !*wordend)
} else { return;
if (!bar->hidden) if (all) {
hide_bar(bar); Bar *first = NULL;
} wl_list_for_each(bar, &bar_list, link) {
} else if (!strcmp(wordbeg, "toggle-visibility")) { if (first) {
if (all) { copy_customtext(&first->title, &bar->title);
wl_list_for_each(bar, &bar_list, link) } else {
if (bar->hidden) parse_into_customtext(&bar->title, wordend);
show_bar(bar); first = bar;
else }
hide_bar(bar); bar->redraw = true;
} else { }
if (bar->hidden) } else {
show_bar(bar); parse_into_customtext(&bar->title, wordend);
else bar->redraw = true;
hide_bar(bar); }
} } else if (!strcmp(cmd, "show")) {
} else if (!strcmp(wordbeg, "set-top")) { if (all) {
if (all) { wl_list_for_each(bar, &bar_list, link)
wl_list_for_each(bar, &bar_list, link) if (bar->hidden)
if (bar->bottom) show_bar(bar);
set_top(bar); } else {
if (bar->hidden)
} else { show_bar(bar);
if (bar->bottom) }
set_top(bar); } else if (!strcmp(cmd, "hide")) {
} if (all) {
} else if (!strcmp(wordbeg, "set-bottom")) { wl_list_for_each(bar, &bar_list, link)
if (all) { if (!bar->hidden)
wl_list_for_each(bar, &bar_list, link) hide_bar(bar);
if (!bar->bottom) } else {
set_bottom(bar); if (!bar->hidden)
hide_bar(bar);
} else { }
if (!bar->bottom) } else if (!strcmp(cmd, "toggle-visibility")) {
set_bottom(bar); if (all) {
} wl_list_for_each(bar, &bar_list, link)
} else if (!strcmp(wordbeg, "toggle-location")) { if (bar->hidden)
if (all) { show_bar(bar);
wl_list_for_each(bar, &bar_list, link) else
if (bar->bottom) hide_bar(bar);
set_top(bar); } else {
else if (bar->hidden)
set_bottom(bar); show_bar(bar);
} else { else
if (bar->bottom) hide_bar(bar);
set_top(bar); }
else } else if (!strcmp(cmd, "set-top")) {
set_bottom(bar); 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