Make an Alarmclock with the at Command and a shell, Part II

The alarmclock from last time, the one build with at, needs some more tinkering, I’m afraid. The argument to Joachim Breitner’s screen-messenger sm gets evaluated too early. The argument given is the date command which means it prints a wrong date/time. The reason for this bug is based on some impatience of the author who tested it with only a minute to wait so differences between the printed time and the actual time were not very visible.

I tried to repair it—to no avail. There might be an easy and fast solution but I didn’t find it and was too impatience to search half of the internet for an answer. I just hacked the program itself. Because of my inability to use diff correctly I put the whole listing of the two functions I changed after the fold.Basis was the version 0.20

static struct option const long_options[] =
{
	{"help",       no_argument,       NULL, 'h'},
	{"version",    no_argument,       NULL, 'V'},
	{"date",       no_argument,       NULL, 'd'},
	{"foreground", required_argument, NULL, 'f'},
	{"background", required_argument, NULL, 'b'},
	{"font",       required_argument, NULL, 'n'},
	{"rotate",     required_argument, NULL, 'r'},
	{0,0,0,0}
};
static void usage(char *cmd) {
	printf("Usage: %s [-h|--help] [-V|--version] [-d|--date] [-f|--foreground=colordesc] [-b|--background=colordesc] [-n|--font=fontdesc] [-r|--rotate=0,1,2,3]\n", cmd);
}

And in main()

int main(int argc, char **argv) {
	GString *input;
        /* For the date */
	char     buf[256];
	time_t t;
	struct tm *tmp;

	int c;
	int input_provided = 0;
        int print_date = 0;

	while ((c = getopt_long (argc, argv, "hVdf:b:n:r:", long_options, (int *) 0)) != EOF) {
		switch (c) {
			case 'h':
				usage(argv[0]);
				return 0;
				break;

			case 'V':
				version();
				return 0;
				break;

			case 'd':
				print_date = 1;
				break;

			case 'f':
				foreground = optarg;
				break;

			case 'b':
				background = optarg;
				break;

			case 'n':
				fontdesc = optarg;
				break;
			case 'r':
				rotation = atoi(optarg);
				break;
			default:
				/* unknown switch received - at least
				 * give usage but continue and use the
				 * data */
				usage(argv[0]);
				break;
		}
	}

	gtk_init(&argc, &argv);

	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
	gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
	gtk_window_set_icon_name (GTK_WINDOW (window), "sm");

	GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(window));
	int w = gdk_screen_get_width(screen);
	int h = gdk_screen_get_height(screen);

	GdkGeometry geometry;
	geometry.min_width = w;
	geometry.min_height = h;
	geometry.max_width = w;
	geometry.max_height = h;
	geometry.base_width = w;
	geometry.base_height = h;
	geometry.win_gravity = GDK_GRAVITY_STATIC;
	gtk_window_set_geometry_hints(GTK_WINDOW(window), window, &geometry,
				      GDK_HINT_MIN_SIZE |
				      GDK_HINT_MAX_SIZE |
				      GDK_HINT_BASE_SIZE |
				      GDK_HINT_WIN_GRAVITY |
				      GDK_HINT_USER_POS);
	gtk_window_set_default_size(GTK_WINDOW(window), w, h);
	gtk_window_move(GTK_WINDOW(window), 0, 0);


	g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

	settings = gtk_settings_get_default();
	GdkColormap *colormap = gtk_widget_get_colormap(GTK_WIDGET(window));
	GdkColor white, black;
	gdk_colormap_alloc_color(colormap, &white, TRUE, TRUE);
	gdk_colormap_alloc_color(colormap, &black, TRUE, TRUE);
	if (foreground != NULL) {
		gdk_color_parse(foreground, &black);
	} else {
		gdk_color_parse("black", &black);
	}
	if (background != NULL) {
		gdk_color_parse(background, &white);
	} else {
		gdk_color_parse("white", &white);
	}

	draw = gtk_drawing_area_new();
	gtk_widget_set_events(draw, GDK_BUTTON_PRESS_MASK|GDK_KEY_PRESS_MASK);
	gtk_widget_set_size_request(draw,400,400);
	gtk_widget_modify_bg(draw, GTK_STATE_NORMAL, &white);
	gtk_widget_modify_fg(draw, GTK_STATE_NORMAL, &black);
	g_signal_connect(G_OBJECT(draw), "button-press-event", G_CALLBACK(text_clicked), NULL);
	g_signal_connect(G_OBJECT(draw), "key-press-event", G_CALLBACK(text_keypress), NULL);
	gtk_widget_set_can_focus(draw, TRUE);

	GdkPixmap *pixmap = gdk_pixmap_new(NULL, 1, 1, 1);
	GdkColor color;
	cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 0, 0);

	tv = gtk_text_view_new();
	tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));

	partial_input = g_string_new("");

	if (argc > optind)
		if (!strcmp(argv[optind], "-") ) {
			// read from stdin
			GIOChannel *chan = g_io_channel_unix_new(0);
			g_io_channel_set_flags(chan, G_IO_FLAG_NONBLOCK, NULL);
			g_io_add_watch (chan, G_IO_IN | G_IO_HUP, &read_chan, NULL);

			input = g_string_new("");
			input_provided++;
		} else {
			int i;

			input = g_string_new("");

			for (i = optind; i < argc; i++) {
				g_string_append(input, argv[i]);

				if (i < argc - 1) {
					g_string_append(input, " ");
				}
			}
			input_provided++;
		}
	else{
		input = g_string_new(";-)");
	}
	if(print_date == 1){
		if(input_provided == 0)
			input = g_string_new("");
		else
			g_string_append(input,"\n");
		/* Lowest common denominator: Standard C */
		t = time(NULL);
		tmp = localtime(&t);
		strftime(buf, 256, "%R\n%d.%m.%y", tmp);
		g_string_append(input,buf);		
	}
	gtk_text_buffer_set_text(tb, input->str, input->len);
	g_string_free(input, TRUE);
	GtkTextIter start, end;
	gtk_text_buffer_get_bounds(tb, &start, &end);
	gtk_text_buffer_select_range(tb, &start, &end);

	quit = gtk_button_new_from_stock(GTK_STOCK_QUIT);
	g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);

	GtkWidget *vbox_button = gtk_vbox_new(FALSE, 0);
	gtk_box_pack_end(GTK_BOX(vbox_button), quit, FALSE, FALSE, 0);

	GtkWidget *hbox = gtk_hbox_new(FALSE,0);
	entry_widget = hbox;
	gtk_box_pack_start(GTK_BOX(hbox), tv,   TRUE,  TRUE,  0);
	gtk_box_pack_start(GTK_BOX(hbox), vbox_button, FALSE, FALSE, 0);

	GtkWidget *vbox = gtk_vbox_new(FALSE,0);
	gtk_box_pack_start(GTK_BOX(vbox), draw, TRUE, TRUE, 0);
	gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);

	gtk_container_add(GTK_CONTAINER(window), vbox);

	font = pango_font_description_new();
	if (fontdesc != NULL) {
		pango_font_description_set_family(font, fontdesc);
	} else {
		pango_font_description_set_family(font, "sans-serif");
	}
	pango_font_description_set_size(font, 200*PANGO_SCALE);

	layout = gtk_widget_create_pango_layout(window,get_text());
	pango_layout_set_font_description(layout, font);
	pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);

	GtkAccelGroup *accel = gtk_accel_group_new();
	guint key;
	GdkModifierType mod;
	gtk_accelerator_parse("<Ctrl>Q", &key, &mod);
	gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
	gtk_accelerator_parse("Escape", &key, &mod);
	gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(clear_text), NULL, NULL));
	gtk_window_add_accel_group(GTK_WINDOW(window), accel);
	gtk_widget_show_all(window);

	g_signal_connect_after(G_OBJECT(draw), "expose-event", G_CALLBACK(redraw), NULL);
	text_change_handler = g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext_show_input), NULL);
	g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext), NULL);
	g_signal_connect(G_OBJECT(tv), "move-cursor", G_CALLBACK(move_cursor), NULL);

	if (!input_provided)
		show_entry();
	else
		hide_entry(NULL);

	gtk_main();

	return 0;
}

The tarball will install the binary in /usr/local/games where Debian Squeeze has it installed in /usr/games so a change in the shell script is needed beside the change of the arguments to sm:

#!/bin/sh
if [ $# -eq 0 ];then
  echo "Usage: $0 [in|at] time"
  exit
fi
xhost +localhost
if [ $1 = "in" ];then
  shift
  echo "env DISPLAY=$DISPLAY /usr/local/games/sm -d" | at now + $*
elif [ $1 = "at" ];then
  shift
  echo "env DISPLAY=$DISPLAY /usr/local/games/sm -d" | at $*
else
  echo "Usage: $0 [in|at] time"
  exit
fi
exit
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s