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