c4947961a94f2f0a1a493e394b649f232e4b5476
[darcs-mirror-screen-message.debian.git] / sm.c
1 /*
2 #     sm.c
3 #     Copyright (C) 2006 - 2014 Joachim Breitner
4 #
5 #     This program is free software; you can redistribute it and/or modify
6 #     it under the terms of the GNU General Public License as published by
7 #     the Free Software Foundation; either version 2 of the License, or
8 #     (at your option) any later version.
9 #
10 #     This program is distributed in the hope that it will be useful,
11 #     but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #     GNU General Public License for more details.
14 #
15 #     You should have received a copy of the GNU General Public License
16 #     along with this program; if not, write to the Free Software
17 #     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18 */
19
20 #include <gtk/gtk.h>
21 #include <gdk/gdk.h>
22 #include <pango/pango.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <math.h>
27
28 #include "config.h"
29
30 #ifndef _GNU_SOURCE
31 #define _GNU_SOURCE                             /* for getopt_long */
32 #endif
33 #include <getopt.h>
34
35 #define min(x,y) ((x) < (y) ? (x) : (y))
36
37 #define AUTOHIDE_TIMEOUT 3
38
39 static gboolean quality = TRUE;
40
41 static int timeout_id=0;
42
43 static GtkWidget* window;
44 static GtkWidget* draw;
45 static GdkCursor *cursor;
46 static GtkWidget* quit;
47 static GtkWidget* tv;
48 static GtkWidget* entry_widget;
49 static GtkSettings* settings;
50 static GtkTextBuffer* tb;
51 static PangoFontDescription *font;
52 static char *foreground = NULL;
53 static char *background = NULL;
54 static char *fontdesc = NULL;
55 static int rotation = 0; // 0 = normal, 1 = left, 2 = inverted, 3 = right
56 static int alignment = 0; // 0 = centered, 1 = left-aligned, 2 = right-aligned
57 static GString *partial_input;
58 static gulong quality_high_handler = 0;
59 static gulong text_change_handler;
60
61 gboolean hide_entry(gpointer *user_data) {
62         timeout_id = 0;
63         gtk_widget_hide(entry_widget);
64         gtk_widget_grab_focus(draw);
65         gtk_widget_queue_draw(draw);
66         gdk_window_set_cursor(gtk_widget_get_window(GTK_WIDGET(draw)), cursor);
67         return FALSE;
68 }
69
70 static void show_entry() {
71         if (timeout_id) {
72                 g_source_remove(timeout_id);
73                 timeout_id = 0;
74         }
75         gtk_widget_show(entry_widget);
76         gtk_widget_grab_focus(tv);
77         gdk_window_set_cursor(gtk_widget_get_window(GTK_WIDGET(draw)), NULL);
78
79         timeout_id = g_timeout_add_seconds (AUTOHIDE_TIMEOUT, (GSourceFunc)hide_entry, NULL);
80 }
81
82 static void clear_text(GtkAccelGroup *accel, GObject *window, guint keyval,  GdkModifierType modifier) {
83         if( gtk_text_buffer_get_char_count(tb) ) {
84                 gtk_text_buffer_set_text(tb,"",-1);
85                 show_entry();
86         } else {
87                 gtk_main_quit();
88         }
89 }
90
91 static char *get_text() {
92         GtkTextIter start, end;
93         gtk_text_buffer_get_start_iter(tb,&start);
94         gtk_text_buffer_get_end_iter(tb,&end);
95         return gtk_text_buffer_get_text(tb, &start, &end, FALSE);
96 }
97
98 static void hq(gboolean q, gboolean force){
99         if (q != quality) {
100                 if (q)
101                         gtk_settings_set_long_property(settings,"gtk-xft-antialias",1,"Hier halt");
102                 else
103                         gtk_settings_set_long_property(settings,"gtk-xft-antialias",0,"Hier halt");
104         }
105         else
106                 if (force)
107                         gtk_widget_queue_draw(draw);
108
109         quality = q;
110 }
111
112 static gboolean quality_high (gpointer data) {
113         quality_high_handler = 0;
114         hq(TRUE, FALSE);
115         return FALSE;
116 }
117
118 static void redraw(GtkWidget *draw, cairo_t *cr, gpointer data) {
119         int q;
120         const char *text = get_text();
121
122         if (strlen(text) > 0) {
123                 int w1, h1;
124                 static PangoLayout* layout;
125
126                 layout = gtk_widget_create_pango_layout(draw, get_text());
127                 pango_layout_set_font_description(layout, font);
128
129                 switch(alignment){
130                         case 0: // center
131                                 pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
132                                 break;
133                         case 1: // left
134                                 pango_layout_set_alignment(layout,PANGO_ALIGN_LEFT);
135                                 break;
136                         case 2: // left
137                                 pango_layout_set_alignment(layout,PANGO_ALIGN_RIGHT);
138                                 break;
139                         default:
140                                 // we propably don't want to annoy the user, so default to
141                                 // the old default-behaviour:
142                                 pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
143                 }
144
145                 pango_layout_get_pixel_size(layout, &w1, &h1);
146                 if (w1>0 && h1>0) {
147                         int w2 = gtk_widget_get_allocated_width(draw);
148                         int h2 = gtk_widget_get_allocated_height(draw);
149
150                         int rw1, rh1;
151                         if (rotation == 0 || rotation == 2) {
152                                 rw1 = w1;
153                                 rh1 = h1;
154                         } else {
155                                 rw1 = h1;
156                                 rh1 = w1;
157                         }
158
159                         double s = min ((double)w2/rw1, (double)h2/rh1);
160
161                         cairo_save(cr);
162
163                         GdkRGBA color;
164                         gtk_style_context_get_color (gtk_widget_get_style_context(draw),
165                                 GTK_STATE_NORMAL, &color);
166                         gdk_cairo_set_source_rgba(cr, &color);
167
168
169                         if (alignment == 1) { // left align
170                                 cairo_translate(cr, (s * rw1)/2, h2/2);
171                         } else if (alignment == 2) { // right align
172                                 cairo_translate(cr, w2 - (s * rw1)/2, h2/2);
173                         } else {
174                                 cairo_translate(cr, w2/2, h2/2);
175                         }
176                         cairo_rotate(cr, rotation * M_PI_2);
177                         cairo_scale(cr, s, s);
178                         cairo_translate(cr, -w1/2, -h1/2);
179                         pango_cairo_show_layout (cr, layout);
180
181                         cairo_restore(cr);
182
183                         if (quality_high_handler) 
184                                 g_source_remove(quality_high_handler);
185                         quality_high_handler = g_timeout_add(0, quality_high, NULL);
186                 }
187                 g_object_unref(layout);
188         }
189 }
190
191 static gboolean text_keypress(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
192         // forward signal to the text view
193         gboolean ret;
194         g_signal_emit_by_name(tv, "key-press-event", event, &ret);
195         gtk_widget_grab_focus(tv);
196         return ret;
197 }
198
199 static gboolean text_clicked(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
200         show_entry();
201         if (event->type == GDK_BUTTON_PRESS && event->button == 2) {
202                 GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
203
204                 gchar *txt = gtk_clipboard_wait_for_text(cb);
205                 if (txt != NULL) {
206                         gtk_text_buffer_set_text(tb,txt,-1);
207                         g_free(txt);
208                 }
209
210         }
211         return FALSE;
212 }
213
214 static gboolean read_chan(GIOChannel *chan, GIOCondition condition, gpointer data){
215         gchar buf[1024];
216         GString *input;
217         GIOStatus stat = G_IO_STATUS_NORMAL;
218         gsize read;
219         GError *err = NULL;
220
221         while ((stat = g_io_channel_read_chars(chan, buf, sizeof(buf), &read, &err)) == G_IO_STATUS_NORMAL && err == NULL) {
222                 g_string_append_len(partial_input, buf, read);
223         }
224
225         if (err != NULL)
226         {
227             fprintf (stderr, "Unable to read stdin: %s\n", err->message);
228             g_error_free (err);
229             return TRUE;
230         }
231
232
233         if (stat == G_IO_STATUS_EOF) {
234                 // There is an end of file, so use the whole input
235                 input = g_string_new_len(partial_input->str, partial_input->len);
236                 g_string_truncate(partial_input, 0);
237         } else {
238                 // There is no end of file. Check for form feed characters.
239                 // Use from the second-to-last to the last.
240                 char *last_ff = strrchr(partial_input->str, '\f');
241                 if (last_ff) {
242                         *last_ff = '\0';
243                         char *snd_last_ff = strrchr(partial_input->str, '\f');
244                         if (snd_last_ff == NULL) snd_last_ff = partial_input->str;
245                         input = g_string_new_len(snd_last_ff, last_ff - snd_last_ff);
246                         g_string_erase(partial_input, 0, last_ff - partial_input->str + 1);
247                 } else {
248                         return TRUE;
249                 }
250         }
251
252         // remove beginning and trailing newlines, if any
253         gssize cnt = 0;
254         while ((input->len > cnt) && (input->str[cnt] == '\n')) {
255                 cnt++;
256         }
257         g_string_erase(input, 0, cnt);
258
259         while ((input->len > 0) && (input->str[input->len - 1] == '\n')) {
260                 g_string_truncate(input, input->len - 1);
261         }
262
263         g_signal_handler_block (tb, text_change_handler);
264         gtk_text_buffer_set_text (tb, input->str, input->len);
265         g_signal_handler_unblock (tb, text_change_handler);
266
267         g_string_free(input, TRUE);
268
269         if (stat == G_IO_STATUS_AGAIN)
270                 return TRUE;
271         else
272                 return FALSE;
273 }
274
275 static void newtext() {
276         if (quality_high_handler) {
277                 g_source_remove(quality_high_handler);
278                 quality_high_handler = 0;
279         }
280         hq(FALSE, TRUE);
281 }
282
283 static void newtext_show_input() {
284         show_entry();
285 }
286
287 static void move_cursor(GtkTextView* tv, GtkMovementStep step, gint count, gboolean extend_selection, gpointer user_data) {
288         show_entry();
289 }
290
291 static struct option const long_options[] =
292 {
293         {"help",       no_argument,       NULL, 'h'},
294         {"version",    no_argument,       NULL, 'V'},
295         {"foreground", required_argument, NULL, 'f'},
296         {"background", required_argument, NULL, 'b'},
297         {"font",       required_argument, NULL, 'n'},
298         {"rotate",     required_argument, NULL, 'r'},
299         {"align",      required_argument, NULL, 'a'},
300         {0,0,0,0}
301 };
302
303 static void usage(char *cmd) {
304         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);
305 }
306
307 static void version() {
308         printf("%s\n", PACKAGE_STRING);
309 }
310
311 int main(int argc, char **argv) {
312         GString *input;
313         int c;
314         int input_provided = 0;
315
316         while ((c = getopt_long (argc, argv, "hVf:b:n:r:a:", long_options, (int *) 0)) != EOF) {
317                 switch (c) {
318                         case 'h':
319                                 usage(argv[0]);
320                                 return 0;
321                                 break;
322
323                         case 'V':
324                                 version();
325                                 return 0;
326                                 break;
327
328                         case 'f':
329                                 foreground = optarg;
330                                 break;
331
332                         case 'b':
333                                 background = optarg;
334                                 break;
335
336                         case 'n':
337                                 fontdesc = optarg;
338                                 break;
339                         case 'r':
340                                 rotation = atoi(optarg);
341                                 break;
342                         case 'a':
343                                 alignment = atoi(optarg);
344                                 break;
345                         default:
346                                 /* unknown switch received - at least
347                                  * give usage but continue and use the
348                                  * data */
349                                 usage(argv[0]);
350                                 break;
351                 }
352         }
353
354         gtk_init(&argc, &argv);
355
356         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
357         gtk_window_set_icon_name (GTK_WINDOW (window), "sm");
358         gtk_window_fullscreen(GTK_WINDOW(window));
359
360         g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
361
362         settings = gtk_settings_get_default();
363         GdkRGBA  white, black;
364         if (foreground != NULL) {
365                 gdk_rgba_parse(&black, foreground);
366         } else {
367                 gdk_rgba_parse(&black, "black");
368         }
369         if (background != NULL) {
370                 gdk_rgba_parse(&white, background);
371         } else {
372                 gdk_rgba_parse(&white, "white");
373         }
374
375         draw = gtk_drawing_area_new();
376         gtk_widget_set_events(draw, GDK_BUTTON_PRESS_MASK|GDK_KEY_PRESS_MASK);
377         gtk_widget_set_size_request(draw,400,400);
378         gtk_widget_override_background_color(draw, GTK_STATE_NORMAL, &white);
379         gtk_widget_override_color(draw, GTK_STATE_NORMAL, &black);
380         g_signal_connect(G_OBJECT(draw), "button-press-event", G_CALLBACK(text_clicked), NULL);
381         g_signal_connect(G_OBJECT(draw), "key-press-event", G_CALLBACK(text_keypress), NULL);
382         gtk_widget_set_can_focus(draw, TRUE);
383
384         cursor = gdk_cursor_new_for_display(gtk_widget_get_display(GTK_WIDGET(draw)), GDK_BLANK_CURSOR);
385
386         tv = gtk_text_view_new();
387         tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
388
389         partial_input = g_string_new("");
390
391         if (argc > optind)
392                 if (!strcmp(argv[optind], "-") ) {
393                         // read from stdin
394                         GIOChannel *chan = g_io_channel_unix_new(0);
395                         g_io_channel_set_flags(chan, G_IO_FLAG_NONBLOCK, NULL);
396                         g_io_add_watch (chan, G_IO_IN | G_IO_HUP, &read_chan, NULL);
397
398                         input = g_string_new("");
399                         input_provided++;
400                 } else {
401                         int i;
402
403                         input = g_string_new("");
404
405                         for (i = optind; i < argc; i++) {
406                                 g_string_append(input, argv[i]);
407
408                                 if (i < argc - 1) {
409                                         g_string_append(input, " ");
410                                 }
411                         }
412                         input_provided++;
413                 }
414         else
415                 input = g_string_new(":-)");
416
417         gtk_text_buffer_set_text(tb, input->str, input->len);
418         g_string_free(input, TRUE);
419         GtkTextIter start, end;
420         gtk_text_buffer_get_bounds(tb, &start, &end);
421         gtk_text_buffer_select_range(tb, &start, &end);
422
423         quit = gtk_button_new_from_icon_name("application-exit", GTK_ICON_SIZE_BUTTON);
424         g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);
425
426         GtkWidget *vbox_button = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
427         gtk_box_pack_end(GTK_BOX(vbox_button), quit, FALSE, FALSE, 0);
428
429         GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,0);
430         entry_widget = hbox;
431         gtk_box_pack_start(GTK_BOX(hbox), tv,   TRUE,  TRUE,  0);
432         gtk_box_pack_start(GTK_BOX(hbox), vbox_button, FALSE, FALSE, 0);
433
434         GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL,0);
435         gtk_box_pack_start(GTK_BOX(vbox), draw, TRUE, TRUE, 0);
436         gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
437
438         gtk_container_add(GTK_CONTAINER(window), vbox);
439
440         font = pango_font_description_new();
441         if (fontdesc != NULL) {
442                 pango_font_description_set_family(font, fontdesc);
443         } else {
444                 pango_font_description_set_family(font, "sans-serif");
445         }
446         pango_font_description_set_size(font, 200*PANGO_SCALE);
447
448         GtkAccelGroup *accel = gtk_accel_group_new();
449         guint key;
450         GdkModifierType mod;
451         gtk_accelerator_parse("<Ctrl>Q", &key, &mod);
452         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
453         gtk_accelerator_parse("Escape", &key, &mod);
454         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(clear_text), NULL, NULL));
455         gtk_window_add_accel_group(GTK_WINDOW(window), accel);
456         gtk_widget_show_all(window);
457
458         g_signal_connect_after(G_OBJECT(draw), "draw", G_CALLBACK(redraw), NULL);
459         text_change_handler = g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext_show_input), NULL);
460         g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext), NULL);
461         g_signal_connect(G_OBJECT(tv), "move-cursor", G_CALLBACK(move_cursor), NULL);
462
463         if (input_provided)
464                 hide_entry(NULL);
465         else
466                 show_entry();
467
468         gtk_main();
469
470         return 0;
471 }