about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAcid Bong <acid-bong@cock.lt>2022-10-21 23:19:09 +0300
committerAcid Bong <acid-bong@cock.lt>2022-10-21 23:19:09 +0300
commit5ba621bcbe6618ea9264d5cce4dc5c6fa44f4135 (patch)
tree20984fa98023d430cd46ebfc12d1180155e7a4e4
parente299bf86bbb1b1f9ee636bdb7e05c7cef40ae517 (diff)
downloaddwm-5ba621bcbe6618ea9264d5cce4dc5c6fa44f4135.tar.gz
patch: restore after restart
-rw-r--r--config.def.h2
-rw-r--r--dwm.c53
2 files changed, 55 insertions, 0 deletions
diff --git a/config.def.h b/config.def.h
index 9d21ce9..f80395f 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  = 2;        /* border pixel of windows */
 static const int gappx     = 10;                 /* gaps between windows */
diff --git a/dwm.c b/dwm.c
index 97c440a..0e15eab 100644
--- a/dwm.c
+++ b/dwm.c
@@ -1358,10 +1358,62 @@ 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 *
@@ -2527,6 +2579,7 @@ main(int argc, char *argv[])
 		die("pledge");
 #endif /* __OpenBSD__ */
 	scan();
+	restoreSession();
 	run();
 	if(restart) execvp(argv[0], argv);
 	cleanup();