event.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. int d;
  103. long unsigned int len;
  104. unsigned char *ret = NULL, *ret_cmd = NULL;
  105. void (*func)(Uicb);
  106. if(XGetWindowProperty(EVDPY(e), W->root, W->net_atom[wmfs_function], 0, 65536,
  107. False, W->net_atom[utf8_string], (Atom*)&d, &d,
  108. (long unsigned int*)&d, (long unsigned int*)&d, &ret) == Success
  109. && ret && ((func = uicb_name_func((char*)ret))))
  110. {
  111. if(XGetWindowProperty(EVDPY(e), W->root, W->net_atom[wmfs_cmd], 0, 65536,
  112. False, W->net_atom[utf8_string], (Atom*)&d, &d,
  113. &len, (long unsigned int*)&d, &ret_cmd) == Success
  114. && len && ret_cmd)
  115. {
  116. func((Uicb)ret_cmd);
  117. XFree(ret_cmd);
  118. }
  119. else
  120. func(NULL);
  121. XFree(ret);
  122. }
  123. }
  124. }
  125. if(type == net_active_window)
  126. if((sy = systray_find(ev->data.l[0])))
  127. XSetInputFocus(W->dpy, sy->win, RevertToNone, CurrentTime);
  128. }
  129. switch(type)
  130. {
  131. /* _NET_WM_STATE */
  132. case net_wm_state:
  133. if((c = client_gb_win(ev->window)))
  134. ewmh_manage_state(ev->data.l, c);
  135. break;
  136. /* _NET_CLOSE_WINDOW */
  137. case net_close_window:
  138. if((c = client_gb_win(ev->window)))
  139. client_close(c);
  140. break;
  141. /* _NET_WM_DESKTOP */
  142. case net_wm_desktop:
  143. break;
  144. }
  145. }
  146. static void
  147. event_configureevent(XEvent *e)
  148. {
  149. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  150. XWindowChanges wc;
  151. struct client *c;
  152. if((c = client_gb_win(ev->window)))
  153. {
  154. if(c->flags & CLIENT_FREE)
  155. {
  156. if(ev->value_mask & CWX)
  157. c->geo.x = ev->x;
  158. if(ev->value_mask & CWY)
  159. c->geo.y = ev->y - c->tbarw - c->border - c->border;
  160. if(ev->value_mask & CWWidth)
  161. c->geo.w = ev->width + c->border + c->border;
  162. if(ev->value_mask & CWHeight)
  163. c->geo.h = ev->height + c->tbarw + c->border;
  164. client_moveresize(c, &c->geo);
  165. }
  166. else
  167. {
  168. if(ev->value_mask & CWWidth)
  169. _fac_resize(c, Right, ev->width - c->wgeo.w);
  170. if(ev->value_mask & CWHeight)
  171. _fac_resize(c, Bottom, ev->height - c->wgeo.h);
  172. client_apply_tgeo(c->tag);
  173. }
  174. }
  175. else
  176. {
  177. wc.x = ev->x;
  178. wc.y = ev->y;
  179. wc.width = ev->width;
  180. wc.height = ev->height;
  181. wc.border_width = ev->border_width;
  182. wc.sibling = ev->above;
  183. wc.stack_mode = ev->detail;
  184. XConfigureWindow(EVDPY(e), ev->window, ev->value_mask, &wc);
  185. }
  186. }
  187. static void
  188. event_destroynotify(XEvent *e)
  189. {
  190. XDestroyWindowEvent *ev = &e->xdestroywindow;
  191. struct client *c;
  192. struct _systray *s;
  193. if((c = client_gb_win(ev->window)))
  194. client_remove(c);
  195. else if((s = systray_find(ev->window)))
  196. {
  197. ewmh_set_wm_state(s->win, WithdrawnState);
  198. systray_del(s);
  199. systray_update();
  200. }
  201. }
  202. static void
  203. event_focusin(XEvent *e)
  204. {
  205. if(W->client
  206. && e->xfocus.window != W->client->win
  207. && e->xfocus.window != W->client->frame)
  208. client_focus(W->client);
  209. }
  210. static void
  211. event_maprequest(XEvent *e)
  212. {
  213. XMapRequestEvent *ev = &e->xmaprequest;
  214. XWindowAttributes at;
  215. struct _systray *s;
  216. /* Which windows to manage */
  217. if(!XGetWindowAttributes(EVDPY(e), ev->window, &at)
  218. || at.override_redirect
  219. || ewmh_manage_window_type_desktop(ev->window)
  220. || ewmh_manage_state_sticky(ev->window))
  221. return;
  222. if(!client_gb_win(ev->window))
  223. client_new(ev->window, &at, false);
  224. else if((s = systray_find(ev->window)))
  225. {
  226. ewmh_send_message(s->win, s->win, "_XEMBED", CurrentTime,
  227. XEMBED_WINDOW_ACTIVATE, 0, 0, 0);
  228. systray_update();
  229. }
  230. }
  231. static void
  232. event_mappingnotify(XEvent *e)
  233. {
  234. XMappingEvent *ev = &e->xmapping;
  235. XRefreshKeyboardMapping(ev);
  236. if(ev->request == MappingKeyboard)
  237. wmfs_grab_keys();
  238. }
  239. static void
  240. event_propertynotify(XEvent *e)
  241. {
  242. XPropertyEvent *ev = &e->xproperty;
  243. XWMHints *h;
  244. struct client *c;
  245. struct _systray *s;
  246. if(ev->state == PropertyDelete)
  247. return;
  248. if((c = client_gb_win(ev->window)))
  249. {
  250. switch(ev->atom)
  251. {
  252. case XA_WM_TRANSIENT_FOR:
  253. break;
  254. case XA_WM_NORMAL_HINTS:
  255. client_get_sizeh(c);
  256. break;
  257. case XA_WM_HINTS:
  258. if((h = XGetWMHints(EVDPY(e), c->win))
  259. && (h->flags & XUrgencyHint)
  260. && c->tag != W->screen->seltag)
  261. {
  262. c->tag->flags |= TAG_URGENT;
  263. infobar_elem_screen_update(c->screen, ElemTag);
  264. XFree(h);
  265. }
  266. break;
  267. default:
  268. if(ev->atom == XA_WM_NAME || ev->atom == W->net_atom[net_wm_name])
  269. client_get_name(c);
  270. break;
  271. }
  272. }
  273. else if((s = systray_find(ev->window)))
  274. {
  275. systray_state(s);
  276. systray_update();
  277. }
  278. }
  279. static void
  280. event_unmapnotify(XEvent *e)
  281. {
  282. XUnmapEvent *ev = &e->xunmap;
  283. struct client *c;
  284. struct _systray *s;
  285. if((c = client_gb_win(ev->window))
  286. && ev->send_event
  287. && ev->event == W->root)
  288. {
  289. int d;
  290. unsigned char *ret = NULL;
  291. if(XGetWindowProperty(EVDPY(e), c->win, W->net_atom[wm_state], 0, 2,
  292. False, W->net_atom[wm_state], (Atom*)&d, &d,
  293. (long unsigned int*)&d, (long unsigned int*)&d, &ret) == Success)
  294. if(*ret == NormalState)
  295. client_remove(c);
  296. }
  297. else if((s = systray_find(ev->window)))
  298. {
  299. systray_del(s);
  300. systray_update();
  301. }
  302. }
  303. static void
  304. event_keypress(XEvent *e)
  305. {
  306. XKeyPressedEvent *ev = &e->xkey;
  307. KeySym keysym = XKeycodeToKeysym(EVDPY(e), (KeyCode)ev->keycode, 0);
  308. struct keybind *k;
  309. screen_update_sel();
  310. status_flush_surface();
  311. SLIST_FOREACH(k, &W->h.keybind, next)
  312. if(k->keysym == keysym && KEYPRESS_MASK(k->mod) == KEYPRESS_MASK(ev->state))
  313. if(k->func)
  314. k->func(k->cmd);
  315. }
  316. static void
  317. event_expose(XEvent *e)
  318. {
  319. XExposeEvent *ev = &e->xexpose;
  320. struct barwin *b;
  321. SLIST_FOREACH(b, &W->h.barwin, next)
  322. if(b->win == ev->window)
  323. {
  324. barwin_refresh(b);
  325. return;
  326. }
  327. }
  328. static void
  329. event_mapnotify(XEvent *e)
  330. {
  331. XMapEvent *ev = &e->xmap;
  332. struct client *c;
  333. struct _systray *s;
  334. if(ev->window != ev->event && !ev->send_event)
  335. return;
  336. if((c = client_gb_win(ev->window)))
  337. client_map(c);
  338. else if((s = systray_find(ev->window)))
  339. {
  340. ewmh_set_wm_state(s->win, NormalState);
  341. ewmh_send_message(s->win, s->win, "_XEMBED", CurrentTime,
  342. XEMBED_WINDOW_ACTIVATE, 0, 0, 0);
  343. }
  344. }
  345. static void
  346. event_selectionclearevent(XEvent *ev)
  347. {
  348. /* Getting selection if lost it */
  349. if(ev->xselectionclear.window == W->systray.win)
  350. systray_acquire();
  351. systray_update();
  352. }
  353. static void
  354. event_dummy(XEvent *e)
  355. {
  356. /* printf("%d\n", e->type);*/
  357. (void)e;
  358. }
  359. void
  360. event_init(void)
  361. {
  362. int i = MAX_EV;
  363. while(i--)
  364. event_handle[i] = event_dummy;
  365. event_handle[ButtonPress] = event_buttonpress;
  366. event_handle[ClientMessage] = event_clientmessageevent;
  367. event_handle[ConfigureRequest] = event_configureevent;
  368. event_handle[DestroyNotify] = event_destroynotify;
  369. event_handle[EnterNotify] = event_enternotify;
  370. event_handle[Expose] = event_expose;
  371. event_handle[FocusIn] = event_focusin;
  372. event_handle[KeyPress] = event_keypress;
  373. event_handle[MapNotify] = event_mapnotify;
  374. event_handle[MapRequest] = event_maprequest;
  375. event_handle[MappingNotify] = event_mappingnotify;
  376. event_handle[PropertyNotify] = event_propertynotify;
  377. /*event_handle[ReparentNotify] = event_reparentnotify;*/
  378. event_handle[SelectionClear] = event_selectionclearevent;
  379. event_handle[UnmapNotify] = event_unmapnotify;
  380. }