Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
5 #include <X11/keysym.h>
14 void (*func
)(const char *arg
);
20 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
21 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
27 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
32 movemouse(Client
*c
) {
33 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
40 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
41 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
44 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
46 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
49 XUngrabPointer(dpy
, CurrentTime
);
51 case ConfigureRequest
:
54 handler
[ev
.type
](&ev
);
58 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
59 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
60 if(abs(wax
+ nx
) < SNAP
)
62 else if(abs((wax
+ waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
63 nx
= wax
+ waw
- c
->w
- 2 * c
->border
;
64 if(abs(way
- ny
) < SNAP
)
66 else if(abs((way
+ wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
67 ny
= way
+ wah
- c
->h
- 2 * c
->border
;
68 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
75 resizemouse(Client
*c
) {
82 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
83 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
86 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
88 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
91 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
92 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
93 XUngrabPointer(dpy
, CurrentTime
);
94 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
96 case ConfigureRequest
:
99 handler
[ev
.type
](&ev
);
103 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
105 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
107 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
114 buttonpress(XEvent
*e
) {
118 XButtonPressedEvent
*ev
= &e
->xbutton
;
121 if(barwin
== ev
->window
) {
123 for(i
= 0; i
< ntags
; i
++) {
126 snprintf(buf
, sizeof buf
, "%d", i
);
127 if(ev
->button
== Button1
) {
128 if(ev
->state
& MODKEY
)
133 else if(ev
->button
== Button3
) {
134 if(ev
->state
& MODKEY
)
142 if((ev
->x
< x
+ blw
) && ev
->button
== Button1
)
145 else if((c
= getclient(ev
->window
))) {
147 if(CLEANMASK(ev
->state
) != MODKEY
)
149 if(ev
->button
== Button1
&& (isfloating() || c
->isfloating
)) {
153 else if(ev
->button
== Button2
)
155 else if(ev
->button
== Button3
156 && (isfloating() || c
->isfloating
) && !c
->isfixed
)
165 configurerequest(XEvent
*e
) {
167 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
170 if((c
= getclient(ev
->window
))) {
172 if(ev
->value_mask
& CWBorderWidth
)
173 c
->border
= ev
->border_width
;
174 if(c
->isfixed
|| c
->isfloating
|| isfloating()) {
175 if(ev
->value_mask
& CWX
)
177 if(ev
->value_mask
& CWY
)
179 if(ev
->value_mask
& CWWidth
)
181 if(ev
->value_mask
& CWHeight
)
183 if((c
->x
+ c
->w
) > sw
&& c
->isfloating
)
184 c
->x
= sw
/ 2 - c
->w
/ 2; /* center in x direction */
185 if((c
->y
+ c
->h
) > sh
&& c
->isfloating
)
186 c
->y
= sh
/ 2 - c
->h
/ 2; /* center in y direction */
187 if((ev
->value_mask
& (CWX
| CWY
))
188 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
191 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
199 wc
.width
= ev
->width
;
200 wc
.height
= ev
->height
;
201 wc
.border_width
= ev
->border_width
;
202 wc
.sibling
= ev
->above
;
203 wc
.stack_mode
= ev
->detail
;
204 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
210 configurenotify(XEvent
*e
) {
211 XConfigureEvent
*ev
= &e
->xconfigure
;
213 if (ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
216 XFreePixmap(dpy
, dc
.drawable
);
217 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
218 XResizeWindow(dpy
, barwin
, sw
, bh
);
225 destroynotify(XEvent
*e
) {
227 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
229 if((c
= getclient(ev
->window
)))
230 unmanage(c
, WithdrawnState
);
234 enternotify(XEvent
*e
) {
236 XCrossingEvent
*ev
= &e
->xcrossing
;
238 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
240 if((c
= getclient(ev
->window
)))
242 else if(ev
->window
== root
) {
250 XExposeEvent
*ev
= &e
->xexpose
;
253 if(barwin
== ev
->window
)
259 keypress(XEvent
*e
) {
260 static unsigned int len
= sizeof key
/ sizeof key
[0];
263 XKeyEvent
*ev
= &e
->xkey
;
265 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
266 for(i
= 0; i
< len
; i
++)
267 if(keysym
== key
[i
].keysym
268 && CLEANMASK(key
[i
].mod
) == CLEANMASK(ev
->state
))
271 key
[i
].func(key
[i
].arg
);
276 leavenotify(XEvent
*e
) {
277 XCrossingEvent
*ev
= &e
->xcrossing
;
279 if((ev
->window
== root
) && !ev
->same_screen
) {
286 mappingnotify(XEvent
*e
) {
287 XMappingEvent
*ev
= &e
->xmapping
;
289 XRefreshKeyboardMapping(ev
);
290 if(ev
->request
== MappingKeyboard
)
295 maprequest(XEvent
*e
) {
296 static XWindowAttributes wa
;
297 XMapRequestEvent
*ev
= &e
->xmaprequest
;
299 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
301 if(wa
.override_redirect
)
303 if(!getclient(ev
->window
))
304 manage(ev
->window
, &wa
);
308 propertynotify(XEvent
*e
) {
311 XPropertyEvent
*ev
= &e
->xproperty
;
313 if(ev
->state
== PropertyDelete
)
315 if((c
= getclient(ev
->window
))) {
318 case XA_WM_TRANSIENT_FOR
:
319 XGetTransientForHint(dpy
, c
->win
, &trans
);
320 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
323 case XA_WM_NORMAL_HINTS
:
327 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
336 unmapnotify(XEvent
*e
) {
338 XUnmapEvent
*ev
= &e
->xunmap
;
340 if((c
= getclient(ev
->window
)) && (ev
->event
== root
)) {
341 if(ev
->send_event
|| c
->unmapped
-- == 0)
342 unmanage(c
, WithdrawnState
);
348 void (*handler
[LASTEvent
]) (XEvent
*) = {
349 [ButtonPress
] = buttonpress
,
350 [ConfigureRequest
] = configurerequest
,
351 [ConfigureNotify
] = configurenotify
,
352 [DestroyNotify
] = destroynotify
,
353 [EnterNotify
] = enternotify
,
354 [LeaveNotify
] = leavenotify
,
356 [KeyPress
] = keypress
,
357 [MappingNotify
] = mappingnotify
,
358 [MapRequest
] = maprequest
,
359 [PropertyNotify
] = propertynotify
,
360 [UnmapNotify
] = unmapnotify
365 static unsigned int len
= sizeof key
/ sizeof key
[0];
369 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
370 for(i
= 0; i
< len
; i
++) {
371 code
= XKeysymToKeycode(dpy
, key
[i
].keysym
);
372 XGrabKey(dpy
, code
, key
[i
].mod
, root
, True
,
373 GrabModeAsync
, GrabModeAsync
);
374 XGrabKey(dpy
, code
, key
[i
].mod
| LockMask
, root
, True
,
375 GrabModeAsync
, GrabModeAsync
);
376 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
, root
, True
,
377 GrabModeAsync
, GrabModeAsync
);
378 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
| LockMask
, root
, True
,
379 GrabModeAsync
, GrabModeAsync
);