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.

150 lines
2.8 KiB

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