dwm-meillo

annotate tag.c @ 743:cb8b18d787d0

treating acroread and mplayer floating by default
author Anselm R. Garbe <arg@suckless.org>
date Fri, 09 Feb 2007 11:58:37 +0100
parents 1ed8c40dde36
children 08b89915c109
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 }
arg@114 99 }
garbeam@76 100 if(ch.res_class)
garbeam@76 101 XFree(ch.res_class);
garbeam@76 102 if(ch.res_name)
garbeam@76 103 XFree(ch.res_name);
garbeam@76 104 }
garbeam@76 105 if(!matched)
arg@261 106 for(i = 0; i < ntags; i++)
arg@262 107 c->tags[i] = seltag[i];
garbeam@76 108 }
garbeam@76 109
garbeam@76 110 void
arg@461 111 tag(Arg *arg) {
arg@284 112 unsigned int i;
arg@284 113
arg@284 114 if(!sel)
arg@284 115 return;
arg@284 116 for(i = 0; i < ntags; i++)
arg@594 117 sel->tags[i] = (arg->i == -1) ? True : False;
arg@611 118 if(arg->i >= 0 && arg->i < ntags)
arg@611 119 sel->tags[arg->i] = True;
arg@533 120 arrange();
arg@284 121 }
arg@284 122
arg@284 123 void
arg@461 124 toggletag(Arg *arg) {
arg@284 125 unsigned int i;
arg@284 126
arg@284 127 if(!sel)
arg@284 128 return;
arg@284 129 sel->tags[arg->i] = !sel->tags[arg->i];
arg@284 130 for(i = 0; i < ntags && !sel->tags[i]; i++);
arg@284 131 if(i == ntags)
arg@284 132 sel->tags[arg->i] = True;
arg@533 133 arrange();
arg@284 134 }