TAG UPSTREAM_sm_0.13
[darcs-mirror-screen-message.debian.git] / sm.c
1 /*
2 #     vlshow.c
3 #     Copyright (C) 2006 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 <math.h>
26
27 #include "config.h"
28
29 #ifndef _GNU_SOURCE
30 #define _GNU_SOURCE                             /* for getopt_long */
31 #endif
32 #include <getopt.h>
33
34 #define min(x,y) ((x) < (y) ? (x) : (y))
35
36 #define AUTOHIDE_TIMEOUT 5
37
38 static gboolean quality = TRUE;
39 static gboolean need_resize = TRUE;
40
41 static int timeout_id=0;
42
43 static GtkWidget* window;
44 static GdkCursor *cursor;
45 static GtkWidget* quit;
46 static GtkWidget* tv;
47 static GtkWidget* entry_widget;
48 static GtkSettings* settings;
49 static GtkTextBuffer* tb;
50 static PangoFontDescription *font;
51 static PangoLayout* layout;
52 static char *foreground = NULL;
53 static char *background = NULL;
54 static char *fontdesc = NULL;
55 static rotation = 0; // 0 = normal, 1 = left, 2 = inverted, 3 = right
56
57 gboolean hide_entry(gpointer *user_data) {
58         gtk_widget_hide(entry_widget);
59         gtk_widget_grab_focus(tv);
60         return FALSE;
61 }
62
63 static void show_entry() {
64         if (timeout_id) {
65                 g_source_remove(timeout_id);
66                 timeout_id = 0;
67         }
68         gtk_widget_show(entry_widget);
69
70         timeout_id = g_timeout_add_seconds (AUTOHIDE_TIMEOUT, (GSourceFunc)hide_entry, NULL);
71 }
72
73 static void realize(GtkWindow *window, GdkScreen *screen, gpointer data) {
74         gdk_window_set_cursor(GTK_WIDGET(window)->window, cursor);
75 }
76
77 static void clear_text(GtkAccelGroup *accel, GObject *window, guint keyval,  GdkModifierType modifier) {
78         if( gtk_text_buffer_get_char_count(tb) ) {
79                 gtk_text_buffer_set_text(tb,"",-1);
80                 show_entry();
81         } else {
82                 gtk_main_quit();
83         }
84 }
85
86 static char *get_text() {
87         GtkTextIter start, end;
88         gtk_text_buffer_get_start_iter(tb,&start);
89         gtk_text_buffer_get_end_iter(tb,&end);
90         return gtk_text_buffer_get_text(tb, &start, &end, FALSE);
91 }
92
93 static void hq(gboolean q, gboolean force){
94         if (q != quality) 
95                 if (q)
96                         gtk_settings_set_long_property(settings,"gtk-xft-antialias",1,"Hier halt");
97                 else
98                         gtk_settings_set_long_property(settings,"gtk-xft-antialias",0,"Hier halt");
99         else
100                 if (force)
101                         gtk_widget_queue_draw(window);
102
103         quality = q;
104 }
105
106 static void redraw() {
107         const char *text = pango_layout_get_text(layout);
108         if (strlen(text) > 0) {
109                 GdkGC *gc = gtk_widget_get_style(window)->fg_gc[GTK_STATE_NORMAL];
110                 int w1, h1;
111                 pango_layout_get_pixel_size(layout, &w1, &h1);
112                 if (w1>0 && h1>0) {
113                         int w2 = window->allocation.width;
114                         int h2 = window->allocation.height;
115
116                         int rw1, rh1;
117                         if (rotation == 0 || rotation == 2) {
118                                 rw1 = w1;
119                                 rh1 = h1;
120                         } else {
121                                 rw1 = h1;
122                                 rh1 = w1;
123                         }
124
125                         double s = min ((double)w2/rw1, (double)h2/rh1);
126                         
127
128                         PangoMatrix matrix = PANGO_MATRIX_INIT;
129                         // Move matrix to the screen center
130                         pango_matrix_translate(&matrix, w2/2, h2/2);
131                         // Scale as required
132                         pango_matrix_scale(&matrix, s, s);
133                         // Rotate if needed
134                         pango_matrix_rotate (&matrix, rotation * 90);
135                         // Move matrix so that text will be centered
136                         //pango_matrix_translate(&matrix, -w1/2, -h1/2);
137                         
138                         // Apply matrix
139                         PangoContext *context =  pango_layout_get_context(layout);
140                         pango_context_set_matrix (context, &matrix);
141                         pango_layout_context_changed (layout);
142
143                         gdk_draw_layout(window->window, gc,
144                                 (w2-(s*rw1))/2, (h2-(s*rh1))/2,layout);
145                         hq(TRUE, FALSE);
146                 }
147         }
148 }
149
150 static gboolean text_clicked(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
151         show_entry();
152         if (event->type == GDK_BUTTON_PRESS && event->button == 2) {
153                 GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
154
155                 gchar *txt = gtk_clipboard_wait_for_text(cb);
156                 if (txt != NULL) {
157                         gtk_text_buffer_set_text(tb,txt,-1);
158                         g_free(txt);
159                 }
160
161         }
162         return FALSE;
163 }
164
165 static void newtext(char *text) {
166         show_entry();
167         pango_layout_set_text(layout, get_text(), -1);
168         hq(FALSE, TRUE);
169 }
170
171 static struct option const long_options[] =
172 {
173         {"help",       no_argument,       NULL, 'h'},
174         {"version",    no_argument,       NULL, 'V'},
175         {"foreground", required_argument, NULL, 'f'},
176         {"background", required_argument, NULL, 'b'},
177         {"font",       required_argument, NULL, 'n'},
178         {"rotate",     required_argument, NULL, 'r'},
179         {0,0,0,0}
180 };
181
182 static void usage(char *cmd) {
183         printf("Usage: %s [-h|--help] [-V|--version] [-f|--foreground=colordesc] [-b|--background=colordesc] [-n|--font=fontdesc] [-r|--rotate=0,1,2,3]\n", cmd);
184 }
185
186 static void version() {
187         printf("%s\n", PACKAGE_STRING);
188 }
189
190 int main(int argc, char **argv) {
191         GString *input;
192         int c;
193
194         while ((c = getopt_long (argc, argv, "hVf:b:n:r:", long_options, (int *) 0)) != EOF) {
195                 switch (c) {
196                         case 'h':
197                                 usage(argv[0]);
198                                 return 0;
199                                 break;
200
201                         case 'V':
202                                 version();
203                                 return 0;
204                                 break;
205
206                         case 'f':
207                                 foreground = optarg;
208                                 break;
209
210                         case 'b':
211                                 background = optarg;
212                                 break;
213
214                         case 'n':
215                                 fontdesc = optarg;
216                                 break;
217                         case 'r':
218                                 rotation = atoi(optarg);
219                                 break;
220                         default:
221                                 /* unknown switch received - at least
222                                  * give usage but continue and use the
223                                  * data */
224                                 usage(argv[0]);
225                                 break;
226                 }
227         }
228
229         gtk_init(&argc, &argv);
230
231         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
232         gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
233         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
234         gtk_window_set_icon_name (GTK_WINDOW (window), "sm");
235
236         GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(window));
237         gtk_widget_set_size_request(window, gdk_screen_get_width(screen),
238                                             gdk_screen_get_height(screen));
239         gtk_window_fullscreen(GTK_WINDOW(window));
240         g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
241
242         settings = gtk_settings_get_default();
243         GdkColormap *colormap = gtk_widget_get_colormap(GTK_WIDGET(window));
244         GdkColor white, black;
245         gdk_colormap_alloc_color(colormap, &white, TRUE, TRUE);
246         gdk_colormap_alloc_color(colormap, &black, TRUE, TRUE);
247         if (foreground != NULL) {
248                 gdk_color_parse(foreground, &black);
249         } else {
250                 gdk_color_parse("black", &black);
251         }
252         if (background != NULL) {
253                 gdk_color_parse(background, &white);
254         } else {
255                 gdk_color_parse("white", &white);
256         }
257         gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &white);
258         gtk_widget_modify_fg(window, GTK_STATE_NORMAL, &black);
259
260         gtk_widget_set_events(window, GDK_BUTTON_PRESS_MASK);
261         g_signal_connect(G_OBJECT(window), "realize", G_CALLBACK(realize), NULL);
262         g_signal_connect(G_OBJECT(window), "button-press-event", G_CALLBACK(text_clicked), NULL);
263
264         GdkPixmap *pixmap = gdk_pixmap_new(NULL, 1, 1, 1);
265         GdkColor color;
266         cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 0, 0);
267
268         tv = gtk_text_view_new();
269         gtk_widget_modify_bg(tv, GTK_STATE_NORMAL, &white);
270         gtk_widget_modify_fg(tv, GTK_STATE_NORMAL, &black);
271         tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
272
273         if (argc > optind)
274                 if (!strcmp(argv[optind], "-") ) {
275                         // read from stdin
276                         gchar buf[1024];
277                         int num;
278
279                         input = g_string_new("");
280
281                         while ((num = fread (buf, 1, sizeof(buf), stdin)) > 0) {
282                                 g_string_append_len(input, buf, num);
283                         }
284
285                         // remove trailing newline, if any
286                         if ((input->len > 0) && (input->str[input->len - 1] == '\n')) {
287                                 g_string_truncate(input, input->len - 1);
288                         }
289                 } else {
290                         int i;
291
292                         input = g_string_new("");
293
294                         for (i = optind; i < argc; i++) {
295                                 g_string_append(input, argv[i]);
296
297                                 if (i < argc - 1) {
298                                         g_string_append(input, " ");
299                                 }
300                         }
301                 }
302         else
303                 input = g_string_new(":-)");
304
305         gtk_text_buffer_set_text(tb, input->str, input->len);
306         GtkTextIter start, end;
307         gtk_text_buffer_get_bounds(tb, &start, &end);
308         gtk_text_buffer_select_range(tb, &start, &end);
309
310         quit = gtk_button_new_from_stock(GTK_STOCK_QUIT);
311         g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);
312         gtk_widget_modify_bg(quit, GTK_STATE_NORMAL, &white);
313         gtk_widget_modify_fg(quit, GTK_STATE_NORMAL, &black);
314
315         GtkWidget *vbox_button = gtk_vbox_new(FALSE, 0);
316         gtk_box_pack_end(GTK_BOX(vbox_button), quit, FALSE, FALSE, 0);
317
318         GtkWidget *hbox = gtk_hbox_new(FALSE,0);
319         entry_widget = hbox;
320         gtk_box_pack_start(GTK_BOX(hbox), tv,   TRUE,  TRUE,  0);
321         gtk_box_pack_start(GTK_BOX(hbox), vbox_button, FALSE, FALSE, 0);
322
323         GtkWidget *vbox = gtk_vbox_new(FALSE,0);
324         gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
325
326         gtk_container_add(GTK_CONTAINER(window), vbox);
327
328         font = pango_font_description_new();
329         if (fontdesc != NULL) {
330                 pango_font_description_set_family(font, fontdesc);
331         } else {
332                 pango_font_description_set_family(font, "sans-serif");
333         }
334         pango_font_description_set_size(font, 200*PANGO_SCALE);
335
336         layout = gtk_widget_create_pango_layout(window,get_text());
337         pango_layout_set_font_description(layout, font);
338         pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
339
340         GtkAccelGroup *accel = gtk_accel_group_new();
341         guint key;
342         GdkModifierType mod;
343         gtk_accelerator_parse("<Ctrl>Q", &key, &mod);
344         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
345         gtk_accelerator_parse("Escape", &key, &mod);
346         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(clear_text), NULL, NULL));
347         gtk_window_add_accel_group(GTK_WINDOW(window), accel);
348         gtk_widget_show_all(window);
349
350         g_signal_connect_after(G_OBJECT(window), "expose-event", G_CALLBACK(redraw), NULL);
351         g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext), NULL);
352
353         show_entry();
354
355         gtk_main();
356
357         return 0;
358 }