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 destroynotify(XEvent
*e
) {
230 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
232 if((c
= getclient(ev
->window
)))
237 enternotify(XEvent
*e
) {
239 XCrossingEvent
*ev
= &e
->xcrossing
;
241 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
243 if((c
= getclient(ev
->window
)))
245 else if(ev
->window
== root
) {
253 XExposeEvent
*ev
= &e
->xexpose
;
256 if(barwin
== ev
->window
)
262 keypress(XEvent
*e
) {
263 static unsigned int len
= sizeof key
/ sizeof key
[0];
266 XKeyEvent
*ev
= &e
->xkey
;
268 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
269 for(i
= 0; i
< len
; i
++)
270 if(keysym
== key
[i
].keysym
271 && CLEANMASK(key
[i
].mod
) == CLEANMASK(ev
->state
))
274 key
[i
].func(key
[i
].arg
);
279 leavenotify(XEvent
*e
) {
280 XCrossingEvent
*ev
= &e
->xcrossing
;
282 if((ev
->window
== root
) && !ev
->same_screen
) {
289 mappingnotify(XEvent
*e
) {
290 XMappingEvent
*ev
= &e
->xmapping
;
292 XRefreshKeyboardMapping(ev
);
293 if(ev
->request
== MappingKeyboard
)
298 maprequest(XEvent
*e
) {
299 static XWindowAttributes wa
;
300 XMapRequestEvent
*ev
= &e
->xmaprequest
;
302 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
304 if(wa
.override_redirect
)
306 if(!getclient(ev
->window
))
307 manage(ev
->window
, &wa
);
311 propertynotify(XEvent
*e
) {
314 XPropertyEvent
*ev
= &e
->xproperty
;
316 if(ev
->state
== PropertyDelete
)
318 if((c
= getclient(ev
->window
))) {
321 case XA_WM_TRANSIENT_FOR
:
322 XGetTransientForHint(dpy
, c
->win
, &trans
);
323 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
326 case XA_WM_NORMAL_HINTS
:
330 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
339 unmapnotify(XEvent
*e
) {
341 XUnmapEvent
*ev
= &e
->xunmap
;
343 if((c
= getclient(ev
->window
)))
349 void (*handler
[LASTEvent
]) (XEvent
*) = {
350 [ButtonPress
] = buttonpress
,
351 [ConfigureRequest
] = configurerequest
,
352 [ConfigureNotify
] = configurenotify
,
353 [DestroyNotify
] = destroynotify
,
354 [EnterNotify
] = enternotify
,
355 [LeaveNotify
] = leavenotify
,
357 [KeyPress
] = keypress
,
358 [MappingNotify
] = mappingnotify
,
359 [MapRequest
] = maprequest
,
360 [PropertyNotify
] = propertynotify
,
361 [UnmapNotify
] = unmapnotify
366 static unsigned int len
= sizeof key
/ sizeof key
[0];
370 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
371 for(i
= 0; i
< len
; i
++) {
372 code
= XKeysymToKeycode(dpy
, key
[i
].keysym
);
373 XGrabKey(dpy
, code
, key
[i
].mod
, root
, True
,
374 GrabModeAsync
, GrabModeAsync
);
375 XGrabKey(dpy
, code
, key
[i
].mod
| LockMask
, root
, True
,
376 GrabModeAsync
, GrabModeAsync
);
377 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
, root
, True
,
378 GrabModeAsync
, GrabModeAsync
);
379 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
| LockMask
, root
, True
,
380 GrabModeAsync
, GrabModeAsync
);