X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=dwm.c;h=0b82158554880cf0e8a9d8e6fb911c83fbb874f4;hb=4076e2ff60fd20caaed18209e36ee82e49d26219;hp=ff9e66ab5983bd3704b9a84f683b91470ed8ee1b;hpb=2e8e5509d9cad9229d2d79a1de75038d94032cfb;p=dwm.git diff --git a/dwm.c b/dwm.c index ff9e66a..0b82158 100644 --- a/dwm.c +++ b/dwm.c @@ -60,6 +60,7 @@ enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */ enum { WMProtocols, WMDelete, WMName, WMState, WMLast };/* default atoms */ /* typedefs */ +typedef struct Monitor Monitor; typedef struct Client Client; struct Client { char name[256]; @@ -74,7 +75,7 @@ struct Client { Client *prev; Client *snext; Window win; - int monitor; + Monitor *monitor; }; typedef struct { @@ -99,7 +100,6 @@ typedef struct { const char *arg; } Key; -typedef struct Monitor Monitor; typedef struct { const char *symbol; void (*arrange)(Monitor *); @@ -144,9 +144,9 @@ void configurerequest(XEvent *e); void destroynotify(XEvent *e); void detach(Client *c); void detachstack(Client *c); -void drawbar(void); -void drawsquare(Monitor *, Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]); -void drawtext(Monitor *, const char *text, unsigned long col[ColLast], Bool invert); +void drawbar(Monitor *m); +void drawsquare(Monitor *m, Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]); +void drawtext(Monitor *m, const char *text, unsigned long col[ColLast], Bool invert); void *emallocz(unsigned int size); void enternotify(XEvent *e); void eprint(const char *errstr, ...); @@ -158,29 +158,31 @@ void focusnext(const char *arg); void focusprev(const char *arg); Client *getclient(Window w); unsigned long getcolor(const char *colstr); +Monitor *getmonitor(Window barwin); long getstate(Window w); Bool gettextprop(Window w, Atom atom, char *text, unsigned int size); void grabbuttons(Client *c, Bool focused); void grabkeys(void); unsigned int idxoftag(const char *tag); void initfont(const char *fontstr); -Bool isoccupied(unsigned int monitor, unsigned int t); +Bool isoccupied(Monitor *monitor, unsigned int t); Bool isprotodel(Client *c); -Bool isurgent(unsigned int monitor, unsigned int t); -Bool isvisible(Client *c, int monitor); +Bool isurgent(Monitor *monitor, unsigned int t); +Bool isvisible(Client *c, Monitor *m); void keypress(XEvent *e); void killclient(const char *arg); void manage(Window w, XWindowAttributes *wa); void mappingnotify(XEvent *e); void maprequest(XEvent *e); +Monitor *monitorat(void); void movemouse(Client *c); -Client *nexttiled(Client *c, int monitor); +Client *nexttiled(Client *c, Monitor *monitor); void propertynotify(XEvent *e); void quit(const char *arg); void reapply(const char *arg); void resize(Client *c, int x, int y, int w, int h, Bool sizehints); void resizemouse(Client *c); -void restack(void); +void restack(Monitor *m); void run(void); void scan(void); void setclientstate(Client *c, long state); @@ -209,14 +211,13 @@ int xerror(Display *dpy, XErrorEvent *ee); int xerrordummy(Display *dsply, XErrorEvent *ee); int xerrorstart(Display *dsply, XErrorEvent *ee); void zoom(const char *arg); -int monitorat(void); void movetomonitor(const char *arg); void selectmonitor(const char *arg); /* variables */ char stext[256]; int mcount = 1; -int selmonitor = 0; +Monitor *selmonitor; int screen; int (*xerrorxlib)(Display *, XErrorEvent *); unsigned int bh, bpos; @@ -276,7 +277,7 @@ applyrules(Client *c) { if(regs[i].propregex && !regexec(regs[i].propregex, buf, 1, &tmp, 0)) { if (rules[i].monitor >= 0 && rules[i].monitor < mcount) { matched_monitor = True; - c->monitor = rules[i].monitor; + c->monitor = &monitors[rules[i].monitor]; } c->isfloating = rules[i].isfloating; @@ -292,7 +293,7 @@ applyrules(Client *c) { if(ch.res_name) XFree(ch.res_name); if(!matched_tag) - memcpy(c->tags, monitors[monitorat()].seltags, sizeof initags); + memcpy(c->tags, monitorat()->seltags, sizeof initags); if (!matched_monitor) c->monitor = monitorat(); } @@ -307,9 +308,9 @@ arrange(void) { else ban(c); - monitors[selmonitor].layout->arrange(&monitors[selmonitor]); + selmonitor->layout->arrange(selmonitor); focus(NULL); - restack(); + restack(selmonitor); } void @@ -330,7 +331,7 @@ void ban(Client *c) { if(c->isbanned) return; - XMoveWindow(dpy, c->win, c->x + 3 * monitors[c->monitor].sw, c->y); + XMoveWindow(dpy, c->win, c->x + 3 * c->monitor->sw, c->y); c->isbanned = True; } @@ -340,7 +341,7 @@ buttonpress(XEvent *e) { Client *c; XButtonPressedEvent *ev = &e->xbutton; - Monitor *m = &monitors[monitorat()]; + Monitor *m = monitorat(); if(ev->window == m->barwin) { x = 0; @@ -370,7 +371,7 @@ buttonpress(XEvent *e) { if(CLEANMASK(ev->state) != MODKEY) return; if(ev->button == Button1) { - restack(); + restack(c->monitor); movemouse(c); } else if(ev->button == Button2) { @@ -380,7 +381,7 @@ buttonpress(XEvent *e) { zoom(NULL); } else if(ev->button == Button3 && !c->isfixed) { - restack(); + restack(c->monitor); resizemouse(c); } } @@ -474,7 +475,7 @@ configure(Client *c) { void configurenotify(XEvent *e) { XConfigureEvent *ev = &e->xconfigure; - Monitor *m = &monitors[selmonitor]; + Monitor *m = selmonitor; if(ev->window == root && (ev->width != m->sw || ev->height != m->sh)) { /* TODO -- update Xinerama dimensions here */ @@ -495,7 +496,7 @@ configurerequest(XEvent *e) { XWindowChanges wc; if((c = getclient(ev->window))) { - Monitor *m = &monitors[c->monitor]; + Monitor *m = c->monitor; if(ev->value_mask & CWBorderWidth) c->border = ev->border_width; if(c->isfixed || c->isfloating || (floating == m->layout->arrange)) { @@ -562,55 +563,51 @@ detachstack(Client *c) { } void -drawbar(void) { - int i, j, x; +drawbar(Monitor *m) { + int j, x; Client *c; - for(i = 0; i < mcount; i++) { - Monitor *m = &monitors[i]; - dc.x = 0; - for(c = stack; c && !isvisible(c, i); c = c->snext); - fprintf(stderr, "m%d %s\n", i, c ? c->name : "NIL"); - for(j = 0; j < LENGTH(tags); j++) { - dc.w = textw(tags[j]); - if(m->seltags[j]) { - drawtext(m, tags[j], dc.sel, isurgent(i, j)); - drawsquare(m, c && c->tags[j] && c->monitor == i, - isoccupied(i, j), isurgent(i, j), dc.sel); - } - else { - drawtext(m, tags[j], dc.norm, isurgent(i, j)); - drawsquare(m, c && c->tags[j] && c->monitor == i, - isoccupied(i, j), isurgent(i, j), dc.norm); - } - dc.x += dc.w; + dc.x = 0; + for(c = stack; c && !isvisible(c, m); c = c->snext); + for(j = 0; j < LENGTH(tags); j++) { + dc.w = textw(tags[j]); + if(m->seltags[j]) { + drawtext(m, tags[j], dc.sel, isurgent(m, j)); + drawsquare(m, c && c->tags[j] && c->monitor == m, + isoccupied(m, j), isurgent(m, j), dc.sel); } - dc.w = blw; - drawtext(m, m->layout->symbol, dc.norm, False); - x = dc.x + dc.w; - if(i == selmonitor) { - dc.w = textw(stext); - dc.x = m->sw - dc.w; - if(dc.x < x) { - dc.x = x; - dc.w = m->sw - x; - } - drawtext(m, stext, dc.norm, False); + else { + drawtext(m, tags[j], dc.norm, isurgent(m, j)); + drawsquare(m, c && c->tags[j] && c->monitor == m, + isoccupied(m, j), isurgent(m, j), dc.norm); } - else - dc.x = m->sw; - if((dc.w = dc.x - x) > bh) { + dc.x += dc.w; + } + dc.w = blw; + drawtext(m, m->layout->symbol, dc.norm, False); + x = dc.x + dc.w; + if(m == selmonitor) { + dc.w = textw(stext); + dc.x = m->sw - dc.w; + if(dc.x < x) { dc.x = x; - if(c) { - drawtext(m, c->name, dc.sel, False); - drawsquare(m, False, c->isfloating, False, dc.sel); - } - else - drawtext(m, NULL, dc.norm, False); + dc.w = m->sw - x; } - XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->sw, bh, 0, 0); - XSync(dpy, False); + drawtext(m, stext, dc.norm, False); } + else + dc.x = m->sw; + if((dc.w = dc.x - x) > bh) { + dc.x = x; + if(c) { + drawtext(m, c->name, dc.sel, False); + drawsquare(m, False, c->isfloating, False, dc.sel); + } + else + drawtext(m, NULL, dc.norm, False); + } + XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->sw, bh, 0, 0); + XSync(dpy, False); } void @@ -696,7 +693,7 @@ enternotify(XEvent *e) { focus(c); else { selmonitor = monitorat(); - fprintf(stderr, "updating selmonitor %d\n", selmonitor); + fprintf(stderr, "updating selmonitor %d\n", selmonitor - monitors); focus(NULL); } } @@ -713,12 +710,11 @@ eprint(const char *errstr, ...) { void expose(XEvent *e) { + Monitor *m; XExposeEvent *ev = &e->xexpose; - if(ev->count == 0) { - if(ev->window == monitors[selmonitor].barwin) - drawbar(); - } + if(ev->count == 0 && (m = getmonitor(ev->window))) + drawbar(m); } void @@ -727,17 +723,14 @@ floating(Monitor *m) { /* default floating layout */ domwfact = dozoom = False; for(c = clients; c; c = c->next) - if(isvisible(c, m->id)) + if(isvisible(c, m)) resize(c, c->x, c->y, c->w, c->h, True); } void focus(Client *c) { - Monitor *m; - if(c) selmonitor = c->monitor; - m = &monitors[selmonitor]; if(!c || (c && !isvisible(c, selmonitor))) for(c = stack; c && !isvisible(c, c->monitor); c = c->snext); if(sel && sel != c) { @@ -750,16 +743,14 @@ focus(Client *c) { grabbuttons(c, True); } sel = c; - drawbar(); if(c) { XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]); XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime); selmonitor = c->monitor; } - else { + else XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); - drawbar(); - } + drawbar(selmonitor); } void @@ -781,7 +772,7 @@ focusnext(const char *arg) { for(c = clients; c && !isvisible(c, selmonitor); c = c->next); if(c) { focus(c); - restack(); + restack(c->monitor); } } @@ -798,7 +789,7 @@ focusprev(const char *arg) { } if(c) { focus(c); - restack(); + restack(c->monitor); } } @@ -820,6 +811,16 @@ getcolor(const char *colstr) { return color.pixel; } +Monitor * +getmonitor(Window barwin) { + unsigned int i; + + for(i = 0; i < mcount; i++) + if(monitors[i].barwin == barwin) + return &monitors[i]; + return NULL; +} + long getstate(Window w) { int format, status; @@ -981,7 +982,7 @@ initfont(const char *fontstr) { } Bool -isoccupied(unsigned int monitor, unsigned int t) { +isoccupied(Monitor *monitor, unsigned int t) { Client *c; for(c = clients; c; c = c->next) @@ -1006,7 +1007,7 @@ isprotodel(Client *c) { } Bool -isurgent(unsigned int monitor, unsigned int t) { +isurgent(Monitor *monitor, unsigned int t) { Client *c; for(c = clients; c; c = c->next) @@ -1016,13 +1017,13 @@ isurgent(unsigned int monitor, unsigned int t) { } Bool -isvisible(Client *c, int monitor) { +isvisible(Client *c, Monitor *m) { unsigned int i; - if(c->monitor != monitor) + if(c->monitor != m) return False; for(i = 0; i < LENGTH(tags); i++) - if(c->tags[i] && monitors[c->monitor].seltags[i]) + if(c->tags[i] && c->monitor->seltags[i]) return True; return False; } @@ -1077,7 +1078,7 @@ manage(Window w, XWindowAttributes *wa) { applyrules(c); - m = &monitors[c->monitor]; + m = selmonitor; c->x = wa->x + m->sx; c->y = wa->y + m->sy; @@ -1146,7 +1147,7 @@ maprequest(XEvent *e) { manage(ev->window, &wa); } -int +Monitor * monitorat() { int i, x, y; Window win; @@ -1156,10 +1157,10 @@ monitorat() { for(i = 0; i < mcount; i++) { if((x >= monitors[i].sx && x < monitors[i].sx + monitors[i].sw) && (y >= monitors[i].sy && y < monitors[i].sy + monitors[i].sh)) { - return i; + return &monitors[i]; } } - return 0; + return NULL; } void @@ -1172,7 +1173,7 @@ movemouse(Client *c) { ocx = nx = c->x; ocy = ny = c->y; - m = &monitors[c->monitor]; + m = c->monitor; if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, None, cursor[CurMove], CurrentTime) != GrabSuccess) return; @@ -1210,7 +1211,7 @@ movemouse(Client *c) { } Client * -nexttiled(Client *c, int monitor) { +nexttiled(Client *c, Monitor *monitor) { for(; c && (c->isfloating || !isvisible(c, monitor)); c = c->next); return c; } @@ -1225,24 +1226,24 @@ propertynotify(XEvent *e) { return; /* ignore */ if((c = getclient(ev->window))) { switch (ev->atom) { - default: break; - case XA_WM_TRANSIENT_FOR: - XGetTransientForHint(dpy, c->win, &trans); - if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL))) - arrange(); - break; - case XA_WM_NORMAL_HINTS: - updatesizehints(c); - break; - case XA_WM_HINTS: - updatewmhints(c); - drawbar(); - break; + default: break; + case XA_WM_TRANSIENT_FOR: + XGetTransientForHint(dpy, c->win, &trans); + if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL))) + arrange(); + break; + case XA_WM_NORMAL_HINTS: + updatesizehints(c); + break; + case XA_WM_HINTS: + updatewmhints(c); + drawbar(c->monitor); + break; } if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) { updatetitle(c); if(c == sel) - drawbar(); + drawbar(c->monitor); } } } @@ -1269,7 +1270,7 @@ resize(Client *c, int x, int y, int w, int h, Bool sizehints) { Monitor *m; XWindowChanges wc; - m = &monitors[c->monitor]; + m = c->monitor; if(sizehints) { /* set minimum possible */ @@ -1340,7 +1341,7 @@ resizemouse(Client *c) { ocx = c->x; ocy = c->y; - m = &monitors[c->monitor]; + m = c->monitor; if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, None, cursor[CurResize], CurrentTime) != GrabSuccess) return; @@ -1375,31 +1376,28 @@ resizemouse(Client *c) { } void -restack(void) { - unsigned int i; +restack(Monitor *m) { Client *c; XEvent ev; XWindowChanges wc; - drawbar(); + drawbar(m); if(!sel) return; - if(sel->isfloating || (monitors[selmonitor].layout->arrange == floating)) + if(sel->isfloating || (m->layout->arrange == floating)) XRaiseWindow(dpy, sel->win); - if(monitors[selmonitor].layout->arrange != floating) { + if(m->layout->arrange != floating) { wc.stack_mode = Below; - wc.sibling = monitors[selmonitor].barwin; + wc.sibling = m->barwin; if(!sel->isfloating) { XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc); wc.sibling = sel->win; } - for(i = 0; i < mcount; i++) { - for(c = nexttiled(clients, i); c; c = nexttiled(c->next, i)) { - if(c == sel) - continue; - XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc); - wc.sibling = c->win; - } + for(c = nexttiled(clients, m); c; c = nexttiled(c->next, m)) { + if(c == sel) + continue; + XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc); + wc.sibling = c->win; } } XSync(dpy, False); @@ -1456,7 +1454,7 @@ run(void) { } break; } - drawbar(); + drawbar(selmonitor); } while(XPending(dpy)) { XNextEvent(dpy, &ev); @@ -1504,7 +1502,7 @@ setclientstate(Client *c, long state) { void setlayout(const char *arg) { unsigned int i; - Monitor *m = &monitors[monitorat()]; + Monitor *m = monitorat(); if(!arg) { m->layout++; @@ -1522,14 +1520,14 @@ setlayout(const char *arg) { if(sel) arrange(); else - drawbar(); + drawbar(m); } void setmwfact(const char *arg) { double delta; - Monitor *m = &monitors[monitorat()]; + Monitor *m = monitorat(); if(!domwfact) return; @@ -1573,7 +1571,7 @@ setup(void) { mcount = 1; if((isxinerama = XineramaIsActive(dpy))) info = XineramaQueryScreens(dpy, &mcount); - monitors = emallocz(mcount * sizeof(Monitor)); + selmonitor = monitors = emallocz(mcount * sizeof(Monitor)); screen = DefaultScreen(dpy); root = RootWindow(dpy, screen); @@ -1651,6 +1649,8 @@ setup(void) { | EnterWindowMask | LeaveWindowMask | StructureNotifyMask; XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); XSelectInput(dpy, root, wa.event_mask); + + drawbar(m); } if(info) XFree(info); @@ -1662,7 +1662,7 @@ setup(void) { compileregs(); selmonitor = monitorat(); - fprintf(stderr, "selmonitor == %d\n", selmonitor); + fprintf(stderr, "selmonitor == %d\n", selmonitor - monitors); } void @@ -1726,7 +1726,7 @@ tile(Monitor *m) { nx = ny = nw = 0; /* gcc stupidity requires this */ - for(n = 0, c = nexttiled(clients, m->id); c; c = nexttiled(c->next, m->id)) + for(n = 0, c = nexttiled(clients, m); c; c = nexttiled(c->next, m)) n++; /* window geoms */ @@ -1735,7 +1735,7 @@ tile(Monitor *m) { if(n > 1 && th < bh) th = m->wah; - for(i = 0, c = mc = nexttiled(clients, m->id); c; c = nexttiled(c->next, m->id)) { + for(i = 0, c = mc = nexttiled(clients, m); c; c = nexttiled(c->next, m)) { if(i == 0) { /* master */ nx = m->wax; ny = m->way; @@ -1769,7 +1769,7 @@ togglebar(const char *arg) { bpos = (BARPOS == BarOff) ? BarTop : BARPOS; else bpos = BarOff; - updatebarpos(&monitors[monitorat()]); + updatebarpos(monitorat()); arrange(); } @@ -1801,7 +1801,7 @@ void toggleview(const char *arg) { unsigned int i, j; - Monitor *m = &monitors[monitorat()]; + Monitor *m = monitorat(); i = idxoftag(arg); m->seltags[i] = !m->seltags[i]; @@ -1981,7 +1981,7 @@ void view(const char *arg) { unsigned int i; Bool tmp[LENGTH(tags)]; - Monitor *m = &monitors[monitorat()]; + Monitor *m = monitorat(); for(i = 0; i < LENGTH(tags); i++) tmp[i] = (NULL == arg); @@ -1997,7 +1997,7 @@ void viewprevtag(const char *arg) { static Bool tmp[LENGTH(tags)]; - Monitor *m = &monitors[monitorat()]; + Monitor *m = monitorat(); memcpy(tmp, m->seltags, sizeof initags); memcpy(m->seltags, m->prevtags, sizeof initags); @@ -2022,19 +2022,35 @@ zoom(const char *arg) { void movetomonitor(const char *arg) { - if (sel) { - sel->monitor = arg ? atoi(arg) : (sel->monitor+1) % mcount; + int i; - memcpy(sel->tags, monitors[sel->monitor].seltags, sizeof initags); - resize(sel, monitors[sel->monitor].wax, monitors[sel->monitor].way, sel->w, sel->h, True); - arrange(); + if (sel) + return; + if(arg) + i = atoi(arg); + else { + for(i = 0; &monitors[i] != sel->monitor && i < mcount; i++); + i++; } + sel->monitor = &monitors[i % mcount]; + + memcpy(sel->tags, sel->monitor->seltags, sizeof initags); + resize(sel, sel->monitor->wax, sel->monitor->way, sel->w, sel->h, True); + arrange(); } void selectmonitor(const char *arg) { - Monitor *m = &monitors[arg ? atoi(arg) : (monitorat()+1) % mcount]; + int i; + Monitor *m; + if(arg) + i = atoi(arg); + else { + for(i = 0; &monitors[i] != sel->monitor && i < mcount; i++); + i++; + } + m = &monitors[i % mcount]; XWarpPointer(dpy, None, root, 0, 0, 0, 0, m->wax+m->waw/2, m->way+m->wah/2); focus(NULL); } @@ -2054,7 +2070,6 @@ main(int argc, char *argv[]) { checkotherwm(); setup(); - drawbar(); scan(); run(); cleanup();