add scratchpad

This commit is contained in:
ktyl 2022-10-06 22:36:35 +01:00
parent 92daefd072
commit 348c914197
4 changed files with 132 additions and 0 deletions

View File

@ -58,11 +58,14 @@ static const Layout layouts[] = {
/* commands */ /* commands */
static const char *dmenucmd[] = { "dmenu_run", "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4, NULL }; static const char *dmenucmd[] = { "dmenu_run", "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4, NULL };
static const char *termcmd[] = { "st", NULL }; static const char *termcmd[] = { "st", NULL };
static const char scratchpadname[] = "scratchpad";
static const char *scratchpadcmd[] = { "st", "-t", scratchpadname, "-g", "120x34", NULL };
static const Key keys[] = { static const Key keys[] = {
/* modifier key function argument */ /* modifier key function argument */
{ MODKEY, XK_p, spawn, {.v = dmenucmd } }, { MODKEY, XK_p, spawn, {.v = dmenucmd } },
{ MODKEY|ShiftMask, XK_Return, spawn, {.v = termcmd } }, { MODKEY|ShiftMask, XK_Return, spawn, {.v = termcmd } },
{ MODKEY, XK_grave, togglescratch, {.v = scratchpadcmd } },
{ MODKEY, XK_b, togglebar, {0} }, { MODKEY, XK_b, togglebar, {0} },
{ MODKEY, XK_j, focusstack, {.i = +1 } }, { MODKEY, XK_j, focusstack, {.i = +1 } },
{ MODKEY, XK_k, focusstack, {.i = -1 } }, { MODKEY, XK_k, focusstack, {.i = -1 } },

View File

@ -61,12 +61,15 @@ static char dmenumon[2] = "0"; /* component of dmenucmd, manipulated in spawn()
static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", col_pink, "-nf", col_gray1, "-sb", col_gray1, "-sf", col_gray3, NULL }; static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", col_pink, "-nf", col_gray1, "-sb", col_gray1, "-sf", col_gray3, NULL };
static const char *browsercmd[] = { "qutebrowser", NULL }; static const char *browsercmd[] = { "qutebrowser", NULL };
static const char *termcmd[] = { "kitty", NULL }; static const char *termcmd[] = { "kitty", NULL };
static const char scratchpadname[] = "scratchpad";
static const char *scratchpadcmd[] = { "st", "-t", scratchpadname, "-g", "120x34", NULL };
static Key keys[] = { static Key keys[] = {
/* modifier key function argument */ /* modifier key function argument */
{ MODKEY, XK_p, spawn, {.v = dmenucmd } }, { MODKEY, XK_p, spawn, {.v = dmenucmd } },
{ MODKEY, XK_q, spawn, {.v = browsercmd } }, { MODKEY, XK_q, spawn, {.v = browsercmd } },
{ MODKEY|ShiftMask, XK_Return, spawn, {.v = termcmd } }, { MODKEY|ShiftMask, XK_Return, spawn, {.v = termcmd } },
{ MODKEY, XK_grave, togglescratch, {.v = scratchpadcmd } },
{ MODKEY, XK_b, togglebar, {0} }, { MODKEY, XK_b, togglebar, {0} },
{ MODKEY, XK_j, focusstack, {.i = +1 } }, { MODKEY, XK_j, focusstack, {.i = +1 } },
{ MODKEY, XK_k, focusstack, {.i = -1 } }, { MODKEY, XK_k, focusstack, {.i = -1 } },

36
dwm.c
View File

@ -216,6 +216,7 @@ static void tagtoprev(const Arg *arg);
static void tile(Monitor *m); static void tile(Monitor *m);
static void togglebar(const Arg *arg); static void togglebar(const Arg *arg);
static void togglefloating(const Arg *arg); static void togglefloating(const Arg *arg);
static void togglescratch(const Arg *arg);
static void toggletag(const Arg *arg); static void toggletag(const Arg *arg);
static void toggleview(const Arg *arg); static void toggleview(const Arg *arg);
static void unfocus(Client *c, int setfocus); static void unfocus(Client *c, int setfocus);
@ -278,6 +279,8 @@ static Window root, wmcheckwin;
/* configuration, allows nested code to access above variables */ /* configuration, allows nested code to access above variables */
#include "config.h" #include "config.h"
static unsigned int scratchtag = 1 << LENGTH(tags);
/* compile-time check if all tags fit into an unsigned int bit array. */ /* compile-time check if all tags fit into an unsigned int bit array. */
struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; }; struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
@ -1057,6 +1060,14 @@ manage(Window w, XWindowAttributes *wa)
c->y = MAX(c->y, c->mon->wy); c->y = MAX(c->y, c->mon->wy);
c->bw = borderpx; c->bw = borderpx;
selmon->tagset[selmon->seltags] &= ~scratchtag;
if (!strcmp(c->name, scratchpadname)) {
c->mon->tagset[c->mon->seltags] |= c->tags = scratchtag;
c->isfloating = True;
c->x = c->mon->wx + (c->mon->ww / 2 - WIDTH(c) / 2);
c->y = c->mon->wy + (c->mon->wh / 2 - HEIGHT(c) / 2);
}
wc.border_width = c->bw; wc.border_width = c->bw;
XConfigureWindow(dpy, w, CWBorderWidth, &wc); XConfigureWindow(dpy, w, CWBorderWidth, &wc);
XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel); XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
@ -1658,6 +1669,9 @@ sigchld(int unused)
void void
spawn(const Arg *arg) spawn(const Arg *arg)
{ {
if (arg->v == dmenucmd)
dmenumon[0] = '0' + selmon->num;
selmon->tagset[selmon->seltags] &= ~scratchtag;
if (fork() == 0) { if (fork() == 0) {
if (dpy) if (dpy)
close(ConnectionNumber(dpy)); close(ConnectionNumber(dpy));
@ -1762,6 +1776,28 @@ togglefloating(const Arg *arg)
arrange(selmon); arrange(selmon);
} }
void
togglescratch(const Arg *arg)
{
Client *c;
unsigned int found = 0;
for (c = selmon->clients; c && !(found = c->tags & scratchtag); c = c->next);
if (found) {
unsigned int newtagset = selmon->tagset[selmon->seltags] ^ scratchtag;
if (newtagset) {
selmon->tagset[selmon->seltags] = newtagset;
focus(NULL);
arrange(selmon);
}
if (ISVISIBLE(c)) {
focus(c);
restack(selmon);
}
} else
spawn(arg);
}
void void
toggletag(const Arg *arg) toggletag(const Arg *arg)
{ {

View File

@ -0,0 +1,90 @@
diff -up a/config.def.h b/config.def.h
--- a/config.def.h 2019-06-06 21:23:27.006661784 +0200
+++ b/config.def.h 2019-06-20 15:05:59.083102462 +0200
@@ -58,11 +58,14 @@ static const Layout layouts[] = {
static char dmenumon[2] = "0"; /* component of dmenucmd, manipulated in spawn() */
static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4, NULL };
static const char *termcmd[] = { "st", NULL };
+static const char scratchpadname[] = "scratchpad";
+static const char *scratchpadcmd[] = { "st", "-t", scratchpadname, "-g", "120x34", NULL };
static Key keys[] = {
/* modifier key function argument */
{ MODKEY, XK_p, spawn, {.v = dmenucmd } },
{ MODKEY|ShiftMask, XK_Return, spawn, {.v = termcmd } },
+ { MODKEY, XK_grave, togglescratch, {.v = scratchpadcmd } },
{ MODKEY, XK_b, togglebar, {0} },
{ MODKEY, XK_j, focusstack, {.i = +1 } },
{ MODKEY, XK_k, focusstack, {.i = -1 } },
diff -up a/dwm.c b/dwm.c
--- a/dwm.c 2019-06-06 21:23:27.023328450 +0200
+++ b/dwm.c 2019-06-20 15:07:01.089767947 +0200
@@ -213,6 +213,7 @@ static void tagmon(const Arg *arg);
static void tile(Monitor *);
static void togglebar(const Arg *arg);
static void togglefloating(const Arg *arg);
+static void togglescratch(const Arg *arg);
static void toggletag(const Arg *arg);
static void toggleview(const Arg *arg);
static void unfocus(Client *c, int setfocus);
@@ -273,6 +274,8 @@ static Window root, wmcheckwin;
/* configuration, allows nested code to access above variables */
#include "config.h"
+static unsigned int scratchtag = 1 << LENGTH(tags);
+
/* compile-time check if all tags fit into an unsigned int bit array. */
struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
@@ -1052,6 +1055,14 @@ manage(Window w, XWindowAttributes *wa)
&& (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
c->bw = borderpx;
+ selmon->tagset[selmon->seltags] &= ~scratchtag;
+ if (!strcmp(c->name, scratchpadname)) {
+ c->mon->tagset[c->mon->seltags] |= c->tags = scratchtag;
+ c->isfloating = True;
+ c->x = c->mon->wx + (c->mon->ww / 2 - WIDTH(c) / 2);
+ c->y = c->mon->wy + (c->mon->wh / 2 - HEIGHT(c) / 2);
+ }
+
wc.border_width = c->bw;
XConfigureWindow(dpy, w, CWBorderWidth, &wc);
XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
@@ -1661,6 +1672,7 @@ spawn(const Arg *arg)
{
if (arg->v == dmenucmd)
dmenumon[0] = '0' + selmon->num;
+ selmon->tagset[selmon->seltags] &= ~scratchtag;
if (fork() == 0) {
if (dpy)
close(ConnectionNumber(dpy));
@@ -1748,6 +1760,28 @@ togglefloating(const Arg *arg)
}
void
+togglescratch(const Arg *arg)
+{
+ Client *c;
+ unsigned int found = 0;
+
+ for (c = selmon->clients; c && !(found = c->tags & scratchtag); c = c->next);
+ if (found) {
+ unsigned int newtagset = selmon->tagset[selmon->seltags] ^ scratchtag;
+ if (newtagset) {
+ selmon->tagset[selmon->seltags] = newtagset;
+ focus(NULL);
+ arrange(selmon);
+ }
+ if (ISVISIBLE(c)) {
+ focus(c);
+ restack(selmon);
+ }
+ } else
+ spawn(arg);
+}
+
+void
toggletag(const Arg *arg)
{
unsigned int newtags;