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