diff options
Diffstat (limited to 'src/sxwm.c')
| -rw-r--r-- | src/sxwm.c | 423 |
1 files changed, 310 insertions, 113 deletions
@@ -52,7 +52,6 @@ void hdl_config_ntf(XEvent *xev); void hdl_config_req(XEvent *xev); void hdl_dummy(XEvent *xev); void hdl_destroy_ntf(XEvent *xev); -void hdl_enter(XEvent *xev); void hdl_keypress(XEvent *xev); void hdl_map_req(XEvent *xev); void hdl_motion(XEvent *xev); @@ -70,6 +69,8 @@ int other_wm_err(Display *dpy, XErrorEvent *ee); /* void reload_config(void); */ /* void resize_master_add(void); */ /* void resize_master_sub(void); */ +/* void resize_stack_add(void); */ +/* void resize_stack_sub(void); */ void run(void); void scan_existing_windows(void); void send_wm_take_focus(Window w); @@ -86,6 +87,7 @@ void update_borders(void); void update_monitors(void); void update_net_client_list(void); void update_struts(void); +void warp_cursor(Client *c); int xerr(Display *dpy, XErrorEvent *ee); void xev_case(XEvent *xev); #include "config.h" @@ -122,6 +124,7 @@ Monitor *mons = NULL; int monsn = 0; Bool global_floating = False; Bool in_ws_switch = False; +Bool running = False; long last_motion_time = 0; int scr_width; @@ -154,8 +157,9 @@ Client *add_client(Window w, int ws) } else { Client *tail = workspaces[ws]; - while (tail->next) + while (tail->next) { tail = tail->next; + } tail->next = c; } @@ -194,6 +198,7 @@ Client *add_client(Window w, int ws) c->floating = False; c->fullscreen = False; c->mapped = True; + c->custom_stack_height = 0; if (global_floating) { c->floating = True; @@ -209,30 +214,36 @@ Client *add_client(Window w, int ws) void change_workspace(int ws) { - if (ws >= NUM_WORKSPACES || ws == current_ws) + if (ws >= NUM_WORKSPACES || ws == current_ws) { return; + } in_ws_switch = True; XGrabServer(dpy); /* unmap those still marked mapped */ for (Client *c = workspaces[current_ws]; c; c = c->next) { - if (c->mapped) + if (c->mapped) { XUnmapWindow(dpy, c->win); + } } current_ws = ws; /* map those still marked mapped */ for (Client *c = workspaces[current_ws]; c; c = c->next) { - if (c->mapped) + if (c->mapped) { XMapWindow(dpy, c->win); + } } tile(); if (workspaces[current_ws]) { focused = workspaces[current_ws]; XSetInputFocus(dpy, focused->win, RevertToPointerRoot, CurrentTime); + if (user_config.warp_cursor) { + warp_cursor(focused); + } } long cd = current_ws; @@ -258,7 +269,7 @@ void close_focused(void) Atom *protos; int n; if (XGetWMProtocols(dpy, focused->win, &protos, &n) && protos) { - for (int i = 0; i < n; i++) + for (int i = 0; i < n; i++) { if (protos[i] == atom_wm_delete) { XEvent ev = {.xclient = {.type = ClientMessage, .window = focused->win, @@ -270,6 +281,7 @@ void close_focused(void) XFree(protos); return; } + } XUnmapWindow(dpy, focused->win); XFree(protos); } @@ -295,6 +307,9 @@ void focus_next(void) focused = (focused->next ? focused->next : workspaces[current_ws]); XSetInputFocus(dpy, focused->win, RevertToPointerRoot, CurrentTime); XRaiseWindow(dpy, focused->win); + if (user_config.warp_cursor) { + warp_cursor(focused); + } update_borders(); } @@ -311,8 +326,9 @@ void focus_prev(void) } if (!prev) { - while (p->next) + while (p->next) { p = p->next; + } focused = p; } else { @@ -321,6 +337,9 @@ void focus_prev(void) XSetInputFocus(dpy, focused->win, RevertToPointerRoot, CurrentTime); XRaiseWindow(dpy, focused->win); + if (user_config.warp_cursor) { + warp_cursor(focused); + } update_borders(); } @@ -328,8 +347,9 @@ int get_monitor_for(Client *c) { int cx = c->x + c->w / 2, cy = c->y + c->h / 2; for (int i = 0; i < monsn; i++) { - if (cx >= (int)mons[i].x && cx < mons[i].x + mons[i].w && cy >= (int)mons[i].y && cy < mons[i].y + mons[i].h) + if (cx >= (int)mons[i].x && cx < mons[i].x + mons[i].w && cy >= (int)mons[i].y && cy < mons[i].y + mons[i].h) { return i; + } } return 0; } @@ -350,15 +370,18 @@ void grab_keys(void) Binding *b = &user_config.binds[i]; if ((b->type == TYPE_CWKSP && b->mods != user_config.modkey) || - (b->type == TYPE_MWKSP && b->mods != (user_config.modkey | ShiftMask))) + (b->type == TYPE_MWKSP && b->mods != (user_config.modkey | ShiftMask))) { continue; + } KeyCode kc = XKeysymToKeycode(dpy, b->keysym); - if (!kc) + if (!kc) { continue; + } - for (size_t g = 0; g < sizeof guards / sizeof *guards; g++) + for (size_t g = 0; g < sizeof guards / sizeof *guards; g++) { XGrabKey(dpy, kc, b->mods | guards[g], root, True, GrabModeAsync, GrabModeAsync); + } } } @@ -506,11 +529,13 @@ void hdl_config_req(XEvent *xev) XConfigureRequestEvent *e = &xev->xconfigurerequest; Client *c = NULL; - for (int ws = 0; ws < NUM_WORKSPACES && !c; ws++) - for (c = workspaces[ws]; c; c = c->next) + for (int ws = 0; ws < NUM_WORKSPACES && !c; ws++) { + for (c = workspaces[ws]; c; c = c->next) { if (c->win == e->window) { break; } + } + } if (!c || c->floating || c->fullscreen) { /* allow client to configure itself */ @@ -585,21 +610,6 @@ void hdl_destroy_ntf(XEvent *xev) } } -void hdl_enter(XEvent *xev) -{ - Window w = xev->xcrossing.window; - - Client *head = workspaces[current_ws]; - for (Client *c = head; c; c = c->next) { - if (c->win == w) { - focused = c; - XSetInputFocus(dpy, w, RevertToPointerRoot, CurrentTime); - update_borders(); - break; - } - } -} - void hdl_keypress(XEvent *xev) { KeySym ks = XkbKeycodeToKeysym(dpy, xev->xkey.keycode, 0, 0); @@ -614,8 +624,9 @@ void hdl_keypress(XEvent *xev) break; case TYPE_FUNC: - if (b->action.fn) + if (b->action.fn) { b->action.fn(); + } break; case TYPE_CWKSP: change_workspace(b->action.ws); @@ -640,10 +651,12 @@ void swap_clients(Client *a, Client *b) Client **head = &workspaces[current_ws]; Client **pa = head, **pb = head; - while (*pa && *pa != a) + while (*pa && *pa != a) { pa = &(*pa)->next; - while (*pb && *pb != b) + } + while (*pb && *pb != b) { pb = &(*pb)->next; + } if (!*pa || !*pb) { return; @@ -726,12 +739,14 @@ void hdl_map_req(XEvent *xev) } Client *c = add_client(w, current_ws); - if (!c) + if (!c) { return; + } Window tr; - if (!should_float && XGetTransientForHint(dpy, w, &tr)) + if (!should_float && XGetTransientForHint(dpy, w, &tr)) { should_float = True; + } XSizeHints sh; long sup; if (!should_float && XGetWMNormalHints(dpy, w, &sh, &sup) && (sh.flags & PMinSize) && (sh.flags & PMaxSize) && @@ -760,22 +775,27 @@ void hdl_map_req(XEvent *xev) /* map & borders */ update_net_client_list(); - if (!global_floating && !c->floating) + if (!global_floating && !c->floating) { tile(); - else if (c->floating) + } + else if (c->floating) { XRaiseWindow(dpy, w); + } + XMapWindow(dpy, w); + for (Client *c = workspaces[current_ws]; c; c = c->next) { + if (c->win == w) { + c->mapped = True; + } + } if (user_config.new_win_focus) { focused = c; XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime); send_wm_take_focus(c->win); + if (user_config.warp_cursor) { + warp_cursor(c); + } } - - XMapWindow(dpy, w); - for (Client *c = workspaces[current_ws]; c; c = c->next) - if (c->win == w) - c->mapped = True; - update_borders(); } @@ -913,8 +933,9 @@ void update_struts(void) Window root_ret, parent_ret, *children; unsigned int nchildren; - if (!XQueryTree(dpy, root, &root_ret, &parent_ret, &children, &nchildren)) + if (!XQueryTree(dpy, root, &root_ret, &parent_ret, &children, &nchildren)) { return; + } for (unsigned int i = 0; i < nchildren; i++) { Window w = children[i]; @@ -926,8 +947,9 @@ void update_struts(void) if (XGetWindowProperty(dpy, w, atom_wm_window_type, 0, 4, False, XA_ATOM, &actual_type, &actual_format, &nitems, &bytes_after, (unsigned char **)&types) != Success || - !types) + !types) { continue; + } Bool is_dock = False; for (unsigned long j = 0; j < nitems; j++) { @@ -937,8 +959,9 @@ void update_struts(void) } } XFree(types); - if (!is_dock) + if (!is_dock) { continue; + } long *str = NULL; Atom actual; @@ -981,14 +1004,17 @@ void init_defaults(void) default_config.border_foc_col = parse_col("#c0cbff"); default_config.border_ufoc_col = parse_col("#555555"); default_config.border_swap_col = parse_col("#fff4c0"); - for (int i = 0; i < MAX_MONITORS; i++) + for (int i = 0; i < MAX_MONITORS; i++) { default_config.master_width[i] = 50 / 100.0f; + } default_config.motion_throttle = 60; default_config.resize_master_amt = 5; + default_config.resize_stack_amt = 20; default_config.snap_distance = 5; default_config.bindsn = 0; default_config.new_win_focus = True; + default_config.warp_cursor = True; for (unsigned long i = 0; i < LENGTH(binds); i++) { default_config.binds[i].mods = binds[i].mods; @@ -1006,7 +1032,10 @@ void move_master_next(void) if (!workspaces[current_ws] || !workspaces[current_ws]->next) { return; } + Client *first = workspaces[current_ws]; + Client *old_focused = focused; + workspaces[current_ws] = first->next; first->next = NULL; @@ -1017,6 +1046,12 @@ void move_master_next(void) tail->next = first; tile(); + if (user_config.warp_cursor && old_focused) { + warp_cursor(old_focused); + } + if (old_focused) { + send_wm_take_focus(old_focused->win); + } update_borders(); } @@ -1025,15 +1060,29 @@ void move_master_prev(void) if (!workspaces[current_ws] || !workspaces[current_ws]->next) { return; } + Client *prev = NULL, *cur = workspaces[current_ws]; + Client *old_focused = focused; + while (cur->next) { prev = cur; cur = cur->next; } - prev->next = NULL; + + if (prev) { + prev->next = NULL; + } + cur->next = workspaces[current_ws]; workspaces[current_ws] = cur; + tile(); + if (user_config.warp_cursor && old_focused) { + warp_cursor(old_focused); + } + if (old_focused) { + send_wm_take_focus(old_focused->win); + } update_borders(); } @@ -1052,8 +1101,9 @@ void move_to_workspace(int ws) XUnmapWindow(dpy, focused->win); /* remove from current list */ Client **pp = &workspaces[current_ws]; - while (*pp && *pp != focused) + while (*pp && *pp != focused) { pp = &(*pp)->next; + } if (*pp) { *pp = focused->next; } @@ -1121,14 +1171,14 @@ void quit(void) XFreeCursor(dpy, c_move); XFreeCursor(dpy, c_normal); XFreeCursor(dpy, c_resize); - errx(0, "quitting..."); + printf("quitting...\n"); + running = False; } void reload_config(void) { puts("sxwm: reloading config..."); memset(&user_config, 0, sizeof(user_config)); - for (int i = 0; i < user_config.bindsn; i++) { free(user_config.binds[i].action.cmd); user_config.binds[i].action.cmd = NULL; @@ -1184,10 +1234,44 @@ void resize_master_sub(void) update_borders(); } +void resize_stack_add(void) +{ + if (!focused || focused->floating || focused == workspaces[current_ws]) { + return; + } + + int bw2 = 2 * user_config.border_width; + int raw_cur = (focused->custom_stack_height > 0) ? focused->custom_stack_height : (focused->h + bw2); + + int raw_new = raw_cur + user_config.resize_stack_amt; + focused->custom_stack_height = raw_new; + tile(); +} + +void resize_stack_sub(void) +{ + if (!focused || focused->floating || focused == workspaces[current_ws]) { + return; + } + + int bw2 = 2 * user_config.border_width; + int raw_cur = (focused->custom_stack_height > 0) ? focused->custom_stack_height : (focused->h + bw2); + + int raw_new = raw_cur - user_config.resize_stack_amt; + int min_raw = bw2 + 1; + + if (raw_new < min_raw) { + raw_new = min_raw; + } + focused->custom_stack_height = raw_new; + tile(); +} + void run(void) { + running = True; XEvent xev; - for (;;) { + while (running) { XNextEvent(dpy, &xev); xev_case(&xev); } @@ -1310,7 +1394,6 @@ void setup(void) evtable[ConfigureNotify] = hdl_config_ntf; evtable[ConfigureRequest] = hdl_config_req; evtable[DestroyNotify] = hdl_destroy_ntf; - evtable[EnterNotify] = hdl_enter; evtable[KeyPress] = hdl_keypress; evtable[MapRequest] = hdl_map_req; evtable[MotionNotify] = hdl_motion; @@ -1385,13 +1468,8 @@ Bool window_should_float(Window w) break; } - printf("[DEBUG] Checking window class '%s' and instance '%s' against should_float[%d][0] = '%s'\n", - ch.res_class ? ch.res_class : "NULL", ch.res_name ? ch.res_name : "NULL", i, - user_config.should_float[i][0]); - if ((ch.res_class && !strcmp(ch.res_class, user_config.should_float[i][0])) || (ch.res_name && !strcmp(ch.res_name, user_config.should_float[i][0]))) { - printf("[DEBUG] Window should float based on class/instance match\n"); XFree(ch.res_class); XFree(ch.res_name); return True; @@ -1460,95 +1538,201 @@ void spawn(const char **argv) void tile(void) { - update_struts(); /* fills reserve_top, reserve_bottom */ - + update_struts(); Client *head = workspaces[current_ws]; int total = 0; for (Client *c = head; c; c = c->next) { - if (!c->mapped || c->floating || c->fullscreen) - continue; - total++; + if (c->mapped && !c->floating && !c->fullscreen) { + total++; + } } if (total == 1) { for (Client *c = head; c; c = c->next) { - if (!c->floating && c->fullscreen) + if (!c->floating && c->fullscreen) { return; + } } } for (int m = 0; m < monsn; m++) { - int mon_x = mons[m].x; - int mon_y = mons[m].y + reserve_top; - int mon_w = mons[m].w; - int mon_h = mons[m].h - reserve_top - reserve_bottom; + int mon_x = mons[m].x, mon_y = mons[m].y + reserve_top; + int mon_w = mons[m].w, mon_h = mons[m].h - reserve_top - reserve_bottom; - int cnt = 0; + Client *stackers[MAXCLIENTS]; + int N = 0; for (Client *c = head; c; c = c->next) { - if (!c->mapped || c->floating || c->fullscreen || c->mon != m) - continue; - cnt++; + if (c->mapped && !c->floating && !c->fullscreen && c->mon == m) { + stackers[N++] = c; + } } - if (!cnt) + + if (N == 0) { continue; + } - int gx = user_config.gaps; - int gy = user_config.gaps; - int tile_x = mon_x + gx; - int tile_y = mon_y + gy; - int tile_w = mon_w - 2 * gx; - int tile_h = mon_h - 2 * gy; - if (tile_w < 1) - tile_w = 1; - if (tile_h < 1) - tile_h = 1; - - /* master‐stack split */ - float mf = user_config.master_width[m]; - if (mf < MF_MIN) - mf = MF_MIN; - if (mf > MF_MAX) - mf = MF_MAX; - int master_w = (cnt > 1) ? (int)(tile_w * mf) : tile_w; - int stack_w = tile_w - master_w - ((cnt > 1) ? gx : 0); - int stack_h = (cnt > 1) ? (tile_h - (cnt - 1) * gy) / (cnt - 1) : 0; - - int i = 0, sy = tile_y; - for (Client *c = head; c; c = c->next) { - if (!c->mapped || c->floating || c->fullscreen || c->mon != m) - continue; + int gx = user_config.gaps, gy = user_config.gaps; + int tile_x = mon_x + gx, tile_y = mon_y + gy; + int tile_w = MAX(1, mon_w - 2 * gx); + int tile_h = MAX(1, mon_h - 2 * gy); + float mf = CLAMP(user_config.master_width[m], MF_MIN, MF_MAX); + int master_w = (N > 1) ? (int)(tile_w * mf) : tile_w; + int stack_w = (N > 1) ? (tile_w - master_w - gx) : 0; - XWindowChanges wc = {.border_width = user_config.border_width}; + { + Client *c = stackers[0]; int bw2 = 2 * user_config.border_width; + XWindowChanges wc = {.x = tile_x, + .y = tile_y, + .width = MAX(1, master_w - bw2), + .height = MAX(1, tile_h - bw2), + .border_width = user_config.border_width}; + XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc); + c->x = wc.x; + c->y = wc.y; + c->w = wc.width; + c->h = wc.height; + } + + if (N == 1) { + update_borders(); + continue; + } - if (i == 0) { - /* master */ - wc.x = tile_x; - wc.y = tile_y; - wc.width = MAX(1, master_w - bw2); - wc.height = MAX(1, tile_h - bw2); + int num_stack = N - 1; + int idx_focus = -1; + for (int i = 1; i < N; i++) { + if (stackers[i] == focused) { + idx_focus = i; + } + } + + Bool is_fixed[MAXCLIENTS] = {0}; + int bw2 = 2 * user_config.border_width; + for (int i = 1; i < N; i++) { + if (stackers[i]->custom_stack_height > 0) { + is_fixed[i] = True; + } + } + if (idx_focus >= 1 && stackers[idx_focus]->custom_stack_height > 0) { + is_fixed[idx_focus] = True; + } + + int total_fixed_heights = 0; + for (int i = 1; i < N; i++) { + if (!is_fixed[i]) { + continue; + } + int h = stackers[i]->custom_stack_height > 0 ? stackers[i]->custom_stack_height : stackers[i]->h + bw2; + total_fixed_heights += h; + } + + int auto_count = 0; + for (int i = 1; i < N; i++) { + if (!is_fixed[i]) { + auto_count++; + } + } + + int total_vgaps = (num_stack - 1) * gy; + int remaining = tile_h - total_fixed_heights - total_vgaps; + int min_raw = bw2 + 1; + int heights_final[MAXCLIENTS] = {0}; + + if (auto_count > 0) { + if (remaining >= auto_count * min_raw) { + + int auto_h = remaining / auto_count, used = 0, count = 0; + for (int i = 1; i < N; i++) { + if (!is_fixed[i]) { + count++; + heights_final[i] = (count < auto_count) ? auto_h : remaining - used; + used += auto_h; + } + } + for (int i = 1; i < N; i++) { + if (is_fixed[i]) { + heights_final[i] = stackers[i]->custom_stack_height > 0 ? stackers[i]->custom_stack_height + : stackers[i]->h + bw2; + } + } } else { - /* stack */ - wc.x = tile_x + master_w + gx; - wc.y = sy; - int h = (i == cnt - 1) ? (tile_y + tile_h - sy) : stack_h; - wc.width = MAX(1, stack_w - bw2); - wc.height = MAX(1, h - bw2); - sy += h + gy; + for (int i = 1; i < N; i++) { + heights_final[i] = is_fixed[i] + ? (stackers[i]->custom_stack_height > 0 ? stackers[i]->custom_stack_height + : stackers[i]->h + bw2) + : min_raw; + } + } + } + else { + int sum_raw = 0; + for (int i = 1; i < N; i++) { + sum_raw += + stackers[i]->custom_stack_height > 0 ? stackers[i]->custom_stack_height : stackers[i]->h + bw2; + } + int remaining_slack = tile_h - total_vgaps - sum_raw; + for (int i = 1; i < N; i++) { + int base_h = + stackers[i]->custom_stack_height > 0 ? stackers[i]->custom_stack_height : stackers[i]->h + bw2; + + /* only grow the bottom window if it isn’t fixed */ + if (i == N - 1 && remaining_slack > 0 && stackers[i]->custom_stack_height == 0) { + heights_final[i] = base_h + remaining_slack; + } + else { + heights_final[i] = base_h; + } + } + } + + int total_height = total_vgaps; + for (int i = 1; i < N; i++) { + total_height += heights_final[i]; + } + + int overfill = total_height - tile_h; + if (overfill > 0) { + /* shrink from top down, excluding bottom */ + for (int i = 1; i < N - 1 && overfill > 0; i++) { + int shrink = MIN(overfill, heights_final[i] - min_raw); + heights_final[i] -= shrink; + overfill -= shrink; } + } + + /* if its not perfectly filled stretch bottom to absorb remainder */ + int actual_stack_height = total_vgaps; + for (int i = 1; i < N; i++) { + actual_stack_height += heights_final[i]; + } + + int shortfall = tile_h - actual_stack_height; + if (shortfall > 0) { + heights_final[N - 1] += shortfall; + } + int sy = tile_y; + int bw = user_config.border_width; + for (int i = 1; i < N; i++) { + Client *c = stackers[i]; + XWindowChanges wc = {.x = tile_x + master_w + gx, + .y = sy, + .width = MAX(1, stack_w - (2 * bw)), + .height = MAX(1, heights_final[i] - (2 * bw)), + .border_width = bw}; XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc); c->x = wc.x; c->y = wc.y; c->w = wc.width; c->h = wc.height; - i++; + sy += heights_final[i] + gy; } - } - update_borders(); + update_borders(); + } } void toggle_floating(void) @@ -1728,6 +1912,19 @@ void update_net_client_list(void) XChangeProperty(dpy, root, prop, XA_WINDOW, 32, PropModeReplace, (unsigned char *)wins, n); } +void warp_cursor(Client *c) +{ + if (!c) { + return; + } + + int center_x = c->x + (c->w / 2); + int center_y = c->y + (c->h / 2); + + XWarpPointer(dpy, None, root, 0, 0, 0, 0, center_x, center_y); + XSync(dpy, False); +} + int xerr(Display *dpy, XErrorEvent *ee) { /* ignore noise & non fatal errors */ |
