da81fb3575ed85886e3612aa326d4fcaf6b89b38
[uzbl-mobile] / uzbl.c
1 /* -*- c-basic-offset: 4; */
2 // Original code taken from the example webkit-gtk+ application. see notice below.
3 // Modified code is licensed under the GPL 3.  See LICENSE file.
4
5
6 /*
7  * Copyright (C) 2006, 2007 Apple Inc.
8  * Copyright (C) 2007 Alp Toker <alp@atoker.com>
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32
33 #define LENGTH(x) (sizeof x / sizeof x[0])
34 #define MAX_BINDINGS 256
35
36 #include <gtk/gtk.h>
37 #include <gdk/gdkx.h>
38 #include <gdk/gdkkeysyms.h>
39 #include <sys/socket.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <sys/un.h>
43 #include <webkit/webkit.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <errno.h>
49 #include <string.h>
50 #include <fcntl.h>
51 #include <sys/socket.h>
52 #include <sys/un.h>
53 #include <libsoup/soup.h>
54 #include "uzbl.h"
55
56 /* housekeeping / internal variables */
57 static GtkWidget*     main_window;
58 static GtkWidget*     mainbar;
59 static GtkWidget*     mainbar_label;
60 static GtkScrollbar*  scbar_v;   // Horizontal and Vertical Scrollbar 
61 static GtkScrollbar*  scbar_h;   // (These are still hidden)
62 static GtkAdjustment* bar_v; // Information about document length
63 static GtkAdjustment* bar_h; // and scrolling position
64 static WebKitWebView* web_view;
65 static gchar*         main_title;
66 static gchar          selected_url[500] = "\0";
67 static gint           load_progress;
68 static Window         xwin = 0;
69 static char           fifo_path[64];
70 static char           socket_path[108];
71 static char           executable_path[500];
72 static GString*       keycmd;
73
74 /* state variables (initial values coming from command line arguments but may be changed later) */
75 static gchar*   uri         = NULL;
76 static gchar*   config_file = NULL;
77 static gchar    config_file_path[500];
78 static gboolean verbose     = FALSE;
79 static gchar*   instance_name   = NULL;
80
81 /* settings from config: group behaviour */
82 static gchar*   history_handler    = NULL;
83 static gchar*   fifo_dir           = NULL;
84 static gchar*   socket_dir         = NULL;
85 static gchar*   download_handler   = NULL;
86 static gboolean always_insert_mode = FALSE;
87 static gboolean show_status        = FALSE;
88 static gboolean insert_mode        = FALSE;
89 static gboolean status_top         = FALSE;
90 static gchar*   modkey             = NULL;
91 static guint    modmask            = 0;
92 static guint    http_debug         = 0;
93
94 /* settings from config: group bindings, key -> action */
95 static GHashTable* bindings;
96
97 /* command list: name -> Command  */
98 static GHashTable* commands;
99
100 /* commandline arguments (set initial values for the state variables) */
101 static GOptionEntry entries[] =
102 {
103     { "uri",     'u', 0, G_OPTION_ARG_STRING, &uri,           "Uri to load", NULL },
104     { "name",    'n', 0, G_OPTION_ARG_STRING, &instance_name, "Name of the current instance", NULL },
105     { "verbose", 'v', 0, G_OPTION_ARG_NONE,   &verbose,       "Be verbose",  NULL },
106     { "config",  'c', 0, G_OPTION_ARG_STRING, &config_file,   "Config file", NULL },
107     { NULL,      0, 0, 0, NULL, NULL, NULL }
108 };
109
110 typedef void (*Command)(WebKitWebView*, const char *);
111
112 /* XDG stuff */
113 static char *XDG_CONFIG_HOME_default[256];
114 static char *XDG_CONFIG_DIRS_default = "/etc/xdg";
115
116 /* libsoup stuff - proxy and friends; networking aptions actually */
117 static SoupSession *soup_session;
118 static SoupLogger *soup_logger;
119 static char *proxy_url = NULL;
120 static char *useragent = NULL;
121 static gint max_conns;
122 static gint max_conns_host;
123
124 /* --- UTILITY FUNCTIONS --- */
125 void
126 eprint(const char *errstr, ...) {
127         va_list ap;
128         vfprintf(stderr, errstr, ap);
129         va_end(ap);
130         exit(EXIT_FAILURE);
131 }
132
133 char *
134 estrdup(const char *str) {
135         void *res = strdup(str);
136         if(!res)
137             eprint("fatal: could not allocate %u bytes\n", strlen(str));
138         return res;
139 }
140
141 char *
142 itos(int val) {
143     char tmp[20];
144
145     snprintf(tmp, sizeof(tmp), "%i", val);
146     return estrdup(tmp);
147 }
148
149 /* --- CALLBACKS --- */
150
151 static gboolean
152 new_window_cb (WebKitWebView *web_view, WebKitWebFrame *frame, WebKitNetworkRequest *request, WebKitWebNavigationAction *navigation_action, WebKitWebPolicyDecision *policy_decision, gpointer user_data) {
153     (void) web_view;
154     (void) frame;
155     (void) navigation_action;
156     (void) policy_decision;
157     (void) user_data;
158     const gchar* uri = webkit_network_request_get_uri (request);
159     printf("New window requested -> %s \n", uri);
160     new_window_load_uri(uri);
161     return (FALSE);
162 }
163
164 WebKitWebView*
165 create_web_view_cb (WebKitWebView  *web_view, WebKitWebFrame *frame, gpointer user_data) {
166     (void) web_view;
167     (void) frame;
168     (void) user_data;
169     if (selected_url[0]!=0) {
170         printf("\nNew web view -> %s\n",selected_url);
171         new_window_load_uri(selected_url);
172     } else {
173         printf("New web view -> %s\n","Nothing to open, exiting");
174     }
175     return (NULL);
176 }
177
178 static gboolean
179 download_cb (WebKitWebView *web_view, GObject *download, gpointer user_data) {
180     (void) web_view;
181     (void) user_data;
182     if (download_handler) {
183         const gchar* uri = webkit_download_get_uri ((WebKitDownload*)download);
184         printf("Download -> %s\n",uri);
185         run_command(download_handler, uri);
186     }
187     return (FALSE);
188 }
189
190 /* scroll a bar in a given direction */
191 static void
192 scroll (GtkAdjustment* bar, const char *param) {
193     gdouble amount;
194     gchar *end;
195
196     amount = g_ascii_strtod(param, &end);
197
198     if (*end)
199         fprintf(stderr, "found something after double: %s\n", end);
200
201     gtk_adjustment_set_value (bar, gtk_adjustment_get_value(bar)+amount);
202 }
203
204 static void scroll_vert(WebKitWebView* page, const char *param) {
205     (void) page;
206
207     scroll(bar_v, param);
208 }
209
210 static void scroll_horz(WebKitWebView* page, const char *param) {
211     (void) page;
212
213     scroll(bar_h, param);
214 }
215
216 static void
217 toggle_status_cb (WebKitWebView* page, const char *param) {
218     (void)page;
219     (void)param;
220
221     if (show_status) {
222         gtk_widget_hide(mainbar);
223     } else {
224         gtk_widget_show(mainbar);
225     }
226     show_status = !show_status;
227     update_title();
228 }
229
230 static void
231 link_hover_cb (WebKitWebView* page, const gchar* title, const gchar* link, gpointer data) {
232     (void) page;
233     (void) title;
234     (void) data;    
235     //ADD HOVER URL TO WINDOW TITLE
236     selected_url[0] = '\0';
237     if (link) {
238         strcpy (selected_url, link);
239     }
240     update_title();
241 }
242
243 static void
244 title_change_cb (WebKitWebView* web_view, WebKitWebFrame* web_frame, const gchar* title, gpointer data) {
245     (void) web_view;
246     (void) web_frame;
247     (void) data;
248     if (main_title)
249         g_free (main_title);
250     main_title = g_strdup (title);
251     update_title();
252 }
253
254 static void
255 progress_change_cb (WebKitWebView* page, gint progress, gpointer data) {
256     (void) page;
257     (void) data;
258     load_progress = progress;
259     update_title();
260 }
261
262 static void
263 load_commit_cb (WebKitWebView* page, WebKitWebFrame* frame, gpointer data) {
264     (void) page;
265     (void) data;
266     free (uri);
267     GString* newuri = g_string_new (webkit_web_frame_get_uri (frame));
268     uri = g_string_free (newuri, FALSE);
269 }
270
271 static void
272 destroy_cb (GtkWidget* widget, gpointer data) {
273     (void) widget;
274     (void) data;
275     gtk_main_quit ();
276 }
277
278 static void
279 log_history_cb () {
280    if (history_handler) {
281        time_t rawtime;
282        struct tm * timeinfo;
283        char date [80];
284        time ( &rawtime );
285        timeinfo = localtime ( &rawtime );
286        strftime (date, 80, "%Y-%m-%d %H:%M:%S", timeinfo);
287        GString* args = g_string_new ("");
288        g_string_printf (args, "'%s'", date);
289        run_command(history_handler, args->str);
290        g_string_free (args, TRUE);
291    }
292 }
293
294 /* VIEW funcs (little webkit wrappers) */
295
296 #define VIEWFUNC(name) static void view_##name(WebKitWebView *page, const char *param){(void)param; webkit_web_view_##name(page);}
297 VIEWFUNC(reload)
298 VIEWFUNC(stop_loading)
299 VIEWFUNC(zoom_in)
300 VIEWFUNC(zoom_out)
301 VIEWFUNC(go_back)
302 VIEWFUNC(go_forward)
303 #undef VIEWFUNC
304
305 /* -- command to callback/function map for things we cannot attach to any signals */
306 // TODO: reload
307
308 static struct {char *name; Command command;} cmdlist[] =
309 {
310     { "back",          view_go_back       },
311     { "forward",       view_go_forward    },
312     { "scroll_vert",   scroll_vert        },
313     { "scroll_horz",   scroll_horz        },
314     { "reload",        view_reload,       }, //Buggy
315     { "refresh",       view_reload,       }, /* for convenience, will change */
316     { "stop",          view_stop_loading, },
317     { "zoom_in",       view_zoom_in,      }, //Can crash (when max zoom reached?).
318     { "zoom_out",      view_zoom_out,     },
319     { "uri",           load_uri           },
320     { "toggle_status", toggle_status_cb   },
321     { "spawn",         spawn              },
322     { "exit",          close_uzbl         },
323     { "insert_mode",   set_insert_mode    }
324 };
325
326 static void
327 commands_hash(void)
328 {
329     unsigned int i;
330     commands = g_hash_table_new(g_str_hash, g_str_equal);
331
332     for (i = 0; i < LENGTH(cmdlist); i++)
333         g_hash_table_insert(commands, cmdlist[i].name, cmdlist[i].command);
334 }
335
336 /* -- CORE FUNCTIONS -- */
337
338 void
339 free_action(gpointer act) {
340     Action *action = (Action*)act;
341     g_free(action->name);
342     if (action->param)
343         g_free(action->param);
344     g_free(action);
345 }
346
347 Action*
348 new_action(const gchar *name, const gchar *param) {
349     Action *action = g_new(Action, 1);
350
351     action->name = g_strdup(name);
352     if (param)
353         action->param = g_strdup(param);
354     else
355         action->param = NULL;
356
357     return action;
358 }
359
360 static bool
361 file_exists (const char * filename) {
362     FILE *file = fopen (filename, "r");
363     if (file) {
364         fclose (file);
365         return true;
366     }
367     return false;
368 }
369
370 void
371 set_insert_mode(WebKitWebView *page, const gchar *param) {
372     (void)page;
373     (void)param;
374
375     insert_mode = TRUE;
376     update_title();
377 }
378
379 static void
380 load_uri (WebKitWebView * web_view, const gchar *param) {
381     if (param) {
382         GString* newuri = g_string_new (param);
383         if (g_strrstr (param, "://") == NULL)
384             g_string_prepend (newuri, "http://"); 
385         webkit_web_view_load_uri (web_view, newuri->str);
386         g_string_free (newuri, TRUE);
387     }
388 }
389
390 static void
391 new_window_load_uri (const gchar * uri) {
392     GString* to_execute = g_string_new ("");
393     g_string_append_printf (to_execute, "%s --uri '%s'", executable_path, uri);
394     int i;
395     for (i = 0; entries[i].long_name != NULL; i++) {
396         if ((entries[i].arg == G_OPTION_ARG_STRING) && (strcmp(entries[i].long_name,"uri")!=0)) {
397             gchar** str = (gchar**)entries[i].arg_data;
398             if (*str!=NULL) {
399                 g_string_append_printf (to_execute, " --%s '%s'", entries[i].long_name, *str);
400             }
401         }
402     }
403     printf("\n%s\n", to_execute->str);
404     g_spawn_command_line_async (to_execute->str, NULL);
405     g_string_free (to_execute, TRUE);
406 }
407
408 static void
409 close_uzbl (WebKitWebView *page, const char *param) {
410     (void)page;
411     (void)param;
412     gtk_main_quit ();
413 }
414
415 // make sure to put '' around args, so that if there is whitespace we can still keep arguments together.
416 static gboolean
417 run_command(const char *command, const char *args) {
418    //command <uzbl conf> <uzbl pid> <uzbl win id> <uzbl fifo file> <uzbl socket file> [args]
419     GString* to_execute = g_string_new ("");
420     gboolean result;
421     g_string_printf (to_execute, "%s '%s' '%i' '%i' '%s' '%s'", command, config_file, (int) getpid() , (int) xwin, fifo_path, socket_path);
422     g_string_append_printf (to_execute, " '%s' '%s'", uri, "TODO title here");
423     if(args) {
424         g_string_append_printf (to_execute, " %s", args);
425     }
426     result = g_spawn_command_line_async (to_execute->str, NULL);
427     printf("Called %s.  Result: %s\n", to_execute->str, (result ? "TRUE" : "FALSE" ));
428     g_string_free (to_execute, TRUE);
429     return result;
430 }
431
432 static void
433 spawn(WebKitWebView *web_view, const char *param) {
434     (void)web_view;
435     run_command(param, NULL);
436 }
437
438 static void
439 parse_command(const char *cmd, const char *param) {
440     Command c;
441
442     if ((c = g_hash_table_lookup(commands, cmd)))
443         c(web_view, param);
444     else
445         fprintf (stderr, "command \"%s\" not understood. ignoring.\n", cmd);
446 }
447
448 static void
449 parse_line(char *line) {
450     gchar **parts;
451
452     g_strstrip(line);
453
454     parts = g_strsplit(line, " ", 2);
455
456     if (!parts)
457         return;
458
459     parse_command(parts[0], parts[1]);
460
461     g_strfreev(parts);
462 }
463
464 enum { FIFO, SOCKET};
465 void
466 build_stream_name(int type) {
467     char *xwin_str;
468
469     xwin_str = itos((int)xwin);
470     switch(type) {
471             case FIFO:
472                     if (fifo_dir) 
473                             sprintf (fifo_path, "%s/uzbl_fifo_%s", fifo_dir,
474                                             instance_name ? instance_name : xwin_str);
475                     else 
476                             sprintf (fifo_path, "/tmp/uzbl_fifo_%s", 
477                                             instance_name ? instance_name : xwin_str);
478                     break;
479             case SOCKET:
480                     if (socket_dir) 
481                             sprintf (socket_path, "%s/uzbl_socket_%s", socket_dir, 
482                                             instance_name ? instance_name : xwin_str);
483                     else 
484                             sprintf (socket_path, "/tmp/uzbl_socket_%s", 
485                                             instance_name ? instance_name : xwin_str);
486                     break;
487              default:
488                     break;
489     }
490 }
491
492 static void
493 control_fifo(GIOChannel *fd) {
494     gchar *ctl_line;
495     if(!fd)
496        return;
497     g_io_channel_read_line(fd, &ctl_line, NULL, NULL, NULL);
498     parse_line(ctl_line);
499     g_free(ctl_line);
500     return;
501 }
502
503 static void
504 create_fifo() {
505     GIOChannel *chan = NULL;
506     GError *error = NULL;
507
508     build_stream_name(FIFO);
509     if (file_exists(fifo_path)) {
510         printf ("Fifo: disabled.  Error when creating %s: File exists\n", fifo_path);
511         return;
512     }
513     if (mkfifo (fifo_path, 0666) == -1) { //TODO: mkfifo blocks (waits for writer)
514         printf ("Fifo: disabled.  Error when creating %s: %s\n", fifo_path, strerror(errno));
515     } else {
516         chan = g_io_channel_new_file((gchar *) fifo_path, "r", &error);
517         if (chan) {
518             printf ("Fifo: created successfully as %s\n", fifo_path);
519             g_io_add_watch(chan, G_IO_IN|G_IO_HUP, (GIOFunc) control_fifo, chan);
520         } else {
521             g_error ("Fifo: error while opening: %s\n", error->message);
522         }
523     }
524     return;
525 }
526
527 static void
528 control_socket(GIOChannel *chan) {
529     struct sockaddr_un remote;
530     char buffer[512], *ctl_line;
531     char temp[128];
532     int sock, clientsock, n, done;
533     unsigned int t;
534
535     sock = g_io_channel_unix_get_fd(chan);
536
537     memset (buffer, 0, sizeof (buffer));
538
539     t          = sizeof (remote);
540     clientsock = accept (sock, (struct sockaddr *) &remote, &t);
541
542     done = 0;
543     do {
544         memset (temp, 0, sizeof (temp));
545         n = recv (clientsock, temp, 128, 0);
546         if (n == 0) {
547             buffer[strlen (buffer)] = '\0';
548             done = 1;
549         }
550         if (!done)
551             strcat (buffer, temp);
552     } while (!done);
553
554     if (strcmp (buffer, "\n") < 0) {
555         buffer[strlen (buffer) - 1] = '\0';
556     } else {
557         buffer[strlen (buffer)] = '\0';
558     }
559     close (clientsock);
560     ctl_line = estrdup(buffer);
561     parse_line (ctl_line);
562
563 /*
564    TODO: we should be able to do it with this.  but glib errors out with "Invalid argument"
565     GError *error = NULL;
566     gsize len;
567     GIOStatus ret;
568     ret = g_io_channel_read_line(chan, &ctl_line, &len, NULL, &error);
569     if (ret == G_IO_STATUS_ERROR)
570         g_error ("Error reading: %s\n", error->message);
571
572     printf("Got line %s (%u bytes) \n",ctl_line, len);
573     if(ctl_line) {
574        parse_line(ctl_line);
575 */
576
577     g_free(ctl_line);
578     return;
579
580
581 static void
582 create_socket() {
583     GIOChannel *chan = NULL;
584     int sock, len;
585     struct sockaddr_un local;
586
587     build_stream_name(SOCKET);
588     sock = socket (AF_UNIX, SOCK_STREAM, 0);
589
590     local.sun_family = AF_UNIX;
591     strcpy (local.sun_path, socket_path);
592     unlink (local.sun_path);
593
594     len = strlen (local.sun_path) + sizeof (local.sun_family);
595     bind (sock, (struct sockaddr *) &local, len);
596
597     if (errno == -1) {
598         printf ("Socket: Could not open in %s: %s\n", socket_path, strerror(errno));
599     } else {
600         printf ("Socket: Opened in %s\n", socket_path);
601         listen (sock, 5);
602
603         if( (chan = g_io_channel_unix_new(sock)) )
604             g_io_add_watch(chan, G_IO_IN|G_IO_HUP, (GIOFunc) control_socket, chan);
605     }
606 }
607
608 static void
609 update_title (void) {
610     GString* string_long = g_string_new ("");
611     GString* string_short = g_string_new ("");
612     char* iname = NULL;
613     int iname_len;
614
615     if(instance_name) {
616             iname_len = strlen(instance_name)+4;
617             iname = malloc(iname_len);
618             snprintf(iname, iname_len, "<%s> ", instance_name);
619             
620             g_string_prepend(string_long, iname);
621             g_string_prepend(string_short, iname);
622             free(iname);
623     }
624
625     g_string_append_printf(string_long, "%s ", keycmd->str);
626     if (!always_insert_mode)
627         g_string_append (string_long, (insert_mode ? "[I] " : "[C] "));
628     if (main_title) {
629         g_string_append (string_long, main_title);
630         g_string_append (string_short, main_title);
631     }
632     g_string_append (string_long, " - Uzbl browser");
633     g_string_append (string_short, " - Uzbl browser");
634     if (load_progress < 100)
635         g_string_append_printf (string_long, " (%d%%)", load_progress);
636
637     if (selected_url[0]!=0) {
638         g_string_append_printf (string_long, " -> (%s)", selected_url);
639     }
640
641     gchar* title_long = g_string_free (string_long, FALSE);
642     gchar* title_short = g_string_free (string_short, FALSE);
643
644     if (show_status) {
645         gtk_window_set_title (GTK_WINDOW(main_window), title_short);
646     gtk_label_set_text(GTK_LABEL(mainbar_label), title_long);
647     } else {
648         gtk_window_set_title (GTK_WINDOW(main_window), title_long);
649     }
650
651     g_free (title_long);
652     g_free (title_short);
653 }
654  
655 static gboolean
656 key_press_cb (WebKitWebView* page, GdkEventKey* event)
657 {
658     //TRUE to stop other handlers from being invoked for the event. FALSE to propagate the event further.
659
660     (void) page;
661     Action *action;
662
663     if (event->type != GDK_KEY_PRESS || event->keyval == GDK_Page_Up || event->keyval == GDK_Page_Down
664         || event->keyval == GDK_Up || event->keyval == GDK_Down || event->keyval == GDK_Left || event->keyval == GDK_Right)
665         return FALSE;
666
667     /* turn off insert mode (if always_insert_mode is not used) */
668     if (insert_mode && (event->keyval == GDK_Escape)) {
669         insert_mode = always_insert_mode;
670         update_title();
671         return TRUE;
672     }
673
674     if (insert_mode && event->state != modmask)
675         return FALSE;
676
677     if (event->keyval == GDK_Escape) {
678         g_string_truncate(keycmd, 0);
679         update_title();
680         return TRUE;
681     }
682
683     if ((event->keyval == GDK_BackSpace) && (keycmd->len > 0)) {
684         g_string_truncate(keycmd, keycmd->len - 1);
685         update_title();
686         return TRUE;
687     }
688
689     if ((event->keyval == GDK_Return) || (event->keyval == GDK_KP_Enter)) {
690         GString* short_keys = g_string_new ("");
691         unsigned int i;
692         for (i=0; i<(keycmd->len); i++) {
693             g_string_append_c(short_keys, keycmd->str[i]);
694             g_string_append_c(short_keys, '_');
695             
696             //printf("\nTesting string: @%s@\n", short_keys->str);
697             if ((action = g_hash_table_lookup(bindings, short_keys->str))) {
698                 GString* parampart = g_string_new (keycmd->str);
699                 g_string_erase (parampart, 0, i+1);
700                 //printf("\nParameter: @%s@\n", parampart->str);
701                 GString* actionname = g_string_new ("");
702                 if (action->name)
703                     g_string_printf (actionname, action->name, parampart->str);
704                 GString* actionparam = g_string_new ("");
705                 if (action->param)
706                     g_string_printf (actionparam, action->param, parampart->str);
707                 parse_command(actionname->str, actionparam->str);
708                 g_string_free (actionname, TRUE);
709                 g_string_free (actionparam, TRUE);
710                 g_string_free (parampart, TRUE);
711                 g_string_truncate(keycmd, 0);
712             }          
713
714             g_string_truncate(short_keys, short_keys->len - 1);
715         }
716         g_string_free (short_keys, TRUE);
717         return (!insert_mode);
718     }
719
720     g_string_append(keycmd, event->string);
721     if ((action = g_hash_table_lookup(bindings, keycmd->str))) {
722         g_string_truncate(keycmd, 0);
723         parse_command(action->name, action->param);
724     }
725
726     update_title();
727
728     return TRUE;
729 }
730
731 static GtkWidget*
732 create_browser () {
733     GtkWidget* scrolled_window = gtk_scrolled_window_new (NULL, NULL);
734     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
735
736     web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
737     gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));
738
739     g_signal_connect (G_OBJECT (web_view), "title-changed", G_CALLBACK (title_change_cb), web_view);
740     g_signal_connect (G_OBJECT (web_view), "load-progress-changed", G_CALLBACK (progress_change_cb), web_view);
741     g_signal_connect (G_OBJECT (web_view), "load-committed", G_CALLBACK (load_commit_cb), web_view);
742     g_signal_connect (G_OBJECT (web_view), "load-committed", G_CALLBACK (log_history_cb), web_view);
743     g_signal_connect (G_OBJECT (web_view), "hovering-over-link", G_CALLBACK (link_hover_cb), web_view);
744     g_signal_connect (G_OBJECT (web_view), "key-press-event", G_CALLBACK (key_press_cb), web_view);
745     g_signal_connect (G_OBJECT (web_view), "new-window-policy-decision-requested", G_CALLBACK (new_window_cb), web_view); 
746     g_signal_connect (G_OBJECT (web_view), "download-requested", G_CALLBACK (download_cb), web_view); 
747     g_signal_connect (G_OBJECT (web_view), "create-web-view", G_CALLBACK (create_web_view_cb), web_view);  
748
749     return scrolled_window;
750 }
751
752 static GtkWidget*
753 create_mainbar () {
754     mainbar = gtk_hbox_new (FALSE, 0);
755     mainbar_label = gtk_label_new ("");  
756     gtk_misc_set_alignment (GTK_MISC(mainbar_label), 0, 0);
757     gtk_misc_set_padding (GTK_MISC(mainbar_label), 2, 2);
758     gtk_box_pack_start (GTK_BOX (mainbar), mainbar_label, TRUE, TRUE, 0);
759     return mainbar;
760 }
761
762 static
763 GtkWidget* create_window () {
764     GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
765     gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
766     gtk_widget_set_name (window, "Uzbl browser");
767     g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy_cb), NULL);
768
769     return window;
770 }
771
772 static void
773 add_binding (const gchar *key, const gchar *act) {
774     char **parts = g_strsplit(act, " ", 2);
775     Action *action;
776
777     if (!parts)
778         return;
779
780     //Debug:
781     printf ("Binding %-10s : %s\n", key, act);
782     action = new_action(parts[0], parts[1]);
783     g_hash_table_insert(bindings, g_strdup(key), action);
784
785     g_strfreev(parts);
786 }
787
788 static void
789 settings_init () {
790     GKeyFile* config;
791     gboolean res  = FALSE;
792     char *saveptr;
793     gchar** keys = NULL;
794
795     if (!config_file) {
796         const char* XDG_CONFIG_HOME = getenv ("XDG_CONFIG_HOME");
797         if (! XDG_CONFIG_HOME || ! strcmp (XDG_CONFIG_HOME, "")) {
798           XDG_CONFIG_HOME = (char*)XDG_CONFIG_HOME_default;
799         }
800         printf("XDG_CONFIG_HOME: %s\n", XDG_CONFIG_HOME);
801     
802         strcpy (config_file_path, XDG_CONFIG_HOME);
803         strcat (config_file_path, "/uzbl/config");
804         if (file_exists (config_file_path)) {
805           printf ("Config file %s found.\n", config_file_path);
806           config_file = &config_file_path[0];
807         } else {
808             // Now we check $XDG_CONFIG_DIRS
809             char *XDG_CONFIG_DIRS = getenv ("XDG_CONFIG_DIRS");
810             if (! XDG_CONFIG_DIRS || ! strcmp (XDG_CONFIG_DIRS, ""))
811                 XDG_CONFIG_DIRS = XDG_CONFIG_DIRS_default;
812
813             printf("XDG_CONFIG_DIRS: %s\n", XDG_CONFIG_DIRS);
814
815             char buffer[512];
816             strcpy (buffer, XDG_CONFIG_DIRS);
817             const gchar* dir = (char *) strtok_r (buffer, ":", &saveptr);
818             while (dir && ! file_exists (config_file_path)) {
819                 strcpy (config_file_path, dir);
820                 strcat (config_file_path, "/uzbl/config_file_pathig");
821                 if (file_exists (config_file_path)) {
822                     printf ("Config file %s found.\n", config_file_path);
823                     config_file = &config_file_path[0];
824                 }
825                 dir = (char * ) strtok_r (NULL, ":", &saveptr);
826             }
827         }
828     }
829
830     if (config_file) {
831         config = g_key_file_new ();
832         res = g_key_file_load_from_file (config, config_file, G_KEY_FILE_NONE, NULL);
833           if(res) {
834             printf ("Config %s loaded\n", config_file);
835           } else {
836             fprintf (stderr, "Config %s loading failed\n", config_file);
837         }
838     } else {
839         printf ("No configuration.\n");
840     }
841
842     if (res) {
843         history_handler    = g_key_file_get_value   (config, "behavior", "history_handler",    NULL);
844         download_handler   = g_key_file_get_value   (config, "behavior", "download_handler",   NULL);
845         always_insert_mode = g_key_file_get_boolean (config, "behavior", "always_insert_mode", NULL);
846         show_status        = g_key_file_get_boolean (config, "behavior", "show_status",        NULL);
847         modkey             = g_key_file_get_value   (config, "behavior", "modkey",             NULL);
848         status_top         = g_key_file_get_boolean (config, "behavior", "status_top",         NULL);
849         if (! fifo_dir)
850             fifo_dir        = g_key_file_get_value  (config, "behavior", "fifo_dir",           NULL);
851         if (! socket_dir)
852             socket_dir     = g_key_file_get_value   (config, "behavior", "socket_dir",         NULL);
853         keys               = g_key_file_get_keys    (config, "bindings", NULL,                 NULL);
854     }
855
856     printf ("History handler: %s\n",    (history_handler    ? history_handler  : "disabled"));
857     printf ("Download manager: %s\n",   (download_handler   ? download_handler : "disabled"));
858     printf ("Fifo directory: %s\n",     (fifo_dir           ? fifo_dir         : "/tmp"));
859     printf ("Socket directory: %s\n",   (socket_dir         ? socket_dir       : "/tmp"));
860     printf ("Always insert mode: %s\n", (always_insert_mode ? "TRUE"           : "FALSE"));
861     printf ("Show status: %s\n",        (show_status        ? "TRUE"           : "FALSE"));
862     printf ("Status top: %s\n",         (status_top         ? "TRUE"           : "FALSE"));
863     printf ("Modkey: %s\n",             (modkey             ? modkey           : "disabled"));
864
865     if (! modkey)
866         modkey = "";
867
868     //POSSIBLE MODKEY VALUES (COMBINATIONS CAN BE USED)
869     gchar* modkeyup = g_utf8_strup (modkey, -1);
870     if (g_strrstr (modkeyup,"SHIFT") != NULL)    modmask |= GDK_SHIFT_MASK;    //the Shift key.
871     if (g_strrstr (modkeyup,"LOCK") != NULL)     modmask |= GDK_LOCK_MASK;     //a Lock key (depending on the modifier mapping of the X server this may either be CapsLock or ShiftLock).
872     if (g_strrstr (modkeyup,"CONTROL") != NULL)  modmask |= GDK_CONTROL_MASK;  //the Control key.
873     if (g_strrstr (modkeyup,"MOD1") != NULL)     modmask |= GDK_MOD1_MASK;     //the fourth modifier key (it depends on the modifier mapping of the X server which key is interpreted as this modifier, but normally it is the Alt key).
874     if (g_strrstr (modkeyup,"MOD2") != NULL)     modmask |= GDK_MOD2_MASK;     //the fifth modifier key (it depends on the modifier mapping of the X server which key is interpreted as this modifier).
875     if (g_strrstr (modkeyup,"MOD3") != NULL)     modmask |= GDK_MOD3_MASK;     //the sixth modifier key (it depends on the modifier mapping of the X server which key is interpreted as this modifier).
876     if (g_strrstr (modkeyup,"MOD4") != NULL)     modmask |= GDK_MOD4_MASK;     //the seventh modifier key (it depends on the modifier mapping of the X server which key is interpreted as this modifier).
877     if (g_strrstr (modkeyup,"MOD5") != NULL)     modmask |= GDK_MOD5_MASK;     //the eighth modifier key (it depends on the modifier mapping of the X server which key is interpreted as this modifier).
878     if (g_strrstr (modkeyup,"BUTTON1") != NULL)  modmask |= GDK_BUTTON1_MASK;  //the first mouse button.
879     if (g_strrstr (modkeyup,"BUTTON2") != NULL)  modmask |= GDK_BUTTON2_MASK;  //the second mouse button.
880     if (g_strrstr (modkeyup,"BUTTON3") != NULL)  modmask |= GDK_BUTTON3_MASK;  //the third mouse button.
881     if (g_strrstr (modkeyup,"BUTTON4") != NULL)  modmask |= GDK_BUTTON4_MASK;  //the fourth mouse button.
882     if (g_strrstr (modkeyup,"BUTTON5") != NULL)  modmask |= GDK_BUTTON5_MASK;  //the fifth mouse button.
883     if (g_strrstr (modkeyup,"SUPER") != NULL)    modmask |= GDK_SUPER_MASK;    //the Super modifier. Since 2.10
884     if (g_strrstr (modkeyup,"HYPER") != NULL)    modmask |= GDK_HYPER_MASK;    //the Hyper modifier. Since 2.10
885     if (g_strrstr (modkeyup,"META") != NULL)     modmask |= GDK_META_MASK;     //the Meta modifier. Since 2.10  */
886     free (modkeyup);
887
888     if (keys) {
889         int i;
890         for (i = 0; keys[i]; i++) {
891             gchar *value = g_key_file_get_string (config, "bindings", keys[i], NULL);
892             
893             add_binding(g_strstrip(keys[i]), value);
894             g_free(value);
895         }
896
897         g_strfreev(keys);
898     }
899
900     /* networking options */
901     proxy_url      = g_key_file_get_value   (config, "network", "proxy_server",       NULL);
902     http_debug     = g_key_file_get_integer (config, "network", "http_debug",         NULL);
903     useragent      = g_key_file_get_value   (config, "network", "user-agent",         NULL);
904     max_conns      = g_key_file_get_integer (config, "network", "max_conns",          NULL);
905     max_conns_host = g_key_file_get_integer (config, "network", "max_conns_per_host", NULL);
906
907     if(proxy_url){
908         g_object_set(G_OBJECT(soup_session), SOUP_SESSION_PROXY_URI, soup_uri_new(proxy_url), NULL);
909     }
910         
911     if(!(http_debug <= 3)){
912         http_debug = 0;
913         fprintf(stderr, "Wrong http_debug level, ignoring.\n");
914     } else if (http_debug > 0) {
915         soup_logger = soup_logger_new(http_debug, -1);
916         soup_session_add_feature(soup_session, SOUP_SESSION_FEATURE(soup_logger));
917     }
918         
919     if(useragent){
920         g_object_set(G_OBJECT(soup_session), SOUP_SESSION_USER_AGENT, useragent, NULL);
921     }
922
923     if(max_conns >= 1){
924         g_object_set(G_OBJECT(soup_session), SOUP_SESSION_MAX_CONNS, max_conns, NULL);
925     }
926
927     if(max_conns_host >= 1){
928         g_object_set(G_OBJECT(soup_session), SOUP_SESSION_MAX_CONNS_PER_HOST, max_conns_host, NULL);
929     }
930
931     printf("Proxy configured: %s\n", proxy_url ? proxy_url : "none");
932     printf("HTTP logging level: %d\n", http_debug);
933     printf("User-agent: %s\n", useragent? useragent : "default");
934     printf("Maximum connections: %d\n", max_conns ? max_conns : 0);
935     printf("Maximum connections per host: %d\n", max_conns_host ? max_conns_host: 0);
936                 
937 }
938
939 int
940 main (int argc, char* argv[]) {
941     gtk_init (&argc, &argv);
942     if (!g_thread_supported ())
943         g_thread_init (NULL);
944
945     printf("Uzbl start location: %s\n", argv[0]);
946     strcpy(executable_path,argv[0]);
947
948     strcat ((char *) XDG_CONFIG_HOME_default, getenv ("HOME"));
949     strcat ((char *) XDG_CONFIG_HOME_default, "/.config");
950
951     GError *error = NULL;
952     GOptionContext* context = g_option_context_new ("- some stuff here maybe someday");
953     g_option_context_add_main_entries (context, entries, NULL);
954     g_option_context_add_group (context, gtk_get_option_group (TRUE));
955     g_option_context_parse (context, &argc, &argv, &error);
956     /* initialize hash table */
957     bindings = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, free_action);
958         
959         soup_session = webkit_get_default_session();
960     keycmd = g_string_new("");
961
962     settings_init ();
963     commands_hash ();
964
965     if (always_insert_mode)
966         insert_mode = TRUE;
967
968     GtkWidget* vbox = gtk_vbox_new (FALSE, 0);
969     if (status_top)
970         gtk_box_pack_start (GTK_BOX (vbox), create_mainbar (), FALSE, TRUE, 0);
971     gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
972     if (!status_top)
973         gtk_box_pack_start (GTK_BOX (vbox), create_mainbar (), FALSE, TRUE, 0);
974
975     main_window = create_window ();
976     gtk_container_add (GTK_CONTAINER (main_window), vbox);
977
978     load_uri (web_view, uri);
979
980     gtk_widget_grab_focus (GTK_WIDGET (web_view));
981     gtk_widget_show_all (main_window);
982     xwin = GDK_WINDOW_XID (GTK_WIDGET (main_window)->window);
983     printf("window_id %i\n",(int) xwin);
984     printf("pid %i\n", getpid ());
985     printf("name: %s\n", instance_name);
986
987     scbar_v = (GtkScrollbar*) gtk_vscrollbar_new (NULL);
988     bar_v = gtk_range_get_adjustment((GtkRange*) scbar_v);
989     scbar_h = (GtkScrollbar*) gtk_hscrollbar_new (NULL);
990     bar_h = gtk_range_get_adjustment((GtkRange*) scbar_h);
991     gtk_widget_set_scroll_adjustments ((GtkWidget*) web_view, bar_h, bar_v);
992
993     if (!show_status)
994         gtk_widget_hide(mainbar);
995
996     create_fifo ();
997     create_socket();
998
999     gtk_main ();
1000
1001     g_string_free(keycmd, TRUE);
1002
1003     unlink (socket_path);
1004     unlink (fifo_path);
1005
1006     g_hash_table_destroy(bindings);
1007     g_hash_table_destroy(commands);
1008     return 0;
1009 }
1010
1011 /* vi: set et ts=4: */