all repos — tint2 @ df241dfd895a0b4332672557c90aa6f5b97b9527

fork of the tint2 desktop panel for my custom setup - only minimized windows across all desktops for the taskbar

src/execplugin/execplugin.c (raw)

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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
#include "execplugin.h"

#include <string.h>
#include <stdio.h>
#include <cairo.h>
#include <cairo-xlib.h>
#include <math.h>
#include <pango/pangocairo.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <fcntl.h>

#include "window.h"
#include "server.h"
#include "panel.h"
#include "timer.h"
#include "common.h"

void execp_timer_callback(void *arg);
char *execp_get_tooltip(void *obj);
void execp_init_fonts();

void default_execp()
{
}

Execp *create_execp()
{
	Execp *execp = calloc(1, sizeof(Execp));
	execp->backend = calloc(1, sizeof(ExecpBackend));
	execp->backend->child_pipe = -1;

	execp->backend->interval = 30;
	execp->backend->cache_icon = TRUE;
	execp->backend->centered = TRUE;
	execp->backend->font_color.alpha = 0.5;

	return execp;
}

gpointer create_execp_frontend(gconstpointer arg, gpointer data)
{
	Execp *execp_backend = (Execp *)arg;

	Execp *execp_frontend = calloc(1, sizeof(Execp));
	execp_frontend->backend = execp_backend->backend;
	execp_backend->backend->instances = g_list_append(execp_backend->backend->instances, execp_frontend);
	execp_frontend->frontend = calloc(1, sizeof(ExecpFrontend));
	return execp_frontend;
}

void destroy_execp(void *obj)
{
	Execp *execp = (Execp *)obj;
	if (execp->frontend) {
		// This is a frontend element
		execp->backend->instances = g_list_remove_all(execp->backend->instances, execp);
		free_and_null(execp->frontend);
	} else {
		// This is a backend element
		stop_timeout(execp->backend->timer);
		execp->backend->timer = NULL;

		if (execp->backend->icon) {
			imlib_context_set_image(execp->backend->icon);
			imlib_free_image();
			execp->backend->icon = NULL;
		}
		free_and_null(execp->backend->buf_output);
		free_and_null(execp->backend->text);
		free_and_null(execp->backend->icon_path);
		if (execp->backend->child) {
			kill(-execp->backend->child, SIGHUP);
			execp->backend->child = 0;
		}
		if (execp->backend->child_pipe >= 0) {
			close(execp->backend->child_pipe);
			execp->backend->child_pipe = -1;
		}

		execp->backend->bg = NULL;
		pango_font_description_free(execp->backend->font_desc);
		execp->backend->font_desc = NULL;
		free_and_null(execp->backend->command);
		free_and_null(execp->backend->tooltip);
		free_and_null(execp->backend->lclick_command);
		free_and_null(execp->backend->mclick_command);
		free_and_null(execp->backend->rclick_command);
		free_and_null(execp->backend->dwheel_command);
		free_and_null(execp->backend->uwheel_command);

		if (execp->backend->instances) {
			fprintf(stderr, "Error: Attempt to destroy backend while there are still frontend instances!\n");
			exit(-1);
		}
		free(execp->backend);
		free(execp);
	}
}

void init_execp()
{
	GList *to_remove = panel_config.execp_list;
	for (int k = 0; k < strlen(panel_items_order) && to_remove; k++) {
		if (panel_items_order[k] == 'E') {
			to_remove = to_remove->next;
		}
	}

	if (to_remove) {
		if (to_remove == panel_config.execp_list) {
			g_list_free_full(to_remove, destroy_execp);
			panel_config.execp_list = NULL;
		} else {
			// Cut panel_config.execp_list
			if (to_remove->prev)
				to_remove->prev->next = NULL;
			to_remove->prev = NULL;
			// Remove all elements of to_remove and to_remove itself
			g_list_free_full(to_remove, destroy_execp);
		}
	}

	execp_init_fonts();
	for (GList *l = panel_config.execp_list; l; l = l->next) {
		Execp *execp = l->data;

		// Set missing config options
		if (!execp->backend->bg)
			execp->backend->bg = &g_array_index(backgrounds, Background, 0);
		execp->backend->buf_capacity = 1024;
		execp->backend->buf_output = calloc(execp->backend->buf_capacity, 1);
		execp->backend->text = strdup(" ");
		execp->backend->icon_path = NULL;
	}
}

void init_execp_panel(void *p)
{
	Panel *panel = (Panel *)p;

	// Make sure this is only done once if there are multiple items
	if (panel->execp_list && ((Execp *)panel->execp_list->data)->frontend)
		return;

	// panel->execp_list is now a copy of the pointer panel_config.execp_list
	// We make it a deep copy
	panel->execp_list = g_list_copy_deep(panel_config.execp_list, create_execp_frontend, NULL);

	for (GList *l = panel->execp_list; l; l = l->next) {
		Execp *execp = l->data;
		execp->area.bg = execp->backend->bg;
		execp->area.paddingx = execp->backend->paddingx;
		execp->area.paddingy = execp->backend->paddingy;
		execp->area.paddingxlr = execp->backend->paddingxlr;

		execp->area.parent = panel;
		execp->area.panel = panel;
		execp->area._draw_foreground = draw_execp;
		execp->area.size_mode = LAYOUT_FIXED;
		execp->area._resize = resize_execp;
		execp->area._get_tooltip_text = execp_get_tooltip;
		execp->area._is_under_mouse = full_width_area_is_under_mouse;
		execp->area.has_mouse_press_effect = panel_config.mouse_effects && (execp->area.has_mouse_over_effect =
				execp->backend->lclick_command || execp->backend->mclick_command || execp->backend->rclick_command ||
				execp->backend->uwheel_command || execp->backend->dwheel_command);

		execp->area.resize_needed = TRUE;
		execp->area.on_screen = TRUE;

		if (!execp->backend->timer)
			execp->backend->timer = add_timeout(10, 0, execp_timer_callback, execp, &execp->backend->timer);
	}
}

void execp_init_fonts()
{
	for (GList *l = panel_config.execp_list; l; l = l->next) {
		Execp *execp = l->data;
		if (!execp->backend->font_desc)
			execp->backend->font_desc = pango_font_description_from_string(get_default_font());
	}
}

void execp_default_font_changed()
{
	gboolean needs_update = FALSE;
	for (GList *l = panel_config.execp_list; l; l = l->next) {
		Execp *execp = l->data;

		if (!execp->backend->has_font) {
			pango_font_description_free(execp->backend->font_desc);
			execp->backend->font_desc = NULL;
			needs_update = TRUE;
		}
	}
	if (!needs_update)
		return;

	execp_init_fonts();
	for (int i = 0; i < num_panels; i++) {
		for (GList *l = panels[i].execp_list; l; l = l->next) {
			Execp *execp = l->data;

			if (!execp->backend->has_font) {
				execp->area.resize_needed = TRUE;
				schedule_redraw(&execp->area);
			}
		}
	}
	panel_refresh = TRUE;
}

void cleanup_execp()
{
	// Cleanup frontends
	for (int i = 0; i < num_panels; i++) {
		g_list_free_full(panels[i].execp_list, destroy_execp);
		panels[i].execp_list = NULL;
	}

	// Cleanup backends
	g_list_free_full(panel_config.execp_list, destroy_execp);
	panel_config.execp_list = NULL;
}

// Called from backend functions.
gboolean reload_icon(Execp *execp)
{
	char *icon_path = execp->backend->icon_path;

	if (execp->backend->has_icon && icon_path) {
		if (execp->backend->icon) {
			imlib_context_set_image(execp->backend->icon);
			imlib_free_image();
		}
		execp->backend->icon = load_image(icon_path, execp->backend->cache_icon);
		if (execp->backend->icon) {
			imlib_context_set_image(execp->backend->icon);
			int w = imlib_image_get_width();
			int h = imlib_image_get_height();
			if (w && h) {
				if (execp->backend->icon_w) {
					if (!execp->backend->icon_h) {
						h = (int)(0.5 + h * execp->backend->icon_w / (float)(w));
						w = execp->backend->icon_w;
					} else {
						w = execp->backend->icon_w;
						h = execp->backend->icon_h;
					}
				} else {
					if (execp->backend->icon_h) {
						w = (int)(0.5 + w * execp->backend->icon_h / (float)(h));
						h = execp->backend->icon_h;
					}
				}
				if (w < 1)
					w = 1;
				if (h < 1)
					h = 1;
			}
			if (w != imlib_image_get_width() || h != imlib_image_get_height()) {
				Imlib_Image icon_scaled =
					imlib_create_cropped_scaled_image(0, 0, imlib_image_get_width(), imlib_image_get_height(), w, h);
				imlib_context_set_image(execp->backend->icon);
				imlib_free_image();
				execp->backend->icon = icon_scaled;
			}
			return TRUE;
		}
	}
	return FALSE;
}

gboolean resize_execp(void *obj)
{
	Execp *execp = obj;
	Panel *panel = execp->area.panel;
	int horiz_padding = (panel_horizontal ? execp->area.paddingxlr : execp->area.paddingy);
	int vert_padding = (panel_horizontal ? execp->area.paddingy : execp->area.paddingxlr);
	int interior_padding = execp->area.paddingx;

	schedule_redraw(&execp->area);

	int icon_w, icon_h;
	if (reload_icon(execp)) {
		if (execp->backend->icon) {
			imlib_context_set_image(execp->backend->icon);
			icon_w = imlib_image_get_width();
			icon_h = imlib_image_get_height();
		} else {
			icon_w = icon_h = 0;
		}
	} else {
		icon_w = icon_h = 0;
	}

	int text_next_line = !panel_horizontal && icon_w > execp->area.width / 2;

	int txt_height_ink, txt_height, txt_width;
	if (panel_horizontal) {
		get_text_size2(execp->backend->font_desc,
					   &txt_height_ink,
					   &txt_height,
					   &txt_width,
					   panel->area.height,
					   panel->area.width,
					   execp->backend->text,
					   strlen(execp->backend->text),
					   PANGO_WRAP_WORD_CHAR,
					   PANGO_ELLIPSIZE_NONE,
					   execp->backend->has_markup);
	} else {
		get_text_size2(execp->backend->font_desc,
					   &txt_height_ink,
					   &txt_height,
					   &txt_width,
					   panel->area.height,
					   !text_next_line
						   ? execp->area.width - icon_w - (icon_w ? interior_padding : 0) -
								 2 * (horiz_padding + execp->area.bg->border.width)
						   : execp->area.width - 2 * (horiz_padding + execp->area.bg->border.width),
					   execp->backend->text,
					   strlen(execp->backend->text),
					   PANGO_WRAP_WORD_CHAR,
					   PANGO_ELLIPSIZE_NONE,
					   execp->backend->has_markup);
	}

	gboolean result = FALSE;
	if (panel_horizontal) {
		int new_size = txt_width;
		if (icon_w)
			new_size += interior_padding + icon_w;
		new_size += 2 * (horiz_padding + execp->area.bg->border.width);
		if (new_size > execp->area.width || new_size < (execp->area.width - 6)) {
			// we try to limit the number of resize
			execp->area.width = new_size + 1;
			result = TRUE;
		}
	} else {
		int new_size;
		if (!text_next_line) {
			new_size = txt_height + (2 * (vert_padding + execp->area.bg->border.width));
			if (new_size < icon_h + (2 * (vert_padding + execp->area.bg->border.width))) {
				new_size = icon_h + (2 * (vert_padding + execp->area.bg->border.width));
			}
		} else {
			new_size = icon_h + interior_padding + txt_height + (2 * (vert_padding + execp->area.bg->border.width));
		}
		if (new_size != execp->area.height) {
			execp->area.height = new_size;
			result = TRUE;
		}
	}
	execp->frontend->textw = txt_width;
	execp->frontend->texth = txt_height;
	if (execp->backend->centered) {
		if (icon_w) {
			if (!text_next_line) {
				execp->frontend->icony = (execp->area.height - icon_h) / 2;
				execp->frontend->iconx = (execp->area.width - txt_width - interior_padding - icon_w) / 2;
				execp->frontend->texty = (execp->area.height - txt_height) / 2;
				execp->frontend->textx = execp->frontend->iconx + icon_w + interior_padding;
			} else {
				execp->frontend->icony = (execp->area.height - icon_h - interior_padding - txt_height) / 2;
				execp->frontend->iconx = (execp->area.width - icon_w) / 2;
				execp->frontend->texty = execp->frontend->icony + icon_h + interior_padding;
				execp->frontend->textx = (execp->area.width - txt_width) / 2;
			}
		} else {
			execp->frontend->texty = (execp->area.height - txt_height) / 2;
			execp->frontend->textx = (execp->area.width - txt_width) / 2;
		}
	} else {
		if (icon_w) {
			if (!text_next_line) {
				execp->frontend->icony = (execp->area.height - icon_h) / 2;
				execp->frontend->iconx = execp->area.bg->border.width + horiz_padding;
				execp->frontend->texty = (execp->area.height - txt_height) / 2;
				execp->frontend->textx = execp->frontend->iconx + icon_w + interior_padding;
			} else {
				execp->frontend->icony = (execp->area.height - icon_h - interior_padding - txt_height) / 2;
				execp->frontend->iconx = execp->area.bg->border.width + horiz_padding;
				execp->frontend->texty = execp->frontend->icony + icon_h + interior_padding;
				execp->frontend->textx = execp->frontend->iconx;
			}
		} else {
			execp->frontend->texty = (execp->area.height - txt_height) / 2;
			execp->frontend->textx = execp->area.bg->border.width + horiz_padding;
		}
	}

	return result;
}

void draw_execp(void *obj, cairo_t *c)
{
	Execp *execp = obj;
	PangoLayout *layout = pango_cairo_create_layout(c);

	if (execp->backend->has_icon && execp->backend->icon) {
		imlib_context_set_image(execp->backend->icon);
		// Render icon
		render_image(execp->area.pix, execp->frontend->iconx, execp->frontend->icony);
	}

	// draw layout
	pango_layout_set_font_description(layout, execp->backend->font_desc);
	pango_layout_set_width(layout, execp->frontend->textw * PANGO_SCALE);
	pango_layout_set_alignment(layout, execp->backend->centered ? PANGO_ALIGN_CENTER : PANGO_ALIGN_LEFT);
	pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
	pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE);
	if (!execp->backend->has_markup)
		pango_layout_set_text(layout, execp->backend->text, strlen(execp->backend->text));
	else
		pango_layout_set_markup(layout, execp->backend->text, strlen(execp->backend->text));

	pango_cairo_update_layout(c, layout);
	draw_text(layout,
			  c,
			  execp->frontend->textx,
			  execp->frontend->texty,
			  &execp->backend->font_color,
			  panel_config.font_shadow);

	g_object_unref(layout);
}

void execp_action(void *obj, int button, int x, int y)
{
	Execp *execp = obj;
	char *command = NULL;
	switch (button) {
	case 1:
		command = execp->backend->lclick_command;
		break;
	case 2:
		command = execp->backend->mclick_command;
		break;
	case 3:
		command = execp->backend->rclick_command;
		break;
	case 4:
		command = execp->backend->uwheel_command;
		break;
	case 5:
		command = execp->backend->dwheel_command;
		break;
	}
	if (command) {
		char *full_cmd = g_strdup_printf("export EXECP_X=%d;"
		                                 "export EXECP_Y=%d;"
		                                 "export EXECP_W=%d;"
		                                 "export EXECP_H=%d; %s",
		                                 x,
		                                 y,
		                                 execp->area.width,
		                                 execp->area.height,
		                                 command);
		tint_exec(full_cmd);
		g_free(full_cmd);
	} else {
		if (execp->backend->child_pipe > 0) {
			// Command currently running, nothing to do
		} else {
			if (execp->backend->timer)
				stop_timeout(execp->backend->timer);
			// Run command right away
			execp->backend->timer = add_timeout(10, 0, execp_timer_callback, execp, &execp->backend->timer);
		}
	}
}

void execp_timer_callback(void *arg)
{
	Execp *execp = arg;

	if (!execp->backend->command)
		return;

	// Still running!
	if (execp->backend->child_pipe > 0)
		return;

	int pipe_fd[2];
	if (pipe(pipe_fd)) {
		// TODO maybe write this in tooltip, but if this happens we're screwed anyways
		fprintf(stderr, "Execp: Creating pipe failed!\n");
		return;
	}

	fcntl(pipe_fd[0], F_SETFL, O_NONBLOCK | fcntl(pipe_fd[0], F_GETFL));

	// Fork and run command, capturing stdout in pipe
	pid_t child = fork();
	if (child == -1) {
		// TODO maybe write this in tooltip, but if this happens we're screwed anyways
		fprintf(stderr, "Fork failed.\n");
		close(pipe_fd[1]);
		close(pipe_fd[0]);
		return;
	} else if (child == 0) {
		// We are in the child
		close(pipe_fd[0]);
		dup2(pipe_fd[1], 1); // 1 is stdout
		close(pipe_fd[1]);
		setpgid(0, 0);
		execl("/bin/sh", "/bin/sh", "-c", execp->backend->command, NULL);
		// This should never happen!
		fprintf(stdout, "execl() failed\nexecl() failed\n");
		fflush(stdout);
		exit(0);
	}
	close(pipe_fd[1]);
	execp->backend->child = child;
	execp->backend->child_pipe = pipe_fd[0];
	execp->backend->buf_length = 0;
	execp->backend->buf_output[execp->backend->buf_length] = '\0';
	execp->backend->last_update_start_time = time(NULL);
}

gboolean read_execp(void *obj)
{
	Execp *execp = (Execp *)obj;

	if (execp->backend->child_pipe < 0)
		return FALSE;

	gboolean command_finished = FALSE;
	while (1) {
		// Make sure there is free space in the buffer
		if (execp->backend->buf_capacity - execp->backend->buf_length < 1024) {
			execp->backend->buf_capacity *= 2;
			execp->backend->buf_output = realloc(execp->backend->buf_output, execp->backend->buf_capacity);
		}
		ssize_t count = read(execp->backend->child_pipe,
							 execp->backend->buf_output + execp->backend->buf_length,
							 execp->backend->buf_capacity - execp->backend->buf_length - 1);
		if (count > 0) {
			// Successful read
			execp->backend->buf_length += count;
			execp->backend->buf_output[execp->backend->buf_length] = '\0';
			continue;
		} else if (count == 0) {
			// End of file
			command_finished = TRUE;
			break;
		} else if (errno == EAGAIN || errno == EWOULDBLOCK) {
			// No more data available at the moment
			break;
		} else if (errno == EINTR) {
			// Harmless interruption by signal
			continue;
		} else {
			// Error
			command_finished = TRUE;
			break;
		}
		break;
	}

	if (command_finished) {
		execp->backend->child = 0;
		close(execp->backend->child_pipe);
		execp->backend->child_pipe = -1;
		if (execp->backend->interval)
			execp->backend->timer =
				add_timeout(execp->backend->interval * 1000, 0, execp_timer_callback, execp, &execp->backend->timer);
	}

	if (!execp->backend->continuous && command_finished) {
		free_and_null(execp->backend->text);
		free_and_null(execp->backend->icon_path);
		if (!execp->backend->has_icon) {
			execp->backend->text = strdup(execp->backend->buf_output);
		} else {
			char *text = strchr(execp->backend->buf_output, '\n');
			if (text) {
				*text = '\0';
				text++;
				execp->backend->text = strdup(text);
			} else {
				execp->backend->text = strdup("");
			}
			execp->backend->icon_path = strdup(execp->backend->buf_output);
		}
		int len = strlen(execp->backend->text);
		if (len > 0 && execp->backend->text[len - 1] == '\n')
			execp->backend->text[len - 1] = '\0';
		execp->backend->buf_length = 0;
		execp->backend->buf_output[execp->backend->buf_length] = '\0';
		execp->backend->last_update_finish_time = time(NULL);
		execp->backend->last_update_duration =
			execp->backend->last_update_finish_time - execp->backend->last_update_start_time;
		return TRUE;
	} else if (execp->backend->continuous > 0) {
		// Count lines in buffer
		int num_lines = 0;
		char *last = execp->backend->buf_output;
		char *end = NULL;
		for (char *c = execp->backend->buf_output; *c; c++) {
			if (*c == '\n') {
				num_lines++;
				if (num_lines == execp->backend->continuous)
					end = c;
			}
			last = c;
		}
		if (*last && *last != '\n')
			num_lines++;
		if (num_lines >= execp->backend->continuous) {
			if (end)
				*end = '\0';
			free_and_null(execp->backend->text);
			free_and_null(execp->backend->icon_path);
			if (!execp->backend->has_icon) {
				execp->backend->text = strdup(execp->backend->buf_output);
			} else {
				char *text = strchr(execp->backend->buf_output, '\n');
				if (text) {
					*text = '\0';
					text++;
					execp->backend->text = strdup(text);
				} else {
					execp->backend->text = strdup("");
				}
				execp->backend->icon_path = strdup(execp->backend->buf_output);
			}
			int len = strlen(execp->backend->text);
			if (len > 0 && execp->backend->text[len - 1] == '\n')
				execp->backend->text[len - 1] = '\0';

			if (end) {
				char *next = end + 1;
				int copied = next - execp->backend->buf_output;
				int remaining = execp->backend->buf_length - copied;
				if (remaining > 0) {
					memmove(execp->backend->buf_output, next, remaining);
					execp->backend->buf_length = remaining;
					execp->backend->buf_output[execp->backend->buf_length] = '\0';
				} else {
					execp->backend->buf_length = 0;
					execp->backend->buf_output[execp->backend->buf_length] = '\0';
				}
			}

			execp->backend->last_update_finish_time = time(NULL);
			execp->backend->last_update_duration =
				execp->backend->last_update_finish_time - execp->backend->last_update_start_time;
			return TRUE;
		}
	}
	return FALSE;
}

const char *time_to_string(int seconds, char *buffer)
{
	if (seconds < 60) {
		sprintf(buffer, "%ds", seconds);
	} else if (seconds < 60 * 60) {
		int m = seconds / 60;
		seconds = seconds % 60;
		int s = seconds;
		sprintf(buffer, "%d:%ds", m, s);
	} else {
		int h = seconds / (60 * 60);
		seconds = seconds % (60 * 60);
		int m = seconds / 60;
		seconds = seconds % 60;
		int s = seconds;
		sprintf(buffer,
				"%d:%d:%ds",
				h,
				m,
				s);
	}
	return buffer;
}

char *execp_get_tooltip(void *obj)
{
	Execp *execp = obj;

	if (execp->backend->tooltip) {
		if (strlen(execp->backend->tooltip) > 0)
			return strdup(execp->backend->tooltip);
		else
			return NULL;
	}

	time_t now = time(NULL);

	char tmp_buf1[256];
	char tmp_buf2[256];
	char tmp_buf3[256];
	if (execp->backend->child_pipe < 0) {
		// Not executing command
		if (execp->backend->last_update_finish_time) {
			// We updated at least once
			if (execp->backend->interval > 0) {
				sprintf(execp->backend->tooltip_text,
						"Last update finished %s ago (took %s). Next update starting in %s.",
						time_to_string((int)(now - execp->backend->last_update_finish_time), tmp_buf1),
						time_to_string((int)execp->backend->last_update_duration, tmp_buf2),
						time_to_string((int)(execp->backend->interval - (now - execp->backend->last_update_finish_time)),
									   tmp_buf3));
			} else {
				sprintf(execp->backend->tooltip_text,
						"Last update finished %s ago (took %s).",
						time_to_string((int)(now - execp->backend->last_update_finish_time), tmp_buf1),
						time_to_string((int)execp->backend->last_update_duration, tmp_buf2));
			}
		} else {
			// we never requested an update
			sprintf(execp->backend->tooltip_text, "Never updated. No update scheduled.");
		}
	} else {
		// Currently executing command
		if (execp->backend->last_update_finish_time) {
			// we finished updating at least once
			sprintf(execp->backend->tooltip_text,
					"Last update finished %s ago. Update in progress (started %s ago).",
					time_to_string((int)(now - execp->backend->last_update_finish_time), tmp_buf1),
					time_to_string((int)(now - execp->backend->last_update_start_time), tmp_buf3));
		} else {
			// we never finished an update
			sprintf(execp->backend->tooltip_text,
					"First update in progress (started %s seconds ago).",
					time_to_string((int)(now - execp->backend->last_update_start_time), tmp_buf1));
		}
	}
	return strdup(execp->backend->tooltip_text);
}