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.
6 * Copyright (C) 2006, 2007 Apple Inc.
7 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
32 #define LENGTH(x) (sizeof x / sizeof x[0])
36 #include <gdk/gdkkeysyms.h>
37 #include <webkit/webkit.h>
42 #include <sys/types.h>
46 /* housekeeping / internal variables */
47 static GtkWidget* main_window;
48 static GtkWidget* mainbar;
49 static GtkWidget* mainbar_label;
50 static WebKitWebView* web_view;
51 static gchar* main_title;
52 static gchar selected_url[500];
53 static gint load_progress;
54 static Window xwin = 0;
55 static char fifopath[64];
57 /* state variables (initial values coming from command line arguments but may be changed later) */
58 static gchar* uri = NULL;
59 static gchar* config_file = NULL;
60 static gboolean verbose = FALSE;
62 /* settings from config: group behaviour */
63 static gchar* history_handler = NULL;
64 static gchar* fifodir = NULL;
65 static gchar* download_handler = NULL;
66 static gboolean always_insert_mode = FALSE;
67 static gboolean show_status = FALSE;
68 static gboolean insert_mode = FALSE;
69 static gboolean status_top = FALSE;
70 static gchar* modkey = NULL;
71 static guint modmask = 0;
78 /* settings from config: group bindings_internal */
79 static Binding internal_bindings[256];
80 static int num_internal_bindings = 0;
82 /* settings from config: group bindings_external */
83 static Binding external_bindings[256];
84 static int num_external_bindings = 0;
86 /* commandline arguments (set initial values for the state variables) */
87 static GOptionEntry entries[] =
89 { "uri", 'u', 0, G_OPTION_ARG_STRING, &uri, "Uri to load", NULL },
90 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
91 { "config", 'c', 0, G_OPTION_ARG_STRING, &config_file, "Config file", NULL },
92 { NULL, 0, 0, 0, NULL, NULL, NULL }
95 /* for internal list of commands */
99 void (*func_1_param)(WebKitWebView*);
100 void (*func_2_params)(WebKitWebView*, const gchar *);
104 char *XDG_CONFIG_HOME_default[256];
105 char *XDG_CONFIG_DIRS_default = "/etc/xdg";
108 update_title (GtkWindow* window);
111 run_command(const char *command, const char *args);
114 /* --- CALLBACKS --- */
116 go_back_cb (WebKitWebView* page) {
118 webkit_web_view_go_back (web_view);
122 go_forward_cb (WebKitWebView* page) {
124 webkit_web_view_go_forward (web_view);
128 toggle_status_cb (WebKitWebView* page) {
131 gtk_widget_hide(mainbar);
133 gtk_widget_show(mainbar);
135 show_status = !show_status;
136 update_title (GTK_WINDOW (main_window));
140 link_hover_cb (WebKitWebView* page, const gchar* title, const gchar* link, gpointer data) {
144 //ADD HOVER URL TO WINDOW TITLE
145 selected_url[0] = '\0';
147 strcpy (selected_url, link);
149 update_title (GTK_WINDOW (main_window));
153 title_change_cb (WebKitWebView* web_view, WebKitWebFrame* web_frame, const gchar* title, gpointer data) {
159 main_title = g_strdup (title);
160 update_title (GTK_WINDOW (main_window));
164 progress_change_cb (WebKitWebView* page, gint progress, gpointer data) {
167 load_progress = progress;
168 update_title (GTK_WINDOW (main_window));
172 load_commit_cb (WebKitWebView* page, WebKitWebFrame* frame, gpointer data) {
175 strcpy (uri, webkit_web_frame_get_uri (frame));
179 destroy_cb (GtkWidget* widget, gpointer data) {
187 if (history_handler) {
189 struct tm * timeinfo;
192 timeinfo = localtime ( &rawtime );
193 strftime (date, 80, "%Y-%m-%d %H:%M:%S", timeinfo);
194 GString* args = g_string_new ("");
195 g_string_printf (args, "'%s' '%s' '%s'", uri, "TODO:page title here", date);
196 run_command(history_handler, args->str);
200 /* -- command to callback/function map for things we cannot attach to any signals */
201 // TODO: reload, home, quit
202 static Command commands[] =
204 { "back", &go_back_cb, NULL },
205 { "forward", &go_forward_cb, NULL },
206 { "refresh", &webkit_web_view_reload, NULL }, //Buggy
207 { "stop", &webkit_web_view_stop_loading, NULL },
208 { "zoom_in", &webkit_web_view_zoom_in, NULL }, //Can crash (when max zoom reached?).
209 { "zoom_out", &webkit_web_view_zoom_out, NULL },
210 { "uri", NULL, &webkit_web_view_load_uri },
211 { "toggle_status", &toggle_status_cb, NULL }
212 //{ "get uri", &webkit_web_view_get_uri},
215 /* -- CORE FUNCTIONS -- */
218 file_exists (const char * filename)
220 FILE *file = fopen (filename, "r");
228 // make sure to put '' around args, so that if there is whitespace we can still keep arguments together.
230 run_command(const char *command, const char *args) {
231 //command <uzbl conf> <uzbl pid> <uzbl win id> <uzbl fifo file> [args]
232 GString* to_execute = g_string_new ("");
234 g_string_printf (to_execute, "%s '%s' '%i' '%i' '%s' %s", command, config_file, (int) getpid() , (int) xwin, "/tmp/uzbl_25165827", args);
235 result = g_spawn_command_line_async (to_execute->str, NULL);
236 printf("Called %s. Result: %s\n", to_execute->str, (result ? "TRUE" : "FALSE" ));
241 parse_command(const char *cmd) {
245 strcpy (buffer, cmd);
246 char * command_name = strtok (buffer, " ");
247 char * command_param = strtok (NULL, " ,");
249 Command *c_tmp = NULL;
250 for (i = 0; i < LENGTH (commands); i++) {
251 c_tmp = &commands[i];
252 if (strncmp (command_name, c_tmp->command, strlen (c_tmp->command)) == 0) {
257 if (c->func_2_params != NULL) {
258 if (command_param != NULL) {
259 printf ("command executing: \"%s %s\"\n", command_name, command_param);
260 c->func_2_params (web_view, command_param);
262 if (c->func_1_param != NULL) {
263 printf ("command executing: \"%s\"\n", command_name);
264 c->func_1_param (web_view);
266 fprintf (stderr, "command needs a parameter. \"%s\" is not complete\n", command_name);
269 } else if (c->func_1_param != NULL) {
270 printf ("command executing: \"%s\"\n", command_name);
271 c->func_1_param (web_view);
274 fprintf (stderr, "command \"%s\" not understood. ignoring.\n", cmd);
281 sprintf (fifopath, "%s/uzbl_%d", fifodir, (int) xwin);
283 sprintf (fifopath, "/tmp/uzbl_%d", (int) xwin);
286 if (mkfifo (fifopath, 0666) == -1) {
287 printf ("Possible error creating fifo\n");
290 printf ("Control fifo opened in %s\n", fifopath);
293 FILE *fifo = fopen (fifopath, "r");
295 printf ("Could not open %s for reading\n", fifopath);
300 memset (buffer, 0, sizeof (buffer));
301 while (!feof (fifo) && fgets (buffer, sizeof (buffer), fifo)) {
302 if (strcmp (buffer, "\n")) {
303 buffer[strlen (buffer) - 1] = '\0'; // Remove newline
304 parse_command (buffer);
314 pthread_t control_thread;
315 pthread_create(&control_thread, NULL, control_fifo, NULL);
319 update_title (GtkWindow* window) {
320 GString* string_long = g_string_new ("");
321 GString* string_short = g_string_new ("");
322 if (!always_insert_mode)
323 g_string_append (string_long, (insert_mode ? "[I] " : "[C] "));
324 g_string_append (string_long, main_title);
325 g_string_append (string_short, main_title);
326 g_string_append (string_long, " - Uzbl browser");
327 g_string_append (string_short, " - Uzbl browser");
328 if (load_progress < 100)
329 g_string_append_printf (string_long, " (%d%%)", load_progress);
331 if (selected_url[0]!=0) {
332 g_string_append_printf (string_long, " -> (%s)", selected_url);
335 gchar* title_long = g_string_free (string_long, FALSE);
336 gchar* title_short = g_string_free (string_short, FALSE);
339 gtk_window_set_title (window, title_short);
340 gtk_label_set_text(GTK_LABEL(mainbar_label), title_long);
342 gtk_window_set_title (window, title_long);
346 g_free (title_short);
350 key_press_cb (WebKitWebView* page, GdkEventKey* event)
354 gboolean result=FALSE; //TRUE to stop other handlers from being invoked for the event. FALSE to propagate the event further.
355 if (event->type != GDK_KEY_PRESS)
358 //TURN OFF/ON INSERT MODE
359 if (!always_insert_mode && ((insert_mode && (event->keyval == GDK_Escape)) || (!insert_mode && (event->string[0] == 'i')))) {
360 insert_mode = !insert_mode;
361 update_title (GTK_WINDOW (main_window));
366 for (i = 0; i < num_internal_bindings; i++) {
367 if (strcmp(event->string, internal_bindings[i].binding) == 0) {
368 if (!insert_mode || (event->state == modmask)) {
369 parse_command (internal_bindings[i].action);
376 for (i = 0; i < num_external_bindings; i++) {
377 if (strcmp(event->string, external_bindings[i].binding) == 0) {
378 if (!insert_mode || (event->state == modmask)) {
379 run_command(external_bindings[i].action, NULL);
386 result = (insert_mode ? FALSE : TRUE);
393 GtkWidget* scrolled_window = gtk_scrolled_window_new (NULL, NULL);
394 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
396 web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
397 gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));
399 g_signal_connect (G_OBJECT (web_view), "title-changed", G_CALLBACK (title_change_cb), web_view);
400 g_signal_connect (G_OBJECT (web_view), "load-progress-changed", G_CALLBACK (progress_change_cb), web_view);
401 g_signal_connect (G_OBJECT (web_view), "load-committed", G_CALLBACK (load_commit_cb), web_view);
402 g_signal_connect (G_OBJECT (web_view), "load-committed", G_CALLBACK (log_history_cb), web_view);
403 g_signal_connect (G_OBJECT (web_view), "hovering-over-link", G_CALLBACK (link_hover_cb), web_view);
404 g_signal_connect (G_OBJECT (web_view), "key-press-event", G_CALLBACK (key_press_cb), web_view);
406 return scrolled_window;
411 mainbar = gtk_hbox_new (FALSE, 0);
412 mainbar_label = gtk_label_new ("");
413 gtk_misc_set_alignment (GTK_MISC(mainbar_label), 0, 0);
414 gtk_misc_set_padding (GTK_MISC(mainbar_label), 2, 2);
415 gtk_box_pack_start (GTK_BOX (mainbar), mainbar_label, TRUE, TRUE, 0);
420 GtkWidget* create_window () {
421 GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
422 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
423 gtk_widget_set_name (window, "Uzbl browser");
424 g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy_cb), NULL);
430 add_binding (char *binding, char *action, bool internal) {
431 Binding bind = {binding, action};
433 internal_bindings[num_internal_bindings] = bind;
434 num_internal_bindings ++;
436 external_bindings[num_external_bindings] = bind;
437 num_external_bindings ++;
444 gboolean res = FALSE;
445 gchar** keysi = NULL;
446 gchar** keyse = NULL;
449 const char* XDG_CONFIG_HOME = getenv ("XDG_CONFIG_HOME");
451 if (! XDG_CONFIG_HOME || ! strcmp (XDG_CONFIG_HOME, "")) {
452 XDG_CONFIG_HOME = (char *)XDG_CONFIG_HOME_default;
454 printf("XDG_CONFIG_HOME: %s\n", XDG_CONFIG_HOME);
456 strcpy (conf, XDG_CONFIG_HOME);
457 strcat (conf, "/uzbl");
458 if (file_exists (conf)) {
459 printf ("Config file %s found.\n", conf);
460 config_file = &conf[0];
462 // Now we check $XDG_CONFIG_DIRS
463 char *XDG_CONFIG_DIRS = getenv ("XDG_CONFIG_DIRS");
464 if (! XDG_CONFIG_DIRS || ! strcmp (XDG_CONFIG_DIRS, ""))
465 XDG_CONFIG_DIRS = XDG_CONFIG_DIRS_default;
467 printf("XDG_CONFIG_DIRS: %s\n", XDG_CONFIG_DIRS);
469 char *dirs = XDG_CONFIG_DIRS;
470 char *dir = (char *)strtok (dirs, ":");
471 while (dir && ! file_exists (conf)) {
473 strcat (conf, "/uzbl");
474 if (file_exists (conf)) {
475 printf ("Config file %s found.\n", conf);
476 config_file = &conf[0];
478 dir = (char *)strtok (NULL, ":");
484 config = g_key_file_new ();
485 res = g_key_file_load_from_file (config, config_file, G_KEY_FILE_NONE, NULL);
487 printf ("Config %s loaded\n", config_file);
489 fprintf (stderr, "Config %s loading failed\n", config_file);
492 printf ("No configuration.\n");
496 history_handler = g_key_file_get_value (config, "behavior", "history_handler", NULL);
497 download_handler = g_key_file_get_value (config, "behavior", "download_handler", NULL);
498 always_insert_mode = g_key_file_get_boolean (config, "behavior", "always_insert_mode", NULL);
499 show_status = g_key_file_get_boolean (config, "behavior", "show_status", NULL);
500 modkey = g_key_file_get_value (config, "behavior", "modkey", NULL);
501 keysi = g_key_file_get_keys (config, "bindings_internal", NULL, NULL);
502 keyse = g_key_file_get_keys (config, "bindings_external", NULL, NULL);
503 status_top = g_key_file_get_boolean (config, "behavior", "status_top", NULL);
505 fifodir = g_key_file_get_value (config, "behavior", "fifodir", NULL);
508 if (history_handler) {
509 printf ("History handler: %s\n", history_handler);
511 printf ("History handler disabled\n");
514 if (download_handler) {
515 printf ("Download manager: %s\n", download_handler);
517 printf ("Download manager disabled\n");
521 printf ("Fifo directory: %s\n", fifodir);
523 printf ("Fifo directory: /tmp\n");
526 printf ("Always insert mode: %s\n", (always_insert_mode ? "TRUE" : "FALSE"));
528 printf ("Show status: %s\n", (show_status ? "TRUE" : "FALSE"));
530 printf ("Status top: %s\n", (status_top ? "TRUE" : "FALSE"));
533 printf ("Modkey: %s\n", modkey);
535 printf ("Modkey disabled\n");
538 //POSSIBLE MODKEY VALUES (COMBINATIONS CAN BE USED)
539 gchar* modkeyup = g_utf8_strup (modkey, -1);
540 if (g_strrstr (modkeyup,"SHIFT") != NULL) modmask |= GDK_SHIFT_MASK; //the Shift key.
541 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).
542 if (g_strrstr (modkeyup,"CONTROL") != NULL) modmask |= GDK_CONTROL_MASK; //the Control key.
543 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).
544 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).
545 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).
546 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).
547 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).
548 if (g_strrstr (modkeyup,"BUTTON1") != NULL) modmask |= GDK_BUTTON1_MASK; //the first mouse button.
549 if (g_strrstr (modkeyup,"BUTTON2") != NULL) modmask |= GDK_BUTTON2_MASK; //the second mouse button.
550 if (g_strrstr (modkeyup,"BUTTON3") != NULL) modmask |= GDK_BUTTON3_MASK; //the third mouse button.
551 if (g_strrstr (modkeyup,"BUTTON4") != NULL) modmask |= GDK_BUTTON4_MASK; //the fourth mouse button.
552 if (g_strrstr (modkeyup,"BUTTON5") != NULL) modmask |= GDK_BUTTON5_MASK; //the fifth mouse button.
553 if (g_strrstr (modkeyup,"SUPER") != NULL) modmask |= GDK_SUPER_MASK; //the Super modifier. Since 2.10
554 if (g_strrstr (modkeyup,"HYPER") != NULL) modmask |= GDK_HYPER_MASK; //the Hyper modifier. Since 2.10
555 if (g_strrstr (modkeyup,"META") != NULL) modmask |= GDK_META_MASK; //the Meta modifier. Since 2.10 */
560 for (i = 0; keysi[i]; i++) {
561 gchar *binding = g_key_file_get_string (config, "bindings_internal", keysi[i], NULL);
562 printf ("Action: %s, Binding: %s (internal)\n", g_strdup (keysi[i]), binding);
563 add_binding (binding, g_strdup (keysi[i]), true);
568 for (i = 0; keyse[i]; i++) {
569 gchar *binding = g_key_file_get_string (config, "bindings_external", keyse[i], NULL);
570 printf ("Action: %s, Binding: %s (external)\n", g_strdup (keyse[i]), binding);
571 add_binding (binding, g_strdup (keyse[i]), false);
577 main (int argc, char* argv[]) {
578 gtk_init (&argc, &argv);
579 if (!g_thread_supported ())
580 g_thread_init (NULL);
582 strcat ((char *) XDG_CONFIG_HOME_default, getenv ("HOME"));
583 strcat ((char *) XDG_CONFIG_HOME_default, "/.config");
585 GError *error = NULL;
586 GOptionContext* context = g_option_context_new ("- some stuff here maybe someday");
587 g_option_context_add_main_entries (context, entries, NULL);
588 g_option_context_add_group (context, gtk_get_option_group (TRUE));
589 g_option_context_parse (context, &argc, &argv, &error);
592 if (always_insert_mode)
595 GtkWidget* vbox = gtk_vbox_new (FALSE, 0);
597 gtk_box_pack_start (GTK_BOX (vbox), create_mainbar (), FALSE, TRUE, 0);
598 gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
600 gtk_box_pack_start (GTK_BOX (vbox), create_mainbar (), FALSE, TRUE, 0);
602 main_window = create_window ();
603 gtk_container_add (GTK_CONTAINER (main_window), vbox);
605 webkit_web_view_load_uri (web_view, uri);
607 gtk_widget_grab_focus (GTK_WIDGET (web_view));
608 gtk_widget_show_all (main_window);
609 xwin = GDK_WINDOW_XID (GTK_WIDGET (main_window)->window);
610 printf("window_id %i\n",(int) xwin);
611 printf("pid %i\n", getpid ());
614 gtk_widget_hide(mainbar);