use the callbacks instead of immediate webkit functions when parsing incomming comman...
[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 <gdk/gdkx.h>
33 #include <webkit/webkit.h>
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 #include <stdlib.h>
41
42 static GtkWidget* main_window;
43 static GtkWidget* uri_entry;
44 static GtkWidget* mainbar;
45 static WebKitWebView* web_view;
46 static gchar* main_title;
47 static gchar* history_file;
48 static gchar* fifodir   = NULL;
49 static char fifopath[64];
50 static gint load_progress;
51 static guint status_context_id;
52 static Window xwin = 0;
53 static gchar* uri = NULL;
54
55 static gboolean verbose = FALSE;
56
57 static GOptionEntry entries[] =
58 {
59   { "uri",     'u', 0, G_OPTION_ARG_STRING, &uri,     "Uri to load", NULL },
60   { "verbose", 'v', 0, G_OPTION_ARG_NONE,   &verbose, "Be verbose",  NULL },
61   { NULL }
62 };
63
64 struct command
65 {
66   char command[256];
67   void (*func)(WebKitWebView*);
68 };
69
70 static struct command commands[256];
71 static int            numcmds = 0;
72
73 static void
74 update_title (GtkWindow* window);
75
76
77 /* --- CALLBACKS --- */
78
79 static void
80 go_back_cb (GtkWidget* widget, gpointer data)
81 {
82     webkit_web_view_go_back (web_view);
83 }
84
85 static void
86 go_forward_cb (GtkWidget* widget, gpointer data)
87 {
88     webkit_web_view_go_forward (web_view);
89 }
90
91
92 static void
93 link_hover_cb (WebKitWebView* page, const gchar* title, const gchar* link, gpointer data)
94 {
95     /* underflow is allowed */
96     //gtk_statusbar_pop (main_statusbar, status_context_id);
97     //if (link)
98     //    gtk_statusbar_push (main_statusbar, status_context_id, link);
99     //TODO implementation roadmap pending..
100 }
101
102 static void
103 title_change_cb (WebKitWebView* web_view, WebKitWebFrame* web_frame, const gchar* title, gpointer data)
104 {
105     if (main_title)
106         g_free (main_title);
107     main_title = g_strdup (title);
108     update_title (GTK_WINDOW (main_window));
109 }
110
111 static void
112 progress_change_cb (WebKitWebView* page, gint progress, gpointer data)
113 {
114     load_progress = progress;
115     update_title (GTK_WINDOW (main_window));
116 }
117
118 static void
119 load_commit_cb (WebKitWebView* page, WebKitWebFrame* frame, gpointer data)
120 {
121     const gchar* uri = webkit_web_frame_get_uri(frame);
122     if (uri)
123         gtk_entry_set_text (GTK_ENTRY (uri_entry), uri);
124 }
125
126 static void
127 destroy_cb (GtkWidget* widget, gpointer data)
128 {
129     gtk_main_quit ();
130 }
131
132 static void
133 activate_uri_entry_cb (GtkWidget* entry, gpointer data)
134 {
135     const gchar * uri = gtk_entry_get_text (GTK_ENTRY (entry));
136     g_assert (uri);
137     webkit_web_view_load_uri (web_view, uri);
138 }
139
140 static void
141 log_history_cb () {
142     FILE * output_file = fopen(history_file, "a");
143     if (output_file == NULL) {
144        fprintf(stderr, "Cannot open %s for logging\n", history_file);
145     } else {
146         time_t rawtime;
147         struct tm * timeinfo;
148         char buffer [80];
149         time ( &rawtime );
150         timeinfo = localtime ( &rawtime );
151         strftime (buffer,80,"%Y-%m-%d %H:%M:%S",timeinfo);
152
153         fprintf(output_file, "%s %s\n",buffer, uri);
154         fclose(output_file);
155     }
156 }
157
158
159 /* -- CORE FUNCTIONS -- */
160
161 static void
162 parse_command(const char *command)
163 {
164   int  i    = 0;
165   bool done = false;
166   char *cmdstr;
167   void (*func)(WebKitWebView*);
168
169   strcpy(cmdstr, command);
170
171   for (i = 0; i < numcmds && ! done; i++)
172     {
173       if (!strncmp (cmdstr, commands[i].command, strlen (commands[i].command)))
174         {
175           func = commands[i].func;
176           done = true;
177         }
178     }
179
180   printf("command received: \"%s\"\n", cmdstr);
181
182   if (done)
183     {
184       func (web_view);
185     }
186   else
187     {
188       if (!strncmp ("http://", command, 7))
189         {
190           printf ("Loading URI \"%s\"\n", command);
191           strcpy(uri, command);
192           webkit_web_view_load_uri (web_view, uri);
193         }
194     }
195 }
196
197 static void
198 *control_fifo()
199 {
200   if (fifodir)
201     {
202       sprintf (fifopath, "%s/uzbl_%d", fifodir, (int) xwin);
203     }
204   else
205     {
206       sprintf (fifopath, "/tmp/uzbl_%d", (int) xwin);
207     }
208
209   if (mkfifo (fifopath, 0666) == -1)
210     {
211       printf ("Possible error creating fifo\n");
212     }
213
214     printf ("ontrol fifo opened in %s\n", fifopath);
215
216     while (true)
217       {
218         FILE *fifo = fopen(fifopath, "r");
219         if (!fifo)
220           {
221             printf("Could not open %s for reading\n", fifopath);
222             return NULL;
223           }
224         
225         char buffer[256];
226         memset (buffer, 0, sizeof (buffer));
227         while (!feof (fifo) && fgets (buffer, sizeof (buffer), fifo))
228           {
229             if (strcmp (buffer, "\n"))
230               {
231                 buffer[strlen (buffer) - 1] = '\0'; // Remove newline
232                 parse_command (buffer);
233               }
234           }
235       }
236     
237     return NULL;
238 }
239
240 static void
241 add_command (char* cmdstr, void* function)
242 {
243   strncpy (commands[numcmds].command, cmdstr, strlen (cmdstr));
244   commands[numcmds].func = function;
245   numcmds++;
246 }
247
248 static void
249 setup_commands ()
250 {
251   // This func. is nice but currently it cannot be used for functions that require arguments or return data. --sentientswitch
252   // TODO: reload, home
253   add_command("back",     &go_back_cb);
254   add_command("forward",  &go_forward_cb);
255   add_command("refresh",  &webkit_web_view_reload); //Buggy
256   add_command("stop",     &webkit_web_view_stop_loading);
257   add_command("zoom_in",  &webkit_web_view_zoom_in); //Can crash (when max zoom reached?).
258   add_command("zoom_out", &webkit_web_view_zoom_out);
259   //add_command("get uri", &webkit_web_view_get_uri);
260 }
261
262 static void
263 setup_threading ()
264 {
265   pthread_t control_thread;
266   pthread_create(&control_thread, NULL, control_fifo, NULL);
267 }
268
269
270
271
272 static void
273 update_title (GtkWindow* window)
274 {
275     GString* string = g_string_new (main_title);
276     g_string_append (string, " - Uzbl browser");
277     if (load_progress < 100)
278         g_string_append_printf (string, " (%d%%)", load_progress);
279     gchar* title = g_string_free (string, FALSE);
280     gtk_window_set_title (window, title);
281     g_free (title);
282 }
283
284 static GtkWidget*
285 create_browser ()
286 {
287     GtkWidget* scrolled_window = gtk_scrolled_window_new (NULL, NULL);
288     gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_NEVER, GTK_POLICY_NEVER); //todo: some sort of display of position/total length. like what emacs does
289
290     web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
291     gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));
292
293     g_signal_connect (G_OBJECT (web_view), "title-changed", G_CALLBACK (title_change_cb), web_view);
294     g_signal_connect (G_OBJECT (web_view), "load-progress-changed", G_CALLBACK (progress_change_cb), web_view);
295     g_signal_connect (G_OBJECT (web_view), "load-committed", G_CALLBACK (load_commit_cb), web_view);
296     g_signal_connect (G_OBJECT (web_view), "load-committed", G_CALLBACK (log_history_cb), web_view);
297     g_signal_connect (G_OBJECT (web_view), "hovering-over-link", G_CALLBACK (link_hover_cb), web_view);
298
299     return scrolled_window;
300 }
301
302 static GtkWidget*
303 create_mainbar ()
304 {
305     mainbar = gtk_hbox_new(FALSE, 0);
306     uri_entry = gtk_entry_new();
307     gtk_entry_set_width_chars(GTK_ENTRY(uri_entry), 40);
308     gtk_entry_set_text(GTK_ENTRY(uri_entry), "http://");
309     gtk_box_pack_start (GTK_BOX (mainbar), uri_entry, FALSE,TRUE , 0);
310     gtk_signal_connect_object (GTK_OBJECT (uri_entry), "activate", GTK_SIGNAL_FUNC (activate_uri_entry_cb), GTK_OBJECT (uri_entry));
311
312     //status_context_id = gtk_statusbar_get_context_id (main_statusbar, "Link Hover");
313
314     return mainbar;
315 }
316
317 static
318 GtkWidget* create_window ()
319 {
320     GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
321     gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
322     gtk_widget_set_name (window, "Uzbl browser");
323     g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy_cb), NULL);
324
325     return window;
326 }
327
328 int main (int argc, char* argv[])
329 {
330     gtk_init (&argc, &argv);
331     if (!g_thread_supported ())
332         g_thread_init (NULL);
333
334     GKeyFile* config = g_key_file_new ();
335     gboolean res = g_key_file_load_from_file (config, "./sampleconfig", G_KEY_FILE_NONE, NULL); //TODO: pass config file as argument
336     if(res) {
337         printf("config loaded\n");
338     } else {
339         fprintf(stderr,"config loading failed\n"); //TODO: exit codes with gtk? 
340     }
341     history_file = g_key_file_get_value (config, "behavior", "history_file", NULL);
342     if(history_file) {
343         printf("history file: %s\n",history_file);
344     } else {
345         printf("history logging disabled\n");
346     }
347
348     GtkWidget* vbox = gtk_vbox_new (FALSE, 0);
349     gtk_box_pack_start (GTK_BOX (vbox), create_mainbar (), FALSE, TRUE, 0);
350     gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
351
352
353
354     main_window = create_window ();
355     gtk_container_add (GTK_CONTAINER (main_window), vbox);
356   GError *error = NULL;
357
358   GOptionContext* context = g_option_context_new ("- some stuff here maybe someday");
359   g_option_context_add_main_entries (context, entries, NULL);
360   g_option_context_add_group (context, gtk_get_option_group (TRUE));
361   g_option_context_parse (context, &argc, &argv, &error);
362
363
364     webkit_web_view_load_uri (web_view, uri);
365
366     gtk_widget_grab_focus (GTK_WIDGET (web_view));
367     gtk_widget_show_all (main_window);
368     xwin = GDK_WINDOW_XID (GTK_WIDGET (main_window)->window);
369     printf("window_id %i\n",(int) xwin);
370     printf("pid %i\n", getpid ());
371
372     setup_commands ();
373     setup_threading ();
374
375     gtk_main ();
376
377     unlink (fifopath);
378     return 0;
379 }