File: sm.c

package info (click to toggle)
screen-message 0.23-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 736 kB
  • ctags: 119
  • sloc: sh: 1,103; ansic: 460; python: 95; makefile: 43
file content (439 lines) | stat: -rw-r--r-- 12,267 bytes parent folder | download
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
/*
#     sm.c
#     Copyright (C) 2006 - 2014 Joachim Breitner
#
#     This program is free software; you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation; either version 2 of the License, or
#     (at your option) any later version.
#
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with this program; if not, write to the Free Software
#     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
*/

#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <pango/pango.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "config.h"

#ifndef _GNU_SOURCE
#define _GNU_SOURCE                             /* for getopt_long */
#endif
#include <getopt.h>

#define min(x,y) ((x) < (y) ? (x) : (y))

#define AUTOHIDE_TIMEOUT 3

static int timeout_id=0;

static GtkWidget* window;
static GtkWidget* draw;
static GdkCursor *cursor;
static GtkWidget* quit;
static GtkWidget* tv;
static GtkWidget* entry_widget;
static GtkTextBuffer* tb;
static PangoFontDescription *font;
static char *foreground = NULL;
static char *background = NULL;
static char *fontdesc = NULL;
static int rotation = 0; // 0 = normal, 1 = left, 2 = inverted, 3 = right
static int alignment = 0; // 0 = centered, 1 = left-aligned, 2 = right-aligned
static GString *partial_input;
static gulong text_change_handler;

gboolean hide_entry(gpointer *user_data) {
	timeout_id = 0;
	gtk_widget_hide(entry_widget);
	gtk_widget_grab_focus(draw);
	gtk_widget_queue_draw(draw);
	gdk_window_set_cursor(gtk_widget_get_window(GTK_WIDGET(draw)), cursor);
	return FALSE;
}

static void show_entry() {
	if (timeout_id) {
		g_source_remove(timeout_id);
		timeout_id = 0;
	}
	gtk_widget_show(entry_widget);
	gtk_widget_grab_focus(tv);
	gdk_window_set_cursor(gtk_widget_get_window(GTK_WIDGET(draw)), NULL);

	timeout_id = g_timeout_add_seconds (AUTOHIDE_TIMEOUT, (GSourceFunc)hide_entry, NULL);
}

static void clear_text(GtkAccelGroup *accel, GObject *window, guint keyval,  GdkModifierType modifier) {
	if( gtk_text_buffer_get_char_count(tb) ) {
		gtk_text_buffer_set_text(tb,"",-1);
		show_entry();
	} else {
		gtk_main_quit();
	}
}

static char *get_text() {
	GtkTextIter start, end;
	gtk_text_buffer_get_start_iter(tb,&start);
	gtk_text_buffer_get_end_iter(tb,&end);
	return gtk_text_buffer_get_text(tb, &start, &end, FALSE);
}


static void redraw(GtkWidget *draw, cairo_t *cr, gpointer data) {
	int q;
	const char *text = get_text();

	if (strlen(text) > 0) {
		int w1, h1;
		static PangoLayout* layout;

		layout = gtk_widget_create_pango_layout(draw, get_text());
		pango_layout_set_font_description(layout, font);

		switch(alignment){
			case 0: // center
				pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
				break;
			case 1: // left
				pango_layout_set_alignment(layout,PANGO_ALIGN_LEFT);
				break;
			case 2: // left
				pango_layout_set_alignment(layout,PANGO_ALIGN_RIGHT);
				break;
			default:
				// we propably don't want to annoy the user, so default to
				// the old default-behaviour:
				pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
		}

		pango_layout_get_pixel_size(layout, &w1, &h1);
		if (w1>0 && h1>0) {
			int w2 = gtk_widget_get_allocated_width(draw);
			int h2 = gtk_widget_get_allocated_height(draw);

			int rw1, rh1;
			if (rotation == 0 || rotation == 2) {
				rw1 = w1;
				rh1 = h1;
			} else {
				rw1 = h1;
				rh1 = w1;
			}

			double s = min ((double)w2/rw1, (double)h2/rh1);

			cairo_save(cr);

			GdkRGBA color;
			gtk_style_context_get_color (gtk_widget_get_style_context(draw),
				GTK_STATE_NORMAL, &color);
			gdk_cairo_set_source_rgba(cr, &color);


			if (alignment == 1) { // left align
				cairo_translate(cr, (s * rw1)/2, h2/2);
			} else if (alignment == 2) { // right align
				cairo_translate(cr, w2 - (s * rw1)/2, h2/2);
			} else {
				cairo_translate(cr, w2/2, h2/2);
			}
			cairo_rotate(cr, rotation * M_PI_2);
			cairo_scale(cr, s, s);
			cairo_translate(cr, -w1/2, -h1/2);
			pango_cairo_show_layout (cr, layout);

			cairo_restore(cr);
		}
		g_object_unref(layout);
	}
}

static gboolean text_keypress(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
	// forward signal to the text view
	gboolean ret;
	g_signal_emit_by_name(tv, "key-press-event", event, &ret);
	gtk_widget_grab_focus(tv);
	return ret;
}

static gboolean text_clicked(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
	show_entry();
	if (event->type == GDK_BUTTON_PRESS && event->button == 2) {
		GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_PRIMARY);

		gchar *txt = gtk_clipboard_wait_for_text(cb);
		if (txt != NULL) {
			gtk_text_buffer_set_text(tb,txt,-1);
			g_free(txt);
		}

	}
	return FALSE;
}

static gboolean read_chan(GIOChannel *chan, GIOCondition condition, gpointer data){
	gchar buf[1024];
	GString *input;
	GIOStatus stat = G_IO_STATUS_NORMAL;
	gsize read;
	GError *err = NULL;

	while ((stat = g_io_channel_read_chars(chan, buf, sizeof(buf), &read, &err)) == G_IO_STATUS_NORMAL && err == NULL) {
		g_string_append_len(partial_input, buf, read);
	}

	if (err != NULL)
	{
	    fprintf (stderr, "Unable to read stdin: %s\n", err->message);
	    g_error_free (err);
	    return TRUE;
	}


	if (stat == G_IO_STATUS_EOF) {
		// There is an end of file, so use the whole input
		input = g_string_new_len(partial_input->str, partial_input->len);
		g_string_truncate(partial_input, 0);
	} else {
		// There is no end of file. Check for form feed characters.
		// Use from the second-to-last to the last.
		char *last_ff = strrchr(partial_input->str, '\f');
		if (last_ff) {
			*last_ff = '\0';
			char *snd_last_ff = strrchr(partial_input->str, '\f');
			if (snd_last_ff == NULL) snd_last_ff = partial_input->str;
			input = g_string_new_len(snd_last_ff, last_ff - snd_last_ff);
			g_string_erase(partial_input, 0, last_ff - partial_input->str + 1);
		} else {
			return TRUE;
		}
	}

	// remove beginning and trailing newlines, if any
	gssize cnt = 0;
	while ((input->len > cnt) && (input->str[cnt] == '\n')) {
		cnt++;
	}
	g_string_erase(input, 0, cnt);

	while ((input->len > 0) && (input->str[input->len - 1] == '\n')) {
		g_string_truncate(input, input->len - 1);
	}

	g_signal_handler_block (tb, text_change_handler);
	gtk_text_buffer_set_text (tb, input->str, input->len);
	g_signal_handler_unblock (tb, text_change_handler);

	g_string_free(input, TRUE);

	if (stat == G_IO_STATUS_AGAIN)
		return TRUE;
	else
		return FALSE;
}

static void newtext() {
	gtk_widget_queue_draw(draw);
}

static void newtext_show_input() {
	show_entry();
}

static void move_cursor(GtkTextView* tv, GtkMovementStep step, gint count, gboolean extend_selection, gpointer user_data) {
	show_entry();
}

static struct option const long_options[] =
{
	{"help",       no_argument,       NULL, 'h'},
	{"version",    no_argument,       NULL, 'V'},
	{"foreground", required_argument, NULL, 'f'},
	{"background", required_argument, NULL, 'b'},
	{"font",       required_argument, NULL, 'n'},
	{"rotate",     required_argument, NULL, 'r'},
	{"align",      required_argument, NULL, 'a'},
	{0,0,0,0}
};

static void usage(char *cmd) {
	printf("Usage: %s [-h|--help] [-V|--version] [-f|--foreground=colordesc] [-b|--background=colordesc] [-n|--font=fontdesc] [-r|--rotate=0,1,2,3] [-a|--align=0,1,2]\n", cmd);
}

static void version() {
	printf("%s\n", PACKAGE_STRING);
}

int main(int argc, char **argv) {
	GString *input;
	int c;
	int input_provided = 0;

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

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

			case 'f':
				foreground = optarg;
				break;

			case 'b':
				background = optarg;
				break;

			case 'n':
				fontdesc = optarg;
				break;
			case 'r':
				rotation = atoi(optarg);
				break;
			case 'a':
				alignment = 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_icon_name (GTK_WINDOW (window), "sm");
	gtk_window_fullscreen(GTK_WINDOW(window));

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

	GdkRGBA  white, black;
	if (foreground != NULL) {
		gdk_rgba_parse(&black, foreground);
	} else {
		gdk_rgba_parse(&black, "black");
	}
	if (background != NULL) {
		gdk_rgba_parse(&white, background);
	} else {
		gdk_rgba_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_override_background_color(draw, GTK_STATE_NORMAL, &white);
	gtk_widget_override_color(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);

	cursor = gdk_cursor_new_for_display(gtk_widget_get_display(GTK_WIDGET(draw)), GDK_BLANK_CURSOR);

	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(":-)");

	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_icon_name("application-exit", GTK_ICON_SIZE_BUTTON);
	g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);

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

	GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,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_box_new(GTK_ORIENTATION_VERTICAL,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);

	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), "draw", 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)
		hide_entry(NULL);
	else
		show_entry();

	gtk_main();

	return 0;
}