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