Xinqi Bao's Git
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
, nx
, ny
;
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
);
41 XUngrabPointer(dpy
, CurrentTime
);
43 case ConfigureRequest
:
46 handler
[ev
.type
](&ev
);
50 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
51 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
52 if(abs(wax
+ nx
) < SNAP
)
54 else if(abs((wax
+ waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
55 nx
= wax
+ waw
- c
->w
- 2 * c
->border
;
56 if(abs(way
- ny
) < SNAP
)
58 else if(abs((way
+ wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
59 ny
= way
+ wah
- c
->h
- 2 * c
->border
;
60 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
67 resizemouse(Client
*c
) {
74 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
75 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
78 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
80 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
83 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
84 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
85 XUngrabPointer(dpy
, CurrentTime
);
86 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
88 case ConfigureRequest
:
91 handler
[ev
.type
](&ev
);
95 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
97 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
99 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
106 buttonpress(XEvent
*e
) {
110 XButtonPressedEvent
*ev
= &e
->xbutton
;
112 if(barwin
== ev
->window
) {
114 for(a
.i
= 0; a
.i
< ntags
; a
.i
++) {
115 x
+= textw(tags
[a
.i
]);
117 if(ev
->button
== Button1
) {
118 if(ev
->state
& MODKEY
)
123 else if(ev
->button
== Button3
) {
124 if(ev
->state
& MODKEY
)
147 else if((c
= getclient(ev
->window
))) {
149 if(CLEANMASK(ev
->state
) != MODKEY
)
151 if(ev
->button
== Button1
&& (arrange
== dofloat
|| c
->isfloat
)) {
155 else if(ev
->button
== Button2
)
157 else if(ev
->button
== Button3
158 && (arrange
== dofloat
|| c
->isfloat
) && !c
->isfixed
)
167 configurerequest(XEvent
*e
) {
170 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
173 if((c
= getclient(ev
->window
))) {
175 if(ev
->value_mask
& CWBorderWidth
)
176 c
->border
= ev
->border_width
;
177 if(c
->isfixed
|| c
->isfloat
|| (arrange
== dofloat
)) {
178 x
= (ev
->value_mask
& CWX
) ? ev
->x
: c
->x
;
179 y
= (ev
->value_mask
& CWY
) ? ev
->y
: c
->y
;
180 w
= (ev
->value_mask
& CWWidth
) ? ev
->width
: c
->w
;
181 h
= (ev
->value_mask
& CWHeight
) ? ev
->height
: c
->h
;
182 if((ev
->value_mask
& (CWX
| CWY
))
183 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
189 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
192 resize(c
, x
, y
, w
, h
, False
);
203 wc
.width
= ev
->width
;
204 wc
.height
= ev
->height
;
205 wc
.border_width
= ev
->border_width
;
206 wc
.sibling
= ev
->above
;
207 wc
.stack_mode
= ev
->detail
;
208 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
214 destroynotify(XEvent
*e
) {
216 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
218 if((c
= getclient(ev
->window
)))
223 enternotify(XEvent
*e
) {
225 XCrossingEvent
*ev
= &e
->xcrossing
;
227 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
229 if((c
= getclient(ev
->window
)) && isvisible(c
))
231 else if(ev
->window
== root
) {
233 for(c
= stack
; c
&& !isvisible(c
); c
= c
->snext
);
240 XExposeEvent
*ev
= &e
->xexpose
;
243 if(barwin
== ev
->window
)
249 keypress(XEvent
*e
) {
250 static unsigned int len
= sizeof key
/ sizeof key
[0];
253 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
258 && CLEANMASK(key
[i
].mod
) == CLEANMASK(ev
->state
))
261 key
[i
].func(&key
[i
].arg
);
266 leavenotify(XEvent
*e
) {
267 XCrossingEvent
*ev
= &e
->xcrossing
;
269 if((ev
->window
== root
) && !ev
->same_screen
) {
276 mappingnotify(XEvent
*e
) {
277 XMappingEvent
*ev
= &e
->xmapping
;
279 XRefreshKeyboardMapping(ev
);
280 if(ev
->request
== MappingKeyboard
)
285 maprequest(XEvent
*e
) {
286 static XWindowAttributes wa
;
287 XMapRequestEvent
*ev
= &e
->xmaprequest
;
289 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
291 if(wa
.override_redirect
)
293 if(!getclient(ev
->window
))
294 manage(ev
->window
, &wa
);
298 propertynotify(XEvent
*e
) {
301 XPropertyEvent
*ev
= &e
->xproperty
;
303 if(ev
->state
== PropertyDelete
)
305 if((c
= getclient(ev
->window
))) {
308 case XA_WM_TRANSIENT_FOR
:
309 XGetTransientForHint(dpy
, c
->win
, &trans
);
310 if(!c
->isfloat
&& (c
->isfloat
= (trans
!= 0)))
313 case XA_WM_NORMAL_HINTS
:
317 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
326 unmapnotify(XEvent
*e
) {
328 XUnmapEvent
*ev
= &e
->xunmap
;
330 if((c
= getclient(ev
->window
)))
336 void (*handler
[LASTEvent
]) (XEvent
*) = {
337 [ButtonPress
] = buttonpress
,
338 [ConfigureRequest
] = configurerequest
,
339 [DestroyNotify
] = destroynotify
,
340 [EnterNotify
] = enternotify
,
341 [LeaveNotify
] = leavenotify
,
343 [KeyPress
] = keypress
,
344 [MappingNotify
] = mappingnotify
,
345 [MapRequest
] = maprequest
,
346 [PropertyNotify
] = propertynotify
,
347 [UnmapNotify
] = unmapnotify
352 static unsigned int len
= sizeof key
/ sizeof key
[0];
356 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
357 for(i
= 0; i
< len
; i
++) {
358 code
= XKeysymToKeycode(dpy
, key
[i
].keysym
);
359 XGrabKey(dpy
, code
, key
[i
].mod
, root
, True
,
360 GrabModeAsync
, GrabModeAsync
);
361 XGrabKey(dpy
, code
, key
[i
].mod
| LockMask
, root
, True
,
362 GrabModeAsync
, GrabModeAsync
);
363 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
, root
, True
,
364 GrabModeAsync
, GrabModeAsync
);
365 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
| LockMask
, root
, True
,
366 GrabModeAsync
, GrabModeAsync
);
374 while(XPending(dpy
)) {
375 XNextEvent(dpy
, &ev
);
377 (handler
[ev
.type
])(&ev
); /* call handler */