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