client.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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)
  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 = XkbKeycodeToKeysym(W->dpy, (KeyCode)ke->keycode, 0, 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. || !COMPCLIENT(c, cm))
  453. return;
  454. layout_split_arrange_closed(c);
  455. cm->flags |= CLIENT_TABBED;
  456. c->geo = cm->geo;
  457. cm->tabmaster = c;
  458. if(cm->flags & CLIENT_FREE)
  459. swap_int((int*)&m[0], (int*)&m[1]);
  460. c->flags |= m[0];
  461. c->flags &= ~m[1];
  462. client_focus(cm);
  463. }
  464. static void
  465. client_untab(struct client *c)
  466. {
  467. struct client *ct, *cc = c->tabmaster;
  468. struct geo og = c->geo;
  469. bool chk = false;
  470. if(c->flags & CLIENT_REMOVEALL || !(c->flags & (CLIENT_TABBED | CLIENT_TABMASTER)))
  471. return;
  472. if(!cc)
  473. {
  474. SLIST_FOREACH(cc, &c->tag->clients, tnext)
  475. if(cc->tabmaster == c)
  476. break;
  477. }
  478. if(cc)
  479. {
  480. client_tab_focus(cc);
  481. c->flags &= ~CLIENT_TABBED;
  482. c->flags |= CLIENT_IGNORE_ENTER;
  483. c->tabmaster = NULL;
  484. /* Looking for tabbed client in cc, if there is not
  485. * remove cc CLIENT_TABMASTER flag.
  486. */
  487. SLIST_FOREACH(ct, &c->tag->clients, tnext)
  488. if(ct->tabmaster == cc)
  489. {
  490. chk = true;
  491. break;
  492. }
  493. if(!chk)
  494. cc->flags &= ~CLIENT_TABMASTER;
  495. if(!(c->flags & CLIENT_DYING))
  496. {
  497. c->geo = c->tgeo = og;
  498. c->tag->sel = cc;
  499. layout_client(c);
  500. client_map(c);
  501. client_moveresize(c, &c->geo);
  502. client_update_props(c, CPROP_TAB);
  503. }
  504. client_frame_update(cc, CCOL(cc));
  505. }
  506. }
  507. void
  508. uicb_client_untab(Uicb cmd)
  509. {
  510. (void)cmd;
  511. if(W->client)
  512. client_untab(W->client);
  513. }
  514. void
  515. client_focus(struct client *c)
  516. {
  517. /* Unfocus selected */
  518. if(W->client && W->client != c)
  519. {
  520. client_grabbuttons(W->client, false);
  521. client_frame_update(W->client, &W->client->ncol);
  522. }
  523. /* Focus c */
  524. if((W->client = c))
  525. {
  526. c->tag->sel = c;
  527. client_grabbuttons(c, true);
  528. client_tab_focus(c);
  529. client_frame_update(c, CCOL(c));
  530. if(c->flags & CLIENT_FREE
  531. && !(c->flags & (CLIENT_FULLSCREEN | CLIENT_TABBED)))
  532. {
  533. c->tag->flags |= CLIENT_IGNORE_ENTER;
  534. XRaiseWindow(W->dpy, c->frame);
  535. }
  536. XSetInputFocus(W->dpy, c->win, RevertToPointerRoot, CurrentTime);
  537. XChangeProperty(W->dpy, W->root, W->net_atom[net_active_window], XA_WINDOW, 32,
  538. PropModeReplace, (unsigned char *)&c->win, 1);
  539. }
  540. else
  541. {
  542. W->client = W->screen->seltag->sel = NULL;
  543. XSetInputFocus(W->dpy, W->root, RevertToPointerRoot, CurrentTime);
  544. }
  545. ewmh_update_wmfs_props();
  546. }
  547. void
  548. uicb_client_focus_click(Uicb cmd)
  549. {
  550. (void)cmd;
  551. struct client *c;
  552. if((c = client_gb_titlebar(W->last_clicked_barwin->win))
  553. || (c = client_gb_frame(W->last_clicked_barwin->win)))
  554. client_focus(c);
  555. }
  556. /** Get a client name
  557. * \param c struct client pointer
  558. */
  559. void
  560. client_get_name(struct client *c)
  561. {
  562. Atom rt;
  563. int rf;
  564. unsigned long ir, il;
  565. /* This one instead XFetchName for utf8 name support */
  566. if(XGetWindowProperty(W->dpy, c->win, W->net_atom[net_wm_name], 0, 65536,
  567. False, W->net_atom[utf8_string], &rt, &rf, &ir, &il, (unsigned char**)&c->title) != Success)
  568. XGetWindowProperty(W->dpy, c->win, W->net_atom[net_wm_name], 0, 65536,
  569. False, W->net_atom[utf8_string], &rt, &rf, &ir, &il, (unsigned char**)&c->title);
  570. /* Still no title... */
  571. if(!c->title)
  572. XFetchName(W->dpy, c->win, &(c->title));
  573. client_frame_update(c, CCOL(c));
  574. }
  575. /** Close a client
  576. * \param c struct client pointer
  577. */
  578. void
  579. client_close(struct client *c)
  580. {
  581. int proto;
  582. XEvent ev;
  583. Atom *atom = NULL;
  584. /* Event will call client_remove */
  585. if(XGetWMProtocols(W->dpy, c->win, &atom, &proto) && atom)
  586. {
  587. while(proto--)
  588. if(atom[proto] == ATOM("WM_DELETE_WINDOW"))
  589. {
  590. ev.type = ClientMessage;
  591. ev.xclient.window = c->win;
  592. ev.xclient.message_type = ATOM("WM_PROTOCOLS");
  593. ev.xclient.format = 32;
  594. ev.xclient.data.l[0] = ATOM("WM_DELETE_WINDOW");
  595. ev.xclient.data.l[1] = CurrentTime;
  596. XSendEvent(W->dpy, c->win, False, NoEventMask, &ev);
  597. XFree(atom);
  598. return;
  599. }
  600. }
  601. XKillClient(W->dpy, c->win);
  602. }
  603. void
  604. uicb_client_close(Uicb cmd)
  605. {
  606. (void)cmd;
  607. if(W->client)
  608. client_close(W->client);
  609. }
  610. void
  611. client_get_sizeh(struct client *c)
  612. {
  613. long msize;
  614. XSizeHints size;
  615. memset(c->sizeh, 0, SHLAST);
  616. if(!XGetWMNormalHints(W->dpy, c->win, &size, &msize) || !size.flags)
  617. size.flags = PSize;
  618. /* base */
  619. if(size.flags & PBaseSize)
  620. {
  621. c->sizeh[BASEW] = size.base_width;
  622. c->sizeh[BASEH] = size.base_height;
  623. }
  624. else if(size.flags & PMinSize)
  625. {
  626. c->sizeh[BASEW] = size.min_width;
  627. c->sizeh[BASEH] = size.min_height;
  628. }
  629. /* inc */
  630. if(size.flags & PResizeInc)
  631. {
  632. c->sizeh[INCW] = size.width_inc;
  633. c->sizeh[INCH] = size.height_inc;
  634. }
  635. /* max */
  636. if(size.flags & PMaxSize)
  637. {
  638. c->sizeh[MAXW] = size.max_width;
  639. c->sizeh[MAXH] = size.max_height;
  640. }
  641. /* min */
  642. if(size.flags & PMinSize)
  643. {
  644. c->sizeh[MINW] = (size.min_width ? size.min_width : 1);
  645. c->sizeh[MINH] = (size.min_height ? size.min_height: 1);
  646. }
  647. else if(size.flags & PBaseSize)
  648. {
  649. c->sizeh[MINW] = (size.base_width ? size.base_width : 1);
  650. c->sizeh[MINH] = (size.base_height ? size.base_height : 1);
  651. }
  652. /* aspect */
  653. if(size.flags & PAspect)
  654. {
  655. c->sizeh[MINAX] = size.min_aspect.x;
  656. c->sizeh[MAXAX] = size.max_aspect.x;
  657. c->sizeh[MINAY] = size.min_aspect.y;
  658. c->sizeh[MAXAY] = size.max_aspect.y;
  659. }
  660. if(c->sizeh[MAXW] && c->sizeh[MINW] && c->sizeh[MAXH] && c->sizeh[MINH]
  661. && c->sizeh[MAXW] == c->sizeh[MINW] && c->sizeh[MAXH] == c->sizeh[MINH])
  662. c->flags |= CLIENT_HINT_FLAG;
  663. }
  664. static void
  665. client_frame_new(struct client *c)
  666. {
  667. struct barwin *frameb;
  668. struct barwin *clientb;
  669. XSetWindowAttributes at =
  670. {
  671. .background_pixel = c->ncol.bg,
  672. .override_redirect = true,
  673. .background_pixmap = ParentRelative,
  674. .event_mask = BARWIN_MASK | BARWIN_ENTERMASK
  675. };
  676. /* Use a fake barwin only to store mousebinds of frame win */
  677. frameb = barwin_new(W->root, 0, 0, 1, 1, 0, 0, false);
  678. clientb = barwin_new(W->root, 0, 0, 1, 1, 0, 0, false);
  679. frameb->win =
  680. c->frame = XCreateWindow(W->dpy, W->root,
  681. c->geo.x, c->geo.y,
  682. c->geo.w, c->geo.h,
  683. 0, CopyFromParent,
  684. InputOutput,
  685. CopyFromParent,
  686. (CWOverrideRedirect | CWBackPixmap
  687. | CWBackPixel | CWEventMask), &at);
  688. clientb->win = c->win;
  689. frameb->mousebinds = W->tmp_head.client;
  690. clientb->mousebinds = W->tmp_head.client;
  691. if(c->tbarw > c->border)
  692. {
  693. c->titlebar = barwin_new(c->frame, 0, 0, 1, c->tbarw,
  694. c->ncol.fg, c->ncol.bg, true);
  695. c->titlebar->mousebinds = W->tmp_head.client;
  696. }
  697. XReparentWindow(W->dpy, c->win, c->frame, c->border, c->tbarw);
  698. }
  699. static void
  700. _apply_rule(struct client *c, struct rule *r)
  701. {
  702. if(r->screen != -1)
  703. c->screen = screen_gb_id(r->screen);
  704. c->tag = c->screen->seltag;
  705. if(r->tag != -1)
  706. c->tag = tag_gb_id(c->screen, r->tag);
  707. c->theme = r->theme;
  708. /* free = false for originally free client */
  709. if(r->flags & RULE_FREE)
  710. c->flags |= CLIENT_FREE;
  711. else
  712. c->flags &= ~CLIENT_FREE;
  713. /* Free rule is not compatible with tab rule */
  714. if(r->flags & RULE_TAB)
  715. W->flags ^= WMFS_TABNOC; /* < can be disable by client_tab_next_opened */
  716. if(r->flags & RULE_IGNORE_TAG)
  717. c->flags |= CLIENT_IGNORE_TAG;
  718. c->flags |= CLIENT_RULED;
  719. }
  720. #define RINSTANCE 0x01
  721. #define RCLASS 0x02
  722. #define RROLE 0x04
  723. #define RNAME 0x08
  724. static void
  725. client_apply_rule(struct client *c)
  726. {
  727. struct rule *r;
  728. struct rule *defaultr = NULL;
  729. char *wmname = NULL;
  730. char *role = NULL;
  731. int f;
  732. unsigned char *data = NULL;
  733. unsigned long n, il;
  734. Flags flags = 0;
  735. Atom rf;
  736. XClassHint xch;
  737. Status s = XGetClassHint(W->dpy, c->win, &xch);
  738. /* Get WM_WINDOW_ROLE */
  739. if(XGetWindowProperty(W->dpy, c->win, ATOM("WM_WINDOW_ROLE"), 0L, 0x7FFFFFFFL, false,
  740. XA_STRING, &rf, &f, &n, &il, &data)
  741. == Success && data)
  742. {
  743. role = xstrdup((char*)data);
  744. XFree(data);
  745. }
  746. /* Get _NET_WM_NAME */
  747. if(XGetWindowProperty(W->dpy, c->win, W->net_atom[net_wm_name], 0, 0x77777777, false,
  748. W->net_atom[utf8_string], &rf, &f, &n, &il, &data)
  749. == Success && data)
  750. {
  751. wmname = xstrdup((char*)data);
  752. XFree(data);
  753. }
  754. /* Apply a specific rule */
  755. SLIST_FOREACH(r, &W->h.rule, next)
  756. {
  757. if (r->instance && !strcmp(r->instance, "*"))
  758. defaultr = r;
  759. if(s)
  760. {
  761. FLAGAPPLY(flags, (xch.res_name && r->instance && !strcmp(xch.res_name, r->instance)), RINSTANCE);
  762. FLAGAPPLY(flags, (xch.res_class && r->class && !strcmp(xch.res_class, r->class)), RCLASS);
  763. }
  764. FLAGAPPLY(flags, (wmname && r->name && !strcmp(wmname, r->name)), RNAME);
  765. FLAGAPPLY(flags, ((role && r->role && !strcmp(role, r->role)) || !role || !r->role), RROLE);
  766. if(flags & (RINSTANCE | RCLASS | RNAME) && flags & RROLE)
  767. _apply_rule(c, r);
  768. flags = 0;
  769. }
  770. if(role)
  771. free(role);
  772. if(wmname)
  773. free(wmname);
  774. /* Apply default rule */
  775. if (!(c->flags & CLIENT_RULED) && defaultr != NULL)
  776. _apply_rule(c, defaultr);
  777. }
  778. struct client*
  779. client_new(Window w, XWindowAttributes *wa, bool scan)
  780. {
  781. struct client *c = xcalloc(1, sizeof(struct client));
  782. /* C attributes */
  783. c->win = w;
  784. c->flags = 0;
  785. c->screen = screen_update_sel();
  786. c->theme = W->ctheme;
  787. c->tag = NULL;
  788. c->tabmaster = NULL;
  789. /* struct geometry */
  790. c->geo.x = wa->x;
  791. c->geo.y = wa->y;
  792. c->geo.w = wa->width;
  793. c->geo.h = wa->height;
  794. c->tgeo = c->wgeo = c->rgeo = c->geo;
  795. c->tbgeo = NULL;
  796. client_get_sizeh(c);
  797. if(!scan)
  798. {
  799. if(c->flags & CLIENT_HINT_FLAG /* && OPTIONKIVABIEN */)
  800. c->flags |= CLIENT_FREE;
  801. client_apply_rule(c);
  802. }
  803. /*
  804. * Conf option set per client, for possibility
  805. * to config only one client
  806. */
  807. c->border = c->theme->client_border_width;
  808. if(!(c->tbarw = c->theme->client_titlebar_width))
  809. c->tbarw = c->border;
  810. c->ncol = c->theme->client_n;
  811. c->scol = c->theme->client_s;
  812. client_frame_new(c);
  813. if(!scan)
  814. {
  815. ewmh_manage_window_type(c);
  816. tag_client((c->flags & CLIENT_RULED ? c->tag : c->screen->seltag), c);
  817. }
  818. /* Map, not at reload */
  819. if(c->tag == c->screen->seltag)
  820. client_map(c);
  821. /* X window attributes */
  822. XSelectInput(W->dpy, w, EnterWindowMask | LeaveWindowMask | StructureNotifyMask | PropertyChangeMask);
  823. XSetWindowBorderWidth(W->dpy, w, 0);
  824. client_grabbuttons(c, false);
  825. /* Attach */
  826. SLIST_INSERT_HEAD(&W->h.client, c, next);
  827. if(!scan)
  828. {
  829. client_get_name(c);
  830. if(W->flags & WMFS_AUTOFOCUS)
  831. client_focus(c);
  832. client_configure(c);
  833. }
  834. ewmh_get_client_list();
  835. return c;
  836. }
  837. void
  838. client_update_props(struct client *c, Flags f)
  839. {
  840. if(f & CPROP_LOC)
  841. {
  842. XChangeProperty(W->dpy, c->win, ATOM("_WMFS_TAG"), XA_CARDINAL, 32,
  843. PropModeReplace, (unsigned char*)&(c->tag->id), 1);
  844. XChangeProperty(W->dpy, c->win, ATOM("_WMFS_SCREEN"), XA_CARDINAL, 32,
  845. PropModeReplace, (unsigned char*)&(c->screen->id), 1);
  846. }
  847. if(f & CPROP_FLAG)
  848. XChangeProperty(W->dpy, c->win, ATOM("_WMFS_FLAGS"), XA_CARDINAL, 32,
  849. PropModeReplace, (unsigned char*)&(c->flags), 1);
  850. if(f & CPROP_GEO)
  851. {
  852. long g[4] = { (long)c->geo.x, (long)c->geo.y, (long)c->geo.w, (long)c->geo.h };
  853. XChangeProperty(W->dpy, c->win, ATOM("_WMFS_GEO"), XA_CARDINAL, 32,
  854. PropModeReplace, (unsigned char*)g, 4);
  855. }
  856. if(f & CPROP_TAB)
  857. {
  858. Window w = (c->tabmaster ? c->tabmaster->win : 0);
  859. XChangeProperty(W->dpy, c->win, ATOM("_WMFS_TABMASTER"), XA_WINDOW, 32,
  860. PropModeReplace, (unsigned char*)&w, 1);
  861. }
  862. }
  863. void
  864. client_geo_hints(struct geo *g, int *s)
  865. {
  866. /* base */
  867. g->w -= s[BASEW];
  868. g->h -= s[BASEH];
  869. /* aspect */
  870. if((s[MINAY] | s[MAXAY] | s[MINAX] | s[MAXAX]) > 0)
  871. {
  872. if(g->w * s[MAXAY] > g->h * s[MAXAX])
  873. g->w = g->h * s[MAXAX] / s[MAXAY];
  874. else if(g->w * s[MINAY] < g->h * s[MINAX])
  875. g->h = g->w * s[MINAY] / s[MINAX];
  876. }
  877. /* incremental */
  878. if(s[INCW])
  879. g->w -= g->w % s[INCW];
  880. if(s[INCH])
  881. g->h -= g->h % s[INCH];
  882. /* base dimension */
  883. g->w += s[BASEW];
  884. g->h += s[BASEH];
  885. if(s[MINW] > 0 && g->w < s[MINW])
  886. g->w = s[MINW];
  887. if(s[MINH] > 0 && g->h < s[MINH])
  888. g->h = s[MINH];
  889. if(s[MAXW] > 0 && g->w > s[MAXW])
  890. g->w = s[MAXW];
  891. if(s[MAXH] > 0 && g->h > s[MAXH])
  892. g->h = s[MAXH];
  893. }
  894. /* Manage window size in frame in tiling mode */
  895. bool
  896. client_winsize(struct client *c, struct geo *g)
  897. {
  898. int ow, oh;
  899. struct geo og = c->wgeo;
  900. struct geo tmp = *g;
  901. tmp.w -= W->padding >> 1;
  902. tmp.h -= W->padding >> 1;
  903. /* Window geo */
  904. c->wgeo.x = c->border;
  905. c->wgeo.y = c->tbarw;
  906. c->wgeo.h = oh = tmp.h - (c->border + c->tbarw);
  907. c->wgeo.w = ow = tmp.w - (c->border << 1);
  908. client_geo_hints(&c->wgeo, (int*)c->sizeh);
  909. /* Check possible problem for tile integration */
  910. if(ow < c->sizeh[MINW] || oh < c->sizeh[MINH])
  911. if(tmp.w < c->geo.w || tmp.h < c->geo.h)
  912. {
  913. c->wgeo = og;
  914. return true;
  915. }
  916. /* Balance position with new size */
  917. c->wgeo.x += (ow - c->wgeo.w) >> 1;
  918. c->flags |= CLIENT_DID_WINSIZE;
  919. return false;
  920. }
  921. void
  922. client_moveresize(struct client *c, struct geo *g)
  923. {
  924. if(c->flags & CLIENT_TABBED)
  925. return;
  926. /* Adjust frame regarding window required size */
  927. if(c->flags & CLIENT_FREE)
  928. {
  929. g->w -= c->border + c->border;
  930. g->h -= c->tbarw + c->border;
  931. client_geo_hints(g, (int*)c->sizeh);
  932. c->wgeo = c->geo = c->rgeo = *g;
  933. c->wgeo.x = c->border;
  934. c->wgeo.y = c->tbarw;
  935. c->geo.w = c->rgeo.w = c->wgeo.w + c->border + c->border;
  936. c->geo.h = c->rgeo.h = c->wgeo.h + c->tbarw + c->border;
  937. c->rgeo.x += c->screen->ugeo.x;
  938. c->rgeo.y += c->screen->ugeo.y;
  939. if(!INAREA(c->rgeo.x, c->rgeo.y, c->screen->ugeo))
  940. {
  941. /* New screen (moved by mouse) */
  942. if(c->flags & CLIENT_MOUSE)
  943. {
  944. c->flags |= CLIENT_IGNORE_LAYOUT;
  945. c->screen = screen_gb_geo(c->rgeo.x, c->rgeo.y);
  946. tag_client(c->screen->seltag, c);
  947. c->geo.x = c->rgeo.x - c->screen->ugeo.x;
  948. c->geo.y = c->rgeo.y - c->screen->ugeo.y;
  949. }
  950. /* Out of the screen case */
  951. else
  952. {
  953. c->geo.x = (c->screen->ugeo.w >> 1) - (c->geo.w >> 1);
  954. c->geo.y = (c->screen->ugeo.h >> 1) - (c->geo.h >> 1);
  955. c->rgeo.x = c->screen->ugeo.x + c->geo.x;
  956. c->rgeo.y = c->screen->ugeo.y + c->geo.y;
  957. }
  958. }
  959. }
  960. /* Adjust window regarding required size for frame (tiling) */
  961. else
  962. {
  963. c->ttgeo = c->tgeo = c->rgeo = c->geo = *g;
  964. if(!(c->flags & CLIENT_DID_WINSIZE))
  965. client_winsize(c, g);
  966. c->rgeo.x += c->screen->ugeo.x;
  967. c->rgeo.y += c->screen->ugeo.y;
  968. c->rgeo.x += W->padding >> 2;
  969. c->rgeo.y += W->padding >> 2;
  970. c->rgeo.w -= W->padding >> 1;
  971. c->rgeo.h -= W->padding >> 1;
  972. }
  973. XMoveResizeWindow(W->dpy, c->frame,
  974. c->rgeo.x, c->rgeo.y,
  975. c->rgeo.w, c->rgeo.h);
  976. if(!(c->flags & CLIENT_FULLSCREEN))
  977. XMoveResizeWindow(W->dpy, c->win,
  978. c->wgeo.x, c->wgeo.y,
  979. c->wgeo.w, c->wgeo.h);
  980. c->flags &= ~CLIENT_DID_WINSIZE;
  981. client_frame_update(c, CCOL(c));
  982. client_update_props(c, CPROP_GEO);
  983. client_configure(c);
  984. }
  985. void
  986. client_place_at_mouse(struct client *c)
  987. {
  988. int x, y;
  989. Window w;
  990. int d, u;
  991. XQueryPointer(W->dpy, W->root, &w, &w, &x, &y, &d, &d, (uint *)&u);
  992. if(x < c->screen->ugeo.x)
  993. x = 0;
  994. if(y < c->screen->ugeo.y)
  995. y = 0;
  996. c->geo.x = ((x + c->geo.w) > c->screen->ugeo.w ? c->screen->ugeo.w - c->geo.w : x);
  997. c->geo.y = ((y + c->geo.h) > c->screen->ugeo.h ? c->screen->ugeo.h - c->geo.h : y);
  998. }
  999. void
  1000. client_maximize(struct client *c)
  1001. {
  1002. c->geo.x = c->geo.y = 0;
  1003. c->geo.w = c->screen->ugeo.w;
  1004. c->geo.h = c->screen->ugeo.h;
  1005. client_moveresize(c, &c->geo);
  1006. layout_save_set(c->tag);
  1007. }
  1008. /*
  1009. * Client factor resize: allow clients to be resized in
  1010. * manual tile layout.
  1011. */
  1012. static inline void
  1013. _fac_apply(struct client *c, enum position p, int fac)
  1014. {
  1015. switch(p)
  1016. {
  1017. case Top:
  1018. c->tgeo.y -= fac;
  1019. case Bottom:
  1020. c->tgeo.h += fac;
  1021. break;
  1022. case Left:
  1023. c->tgeo.x -= fac;
  1024. default:
  1025. case Right:
  1026. c->tgeo.w += fac;
  1027. break;
  1028. }
  1029. c->flags |= (CLIENT_IGNORE_ENTER | CLIENT_FAC_APPLIED);
  1030. }
  1031. static inline void
  1032. _fac_arrange_row(struct client *c, enum position p, int fac)
  1033. {
  1034. struct geo g = c->tgeo;
  1035. struct client *cc;
  1036. /* Travel clients to search row parents and apply fac */
  1037. FOREACH_NFCLIENT(cc, &c->tag->clients, tnext)
  1038. if(GEO_PARENTROW(g, cc->tgeo, p))
  1039. _fac_apply(cc, p, fac);
  1040. }
  1041. static inline void
  1042. _fac_check_to_reverse(struct client *c)
  1043. {
  1044. struct client *gc, *cc;
  1045. /*
  1046. * Check if every clients are compatible with
  1047. * future globals geo. Problem here is that we must *not*
  1048. * resize client because of possible error with next
  1049. * clients in linked list.
  1050. */
  1051. FOREACH_NFCLIENT(gc, &c->tag->clients, tnext)
  1052. if(gc->flags & CLIENT_FAC_APPLIED
  1053. && client_winsize(gc, &gc->tgeo))
  1054. {
  1055. /*
  1056. * Reverse back the flag and the window geo
  1057. * in previous affected clients
  1058. */
  1059. FOREACH_NFCLIENT(cc, &c->tag->clients, tnext)
  1060. {
  1061. cc->tgeo = cc->ttgeo;
  1062. cc->flags &= ~CLIENT_DID_WINSIZE;
  1063. }
  1064. return;
  1065. }
  1066. }
  1067. void
  1068. _fac_resize(struct client *c, enum position p, int fac)
  1069. {
  1070. struct client *cc, *gc = client_next_with_pos(c, p);
  1071. enum position rp = RPOS(p);
  1072. if(!gc || gc->screen != c->screen || !(c->flags & CLIENT_TILED))
  1073. return;
  1074. FOREACH_NFCLIENT(cc, &c->tag->clients, tnext)
  1075. cc->ttgeo = cc->tgeo;
  1076. if(GEO_CHECK2(c->tgeo, gc->tgeo, p))
  1077. {
  1078. _fac_apply(c, p, fac);
  1079. _fac_apply(gc, rp, -fac);
  1080. }
  1081. else
  1082. {
  1083. _fac_arrange_row(c, p, fac);
  1084. _fac_arrange_row(gc, rp, -fac);
  1085. }
  1086. _fac_check_to_reverse(c);
  1087. }
  1088. void
  1089. client_apply_tgeo(struct tag *t)
  1090. {
  1091. struct client *c;
  1092. FOREACH_NFCLIENT(c, &t->clients, tnext)
  1093. {
  1094. client_moveresize(c, &c->tgeo);
  1095. c->flags &= ~CLIENT_FAC_APPLIED;
  1096. }
  1097. }
  1098. #define _REV_BORDER() \
  1099. do { \
  1100. FOREACH_NFCLIENT(gc, &c->tag->clients, tnext) \
  1101. draw_reversed_rect(W->root, gc, true); \
  1102. } while(/* CONSTCOND */ 0);
  1103. void
  1104. client_fac_resize(struct client *c, enum position p, int fac)
  1105. {
  1106. struct keybind *k;
  1107. struct client *gc;
  1108. bool b = true;
  1109. XEvent ev;
  1110. KeySym keysym;
  1111. /* Do it once before */
  1112. _fac_resize(c, p, fac);
  1113. /* TODO
  1114. if(option_simple_manual_resize)
  1115. return;
  1116. */
  1117. XGrabServer(W->dpy);
  1118. XGrabKeyboard(W->dpy, W->root, True, GrabModeAsync, GrabModeAsync, CurrentTime);
  1119. _REV_BORDER();
  1120. do
  1121. {
  1122. XMaskEvent(W->dpy, KeyPressMask, &ev);
  1123. if(ev.type == KeyPress)
  1124. {
  1125. XKeyPressedEvent *ke = &ev.xkey;
  1126. keysym = XkbKeycodeToKeysym(W->dpy, (KeyCode)ke->keycode, 0, 0);
  1127. _REV_BORDER();
  1128. SLIST_FOREACH(k, &W->h.keybind, next)
  1129. if(k->keysym == keysym && KEYPRESS_MASK(k->mod) == KEYPRESS_MASK(ke->state)
  1130. && k->func)
  1131. {
  1132. if(k->func == uicb_client_resize_Right)
  1133. _fac_resize(c, Right, ATOI(k->cmd));
  1134. else if(k->func == uicb_client_resize_Left)
  1135. _fac_resize(c, Left, ATOI(k->cmd));
  1136. else if(k->func == uicb_client_resize_Top)
  1137. _fac_resize(c, Top, ATOI(k->cmd));
  1138. else if(k->func == uicb_client_resize_Bottom)
  1139. _fac_resize(c, Bottom, ATOI(k->cmd));
  1140. else
  1141. {
  1142. k->func(k->cmd);
  1143. keysym = XK_Escape;
  1144. }
  1145. }
  1146. _REV_BORDER();
  1147. /* Gtfo of this loop */
  1148. if(keysym == XK_Return)
  1149. break;
  1150. else if(keysym == XK_Escape)
  1151. {
  1152. b = false;
  1153. break;
  1154. }
  1155. XSync(W->dpy, False);
  1156. }
  1157. XNextEvent(W->dpy, &ev);
  1158. } while(ev.type != KeyPress);
  1159. _REV_BORDER();
  1160. /* Success, resize clients */
  1161. if(b)
  1162. {
  1163. client_apply_tgeo(c->tag);
  1164. layout_save_set(c->tag);
  1165. }
  1166. /* Aborted with escape, Set back original geos */
  1167. else
  1168. {
  1169. FOREACH_NFCLIENT(gc, &c->tag->clients, tnext)
  1170. {
  1171. gc->tgeo = gc->geo;
  1172. gc->flags &= ~CLIENT_DID_WINSIZE;
  1173. }
  1174. }
  1175. XUngrabServer(W->dpy);
  1176. XUngrabKeyboard(W->dpy, CurrentTime);
  1177. }
  1178. void
  1179. client_fac_hint(struct client *c)
  1180. {
  1181. int w = c->sizeh[MINW] + c->border + c->border;
  1182. int h = c->sizeh[MINH] + c->tbarw + c->border;
  1183. if(c->geo.h < h)
  1184. _fac_resize(c, Top, (h - c->geo.h));
  1185. if(c->tgeo.h < h)
  1186. _fac_resize(c, Bottom, (h - c->tgeo.h));
  1187. if(c->geo.w < w)
  1188. _fac_resize(c, Left, (w - c->geo.w));
  1189. if(c->tgeo.w < w)
  1190. _fac_resize(c, Right, (w - c->tgeo.w));
  1191. client_apply_tgeo(c->tag);
  1192. }
  1193. void
  1194. client_remove(struct client *c)
  1195. {
  1196. c->flags |= CLIENT_DYING;
  1197. client_untab(c);
  1198. XGrabServer(W->dpy);
  1199. XSetErrorHandler(wmfs_error_handler_dummy);
  1200. XReparentWindow(W->dpy, c->win, W->root, c->rgeo.x, c->rgeo.y);
  1201. XUngrabButton(W->dpy, AnyButton, AnyModifier, c->win);
  1202. ewmh_set_wm_state(c->win, WithdrawnState);
  1203. XSync(W->dpy, false);
  1204. XSetErrorHandler(wmfs_error_handler);
  1205. XUngrabServer(W->dpy);
  1206. SLIST_REMOVE(&W->h.client, c, client, next);
  1207. tag_client(NULL, c);
  1208. /* Remove frame */
  1209. if(c->titlebar)
  1210. barwin_remove(c->titlebar);
  1211. XDestroyWindow(W->dpy, c->frame);
  1212. free(c);
  1213. ewmh_get_client_list();
  1214. }
  1215. void
  1216. uicb_client_toggle_free(Uicb cmd)
  1217. {
  1218. struct client *c;
  1219. (void)cmd;
  1220. if(!(W->client))
  1221. return;
  1222. W->client->flags ^= CLIENT_FREE;
  1223. layout_client(W->client);
  1224. /* Set tabbed client of toggled client as free */
  1225. if(W->client->flags & CLIENT_TABMASTER)
  1226. {
  1227. SLIST_FOREACH(c, &W->client->tag->clients, tnext)
  1228. if(c->tabmaster == W->client && c != W->client)
  1229. c->flags ^= CLIENT_FREE;
  1230. }
  1231. }
  1232. void uicb_client_toggle_ignore_tag(Uicb cmd)
  1233. {
  1234. struct client *c;
  1235. (void)cmd;
  1236. if(!(W->client))
  1237. return;
  1238. W->client->flags ^= CLIENT_IGNORE_TAG;
  1239. /* Set tabbed client of toggled client as ignore_tag */
  1240. if(W->client->flags & CLIENT_TABMASTER)
  1241. {
  1242. SLIST_FOREACH(c, &W->client->tag->clients, tnext)
  1243. if(c->tabmaster == W->client && c != W->client)
  1244. c->flags ^= CLIENT_IGNORE_TAG;
  1245. }
  1246. }
  1247. void
  1248. uicb_client_tab_next_opened(Uicb cmd)
  1249. {
  1250. (void)cmd;
  1251. W->flags ^= WMFS_TABNOC;
  1252. }
  1253. void
  1254. client_free(void)
  1255. {
  1256. FREE_LIST(client, W->h.client);
  1257. }