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