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.

155 lines
2.9 KiB

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