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.

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