client.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 "util.h"
  9. #include "barwin.h"
  10. #include "ewmh.h"
  11. #include "layout.h"
  12. #include "draw.h"
  13. #define CLIENT_MOUSE_MOD Mod1Mask
  14. #define CLIENT_RESIZE_DIR(D) \
  15. void uicb_client_resize_##D(Uicb cmd) \
  16. { \
  17. if(W->client) \
  18. client_fac_resize(W->client, D, ATOI(cmd)); \
  19. }
  20. #define CLIENT_ACTION_DIR(A, D) \
  21. void uicb_client_##A##_##D(Uicb cmd) \
  22. { \
  23. (void)cmd; \
  24. struct client *c; \
  25. if(W->client && (c = client_next_with_pos(W->client, D))) \
  26. client_##A(c); \
  27. }
  28. #define CLIENT_ACTION_LIST(A, L) \
  29. void uicb_client_##A##_##L(Uicb cmd) \
  30. { \
  31. (void)cmd; \
  32. struct client *c; \
  33. if(W->client && (c = client_##L(W->client))) \
  34. client_##A(c); \
  35. }
  36. /* uicb_client_resize_dir() */
  37. CLIENT_RESIZE_DIR(Right)
  38. CLIENT_RESIZE_DIR(Left)
  39. CLIENT_RESIZE_DIR(Top)
  40. CLIENT_RESIZE_DIR(Bottom)
  41. /* uicb_client_focus_dir() */
  42. CLIENT_ACTION_DIR(focus, Right)
  43. CLIENT_ACTION_DIR(focus, Left)
  44. CLIENT_ACTION_DIR(focus, Top)
  45. CLIENT_ACTION_DIR(focus, Bottom)
  46. /* uicb_client_swapsel_dir() */
  47. #define client_swapsel(c) client_swap(W->client, c)
  48. CLIENT_ACTION_DIR(swapsel, Right)
  49. CLIENT_ACTION_DIR(swapsel, Left)
  50. CLIENT_ACTION_DIR(swapsel, Top)
  51. CLIENT_ACTION_DIR(swapsel, Bottom)
  52. /* uicb_client_focus_next/prev() */
  53. CLIENT_ACTION_LIST(focus, next)
  54. CLIENT_ACTION_LIST(focus, prev)
  55. /* uicb_client_swapsel_next/prev() */
  56. CLIENT_ACTION_LIST(swapsel, next)
  57. CLIENT_ACTION_LIST(swapsel, prev)
  58. /** Send a ConfigureRequest event to the struct client
  59. * \param c struct client pointer
  60. */
  61. inline void
  62. client_configure(struct client *c)
  63. {
  64. XConfigureEvent ev =
  65. {
  66. .type = ConfigureNotify,
  67. .event = c->win,
  68. .window = c->win,
  69. .x = c->geo.x,
  70. .y = c->geo.y,
  71. .width = c->geo.w,
  72. .height = c->geo.h,
  73. .above = None,
  74. .border_width = 0,
  75. .override_redirect = 0
  76. };
  77. XSendEvent(W->dpy, c->win, False, StructureNotifyMask, (XEvent *)&ev);
  78. XSync(W->dpy, False);
  79. }
  80. struct client*
  81. client_gb_win(Window w)
  82. {
  83. struct client *c = SLIST_FIRST(&W->h.client);
  84. while(c && c->win != w)
  85. c = SLIST_NEXT(c, next);
  86. return c;
  87. }
  88. struct client*
  89. client_gb_pos(struct tag *t, int x, int y)
  90. {
  91. struct client *c = SLIST_FIRST(&t->clients);
  92. while(c)
  93. {
  94. if(INAREA(x, y, c->geo))
  95. return c;
  96. c = SLIST_NEXT(c, tnext);
  97. }
  98. return NULL;
  99. }
  100. /*
  101. * Get client left/right/top/bottom of selected client
  102. */
  103. struct client*
  104. client_next_with_pos(struct client *bc, enum position p)
  105. {
  106. struct client *c;
  107. static const char scanfac[PositionLast] = { +10, -10, 0, 0 };
  108. enum position ip = Bottom - p;
  109. int x = bc->geo.x + ((p == Right) ? bc->geo.w : 0);
  110. int y = bc->geo.y + ((p == Bottom) ? bc->geo.h : 0);
  111. if(p > Left)
  112. x += bc->geo.w >> 1;
  113. if(LDIR(p))
  114. y += bc->geo.h >> 1;
  115. /* Scan in right direction to next(p) physical client */
  116. while((c = client_gb_pos(bc->tag, x, y)) == bc)
  117. {
  118. x += scanfac[p];
  119. y += scanfac[ip];
  120. }
  121. return c;
  122. }
  123. void
  124. client_swap(struct client *c1, struct client *c2)
  125. {
  126. struct tag *t;
  127. struct geo g;
  128. if(c1 == c2 || !c1 || !c2)
  129. return;
  130. t = c1->tag;
  131. g = c1->geo;
  132. swap_ptr((void**)&c1->screen, (void**)&c2->screen);
  133. tag_client(c2->tag, c1);
  134. tag_client(t, c2);
  135. client_moveresize(c1, &c2->geo);
  136. client_moveresize(c2, &g);
  137. }
  138. static void
  139. client_grabbuttons(struct client *c, bool focused)
  140. {
  141. XUngrabButton(W->dpy, AnyButton, AnyModifier, c->win);
  142. if(focused)
  143. {
  144. int i = 0;
  145. while(i++ != Button5)
  146. {
  147. XGrabButton(W->dpy, i, CLIENT_MOUSE_MOD, c->win, False,
  148. ButtonMask, GrabModeAsync, GrabModeSync, None, None);
  149. XGrabButton(W->dpy, i, CLIENT_MOUSE_MOD | LockMask, c->win, False,
  150. ButtonMask, GrabModeAsync, GrabModeSync, None, None);
  151. XGrabButton(W->dpy, i, CLIENT_MOUSE_MOD | W->numlockmask, c->win, False,
  152. ButtonMask, GrabModeAsync, GrabModeSync, None, None);
  153. XGrabButton(W->dpy, i, CLIENT_MOUSE_MOD | LockMask | W->numlockmask, c->win, False,
  154. ButtonMask, GrabModeAsync, GrabModeSync, None, None);
  155. }
  156. return;
  157. }
  158. XGrabButton(W->dpy, AnyButton, AnyModifier, c->win, False,
  159. ButtonMask, GrabModeAsync, GrabModeSync, None, None);
  160. }
  161. static inline void
  162. client_draw_bord(struct client *c)
  163. {
  164. struct geo ge = { 0, 0, c->screen->ugeo.w, c->screen->ugeo.h };
  165. draw_rect(c->tag->frame, ge, THEME_DEFAULT->client_n.bg);
  166. /* Selected client's border */
  167. if(W->client)
  168. draw_rect(W->client->tag->frame, W->client->tag->sel->geo, THEME_DEFAULT->client_s.bg);
  169. }
  170. void
  171. client_focus(struct client *c)
  172. {
  173. /* Unfocus selected */
  174. if(W->client && W->client != c)
  175. client_grabbuttons(W->client, false);
  176. /* Focus c */
  177. if((W->client = c))
  178. {
  179. c->tag->sel = c;
  180. client_draw_bord(c);
  181. client_grabbuttons(c, true);
  182. XSetInputFocus(W->dpy, c->win, RevertToPointerRoot, CurrentTime);
  183. }
  184. else
  185. {
  186. W->client = W->screen->seltag->sel = NULL;
  187. XSetInputFocus(W->dpy, W->root, RevertToPointerRoot, CurrentTime);
  188. }
  189. }
  190. /** Get a client name
  191. * \param c struct client pointer
  192. */
  193. void
  194. client_get_name(struct client *c)
  195. {
  196. Atom rt;
  197. int rf;
  198. unsigned long ir, il;
  199. /* This one instead XFetchName for utf8 name support */
  200. if(XGetWindowProperty(W->dpy, c->win, ATOM("_NET_WM_NAME"), 0, 4096,
  201. False, ATOM("UTF8_STRING"), &rt, &rf, &ir, &il, (unsigned char**)&c->title) != Success)
  202. XGetWindowProperty(W->dpy, c->win, ATOM("WM_NAME"), 0, 4096,
  203. False, ATOM("UTF8_STRING"), &rt, &rf, &ir, &il, (unsigned char**)&c->title);
  204. /* Still no title... */
  205. if(!c->title)
  206. XFetchName(W->dpy, c->win, &(c->title));
  207. }
  208. /** Close a client
  209. * \param c struct client pointer
  210. */
  211. void
  212. client_close(struct client *c)
  213. {
  214. int proto;
  215. XEvent ev;
  216. Atom *atom = NULL;
  217. /* Event will call client_remove */
  218. if(XGetWMProtocols(W->dpy, c->win, &atom, &proto) && atom)
  219. {
  220. while(proto--)
  221. if(atom[proto] == ATOM("WM_DELETE_WINDOW"))
  222. {
  223. ev.type = ClientMessage;
  224. ev.xclient.window = c->win;
  225. ev.xclient.message_type = ATOM("WM_PROTOCOLS");
  226. ev.xclient.format = 32;
  227. ev.xclient.data.l[0] = ATOM("WM_DELETE_WINDOW");
  228. ev.xclient.data.l[1] = CurrentTime;
  229. XSendEvent(W->dpy, c->win, False, NoEventMask, &ev);
  230. XFree(atom);
  231. return;
  232. }
  233. }
  234. XKillClient(W->dpy, c->win);
  235. }
  236. void
  237. uicb_client_close(Uicb cmd)
  238. {
  239. (void)cmd;
  240. if(W->client)
  241. client_close(W->client);
  242. }
  243. static void
  244. client_get_sizeh(struct client *c)
  245. {
  246. long msize;
  247. XSizeHints size;
  248. memset(c->sizeh, 0, SHLAST);
  249. if(!XGetWMNormalHints(W->dpy, c->win, &size, &msize) || !size.flags)
  250. size.flags = PSize;
  251. /* base */
  252. if(size.flags & PBaseSize)
  253. {
  254. c->sizeh[BASEW] = size.base_width;
  255. c->sizeh[BASEH] = size.base_height;
  256. }
  257. else if(size.flags & PMinSize)
  258. {
  259. c->sizeh[BASEW] = size.min_width;
  260. c->sizeh[BASEH] = size.min_height;
  261. }
  262. /* inc */
  263. if(size.flags & PResizeInc)
  264. {
  265. c->sizeh[INCW] = size.width_inc;
  266. c->sizeh[INCH] = size.height_inc;
  267. }
  268. /* max */
  269. if(size.flags & PMaxSize)
  270. {
  271. c->sizeh[MAXW] = size.max_width;
  272. c->sizeh[MAXH] = size.max_height;
  273. }
  274. /* min */
  275. if(size.flags & PMinSize)
  276. {
  277. c->sizeh[MINW] = (size.min_width ? size.min_width : 1);
  278. c->sizeh[MINH] = (size.min_height ? size.min_height: 1);
  279. }
  280. else if(size.flags & PBaseSize)
  281. {
  282. c->sizeh[MINW] = (size.base_width ? size.base_width : 1);
  283. c->sizeh[MINH] = (size.base_height ? size.base_height : 1);
  284. }
  285. /* aspect */
  286. if(size.flags & PAspect)
  287. {
  288. c->sizeh[MINAX] = size.min_aspect.x;
  289. c->sizeh[MAXAX] = size.max_aspect.x;
  290. c->sizeh[MINAY] = size.min_aspect.y;
  291. c->sizeh[MAXAY] = size.max_aspect.y;
  292. }
  293. if(c->sizeh[MAXW] && c->sizeh[MINW] && c->sizeh[MAXH] && c->sizeh[MINH]
  294. && c->sizeh[MAXW] == c->sizeh[MINW] && c->sizeh[MAXH] == c->sizeh[MINH])
  295. c->flags |= CLIENT_HINT_FLAG;
  296. }
  297. struct client*
  298. client_new(Window w, XWindowAttributes *wa)
  299. {
  300. struct client *c = xcalloc(1, sizeof(struct client));
  301. /* C attributes */
  302. c->win = w;
  303. c->screen = W->screen;
  304. c->flags = 0;
  305. c->tag = NULL;
  306. /* struct geometry */
  307. c->geo.x = wa->x;
  308. c->geo.y = wa->y;
  309. c->geo.w = wa->width;
  310. c->geo.h = wa->height;
  311. c->tgeo = c->wgeo = c->geo;
  312. /* Set tag */
  313. client_get_sizeh(c);
  314. tag_client(W->screen->seltag, c);
  315. /* X window attributes */
  316. XSelectInput(W->dpy, w, EnterWindowMask | LeaveWindowMask | StructureNotifyMask | PropertyChangeMask);
  317. XSetWindowBorderWidth(W->dpy, w, 0);
  318. client_grabbuttons(c, false);
  319. /* Attach */
  320. SLIST_INSERT_HEAD(&W->h.client, c, next);
  321. /* Map */
  322. WIN_STATE(w, Map);
  323. ewmh_set_wm_state(w, NormalState);
  324. client_get_name(c);
  325. client_focus(c);
  326. client_configure(c);
  327. return c;
  328. }
  329. static void
  330. client_geo_hints(struct geo *g, int *s)
  331. {
  332. /* base */
  333. g->w -= s[BASEW];
  334. g->h -= s[BASEH];
  335. /* aspect */
  336. if((s[MINAY] | s[MAXAY] | s[MINAX] | s[MAXAX]) > 0)
  337. {
  338. if(g->w * s[MAXAY] > g->h * s[MAXAX])
  339. g->w = g->h * s[MAXAX] / s[MAXAY];
  340. else if(g->w * s[MINAY] < g->h * s[MINAX])
  341. g->h = g->w * s[MINAY] / s[MINAX];
  342. }
  343. /* incremental */
  344. if(s[INCW])
  345. g->w -= g->w % s[INCW];
  346. if(s[INCH])
  347. g->h -= g->h % s[INCH];
  348. /* base dimension */
  349. g->w += s[BASEW];
  350. g->h += s[BASEH];
  351. if(s[MINW] > 0 && g->w < s[MINW])
  352. g->w = s[MINW];
  353. if(s[MINH] > 0 && g->h < s[MINH])
  354. g->h = s[MINH];
  355. if(s[MAXW] > 0 && g->w > s[MAXW])
  356. g->w = s[MAXW];
  357. if(s[MAXH] > 0 && g->h > s[MAXH])
  358. g->h = s[MAXH];
  359. }
  360. void
  361. client_moveresize(struct client *c, struct geo *g)
  362. {
  363. int bord = THEME_DEFAULT->client_border_width;
  364. c->geo = *g;
  365. /* Window geo */
  366. c->wgeo.x = g->x + bord;
  367. c->wgeo.y = g->y + bord ;
  368. c->wgeo.w = g->w - (bord << 1);
  369. c->wgeo.h = g->h - (bord << 1);
  370. client_geo_hints(&c->wgeo, (int*)c->sizeh);
  371. XMoveResizeWindow(W->dpy, c->win,
  372. c->wgeo.x, c->wgeo.y,
  373. c->wgeo.w, c->wgeo.h);
  374. client_draw_bord(c);
  375. client_configure(c);
  376. }
  377. void
  378. client_maximize(struct client *c)
  379. {
  380. c->geo = c->tag->screen->ugeo;
  381. c->geo.x = c->geo.y = 0; /* Frame x/y, not screen geo */
  382. c->geo.w = c->tag->screen->ugeo.w;
  383. c->geo.h = c->tag->screen->ugeo.h;
  384. client_moveresize(c, &c->geo);
  385. }
  386. void
  387. client_fac_resize(struct client *c, enum position p, int fac)
  388. {
  389. struct client *gc = client_next_with_pos(c, p);
  390. enum position rp = RPOS(p);
  391. if(!gc || gc->screen != c->screen)
  392. return;
  393. /* Check futur size/pos */
  394. if(!client_fac_geo(c, p, fac)
  395. || !client_fac_geo(gc, rp, -fac)
  396. || !client_fac_check_row(c, p, fac)
  397. || !client_fac_check_row(gc, rp, -fac))
  398. return;
  399. /* Simple resize with only c & gc */
  400. if(GEO_CHECK2(c->geo, gc->geo, p))
  401. {
  402. client_moveresize(c, &c->tgeo);
  403. client_moveresize(gc, &gc->tgeo);
  404. }
  405. /* Resize with row parents */
  406. else
  407. {
  408. client_fac_arrange_row(c, p, fac);
  409. client_fac_arrange_row(gc, rp, -fac);
  410. }
  411. }
  412. void
  413. client_remove(struct client *c)
  414. {
  415. XGrabServer(W->dpy);
  416. XSetErrorHandler(wmfs_error_handler_dummy);
  417. XReparentWindow(W->dpy, c->win, W->root, c->geo.x, c->geo.y);
  418. /* Remove from global client list */
  419. SLIST_REMOVE(&W->h.client, c, client, next);
  420. tag_client(NULL, c);
  421. ewmh_set_wm_state(c->win, WithdrawnState);
  422. XUngrabServer(W->dpy);
  423. XSync(W->dpy, False);
  424. XSetErrorHandler(wmfs_error_handler);
  425. free(c);
  426. }
  427. void
  428. client_free(void)
  429. {
  430. FREE_LIST(client, W->h.client);
  431. }