#include <gtk/gtk.h>
gchar *incfiles="/usr/share/sdesk/func*";

static void run(gpointer s){
	if (!g_spawn_command_line_async(s,NULL)) {
		/* maybe do this only once in main? ... not in win32? */
		gchar *buf=g_strdup_printf("for x in %s;do [ -f \"$x\" ] && . $x;done;"
		"type %s >/dev/null 2>&1 && %s",incfiles,(char *)s,(char *)s);
		system(buf);
		g_free(buf);
	}
}

void key_press_event(GtkWidget *widget, GdkEventKey *event){
	g_print("KEY=%s\n",gdk_keyval_name(event->keyval));
}

void button_press_event(GtkWidget *widget, GdkEventButton *event){
	g_print("BUTTON=%d\nBUTTON_X=%d\nBUTTON_Y=%d\n",event->button,
	(unsigned int) event->x, (unsigned int)event->y);
}

static void leftclick(GtkStatusIcon *si, gpointer s){run(s);}
static void rightclick(GtkStatusIcon *si, guint b,guint a_t, gpointer s){run(s);}

/** refreshes the status icon image from file if it changes */
static void refresh(GFileMonitor *monitor, GFile *file, GFile *to_file, GFileMonitorEvent event, gpointer si){
	gtk_status_icon_set_from_file(si,g_file_get_path(file));
}


/** refreshes the background image from file if it changes */
static void bg_refresh(GFileMonitor *monitor, GFile *file, GFile *to_file, GFileMonitorEvent event, gpointer bg){
	gtk_image_set_from_file(GTK_IMAGE(bg),g_file_get_path(file));
}

/** gets a new mouse over tooltip from file if it changes */
static void updatetooltip(GFileMonitor *monitor,   GFile *file, GFile *to_file, GFileMonitorEvent event, gpointer si){
	gtk_status_icon_set_tooltip_markup(si, g_mapped_file_get_contents(g_mapped_file_new(g_file_get_path(file), FALSE, NULL)));
}

int main(int argc, char *argv[]){
	GtkStatusIcon *si;
	char i=1;
	gtk_init (&argc, &argv);
while(i<argc && argv[i][0]=='-'){
	switch(argv[i][1]){
	case 'i': /** setup include files **/
		incfiles=argv[++i];
		break;

	case 'b': /** setup desktop background image **/
		if (g_file_test(argv[++i], G_FILE_TEST_EXISTS)){
			GtkWidget *window, *image, *eventbox;
			window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
			gtk_window_set_default_size(GTK_WINDOW(window),gdk_screen_width(),gdk_screen_height());
			gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DESKTOP);
			gtk_window_set_focus_on_map (GTK_WINDOW (window), FALSE);
			eventbox = gtk_event_box_new ();
			image = gtk_image_new_from_file(argv[i]);
			gtk_container_add(GTK_CONTAINER(window), eventbox);
			gtk_container_add(GTK_CONTAINER(eventbox), image);
			gtk_widget_show_all(window);
			g_signal_connect((gpointer)window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
			g_signal_connect((gpointer) window, "button_press_event", G_CALLBACK(button_press_event), NULL);
			g_signal_connect ((gpointer) window, "key_press_event", G_CALLBACK (key_press_event), NULL);
			g_signal_connect(g_file_monitor_file(g_file_new_for_path(argv[i]), G_FILE_MONITOR_NONE, FALSE, NULL),
				"changed", G_CALLBACK(bg_refresh),(gpointer) image);
		}else{
			g_printerr("error: could not stat file %s\n",argv[i]);
			return 2;
		}
		break;

	case 't' : /** loop through icon, tooltip, click messages **/
		/** status icon **/
		if (g_file_test(argv[++i], G_FILE_TEST_EXISTS)){
			si = gtk_status_icon_new_from_file(argv[i]);
			g_signal_connect(g_file_monitor_file(g_file_new_for_path(argv[i]), G_FILE_MONITOR_NONE, FALSE, NULL),
				"changed", G_CALLBACK(refresh),(gpointer) si);
		}else{
			g_printerr("error: could not stat file %s\n",argv[i]);
			return 2;
		}
		/** tooltip **/
		if (g_file_test(argv[++i], G_FILE_TEST_EXISTS)){
			gtk_status_icon_set_tooltip_markup(si, g_mapped_file_get_contents(g_mapped_file_new(argv[i], FALSE, NULL)));
			g_signal_connect(g_file_monitor_file(g_file_new_for_path(argv[i]), G_FILE_MONITOR_NONE, FALSE, NULL),
				"changed", G_CALLBACK(updatetooltip),(gpointer) si);
		}else if (argv[i]!=NULL) gtk_status_icon_set_tooltip_markup(si, argv[i]);
		/** left click action **/
		if (argv[++i]!=NULL) g_signal_connect(G_OBJECT(si), "activate", G_CALLBACK(leftclick),(gpointer) argv[i]);
		/** right click action **/
		if (argv[++i]!=NULL) g_signal_connect(G_OBJECT(si), "popup-menu", G_CALLBACK(rightclick), (gpointer) argv[i]);
		break;

	default :
		g_printerr(
		"usage:\n%s [-i files] [-b] -t image tooltip left-action right-action ...\n\n"
		"  -i           full path to file(s) with shell functions to include\n"
		"                   by default it looks in /usr/share/sit/func*\n"
		"  -b <file>    use image file as desktop background image\n"
		"  -t           tray icon: must have the following parameters\n"
		"  image        full path to image file\n"
		"  tooltip      full path to tooltip file or a \"quoted tooltip\"\n"
		"  left-action  action to run when left mouse button is clicked\n"
		"  right-action action to run when right mouse button is clicked\n\n"
		,argv[0]);
		return 1;
	}
	++i;
}

gtk_main();
}
