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.

139 lines
2.5 KiB

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