Xinqi Bao's Git
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
7 #include <X11/keysym.h>
15 void (*func
)(Arg
*arg
);
21 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
24 synconfig(Client
*c
, int x
, int y
, int w
, int h
, unsigned int border
) {
27 synev
.type
= ConfigureNotify
;
28 synev
.xconfigure
.display
= dpy
;
29 synev
.xconfigure
.event
= c
->win
;
30 synev
.xconfigure
.window
= c
->win
;
31 synev
.xconfigure
.x
= x
;
32 synev
.xconfigure
.y
= y
;
33 synev
.xconfigure
.width
= w
;
34 synev
.xconfigure
.height
= h
;
35 synev
.xconfigure
.border_width
= border
;
36 synev
.xconfigure
.above
= None
;
37 XSendEvent(dpy
, c
->win
, True
, NoEventMask
, &synev
);
41 movemouse(Client
*c
) {
42 int x1
, y1
, ocx
, ocy
, di
;
49 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
50 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
52 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
54 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| StructureNotifyMask
, &ev
);
58 case ConfigureRequest
:
59 synconfig(c
, c
->x
, c
->y
, c
->w
, c
->h
, ev
.xconfigure
.border_width
);
67 c
->x
= ocx
+ (ev
.xmotion
.x
- x1
);
68 c
->y
= ocy
+ (ev
.xmotion
.y
- y1
);
69 resize(c
, False
, TopLeft
);
72 XUngrabPointer(dpy
, CurrentTime
);
76 XUngrabPointer(dpy
, CurrentTime
);
77 handler
[ev
.type
](&ev
);
84 resizemouse(Client
*c
) {
92 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
93 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
95 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
, c
->h
);
97 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| StructureNotifyMask
, &ev
);
101 case ConfigureRequest
:
102 synconfig(c
, c
->x
, c
->y
, c
->w
, c
->h
, ev
.xconfigure
.border_width
);
106 handler
[Expose
](&ev
);
110 if((nw
= abs(ocx
- ev
.xmotion
.x
)))
112 if((nh
= abs(ocy
- ev
.xmotion
.y
)))
114 c
->x
= (ocx
<= ev
.xmotion
.x
) ? ocx
: ocx
- c
->w
;
115 c
->y
= (ocy
<= ev
.xmotion
.y
) ? ocy
: ocy
- c
->h
;
116 if(ocx
<= ev
.xmotion
.x
)
117 sticky
= (ocy
<= ev
.xmotion
.y
) ? TopLeft
: BotLeft
;
119 sticky
= (ocy
<= ev
.xmotion
.y
) ? TopRight
: BotRight
;
120 resize(c
, True
, sticky
);
123 XUngrabPointer(dpy
, CurrentTime
);
127 XUngrabPointer(dpy
, CurrentTime
);
128 handler
[ev
.type
](&ev
);
135 buttonpress(XEvent
*e
) {
139 XButtonPressedEvent
*ev
= &e
->xbutton
;
141 if(barwin
== ev
->window
) {
143 for(a
.i
= 0; a
.i
< ntags
; a
.i
++) {
144 x
+= textw(tags
[a
.i
]);
146 if(ev
->button
== Button1
) {
147 if(ev
->state
& MODKEY
)
152 else if(ev
->button
== Button3
) {
153 if(ev
->state
& MODKEY
)
161 if(ev
->x
< x
+ bmw
) {
162 if(ev
->button
== Button1
)
166 else if((c
= getclient(ev
->window
))) {
168 if(CLEANMASK(ev
->state
) != MODKEY
)
170 if(ev
->button
== Button1
&& (arrange
== dofloat
|| c
->isfloat
)) {
174 else if(ev
->button
== Button2
)
176 else if(ev
->button
== Button3
&& (arrange
== dofloat
|| c
->isfloat
)) {
184 configurerequest(XEvent
*e
) {
185 unsigned long newmask
;
187 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
190 if((c
= getclient(ev
->window
))) {
191 if((c
== sel
) && !c
->isfloat
&& (arrange
!= dofloat
)) {
192 synconfig(c
, sx
, sy
+ bh
, sw
- 2, sh
- 2 - bh
, ev
->border_width
);
197 if(ev
->value_mask
& CWX
)
199 if(ev
->value_mask
& CWY
)
201 if(ev
->value_mask
& CWWidth
)
203 if(ev
->value_mask
& CWHeight
)
205 if(ev
->value_mask
& CWBorderWidth
)
206 c
->border
= ev
->border_width
;
212 newmask
= ev
->value_mask
& (~(CWSibling
| CWStackMode
| CWBorderWidth
));
214 XConfigureWindow(dpy
, c
->win
, newmask
, &wc
);
216 synconfig(c
, c
->x
, c
->y
, c
->w
, c
->h
, c
->border
);
219 resize(c
, False
, TopLeft
);
226 wc
.width
= ev
->width
;
227 wc
.height
= ev
->height
;
228 wc
.border_width
= ev
->border_width
;
229 wc
.sibling
= ev
->above
;
230 wc
.stack_mode
= ev
->detail
;
231 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
237 destroynotify(XEvent
*e
) {
239 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
241 if((c
= getclient(ev
->window
)))
246 enternotify(XEvent
*e
) {
248 XCrossingEvent
*ev
= &e
->xcrossing
;
250 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
253 if(((c
= getclient(ev
->window
)) || (c
= getctitle(ev
->window
))) && isvisible(c
))
255 else if(ev
->window
== root
) {
257 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
265 XExposeEvent
*ev
= &e
->xexpose
;
268 if(barwin
== ev
->window
)
270 else if((c
= getctitle(ev
->window
)))
276 keypress(XEvent
*e
) {
277 static unsigned int len
= sizeof(key
) / sizeof(key
[0]);
280 XKeyEvent
*ev
= &e
->xkey
;
282 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
283 for(i
= 0; i
< len
; i
++) {
284 if(keysym
== key
[i
].keysym
285 && CLEANMASK(key
[i
].mod
) == CLEANMASK(ev
->state
))
288 key
[i
].func(&key
[i
].arg
);
295 leavenotify(XEvent
*e
) {
296 XCrossingEvent
*ev
= &e
->xcrossing
;
298 if((ev
->window
== root
) && !ev
->same_screen
) {
305 mappingnotify(XEvent
*e
) {
306 XMappingEvent
*ev
= &e
->xmapping
;
308 XRefreshKeyboardMapping(ev
);
309 if(ev
->request
== MappingKeyboard
)
314 maprequest(XEvent
*e
) {
315 static XWindowAttributes wa
;
316 XMapRequestEvent
*ev
= &e
->xmaprequest
;
318 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
321 if(wa
.override_redirect
) {
322 XSelectInput(dpy
, ev
->window
,
323 (StructureNotifyMask
| PropertyChangeMask
));
327 if(!getclient(ev
->window
))
328 manage(ev
->window
, &wa
);
332 propertynotify(XEvent
*e
) {
335 XPropertyEvent
*ev
= &e
->xproperty
;
337 if(ev
->state
== PropertyDelete
)
340 if((c
= getclient(ev
->window
))) {
341 if(ev
->atom
== wmatom
[WMProtocols
]) {
342 c
->proto
= getproto(c
->win
);
347 case XA_WM_TRANSIENT_FOR
:
348 XGetTransientForHint(dpy
, c
->win
, &trans
);
349 if(!c
->isfloat
&& (c
->isfloat
= (trans
!= 0)))
352 case XA_WM_NORMAL_HINTS
:
356 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
364 unmapnotify(XEvent
*e
) {
366 XUnmapEvent
*ev
= &e
->xunmap
;
368 if((c
= getclient(ev
->window
)))
374 void (*handler
[LASTEvent
]) (XEvent
*) = {
375 [ButtonPress
] = buttonpress
,
376 [ConfigureRequest
] = configurerequest
,
377 [DestroyNotify
] = destroynotify
,
378 [EnterNotify
] = enternotify
,
379 [LeaveNotify
] = leavenotify
,
381 [KeyPress
] = keypress
,
382 [MappingNotify
] = mappingnotify
,
383 [MapRequest
] = maprequest
,
384 [PropertyNotify
] = propertynotify
,
385 [UnmapNotify
] = unmapnotify
390 static unsigned int len
= sizeof(key
) / sizeof(key
[0]);
394 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
395 for(i
= 0; i
< len
; i
++) {
396 code
= XKeysymToKeycode(dpy
, key
[i
].keysym
);
397 XGrabKey(dpy
, code
, key
[i
].mod
, root
, True
,
398 GrabModeAsync
, GrabModeAsync
);
399 XGrabKey(dpy
, code
, key
[i
].mod
| LockMask
, root
, True
,
400 GrabModeAsync
, GrabModeAsync
);
401 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
, root
, True
,
402 GrabModeAsync
, GrabModeAsync
);
403 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
| LockMask
, root
, True
,
404 GrabModeAsync
, GrabModeAsync
);
412 while(XPending(dpy
)) {
413 XNextEvent(dpy
, &ev
);
415 (handler
[ev
.type
])(&ev
); /* call handler */