sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

dwm-restoreafterrestart-20220709-d3f93c7.diff (2399B)


      1 From 9fd4a02b57aa8a764d8105d5f2f854372f4ef559 Mon Sep 17 00:00:00 2001
      2 From: ViliamKovac1223 <viliamkovac1223@gmail.com>
      3 Date: Sat, 9 Jul 2022 17:35:54 +0200
      4 Subject: [PATCH] add restore patch
      5 
      6 ---
      7  config.def.h |  2 ++
      8  dwm.c        | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++
      9  2 files changed, 55 insertions(+)
     10 
     11 diff --git a/config.def.h b/config.def.h
     12 index 6ec4146..0b91976 100644
     13 --- a/config.def.h
     14 +++ b/config.def.h
     15 @@ -1,5 +1,7 @@
     16  /* See LICENSE file for copyright and license details. */
     17  
     18 +#define SESSION_FILE "/tmp/dwm-session"
     19 +
     20  /* appearance */
     21  static const unsigned int borderpx  = 1;        /* border pixel of windows */
     22  static const unsigned int snap      = 32;       /* snap pixel */
     23 diff --git a/dwm.c b/dwm.c
     24 index 74cec7e..76b40a2 100644
     25 --- a/dwm.c
     26 +++ b/dwm.c
     27 @@ -1255,11 +1255,63 @@ propertynotify(XEvent *e)
     28  	}
     29  }
     30  
     31 +void
     32 +saveSession(void)
     33 +{
     34 +	FILE *fw = fopen(SESSION_FILE, "w");
     35 +	for (Client *c = selmon->clients; c != NULL; c = c->next) { // get all the clients with their tags and write them to the file
     36 +		fprintf(fw, "%lu %u\n", c->win, c->tags);
     37 +	}
     38 +	fclose(fw);
     39 +}
     40 +
     41 +void
     42 +restoreSession(void)
     43 +{
     44 +	// restore session
     45 +	FILE *fr = fopen(SESSION_FILE, "r");
     46 +	if (!fr)
     47 +		return;
     48 +
     49 +	char *str = malloc(23 * sizeof(char)); // allocate enough space for excepted input from text file
     50 +	while (fscanf(fr, "%[^\n] ", str) != EOF) { // read file till the end
     51 +		long unsigned int winId;
     52 +		unsigned int tagsForWin;
     53 +		int check = sscanf(str, "%lu %u", &winId, &tagsForWin); // get data
     54 +		if (check != 2) // break loop if data wasn't read correctly
     55 +			break;
     56 +		
     57 +		for (Client *c = selmon->clients; c ; c = c->next) { // add tags to every window by winId
     58 +			if (c->win == winId) {
     59 +				c->tags = tagsForWin;
     60 +				break;
     61 +			}
     62 +		}
     63 +    }
     64 +
     65 +	for (Client *c = selmon->clients; c ; c = c->next) { // refocus on windows
     66 +		focus(c);
     67 +		restack(c->mon);
     68 +	}
     69 +
     70 +	for (Monitor *m = selmon; m; m = m->next) // rearrange all monitors
     71 +		arrange(m);
     72 +
     73 +	free(str);
     74 +	fclose(fr);
     75 +	
     76 +	// delete a file
     77 +	remove(SESSION_FILE);
     78 +}
     79 +
     80  void
     81  quit(const Arg *arg)
     82  {
     83  	if(arg->i) restart = 1;
     84  	running = 0;
     85 +
     86 +	if (restart == 1)
     87 +		saveSession();
     88  }
     89  
     90  Monitor *
     91 @@ -2173,6 +2225,7 @@ main(int argc, char *argv[])
     92  		die("pledge");
     93  #endif /* __OpenBSD__ */
     94  	scan();
     95 +	restoreSession();
     96  	run();
     97  	if(restart) execvp(argv[0], argv);
     98  	cleanup();
     99 -- 
    100 2.35.1
    101