diff options
| author | Abhinav Prasai <72694427+uint23@users.noreply.github.com> | 2025-06-06 23:45:31 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-06 23:45:31 +0100 |
| commit | 161be8b6e0fa43910c866dc3e925f2d288283dc3 (patch) | |
| tree | 5f49360bd6e23e30e0987bddd9e9293142a665f9 | |
| parent | b0bf3e6250b767f2786f340b577df5f445f9d6aa (diff) | |
| parent | 08c1543e60d82ca2bff0b3b1027c546ddff07ca3 (diff) | |
Merge pull request #77 from RickConsole/main
Add multi-monitor focus switching and window moving
| -rw-r--r-- | README.md | 6 | ||||
| -rw-r--r-- | default_sxwmrc | 8 | ||||
| -rw-r--r-- | src/config.h | 7 | ||||
| -rw-r--r-- | src/defs.h | 4 | ||||
| -rw-r--r-- | src/sxwm.c | 165 |
5 files changed, 189 insertions, 1 deletions
@@ -110,6 +110,10 @@ workspace : modifier + modifier + ... + key : swap n | `decrease_gaps` | Shrinks gaps. | | `focus_next` | Moves focus forward in the stack. | | `focus_previous` | Moves focus backward in the stack. | +| `focus_next_mon` | Switches focus to the next monitor. | +| `focus_prev_mon` | Switches focus to the previous monitor. | +| `move_next_mon` | Moves the focused window to the next monitor. | +| `move_prev_mon` | Moves the focused window to the previous monitor. | | `increase_gaps` | Expands gaps. | | `master_next` | Moves focused window down in master/stack order. | | `master_prev` | Moves focused window up in master/stack order. | @@ -147,6 +151,8 @@ workspace : mod + shift + 5 : swap 5 | `MOD` + Left Mouse | Move window by mouse | | `MOD` + Right Mouse | Resize window by mouse | | `MOD` + `j` / `k` | Focus next / previous | +| `MOD` + `,` / `.` | Focus prev / next monitor | +| `MOD` + `Shift` + `,` / `.` | Move window to prev / next monitor | | `MOD` + `Shift` + `j` / `k` | Move in master stack | | `MOD` + `Space` | Toggle floating | | `MOD` + `Shift` + `Space` | Toggle all floating | diff --git a/default_sxwmrc b/default_sxwmrc index 6efede7..89cf67d 100644 --- a/default_sxwmrc +++ b/default_sxwmrc @@ -34,6 +34,14 @@ call : mod + shift + e : quit call : mod + j : focus_next call : mod + k : focus_prev +# Monitor Focus: +call : mod + comma : focus_prev_mon +call : mod + period : focus_next_mon + +# Move Window Between Monitors: +call : mod + shift + comma : swap_prev_mon +call : mod + shift + period : swap_next_mon + # Master/Stack Movement call : mod + shift + j : master_next call : mod + shift + k : master_previous diff --git a/src/config.h b/src/config.h index 7c42764..3c0f0bc 100644 --- a/src/config.h +++ b/src/config.h @@ -13,6 +13,11 @@ const Binding binds[] = { {Mod4Mask, XK_j, {.fn = focus_next}, TYPE_FUNC}, {Mod4Mask, XK_k, {.fn = focus_prev}, TYPE_FUNC}, + {Mod4Mask, XK_comma, {.fn = focus_prev_mon}, TYPE_FUNC}, + {Mod4Mask, XK_period, {.fn = focus_next_mon}, TYPE_FUNC}, + {Mod4Mask | ShiftMask, XK_comma, {.fn = move_prev_mon}, TYPE_FUNC}, + {Mod4Mask | ShiftMask, XK_period, {.fn = move_next_mon}, TYPE_FUNC}, + {Mod4Mask | ShiftMask, XK_j, {.fn = move_master_next}, TYPE_FUNC}, {Mod4Mask | ShiftMask, XK_k, {.fn = move_master_prev}, TYPE_FUNC}, @@ -53,4 +58,4 @@ const Binding binds[] = { {Mod4Mask | ShiftMask, XK_8, {.ws = 7}, TYPE_MWKSP}, {Mod4Mask, XK_9, {.ws = 8}, TYPE_CWKSP}, {Mod4Mask | ShiftMask, XK_9, {.ws = 8}, TYPE_MWKSP}, -};
\ No newline at end of file +}; @@ -107,6 +107,10 @@ extern void close_focused(void); extern void dec_gaps(void); extern void focus_next(void); extern void focus_prev(void); +extern void focus_next_mon(void); +extern void focus_prev_mon(void); +extern void move_next_mon(void); +extern void move_prev_mon(void); extern void inc_gaps(void); extern void move_master_next(void); extern void move_master_prev(void); @@ -123,6 +123,7 @@ Window root; Window wm_check_win; Monitor *mons = NULL; int monsn = 0; +int current_monitor = 0; Bool global_floating = False; Bool in_ws_switch = False; Bool running = False; @@ -323,6 +324,7 @@ void focus_next(void) } focused = (focused->next ? focused->next : workspaces[current_ws]); + current_monitor = focused->mon; XSetInputFocus(dpy, focused->win, RevertToPointerRoot, CurrentTime); XRaiseWindow(dpy, focused->win); if (user_config.warp_cursor) { @@ -353,6 +355,7 @@ void focus_prev(void) focused = prev; } + current_monitor = focused->mon; XSetInputFocus(dpy, focused->win, RevertToPointerRoot, CurrentTime); XRaiseWindow(dpy, focused->win); if (user_config.warp_cursor) { @@ -361,6 +364,168 @@ void focus_prev(void) update_borders(); } +void focus_next_mon(void) +{ + if (monsn <= 1) { + return; /* only one monitor, nothing to switch to */ + } + + /* use current_monitor if no focused window, otherwise use focused window's monitor */ + int current_mon = focused ? focused->mon : current_monitor; + int target_mon = (current_mon + 1) % monsn; + + /* find the first window on the target monitor in current workspace */ + Client *target_client = NULL; + for (Client *c = workspaces[current_ws]; c; c = c->next) { + if (c->mon == target_mon && c->mapped && !c->fullscreen) { + target_client = c; + break; + } + } + + if (target_client) { + /* focus the window on target monitor */ + focused = target_client; + current_monitor = target_mon; + XSetInputFocus(dpy, focused->win, RevertToPointerRoot, CurrentTime); + XRaiseWindow(dpy, focused->win); + if (user_config.warp_cursor) { + warp_cursor(focused); + } + update_borders(); + } + else { + /* no windows on target monitor, just move cursor to center and update current_monitor */ + current_monitor = target_mon; + int center_x = mons[target_mon].x + mons[target_mon].w / 2; + int center_y = mons[target_mon].y + mons[target_mon].h / 2; + XWarpPointer(dpy, None, root, 0, 0, 0, 0, center_x, center_y); + XSync(dpy, False); + } +} + +void focus_prev_mon(void) +{ + if (monsn <= 1) { + return; /* only one monitor, nothing to switch to */ + } + + /* use current_monitor if no focused window, otherwise use focused window's monitor */ + int current_mon = focused ? focused->mon : current_monitor; + int target_mon = (current_mon - 1 + monsn) % monsn; + + /* find the first window on the target monitor in current workspace */ + Client *target_client = NULL; + for (Client *c = workspaces[current_ws]; c; c = c->next) { + if (c->mon == target_mon && c->mapped && !c->fullscreen) { + target_client = c; + break; + } + } + + if (target_client) { + /* focus the window on target monitor */ + focused = target_client; + current_monitor = target_mon; + XSetInputFocus(dpy, focused->win, RevertToPointerRoot, CurrentTime); + XRaiseWindow(dpy, focused->win); + if (user_config.warp_cursor) { + warp_cursor(focused); + } + update_borders(); + } + else { + /* no windows on target monitor, just move cursor to center and update current_monitor */ + current_monitor = target_mon; + int center_x = mons[target_mon].x + mons[target_mon].w / 2; + int center_y = mons[target_mon].y + mons[target_mon].h / 2; + XWarpPointer(dpy, None, root, 0, 0, 0, 0, center_x, center_y); + XSync(dpy, False); + } +} + +void move_next_mon(void) +{ + if (!focused || monsn <= 1) { + return; /* no focused window or only one monitor */ + } + + int target_mon = (focused->mon + 1) % monsn; + + /* update window's monitor assignment */ + focused->mon = target_mon; + current_monitor = target_mon; + + /* if window is floating, center it on the target monitor */ + if (focused->floating) { + int mx = mons[target_mon].x, my = mons[target_mon].y; + int mw = mons[target_mon].w, mh = mons[target_mon].h; + int x = mx + (mw - focused->w) / 2; + int y = my + (mh - focused->h) / 2; + + /* ensure window stays within monitor bounds */ + if (x < mx) x = mx; + if (y < my) y = my; + if (x + focused->w > mx + mw) x = mx + mw - focused->w; + if (y + focused->h > my + mh) y = my + mh - focused->h; + + focused->x = x; + focused->y = y; + XMoveWindow(dpy, focused->win, x, y); + } + + /* retile to update layouts on both monitors */ + tile(); + + /* follow the window with cursor if enabled */ + if (user_config.warp_cursor) { + warp_cursor(focused); + } + + update_borders(); +} + +void move_prev_mon(void) +{ + if (!focused || monsn <= 1) { + return; /* no focused window or only one monitor */ + } + + int target_mon = (focused->mon - 1 + monsn) % monsn; + + /* update window's monitor assignment */ + focused->mon = target_mon; + current_monitor = target_mon; + + /* if window is floating, center it on the target monitor */ + if (focused->floating) { + int mx = mons[target_mon].x, my = mons[target_mon].y; + int mw = mons[target_mon].w, mh = mons[target_mon].h; + int x = mx + (mw - focused->w) / 2; + int y = my + (mh - focused->h) / 2; + + /* ensure window stays within monitor bounds */ + if (x < mx) x = mx; + if (y < my) y = my; + if (x + focused->w > mx + mw) x = mx + mw - focused->w; + if (y + focused->h > my + mh) y = my + mh - focused->h; + + focused->x = x; + focused->y = y; + XMoveWindow(dpy, focused->win, x, y); + } + + /* retile to update layouts on both monitors */ + tile(); + + /* follow the window with cursor if enabled */ + if (user_config.warp_cursor) { + warp_cursor(focused); + } + + update_borders(); +} + int get_monitor_for(Client *c) { int cx = c->x + c->w / 2, cy = c->y + c->h / 2; |
