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