add jouz to authors file
[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
32 #define LENGTH(x) (sizeof x / sizeof x[0])
33 #define MAX_BINDINGS 256
34
35 #include <gtk/gtk.h>
36 #include <gdk/gdkx.h>
37 #include <gdk/gdkkeysyms.h>
38 #include <webkit/webkit.h>
39 #include <pthread.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <sys/stat.h>
43 #include <sys/types.h>
44 #include <unistd.h>
45 #include <stdlib.h>
46 #include <errno.h>
47 #include <string.h>
48 #include <sys/types.h>
49 #include <fcntl.h>
50 #include <sys/socket.h>
51 #include <sys/un.h>
52
53 /* housekeeping / internal variables */
54 static GtkWidget* main_window;
55 static GtkWidget* mainbar;
56 static GtkWidget* mainbar_label;
57 static GtkScrollbar* scbar_v;   // Horizontal and Vertical Scrollbar 
58 static GtkScrollbar* scbar_h;   // (These are still hidden)
59 static GtkAdjustment* bar_v; // Information about document length
60 static GtkAdjustment* bar_h; // and scrolling position
61 static WebKitWebView* web_view;
62 static gchar* main_title;
63 static gchar selected_url[500] = "\0";
64 static gint load_progress;
65 static Window xwin = 0;
66 static char fifo_path[64];
67 static char socket_path[108];
68 static GString *keycmd;
69
70 /* state variables (initial values coming from command line arguments but may be changed later) */
71 static gchar*   uri         = NULL;
72 static gchar*   config_file = NULL;
73 static gchar    config_file_path[500];
74 static gboolean verbose     = FALSE;
75
76 /* settings from config: group behaviour */
77 static gchar*   history_handler    = NULL;
78 static gchar*   fifo_dir           = NULL;
79 static gchar*   socket_dir         = NULL;
80 static gchar*   download_handler   = NULL;
81 static gboolean always_insert_mode = FALSE;
82 static gboolean show_status        = FALSE;
83 static gboolean insert_mode        = FALSE;
84 static gboolean status_top         = FALSE;
85 static gchar*   modkey             = NULL;
86 static guint    modmask            = 0;
87 static gdouble   hscroll            = 20;
88 static gdouble   vscroll            = 20;
89
90 /* settings from config: group bindings, key -> action */
91 static GHashTable *bindings;
92
93 /* command list: name -> Command  */
94 static GHashTable *commands;
95
96 typedef struct {
97     char *name;
98     char *param;
99 } Action;
100
101 /* commandline arguments (set initial values for the state variables) */
102 static GOptionEntry entries[] =
103 {
104     { "uri",     'u', 0, G_OPTION_ARG_STRING, &uri,         "Uri to load", 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 static void
117 update_title(void);
118
119 static void
120 load_uri ( WebKitWebView * web_view, const gchar * uri);
121
122 static void
123 new_window_load_uri (const gchar * uri);
124
125 static void
126 close_uzbl (WebKitWebView *page, const char *param);
127
128 static gboolean
129 run_command(const char *command, const char *args);
130
131 static void
132 spawn(WebKitWebView *web_view, const char *param);
133
134 static void
135 free_action(gpointer action);
136
137 static Action*
138 new_action(const gchar *name, const gchar *param);
139
140 static void
141 set_insert_mode(WebKitWebView *page, const gchar *param);
142
143
144
145 /* --- CALLBACKS --- */
146
147 static gboolean
148 new_window_cb (WebKitWebView *web_view, WebKitWebFrame *frame, WebKitNetworkRequest *request, WebKitWebNavigationAction *navigation_action, WebKitWebPolicyDecision *policy_decision, gpointer user_data) {
149     (void) web_view;
150     (void) frame;
151     (void) navigation_action;
152     (void) policy_decision;
153     (void) user_data;
154     const gchar* uri = webkit_network_request_get_uri (request);
155     printf("New window requested -> %s \n", uri);
156     new_window_load_uri(uri);
157     return (FALSE);
158 }
159
160 WebKitWebView*
161 create_web_view_cb (WebKitWebView  *web_view, WebKitWebFrame *frame, gpointer user_data) {
162     (void) web_view;
163     (void) frame;
164     (void) user_data;
165     if (selected_url[0]!=0) {
166         printf("\nNew web view -> %s\n",selected_url);
167         new_window_load_uri(selected_url);
168     } else {
169         printf("New web view -> %s\n","Nothing to open, exiting");
170     }
171     return (NULL);
172 }
173
174 static gboolean
175 download_cb (WebKitWebView *web_view, GObject *download, gpointer user_data) {
176     (void) web_view;
177     (void) user_data;
178     if (download_handler) {
179         const gchar* uri = webkit_download_get_uri ((WebKitDownload*)download);
180         printf("Download -> %s\n",uri);
181         run_command(download_handler, uri);
182     }
183     return (FALSE);
184 }
185
186 /* scroll a bar in a given direction */
187 static void
188 scroll (double i, GtkAdjustment* bar) {
189     gtk_adjustment_set_value (bar, gtk_adjustment_get_value(bar)+i);
190 }
191
192 static void scroll_up (WebKitWebView* page, const char *param) {
193     (void) page;
194     (void) param;
195
196     scroll (-vscroll, bar_v);
197 }
198
199 static void scroll_left (WebKitWebView* page, const char *param) {
200     (void) page;
201     (void) param;
202
203     scroll (-hscroll, bar_h);
204 }
205
206 static void scroll_down (WebKitWebView* page, const char *param) {
207     (void) page;
208     (void) param;
209
210     scroll (vscroll, bar_v);
211 }
212
213 static void scroll_right (WebKitWebView* page, const char *param) {
214     (void) page;
215     (void) param;
216
217     scroll (hscroll, bar_h);
218 }
219
220 static void
221 toggle_status_cb (WebKitWebView* page, const char *param) {
222     (void)page;
223     (void)param;
224
225     if (show_status) {
226         gtk_widget_hide(mainbar);
227     } else {
228         gtk_widget_show(mainbar);
229     }
230     show_status = !show_status;
231     update_title();
232 }
233
234 static void
235 link_hover_cb (WebKitWebView* page, const gchar* title, const gchar* link, gpointer data) {
236     (void) page;
237     (void) title;
238     (void) data;    
239     //ADD HOVER URL TO WINDOW TITLE
240     selected_url[0] = '\0';
241     if (link) {
242         strcpy (selected_url, link);
243     }
244     update_title();
245 }
246
247 static void
248 title_change_cb (WebKitWebView* web_view, WebKitWebFrame* web_frame, const gchar* title, gpointer data) {
249     (void) web_view;
250     (void) web_frame;
251     (void) data;
252     if (main_title)
253         g_free (main_title);
254     main_title = g_strdup (title);
255     update_title();
256 }
257
258 static void
259 progress_change_cb (WebKitWebView* page, gint progress, gpointer data) {
260     (void) page;
261     (void) data;
262     load_progress = progress;
263     update_title();
264 }
265
266 static void
267 load_commit_cb (WebKitWebView* page, WebKitWebFrame* frame, gpointer data) {
268     (void) page;
269     (void) data;
270     free (uri);
271     GString* newuri = g_string_new (webkit_web_frame_get_uri (frame));
272     uri = g_string_free (newuri, FALSE);
273 }
274
275 static void
276 destroy_cb (GtkWidget* widget, gpointer data) {
277     (void) widget;
278     (void) data;
279     gtk_main_quit ();
280 }
281
282 static void
283 log_history_cb () {
284    if (history_handler) {
285        time_t rawtime;
286        struct tm * timeinfo;
287        char date [80];
288        time ( &rawtime );
289        timeinfo = localtime ( &rawtime );
290        strftime (date, 80, "%Y-%m-%d %H:%M:%S", timeinfo);
291        GString* args = g_string_new ("");
292        g_string_printf (args, "'%s' '%s' '%s'", uri, "TODO:page title here", date);
293        run_command(history_handler, args->str);
294        g_string_free (args, TRUE);
295    }
296 }
297
298 /* VIEW funcs (little webkit wrappers) */
299
300 #define VIEWFUNC(name) static void view_##name(WebKitWebView *page, const char *param){(void)param; webkit_web_view_##name(page);}
301 VIEWFUNC(reload)
302 VIEWFUNC(stop_loading)
303 VIEWFUNC(zoom_in)
304 VIEWFUNC(zoom_out)
305 VIEWFUNC(go_back)
306 VIEWFUNC(go_forward)
307 #undef VIEWFUNC
308
309 /* -- command to callback/function map for things we cannot attach to any signals */
310 // TODO: reload
311
312 static struct {char *name; Command command;} cmdlist[] =
313 {
314     { "back",          view_go_back       },
315     { "forward",       view_go_forward    },
316     { "scroll_down",   scroll_down        },
317     { "scroll_up",     scroll_up          },
318     { "scroll_left",   scroll_left        },
319     { "scroll_right",  scroll_right       },
320     { "reload",        view_reload,       }, //Buggy
321     { "refresh",       view_reload,       }, /* for convenience, will change */
322     { "stop",          view_stop_loading, },
323     { "zoom_in",       view_zoom_in,      }, //Can crash (when max zoom reached?).
324     { "zoom_out",      view_zoom_out,     },
325     { "uri",           load_uri           },
326     { "toggle_status", toggle_status_cb   },
327     { "spawn",         spawn              },
328     { "exit",          close_uzbl         },
329     { "insert_mode",   set_insert_mode    }
330 };
331
332 static void
333 commands_hash(void)
334 {
335     unsigned int i;
336     commands = g_hash_table_new(g_str_hash, g_str_equal);
337
338     for (i = 0; i < LENGTH(cmdlist); i++)
339         g_hash_table_insert(commands, cmdlist[i].name, cmdlist[i].command);
340 }
341
342 /* -- CORE FUNCTIONS -- */
343
344 void
345 free_action(gpointer act) {
346     Action *action = (Action*)act;
347     g_free(action->name);
348     if (action->param)
349         g_free(action->param);
350     g_free(action);
351 }
352
353 Action*
354 new_action(const gchar *name, const gchar *param) {
355     Action *action = g_new(Action, 1);
356
357     action->name = g_strdup(name);
358     if (param)
359         action->param = g_strdup(param);
360     else
361         action->param = NULL;
362
363     return action;
364 }
365
366 static bool
367 file_exists (const char * filename) {
368     FILE *file = fopen (filename, "r");
369     if (file) {
370         fclose (file);
371         return true;
372     }
373     return false;
374 }
375
376 void
377 set_insert_mode(WebKitWebView *page, const gchar *param) {
378     (void)page;
379     (void)param;
380
381     insert_mode = TRUE;
382     update_title();
383 }
384
385 static void
386 load_uri (WebKitWebView * web_view, const gchar *param) {
387     if (param) {
388         GString* newuri = g_string_new (param);
389         if (g_strrstr (param, "://") == NULL)
390             g_string_prepend (newuri, "http://"); 
391         webkit_web_view_load_uri (web_view, newuri->str);
392         g_string_free (newuri, TRUE);
393     }
394 }
395
396 static void
397 new_window_load_uri (const gchar * uri) {
398     GString* to_execute = g_string_new ("");
399     if (!config_file) {
400         g_string_printf (to_execute, "uzbl --uri '%s'", uri);
401     } else {
402         g_string_printf (to_execute, "uzbl --uri '%s' --config '%s'", uri, config_file);
403     }
404     printf("Spawning %s\n",to_execute->str);
405     if (!g_spawn_command_line_async (to_execute->str, NULL)) {
406         if (!config_file) {
407             g_string_printf (to_execute, "./uzbl --uri '%s'", uri);
408         } else {
409             g_string_printf (to_execute, "./uzbl --uri '%s' --config '%s'", uri, config_file);
410         }
411         printf("Spawning %s\n",to_execute->str);
412     g_spawn_command_line_async (to_execute->str, NULL);
413     }
414     g_string_free (to_execute, TRUE);
415 }
416
417 static void
418 close_uzbl (WebKitWebView *page, const char *param) {
419     (void)page;
420     (void)param;
421     gtk_main_quit ();
422 }
423
424 // make sure to put '' around args, so that if there is whitespace we can still keep arguments together.
425 static gboolean
426 run_command(const char *command, const char *args) {
427    //command <uzbl conf> <uzbl pid> <uzbl win id> <uzbl fifo file> <uzbl socket file> [args]
428     GString* to_execute = g_string_new ("");
429     gboolean result;
430     g_string_printf (to_execute, "%s '%s' '%i' '%i' '%s' '%s'", command, config_file, (int) getpid() , (int) xwin, fifo_path, socket_path);
431     if(args) {
432         g_string_append_printf (to_execute, " %s", args);
433     }
434     result = g_spawn_command_line_async (to_execute->str, NULL);
435     printf("Called %s.  Result: %s\n", to_execute->str, (result ? "TRUE" : "FALSE" ));
436     g_string_free (to_execute, TRUE);
437     return result;
438 }
439
440 static void
441 spawn(WebKitWebView *web_view, const char *param) {
442     (void)web_view;
443     run_command(param, NULL);
444 }
445
446 static void
447 parse_command(const char *cmd, const char *param) {
448     Command c;
449
450     if ((c = g_hash_table_lookup(commands, cmd)))
451         c(web_view, param);
452     else
453         fprintf (stderr, "command \"%s\" not understood. ignoring.\n", cmd);
454 }
455
456 static void
457 parse_line(char *line) {
458     gchar **parts;
459
460     g_strstrip(line);
461
462     parts = g_strsplit(line, " ", 2);
463
464     if (!parts)
465         return;
466
467     parse_command(parts[0], parts[1]);
468
469     g_strfreev(parts);
470 }
471
472 static void
473 control_fifo(GIOChannel *fd) {
474     gchar *ctl_line;
475     gsize term_pos;
476
477     if(!fd)
478        return;
479
480     g_io_channel_read_line(fd, &ctl_line, NULL, &term_pos, NULL);
481     ctl_line[term_pos] ='\0';
482
483     parse_line(ctl_line);
484
485     g_free(ctl_line);
486
487     return;
488 }
489
490 static void
491 create_fifo() {
492     GIOChannel *chan = NULL;
493
494     if (fifo_dir) {
495         sprintf (fifo_path, "%s/uzbl_fifo_%d", fifo_dir, (int) xwin);
496     } else {
497         sprintf (fifo_path, "/tmp/uzbl_fifo_%d", (int) xwin);
498     }
499     printf ("Control fifo opened in %s\n", fifo_path);
500     if (mkfifo (fifo_path, 0666) == -1) {
501         printf ("Possible error creating fifo\n");
502     }
503
504     if( (chan = g_io_channel_new_file((gchar *) fifo_path, "r+", NULL)) )
505         g_io_add_watch(chan, G_IO_IN|G_IO_HUP, (GIOFunc) control_fifo, chan);
506     return;
507 }
508
509 static void
510 *control_socket() {
511     if (socket_dir) {
512         sprintf (socket_path, "%s/uzbl_socket_%d", socket_dir, (int) xwin);
513     } else {
514         sprintf (socket_path, "/tmp/uzbl_socket_%d", (int) xwin);
515     }
516  
517     int sock, clientsock, len;
518     unsigned int t;
519     struct sockaddr_un local, remote;
520
521     sock = socket (AF_UNIX, SOCK_STREAM, 0);
522
523     local.sun_family = AF_UNIX;
524     strcpy (local.sun_path, socket_path);
525     unlink (local.sun_path);
526
527     len = strlen (local.sun_path) + sizeof (local.sun_family);
528     bind (sock, (struct sockaddr *) &local, len);
529
530     if (errno == -1) {
531         printf ("A problem occurred when opening a socket in %s\n", socket_path);
532     } else {
533         printf ("Control socket opened in %s\n", socket_path);
534     }
535
536     listen (sock, 5);
537  
538     char buffer[512];
539     char temp[128];
540     int done, n;
541     for(;;) {
542         memset (buffer, 0, sizeof (buffer));
543
544         t          = sizeof (remote);
545         clientsock = accept (sock, (struct sockaddr *) &remote, &t);
546         printf ("Connected to client\n");
547
548         done = 0;
549         do {
550             memset (temp, 0, sizeof (temp));
551             n = recv (clientsock, temp, 128, 0);
552             if (n == 0) {
553                 buffer[strlen (buffer)] = '\0';
554                 done = 1;
555             }
556
557             if (!done)
558                 strcat (buffer, temp);
559         } while (!done);
560
561         if (strcmp (buffer, "\n") < 0) {
562             buffer[strlen (buffer) - 1] = '\0';
563         } else {
564           buffer[strlen (buffer)] = '\0';
565         }
566
567         parse_line (buffer);
568         close (clientsock);
569     }
570
571     return NULL;
572
573  
574 static void
575 setup_threading () {
576     pthread_t control_thread;
577     pthread_create(&control_thread, NULL, control_socket, NULL);
578     pthread_detach(control_thread);
579 }
580
581 static void
582 update_title (void) {
583     GString* string_long = g_string_new ("");
584     GString* string_short = g_string_new ("");
585
586     g_string_append_printf(string_long, "%s ", keycmd->str);
587     if (!always_insert_mode)
588         g_string_append (string_long, (insert_mode ? "[I] " : "[C] "));
589     if (main_title) {
590         g_string_append (string_long, main_title);
591         g_string_append (string_short, main_title);
592     }
593     g_string_append (string_long, " - Uzbl browser");
594     g_string_append (string_short, " - Uzbl browser");
595     if (load_progress < 100)
596         g_string_append_printf (string_long, " (%d%%)", load_progress);
597
598     if (selected_url[0]!=0) {
599         g_string_append_printf (string_long, " -> (%s)", selected_url);
600     }
601
602     gchar* title_long = g_string_free (string_long, FALSE);
603     gchar* title_short = g_string_free (string_short, FALSE);
604
605     if (show_status) {
606         gtk_window_set_title (GTK_WINDOW(main_window), title_short);
607     gtk_label_set_text(GTK_LABEL(mainbar_label), title_long);
608     } else {
609         gtk_window_set_title (GTK_WINDOW(main_window), title_long);
610     }
611
612     g_free (title_long);
613     g_free (title_short);
614 }
615  
616 static gboolean
617 key_press_cb (WebKitWebView* page, GdkEventKey* event)
618 {
619     //TRUE to stop other handlers from being invoked for the event. FALSE to propagate the event further.
620
621     (void) page;
622     Action *action;
623
624     if (event->type != GDK_KEY_PRESS || event->keyval == GDK_Page_Up || event->keyval == GDK_Page_Down
625         || event->keyval == GDK_Up || event->keyval == GDK_Down || event->keyval == GDK_Left || event->keyval == GDK_Right)
626         return FALSE;
627
628     /* turn off insert mode (if always_insert_mode is not used) */
629     if (insert_mode && (event->keyval == GDK_Escape)) {
630         insert_mode = always_insert_mode;
631         update_title();
632         return TRUE;
633     }
634
635     if (insert_mode && event->state != modmask)
636         return FALSE;
637
638
639     if (event->keyval == GDK_Escape) {
640         g_string_truncate(keycmd, 0);
641
642         update_title();
643
644         return TRUE;
645     }
646
647     g_string_append(keycmd, event->string);
648
649     if ((action = g_hash_table_lookup(bindings, keycmd->str))) {
650         g_string_truncate(keycmd, 0);
651
652         parse_command(action->name, action->param);
653     }
654
655     update_title();
656
657     return TRUE;
658 }
659
660 static GtkWidget*
661 create_browser () {
662     GtkWidget* scrolled_window = gtk_scrolled_window_new (NULL, NULL);
663     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
664
665     web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
666     gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));
667
668     g_signal_connect (G_OBJECT (web_view), "title-changed", G_CALLBACK (title_change_cb), web_view);
669     g_signal_connect (G_OBJECT (web_view), "load-progress-changed", G_CALLBACK (progress_change_cb), web_view);
670     g_signal_connect (G_OBJECT (web_view), "load-committed", G_CALLBACK (load_commit_cb), web_view);
671     g_signal_connect (G_OBJECT (web_view), "load-committed", G_CALLBACK (log_history_cb), web_view);
672     g_signal_connect (G_OBJECT (web_view), "hovering-over-link", G_CALLBACK (link_hover_cb), web_view);
673     g_signal_connect (G_OBJECT (web_view), "key-press-event", G_CALLBACK (key_press_cb), web_view);
674     g_signal_connect (G_OBJECT (web_view), "new-window-policy-decision-requested", G_CALLBACK (new_window_cb), web_view); 
675     g_signal_connect (G_OBJECT (web_view), "download-requested", G_CALLBACK (download_cb), web_view); 
676     g_signal_connect (G_OBJECT (web_view), "create-web-view", G_CALLBACK (create_web_view_cb), web_view);  
677
678     return scrolled_window;
679 }
680
681 static GtkWidget*
682 create_mainbar () {
683     mainbar = gtk_hbox_new (FALSE, 0);
684     mainbar_label = gtk_label_new ("");  
685     gtk_misc_set_alignment (GTK_MISC(mainbar_label), 0, 0);
686     gtk_misc_set_padding (GTK_MISC(mainbar_label), 2, 2);
687     gtk_box_pack_start (GTK_BOX (mainbar), mainbar_label, TRUE, TRUE, 0);
688     return mainbar;
689 }
690
691 static
692 GtkWidget* create_window () {
693     GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
694     gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
695     gtk_widget_set_name (window, "Uzbl browser");
696     g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy_cb), NULL);
697
698     return window;
699 }
700
701 static void
702 add_binding (const gchar *key, const gchar *act) {
703     char **parts = g_strsplit(act, " ", 2);
704     Action *action;
705
706     if (!parts)
707         return;
708
709     action = new_action(parts[0], parts[1]);
710     g_hash_table_insert(bindings, g_strdup(key), action);
711
712     g_strfreev(parts);
713 }
714
715 static void
716 settings_init () {
717     GKeyFile* config;
718     gboolean res  = FALSE;
719     char *saveptr;
720     gchar** keys = NULL;
721
722     if (!config_file) {
723         const char* XDG_CONFIG_HOME = getenv ("XDG_CONFIG_HOME");
724         if (! XDG_CONFIG_HOME || ! strcmp (XDG_CONFIG_HOME, "")) {
725           XDG_CONFIG_HOME = (char*)XDG_CONFIG_HOME_default;
726         }
727         printf("XDG_CONFIG_HOME: %s\n", XDG_CONFIG_HOME);
728     
729         strcpy (config_file_path, XDG_CONFIG_HOME);
730         strcat (config_file_path, "/uzbl/config");
731         if (file_exists (config_file_path)) {
732           printf ("Config file %s found.\n", config_file_path);
733           config_file = &config_file_path[0];
734         } else {
735             // Now we check $XDG_CONFIG_DIRS
736             char *XDG_CONFIG_DIRS = getenv ("XDG_CONFIG_DIRS");
737             if (! XDG_CONFIG_DIRS || ! strcmp (XDG_CONFIG_DIRS, ""))
738                 XDG_CONFIG_DIRS = XDG_CONFIG_DIRS_default;
739
740             printf("XDG_CONFIG_DIRS: %s\n", XDG_CONFIG_DIRS);
741
742             char buffer[512];
743             strcpy (buffer, XDG_CONFIG_DIRS);
744             const gchar* dir = (char *) strtok_r (buffer, ":", &saveptr);
745             while (dir && ! file_exists (config_file_path)) {
746                 strcpy (config_file_path, dir);
747                 strcat (config_file_path, "/uzbl/config_file_pathig");
748                 if (file_exists (config_file_path)) {
749                     printf ("Config file %s found.\n", config_file_path);
750                     config_file = &config_file_path[0];
751                 }
752                 dir = (char * ) strtok_r (NULL, ":", &saveptr);
753             }
754         }
755     }
756
757     if (config_file) {
758         config = g_key_file_new ();
759         res = g_key_file_load_from_file (config, config_file, G_KEY_FILE_NONE, NULL);
760           if(res) {
761             printf ("Config %s loaded\n", config_file);
762           } else {
763             fprintf (stderr, "Config %s loading failed\n", config_file);
764         }
765     } else {
766         printf ("No configuration.\n");
767     }
768
769     if (res) {
770         history_handler    = g_key_file_get_value   (config, "behavior", "history_handler",    NULL);
771         download_handler   = g_key_file_get_value   (config, "behavior", "download_handler",   NULL);
772         always_insert_mode = g_key_file_get_boolean (config, "behavior", "always_insert_mode", NULL);
773         show_status        = g_key_file_get_boolean (config, "behavior", "show_status",        NULL);
774         modkey             = g_key_file_get_value   (config, "behavior", "modkey",             NULL);
775         status_top         = g_key_file_get_boolean (config, "behavior", "status_top",         NULL);
776         if (! fifo_dir)
777             fifo_dir        = g_key_file_get_value   (config, "behavior", "fifodir",            NULL);
778         if (! socket_dir)
779             socket_dir     = g_key_file_get_value   (config, "behavior", "socket_dir",         NULL);
780         keys               = g_key_file_get_keys    (config, "bindings", NULL,                 NULL);
781     }
782     
783     printf ("History handler: %s\n",    (history_handler    ? history_handler  : "disabled"));
784     printf ("Download manager: %s\n",   (download_handler   ? download_handler : "disabled"));
785     printf ("Fifo directory: %s\n",     (fifo_dir           ? fifo_dir         : "/tmp"));
786     printf ("Socket directory: %s\n",   (socket_dir         ? socket_dir       : "/tmp"));
787     printf ("Always insert mode: %s\n", (always_insert_mode ? "TRUE"           : "FALSE"));
788     printf ("Show status: %s\n",        (show_status        ? "TRUE"           : "FALSE"));
789     printf ("Status top: %s\n",         (status_top         ? "TRUE"           : "FALSE"));
790     printf ("Modkey: %s\n",             (modkey             ? modkey           : "disabled"));
791
792     if (! modkey)
793         modkey = "";
794
795     //POSSIBLE MODKEY VALUES (COMBINATIONS CAN BE USED)
796     gchar* modkeyup = g_utf8_strup (modkey, -1);
797     if (g_strrstr (modkeyup,"SHIFT") != NULL)    modmask |= GDK_SHIFT_MASK;    //the Shift key.
798     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).
799     if (g_strrstr (modkeyup,"CONTROL") != NULL)  modmask |= GDK_CONTROL_MASK;  //the Control key.
800     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).
801     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).
802     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).
803     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).
804     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).
805     if (g_strrstr (modkeyup,"BUTTON1") != NULL)  modmask |= GDK_BUTTON1_MASK;  //the first mouse button.
806     if (g_strrstr (modkeyup,"BUTTON2") != NULL)  modmask |= GDK_BUTTON2_MASK;  //the second mouse button.
807     if (g_strrstr (modkeyup,"BUTTON3") != NULL)  modmask |= GDK_BUTTON3_MASK;  //the third mouse button.
808     if (g_strrstr (modkeyup,"BUTTON4") != NULL)  modmask |= GDK_BUTTON4_MASK;  //the fourth mouse button.
809     if (g_strrstr (modkeyup,"BUTTON5") != NULL)  modmask |= GDK_BUTTON5_MASK;  //the fifth mouse button.
810     if (g_strrstr (modkeyup,"SUPER") != NULL)    modmask |= GDK_SUPER_MASK;    //the Super modifier. Since 2.10
811     if (g_strrstr (modkeyup,"HYPER") != NULL)    modmask |= GDK_HYPER_MASK;    //the Hyper modifier. Since 2.10
812     if (g_strrstr (modkeyup,"META") != NULL)     modmask |= GDK_META_MASK;     //the Meta modifier. Since 2.10  */
813     free (modkeyup);
814
815     if (keys) {
816         int i;
817         for (i = 0; keys[i]; i++) {
818             gchar *value = g_key_file_get_string (config, "bindings", keys[i], NULL);
819             
820             add_binding(g_strstrip(keys[i]), value);
821             g_free(value);
822         }
823
824         g_strfreev(keys);
825     }
826 }
827
828 int
829 main (int argc, char* argv[]) {
830     gtk_init (&argc, &argv);
831     if (!g_thread_supported ())
832         g_thread_init (NULL);
833
834     printf("Uzbl start location: %s\n", argv[0]);
835
836     strcat ((char *) XDG_CONFIG_HOME_default, getenv ("HOME"));
837     strcat ((char *) XDG_CONFIG_HOME_default, "/.config");
838
839     GError *error = NULL;
840     GOptionContext* context = g_option_context_new ("- some stuff here maybe someday");
841     g_option_context_add_main_entries (context, entries, NULL);
842     g_option_context_add_group (context, gtk_get_option_group (TRUE));
843     g_option_context_parse (context, &argc, &argv, &error);
844     /* initialize hash table */
845     bindings = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, free_action);
846
847     keycmd = g_string_new("");
848
849     settings_init ();
850     commands_hash ();
851
852     if (always_insert_mode)
853         insert_mode = TRUE;
854
855     GtkWidget* vbox = gtk_vbox_new (FALSE, 0);
856     if (status_top)
857         gtk_box_pack_start (GTK_BOX (vbox), create_mainbar (), FALSE, TRUE, 0);
858     gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
859     if (!status_top)
860         gtk_box_pack_start (GTK_BOX (vbox), create_mainbar (), FALSE, TRUE, 0);
861
862     main_window = create_window ();
863     gtk_container_add (GTK_CONTAINER (main_window), vbox);
864
865     load_uri (web_view, uri);
866
867     gtk_widget_grab_focus (GTK_WIDGET (web_view));
868     gtk_widget_show_all (main_window);
869     xwin = GDK_WINDOW_XID (GTK_WIDGET (main_window)->window);
870     printf("window_id %i\n",(int) xwin);
871     printf("pid %i\n", getpid ());
872
873     scbar_v = (GtkScrollbar*) gtk_vscrollbar_new (NULL);
874     bar_v = gtk_range_get_adjustment((GtkRange*) scbar_v);
875     scbar_h = (GtkScrollbar*) gtk_hscrollbar_new (NULL);
876     bar_h = gtk_range_get_adjustment((GtkRange*) scbar_h);
877     gtk_widget_set_scroll_adjustments ((GtkWidget*) web_view, bar_h, bar_v);
878
879     if (!show_status)
880         gtk_widget_hide(mainbar);
881
882     setup_threading ();
883     create_fifo ();
884
885     gtk_main ();
886
887     g_string_free(keycmd, TRUE);
888
889     unlink (socket_path);
890     unlink (fifo_path);
891
892     g_hash_table_destroy(bindings);
893     g_hash_table_destroy(commands);
894     return 0;
895 }
896
897 /* vi: set et ts=4: */