From d365278aab24c44cda8c4d79bcfc6eca34a78f4f Mon Sep 17 00:00:00 2001 From: ProsperousPotato Date: Fri, 9 Jan 2026 20:31:56 +0000 Subject: alphabetical functions, better indentation --- dwm.c | 449 +++++++++++++++++++++++++++++++++--------------------------------- 1 file changed, 222 insertions(+), 227 deletions(-) (limited to 'dwm.c') diff --git a/dwm.c b/dwm.c index d7c7a30..f79f9c2 100644 --- a/dwm.c +++ b/dwm.c @@ -152,11 +152,13 @@ static void arrange(Monitor *m); static void arrangemon(Monitor *m); static void attach(Client *c); static void attachstack(Client *c); +static void autostart_exec(void); static void buttonpress(XEvent *e); static void checkotherwm(void); static void cleanup(void); static void cleanupmon(Monitor *mon); static void clientmessage(XEvent *e); +static void clientresize(const Arg *arg); static void configure(Client *c); static void configurenotify(XEvent *e); static void configurerequest(XEvent *e); @@ -196,10 +198,10 @@ static void resize(Client *c, int x, int y, int w, int h, int interact); static void resizeclient(Client *c, int x, int y, int w, int h); static void resizemouse(const Arg *arg); static void restack(Monitor *m); -static void clientresize(const Arg *arg); static void resetmfact(const Arg *arg); static void run(void); static void scan(void); +static void search(const Arg *arg); static int sendevent(Client *c, Atom proto); static void sendmon(Client *c, Monitor *m); static void setclientstate(Client *c, long state); @@ -218,6 +220,7 @@ static void tagmon(const Arg *arg); static void tile(Monitor *m); static void togglefloating(const Arg *arg); static void togglefullscr(const Arg *arg); +static void togglemouse(const Arg *arg); static void toggletag(const Arg *arg); static void toggleview(const Arg *arg); static void unfocus(Client *c, int setfocus); @@ -237,9 +240,6 @@ static int xerror(Display *dpy, XErrorEvent *ee); static int xerrordummy(Display *dpy, XErrorEvent *ee); static int xerrorstart(Display *dpy, XErrorEvent *ee); static void zoom(const Arg *arg); -static void togglemouse(const Arg *arg); -static void search(const Arg *arg); -static void autostart_exec(void); static pid_t getparentprocess(pid_t p); static int isdescprocess(pid_t p, pid_t c); @@ -434,6 +434,29 @@ attachstack(Client *c) c->mon->stack = c; } +static void +autostart_exec() { + const char *const *p; + size_t i = 0; + + for (p = autostart; *p; autostart_len++, p++) + while (*++p); + + autostart_pids = malloc(autostart_len * sizeof(pid_t)); + + for (p = autostart; *p; i++, p++) { + if ((autostart_pids[i] = fork()) == 0) { + setsid(); + execvp(*p, (char *const *)p); + fprintf(stderr, "dwm: execvp %s\n", *p); + perror(" failed"); + _exit(EXIT_FAILURE); + } + + while (*++p); + } +} + void swallow(Client *p, Client *c) { @@ -577,6 +600,23 @@ clientmessage(XEvent *e) } } +void +clientresize(const Arg *arg) +{ + Client *c; + + if (!(c = selmon->sel)) + return; + + if (c->isfullscreen) + return; + + if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) + resizemouse(arg); + else + dragmfact(arg); +} + void configure(Client *c) { @@ -1545,23 +1585,6 @@ resizemouse(const Arg *arg) } } -void -clientresize(const Arg *arg) -{ - Client *c; - - if (!(c = selmon->sel)) - return; - - if (c->isfullscreen) - return; - - if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) - resizemouse(arg); - else - dragmfact(arg); -} - void restack(Monitor *m) { @@ -1631,6 +1654,137 @@ scan(void) } } +void +search(const Arg *arg) { + Client *c; + Monitor *m; + char *names = NULL; + size_t namesize = 0; + size_t nameslen = 0; + int clientnum = 0; + Client **clients = NULL; + size_t clientsize = 0; + int mode = arg->i; + + for (m = mons; m; m = m->next) { + for (c = m->clients; c; c = c->next) { + if (!ISVISIBLE(c)) { + clientnum++; + + char ts[64] = "["; + int tl = 1; + int first = 1; + for (int i = 0; i < LENGTH(tags); i++) { + if (c->tags & (1 << i)) { + if (!first) tl += snprintf(ts + tl, sizeof(ts) - tl, ","); + tl += snprintf(ts + tl, sizeof(ts) - tl, "%d", i + 1); + first = 0; + } + } + tl += snprintf(ts + tl, sizeof(ts) - tl, "] "); + + size_t namelen = strlen(c->name); + size_t needed = nameslen + tl + namelen + 1; + + if (needed > namesize) { + namesize = needed * 2; + names = realloc(names, namesize); + if (!names) + die("search: realloc failed"); + } + + if (clientnum > clientsize) { + clientsize = clientnum * 2; + clients = realloc(clients, clientsize * sizeof(Client *)); + if (!clients) + die("search: realloc failed"); + } + + strlcpy(names + nameslen, ts, namesize - nameslen); + nameslen += tl; + strlcpy(names + nameslen, c->name, namesize - nameslen); + + nameslen += namelen; + names[nameslen] = '\n'; + nameslen++; + + clients[clientnum - 1] = c; + } + } + } + + if (clientnum == 0) + return; + + names[nameslen - 1] = '\0'; + + char dmenucmd[256]; + snprintf(dmenucmd, sizeof(dmenucmd), "echo '%s' | dmenu -vi -l 10 -i -p 'Find client:'", names); + + FILE *fp = popen(dmenucmd, "r"); + if (!fp) { + free(names); + free(clients); + return; + } + + char selname[256]; + if (fgets(selname, sizeof(selname), fp) != NULL) { + size_t len = strlen(selname); + if (len > 0 && selname[len - 1] == '\n') + selname[len - 1] = '\0'; + + char *clientname = strchr(selname, ']'); + if (clientname) { + clientname += 2; + + for (int i = 0; i < clientnum; i++) { + if (strcmp(clients[i]->name, clientname) == 0) { + Client *selclient = clients[i]; + + if (mode == 1 || mode == 2) { + selclient->tags = selmon->tagset[selmon->seltags]; + + if (selclient->mon != selmon) { + detach(selclient); + detachstack(selclient); + selclient->mon = selmon; + attach(selclient); + attachstack(selclient); + } + + focus(selclient); + + if (mode == 1) { + arrange(selmon); + if (selclient != nexttiled(selmon->clients)) { + zoom(0); + } + } else if (mode == 2) + killclient(0); + + } else { + if (selclient->mon != selmon) + selmon = selclient->mon; + + Arg view_arg; + view_arg.ui = selclient->tags; + view(&view_arg); + + focus(selclient); + } + + break; + } + } + } + } + + pclose(fp); + free(names); + free(clients); +} + void sendmon(Client *c, Monitor *m) { @@ -1824,7 +1978,7 @@ setup(void) scheme[i] = drw_scm_create(drw, colors[i], 5); /* init mouse */ - mousedefault == 0 ? togglemouse(NULL) : (void)0 ; + if (mousedefault == 0) togglemouse(NULL); /* supporting window for NetWMCheck */ wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0); @@ -2002,6 +2156,52 @@ togglefullscr(const Arg *arg) setfullscreen(selmon->sel, !selmon->sel->isfullscreen); } +void +togglemouse(const Arg *arg) { + if (ml) { + XUngrabPointer(dpy, CurrentTime); + XWarpPointer(dpy, None, selmon->sel->win, 0, 0, 0, 0, selmon->sel->w/2, selmon->sel->h/2); + if (fw) { + XDestroyWindow(dpy, fw); + fw = 0; + } + ml = 0; + return; + } + + Window root = RootWindow(dpy, screen); + + XSetWindowAttributes attrs; + attrs.override_redirect = True; + attrs.event_mask = 0; + + fw = XCreateWindow( + dpy, root, 0, sh - 1, 1, 1, 0, + DefaultDepth(dpy, screen), + InputOutput, DefaultVisual(dpy, screen), + CWOverrideRedirect | CWEventMask, &attrs + ); + + XMapRaised(dpy, fw); + XSync(dpy, False); + + XWarpPointer(dpy, None, root, 0, 0, 0, 0, 0, sh - 1); + + if (XGrabPointer(dpy, fw, True, + 0, + GrabModeAsync, + GrabModeAsync, + fw, + None, + CurrentTime) != GrabSuccess) { + XDestroyWindow(dpy, fw); + fw = 0; + return; + } + + ml = 1; +} + void toggletag(const Arg *arg) { @@ -2517,211 +2717,6 @@ zoom(const Arg *arg) XWarpPointer(dpy, None, selmon->sel->win, 0, 0, 0, 0, selmon->sel->w/2, selmon->sel->h/2); } -void -search(const Arg *arg) { - Client *c; - Monitor *m; - char *names = NULL; - size_t namesize = 0; - size_t nameslen = 0; - int clientnum = 0; - Client **clients = NULL; - size_t clientsize = 0; - int mode = arg->i; - - for (m = mons; m; m = m->next) { - for (c = m->clients; c; c = c->next) { - if (!ISVISIBLE(c)) { - clientnum++; - - char ts[64] = "["; - int tl = 1; - int first = 1; - for (int i = 0; i < LENGTH(tags); i++) { - if (c->tags & (1 << i)) { - if (!first) tl += snprintf(ts + tl, sizeof(ts) - tl, ","); - tl += snprintf(ts + tl, sizeof(ts) - tl, "%d", i + 1); - first = 0; - } - } - tl += snprintf(ts + tl, sizeof(ts) - tl, "] "); - - size_t namelen = strlen(c->name); - size_t needed = nameslen + tl + namelen + 1; - - if (needed > namesize) { - namesize = needed * 2; - names = realloc(names, namesize); - if (!names) - die("search: realloc failed"); - } - - if (clientnum > clientsize) { - clientsize = clientnum * 2; - clients = realloc(clients, clientsize * sizeof(Client *)); - if (!clients) - die("search: realloc failed"); - } - - strlcpy(names + nameslen, ts, namesize - nameslen); - nameslen += tl; - strlcpy(names + nameslen, c->name, namesize - nameslen); - - nameslen += namelen; - names[nameslen] = '\n'; - nameslen++; - - clients[clientnum - 1] = c; - } - } - } - - if (clientnum == 0) - return; - - names[nameslen - 1] = '\0'; - - char dmenucmd[256]; - snprintf(dmenucmd, sizeof(dmenucmd), "echo '%s' | dmenu -vi -l 10 -i -p 'Find client:'", names); - - FILE *fp = popen(dmenucmd, "r"); - if (!fp) { - free(names); - free(clients); - return; - } - - char selname[256]; - if (fgets(selname, sizeof(selname), fp) != NULL) { - size_t len = strlen(selname); - if (len > 0 && selname[len - 1] == '\n') - selname[len - 1] = '\0'; - - char *clientname = strchr(selname, ']'); - if (clientname) { - clientname += 2; - - for (int i = 0; i < clientnum; i++) { - if (strcmp(clients[i]->name, clientname) == 0) { - Client *selclient = clients[i]; - - if (mode == 1 || mode == 2) { - selclient->tags = selmon->tagset[selmon->seltags]; - - if (selclient->mon != selmon) { - detach(selclient); - detachstack(selclient); - selclient->mon = selmon; - attach(selclient); - attachstack(selclient); - } - - focus(selclient); - - if (mode == 1) { - arrange(selmon); - if (selclient != nexttiled(selmon->clients)) { - zoom(0); - } - } else if (mode == 2) - killclient(0); - - } else { - if (selclient->mon != selmon) - selmon = selclient->mon; - - Arg view_arg; - view_arg.ui = selclient->tags; - view(&view_arg); - - focus(selclient); - - if (selclient != nexttiled(selmon->clients)) { - zoom(0); - } - - } - - break; - } - } - } - } - - pclose(fp); - free(names); - free(clients); -} - -void -togglemouse(const Arg *arg) { - if (ml) { - XUngrabPointer(dpy, CurrentTime); - XWarpPointer(dpy, None, selmon->sel->win, 0, 0, 0, 0, selmon->sel->w/2, selmon->sel->h/2); - if (fw) { - XDestroyWindow(dpy, fw); - fw = 0; - } - ml = 0; - return; - } - - Window root = RootWindow(dpy, screen); - - XSetWindowAttributes attrs; - attrs.override_redirect = True; - attrs.event_mask = 0; - - fw = XCreateWindow( - dpy, root, 0, sh - 1, 1, 1, 0, - DefaultDepth(dpy, screen), - InputOutput, DefaultVisual(dpy, screen), - CWOverrideRedirect | CWEventMask, &attrs - ); - - XMapRaised(dpy, fw); - XSync(dpy, False); - - XWarpPointer(dpy, None, root, 0, 0, 0, 0, 0, sh - 1); - - if (XGrabPointer(dpy, fw, True, - 0, - GrabModeAsync, - GrabModeAsync, - fw, - None, - CurrentTime) != GrabSuccess) { - XDestroyWindow(dpy, fw); - fw = 0; - return; - } - - ml = 1; -} - -static void -autostart_exec() { - const char *const *p; - size_t i = 0; - - for (p = autostart; *p; autostart_len++, p++) - while (*++p); - - autostart_pids = malloc(autostart_len * sizeof(pid_t)); - - for (p = autostart; *p; i++, p++) { - if ((autostart_pids[i] = fork()) == 0) { - setsid(); - execvp(*p, (char *const *)p); - fprintf(stderr, "dwm: execvp %s\n", *p); - perror(" failed"); - _exit(EXIT_FAILURE); - } - - while (*++p); - } -} - int main(int argc, char *argv[]) { -- cgit v1.2.3