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.

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