tag.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * wmfs2 by Martin Duquesnoy <xorg62@gmail.com> { for(i = 2011; i < 2111; ++i) ©(i); }
  3. * For license, see COPYING.
  4. */
  5. #include <X11/Xutil.h> /* IconicState / NormalState */
  6. #include "tag.h"
  7. #include "util.h"
  8. #include "infobar.h"
  9. #include "client.h"
  10. #include "config.h"
  11. #include "barwin.h"
  12. #include "ewmh.h"
  13. #include "layout.h"
  14. struct tag*
  15. tag_new(struct screen *s, char *name)
  16. {
  17. struct tag *t, *l;
  18. t = xcalloc(1, sizeof(struct tag));
  19. t->screen = s;
  20. t->name = xstrdup(name);
  21. t->flags = 0;
  22. t->id = 0;
  23. t->sel = NULL;
  24. t->prev = NULL;
  25. if((l = TAILQ_LAST(&s->tags, tsub)))
  26. t->id = l->id + 1;
  27. SLIST_INIT(&t->clients);
  28. TAILQ_INIT(&t->sets);
  29. TAILQ_INSERT_TAIL(&s->tags, t, next);
  30. return t;
  31. }
  32. void
  33. tag_screen(struct screen *s, struct tag *t)
  34. {
  35. if(t == s->seltag)
  36. t = t->prev;
  37. if(!t)
  38. t = TAILQ_FIRST(&s->tags);
  39. t->prev = s->seltag;
  40. s->seltag = t;
  41. clients_arrange_map();
  42. if(!SLIST_EMPTY(&t->clients) && !(W->flags & WMFS_SCAN))
  43. client_focus( client_tab_next(t->sel));
  44. if(t->flags & TAG_URGENT)
  45. t->flags ^= TAG_URGENT;
  46. infobar_elem_screen_update(s, ElemTag);
  47. ewmh_update_wmfs_props();
  48. }
  49. /* Set t to NULL to untag c from c->tag */
  50. void
  51. tag_client(struct tag *t, struct client *c)
  52. {
  53. /* Remove client from its previous tag */
  54. if(c->tag && !(c->flags & CLIENT_RULED))
  55. {
  56. if(c->tag == t)
  57. return;
  58. if(!(c->flags & (CLIENT_IGNORE_LAYOUT | CLIENT_FREE)))
  59. layout_split_arrange_closed(c);
  60. if(!(c->flags & CLIENT_REMOVEALL))
  61. {
  62. SLIST_REMOVE(&c->tag->clients, c, client, tnext);
  63. if(c->tag->sel == c || W->client == c)
  64. client_focus( client_tab_next( client_next(c)));
  65. }
  66. }
  67. c->flags &= ~CLIENT_RULED;
  68. /* Client remove */
  69. if(!t)
  70. return;
  71. c->prevtag = c->tag;
  72. c->tag = t;
  73. c->screen = t->screen;
  74. client_update_props(c, CPROP_LOC);
  75. SLIST_INSERT_HEAD(&t->clients, c, tnext);
  76. infobar_elem_screen_update(t->screen, ElemTag);
  77. if(c->flags & CLIENT_TABMASTER && c->prevtag)
  78. {
  79. struct client *cc;
  80. SLIST_FOREACH(cc, &c->prevtag->clients, tnext)
  81. if(cc->tabmaster == c)
  82. {
  83. cc->flags |= CLIENT_IGNORE_LAYOUT;
  84. tag_client(t, cc);
  85. }
  86. }
  87. layout_client(c);
  88. if(t != c->screen->seltag || c->flags & CLIENT_TABBED)
  89. client_unmap(c);
  90. }
  91. void
  92. uicb_tag_set(Uicb cmd)
  93. {
  94. int i = 0, n = ATOI(cmd);
  95. struct tag *t;
  96. TAILQ_FOREACH(t, &W->screen->tags, next)
  97. if(i++ == n)
  98. {
  99. tag_screen(W->screen, t);
  100. return;
  101. }
  102. }
  103. void
  104. uicb_tag_set_with_name(Uicb cmd)
  105. {
  106. struct tag *t;
  107. TAILQ_FOREACH(t, &W->screen->tags, next)
  108. if(!strcmp(cmd, t->name))
  109. {
  110. tag_screen(W->screen, t);
  111. return;
  112. }
  113. }
  114. void
  115. uicb_tag_next(Uicb cmd)
  116. {
  117. (void)cmd;
  118. struct tag *t;
  119. if((t = TAILQ_NEXT(W->screen->seltag, next)))
  120. tag_screen(W->screen, t);
  121. else if( /* CIRCULAR OPTION */ 1)
  122. tag_screen(W->screen, TAILQ_FIRST(&W->screen->tags));
  123. }
  124. void
  125. uicb_tag_prev(Uicb cmd)
  126. {
  127. (void)cmd;
  128. struct tag *t;
  129. if((t = TAILQ_PREV(W->screen->seltag, tsub, next)))
  130. tag_screen(W->screen, t);
  131. else if( /* CIRCULAR OPTION */ 1)
  132. tag_screen(W->screen, TAILQ_LAST(&W->screen->tags, tsub));
  133. }
  134. void
  135. uicb_tag_client(Uicb cmd)
  136. {
  137. struct tag *t;
  138. int id = ATOI(cmd);
  139. if((t = tag_gb_id(W->screen, id)))
  140. tag_client(t, W->client);
  141. }
  142. void
  143. uicb_tag_move_client_next(Uicb cmd)
  144. {
  145. (void)cmd;
  146. struct tag *t;
  147. if((t = TAILQ_NEXT(W->screen->seltag, next)))
  148. tag_client(t, W->client);
  149. else if( /* CIRCULAR OPTION */ 1)
  150. tag_client(TAILQ_FIRST(&W->screen->tags), W->client);
  151. }
  152. void
  153. uicb_tag_move_client_prev(Uicb cmd)
  154. {
  155. (void)cmd;
  156. struct tag *t;
  157. if((t = TAILQ_PREV(W->screen->seltag, tsub, next)))
  158. tag_client(t, W->client);
  159. else if( /* CIRCULAR OPTION */ 1)
  160. tag_client(TAILQ_LAST(&W->screen->tags, tsub), W->client);
  161. }
  162. void
  163. uicb_tag_click(Uicb cmd)
  164. {
  165. (void)cmd;
  166. struct tag *t;
  167. if((t = (struct tag*)W->last_clicked_barwin->ptr)
  168. && t->screen == W->screen)
  169. tag_screen(W->screen, t);
  170. }
  171. static void
  172. tag_remove(struct tag *t)
  173. {
  174. free(t->name);
  175. layout_free_set(t);
  176. free(t);
  177. }
  178. void
  179. tag_free(struct screen *s)
  180. {
  181. struct tag *t;
  182. TAILQ_FOREACH(t, &s->tags, next)
  183. {
  184. TAILQ_REMOVE(&s->tags, t, next);
  185. tag_remove(t);
  186. }
  187. }