aewl

annotate tag.c @ 751:743061dbe7f2

made Mod-F4 (shutdown) work on notebook
author meillo@marmaro.de
date Sat, 16 Jun 2007 21:44:00 +0200
parents 6692d7e7e156
children
rev   line source
arg@644 1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
garbeam@75 2 * See LICENSE file for license details.
garbeam@75 3 */
garbeam@76 4 #include "dwm.h"
arg@114 5 #include <regex.h>
arg@114 6 #include <stdio.h>
arg@191 7 #include <stdlib.h>
garbeam@75 8 #include <string.h>
arg@114 9 #include <sys/types.h>
garbeam@75 10 #include <X11/Xutil.h>
garbeam@75 11
garbeam@76 12
arg@114 13 typedef struct {
arg@191 14 const char *clpattern;
arg@191 15 const char *tpattern;
arg@114 16 Bool isfloat;
arg@114 17 } Rule;
arg@114 18
arg@191 19 typedef struct {
arg@191 20 regex_t *clregex;
arg@191 21 regex_t *tregex;
arg@191 22 } RReg;
arg@191 23
arg@191 24 /* static */
arg@191 25
arg@146 26 TAGS
arg@146 27 RULES
garbeam@84 28
arg@191 29 static RReg *rreg = NULL;
arg@191 30 static unsigned int len = 0;
arg@191 31
arg@125 32 /* extern */
arg@125 33
garbeam@76 34 Client *
arg@461 35 getnext(Client *c) {
arg@261 36 for(; c && !isvisible(c); c = c->next);
garbeam@76 37 return c;
garbeam@75 38 }
garbeam@75 39
arg@127 40 Client *
arg@461 41 getprev(Client *c) {
arg@261 42 for(; c && !isvisible(c); c = c->prev);
arg@127 43 return c;
arg@127 44 }
arg@127 45
garbeam@75 46 void
arg@487 47 initrregs(void) {
arg@191 48 unsigned int i;
arg@191 49 regex_t *reg;
arg@191 50
arg@191 51 if(rreg)
arg@191 52 return;
arg@581 53 len = sizeof rule / sizeof rule[0];
arg@191 54 rreg = emallocz(len * sizeof(RReg));
arg@191 55 for(i = 0; i < len; i++) {
arg@191 56 if(rule[i].clpattern) {
arg@191 57 reg = emallocz(sizeof(regex_t));
arg@609 58 if(regcomp(reg, rule[i].clpattern, REG_EXTENDED))
arg@191 59 free(reg);
arg@191 60 else
arg@191 61 rreg[i].clregex = reg;
arg@191 62 }
arg@191 63 if(rule[i].tpattern) {
arg@191 64 reg = emallocz(sizeof(regex_t));
arg@609 65 if(regcomp(reg, rule[i].tpattern, REG_EXTENDED))
arg@191 66 free(reg);
arg@191 67 else
arg@191 68 rreg[i].tregex = reg;
arg@191 69 }
arg@191 70 }
arg@191 71 }
arg@191 72
arg@270 73 void
arg@461 74 settags(Client *c, Client *trans) {
arg@336 75 char prop[512];
arg@191 76 unsigned int i, j;
arg@114 77 regmatch_t tmp;
arg@431 78 Bool matched = trans != NULL;
arg@742 79 XClassHint ch = { 0 };
garbeam@76 80
arg@431 81 if(matched) {
arg@431 82 for(i = 0; i < ntags; i++)
arg@431 83 c->tags[i] = trans->tags[i];
arg@431 84 }
arg@742 85 else {
arg@742 86 XGetClassHint(dpy, c->win, &ch);
arg@581 87 snprintf(prop, sizeof prop, "%s:%s:%s",
arg@114 88 ch.res_class ? ch.res_class : "",
arg@336 89 ch.res_name ? ch.res_name : "", c->name);
arg@609 90 for(i = 0; i < len; i++)
arg@336 91 if(rreg[i].clregex && !regexec(rreg[i].clregex, prop, 1, &tmp, 0)) {
arg@191 92 c->isfloat = rule[i].isfloat;
arg@191 93 for(j = 0; rreg[i].tregex && j < ntags; j++) {
arg@191 94 if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
arg@191 95 matched = True;
arg@191 96 c->tags[j] = True;
arg@191 97 }
garbeam@76 98 }
meillo@747 99 break; /* perform only the first rule matching */
arg@114 100 }
garbeam@76 101 if(ch.res_class)
garbeam@76 102 XFree(ch.res_class);
garbeam@76 103 if(ch.res_name)
garbeam@76 104 XFree(ch.res_name);
garbeam@76 105 }
garbeam@76 106 if(!matched)
arg@261 107 for(i = 0; i < ntags; i++)
arg@262 108 c->tags[i] = seltag[i];
garbeam@76 109 }
garbeam@76 110
garbeam@76 111 void
arg@461 112 tag(Arg *arg) {
arg@284 113 unsigned int i;
arg@284 114
arg@284 115 if(!sel)
arg@284 116 return;
arg@284 117 for(i = 0; i < ntags; i++)
arg@594 118 sel->tags[i] = (arg->i == -1) ? True : False;
arg@611 119 if(arg->i >= 0 && arg->i < ntags)
arg@611 120 sel->tags[arg->i] = True;
arg@533 121 arrange();
arg@284 122 }
arg@284 123
arg@284 124 void
arg@461 125 toggletag(Arg *arg) {
arg@284 126 unsigned int i;
arg@284 127
arg@284 128 if(!sel)
arg@284 129 return;
arg@284 130 sel->tags[arg->i] = !sel->tags[arg->i];
arg@284 131 for(i = 0; i < ntags && !sel->tags[i]; i++);
arg@284 132 if(i == ntags)
arg@284 133 sel->tags[arg->i] = True;
arg@533 134 arrange();
arg@284 135 }
meillo@745 136
meillo@749 137 /* begin code by jukka */
meillo@745 138 void
meillo@745 139 viewnext(Arg *arg) {
meillo@745 140 unsigned int i;
meillo@745 141 Bool last = seltag[ntags-1];
meillo@745 142
meillo@745 143 for (i=ntags-1; i>0; --i)
meillo@745 144 seltag[i] = seltag[i-1];
meillo@745 145 seltag[0] = last;
meillo@745 146 arrange();
meillo@745 147 }
meillo@749 148 /* end code by jukka */