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.

250 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. if(i + 1 == n)
  94. c->h = sh - c->y - 2;
  95. else
  96. c->h = h - 2;
  97. }
  98. else { /* fallback if h < bh */
  99. c->x = sx + mw;
  100. c->y = sy + bh;
  101. c->w = w - 2;
  102. c->h = sh - 2 - bh;
  103. }
  104. resize(c, False, TopLeft);
  105. i++;
  106. }
  107. else
  108. ban(c);
  109. }
  110. if((sel = getnext(clients))) {
  111. higher(sel);
  112. focus(sel);
  113. }
  114. else
  115. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  116. drawall();
  117. }
  118. Client *
  119. getnext(Client *c)
  120. {
  121. for(; c && !c->tags[tsel]; c = c->next);
  122. return c;
  123. }
  124. Client *
  125. getprev(Client *c)
  126. {
  127. for(; c && !c->tags[tsel]; c = c->prev);
  128. return c;
  129. }
  130. void
  131. initrregs()
  132. {
  133. unsigned int i;
  134. regex_t *reg;
  135. if(rreg)
  136. return;
  137. len = sizeof(rule) / sizeof(rule[0]);
  138. rreg = emallocz(len * sizeof(RReg));
  139. for(i = 0; i < len; i++) {
  140. if(rule[i].clpattern) {
  141. reg = emallocz(sizeof(regex_t));
  142. if(regcomp(reg, rule[i].clpattern, 0))
  143. free(reg);
  144. else
  145. rreg[i].clregex = reg;
  146. }
  147. if(rule[i].tpattern) {
  148. reg = emallocz(sizeof(regex_t));
  149. if(regcomp(reg, rule[i].tpattern, 0))
  150. free(reg);
  151. else
  152. rreg[i].tregex = reg;
  153. }
  154. }
  155. }
  156. void
  157. replacetag(Arg *arg)
  158. {
  159. int i;
  160. if(!sel)
  161. return;
  162. for(i = 0; i < ntags; i++)
  163. sel->tags[i] = False;
  164. appendtag(arg);
  165. }
  166. void
  167. settags(Client *c)
  168. {
  169. char classinst[256];
  170. unsigned int i, j;
  171. regmatch_t tmp;
  172. Bool matched = False;
  173. XClassHint ch;
  174. if(XGetClassHint(dpy, c->win, &ch)) {
  175. snprintf(classinst, sizeof(classinst), "%s:%s",
  176. ch.res_class ? ch.res_class : "",
  177. ch.res_name ? ch.res_name : "");
  178. for(i = 0; !matched && i < len; i++)
  179. if(rreg[i].clregex && !regexec(rreg[i].clregex, classinst, 1, &tmp, 0)) {
  180. c->isfloat = rule[i].isfloat;
  181. for(j = 0; rreg[i].tregex && j < ntags; j++) {
  182. if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
  183. matched = True;
  184. c->tags[j] = True;
  185. }
  186. }
  187. }
  188. if(ch.res_class)
  189. XFree(ch.res_class);
  190. if(ch.res_name)
  191. XFree(ch.res_name);
  192. }
  193. if(!matched)
  194. c->tags[tsel] = True;
  195. }
  196. void
  197. togglemode(Arg *arg)
  198. {
  199. arrange = arrange == dofloat ? dotile : dofloat;
  200. arrange(NULL);
  201. }
  202. void
  203. view(Arg *arg)
  204. {
  205. tsel = arg->i;
  206. arrange(NULL);
  207. drawall();
  208. }
  209. void
  210. viewnext(Arg *arg)
  211. {
  212. arg->i = (tsel < ntags-1) ? tsel+1 : 0;
  213. view(arg);
  214. }
  215. void
  216. viewprev(Arg *arg)
  217. {
  218. arg->i = (tsel > 0) ? tsel-1 : ntags-1;
  219. view(arg);
  220. }