client.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  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 = 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. || !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. ewmh_manage_state_sticky(c);
  804. /*
  805. * Conf option set per client, for possibility
  806. * to config only one client
  807. */
  808. c->border = c->theme->client_border_width;
  809. if(!(c->tbarw = c->theme->client_titlebar_width) || (c->flags & CLIENT_STICKY))
  810. c->tbarw = c->border;
  811. c->ncol = c->theme->client_n;
  812. c->scol = c->theme->client_s;
  813. client_frame_new(c);
  814. if(!scan)
  815. {
  816. ewmh_manage_window_type(c);
  817. tag_client((c->flags & CLIENT_RULED ? c->tag : c->screen->seltag), c);
  818. }
  819. /* Map, not at reload */
  820. if(c->tag == c->screen->seltag)
  821. client_map(c);
  822. /* X window attributes */
  823. XSelectInput(W->dpy, w, EnterWindowMask | LeaveWindowMask | StructureNotifyMask | PropertyChangeMask);
  824. XSetWindowBorderWidth(W->dpy, w, 0);
  825. client_grabbuttons(c, false);
  826. /* Attach */
  827. SLIST_INSERT_HEAD(&W->h.client, c, next);
  828. if(!scan)
  829. {
  830. client_get_name(c);
  831. if(W->flags & WMFS_AUTOFOCUS)
  832. client_focus(c);
  833. client_configure(c);
  834. }
  835. ewmh_get_client_list();
  836. return c;
  837. }
  838. void
  839. client_update_props(struct client *c, Flags f)
  840. {
  841. if(f & CPROP_LOC)
  842. {
  843. XChangeProperty(W->dpy, c->win, ATOM("_WMFS_TAG"), XA_CARDINAL, 32,
  844. PropModeReplace, (unsigned char*)&(c->tag->id), 1);
  845. XChangeProperty(W->dpy, c->win, ATOM("_WMFS_SCREEN"), XA_CARDINAL, 32,
  846. PropModeReplace, (unsigned char*)&(c->screen->id), 1);
  847. }
  848. if(f & CPROP_FLAG)
  849. XChangeProperty(W->dpy, c->win, ATOM("_WMFS_FLAGS"), XA_CARDINAL, 32,
  850. PropModeReplace, (unsigned char*)&(c->flags), 1);
  851. if(f & CPROP_GEO)
  852. {
  853. long g[4] = { (long)c->geo.x, (long)c->geo.y, (long)c->geo.w, (long)c->geo.h };
  854. XChangeProperty(W->dpy, c->win, ATOM("_WMFS_GEO"), XA_CARDINAL, 32,
  855. PropModeReplace, (unsigned char*)g, 4);
  856. }
  857. if(f & CPROP_TAB)
  858. {
  859. Window w = (c->tabmaster ? c->tabmaster->win : 0);
  860. XChangeProperty(W->dpy, c->win, ATOM("_WMFS_TABMASTER"), XA_WINDOW, 32,
  861. PropModeReplace, (unsigned char*)&w, 1);
  862. }
  863. }
  864. void
  865. client_geo_hints(struct geo *g, int *s)
  866. {
  867. /* base */
  868. g->w -= s[BASEW];
  869. g->h -= s[BASEH];
  870. /* aspect */
  871. if((s[MINAY] | s[MAXAY] | s[MINAX] | s[MAXAX]) > 0)
  872. {
  873. if(g->w * s[MAXAY] > g->h * s[MAXAX])
  874. g->w = g->h * s[MAXAX] / s[MAXAY];
  875. else if(g->w * s[MINAY] < g->h * s[MINAX])
  876. g->h = g->w * s[MINAY] / s[MINAX];
  877. }
  878. /* incremental */
  879. if(s[INCW])
  880. g->w -= g->w % s[INCW];
  881. if(s[INCH])
  882. g->h -= g->h % s[INCH];
  883. /* base dimension */
  884. g->w += s[BASEW];
  885. g->h += s[BASEH];
  886. if(s[MINW] > 0 && g->w < s[MINW])
  887. g->w = s[MINW];
  888. if(s[MINH] > 0 && g->h < s[MINH])
  889. g->h = s[MINH];
  890. if(s[MAXW] > 0 && g->w > s[MAXW])
  891. g->w = s[MAXW];
  892. if(s[MAXH] > 0 && g->h > s[MAXH])
  893. g->h = s[MAXH];
  894. }
  895. /* Manage window size in frame in tiling mode */
  896. bool
  897. client_winsize(struct client *c, struct geo *g)
  898. {
  899. int ow, oh;
  900. struct geo og = c->wgeo;
  901. struct geo tmp = *g;
  902. tmp.w -= W->padding >> 1;
  903. tmp.h -= W->padding >> 1;
  904. /* Window geo */
  905. c->wgeo.x = c->border;
  906. c->wgeo.y = c->tbarw;
  907. c->wgeo.h = oh = tmp.h - (c->border + c->tbarw);
  908. c->wgeo.w = ow = tmp.w - (c->border << 1);
  909. client_geo_hints(&c->wgeo, (int*)c->sizeh);
  910. /* Check possible problem for tile integration */
  911. if(ow < c->sizeh[MINW] || oh < c->sizeh[MINH])
  912. if(tmp.w < c->geo.w || tmp.h < c->geo.h)
  913. {
  914. c->wgeo = og;
  915. return true;
  916. }
  917. /* Balance position with new size */
  918. c->wgeo.x += (ow - c->wgeo.w) >> 1;
  919. c->flags |= CLIENT_DID_WINSIZE;
  920. return false;
  921. }
  922. void
  923. client_moveresize(struct client *c, struct geo *g)
  924. {
  925. if(c->flags & CLIENT_TABBED)
  926. return;
  927. /* Adjust frame regarding window required size */
  928. if(c->flags & CLIENT_FREE)
  929. {
  930. g->w -= c->border + c->border;
  931. g->h -= c->tbarw + c->border;
  932. client_geo_hints(g, (int*)c->sizeh);
  933. c->wgeo = c->geo = c->rgeo = *g;
  934. c->wgeo.x = c->border;
  935. c->wgeo.y = c->tbarw;
  936. c->geo.w = c->rgeo.w = c->wgeo.w + c->border + c->border;
  937. c->geo.h = c->rgeo.h = c->wgeo.h + c->tbarw + c->border;
  938. c->rgeo.x += c->screen->ugeo.x;
  939. c->rgeo.y += c->screen->ugeo.y;
  940. if(!INAREA(c->rgeo.x, c->rgeo.y, c->screen->ugeo))
  941. {
  942. /* New screen (moved by mouse) */
  943. if(c->flags & CLIENT_MOUSE)
  944. {
  945. c->flags |= CLIENT_IGNORE_LAYOUT;
  946. c->screen = screen_gb_geo(c->rgeo.x, c->rgeo.y);
  947. tag_client(c->screen->seltag, c);
  948. c->geo.x = c->rgeo.x - c->screen->ugeo.x;
  949. c->geo.y = c->rgeo.y - c->screen->ugeo.y;
  950. }
  951. /* Out of the screen case */
  952. else
  953. {
  954. c->geo.x = (c->screen->ugeo.w >> 1) - (c->geo.w >> 1);
  955. c->geo.y = (c->screen->ugeo.h >> 1) - (c->geo.h >> 1);
  956. c->rgeo.x = c->screen->ugeo.x + c->geo.x;
  957. c->rgeo.y = c->screen->ugeo.y + c->geo.y;
  958. }
  959. }
  960. }
  961. /* Adjust window regarding required size for frame (tiling) */
  962. else
  963. {
  964. c->ttgeo = c->tgeo = c->rgeo = c->geo = *g;
  965. if(!(c->flags & CLIENT_DID_WINSIZE))
  966. client_winsize(c, g);
  967. c->rgeo.x += c->screen->ugeo.x;
  968. c->rgeo.y += c->screen->ugeo.y;
  969. c->rgeo.x += W->padding >> 2;
  970. c->rgeo.y += W->padding >> 2;
  971. c->rgeo.w -= W->padding >> 1;
  972. c->rgeo.h -= W->padding >> 1;
  973. }
  974. XMoveResizeWindow(W->dpy, c->frame,
  975. c->rgeo.x, c->rgeo.y,
  976. c->rgeo.w, c->rgeo.h);
  977. if(!(c->flags & CLIENT_FULLSCREEN))
  978. XMoveResizeWindow(W->dpy, c->win,
  979. c->wgeo.x, c->wgeo.y,
  980. c->wgeo.w, c->wgeo.h);
  981. c->flags &= ~CLIENT_DID_WINSIZE;
  982. client_frame_update(c, CCOL(c));
  983. client_update_props(c, CPROP_GEO);
  984. client_configure(c);
  985. }
  986. void
  987. client_place_at_mouse(struct client *c)
  988. {
  989. int x, y;
  990. Window w;
  991. int d, u;
  992. XQueryPointer(W->dpy, W->root, &w, &w, &x, &y, &d, &d, (uint *)&u);
  993. if(x < c->screen->ugeo.x)
  994. x = 0;
  995. if(y < c->screen->ugeo.y)
  996. y = 0;
  997. c->geo.x = ((x + c->geo.w) > c->screen->ugeo.w ? c->screen->ugeo.w - c->geo.w : x);
  998. c->geo.y = ((y + c->geo.h) > c->screen->ugeo.h ? c->screen->ugeo.h - c->geo.h : y);
  999. }
  1000. void
  1001. client_maximize(struct client *c)
  1002. {
  1003. c->geo.x = c->geo.y = 0;
  1004. c->geo.w = c->screen->ugeo.w;
  1005. c->geo.h = c->screen->ugeo.h;
  1006. client_moveresize(c, &c->geo);
  1007. layout_save_set(c->tag);
  1008. }
  1009. /*
  1010. * Client factor resize: allow clients to be resized in
  1011. * manual tile layout.
  1012. */
  1013. static inline void
  1014. _fac_apply(struct client *c, enum position p, int fac)
  1015. {
  1016. switch(p)
  1017. {
  1018. case Top:
  1019. c->tgeo.y -= fac;
  1020. case Bottom:
  1021. c->tgeo.h += fac;
  1022. break;
  1023. case Left:
  1024. c->tgeo.x -= fac;
  1025. default:
  1026. case Right:
  1027. c->tgeo.w += fac;
  1028. break;
  1029. }
  1030. c->flags |= (CLIENT_IGNORE_ENTER | CLIENT_FAC_APPLIED);
  1031. }
  1032. static inline void
  1033. _fac_arrange_row(struct client *c, enum position p, int fac)
  1034. {
  1035. struct geo g = c->tgeo;
  1036. struct client *cc;
  1037. /* Travel clients to search row parents and apply fac */
  1038. FOREACH_NFCLIENT(cc, &c->tag->clients, tnext)
  1039. if(GEO_PARENTROW(g, cc->tgeo, p))
  1040. _fac_apply(cc, p, fac);
  1041. }
  1042. static inline void
  1043. _fac_check_to_reverse(struct client *c)
  1044. {
  1045. struct client *gc, *cc;
  1046. /*
  1047. * Check if every clients are compatible with
  1048. * future globals geo. Problem here is that we must *not*
  1049. * resize client because of possible error with next
  1050. * clients in linked list.
  1051. */
  1052. FOREACH_NFCLIENT(gc, &c->tag->clients, tnext)
  1053. if(gc->flags & CLIENT_FAC_APPLIED
  1054. && client_winsize(gc, &gc->tgeo))
  1055. {
  1056. /*
  1057. * Reverse back the flag and the window geo
  1058. * in previous affected clients
  1059. */
  1060. FOREACH_NFCLIENT(cc, &c->tag->clients, tnext)
  1061. {
  1062. cc->tgeo = cc->ttgeo;
  1063. cc->flags &= ~CLIENT_DID_WINSIZE;
  1064. }
  1065. return;
  1066. }
  1067. }
  1068. void
  1069. _fac_resize(struct client *c, enum position p, int fac)
  1070. {
  1071. struct client *cc, *gc = client_next_with_pos(c, p);
  1072. enum position rp = RPOS(p);
  1073. if(!gc || gc->screen != c->screen || !(c->flags & CLIENT_TILED))
  1074. return;
  1075. FOREACH_NFCLIENT(cc, &c->tag->clients, tnext)
  1076. cc->ttgeo = cc->tgeo;
  1077. if(GEO_CHECK2(c->tgeo, gc->tgeo, p))
  1078. {
  1079. _fac_apply(c, p, fac);
  1080. _fac_apply(gc, rp, -fac);
  1081. }
  1082. else
  1083. {
  1084. _fac_arrange_row(c, p, fac);
  1085. _fac_arrange_row(gc, rp, -fac);
  1086. }
  1087. _fac_check_to_reverse(c);
  1088. }
  1089. void
  1090. client_apply_tgeo(struct tag *t)
  1091. {
  1092. struct client *c;
  1093. FOREACH_NFCLIENT(c, &t->clients, tnext)
  1094. {
  1095. client_moveresize(c, &c->tgeo);
  1096. c->flags &= ~CLIENT_FAC_APPLIED;
  1097. }
  1098. }
  1099. #define _REV_BORDER() \
  1100. do { \
  1101. FOREACH_NFCLIENT(gc, &c->tag->clients, tnext) \
  1102. draw_reversed_rect(W->root, gc, true); \
  1103. } while(/* CONSTCOND */ 0);
  1104. void
  1105. client_fac_resize(struct client *c, enum position p, int fac)
  1106. {
  1107. struct keybind *k;
  1108. struct client *gc;
  1109. bool b = true;
  1110. XEvent ev;
  1111. KeySym keysym;
  1112. /* Do it once before */
  1113. _fac_resize(c, p, fac);
  1114. /* TODO
  1115. if(option_simple_manual_resize)
  1116. return;
  1117. */
  1118. XGrabServer(W->dpy);
  1119. XGrabKeyboard(W->dpy, W->root, True, GrabModeAsync, GrabModeAsync, CurrentTime);
  1120. _REV_BORDER();
  1121. do
  1122. {
  1123. XMaskEvent(W->dpy, KeyPressMask, &ev);
  1124. if(ev.type == KeyPress)
  1125. {
  1126. XKeyPressedEvent *ke = &ev.xkey;
  1127. keysym = XKeycodeToKeysym(W->dpy, (KeyCode)ke->keycode, 0);
  1128. _REV_BORDER();
  1129. SLIST_FOREACH(k, &W->h.keybind, next)
  1130. if(k->keysym == keysym && KEYPRESS_MASK(k->mod) == KEYPRESS_MASK(ke->state)
  1131. && k->func)
  1132. {
  1133. if(k->func == uicb_client_resize_Right)
  1134. _fac_resize(c, Right, ATOI(k->cmd));
  1135. else if(k->func == uicb_client_resize_Left)
  1136. _fac_resize(c, Left, ATOI(k->cmd));
  1137. else if(k->func == uicb_client_resize_Top)
  1138. _fac_resize(c, Top, ATOI(k->cmd));
  1139. else if(k->func == uicb_client_resize_Bottom)
  1140. _fac_resize(c, Bottom, ATOI(k->cmd));
  1141. else
  1142. {
  1143. k->func(k->cmd);
  1144. keysym = XK_Escape;
  1145. }
  1146. }
  1147. _REV_BORDER();
  1148. /* Gtfo of this loop */
  1149. if(keysym == XK_Return)
  1150. break;
  1151. else if(keysym == XK_Escape)
  1152. {
  1153. b = false;
  1154. break;
  1155. }
  1156. XSync(W->dpy, False);
  1157. }
  1158. XNextEvent(W->dpy, &ev);
  1159. } while(ev.type != KeyPress);
  1160. _REV_BORDER();
  1161. /* Success, resize clients */
  1162. if(b)
  1163. {
  1164. client_apply_tgeo(c->tag);
  1165. layout_save_set(c->tag);
  1166. }
  1167. /* Aborted with escape, Set back original geos */
  1168. else
  1169. {
  1170. FOREACH_NFCLIENT(gc, &c->tag->clients, tnext)
  1171. {
  1172. gc->tgeo = gc->geo;
  1173. gc->flags &= ~CLIENT_DID_WINSIZE;
  1174. }
  1175. }
  1176. XUngrabServer(W->dpy);
  1177. XUngrabKeyboard(W->dpy, CurrentTime);
  1178. }
  1179. void
  1180. client_fac_hint(struct client *c)
  1181. {
  1182. int w = c->sizeh[MINW] + c->border + c->border;
  1183. int h = c->sizeh[MINH] + c->tbarw + c->border;
  1184. if(c->geo.h < h)
  1185. _fac_resize(c, Top, (h - c->geo.h));
  1186. if(c->tgeo.h < h)
  1187. _fac_resize(c, Bottom, (h - c->tgeo.h));
  1188. if(c->geo.w < w)
  1189. _fac_resize(c, Left, (w - c->geo.w));
  1190. if(c->tgeo.w < w)
  1191. _fac_resize(c, Right, (w - c->tgeo.w));
  1192. client_apply_tgeo(c->tag);
  1193. }
  1194. void
  1195. client_remove(struct client *c)
  1196. {
  1197. c->flags |= CLIENT_DYING;
  1198. client_untab(c);
  1199. XGrabServer(W->dpy);
  1200. XSetErrorHandler(wmfs_error_handler_dummy);
  1201. XReparentWindow(W->dpy, c->win, W->root, c->rgeo.x, c->rgeo.y);
  1202. XUngrabButton(W->dpy, AnyButton, AnyModifier, c->win);
  1203. ewmh_set_wm_state(c->win, WithdrawnState);
  1204. XSync(W->dpy, false);
  1205. XSetErrorHandler(wmfs_error_handler);
  1206. XUngrabServer(W->dpy);
  1207. SLIST_REMOVE(&W->h.client, c, client, next);
  1208. tag_client(NULL, c);
  1209. /* Remove frame */
  1210. if(c->titlebar)
  1211. barwin_remove(c->titlebar);
  1212. XDestroyWindow(W->dpy, c->frame);
  1213. free(c);
  1214. ewmh_get_client_list();
  1215. }
  1216. void
  1217. uicb_client_toggle_free(Uicb cmd)
  1218. {
  1219. struct client *c;
  1220. (void)cmd;
  1221. if(!(W->client))
  1222. return;
  1223. W->client->flags ^= CLIENT_FREE;
  1224. layout_client(W->client);
  1225. /* Set tabbed client of toggled client as free */
  1226. if(W->client->flags & CLIENT_TABMASTER)
  1227. {
  1228. SLIST_FOREACH(c, &W->client->tag->clients, tnext)
  1229. if(c->tabmaster == W->client && c != W->client)
  1230. c->flags ^= CLIENT_FREE;
  1231. }
  1232. }
  1233. void uicb_client_toggle_ignore_tag(Uicb cmd)
  1234. {
  1235. struct client *c;
  1236. (void)cmd;
  1237. if(!(W->client))
  1238. return;
  1239. W->client->flags ^= CLIENT_IGNORE_TAG;
  1240. /* Set tabbed client of toggled client as ignore_tag */
  1241. if(W->client->flags & CLIENT_TABMASTER)
  1242. {
  1243. SLIST_FOREACH(c, &W->client->tag->clients, tnext)
  1244. if(c->tabmaster == W->client && c != W->client)
  1245. c->flags ^= CLIENT_IGNORE_TAG;
  1246. }
  1247. }
  1248. void
  1249. uicb_client_tab_next_opened(Uicb cmd)
  1250. {
  1251. (void)cmd;
  1252. W->flags ^= WMFS_TABNOC;
  1253. }
  1254. void
  1255. client_free(void)
  1256. {
  1257. FREE_LIST(client, W->h.client);
  1258. }