wmii

git clone git://oldgit.suckless.org/wmii/
Log | Files | Refs | README | LICENSE

commit fecbab94dd2421298d8756e2280264ac909494c2
parent 55e8f6e55caa6d2799a4ecb6a604db931fd37e8f
Author: Anthony Martin <ality@pbrane.org>
Date:   Thu, 23 Mar 2006 05:22:43 -0800

cleaned up formatting in event.c, kb.c, mouse.c, and wm.h


Diffstat:
cmd/wm/event.c | 190++++++++++++++++++++++++++++++++++++++++---------------------------------------
cmd/wm/kb.c | 181++++++++++++++++++++++++++++++++++++++++---------------------------------------
cmd/wm/mouse.c | 989+++++++++++++++++++++++++++++++++++++++----------------------------------------
cmd/wm/wm.h | 62+++++++++++++++++++++++++++++++-------------------------------
4 files changed, 705 insertions(+), 717 deletions(-)

diff --git a/cmd/wm/event.c b/cmd/wm/event.c @@ -26,44 +26,44 @@ void (*handler[LASTEvent]) (XEvent *); void init_x_event_handler() { - int i; - /* init event handler */ - for(i = 0; i < LASTEvent; i++) { - handler[i] = 0; - } - handler[ButtonPress] = handle_buttonpress; - handler[ConfigureRequest] = handle_configurerequest; - handler[DestroyNotify] = handle_destroynotify; - handler[Expose] = handle_expose; - handler[KeyPress] = handle_keypress; - handler[KeymapNotify] = handle_keymapnotify; - handler[MapRequest] = handle_maprequest; - handler[PropertyNotify] = handle_propertynotify; - handler[UnmapNotify] = handle_unmapnotify; + int i; + /* init event handler */ + for(i = 0; i < LASTEvent; i++) + handler[i] = 0; + handler[ButtonPress] = handle_buttonpress; + handler[ConfigureRequest] = handle_configurerequest; + handler[DestroyNotify] = handle_destroynotify; + handler[Expose] = handle_expose; + handler[KeyPress] = handle_keypress; + handler[KeymapNotify] = handle_keymapnotify; + handler[MapRequest] = handle_maprequest; + handler[PropertyNotify] = handle_propertynotify; + handler[UnmapNotify] = handle_unmapnotify; } void check_x_event(IXPConn *c) { - XEvent ev; - while(XPending(dpy)) { /* main evet loop */ - XNextEvent(dpy, &ev); - if(handler[ev.type]) - (handler[ev.type]) (&ev); /* call handler */ - } + XEvent ev; + while(XPending(dpy)) { /* main evet loop */ + XNextEvent(dpy, &ev); + if(handler[ev.type]) + (handler[ev.type]) (&ev); /* call handler */ + } } static void handle_buttonpress(XEvent *e) { - Client *c; - XButtonPressedEvent *ev = &e->xbutton; + Client *c; + XButtonPressedEvent *ev = &e->xbutton; static char buf[32]; if(ev->window == barwin) { unsigned int i; for(i = 0; i < nlabel; i++) if(blitz_ispointinrect(ev->x, ev->y, &label[i]->rect)) { - snprintf(buf, sizeof(buf), "LabelClick %s %d\n", label[i]->name, ev->button); + snprintf(buf, sizeof(buf), "LabelClick %s %d\n", + label[i]->name, ev->button); write_event(buf, True); return; } @@ -76,7 +76,8 @@ handle_buttonpress(XEvent *e) } } if(c->nframe) { - snprintf(buf, sizeof(buf), "ClientClick %d %d\n", frame2index(c->frame[c->sel]) + 1, ev->button); + snprintf(buf, sizeof(buf), "ClientClick %d %d\n", + frame2index(c->frame[c->sel]) + 1, ev->button); write_event(buf, True); } } @@ -103,7 +104,8 @@ handle_buttonpress(XEvent *e) focus(c); if(c->nframe) { - snprintf(buf, sizeof(buf), "ClientClick %d %d\n", frame2index(c->frame[c->sel]) + 1, ev->button); + snprintf(buf, sizeof(buf), "ClientClick %d %d\n", + frame2index(c->frame[c->sel]) + 1, ev->button); write_event(buf, True); } } @@ -113,76 +115,76 @@ handle_buttonpress(XEvent *e) static void handle_configurerequest(XEvent *e) { - XConfigureRequestEvent *ev = &e->xconfigurerequest; - XWindowChanges wc; - Client *c; + XConfigureRequestEvent *ev = &e->xconfigurerequest; + XWindowChanges wc; + Client *c; - c = win2client(ev->window); + c = win2client(ev->window); ev->value_mask &= ~CWSibling; - if(c) { - gravitate(c, True); + if(c) { + gravitate(c, True); - if(c->frame && (area2index(c->frame[c->sel]->area) == 0)) { + if(c->frame && (area2index(c->frame[c->sel]->area) == 0)) { if(ev->value_mask & CWX) c->rect.x = ev->x; if(ev->value_mask & CWY) c->rect.y = ev->y; - if(ev->value_mask & CWWidth) - c->rect.width = ev->width; - if(ev->value_mask & CWHeight) - c->rect.height = ev->height; - } - if(ev->value_mask & CWBorderWidth) - c->border = ev->border_width; + if(ev->value_mask & CWWidth) + c->rect.width = ev->width; + if(ev->value_mask & CWHeight) + c->rect.height = ev->height; + } + if(ev->value_mask & CWBorderWidth) + c->border = ev->border_width; - gravitate(c, False); + gravitate(c, False); - if(c->nframe) { + if(c->nframe) { Frame *f = c->frame[c->sel]; - f->rect.x = wc.x = c->rect.x - def.border; - f->rect.y = wc.y = c->rect.y - bar_height(); - f->rect.width = wc.width = c->rect.width + 2 * def.border; - f->rect.height = wc.height = c->rect.height + def.border + bar_height(); - wc.border_width = 1; + f->rect.x = wc.x = c->rect.x - def.border; + f->rect.y = wc.y = c->rect.y - bar_height(); + f->rect.width = wc.width = c->rect.width + 2 * def.border; + f->rect.height = wc.height = c->rect.height + def.border + + bar_height(); + wc.border_width = 1; wc.sibling = None; wc.stack_mode = ev->detail; if(f->area->view != view[sel]) f->rect.x += 2 * rect.width; - XConfigureWindow(dpy, c->framewin, ev->value_mask, &wc); - configure_client(c); - } - } + XConfigureWindow(dpy, c->framewin, ev->value_mask, &wc); + configure_client(c); + } + } - wc.x = ev->x; - wc.y = ev->y; - wc.width = ev->width; - wc.height = ev->height; + wc.x = ev->x; + wc.y = ev->y; + wc.width = ev->width; + wc.height = ev->height; - if(c && c->frame) { + if(c && c->frame) { wc.x = def.border; wc.y = bar_height(); - if(area2index(c->frame[c->sel]->area) > 0) { - wc.width = c->rect.width; - wc.height = c->rect.height; - } - } + if(area2index(c->frame[c->sel]->area) > 0) { + wc.width = c->rect.width; + wc.height = c->rect.height; + } + } - wc.border_width = 0; + wc.border_width = 0; wc.sibling = None; wc.stack_mode = Above; ev->value_mask &= ~CWStackMode; ev->value_mask |= CWBorderWidth; - XConfigureWindow(dpy, ev->window, ev->value_mask, &wc); - XSync(dpy, False); - + XConfigureWindow(dpy, ev->window, ev->value_mask, &wc); + XSync(dpy, False); } static void handle_destroynotify(XEvent *e) { - Client *c; - XDestroyWindowEvent *ev = &e->xdestroywindow; + Client *c; + XDestroyWindowEvent *ev = &e->xdestroywindow; if((c = win2client(ev->window))) destroy_client(c); @@ -192,13 +194,13 @@ static void handle_expose(XEvent *e) { XExposeEvent *ev = &e->xexpose; - static Client *c; - if(ev->count == 0) { - if(ev->window == barwin) + static Client *c; + if(ev->count == 0) { + if(ev->window == barwin) draw_bar(); else if((c = win2clientframe(ev->window))) - draw_client(c); - } + draw_client(c); + } } static void @@ -206,7 +208,7 @@ handle_keypress(XEvent *e) { XKeyEvent *ev = &e->xkey; ev->state &= valid_mask; - handle_key(root, ev->state, (KeyCode) ev->keycode); + handle_key(root, ev->state, (KeyCode) ev->keycode); } static void @@ -218,41 +220,41 @@ handle_keymapnotify(XEvent *e) static void handle_maprequest(XEvent *e) { - XMapRequestEvent *ev = &e->xmaprequest; - static XWindowAttributes wa; + XMapRequestEvent *ev = &e->xmaprequest; + static XWindowAttributes wa; - if(!XGetWindowAttributes(dpy, ev->window, &wa)) - return; - if(wa.override_redirect) { - XSelectInput(dpy, ev->window, - (StructureNotifyMask | PropertyChangeMask)); - return; - } + if(!XGetWindowAttributes(dpy, ev->window, &wa)) + return; + if(wa.override_redirect) { + XSelectInput(dpy, ev->window, + (StructureNotifyMask | PropertyChangeMask)); + return; + } - /* there're client which send map requests twice */ - if(!win2client(ev->window)) - manage_client(alloc_client(ev->window, &wa)); + /* there're client which send map requests twice */ + if(!win2client(ev->window)) + manage_client(alloc_client(ev->window, &wa)); } static void handle_propertynotify(XEvent *e) { - XPropertyEvent *ev = &e->xproperty; - Client *c; + XPropertyEvent *ev = &e->xproperty; + Client *c; - if(ev->state == PropertyDelete) - return; /* ignore */ + if(ev->state == PropertyDelete) + return; /* ignore */ - if((c = win2client(ev->window))) - update_client_property(c, ev); + if((c = win2client(ev->window))) + update_client_property(c, ev); } static void handle_unmapnotify(XEvent *e) { - Client *c; - XUnmapEvent *ev = &e->xunmap; + Client *c; + XUnmapEvent *ev = &e->xunmap; - if((c = win2client(ev->window))) - destroy_client(c); + if((c = win2client(ev->window))) + destroy_client(c); } diff --git a/cmd/wm/kb.c b/cmd/wm/kb.c @@ -13,28 +13,27 @@ void init_lock_modifiers() { - XModifierKeymap *modmap; - KeyCode num_lock; - static int masks[] = { - ShiftMask, LockMask, ControlMask, Mod1Mask, - Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask - }; - int i; + XModifierKeymap *modmap; + KeyCode num_lock; + static int masks[] = { + ShiftMask, LockMask, ControlMask, Mod1Mask, + Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask + }; + int i; - num_lock_mask = 0; - modmap = XGetModifierMapping(dpy); - num_lock = XKeysymToKeycode(dpy, XStringToKeysym("Num_Lock")); + num_lock_mask = 0; + modmap = XGetModifierMapping(dpy); + num_lock = XKeysymToKeycode(dpy, XStringToKeysym("Num_Lock")); - if(modmap && modmap->max_keypermod > 0) { - int max = (sizeof(masks) / sizeof(int)) * modmap->max_keypermod; - for(i = 0; i < max; i++) { - if(num_lock && (modmap->modifiermap[i] == num_lock)) - num_lock_mask = masks[i / modmap->max_keypermod]; - } - } - XFreeModifiermap(modmap); + if(modmap && modmap->max_keypermod > 0) { + int max = (sizeof(masks) / sizeof(int)) * modmap->max_keypermod; + for(i = 0; i < max; i++) + if(num_lock && (modmap->modifiermap[i] == num_lock)) + num_lock_mask = masks[i / modmap->max_keypermod]; + } + XFreeModifiermap(modmap); - valid_mask = 255 & ~(num_lock_mask | LockMask); + valid_mask = 255 & ~(num_lock_mask | LockMask); } static unsigned long @@ -61,26 +60,26 @@ blitz_strtomod(char *val) static void grab_key(Key *k) { - XGrabKey(dpy, k->key, k->mod, root, - True, GrabModeAsync, GrabModeAsync); - if(num_lock_mask) { - XGrabKey(dpy, k->key, k->mod | num_lock_mask, root, - True, GrabModeAsync, GrabModeAsync); - XGrabKey(dpy, k->key, k->mod | num_lock_mask | LockMask, root, - True, GrabModeAsync, GrabModeAsync); - } - XSync(dpy, False); + XGrabKey(dpy, k->key, k->mod, root, + True, GrabModeAsync, GrabModeAsync); + if(num_lock_mask) { + XGrabKey(dpy, k->key, k->mod | num_lock_mask, root, + True, GrabModeAsync, GrabModeAsync); + XGrabKey(dpy, k->key, k->mod | num_lock_mask | LockMask, root, + True, GrabModeAsync, GrabModeAsync); + } + XSync(dpy, False); } static void ungrab_key(Key *k) { - XUngrabKey(dpy, k->key, k->mod, root); - if(num_lock_mask) { - XUngrabKey(dpy, k->key, k->mod | num_lock_mask, root); - XUngrabKey(dpy, k->key, k->mod | num_lock_mask | LockMask, root); - } - XSync(dpy, False); + XUngrabKey(dpy, k->key, k->mod, root); + if(num_lock_mask) { + XUngrabKey(dpy, k->key, k->mod | num_lock_mask, root); + XUngrabKey(dpy, k->key, k->mod | num_lock_mask | LockMask, root); + } + XSync(dpy, False); } static Key * @@ -97,42 +96,42 @@ static Key * get_key(const char *name) { char buf[128]; - char *seq[8]; - char *kstr; - unsigned int i, toks; + char *seq[8]; + char *kstr; + unsigned int i, toks; static unsigned short id = 1; - Key *k = 0, *r = 0; + Key *k = 0, *r = 0; if((k = name2key(name))) { ungrab_key(k); return k; } - cext_strlcpy(buf, name, sizeof(buf)); - toks = cext_tokenize(seq, 8, buf, ','); + cext_strlcpy(buf, name, sizeof(buf)); + toks = cext_tokenize(seq, 8, buf, ','); - for(i = 0; i < toks; i++) { - if(!k) - r = k = cext_emallocz(sizeof(Key)); - else { - k->next = cext_emallocz(sizeof(Key)); - k = k->next; - } - cext_strlcpy(k->name, name, sizeof(k->name)); - kstr = strrchr(seq[i], '-'); - if(kstr) - kstr++; - else - kstr = seq[i]; - k->key = XKeysymToKeycode(dpy, XStringToKeysym(kstr)); - k->mod = blitz_strtomod(seq[i]); - } + for(i = 0; i < toks; i++) { + if(!k) + r = k = cext_emallocz(sizeof(Key)); + else { + k->next = cext_emallocz(sizeof(Key)); + k = k->next; + } + cext_strlcpy(k->name, name, sizeof(k->name)); + kstr = strrchr(seq[i], '-'); + if(kstr) + kstr++; + else + kstr = seq[i]; + k->key = XKeysymToKeycode(dpy, XStringToKeysym(kstr)); + k->mod = blitz_strtomod(seq[i]); + } if(r) { r->id = id++; key = (Key **)cext_array_attach((void **)key, r, sizeof(Key *), &keysz); nkey++; } - + return r; } @@ -141,48 +140,49 @@ destroy_key(Key *k) { cext_array_detach((void **)key, k, &keysz); nkey--; - if(k->next) - destroy_key(k->next); - free(k); + if(k->next) + destroy_key(k->next); + free(k); } static void next_keystroke(unsigned long *mod, KeyCode *keyCode) { - XEvent e; - KeySym sym; - *mod = 0; - do { - XMaskEvent(dpy, KeyPressMask, &e); - *mod |= e.xkey.state & valid_mask; - *keyCode = (KeyCode) e.xkey.keycode; - sym = XKeycodeToKeysym(dpy, e.xkey.keycode, 0); - } while(IsModifierKey(sym)); + XEvent e; + KeySym sym; + *mod = 0; + do { + XMaskEvent(dpy, KeyPressMask, &e); + *mod |= e.xkey.state & valid_mask; + *keyCode = (KeyCode) e.xkey.keycode; + sym = XKeycodeToKeysym(dpy, e.xkey.keycode, 0); + } while(IsModifierKey(sym)); } static void emulate_key_press(unsigned long mod, KeyCode key) { - XEvent e; - Window client_win; - int revert; + XEvent e; + Window client_win; + int revert; - XGetInputFocus(dpy, &client_win, &revert); + XGetInputFocus(dpy, &client_win, &revert); - e.xkey.type = KeyPress; - e.xkey.time = CurrentTime; - e.xkey.window = client_win; - e.xkey.display = dpy; - e.xkey.state = mod; - e.xkey.keycode = key; - XSendEvent(dpy, client_win, True, KeyPressMask, &e); - e.xkey.type = KeyRelease; - XSendEvent(dpy, client_win, True, KeyReleaseMask, &e); - XSync(dpy, False); + e.xkey.type = KeyPress; + e.xkey.time = CurrentTime; + e.xkey.window = client_win; + e.xkey.display = dpy; + e.xkey.state = mod; + e.xkey.keycode = key; + XSendEvent(dpy, client_win, True, KeyPressMask, &e); + e.xkey.type = KeyRelease; + XSendEvent(dpy, client_win, True, KeyReleaseMask, &e); + XSync(dpy, False); } static Key ** -match_keys(Key **keys, unsigned int n, unsigned long mod, KeyCode keycode, Bool next, unsigned int *nres) +match_keys(Key **keys, unsigned int n, unsigned long mod, KeyCode keycode, + Bool next, unsigned int *nres) { Key **result = nil; unsigned int ressz = 0; @@ -191,7 +191,8 @@ match_keys(Key **keys, unsigned int n, unsigned long mod, KeyCode keycode, Bool for(i = 0; i < n; i++) { Key *k = next ? keys[i]->next : keys[i]; if(k && (k->mod == mod) && (k->key == keycode)) { - result = (Key **)cext_array_attach((void **)result, k, sizeof(Key *), &ressz); + result = (Key **)cext_array_attach((void **)result, k, + sizeof(Key *), &ressz); (*nres)++; } } @@ -201,13 +202,13 @@ match_keys(Key **keys, unsigned int n, unsigned long mod, KeyCode keycode, Bool static void handle_key_seq(Window w, Key **done, unsigned int ndone) { - unsigned long mod; - KeyCode key; + unsigned long mod; + KeyCode key; Key **found = nil; unsigned int nfound = 0; char buf[128]; - next_keystroke(&mod, &key); + next_keystroke(&mod, &key); found = match_keys(done, ndone, mod, key, True, &nfound); if((done[0]->mod == mod) && (done[0]->key == key)) @@ -253,7 +254,7 @@ handle_key(Window w, unsigned long mod, KeyCode keycode) XUngrabKeyboard(dpy, CurrentTime); XSync(dpy, False); break; - } + } free(found); } @@ -264,7 +265,7 @@ update_keys() char *l, *p; init_lock_modifiers(); - + while(nkey) { ungrab_key(key[0]); destroy_key(key[0]); diff --git a/cmd/wm/mouse.c b/cmd/wm/mouse.c @@ -12,561 +12,546 @@ Align xy2align(XRectangle *rect, int x, int y) { - - int w = x <= rect->x + rect->width / 2; - int n = y <= rect->y + rect->height / 2; - int e = x > rect->x + rect->width / 2; - int s = y > rect->y + rect->height / 2; - int nw = w && n; - int ne = e && n; - int sw = w && s; - int se = e && s; - - if(nw) - return NWEST; - else if(ne) - return NEAST; - else if(se) - return SEAST; - else if(sw) - return SWEST; - else if(w) - return WEST; - else if(e) - return EAST; - else if(n) - return NORTH; - else if(s) - return SOUTH; - - return CENTER; + int w = x <= rect->x + rect->width / 2; + int n = y <= rect->y + rect->height / 2; + int e = x > rect->x + rect->width / 2; + int s = y > rect->y + rect->height / 2; + int nw = w && n; + int ne = e && n; + int sw = w && s; + int se = e && s; + + if(nw) + return NWEST; + else if(ne) + return NEAST; + else if(se) + return SEAST; + else if(sw) + return SWEST; + else if(w) + return WEST; + else if(e) + return EAST; + else if(n) + return NORTH; + else if(s) + return SOUTH; + + return CENTER; } static int check_vert_match(XRectangle *r, XRectangle *neighbor) { - /* check if neighbor matches edge */ - return (((neighbor->y <= r->y) - && (neighbor->y + neighbor->height >= r->y)) - || ((neighbor->y >= r->y) - && (r->y + r->height >= neighbor->y))); + /* check if neighbor matches edge */ + return (((neighbor->y <= r->y) && (neighbor->y + neighbor->height >= r->y)) + || ((neighbor->y >= r->y) && (r->y + r->height >= neighbor->y))); } static int check_horiz_match(XRectangle * r, XRectangle * neighbor) { - /* check if neighbor matches edge */ - return (((neighbor->x <= r->x) - && (neighbor->x + neighbor->width >= r->x)) - || ((neighbor->x >= r->x) - && (r->x + r->width >= neighbor->x))); + /* check if neighbor matches edge */ + return (((neighbor->x <= r->x) && (neighbor->x + neighbor->width >= r->x)) + || ((neighbor->x >= r->x) + && (r->x + r->width >= neighbor->x))); } static void snap_move(XRectangle * r, XRectangle * rects, - unsigned int num, int snapw, int snaph) + unsigned int num, int snapw, int snaph) { - - int i, j, w = 0, n = 0, e = 0, s = 0; - - /* snap to other windows */ - for(i = 0; i <= snapw && !(w && e); i++) { - - for(j = 0; j < num && !(w && e); j++) { - - /* check west neighbors leftwards */ - if(!w) { - if(r->x - i == (rects[j].x + rects[j].width)) { - /* - * west edge of neighbor found, check - * vert match - */ - w = check_vert_match(r, &rects[j]); - if(w) - r->x = rects[j].x + rects[j].width; - } - } - /* check west neighbors rightwards */ - if(!w) { - if(r->x + i == (rects[j].x + rects[j].width)) { - /* - * west edge of neighbor found, check - * vert match - */ - w = check_vert_match(r, &rects[j]); - if(w) - r->x = rects[j].x + rects[j].width; - } - } - /* check east neighbors leftwards */ - if(!e) { - if(r->x + r->width - i == rects[j].x) { - /* - * east edge of neighbor found, check - * vert match - */ - e = check_vert_match(r, &rects[j]); - if(e) - r->x = rects[j].x - r->width; - } - } - /* check east neighbors rightwards */ - if(!e) { - if(r->x + r->width + i == rects[j].x) { - /* - * east edge of neighbor found, check - * vert match - */ - e = check_vert_match(r, &rects[j]); - if(e) - r->x = rects[j].x - r->width; - } - } - } - - /* snap to west screen border */ - if(!w && (r->x - i == rect.x)) { - w = 1; - r->x = rect.x; - } - /* snap to west screen border */ - if(!w && (r->x + i == rect.x)) { - w = 1; - r->x = rect.x; - } - /* snap to east screen border */ - if(!e && (r->x + r->width - i == rect.width)) { - e = 1; - r->x = rect.x + rect.width - r->width; - } - if(!e && (r->x + r->width + i == rect.width)) { - e = 1; - r->x = rect.x + rect.width - r->width; - } - } - - for(i = 0; i <= snaph && !(n && s); i++) { - - for(j = 0; j < num && !(n && s); j++) { - /* check north neighbors upwards */ - if(!n) { - if(r->y - i == (rects[j].y + rects[j].height)) { - /* - * north edge of neighbor found, - * check horiz match - */ - n = check_horiz_match(r, &rects[j]); - if(n) - r->y = rects[j].y + rects[j].height; - } - } - /* check north neighbors downwards */ - if(!n) { - if(r->y + i == (rects[j].y + rects[j].height)) { - /* - * north edge of neighbor found, - * check horiz match - */ - n = check_horiz_match(r, &rects[j]); - if(n) - r->y = rects[j].y + rects[j].height; - } - } - /* check south neighbors upwards */ - if(!s) { - if(r->y + r->height - i == rects[j].y) { - /* - * south edge of neighbor found, - * check horiz match - */ - s = check_horiz_match(r, &rects[j]); - if(s) - r->y = rects[j].y - r->height; - } - } - /* check south neighbors downwards */ - if(!s) { - if(r->y + r->height + i == rects[j].y) { - /* - * south edge of neighbor found, - * check horiz match - */ - s = check_horiz_match(r, &rects[j]); - if(s) - r->y = rects[j].y - r->height; - } - } - } - - /* snap to north screen border */ - if(!n && (r->y - i == rect.y)) { - n = 1; - r->y = rect.y; - } - if(!n && (r->y + i == rect.y)) { - n = 1; - r->y = rect.y; - } - /* snap to south screen border */ - if(!s && (r->y + r->height - i == rect.height)) { - s = 1; - r->y = rect.y + rect.height - r->height; - } - if(!s && (r->y + r->height + i == rect.height)) { - s = 1; - r->y = rect.y + rect.height - r->height; - } - } + int i, j, w = 0, n = 0, e = 0, s = 0; + + /* snap to other windows */ + for(i = 0; i <= snapw && !(w && e); i++) { + for(j = 0; j < num && !(w && e); j++) { + /* check west neighbors leftwards */ + if(!w) { + if(r->x - i == (rects[j].x + rects[j].width)) { + /* + * west edge of neighbor found, check + * vert match + */ + w = check_vert_match(r, &rects[j]); + if(w) + r->x = rects[j].x + rects[j].width; + } + } + /* check west neighbors rightwards */ + if(!w) { + if(r->x + i == (rects[j].x + rects[j].width)) { + /* + * west edge of neighbor found, check + * vert match + */ + w = check_vert_match(r, &rects[j]); + if(w) + r->x = rects[j].x + rects[j].width; + } + } + /* check east neighbors leftwards */ + if(!e) { + if(r->x + r->width - i == rects[j].x) { + /* + * east edge of neighbor found, check + * vert match + */ + e = check_vert_match(r, &rects[j]); + if(e) + r->x = rects[j].x - r->width; + } + } + /* check east neighbors rightwards */ + if(!e) { + if(r->x + r->width + i == rects[j].x) { + /* + * east edge of neighbor found, check + * vert match + */ + e = check_vert_match(r, &rects[j]); + if(e) + r->x = rects[j].x - r->width; + } + } + } + + /* snap to west screen border */ + if(!w && (r->x - i == rect.x)) { + w = 1; + r->x = rect.x; + } + /* snap to west screen border */ + if(!w && (r->x + i == rect.x)) { + w = 1; + r->x = rect.x; + } + /* snap to east screen border */ + if(!e && (r->x + r->width - i == rect.width)) { + e = 1; + r->x = rect.x + rect.width - r->width; + } + if(!e && (r->x + r->width + i == rect.width)) { + e = 1; + r->x = rect.x + rect.width - r->width; + } + } + + for(i = 0; i <= snaph && !(n && s); i++) { + for(j = 0; j < num && !(n && s); j++) { + /* check north neighbors upwards */ + if(!n) { + if(r->y - i == (rects[j].y + rects[j].height)) { + /* + * north edge of neighbor found, + * check horiz match + */ + n = check_horiz_match(r, &rects[j]); + if(n) + r->y = rects[j].y + rects[j].height; + } + } + /* check north neighbors downwards */ + if(!n) { + if(r->y + i == (rects[j].y + rects[j].height)) { + /* + * north edge of neighbor found, + * check horiz match + */ + n = check_horiz_match(r, &rects[j]); + if(n) + r->y = rects[j].y + rects[j].height; + } + } + /* check south neighbors upwards */ + if(!s) { + if(r->y + r->height - i == rects[j].y) { + /* + * south edge of neighbor found, + * check horiz match + */ + s = check_horiz_match(r, &rects[j]); + if(s) + r->y = rects[j].y - r->height; + } + } + /* check south neighbors downwards */ + if(!s) { + if(r->y + r->height + i == rects[j].y) { + /* + * south edge of neighbor found, + * check horiz match + */ + s = check_horiz_match(r, &rects[j]); + if(s) + r->y = rects[j].y - r->height; + } + } + } + + /* snap to north screen border */ + if(!n && (r->y - i == rect.y)) { + n = 1; + r->y = rect.y; + } + if(!n && (r->y + i == rect.y)) { + n = 1; + r->y = rect.y; + } + /* snap to south screen border */ + if(!s && (r->y + r->height - i == rect.height)) { + s = 1; + r->y = rect.y + rect.height - r->height; + } + if(!s && (r->y + r->height + i == rect.height)) { + s = 1; + r->y = rect.y + rect.height - r->height; + } + } } static void draw_pseudo_border(XRectangle * r) { - XRectangle pseudo = *r; - - pseudo.x += 2; - pseudo.y += 2; - pseudo.width -= 4; - pseudo.height -= 4; - XDrawRectangles(dpy, root, xorgc, &pseudo, 1); - XSync(dpy, False); + XRectangle pseudo = *r; + + pseudo.x += 2; + pseudo.y += 2; + pseudo.width -= 4; + pseudo.height -= 4; + XDrawRectangles(dpy, root, xorgc, &pseudo, 1); + XSync(dpy, False); } - void mouse_move(Client *c) { - int px = 0, py = 0, wex, wey, ex, ey, first = 1, i; - Window dummy; - XEvent ev; - /* borders */ - int snapw = rect.width * def.snap / 1000; - int snaph = rect.height * def.snap / 1000; - unsigned int num; - unsigned int dmask; - XRectangle *rects = rectangles(c->frame[c->sel]->area->view, + int px = 0, py = 0, wex, wey, ex, ey, first = 1, i; + Window dummy; + XEvent ev; + /* borders */ + int snapw = rect.width * def.snap / 1000; + int snaph = rect.height * def.snap / 1000; + unsigned int num; + unsigned int dmask; + XRectangle *rects = rectangles(c->frame[c->sel]->area->view, area2index(c->frame[c->sel]->area) == 0, &num); - XRectangle frect = c->frame[c->sel]->rect; - XPoint pt; - - XQueryPointer(dpy, c->framewin, &dummy, &dummy, &i, &i, &wex, &wey, &dmask); - XTranslateCoordinates(dpy, c->framewin, root, wex, wey, &ex, &ey, &dummy); - pt.x = ex; - pt.y = ey; - XSync(dpy, False); - XGrabServer(dpy); - while(XGrabPointer - (dpy, root, False, ButtonMotionMask | ButtonReleaseMask, - GrabModeAsync, GrabModeAsync, None, cursor[CurMove], - CurrentTime) != GrabSuccess) - usleep(1000); - - for(;;) { - while(!XCheckMaskEvent - (dpy, ButtonReleaseMask | ButtonMotionMask, &ev)) { - usleep(1000); - continue; - } - - switch (ev.type) { - case ButtonRelease: - if(!first) { - draw_pseudo_border(&frect); + XRectangle frect = c->frame[c->sel]->rect; + XPoint pt; + + XQueryPointer(dpy, c->framewin, &dummy, &dummy, &i, &i, &wex, &wey, &dmask); + XTranslateCoordinates(dpy, c->framewin, root, wex, wey, &ex, &ey, &dummy); + pt.x = ex; + pt.y = ey; + XSync(dpy, False); + XGrabServer(dpy); + while(XGrabPointer(dpy, root, False, ButtonMotionMask | ButtonReleaseMask, + GrabModeAsync, GrabModeAsync, None, cursor[CurMove], + CurrentTime) != GrabSuccess) + usleep(1000); + + for(;;) { + while(!XCheckMaskEvent(dpy, ButtonReleaseMask | ButtonMotionMask, &ev)) { + usleep(1000); + continue; + } + + switch (ev.type) { + case ButtonRelease: + if(!first) { + draw_pseudo_border(&frect); if(area2index(c->frame[c->sel]->area)) resize_area(c, &frect, &pt); else resize_client(c, &frect, False); - } - free(rects); - XUngrabPointer(dpy, CurrentTime /* ev.xbutton.time */ ); - XUngrabServer(dpy); - XSync(dpy, False); - return; - break; - case MotionNotify: - pt.x = ev.xmotion.x; - pt.y = ev.xmotion.y; - XTranslateCoordinates(dpy, c->framewin, root, ev.xmotion.x, - ev.xmotion.y, &px, &py, &dummy); - if(first) - first = 0; - else - draw_pseudo_border(&frect); - frect.x = px - ex; - frect.y = py - ey; - snap_move(&frect, rects, num, snapw, snaph); - draw_pseudo_border(&frect); - break; - } - } + } + free(rects); + XUngrabPointer(dpy, CurrentTime /* ev.xbutton.time */ ); + XUngrabServer(dpy); + XSync(dpy, False); + return; + break; + case MotionNotify: + pt.x = ev.xmotion.x; + pt.y = ev.xmotion.y; + XTranslateCoordinates(dpy, c->framewin, root, ev.xmotion.x, + ev.xmotion.y, &px, &py, &dummy); + if(first) + first = 0; + else + draw_pseudo_border(&frect); + frect.x = px - ex; + frect.y = py - ey; + snap_move(&frect, rects, num, snapw, snaph); + draw_pseudo_border(&frect); + break; + } + } } static void snap_resize(XRectangle * r, XRectangle * o, Align align, - XRectangle * rects, unsigned int num, int px, int ox, int py, - int oy, int snapw, int snaph) + XRectangle * rects, unsigned int num, int px, int ox, int py, + int oy, int snapw, int snaph) { - int i, j, pend = 0; - int w, h; - - /* x */ - switch (align) { - case NEAST: - case EAST: - case SEAST: - w = px - r->x + (o->width - ox); - if(w < 10) - break; - r->width = w; - if(w <= snapw) - break; - /* snap to border */ - for(i = 0; !pend && (i < snapw); i++) { - if(r->x + r->width - i == rect.x + rect.width) { - r->width -= i; - break; - } - if(r->x + r->width + i == rect.x + rect.width) { - r->width += i; - break; - } - for(j = 0; j < num; j++) { - if(r->x + r->width - i == rects[j].x) { - pend = check_vert_match(r, &rects[j]); - if(pend) { - r->width -= i; - break; - } - } - if(r->x + r->width + i == rects[j].x) { - pend = check_vert_match(r, &rects[j]); - if(pend) { - r->width += i; - break; - } - } - } - } - break; - case NWEST: - case WEST: - case SWEST: - w = r->width + r->x - px + ox; - if(w < 10) - break; - r->width = w; - r->x = px - ox; - if(w <= snapw) - break; - /* snap to border */ - for(i = 0; !pend && (i < snapw); i++) { - if(r->x - i == rect.x) { - r->x -= i; - r->width += i; - break; - } - if(r->x + i == rect.x) { - r->x += i; - r->width -= i; - break; - } - for(j = 0; j < num; j++) { - if(r->x - i == rects[j].x + rects[j].width) { - pend = check_vert_match(r, &rects[j]); - if(pend) { - r->x -= i; - r->width += i; - break; - } - } - if(r->x + i == rects[j].x + rects[j].width) { - pend = check_vert_match(r, &rects[j]); - if(pend) { - r->x += i; - r->width -= i; - break; - } - } - } - } - break; - default: - break; - } - - /* y */ - pend = 0; - switch (align) { - case SWEST: - case SOUTH: - case SEAST: - h = py - r->y + (o->height - oy); - if(h < 10) - break; - r->height = h; - if(h <= snaph) - break; - /* snap to border */ - for(i = 0; !pend && (i < snaph); i++) { - if(r->y + r->height - i == rect.y + rect.height) { - r->height -= i; - break; - } - if(r->y + r->height + i == rect.y + rect.height) { - r->height += i; - break; - } - for(j = 0; j < num; j++) { - if(r->y + r->height - i == rects[j].y) { - pend = check_horiz_match(r, &rects[j]); - if(pend) { - r->height -= i; - break; - } - } - if(r->y + r->height + i == rects[j].y) { - pend = check_horiz_match(r, &rects[j]); - if(pend) { - r->height += i; - break; - } - } - } - } - break; - case NWEST: - case NORTH: - case NEAST: - h = r->height + r->y - py + oy; - if(h < 10) - break; - r->height = h; - r->y = py - oy; - if(h <= snaph) - break; - /* snap to border */ - for(i = 0; !pend && (i < snaph); i++) { - if(r->y - i == rect.y) { - r->y -= i; - r->height += i; - break; - } - if(r->y + i == rect.y) { - r->y += i; - r->height -= i; - break; - } - for(j = 0; j < num; j++) { - if(r->y - i == rects[j].y + rects[j].height) { - pend = check_horiz_match(r, &rects[j]); - if(pend) { - r->y -= i; - r->height += i; - break; - } - } - if(r->y + i == rects[j].y + rects[j].height) { - pend = check_horiz_match(r, &rects[j]); - if(pend) { - r->y += i; - r->height -= i; - break; - } - } - } - } - break; - default: - break; - } + int i, j, pend = 0; + int w, h; + + /* x */ + switch (align) { + case NEAST: + case EAST: + case SEAST: + w = px - r->x + (o->width - ox); + if(w < 10) + break; + r->width = w; + if(w <= snapw) + break; + /* snap to border */ + for(i = 0; !pend && (i < snapw); i++) { + if(r->x + r->width - i == rect.x + rect.width) { + r->width -= i; + break; + } + if(r->x + r->width + i == rect.x + rect.width) { + r->width += i; + break; + } + for(j = 0; j < num; j++) { + if(r->x + r->width - i == rects[j].x) { + pend = check_vert_match(r, &rects[j]); + if(pend) { + r->width -= i; + break; + } + } + if(r->x + r->width + i == rects[j].x) { + pend = check_vert_match(r, &rects[j]); + if(pend) { + r->width += i; + break; + } + } + } + } + break; + case NWEST: + case WEST: + case SWEST: + w = r->width + r->x - px + ox; + if(w < 10) + break; + r->width = w; + r->x = px - ox; + if(w <= snapw) + break; + /* snap to border */ + for(i = 0; !pend && (i < snapw); i++) { + if(r->x - i == rect.x) { + r->x -= i; + r->width += i; + break; + } + if(r->x + i == rect.x) { + r->x += i; + r->width -= i; + break; + } + for(j = 0; j < num; j++) { + if(r->x - i == rects[j].x + rects[j].width) { + pend = check_vert_match(r, &rects[j]); + if(pend) { + r->x -= i; + r->width += i; + break; + } + } + if(r->x + i == rects[j].x + rects[j].width) { + pend = check_vert_match(r, &rects[j]); + if(pend) { + r->x += i; + r->width -= i; + break; + } + } + } + } + break; + default: + break; + } + + /* y */ + pend = 0; + switch (align) { + case SWEST: + case SOUTH: + case SEAST: + h = py - r->y + (o->height - oy); + if(h < 10) + break; + r->height = h; + if(h <= snaph) + break; + /* snap to border */ + for(i = 0; !pend && (i < snaph); i++) { + if(r->y + r->height - i == rect.y + rect.height) { + r->height -= i; + break; + } + if(r->y + r->height + i == rect.y + rect.height) { + r->height += i; + break; + } + for(j = 0; j < num; j++) { + if(r->y + r->height - i == rects[j].y) { + pend = check_horiz_match(r, &rects[j]); + if(pend) { + r->height -= i; + break; + } + } + if(r->y + r->height + i == rects[j].y) { + pend = check_horiz_match(r, &rects[j]); + if(pend) { + r->height += i; + break; + } + } + } + } + break; + case NWEST: + case NORTH: + case NEAST: + h = r->height + r->y - py + oy; + if(h < 10) + break; + r->height = h; + r->y = py - oy; + if(h <= snaph) + break; + /* snap to border */ + for(i = 0; !pend && (i < snaph); i++) { + if(r->y - i == rect.y) { + r->y -= i; + r->height += i; + break; + } + if(r->y + i == rect.y) { + r->y += i; + r->height -= i; + break; + } + for(j = 0; j < num; j++) { + if(r->y - i == rects[j].y + rects[j].height) { + pend = check_horiz_match(r, &rects[j]); + if(pend) { + r->y -= i; + r->height += i; + break; + } + } + if(r->y + i == rects[j].y + rects[j].height) { + pend = check_horiz_match(r, &rects[j]); + if(pend) { + r->y += i; + r->height -= i; + break; + } + } + } + } + break; + default: + break; + } } - void mouse_resize(Client *c, Align align) { - int px = 0, py = 0, i, ox, oy, first = 1; - Window dummy; - XEvent ev; - /* borders */ - int snapw = rect.width * def.snap / 1000; - int snaph = rect.height * def.snap / 1000; - unsigned int dmask; - unsigned int num; - XRectangle *rects = rectangles(c->frame[c->sel]->area->view, + int px = 0, py = 0, i, ox, oy, first = 1; + Window dummy; + XEvent ev; + /* borders */ + int snapw = rect.width * def.snap / 1000; + int snaph = rect.height * def.snap / 1000; + unsigned int dmask; + unsigned int num; + XRectangle *rects = rectangles(c->frame[c->sel]->area->view, area2index(c->frame[c->sel]->area) == 0, &num); - XRectangle frect = c->frame[c->sel]->rect; - XRectangle origin = frect; - - XQueryPointer(dpy, c->framewin, &dummy, &dummy, &i, &i, &ox, &oy, &dmask); - XSync(dpy, False); - XGrabServer(dpy); - while(XGrabPointer - (dpy, c->framewin, False, ButtonMotionMask | ButtonReleaseMask, - GrabModeAsync, GrabModeAsync, None, cursor[CurResize], - CurrentTime) != GrabSuccess) - usleep(1000); - - for(;;) { - while(!XCheckMaskEvent - (dpy, ButtonReleaseMask | ButtonMotionMask, &ev)) { - usleep(1000); - continue; - } - - switch (ev.type) { - case ButtonRelease: - if(!first) { - XPoint pt; - draw_pseudo_border(&frect); - pt.x = px; - pt.y = py; + XRectangle frect = c->frame[c->sel]->rect; + XRectangle origin = frect; + + XQueryPointer(dpy, c->framewin, &dummy, &dummy, &i, &i, &ox, &oy, &dmask); + XSync(dpy, False); + XGrabServer(dpy); + while(XGrabPointer(dpy, c->framewin, False, ButtonMotionMask | ButtonReleaseMask, + GrabModeAsync, GrabModeAsync, None, cursor[CurResize], + CurrentTime) != GrabSuccess) + usleep(1000); + + for(;;) { + while(!XCheckMaskEvent(dpy, ButtonReleaseMask | ButtonMotionMask, &ev)) { + usleep(1000); + continue; + } + + switch (ev.type) { + case ButtonRelease: + if(!first) { + XPoint pt; + draw_pseudo_border(&frect); + pt.x = px; + pt.y = py; if(area2index(c->frame[c->sel]->area)) resize_area(c, &frect, &pt); else resize_client(c, &frect, False); - } - XUngrabPointer(dpy, CurrentTime /* ev.xbutton.time */ ); - XUngrabServer(dpy); - XSync(dpy, False); - return; - break; - case MotionNotify: - XTranslateCoordinates(dpy, c->framewin, root, ev.xmotion.x, - ev.xmotion.y, &px, &py, &dummy); - - if(first) - first = 0; - else - draw_pseudo_border(&frect); - - snap_resize(&frect, &origin, align, rects, num, px, - ox, py, oy, snapw, snaph); - draw_pseudo_border(&frect); - break; - } - } + } + XUngrabPointer(dpy, CurrentTime /* ev.xbutton.time */ ); + XUngrabServer(dpy); + XSync(dpy, False); + return; + break; + case MotionNotify: + XTranslateCoordinates(dpy, c->framewin, root, ev.xmotion.x, + ev.xmotion.y, &px, &py, &dummy); + if(first) + first = 0; + else + draw_pseudo_border(&frect); + + snap_resize(&frect, &origin, align, rects, num, px, + ox, py, oy, snapw, snaph); + draw_pseudo_border(&frect); + break; + } + } } void grab_mouse(Window w, unsigned long mod, unsigned int button) { - XGrabButton(dpy, button, mod, w, False, - ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None); - if((mod != AnyModifier) && num_lock_mask) { - XGrabButton(dpy, button, mod | num_lock_mask, w, False, - ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None); - XGrabButton(dpy, button, mod | num_lock_mask | LockMask, w, - False, ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None); - } + XGrabButton(dpy, button, mod, w, False, ButtonPressMask, + GrabModeAsync, GrabModeAsync, None, None); + if((mod != AnyModifier) && num_lock_mask) { + XGrabButton(dpy, button, mod | num_lock_mask, w, False, ButtonPressMask, + GrabModeAsync, GrabModeAsync, None, None); + XGrabButton(dpy, button, mod | num_lock_mask | LockMask, w, False, + ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None); + } } void ungrab_mouse(Window w, unsigned long mod, unsigned int button) { - XUngrabButton(dpy, button, mod, w); - if(mod != AnyModifier && num_lock_mask) { - XUngrabButton(dpy, button, mod | num_lock_mask, w); - XUngrabButton(dpy, button, mod | num_lock_mask | LockMask, w); - } + XUngrabButton(dpy, button, mod, w); + if(mod != AnyModifier && num_lock_mask) { + XUngrabButton(dpy, button, mod | num_lock_mask, w); + XUngrabButton(dpy, button, mod | num_lock_mask | LockMask, w); + } } diff --git a/cmd/wm/wm.h b/cmd/wm/wm.h @@ -35,7 +35,7 @@ enum { /* 8-bit qid.path.type */ enum { - FsDroot, + FsDroot, FsDdef, FsDview, FsDarea, @@ -43,13 +43,13 @@ enum { FsDclient, FsDGclient, FsDbar, - FsDlabel, + FsDlabel, FsFexpand, - FsFdata, - FsFcolors, - FsFfont, - FsFselcolors, - FsFnormcolors, + FsFdata, + FsFcolors, + FsFfont, + FsFselcolors, + FsFnormcolors, FsFkeys, FsFborder, FsFsnap, @@ -65,15 +65,15 @@ enum { FsFmode }; -#define MAX_TAGS 8 -#define MAX_TAGLEN 32 +#define MAX_TAGS 8 +#define MAX_TAGLEN 32 -#define DEF_BORDER 3 -#define DEF_SNAP 20 +#define DEF_BORDER 3 +#define DEF_SNAP 20 -#define ROOT_MASK SubstructureRedirectMask -#define CLIENT_MASK (StructureNotifyMask | PropertyChangeMask) -#define WM_PROTOCOL_DELWIN 1 +#define ROOT_MASK SubstructureRedirectMask +#define CLIENT_MASK (StructureNotifyMask | PropertyChangeMask) +#define WM_PROTOCOL_DELWIN 1 typedef struct View View; typedef struct Area Area; @@ -93,7 +93,7 @@ struct View { struct Area { unsigned short id; - Frame **frame; + Frame **frame; View *view; unsigned int framesz; unsigned int sel; @@ -115,15 +115,15 @@ struct Client { char tag[MAX_TAGS][MAX_TAGLEN]; unsigned int ntag; char classinst[256]; - int proto; - unsigned int border; - Bool destroyed; - Window win; - Window trans; - XRectangle rect; - XSizeHints size; + int proto; + unsigned int border; + Bool destroyed; + Window win; + Window trans; + XRectangle rect; + XSizeHints size; Window framewin; - GC gc; + GC gc; Frame **frame; unsigned int framesz; unsigned int sel; @@ -134,16 +134,16 @@ struct Client { typedef struct Key Key; struct Key { unsigned short id; - char name[128]; - unsigned long mod; - KeyCode key; - Key *next; + char name[128]; + unsigned long mod; + KeyCode key; + Key *next; }; typedef struct { - char name[256]; + char name[256]; unsigned short id; - char data[256]; + char data[256]; char colstr[24]; Color color; XRectangle rect; @@ -263,7 +263,7 @@ Client *win2clientframe(Window w); /* fs.c */ unsigned long long mkqpath(unsigned char type, unsigned short pg, - unsigned short area, unsigned short cl); + unsigned short area, unsigned short cl); void write_event(char *event, Bool enqueue); void new_ixp_conn(IXPConn *c); @@ -286,7 +286,7 @@ void match_tags(Client *c); /* tag.c */ unsigned int str2tags(char tags[MAX_TAGS][MAX_TAGLEN], const char *stags); void tags2str(char *stags, unsigned int stagsz, - char tags[MAX_TAGS][MAX_TAGLEN], unsigned int ntags); + char tags[MAX_TAGS][MAX_TAGLEN], unsigned int ntags); Bool istag(char **tags, unsigned int ntags, char *tag); void update_tags();