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.

386 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
  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. const char *browse[] = { "firefox", NULL };
  13. const char *gimp[] = { "gimp", NULL };
  14. const char *term[] = {
  15. "urxvtc", "-tr", "+sb", "-bg", "black", "-fg", "white", "-cr", "white",
  16. "-fn", "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*", NULL
  17. };
  18. const char *xlock[] = { "xlock", NULL };
  19. Key key[] = {
  20. /* modifier key function arguments */
  21. { ControlMask, XK_0, appendtag, { .i = Tscratch } },
  22. { ControlMask, XK_1, appendtag, { .i = Tdev } },
  23. { ControlMask, XK_2, appendtag, { .i = Twww } },
  24. { ControlMask, XK_3, appendtag, { .i = Twork } },
  25. { Mod1Mask, XK_0, view, { .i = Tscratch } },
  26. { Mod1Mask, XK_1, view, { .i = Tdev } },
  27. { Mod1Mask, XK_2, view, { .i = Twww } },
  28. { Mod1Mask, XK_3, view, { .i = Twork } },
  29. { Mod1Mask, XK_j, focusnext, { 0 } },
  30. { Mod1Mask, XK_k, focusprev, { 0 } },
  31. { Mod1Mask, XK_m, maximize, { 0 } },
  32. { Mod1Mask, XK_space, dotile, { 0 } },
  33. { Mod1Mask, XK_Return, zoom, { 0 } },
  34. { ControlMask|ShiftMask,XK_0, heretag, { .i = Tscratch } },
  35. { ControlMask|ShiftMask,XK_1, heretag, { .i = Tdev } },
  36. { ControlMask|ShiftMask,XK_2, heretag, { .i = Twww } },
  37. { ControlMask|ShiftMask,XK_3, heretag, { .i = Twork } },
  38. { Mod1Mask|ShiftMask, XK_0, replacetag, { .i = Tscratch } },
  39. { Mod1Mask|ShiftMask, XK_1, replacetag, { .i = Tdev } },
  40. { Mod1Mask|ShiftMask, XK_2, replacetag, { .i = Twww } },
  41. { Mod1Mask|ShiftMask, XK_3, replacetag, { .i = Twork } },
  42. { Mod1Mask|ShiftMask, XK_c, killclient, { 0 } },
  43. { Mod1Mask|ShiftMask, XK_g, spawn, { .argv = gimp } },
  44. { Mod1Mask|ShiftMask, XK_l, spawn, { .argv = xlock } },
  45. { Mod1Mask|ShiftMask, XK_q, quit, { 0 } },
  46. { Mod1Mask|ShiftMask, XK_space, dofloat, { 0 } },
  47. { Mod1Mask|ShiftMask, XK_w, spawn, { .argv = browse } },
  48. { Mod1Mask|ShiftMask, XK_Return, spawn, { .argv = term } },
  49. };
  50. /* static */
  51. static void
  52. movemouse(Client *c)
  53. {
  54. XEvent ev;
  55. int x1, y1, ocx, ocy, di;
  56. unsigned int dui;
  57. Window dummy;
  58. ocx = *c->x;
  59. ocy = *c->y;
  60. if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
  61. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  62. return;
  63. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  64. for(;;) {
  65. XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
  66. switch (ev.type) {
  67. default: break;
  68. case Expose:
  69. handler[Expose](&ev);
  70. break;
  71. case MotionNotify:
  72. XSync(dpy, False);
  73. *c->x = ocx + (ev.xmotion.x - x1);
  74. *c->y = ocy + (ev.xmotion.y - y1);
  75. resize(c, False, TopLeft);
  76. break;
  77. case ButtonRelease:
  78. XUngrabPointer(dpy, CurrentTime);
  79. return;
  80. }
  81. }
  82. }
  83. static void
  84. resizemouse(Client *c)
  85. {
  86. XEvent ev;
  87. int ocx, ocy;
  88. Corner sticky;
  89. ocx = *c->x;
  90. ocy = *c->y;
  91. if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
  92. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  93. return;
  94. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, *c->w, *c->h);
  95. for(;;) {
  96. XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
  97. switch(ev.type) {
  98. default: break;
  99. case Expose:
  100. handler[Expose](&ev);
  101. break;
  102. case MotionNotify:
  103. XSync(dpy, False);
  104. *c->w = abs(ocx - ev.xmotion.x);
  105. *c->h = abs(ocy - ev.xmotion.y);
  106. *c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - *c->w;
  107. *c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - *c->h;
  108. if(ocx <= ev.xmotion.x) {
  109. if(ocy <= ev.xmotion.y)
  110. sticky = TopLeft;
  111. else
  112. sticky = BottomLeft;
  113. } else {
  114. if(ocy <= ev.xmotion.y)
  115. sticky = TopRight;
  116. else
  117. sticky = BottomRight;
  118. }
  119. resize(c, True, sticky);
  120. break;
  121. case ButtonRelease:
  122. XUngrabPointer(dpy, CurrentTime);
  123. return;
  124. }
  125. }
  126. }
  127. static void
  128. buttonpress(XEvent *e)
  129. {
  130. int x;
  131. Arg a;
  132. XButtonPressedEvent *ev = &e->xbutton;
  133. Client *c;
  134. if(barwin == ev->window) {
  135. switch(ev->button) {
  136. default:
  137. x = 0;
  138. for(a.i = 0; a.i < TLast; a.i++) {
  139. x += textw(tags[a.i]);
  140. if(ev->x < x) {
  141. view(&a);
  142. break;
  143. }
  144. }
  145. break;
  146. case Button4:
  147. a.i = (tsel + 1 < TLast) ? tsel + 1 : 0;
  148. view(&a);
  149. break;
  150. case Button5:
  151. a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1;
  152. view(&a);
  153. break;
  154. }
  155. }
  156. else if((c = getclient(ev->window))) {
  157. switch(ev->button) {
  158. default:
  159. break;
  160. case Button1:
  161. if(arrange == dotile && !c->isfloat) {
  162. if((ev->state & ControlMask) && (ev->button == Button1))
  163. zoom(NULL);
  164. }
  165. else {
  166. higher(c);
  167. movemouse(c);
  168. }
  169. break;
  170. case Button2:
  171. lower(c);
  172. break;
  173. case Button3:
  174. if(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. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  186. XWindowChanges wc;
  187. Client *c;
  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. XCrossingEvent *ev = &e->xcrossing;
  228. Client *c;
  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. XExposeEvent *ev = &e->xexpose;
  240. Client *c;
  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. XKeyEvent *ev = &e->xkey;
  252. static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
  253. unsigned int i;
  254. KeySym keysym;
  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. XMapRequestEvent *ev = &e->xmaprequest;
  274. static XWindowAttributes wa;
  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. XPropertyEvent *ev = &e->xproperty;
  289. Window trans;
  290. Client *c;
  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 = key ? sizeof(key) / sizeof(key[0]) : 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. }