aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoe <rbo@gmx.us>2024-12-31 11:25:32 +0100
committerJoe <rbo@gmx.us>2024-12-31 11:25:32 +0100
commitcb1548919ec3ef0920afdd335de251e8722fae0e (patch)
tree278b68edf4564bae2d75c7685e88b1dcac3adb8c
parentreverted preserveonrestart (diff)
downloaddwm-cb1548919ec3ef0920afdd335de251e8722fae0e.tar.gz
dwm-cb1548919ec3ef0920afdd335de251e8722fae0e.tar.bz2
dwm-cb1548919ec3ef0920afdd335de251e8722fae0e.tar.xz
dwm-cb1548919ec3ef0920afdd335de251e8722fae0e.tar.zst
dwm-cb1548919ec3ef0920afdd335de251e8722fae0e.zip
bim
-rw-r--r--applied/dwm-restoreafterrestart-20220709-d3f93c7.diff101
-rw-r--r--dwm.c75
2 files changed, 176 insertions, 0 deletions
diff --git a/applied/dwm-restoreafterrestart-20220709-d3f93c7.diff b/applied/dwm-restoreafterrestart-20220709-d3f93c7.diff
new file mode 100644
index 0000000..d3b95ce
--- /dev/null
+++ b/applied/dwm-restoreafterrestart-20220709-d3f93c7.diff
@@ -0,0 +1,101 @@
+From 9fd4a02b57aa8a764d8105d5f2f854372f4ef559 Mon Sep 17 00:00:00 2001
+From: ViliamKovac1223 <viliamkovac1223@gmail.com>
+Date: Sat, 9 Jul 2022 17:35:54 +0200
+Subject: [PATCH] add restore patch
+
+---
+ config.def.h | 2 ++
+ dwm.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 2 files changed, 55 insertions(+)
+
+diff --git a/config.def.h b/config.def.h
+index 6ec4146..0b91976 100644
+--- a/config.def.h
++++ b/config.def.h
+@@ -1,5 +1,7 @@
+ /* See LICENSE file for copyright and license details. */
+
++#define SESSION_FILE "/tmp/dwm-session"
++
+ /* appearance */
+ static const unsigned int borderpx = 1; /* border pixel of windows */
+ static const unsigned int snap = 32; /* snap pixel */
+diff --git a/dwm.c b/dwm.c
+index 74cec7e..76b40a2 100644
+--- a/dwm.c
++++ b/dwm.c
+@@ -1255,11 +1255,63 @@ propertynotify(XEvent *e)
+ }
+ }
+
++void
++saveSession(void)
++{
++ FILE *fw = fopen(SESSION_FILE, "w");
++ for (Client *c = selmon->clients; c != NULL; c = c->next) { // get all the clients with their tags and write them to the file
++ fprintf(fw, "%lu %u\n", c->win, c->tags);
++ }
++ fclose(fw);
++}
++
++void
++restoreSession(void)
++{
++ // restore session
++ FILE *fr = fopen(SESSION_FILE, "r");
++ if (!fr)
++ return;
++
++ char *str = malloc(23 * sizeof(char)); // allocate enough space for excepted input from text file
++ while (fscanf(fr, "%[^\n] ", str) != EOF) { // read file till the end
++ long unsigned int winId;
++ unsigned int tagsForWin;
++ int check = sscanf(str, "%lu %u", &winId, &tagsForWin); // get data
++ if (check != 2) // break loop if data wasn't read correctly
++ break;
++
++ for (Client *c = selmon->clients; c ; c = c->next) { // add tags to every window by winId
++ if (c->win == winId) {
++ c->tags = tagsForWin;
++ break;
++ }
++ }
++ }
++
++ for (Client *c = selmon->clients; c ; c = c->next) { // refocus on windows
++ focus(c);
++ restack(c->mon);
++ }
++
++ for (Monitor *m = selmon; m; m = m->next) // rearrange all monitors
++ arrange(m);
++
++ free(str);
++ fclose(fr);
++
++ // delete a file
++ remove(SESSION_FILE);
++}
++
+ void
+ quit(const Arg *arg)
+ {
+ if(arg->i) restart = 1;
+ running = 0;
++
++ if (restart == 1)
++ saveSession();
+ }
+
+ Monitor *
+@@ -2173,6 +2225,7 @@ main(int argc, char *argv[])
+ die("pledge");
+ #endif /* __OpenBSD__ */
+ scan();
++ restoreSession();
+ run();
+ if(restart) execvp(argv[0], argv);
+ cleanup();
+--
+2.35.1
+
diff --git a/dwm.c b/dwm.c
index 2f9acf2..5639a54 100644
--- a/dwm.c
+++ b/dwm.c
@@ -79,6 +79,8 @@
#define VERSION_MINOR 0
#define XEMBED_EMBEDDED_VERSION (VERSION_MAJOR << 16) | VERSION_MINOR
+#define SESSION_FILE "/tmp/dwm-session"
+
/* enums */
enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
enum { SchemeNorm, SchemeSel }; /* color schemes */
@@ -247,7 +249,9 @@ static void resizeclient(Client *c, int x, int y, int w, int h);
static void resizemouse(const Arg *arg);
static void resizerequest(XEvent *e);
static void restack(Monitor *m);
+static void restoresession(void);
static void run(void);
+static void savesession(void);
static void scan(void);
static int sendevent(Window w, Atom proto, int m, long d0, long d1, long d2, long d3, long d4);
static void sendmon(Client *c, Monitor *m);
@@ -2134,6 +2138,8 @@ quit(const Arg *arg)
if (arg->i)
restart = 1;
running = 0;
+ if (restart == 1)
+ savesession();
}
Monitor *
@@ -2289,6 +2295,58 @@ restack(Monitor *m)
}
void
+restoresession(void)
+{
+ FILE *fr;
+ Client *c;
+ Monitor *m;
+ char *str;
+ long unsigned int winId;
+ unsigned int tagsForWin;
+ int mon;
+ int check;
+
+ // restore session
+ fr = fopen(SESSION_FILE, "r");
+ if (!fr)
+ return;
+
+ str = malloc(25 * sizeof(char)); // allocate enough space for excepted input from text file
+ while (fscanf(fr, "%[^\n] ", str) != EOF) { // read file till the end
+ check = sscanf(str, "%lu %u %d", &winId, &tagsForWin, &mon); // get data
+ if (check != 3) // break loop if data wasn't read correctly
+ break;
+
+ for (c = selmon->clients; c ; c = c->next) { // add tags to every window by winId
+ if (c->win == winId) {
+ for (m = selmon; m != NULL; m = m->next) {
+ if (m->num == mon) {
+ break;
+ }
+ }
+ sendmon(c, m);
+ c->tags = tagsForWin;
+ break;
+ }
+ }
+ }
+
+ for (c = selmon->clients; c ; c = c->next) { // refocus on windows
+ focus(c);
+ restack(c->mon);
+ }
+
+ for (m = selmon; m; m = m->next) // rearrange all monitors
+ arrange(m);
+
+ free(str);
+ fclose(fr);
+
+ // delete a file
+ // remove(SESSION_FILE);
+}
+
+void
run(void)
{
XEvent ev;
@@ -2300,6 +2358,22 @@ run(void)
}
void
+savesession(void)
+{
+ FILE *fw;
+ Client *c;
+ Monitor *m;
+
+ fw = fopen(SESSION_FILE, "w");
+ for (m = selmon; m != NULL; m = m->next) {
+ for (c = m->clients; c != NULL; c = c->next) { // get all the clients with their tags and write them to the file
+ fprintf(fw, "%lu %u %d\n", c->win, c->tags, c->mon->num);
+ }
+ }
+ fclose(fw);
+}
+
+void
scan(void)
{
unsigned int i, num;
@@ -3734,6 +3808,7 @@ main(int argc, char *argv[])
die("pledge");
#endif /* __OpenBSD__ */
scan();
+ restoresession();
run();
if (restart)
execvp(argv[0], argv);