Xinqi Bao's Git
ac96df1c190ad464d4ac2c5e67490189170811a0
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
)
138 else if(ev
->button
== Button4
)
140 else if(ev
->button
== Button5
)
151 else if(ev
->button
== Button4
)
153 else if(ev
->button
== Button5
)
156 else if((c
= getclient(ev
->window
))) {
158 if(CLEANMASK(ev
->state
) != MODKEY
)
160 if(ev
->button
== Button1
&& (lt
->arrange
== floating
|| c
->isfloating
)) {
164 else if(ev
->button
== Button2
)
166 else if(ev
->button
== Button3
167 && (lt
->arrange
== floating
|| c
->isfloating
) && !c
->isfixed
)
176 configurerequest(XEvent
*e
) {
178 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
181 if((c
= getclient(ev
->window
))) {
183 if(ev
->value_mask
& CWBorderWidth
)
184 c
->border
= ev
->border_width
;
185 if(c
->isfixed
|| c
->isfloating
|| (lt
->arrange
== floating
)) {
186 if(ev
->value_mask
& CWX
)
188 if(ev
->value_mask
& CWY
)
190 if(ev
->value_mask
& CWWidth
)
192 if(ev
->value_mask
& CWHeight
)
194 if((c
->x
+ c
->w
) > sw
&& c
->isfloating
)
195 c
->x
= sw
/ 2 - c
->w
/ 2; /* center in x direction */
196 if((c
->y
+ c
->h
) > sh
&& c
->isfloating
)
197 c
->y
= sh
/ 2 - c
->h
/ 2; /* center in y direction */
198 if((ev
->value_mask
& (CWX
| CWY
))
199 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
202 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
210 wc
.width
= ev
->width
;
211 wc
.height
= ev
->height
;
212 wc
.border_width
= ev
->border_width
;
213 wc
.sibling
= ev
->above
;
214 wc
.stack_mode
= ev
->detail
;
215 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
221 configurenotify(XEvent
*e
) {
222 XConfigureEvent
*ev
= &e
->xconfigure
;
224 if (ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
227 XFreePixmap(dpy
, dc
.drawable
);
228 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
229 XResizeWindow(dpy
, barwin
, sw
, bh
);
236 destroynotify(XEvent
*e
) {
238 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
240 if((c
= getclient(ev
->window
)))
245 enternotify(XEvent
*e
) {
247 XCrossingEvent
*ev
= &e
->xcrossing
;
249 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
251 if((c
= getclient(ev
->window
)))
253 else if(ev
->window
== root
) {
261 XExposeEvent
*ev
= &e
->xexpose
;
264 if(barwin
== ev
->window
)
270 keypress(XEvent
*e
) {
271 static unsigned int len
= sizeof key
/ sizeof key
[0];
274 XKeyEvent
*ev
= &e
->xkey
;
276 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
277 for(i
= 0; i
< len
; i
++)
278 if(keysym
== key
[i
].keysym
279 && CLEANMASK(key
[i
].mod
) == CLEANMASK(ev
->state
))
282 key
[i
].func(key
[i
].arg
);
287 leavenotify(XEvent
*e
) {
288 XCrossingEvent
*ev
= &e
->xcrossing
;
290 if((ev
->window
== root
) && !ev
->same_screen
) {
297 mappingnotify(XEvent
*e
) {
298 XMappingEvent
*ev
= &e
->xmapping
;
300 XRefreshKeyboardMapping(ev
);
301 if(ev
->request
== MappingKeyboard
)
306 maprequest(XEvent
*e
) {
307 static XWindowAttributes wa
;
308 XMapRequestEvent
*ev
= &e
->xmaprequest
;
310 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
312 if(wa
.override_redirect
)
314 if(!getclient(ev
->window
))
315 manage(ev
->window
, &wa
);
319 propertynotify(XEvent
*e
) {
322 XPropertyEvent
*ev
= &e
->xproperty
;
324 if(ev
->state
== PropertyDelete
)
326 if((c
= getclient(ev
->window
))) {
329 case XA_WM_TRANSIENT_FOR
:
330 XGetTransientForHint(dpy
, c
->win
, &trans
);
331 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
334 case XA_WM_NORMAL_HINTS
:
338 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
347 unmapnotify(XEvent
*e
) {
349 XUnmapEvent
*ev
= &e
->xunmap
;
351 if((c
= getclient(ev
->window
)))
357 void (*handler
[LASTEvent
]) (XEvent
*) = {
358 [ButtonPress
] = buttonpress
,
359 [ConfigureRequest
] = configurerequest
,
360 [ConfigureNotify
] = configurenotify
,
361 [DestroyNotify
] = destroynotify
,
362 [EnterNotify
] = enternotify
,
363 [LeaveNotify
] = leavenotify
,
365 [KeyPress
] = keypress
,
366 [MappingNotify
] = mappingnotify
,
367 [MapRequest
] = maprequest
,
368 [PropertyNotify
] = propertynotify
,
369 [UnmapNotify
] = unmapnotify
374 static unsigned int len
= sizeof key
/ sizeof key
[0];
378 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
379 for(i
= 0; i
< len
; i
++) {
380 code
= XKeysymToKeycode(dpy
, key
[i
].keysym
);
381 XGrabKey(dpy
, code
, key
[i
].mod
, root
, True
,
382 GrabModeAsync
, GrabModeAsync
);
383 XGrabKey(dpy
, code
, key
[i
].mod
| LockMask
, root
, True
,
384 GrabModeAsync
, GrabModeAsync
);
385 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
, root
, True
,
386 GrabModeAsync
, GrabModeAsync
);
387 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
| LockMask
, root
, True
,
388 GrabModeAsync
, GrabModeAsync
);