Command parsing now works.
[uzbl-mobile] / uzbl.c
1 // Original code taken from the example webkit-gtk+ application. see notice below.
2
3 /*
4  * Copyright (C) 2006, 2007 Apple Inc.
5  * Copyright (C) 2007 Alp Toker <alp@atoker.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <gtk/gtk.h>
30 #include <webkit/webkit.h>
31
32 #include <pthread.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <unistd.h>
38
39 static GtkWidget*     main_window;
40 static WebKitWebView* web_view;
41
42 static gchar* uri         = NULL;
43 static gchar* fifodir = NULL;
44 static gint mechmode  = 0;
45 static char fifopath[64];
46
47 static GOptionEntry entries[] =
48 {
49         { "uri",      'u', 0, G_OPTION_ARG_STRING, &uri,      "Uri to load",                                   NULL },
50         { "fifo-dir", 'd', 0, G_OPTION_ARG_STRING, &fifodir,  "Directory to place FIFOs",                      NULL },
51         { "mechmode", 'm', 0, G_OPTION_ARG_INT,    &mechmode, "Enable output suitable for machine processing", NULL },
52         { NULL }
53 };
54
55 struct command
56 {
57   char command[256];
58   void (*func)(WebKitWebView*);
59 };
60
61 static struct command commands[256];
62 static int            numcmds = 0;
63
64 static GtkWidget* create_browser ()
65 {
66         GtkWidget* scrolled_window = gtk_scrolled_window_new (NULL, NULL);
67         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
68
69         web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
70         gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));
71
72         return scrolled_window;
73 }
74
75 static GtkWidget* create_window ()
76 {
77         GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
78         gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
79         gtk_widget_set_name (window, "Uzbl Browser");
80         g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
81
82         return window;
83 }
84
85 static void parse_command(char *command)
86 {
87   int  i    = 0;
88   bool done = false;
89
90   for (i = 0; i < numcmds && !done; i++)
91     {
92       printf("Looping\n");
93       if (!strcmp(command, commands[i].command))
94         {
95           printf("Parsed command \"%s\"\n", commands[i].command);
96           commands[i].func(web_view);
97           done = true;
98         }
99     }
100
101   if(!done)
102     {
103       if (!strncmp("http://", command, 7))
104         {
105           printf("Loading URI \"%s\"\n", command);
106           uri = command;
107           webkit_web_view_load_uri (web_view, uri);
108         }
109       else
110         {
111           printf("Unhandled command \"%s\"\n", command);
112         }
113     }
114 }
115
116 static void *control_fifo()
117 {
118         if (fifodir) {
119                 sprintf(fifopath, "%s/uzbl_%d", fifodir, getpid());
120         } else {
121                 sprintf(fifopath, "/tmp/uzbl_%d", getpid());
122         }
123
124         if (mkfifo (fifopath, 0666) == -1) {
125                 printf("Possible error creating fifo\n");
126         }
127
128         if (mechmode) {
129                 printf("%s\n", fifopath);
130         } else {
131                 printf ("Opened control fifo in %s\n", fifopath);
132         }
133
134         while (true)
135         {
136                 FILE *fifo = fopen(fifopath, "r");
137                 if (!fifo) {
138                         printf("Could not open %s for reading\n", fifopath);
139                         return NULL;
140                 }
141
142                 char buffer[256];
143                 memset(buffer, 0, sizeof(buffer));
144                 while (!feof(fifo) && fgets(buffer, sizeof(buffer), fifo)) {
145                         if (strcmp(buffer, "\n")) {
146                                 buffer[strlen(buffer)-1] = '\0'; // Remove newline
147                                 parse_command(buffer);
148                         }
149                 }
150         }
151
152         return NULL;
153 }
154
155 static void add_command (char* cmdstr, void* function)
156 {
157   /*struct command commands[numcmds];*/
158   /*printf("     Defining string\n");*/
159   strncpy(commands[numcmds].command, cmdstr, strlen(cmdstr));
160   /*printf("     Adding function\n");*/
161   commands[numcmds].func = function;
162   /*printf("     Incrementing count\n");*/
163   numcmds++;
164   /*printf("     Done\n");*/
165 }
166
167 static bool setup_gtk(int argc, char* argv[])
168 {
169   gtk_init (&argc, &argv);
170
171         GtkWidget* vbox = gtk_vbox_new (FALSE, 0);
172         gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
173
174         main_window = create_window ();
175         gtk_container_add (GTK_CONTAINER (main_window), vbox);
176         GError *error = NULL;
177
178         GOptionContext* context = g_option_context_new ("- The Usable Browser, controlled entirely through a FIFO");
179         g_option_context_add_main_entries (context, entries, NULL);
180         g_option_context_add_group (context, gtk_get_option_group (TRUE));
181         g_option_context_parse (context, &argc, &argv, &error);
182
183         if (uri) {
184                 webkit_web_view_load_uri (web_view, uri);
185         } else {
186                 webkit_web_view_load_uri (web_view, "http://www.google.com");
187         }
188
189         gtk_widget_grab_focus (GTK_WIDGET (web_view));
190         gtk_widget_show_all (main_window);
191
192   return true;
193 }
194
195 static void setup_commands()
196 {
197   add_command("back",    &webkit_web_view_go_back);
198   add_command("forward", &webkit_web_view_go_forward);
199 }
200
201 static bool setup_threading()
202 {
203         pthread_t control_thread;
204         pthread_create(&control_thread, NULL, control_fifo, NULL);
205
206   return true;
207 }
208
209 int main (int argc, char* argv[])
210 {
211         if (!g_thread_supported ())
212                 g_thread_init (NULL);
213
214   setup_gtk (argc, argv);
215   setup_commands ();
216   printf("%s\n", commands[0].command);
217   setup_threading ();
218         gtk_main ();
219
220         /*pthread_join (control_thread, NULL); For some reason it doesn't terminate upon the browser closing when this is enabled. */
221
222         printf ("Shutting down...\n");
223
224         // Remove FIFO
225         unlink (fifopath);
226
227         return 0;
228 }