Skip to content

Commit

Permalink
added moving of focus and cursor with keybinds, all options configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
jasper-clarke committed Nov 9, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent d116586 commit 5918c30
Showing 6 changed files with 72 additions and 14 deletions.
7 changes: 3 additions & 4 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# TODO List

- Recalculate window focus when a new window is opened
- When a window is closed focus the master window
- Move focus with window when its moved to another monitor
- When focus moves, move the cursor to the center of the focused window
- Ensure that the new window center will be the new position of the recalculated window

- Add all configuration options to configurer
34 changes: 32 additions & 2 deletions atlas.c
Original file line number Diff line number Diff line change
@@ -631,6 +631,7 @@ void focusMonitor(const Arg *arg) {
unfocus(selectedMonitor->sel, 0);
selectedMonitor = m;
focus(NULL);
moveCursorToClientCenter(selectedMonitor->sel);
}

void focusstack(const Arg *arg) {
@@ -656,6 +657,7 @@ void focusstack(const Arg *arg) {
}
if (c) {
focus(c);
moveCursorToClientCenter(c);
restack(selectedMonitor);
}
}
@@ -870,7 +872,12 @@ void manage(Window w, XWindowAttributes *wa) {
c->mon->sel = c;
arrange(c->mon);
XMapWindow(dpy, c->win);
focus(NULL);
if (cfg.focusNewWindows) {
focus(c);
moveCursorToClientCenter(c);
} else {
focus(NULL);
}
}

void mappingnotify(XEvent *e) {
@@ -1033,6 +1040,19 @@ void handlePropertyChange(XEvent *e) {
}
}

void moveCursorToClientCenter(Client *c) {
if (!c || !cfg.moveCursorWithFocus)
return;

// Calculate center coordinates of the window
int x = c->x + (c->w / 2);
int y = c->y + (c->h / 2);

// Move cursor to window center
XWarpPointer(dpy, None, root, 0, 0, 0, 0, x, y);
XFlush(dpy);
}

void quit(const Arg *arg) { running = 0; }

Monitor *getMonitorForArea(int x, int y, int w, int h) {
@@ -1504,6 +1524,8 @@ void directWindowToMonitor(const Arg *arg) {
if (!selectedMonitor->sel || !monitors->next)
return;
sendWindowToMonitor(selectedMonitor->sel, findMonitorInDirection(arg->i));
focusMonitor(arg);
moveCursorToClientCenter(selectedMonitor->sel);
}

void toggleDash(const Arg *arg) {
@@ -1584,7 +1606,15 @@ void unmanage(Client *c, int destroyed) {
XUngrabServer(dpy);
}
free(c);
focus(NULL);

Client *next = getNextTiledWindow(m->clients);
if (next && cfg.focusMasterOnClose) {
focus(next);
moveCursorToClientCenter(next);
} else {
focus(NULL);
}

updateclientlist();
arrange(m);
}
1 change: 1 addition & 0 deletions atlas.h
Original file line number Diff line number Diff line change
@@ -233,6 +233,7 @@ void mappingnotify(XEvent *e);
void maprequest(XEvent *e);
void handleMouseMotion(XEvent *e);
void movemouse(const Arg *arg);
void moveCursorToClientCenter(Client *c);
void pop(Client *c);
void handlePropertyChange(XEvent *e);
void quit(const Arg *arg);
13 changes: 6 additions & 7 deletions config.h
Original file line number Diff line number Diff line change
@@ -68,12 +68,11 @@ static const Key keys[] = {
{MODKEY, XK_q, killclient, {0}},

{MODKEY, XK_t, setlayout, {.v = &layouts[0]}},
{MODKEY, XK_f, setlayout, {.v = &layouts[1]}},
{MODKEY, XK_m, setlayout, {.v = &layouts[2]}},
{MODKEY, XK_d, setlayout, {.v = &layouts[3]}},
{MODKEY | ShiftMask, XK_d, setlayout, {.v = &layouts[4]}},
{MODKEY, XK_space, setlayout, {0}},
{MODKEY | ShiftMask, XK_space, toggleWindowFloating, {0}},
{MODKEY,
XK_space,
spawn,
{.v = "rofi -show drun -theme ~/.config/rofi/launcher.rasi"}},
{MODKEY, XK_f, toggleWindowFloating, {0}},

{MODKEY, XK_0, view, {.ui = ~0}},
{MODKEY | ShiftMask, XK_0, tag, {.ui = ~0}},
@@ -84,7 +83,7 @@ static const Key keys[] = {
{MODKEY | ShiftMask, XK_l, directWindowToMonitor, {.i = +1}},

{MODKEY | ShiftMask, XK_q, quit, {0}},
{MODKEY | ShiftMask, XK_r, reload, {0}},
{MODKEY, XK_r, reload, {0}},

TAGKEYS(XK_1, 0) TAGKEYS(XK_2, 1) TAGKEYS(XK_3, 2) TAGKEYS(XK_4, 3)
TAGKEYS(XK_5, 4) TAGKEYS(XK_6, 5) TAGKEYS(XK_7, 6) TAGKEYS(XK_8, 7)
26 changes: 25 additions & 1 deletion configurer.c
Original file line number Diff line number Diff line change
@@ -14,7 +14,10 @@ Config cfg = {.outerGaps = 20,
.numMasterWindows = 1,
.lockFullscreen = 1,
.showDash = 1,
.topBar = 1};
.topBar = 1,
.focusNewWindows = 1,
.moveCursorWithFocus = 1,
.focusMasterOnClose = 1};

void update_window_manager_state(void) {
Monitor *m;
@@ -149,6 +152,27 @@ int load_config(const char *config_path) {
}
}

// window configuration
toml_table_t *windows = toml_table_in(conf, "windows");
if (layout) {
toml_datum_t focus_new_windows = toml_bool_in(layout, "focus_new_windows");
if (focus_new_windows.ok) {
cfg.focusNewWindows = focus_new_windows.u.b;
}

toml_datum_t move_cursor_with_focus =
toml_bool_in(layout, "move_cursor_with_focus");
if (move_cursor_with_focus.ok) {
cfg.moveCursorWithFocus = move_cursor_with_focus.u.b;
}

toml_datum_t focus_master_on_close =
toml_bool_in(layout, "focus_master_on_close");
if (focus_master_on_close.ok) {
cfg.focusMasterOnClose = focus_master_on_close.u.b;
}
}

toml_free(conf);
return 1;
}
5 changes: 5 additions & 0 deletions configurer.h
Original file line number Diff line number Diff line change
@@ -22,6 +22,11 @@ typedef struct {
float masterFactor;
int numMasterWindows;
int lockFullscreen;

// Window
int focusNewWindows;
int focusMasterOnClose;
int moveCursorWithFocus;
} Config;

// Global configuration instance

0 comments on commit 5918c30

Please sign in to comment.