Xinqi Bao's Git
a442d2ee2dce289aed30f0ff8a7a0e3591c183b8
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
7 #include <X11/keysym.h>
15 void (*func
)(const char *arg
);
21 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
22 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
28 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
33 movemouse(Client
*c
) {
34 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
41 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
42 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
45 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
47 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
50 XUngrabPointer(dpy
, CurrentTime
);
52 case ConfigureRequest
:
55 handler
[ev
.type
](&ev
);
59 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
60 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
61 if(abs(wax
+ nx
) < SNAP
)
63 else if(abs((wax
+ waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
64 nx
= wax
+ waw
- c
->w
- 2 * c
->border
;
65 if(abs(way
- ny
) < SNAP
)
67 else if(abs((way
+ wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
68 ny
= way
+ wah
- c
->h
- 2 * c
->border
;
69 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
76 resizemouse(Client
*c
) {
83 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
84 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
87 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
89 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
92 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
93 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
94 XUngrabPointer(dpy
, CurrentTime
);
95 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
97 case ConfigureRequest
:
100 handler
[ev
.type
](&ev
);
104 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
106 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
108 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
115 buttonpress(XEvent
*e
) {
119 XButtonPressedEvent
*ev
= &e
->xbutton
;
122 if(barwin
== ev
->window
) {
124 for(i
= 0; i
< ntags
; i
++) {
127 snprintf(buf
, sizeof buf
, "%d", i
);
128 if(ev
->button
== Button1
) {
129 if(ev
->state
& MODKEY
)
134 else if(ev
->button
== Button3
) {
135 if(ev
->state
& MODKEY
)
150 else if((c
= getclient(ev
->window
))) {
152 if(CLEANMASK(ev
->state
) != MODKEY
)
154 if(ev
->button
== Button1
&& (lt
->arrange
== untile
|| c
->isuntiled
)) {
158 else if(ev
->button
== Button2
)
160 else if(ev
->button
== Button3
161 && (lt
->arrange
== untile
|| c
->isuntiled
) && !c
->isfixed
)
170 configurerequest(XEvent
*e
) {
172 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
175 if((c
= getclient(ev
->window
))) {
177 if(ev
->value_mask
& CWBorderWidth
)
178 c
->border
= ev
->border_width
;
179 if(c
->isfixed
|| c
->isuntiled
|| (lt
->arrange
== untile
)) {
180 if(ev
->value_mask
& CWX
)
182 if(ev
->value_mask
& CWY
)
184 if(ev
->value_mask
& CWWidth
)
186 if(ev
->value_mask
& CWHeight
)
188 if((ev
->value_mask
& (CWX
| CWY
))
189 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
192 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
200 wc
.width
= ev
->width
;
201 wc
.height
= ev
->height
;
202 wc
.border_width
= ev
->border_width
;
203 wc
.sibling
= ev
->above
;
204 wc
.stack_mode
= ev
->detail
;
205 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
211 destroynotify(XEvent
*e
) {
213 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
215 if((c
= getclient(ev
->window
)))
220 enternotify(XEvent
*e
) {
222 XCrossingEvent
*ev
= &e
->xcrossing
;
224 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
226 if((c
= getclient(ev
->window
)) && isvisible(c
))
228 else if(ev
->window
== root
) {
230 for(c
= stack
; c
&& !isvisible(c
); c
= c
->snext
);
237 XExposeEvent
*ev
= &e
->xexpose
;
240 if(barwin
== ev
->window
)
246 keypress(XEvent
*e
) {
247 static unsigned int len
= sizeof key
/ sizeof key
[0];
250 XKeyEvent
*ev
= &e
->xkey
;
252 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
253 for(i
= 0; i
< len
; i
++)
254 if(keysym
== key
[i
].keysym
255 && CLEANMASK(key
[i
].mod
) == CLEANMASK(ev
->state
))
258 key
[i
].func(key
[i
].arg
);
263 leavenotify(XEvent
*e
) {
264 XCrossingEvent
*ev
= &e
->xcrossing
;
266 if((ev
->window
== root
) && !ev
->same_screen
) {
273 mappingnotify(XEvent
*e
) {
274 XMappingEvent
*ev
= &e
->xmapping
;
276 XRefreshKeyboardMapping(ev
);
277 if(ev
->request
== MappingKeyboard
)
282 maprequest(XEvent
*e
) {
283 static XWindowAttributes wa
;
284 XMapRequestEvent
*ev
= &e
->xmaprequest
;
286 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
288 if(wa
.override_redirect
)
290 if(!getclient(ev
->window
))
291 manage(ev
->window
, &wa
);
295 propertynotify(XEvent
*e
) {
298 XPropertyEvent
*ev
= &e
->xproperty
;
300 if(ev
->state
== PropertyDelete
)
302 if((c
= getclient(ev
->window
))) {
305 case XA_WM_TRANSIENT_FOR
:
306 XGetTransientForHint(dpy
, c
->win
, &trans
);
307 if(!c
->isuntiled
&& (c
->isuntiled
= (getclient(trans
) != NULL
)))
310 case XA_WM_NORMAL_HINTS
:
314 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
323 unmapnotify(XEvent
*e
) {
325 XUnmapEvent
*ev
= &e
->xunmap
;
327 if((c
= getclient(ev
->window
)))
333 void (*handler
[LASTEvent
]) (XEvent
*) = {
334 [ButtonPress
] = buttonpress
,
335 [ConfigureRequest
] = configurerequest
,
336 [DestroyNotify
] = destroynotify
,
337 [EnterNotify
] = enternotify
,
338 [LeaveNotify
] = leavenotify
,
340 [KeyPress
] = keypress
,
341 [MappingNotify
] = mappingnotify
,
342 [MapRequest
] = maprequest
,
343 [PropertyNotify
] = propertynotify
,
344 [UnmapNotify
] = unmapnotify
349 static unsigned int len
= sizeof key
/ sizeof key
[0];
353 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
354 for(i
= 0; i
< len
; i
++) {
355 code
= XKeysymToKeycode(dpy
, key
[i
].keysym
);
356 XGrabKey(dpy
, code
, key
[i
].mod
, root
, True
,
357 GrabModeAsync
, GrabModeAsync
);
358 XGrabKey(dpy
, code
, key
[i
].mod
| LockMask
, root
, True
,
359 GrabModeAsync
, GrabModeAsync
);
360 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
, root
, True
,
361 GrabModeAsync
, GrabModeAsync
);
362 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
| LockMask
, root
, True
,
363 GrabModeAsync
, GrabModeAsync
);