TAG UPSTREAM_sm_0.17
[darcs-mirror-screen-message.debian.git] / sm.c
diff --git a/sm.c b/sm.c
index ad53f93..92c22e9 100644 (file)
--- a/sm.c
+++ b/sm.c
 #include <pango/pango.h>
 #include <string.h>
 #include <stdio.h>
+#include <math.h>
 
-static int min(int x, int y) {
-       return x < y ? x : y;
-}
+#include "config.h"
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE                             /* for getopt_long */
+#endif
+#include <getopt.h>
+
+#define min(x,y) ((x) < (y) ? (x) : (y))
 
+#define AUTOHIDE_TIMEOUT 3
 
 static gboolean quality = TRUE;
 static gboolean need_resize = TRUE;
 
+static int timeout_id=0;
+
 static GtkWidget* window;
 static GtkWidget* draw;
 static GdkCursor *cursor;
 static GtkWidget* quit;
 static GtkWidget* tv;
+static GtkWidget* entry_widget;
 static GtkSettings* settings;
 static GtkTextBuffer* tb;
 static PangoFontDescription *font;
 static PangoLayout* layout;
+static char *foreground = NULL;
+static char *background = NULL;
+static char *fontdesc = NULL;
+static rotation = 0; // 0 = normal, 1 = left, 2 = inverted, 3 = right
+
+gboolean hide_entry(gpointer *user_data) {
+       gtk_widget_hide(entry_widget);
+       gtk_widget_grab_focus(tv);
+       gtk_widget_queue_draw(draw);
+       gdk_window_set_cursor(GTK_WIDGET(draw)->window, cursor);
+       return FALSE;
+}
+
+static void show_entry() {
+       if (timeout_id) {
+               g_source_remove(timeout_id);
+               timeout_id = 0;
+       }
+       gtk_widget_show(entry_widget);
+       gdk_window_set_cursor(GTK_WIDGET(draw)->window, NULL);
 
-static void realize(GtkWindow *window, GdkScreen *screen, gpointer data) {
-       gdk_window_set_cursor(draw->window, cursor);
+       timeout_id = g_timeout_add_seconds (AUTOHIDE_TIMEOUT, (GSourceFunc)hide_entry, NULL);
 }
 
 static void clear_text(GtkAccelGroup *accel, GObject *window, guint keyval,  GdkModifierType modifier) {
-       if( gtk_text_buffer_get_char_count(tb) )
+       if( gtk_text_buffer_get_char_count(tb) ) {
                gtk_text_buffer_set_text(tb,"",-1);
-       else
+               show_entry();
+       } else {
                gtk_main_quit();
+       }
 }
 
 static char *get_text() {
@@ -59,7 +90,6 @@ static char *get_text() {
        return gtk_text_buffer_get_text(tb, &start, &end, FALSE);
 }
 
-
 static void hq(gboolean q, gboolean force){
        if (q != quality) 
                if (q)
@@ -80,15 +110,51 @@ static void redraw() {
                int w1, h1;
                pango_layout_get_pixel_size(layout, &w1, &h1);
                if (w1>0 && h1>0) {
-                       int w2 = draw->allocation.width;
-                       int h2 = draw->allocation.height;
-                       gdk_draw_layout(draw->window, gc, (w2-w1)/2,(h2-h1)/2,layout);
+                       int w2 = window->allocation.width;
+                       int h2 = window->allocation.height;
+
+                       int rw1, rh1;
+                       if (rotation == 0 || rotation == 2) {
+                               rw1 = w1;
+                               rh1 = h1;
+                       } else {
+                               rw1 = h1;
+                               rh1 = w1;
+                       }
+
+                       double s = min ((double)w2/rw1, (double)h2/rh1);
+                       
+
+                       PangoMatrix matrix = PANGO_MATRIX_INIT;
+                       // Move matrix to the screen center
+                       pango_matrix_translate(&matrix, w2/2, h2/2);
+                       // Scale as required
+                       pango_matrix_scale(&matrix, s, s);
+                       // Rotate if needed
+                       pango_matrix_rotate (&matrix, rotation * 90);
+                       // Move matrix so that text will be centered
+                       //pango_matrix_translate(&matrix, -w1/2, -h1/2);
+                       
+                       // Apply matrix
+                       PangoContext *context =  pango_layout_get_context(layout);
+                       pango_context_set_matrix (context, &matrix);
+                       pango_layout_context_changed (layout);
+
+                       gdk_draw_layout(draw->window, gc,
+                               (w2-(s*rw1))/2, (h2-(s*rh1))/2,layout);
+                       
+                       // Reset matrix
+                       PangoMatrix matrix2 = PANGO_MATRIX_INIT;
+                       pango_context_set_matrix (context, &matrix2);
+                       pango_layout_context_changed (layout);
+
                        hq(TRUE, FALSE);
                }
        }
 }
 
 static gboolean text_clicked(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
+       show_entry();
        if (event->type == GDK_BUTTON_PRESS && event->button == 2) {
                GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
 
@@ -102,37 +168,82 @@ static gboolean text_clicked(GtkWidget *widget, GdkEventButton *event, gpointer
        return FALSE;
 }
 
-static void resize() {
-       int w1, h1, w2, h2;
-       pango_layout_get_pixel_size(layout, &w1, &h1);
-       if (w1>0 && h1>0) {
-               w2 = draw->allocation.width;
-               h2 = draw->allocation.height;
-               int s = pango_font_description_get_size(font);
-               s = min ((s*w2/w1), (s*h2/h1));
-               pango_font_description_set_size(font,s);
-               pango_layout_set_font_description(layout, font);
-               need_resize = FALSE;
-       }
-       else
-               need_resize = TRUE;
+static void newtext() {
+       show_entry();
+       pango_layout_set_text(layout, get_text(), -1);
+       hq(FALSE, TRUE);
 }
 
+static void move_cursor(GtkTextView* tv, GtkMovementStep step, gint count, gboolean extend_selection, gpointer user_data) {
+       show_entry();
+}
 
-static void newtext(char *text) {
-       pango_layout_set_text(layout, get_text(), -1);
-       resize();
-       hq(FALSE, TRUE);
+static struct option const long_options[] =
+{
+       {"help",       no_argument,       NULL, 'h'},
+       {"version",    no_argument,       NULL, 'V'},
+       {"foreground", required_argument, NULL, 'f'},
+       {"background", required_argument, NULL, 'b'},
+       {"font",       required_argument, NULL, 'n'},
+       {"rotate",     required_argument, NULL, 'r'},
+       {0,0,0,0}
+};
+
+static void usage(char *cmd) {
+       printf("Usage: %s [-h|--help] [-V|--version] [-f|--foreground=colordesc] [-b|--background=colordesc] [-n|--font=fontdesc] [-r|--rotate=0,1,2,3]\n", cmd);
+}
+
+static void version() {
+       printf("%s\n", PACKAGE_STRING);
 }
 
 int main(int argc, char **argv) {
        GString *input;
+       int c;
+       int input_provided = 0;
+
+       while ((c = getopt_long (argc, argv, "hVf:b:n:r:", long_options, (int *) 0)) != EOF) {
+               switch (c) {
+                       case 'h':
+                               usage(argv[0]);
+                               return 0;
+                               break;
+
+                       case 'V':
+                               version();
+                               return 0;
+                               break;
+
+                       case 'f':
+                               foreground = optarg;
+                               break;
+
+                       case 'b':
+                               background = optarg;
+                               break;
+
+                       case 'n':
+                               fontdesc = optarg;
+                               break;
+                       case 'r':
+                               rotation = atoi(optarg);
+                               break;
+                       default:
+                               /* unknown switch received - at least
+                                * give usage but continue and use the
+                                * data */
+                               usage(argv[0]);
+                               break;
+               }
+       }
 
        gtk_init(&argc, &argv);
 
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
        gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
+       gtk_window_set_icon_name (GTK_WINDOW (window), "sm");
+
        GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(window));
        gtk_widget_set_size_request(window, gdk_screen_get_width(screen),
                                            gdk_screen_get_height(screen));
@@ -143,17 +254,23 @@ int main(int argc, char **argv) {
        GdkColormap *colormap = gtk_widget_get_colormap(GTK_WIDGET(window));
        GdkColor white, black;
        gdk_colormap_alloc_color(colormap, &white, TRUE, TRUE);
-       gdk_color_parse("white", &white);
-       gdk_color_parse("black", &black);
-       gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &white);
-       gtk_widget_modify_fg(window, GTK_STATE_NORMAL, &black);
+       gdk_colormap_alloc_color(colormap, &black, TRUE, TRUE);
+       if (foreground != NULL) {
+               gdk_color_parse(foreground, &black);
+       } else {
+               gdk_color_parse("black", &black);
+       }
+       if (background != NULL) {
+               gdk_color_parse(background, &white);
+       } else {
+               gdk_color_parse("white", &white);
+       }
 
        draw = gtk_drawing_area_new();
        gtk_widget_set_events(draw, GDK_BUTTON_PRESS_MASK);
        gtk_widget_set_size_request(draw,400,400);
        gtk_widget_modify_bg(draw, GTK_STATE_NORMAL, &white);
        gtk_widget_modify_fg(draw, GTK_STATE_NORMAL, &black);
-       g_signal_connect(G_OBJECT(draw), "realize", G_CALLBACK(realize), NULL);
        g_signal_connect(G_OBJECT(draw), "button-press-event", G_CALLBACK(text_clicked), NULL);
 
        GdkPixmap *pixmap = gdk_pixmap_new(NULL, 1, 1, 1);
@@ -161,12 +278,10 @@ int main(int argc, char **argv) {
        cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 0, 0);
 
        tv = gtk_text_view_new();
-       gtk_widget_modify_bg(tv, GTK_STATE_NORMAL, &white);
-       gtk_widget_modify_fg(tv, GTK_STATE_NORMAL, &black);
        tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
 
-       if (argc > 1)
-               if (!strcmp(argv[1], "-") ) {
+       if (argc > optind)
+               if (!strcmp(argv[optind], "-") ) {
                        // read from stdin
                        gchar buf[1024];
                        int num;
@@ -176,18 +291,25 @@ int main(int argc, char **argv) {
                        while ((num = fread (buf, 1, sizeof(buf), stdin)) > 0) {
                                g_string_append_len(input, buf, num);
                        }
+
+                       // remove trailing newline, if any
+                       if ((input->len > 0) && (input->str[input->len - 1] == '\n')) {
+                               g_string_truncate(input, input->len - 1);
+                       }
+                       input_provided++;
                } else {
                        int i;
 
                        input = g_string_new("");
 
-                       for (i = 1; i < argc; i++) {
+                       for (i = optind; i < argc; i++) {
                                g_string_append(input, argv[i]);
 
                                if (i < argc - 1) {
                                        g_string_append(input, " ");
                                }
                        }
+                       input_provided++;
                }
        else
                input = g_string_new(":-)");
@@ -199,24 +321,30 @@ int main(int argc, char **argv) {
 
        quit = gtk_button_new_from_stock(GTK_STOCK_QUIT);
        g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);
-       gtk_widget_modify_bg(quit, GTK_STATE_NORMAL, &white);
-       gtk_widget_modify_fg(quit, GTK_STATE_NORMAL, &black);
+
+       GtkWidget *vbox_button = gtk_vbox_new(FALSE, 0);
+       gtk_box_pack_end(GTK_BOX(vbox_button), quit, FALSE, FALSE, 0);
 
        GtkWidget *hbox = gtk_hbox_new(FALSE,0);
+       entry_widget = hbox;
        gtk_box_pack_start(GTK_BOX(hbox), tv,   TRUE,  TRUE,  0);
-       gtk_box_pack_start(GTK_BOX(hbox), quit, FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(hbox), vbox_button, FALSE, FALSE, 0);
 
        GtkWidget *vbox = gtk_vbox_new(FALSE,0);
-       gtk_box_pack_start(GTK_BOX(vbox), draw, TRUE,  TRUE,  0);
-       gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(vbox), draw, TRUE, TRUE, 0);
+       gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
 
        gtk_container_add(GTK_CONTAINER(window), vbox);
 
        font = pango_font_description_new();
-        pango_font_description_set_family(font, "sans-serif");
-       pango_font_description_set_size(font, 20*PANGO_SCALE);
+       if (fontdesc != NULL) {
+               pango_font_description_set_family(font, fontdesc);
+       } else {
+               pango_font_description_set_family(font, "sans-serif");
+       }
+       pango_font_description_set_size(font, 200*PANGO_SCALE);
 
-       layout=  gtk_widget_create_pango_layout(draw,get_text());
+       layout = gtk_widget_create_pango_layout(window,get_text());
        pango_layout_set_font_description(layout, font);
        pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
 
@@ -230,11 +358,14 @@ int main(int argc, char **argv) {
        gtk_window_add_accel_group(GTK_WINDOW(window), accel);
        gtk_widget_show_all(window);
 
-       g_signal_connect(G_OBJECT(draw), "configure-event", G_CALLBACK(resize), NULL);
-       g_signal_connect(G_OBJECT(draw), "expose-event", G_CALLBACK(redraw), NULL);
+       g_signal_connect_after(G_OBJECT(draw), "expose-event", G_CALLBACK(redraw), NULL);
        g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext), NULL);
+       g_signal_connect(G_OBJECT(tv), "move-cursor", G_CALLBACK(move_cursor), NULL);
 
-       resize();
+       if (!input_provided)
+               show_entry();
+       else
+               hide_entry(NULL);
 
        gtk_main();