You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
3.8 KiB

19 years ago
19 years ago
  1. /*
  2. * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include "dwm.h"
  6. #include <regex.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/types.h>
  11. #include <X11/Xutil.h>
  12. typedef struct {
  13. const char *clpattern;
  14. const char *tpattern;
  15. Bool isfloat;
  16. } Rule;
  17. typedef struct {
  18. regex_t *clregex;
  19. regex_t *tregex;
  20. } RReg;
  21. /* static */
  22. TAGS
  23. RULES
  24. static RReg *rreg = NULL;
  25. static unsigned int len = 0;
  26. void (*arrange)(Arg *) = DEFMODE;
  27. /* extern */
  28. void
  29. appendtag(Arg *arg)
  30. {
  31. if(!sel)
  32. return;
  33. sel->tags[arg->i] = True;
  34. arrange(NULL);
  35. }
  36. void
  37. dofloat(Arg *arg)
  38. {
  39. Client *c;
  40. for(c = clients; c; c = c->next) {
  41. c->ismax = False;
  42. if(c->tags[tsel]) {
  43. resize(c, True, TopLeft);
  44. }
  45. else
  46. ban(c);
  47. }
  48. if((sel = getnext(clients))) {
  49. higher(sel);
  50. focus(sel);
  51. }
  52. else
  53. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  54. drawall();
  55. }
  56. void
  57. dotile(Arg *arg)
  58. {
  59. int n, i, w, h;
  60. Client *c;
  61. w = sw - mw;
  62. for(n = 0, c = clients; c; c = c->next)
  63. if(c->tags[tsel] && !c->isfloat)
  64. n++;
  65. if(n > 1)
  66. h = (sh - bh) / (n - 1);
  67. else
  68. h = sh - bh;
  69. for(i = 0, c = clients; c; c = c->next) {
  70. c->ismax = False;
  71. if(c->tags[tsel]) {
  72. if(c->isfloat) {
  73. higher(c);
  74. resize(c, True, TopLeft);
  75. continue;
  76. }
  77. if(n == 1) {
  78. c->x = sx;
  79. c->y = sy + bh;
  80. c->w = sw - 2;
  81. c->h = sh - 2 - bh;
  82. }
  83. else if(i == 0) {
  84. c->x = sx;
  85. c->y = sy + bh;
  86. c->w = mw - 2;
  87. c->h = sh - 2 - bh;
  88. }
  89. else if(h > bh) {
  90. c->x = sx + mw;
  91. c->y = sy + (i - 1) * h + bh;
  92. c->w = w - 2;
  93. c->h = h - 2;
  94. }
  95. else { /* fallback if h < bh */
  96. c->x = sx + mw;
  97. c->y = sy + bh;
  98. c->w = w - 2;
  99. c->h = sh - 2 - bh;
  100. }
  101. resize(c, False, TopLeft);
  102. i++;
  103. }
  104. else
  105. ban(c);
  106. }
  107. if((sel = getnext(clients))) {
  108. higher(sel);
  109. focus(sel);
  110. }
  111. else
  112. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  113. drawall();
  114. }
  115. Client *
  116. getnext(Client *c)
  117. {
  118. for(; c && !c->tags[tsel]; c = c->next);
  119. return c;
  120. }
  121. Client *
  122. getprev(Client *c)
  123. {
  124. for(; c && !c->tags[tsel]; c = c->prev);
  125. return c;
  126. }
  127. void
  128. initrregs()
  129. {
  130. unsigned int i;
  131. regex_t *reg;
  132. if(rreg)
  133. return;
  134. len = sizeof(rule) / sizeof(rule[0]);
  135. rreg = emallocz(len * sizeof(RReg));
  136. for(i = 0; i < len; i++) {
  137. if(rule[i].clpattern) {
  138. reg = emallocz(sizeof(regex_t));
  139. if(regcomp(reg, rule[i].clpattern, 0))
  140. free(reg);
  141. else
  142. rreg[i].clregex = reg;
  143. }
  144. if(rule[i].tpattern) {
  145. reg = emallocz(sizeof(regex_t));
  146. if(regcomp(reg, rule[i].tpattern, 0))
  147. free(reg);
  148. else
  149. rreg[i].tregex = reg;
  150. }
  151. }
  152. }
  153. void
  154. replacetag(Arg *arg)
  155. {
  156. int i;
  157. if(!sel)
  158. return;
  159. for(i = 0; i < ntags; i++)
  160. sel->tags[i] = False;
  161. appendtag(arg);
  162. }
  163. void
  164. settags(Client *c)
  165. {
  166. char classinst[256];
  167. unsigned int i, j;
  168. regmatch_t tmp;
  169. Bool matched = False;
  170. XClassHint ch;
  171. if(XGetClassHint(dpy, c->win, &ch)) {
  172. snprintf(classinst, sizeof(classinst), "%s:%s",
  173. ch.res_class ? ch.res_class : "",
  174. ch.res_name ? ch.res_name : "");
  175. for(i = 0; !matched && i < len; i++)
  176. if(rreg[i].clregex && !regexec(rreg[i].clregex, classinst, 1, &tmp, 0)) {
  177. c->isfloat = rule[i].isfloat;
  178. for(j = 0; rreg[i].tregex && j < ntags; j++) {
  179. if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
  180. matched = True;
  181. c->tags[j] = True;
  182. }
  183. }
  184. }
  185. if(ch.res_class)
  186. XFree(ch.res_class);
  187. if(ch.res_name)
  188. XFree(ch.res_name);
  189. }
  190. if(!matched)
  191. c->tags[tsel] = True;
  192. }
  193. void
  194. togglemode(Arg *arg)
  195. {
  196. arrange = arrange == dofloat ? dotile : dofloat;
  197. arrange(NULL);
  198. }
  199. void
  200. view(Arg *arg)
  201. {
  202. tsel = arg->i;
  203. arrange(NULL);
  204. drawall();
  205. }
  206. void
  207. viewnext(Arg *arg)
  208. {
  209. arg->i = (tsel < ntags-1) ? tsel+1 : 0;
  210. view(arg);
  211. }
  212. void
  213. viewprev(Arg *arg)
  214. {
  215. arg->i = (tsel > 0) ? tsel-1 : ntags-1;
  216. view(arg);
  217. }