dwm-meillo

view dev.c @ 60:24f9c674d03f

made stdin reader more robust
author Anselm R. Garbe <garbeam@wmii.de>
date Fri, 14 Jul 2006 12:08:32 +0200
parents 529901e6a227
children
line source
1 /*
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
4 */
6 #include "dwm.h"
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <X11/keysym.h>
13 /********** CUSTOMIZE **********/
15 const char *term[] = {
16 "urxvtc", "-tr", "+sb", "-bg", "black", "-fg", "white", "-fn",
17 "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*",NULL
18 };
19 const char *browse[] = { "firefox", NULL };
20 const char *xlock[] = { "xlock", NULL };
22 static Key key[] = {
23 /* modifier key function arguments */
24 { Mod1Mask, XK_Return, zoom, { 0 } },
25 { Mod1Mask, XK_k, prevc, { 0 } },
26 { Mod1Mask, XK_j, nextc, { 0 } },
27 { Mod1Mask, XK_m, max, { 0 } },
28 { Mod1Mask, XK_0, view, { .i = Tscratch } },
29 { Mod1Mask, XK_1, view, { .i = Tdev } },
30 { Mod1Mask, XK_2, view, { .i = Twww } },
31 { Mod1Mask, XK_3, view, { .i = Twork } },
32 { Mod1Mask, XK_space, tiling, { 0 } },
33 { Mod1Mask|ShiftMask, XK_space, floating, { 0 } },
34 { Mod1Mask|ShiftMask, XK_0, ttrunc, { .i = Tscratch } },
35 { Mod1Mask|ShiftMask, XK_1, ttrunc, { .i = Tdev } },
36 { Mod1Mask|ShiftMask, XK_2, ttrunc, { .i = Twww } },
37 { Mod1Mask|ShiftMask, XK_3, ttrunc, { .i = Twork } },
38 { Mod1Mask|ShiftMask, XK_c, ckill, { 0 } },
39 { Mod1Mask|ShiftMask, XK_q, quit, { 0 } },
40 { Mod1Mask|ShiftMask, XK_Return, spawn, { .argv = term } },
41 { Mod1Mask|ShiftMask, XK_w, spawn, { .argv = browse } },
42 { Mod1Mask|ShiftMask, XK_l, spawn, { .argv = xlock } },
43 { ControlMask, XK_0, tappend, { .i = Tscratch } },
44 { ControlMask, XK_1, tappend, { .i = Tdev } },
45 { ControlMask, XK_2, tappend, { .i = Twww } },
46 { ControlMask, XK_3, tappend, { .i = Twork } },
47 };
49 /********** CUSTOMIZE **********/
51 void
52 update_keys(void)
53 {
54 static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
55 unsigned int i;
56 KeyCode code;
58 for(i = 0; i < len; i++) {
59 code = XKeysymToKeycode(dpy, key[i].keysym);
60 XUngrabKey(dpy, code, key[i].mod, root);
61 XGrabKey(dpy, code, key[i].mod, root, True, GrabModeAsync, GrabModeAsync);
62 }
63 }
65 void
66 keypress(XEvent *e)
67 {
68 XKeyEvent *ev = &e->xkey;
69 static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
70 unsigned int i;
71 KeySym keysym;
73 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
74 for(i = 0; i < len; i++)
75 if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
76 if(key[i].func)
77 key[i].func(&key[i].arg);
78 return;
79 }
80 }
82 #define ButtonMask (ButtonPressMask | ButtonReleaseMask)
83 #define MouseMask (ButtonMask | PointerMotionMask)
85 void
86 mresize(Client *c)
87 {
88 XEvent ev;
89 int ocx, ocy;
91 ocx = c->x;
92 ocy = c->y;
93 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
94 None, cursor[CurResize], CurrentTime) != GrabSuccess)
95 return;
96 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
97 for(;;) {
98 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
99 switch(ev.type) {
100 default: break;
101 case Expose:
102 handler[Expose](&ev);
103 break;
104 case MotionNotify:
105 XFlush(dpy);
106 c->w = abs(ocx - ev.xmotion.x);
107 c->h = abs(ocy - ev.xmotion.y);
108 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
109 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
110 resize(c, True);
111 break;
112 case ButtonRelease:
113 XUngrabPointer(dpy, CurrentTime);
114 return;
115 }
116 }
117 }
119 void
120 mmove(Client *c)
121 {
122 XEvent ev;
123 int x1, y1, ocx, ocy, di;
124 unsigned int dui;
125 Window dummy;
127 ocx = c->x;
128 ocy = c->y;
129 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
130 None, cursor[CurMove], CurrentTime) != GrabSuccess)
131 return;
132 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
133 for(;;) {
134 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
135 switch (ev.type) {
136 default: break;
137 case Expose:
138 handler[Expose](&ev);
139 break;
140 case MotionNotify:
141 XFlush(dpy);
142 c->x = ocx + (ev.xmotion.x - x1);
143 c->y = ocy + (ev.xmotion.y - y1);
144 resize(c, False);
145 break;
146 case ButtonRelease:
147 XUngrabPointer(dpy, CurrentTime);
148 return;
149 }
150 }
151 }