Revert to gtk_window_fullscreen for going fullscreen
[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         gtk_widget_hide(entry_widget);
63         gtk_widget_grab_focus(draw);
64         gtk_widget_queue_draw(draw);
65         gdk_window_set_cursor(gtk_widget_get_window(GTK_WIDGET(draw)), cursor);
66         return FALSE;
67 }
68
69 static void show_entry() {
70         if (timeout_id) {
71                 g_source_remove(timeout_id);
72                 timeout_id = 0;
73         }
74         gtk_widget_show(entry_widget);
75         gtk_widget_grab_focus(tv);
76         gdk_window_set_cursor(gtk_widget_get_window(GTK_WIDGET(draw)), NULL);
77
78         timeout_id = g_timeout_add_seconds (AUTOHIDE_TIMEOUT, (GSourceFunc)hide_entry, NULL);
79 }
80
81 static void clear_text(GtkAccelGroup *accel, GObject *window, guint keyval,  GdkModifierType modifier) {
82         if( gtk_text_buffer_get_char_count(tb) ) {
83                 gtk_text_buffer_set_text(tb,"",-1);
84                 show_entry();
85         } else {
86                 gtk_main_quit();
87         }
88 }
89
90 static char *get_text() {
91         GtkTextIter start, end;
92         gtk_text_buffer_get_start_iter(tb,&start);
93         gtk_text_buffer_get_end_iter(tb,&end);
94         return gtk_text_buffer_get_text(tb, &start, &end, FALSE);
95 }
96
97 static void hq(gboolean q, gboolean force){
98         if (q != quality) {
99                 if (q)
100                         gtk_settings_set_long_property(settings,"gtk-xft-antialias",1,"Hier halt");
101                 else
102                         gtk_settings_set_long_property(settings,"gtk-xft-antialias",0,"Hier halt");
103         }
104         else
105                 if (force)
106                         gtk_widget_queue_draw(draw);
107
108         quality = q;
109 }
110
111 static gboolean quality_high (gpointer data) {
112         hq(TRUE, FALSE);
113         return FALSE;
114 }
115
116 static void redraw(GtkWidget *draw, cairo_t *cr, gpointer data) {
117         int q;
118         const char *text = get_text();
119
120         if (strlen(text) > 0) {
121                 int w1, h1;
122                 static PangoLayout* layout;
123
124                 layout = gtk_widget_create_pango_layout(draw, get_text());
125                 pango_layout_set_font_description(layout, font);
126
127                 switch(alignment){
128                         case 0: // center
129                                 pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
130                                 break;
131                         case 1: // left
132                                 pango_layout_set_alignment(layout,PANGO_ALIGN_LEFT);
133                                 break;
134                         case 2: // left
135                                 pango_layout_set_alignment(layout,PANGO_ALIGN_RIGHT);
136                                 break;
137                         default:
138                                 // we propably don't want to annoy the user, so default to
139                                 // the old default-behaviour:
140                                 pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
141                 }
142
143                 pango_layout_get_pixel_size(layout, &w1, &h1);
144                 if (w1>0 && h1>0) {
145                         int w2 = gtk_widget_get_allocated_width(draw);
146                         int h2 = gtk_widget_get_allocated_height(draw);
147
148                         int rw1, rh1;
149                         if (rotation == 0 || rotation == 2) {
150                                 rw1 = w1;
151                                 rh1 = h1;
152                         } else {
153                                 rw1 = h1;
154                                 rh1 = w1;
155                         }
156
157                         double s = min ((double)w2/rw1, (double)h2/rh1);
158
159                         cairo_save(cr);
160
161                         GdkRGBA color;
162                         gtk_style_context_get_color (gtk_widget_get_style_context(draw),
163                                 GTK_STATE_NORMAL, &color);
164                         gdk_cairo_set_source_rgba(cr, &color);
165
166
167                         if (alignment == 1) { // left align
168                                 cairo_translate(cr, (s * rw1)/2, h2/2);
169                         } else if (alignment == 2) { // right align
170                                 cairo_translate(cr, w2 - (s * rw1)/2, h2/2);
171                         } else {
172                                 cairo_translate(cr, w2/2, h2/2);
173                         }
174                         cairo_rotate(cr, rotation * M_PI_2);
175                         cairo_scale(cr, s, s);
176                         cairo_translate(cr, -w1/2, -h1/2);
177                         pango_cairo_show_layout (cr, layout);
178
179                         cairo_restore(cr);
180
181                         if (quality_high_handler) 
182                                 g_source_remove(quality_high_handler);
183                         quality_high_handler = g_timeout_add(0, quality_high, NULL);
184                 }
185                 g_object_unref(layout);
186         }
187 }
188
189 static gboolean text_keypress(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
190         // forward signal to the text view
191         gboolean ret;
192         g_signal_emit_by_name(tv, "key-press-event", event, &ret);
193         gtk_widget_grab_focus(tv);
194         return ret;
195 }
196
197 static gboolean text_clicked(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
198         show_entry();
199         if (event->type == GDK_BUTTON_PRESS && event->button == 2) {
200                 GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
201
202                 gchar *txt = gtk_clipboard_wait_for_text(cb);
203                 if (txt != NULL) {
204                         gtk_text_buffer_set_text(tb,txt,-1);
205                         g_free(txt);
206                 }
207
208         }
209         return FALSE;
210 }
211
212 static gboolean read_chan(GIOChannel *chan, GIOCondition condition, gpointer data){
213         gchar buf[1024];
214         GString *input;
215         GIOStatus stat = G_IO_STATUS_NORMAL;
216         gsize read;
217         GError *err = NULL;
218
219         while ((stat = g_io_channel_read_chars(chan, buf, sizeof(buf), &read, &err)) == G_IO_STATUS_NORMAL && err == NULL) {
220                 g_string_append_len(partial_input, buf, read);
221         }
222
223         if (err != NULL)
224         {
225             fprintf (stderr, "Unable to read stdin: %s\n", err->message);
226             g_error_free (err);
227             return TRUE;
228         }
229
230
231         if (stat == G_IO_STATUS_EOF) {
232                 // There is an end of file, so use the whole input
233                 input = g_string_new_len(partial_input->str, partial_input->len);
234                 g_string_truncate(partial_input, 0);
235         } else {
236                 // There is no end of file. Check for form feed characters.
237                 // Use from the second-to-last to the last.
238                 char *last_ff = strrchr(partial_input->str, '\f');
239                 if (last_ff) {
240                         *last_ff = '\0';
241                         char *snd_last_ff = strrchr(partial_input->str, '\f');
242                         if (snd_last_ff == NULL) snd_last_ff = partial_input->str;
243                         input = g_string_new_len(snd_last_ff, last_ff - snd_last_ff);
244                         g_string_erase(partial_input, 0, last_ff - partial_input->str + 1);
245                 } else {
246                         return TRUE;
247                 }
248         }
249
250         // remove beginning and trailing newlines, if any
251         gssize cnt = 0;
252         while ((input->len > cnt) && (input->str[cnt] == '\n')) {
253                 cnt++;
254         }
255         g_string_erase(input, 0, cnt);
256
257         while ((input->len > 0) && (input->str[input->len - 1] == '\n')) {
258                 g_string_truncate(input, input->len - 1);
259         }
260
261         g_signal_handler_block (tb, text_change_handler);
262         gtk_text_buffer_set_text (tb, input->str, input->len);
263         g_signal_handler_unblock (tb, text_change_handler);
264
265         g_string_free(input, TRUE);
266
267         if (stat == G_IO_STATUS_AGAIN)
268                 return TRUE;
269         else
270                 return FALSE;
271 }
272
273 static void newtext() {
274         if (quality_high_handler) {
275                 g_source_remove(quality_high_handler);
276                 quality_high_handler = 0;
277         }
278         hq(FALSE, TRUE);
279 }
280
281 static void newtext_show_input() {
282         show_entry();
283 }
284
285 static void move_cursor(GtkTextView* tv, GtkMovementStep step, gint count, gboolean extend_selection, gpointer user_data) {
286         show_entry();
287 }
288
289 static struct option const long_options[] =
290 {
291         {"help",       no_argument,       NULL, 'h'},
292         {"version",    no_argument,       NULL, 'V'},
293         {"foreground", required_argument, NULL, 'f'},
294         {"background", required_argument, NULL, 'b'},
295         {"font",       required_argument, NULL, 'n'},
296         {"rotate",     required_argument, NULL, 'r'},
297         {"align",      required_argument, NULL, 'a'},
298         {0,0,0,0}
299 };
300
301 static void usage(char *cmd) {
302         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);
303 }
304
305 static void version() {
306         printf("%s\n", PACKAGE_STRING);
307 }
308
309 int main(int argc, char **argv) {
310         GString *input;
311         int c;
312         int input_provided = 0;
313
314         while ((c = getopt_long (argc, argv, "hVf:b:n:r:a:", long_options, (int *) 0)) != EOF) {
315                 switch (c) {
316                         case 'h':
317                                 usage(argv[0]);
318                                 return 0;
319                                 break;
320
321                         case 'V':
322                                 version();
323                                 return 0;
324                                 break;
325
326                         case 'f':
327                                 foreground = optarg;
328                                 break;
329
330                         case 'b':
331                                 background = optarg;
332                                 break;
333
334                         case 'n':
335                                 fontdesc = optarg;
336                                 break;
337                         case 'r':
338                                 rotation = atoi(optarg);
339                                 break;
340                         case 'a':
341                                 alignment = atoi(optarg);
342                                 break;
343                         default:
344                                 /* unknown switch received - at least
345                                  * give usage but continue and use the
346                                  * data */
347                                 usage(argv[0]);
348                                 break;
349                 }
350         }
351
352         gtk_init(&argc, &argv);
353
354         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
355         gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
356         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
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_stock(GTK_STOCK_QUIT);
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                 show_entry();
465         else
466                 hide_entry(NULL);
467
468         gtk_main();
469
470         return 0;
471 }