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
)
53 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
55 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| StructureNotifyMask
, &ev
);
59 case ConfigureRequest
:
60 synconfig(c
, c
->x
, c
->y
, c
->w
, c
->h
, ev
.xconfigure
.border_width
);
68 c
->x
= ocx
+ (ev
.xmotion
.x
- x1
);
69 c
->y
= ocy
+ (ev
.xmotion
.y
- y1
);
70 resize(c
, False
, TopLeft
);
73 XUngrabPointer(dpy
, CurrentTime
);
77 XUngrabPointer(dpy
, CurrentTime
);
78 handler
[ev
.type
](&ev
);
85 resizemouse(Client
*c
) {
93 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
94 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
97 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
, c
->h
);
99 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| StructureNotifyMask
, &ev
);
103 case ConfigureRequest
:
104 synconfig(c
, c
->x
, c
->y
, c
->w
, c
->h
, ev
.xconfigure
.border_width
);
108 handler
[Expose
](&ev
);
112 if((nw
= abs(ocx
- ev
.xmotion
.x
)))
114 if((nh
= abs(ocy
- ev
.xmotion
.y
)))
116 c
->x
= (ocx
<= ev
.xmotion
.x
) ? ocx
: ocx
- c
->w
;
117 c
->y
= (ocy
<= ev
.xmotion
.y
) ? ocy
: ocy
- c
->h
;
118 if(ocx
<= ev
.xmotion
.x
)
119 sticky
= (ocy
<= ev
.xmotion
.y
) ? TopLeft
: BotLeft
;
121 sticky
= (ocy
<= ev
.xmotion
.y
) ? TopRight
: BotRight
;
122 resize(c
, True
, sticky
);
125 XUngrabPointer(dpy
, CurrentTime
);
129 XUngrabPointer(dpy
, CurrentTime
);
130 handler
[ev
.type
](&ev
);
137 buttonpress(XEvent
*e
) {
141 XButtonPressedEvent
*ev
= &e
->xbutton
;
143 if(barwin
== ev
->window
) {
145 for(a
.i
= 0; a
.i
< ntags
; a
.i
++) {
146 x
+= textw(tags
[a
.i
]);
148 if(ev
->button
== Button1
) {
149 if(ev
->state
& MODKEY
)
154 else if(ev
->button
== Button3
) {
155 if(ev
->state
& MODKEY
)
163 if(ev
->x
< x
+ bmw
) {
164 if(ev
->button
== Button1
)
168 else if((c
= getclient(ev
->window
))) {
170 if(CLEANMASK(ev
->state
) != MODKEY
)
172 if(ev
->button
== Button1
&& (arrange
== dofloat
|| c
->isfloat
)) {
176 else if(ev
->button
== Button2
)
178 else if(ev
->button
== Button3
&& (arrange
== dofloat
|| c
->isfloat
)) {
186 configurerequest(XEvent
*e
) {
187 unsigned long newmask
;
189 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
192 if((c
= getclient(ev
->window
))) {
195 if(ev
->value_mask
& CWX
)
197 if(ev
->value_mask
& CWY
)
199 if(ev
->value_mask
& CWWidth
)
201 if(ev
->value_mask
& CWHeight
)
203 if(ev
->value_mask
& CWBorderWidth
)
204 c
->border
= ev
->border_width
;
210 newmask
= ev
->value_mask
& (~(CWSibling
| CWStackMode
| CWBorderWidth
));
212 XConfigureWindow(dpy
, c
->win
, newmask
, &wc
);
214 synconfig(c
, c
->x
, c
->y
, c
->w
, c
->h
, c
->border
);
217 resize(c
, False
, TopLeft
);
224 wc
.width
= ev
->width
;
225 wc
.height
= ev
->height
;
226 wc
.border_width
= ev
->border_width
;
227 wc
.sibling
= ev
->above
;
228 wc
.stack_mode
= ev
->detail
;
229 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
235 destroynotify(XEvent
*e
) {
237 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
239 if((c
= getclient(ev
->window
)))
244 enternotify(XEvent
*e
) {
246 XCrossingEvent
*ev
= &e
->xcrossing
;
248 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
251 if(((c
= getclient(ev
->window
)) || (c
= getctitle(ev
->window
))) && isvisible(c
))
253 else if(ev
->window
== root
) {
255 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
263 XExposeEvent
*ev
= &e
->xexpose
;
266 if(barwin
== ev
->window
)
268 else if((c
= getctitle(ev
->window
)))
274 keypress(XEvent
*e
) {
275 static unsigned int len
= sizeof(key
) / sizeof(key
[0]);
278 XKeyEvent
*ev
= &e
->xkey
;
280 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
281 for(i
= 0; i
< len
; i
++) {
282 if(keysym
== key
[i
].keysym
283 && CLEANMASK(key
[i
].mod
) == CLEANMASK(ev
->state
))
286 key
[i
].func(&key
[i
].arg
);
293 leavenotify(XEvent
*e
) {
294 XCrossingEvent
*ev
= &e
->xcrossing
;
296 if((ev
->window
== root
) && !ev
->same_screen
) {
303 mappingnotify(XEvent
*e
) {
304 XMappingEvent
*ev
= &e
->xmapping
;
306 XRefreshKeyboardMapping(ev
);
307 if(ev
->request
== MappingKeyboard
)
312 maprequest(XEvent
*e
) {
313 static XWindowAttributes wa
;
314 XMapRequestEvent
*ev
= &e
->xmaprequest
;
316 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
319 if(wa
.override_redirect
) {
320 XSelectInput(dpy
, ev
->window
,
321 (StructureNotifyMask
| PropertyChangeMask
));
325 if(!getclient(ev
->window
))
326 manage(ev
->window
, &wa
);
330 propertynotify(XEvent
*e
) {
333 XPropertyEvent
*ev
= &e
->xproperty
;
335 if(ev
->state
== PropertyDelete
)
338 if((c
= getclient(ev
->window
))) {
339 if(ev
->atom
== wmatom
[WMProtocols
]) {
340 c
->proto
= getproto(c
->win
);
345 case XA_WM_TRANSIENT_FOR
:
346 XGetTransientForHint(dpy
, c
->win
, &trans
);
347 if(!c
->isfloat
&& (c
->isfloat
= (trans
!= 0)))
350 case XA_WM_NORMAL_HINTS
:
354 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
362 unmapnotify(XEvent
*e
) {
364 XUnmapEvent
*ev
= &e
->xunmap
;
366 if((c
= getclient(ev
->window
)))
372 void (*handler
[LASTEvent
]) (XEvent
*) = {
373 [ButtonPress
] = buttonpress
,
374 [ConfigureRequest
] = configurerequest
,
375 [DestroyNotify
] = destroynotify
,
376 [EnterNotify
] = enternotify
,
377 [LeaveNotify
] = leavenotify
,
379 [KeyPress
] = keypress
,
380 [MappingNotify
] = mappingnotify
,
381 [MapRequest
] = maprequest
,
382 [PropertyNotify
] = propertynotify
,
383 [UnmapNotify
] = unmapnotify
388 static unsigned int len
= sizeof(key
) / sizeof(key
[0]);
392 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
393 for(i
= 0; i
< len
; i
++) {
394 code
= XKeysymToKeycode(dpy
, key
[i
].keysym
);
395 XGrabKey(dpy
, code
, key
[i
].mod
, root
, True
,
396 GrabModeAsync
, GrabModeAsync
);
397 XGrabKey(dpy
, code
, key
[i
].mod
| LockMask
, root
, True
,
398 GrabModeAsync
, GrabModeAsync
);
399 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
, root
, True
,
400 GrabModeAsync
, GrabModeAsync
);
401 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
| LockMask
, root
, True
,
402 GrabModeAsync
, GrabModeAsync
);
410 while(XPending(dpy
)) {
411 XNextEvent(dpy
, &ev
);
413 (handler
[ev
.type
])(&ev
); /* call handler */