Xinqi Bao's Git
cfe2d2aec5fda89529f12fd827884f150ea01a46
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
6 #include <X11/keysym.h>
14 void (*func
)(Arg
*arg
);
20 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
21 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
24 movemouse(Client
*c
) {
25 int x1
, y1
, ocx
, ocy
, di
;
32 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
33 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
36 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
38 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
42 XUngrabPointer(dpy
, CurrentTime
);
44 case ConfigureRequest
:
47 handler
[ev
.type
](&ev
);
51 c
->x
= ocx
+ (ev
.xmotion
.x
- x1
);
52 c
->y
= ocy
+ (ev
.xmotion
.y
- y1
);
53 if(abs(wax
+ c
->x
) < SNAP
)
55 else if(abs((wax
+ waw
) - (c
->x
+ c
->w
+ 2 * c
->border
)) < SNAP
)
56 c
->x
= wax
+ waw
- c
->w
- 2 * c
->border
;
57 if(abs(way
- c
->y
) < SNAP
)
59 else if(abs((way
+ wah
) - (c
->y
+ c
->h
+ 2 * c
->border
)) < SNAP
)
60 c
->y
= way
+ wah
- c
->h
- 2 * c
->border
;
68 resizemouse(Client
*c
) {
75 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
76 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
79 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
81 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
85 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
86 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
87 XUngrabPointer(dpy
, CurrentTime
);
89 case ConfigureRequest
:
92 handler
[ev
.type
](&ev
);
96 nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1;
97 c
->w
= nw
> 0 ? nw
: 1;
98 nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1;
99 c
->h
= nh
> 0 ? nh
: 1;
107 buttonpress(XEvent
*e
) {
111 XButtonPressedEvent
*ev
= &e
->xbutton
;
113 if(barwin
== ev
->window
) {
115 for(a
.i
= 0; a
.i
< ntags
; a
.i
++) {
116 x
+= textw(tags
[a
.i
]);
118 if(ev
->button
== Button1
) {
119 if(ev
->state
& MODKEY
)
124 else if(ev
->button
== Button3
) {
125 if(ev
->state
& MODKEY
)
148 else if((c
= getclient(ev
->window
))) {
150 if(CLEANMASK(ev
->state
) != MODKEY
)
152 if(ev
->button
== Button1
&& (arrange
== dofloat
|| c
->isfloat
)) {
156 else if(ev
->button
== Button2
)
158 else if(ev
->button
== Button3
&& (arrange
== dofloat
|| c
->isfloat
) &&
167 configurerequest(XEvent
*e
) {
168 unsigned long newmask
;
170 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
173 if((c
= getclient(ev
->window
))) {
175 if(ev
->value_mask
& CWX
)
177 if(ev
->value_mask
& CWY
)
179 if(ev
->value_mask
& CWWidth
)
181 if(ev
->value_mask
& CWHeight
)
183 if(ev
->value_mask
& CWBorderWidth
)
184 c
->border
= ev
->border_width
;
189 newmask
= ev
->value_mask
& (~(CWSibling
| CWStackMode
| CWBorderWidth
));
191 XConfigureWindow(dpy
, c
->win
, newmask
, &wc
);
198 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
206 wc
.width
= ev
->width
;
207 wc
.height
= ev
->height
;
208 wc
.border_width
= ev
->border_width
;
209 wc
.sibling
= ev
->above
;
210 wc
.stack_mode
= ev
->detail
;
211 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
217 destroynotify(XEvent
*e
) {
219 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
221 if((c
= getclient(ev
->window
)))
226 enternotify(XEvent
*e
) {
228 XCrossingEvent
*ev
= &e
->xcrossing
;
230 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
232 if((c
= getclient(ev
->window
)) && isvisible(c
))
234 else if(ev
->window
== root
) {
236 for(c
= stack
; c
&& !isvisible(c
); c
= c
->snext
);
243 XExposeEvent
*ev
= &e
->xexpose
;
246 if(barwin
== ev
->window
)
252 keypress(XEvent
*e
) {
253 static unsigned int len
= sizeof key
/ sizeof key
[0];
256 XKeyEvent
*ev
= &e
->xkey
;
258 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
259 for(i
= 0; i
< len
; i
++) {
260 if(keysym
== key
[i
].keysym
261 && CLEANMASK(key
[i
].mod
) == CLEANMASK(ev
->state
))
264 key
[i
].func(&key
[i
].arg
);
270 leavenotify(XEvent
*e
) {
271 XCrossingEvent
*ev
= &e
->xcrossing
;
273 if((ev
->window
== root
) && !ev
->same_screen
) {
280 mappingnotify(XEvent
*e
) {
281 XMappingEvent
*ev
= &e
->xmapping
;
283 XRefreshKeyboardMapping(ev
);
284 if(ev
->request
== MappingKeyboard
)
289 maprequest(XEvent
*e
) {
290 static XWindowAttributes wa
;
291 XMapRequestEvent
*ev
= &e
->xmaprequest
;
293 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
295 if(wa
.override_redirect
) {
296 XSelectInput(dpy
, ev
->window
,
297 (StructureNotifyMask
| PropertyChangeMask
));
300 if(!getclient(ev
->window
))
301 manage(ev
->window
, &wa
);
305 propertynotify(XEvent
*e
) {
308 XPropertyEvent
*ev
= &e
->xproperty
;
310 if(ev
->state
== PropertyDelete
)
312 if((c
= getclient(ev
->window
))) {
315 case XA_WM_TRANSIENT_FOR
:
316 XGetTransientForHint(dpy
, c
->win
, &trans
);
317 if(!c
->isfloat
&& (c
->isfloat
= (trans
!= 0)))
320 case XA_WM_NORMAL_HINTS
:
324 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
333 unmapnotify(XEvent
*e
) {
335 XUnmapEvent
*ev
= &e
->xunmap
;
337 if((c
= getclient(ev
->window
)))
343 void (*handler
[LASTEvent
]) (XEvent
*) = {
344 [ButtonPress
] = buttonpress
,
345 [ConfigureRequest
] = configurerequest
,
346 [DestroyNotify
] = destroynotify
,
347 [EnterNotify
] = enternotify
,
348 [LeaveNotify
] = leavenotify
,
350 [KeyPress
] = keypress
,
351 [MappingNotify
] = mappingnotify
,
352 [MapRequest
] = maprequest
,
353 [PropertyNotify
] = propertynotify
,
354 [UnmapNotify
] = unmapnotify
359 static unsigned int len
= sizeof key
/ sizeof key
[0];
363 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
364 for(i
= 0; i
< len
; i
++) {
365 code
= XKeysymToKeycode(dpy
, key
[i
].keysym
);
366 XGrabKey(dpy
, code
, key
[i
].mod
, root
, True
,
367 GrabModeAsync
, GrabModeAsync
);
368 XGrabKey(dpy
, code
, key
[i
].mod
| LockMask
, root
, True
,
369 GrabModeAsync
, GrabModeAsync
);
370 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
, root
, True
,
371 GrabModeAsync
, GrabModeAsync
);
372 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
| LockMask
, root
, True
,
373 GrabModeAsync
, GrabModeAsync
);
381 while(XPending(dpy
)) {
382 XNextEvent(dpy
, &ev
);
384 (handler
[ev
.type
])(&ev
); /* call handler */