Xinqi Bao's Git
2773c6f3243669e0394596d369f58dba63de2d36
1 /* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
2 * © 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
3 * See LICENSE file for license details. */
7 #include <X11/keysym.h>
15 void (*func
)(const char *arg
);
21 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
22 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
28 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
33 movemouse(Client
*c
) {
34 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
41 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
42 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
45 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
47 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
50 XUngrabPointer(dpy
, CurrentTime
);
52 case ConfigureRequest
:
55 handler
[ev
.type
](&ev
);
59 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
60 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
61 if(abs(wax
+ nx
) < SNAP
)
63 else if(abs((wax
+ waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
64 nx
= wax
+ waw
- c
->w
- 2 * c
->border
;
65 if(abs(way
- ny
) < SNAP
)
67 else if(abs((way
+ wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
68 ny
= way
+ wah
- c
->h
- 2 * c
->border
;
69 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
76 resizemouse(Client
*c
) {
83 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
84 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
87 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
89 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
92 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
93 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
94 XUngrabPointer(dpy
, CurrentTime
);
95 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
97 case ConfigureRequest
:
100 handler
[ev
.type
](&ev
);
104 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
106 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
108 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
115 buttonpress(XEvent
*e
) {
119 XButtonPressedEvent
*ev
= &e
->xbutton
;
122 if(barwin
== ev
->window
) {
124 for(i
= 0; i
< ntags
; i
++) {
127 snprintf(buf
, sizeof buf
, "%d", i
);
128 if(ev
->button
== Button1
) {
129 if(ev
->state
& MODKEY
)
134 else if(ev
->button
== Button3
) {
135 if(ev
->state
& MODKEY
)
150 else if((c
= getclient(ev
->window
))) {
152 if(CLEANMASK(ev
->state
) != MODKEY
)
154 if(ev
->button
== Button1
&& (lt
->arrange
== floating
|| c
->isfloating
)) {
158 else if(ev
->button
== Button2
)
160 else if(ev
->button
== Button3
161 && (lt
->arrange
== floating
|| c
->isfloating
) && !c
->isfixed
)
170 configurerequest(XEvent
*e
) {
172 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
175 if((c
= getclient(ev
->window
))) {
177 if(ev
->value_mask
& CWBorderWidth
)
178 c
->border
= ev
->border_width
;
179 if(c
->isfixed
|| c
->isfloating
|| (lt
->arrange
== floating
)) {
180 if(ev
->value_mask
& CWX
)
182 if(ev
->value_mask
& CWY
)
184 if(ev
->value_mask
& CWWidth
)
186 if(ev
->value_mask
& CWHeight
)
188 if((c
->x
+ c
->w
) > sw
&& c
->isfloating
)
189 c
->x
= sw
/ 2 - c
->w
/ 2; /* center in x direction */
190 if((c
->y
+ c
->h
) > sh
&& c
->isfloating
)
191 c
->y
= sh
/ 2 - c
->h
/ 2; /* center in y direction */
192 if((ev
->value_mask
& (CWX
| CWY
))
193 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
196 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
204 wc
.width
= ev
->width
;
205 wc
.height
= ev
->height
;
206 wc
.border_width
= ev
->border_width
;
207 wc
.sibling
= ev
->above
;
208 wc
.stack_mode
= ev
->detail
;
209 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
215 configurenotify(XEvent
*e
) {
216 XConfigureEvent
*ev
= &e
->xconfigure
;
218 if (ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
221 XFreePixmap(dpy
, dc
.drawable
);
222 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
223 XResizeWindow(dpy
, barwin
, sw
, bh
);
230 destroynotify(XEvent
*e
) {
232 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
234 if((c
= getclient(ev
->window
)))
239 enternotify(XEvent
*e
) {
241 XCrossingEvent
*ev
= &e
->xcrossing
;
243 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
245 if((c
= getclient(ev
->window
)) && isvisible(c
))
247 else if(ev
->window
== root
) {
255 XExposeEvent
*ev
= &e
->xexpose
;
258 if(barwin
== ev
->window
)
264 keypress(XEvent
*e
) {
265 static unsigned int len
= sizeof key
/ sizeof key
[0];
268 XKeyEvent
*ev
= &e
->xkey
;
270 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
271 for(i
= 0; i
< len
; i
++)
272 if(keysym
== key
[i
].keysym
273 && CLEANMASK(key
[i
].mod
) == CLEANMASK(ev
->state
))
276 key
[i
].func(key
[i
].arg
);
281 leavenotify(XEvent
*e
) {
282 XCrossingEvent
*ev
= &e
->xcrossing
;
284 if((ev
->window
== root
) && !ev
->same_screen
) {
291 mappingnotify(XEvent
*e
) {
292 XMappingEvent
*ev
= &e
->xmapping
;
294 XRefreshKeyboardMapping(ev
);
295 if(ev
->request
== MappingKeyboard
)
300 maprequest(XEvent
*e
) {
301 static XWindowAttributes wa
;
302 XMapRequestEvent
*ev
= &e
->xmaprequest
;
304 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
306 if(wa
.override_redirect
)
308 if(!getclient(ev
->window
))
309 manage(ev
->window
, &wa
);
313 propertynotify(XEvent
*e
) {
316 XPropertyEvent
*ev
= &e
->xproperty
;
318 if(ev
->state
== PropertyDelete
)
320 if((c
= getclient(ev
->window
))) {
323 case XA_WM_TRANSIENT_FOR
:
324 XGetTransientForHint(dpy
, c
->win
, &trans
);
325 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
328 case XA_WM_NORMAL_HINTS
:
332 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
341 unmapnotify(XEvent
*e
) {
343 XUnmapEvent
*ev
= &e
->xunmap
;
345 if((c
= getclient(ev
->window
)))
351 void (*handler
[LASTEvent
]) (XEvent
*) = {
352 [ButtonPress
] = buttonpress
,
353 [ConfigureRequest
] = configurerequest
,
354 [ConfigureNotify
] = configurenotify
,
355 [DestroyNotify
] = destroynotify
,
356 [EnterNotify
] = enternotify
,
357 [LeaveNotify
] = leavenotify
,
359 [KeyPress
] = keypress
,
360 [MappingNotify
] = mappingnotify
,
361 [MapRequest
] = maprequest
,
362 [PropertyNotify
] = propertynotify
,
363 [UnmapNotify
] = unmapnotify
368 static unsigned int len
= sizeof key
/ sizeof key
[0];
372 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
373 for(i
= 0; i
< len
; i
++) {
374 code
= XKeysymToKeycode(dpy
, key
[i
].keysym
);
375 XGrabKey(dpy
, code
, key
[i
].mod
, root
, True
,
376 GrabModeAsync
, GrabModeAsync
);
377 XGrabKey(dpy
, code
, key
[i
].mod
| LockMask
, root
, True
,
378 GrabModeAsync
, GrabModeAsync
);
379 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
, root
, True
,
380 GrabModeAsync
, GrabModeAsync
);
381 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
| LockMask
, root
, True
,
382 GrabModeAsync
, GrabModeAsync
);