client.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  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>
  6. #include "client.h"
  7. #include "config.h"
  8. #include "event.h"
  9. #include "barwin.h"
  10. #include "draw.h"
  11. #include "screen.h"
  12. #include "mouse.h"
  13. #define CLIENT_RESIZE_DIR(D) \
  14. void uicb_client_resize_##D(Uicb cmd) \
  15. { \
  16. if(W->client) \
  17. client_fac_resize(W->client, D, ATOI(cmd)); \
  18. }
  19. #define CLIENT_ACTION_DIR(A, D) \
  20. void uicb_client_##A##_##D(Uicb cmd) \
  21. { \
  22. (void)cmd; \
  23. struct client *c; \
  24. if(W->client && (c = client_next_with_pos(W->client, D))) \
  25. client_##A(c); \
  26. }
  27. #define CLIENT_ACTION_IDIR(A, D) \
  28. void uicb_client_##A##_##D(Uicb cmd) \
  29. { \
  30. (void)cmd; \
  31. if(W->client) \
  32. client_##A(W->client, D); \
  33. }
  34. #define CLIENT_ACTION_LIST(A, L) \
  35. void uicb_client_##A##_##L(Uicb cmd) \
  36. { \
  37. (void)cmd; \
  38. struct client *c; \
  39. if(W->client && (c = client_##L(W->client))) \
  40. client_##A(c); \
  41. }
  42. /* uicb_client_resize_dir() */
  43. CLIENT_RESIZE_DIR(Right)
  44. CLIENT_RESIZE_DIR(Left)
  45. CLIENT_RESIZE_DIR(Top)
  46. CLIENT_RESIZE_DIR(Bottom)
  47. /* uicb_client_focus_dir() */
  48. CLIENT_ACTION_DIR(focus, Right)
  49. CLIENT_ACTION_DIR(focus, Left)
  50. CLIENT_ACTION_DIR(focus, Top)
  51. CLIENT_ACTION_DIR(focus, Bottom)
  52. /* uicb_client_tab_dir() */
  53. #define client_tab(c) _client_tab(W->client, c)
  54. CLIENT_ACTION_DIR(tab, Right)
  55. CLIENT_ACTION_DIR(tab, Left)
  56. CLIENT_ACTION_DIR(tab, Top)
  57. CLIENT_ACTION_DIR(tab, Bottom)
  58. /* uicb_client_swap_dir() */
  59. CLIENT_ACTION_IDIR(swap, Right)
  60. CLIENT_ACTION_IDIR(swap, Left)
  61. CLIENT_ACTION_IDIR(swap, Top)
  62. CLIENT_ACTION_IDIR(swap, Bottom)
  63. /* uicb_client_focus_next/prev() */
  64. CLIENT_ACTION_LIST(focus, next)
  65. CLIENT_ACTION_LIST(focus, prev)
  66. /* uicb_client_swapsel_next/prev() */
  67. #define client_swapsel(c) client_swap2(W->client, c)
  68. CLIENT_ACTION_LIST(swapsel, next)
  69. CLIENT_ACTION_LIST(swapsel, prev)
  70. /* uicb_client_focus_next/prev_tab() */
  71. CLIENT_ACTION_LIST(focus, next_tab)
  72. CLIENT_ACTION_LIST(focus, prev_tab)
  73. /** Send a ConfigureRequest event to the struct client
  74. * \param c struct client pointer
  75. */
  76. void
  77. client_configure(struct client *c)
  78. {
  79. XConfigureEvent ev =
  80. {
  81. .type = ConfigureNotify,
  82. .event = c->win,
  83. .window = c->win,
  84. .x = c->rgeo.x + c->border,
  85. .y = c->rgeo.y + c->tbarw,
  86. .width = c->wgeo.w,
  87. .height = c->wgeo.h,
  88. .above = None,
  89. .border_width = 0,
  90. .override_redirect = 0
  91. };
  92. XSendEvent(W->dpy, c->win, false, StructureNotifyMask, (XEvent *)&ev);
  93. XSync(W->dpy, false);
  94. }
  95. struct client*
  96. client_gb_win(Window w)
  97. {
  98. struct client *c = SLIST_FIRST(&W->h.client);
  99. while(c)
  100. {
  101. if(c->win == w)
  102. return c;
  103. c = SLIST_NEXT(c, next);
  104. }
  105. return NULL;
  106. }
  107. struct client*
  108. client_gb_frame(Window w)
  109. {
  110. struct client *c = SLIST_FIRST(&W->h.client);
  111. while(c)
  112. {
  113. if(c->frame == w)
  114. return c;
  115. c = SLIST_NEXT(c, next);
  116. }
  117. return NULL;
  118. }
  119. struct client*
  120. client_gb_pos(struct tag *t, int x, int y)
  121. {
  122. struct client *c;
  123. FOREACH_NFCLIENT(c, &t->clients, tnext)
  124. if(INAREA(x, y, c->geo))
  125. return c;
  126. return NULL;
  127. }
  128. struct client*
  129. client_gb_titlebar(Window w)
  130. {
  131. struct client *c = SLIST_FIRST(&W->h.client);
  132. if(!c->titlebar)
  133. return NULL;
  134. while(c && c->titlebar->win != w)
  135. {
  136. if(c->titlebar->win == w)
  137. return c;
  138. c = SLIST_NEXT(c, next);
  139. }
  140. return NULL;
  141. }
  142. /*
  143. * Get client left/right/top/bottom of selected client
  144. */
  145. struct client*
  146. client_next_with_pos(struct client *bc, enum position p)
  147. {
  148. struct client *c;
  149. static const char scanfac[PositionLast] = { +10, -10, 0, 0 };
  150. enum position ip = Bottom - p;
  151. int x = bc->geo.x + ((p == Right) ? bc->geo.w : 0);
  152. int y = bc->geo.y + ((p == Bottom) ? bc->geo.h : 0);
  153. if(p > Left)
  154. x += bc->geo.w >> 1;
  155. if(LDIR(p))
  156. y += bc->geo.h >> 1;
  157. /* Scan in right direction to next(p) physical client */
  158. while((c = client_gb_pos(bc->tag, x, y)) == bc)
  159. {
  160. x += scanfac[p];
  161. y += scanfac[ip];
  162. }
  163. return c;
  164. }
  165. #define FLAG_SWAP2(f1, f2, m1) \
  166. if((f1 & m1) != (f2 & m1)) \
  167. { \
  168. f1 ^= m1; \
  169. f2 ^= m1; \
  170. }
  171. #define SWAP_ARRANGE_TAB(C) \
  172. SLIST_FOREACH(c, &C->tag->clients, tnext) \
  173. { \
  174. if(c->tabmaster == C) \
  175. { \
  176. c->screen = C->screen; \
  177. if((C->flags & CLIENT_FREE) != (c->flags & CLIENT_FREE)) \
  178. { \
  179. c->flags ^= CLIENT_FREE; \
  180. c->flags ^= CLIENT_TILED; \
  181. } \
  182. } \
  183. }
  184. void
  185. client_swap2(struct client *c1, struct client *c2)
  186. {
  187. struct client *c;
  188. struct tag *t;
  189. struct geo g;
  190. /* Conflict / errors */
  191. if(c1 == c2 || !c1 || !c2)
  192. return;
  193. /* Reverse FREE/TILED flags if there are different */
  194. FLAG_SWAP2(c1->flags, c2->flags, CLIENT_FREE);
  195. FLAG_SWAP2(c1->flags, c2->flags, CLIENT_TILED);
  196. if(c1->screen != c2->screen)
  197. swap_ptr((void**)&c1->screen, (void**)&c2->screen);
  198. /*
  199. * Arrange flags for all tabbed client of
  200. * possible c1/c2 tabmaster
  201. */
  202. if(c1->flags & CLIENT_TABMASTER)
  203. SWAP_ARRANGE_TAB(c1);
  204. if(c2->flags & CLIENT_TABMASTER)
  205. SWAP_ARRANGE_TAB(c2);
  206. if(c1->tag != c2->tag)
  207. {
  208. c1->flags |= CLIENT_IGNORE_LAYOUT;
  209. c2->flags |= CLIENT_IGNORE_LAYOUT;
  210. t = c1->tag;
  211. tag_client(c2->tag, c1);
  212. tag_client(t, c2);
  213. }
  214. g = c1->geo;
  215. client_moveresize(c1, &c2->geo);
  216. client_moveresize(c2, &g);
  217. c1->flags |= CLIENT_IGNORE_ENTER;
  218. c2->flags |= CLIENT_IGNORE_ENTER;
  219. }
  220. static inline struct client*
  221. _swap_get(struct client *c, enum position p)
  222. {
  223. struct client *ret = client_next_with_pos(c, p);
  224. if(!ret)
  225. return c;
  226. return ret;
  227. }
  228. #define _REV_SBORDER() \
  229. draw_reversed_rect(W->root, c2, false);
  230. void
  231. client_swap(struct client *c, enum position p)
  232. {
  233. struct keybind *k;
  234. struct client *c2;
  235. bool b = true;
  236. XEvent ev;
  237. KeySym keysym;
  238. c2 = _swap_get(c, p);
  239. /* TODO
  240. if(option_simple_manual_resize)
  241. {
  242. _swap(c, c2);
  243. return;
  244. }
  245. */
  246. XGrabKeyboard(W->dpy, W->root, True, GrabModeAsync, GrabModeAsync, CurrentTime);
  247. _REV_SBORDER();
  248. do
  249. {
  250. XMaskEvent(W->dpy, KeyPressMask, &ev);
  251. if(ev.type == KeyPress)
  252. {
  253. XKeyPressedEvent *ke = &ev.xkey;
  254. keysym = XKeycodeToKeysym(W->dpy, (KeyCode)ke->keycode, 0);
  255. _REV_SBORDER();
  256. SLIST_FOREACH(k, &W->h.keybind, next)
  257. if(k->keysym == keysym && KEYPRESS_MASK(k->mod) == KEYPRESS_MASK(ke->state)
  258. && k->func)
  259. {
  260. if(k->func == uicb_client_swap_Right)
  261. c2 = _swap_get(c2, Right);
  262. else if(k->func == uicb_client_swap_Left)
  263. c2 = _swap_get(c2, Left);
  264. else if(k->func == uicb_client_swap_Top)
  265. c2 = _swap_get(c2, Top);
  266. else if(k->func == uicb_client_swap_Bottom)
  267. c2 = _swap_get(c2, Bottom);
  268. else
  269. {
  270. k->func(k->cmd);
  271. keysym = XK_Escape;
  272. }
  273. }
  274. _REV_SBORDER();
  275. /* Gtfo of this loop */
  276. if(keysym == XK_Return)
  277. break;
  278. else if(keysym == XK_Escape)
  279. {
  280. b = false;
  281. break;
  282. }
  283. XSync(W->dpy, False);
  284. }
  285. XNextEvent(W->dpy, &ev);
  286. } while(ev.type != KeyPress);
  287. _REV_SBORDER();
  288. if(b)
  289. client_swap2(c, c2);
  290. XUngrabKeyboard(W->dpy, CurrentTime);
  291. }
  292. static void
  293. client_grabbuttons(struct client *c, bool focused)
  294. {
  295. XUngrabButton(W->dpy, AnyButton, AnyModifier, c->win);
  296. if(focused)
  297. {
  298. int i = 0;
  299. while(i++ != Button5)
  300. {
  301. XGrabButton(W->dpy, i, W->client_mod, c->win, False,
  302. ButtonMask, GrabModeAsync, GrabModeSync, None, None);
  303. XGrabButton(W->dpy, i, W->client_mod | LockMask, c->win, False,
  304. ButtonMask, GrabModeAsync, GrabModeSync, None, None);
  305. XGrabButton(W->dpy, i, W->client_mod | W->numlockmask, c->win, False,
  306. ButtonMask, GrabModeAsync, GrabModeSync, None, None);
  307. XGrabButton(W->dpy, i, W->client_mod | LockMask | W->numlockmask, c->win, False,
  308. ButtonMask, GrabModeAsync, GrabModeSync, None, None);
  309. }
  310. return;
  311. }
  312. XGrabButton(W->dpy, AnyButton, AnyModifier, c->win, False,
  313. ButtonMask, GrabModeAsync, GrabModeSync, None, None);
  314. }
  315. #define _XTEXT() \
  316. if((xt = ((f >> 1) - (w >> 1))) < 0) \
  317. xt = c->border << 1;
  318. #define _REMAINDER() \
  319. if((rm = ((x + f) - (c->rgeo.w - c->border))) > 0) \
  320. f -= rm;
  321. #define _STATUSLINE(C, b) \
  322. do { \
  323. sctx = (b ? &c->theme->client_s_sl : &c->theme->client_n_sl); \
  324. sctx->barwin = C->titlebar; \
  325. status_copy_mousebind(sctx); \
  326. status_render(sctx); \
  327. if(C->flags & CLIENT_FREE) \
  328. { \
  329. sctx = &c->theme->client_f_sl; \
  330. sctx->barwin = C->titlebar; \
  331. status_copy_mousebind(sctx); \
  332. status_render(sctx); \
  333. } \
  334. } while(/* CONSTCOND */ 0);
  335. void
  336. client_frame_update(struct client *c, struct colpair *cp)
  337. {
  338. struct client *cc;
  339. struct status_ctx *sctx;
  340. int y, f, xt, rm, w, n = 1;
  341. if(c->flags & CLIENT_TABBED)
  342. c = c->tabmaster;
  343. XSetWindowBackground(W->dpy, c->frame, cp->bg);
  344. XClearWindow(W->dpy, c->frame);
  345. if(!c->titlebar || !c->title)
  346. return;
  347. c->titlebar->fg = cp->fg;
  348. c->titlebar->bg = cp->bg;
  349. /* Get number of tabbed client if c is tabmaster */
  350. if(c->flags & CLIENT_TABMASTER)
  351. {
  352. SLIST_FOREACH(cc, &c->tag->clients, tnext)
  353. if(c == cc->tabmaster)
  354. ++n;
  355. }
  356. f = (c->rgeo.w / n) + (c->border * 1.5);
  357. y = TEXTY(c->theme, c->tbarw);
  358. if(n == 1)
  359. {
  360. w = draw_textw(c->theme, c->title);
  361. _XTEXT();
  362. barwin_reparent(c->titlebar, c->frame);
  363. barwin_move(c->titlebar, 0, 0);
  364. barwin_resize(c->titlebar, f, c->tbarw);
  365. barwin_refresh_color(c->titlebar);
  366. _STATUSLINE(c, (cp == &c->scol));
  367. draw_text(c->titlebar->dr, c->theme, xt, y, cp->fg, c->title);
  368. barwin_refresh(c->titlebar);
  369. }
  370. /* Tabbing case, multiple titlebar in frame */
  371. else
  372. {
  373. struct geo g = { f - 1, 0, 1, c->titlebar->geo.h };
  374. int x = c->border;
  375. char *title;
  376. SLIST_FOREACH(cc, &c->tag->clients, tnext)
  377. {
  378. title = (cc->title ? cc->title : "WMFS");
  379. w = draw_textw(c->theme, title);
  380. _XTEXT();
  381. if(cc == c)
  382. {
  383. barwin_reparent(c->titlebar, c->frame);
  384. barwin_move(c->titlebar, x, 0);
  385. _REMAINDER();
  386. barwin_resize(c->titlebar, f, c->tbarw);
  387. barwin_refresh_color(c->titlebar);
  388. _STATUSLINE(c, true);
  389. draw_rect(c->titlebar->dr, &g, c->scol.bg);
  390. draw_text(c->titlebar->dr, c->theme, xt, y, cp->fg, title);
  391. barwin_refresh(c->titlebar);
  392. x += f;
  393. }
  394. if(cc->tabmaster == c)
  395. {
  396. barwin_reparent(cc->titlebar, c->frame);
  397. barwin_map(cc->titlebar);
  398. barwin_move(cc->titlebar, x, 1);
  399. _REMAINDER();
  400. barwin_resize(cc->titlebar, f, c->tbarw - 2);
  401. barwin_refresh_color(cc->titlebar);
  402. _STATUSLINE(cc, false);
  403. draw_rect(cc->titlebar->dr, &g, c->scol.bg);
  404. draw_text(cc->titlebar->dr, c->theme, xt, y - 1, c->ncol.fg, title);
  405. barwin_refresh(cc->titlebar);
  406. x += f;
  407. }
  408. }
  409. }
  410. }
  411. void
  412. client_tab_focus(struct client *c)
  413. {
  414. if(c->flags & CLIENT_TABBED && c->tabmaster)
  415. {
  416. struct client *cc;
  417. struct geo g = c->tabmaster->geo;
  418. c->flags |= CLIENT_TABMASTER;
  419. c->flags &= ~CLIENT_TABBED;
  420. client_moveresize(c, &c->tabmaster->geo);
  421. if(c->tag == c->screen->seltag)
  422. client_map(c);
  423. c->tabmaster->flags &= ~CLIENT_TABMASTER;
  424. c->tabmaster->flags |= CLIENT_TABBED;
  425. client_unmap(c->tabmaster);
  426. if(!(c->flags & CLIENT_DYING))
  427. {
  428. g.x += W->xmaxw;
  429. g.y += W->xmaxh;
  430. c->tabmaster->geo = c->tabmaster->tgeo = g;
  431. }
  432. /* Update tabmaster of tabbed client */
  433. SLIST_FOREACH(cc, &c->tag->clients, tnext)
  434. if(cc != c && cc->tabmaster == c->tabmaster)
  435. {
  436. cc->tabmaster = c;
  437. client_update_props(cc, CPROP_TAB);
  438. }
  439. c->tabmaster->tabmaster = c;
  440. client_update_props(c->tabmaster, CPROP_TAB);
  441. c->tabmaster = NULL;
  442. client_update_props(c, CPROP_TAB);
  443. }
  444. }
  445. void
  446. _client_tab(struct client *c, struct client *cm)
  447. {
  448. Flags m[2] = { CLIENT_TILED, CLIENT_FREE };
  449. /* Do not tab already tabbed client */
  450. if(c->flags & (CLIENT_TABBED | CLIENT_TABMASTER)
  451. || c->tag != cm->tag || c == cm)
  452. return;
  453. layout_split_arrange_closed(c);
  454. cm->flags |= CLIENT_TABBED;
  455. c->geo = cm->geo;
  456. cm->tabmaster = c;
  457. if(cm->flags & CLIENT_FREE)
  458. swap_int((int*)&m[0], (int*)&m[1]);
  459. c->flags |= m[0];
  460. c->flags &= ~m[1];
  461. client_focus(cm);
  462. }
  463. static void
  464. client_untab(struct client *c)
  465. {
  466. struct client *ct, *cc = c->tabmaster;
  467. struct geo og = c->geo;
  468. bool chk = false;
  469. if(c->flags & CLIENT_REMOVEALL || !(c->flags & (CLIENT_TABBED | CLIENT_TABMASTER)))
  470. return;
  471. if(!cc)
  472. {
  473. SLIST_FOREACH(cc, &c->tag->clients, tnext)
  474. if(cc->tabmaster == c)
  475. break;
  476. }
  477. if(cc)
  478. {
  479. client_tab_focus(cc);
  480. c->flags &= ~CLIENT_TABBED;
  481. c->flags |= CLIENT_IGNORE_ENTER;
  482. c->tabmaster = NULL;
  483. /* Looking for tabbed client in cc, if there is not
  484. * remove cc CLIENT_TABMASTER flag.
  485. */
  486. SLIST_FOREACH(ct, &c->tag->clients, tnext)
  487. if(ct->tabmaster == cc)
  488. {
  489. chk = true;
  490. break;
  491. }
  492. if(!chk)
  493. cc->flags &= ~CLIENT_TABMASTER;
  494. if(!(c->flags & CLIENT_DYING))
  495. {
  496. c->geo = c->tgeo = og;
  497. c->tag->sel = cc;
  498. layout_client(c);
  499. client_map(c);
  500. client_moveresize(c, &c->geo);
  501. client_update_props(c, CPROP_TAB);
  502. }
  503. client_frame_update(cc, CCOL(cc));
  504. }
  505. }
  506. void
  507. uicb_client_untab(Uicb cmd)
  508. {
  509. (void)cmd;
  510. if(W->client)
  511. client_untab(W->client);
  512. }
  513. void
  514. client_focus(struct client *c)
  515. {
  516. /* Unfocus selected */
  517. if(W->client && W->client != c)
  518. {
  519. client_grabbuttons(W->client, false);
  520. client_frame_update(W->client, &W->client->ncol);
  521. }
  522. /* Focus c */
  523. if((W->client = c))
  524. {
  525. c->tag->sel = c;
  526. client_grabbuttons(c, true);
  527. client_tab_focus(c);
  528. client_frame_update(c, CCOL(c));
  529. if(c->flags & CLIENT_FREE
  530. && !(c->flags & (CLIENT_FULLSCREEN | CLIENT_TABBED)))
  531. {
  532. c->tag->flags |= CLIENT_IGNORE_ENTER;
  533. XRaiseWindow(W->dpy, c->frame);
  534. }
  535. XSetInputFocus(W->dpy, c->win, RevertToPointerRoot, CurrentTime);
  536. XChangeProperty(W->dpy, W->root, W->net_atom[net_active_window], XA_WINDOW, 32,
  537. PropModeReplace, (unsigned char *)&c->win, 1);
  538. }
  539. else
  540. {
  541. W->client = W->screen->seltag->sel = NULL;
  542. XSetInputFocus(W->dpy, W->root, RevertToPointerRoot, CurrentTime);
  543. }
  544. ewmh_update_wmfs_props();
  545. }
  546. void
  547. uicb_client_focus_click(Uicb cmd)
  548. {
  549. (void)cmd;
  550. struct client *c;
  551. if((c = client_gb_titlebar(W->last_clicked_barwin->win))
  552. || (c = client_gb_frame(W->last_clicked_barwin->win)))
  553. client_focus(c);
  554. }
  555. /** Get a client name
  556. * \param c struct client pointer
  557. */
  558. void
  559. client_get_name(struct client *c)
  560. {
  561. Atom rt;
  562. int rf;
  563. unsigned long ir, il;
  564. /* This one instead XFetchName for utf8 name support */
  565. if(XGetWindowProperty(W->dpy, c->win, W->net_atom[net_wm_name], 0, 65536,
  566. False, W->net_atom[utf8_string], &rt, &rf, &ir, &il, (unsigned char**)&c->title) != Success)
  567. XGetWindowProperty(W->dpy, c->win, W->net_atom[net_wm_name], 0, 65536,
  568. False, W->net_atom[utf8_string], &rt, &rf, &ir, &il, (unsigned char**)&c->title);
  569. /* Still no title... */
  570. if(!c->title)
  571. XFetchName(W->dpy, c->win, &(c->title));
  572. client_frame_update(c, CCOL(c));
  573. }
  574. /** Close a client
  575. * \param c struct client pointer
  576. */
  577. void
  578. client_close(struct client *c)
  579. {
  580. int proto;
  581. XEvent ev;
  582. Atom *atom = NULL;
  583. /* Event will call client_remove */
  584. if(XGetWMProtocols(W->dpy, c->win, &atom, &proto) && atom)
  585. {
  586. while(proto--)
  587. if(atom[proto] == ATOM("WM_DELETE_WINDOW"))
  588. {
  589. ev.type = ClientMessage;
  590. ev.xclient.window = c->win;
  591. ev.xclient.message_type = ATOM("WM_PROTOCOLS");
  592. ev.xclient.format = 32;
  593. ev.xclient.data.l[0] = ATOM("WM_DELETE_WINDOW");
  594. ev.xclient.data.l[1] = CurrentTime;
  595. XSendEvent(W->dpy, c->win, False, NoEventMask, &ev);
  596. XFree(atom);
  597. return;
  598. }
  599. }
  600. XKillClient(W->dpy, c->win);
  601. }
  602. void
  603. uicb_client_close(Uicb cmd)
  604. {
  605. (void)cmd;
  606. if(W->client)
  607. client_close(W->client);
  608. }
  609. void
  610. client_get_sizeh(struct client *c)
  611. {
  612. long msize;
  613. XSizeHints size;
  614. memset(c->sizeh, 0, SHLAST);
  615. if(!XGetWMNormalHints(W->dpy, c->win, &size, &msize) || !size.flags)
  616. size.flags = PSize;
  617. /* base */
  618. if(size.flags & PBaseSize)
  619. {
  620. c->sizeh[BASEW] = size.base_width;
  621. c->sizeh[BASEH] = size.base_height;
  622. }
  623. else if(size.flags & PMinSize)
  624. {
  625. c->sizeh[BASEW] = size.min_width;
  626. c->sizeh[BASEH] = size.min_height;
  627. }
  628. /* inc */
  629. if(size.flags & PResizeInc)
  630. {
  631. c->sizeh[INCW] = size.width_inc;
  632. c->sizeh[INCH] = size.height_inc;
  633. }
  634. /* max */
  635. if(size.flags & PMaxSize)
  636. {
  637. c->sizeh[MAXW] = size.max_width;
  638. c->sizeh[MAXH] = size.max_height;
  639. }
  640. /* min */
  641. if(size.flags & PMinSize)
  642. {
  643. c->sizeh[MINW] = (size.min_width ? size.min_width : 1);
  644. c->sizeh[MINH] = (size.min_height ? size.min_height: 1);
  645. }
  646. else if(size.flags & PBaseSize)
  647. {
  648. c->sizeh[MINW] = (size.base_width ? size.base_width : 1);
  649. c->sizeh[MINH] = (size.base_height ? size.base_height : 1);
  650. }
  651. /* aspect */
  652. if(size.flags & PAspect)
  653. {
  654. c->sizeh[MINAX] = size.min_aspect.x;
  655. c->sizeh[MAXAX] = size.max_aspect.x;
  656. c->sizeh[MINAY] = size.min_aspect.y;
  657. c->sizeh[MAXAY] = size.max_aspect.y;
  658. }
  659. if(c->sizeh[MAXW] && c->sizeh[MINW] && c->sizeh[MAXH] && c->sizeh[MINH]
  660. && c->sizeh[MAXW] == c->sizeh[MINW] && c->sizeh[MAXH] == c->sizeh[MINH])
  661. c->flags |= CLIENT_HINT_FLAG;
  662. }
  663. static void
  664. client_frame_new(struct client *c)
  665. {
  666. struct barwin *frameb;
  667. struct barwin *clientb;
  668. XSetWindowAttributes at =
  669. {
  670. .background_pixel = c->ncol.bg,
  671. .override_redirect = true,
  672. .background_pixmap = ParentRelative,
  673. .event_mask = BARWIN_MASK | BARWIN_ENTERMASK
  674. };
  675. /* Use a fake barwin only to store mousebinds of frame win */
  676. frameb = barwin_new(W->root, 0, 0, 1, 1, 0, 0, false);
  677. clientb = barwin_new(W->root, 0, 0, 1, 1, 0, 0, false);
  678. frameb->win =
  679. c->frame = XCreateWindow(W->dpy, W->root,
  680. c->geo.x, c->geo.y,
  681. c->geo.w, c->geo.h,
  682. 0, CopyFromParent,
  683. InputOutput,
  684. CopyFromParent,
  685. (CWOverrideRedirect | CWBackPixmap
  686. | CWBackPixel | CWEventMask), &at);
  687. clientb->win = c->win;
  688. frameb->mousebinds = W->tmp_head.client;
  689. clientb->mousebinds = W->tmp_head.client;
  690. if(c->tbarw > c->border)
  691. {
  692. c->titlebar = barwin_new(c->frame, 0, 0, 1, c->tbarw,
  693. c->ncol.fg, c->ncol.bg, true);
  694. c->titlebar->mousebinds = W->tmp_head.client;
  695. }
  696. XReparentWindow(W->dpy, c->win, c->frame, c->border, c->tbarw);
  697. }
  698. #define RINSTANCE 0x01
  699. #define RCLASS 0x02
  700. #define RROLE 0x04
  701. #define RNAME 0x08
  702. static void
  703. client_apply_rule(struct client *c)
  704. {
  705. struct rule *r;
  706. char *wmname = NULL;
  707. char *role = NULL;
  708. int f;
  709. unsigned char *data = NULL;
  710. unsigned long n, il;
  711. Flags flags = 0;
  712. Atom rf;
  713. XClassHint xch;
  714. Status s = XGetClassHint(W->dpy, c->win, &xch);
  715. /* Get WM_WINDOW_ROLE */
  716. if(XGetWindowProperty(W->dpy, c->win, ATOM("WM_WINDOW_ROLE"), 0L, 0x7FFFFFFFL, false,
  717. XA_STRING, &rf, &f, &n, &il, &data)
  718. == Success && data)
  719. {
  720. role = xstrdup((char*)data);
  721. XFree(data);
  722. }
  723. /* Get _NET_WM_NAME */
  724. if(XGetWindowProperty(W->dpy, c->win, W->net_atom[net_wm_name], 0, 0x77777777, false,
  725. W->net_atom[utf8_string], &rf, &f, &n, &il, &data)
  726. == Success && data)
  727. {
  728. wmname = xstrdup((char*)data);
  729. XFree(data);
  730. }
  731. SLIST_FOREACH(r, &W->h.rule, next)
  732. {
  733. if(s)
  734. {
  735. FLAGAPPLY(flags, (xch.res_name && r->instance && !strcmp(xch.res_name, r->instance)), RINSTANCE);
  736. FLAGAPPLY(flags, (xch.res_class && r->class && !strcmp(xch.res_class, r->class)), RCLASS);
  737. }
  738. FLAGAPPLY(flags, (wmname && r->name && !strcmp(wmname, r->name)), RNAME);
  739. FLAGAPPLY(flags, ((role && r->role && !strcmp(role, r->role)) || !role || !r->role), RROLE);
  740. if(flags & (RINSTANCE | RCLASS | RNAME) && flags & RROLE)
  741. {
  742. if(r->screen != -1)
  743. c->screen = screen_gb_id(r->screen);
  744. c->tag = c->screen->seltag;
  745. if(r->tag != -1)
  746. c->tag = tag_gb_id(c->screen, r->tag);
  747. c->theme = r->theme;
  748. /* free = false for originally free client */
  749. if(r->flags & RULE_FREE)
  750. c->flags |= CLIENT_FREE;
  751. else
  752. c->flags &= ~CLIENT_FREE;
  753. /* Free rule is not compatible with tab rule */
  754. if(r->flags & RULE_TAB)
  755. W->flags ^= WMFS_TABNOC; /* < can be disable by client_tab_next_opened */
  756. /* TODO
  757. if(r->flags & RULE_IGNORE_TAG)
  758. {}
  759. */
  760. c->flags |= CLIENT_RULED;
  761. }
  762. flags = 0;
  763. }
  764. if(role)
  765. free(role);
  766. if(wmname)
  767. free(wmname);
  768. }
  769. struct client*
  770. client_new(Window w, XWindowAttributes *wa, bool scan)
  771. {
  772. struct client *c = xcalloc(1, sizeof(struct client));
  773. /* C attributes */
  774. c->win = w;
  775. c->flags = 0;
  776. c->screen = screen_update_sel();
  777. c->theme = W->ctheme;
  778. c->tag = NULL;
  779. c->tabmaster = NULL;
  780. /* struct geometry */
  781. c->geo.x = wa->x;
  782. c->geo.y = wa->y;
  783. c->geo.w = wa->width;
  784. c->geo.h = wa->height;
  785. c->tgeo = c->wgeo = c->rgeo = c->geo;
  786. c->tbgeo = NULL;
  787. client_get_sizeh(c);
  788. if(!scan)
  789. {
  790. if(c->flags & CLIENT_HINT_FLAG /* && OPTIONKIVABIEN */)
  791. c->flags |= CLIENT_FREE;
  792. client_apply_rule(c);
  793. }
  794. /*
  795. * Conf option set per client, for possibility
  796. * to config only one client
  797. */
  798. c->border = c->theme->client_border_width;
  799. if(!(c->tbarw = c->theme->client_titlebar_width))
  800. c->tbarw = c->border;
  801. c->ncol = c->theme->client_n;
  802. c->scol = c->theme->client_s;
  803. client_frame_new(c);
  804. if(!scan)
  805. {
  806. ewmh_manage_window_type(c);
  807. tag_client((c->flags & CLIENT_RULED ? c->tag : c->screen->seltag), c);
  808. }
  809. /* Map, not at reload */
  810. if(c->tag == c->screen->seltag)
  811. client_map(c);
  812. /* X window attributes */
  813. XSelectInput(W->dpy, w, EnterWindowMask | LeaveWindowMask | StructureNotifyMask | PropertyChangeMask);
  814. XSetWindowBorderWidth(W->dpy, w, 0);
  815. client_grabbuttons(c, false);
  816. /* Attach */
  817. SLIST_INSERT_HEAD(&W->h.client, c, next);
  818. if(!scan)
  819. {
  820. client_get_name(c);
  821. client_focus(c);
  822. client_configure(c);
  823. }
  824. ewmh_get_client_list();
  825. return c;
  826. }
  827. void
  828. client_update_props(struct client *c, Flags f)
  829. {
  830. if(f & CPROP_LOC)
  831. {
  832. XChangeProperty(W->dpy, c->win, ATOM("_WMFS_TAG"), XA_CARDINAL, 32,
  833. PropModeReplace, (unsigned char*)&(c->tag->id), 1);
  834. XChangeProperty(W->dpy, c->win, ATOM("_WMFS_SCREEN"), XA_CARDINAL, 32,
  835. PropModeReplace, (unsigned char*)&(c->screen->id), 1);
  836. }
  837. if(f & CPROP_FLAG)
  838. XChangeProperty(W->dpy, c->win, ATOM("_WMFS_FLAGS"), XA_CARDINAL, 32,
  839. PropModeReplace, (unsigned char*)&(c->flags), 1);
  840. if(f & CPROP_GEO)
  841. {
  842. long g[4] = { (long)c->geo.x, (long)c->geo.y, (long)c->geo.w, (long)c->geo.h };
  843. XChangeProperty(W->dpy, c->win, ATOM("_WMFS_GEO"), XA_CARDINAL, 32,
  844. PropModeReplace, (unsigned char*)g, 4);
  845. }
  846. if(f & CPROP_TAB)
  847. {
  848. Window w = (c->tabmaster ? c->tabmaster->win : 0);
  849. XChangeProperty(W->dpy, c->win, ATOM("_WMFS_TABMASTER"), XA_WINDOW, 32,
  850. PropModeReplace, (unsigned char*)&w, 1);
  851. }
  852. }
  853. void
  854. client_geo_hints(struct geo *g, int *s)
  855. {
  856. /* base */
  857. g->w -= s[BASEW];
  858. g->h -= s[BASEH];
  859. /* aspect */
  860. if((s[MINAY] | s[MAXAY] | s[MINAX] | s[MAXAX]) > 0)
  861. {
  862. if(g->w * s[MAXAY] > g->h * s[MAXAX])
  863. g->w = g->h * s[MAXAX] / s[MAXAY];
  864. else if(g->w * s[MINAY] < g->h * s[MINAX])
  865. g->h = g->w * s[MINAY] / s[MINAX];
  866. }
  867. /* incremental */
  868. if(s[INCW])
  869. g->w -= g->w % s[INCW];
  870. if(s[INCH])
  871. g->h -= g->h % s[INCH];
  872. /* base dimension */
  873. g->w += s[BASEW];
  874. g->h += s[BASEH];
  875. if(s[MINW] > 0 && g->w < s[MINW])
  876. g->w = s[MINW];
  877. if(s[MINH] > 0 && g->h < s[MINH])
  878. g->h = s[MINH];
  879. if(s[MAXW] > 0 && g->w > s[MAXW])
  880. g->w = s[MAXW];
  881. if(s[MAXH] > 0 && g->h > s[MAXH])
  882. g->h = s[MAXH];
  883. }
  884. /* Manage window size in frame in tiling mode */
  885. bool
  886. client_winsize(struct client *c, struct geo *g)
  887. {
  888. int ow, oh;
  889. struct geo og = c->wgeo;
  890. /* Window geo */
  891. c->wgeo.x = c->border;
  892. c->wgeo.y = c->tbarw;
  893. c->wgeo.h = oh = g->h - (c->border + c->tbarw);
  894. c->wgeo.w = ow = g->w - (c->border << 1);
  895. client_geo_hints(&c->wgeo, (int*)c->sizeh);
  896. /* Check possible problem for tile integration */
  897. if(ow < c->sizeh[MINW] || oh < c->sizeh[MINH])
  898. if(g->w < c->geo.w || g->h < c->geo.h)
  899. {
  900. c->wgeo = og;
  901. return true;
  902. }
  903. /* Balance position with new size */
  904. c->wgeo.x += (ow - c->wgeo.w) >> 1;
  905. c->flags |= CLIENT_DID_WINSIZE;
  906. return false;
  907. }
  908. void
  909. client_moveresize(struct client *c, struct geo *g)
  910. {
  911. if(c->flags & CLIENT_TABBED)
  912. return;
  913. /* Adjust frame regarding window required size */
  914. if(c->flags & CLIENT_FREE)
  915. {
  916. g->w -= c->border + c->border;
  917. g->h -= c->tbarw + c->border;
  918. client_geo_hints(g, (int*)c->sizeh);
  919. c->wgeo = c->geo = c->rgeo = *g;
  920. c->wgeo.x = c->border;
  921. c->wgeo.y = c->tbarw;
  922. c->geo.w = c->rgeo.w = c->wgeo.w + c->border + c->border;
  923. c->geo.h = c->rgeo.h = c->wgeo.h + c->tbarw + c->border;
  924. c->rgeo.x += c->screen->ugeo.x;
  925. c->rgeo.y += c->screen->ugeo.y;
  926. if(!INAREA(c->rgeo.x, c->rgeo.y, c->screen->ugeo))
  927. {
  928. /* New screen (moved by mouse) */
  929. if(c->flags & CLIENT_MOUSE)
  930. {
  931. c->flags |= CLIENT_IGNORE_LAYOUT;
  932. c->screen = screen_gb_geo(c->rgeo.x, c->rgeo.y);
  933. tag_client(c->screen->seltag, c);
  934. c->geo.x = c->rgeo.x - c->screen->ugeo.x;
  935. c->geo.y = c->rgeo.y - c->screen->ugeo.y;
  936. }
  937. /* Out of the screen case */
  938. else
  939. {
  940. c->geo.x = (c->screen->ugeo.w >> 1) - (c->geo.w >> 1);
  941. c->geo.y = (c->screen->ugeo.h >> 1) - (c->geo.h >> 1);
  942. c->rgeo.x = c->screen->ugeo.x + c->geo.x;
  943. c->rgeo.y = c->screen->ugeo.y + c->geo.y;
  944. }
  945. }
  946. }
  947. /* Adjust window regarding required size for frame (tiling) */
  948. else
  949. {
  950. c->ttgeo = c->tgeo = c->rgeo = c->geo = *g;
  951. if(!(c->flags & CLIENT_DID_WINSIZE))
  952. client_winsize(c, g);
  953. c->rgeo.x += c->screen->ugeo.x;
  954. c->rgeo.y += c->screen->ugeo.y;
  955. }
  956. XMoveResizeWindow(W->dpy, c->frame,
  957. c->rgeo.x, c->rgeo.y,
  958. c->rgeo.w, c->rgeo.h);
  959. if(!(c->flags & CLIENT_FULLSCREEN))
  960. XMoveResizeWindow(W->dpy, c->win,
  961. c->wgeo.x, c->wgeo.y,
  962. c->wgeo.w, c->wgeo.h);
  963. c->flags &= ~CLIENT_DID_WINSIZE;
  964. client_frame_update(c, CCOL(c));
  965. client_update_props(c, CPROP_GEO);
  966. client_configure(c);
  967. }
  968. void
  969. client_maximize(struct client *c)
  970. {
  971. c->geo.x = c->geo.y = 0;
  972. c->geo.w = c->screen->ugeo.w;
  973. c->geo.h = c->screen->ugeo.h;
  974. client_moveresize(c, &c->geo);
  975. layout_save_set(c->tag);
  976. }
  977. /*
  978. * Client factor resize: allow clients to be resized in
  979. * manual tile layout.
  980. */
  981. static inline void
  982. _fac_apply(struct client *c, enum position p, int fac)
  983. {
  984. switch(p)
  985. {
  986. case Top:
  987. c->tgeo.y -= fac;
  988. case Bottom:
  989. c->tgeo.h += fac;
  990. break;
  991. case Left:
  992. c->tgeo.x -= fac;
  993. default:
  994. case Right:
  995. c->tgeo.w += fac;
  996. break;
  997. }
  998. c->flags |= (CLIENT_IGNORE_ENTER | CLIENT_FAC_APPLIED);
  999. }
  1000. static inline void
  1001. _fac_arrange_row(struct client *c, enum position p, int fac)
  1002. {
  1003. struct geo g = c->tgeo;
  1004. struct client *cc;
  1005. /* Travel clients to search row parents and apply fac */
  1006. FOREACH_NFCLIENT(cc, &c->tag->clients, tnext)
  1007. if(GEO_PARENTROW(g, cc->tgeo, p))
  1008. _fac_apply(cc, p, fac);
  1009. }
  1010. static inline void
  1011. _fac_check_to_reverse(struct client *c)
  1012. {
  1013. struct client *gc, *cc;
  1014. /*
  1015. * Check if every clients are compatible with
  1016. * future globals geo. Problem here is that we must *not*
  1017. * resize client because of possible error with next
  1018. * clients in linked list.
  1019. */
  1020. FOREACH_NFCLIENT(gc, &c->tag->clients, tnext)
  1021. if(gc->flags & CLIENT_FAC_APPLIED
  1022. && client_winsize(gc, &gc->tgeo))
  1023. {
  1024. /*
  1025. * Reverse back the flag and the window geo
  1026. * in previous affected clients
  1027. */
  1028. FOREACH_NFCLIENT(cc, &c->tag->clients, tnext)
  1029. {
  1030. cc->tgeo = cc->ttgeo;
  1031. cc->flags &= ~CLIENT_DID_WINSIZE;
  1032. }
  1033. return;
  1034. }
  1035. }
  1036. void
  1037. _fac_resize(struct client *c, enum position p, int fac)
  1038. {
  1039. struct client *cc, *gc = client_next_with_pos(c, p);
  1040. enum position rp = RPOS(p);
  1041. if(!gc || gc->screen != c->screen || !(c->flags & CLIENT_TILED))
  1042. return;
  1043. FOREACH_NFCLIENT(cc, &c->tag->clients, tnext)
  1044. cc->ttgeo = cc->tgeo;
  1045. if(GEO_CHECK2(c->tgeo, gc->tgeo, p))
  1046. {
  1047. _fac_apply(c, p, fac);
  1048. _fac_apply(gc, rp, -fac);
  1049. }
  1050. else
  1051. {
  1052. _fac_arrange_row(c, p, fac);
  1053. _fac_arrange_row(gc, rp, -fac);
  1054. }
  1055. _fac_check_to_reverse(c);
  1056. }
  1057. void
  1058. client_apply_tgeo(struct tag *t)
  1059. {
  1060. struct client *c;
  1061. FOREACH_NFCLIENT(c, &t->clients, tnext)
  1062. {
  1063. client_moveresize(c, &c->tgeo);
  1064. c->flags &= ~CLIENT_FAC_APPLIED;
  1065. }
  1066. }
  1067. #define _REV_BORDER() \
  1068. do { \
  1069. FOREACH_NFCLIENT(gc, &c->tag->clients, tnext) \
  1070. draw_reversed_rect(W->root, gc, true); \
  1071. } while(/* CONSTCOND */ 0);
  1072. void
  1073. client_fac_resize(struct client *c, enum position p, int fac)
  1074. {
  1075. struct keybind *k;
  1076. struct client *gc;
  1077. bool b = true;
  1078. XEvent ev;
  1079. KeySym keysym;
  1080. /* Do it once before */
  1081. _fac_resize(c, p, fac);
  1082. /* TODO
  1083. if(option_simple_manual_resize)
  1084. return;
  1085. */
  1086. XGrabServer(W->dpy);
  1087. XGrabKeyboard(W->dpy, W->root, True, GrabModeAsync, GrabModeAsync, CurrentTime);
  1088. _REV_BORDER();
  1089. do
  1090. {
  1091. XMaskEvent(W->dpy, KeyPressMask, &ev);
  1092. if(ev.type == KeyPress)
  1093. {
  1094. XKeyPressedEvent *ke = &ev.xkey;
  1095. keysym = XKeycodeToKeysym(W->dpy, (KeyCode)ke->keycode, 0);
  1096. _REV_BORDER();
  1097. SLIST_FOREACH(k, &W->h.keybind, next)
  1098. if(k->keysym == keysym && KEYPRESS_MASK(k->mod) == KEYPRESS_MASK(ke->state)
  1099. && k->func)
  1100. {
  1101. if(k->func == uicb_client_resize_Right)
  1102. _fac_resize(c, Right, ATOI(k->cmd));
  1103. else if(k->func == uicb_client_resize_Left)
  1104. _fac_resize(c, Left, ATOI(k->cmd));
  1105. else if(k->func == uicb_client_resize_Top)
  1106. _fac_resize(c, Top, ATOI(k->cmd));
  1107. else if(k->func == uicb_client_resize_Bottom)
  1108. _fac_resize(c, Bottom, ATOI(k->cmd));
  1109. else
  1110. {
  1111. k->func(k->cmd);
  1112. keysym = XK_Escape;
  1113. }
  1114. }
  1115. _REV_BORDER();
  1116. /* Gtfo of this loop */
  1117. if(keysym == XK_Return)
  1118. break;
  1119. else if(keysym == XK_Escape)
  1120. {
  1121. b = false;
  1122. break;
  1123. }
  1124. XSync(W->dpy, False);
  1125. }
  1126. XNextEvent(W->dpy, &ev);
  1127. } while(ev.type != KeyPress);
  1128. _REV_BORDER();
  1129. /* Success, resize clients */
  1130. if(b)
  1131. {
  1132. client_apply_tgeo(c->tag);
  1133. layout_save_set(c->tag);
  1134. }
  1135. /* Aborted with escape, Set back original geos */
  1136. else
  1137. {
  1138. FOREACH_NFCLIENT(gc, &c->tag->clients, tnext)
  1139. {
  1140. gc->tgeo = gc->geo;
  1141. gc->flags &= ~CLIENT_DID_WINSIZE;
  1142. }
  1143. }
  1144. XUngrabServer(W->dpy);
  1145. XUngrabKeyboard(W->dpy, CurrentTime);
  1146. }
  1147. void
  1148. client_fac_hint(struct client *c)
  1149. {
  1150. int w = c->sizeh[MINW] + c->border + c->border;
  1151. int h = c->sizeh[MINH] + c->tbarw + c->border;
  1152. if(c->geo.h < h)
  1153. _fac_resize(c, Top, (h - c->geo.h));
  1154. if(c->tgeo.h < h)
  1155. _fac_resize(c, Bottom, (h - c->tgeo.h));
  1156. if(c->geo.w < w)
  1157. _fac_resize(c, Left, (w - c->geo.w));
  1158. if(c->tgeo.w < w)
  1159. _fac_resize(c, Right, (w - c->tgeo.w));
  1160. client_apply_tgeo(c->tag);
  1161. }
  1162. void
  1163. client_remove(struct client *c)
  1164. {
  1165. c->flags |= CLIENT_DYING;
  1166. client_untab(c);
  1167. XGrabServer(W->dpy);
  1168. XSetErrorHandler(wmfs_error_handler_dummy);
  1169. XReparentWindow(W->dpy, c->win, W->root, c->rgeo.x, c->rgeo.y);
  1170. XUngrabButton(W->dpy, AnyButton, AnyModifier, c->win);
  1171. ewmh_set_wm_state(c->win, WithdrawnState);
  1172. XSync(W->dpy, false);
  1173. XSetErrorHandler(wmfs_error_handler);
  1174. XUngrabServer(W->dpy);
  1175. SLIST_REMOVE(&W->h.client, c, client, next);
  1176. tag_client(NULL, c);
  1177. /* Remove frame */
  1178. if(c->titlebar)
  1179. barwin_remove(c->titlebar);
  1180. XDestroyWindow(W->dpy, c->frame);
  1181. free(c);
  1182. ewmh_get_client_list();
  1183. }
  1184. void
  1185. uicb_client_toggle_free(Uicb cmd)
  1186. {
  1187. struct client *c;
  1188. (void)cmd;
  1189. if(!(W->client))
  1190. return;
  1191. W->client->flags ^= CLIENT_FREE;
  1192. layout_client(W->client);
  1193. /* Set tabbed client of toggled client as free */
  1194. if(W->client->flags & CLIENT_TABMASTER)
  1195. {
  1196. SLIST_FOREACH(c, &W->client->tag->clients, tnext)
  1197. if(c->tabmaster == W->client && c != W->client)
  1198. c->flags ^= CLIENT_FREE;
  1199. }
  1200. }
  1201. void
  1202. uicb_client_tab_next_opened(Uicb cmd)
  1203. {
  1204. (void)cmd;
  1205. W->flags ^= WMFS_TABNOC;
  1206. }
  1207. void
  1208. client_free(void)
  1209. {
  1210. FREE_LIST(client, W->h.client);
  1211. }