Added FIFO thread. Currently doesn't work.
[uzbl-mobile] / uzbl.c
diff --git a/uzbl.c b/uzbl.c
index c66dd53..c2a18ca 100644 (file)
--- a/uzbl.c
+++ b/uzbl.c
 #include <gtk/gtk.h>
 #include <webkit/webkit.h>
 
+#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;
@@ -37,8 +47,6 @@ static gchar* main_title;
 static gint load_progress;
 static guint status_context_id;
 
-
-
 static gchar* uri = NULL;
 static gboolean verbose = FALSE;
 
@@ -50,8 +58,6 @@ static GOptionEntry entries[] =
 };
 
 
-
-
 static void
 activate_uri_entry_cb (GtkWidget* entry, gpointer data)
 {
@@ -157,9 +163,69 @@ static GtkWidget* create_window ()
     return window;
 }
 
+
+static bool parse_command(char *command)
+{
+  bool output;
+  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);
+      while (num > 0)
+        {
+          if ((num = read(fd, cmd, 300)) == -1)
+            perror("read");
+          else
+            {
+              cmd[num] = '\0';
+              if(! parse_command(cmd))
+                printf("Unknown command \"%s\".\n", cmd);
+            }
+        }
+      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);
 
@@ -169,19 +235,28 @@ int main (int argc, char* argv[])
 
     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);
+    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);
 
+    char *uri = "http://www.google.com";
+    if(argc == 2)
+      uri = argv[1];
 
     webkit_web_view_load_uri (web_view, uri);
 
     gtk_widget_grab_focus (GTK_WIDGET (web_view));
     gtk_widget_show_all (main_window);
+
+    pthread_t control_thread;
+    int ret;
+
+    ret = pthread_create(&control_thread, NULL, control_fifo, (void*) NULL);
     gtk_main ();
 
+    pthread_join(control_thread, NULL);
     return 0;
 }