Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
5 #include <X11/keysym.h>
13 void (*func
)(const char *arg
);
19 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
20 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
26 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
31 movemouse(Client
*c
) {
32 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
39 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
40 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
43 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
45 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
48 XUngrabPointer(dpy
, CurrentTime
);
50 case ConfigureRequest
:
53 handler
[ev
.type
](&ev
);
57 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
58 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
59 if(abs(wax
+ nx
) < SNAP
)
61 else if(abs((wax
+ waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
62 nx
= wax
+ waw
- c
->w
- 2 * c
->border
;
63 if(abs(way
- ny
) < SNAP
)
65 else if(abs((way
+ wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
66 ny
= way
+ wah
- c
->h
- 2 * c
->border
;
67 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
74 resizemouse(Client
*c
) {
81 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
82 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
85 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
87 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
90 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
91 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
92 XUngrabPointer(dpy
, CurrentTime
);
93 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
95 case ConfigureRequest
:
98 handler
[ev
.type
](&ev
);
102 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
104 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
106 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
113 buttonpress(XEvent
*e
) {
117 XButtonPressedEvent
*ev
= &e
->xbutton
;
120 if(barwin
== ev
->window
) {
122 for(i
= 0; i
< ntags
; i
++) {
125 snprintf(buf
, sizeof buf
, "%d", i
);
126 if(ev
->button
== Button1
) {
127 if(ev
->state
& MODKEY
)
132 else if(ev
->button
== Button3
) {
133 if(ev
->state
& MODKEY
)
148 else if((c
= getclient(ev
->window
))) {
150 if(CLEANMASK(ev
->state
) != MODKEY
)
152 if(ev
->button
== Button1
&& (lt
->arrange
== floating
|| c
->isfloating
)) {
156 else if(ev
->button
== Button2
)
158 else if(ev
->button
== Button3
159 && (lt
->arrange
== floating
|| c
->isfloating
) && !c
->isfixed
)
168 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
->isfloating
|| (lt
->arrange
== floating
)) {
178 if(ev
->value_mask
& CWX
)
180 if(ev
->value_mask
& CWY
)
182 if(ev
->value_mask
& CWWidth
)
184 if(ev
->value_mask
& CWHeight
)
186 if((c
->x
+ c
->w
) > sw
&& c
->isfloating
)
187 c
->x
= sw
/ 2 - c
->w
/ 2; /* center in x direction */
188 if((c
->y
+ c
->h
) > sh
&& c
->isfloating
)
189 c
->y
= sh
/ 2 - c
->h
/ 2; /* center in y direction */
190 if((ev
->value_mask
& (CWX
| CWY
))
191 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
194 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
202 wc
.width
= ev
->width
;
203 wc
.height
= ev
->height
;
204 wc
.border_width
= ev
->border_width
;
205 wc
.sibling
= ev
->above
;
206 wc
.stack_mode
= ev
->detail
;
207 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
213 configurenotify(XEvent
*e
) {
214 XConfigureEvent
*ev
= &e
->xconfigure
;
216 if (ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
219 XFreePixmap(dpy
, dc
.drawable
);
220 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
221 XResizeWindow(dpy
, barwin
, sw
, bh
);
228 createnotify(XEvent
*e
) {
229 static XWindowAttributes wa
;
230 XCreateWindowEvent
*ev
= &e
->xcreatewindow
;
232 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
234 if(wa
.override_redirect
)
236 if(!getclient(ev
->window
) && (wa
.map_state
== IsViewable
))
237 manage(ev
->window
, &wa
);
241 destroynotify(XEvent
*e
) {
243 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
245 if((c
= getclient(ev
->window
)))
250 enternotify(XEvent
*e
) {
252 XCrossingEvent
*ev
= &e
->xcrossing
;
254 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
256 if((c
= getclient(ev
->window
)))
258 else if(ev
->window
== root
) {
266 XExposeEvent
*ev
= &e
->xexpose
;
269 if(barwin
== ev
->window
)
275 keypress(XEvent
*e
) {
276 static unsigned int len
= sizeof key
/ sizeof key
[0];
279 XKeyEvent
*ev
= &e
->xkey
;
281 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
282 for(i
= 0; i
< len
; i
++)
283 if(keysym
== key
[i
].keysym
284 && CLEANMASK(key
[i
].mod
) == CLEANMASK(ev
->state
))
287 key
[i
].func(key
[i
].arg
);
292 leavenotify(XEvent
*e
) {
293 XCrossingEvent
*ev
= &e
->xcrossing
;
295 if((ev
->window
== root
) && !ev
->same_screen
) {
302 mappingnotify(XEvent
*e
) {
303 XMappingEvent
*ev
= &e
->xmapping
;
305 XRefreshKeyboardMapping(ev
);
306 if(ev
->request
== MappingKeyboard
)
311 maprequest(XEvent
*e
) {
312 static XWindowAttributes wa
;
313 XMapRequestEvent
*ev
= &e
->xmaprequest
;
315 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
317 if(wa
.override_redirect
)
319 if(!getclient(ev
->window
))
320 manage(ev
->window
, &wa
);
324 propertynotify(XEvent
*e
) {
327 XPropertyEvent
*ev
= &e
->xproperty
;
329 if(ev
->state
== PropertyDelete
)
331 if((c
= getclient(ev
->window
))) {
334 case XA_WM_TRANSIENT_FOR
:
335 XGetTransientForHint(dpy
, c
->win
, &trans
);
336 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
339 case XA_WM_NORMAL_HINTS
:
343 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
352 unmapnotify(XEvent
*e
) {
354 XUnmapEvent
*ev
= &e
->xunmap
;
356 if((c
= getclient(ev
->window
)))
362 void (*handler
[LASTEvent
]) (XEvent
*) = {
363 [ButtonPress
] = buttonpress
,
364 [ConfigureRequest
] = configurerequest
,
365 [ConfigureNotify
] = configurenotify
,
366 [CreateNotify
] = createnotify
,
367 [DestroyNotify
] = destroynotify
,
368 [EnterNotify
] = enternotify
,
369 [LeaveNotify
] = leavenotify
,
371 [KeyPress
] = keypress
,
372 [MappingNotify
] = mappingnotify
,
373 [MapRequest
] = maprequest
,
374 [PropertyNotify
] = propertynotify
,
375 [UnmapNotify
] = unmapnotify
380 static unsigned int len
= sizeof key
/ sizeof key
[0];
384 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
385 for(i
= 0; i
< len
; i
++) {
386 code
= XKeysymToKeycode(dpy
, key
[i
].keysym
);
387 XGrabKey(dpy
, code
, key
[i
].mod
, root
, True
,
388 GrabModeAsync
, GrabModeAsync
);
389 XGrabKey(dpy
, code
, key
[i
].mod
| LockMask
, root
, True
,
390 GrabModeAsync
, GrabModeAsync
);
391 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
, root
, True
,
392 GrabModeAsync
, GrabModeAsync
);
393 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
| LockMask
, root
, True
,
394 GrabModeAsync
, GrabModeAsync
);