Tidied up the code a little.
[uzbl-mobile] / uzbl.c
diff --git a/uzbl.c b/uzbl.c
index 4a34476..f00fb6c 100644 (file)
--- a/uzbl.c
+++ b/uzbl.c
 #include <gtk/gtk.h>
 #include <webkit/webkit.h>
 
-static GtkWidget* main_window;
-static GtkWidget* uri_entry;
-static GtkStatusbar* main_statusbar;
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <pthread.h>
+
+static GtkWidget*     main_window;
+static GtkWidget*     uri_entry;
+static GtkStatusbar*  main_statusbar;
 static WebKitWebView* web_view;
-static gchar* main_title;
-static gint load_progress;
-static guint status_context_id;
+static gchar*         main_title;
+static gint           load_progress;
+static guint          status_context_id;
 
-static void
-activate_uri_entry_cb (GtkWidget* entry, gpointer data)
+static gchar* uri          = NULL;
+static gboolean verbose    = FALSE;
+
+static GOptionEntry entries[] =
+{
+    { "uri",     'u',  0, G_OPTION_ARG_STRING, &uri,     "Uri to load", NULL },
+    { "verbose", 'v',  0, G_OPTION_ARG_NONE,   &verbose, "Be verbose", NULL },
+    { NULL }
+};
+
+
+static void activate_uri_entry_cb (GtkWidget* entry, gpointer data)
 {
     const gchar* uri = gtk_entry_get_text (GTK_ENTRY (entry));
     g_assert (uri);
     webkit_web_view_load_uri (web_view, uri);
 }
 
-static void
-update_title (GtkWindow* window)
+static void update_title (GtkWindow* window)
 {
     GString* string = g_string_new (main_title);
     g_string_append (string, " - Uzbl browser");
@@ -57,8 +76,7 @@ update_title (GtkWindow* window)
     g_free (title);
 }
 
-static void
-link_hover_cb (WebKitWebView* page, const gchar* title, const gchar* link, gpointer data)
+static void link_hover_cb (WebKitWebView* page, const gchar* title, const gchar* link, gpointer data)
 {
     /* underflow is allowed */
     gtk_statusbar_pop (main_statusbar, status_context_id);
@@ -66,8 +84,7 @@ link_hover_cb (WebKitWebView* page, const gchar* title, const gchar* link, gpoin
         gtk_statusbar_push (main_statusbar, status_context_id, link);
 }
 
-static void
-title_change_cb (WebKitWebView* web_view, WebKitWebFrame* web_frame, const gchar* title, gpointer data)
+static void title_change_cb (WebKitWebView* web_view, WebKitWebFrame* web_frame, const gchar* title, gpointer data)
 {
     if (main_title)
         g_free (main_title);
@@ -75,44 +92,38 @@ title_change_cb (WebKitWebView* web_view, WebKitWebFrame* web_frame, const gchar
     update_title (GTK_WINDOW (main_window));
 }
 
-static void
-progress_change_cb (WebKitWebView* page, gint progress, gpointer data)
+static void progress_change_cb (WebKitWebView* page, gint progress, gpointer data)
 {
     load_progress = progress;
     update_title (GTK_WINDOW (main_window));
 }
 
-static void
-load_commit_cb (WebKitWebView* page, WebKitWebFrame* frame, gpointer data)
+static void load_commit_cb (WebKitWebView* page, WebKitWebFrame* frame, gpointer data)
 {
     const gchar* uri = webkit_web_frame_get_uri(frame);
     if (uri)
         gtk_entry_set_text (GTK_ENTRY (uri_entry), uri);
 }
 
-static void
-destroy_cb (GtkWidget* widget, gpointer data)
+static void destroy_cb (GtkWidget* widget, gpointer data)
 {
     gtk_main_quit ();
 }
 
-static void
-go_back_cb (GtkWidget* widget, gpointer data)
+static void go_back_cb (GtkWidget* widget, gpointer data)
 {
     webkit_web_view_go_back (web_view);
 }
 
-static void
-go_forward_cb (GtkWidget* widget, gpointer data)
+static void go_forward_cb (GtkWidget* widget, gpointer data)
 {
     webkit_web_view_go_forward (web_view);
 }
 
-static GtkWidget*
-create_browser ()
+static GtkWidget* create_browser ()
 {
     GtkWidget* scrolled_window = gtk_scrolled_window_new (NULL, NULL);
-    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
+    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
 
     web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
     gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));
@@ -125,8 +136,7 @@ create_browser ()
     return scrolled_window;
 }
 
-static GtkWidget*
-create_statusbar ()
+static GtkWidget* create_statusbar ()
 {
     main_statusbar = GTK_STATUSBAR (gtk_statusbar_new ());
     status_context_id = gtk_statusbar_get_context_id (main_statusbar, "Link Hover");
@@ -144,25 +154,98 @@ static GtkWidget* create_window ()
     return window;
 }
 
+
+static bool parse_command(char *command)
+{
+    bool output = true;
+
+    if(strcmp(command, "forward") == 0)
+    {
+        printf("Going forward\n");
+        webkit_web_view_go_forward (web_view);
+    }
+    else if(strcmp(command, "back") == 0)
+    {
+        printf("Going back\n");
+        webkit_web_view_go_back (web_view);
+    }
+    else if(strncmp("http://", command, 7) == 0)
+    {
+        printf("Loading URI \"%s\"\n", command);
+        uri = command;
+        webkit_web_view_load_uri (web_view, uri);
+    }
+    else
+    {
+        output = false;
+    }
+
+    return output;
+}
+
+static void control_fifo()
+{
+    char *cmd = (char *)malloc(1024);
+    int num, fd;
+    char *fifoname = "/tmp/uzbl";
+
+    umask (0);
+    mknod (fifoname, S_IFIFO | 0666 , 0); /* Do some stuff to work with multiple instances later foo-$PID or something */
+    printf ("Opened control fifo in %s\n", fifoname);
+
+    while (true)
+    {
+        fd = open (fifoname, O_RDONLY);
+        printf ("Looping...\n");
+        while (num > 0)
+        {
+            if ((num = read(fd, cmd, 300)) == -1)
+                perror("read");
+            else
+            {
+                cmd[num - 1] = '\0';
+                if(! parse_command(cmd))
+                    printf("Unknown command \"%s\".\n", cmd);
+                cmd[0] = '\0';
+            }
+        }
+        num = 1;
+    }
+    printf("Oops, this code should never be run.\n");
+}
+
 int main (int argc, char* argv[])
 {
-    gtk_init (&argc, &argv);
     if (!g_thread_supported ())
         g_thread_init (NULL);
 
+    gtk_init (&argc, &argv);
+
     GtkWidget* vbox = gtk_vbox_new (FALSE, 0);
     gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
-    gtk_box_pack_start (GTK_BOX (vbox), create_statusbar (), FALSE, FALSE, 0);
+
+    if(! hidestatus)
+        gtk_box_pack_start (GTK_BOX (vbox), create_statusbar (), FALSE, FALSE, 0);
 
     main_window = create_window ();
     gtk_container_add (GTK_CONTAINER (main_window), vbox);
+    GError *error = NULL;
+    
+    GOptionContext* context = g_option_context_new ("- some stuff here maybe someday");
+    g_option_context_add_main_entries (context, entries, NULL);
+    g_option_context_add_group (context, gtk_get_option_group (TRUE));
+    g_option_context_parse (context, &argc, &argv, &error);
 
-    gchar* uri = (gchar*) (argc > 1 ? argv[1] : "http://www.google.com/");
     webkit_web_view_load_uri (web_view, uri);
 
     gtk_widget_grab_focus (GTK_WIDGET (web_view));
     gtk_widget_show_all (main_window);
-    gtk_main ();
 
+    pthread_t control_thread;
+    int ret;
+
+    ret = pthread_create(&control_thread, NULL, control_fifo, (void*) NULL);
+    gtk_main ();
+    /*pthread_join(control_thread, NULL); For some reason it doesn't terminate upon the browser closing when this is enabled. */
     return 0;
 }