aboutsummaryrefslogtreecommitdiffstats
path: root/mbmonitor.c
blob: beeec399f7167d0ecc821a88a9761168e37f0d7a (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * mb-applet-startup-monitor - A tray app that provides feedback
 *                             on application startup.
 *
 * Copyright 2004, Openedhand Ltd. By Matthew Allum <mallum@o-hand.com>
 *
 * Based very roughly on GPE's startup monitor. 
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>

#define SN_API_NOT_YET_FROZEN 
#include <libsn/sn.h>

#include <libmb/mb.h>

#define TIMEOUT		   20
#define POLLTIME	   10
#define HOURGLASS_N_FRAMES 8 

typedef struct LaunchList LaunchList;

struct LaunchList
{
  char       *id;
  time_t      when;
  LaunchList *next;
};

/* 
 * Lazy boy globals :/
 */
static MBTrayApp     *app = NULL;
static LaunchList    *launch_list = NULL;
static MBPixbuf      *pb;
static MBPixbufImage *img_hourglass[HOURGLASS_N_FRAMES] = { 0,0,0,0,0,0,0,0 };static  MBPixbufImage *img_hourglass_scaled[HOURGLASS_N_FRAMES] = { 0,0,0,0,0,0,0,0 };
static Bool           hourglass_shown;
static int            hourglass_cur_frame_n = 0;
static char          *ThemeName = NULL;

static SnDisplay        *display;
static SnMonitorContext *context;

static void
show_hourglass (void)
{
  mb_tray_app_request_offset (app, -1);
  mb_tray_app_set_session (app, False); 
  mb_tray_app_unhide (app);
  hourglass_shown = TRUE;
}

static void
hide_hourglass (void)
{
  mb_tray_app_hide (app);
  XFlush(mb_tray_app_xdisplay(app));
  XSelectInput (mb_tray_app_xdisplay(app), mb_tray_app_xrootwin(app), 
		PropertyChangeMask | StructureNotifyMask);
  hourglass_shown = FALSE;
}

static void
monitor_event_func (SnMonitorEvent *event,
                    void            *user_data)
{
  SnMonitorContext *context;
  SnStartupSequence *sequence;
  const char *id;
  time_t t;
  
  context = sn_monitor_event_get_context (event);
  sequence = sn_monitor_event_get_startup_sequence (event);
  id = sn_startup_sequence_get_id (sequence);

  switch (sn_monitor_event_get_type (event))
    {
    case SN_MONITOR_EVENT_INITIATED:
      {
	
	LaunchList *item = launch_list;

	if (item == NULL)
	  {
	    launch_list = item = malloc(sizeof(LaunchList));
	  }
	else
	  {
	    while (item->next != NULL)
	      item = item->next;
	    item->next = malloc(sizeof(LaunchList));
	    item = item->next;
	  }

	item->next = NULL;
	item->id   = strdup (id);
	t = time (NULL);
	item->when = t + TIMEOUT;

	if (! hourglass_shown)
	  show_hourglass ();
      }
      break;

    case SN_MONITOR_EVENT_COMPLETED:
    case SN_MONITOR_EVENT_CANCELED:
      {
	LaunchList *item = launch_list, *last_item = NULL;

	while (item != NULL)
	  {
	    if (!strcmp(item->id, id))
	      {
		if (last_item == NULL)
		  launch_list = item->next;
		else
		  last_item->next = item->next;

		free(item->id);
		free(item);

		break;
	      }
	    last_item = item;
	    item = item->next;
	  }

	if (launch_list == NULL && hourglass_shown)
	  hide_hourglass ();
      }
      break;
    default:
      break; 			/* Nothing */
    }
}

void
paint_callback ( MBTrayApp *app, Drawable drw )
{
  MBPixbufImage *img_backing = NULL;

  if (!hourglass_shown) return;
  
  img_backing = mb_tray_app_get_background (app, pb);

  /* CurrentVolLevel */
  mb_pixbuf_img_composite(pb, img_backing, 
			  img_hourglass_scaled[hourglass_cur_frame_n], 
			  0, 0);

  mb_pixbuf_img_render_to_drawable(pb, img_backing, drw, 0, 0);

  mb_pixbuf_img_free( pb, img_backing );
}

void
resize_callback ( MBTrayApp *app, int w, int h )
{
  int i;

  if (!hourglass_shown) return;

  for (i=0; i<HOURGLASS_N_FRAMES; i++)
    {
      if (img_hourglass_scaled[i] != NULL) 
	mb_pixbuf_img_free(pb, img_hourglass_scaled[i]);
      img_hourglass_scaled[i] = mb_pixbuf_img_scale(pb, 
						    img_hourglass[i], w, h);
    }
}

void
button_callback (MBTrayApp *app, int x, int y, Bool is_released )
{
  ; 				/* Nothing yet */
}

void
load_icons(void)
{
  int   i;
  char *icon_path;
  char  icon_name[128] = { 0 };
  
  for (i=0; i<HOURGLASS_N_FRAMES; i++)
    {
      if (img_hourglass[i] != NULL) 
	mb_pixbuf_img_free(pb, img_hourglass[i]);

      sprintf(icon_name, "hourglass-%i.png", i+1);

      icon_path = mb_dot_desktop_icon_get_full_path (ThemeName, 
						     32, 
						     icon_name);
      
      if (icon_path == NULL 
	  || !(img_hourglass[i] = mb_pixbuf_img_new_from_file(pb, icon_path)))
	{
	  fprintf(stderr, 
		  "mb-applet-startup-monitor: failed to load icon '%s'\n", 
		  icon_name );
	  exit(1);
	}
      free(icon_path);
    }
}

void 
theme_callback (MBTrayApp *app, char *theme_name)
{
  /* TODO
  if (!theme_name) return;
  if (ThemeName) free(ThemeName);
  ThemeName = strdup(theme_name);
  load_icons(); 	
  resize_callback (app, mb_tray_app_width(app), mb_tray_app_width(app) );
  */
}

void
timeout_callback ( MBTrayApp *app )
{
  LaunchList *item = launch_list;
  LaunchList *last_item = NULL;
  time_t t;

  if (!hourglass_shown) return;

  t = time (NULL);

  /* timeouts */
  while (item)
    {
      if ((item->when - t) <= 0)
	{
	  if (last_item == NULL)
	    launch_list = item->next;
	  else
	    last_item->next = item->next;
	  
	  free(item->id);
	  free(item);
	  
	  break;
	}
      last_item = item;
      item = item->next;
    }
  
  if (launch_list == NULL && hourglass_shown)
    {
      hide_hourglass ();
      return;
    }

  hourglass_cur_frame_n++;
  if (hourglass_cur_frame_n == 8)
    hourglass_cur_frame_n = 0;

  mb_tray_app_repaint (app);
}

void 
xevent_callback( MBTrayApp *mb_tray_app, XEvent *event ) 
{
  sn_display_process_event (display, event);
}

int
main (int argc, char **argv)
{
  struct timeval tv;

  app = mb_tray_app_new ( "Startup Monitor",
			  resize_callback,
			  paint_callback,
			  &argc,
			  &argv );  

  /* if (app == NULL) usage(); */

  
  pb = mb_pixbuf_new(mb_tray_app_xdisplay(app), 
		     mb_tray_app_xscreen(app));

  load_icons();

  XSelectInput (mb_tray_app_xdisplay(app), mb_tray_app_xrootwin(app), 
		PropertyChangeMask | StructureNotifyMask);

  mb_tray_app_set_xevent_callback (app, xevent_callback); 

  display = sn_display_new (mb_tray_app_xdisplay(app), NULL, NULL);

  context = sn_monitor_context_new (display, 
				    mb_tray_app_xscreen(app),
                                    monitor_event_func,
                                    NULL, NULL);  
  
  mb_tray_app_set_button_callback (app, button_callback );

  memset(&tv,0,sizeof(struct timeval));
  tv.tv_sec = 0;
  tv.tv_usec = 100000;

  mb_tray_app_set_timeout_callback (app, timeout_callback, &tv); 

  /* Add timeout call for failed launch apps */

  mb_tray_app_request_offset (app, -1);

  /* Dont save us in panel session */
  mb_tray_app_set_session (app, False); 

  mb_tray_app_hide (app);

  mb_tray_app_main (app);

  return 0;
}