tag.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* See LICENSE file for copyright and license details. */
  2. #include "dwm.h"
  3. #include <regex.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <X11/Xutil.h>
  7. /* static */
  8. typedef struct {
  9. const char *prop;
  10. const char *tags;
  11. Bool isfloating;
  12. } Rule;
  13. typedef struct {
  14. regex_t *propregex;
  15. regex_t *tagregex;
  16. } Regs;
  17. TAGS
  18. RULES
  19. static Regs *regs = NULL;
  20. static unsigned int nrules = 0;
  21. static char prop[512];
  22. static unsigned int
  23. idxoftag(const char *tag) {
  24. unsigned int i;
  25. for(i = 0; i < ntags; i++)
  26. if(tags[i] == tag)
  27. return i;
  28. return 0;
  29. }
  30. /* extern */
  31. void
  32. applyrules(Client *c) {
  33. unsigned int i, j;
  34. regmatch_t tmp;
  35. Bool matched = False;
  36. XClassHint ch = { 0 };
  37. /* rule matching */
  38. XGetClassHint(dpy, c->win, &ch);
  39. snprintf(prop, sizeof prop, "%s:%s:%s",
  40. ch.res_class ? ch.res_class : "",
  41. ch.res_name ? ch.res_name : "", c->name);
  42. for(i = 0; i < nrules; i++)
  43. if(regs[i].propregex && !regexec(regs[i].propregex, prop, 1, &tmp, 0)) {
  44. c->isfloating = rules[i].isfloating;
  45. for(j = 0; regs[i].tagregex && j < ntags; j++) {
  46. if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
  47. matched = True;
  48. c->tags[j] = True;
  49. }
  50. }
  51. }
  52. if(ch.res_class)
  53. XFree(ch.res_class);
  54. if(ch.res_name)
  55. XFree(ch.res_name);
  56. if(!matched)
  57. for(i = 0; i < ntags; i++)
  58. c->tags[i] = seltags[i];
  59. }
  60. void
  61. compileregs(void) {
  62. unsigned int i;
  63. regex_t *reg;
  64. if(regs)
  65. return;
  66. nrules = sizeof rules / sizeof rules[0];
  67. regs = emallocz(nrules * sizeof(Regs));
  68. for(i = 0; i < nrules; i++) {
  69. if(rules[i].prop) {
  70. reg = emallocz(sizeof(regex_t));
  71. if(regcomp(reg, rules[i].prop, REG_EXTENDED))
  72. free(reg);
  73. else
  74. regs[i].propregex = reg;
  75. }
  76. if(rules[i].tags) {
  77. reg = emallocz(sizeof(regex_t));
  78. if(regcomp(reg, rules[i].tags, REG_EXTENDED))
  79. free(reg);
  80. else
  81. regs[i].tagregex = reg;
  82. }
  83. }
  84. }
  85. Bool
  86. isvisible(Client *c) {
  87. unsigned int i;
  88. for(i = 0; i < ntags; i++)
  89. if(c->tags[i] && seltags[i])
  90. return True;
  91. return False;
  92. }
  93. void
  94. tag(const char *arg) {
  95. unsigned int i;
  96. if(!sel)
  97. return;
  98. for(i = 0; i < ntags; i++)
  99. sel->tags[i] = arg == NULL;
  100. i = idxoftag(arg);
  101. if(i >= 0 && i < ntags)
  102. sel->tags[i] = True;
  103. saveprops(sel);
  104. arrange();
  105. }
  106. void
  107. togglefloating(const char *arg) {
  108. if(!sel || isfloating())
  109. return;
  110. sel->isfloating = !sel->isfloating;
  111. if(sel->isfloating) {
  112. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  113. saveprops(sel);
  114. }
  115. arrange();
  116. }
  117. void
  118. toggletag(const char *arg) {
  119. unsigned int i, j;
  120. if(!sel)
  121. return;
  122. i = idxoftag(arg);
  123. sel->tags[i] = !sel->tags[i];
  124. for(j = 0; j < ntags && !sel->tags[j]; j++);
  125. if(j == ntags)
  126. sel->tags[i] = True;
  127. saveprops(sel);
  128. arrange();
  129. }
  130. void
  131. toggleview(const char *arg) {
  132. unsigned int i, j;
  133. i = idxoftag(arg);
  134. seltags[i] = !seltags[i];
  135. for(j = 0; j < ntags && !seltags[j]; j++);
  136. if(j == ntags)
  137. seltags[i] = True; /* cannot toggle last view */
  138. savedwmprops();
  139. arrange();
  140. }
  141. void
  142. view(const char *arg) {
  143. unsigned int i;
  144. for(i = 0; i < ntags; i++)
  145. seltags[i] = arg == NULL;
  146. i = idxoftag(arg);
  147. if(i >= 0 && i < ntags)
  148. seltags[i] = True;
  149. savedwmprops();
  150. arrange();
  151. }