event.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * wmfs2 by Martin Duquesnoy <xorg62@gmail.com> { for(i = 2011; i < 2111; ++i) ©(i); }
  3. * For license, see COPYING.
  4. */
  5. #include "event.h"
  6. #include "ewmh.h"
  7. #include "config.h"
  8. #include "util.h"
  9. #include "wmfs.h"
  10. #include "client.h"
  11. #include "barwin.h"
  12. #include "screen.h"
  13. #include "systray.h"
  14. #include "infobar.h"
  15. #define EVDPY(e) (e)->xany.display
  16. #define MOUSE_DO_BIND(m) \
  17. if(m->button == ev->button) \
  18. if(!m->use_area || (m->use_area && INAREA(ev->x, ev->y, m->area))) \
  19. if(m->func) \
  20. m->func(m->cmd);
  21. static void
  22. event_buttonpress(XEvent *e)
  23. {
  24. XButtonEvent *ev = &e->xbutton;
  25. struct mousebind *m;
  26. struct barwin *b;
  27. struct client *c;
  28. screen_update_sel();
  29. status_flush_surface();
  30. SLIST_FOREACH(b, &W->h.barwin, next)
  31. if(b->win == ev->window)
  32. {
  33. W->last_clicked_barwin = b;
  34. SLIST_FOREACH(m, &b->mousebinds, next)
  35. MOUSE_DO_BIND(m);
  36. SLIST_FOREACH(m, &b->statusmousebinds, next)
  37. MOUSE_DO_BIND(m);
  38. break;
  39. }
  40. if((c = client_gb_win(ev->window)) && c != W->client
  41. && ev->button == 1 && W->cfocus & CFOCUS_CLICK)
  42. client_focus(c);
  43. }
  44. static void
  45. event_enternotify(XEvent *e)
  46. {
  47. XCrossingEvent *ev = &e->xcrossing;
  48. struct client *c;
  49. if((ev->mode != NotifyNormal
  50. || ev->detail == NotifyInferior)
  51. && ev->window != W->root)
  52. return;
  53. if(ev->window == W->systray.win || systray_find(ev->window))
  54. return;
  55. if((c = client_gb_win(ev->window))
  56. || (c = client_gb_frame(ev->window)))
  57. {
  58. if(c->flags & CLIENT_IGNORE_ENTER)
  59. c->flags ^= CLIENT_IGNORE_ENTER;
  60. else if(c->tag->flags & TAG_IGNORE_ENTER)
  61. c->tag->flags ^= TAG_IGNORE_ENTER;
  62. else if(c != W->client && !(c->flags & CLIENT_TABBED)
  63. && W->cfocus & CFOCUS_ENTER)
  64. client_focus(c);
  65. }
  66. }
  67. static void
  68. event_clientmessageevent(XEvent *e)
  69. {
  70. XClientMessageEvent *ev = &e->xclient;
  71. struct client *c;
  72. struct _systray *sy;
  73. int type = 0;
  74. while(type < net_last && W->net_atom[type] != ev->message_type)
  75. ++type;
  76. /*
  77. * Systray message
  78. * _NET_WM_SYSTRAY_TRAY_OPCODE
  79. */
  80. if(ev->window == W->systray.win && type == net_system_tray_opcode)
  81. {
  82. if(ev->data.l[1] == XEMBED_EMBEDDED_NOTIFY)
  83. {
  84. systray_add(ev->data.l[2]);
  85. systray_update();
  86. }
  87. else if(ev->data.l[1] == XEMBED_REQUEST_FOCUS)
  88. {
  89. if((sy = systray_find(ev->data.l[2])))
  90. ewmh_send_message(sy->win, sy->win, "_XEMBED", XEMBED_FOCUS_IN,
  91. XEMBED_FOCUS_CURRENT, 0, 0, 0);
  92. }
  93. }
  94. else if(ev->window == W->root)
  95. {
  96. /* WMFS message */
  97. if(ev->data.l[4])
  98. {
  99. /* Manage _WMFS_FUNCTION && _WMFS_CMD */
  100. if(type == wmfs_function || type == wmfs_cmd)
  101. {
  102. Atom rt;
  103. int d;
  104. long unsigned int len, il;
  105. unsigned char *ret = NULL, *ret_cmd = NULL;
  106. void (*func)(Uicb);
  107. if(XGetWindowProperty(EVDPY(e), W->root, W->net_atom[wmfs_function], 0, 65536,
  108. False, W->net_atom[utf8_string], &rt, &d,
  109. &len, &il, &ret) == Success
  110. && ret && ((func = uicb_name_func((char*)ret))))
  111. {
  112. if(XGetWindowProperty(EVDPY(e), W->root, W->net_atom[wmfs_cmd], 0, 65536,
  113. False, W->net_atom[utf8_string], &rt, &d,
  114. &len, &il, &ret_cmd) == Success
  115. && len && ret_cmd)
  116. {
  117. func((Uicb)ret_cmd);
  118. XFree(ret_cmd);
  119. }
  120. else
  121. func(NULL);
  122. XFree(ret);
  123. }
  124. }
  125. }
  126. if(type == net_active_window)
  127. if((sy = systray_find(ev->data.l[0])))
  128. XSetInputFocus(W->dpy, sy->win, RevertToNone, CurrentTime);
  129. }
  130. switch(type)
  131. {
  132. /* _NET_WM_STATE */
  133. case net_wm_state:
  134. if((c = client_gb_win(ev->window)))
  135. ewmh_manage_state(ev->data.l, c);
  136. break;
  137. /* _NET_CLOSE_WINDOW */
  138. case net_close_window:
  139. if((c = client_gb_win(ev->window)))
  140. client_close(c);
  141. break;
  142. /* _NET_WM_DESKTOP */
  143. case net_wm_desktop:
  144. break;
  145. }
  146. }
  147. static void
  148. event_configureevent(XEvent *e)
  149. {
  150. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  151. XWindowChanges wc;
  152. struct client *c;
  153. if((c = client_gb_win(ev->window)))
  154. {
  155. if(c->flags & CLIENT_FREE)
  156. {
  157. if(ev->value_mask & CWX)
  158. c->geo.x = ev->x;
  159. if(ev->value_mask & CWY)
  160. c->geo.y = ev->y - c->tbarw - c->border - c->border;
  161. if(ev->value_mask & CWWidth)
  162. c->geo.w = ev->width + c->border + c->border;
  163. if(ev->value_mask & CWHeight)
  164. c->geo.h = ev->height + c->tbarw + c->border;
  165. client_moveresize(c, &c->geo);
  166. }
  167. else
  168. {
  169. if(ev->value_mask & CWWidth)
  170. _fac_resize(c, Right, ev->width - c->wgeo.w);
  171. if(ev->value_mask & CWHeight)
  172. _fac_resize(c, Bottom, ev->height - c->wgeo.h);
  173. client_apply_tgeo(c->tag);
  174. }
  175. }
  176. else
  177. {
  178. wc.x = ev->x;
  179. wc.y = ev->y;
  180. wc.width = ev->width;
  181. wc.height = ev->height;
  182. wc.border_width = ev->border_width;
  183. wc.sibling = ev->above;
  184. wc.stack_mode = ev->detail;
  185. XConfigureWindow(EVDPY(e), ev->window, ev->value_mask, &wc);
  186. }
  187. }
  188. static void
  189. event_destroynotify(XEvent *e)
  190. {
  191. XDestroyWindowEvent *ev = &e->xdestroywindow;
  192. struct client *c;
  193. struct _systray *s;
  194. if((c = client_gb_win(ev->window)))
  195. client_remove(c);
  196. else if((s = systray_find(ev->window)))
  197. {
  198. ewmh_set_wm_state(s->win, WithdrawnState);
  199. systray_del(s);
  200. systray_update();
  201. }
  202. }
  203. static void
  204. event_focusin(XEvent *e)
  205. {
  206. if(W->client
  207. && e->xfocus.window != W->client->win
  208. && e->xfocus.window != W->client->frame)
  209. client_focus(W->client);
  210. }
  211. static void
  212. event_maprequest(XEvent *e)
  213. {
  214. XMapRequestEvent *ev = &e->xmaprequest;
  215. XWindowAttributes at;
  216. struct _systray *s;
  217. /* Which windows to manage */
  218. if(!XGetWindowAttributes(EVDPY(e), ev->window, &at)
  219. || at.override_redirect
  220. || ewmh_manage_window_type_desktop(ev->window)
  221. || ewmh_manage_state_sticky(ev->window))
  222. return;
  223. if(!client_gb_win(ev->window))
  224. client_new(ev->window, &at, false);
  225. else if((s = systray_find(ev->window)))
  226. {
  227. ewmh_send_message(s->win, s->win, "_XEMBED", CurrentTime,
  228. XEMBED_WINDOW_ACTIVATE, 0, 0, 0);
  229. systray_update();
  230. }
  231. }
  232. static void
  233. event_mappingnotify(XEvent *e)
  234. {
  235. XMappingEvent *ev = &e->xmapping;
  236. XRefreshKeyboardMapping(ev);
  237. if(ev->request == MappingKeyboard)
  238. wmfs_grab_keys();
  239. }
  240. static void
  241. event_propertynotify(XEvent *e)
  242. {
  243. XPropertyEvent *ev = &e->xproperty;
  244. XWMHints *h;
  245. struct client *c;
  246. struct _systray *s;
  247. if(ev->state == PropertyDelete)
  248. return;
  249. if((c = client_gb_win(ev->window)))
  250. {
  251. switch(ev->atom)
  252. {
  253. case XA_WM_TRANSIENT_FOR:
  254. break;
  255. case XA_WM_NORMAL_HINTS:
  256. client_get_sizeh(c);
  257. break;
  258. case XA_WM_HINTS:
  259. if((h = XGetWMHints(EVDPY(e), c->win))
  260. && (h->flags & XUrgencyHint)
  261. && c->tag != W->screen->seltag)
  262. {
  263. c->tag->flags |= TAG_URGENT;
  264. infobar_elem_screen_update(c->screen, ElemTag);
  265. XFree(h);
  266. }
  267. break;
  268. default:
  269. if(ev->atom == XA_WM_NAME || ev->atom == W->net_atom[net_wm_name])
  270. client_get_name(c);
  271. break;
  272. }
  273. }
  274. else if((s = systray_find(ev->window)))
  275. {
  276. systray_state(s);
  277. systray_update();
  278. }
  279. }
  280. static void
  281. event_unmapnotify(XEvent *e)
  282. {
  283. XUnmapEvent *ev = &e->xunmap;
  284. struct client *c;
  285. struct _systray *s;
  286. if((c = client_gb_win(ev->window))
  287. && ev->send_event
  288. && ev->event == W->root)
  289. {
  290. Atom rt;
  291. unsigned long n, il;
  292. int d;
  293. unsigned char *ret = NULL;
  294. if(XGetWindowProperty(EVDPY(e), c->win, W->net_atom[wm_state], 0, 2,
  295. False, W->net_atom[wm_state], &rt, &d,
  296. &n, &il, &ret) == Success)
  297. if(*ret == NormalState)
  298. client_remove(c);
  299. }
  300. else if((s = systray_find(ev->window)))
  301. {
  302. systray_del(s);
  303. systray_update();
  304. }
  305. }
  306. static void
  307. event_keypress(XEvent *e)
  308. {
  309. XKeyPressedEvent *ev = &e->xkey;
  310. KeySym keysym = XkbKeycodeToKeysym(EVDPY(e), (KeyCode)ev->keycode, 0, 0);
  311. struct keybind *k;
  312. screen_update_sel();
  313. status_flush_surface();
  314. SLIST_FOREACH(k, &W->h.keybind, next)
  315. if(k->keysym == keysym && KEYPRESS_MASK(k->mod) == KEYPRESS_MASK(ev->state))
  316. if(k->func)
  317. k->func(k->cmd);
  318. }
  319. static void
  320. event_expose(XEvent *e)
  321. {
  322. XExposeEvent *ev = &e->xexpose;
  323. struct barwin *b;
  324. SLIST_FOREACH(b, &W->h.barwin, next)
  325. if(b->win == ev->window)
  326. {
  327. barwin_refresh(b);
  328. return;
  329. }
  330. }
  331. static void
  332. event_mapnotify(XEvent *e)
  333. {
  334. XMapEvent *ev = &e->xmap;
  335. struct client *c;
  336. struct _systray *s;
  337. if(ev->window != ev->event && !ev->send_event)
  338. return;
  339. if((c = client_gb_win(ev->window)))
  340. client_map(c);
  341. else if((s = systray_find(ev->window)))
  342. {
  343. ewmh_set_wm_state(s->win, NormalState);
  344. ewmh_send_message(s->win, s->win, "_XEMBED", CurrentTime,
  345. XEMBED_WINDOW_ACTIVATE, 0, 0, 0);
  346. }
  347. }
  348. static void
  349. event_selectionclearevent(XEvent *ev)
  350. {
  351. /* Getting selection if lost it */
  352. if(ev->xselectionclear.window == W->systray.win)
  353. systray_acquire();
  354. systray_update();
  355. }
  356. static void
  357. event_dummy(XEvent *e)
  358. {
  359. /* printf("%d\n", e->type);*/
  360. (void)e;
  361. }
  362. void
  363. event_init(void)
  364. {
  365. int i = MAX_EV;
  366. while(i--)
  367. event_handle[i] = event_dummy;
  368. event_handle[ButtonPress] = event_buttonpress;
  369. event_handle[ClientMessage] = event_clientmessageevent;
  370. event_handle[ConfigureRequest] = event_configureevent;
  371. event_handle[DestroyNotify] = event_destroynotify;
  372. event_handle[EnterNotify] = event_enternotify;
  373. event_handle[Expose] = event_expose;
  374. event_handle[FocusIn] = event_focusin;
  375. event_handle[KeyPress] = event_keypress;
  376. event_handle[MapNotify] = event_mapnotify;
  377. event_handle[MapRequest] = event_maprequest;
  378. event_handle[MappingNotify] = event_mappingnotify;
  379. event_handle[PropertyNotify] = event_propertynotify;
  380. /*event_handle[ReparentNotify] = event_reparentnotify;*/
  381. event_handle[SelectionClear] = event_selectionclearevent;
  382. event_handle[UnmapNotify] = event_unmapnotify;
  383. }