Configuration of dwm for Mac Computers
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.

177 lines
3.4 KiB

17 years ago
17 years ago
17 years ago
  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 <string.h>
  7. #include <X11/Xatom.h>
  8. #include <X11/Xutil.h>
  9. /* static */
  10. typedef struct {
  11. const char *prop;
  12. const char *tags;
  13. Bool isfloating;
  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. static char prop[512];
  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. unsigned int i, j;
  61. regmatch_t tmp;
  62. Bool matched = trans != NULL;
  63. XClassHint ch = { 0 };
  64. XTextProperty name;
  65. if(matched) {
  66. for(i = 0; i < ntags; i++)
  67. c->tags[i] = trans->tags[i];
  68. return;
  69. }
  70. else {
  71. /* check if window has set a property */
  72. name.nitems = 0;
  73. XGetTextProperty(dpy, c->win, &name, dwmtags);
  74. if(name.nitems && name.encoding == XA_STRING) {
  75. strncpy(prop, (char *)name.value, sizeof prop - 1);
  76. prop[sizeof prop - 1] = '\0';
  77. XFree(name.value);
  78. for(i = 0; i < ntags && i < sizeof prop - 1 && prop[i] != '\0'; i++)
  79. if((c->tags[i] = prop[i] == '+'))
  80. matched = True;
  81. }
  82. if(matched)
  83. return;
  84. /* rule matching */
  85. XGetClassHint(dpy, c->win, &ch);
  86. snprintf(prop, sizeof prop, "%s:%s:%s",
  87. ch.res_class ? ch.res_class : "",
  88. ch.res_name ? ch.res_name : "", c->name);
  89. for(i = 0; i < nrules; i++)
  90. if(regs[i].propregex && !regexec(regs[i].propregex, prop, 1, &tmp, 0)) {
  91. c->isfloating = rule[i].isfloating;
  92. for(j = 0; regs[i].tagregex && j < ntags; j++) {
  93. if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
  94. matched = True;
  95. c->tags[j] = True;
  96. }
  97. }
  98. }
  99. if(ch.res_class)
  100. XFree(ch.res_class);
  101. if(ch.res_name)
  102. XFree(ch.res_name);
  103. }
  104. if(!matched)
  105. for(i = 0; i < ntags; i++)
  106. c->tags[i] = seltag[i];
  107. }
  108. void
  109. tag(const char *arg) {
  110. int i;
  111. if(!sel)
  112. return;
  113. for(i = 0; i < ntags; i++)
  114. sel->tags[i] = arg == NULL;
  115. i = arg ? atoi(arg) : 0;
  116. if(i >= 0 && i < ntags)
  117. sel->tags[i] = True;
  118. if(sel) {
  119. for(i = 0; i < ntags && i < sizeof prop - 1; i++)
  120. prop[i] = sel->tags[i] ? '+' : '-';
  121. prop[i] = '\0';
  122. XChangeProperty(dpy, sel->win, dwmtags, XA_STRING, 8, PropModeReplace, (unsigned char *)prop, i);
  123. }
  124. arrange();
  125. }
  126. void
  127. toggletag(const char *arg) {
  128. int i, j;
  129. if(!sel)
  130. return;
  131. i = arg ? atoi(arg) : 0;
  132. sel->tags[i] = !sel->tags[i];
  133. for(j = 0; j < ntags && !sel->tags[j]; j++);
  134. if(j == ntags)
  135. sel->tags[i] = True;
  136. arrange();
  137. }
  138. void
  139. toggleview(const char *arg) {
  140. int i, j;
  141. i = arg ? atoi(arg) : 0;
  142. seltag[i] = !seltag[i];
  143. for(j = 0; j < ntags && !seltag[j]; j++);
  144. if(j == ntags)
  145. seltag[i] = True; /* cannot toggle last view */
  146. arrange();
  147. }
  148. void
  149. view(const char *arg) {
  150. int i;
  151. for(i = 0; i < ntags; i++)
  152. seltag[i] = arg == NULL;
  153. i = arg ? atoi(arg) : 0;
  154. if(i >= 0 && i < ntags)
  155. seltag[i] = True;
  156. arrange();
  157. }