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.

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