You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

393 lines
8.5 KiB

19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
  1. /*
  2. * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include "dwm.h"
  6. #include <stdlib.h>
  7. #include <X11/keysym.h>
  8. #include <X11/Xatom.h>
  9. #define ButtonMask (ButtonPressMask | ButtonReleaseMask)
  10. #define MouseMask (ButtonMask | PointerMotionMask)
  11. /* CUSTOMIZE */
  12. typedef struct {
  13. unsigned long mod;
  14. KeySym keysym;
  15. void (*func)(Arg *arg);
  16. Arg arg;
  17. } Key;
  18. /*
  19. const char *browse[] = { "firefox", NULL };
  20. const char *gimp[] = { "gimp", NULL };
  21. */
  22. const char *term[] = { "xterm", NULL };
  23. /*
  24. "urxvtc", "-tr", "+sb", "-bg", "black", "-fg", "white", "-cr", "white",
  25. "-fn", "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*", NULL
  26. };
  27. coonst char *xlock[] = { "xlock", NULL };
  28. */
  29. static Key key[] = {
  30. /* modifier key function arguments */
  31. { ControlMask, XK_0, appendtag, { .i = Tscratch } },
  32. { ControlMask, XK_1, appendtag, { .i = Tdev } },
  33. { ControlMask, XK_2, appendtag, { .i = Twww } },
  34. { ControlMask, XK_3, appendtag, { .i = Twork } },
  35. { MODKEY, XK_0, view, { .i = Tscratch } },
  36. { MODKEY, XK_1, view, { .i = Tdev } },
  37. { MODKEY, XK_2, view, { .i = Twww } },
  38. { MODKEY, XK_3, view, { .i = Twork } },
  39. { MODKEY, XK_j, focusnext, { 0 } },
  40. { MODKEY, XK_k, focusprev, { 0 } },
  41. { MODKEY, XK_m, maximize, { 0 } },
  42. { MODKEY, XK_space, dotile, { 0 } },
  43. { MODKEY, XK_Return, zoom, { 0 } },
  44. { ControlMask|ShiftMask,XK_0, heretag, { .i = Tscratch } },
  45. { ControlMask|ShiftMask,XK_1, heretag, { .i = Tdev } },
  46. { ControlMask|ShiftMask,XK_2, heretag, { .i = Twww } },
  47. { ControlMask|ShiftMask,XK_3, heretag, { .i = Twork } },
  48. { MODKEY|ShiftMask, XK_0, replacetag, { .i = Tscratch } },
  49. { MODKEY|ShiftMask, XK_1, replacetag, { .i = Tdev } },
  50. { MODKEY|ShiftMask, XK_2, replacetag, { .i = Twww } },
  51. { MODKEY|ShiftMask, XK_3, replacetag, { .i = Twork } },
  52. { MODKEY|ShiftMask, XK_c, killclient, { 0 } },
  53. /*
  54. { MODKEY|ShiftMask, XK_g, spawn, { .argv = gimp } },
  55. { MODKEY|ShiftMask, XK_l, spawn, { .argv = xlock } },
  56. */
  57. { MODKEY|ShiftMask, XK_q, quit, { 0 } },
  58. { MODKEY|ShiftMask, XK_space, dofloat, { 0 } },
  59. /*{ MODKEY|ShiftMask, XK_w, spawn, { .argv = browse } },*/
  60. { MODKEY|ShiftMask, XK_Return, spawn, { .argv = term } },
  61. };
  62. /* static */
  63. static void
  64. movemouse(Client *c)
  65. {
  66. XEvent ev;
  67. int x1, y1, ocx, ocy, di;
  68. unsigned int dui;
  69. Window dummy;
  70. ocx = c->x;
  71. ocy = c->y;
  72. if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
  73. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  74. return;
  75. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  76. for(;;) {
  77. XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
  78. switch (ev.type) {
  79. default: break;
  80. case Expose:
  81. handler[Expose](&ev);
  82. break;
  83. case MotionNotify:
  84. XSync(dpy, False);
  85. c->x = ocx + (ev.xmotion.x - x1);
  86. c->y = ocy + (ev.xmotion.y - y1);
  87. resize(c, False, TopLeft);
  88. break;
  89. case ButtonRelease:
  90. XUngrabPointer(dpy, CurrentTime);
  91. return;
  92. }
  93. }
  94. }
  95. static void
  96. resizemouse(Client *c)
  97. {
  98. XEvent ev;
  99. int ocx, ocy;
  100. Corner sticky;
  101. ocx = c->x;
  102. ocy = c->y;
  103. if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
  104. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  105. return;
  106. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
  107. for(;;) {
  108. XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
  109. switch(ev.type) {
  110. default: break;
  111. case Expose:
  112. handler[Expose](&ev);
  113. break;
  114. case MotionNotify:
  115. XSync(dpy, False);
  116. c->w = abs(ocx - ev.xmotion.x);
  117. c->h = abs(ocy - ev.xmotion.y);
  118. c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
  119. c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
  120. if(ocx <= ev.xmotion.x)
  121. sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
  122. else
  123. sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
  124. resize(c, True, sticky);
  125. break;
  126. case ButtonRelease:
  127. XUngrabPointer(dpy, CurrentTime);
  128. return;
  129. }
  130. }
  131. }
  132. static void
  133. buttonpress(XEvent *e)
  134. {
  135. int x;
  136. Arg a;
  137. XButtonPressedEvent *ev = &e->xbutton;
  138. Client *c;
  139. if(barwin == ev->window) {
  140. switch(ev->button) {
  141. default:
  142. x = 0;
  143. for(a.i = 0; a.i < TLast; a.i++) {
  144. x += textw(tags[a.i]);
  145. if(ev->x < x) {
  146. view(&a);
  147. break;
  148. }
  149. }
  150. break;
  151. case Button4:
  152. a.i = (tsel + 1 < TLast) ? tsel + 1 : 0;
  153. view(&a);
  154. break;
  155. case Button5:
  156. a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1;
  157. view(&a);
  158. break;
  159. }
  160. }
  161. else if((c = getclient(ev->window))) {
  162. switch(ev->button) {
  163. default:
  164. break;
  165. case Button1:
  166. if(arrange == dotile && !c->isfloat) {
  167. if((ev->state & ControlMask) && (ev->button == Button1))
  168. zoom(NULL);
  169. }
  170. else {
  171. higher(c);
  172. movemouse(c);
  173. }
  174. break;
  175. case Button2:
  176. lower(c);
  177. break;
  178. case Button3:
  179. if(arrange == dofloat || c->isfloat) {
  180. higher(c);
  181. resizemouse(c);
  182. }
  183. break;
  184. }
  185. }
  186. }
  187. static void
  188. configurerequest(XEvent *e)
  189. {
  190. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  191. XWindowChanges wc;
  192. Client *c;
  193. ev->value_mask &= ~CWSibling;
  194. if((c = getclient(ev->window))) {
  195. gravitate(c, True);
  196. if(ev->value_mask & CWX)
  197. c->x = ev->x;
  198. if(ev->value_mask & CWY)
  199. c->y = ev->y;
  200. if(ev->value_mask & CWWidth)
  201. c->w = ev->width;
  202. if(ev->value_mask & CWHeight)
  203. c->h = ev->height;
  204. if(ev->value_mask & CWBorderWidth)
  205. c->border = 1;
  206. gravitate(c, False);
  207. resize(c, True, TopLeft);
  208. }
  209. wc.x = ev->x;
  210. wc.y = ev->y;
  211. wc.width = ev->width;
  212. wc.height = ev->height;
  213. wc.border_width = 1;
  214. wc.sibling = None;
  215. wc.stack_mode = Above;
  216. ev->value_mask &= ~CWStackMode;
  217. ev->value_mask |= CWBorderWidth;
  218. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  219. XSync(dpy, False);
  220. }
  221. static void
  222. destroynotify(XEvent *e)
  223. {
  224. Client *c;
  225. XDestroyWindowEvent *ev = &e->xdestroywindow;
  226. if((c = getclient(ev->window)))
  227. unmanage(c);
  228. }
  229. static void
  230. enternotify(XEvent *e)
  231. {
  232. XCrossingEvent *ev = &e->xcrossing;
  233. Client *c;
  234. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  235. return;
  236. if((c = getclient(ev->window)))
  237. focus(c);
  238. else if(ev->window == root)
  239. issel = True;
  240. }
  241. static void
  242. expose(XEvent *e)
  243. {
  244. XExposeEvent *ev = &e->xexpose;
  245. Client *c;
  246. if(ev->count == 0) {
  247. if(barwin == ev->window)
  248. drawstatus();
  249. else if((c = getctitle(ev->window)))
  250. drawtitle(c);
  251. }
  252. }
  253. static void
  254. keypress(XEvent *e)
  255. {
  256. XKeyEvent *ev = &e->xkey;
  257. static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
  258. unsigned int i;
  259. KeySym keysym;
  260. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  261. for(i = 0; i < len; i++)
  262. if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
  263. if(key[i].func)
  264. key[i].func(&key[i].arg);
  265. return;
  266. }
  267. }
  268. static void
  269. leavenotify(XEvent *e)
  270. {
  271. XCrossingEvent *ev = &e->xcrossing;
  272. if((ev->window == root) && !ev->same_screen)
  273. issel = True;
  274. }
  275. static void
  276. maprequest(XEvent *e)
  277. {
  278. XMapRequestEvent *ev = &e->xmaprequest;
  279. static XWindowAttributes wa;
  280. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  281. return;
  282. if(wa.override_redirect) {
  283. XSelectInput(dpy, ev->window,
  284. (StructureNotifyMask | PropertyChangeMask));
  285. return;
  286. }
  287. if(!getclient(ev->window))
  288. manage(ev->window, &wa);
  289. }
  290. static void
  291. propertynotify(XEvent *e)
  292. {
  293. XPropertyEvent *ev = &e->xproperty;
  294. Window trans;
  295. Client *c;
  296. if(ev->state == PropertyDelete)
  297. return; /* ignore */
  298. if((c = getclient(ev->window))) {
  299. if(ev->atom == wmatom[WMProtocols]) {
  300. c->proto = getproto(c->win);
  301. return;
  302. }
  303. switch (ev->atom) {
  304. default: break;
  305. case XA_WM_TRANSIENT_FOR:
  306. XGetTransientForHint(dpy, c->win, &trans);
  307. if(!c->isfloat && (c->isfloat = (trans != 0)))
  308. arrange(NULL);
  309. break;
  310. case XA_WM_NORMAL_HINTS:
  311. setsize(c);
  312. break;
  313. }
  314. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  315. settitle(c);
  316. drawtitle(c);
  317. }
  318. }
  319. }
  320. static void
  321. unmapnotify(XEvent *e)
  322. {
  323. Client *c;
  324. XUnmapEvent *ev = &e->xunmap;
  325. if((c = getclient(ev->window)))
  326. unmanage(c);
  327. }
  328. /* extern */
  329. void (*handler[LASTEvent]) (XEvent *) = {
  330. [ButtonPress] = buttonpress,
  331. [ConfigureRequest] = configurerequest,
  332. [DestroyNotify] = destroynotify,
  333. [EnterNotify] = enternotify,
  334. [LeaveNotify] = leavenotify,
  335. [Expose] = expose,
  336. [KeyPress] = keypress,
  337. [MapRequest] = maprequest,
  338. [PropertyNotify] = propertynotify,
  339. [UnmapNotify] = unmapnotify
  340. };
  341. void
  342. grabkeys()
  343. {
  344. static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
  345. unsigned int i;
  346. KeyCode code;
  347. for(i = 0; i < len; i++) {
  348. code = XKeysymToKeycode(dpy, key[i].keysym);
  349. XUngrabKey(dpy, code, key[i].mod, root);
  350. XGrabKey(dpy, code, key[i].mod, root, True,
  351. GrabModeAsync, GrabModeAsync);
  352. }
  353. }