Added command/insert modes (Esc to switch).
[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/gdk.h>
33 #include <gdk/gdkx.h>
34 #include <gdk/gdkkeys.h>
35 #include <gdk/gdkkeysyms.h>
36 #include <webkit/webkit.h>
37
38 #include <pthread.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <sys/stat.h>
42 #include <sys/types.h>
43 #include <unistd.h>
44
45 static GtkWidget*     main_window;
46 static GtkWidget*     modeline;
47 static WebKitWebView* web_view;
48
49 static gchar* history_file;
50 static gchar* home_page;
51 static gchar* uri       = NULL;
52 static gchar* fifodir   = NULL;
53 static gint   mechmode  = 0;
54 static char   fifopath[64];
55 static bool   modevis = FALSE;
56
57 static GOptionEntry entries[] =
58 {
59   { "uri",      'u', 0, G_OPTION_ARG_STRING, &uri,      "Uri to load",                                   NULL },
60   { "fifo-dir", 'd', 0, G_OPTION_ARG_STRING, &fifodir,  "Directory to place FIFOs",                      NULL },
61   { "mechmode", 'm', 0, G_OPTION_ARG_INT,    &mechmode, "Enable output suitable for machine processing", NULL },
62   { NULL }
63 };
64
65 struct command
66 {
67   char command[256];
68   void (*func)(WebKitWebView*);
69 };
70
71 static struct command commands[256];
72 static int            numcmds = 0;
73
74 static void parse_command(char*);
75
76 static bool parse_modeline (GtkWidget* mode, GdkEventKey* event)
77 {
78   parse_command (gtk_entry_get_text (modeline));
79   return false;
80 }
81
82 static void log_history_cb (WebKitWebView* page, WebKitWebFrame* frame, gpointer data)
83 {
84   strncpy (uri, webkit_web_frame_get_uri (frame), strlen (webkit_web_frame_get_uri (frame)));
85   
86   FILE * output_file = fopen (history_file, "a");
87   if (output_file == NULL)
88     {
89       fprintf (stderr, "Cannot open %s for logging\n", history_file);
90     }
91   else
92     {
93       time_t rawtime;
94       struct tm * timeinfo;
95       char buffer [80];
96       time (&rawtime);
97       timeinfo = localtime (&rawtime);
98       strftime (buffer,80,"%Y-%m-%d %H:%M:%S",timeinfo);
99       
100       fprintf (output_file, "%s %s\n",buffer, uri);
101       fclose (output_file);
102     }
103 }
104
105 static void toggle_command_mode ()
106 {
107   if (modevis)
108     {
109       gtk_widget_hide (modeline);
110       gtk_widget_grab_default (modeline);
111     }
112   else
113     {
114       gtk_widget_show (modeline);
115       gtk_widget_grab_focus (modeline);
116     }
117   modevis = ! modevis;
118 }
119
120 static gboolean key_press_cb (WebKitWebView* page, GdkEventKey* event)
121 {
122   gboolean result=FALSE; //TRUE to stop other handlers from being invoked for the event. FALSE to propagate the event further.
123   if ((event->type==GDK_KEY_PRESS) && (event->keyval==GDK_Escape))
124     {
125       toggle_command_mode ();
126       result=TRUE;
127     }
128  
129   return(result);
130 }
131
132 static GtkWidget* create_browser ()
133 {
134   GtkWidget* scrolled_window = gtk_scrolled_window_new (NULL, NULL);
135   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
136
137   web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
138   gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));
139
140   g_signal_connect (G_OBJECT (web_view), "load-committed", G_CALLBACK (log_history_cb), web_view);
141
142   return scrolled_window;
143 }
144
145 static GtkWidget* create_window ()
146 {
147   GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
148   gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
149   gtk_widget_set_name (window, "Uzbl Browser");
150   g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
151   g_signal_connect (G_OBJECT (window), "key-press-event", G_CALLBACK(key_press_cb), NULL);
152
153   return window;
154 }
155
156 static GtkWidget* create_modeline ()
157 {
158   GtkWidget* modeline = gtk_entry_new ();
159   g_signal_connect (G_OBJECT (modeline), "key-press-event", G_CALLBACK(parse_modeline), modeline);
160
161   return modeline;
162 }
163
164 static void parse_command(char *command)
165 {
166   int  i    = 0;
167   bool done = false;
168
169   for (i = 0; i < numcmds && !done; i++)
170     {
171       if (!strcmp(command, commands[i].command))
172         {
173           printf("Parsed command \"%s\"\n", commands[i].command);
174           commands[i].func(web_view);
175           done = true;
176         }
177     }
178
179   if(!done)
180     {
181       if (!strncmp("http://", command, 7))
182         {
183           printf("Loading URI \"%s\"\n", command);
184           uri = command;
185           webkit_web_view_load_uri (web_view, uri);
186         }
187       else
188         {
189           printf("Unhandled command \"%s\"\n", command);
190         }
191     }
192 }
193
194 static void *control_fifo()
195 {
196   if (fifodir) {
197     sprintf(fifopath, "%s/uzbl_%d", fifodir, getpid());
198   } else {
199     sprintf(fifopath, "/tmp/uzbl_%d", getpid());
200   }
201
202   if (mkfifo (fifopath, 0666) == -1) {
203     printf("Possible error creating fifo\n");
204   }
205
206   if (mechmode) {
207     printf("%s\n", fifopath);
208   } else {
209     printf ("Opened control fifo in %s\n", fifopath);
210   }
211
212   while (true)
213   {
214     FILE *fifo = fopen(fifopath, "r");
215     if (!fifo) {
216       printf("Could not open %s for reading\n", fifopath);
217       return NULL;
218     }
219
220     char buffer[256];
221     memset(buffer, 0, sizeof(buffer));
222     while (!feof(fifo) && fgets(buffer, sizeof(buffer), fifo)) {
223       if (strcmp(buffer, "\n")) {
224         buffer[strlen(buffer)-1] = '\0'; // Remove newline
225         parse_command(buffer);
226       }
227     }
228   }
229
230   return NULL;
231 }
232
233 static void add_command (char* cmdstr, void* function)
234 {
235   strncpy(commands[numcmds].command, cmdstr, strlen(cmdstr));
236   commands[numcmds].func = function;
237   numcmds++;
238 }
239
240 static bool setup_gtk (int argc, char* argv[])
241 {
242   gtk_init (&argc, &argv);
243
244   GtkWidget* vbox = gtk_vbox_new (FALSE, 0);
245   gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
246   modeline = create_modeline ();
247   gtk_box_pack_start (GTK_BOX (vbox), modeline, FALSE, FALSE, 0);
248
249   main_window = create_window ();
250   gtk_container_add (GTK_CONTAINER (main_window), vbox);
251   GError *error = NULL;
252
253   GOptionContext* context = g_option_context_new ("- The Usable Browser, controlled entirely through a FIFO");
254   g_option_context_add_main_entries (context, entries, NULL);
255   g_option_context_add_group (context, gtk_get_option_group (TRUE));
256   g_option_context_parse (context, &argc, &argv, &error);
257
258   if (uri)
259     {
260       webkit_web_view_load_uri (web_view, uri);
261     }
262
263   gtk_widget_grab_focus (GTK_WIDGET (web_view));
264   gtk_widget_show_all (main_window);
265   gtk_widget_hide(modeline);
266
267   return true;
268 }
269
270 static void setup_commands ()
271 {
272   //This func. is nice but currently it cannot be used for functions that require arguments or return data. --sentientswitch
273
274   //Commands are grouped:
275   //    nav     -       commands that actually navigate to different pages
276   //    view    -       commands that affect how the page is rendered
277   //    get     -       commands that return properties
278   //    set     -       commands that return properties
279   //add more as necessary.
280
281   add_command("nav back",    &webkit_web_view_go_back);
282   add_command("nav forward", &webkit_web_view_go_forward);
283   add_command("nav reload", &webkit_web_view_reload); //Buggy
284   add_command("nav stop", &webkit_web_view_stop_loading);
285   add_command("view zoom +", &webkit_web_view_zoom_in); //Can crash (when max zoom reached?).
286   add_command("view zoom -", &webkit_web_view_zoom_out); //Crashes as zoom +
287   //add_command("get uri", &webkit_web_view_get_uri);
288 }
289
290 static void setup_threading ()
291 {
292   pthread_t control_thread;
293   pthread_create(&control_thread, NULL, control_fifo, NULL);
294 }
295
296 static void setup_settings ()
297 {
298   GKeyFile* config = g_key_file_new ();
299   gboolean  res    = g_key_file_load_from_file (config, "./config", G_KEY_FILE_NONE, NULL); //TODO: pass config file as argument
300
301   if (res)
302     {
303       printf ("Config loaded\n");
304     }
305   else
306     {
307       fprintf (stderr, "config loading failed\n"); //TODO: exit codes with gtk? 
308     }
309
310   history_file = g_key_file_get_value (config, "behavior", "history_file", NULL);
311   if (history_file)
312     {
313       printf ("Setting history file to: %s\n", history_file);
314     }
315   else
316     {
317       printf ("History logging disabled\n");
318     }
319
320   home_page = g_key_file_get_value (config, "behavior", "home_page", NULL);
321   if (home_page)
322     {
323       printf ("Setting home page to: %s\n", home_page);
324     }
325   else
326     {
327       printf ("Home page disabled\n");
328     }
329 }
330
331 int main (int argc, char* argv[])
332 {
333   if (!g_thread_supported ())
334     g_thread_init (NULL);
335
336   setup_settings ();
337   setup_gtk (argc, argv);
338   setup_commands ();
339   setup_threading ();
340   gtk_main ();
341
342   printf ("Shutting down...\n");
343
344   unlink (fifopath);
345
346   return 0;
347 }