Xinqi Bao's Git
ce23e416e1441b8d23cd4749bdd0acebabf101e7
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 * © 2006-2007 Jukka Salmi <jukka at salmi dot ch>
4 * © 2007 Premysl Hruby <dfenze at gmail dot com>
5 * © 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
6 * See LICENSE file for license details. */
10 #include <X11/keysym.h>
11 #include <X11/Xatom.h>
18 void (*func
)(const char *arg
);
24 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
25 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
31 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
36 movemouse(Client
*c
) {
37 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
44 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
45 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
48 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
50 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
53 XUngrabPointer(dpy
, CurrentTime
);
55 case ConfigureRequest
:
58 handler
[ev
.type
](&ev
);
62 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
63 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
64 if(abs(wax
+ nx
) < SNAP
)
66 else if(abs((wax
+ waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
67 nx
= wax
+ waw
- c
->w
- 2 * c
->border
;
68 if(abs(way
- ny
) < SNAP
)
70 else if(abs((way
+ wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
71 ny
= way
+ wah
- c
->h
- 2 * c
->border
;
72 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
79 resizemouse(Client
*c
) {
86 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
87 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
90 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
92 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
95 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
96 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
97 XUngrabPointer(dpy
, CurrentTime
);
98 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
100 case ConfigureRequest
:
103 handler
[ev
.type
](&ev
);
107 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
109 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
111 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
118 buttonpress(XEvent
*e
) {
122 XButtonPressedEvent
*ev
= &e
->xbutton
;
125 if(barwin
== ev
->window
) {
127 for(i
= 0; i
< ntags
; i
++) {
130 snprintf(buf
, sizeof buf
, "%d", i
);
131 if(ev
->button
== Button1
) {
132 if(ev
->state
& MODKEY
)
137 else if(ev
->button
== Button3
) {
138 if(ev
->state
& MODKEY
)
153 else if((c
= getclient(ev
->window
))) {
155 if(CLEANMASK(ev
->state
) != MODKEY
)
157 if(ev
->button
== Button1
&& (lt
->arrange
== floating
|| c
->isfloating
)) {
161 else if(ev
->button
== Button2
)
163 else if(ev
->button
== Button3
164 && (lt
->arrange
== floating
|| c
->isfloating
) && !c
->isfixed
)
173 configurerequest(XEvent
*e
) {
175 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
178 if((c
= getclient(ev
->window
))) {
180 if(ev
->value_mask
& CWBorderWidth
)
181 c
->border
= ev
->border_width
;
182 if(c
->isfixed
|| c
->isfloating
|| (lt
->arrange
== floating
)) {
183 if(ev
->value_mask
& CWX
)
185 if(ev
->value_mask
& CWY
)
187 if(ev
->value_mask
& CWWidth
)
189 if(ev
->value_mask
& CWHeight
)
191 if((c
->x
+ c
->w
) > sw
&& c
->isfloating
)
192 c
->x
= sw
/ 2 - c
->w
/ 2; /* center in x direction */
193 if((c
->y
+ c
->h
) > sh
&& c
->isfloating
)
194 c
->y
= sh
/ 2 - c
->h
/ 2; /* center in y direction */
195 if((ev
->value_mask
& (CWX
| CWY
))
196 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
199 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
207 wc
.width
= ev
->width
;
208 wc
.height
= ev
->height
;
209 wc
.border_width
= ev
->border_width
;
210 wc
.sibling
= ev
->above
;
211 wc
.stack_mode
= ev
->detail
;
212 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
218 configurenotify(XEvent
*e
) {
219 XConfigureEvent
*ev
= &e
->xconfigure
;
221 if (ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
224 XFreePixmap(dpy
, dc
.drawable
);
225 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
226 XResizeWindow(dpy
, barwin
, sw
, bh
);
233 destroynotify(XEvent
*e
) {
235 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
237 if((c
= getclient(ev
->window
)))
242 enternotify(XEvent
*e
) {
244 XCrossingEvent
*ev
= &e
->xcrossing
;
246 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
248 if((c
= getclient(ev
->window
)))
250 else if(ev
->window
== root
) {
258 XExposeEvent
*ev
= &e
->xexpose
;
261 if(barwin
== ev
->window
)
267 keypress(XEvent
*e
) {
268 static unsigned int len
= sizeof key
/ sizeof key
[0];
271 XKeyEvent
*ev
= &e
->xkey
;
273 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
274 for(i
= 0; i
< len
; i
++)
275 if(keysym
== key
[i
].keysym
276 && CLEANMASK(key
[i
].mod
) == CLEANMASK(ev
->state
))
279 key
[i
].func(key
[i
].arg
);
284 leavenotify(XEvent
*e
) {
285 XCrossingEvent
*ev
= &e
->xcrossing
;
287 if((ev
->window
== root
) && !ev
->same_screen
) {
294 mappingnotify(XEvent
*e
) {
295 XMappingEvent
*ev
= &e
->xmapping
;
297 XRefreshKeyboardMapping(ev
);
298 if(ev
->request
== MappingKeyboard
)
303 maprequest(XEvent
*e
) {
304 static XWindowAttributes wa
;
305 XMapRequestEvent
*ev
= &e
->xmaprequest
;
307 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
309 if(wa
.override_redirect
)
311 if(!getclient(ev
->window
))
312 manage(ev
->window
, &wa
);
316 propertynotify(XEvent
*e
) {
319 XPropertyEvent
*ev
= &e
->xproperty
;
321 if(ev
->state
== PropertyDelete
)
323 if((c
= getclient(ev
->window
))) {
326 case XA_WM_TRANSIENT_FOR
:
327 XGetTransientForHint(dpy
, c
->win
, &trans
);
328 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
331 case XA_WM_NORMAL_HINTS
:
335 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
344 unmapnotify(XEvent
*e
) {
346 XUnmapEvent
*ev
= &e
->xunmap
;
348 if((c
= getclient(ev
->window
)))
354 void (*handler
[LASTEvent
]) (XEvent
*) = {
355 [ButtonPress
] = buttonpress
,
356 [ConfigureRequest
] = configurerequest
,
357 [ConfigureNotify
] = configurenotify
,
358 [DestroyNotify
] = destroynotify
,
359 [EnterNotify
] = enternotify
,
360 [LeaveNotify
] = leavenotify
,
362 [KeyPress
] = keypress
,
363 [MappingNotify
] = mappingnotify
,
364 [MapRequest
] = maprequest
,
365 [PropertyNotify
] = propertynotify
,
366 [UnmapNotify
] = unmapnotify
371 static unsigned int len
= sizeof key
/ sizeof key
[0];
375 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
376 for(i
= 0; i
< len
; i
++) {
377 code
= XKeysymToKeycode(dpy
, key
[i
].keysym
);
378 XGrabKey(dpy
, code
, key
[i
].mod
, root
, True
,
379 GrabModeAsync
, GrabModeAsync
);
380 XGrabKey(dpy
, code
, key
[i
].mod
| LockMask
, root
, True
,
381 GrabModeAsync
, GrabModeAsync
);
382 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
, root
, True
,
383 GrabModeAsync
, GrabModeAsync
);
384 XGrabKey(dpy
, code
, key
[i
].mod
| numlockmask
| LockMask
, root
, True
,
385 GrabModeAsync
, GrabModeAsync
);