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.

251 lines
3.8 KiB

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