client.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. 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. inline 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. /** Get client left/right/top/bottom of selected client
  101. *\param bc Base client
  102. *\param pos Position
  103. *\return Client found or NULL
  104. */
  105. struct client*
  106. client_next_with_pos(struct client *bc, Position p)
  107. {
  108. struct client *c;
  109. int x, y;
  110. const static char scanfac[PositionLast] = { +10, -10, 0, 0 };
  111. Position ip = Bottom - p;
  112. /*
  113. * Set start place of pointer (edge with position
  114. * of base client) for faster scanning.
  115. */
  116. x = bc->geo.x + ((p == Right) ? bc->geo.w : 0);
  117. y = bc->geo.y + ((p == Bottom) ? bc->geo.h : 0);
  118. y += ((LDIR(p)) ? (bc->geo.h >> 1) : 0);
  119. x += ((p > Left) ? (bc->geo.w >> 1) : 0);
  120. /* Scan in right direction to next(p) physical client */
  121. while((c = client_gb_pos(bc->tag, x, y)) == bc)
  122. {
  123. x += scanfac[p];
  124. y += scanfac[ip];
  125. }
  126. return c;
  127. }
  128. void
  129. client_swap(struct client *c1, struct client *c2)
  130. {
  131. struct tag *t;
  132. struct geo g;
  133. if(c1 == c2 || !c1 || !c2)
  134. return;
  135. t = c1->tag;
  136. g = c1->geo;
  137. swap_ptr((void**)&c1->screen, (void**)&c2->screen);
  138. tag_client(c2->tag, c1);
  139. tag_client(t, c2);
  140. client_moveresize(c1, c2->geo);
  141. client_moveresize(c2, g);
  142. }
  143. static void
  144. client_grabbuttons(struct client *c, bool focused)
  145. {
  146. XUngrabButton(W->dpy, AnyButton, AnyModifier, c->win);
  147. if(focused)
  148. {
  149. int i = 0;
  150. while(i++ != Button5)
  151. {
  152. XGrabButton(W->dpy, i, CLIENT_MOUSE_MOD, c->win, False,
  153. ButtonMask, GrabModeAsync, GrabModeSync, None, None);
  154. XGrabButton(W->dpy, i, CLIENT_MOUSE_MOD | LockMask, c->win, False,
  155. ButtonMask, GrabModeAsync, GrabModeSync, None, None);
  156. XGrabButton(W->dpy, i, CLIENT_MOUSE_MOD | W->numlockmask, c->win, False,
  157. ButtonMask, GrabModeAsync, GrabModeSync, None, None);
  158. XGrabButton(W->dpy, i, CLIENT_MOUSE_MOD | LockMask | W->numlockmask, c->win, False,
  159. ButtonMask, GrabModeAsync, GrabModeSync, None, None);
  160. }
  161. return;
  162. }
  163. XGrabButton(W->dpy, AnyButton, AnyModifier, c->win, False,
  164. ButtonMask, GrabModeAsync, GrabModeSync, None, None);
  165. }
  166. static inline void
  167. client_draw_bord(struct client *c)
  168. {
  169. struct geo g = { 0, 0, c->screen->ugeo.w, c->screen->ugeo.h };
  170. draw_rect(c->tag->frame, g, THEME_DEFAULT->client_n.bg);
  171. /* Selected client's border */
  172. if(W->client)
  173. draw_rect(W->client->tag->frame, W->client->tag->sel->geo, THEME_DEFAULT->client_s.bg);
  174. }
  175. void
  176. client_focus(struct client *c)
  177. {
  178. /* Unfocus selected */
  179. if(W->client && W->client != c)
  180. client_grabbuttons(W->client, false);
  181. /* Focus c */
  182. if((W->client = c))
  183. {
  184. c->tag->sel = c;
  185. client_draw_bord(c);
  186. client_grabbuttons(c, true);
  187. XSetInputFocus(W->dpy, c->win, RevertToPointerRoot, CurrentTime);
  188. }
  189. else
  190. {
  191. W->client = W->screen->seltag->sel = NULL;
  192. XSetInputFocus(W->dpy, W->root, RevertToPointerRoot, CurrentTime);
  193. }
  194. }
  195. /** Get a client name
  196. * \param c struct client pointer
  197. */
  198. void
  199. client_get_name(struct client *c)
  200. {
  201. Atom rt;
  202. int rf;
  203. unsigned long ir, il;
  204. /* This one instead XFetchName for utf8 name support */
  205. if(XGetWindowProperty(W->dpy, c->win, ATOM("_NET_WM_NAME"), 0, 4096,
  206. False, ATOM("UTF8_STRING"), &rt, &rf, &ir, &il, (unsigned char**)&c->title) != Success)
  207. XGetWindowProperty(W->dpy, c->win, ATOM("WM_NAME"), 0, 4096,
  208. False, ATOM("UTF8_STRING"), &rt, &rf, &ir, &il, (unsigned char**)&c->title);
  209. /* Still no title... */
  210. if(!c->title)
  211. XFetchName(W->dpy, c->win, &(c->title));
  212. }
  213. /** Close a client
  214. * \param c struct client pointer
  215. */
  216. void
  217. client_close(struct client *c)
  218. {
  219. int proto;
  220. XEvent ev;
  221. Atom *atom = NULL;
  222. /* Event will call client_remove */
  223. if(XGetWMProtocols(W->dpy, c->win, &atom, &proto) && atom)
  224. {
  225. while(proto--)
  226. if(atom[proto] == ATOM("WM_DELETE_WINDOW"))
  227. {
  228. ev.type = ClientMessage;
  229. ev.xclient.window = c->win;
  230. ev.xclient.message_type = ATOM("WM_PROTOCOLS");
  231. ev.xclient.format = 32;
  232. ev.xclient.data.l[0] = ATOM("WM_DELETE_WINDOW");
  233. ev.xclient.data.l[1] = CurrentTime;
  234. XSendEvent(W->dpy, c->win, False, NoEventMask, &ev);
  235. XFree(atom);
  236. return;
  237. }
  238. }
  239. XKillClient(W->dpy, c->win);
  240. }
  241. void
  242. uicb_client_close(Uicb cmd)
  243. {
  244. if(W->client)
  245. client_close(W->client);
  246. }
  247. struct client*
  248. client_new(Window w, XWindowAttributes *wa)
  249. {
  250. struct client *c = xcalloc(1, sizeof(struct client));
  251. /* C attributes */
  252. c->win = w;
  253. c->screen = W->screen;
  254. c->flags = 0;
  255. c->tag = NULL;
  256. /* struct geometry */
  257. c->geo.x = wa->x;
  258. c->geo.y = wa->y;
  259. c->geo.w = wa->width;
  260. c->geo.h = wa->height;
  261. c->tgeo = c->wgeo = c->geo;
  262. /* Set tag */
  263. tag_client(W->screen->seltag, c);
  264. /* X window attributes */
  265. XSelectInput(W->dpy, w, EnterWindowMask | LeaveWindowMask | StructureNotifyMask | PropertyChangeMask);
  266. XSetWindowBorderWidth(W->dpy, w, 0);
  267. client_grabbuttons(c, false);
  268. /* Attach */
  269. SLIST_INSERT_HEAD(&W->h.client, c, next);
  270. /* Map */
  271. WIN_STATE(w, Map);
  272. ewmh_set_wm_state(w, NormalState);
  273. client_get_name(c);
  274. client_focus(c);
  275. client_configure(c);
  276. return c;
  277. }
  278. void
  279. client_moveresize(struct client *c, struct geo g)
  280. {
  281. int bord = THEME_DEFAULT->client_border_width;
  282. c->geo = g;
  283. /* Window geo */
  284. c->wgeo.x = g.x + bord;
  285. c->wgeo.y = g.y + bord ;
  286. c->wgeo.w = g.w - (bord << 1);
  287. c->wgeo.h = g.h - (bord << 1);
  288. XMoveResizeWindow(W->dpy, c->win,
  289. c->wgeo.x, c->wgeo.y,
  290. c->wgeo.w, c->wgeo.h);
  291. client_draw_bord(c);
  292. client_configure(c);
  293. }
  294. void
  295. client_maximize(struct client *c)
  296. {
  297. c->geo = c->tag->screen->ugeo;
  298. c->geo.x = c->geo.y = 0; /* Frame x/y, not screen geo */
  299. c->geo.w = c->tag->screen->ugeo.w;
  300. c->geo.h = c->tag->screen->ugeo.h;
  301. client_moveresize(c, c->geo);
  302. }
  303. void
  304. client_fac_resize(struct client *c, Position p, int fac)
  305. {
  306. struct client *gc = client_next_with_pos(c, p);
  307. Position rp = RPOS(p);
  308. if(!gc || gc->screen != c->screen)
  309. return;
  310. /* Check futur size/pos */
  311. if(!client_fac_geo(c, p, fac)
  312. || !client_fac_geo(gc, rp, -fac)
  313. || !client_fac_check_row(c, p, fac)
  314. || !client_fac_check_row(gc, rp, -fac))
  315. return;
  316. /* Simple resize with only c & gc */
  317. if(GEO_CHECK2(c->geo, gc->geo, p))
  318. {
  319. client_moveresize(c, c->tgeo);
  320. client_moveresize(gc, gc->tgeo);
  321. }
  322. /* Resize with row parents */
  323. else
  324. {
  325. client_fac_arrange_row(c, p, fac);
  326. client_fac_arrange_row(gc, rp, -fac);
  327. }
  328. }
  329. void
  330. client_remove(struct client *c)
  331. {
  332. XGrabServer(W->dpy);
  333. XSetErrorHandler(wmfs_error_handler_dummy);
  334. XReparentWindow(W->dpy, c->win, W->root, c->geo.x, c->geo.y);
  335. /* Remove from global client list */
  336. SLIST_REMOVE(&W->h.client, c, client, next);
  337. tag_client(NULL, c);
  338. ewmh_set_wm_state(c->win, WithdrawnState);
  339. XUngrabServer(W->dpy);
  340. XSync(W->dpy, False);
  341. XSetErrorHandler(wmfs_error_handler);
  342. free(c);
  343. }
  344. void
  345. client_free(void)
  346. {
  347. FREE_LIST(client, W->h.client);
  348. }