layout.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * wmfs2 by Martin Duquesnoy <xorg62@gmail.com> { for(i = 2011; i < 2111; ++i) ©(i); }
  3. * For license, see COPYING.
  4. */
  5. #include "layout.h"
  6. #include "config.h"
  7. #include "client.h"
  8. #include "util.h"
  9. static struct geo
  10. layout_split(struct client *c, bool vertical)
  11. {
  12. struct geo og, geo;
  13. geo = og = c->geo;
  14. if(vertical)
  15. {
  16. c->geo.w >>= 1;
  17. geo.x = c->geo.x + c->geo.w;
  18. geo.w >>= 1;
  19. /* Remainder */
  20. geo.w += (og.x + og.w) - (geo.x + geo.w);
  21. }
  22. else
  23. {
  24. c->geo.h >>= 1;
  25. geo.y = c->geo.y + c->geo.h;
  26. geo.h >>= 1;
  27. /* Remainder */
  28. geo.h += (og.y + og.h) - (geo.y + geo.h);
  29. }
  30. client_moveresize(c, c->geo);
  31. return geo;
  32. }
  33. static inline void
  34. layout_split_arrange_size(struct geo g, struct client *c, Position p)
  35. {
  36. if(LDIR(p))
  37. {
  38. c->geo.w += g.w;
  39. if(p == Right)
  40. c->geo.x = g.x;
  41. }
  42. else
  43. {
  44. c->geo.h += g.h;
  45. if(p == Bottom)
  46. c->geo.y = g.y;
  47. }
  48. client_moveresize(c, c->geo);
  49. }
  50. static inline bool
  51. layout_split_check_row_dir(struct client *c, struct client *g, Position p)
  52. {
  53. struct geo cgeo = c->geo;
  54. struct client *cc;
  55. int s = 0, cs = (LDIR(p) ? g->geo.h : g->geo.w);
  56. SLIST_FOREACH(cc, &c->tag->clients, tnext)
  57. if(GEO_PARENTROW(cgeo, cc->geo, RPOS(p))
  58. && GEO_CHECK_ROW(cc->geo, g->geo, p))
  59. {
  60. s += (LDIR(p) ? cc->geo.h : cc->geo.w);
  61. if(s == cs)
  62. return true;
  63. if(s > cs)
  64. return false;
  65. }
  66. return false;
  67. }
  68. /* Use ghost client properties to fix holes in tile
  69. *
  70. * ~ .--. ~ ~
  71. *_____ ~ /xx \ ~ ~
  72. * |>>| ~\O _ (____ ~
  73. * | |__.| .--'-==~ ~
  74. * |>>'---\ '. ~ , ~
  75. *__|__| '. '-.___.-'/ ~
  76. * '-.__ _.' ~
  77. * ````` ~
  78. */
  79. void
  80. layout_split_arrange_closed(struct client *ghost)
  81. {
  82. struct client *c, *cc;
  83. struct geo g;
  84. bool b = false;
  85. Position p;
  86. /* Search for single parent for easy resize
  87. * Example case:
  88. * ___________ ___________
  89. * | | B | -> -> | | |
  90. * | A |_____| -> Close -> | A | B |
  91. * | | C | -> C -> | |v v v|
  92. * |_____|_____| -> -> |_____|_____|
  93. */
  94. for(p = Right; p < Center; ++p) /* Check every direction */
  95. {
  96. if((c = client_next_with_pos(ghost, p)))
  97. if(GEO_CHECK2(ghost->geo, c->geo, p))
  98. {
  99. layout_split_arrange_size(ghost->geo, c, p);
  100. return;
  101. }
  102. }
  103. /* Check row parents for full resize
  104. * Example case:
  105. * ___________ ___________
  106. * | | B | -> -> | << B |
  107. * | A |_____| -> Close -> |___________|
  108. * | | C | -> A -> | << C |
  109. * |_____|_____| -> -> |___________|
  110. */
  111. for(p = Right; p < Center && !b; ++p)
  112. {
  113. if((c = client_next_with_pos(ghost, p))
  114. && layout_split_check_row_dir(c, ghost, p))
  115. {
  116. g = c->geo;
  117. SLIST_FOREACH(cc, &c->tag->clients, tnext)
  118. if(GEO_PARENTROW(g, cc->geo, RPOS(p))
  119. && GEO_CHECK_ROW(cc->geo, ghost->geo, p))
  120. {
  121. layout_split_arrange_size(ghost->geo, cc, p);
  122. b = true;
  123. }
  124. }
  125. }
  126. }
  127. /* Integrate a client in split layout: split sc and fill c in new geo */
  128. void
  129. layout_split_integrate(struct client *c, struct client *sc)
  130. {
  131. struct geo g;
  132. /* No sc */
  133. if(!sc || sc == c || sc->tag != c->tag)
  134. {
  135. /*
  136. * Not even a first client in list, then
  137. * maximize the lonely client
  138. */
  139. if(!(sc = SLIST_FIRST(&c->tag->clients)))
  140. {
  141. client_maximize(c);
  142. return;
  143. }
  144. }
  145. g = layout_split(sc, (sc->geo.h < sc->geo.w));
  146. client_moveresize(c, g);
  147. }
  148. /* Arrange inter-clients holes:
  149. * ___________ ___________
  150. * | || | -> | | |
  151. * | A || B | -> | A >| B |
  152. * | || | -> | >| |
  153. * |_____||____| -> |______|____|
  154. * ^ void
  155. *
  156. * and client-screen edge holes
  157. * ___________ ___________
  158. * | | || -> | | |
  159. * | A | B || -> | A | B >|
  160. * | | || -> | | >|
  161. * |_____|----'| -> |_____|__v__|
  162. * ^^^ void
  163. */
  164. static inline void
  165. layout_fix_hole(struct client *c)
  166. {
  167. struct client *cr = client_next_with_pos(c, Right);
  168. struct client *cb = client_next_with_pos(c, Bottom);
  169. c->geo.w += (cr ? cr->geo.x : c->screen->ugeo.w) - (c->geo.x + c->geo.w);
  170. c->geo.h += (cb ? cb->geo.y : c->screen->ugeo.h) - (c->geo.y + c->geo.h);
  171. client_moveresize(c, c->geo);
  172. }
  173. /* Layout rotation: Rotate 90° all client to right or left.
  174. * Avoid if(left) condition in layout_rotate loop; use func ptr
  175. *
  176. * Left rotation
  177. * ____________ ____________
  178. * | | B | -> | | A |
  179. * | A |_______| -> |__|_________|
  180. * |____| C | D | -> |_____| B |
  181. * |____|___|___| -> |_____|______|
  182. *
  183. * Right rotation
  184. * ____________ ____________
  185. * | | B | -> | B |_____|
  186. * | A |_______| -> |______|_____|
  187. * |____| C | D | -> | A | |
  188. * |____|___|___| -> |_________|__|
  189. *
  190. */
  191. static inline void
  192. _pos_rotate_left(struct geo *g, struct geo ug, struct geo og)
  193. {
  194. g->x = (ug.h - (og.y + og.h));
  195. g->y = og.x;
  196. }
  197. static inline void
  198. _pos_rotate_right(struct geo *g, struct geo ug, struct geo og)
  199. {
  200. g->x = og.y;
  201. g->y = (ug.w - (og.x + og.w));
  202. }
  203. static void
  204. layout_rotate(struct tag *t, bool left)
  205. {
  206. struct client *c;
  207. struct geo g;
  208. float f1 = (float)t->screen->ugeo.w / (float)t->screen->ugeo.h;
  209. float f2 = 1 / f1;
  210. void (*pos)(struct geo*, struct geo, struct geo) =
  211. (left ? _pos_rotate_left : _pos_rotate_right);
  212. SLIST_FOREACH(c, &t->clients, tnext)
  213. {
  214. pos(&g, t->screen->ugeo, c->geo);
  215. g.x *= f1;
  216. g.y *= f2;
  217. g.w = c->geo.h * f1;
  218. g.h = c->geo.w * f2;
  219. client_moveresize(c, g);
  220. }
  221. /* Rotate sometimes do not set back perfect size.. */
  222. SLIST_FOREACH(c, &t->clients, tnext)
  223. layout_fix_hole(c);
  224. }
  225. void
  226. uicb_layout_rotate_left(Uicb cmd)
  227. {
  228. layout_rotate(W->screen->seltag, true);
  229. }
  230. void
  231. uicb_layout_rotate_right(Uicb cmd)
  232. {
  233. layout_rotate(W->screen->seltag, false);
  234. }
  235. /*
  236. * Really simple functions, don't need static no-uicb backend
  237. * so we avoid the use of if(vertical) .. else
  238. *
  239. * Vertical mirror
  240. * ____________ ____________
  241. * | | B | -> | B | |
  242. * | A |_______| -> |_______| A |
  243. * | | C | D | -> | D | C | |
  244. * |____|___|___| -> |___|___|____|
  245. *
  246. * Horizontal mirror
  247. * ____________ ____________
  248. * | | B | -> | | C | D |
  249. * | A |_______| -> | A |___|___|
  250. * | | C | D | -> | | B |
  251. * |____|___|___| -> |____|_______|
  252. */
  253. void
  254. uicb_layout_vmirror(Uicb cmd)
  255. {
  256. struct client *c;
  257. SLIST_FOREACH(c, &W->screen->seltag->clients, tnext)
  258. {
  259. c->geo.x = W->screen->ugeo.w - (c->geo.x + c->geo.w);
  260. client_moveresize(c, c->geo);
  261. }
  262. }
  263. void
  264. uicb_layout_hmirror(Uicb cmd)
  265. {
  266. struct client *c;
  267. SLIST_FOREACH(c, &W->screen->seltag->clients, tnext)
  268. {
  269. c->geo.y = W->screen->ugeo.h - (c->geo.y + c->geo.h);
  270. client_moveresize(c, c->geo);
  271. }
  272. }