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.

219 lines
3.5 KiB

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