added screen.c, removed layout.c and tag.c
47765f72
8 file(s) · +407 −420
| 3 | 3 | ||
| 4 | 4 | include config.mk |
|
| 5 | 5 | ||
| 6 | - | SRC += client.c draw.c event.c layout.c main.c tag.c util.c |
|
| 6 | + | SRC += client.c draw.c event.c main.c screen.c util.c |
|
| 7 | 7 | OBJ = ${SRC:.c=.o} |
|
| 8 | 8 | ||
| 9 | 9 | all: options dwm |
| 182 | 182 | } |
|
| 183 | 183 | ||
| 184 | 184 | Bool |
|
| 185 | - | loadprops(Client *c) { |
|
| 185 | + | getprops(Client *c) { |
|
| 186 | 186 | unsigned int i; |
|
| 187 | 187 | Bool result = False; |
|
| 188 | 188 | ||
| 242 | 242 | if(t) |
|
| 243 | 243 | for(i = 0; i < ntags; i++) |
|
| 244 | 244 | c->tags[i] = t->tags[i]; |
|
| 245 | - | if(!loadprops(c)) |
|
| 245 | + | if(!getprops(c)) |
|
| 246 | 246 | applyrules(c); |
|
| 247 | 247 | if(!c->isfloating) |
|
| 248 | 248 | c->isfloating = (rettrans == Success) || c->isfixed; |
|
| 249 | - | saveprops(c); |
|
| 249 | + | setprops(c); |
|
| 250 | 250 | attach(c); |
|
| 251 | 251 | attachstack(c); |
|
| 252 | 252 | XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */ |
|
| 318 | 318 | } |
|
| 319 | 319 | ||
| 320 | 320 | void |
|
| 321 | - | saveprops(Client *c) { |
|
| 321 | + | setprops(Client *c) { |
|
| 322 | 322 | unsigned int i; |
|
| 323 | 323 | ||
| 324 | 324 | for(i = 0; i < ntags && i < sizeof prop - 1; i++) |
|
| 20 | 20 | # flags |
|
| 21 | 21 | CFLAGS = -Os ${INCS} -DVERSION=\"${VERSION}\" |
|
| 22 | 22 | LDFLAGS = -s ${LIBS} |
|
| 23 | - | CFLAGS = -g -Wall -O2 ${INCS} -DVERSION=\"${VERSION}\" |
|
| 24 | - | LDFLAGS = -g ${LIBS} |
|
| 23 | + | #CFLAGS = -g -Wall -O2 ${INCS} -DVERSION=\"${VERSION}\" |
|
| 24 | + | #LDFLAGS = -g ${LIBS} |
|
| 25 | 25 | ||
| 26 | 26 | # Solaris |
|
| 27 | 27 | #CFLAGS = -fast ${INCS} -DVERSION=\"${VERSION}\" |
| 96 | 96 | void detach(Client *c); /* detaches c from global client list */ |
|
| 97 | 97 | void focus(Client *c); /* focus c if visible && !NULL, or focus top visible */ |
|
| 98 | 98 | void killclient(const char *arg); /* kill sel nicely */ |
|
| 99 | - | Bool loadprops(Client *c); /* loads client properties */ |
|
| 99 | + | Bool getprops(Client *c); /* gets client properties */ |
|
| 100 | 100 | void manage(Window w, XWindowAttributes *wa); /* manage new client */ |
|
| 101 | 101 | void resize(Client *c, int x, int y, |
|
| 102 | 102 | int w, int h, Bool sizehints); /* resize with given coordinates c*/ |
|
| 103 | - | void saveprops(Client *c); /* saves client properties */ |
|
| 103 | + | void setprops(Client *c); /* sets client properties */ |
|
| 104 | 104 | void unban(Client *c); /* unbans c */ |
|
| 105 | 105 | void unmanage(Client *c, long state); /* unmanage c */ |
|
| 106 | 106 | void updatesizehints(Client *c); /* update the size hint variables of c */ |
|
| 114 | 114 | /* event.c */ |
|
| 115 | 115 | void grabkeys(void); /* grab all keys defined in config.h */ |
|
| 116 | 116 | ||
| 117 | - | /* layout.c */ |
|
| 118 | - | void arrange(void); /* arranges all windows depending on the layout in use */ |
|
| 119 | - | void focusnext(const char *arg); /* focuses next visible client */ |
|
| 120 | - | void focusprev(const char *arg); /* focuses prev visible client */ |
|
| 121 | - | const char *getsymbol(void); /* returns symbol of enabled layout */ |
|
| 122 | - | Bool isfloating(void); /* returns True if floating layout is enabled */ |
|
| 123 | - | Bool isarrange(void (*func)()); /* returns True if func is the layout function in use */ |
|
| 124 | - | void initlayouts(void); /* initialize layout array */ |
|
| 125 | - | void loaddwmprops(void); /* loads dwm properties */ |
|
| 126 | - | Client *nexttiled(Client *c); /* returns tiled successor of c */ |
|
| 127 | - | void restack(void); /* restores z layers of all clients */ |
|
| 128 | - | void savedwmprops(void); /* saves dwm properties */ |
|
| 129 | - | void setlayout(const char *arg); /* sets layout, NULL means next layout */ |
|
| 130 | - | void togglebar(const char *arg); /* shows/hides the bar */ |
|
| 131 | - | void togglemax(const char *arg); /* toggles maximization of floating client */ |
|
| 132 | - | ||
| 133 | 117 | /* main.c */ |
|
| 134 | 118 | Bool gettextprop(Window w, Atom atom, |
|
| 135 | 119 | char *text, unsigned int size); /* return text property, UTF-8 compliant */ |
|
| 137 | 121 | void quit(const char *arg); /* quit dwm nicely */ |
|
| 138 | 122 | int xerror(Display *dsply, XErrorEvent *ee); /* dwm's X error handler */ |
|
| 139 | 123 | ||
| 140 | - | /* tag.c */ |
|
| 124 | + | /* screen.c */ |
|
| 141 | 125 | void applyrules(Client *c); /* applies rules to c */ |
|
| 126 | + | void arrange(void); /* arranges all windows depending on the layout in use */ |
|
| 142 | 127 | void compileregs(void); /* initialize regexps of rules defined in config.h */ |
|
| 128 | + | void focusnext(const char *arg); /* focuses next visible client */ |
|
| 129 | + | void focusprev(const char *arg); /* focuses prev visible client */ |
|
| 130 | + | const char *getsymbol(void); /* returns symbol of enabled layout */ |
|
| 131 | + | void initlayouts(void); /* initialize layout array */ |
|
| 132 | + | Bool isarrange(void (*func)()); /* returns True if func is the layout function in use */ |
|
| 133 | + | Bool isfloating(void); /* returns True if floating layout is enabled */ |
|
| 143 | 134 | Bool isvisible(Client *c); /* returns True if client is visible */ |
|
| 135 | + | void getdwmprops(void); /* gets dwm properties */ |
|
| 136 | + | Client *nexttiled(Client *c); /* returns tiled successor of c */ |
|
| 137 | + | void restack(void); /* restores z layers of all clients */ |
|
| 138 | + | void setlayout(const char *arg); /* sets layout, NULL means next layout */ |
|
| 144 | 139 | void tag(const char *arg); /* tags sel with arg's index */ |
|
| 140 | + | void togglebar(const char *arg); /* shows/hides the bar */ |
|
| 145 | 141 | void togglefloating(const char *arg); /* toggles sel between floating/tiled state */ |
|
| 142 | + | void togglemax(const char *arg); /* toggles maximization of floating client */ |
|
| 146 | 143 | void toggletag(const char *arg); /* toggles sel tags with arg's index */ |
|
| 147 | 144 | void toggleview(const char *arg); /* toggles the tag with arg's index (in)visible */ |
|
| 148 | 145 | void view(const char *arg); /* views the tag with arg's index */ |
|
| 1 | - | /* See LICENSE file for copyright and license details. */ |
|
| 2 | - | #include "dwm.h" |
|
| 3 | - | #include <stdlib.h> |
|
| 4 | - | #include <string.h> |
|
| 5 | - | #include <X11/Xatom.h> |
|
| 6 | - | #include <X11/Xutil.h> |
|
| 7 | - | ||
| 8 | - | /* static */ |
|
| 9 | - | ||
| 10 | - | typedef struct { |
|
| 11 | - | const char *symbol; |
|
| 12 | - | void (*arrange)(void); |
|
| 13 | - | } Layout; |
|
| 14 | - | ||
| 15 | - | unsigned int blw = 0; |
|
| 16 | - | static char prop[128]; |
|
| 17 | - | static unsigned int ltidx = 0; /* default */ |
|
| 18 | - | ||
| 19 | - | static void |
|
| 20 | - | floating(void) { /* default floating layout */ |
|
| 21 | - | Client *c; |
|
| 22 | - | ||
| 23 | - | for(c = clients; c; c = c->next) |
|
| 24 | - | if(isvisible(c)) |
|
| 25 | - | resize(c, c->x, c->y, c->w, c->h, True); |
|
| 26 | - | } |
|
| 27 | - | ||
| 28 | - | static unsigned int nlayouts = 0; |
|
| 29 | - | ||
| 30 | - | LAYOUTS |
|
| 31 | - | ||
| 32 | - | /* extern */ |
|
| 33 | - | ||
| 34 | - | void |
|
| 35 | - | arrange(void) { |
|
| 36 | - | Client *c; |
|
| 37 | - | ||
| 38 | - | for(c = clients; c; c = c->next) |
|
| 39 | - | if(isvisible(c)) |
|
| 40 | - | unban(c); |
|
| 41 | - | else |
|
| 42 | - | ban(c); |
|
| 43 | - | layouts[ltidx].arrange(); |
|
| 44 | - | focus(NULL); |
|
| 45 | - | restack(); |
|
| 46 | - | } |
|
| 47 | - | ||
| 48 | - | void |
|
| 49 | - | focusnext(const char *arg) { |
|
| 50 | - | Client *c; |
|
| 51 | - | ||
| 52 | - | if(!sel) |
|
| 53 | - | return; |
|
| 54 | - | for(c = sel->next; c && !isvisible(c); c = c->next); |
|
| 55 | - | if(!c) |
|
| 56 | - | for(c = clients; c && !isvisible(c); c = c->next); |
|
| 57 | - | if(c) { |
|
| 58 | - | focus(c); |
|
| 59 | - | restack(); |
|
| 60 | - | } |
|
| 61 | - | } |
|
| 62 | - | ||
| 63 | - | void |
|
| 64 | - | focusprev(const char *arg) { |
|
| 65 | - | Client *c; |
|
| 66 | - | ||
| 67 | - | if(!sel) |
|
| 68 | - | return; |
|
| 69 | - | for(c = sel->prev; c && !isvisible(c); c = c->prev); |
|
| 70 | - | if(!c) { |
|
| 71 | - | for(c = clients; c && c->next; c = c->next); |
|
| 72 | - | for(; c && !isvisible(c); c = c->prev); |
|
| 73 | - | } |
|
| 74 | - | if(c) { |
|
| 75 | - | focus(c); |
|
| 76 | - | restack(); |
|
| 77 | - | } |
|
| 78 | - | } |
|
| 79 | - | ||
| 80 | - | const char * |
|
| 81 | - | getsymbol(void) |
|
| 82 | - | { |
|
| 83 | - | return layouts[ltidx].symbol; |
|
| 84 | - | } |
|
| 85 | - | ||
| 86 | - | Bool |
|
| 87 | - | isfloating(void) { |
|
| 88 | - | return layouts[ltidx].arrange == floating; |
|
| 89 | - | } |
|
| 90 | - | ||
| 91 | - | Bool |
|
| 92 | - | isarrange(void (*func)()) |
|
| 93 | - | { |
|
| 94 | - | return func == layouts[ltidx].arrange; |
|
| 95 | - | } |
|
| 96 | - | ||
| 97 | - | void |
|
| 98 | - | initlayouts(void) { |
|
| 99 | - | unsigned int i, w; |
|
| 100 | - | ||
| 101 | - | nlayouts = sizeof layouts / sizeof layouts[0]; |
|
| 102 | - | for(blw = i = 0; i < nlayouts; i++) { |
|
| 103 | - | w = textw(layouts[i].symbol); |
|
| 104 | - | if(w > blw) |
|
| 105 | - | blw = w; |
|
| 106 | - | } |
|
| 107 | - | } |
|
| 108 | - | ||
| 109 | - | void |
|
| 110 | - | loaddwmprops(void) { |
|
| 111 | - | unsigned int i; |
|
| 112 | - | ||
| 113 | - | if(gettextprop(root, dwmprops, prop, sizeof prop)) { |
|
| 114 | - | for(i = 0; i < ntags && i < sizeof prop - 1 && prop[i] != '\0'; i++) |
|
| 115 | - | seltags[i] = prop[i] == '1'; |
|
| 116 | - | if(i < sizeof prop - 1 && prop[i] != '\0') { |
|
| 117 | - | if(prop[i] < nlayouts) |
|
| 118 | - | ltidx = prop[i]; |
|
| 119 | - | } |
|
| 120 | - | } |
|
| 121 | - | } |
|
| 122 | - | ||
| 123 | - | Client * |
|
| 124 | - | nexttiled(Client *c) { |
|
| 125 | - | for(; c && (c->isfloating || !isvisible(c)); c = c->next); |
|
| 126 | - | return c; |
|
| 127 | - | } |
|
| 128 | - | ||
| 129 | - | void |
|
| 130 | - | restack(void) { |
|
| 131 | - | Client *c; |
|
| 132 | - | XEvent ev; |
|
| 133 | - | XWindowChanges wc; |
|
| 134 | - | ||
| 135 | - | drawstatus(); |
|
| 136 | - | if(!sel) |
|
| 137 | - | return; |
|
| 138 | - | if(sel->isfloating || isfloating()) |
|
| 139 | - | XRaiseWindow(dpy, sel->win); |
|
| 140 | - | if(!isfloating()) { |
|
| 141 | - | wc.stack_mode = Below; |
|
| 142 | - | wc.sibling = barwin; |
|
| 143 | - | if(!sel->isfloating) { |
|
| 144 | - | XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc); |
|
| 145 | - | wc.sibling = sel->win; |
|
| 146 | - | } |
|
| 147 | - | for(c = nexttiled(clients); c; c = nexttiled(c->next)) { |
|
| 148 | - | if(c == sel) |
|
| 149 | - | continue; |
|
| 150 | - | XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc); |
|
| 151 | - | wc.sibling = c->win; |
|
| 152 | - | } |
|
| 153 | - | } |
|
| 154 | - | XSync(dpy, False); |
|
| 155 | - | while(XCheckMaskEvent(dpy, EnterWindowMask, &ev)); |
|
| 156 | - | } |
|
| 157 | - | ||
| 158 | - | void |
|
| 159 | - | savedwmprops(void) { |
|
| 160 | - | unsigned int i; |
|
| 161 | - | ||
| 162 | - | for(i = 0; i < ntags && i < sizeof prop - 1; i++) |
|
| 163 | - | prop[i] = seltags[i] ? '1' : '0'; |
|
| 164 | - | if(i < sizeof prop - 1) |
|
| 165 | - | prop[i++] = (char)ltidx; |
|
| 166 | - | prop[i] = '\0'; |
|
| 167 | - | XChangeProperty(dpy, root, dwmprops, XA_STRING, 8, |
|
| 168 | - | PropModeReplace, (unsigned char *)prop, i); |
|
| 169 | - | } |
|
| 170 | - | ||
| 171 | - | void |
|
| 172 | - | setlayout(const char *arg) { |
|
| 173 | - | int i; |
|
| 174 | - | ||
| 175 | - | if(!arg) { |
|
| 176 | - | if(++ltidx == nlayouts) |
|
| 177 | - | ltidx = 0;; |
|
| 178 | - | } |
|
| 179 | - | else { |
|
| 180 | - | i = atoi(arg); |
|
| 181 | - | if(i < 0 || i >= nlayouts) |
|
| 182 | - | return; |
|
| 183 | - | ltidx = i; |
|
| 184 | - | } |
|
| 185 | - | if(sel) |
|
| 186 | - | arrange(); |
|
| 187 | - | else |
|
| 188 | - | drawstatus(); |
|
| 189 | - | savedwmprops(); |
|
| 190 | - | } |
|
| 191 | - | ||
| 192 | - | void |
|
| 193 | - | togglebar(const char *arg) { |
|
| 194 | - | if(bpos == BarOff) |
|
| 195 | - | bpos = (BARPOS == BarOff) ? BarTop : BARPOS; |
|
| 196 | - | else |
|
| 197 | - | bpos = BarOff; |
|
| 198 | - | updatebarpos(); |
|
| 199 | - | arrange(); |
|
| 200 | - | } |
|
| 201 | - | ||
| 202 | - | void |
|
| 203 | - | togglemax(const char *arg) { |
|
| 204 | - | XEvent ev; |
|
| 205 | - | ||
| 206 | - | if(!sel || (!isfloating() && !sel->isfloating) || sel->isfixed) |
|
| 207 | - | return; |
|
| 208 | - | if((sel->ismax = !sel->ismax)) { |
|
| 209 | - | sel->rx = sel->x; |
|
| 210 | - | sel->ry = sel->y; |
|
| 211 | - | sel->rw = sel->w; |
|
| 212 | - | sel->rh = sel->h; |
|
| 213 | - | resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True); |
|
| 214 | - | } |
|
| 215 | - | else |
|
| 216 | - | resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True); |
|
| 217 | - | drawstatus(); |
|
| 218 | - | while(XCheckMaskEvent(dpy, EnterWindowMask, &ev)); |
|
| 219 | - | } |
| 206 | 206 | XSetFont(dpy, dc.gc, dc.font.xfont->fid); |
|
| 207 | 207 | /* multihead support */ |
|
| 208 | 208 | selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); |
|
| 209 | - | loaddwmprops(); |
|
| 209 | + | getdwmprops(); |
|
| 210 | 210 | } |
|
| 211 | 211 | ||
| 212 | 212 | /* |
| 1 | + | /* See LICENSE file for copyright and license details. */ |
|
| 2 | + | #include "dwm.h" |
|
| 3 | + | #include <regex.h> |
|
| 4 | + | #include <stdio.h> |
|
| 5 | + | #include <stdlib.h> |
|
| 6 | + | #include <string.h> |
|
| 7 | + | #include <X11/Xatom.h> |
|
| 8 | + | #include <X11/Xutil.h> |
|
| 9 | + | ||
| 10 | + | /* static */ |
|
| 11 | + | ||
| 12 | + | typedef struct { |
|
| 13 | + | const char *symbol; |
|
| 14 | + | void (*arrange)(void); |
|
| 15 | + | } Layout; |
|
| 16 | + | ||
| 17 | + | typedef struct { |
|
| 18 | + | const char *prop; |
|
| 19 | + | const char *tags; |
|
| 20 | + | Bool isfloating; |
|
| 21 | + | } Rule; |
|
| 22 | + | ||
| 23 | + | typedef struct { |
|
| 24 | + | regex_t *propregex; |
|
| 25 | + | regex_t *tagregex; |
|
| 26 | + | } Regs; |
|
| 27 | + | ||
| 28 | + | TAGS |
|
| 29 | + | RULES |
|
| 30 | + | ||
| 31 | + | static char prop[512]; |
|
| 32 | + | static unsigned int nrules = 0; |
|
| 33 | + | static unsigned int nlayouts = 0; |
|
| 34 | + | static unsigned int ltidx = 0; /* default */ |
|
| 35 | + | static Regs *regs = NULL; |
|
| 36 | + | ||
| 37 | + | static unsigned int |
|
| 38 | + | idxoftag(const char *tag) { |
|
| 39 | + | unsigned int i; |
|
| 40 | + | ||
| 41 | + | for(i = 0; i < ntags; i++) |
|
| 42 | + | if(tags[i] == tag) |
|
| 43 | + | return i; |
|
| 44 | + | return 0; |
|
| 45 | + | } |
|
| 46 | + | ||
| 47 | + | static void |
|
| 48 | + | floating(void) { /* default floating layout */ |
|
| 49 | + | Client *c; |
|
| 50 | + | ||
| 51 | + | for(c = clients; c; c = c->next) |
|
| 52 | + | if(isvisible(c)) |
|
| 53 | + | resize(c, c->x, c->y, c->w, c->h, True); |
|
| 54 | + | } |
|
| 55 | + | ||
| 56 | + | static void |
|
| 57 | + | setdwmprops(void) { |
|
| 58 | + | unsigned int i; |
|
| 59 | + | ||
| 60 | + | for(i = 0; i < ntags && i < sizeof prop - 1; i++) |
|
| 61 | + | prop[i] = seltags[i] ? '1' : '0'; |
|
| 62 | + | if(i < sizeof prop - 1) |
|
| 63 | + | prop[i++] = (char)ltidx; |
|
| 64 | + | prop[i] = '\0'; |
|
| 65 | + | XChangeProperty(dpy, root, dwmprops, XA_STRING, 8, |
|
| 66 | + | PropModeReplace, (unsigned char *)prop, i); |
|
| 67 | + | } |
|
| 68 | + | ||
| 69 | + | LAYOUTS |
|
| 70 | + | ||
| 71 | + | /* extern */ |
|
| 72 | + | ||
| 73 | + | unsigned int blw = 0; |
|
| 74 | + | ||
| 75 | + | void |
|
| 76 | + | applyrules(Client *c) { |
|
| 77 | + | unsigned int i, j; |
|
| 78 | + | regmatch_t tmp; |
|
| 79 | + | Bool matched = False; |
|
| 80 | + | XClassHint ch = { 0 }; |
|
| 81 | + | ||
| 82 | + | /* rule matching */ |
|
| 83 | + | XGetClassHint(dpy, c->win, &ch); |
|
| 84 | + | snprintf(prop, sizeof prop, "%s:%s:%s", |
|
| 85 | + | ch.res_class ? ch.res_class : "", |
|
| 86 | + | ch.res_name ? ch.res_name : "", c->name); |
|
| 87 | + | for(i = 0; i < nrules; i++) |
|
| 88 | + | if(regs[i].propregex && !regexec(regs[i].propregex, prop, 1, &tmp, 0)) { |
|
| 89 | + | c->isfloating = rules[i].isfloating; |
|
| 90 | + | for(j = 0; regs[i].tagregex && j < ntags; j++) { |
|
| 91 | + | if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) { |
|
| 92 | + | matched = True; |
|
| 93 | + | c->tags[j] = True; |
|
| 94 | + | } |
|
| 95 | + | } |
|
| 96 | + | } |
|
| 97 | + | if(ch.res_class) |
|
| 98 | + | XFree(ch.res_class); |
|
| 99 | + | if(ch.res_name) |
|
| 100 | + | XFree(ch.res_name); |
|
| 101 | + | if(!matched) |
|
| 102 | + | for(i = 0; i < ntags; i++) |
|
| 103 | + | c->tags[i] = seltags[i]; |
|
| 104 | + | } |
|
| 105 | + | ||
| 106 | + | void |
|
| 107 | + | arrange(void) { |
|
| 108 | + | Client *c; |
|
| 109 | + | ||
| 110 | + | for(c = clients; c; c = c->next) |
|
| 111 | + | if(isvisible(c)) |
|
| 112 | + | unban(c); |
|
| 113 | + | else |
|
| 114 | + | ban(c); |
|
| 115 | + | layouts[ltidx].arrange(); |
|
| 116 | + | focus(NULL); |
|
| 117 | + | restack(); |
|
| 118 | + | } |
|
| 119 | + | ||
| 120 | + | void |
|
| 121 | + | compileregs(void) { |
|
| 122 | + | unsigned int i; |
|
| 123 | + | regex_t *reg; |
|
| 124 | + | ||
| 125 | + | if(regs) |
|
| 126 | + | return; |
|
| 127 | + | nrules = sizeof rules / sizeof rules[0]; |
|
| 128 | + | regs = emallocz(nrules * sizeof(Regs)); |
|
| 129 | + | for(i = 0; i < nrules; i++) { |
|
| 130 | + | if(rules[i].prop) { |
|
| 131 | + | reg = emallocz(sizeof(regex_t)); |
|
| 132 | + | if(regcomp(reg, rules[i].prop, REG_EXTENDED)) |
|
| 133 | + | free(reg); |
|
| 134 | + | else |
|
| 135 | + | regs[i].propregex = reg; |
|
| 136 | + | } |
|
| 137 | + | if(rules[i].tags) { |
|
| 138 | + | reg = emallocz(sizeof(regex_t)); |
|
| 139 | + | if(regcomp(reg, rules[i].tags, REG_EXTENDED)) |
|
| 140 | + | free(reg); |
|
| 141 | + | else |
|
| 142 | + | regs[i].tagregex = reg; |
|
| 143 | + | } |
|
| 144 | + | } |
|
| 145 | + | } |
|
| 146 | + | ||
| 147 | + | void |
|
| 148 | + | focusnext(const char *arg) { |
|
| 149 | + | Client *c; |
|
| 150 | + | ||
| 151 | + | if(!sel) |
|
| 152 | + | return; |
|
| 153 | + | for(c = sel->next; c && !isvisible(c); c = c->next); |
|
| 154 | + | if(!c) |
|
| 155 | + | for(c = clients; c && !isvisible(c); c = c->next); |
|
| 156 | + | if(c) { |
|
| 157 | + | focus(c); |
|
| 158 | + | restack(); |
|
| 159 | + | } |
|
| 160 | + | } |
|
| 161 | + | ||
| 162 | + | void |
|
| 163 | + | focusprev(const char *arg) { |
|
| 164 | + | Client *c; |
|
| 165 | + | ||
| 166 | + | if(!sel) |
|
| 167 | + | return; |
|
| 168 | + | for(c = sel->prev; c && !isvisible(c); c = c->prev); |
|
| 169 | + | if(!c) { |
|
| 170 | + | for(c = clients; c && c->next; c = c->next); |
|
| 171 | + | for(; c && !isvisible(c); c = c->prev); |
|
| 172 | + | } |
|
| 173 | + | if(c) { |
|
| 174 | + | focus(c); |
|
| 175 | + | restack(); |
|
| 176 | + | } |
|
| 177 | + | } |
|
| 178 | + | ||
| 179 | + | const char * |
|
| 180 | + | getsymbol(void) |
|
| 181 | + | { |
|
| 182 | + | return layouts[ltidx].symbol; |
|
| 183 | + | } |
|
| 184 | + | ||
| 185 | + | void |
|
| 186 | + | initlayouts(void) { |
|
| 187 | + | unsigned int i, w; |
|
| 188 | + | ||
| 189 | + | nlayouts = sizeof layouts / sizeof layouts[0]; |
|
| 190 | + | for(blw = i = 0; i < nlayouts; i++) { |
|
| 191 | + | w = textw(layouts[i].symbol); |
|
| 192 | + | if(w > blw) |
|
| 193 | + | blw = w; |
|
| 194 | + | } |
|
| 195 | + | } |
|
| 196 | + | ||
| 197 | + | Bool |
|
| 198 | + | isfloating(void) { |
|
| 199 | + | return layouts[ltidx].arrange == floating; |
|
| 200 | + | } |
|
| 201 | + | ||
| 202 | + | Bool |
|
| 203 | + | isarrange(void (*func)()) |
|
| 204 | + | { |
|
| 205 | + | return func == layouts[ltidx].arrange; |
|
| 206 | + | } |
|
| 207 | + | ||
| 208 | + | Bool |
|
| 209 | + | isvisible(Client *c) { |
|
| 210 | + | unsigned int i; |
|
| 211 | + | ||
| 212 | + | for(i = 0; i < ntags; i++) |
|
| 213 | + | if(c->tags[i] && seltags[i]) |
|
| 214 | + | return True; |
|
| 215 | + | return False; |
|
| 216 | + | } |
|
| 217 | + | ||
| 218 | + | void |
|
| 219 | + | getdwmprops(void) { |
|
| 220 | + | unsigned int i; |
|
| 221 | + | ||
| 222 | + | if(gettextprop(root, dwmprops, prop, sizeof prop)) { |
|
| 223 | + | for(i = 0; i < ntags && i < sizeof prop - 1 && prop[i] != '\0'; i++) |
|
| 224 | + | seltags[i] = prop[i] == '1'; |
|
| 225 | + | if(i < sizeof prop - 1 && prop[i] != '\0') { |
|
| 226 | + | if(prop[i] < nlayouts) |
|
| 227 | + | ltidx = prop[i]; |
|
| 228 | + | } |
|
| 229 | + | } |
|
| 230 | + | } |
|
| 231 | + | ||
| 232 | + | Client * |
|
| 233 | + | nexttiled(Client *c) { |
|
| 234 | + | for(; c && (c->isfloating || !isvisible(c)); c = c->next); |
|
| 235 | + | return c; |
|
| 236 | + | } |
|
| 237 | + | ||
| 238 | + | void |
|
| 239 | + | restack(void) { |
|
| 240 | + | Client *c; |
|
| 241 | + | XEvent ev; |
|
| 242 | + | XWindowChanges wc; |
|
| 243 | + | ||
| 244 | + | drawstatus(); |
|
| 245 | + | if(!sel) |
|
| 246 | + | return; |
|
| 247 | + | if(sel->isfloating || isfloating()) |
|
| 248 | + | XRaiseWindow(dpy, sel->win); |
|
| 249 | + | if(!isfloating()) { |
|
| 250 | + | wc.stack_mode = Below; |
|
| 251 | + | wc.sibling = barwin; |
|
| 252 | + | if(!sel->isfloating) { |
|
| 253 | + | XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc); |
|
| 254 | + | wc.sibling = sel->win; |
|
| 255 | + | } |
|
| 256 | + | for(c = nexttiled(clients); c; c = nexttiled(c->next)) { |
|
| 257 | + | if(c == sel) |
|
| 258 | + | continue; |
|
| 259 | + | XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc); |
|
| 260 | + | wc.sibling = c->win; |
|
| 261 | + | } |
|
| 262 | + | } |
|
| 263 | + | XSync(dpy, False); |
|
| 264 | + | while(XCheckMaskEvent(dpy, EnterWindowMask, &ev)); |
|
| 265 | + | } |
|
| 266 | + | ||
| 267 | + | void |
|
| 268 | + | setlayout(const char *arg) { |
|
| 269 | + | int i; |
|
| 270 | + | ||
| 271 | + | if(!arg) { |
|
| 272 | + | if(++ltidx == nlayouts) |
|
| 273 | + | ltidx = 0;; |
|
| 274 | + | } |
|
| 275 | + | else { |
|
| 276 | + | i = atoi(arg); |
|
| 277 | + | if(i < 0 || i >= nlayouts) |
|
| 278 | + | return; |
|
| 279 | + | ltidx = i; |
|
| 280 | + | } |
|
| 281 | + | if(sel) |
|
| 282 | + | arrange(); |
|
| 283 | + | else |
|
| 284 | + | drawstatus(); |
|
| 285 | + | setdwmprops(); |
|
| 286 | + | } |
|
| 287 | + | ||
| 288 | + | void |
|
| 289 | + | tag(const char *arg) { |
|
| 290 | + | unsigned int i; |
|
| 291 | + | ||
| 292 | + | if(!sel) |
|
| 293 | + | return; |
|
| 294 | + | for(i = 0; i < ntags; i++) |
|
| 295 | + | sel->tags[i] = arg == NULL; |
|
| 296 | + | i = idxoftag(arg); |
|
| 297 | + | if(i >= 0 && i < ntags) |
|
| 298 | + | sel->tags[i] = True; |
|
| 299 | + | setprops(sel); |
|
| 300 | + | arrange(); |
|
| 301 | + | } |
|
| 302 | + | ||
| 303 | + | void |
|
| 304 | + | togglebar(const char *arg) { |
|
| 305 | + | if(bpos == BarOff) |
|
| 306 | + | bpos = (BARPOS == BarOff) ? BarTop : BARPOS; |
|
| 307 | + | else |
|
| 308 | + | bpos = BarOff; |
|
| 309 | + | updatebarpos(); |
|
| 310 | + | arrange(); |
|
| 311 | + | } |
|
| 312 | + | ||
| 313 | + | void |
|
| 314 | + | togglefloating(const char *arg) { |
|
| 315 | + | if(!sel || isfloating()) |
|
| 316 | + | return; |
|
| 317 | + | sel->isfloating = !sel->isfloating; |
|
| 318 | + | if(sel->isfloating) { |
|
| 319 | + | resize(sel, sel->x, sel->y, sel->w, sel->h, True); |
|
| 320 | + | setprops(sel); |
|
| 321 | + | } |
|
| 322 | + | arrange(); |
|
| 323 | + | } |
|
| 324 | + | ||
| 325 | + | void |
|
| 326 | + | togglemax(const char *arg) { |
|
| 327 | + | XEvent ev; |
|
| 328 | + | ||
| 329 | + | if(!sel || (!isfloating() && !sel->isfloating) || sel->isfixed) |
|
| 330 | + | return; |
|
| 331 | + | if((sel->ismax = !sel->ismax)) { |
|
| 332 | + | sel->rx = sel->x; |
|
| 333 | + | sel->ry = sel->y; |
|
| 334 | + | sel->rw = sel->w; |
|
| 335 | + | sel->rh = sel->h; |
|
| 336 | + | resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True); |
|
| 337 | + | } |
|
| 338 | + | else |
|
| 339 | + | resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True); |
|
| 340 | + | drawstatus(); |
|
| 341 | + | while(XCheckMaskEvent(dpy, EnterWindowMask, &ev)); |
|
| 342 | + | } |
|
| 343 | + | ||
| 344 | + | void |
|
| 345 | + | toggletag(const char *arg) { |
|
| 346 | + | unsigned int i, j; |
|
| 347 | + | ||
| 348 | + | if(!sel) |
|
| 349 | + | return; |
|
| 350 | + | i = idxoftag(arg); |
|
| 351 | + | sel->tags[i] = !sel->tags[i]; |
|
| 352 | + | for(j = 0; j < ntags && !sel->tags[j]; j++); |
|
| 353 | + | if(j == ntags) |
|
| 354 | + | sel->tags[i] = True; |
|
| 355 | + | setprops(sel); |
|
| 356 | + | arrange(); |
|
| 357 | + | } |
|
| 358 | + | ||
| 359 | + | void |
|
| 360 | + | toggleview(const char *arg) { |
|
| 361 | + | unsigned int i, j; |
|
| 362 | + | ||
| 363 | + | i = idxoftag(arg); |
|
| 364 | + | seltags[i] = !seltags[i]; |
|
| 365 | + | for(j = 0; j < ntags && !seltags[j]; j++); |
|
| 366 | + | if(j == ntags) |
|
| 367 | + | seltags[i] = True; /* cannot toggle last view */ |
|
| 368 | + | setdwmprops(); |
|
| 369 | + | arrange(); |
|
| 370 | + | } |
|
| 371 | + | ||
| 372 | + | void |
|
| 373 | + | view(const char *arg) { |
|
| 374 | + | unsigned int i; |
|
| 375 | + | ||
| 376 | + | for(i = 0; i < ntags; i++) |
|
| 377 | + | seltags[i] = arg == NULL; |
|
| 378 | + | i = idxoftag(arg); |
|
| 379 | + | if(i >= 0 && i < ntags) |
|
| 380 | + | seltags[i] = True; |
|
| 381 | + | setdwmprops(); |
|
| 382 | + | arrange(); |
|
| 383 | + | } |
| 1 | - | /* See LICENSE file for copyright and license details. */ |
|
| 2 | - | #include "dwm.h" |
|
| 3 | - | #include <regex.h> |
|
| 4 | - | #include <stdio.h> |
|
| 5 | - | #include <stdlib.h> |
|
| 6 | - | #include <X11/Xutil.h> |
|
| 7 | - | ||
| 8 | - | /* static */ |
|
| 9 | - | ||
| 10 | - | typedef struct { |
|
| 11 | - | const char *prop; |
|
| 12 | - | const char *tags; |
|
| 13 | - | Bool isfloating; |
|
| 14 | - | } Rule; |
|
| 15 | - | ||
| 16 | - | typedef struct { |
|
| 17 | - | regex_t *propregex; |
|
| 18 | - | regex_t *tagregex; |
|
| 19 | - | } Regs; |
|
| 20 | - | ||
| 21 | - | TAGS |
|
| 22 | - | RULES |
|
| 23 | - | ||
| 24 | - | static Regs *regs = NULL; |
|
| 25 | - | static unsigned int nrules = 0; |
|
| 26 | - | static char prop[512]; |
|
| 27 | - | ||
| 28 | - | static unsigned int |
|
| 29 | - | idxoftag(const char *tag) { |
|
| 30 | - | unsigned int i; |
|
| 31 | - | ||
| 32 | - | for(i = 0; i < ntags; i++) |
|
| 33 | - | if(tags[i] == tag) |
|
| 34 | - | return i; |
|
| 35 | - | return 0; |
|
| 36 | - | } |
|
| 37 | - | ||
| 38 | - | /* extern */ |
|
| 39 | - | ||
| 40 | - | void |
|
| 41 | - | applyrules(Client *c) { |
|
| 42 | - | unsigned int i, j; |
|
| 43 | - | regmatch_t tmp; |
|
| 44 | - | Bool matched = False; |
|
| 45 | - | XClassHint ch = { 0 }; |
|
| 46 | - | ||
| 47 | - | /* rule matching */ |
|
| 48 | - | XGetClassHint(dpy, c->win, &ch); |
|
| 49 | - | snprintf(prop, sizeof prop, "%s:%s:%s", |
|
| 50 | - | ch.res_class ? ch.res_class : "", |
|
| 51 | - | ch.res_name ? ch.res_name : "", c->name); |
|
| 52 | - | for(i = 0; i < nrules; i++) |
|
| 53 | - | if(regs[i].propregex && !regexec(regs[i].propregex, prop, 1, &tmp, 0)) { |
|
| 54 | - | c->isfloating = rules[i].isfloating; |
|
| 55 | - | for(j = 0; regs[i].tagregex && j < ntags; j++) { |
|
| 56 | - | if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) { |
|
| 57 | - | matched = True; |
|
| 58 | - | c->tags[j] = True; |
|
| 59 | - | } |
|
| 60 | - | } |
|
| 61 | - | } |
|
| 62 | - | if(ch.res_class) |
|
| 63 | - | XFree(ch.res_class); |
|
| 64 | - | if(ch.res_name) |
|
| 65 | - | XFree(ch.res_name); |
|
| 66 | - | if(!matched) |
|
| 67 | - | for(i = 0; i < ntags; i++) |
|
| 68 | - | c->tags[i] = seltags[i]; |
|
| 69 | - | } |
|
| 70 | - | ||
| 71 | - | void |
|
| 72 | - | compileregs(void) { |
|
| 73 | - | unsigned int i; |
|
| 74 | - | regex_t *reg; |
|
| 75 | - | ||
| 76 | - | if(regs) |
|
| 77 | - | return; |
|
| 78 | - | nrules = sizeof rules / sizeof rules[0]; |
|
| 79 | - | regs = emallocz(nrules * sizeof(Regs)); |
|
| 80 | - | for(i = 0; i < nrules; i++) { |
|
| 81 | - | if(rules[i].prop) { |
|
| 82 | - | reg = emallocz(sizeof(regex_t)); |
|
| 83 | - | if(regcomp(reg, rules[i].prop, REG_EXTENDED)) |
|
| 84 | - | free(reg); |
|
| 85 | - | else |
|
| 86 | - | regs[i].propregex = reg; |
|
| 87 | - | } |
|
| 88 | - | if(rules[i].tags) { |
|
| 89 | - | reg = emallocz(sizeof(regex_t)); |
|
| 90 | - | if(regcomp(reg, rules[i].tags, REG_EXTENDED)) |
|
| 91 | - | free(reg); |
|
| 92 | - | else |
|
| 93 | - | regs[i].tagregex = reg; |
|
| 94 | - | } |
|
| 95 | - | } |
|
| 96 | - | } |
|
| 97 | - | ||
| 98 | - | Bool |
|
| 99 | - | isvisible(Client *c) { |
|
| 100 | - | unsigned int i; |
|
| 101 | - | ||
| 102 | - | for(i = 0; i < ntags; i++) |
|
| 103 | - | if(c->tags[i] && seltags[i]) |
|
| 104 | - | return True; |
|
| 105 | - | return False; |
|
| 106 | - | } |
|
| 107 | - | ||
| 108 | - | void |
|
| 109 | - | tag(const char *arg) { |
|
| 110 | - | unsigned int i; |
|
| 111 | - | ||
| 112 | - | if(!sel) |
|
| 113 | - | return; |
|
| 114 | - | for(i = 0; i < ntags; i++) |
|
| 115 | - | sel->tags[i] = arg == NULL; |
|
| 116 | - | i = idxoftag(arg); |
|
| 117 | - | if(i >= 0 && i < ntags) |
|
| 118 | - | sel->tags[i] = True; |
|
| 119 | - | saveprops(sel); |
|
| 120 | - | arrange(); |
|
| 121 | - | } |
|
| 122 | - | ||
| 123 | - | void |
|
| 124 | - | togglefloating(const char *arg) { |
|
| 125 | - | if(!sel || isfloating()) |
|
| 126 | - | return; |
|
| 127 | - | sel->isfloating = !sel->isfloating; |
|
| 128 | - | if(sel->isfloating) { |
|
| 129 | - | resize(sel, sel->x, sel->y, sel->w, sel->h, True); |
|
| 130 | - | saveprops(sel); |
|
| 131 | - | } |
|
| 132 | - | arrange(); |
|
| 133 | - | } |
|
| 134 | - | ||
| 135 | - | void |
|
| 136 | - | toggletag(const char *arg) { |
|
| 137 | - | unsigned int i, j; |
|
| 138 | - | ||
| 139 | - | if(!sel) |
|
| 140 | - | return; |
|
| 141 | - | i = idxoftag(arg); |
|
| 142 | - | sel->tags[i] = !sel->tags[i]; |
|
| 143 | - | for(j = 0; j < ntags && !sel->tags[j]; j++); |
|
| 144 | - | if(j == ntags) |
|
| 145 | - | sel->tags[i] = True; |
|
| 146 | - | saveprops(sel); |
|
| 147 | - | arrange(); |
|
| 148 | - | } |
|
| 149 | - | ||
| 150 | - | void |
|
| 151 | - | toggleview(const char *arg) { |
|
| 152 | - | unsigned int i, j; |
|
| 153 | - | ||
| 154 | - | i = idxoftag(arg); |
|
| 155 | - | seltags[i] = !seltags[i]; |
|
| 156 | - | for(j = 0; j < ntags && !seltags[j]; j++); |
|
| 157 | - | if(j == ntags) |
|
| 158 | - | seltags[i] = True; /* cannot toggle last view */ |
|
| 159 | - | savedwmprops(); |
|
| 160 | - | arrange(); |
|
| 161 | - | } |
|
| 162 | - | ||
| 163 | - | void |
|
| 164 | - | view(const char *arg) { |
|
| 165 | - | unsigned int i; |
|
| 166 | - | ||
| 167 | - | for(i = 0; i < ntags; i++) |
|
| 168 | - | seltags[i] = arg == NULL; |
|
| 169 | - | i = idxoftag(arg); |
|
| 170 | - | if(i >= 0 && i < ntags) |
|
| 171 | - | seltags[i] = True; |
|
| 172 | - | savedwmprops(); |
|
| 173 | - | arrange(); |
|
| 174 | - | } |