Xinqi Bao's Git
db3ce46eff6fa9e3cc64a0823c55963aadc6575b
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
) {
169 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
172 if((c
= getclient(ev
->window
))) {
174 if(ev
->value_mask
& CWBorderWidth
)
175 c
->border
= ev
->border_width
;
176 if(c
->isfixed
|| c
->isfloat
|| (arrange
== dofloat
)) {
177 if(ev
->value_mask
& CWX
)
179 if(ev
->value_mask
& CWY
)
181 if(ev
->value_mask
& CWWidth
)
183 if(ev
->value_mask
& CWHeight
)
185 if((ev
->value_mask
& (CWX
| CWY
))
186 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
189 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
197 wc
.width
= ev
->width
;
198 wc
.height
= ev
->height
;
199 wc
.border_width
= ev
->border_width
;
200 wc
.sibling
= ev
->above
;
201 wc
.stack_mode
= ev
->detail
;
202 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
208 destroynotify(XEvent
*e
) {
210 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
212 if((c
= getclient(ev
->window
)))
217 enternotify(XEvent
*e
) {
219 XCrossingEvent
*ev
= &e
->xcrossing
;
221 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
223 if((c
= getclient(ev
->window
)) && isvisible(c
))
225 else if(ev
->window
== root
) {
227 for(c
= stack
; c
&& !isvisible(c
); c
= c
->snext
);
234 XExposeEvent
*ev
= &e
->xexpose
;
237 if(barwin
== ev
->window
)
243 keypress(XEvent
*e
) {
244 static unsigned int len
= sizeof key
/ sizeof key
[0];
247 XKeyEvent
*ev
= &e
->xkey
;
249 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
250 for(i
= 0; i
< len
; i
++)
251 if(keysym
== key
[i
].keysym
252 && CLEANMASK(key
[i
].mod
) == CLEANMASK(ev
->state
))
255 key
[i
].func(&key
[i
].arg
);
260 leavenotify(XEvent
*e
) {
261 XCrossingEvent
*ev
= &e
->xcrossing
;
263 if((ev
->window
== root
) && !ev
->same_screen
) {
270 mappingnotify(XEvent
*e
) {
271 XMappingEvent
*ev
= &e
->xmapping
;
273 XRefreshKeyboardMapping(ev
);
274 if(ev
->request
== MappingKeyboard
)
279 maprequest(XEvent
*e
) {
280 static XWindowAttributes wa
;
281 XMapRequestEvent
*ev
= &e
->xmaprequest
;
283 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
285 if(wa
.override_redirect
)
287 if(!getclient(ev
->window
))
288 manage(ev
->window
, &wa
);
292 propertynotify(XEvent
*e
) {
295 XPropertyEvent
*ev
= &e
->xproperty
;
297 if(ev
->state
== PropertyDelete
)
299 if((c
= getclient(ev
->window
))) {
302 case XA_WM_TRANSIENT_FOR
:
303 XGetTransientForHint(dpy
, c
->win
, &trans
);
304 if(!c
->isfloat
&& (c
->isfloat
= (trans
!= 0)))
307 case XA_WM_NORMAL_HINTS
:
311 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
320 unmapnotify(XEvent
*e
) {
322 XUnmapEvent
*ev
= &e
->xunmap
;
324 if((c
= getclient(ev
->window
)))
330 void (*handler
[LASTEvent
]) (XEvent
*) = {
331 [ButtonPress
] = buttonpress
,
332 [ConfigureRequest
] = configurerequest
,
333 [DestroyNotify
] = destroynotify
,
334 [EnterNotify
] = enternotify
,
335 [LeaveNotify
] = leavenotify
,
337 [KeyPress
] = keypress
,
338 [MappingNotify
] = mappingnotify
,
339 [MapRequest
] = maprequest
,
340 [PropertyNotify
] = propertynotify
,
341 [UnmapNotify
] = unmapnotify
346 static unsigned int len
= sizeof key
/ sizeof key
[0];
350 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
351 for(i
= 0; i
< len
; i
++) {
352 code
= XKeysymToKeycode(dpy
, key
[i
].keysym
);
353 XGrabKey(dpy
, code
, key
[i
].mod
, root
, True
,
354 GrabModeAsync
, GrabModeAsync
);
355 XGrabKey(dpy
, code
, key
[i
].mod
| LockMask
, root
, True
,
356 GrabModeAsync
, GrabModeAsync
);
357 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
, root
, True
,
358 GrabModeAsync
, GrabModeAsync
);
359 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
| LockMask
, root
, True
,
360 GrabModeAsync
, GrabModeAsync
);
368 while(XPending(dpy
)) {
369 XNextEvent(dpy
, &ev
);
371 (handler
[ev
.type
])(&ev
); /* call handler */