Show partially read text from stdin if separated by \f
[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 PangoLayout* layout;
58 static char *foreground = NULL;
59 static char *background = NULL;
60 static char *fontdesc = NULL;
61 static int rotation = 0; // 0 = normal, 1 = left, 2 = inverted, 3 = right
62 static GString *partial_input;
63
64 gboolean hide_entry(gpointer *user_data) {
65         gtk_widget_hide(entry_widget);
66         gtk_widget_grab_focus(draw);
67         gtk_widget_queue_draw(draw);
68         gdk_window_set_cursor(GTK_WIDGET(draw)->window, cursor);
69         return FALSE;
70 }
71
72 static void show_entry() {
73         if (timeout_id) {
74                 g_source_remove(timeout_id);
75                 timeout_id = 0;
76         }
77         gtk_widget_show(entry_widget);
78         gtk_widget_grab_focus(tv);
79         gdk_window_set_cursor(GTK_WIDGET(draw)->window, NULL);
80
81         timeout_id = g_timeout_add_seconds (AUTOHIDE_TIMEOUT, (GSourceFunc)hide_entry, NULL);
82 }
83
84 static void clear_text(GtkAccelGroup *accel, GObject *window, guint keyval,  GdkModifierType modifier) {
85         if( gtk_text_buffer_get_char_count(tb) ) {
86                 gtk_text_buffer_set_text(tb,"",-1);
87                 show_entry();
88         } else {
89                 gtk_main_quit();
90         }
91 }
92
93 static char *get_text() {
94         GtkTextIter start, end;
95         gtk_text_buffer_get_start_iter(tb,&start);
96         gtk_text_buffer_get_end_iter(tb,&end);
97         return gtk_text_buffer_get_text(tb, &start, &end, FALSE);
98 }
99
100 static void hq(gboolean q, gboolean force){
101         if (q != quality) 
102                 if (q)
103                         gtk_settings_set_long_property(settings,"gtk-xft-antialias",1,"Hier halt");
104                 else
105                         gtk_settings_set_long_property(settings,"gtk-xft-antialias",0,"Hier halt");
106         else
107                 if (force)
108                         gtk_widget_queue_draw(draw);
109
110         quality = q;
111 }
112
113 static void redraw() {
114         const char *text = pango_layout_get_text(layout);
115         if (strlen(text) > 0) {
116                 GdkGC *gc = gtk_widget_get_style(draw)->fg_gc[GTK_STATE_NORMAL];
117                 int w1, h1;
118                 pango_layout_get_pixel_size(layout, &w1, &h1);
119                 if (w1>0 && h1>0) {
120                         int w2 = window->allocation.width;
121                         int h2 = window->allocation.height;
122
123                         int rw1, rh1;
124                         if (rotation == 0 || rotation == 2) {
125                                 rw1 = w1;
126                                 rh1 = h1;
127                         } else {
128                                 rw1 = h1;
129                                 rh1 = w1;
130                         }
131
132                         double s = min ((double)w2/rw1, (double)h2/rh1);
133                         
134
135                         PangoMatrix matrix = PANGO_MATRIX_INIT;
136                         // Move matrix to the screen center
137                         pango_matrix_translate(&matrix, w2/2, h2/2);
138                         // Scale as required
139                         pango_matrix_scale(&matrix, s, s);
140                         // Rotate if needed
141                         pango_matrix_rotate (&matrix, rotation * 90);
142                         // Move matrix so that text will be centered
143                         //pango_matrix_translate(&matrix, -w1/2, -h1/2);
144                         
145                         // Apply matrix
146                         PangoContext *context =  pango_layout_get_context(layout);
147                         pango_context_set_matrix (context, &matrix);
148                         pango_layout_context_changed (layout);
149
150                         gdk_draw_layout(draw->window, gc,
151                                 (w2-(s*rw1))/2, (h2-(s*rh1))/2,layout);
152                         
153                         // Reset matrix
154                         PangoMatrix matrix2 = PANGO_MATRIX_INIT;
155                         pango_context_set_matrix (context, &matrix2);
156                         pango_layout_context_changed (layout);
157
158                         hq(TRUE, FALSE);
159                 }
160         }
161 }
162
163 static gboolean text_keypress(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
164         // forward signal to the text view
165         gboolean ret;
166         g_signal_emit_by_name(tv, "key-press-event", event, &ret);
167         gtk_widget_grab_focus(tv);
168         return ret;
169 }
170
171 static gboolean text_clicked(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
172         show_entry();
173         if (event->type == GDK_BUTTON_PRESS && event->button == 2) {
174                 GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
175
176                 gchar *txt = gtk_clipboard_wait_for_text(cb);
177                 if (txt != NULL) {
178                         gtk_text_buffer_set_text(tb,txt,-1);
179                         g_free(txt);
180                 }
181
182         }
183         return FALSE;
184 }
185
186 static gboolean read_chan(GIOChannel *chan, GIOCondition condition, gpointer data){
187         gchar buf[1024];
188         GString *input;
189         GIOStatus stat = G_IO_STATUS_NORMAL;
190         gsize read;
191         GError *err = NULL;
192
193         while ((stat = g_io_channel_read_chars(chan, buf, sizeof(buf), &read, &err)) == G_IO_STATUS_NORMAL && err == NULL) {
194                 g_string_append_len(partial_input, buf, read);
195         }
196
197         if (err != NULL)
198         {
199             fprintf (stderr, "Unable to read stdin: %s\n", err->message);
200             g_error_free (err);
201             return TRUE;
202         }
203
204
205         if (stat == G_IO_STATUS_EOF) {
206                 // There is an end of file, so use the whole input
207                 input = g_string_new_len(partial_input->str, partial_input->len);
208                 g_string_truncate(partial_input, 0);
209         } else {
210                 // There is no end of file. Check for form feed characters.
211                 // Use from the second-to-last to the last.
212                 char *last_ff = strrchr(partial_input->str, '\f');
213                 if (last_ff) {
214                         *last_ff = '\0';
215                         char *snd_last_ff = strrchr(partial_input->str, '\f');
216                         if (snd_last_ff == NULL) snd_last_ff = partial_input->str;
217                         input = g_string_new_len(snd_last_ff, last_ff - snd_last_ff);
218                         g_string_erase(partial_input, 0, last_ff - partial_input->str + 1);
219                 } else {
220                         return TRUE;
221                 }
222         }
223
224         // remove beginning and trailing newlines, if any
225         gssize cnt = 0;
226         while ((input->len > cnt) && (input->str[cnt] == '\n')) {
227                 cnt++;
228         }
229         g_string_erase(input, 0, cnt);
230
231         while ((input->len > 0) && (input->str[input->len - 1] == '\n')) {
232                 g_string_truncate(input, input->len - 1);
233         }
234
235         gtk_text_buffer_set_text (tb, input->str, input->len);
236         g_string_free(input, TRUE);
237
238         if (stat == G_IO_STATUS_AGAIN)
239                 return TRUE;
240         else
241                 return FALSE;
242 }
243
244 static void newtext() {
245         show_entry();
246         pango_layout_set_text(layout, get_text(), -1);
247         hq(FALSE, TRUE);
248 }
249
250 static void move_cursor(GtkTextView* tv, GtkMovementStep step, gint count, gboolean extend_selection, gpointer user_data) {
251         show_entry();
252 }
253
254 static struct option const long_options[] =
255 {
256         {"help",       no_argument,       NULL, 'h'},
257         {"version",    no_argument,       NULL, 'V'},
258         {"foreground", required_argument, NULL, 'f'},
259         {"background", required_argument, NULL, 'b'},
260         {"font",       required_argument, NULL, 'n'},
261         {"rotate",     required_argument, NULL, 'r'},
262         {0,0,0,0}
263 };
264
265 static void usage(char *cmd) {
266         printf("Usage: %s [-h|--help] [-V|--version] [-f|--foreground=colordesc] [-b|--background=colordesc] [-n|--font=fontdesc] [-r|--rotate=0,1,2,3]\n", cmd);
267 }
268
269 static void version() {
270         printf("%s\n", PACKAGE_STRING);
271 }
272
273 int main(int argc, char **argv) {
274         GString *input;
275         int c;
276         int input_provided = 0;
277
278         while ((c = getopt_long (argc, argv, "hVf:b:n:r:", long_options, (int *) 0)) != EOF) {
279                 switch (c) {
280                         case 'h':
281                                 usage(argv[0]);
282                                 return 0;
283                                 break;
284
285                         case 'V':
286                                 version();
287                                 return 0;
288                                 break;
289
290                         case 'f':
291                                 foreground = optarg;
292                                 break;
293
294                         case 'b':
295                                 background = optarg;
296                                 break;
297
298                         case 'n':
299                                 fontdesc = optarg;
300                                 break;
301                         case 'r':
302                                 rotation = atoi(optarg);
303                                 break;
304                         default:
305                                 /* unknown switch received - at least
306                                  * give usage but continue and use the
307                                  * data */
308                                 usage(argv[0]);
309                                 break;
310                 }
311         }
312
313         gtk_init(&argc, &argv);
314
315         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
316         gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
317         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
318         gtk_window_set_icon_name (GTK_WINDOW (window), "sm");
319
320         GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(window));
321         int w = gdk_screen_get_width(screen);
322         int h = gdk_screen_get_height(screen);
323
324         GdkGeometry geometry;
325         geometry.min_width = w;
326         geometry.min_height = h;
327         geometry.max_width = w;
328         geometry.max_height = h;
329         geometry.base_width = w;
330         geometry.base_height = h;
331         geometry.win_gravity = GDK_GRAVITY_STATIC;
332         gtk_window_set_geometry_hints(GTK_WINDOW(window), window, &geometry,
333                                       GDK_HINT_MIN_SIZE |
334                                       GDK_HINT_MAX_SIZE |
335                                       GDK_HINT_BASE_SIZE |
336                                       GDK_HINT_WIN_GRAVITY |
337                                       GDK_HINT_USER_POS);
338         gtk_window_set_default_size(GTK_WINDOW(window), w, h);
339         gtk_window_move(GTK_WINDOW(window), 0, 0);
340
341
342         g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
343
344         settings = gtk_settings_get_default();
345         GdkColormap *colormap = gtk_widget_get_colormap(GTK_WIDGET(window));
346         GdkColor white, black;
347         gdk_colormap_alloc_color(colormap, &white, TRUE, TRUE);
348         gdk_colormap_alloc_color(colormap, &black, TRUE, TRUE);
349         if (foreground != NULL) {
350                 gdk_color_parse(foreground, &black);
351         } else {
352                 gdk_color_parse("black", &black);
353         }
354         if (background != NULL) {
355                 gdk_color_parse(background, &white);
356         } else {
357                 gdk_color_parse("white", &white);
358         }
359
360         draw = gtk_drawing_area_new();
361         gtk_widget_set_events(draw, GDK_BUTTON_PRESS_MASK|GDK_KEY_PRESS_MASK);
362         gtk_widget_set_size_request(draw,400,400);
363         gtk_widget_modify_bg(draw, GTK_STATE_NORMAL, &white);
364         gtk_widget_modify_fg(draw, GTK_STATE_NORMAL, &black);
365         g_signal_connect(G_OBJECT(draw), "button-press-event", G_CALLBACK(text_clicked), NULL);
366         g_signal_connect(G_OBJECT(draw), "key-press-event", G_CALLBACK(text_keypress), NULL);
367         gtk_widget_set_can_focus(draw, TRUE);
368
369         GdkPixmap *pixmap = gdk_pixmap_new(NULL, 1, 1, 1);
370         GdkColor color;
371         cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 0, 0);
372
373         tv = gtk_text_view_new();
374         tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
375
376         partial_input = g_string_new("");
377
378         if (argc > optind)
379                 if (!strcmp(argv[optind], "-") ) {
380                         // read from stdin
381                         GIOChannel *chan = g_io_channel_unix_new(0);
382                         g_io_channel_set_flags(chan, G_IO_FLAG_NONBLOCK, NULL);
383                         g_io_add_watch (chan, G_IO_IN | G_IO_HUP, &read_chan, NULL);
384
385                         input = g_string_new("");
386                         input_provided++;
387                 } else {
388                         int i;
389
390                         input = g_string_new("");
391
392                         for (i = optind; i < argc; i++) {
393                                 g_string_append(input, argv[i]);
394
395                                 if (i < argc - 1) {
396                                         g_string_append(input, " ");
397                                 }
398                         }
399                         input_provided++;
400                 }
401         else
402                 input = g_string_new(":-)");
403
404         gtk_text_buffer_set_text(tb, input->str, input->len);
405         g_string_free(input, TRUE);
406         GtkTextIter start, end;
407         gtk_text_buffer_get_bounds(tb, &start, &end);
408         gtk_text_buffer_select_range(tb, &start, &end);
409
410         quit = gtk_button_new_from_stock(GTK_STOCK_QUIT);
411         g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);
412
413         GtkWidget *vbox_button = gtk_vbox_new(FALSE, 0);
414         gtk_box_pack_end(GTK_BOX(vbox_button), quit, FALSE, FALSE, 0);
415
416         GtkWidget *hbox = gtk_hbox_new(FALSE,0);
417         entry_widget = hbox;
418         gtk_box_pack_start(GTK_BOX(hbox), tv,   TRUE,  TRUE,  0);
419         gtk_box_pack_start(GTK_BOX(hbox), vbox_button, FALSE, FALSE, 0);
420
421         GtkWidget *vbox = gtk_vbox_new(FALSE,0);
422         gtk_box_pack_start(GTK_BOX(vbox), draw, TRUE, TRUE, 0);
423         gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
424
425         gtk_container_add(GTK_CONTAINER(window), vbox);
426
427         font = pango_font_description_new();
428         if (fontdesc != NULL) {
429                 pango_font_description_set_family(font, fontdesc);
430         } else {
431                 pango_font_description_set_family(font, "sans-serif");
432         }
433         pango_font_description_set_size(font, 200*PANGO_SCALE);
434
435         layout = gtk_widget_create_pango_layout(window,get_text());
436         pango_layout_set_font_description(layout, font);
437         pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
438
439         GtkAccelGroup *accel = gtk_accel_group_new();
440         guint key;
441         GdkModifierType mod;
442         gtk_accelerator_parse("<Ctrl>Q", &key, &mod);
443         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
444         gtk_accelerator_parse("Escape", &key, &mod);
445         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(clear_text), NULL, NULL));
446         gtk_window_add_accel_group(GTK_WINDOW(window), accel);
447         gtk_widget_show_all(window);
448
449         g_signal_connect_after(G_OBJECT(draw), "expose-event", G_CALLBACK(redraw), NULL);
450         g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext), NULL);
451         g_signal_connect(G_OBJECT(tv), "move-cursor", G_CALLBACK(move_cursor), NULL);
452
453         if (!input_provided)
454                 show_entry();
455         else
456                 hide_entry(NULL);
457
458         gtk_main();
459
460         return 0;
461 }