aboutsummaryrefslogtreecommitdiffstats
path: root/src/session.c
blob: aeadf9af2da0d25903de345f009629ea2d9b04ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "session.h"

#define DEFAULT_SESSIONS "mbmenu,minitime"

void 
session_set_defaults(MBPanel *panel, char *defaults)
{
  panel->session_defaults_cur_pos = strdup(defaults);
  panel->use_alt_session_defaults = True;
}

void
session_destroy(MBPanel *panel)
{
  char sessionfile[512] = { 0 };

  if (!panel->use_session) return;

  if (panel->system_tray_id)
    snprintf(sessionfile, 512, "%s/.matchbox/%s.%i", getenv("HOME"), 
	     PANELFILE, panel->system_tray_id );   
  else
    snprintf(sessionfile, 512, "%s/.matchbox/%s", 
	     getenv("HOME"), PANELFILE );   
  
  unlink(sessionfile);
}

void 
session_init(MBPanel *panel)
{
  char sessionfile[512] = { 0 };
  struct stat st;

  DBG("%s() called\n", __func__);

  panel->session_preexisting_lock   = False;
  panel->session_run_first_time     = False;

  if (!panel->use_session && !panel->use_alt_session_defaults) return;

  /*
  if (panel->use_alt_session_defaults)
    {
      panel->session_cur_gravity = PAPP_GRAVITY_END;
      panel->session_run_first_time = True;
    }
  */ 

  if (getenv("HOME") == NULL)
    {
      fprintf(stderr, "matchbox-panel: unable to get home directory, is HOME set?\n");
      panel->session_preexisting_lock = False;
      return;
    }

  snprintf(sessionfile, 512, "%s/.matchbox", getenv("HOME"));
  
  /* Check if ~/.matchbox exists and create if not */
  if (stat(sessionfile, &st) != 0 /* || !S_ISDIR(st.st_mode) */
      || !panel->use_session)
    {
      if (panel->use_session)
	{
	  fprintf(stderr, "matchbox-panel: creating %s directory for session files\n",
		  sessionfile);
	  mkdir(sessionfile, 0755);
	}
      panel->session_cur_gravity = PAPP_GRAVITY_END;
      panel->session_run_first_time = True;
      if (!panel->use_alt_session_defaults)
	panel->session_defaults_cur_pos = strdup(DEFAULT_SESSIONS);
    }
  else
    {
      /* We have a ~/.matchbox , see if we have a session file 
	 and set defualts if not. 
      */
      if (panel->system_tray_id)
	snprintf(sessionfile, 512, "%s/.matchbox/%s.%i", getenv("HOME"), 
		 PANELFILE, panel->system_tray_id );   
      else
	snprintf(sessionfile, 512, "%s/.matchbox/%s", 
		 getenv("HOME"), PANELFILE );   

      if ((panel->session_fp = fopen(sessionfile, "r")) == NULL)
	{
	  fprintf(stderr,
		  "mbpanel: Session file does not exist ( tryed %s )\n", 
		  sessionfile);
	  if (!panel->use_alt_session_defaults)
	    {
	      panel->session_defaults_cur_pos = strdup(DEFAULT_SESSIONS);
	    }
	  panel->session_cur_gravity = PAPP_GRAVITY_START;
	  panel->session_run_first_time = True;
	}
      else
	{
	    DBG("%s() opened %s\n", __func__, sessionfile);
	}
    }

  panel->session_preexisting_lock = True;  /* we are loading session data  */
  panel->session_entry_cur[0] = 0; 

  session_preexisting_start_next(panel);
   
}

void session_save(MBPanel *panel)
{
  char *sessionfile = alloca(sizeof(char)*255);

  int i = 0;
  MBPanelApp *papp = NULL;
  MBPanelApp *papp_heads[] = { panel->apps_start_head, 
			       panel->apps_end_head    };
  
  DBG("%s() called\n", __func__);

  if (!panel->use_session 
      || session_preexisting_restarting(panel))
    return;

  if (getenv("HOME") == NULL)
    {
      fprintf(stderr, "matchbox-panel: unable to get home directory, is HOME set?\n");
      return;
    }

  if (panel->system_tray_id)
    snprintf(sessionfile, 255, "%s/.matchbox/%s.%i", getenv("HOME"), 
	     PANELFILE, panel->system_tray_id );   
  else
    snprintf(sessionfile, 255, "%s/.matchbox/%s", 
	     getenv("HOME"), PANELFILE );   
  
  if ((panel->session_fp = fopen(sessionfile, "w")) == NULL)
    { 
      fprintf(stderr,"matchbox-panel: Unable to create Session file ( %s )\n", 
	      sessionfile); 
      return; 
    }

  DBG("%s() still called\n", __func__);
  
  while (i < 2)
    {
      papp = papp_heads[i];
      while( papp != NULL )
	{
	  DBG("%s() writing %s\n", __func__, papp->cmd_str);
	  if (papp->cmd_str)
	    {
	      DBG("%s() writing %s\n", __func__, papp->cmd_str);
	      fprintf(panel->session_fp, "%s\n", papp->cmd_str);  
	    }
	  papp = papp->next;
	}
      fprintf(panel->session_fp, "\t\n" );
      i++;
    }
  
  fclose(panel->session_fp);
}

Bool 
session_preexisting_restarting(MBPanel *panel)
{
  return panel->session_preexisting_lock;
}

Bool
session_preexisting_start_next(MBPanel *panel)
{
  if (!session_preexisting_restarting(panel)) return False;

  if (panel->session_entry_cur[0] == '\0' 
      && session_preexisting_get_next(panel))
    {
      DBG("%s() starting %s\n", __func__, panel->session_entry_cur);
      panel->session_needed_pid = util_fork_exec(panel->session_entry_cur);
      return True;
    }
  return False;
}

Bool 
session_preexisting_win_matches_wanted(MBPanel *panel, Window win, 
				       char *win_cmd)
{
  pid_t win_pid = 0;
  if (!session_preexisting_restarting(panel)) return False;

  DBG("%s() called\n", __func__);

  if (panel->session_entry_cur) /* what were waiting on */
    {
      /* Check if its got the pid we expect */
      win_pid = util_get_window_pid_from_prop(panel, win);

      DBG("%s() win pid is %i\n", __func__, win_pid);

      if (win_pid && win_pid == panel->session_needed_pid)
	return True;

      DBG("%s() pid failed, comparing '%s' vs '%s'\n", __func__, 
	  win_cmd, panel->session_entry_cur );

      /* check cmd str */
      if (win_cmd && !strncmp(win_cmd, panel->session_entry_cur, 
			      strlen(win_cmd)))
	return True;
    }

  return False;
}

void
session_preexisting_clear_current(MBPanel *panel)
{
  panel->session_entry_cur[0] = '\0';
}

Bool 
session_preexisting_handle_timeouts(MBPanel *panel)
{
  if (!session_preexisting_restarting(panel)) return False;

  DBG("%s() called\n", __func__);

  /* catch session timeouts */
  if (panel->session_entry_cur[0] != '\0')
    {
      if ( (time(NULL)-panel->session_start_time) > SESSION_TIMEOUT)
	{
	  fprintf(stderr, "Session timeout on %s\n", panel->session_entry_cur);
	  session_preexisting_clear_current(panel);
	  session_preexisting_start_next(panel);
	}
    }
  return True;
}

Bool 
session_preexisting_get_next(MBPanel *panel) /* session_restarting_get_next  */
{
  char *tmp;

  if (!session_preexisting_restarting(panel)) return False;

  panel->session_init_offset = 10;

  if (panel->session_run_first_time)
    {
      if (*panel->session_defaults_cur_pos != '\0')
	{
	  char *prev_pos  = panel->session_defaults_cur_pos;
	  while ( *panel->session_defaults_cur_pos != '\0')
	    {
	      if ( *panel->session_defaults_cur_pos == ',')
	       {
		 *panel->session_defaults_cur_pos = '\0';
		 panel->session_defaults_cur_pos++;
		 break;
	       }
	      panel->session_defaults_cur_pos++;
	    }

	  strncpy(panel->session_entry_cur, prev_pos, 512);
         }
      else
	{
	  panel->session_run_first_time   = False;
	  panel->session_preexisting_lock = False;
	  session_save(panel);
	  return False;
	}
    }
  else
    {

      if (fgets(panel->session_entry_cur, 512, panel->session_fp) == NULL)
	{
	  fclose(panel->session_fp); /* All sessions done */
	  panel->session_preexisting_lock = False;
	  session_save(panel);
	  return False;
	}
      
      /* tab + newline -> change the session gravity */
      if (!strcmp(panel->session_entry_cur, "\t\n"))
	{
	  panel->session_cur_gravity = PAPP_GRAVITY_END;
	  return session_preexisting_get_next(panel);
	}

      if ( panel->session_entry_cur[strlen(panel->session_entry_cur)-1] == '\n')
	panel->session_entry_cur[strlen(panel->session_entry_cur)-1] = '\0';

      if ( (tmp = strstr(panel->session_entry_cur, "\t\t")) != NULL )
	{
	  panel->session_init_offset = atoi(tmp);
	  *tmp = '\0';
	}
    }

  panel->session_start_time = time(NULL);
  return True;
}