Fixed wrong history entries.
[vimprobable2.git] / main.c
blobc8fe5cb6091fbc4dc93b37e56b5b77ae8a716635
1 /*
2 (c) 2009 by Leon Winter
3 (c) 2009-2012 by Hannes Schueller
4 (c) 2009-2010 by Matto Fransen
5 (c) 2010-2011 by Hans-Peter Deifel
6 (c) 2010-2011 by Thomas Adam
7 (c) 2011 by Albert Kim
8 (c) 2011 by Daniel Carl
9 (c) 2012 by Matthew Carter
10 see LICENSE file
13 #include <X11/Xlib.h>
14 #include "includes.h"
15 #include "vimprobable.h"
16 #include "utilities.h"
17 #include "callbacks.h"
18 #include "javascript.h"
20 /* the CLEAN_MOD_*_MASK defines have all the bits set that will be stripped from the modifier bit field */
21 #define CLEAN_MOD_NUMLOCK_MASK (GDK_MOD2_MASK)
22 #define CLEAN_MOD_BUTTON_MASK (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK|GDK_BUTTON3_MASK|GDK_BUTTON4_MASK|GDK_BUTTON5_MASK)
24 /* remove unused bits, numlock symbol and buttons from keymask */
25 #define CLEAN(mask) (mask & (GDK_MODIFIER_MASK) & ~(CLEAN_MOD_NUMLOCK_MASK) & ~(CLEAN_MOD_BUTTON_MASK))
27 #define IS_ESCAPE(event) (IS_ESCAPE_KEY(CLEAN(event->state), event->keyval))
28 #define IS_ESCAPE_KEY(s, k) ((s == 0 && k == GDK_Escape) || \
29 (s == GDK_CONTROL_MASK && k == GDK_bracketleft))
31 /* callbacks here */
32 static void inputbox_activate_cb(GtkEntry *entry, gpointer user_data);
33 static gboolean inputbox_keypress_cb(GtkEntry *entry, GdkEventKey *event);
34 static gboolean inputbox_keyrelease_cb(GtkEntry *entry, GdkEventKey *event);
35 static gboolean inputbox_changed_cb(GtkEditable *entry, gpointer user_data);
36 static WebKitWebView* inspector_inspect_web_view_cb(gpointer inspector, WebKitWebView* web_view);
37 static gboolean notify_event_cb(GtkWidget *widget, GdkEvent *event, gpointer user_data);
38 static gboolean webview_console_cb(WebKitWebView *webview, char *message, int line, char *source, gpointer user_data);
39 static gboolean webview_download_cb(WebKitWebView *webview, WebKitDownload *download, gpointer user_data);
40 static void webview_hoverlink_cb(WebKitWebView *webview, char *title, char *link, gpointer data);
41 static gboolean webview_keypress_cb(WebKitWebView *webview, GdkEventKey *event);
42 static void webview_load_committed_cb(WebKitWebView *webview, WebKitWebFrame *frame, gpointer user_data);
43 static void webview_load_finished_cb(WebKitWebView *webview, WebKitWebFrame *frame, gpointer user_data);
44 static gboolean webview_mimetype_cb(WebKitWebView *webview, WebKitWebFrame *frame, WebKitNetworkRequest *request,
45 char *mime_type, WebKitWebPolicyDecision *decision, gpointer user_data);
46 static gboolean webview_new_window_cb(WebKitWebView *webview, WebKitWebFrame *frame, WebKitNetworkRequest *request,
47 WebKitWebNavigationAction *action, WebKitWebPolicyDecision *decision, gpointer user_data);
48 static gboolean webview_open_in_new_window_cb(WebKitWebView *webview, WebKitWebFrame *frame, gpointer user_data);
49 static void webview_progress_changed_cb(WebKitWebView *webview, int progress, gpointer user_data);
50 static void webview_title_changed_cb(WebKitWebView *webview, WebKitWebFrame *frame, char *title, gpointer user_data);
51 static void window_destroyed_cb(GtkWidget *window, gpointer func_data);
52 static gboolean blank_cb(void);
54 /* functions */
55 static gboolean bookmark(const Arg *arg);
56 static gboolean browser_settings(const Arg *arg);
57 static gboolean commandhistoryfetch(const Arg *arg);
58 static gboolean complete(const Arg *arg);
59 static gboolean descend(const Arg *arg);
60 gboolean echo(const Arg *arg);
61 static gboolean focus_input(const Arg *arg);
62 static gboolean input(const Arg *arg);
63 static gboolean navigate(const Arg *arg);
64 static gboolean number(const Arg *arg);
65 static gboolean open_arg(const Arg *arg);
66 static gboolean open_remembered(const Arg *arg);
67 static gboolean paste(const Arg *arg);
68 static gboolean quickmark(const Arg *arg);
69 static gboolean quit(const Arg *arg);
70 static gboolean revive(const Arg *arg);
71 static gboolean print_frame(const Arg *arg);
72 static gboolean search(const Arg *arg);
73 static gboolean set(const Arg *arg);
74 static gboolean script(const Arg *arg);
75 static gboolean scroll(const Arg *arg);
76 static gboolean search_tag(const Arg *arg);
77 static gboolean yank(const Arg *arg);
78 static gboolean view_source(const Arg * arg);
79 static gboolean zoom(const Arg *arg);
80 static gboolean fake_key_event(const Arg *arg);
82 static void update_url(const char *uri);
83 static void setup_modkeys(void);
84 static void setup_gui(void);
85 static void setup_settings(void);
86 static void setup_signals(void);
87 static void ascii_bar(int total, int state, char *string);
88 static gchar *jsapi_ref_to_string(JSContextRef context, JSValueRef ref);
89 static void jsapi_evaluate_script(const gchar *script, gchar **value, gchar **message);
90 static void download_progress(WebKitDownload *d, GParamSpec *pspec);
91 static void set_widget_font_and_color(GtkWidget *widget, const char *font_str,
92 const char *bg_color_str, const char *fg_color_str);
94 static gboolean history(void);
95 static gboolean process_set_line(char *line);
96 void save_command_history(char *line);
97 void toggle_proxy(gboolean onoff);
98 void toggle_scrollbars(gboolean onoff);
100 gboolean process_keypress(GdkEventKey *event);
101 void fill_suggline(char * suggline, const char * command, const char *fill_with);
102 GtkWidget * fill_eventbox(const char * completion_line);
103 static void mop_up(void);
105 #include "main.h"
107 /* variables */
108 static GtkWindow *window;
109 static GtkWidget *viewport;
110 static GtkBox *box;
111 static GtkScrollbar *scroll_h;
112 static GtkScrollbar *scroll_v;
113 static GtkAdjustment *adjust_h;
114 static GtkAdjustment *adjust_v;
115 static GtkWidget *inputbox;
116 static GtkWidget *eventbox;
117 static GtkBox *statusbar;
118 static GtkWidget *status_url;
119 static GtkWidget *status_state;
120 static WebKitWebView *webview;
121 static SoupSession *session;
122 static GtkClipboard *clipboards[2];
123 static GdkKeymap *keymap;
125 static char **args;
126 static unsigned int mode = ModeNormal;
127 static unsigned int count = 0;
128 static float zoomstep;
129 char *modkeys;
130 static char current_modkey;
131 static char *search_handle;
132 static gboolean search_direction;
133 static gboolean echo_active = TRUE;
134 WebKitWebInspector *inspector;
136 static GdkNativeWindow embed = 0;
137 static char *configfile = NULL;
138 static char *winid = NULL;
140 static char rememberedURI[1024] = "";
141 static char followTarget[8] = "";
142 char *error_msg = NULL;
143 char *config_base = NULL;
144 static gboolean manual_focus = FALSE;
146 GList *activeDownloads;
148 #include "config.h"
149 #include "keymap.h"
151 GList *commandhistory = NULL;
152 int commandpointer = 0;
154 KeyList *keylistroot = NULL;
156 /* Cookie support. */
157 #ifdef ENABLE_COOKIE_SUPPORT
158 static SoupCookieJar *session_cookie_jar = NULL;
159 static SoupCookieJar *file_cookie_jar = NULL;
160 static time_t cookie_timeout = 4800;
161 static char *cookie_store;
162 static void setup_cookies(void);
163 static char *get_cookies(SoupURI *soup_uri);
164 static void load_all_cookies(void);
165 static void new_generic_request(SoupSession *soup_ses, SoupMessage *soup_msg, gpointer unused);
166 static void update_cookie_jar(SoupCookieJar *jar, SoupCookie *old, SoupCookie *new);
167 static void handle_cookie_request(SoupMessage *soup_msg, gpointer unused);
168 #endif
169 /* callbacks */
170 void
171 window_destroyed_cb(GtkWidget *window, gpointer func_data) {
172 quit(NULL);
175 void
176 webview_title_changed_cb(WebKitWebView *webview, WebKitWebFrame *frame, char *title, gpointer user_data) {
177 gtk_window_set_title(window, title);
180 void
181 webview_progress_changed_cb(WebKitWebView *webview, int progress, gpointer user_data) {
182 #ifdef ENABLE_GTK_PROGRESS_BAR
183 gtk_entry_set_progress_fraction(GTK_ENTRY(inputbox), progress == 100 ? 0 : (double)progress/100);
184 #endif
185 update_state();
188 #ifdef ENABLE_WGET_PROGRESS_BAR
189 void
190 ascii_bar(int total, int state, char *string) {
191 int i;
193 for (i = 0; i < state; i++)
194 string[i] = progressbartickchar;
195 string[i++] = progressbarcurrent;
196 for (; i < total; i++)
197 string[i] = progressbarspacer;
198 string[i] = '\0';
200 #endif
202 void
203 webview_load_committed_cb(WebKitWebView *webview, WebKitWebFrame *frame, gpointer user_data) {
204 Arg a = { .i = Silent, .s = g_strdup(JS_SETUP_HINTS) };
205 const char *uri = webkit_web_view_get_uri(webview);
207 update_url(uri);
208 script(&a);
209 g_free(a.s);
211 if (mode == ModeInsert || mode == ModeHints) {
212 Arg a = { .i = ModeNormal };
213 set(&a);
215 manual_focus = FALSE;
218 void
219 webview_load_finished_cb(WebKitWebView *webview, WebKitWebFrame *frame, gpointer user_data) {
220 WebKitWebSettings *settings = webkit_web_view_get_settings(webview);
221 gboolean scripts;
223 g_object_get(settings, "enable-scripts", &scripts, NULL);
224 if (escape_input_on_load && scripts && !manual_focus && !gtk_widget_is_focus(inputbox)) {
225 Arg a = { .i = Silent, .s = g_strdup("hints.clearFocus();") };
226 script(&a);
227 g_free(a.s);
228 a.i = ModeNormal;
229 a.s = NULL;
230 set(&a);
232 if (HISTORY_MAX_ENTRIES > 0)
233 history();
234 update_state();
237 static gboolean
238 webview_open_in_new_window_cb(WebKitWebView *webview, WebKitWebFrame *frame, gpointer user_data) {
239 Arg a = { .i = TargetNew, .s = (char*)webkit_web_view_get_uri(webview) };
240 if (strlen(rememberedURI) > 0) {
241 a.s = rememberedURI;
243 open_arg(&a);
244 return FALSE;
247 gboolean
248 webview_new_window_cb(WebKitWebView *webview, WebKitWebFrame *frame, WebKitNetworkRequest *request,
249 WebKitWebNavigationAction *action, WebKitWebPolicyDecision *decision, gpointer user_data) {
250 Arg a = { .i = TargetNew, .s = (char*)webkit_network_request_get_uri(request) };
251 open_arg(&a);
252 webkit_web_policy_decision_ignore(decision);
253 return TRUE;
256 gboolean
257 webview_mimetype_cb(WebKitWebView *webview, WebKitWebFrame *frame, WebKitNetworkRequest *request,
258 char *mime_type, WebKitWebPolicyDecision *decision, gpointer user_data) {
259 if (webkit_web_view_can_show_mime_type(webview, mime_type) == FALSE) {
260 webkit_web_policy_decision_download(decision);
261 return TRUE;
262 } else {
263 return FALSE;
267 static WebKitWebView*
268 inspector_inspect_web_view_cb(gpointer inspector, WebKitWebView* web_view) {
269 gchar* inspector_title;
270 GtkWidget* inspector_window;
271 GtkWidget* inspector_view;
273 /* just enough code to show the inspector - no signal handling etc. */
274 inspector_title = g_strdup_printf("Inspect page - %s - Vimprobable2", webkit_web_view_get_uri(web_view));
275 if (embed) {
276 inspector_window = gtk_plug_new(embed);
277 } else {
278 inspector_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
279 gtk_window_set_wmclass(window, "vimprobable2", "Vimprobable2");
281 gtk_window_set_title(GTK_WINDOW(inspector_window), inspector_title);
282 g_free(inspector_title);
283 inspector_view = webkit_web_view_new();
284 gtk_container_add(GTK_CONTAINER(inspector_window), inspector_view);
285 gtk_widget_show_all(inspector_window);
286 return WEBKIT_WEB_VIEW(inspector_view);
289 gboolean
290 webview_download_cb(WebKitWebView *webview, WebKitDownload *download, gpointer user_data) {
291 const gchar *filename;
292 gchar *uri, *path;
293 uint32_t size;
294 Arg a;
295 WebKitDownloadStatus status;
297 filename = webkit_download_get_suggested_filename(download);
298 if (filename == NULL || strlen(filename) == 0) {
299 filename = "vimprobable_download";
301 path = g_build_filename(g_strdup_printf(DOWNLOADS_PATH), filename, NULL);
302 uri = g_strconcat("file://", path, NULL);
303 webkit_download_set_destination_uri(download, uri);
304 g_free(uri);
305 size = (uint32_t)webkit_download_get_total_size(download);
306 a.i = Info;
307 if (size > 0)
308 a.s = g_strdup_printf("Download %s started (expected size: %u bytes)...", filename, size);
309 else
310 a.s = g_strdup_printf("Download %s started (unknown size)...", filename);
311 echo(&a);
312 g_free(a.s);
313 activeDownloads = g_list_prepend(activeDownloads, download);
314 g_signal_connect(download, "notify::progress", G_CALLBACK(download_progress), NULL);
315 g_signal_connect(download, "notify::status", G_CALLBACK(download_progress), NULL);
316 status = webkit_download_get_status(download);
317 if (status == WEBKIT_DOWNLOAD_STATUS_CREATED)
318 webkit_download_start(download);
319 update_state();
320 return TRUE;
323 gboolean
324 blank_cb(void) {
325 return TRUE;
328 void
329 download_progress(WebKitDownload *d, GParamSpec *pspec) {
330 Arg a;
331 WebKitDownloadStatus status = webkit_download_get_status(d);
333 if (status != WEBKIT_DOWNLOAD_STATUS_STARTED && status != WEBKIT_DOWNLOAD_STATUS_CREATED) {
334 if (status != WEBKIT_DOWNLOAD_STATUS_FINISHED) {
335 a.i = Error;
336 a.s = g_strdup_printf("Error while downloading %s", webkit_download_get_suggested_filename(d));
337 echo(&a);
338 } else {
339 a.i = Info;
340 a.s = g_strdup_printf("Download %s finished", webkit_download_get_suggested_filename(d));
341 echo(&a);
343 g_free(a.s);
344 activeDownloads = g_list_remove(activeDownloads, d);
346 update_state();
350 gboolean
351 process_keypress(GdkEventKey *event) {
352 KeyList *current;
353 guint keyval;
354 GdkModifierType irrelevant;
356 /* Get a mask of modifiers that shouldn't be considered for this event.
357 * E.g.: It shouldn't matter whether ';' is shifted or not. */
358 gdk_keymap_translate_keyboard_state(keymap, event->hardware_keycode,
359 event->state, event->group, &keyval, NULL, NULL, &irrelevant);
361 current = keylistroot;
363 while (current != NULL) {
364 if (current->Element.mask == (CLEAN(event->state) & ~irrelevant)
365 && (current->Element.modkey == current_modkey
366 || (!current->Element.modkey && !current_modkey)
367 || current->Element.modkey == GDK_VoidSymbol ) /* wildcard */
368 && current->Element.key == keyval
369 && current->Element.func)
370 if (current->Element.func(&current->Element.arg)) {
371 current_modkey = count = 0;
372 update_state();
373 return TRUE;
375 current = current->next;
377 return FALSE;
380 gboolean
381 webview_keypress_cb(WebKitWebView *webview, GdkEventKey *event) {
382 Arg a = { .i = ModeNormal, .s = NULL };
383 guint keyval;
384 GdkModifierType irrelevant;
386 /* Get a mask of modifiers that shouldn't be considered for this event.
387 * E.g.: It shouldn't matter whether ';' is shifted or not. */
388 gdk_keymap_translate_keyboard_state(keymap, event->hardware_keycode,
389 event->state, event->group, &keyval, NULL, NULL, &irrelevant);
391 switch (mode) {
392 case ModeNormal:
393 if ((CLEAN(event->state) & ~irrelevant) == 0) {
394 if (IS_ESCAPE(event)) {
395 a.i = Info;
396 a.s = g_strdup("");
397 echo(&a);
398 g_free(a.s);
399 } else if (current_modkey == 0 && ((event->keyval >= GDK_1 && event->keyval <= GDK_9)
400 || (event->keyval == GDK_0 && count))) {
401 count = (count ? count * 10 : 0) + (event->keyval - GDK_0);
402 update_state();
403 return TRUE;
404 } else if (strchr(modkeys, event->keyval) && current_modkey != event->keyval) {
405 current_modkey = event->keyval;
406 update_state();
407 return TRUE;
410 /* keybindings */
411 if (process_keypress(event) == TRUE) return TRUE;
413 break;
414 case ModeInsert:
415 if (IS_ESCAPE(event)) {
416 a.i = Silent;
417 a.s = g_strdup("hints.clearFocus();");
418 script(&a);
419 g_free(a.s);
420 a.i = ModeNormal;
421 return set(&a);
423 case ModePassThrough:
424 if (IS_ESCAPE(event)) {
425 echo(&a);
426 set(&a);
427 return TRUE;
429 break;
430 case ModeSendKey:
431 echo(&a);
432 set(&a);
433 break;
435 return FALSE;
438 void
439 set_widget_font_and_color(GtkWidget *widget, const char *font_str, const char *bg_color_str,
440 const char *fg_color_str) {
441 GdkColor fg_color;
442 GdkColor bg_color;
443 PangoFontDescription *font;
445 font = pango_font_description_from_string(font_str);
446 gtk_widget_modify_font(widget, font);
447 pango_font_description_free(font);
449 if (fg_color_str)
450 gdk_color_parse(fg_color_str, &fg_color);
451 if (bg_color_str)
452 gdk_color_parse(bg_color_str, &bg_color);
454 gtk_widget_modify_text(widget, GTK_STATE_NORMAL, fg_color_str ? &fg_color : NULL);
455 gtk_widget_modify_base(widget, GTK_STATE_NORMAL, bg_color_str ? &bg_color : NULL);
457 return;
460 void
461 webview_hoverlink_cb(WebKitWebView *webview, char *title, char *link, gpointer data) {
462 const char *uri = webkit_web_view_get_uri(webview);
463 char *markup;
465 memset(rememberedURI, 0, 1024);
466 if (link) {
467 markup = g_markup_printf_escaped("<span font=\"%s\">Link: %s</span>", statusfont, link);
468 gtk_label_set_markup(GTK_LABEL(status_url), markup);
469 strncpy(rememberedURI, link, 1024);
470 g_free(markup);
471 } else
472 update_url(uri);
475 gboolean
476 webview_console_cb(WebKitWebView *webview, char *message, int line, char *source, gpointer user_data) {
477 Arg a;
479 /* Don't change internal mode if the browser doesn't have focus to prevent inconsistent states */
480 if (gtk_window_has_toplevel_focus(window)) {
481 if (!strcmp(message, "hintmode_off") || !strcmp(message, "insertmode_off")) {
482 a.i = ModeNormal;
483 return set(&a);
484 } else if (!strcmp(message, "insertmode_on")) {
485 a.i = ModeInsert;
486 return set(&a);
489 return FALSE;
492 void
493 inputbox_activate_cb(GtkEntry *entry, gpointer user_data) {
494 char *text;
495 guint16 length = gtk_entry_get_text_length(entry);
496 Arg a;
497 gboolean success = FALSE, forward = FALSE;
499 a.i = HideCompletion;
500 complete(&a);
501 if (length == 0)
502 return;
503 text = (char*)gtk_entry_get_text(entry);
504 if (length > 1 && text[0] == ':') {
505 success = process_line((text + 1));
506 } else if (length > 1 && ((forward = text[0] == '/') || text[0] == '?')) {
507 webkit_web_view_unmark_text_matches(webview);
508 #ifdef ENABLE_MATCH_HIGHLITING
509 webkit_web_view_mark_text_matches(webview, &text[1], FALSE, 0);
510 webkit_web_view_set_highlight_text_matches(webview, TRUE);
511 #endif
512 count = 0;
513 #ifndef ENABLE_INCREMENTAL_SEARCH
514 a.s =& text[1];
515 a.i = searchoptions | (forward ? DirectionForward : DirectionBackwards);
516 search(&a);
517 #else
518 search_direction = forward;
519 search_handle = g_strdup(&text[1]);
520 #endif
521 } else if (text[0] == '.' || text[0] == ',' || text[0] == ';') {
522 a.i = Silent;
523 a.s = g_strdup_printf("hints.fire();");
524 script(&a);
525 g_free(a.s);
526 update_state();
527 } else
528 return;
529 if (!echo_active)
530 gtk_entry_set_text(entry, "");
531 gtk_widget_grab_focus(GTK_WIDGET(webview));
534 gboolean
535 inputbox_keypress_cb(GtkEntry *entry, GdkEventKey *event) {
536 Arg a;
537 int numval;
539 if (mode == ModeHints) {
540 if (event->keyval == GDK_Tab) {
541 a.i = Silent;
542 a.s = g_strdup_printf("hints.focusNextHint();");
543 script(&a);
544 g_free(a.s);
545 update_state();
546 return TRUE;
548 if (event->keyval == GDK_ISO_Left_Tab) {
549 a.i = Silent;
550 a.s = g_strdup_printf("hints.focusPreviousHint();");
551 script(&a);
552 g_free(a.s);
553 update_state();
554 return TRUE;
556 if (event->keyval == GDK_Return) {
557 a.i = Silent;
558 a.s = g_strdup_printf("hints.fire();");
559 script(&a);
560 g_free(a.s);
561 update_state();
562 return TRUE;
565 switch (event->keyval) {
566 case GDK_bracketleft:
567 case GDK_Escape:
568 if (!IS_ESCAPE(event)) break;
569 a.i = HideCompletion;
570 complete(&a);
571 a.i = ModeNormal;
572 commandpointer = 0;
573 return set(&a);
574 break;
575 case GDK_Tab:
576 a.i = DirectionNext;
577 return complete(&a);
578 break;
579 case GDK_Up:
580 a.i = DirectionPrev;
581 return commandhistoryfetch(&a);
582 break;
583 case GDK_Down:
584 a.i = DirectionNext;
585 return commandhistoryfetch(&a);
586 break;
587 case GDK_ISO_Left_Tab:
588 a.i = DirectionPrev;
589 return complete(&a);
590 break;
593 if (mode == ModeHints) {
594 if ((CLEAN(event->state) & GDK_SHIFT_MASK) &&
595 (CLEAN(event->state) & GDK_CONTROL_MASK) &&
596 (event->keyval == GDK_BackSpace)) {
597 count /= 10;
598 a.i = Silent;
599 a.s = g_strdup_printf("hints.updateHints(%d);", count);
600 script(&a);
601 g_free(a.s);
602 update_state();
603 return TRUE;
606 numval = g_unichar_digit_value((gunichar) gdk_keyval_to_unicode(event->keyval));
607 if ((numval >= 1 && numval <= 9) || (numval == 0 && count)) {
608 /* allow a zero as non-first number */
609 count = (count ? count * 10 : 0) + numval;
610 a.i = Silent;
611 a.s = g_strdup_printf("hints.updateHints(%d);", count);
612 script(&a);
613 g_free(a.s);
614 update_state();
615 return TRUE;
619 return FALSE;
622 gboolean
623 notify_event_cb(GtkWidget *widget, GdkEvent *event, gpointer user_data) {
624 int i;
625 WebKitHitTestResult *result;
626 WebKitHitTestResultContext context;
627 if (mode == ModeNormal && event->type == GDK_BUTTON_RELEASE) {
628 /* handle mouse click events */
629 for (i = 0; i < LENGTH(mouse); i++) {
630 if (mouse[i].mask == CLEAN(event->button.state)
631 && (mouse[i].modkey == current_modkey
632 || (!mouse[i].modkey && !current_modkey)
633 || mouse[i].modkey == GDK_VoidSymbol) /* wildcard */
634 && mouse[i].button == event->button.button
635 && mouse[i].func) {
636 if (mouse[i].func(&mouse[i].arg)) {
637 current_modkey = count = 0;
638 update_state();
639 return TRUE;
643 result = webkit_web_view_get_hit_test_result(WEBKIT_WEB_VIEW(widget), (GdkEventButton*)event);
644 g_object_get(result, "context", &context, NULL);
645 if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE) {
646 Arg a = { .i = ModeInsert };
647 set(&a);
648 manual_focus = TRUE;
650 } else if (mode == ModeInsert && event->type == GDK_BUTTON_RELEASE) {
651 result = webkit_web_view_get_hit_test_result(WEBKIT_WEB_VIEW(widget), (GdkEventButton*)event);
652 g_object_get(result, "context", &context, NULL);
653 if (!(context & WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE)) {
654 Arg a = { .i = ModeNormal };
655 set(&a);
657 } else {
658 gchar *value = NULL, *message = NULL;
659 jsapi_evaluate_script("window.getSelection().focusNode", &value, &message);
660 if (value && !strcmp(value, "[object HTMLFormElement]")) {
661 Arg a = { .i = ModeInsert, .s = NULL };
662 set(&a);
663 manual_focus = TRUE;
665 g_free(value);
666 g_free(message);
668 return FALSE;
671 static gboolean inputbox_keyrelease_cb(GtkEntry *entry, GdkEventKey *event) {
672 Arg a;
673 guint16 length = gtk_entry_get_text_length(entry);
675 if (!length) {
676 a.i = HideCompletion;
677 complete(&a);
678 a.i = ModeNormal;
679 return set(&a);
681 return FALSE;
684 static gboolean inputbox_changed_cb(GtkEditable *entry, gpointer user_data) {
685 Arg a;
686 char *text = (char*)gtk_entry_get_text(GTK_ENTRY(entry));
687 guint16 length = gtk_entry_get_text_length(GTK_ENTRY(entry));
688 gboolean forward = FALSE;
690 /* Update incremental search if the user changes the search text.
692 * Note: gtk_widget_is_focus() is a poor way to check if the change comes
693 * from the user. But if the entry is focused and the text is set
694 * through gtk_entry_set_text() in some asyncrounous operation,
695 * I would consider that a bug.
698 if (gtk_widget_is_focus(GTK_WIDGET(entry)) && length > 1 && ((forward = text[0] == '/') || text[0] == '?')) {
699 webkit_web_view_unmark_text_matches(webview);
700 webkit_web_view_search_text(webview, &text[1], searchoptions & CaseSensitive, forward, searchoptions & Wrapping);
701 return TRUE;
702 } else if (gtk_widget_is_focus(GTK_WIDGET(entry)) && length >= 1 &&
703 (text[0] == '.' || text[0] == ',' || text[0] == ';')) {
704 a.i = Silent;
705 switch (text[0]) {
706 case '.':
707 a.s = g_strconcat("hints.createHints('", text + 1, "', 'f');", NULL);
708 break;
710 case ',':
711 a.s = g_strconcat("hints.createHints('", text + 1, "', 'F');", NULL);
712 break;
714 case ';':
715 a.s = NULL;
716 switch (text[1]) {
717 case 's':
718 a.s = g_strconcat("hints.createHints('", text + 2, "', 's');", NULL);
719 break;
720 case 'y':
721 a.s = g_strconcat("hints.createHints('", text + 2, "', 'y');", NULL);
722 break;
723 case 'o':
724 a.s = g_strconcat("hints.createHints('", text + 2, "', 'f');", NULL);
725 break;
726 case 't': case 'w':
727 a.s = g_strconcat("hints.createHints('", text + 2, "', 'F');", NULL);
728 break;
729 case 'O': case 'T': case 'W':
730 a.s = g_strconcat("hints.createHints('", text + 2, "', 'O');", NULL);
731 break;
732 case 'i':
733 a.s = g_strconcat("hints.createHints('", text + 2, "', 'i');", NULL);
734 break;
735 case 'I':
736 a.s = g_strconcat("hints.createHints('", text + 2, "', 'I');", NULL);
737 break;
739 break;
741 count = 0;
742 if (a.s) {
743 script(&a);
744 g_free(a.s);
747 return TRUE;
748 } else if (length == 0 && followTarget[0]) {
749 mode = ModeNormal;
750 a.i = Silent;
751 a.s = g_strdup("hints.clearHints();");
752 script(&a);
753 g_free(a.s);
754 count = 0;
755 update_state();
758 return FALSE;
761 /* funcs here */
763 void fill_suggline(char * suggline, const char * command, const char *fill_with) {
764 memset(suggline, 0, 512);
765 strncpy(suggline, command, 512);
766 strncat(suggline, " ", 1);
767 strncat(suggline, fill_with, 512 - strlen(suggline) - 1);
770 GtkWidget * fill_eventbox(const char * completion_line) {
771 GtkBox * row;
772 GtkWidget *row_eventbox, *el;
773 GdkColor color;
774 char *markup, *markup_tmp;
776 row = GTK_BOX(gtk_hbox_new(FALSE, 0));
777 row_eventbox = gtk_event_box_new();
778 gdk_color_parse(completionbgcolor[0], &color);
779 gtk_widget_modify_bg(row_eventbox, GTK_STATE_NORMAL, &color);
780 el = gtk_label_new(NULL);
781 markup_tmp = g_markup_escape_text(completion_line, strlen(completion_line));
782 markup = g_strconcat("<span font=\"", completionfont[0], "\" color=\"", completioncolor[0], "\">",
783 markup_tmp, "</span>", NULL);
784 gtk_label_set_markup(GTK_LABEL(el), markup);
785 g_free(markup_tmp);
786 g_free(markup);
787 gtk_misc_set_alignment(GTK_MISC(el), 0, 0);
788 gtk_box_pack_start(row, el, TRUE, TRUE, 2);
789 gtk_container_add(GTK_CONTAINER(row_eventbox), GTK_WIDGET(row));
790 return row_eventbox;
793 gboolean
794 complete(const Arg *arg) {
795 char *str, *p, *s, *markup, *entry, *searchfor, command[32] = "", suggline[512] = "", **suggurls;
796 size_t listlen, len, cmdlen;
797 int i, spacepos;
798 Listelement *elementlist = NULL, *elementpointer;
799 gboolean highlight = FALSE;
800 GtkBox *row;
801 GtkWidget *row_eventbox, *el;
802 GtkBox *_table;
803 GdkColor color;
804 static GtkWidget *table, *top_border;
805 static char *prefix;
806 static char **suggestions;
807 static GtkWidget **widgets;
808 static int n = 0, m, current = -1;
810 str = (char*)gtk_entry_get_text(GTK_ENTRY(inputbox));
811 len = strlen(str);
813 /* Get the length of the list of commands for completion. We need this to
814 * malloc/realloc correctly.
816 listlen = LENGTH(commands);
818 if ((len == 0 || str[0] != ':') && arg->i != HideCompletion)
819 return TRUE;
820 if (prefix) {
821 if (arg->i != HideCompletion && widgets && current != -1 && !strcmp(&str[1], suggestions[current])) {
822 gdk_color_parse(completionbgcolor[0], &color);
823 gtk_widget_modify_bg(widgets[current], GTK_STATE_NORMAL, &color);
824 current = (n + current + (arg->i == DirectionPrev ? -1 : 1)) % n;
825 if ((arg->i == DirectionNext && current == 0)
826 || (arg->i == DirectionPrev && current == n - 1))
827 current = -1;
828 } else {
829 free(widgets);
830 free(suggestions);
831 free(prefix);
832 gtk_widget_destroy(GTK_WIDGET(table));
833 gtk_widget_destroy(GTK_WIDGET(top_border));
834 table = NULL;
835 widgets = NULL;
836 suggestions = NULL;
837 prefix = NULL;
838 n = 0;
839 current = -1;
840 if (arg->i == HideCompletion)
841 return TRUE;
843 } else if (arg->i == HideCompletion)
844 return TRUE;
845 if (!widgets) {
846 prefix = g_strdup(str);
847 widgets = malloc(sizeof(GtkWidget*) * listlen);
848 suggestions = malloc(sizeof(char*) * listlen);
849 top_border = gtk_event_box_new();
850 gtk_widget_set_size_request(GTK_WIDGET(top_border), 0, 1);
851 gdk_color_parse(completioncolor[2], &color);
852 gtk_widget_modify_bg(top_border, GTK_STATE_NORMAL, &color);
853 table = gtk_event_box_new();
854 gdk_color_parse(completionbgcolor[0], &color);
855 _table = GTK_BOX(gtk_vbox_new(FALSE, 0));
856 highlight = len > 1;
857 if (strchr(str, ' ') == NULL) {
858 /* command completion */
859 listlen = LENGTH(commands);
860 for (i = 0; i < listlen; i++) {
861 if (commands[i].cmd == NULL)
862 break;
863 cmdlen = strlen(commands[i].cmd);
864 if (!highlight || (n < MAX_LIST_SIZE && len - 1 <= cmdlen && !strncmp(&str[1], commands[i].cmd, len - 1))) {
865 p = s = malloc(sizeof(char*) * (highlight ? sizeof(COMPLETION_TAG_OPEN) + sizeof(COMPLETION_TAG_CLOSE) - 1 : 1) + cmdlen);
866 if (highlight) {
867 memcpy(p, COMPLETION_TAG_OPEN, sizeof(COMPLETION_TAG_OPEN) - 1);
868 memcpy((p += sizeof(COMPLETION_TAG_OPEN) - 1), &str[1], len - 1);
869 memcpy((p += len - 1), COMPLETION_TAG_CLOSE, sizeof(COMPLETION_TAG_CLOSE) - 1);
870 p += sizeof(COMPLETION_TAG_CLOSE) - 1;
872 memcpy(p, &commands[i].cmd[len - 1], cmdlen - len + 2);
873 row = GTK_BOX(gtk_hbox_new(FALSE, 0));
874 row_eventbox = gtk_event_box_new();
875 gtk_widget_modify_bg(row_eventbox, GTK_STATE_NORMAL, &color);
876 el = gtk_label_new(NULL);
877 markup = g_strconcat("<span font=\"", completionfont[0], "\" color=\"", completioncolor[0], "\">", s, "</span>", NULL);
878 free(s);
879 gtk_label_set_markup(GTK_LABEL(el), markup);
880 g_free(markup);
881 gtk_misc_set_alignment(GTK_MISC(el), 0, 0);
882 gtk_box_pack_start(row, el, TRUE, TRUE, 2);
883 gtk_container_add(GTK_CONTAINER(row_eventbox), GTK_WIDGET(row));
884 gtk_box_pack_start(_table, GTK_WIDGET(row_eventbox), FALSE, FALSE, 0);
885 suggestions[n] = commands[i].cmd;
886 widgets[n++] = row_eventbox;
889 } else {
890 entry = (char *)malloc(512 * sizeof(char));
891 if (entry == NULL) {
892 return FALSE;
894 memset(entry, 0, 512);
895 suggurls = malloc(sizeof(char*) * listlen);
896 if (suggurls == NULL) {
897 return FALSE;
899 spacepos = strcspn(str, " ");
900 searchfor = (str + spacepos + 1);
901 strncpy(command, (str + 1), spacepos - 1);
902 if (strlen(command) == 3 && strncmp(command, "set", 3) == 0) {
903 /* browser settings */
904 listlen = LENGTH(browsersettings);
905 for (i = 0; i < listlen; i++) {
906 if (n < MAX_LIST_SIZE && strstr(browsersettings[i].name, searchfor) != NULL) {
907 /* match */
908 fill_suggline(suggline, command, browsersettings[i].name);
909 /* FIXME(HP): This memory is never freed */
910 suggurls[n] = (char *)malloc(sizeof(char) * 512 + 1);
911 strncpy(suggurls[n], suggline, 512);
912 suggestions[n] = suggurls[n];
913 row_eventbox = fill_eventbox(suggline);
914 gtk_box_pack_start(_table, GTK_WIDGET(row_eventbox), FALSE, FALSE, 0);
915 widgets[n++] = row_eventbox;
919 } else if (strlen(command) == 2 && strncmp(command, "qt", 2) == 0) {
920 /* completion on tags */
921 spacepos = strcspn(str, " ");
922 searchfor = (str + spacepos + 1);
923 elementlist = complete_list(searchfor, 1, elementlist);
924 } else {
925 /* URL completion: bookmarks */
926 elementlist = complete_list(searchfor, 0, elementlist);
927 m = count_list(elementlist);
928 if (m < MAX_LIST_SIZE) {
929 /* URL completion: history */
930 elementlist = complete_list(searchfor, 2, elementlist);
933 elementpointer = elementlist;
934 while (elementpointer != NULL) {
935 fill_suggline(suggline, command, elementpointer->element);
936 /* FIXME(HP): This memory is never freed */
937 suggurls[n] = (char *)malloc(sizeof(char) * 512 + 1);
938 strncpy(suggurls[n], suggline, 512);
939 suggestions[n] = suggurls[n];
940 row_eventbox = fill_eventbox(suggline);
941 gtk_box_pack_start(_table, GTK_WIDGET(row_eventbox), FALSE, FALSE, 0);
942 widgets[n++] = row_eventbox;
943 elementpointer = elementpointer->next;
944 if (n >= MAX_LIST_SIZE)
945 break;
947 free_list(elementlist);
948 if (suggurls != NULL) {
949 free(suggurls);
950 suggurls = NULL;
952 if (entry != NULL) {
953 free(entry);
954 entry = NULL;
957 /* TA: FIXME - this needs rethinking entirely. */
959 GtkWidget **widgets_temp = realloc(widgets, sizeof(*widgets) * n);
960 if (widgets_temp == NULL && widgets == NULL) {
961 fprintf(stderr, "Couldn't realloc() widgets\n");
962 exit(1);
964 widgets = widgets_temp;
965 char **suggestions_temp = realloc(suggestions, sizeof(*suggestions) * n);
966 if (suggestions_temp == NULL && suggestions == NULL) {
967 fprintf(stderr, "Couldn't realloc() suggestions\n");
968 exit(1);
970 suggestions = suggestions_temp;
972 if (!n) {
973 gdk_color_parse(completionbgcolor[1], &color);
974 gtk_widget_modify_bg(table, GTK_STATE_NORMAL, &color);
975 el = gtk_label_new(NULL);
976 gtk_misc_set_alignment(GTK_MISC(el), 0, 0);
977 markup = g_strconcat("<span font=\"", completionfont[1], "\" color=\"", completioncolor[1], "\">No Completions</span>", NULL);
978 gtk_label_set_markup(GTK_LABEL(el), markup);
979 g_free(markup);
980 gtk_box_pack_start(_table, GTK_WIDGET(el), FALSE, FALSE, 0);
982 gtk_box_pack_start(box, GTK_WIDGET(top_border), FALSE, FALSE, 0);
983 gtk_container_add(GTK_CONTAINER(table), GTK_WIDGET(_table));
984 gtk_box_pack_start(box, GTK_WIDGET(table), FALSE, FALSE, 0);
985 gtk_widget_show_all(GTK_WIDGET(window));
986 if (!n)
987 return TRUE;
988 current = arg->i == DirectionPrev ? n - 1 : 0;
990 if (current != -1) {
991 gdk_color_parse(completionbgcolor[2], &color);
992 gtk_widget_modify_bg(GTK_WIDGET(widgets[current]), GTK_STATE_NORMAL, &color);
993 s = g_strconcat(":", suggestions[current], NULL);
994 gtk_entry_set_text(GTK_ENTRY(inputbox), s);
995 g_free(s);
996 } else
997 gtk_entry_set_text(GTK_ENTRY(inputbox), prefix);
998 gtk_editable_set_position(GTK_EDITABLE(inputbox), -1);
999 return TRUE;
1002 gboolean
1003 descend(const Arg *arg) {
1004 char *source = (char*)webkit_web_view_get_uri(webview), *p = &source[0], *new;
1005 int i, len;
1006 count = count ? count : 1;
1008 if (!source)
1009 return TRUE;
1010 if (arg->i == Rootdir) {
1011 for (i = 0; i < 3; i++) /* get to the third slash */
1012 if (!(p = strchr(++p, '/')))
1013 return TRUE; /* if we cannot find it quit */
1014 } else {
1015 len = strlen(source);
1016 if (!len) /* if string is empty quit */
1017 return TRUE;
1018 p = source + len; /* start at the end */
1019 if (*(p - 1) == '/') /* /\/$/ is not an additional level */
1020 ++count;
1021 for (i = 0; i < count; i++)
1022 while(*(p--) != '/' || *p == '/') /* count /\/+/ as one slash */
1023 if (p == source) /* if we reach the first char pointer quit */
1024 return TRUE;
1025 ++p; /* since we do p-- in the while, we are pointing at
1026 the char before the slash, so +1 */
1028 len = p - source + 1; /* new length = end - start + 1 */
1029 new = malloc(len + 1);
1030 memcpy(new, source, len);
1031 new[len] = '\0';
1032 webkit_web_view_load_uri(webview, new);
1033 free(new);
1034 return TRUE;
1037 gboolean
1038 echo(const Arg *arg) {
1039 int index = !arg->s ? 0 : arg->i & (~NoAutoHide);
1041 if (index < Info || index > Error)
1042 return TRUE;
1044 set_widget_font_and_color(inputbox, urlboxfont[index], urlboxbgcolor[index], urlboxcolor[index]);
1045 gtk_entry_set_text(GTK_ENTRY(inputbox), !arg->s ? "" : arg->s);
1047 return TRUE;
1050 gboolean
1051 input(const Arg *arg) {
1052 int pos = 0;
1053 count = 0;
1054 const char *url;
1055 int index = Info;
1056 Arg a;
1058 /* if inputbox hidden, show it again */
1059 if (!gtk_widget_get_visible(inputbox))
1060 gtk_widget_set_visible(inputbox, TRUE);
1062 update_state();
1064 /* Set the colour and font back to the default, so that we don't still
1065 * maintain a red colour from a warning from an end of search indicator,
1066 * etc.
1068 set_widget_font_and_color(inputbox, urlboxfont[index], urlboxbgcolor[index], urlboxcolor[index]);
1070 /* to avoid things like :open URL :open URL2 or :open :open URL */
1071 gtk_entry_set_text(GTK_ENTRY(inputbox), "");
1072 gtk_editable_insert_text(GTK_EDITABLE(inputbox), arg->s, -1, &pos);
1073 if (arg->i & InsertCurrentURL && (url = webkit_web_view_get_uri(webview)))
1074 gtk_editable_insert_text(GTK_EDITABLE(inputbox), url, -1, &pos);
1076 gtk_widget_grab_focus(inputbox);
1077 gtk_editable_set_position(GTK_EDITABLE(inputbox), -1);
1079 if (arg->s[0] == '.' || arg->s[0] == ',' || arg->s[0] == ';') {
1080 mode = ModeHints;
1081 memset(followTarget, 0, 8);
1082 strncpy(followTarget, "current", 8);
1083 a.i = Silent;
1084 switch (arg->s[0]) {
1085 case '.':
1086 a.s = g_strdup("hints.createHints('', 'f');");
1087 break;
1089 case ',':
1090 a.s = g_strdup("hints.createHints('', 'F');");
1091 break;
1093 case ';':
1094 a.s = NULL;
1095 if (arg->s[1]) {
1096 switch (arg->s[1]) {
1097 case 's':
1098 a.s = g_strdup("hints.createHints('', 's');");
1099 break;
1100 case 'y':
1101 a.s = g_strdup("hints.createHints('', 'y');");
1102 break;
1103 case 'o':
1104 a.s = g_strdup("hints.createHints('', 'f');");
1105 break;
1106 case 't': case 'w':
1107 a.s = g_strdup("hints.createHints('', 'F');");
1108 break;
1109 case 'O': case 'T': case 'W':
1110 a.s = g_strdup("hints.createHints('', 'O');");
1111 break;
1112 case 'i':
1113 a.s = g_strdup("hints.createHints('', 'i');");
1114 break;
1115 case 'I':
1116 a.s = g_strdup("hints.createHints('', 'I');");
1117 break;
1120 break;
1122 count = 0;
1123 if (a.s) {
1124 script(&a);
1125 g_free(a.s);
1129 return TRUE;
1132 gboolean
1133 navigate(const Arg *arg) {
1134 if (arg->i & NavigationForwardBack)
1135 webkit_web_view_go_back_or_forward(webview, (arg->i == NavigationBack ? -1 : 1) * (count ? count : 1));
1136 else if (arg->i & NavigationReloadActions)
1137 (arg->i == NavigationReload ? webkit_web_view_reload : webkit_web_view_reload_bypass_cache)(webview);
1138 else
1139 webkit_web_view_stop_loading(webview);
1140 return TRUE;
1143 gboolean
1144 number(const Arg *arg) {
1145 const char *source = webkit_web_view_get_uri(webview);
1146 char *uri, *p, *new;
1147 int number, diff = (count ? count : 1) * (arg->i == Increment ? 1 : -1);
1149 if (!source)
1150 return TRUE;
1151 uri = g_strdup(source); /* copy string */
1152 p =& uri[0];
1153 while(*p != '\0') /* goto the end of the string */
1154 ++p;
1155 --p;
1156 while(*p >= '0' && *p <= '9') /* go back until non number char is reached */
1157 --p;
1158 if (*(++p) == '\0') { /* if no numbers were found abort */
1159 free(uri);
1160 return TRUE;
1162 number = atoi(p) + diff; /* apply diff on number */
1163 *p = '\0';
1164 new = g_strdup_printf("%s%d", uri, number); /* create new uri */
1165 webkit_web_view_load_uri(webview, new);
1166 g_free(new);
1167 free(uri);
1168 return TRUE;
1171 gboolean
1172 open_arg(const Arg *arg) {
1173 char *argv[64];
1174 char *s = arg->s, *p = NULL, *new;
1175 Arg a = { .i = NavigationReload };
1176 int len;
1177 char *search_uri, *search_term;
1179 if (embed) {
1180 argv[0] = *args;
1181 argv[1] = "-e";
1182 argv[2] = winid;
1183 argv[3] = arg->s;
1184 argv[4] = NULL;
1185 } else {
1186 argv[0] = *args;
1187 argv[1] = arg->s;
1188 argv[2] = NULL;
1191 if (!arg->s)
1192 navigate(&a);
1193 else if (arg->i == TargetCurrent) {
1194 while(*s == ' ') /* strip leading whitespace */
1195 ++s;
1196 p = (s + strlen(s) - 1);
1197 while(*p == ' ') /* strip trailing whitespace */
1198 --p;
1199 *(p + 1) = '\0';
1200 len = strlen(s);
1201 new = NULL;
1202 /* check for external handlers */
1203 if (open_handler(s))
1204 return TRUE;
1205 /* check for search engines */
1206 p = strchr(s, ' ');
1207 if (p) { /* check for search engines */
1208 *p = '\0';
1209 search_uri = find_uri_for_searchengine(s);
1210 if (search_uri != NULL) {
1211 search_term = soup_uri_encode(p+1, "&");
1212 new = g_strdup_printf(search_uri, search_term);
1213 g_free(search_term);
1215 *p = ' ';
1217 if (!new) {
1218 if (len > 3 && strstr(s, "://")) { /* valid url? */
1219 p = new = g_malloc(len + 1);
1220 while(*s != '\0') { /* strip whitespaces */
1221 if (*s != ' ')
1222 *(p++) = *s;
1223 ++s;
1225 *p = '\0';
1226 } else if (strcspn(s, "/") == 0 || strcspn(s, "./") == 0) { /* prepend "file://" */
1227 new = g_malloc(sizeof("file://") + len);
1228 strcpy(new, "file://");
1229 memcpy(&new[sizeof("file://") - 1], s, len + 1);
1230 } else if (p || !strchr(s, '.')) { /* whitespaces or no dot? */
1231 search_uri = find_uri_for_searchengine(defaultsearch);
1232 if (search_uri != NULL) {
1233 search_term = soup_uri_encode(s, "&");
1234 new = g_strdup_printf(search_uri, search_term);
1235 g_free(search_term);
1237 } else { /* prepend "http://" */
1238 new = g_malloc(sizeof("http://") + len);
1239 strcpy(new, "http://");
1240 memcpy(&new[sizeof("http://") - 1], s, len + 1);
1243 webkit_web_view_load_uri(webview, new);
1244 g_free(new);
1245 } else
1246 g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL);
1247 return TRUE;
1250 gboolean
1251 open_remembered(const Arg *arg)
1253 Arg a = {arg->i, rememberedURI};
1255 if (strcmp(rememberedURI, "")) {
1256 open_arg(&a);
1258 return TRUE;
1261 gboolean
1262 yank(const Arg *arg) {
1263 const char *url, *feedback, *content;
1265 if (arg->i & SourceSelection) {
1266 webkit_web_view_copy_clipboard(webview);
1267 if (arg->i & ClipboardPrimary)
1268 content = gtk_clipboard_wait_for_text(clipboards[0]);
1269 if (!content && arg->i & ClipboardGTK)
1270 content = gtk_clipboard_wait_for_text(clipboards[1]);
1271 if (content) {
1272 feedback = g_strconcat("Yanked ", content, NULL);
1273 g_free((gpointer *)content);
1274 give_feedback(feedback);
1275 g_free((gpointer *)feedback);
1277 } else {
1278 if (arg->i & SourceURL) {
1279 url = webkit_web_view_get_uri(webview);
1280 } else {
1281 url = arg->s;
1283 if (!url)
1284 return TRUE;
1285 feedback = g_strconcat("Yanked ", url, NULL);
1286 give_feedback(feedback);
1287 if (arg->i & ClipboardPrimary)
1288 gtk_clipboard_set_text(clipboards[0], url, -1);
1289 if (arg->i & ClipboardGTK)
1290 gtk_clipboard_set_text(clipboards[1], url, -1);
1292 return TRUE;
1295 gboolean
1296 paste(const Arg *arg) {
1297 Arg a = { .i = arg->i & TargetNew, .s = NULL };
1299 /* If we're over a link, open it in a new target. */
1300 if (strlen(rememberedURI) > 0) {
1301 Arg new_target = { .i = TargetNew, .s = arg->s };
1302 open_arg(&new_target);
1303 return TRUE;
1306 if (arg->i & ClipboardPrimary)
1307 a.s = gtk_clipboard_wait_for_text(clipboards[0]);
1308 if (!a.s && arg->i & ClipboardGTK)
1309 a.s = gtk_clipboard_wait_for_text(clipboards[1]);
1310 if (a.s) {
1311 open_arg(&a);
1312 g_free(a.s);
1314 return TRUE;
1317 gboolean
1318 quit(const Arg *arg) {
1319 FILE *f;
1320 const char *filename;
1321 const char *uri = webkit_web_view_get_uri(webview);
1322 if (uri != NULL) {
1323 /* write last URL into status file for recreation with "u" */
1324 filename = g_strdup_printf(CLOSED_URL_FILENAME);
1325 f = fopen(filename, "w");
1326 g_free((gpointer *)filename);
1327 if (f != NULL) {
1328 fprintf(f, "%s", uri);
1329 fclose(f);
1332 gtk_main_quit();
1333 return TRUE;
1336 gboolean
1337 revive(const Arg *arg) {
1338 FILE *f;
1339 const char *filename;
1340 char buffer[512] = "";
1341 Arg a = { .i = TargetNew, .s = NULL };
1342 /* get the URL of the window which has been closed last */
1343 filename = g_strdup_printf(CLOSED_URL_FILENAME);
1344 f = fopen(filename, "r");
1345 g_free((gpointer *)filename);
1346 if (f != NULL) {
1347 fgets(buffer, 512, f);
1348 fclose(f);
1350 if (strlen(buffer) > 0) {
1351 a.s = buffer;
1352 open_arg(&a);
1353 return TRUE;
1355 return FALSE;
1358 static
1359 gboolean print_frame(const Arg *arg)
1361 WebKitWebFrame *frame = webkit_web_view_get_main_frame(webview);
1362 webkit_web_frame_print (frame);
1363 return TRUE;
1366 gboolean
1367 search(const Arg *arg) {
1368 count = count ? count : 1;
1369 gboolean success, direction = arg->i & DirectionPrev;
1370 Arg a;
1372 if (arg->s) {
1373 free(search_handle);
1374 search_handle = g_strdup(arg->s);
1376 if (!search_handle)
1377 return TRUE;
1378 if (arg->i & DirectionAbsolute)
1379 search_direction = direction;
1380 else
1381 direction ^= search_direction;
1382 do {
1383 success = webkit_web_view_search_text(webview, search_handle, arg->i & CaseSensitive, direction, FALSE);
1384 if (!success) {
1385 if (arg->i & Wrapping) {
1386 success = webkit_web_view_search_text(webview, search_handle, arg->i & CaseSensitive, direction, TRUE);
1387 if (success) {
1388 a.i = Warning;
1389 a.s = g_strdup_printf("search hit %s, continuing at %s",
1390 direction ? "BOTTOM" : "TOP",
1391 direction ? "TOP" : "BOTTOM");
1392 echo(&a);
1393 g_free(a.s);
1394 } else
1395 break;
1396 } else
1397 break;
1399 } while(--count);
1400 if (!success) {
1401 a.i = Error;
1402 a.s = g_strdup_printf("Pattern not found: %s", search_handle);
1403 echo(&a);
1404 g_free(a.s);
1406 return TRUE;
1409 gboolean
1410 set(const Arg *arg) {
1411 Arg a = { .i = Info | NoAutoHide };
1413 switch (arg->i) {
1414 case ModeNormal:
1415 if (search_handle) {
1416 search_handle = NULL;
1417 webkit_web_view_unmark_text_matches(webview);
1419 gtk_entry_set_text(GTK_ENTRY(inputbox), "");
1420 gtk_widget_grab_focus(GTK_WIDGET(webview));
1421 break;
1422 case ModePassThrough:
1423 a.s = g_strdup("-- PASS THROUGH --");
1424 echo(&a);
1425 g_free(a.s);
1426 break;
1427 case ModeSendKey:
1428 a.s = g_strdup("-- PASS TROUGH (next) --");
1429 echo(&a);
1430 g_free(a.s);
1431 break;
1432 case ModeInsert: /* should not be called manually but automatically */
1433 a.s = g_strdup("-- INSERT --");
1434 echo(&a);
1435 g_free(a.s);
1436 break;
1437 default:
1438 return TRUE;
1440 mode = arg->i;
1441 return TRUE;
1444 gchar*
1445 jsapi_ref_to_string(JSContextRef context, JSValueRef ref) {
1446 JSStringRef string_ref;
1447 gchar *string;
1448 size_t length;
1450 string_ref = JSValueToStringCopy(context, ref, NULL);
1451 length = JSStringGetMaximumUTF8CStringSize(string_ref);
1452 string = g_new(gchar, length);
1453 JSStringGetUTF8CString(string_ref, string, length);
1454 JSStringRelease(string_ref);
1455 return string;
1458 void
1459 jsapi_evaluate_script(const gchar *script, gchar **value, gchar **message) {
1460 WebKitWebFrame *frame = webkit_web_view_get_main_frame(webview);
1461 JSGlobalContextRef context = webkit_web_frame_get_global_context(frame);
1462 JSStringRef str;
1463 JSValueRef val, exception;
1465 str = JSStringCreateWithUTF8CString(script);
1466 val = JSEvaluateScript(context, str, JSContextGetGlobalObject(context), NULL, 0, &exception);
1467 JSStringRelease(str);
1468 if (!val)
1469 *message = jsapi_ref_to_string(context, exception);
1470 else
1471 *value = jsapi_ref_to_string(context, val);
1474 gboolean
1475 quickmark(const Arg *a) {
1476 int i, b;
1477 b = atoi(a->s);
1478 char *fn = g_strdup_printf(QUICKMARK_FILE);
1479 FILE *fp;
1480 fp = fopen(fn, "r");
1481 g_free(fn);
1482 fn = NULL;
1483 char buf[100];
1485 if (fp != NULL && b < 10) {
1486 for( i=0; i < b; ++i ) {
1487 if (feof(fp)) {
1488 break;
1490 fgets(buf, 100, fp);
1492 char *ptr = strrchr(buf, '\n');
1493 *ptr = '\0';
1494 Arg x = { .s = buf };
1495 if (strlen(buf))
1496 return open_arg(&x);
1497 else {
1498 x.i = Error;
1499 x.s = g_strdup_printf("Quickmark %d not defined", b);
1500 echo(&x);
1501 g_free(x.s);
1502 return false;
1504 } else { return false; }
1507 gboolean
1508 script(const Arg *arg) {
1509 gchar *value = NULL, *message = NULL;
1510 char text[1024] = "";
1511 Arg a;
1512 WebKitNetworkRequest *request;
1513 WebKitDownload *download;
1515 if (!arg->s) {
1516 set_error("Missing argument.");
1517 return FALSE;
1519 jsapi_evaluate_script(arg->s, &value, &message);
1520 if (message) {
1521 set_error(message);
1522 g_free(message);
1523 return FALSE;
1525 g_free(message);
1526 if (arg->i != Silent && value) {
1527 a.i = arg->i;
1528 a.s = g_strdup(value);
1529 echo(&a);
1530 g_free(a.s);
1532 /* switch mode according to scripts return value */
1533 if (value) {
1534 if (strncmp(value, "done;", 5) == 0) {
1535 a.i = ModeNormal;
1536 set(&a);
1537 } else if (strncmp(value, "insert;", 7) == 0) {
1538 a.i = ModeInsert;
1539 set(&a);
1540 manual_focus = TRUE;
1541 } else if (strncmp(value, "save;", 5) == 0) {
1542 /* forced download */
1543 a.i = ModeNormal;
1544 set(&a);
1545 request = webkit_network_request_new((value + 5));
1546 download = webkit_download_new(request);
1547 webview_download_cb(webview, download, (gpointer *)NULL);
1548 } else if (strncmp(value, "yank;", 5) == 0) {
1549 /* yank link URL to clipboard */
1550 a.i = ModeNormal;
1551 set(&a);
1552 a.i = ClipboardPrimary | ClipboardGTK;
1553 a.s = (value + 5);
1554 yank(&a);
1555 } else if (strncmp(value, "colon;", 6) == 0) {
1556 /* use link URL for colon command */
1557 strncpy(text, (char *)gtk_entry_get_text(GTK_ENTRY(inputbox)), 1023);
1558 a.i = ModeNormal;
1559 set(&a);
1560 switch (text[1]) {
1561 case 'O':
1562 a.s = g_strconcat(":open ", (value + 6), NULL);
1563 break;
1564 case 'T': case 'W':
1565 a.s = g_strconcat(":tabopen ", (value + 6), NULL);
1566 break;
1568 if (a.s) {
1569 input(&a);
1570 g_free(a.s);
1572 } else if (strncmp(value, "error;", 6) == 0) {
1573 a.i = Error;
1574 set(&a);
1577 g_free(value);
1578 return TRUE;
1581 gboolean
1582 scroll(const Arg *arg) {
1583 GtkAdjustment *adjust = (arg->i & OrientationHoriz) ? adjust_h : adjust_v;
1584 int max = gtk_adjustment_get_upper(adjust) - gtk_adjustment_get_page_size(adjust);
1585 float val = gtk_adjustment_get_value(adjust) / max * 100;
1586 int direction = (arg->i & (1 << 2)) ? 1 : -1;
1588 if ((direction == 1 && val < 100) || (direction == -1 && val > 0)) {
1589 if (arg->i & ScrollMove)
1590 gtk_adjustment_set_value(adjust, gtk_adjustment_get_value(adjust) +
1591 direction * /* direction */
1592 ((arg->i & UnitLine || (arg->i & UnitBuffer && count)) ? (scrollstep * (count ? count : 1)) : (
1593 arg->i & UnitBuffer ? gtk_adjustment_get_page_size(adjust) / 2 :
1594 (count ? count : 1) * (gtk_adjustment_get_page_size(adjust) -
1595 (gtk_adjustment_get_page_size(adjust) > pagingkeep ? pagingkeep : 0)))));
1596 else
1597 gtk_adjustment_set_value(adjust,
1598 ((direction == 1) ? gtk_adjustment_get_upper : gtk_adjustment_get_lower)(adjust));
1599 update_state();
1601 return TRUE;
1604 gboolean
1605 zoom(const Arg *arg) {
1606 webkit_web_view_set_full_content_zoom(webview, (arg->i & ZoomFullContent) > 0);
1607 webkit_web_view_set_zoom_level(webview, (arg->i & ZoomOut) ?
1608 webkit_web_view_get_zoom_level(webview) +
1609 (((float)(count ? count : 1)) * (arg->i & (1 << 1) ? 1.0 : -1.0) * zoomstep) :
1610 (count ? (float)count / 100.0 : 1.0));
1611 return TRUE;
1614 gboolean
1615 fake_key_event(const Arg *a) {
1616 if(!embed) {
1617 return FALSE;
1619 Arg err;
1620 err.i = Error;
1621 Display *xdpy;
1622 if ( (xdpy = XOpenDisplay(NULL)) == NULL ) {
1623 err.s = g_strdup("Couldn't find the XDisplay.");
1624 echo(&err);
1625 g_free(err.s);
1626 return FALSE;
1629 XKeyEvent xk;
1630 xk.display = xdpy;
1631 xk.subwindow = None;
1632 xk.time = CurrentTime;
1633 xk.same_screen = True;
1634 xk.x = xk.y = xk.x_root = xk.y_root = 1;
1635 xk.window = embed;
1636 xk.state = a->i;
1638 if( ! a->s ) {
1639 err.s = g_strdup("Zero pointer as argument! Check your config.h");
1640 echo(&err);
1641 g_free(err.s);
1642 return FALSE;
1645 KeySym keysym;
1646 if( (keysym = XStringToKeysym(a->s)) == NoSymbol ) {
1647 err.s = g_strdup_printf("Couldn't translate %s to keysym", a->s );
1648 echo(&err);
1649 g_free(err.s);
1650 return FALSE;
1653 if( (xk.keycode = XKeysymToKeycode(xdpy, keysym)) == NoSymbol ) {
1654 err.s = g_strdup("Couldn't translate keysym to keycode");
1655 echo(&err);
1656 g_free(err.s);
1657 return FALSE;
1660 xk.type = KeyPress;
1661 if( !XSendEvent(xdpy, embed, True, KeyPressMask, (XEvent *)&xk) ) {
1662 err.s = g_strdup("XSendEvent failed");
1663 echo(&err);
1664 g_free(err.s);
1665 return FALSE;
1667 XFlush(xdpy);
1669 return TRUE;
1673 gboolean
1674 commandhistoryfetch(const Arg *arg) {
1675 const int length = g_list_length(commandhistory);
1677 if (length > 0) {
1678 if (arg->i == DirectionPrev) {
1679 commandpointer = (length + commandpointer - 1) % length;
1680 } else {
1681 commandpointer = (length + commandpointer + 1) % length;
1684 const char* command = (char *)g_list_nth_data(commandhistory, commandpointer);
1685 gtk_entry_set_text(GTK_ENTRY(inputbox), g_strconcat(":", command, NULL));
1686 gtk_editable_set_position(GTK_EDITABLE(inputbox), -1);
1687 return TRUE;
1690 return FALSE;
1693 gboolean
1694 bookmark(const Arg *arg) {
1695 FILE *f;
1696 const char *filename;
1697 const char *uri = webkit_web_view_get_uri(webview);
1698 const char *title = webkit_web_view_get_title(webview);
1699 filename = g_strdup_printf(BOOKMARKS_STORAGE_FILENAME);
1700 f = fopen(filename, "a");
1701 g_free((gpointer *)filename);
1702 if (uri == NULL || strlen(uri) == 0) {
1703 set_error("No URI found to bookmark.");
1704 return FALSE;
1706 if (f != NULL) {
1707 fprintf(f, "%s", uri);
1708 if (title != NULL) {
1709 fprintf(f, "%s", " ");
1710 fprintf(f, "%s", title);
1712 if (arg->s && strlen(arg->s)) {
1713 build_taglist(arg, f);
1715 fprintf(f, "%s", "\n");
1716 fclose(f);
1717 give_feedback( "Bookmark saved" );
1718 return TRUE;
1719 } else {
1720 set_error("Bookmarks file not found.");
1721 return FALSE;
1725 gboolean
1726 history() {
1727 FILE *f;
1728 const char *filename;
1729 const char *uri = webkit_web_view_get_uri(webview);
1730 const char *title = webkit_web_view_get_title(webview);
1731 char *entry, buffer[512], *new;
1732 int n, i = 0;
1733 gboolean finished = FALSE;
1734 if (uri != NULL) {
1735 if (title != NULL) {
1736 entry = malloc((strlen(uri) + strlen(title) + 2) * sizeof(char));
1737 memset(entry, 0, strlen(uri) + strlen(title) + 2);
1738 } else {
1739 entry = malloc((strlen(uri) + 1) * sizeof(char));
1740 memset(entry, 0, strlen(uri) + 1);
1742 if (entry != NULL) {
1743 strncpy(entry, uri, strlen(uri));
1744 if (title != NULL) {
1745 strncat(entry, " ", 1);
1746 strncat(entry, title, strlen(title));
1748 n = strlen(entry);
1749 filename = g_strdup_printf(HISTORY_STORAGE_FILENAME);
1750 f = fopen(filename, "r");
1751 if (f != NULL) {
1752 new = (char *)malloc(HISTORY_MAX_ENTRIES * 512 * sizeof(char) + 1);
1753 if (new != NULL) {
1754 memset(new, 0, HISTORY_MAX_ENTRIES * 512 * sizeof(char) + 1);
1755 /* newest entries go on top */
1756 strncpy(new, entry, strlen(entry));
1757 strncat(new, "\n", 1);
1758 /* retain at most HISTORY_MAX_ENTIRES - 1 old entries */
1759 while (finished != TRUE) {
1760 if ((char *)NULL == fgets(buffer, 512, f)) {
1761 /* check if end of file was reached / error occured */
1762 if (!feof(f)) {
1763 break;
1765 /* end of file reached */
1766 finished = TRUE;
1767 continue;
1769 /* compare line (-1 because of newline character) */
1770 if (n != strlen(buffer) - 1 || strncmp(entry, buffer, n) != 0) {
1771 /* if the URI is already in history; we put it on top and skip it here */
1772 strncat(new, buffer, 512);
1773 i++;
1775 if ((i + 1) >= HISTORY_MAX_ENTRIES) {
1776 break;
1779 fclose(f);
1781 f = fopen(filename, "w");
1782 g_free((gpointer *)filename);
1783 if (f != NULL) {
1784 fprintf(f, "%s", new);
1785 fclose(f);
1787 if (new != NULL) {
1788 free(new);
1789 new = NULL;
1793 if (entry != NULL) {
1794 free(entry);
1795 entry = NULL;
1798 return TRUE;
1801 static gboolean
1802 view_source(const Arg * arg) {
1803 gboolean current_mode = webkit_web_view_get_view_source_mode(webview);
1804 webkit_web_view_set_view_source_mode(webview, !current_mode);
1805 webkit_web_view_reload(webview);
1806 return TRUE;
1809 static gboolean
1810 focus_input(const Arg *arg) {
1811 static Arg a;
1813 a.s = g_strdup("hints.focusInput();");
1814 a.i = Silent;
1815 script(&a);
1816 g_free(a.s);
1817 update_state();
1818 manual_focus = TRUE;
1819 return TRUE;
1822 static gboolean
1823 browser_settings(const Arg *arg) {
1824 char line[255];
1825 if (!arg->s) {
1826 set_error("Missing argument.");
1827 return FALSE;
1829 strncpy(line, arg->s, 254);
1830 if (process_set_line(line))
1831 return TRUE;
1832 else {
1833 set_error("Invalid setting.");
1834 return FALSE;
1838 char *
1839 search_word(int whichword) {
1840 int k = 0;
1841 static char word[240];
1842 char *c = my_pair.line;
1844 while (isspace(*c) && *c)
1845 c++;
1847 switch (whichword) {
1848 case 0:
1849 while (*c && !isspace (*c) && *c != '=' && k < 240) {
1850 word[k++] = *c;
1851 c++;
1853 word[k] = '\0';
1854 strncpy(my_pair.what, word, 20);
1855 break;
1856 case 1:
1857 while (*c && k < 240) {
1858 word[k++] = *c;
1859 c++;
1861 word[k] = '\0';
1862 strncpy(my_pair.value, word, 240);
1863 break;
1866 return c;
1869 static gboolean
1870 process_set_line(char *line) {
1871 char *c;
1872 int listlen, i;
1873 gboolean boolval;
1874 WebKitWebSettings *settings;
1876 settings = webkit_web_view_get_settings(webview);
1877 my_pair.line = line;
1878 c = search_word(0);
1879 if (!strlen(my_pair.what))
1880 return FALSE;
1882 while (isspace(*c) && *c)
1883 c++;
1885 if (*c == ':' || *c == '=')
1886 c++;
1888 my_pair.line = c;
1889 c = search_word(1);
1891 listlen = LENGTH(browsersettings);
1892 for (i = 0; i < listlen; i++) {
1893 if (strlen(browsersettings[i].name) == strlen(my_pair.what) && strncmp(browsersettings[i].name, my_pair.what, strlen(my_pair.what)) == 0) {
1894 /* mandatory argument not provided */
1895 if (strlen(my_pair.value) == 0)
1896 return FALSE;
1897 /* process qmark? */
1898 if (strlen(my_pair.what) == 5 && strncmp("qmark", my_pair.what, 5) == 0) {
1899 return (process_save_qmark(my_pair.value, webview));
1901 /* interpret boolean values */
1902 if (browsersettings[i].boolval) {
1903 if (strncmp(my_pair.value, "on", 2) == 0 || strncmp(my_pair.value, "true", 4) == 0 || strncmp(my_pair.value, "ON", 2) == 0 || strncmp(my_pair.value, "TRUE", 4) == 0) {
1904 boolval = TRUE;
1905 } else if (strncmp(my_pair.value, "off", 3) == 0 || strncmp(my_pair.value, "false", 5) == 0 || strncmp(my_pair.value, "OFF", 3) == 0 || strncmp(my_pair.value, "FALSE", 5) == 0) {
1906 boolval = FALSE;
1907 } else {
1908 return FALSE;
1910 } else if (browsersettings[i].colourval) {
1911 /* interpret as hexadecimal colour */
1912 if (!parse_colour(my_pair.value)) {
1913 return FALSE;
1916 if (browsersettings[i].var != NULL) {
1917 strncpy(browsersettings[i].var, my_pair.value, MAX_SETTING_SIZE);
1918 if (strlen(my_pair.value) > MAX_SETTING_SIZE - 1) {
1919 /* in this case, \0 will not have been copied */
1920 browsersettings[i].var[MAX_SETTING_SIZE - 1] = '\0';
1921 /* in case this string is also used for a webkit setting, make sure it's consistent */
1922 my_pair.value[MAX_SETTING_SIZE - 1] = '\0';
1923 give_feedback("String too long; automatically truncated!");
1926 if (strlen(browsersettings[i].webkit) > 0) {
1927 /* activate appropriate webkit setting */
1928 if (browsersettings[i].boolval) {
1929 g_object_set((GObject*)settings, browsersettings[i].webkit, boolval, NULL);
1930 } else if (browsersettings[i].intval) {
1931 g_object_set((GObject*)settings, browsersettings[i].webkit, atoi(my_pair.value), NULL);
1932 } else {
1933 g_object_set((GObject*)settings, browsersettings[i].webkit, my_pair.value, NULL);
1935 webkit_web_view_set_settings(webview, settings);
1938 if (strlen(my_pair.what) == 14) {
1939 if (strncmp("acceptlanguage", my_pair.what, 14) == 0) {
1940 g_object_set(G_OBJECT(session), "accept-language", acceptlanguage, NULL);
1941 } else if (strncmp("completioncase", my_pair.what, 14) == 0) {
1942 complete_case_sensitive = boolval;
1944 } else if (strlen(my_pair.what) == 5 && strncmp("proxy", my_pair.what, 5) == 0) {
1945 toggle_proxy(boolval);
1946 } else if (strlen(my_pair.what) == 10 && strncmp("scrollbars", my_pair.what, 10) == 0) {
1947 toggle_scrollbars(boolval);
1948 } else if (strlen(my_pair.what) == 9 && strncmp("statusbar", my_pair.what, 9) == 0) {
1949 gtk_widget_set_visible(GTK_WIDGET(statusbar), boolval);
1950 } else if (strlen(my_pair.what) == 8 && strncmp("inputbox", my_pair.what, 8) == 0) {
1951 gtk_widget_set_visible(inputbox, boolval);
1952 } else if (strlen(my_pair.what) == 11 && strncmp("escapeinput", my_pair.what, 11) == 0) {
1953 escape_input_on_load = boolval;
1956 /* SSL certificate checking */
1957 if (strlen(my_pair.what) == 9 && strncmp("strictssl", my_pair.what, 9) == 0) {
1958 if (boolval) {
1959 strict_ssl = TRUE;
1960 g_object_set(G_OBJECT(session), "ssl-strict", TRUE, NULL);
1961 } else {
1962 strict_ssl = FALSE;
1963 g_object_set(G_OBJECT(session), "ssl-strict", FALSE, NULL);
1966 if (strlen(my_pair.what) == 8 && strncmp("cabundle", my_pair.what, 8) == 0) {
1967 g_object_set(G_OBJECT(session), SOUP_SESSION_SSL_CA_FILE, ca_bundle, NULL);
1970 /* reload page? */
1971 if (browsersettings[i].reload)
1972 webkit_web_view_reload(webview);
1973 return TRUE;
1976 return FALSE;
1979 gboolean
1980 process_line(char *line) {
1981 char *c = line;
1982 int i;
1983 size_t len, length = strlen(line);
1984 gboolean found = FALSE, success = FALSE;
1985 Arg a;
1987 while (isspace(*c))
1988 c++;
1989 /* Ignore blank lines. */
1990 if (c[0] == '\0')
1991 return TRUE;
1992 for (i = 0; i < LENGTH(commands); i++) {
1993 if (commands[i].cmd == NULL)
1994 break;
1995 len = strlen(commands[i].cmd);
1996 if (length >= len && !strncmp(c, commands[i].cmd, len) && (c[len] == ' ' || !c[len])) {
1997 found = TRUE;
1998 save_command_history(c);
1999 a.i = commands[i].arg.i;
2000 a.s = g_strdup(length > len + 1 ? &c[len + 1] : commands[i].arg.s);
2001 success = commands[i].func(&a);
2002 g_free(a.s);
2003 break;
2006 if (!found) {
2007 a.i = Error;
2008 a.s = g_strdup_printf("Not a browser command: %s", c);
2009 echo(&a);
2010 g_free(a.s);
2011 } else if (!success) {
2012 a.i = Error;
2013 if (error_msg != NULL) {
2014 a.s = g_strdup_printf("%s", error_msg);
2015 g_free(error_msg);
2016 error_msg = NULL;
2017 } else {
2018 a.s = g_strdup_printf("Unknown error. Please file a bug report!");
2020 echo(&a);
2021 g_free(a.s);
2023 return success;
2026 static gboolean
2027 search_tag(const Arg * a) {
2028 FILE *f;
2029 const char *filename;
2030 const char *tag = a->s;
2031 char s[BUFFERSIZE], foundtag[40], url[BUFFERSIZE];
2032 int t, i, intag, k;
2034 if (!tag) {
2035 /* The user must give us something to load up. */
2036 set_error("Bookmark tag required with this option.");
2037 return FALSE;
2040 if (strlen(tag) > MAXTAGSIZE) {
2041 set_error("Tag too long.");
2042 return FALSE;
2045 filename = g_strdup_printf(BOOKMARKS_STORAGE_FILENAME);
2046 f = fopen(filename, "r");
2047 g_free((gpointer *)filename);
2048 if (f == NULL) {
2049 set_error("Couldn't open bookmarks file.");
2050 return FALSE;
2052 while (fgets(s, BUFFERSIZE-1, f)) {
2053 intag = 0;
2054 t = strlen(s) - 1;
2055 while (isspace(s[t]))
2056 t--;
2057 if (s[t] != ']') continue;
2058 while (t > 0) {
2059 if (s[t] == ']') {
2060 if (!intag)
2061 intag = t;
2062 else
2063 intag = 0;
2064 } else {
2065 if (s[t] == '[') {
2066 if (intag) {
2067 i = 0;
2068 k = t + 1;
2069 while (k < intag)
2070 foundtag[i++] = s[k++];
2071 foundtag[i] = '\0';
2072 /* foundtag now contains the tag */
2073 if (strlen(foundtag) < MAXTAGSIZE && strcmp(tag, foundtag) == 0) {
2074 i = 0;
2075 while (isspace(s[i])) i++;
2076 k = 0;
2077 while (s[i] && !isspace(s[i])) url[k++] = s[i++];
2078 url[k] = '\0';
2079 Arg x = { .i = TargetNew, .s = url };
2080 open_arg(&x);
2083 intag = 0;
2086 t--;
2089 return TRUE;
2092 void
2093 toggle_proxy(gboolean onoff) {
2094 SoupURI *proxy_uri;
2095 char *filename, *new;
2097 if (onoff == FALSE) {
2098 g_object_set(session, "proxy-uri", NULL, NULL);
2099 } else {
2100 filename = (char *)g_getenv("http_proxy");
2102 /* Fallthrough to checking HTTP_PROXY as well, since this can also be
2103 * defined.
2105 if (filename == NULL)
2106 filename = (char *)g_getenv("HTTP_PROXY");
2108 if (filename != NULL && 0 < strlen(filename)) {
2109 new = g_strrstr(filename, "http://") ? g_strdup(filename) : g_strdup_printf("http://%s", filename);
2110 proxy_uri = soup_uri_new(new);
2112 g_object_set(session, "proxy-uri", proxy_uri, NULL);
2114 soup_uri_free(proxy_uri);
2115 g_free(new);
2120 void
2121 toggle_scrollbars(gboolean onoff) {
2122 if (onoff == TRUE) {
2123 adjust_h = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(viewport));
2124 adjust_v = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(viewport));
2125 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(viewport), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2126 } else {
2127 adjust_v = gtk_range_get_adjustment(GTK_RANGE(scroll_v));
2128 adjust_h = gtk_range_get_adjustment(GTK_RANGE(scroll_h));
2129 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(viewport), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
2131 gtk_widget_set_scroll_adjustments (GTK_WIDGET(webview), adjust_h, adjust_v);
2133 return;
2136 void
2137 update_url(const char *uri) {
2138 gboolean ssl = g_str_has_prefix(uri, "https://");
2139 GdkColor color;
2140 WebKitWebFrame *frame;
2141 WebKitWebDataSource *src;
2142 WebKitNetworkRequest *request;
2143 SoupMessage *msg;
2144 gboolean ssl_ok;
2145 char *sslactivecolor;
2146 gchar *markup;
2147 #ifdef ENABLE_HISTORY_INDICATOR
2148 char before[] = " [";
2149 char after[] = "]";
2150 gboolean back = webkit_web_view_can_go_back(webview);
2151 gboolean fwd = webkit_web_view_can_go_forward(webview);
2153 if (!back && !fwd)
2154 before[0] = after[0] = '\0';
2155 #endif
2156 markup = g_markup_printf_escaped(
2157 #ifdef ENABLE_HISTORY_INDICATOR
2158 "<span font=\"%s\">%s%s%s%s%s</span>", statusfont, uri,
2159 before, back ? "+" : "", fwd ? "-" : "", after
2160 #else
2161 "<span font=\"%s\">%s</span>", statusfont, uri
2162 #endif
2164 gtk_label_set_markup(GTK_LABEL(status_url), markup);
2165 g_free(markup);
2166 if (ssl) {
2167 frame = webkit_web_view_get_main_frame(webview);
2168 src = webkit_web_frame_get_data_source(frame);
2169 request = webkit_web_data_source_get_request(src);
2170 msg = webkit_network_request_get_message(request);
2171 ssl_ok = soup_message_get_flags(msg) & SOUP_MESSAGE_CERTIFICATE_TRUSTED;
2172 if (ssl_ok)
2173 sslactivecolor = sslbgcolor;
2174 else
2175 sslactivecolor = sslinvalidbgcolor;
2177 gdk_color_parse(ssl ? sslactivecolor : statusbgcolor, &color);
2178 gtk_widget_modify_bg(eventbox, GTK_STATE_NORMAL, &color);
2179 gdk_color_parse(ssl ? sslcolor : statuscolor, &color);
2180 gtk_widget_modify_fg(GTK_WIDGET(status_url), GTK_STATE_NORMAL, &color);
2181 gtk_widget_modify_fg(GTK_WIDGET(status_state), GTK_STATE_NORMAL, &color);
2184 void
2185 update_state() {
2186 char *markup;
2187 int download_count = g_list_length(activeDownloads);
2188 GString *status = g_string_new("");
2190 /* construct the status line */
2192 /* count, modkey and input buffer */
2193 g_string_append_printf(status, "%.0d", count);
2194 if (current_modkey) g_string_append_c(status, current_modkey);
2196 /* the number of active downloads */
2197 if (activeDownloads) {
2198 g_string_append_printf(status, " %d active %s", download_count,
2199 (download_count == 1) ? "download" : "downloads");
2202 #ifdef ENABLE_WGET_PROGRESS_BAR
2203 /* the progressbar */
2205 int progress = -1;
2206 char progressbar[progressbartick + 1];
2208 if (activeDownloads) {
2209 progress = 0;
2210 GList *ptr;
2212 for (ptr = activeDownloads; ptr; ptr = g_list_next(ptr)) {
2213 progress += 100 * webkit_download_get_progress(ptr->data);
2216 progress /= download_count;
2218 } else if (webkit_web_view_get_load_status(webview) != WEBKIT_LOAD_FINISHED
2219 && webkit_web_view_get_load_status(webview) != WEBKIT_LOAD_FAILED) {
2221 progress = webkit_web_view_get_progress(webview) * 100;
2224 if (progress >= 0) {
2225 ascii_bar(progressbartick, progress * progressbartick / 100, progressbar);
2226 g_string_append_printf(status, " %c%s%c",
2227 progressborderleft, progressbar, progressborderright);
2230 #endif
2232 /* and the current scroll position */
2234 int max = gtk_adjustment_get_upper(adjust_v) - gtk_adjustment_get_page_size(adjust_v);
2235 int val = (int)(gtk_adjustment_get_value(adjust_v) / max * 100);
2237 if (max == 0)
2238 g_string_append(status, " All");
2239 else if (val == 0)
2240 g_string_append(status, " Top");
2241 else if (val == 100)
2242 g_string_append(status, " Bot");
2243 else
2244 g_string_append_printf(status, " %d%%", val);
2248 markup = g_markup_printf_escaped("<span font=\"%s\">%s</span>", statusfont, status->str);
2249 gtk_label_set_markup(GTK_LABEL(status_state), markup);
2250 g_free(markup);
2252 g_string_free(status, TRUE);
2255 void
2256 setup_modkeys() {
2257 unsigned int i;
2258 modkeys = calloc(LENGTH(keys) + 1, sizeof(char));
2259 char *ptr = modkeys;
2261 for (i = 0; i < LENGTH(keys); i++)
2262 if (keys[i].modkey && !strchr(modkeys, keys[i].modkey))
2263 *(ptr++) = keys[i].modkey;
2264 modkeys = realloc(modkeys, &ptr[0] - &modkeys[0] + 1);
2267 void
2268 setup_gui() {
2269 scroll_h = GTK_SCROLLBAR(gtk_hscrollbar_new(NULL));
2270 scroll_v = GTK_SCROLLBAR(gtk_vscrollbar_new(NULL));
2271 adjust_h = gtk_range_get_adjustment(GTK_RANGE(scroll_h));
2272 adjust_v = gtk_range_get_adjustment(GTK_RANGE(scroll_v));
2273 if (embed) {
2274 window = GTK_WINDOW(gtk_plug_new(embed));
2275 } else {
2276 window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
2277 gtk_window_set_wmclass(GTK_WINDOW(window), "vimprobable2", "Vimprobable2");
2279 gtk_window_set_default_size(GTK_WINDOW(window), 640, 480);
2280 box = GTK_BOX(gtk_vbox_new(FALSE, 0));
2281 inputbox = gtk_entry_new();
2282 webview = (WebKitWebView*)webkit_web_view_new();
2283 statusbar = GTK_BOX(gtk_hbox_new(FALSE, 0));
2284 eventbox = gtk_event_box_new();
2285 status_url = gtk_label_new(NULL);
2286 status_state = gtk_label_new(NULL);
2287 GdkColor bg;
2288 PangoFontDescription *font;
2289 GdkGeometry hints = { 1, 1 };
2290 inspector = webkit_web_view_get_inspector(WEBKIT_WEB_VIEW(webview));
2292 clipboards[0] = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
2293 clipboards[1] = gtk_clipboard_get(GDK_NONE);
2294 setup_settings();
2295 gdk_color_parse(statusbgcolor, &bg);
2296 gtk_widget_modify_bg(eventbox, GTK_STATE_NORMAL, &bg);
2297 gtk_widget_set_name(GTK_WIDGET(window), "Vimprobable2");
2298 gtk_window_set_geometry_hints(window, NULL, &hints, GDK_HINT_MIN_SIZE);
2300 keymap = gdk_keymap_get_default();
2302 #ifdef DISABLE_SCROLLBAR
2303 viewport = gtk_scrolled_window_new(NULL, NULL);
2304 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(viewport), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
2305 #else
2306 /* Ensure we still see scrollbars. */
2307 GtkWidget *viewport = gtk_scrolled_window_new(adjust_h, adjust_v);
2308 #endif
2310 setup_signals();
2311 gtk_container_add(GTK_CONTAINER(viewport), GTK_WIDGET(webview));
2313 /* Ensure we set the scroll adjustments now, so that if we're not drawing
2314 * titlebars, we can still scroll.
2316 gtk_widget_set_scroll_adjustments(GTK_WIDGET(webview), adjust_h, adjust_v);
2318 font = pango_font_description_from_string(urlboxfont[0]);
2319 gtk_widget_modify_font(GTK_WIDGET(inputbox), font);
2320 pango_font_description_free(font);
2321 gtk_entry_set_inner_border(GTK_ENTRY(inputbox), NULL);
2322 gtk_misc_set_alignment(GTK_MISC(status_url), 0.0, 0.0);
2323 gtk_misc_set_alignment(GTK_MISC(status_state), 1.0, 0.0);
2324 gtk_box_pack_start(statusbar, status_url, TRUE, TRUE, 2);
2325 gtk_box_pack_start(statusbar, status_state, FALSE, FALSE, 2);
2326 gtk_container_add(GTK_CONTAINER(eventbox), GTK_WIDGET(statusbar));
2327 gtk_box_pack_start(box, viewport, TRUE, TRUE, 0);
2328 gtk_box_pack_start(box, eventbox, FALSE, FALSE, 0);
2329 gtk_entry_set_has_frame(GTK_ENTRY(inputbox), FALSE);
2330 gtk_box_pack_end(box, inputbox, FALSE, FALSE, 0);
2331 gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(box));
2332 gtk_widget_grab_focus(GTK_WIDGET(webview));
2333 gtk_widget_show_all(GTK_WIDGET(window));
2334 set_widget_font_and_color(inputbox, urlboxfont[0], urlboxbgcolor[0], urlboxcolor[0]);
2335 g_object_set(gtk_widget_get_settings(inputbox), "gtk-entry-select-on-focus", FALSE, NULL);
2338 void
2339 setup_settings() {
2340 WebKitWebSettings *settings = (WebKitWebSettings*)webkit_web_settings_new();
2341 char *filename, *file_url;
2343 session = webkit_get_default_session();
2344 g_object_set(G_OBJECT(session), "ssl-ca-file", ca_bundle, NULL);
2345 g_object_set(G_OBJECT(session), "ssl-strict", strict_ssl, NULL);
2346 g_object_set(G_OBJECT(settings), "default-font-size", DEFAULT_FONT_SIZE, NULL);
2347 g_object_set(G_OBJECT(settings), "enable-scripts", enablePlugins, NULL);
2348 g_object_set(G_OBJECT(settings), "enable-plugins", enablePlugins, NULL);
2349 g_object_set(G_OBJECT(settings), "enable-java-applet", enableJava, NULL);
2350 g_object_set(G_OBJECT(settings), "enable-page-cache", enablePagecache, NULL);
2351 filename = g_strdup_printf(USER_STYLESHEET);
2352 file_url = g_strdup_printf("file://%s", filename);
2353 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", file_url, NULL);
2354 g_free(file_url);
2355 g_free(filename);
2356 g_object_set(G_OBJECT(settings), "user-agent", useragent, NULL);
2357 g_object_get(G_OBJECT(settings), "zoom-step", &zoomstep, NULL);
2358 webkit_web_view_set_settings(webview, settings);
2360 /* proxy */
2361 toggle_proxy(use_proxy);
2364 void
2365 setup_signals() {
2366 WebKitWebFrame *frame = webkit_web_view_get_main_frame(webview);
2367 #ifdef ENABLE_COOKIE_SUPPORT
2368 /* Headers. */
2369 g_signal_connect_after(G_OBJECT(session), "request-started", G_CALLBACK(new_generic_request), NULL);
2370 #endif
2371 /* Accept-language header */
2372 g_object_set(G_OBJECT(session), "accept-language", acceptlanguage, NULL);
2373 /* window */
2374 g_object_connect(G_OBJECT(window),
2375 "signal::destroy", G_CALLBACK(window_destroyed_cb), NULL,
2376 NULL);
2377 /* frame */
2378 g_signal_connect(G_OBJECT(frame),
2379 "scrollbars-policy-changed", G_CALLBACK(blank_cb), NULL);
2380 /* webview */
2381 g_object_connect(G_OBJECT(webview),
2382 "signal::title-changed", G_CALLBACK(webview_title_changed_cb), NULL,
2383 "signal::load-progress-changed", G_CALLBACK(webview_progress_changed_cb), NULL,
2384 "signal::load-committed", G_CALLBACK(webview_load_committed_cb), NULL,
2385 "signal::load-finished", G_CALLBACK(webview_load_finished_cb), NULL,
2386 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_navigation_cb), NULL,
2387 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_new_window_cb), NULL,
2388 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb), NULL,
2389 "signal::download-requested", G_CALLBACK(webview_download_cb), NULL,
2390 "signal::key-press-event", G_CALLBACK(webview_keypress_cb), NULL,
2391 "signal::hovering-over-link", G_CALLBACK(webview_hoverlink_cb), NULL,
2392 "signal::console-message", G_CALLBACK(webview_console_cb), NULL,
2393 "signal::create-web-view", G_CALLBACK(webview_open_in_new_window_cb), NULL,
2394 "signal::event", G_CALLBACK(notify_event_cb), NULL,
2395 NULL);
2396 /* webview adjustment */
2397 g_object_connect(G_OBJECT(adjust_v),
2398 "signal::value-changed", G_CALLBACK(webview_scroll_cb), NULL,
2399 NULL);
2400 /* inputbox */
2401 g_object_connect(G_OBJECT(inputbox),
2402 "signal::activate", G_CALLBACK(inputbox_activate_cb), NULL,
2403 "signal::key-press-event", G_CALLBACK(inputbox_keypress_cb), NULL,
2404 "signal::key-release-event", G_CALLBACK(inputbox_keyrelease_cb), NULL,
2405 #ifdef ENABLE_INCREMENTAL_SEARCH
2406 "signal::changed", G_CALLBACK(inputbox_changed_cb), NULL,
2407 #endif
2408 NULL);
2409 /* inspector */
2410 g_signal_connect(G_OBJECT(inspector),
2411 "inspect-web-view", G_CALLBACK(inspector_inspect_web_view_cb), NULL);
2414 #ifdef ENABLE_COOKIE_SUPPORT
2415 void
2416 setup_cookies()
2418 if (file_cookie_jar)
2419 g_object_unref(file_cookie_jar);
2421 if (session_cookie_jar)
2422 g_object_unref(session_cookie_jar);
2424 session_cookie_jar = soup_cookie_jar_new();
2426 cookie_store = g_strdup_printf(COOKIES_STORAGE_FILENAME);
2428 load_all_cookies();
2430 g_signal_connect(G_OBJECT(file_cookie_jar), "changed",
2431 G_CALLBACK(update_cookie_jar), NULL);
2433 return;
2436 /* TA: XXX - we should be using this callback for any header-requests we
2437 * receive (hence the name "new_generic_request" -- but for now, its use
2438 * is limited to handling cookies.
2440 void
2441 new_generic_request(SoupSession *session, SoupMessage *soup_msg, gpointer unused)
2443 SoupMessageHeaders *soup_msg_h;
2444 SoupURI *uri;
2445 char *cookie_str;
2447 soup_msg_h = soup_msg->request_headers;
2448 soup_message_headers_remove(soup_msg_h, "Cookie");
2449 uri = soup_message_get_uri(soup_msg);
2450 if ((cookie_str = get_cookies(uri))) {
2451 soup_message_headers_append(soup_msg_h, "Cookie", cookie_str);
2452 g_free(cookie_str);
2455 g_signal_connect_after(G_OBJECT(soup_msg), "got-headers", G_CALLBACK(handle_cookie_request), NULL);
2457 return;
2460 char *
2461 get_cookies(SoupURI *soup_uri) {
2462 char *cookie_str;
2464 cookie_str = soup_cookie_jar_get_cookies(file_cookie_jar, soup_uri, TRUE);
2466 return cookie_str;
2469 void
2470 handle_cookie_request(SoupMessage *soup_msg, gpointer unused)
2472 GSList *resp_cookie = NULL, *cookie_list;
2473 SoupCookie *cookie;
2475 cookie_list = soup_cookies_from_response(soup_msg);
2476 for(resp_cookie = cookie_list; resp_cookie; resp_cookie = g_slist_next(resp_cookie))
2478 SoupDate *soup_date;
2479 cookie = soup_cookie_copy((SoupCookie *)resp_cookie->data);
2481 if (cookie_timeout && cookie->expires == NULL) {
2482 soup_date = soup_date_new_from_time_t(time(NULL) + cookie_timeout * 10);
2483 soup_cookie_set_expires(cookie, soup_date);
2484 soup_date_free(soup_date);
2486 soup_cookie_jar_add_cookie(file_cookie_jar, cookie);
2489 soup_cookies_free(cookie_list);
2491 return;
2494 void
2495 update_cookie_jar(SoupCookieJar *jar, SoupCookie *old, SoupCookie *new)
2497 if (!new) {
2498 /* Nothing to do. */
2499 return;
2502 SoupCookie *copy;
2503 copy = soup_cookie_copy(new);
2505 soup_cookie_jar_add_cookie(session_cookie_jar, copy);
2507 return;
2510 void
2511 load_all_cookies(void)
2513 GSList *cookie_list;
2514 file_cookie_jar = soup_cookie_jar_text_new(cookie_store, COOKIES_STORAGE_READONLY);
2516 /* Put them back in the session store. */
2517 GSList *cookies_from_file = soup_cookie_jar_all_cookies(file_cookie_jar);
2518 cookie_list = cookies_from_file;
2520 for (; cookies_from_file;
2521 cookies_from_file = cookies_from_file->next)
2523 soup_cookie_jar_add_cookie(session_cookie_jar, cookies_from_file->data);
2526 soup_cookies_free(cookies_from_file);
2527 g_slist_free(cookie_list);
2529 return;
2532 #endif
2534 void
2535 mop_up(void) {
2536 /* Free up any nasty globals before exiting. */
2537 #ifdef ENABLE_COOKIE_SUPPORT
2538 if (cookie_store)
2539 g_free(cookie_store);
2540 #endif
2541 return;
2545 main(int argc, char *argv[]) {
2546 static Arg a;
2547 static char url[256] = "";
2548 static gboolean ver = false;
2549 static gboolean configfile_exists = FALSE;
2550 static const char *cfile = NULL;
2551 static GOptionEntry opts[] = {
2552 { "version", 'v', 0, G_OPTION_ARG_NONE, &ver, "print version", NULL },
2553 { "embed", 'e', 0, G_OPTION_ARG_STRING, &winid, "embedded", NULL },
2554 { "configfile", 'c', 0, G_OPTION_ARG_STRING, &cfile, "config file", NULL },
2555 { NULL }
2557 static GError *err;
2558 args = argv;
2560 /* command line argument: version */
2561 if (!gtk_init_with_args(&argc, &argv, "[<uri>]", opts, NULL, &err)) {
2562 g_printerr("can't init gtk: %s\n", err->message);
2563 g_error_free(err);
2564 return EXIT_FAILURE;
2567 if (ver) {
2568 printf("%s\n", INTERNAL_VERSION);
2569 return EXIT_SUCCESS;
2572 if( getenv("XDG_CONFIG_HOME") )
2573 config_base = g_strdup_printf("%s", getenv("XDG_CONFIG_HOME"));
2574 else
2575 config_base = g_strdup_printf("%s/.config/", getenv("HOME"));
2577 if (cfile)
2578 configfile = g_strdup(cfile);
2579 else
2580 configfile = g_strdup_printf(RCFILE);
2582 if (!g_thread_supported())
2583 g_thread_init(NULL);
2585 if (winid)
2586 embed = atoi(winid);
2588 setup_modkeys();
2589 make_keyslist();
2590 setup_gui();
2591 #ifdef ENABLE_COOKIE_SUPPORT
2592 setup_cookies();
2593 #endif
2595 make_searchengines_list(searchengines, LENGTH(searchengines));
2596 make_uri_handlers_list(uri_handlers, LENGTH(uri_handlers));
2598 /* Check if the specified file exists. */
2599 /* And only warn the user, if they explicitly asked for a config on the
2600 * command line.
2602 if (!(access(configfile, F_OK) == 0) && cfile) {
2603 char *feedback_str;
2605 feedback_str = g_strdup_printf("Config file '%s' doesn't exist", cfile);
2606 give_feedback(feedback_str);
2607 g_free(feedback_str);
2608 } else if ((access(configfile, F_OK) == 0))
2609 configfile_exists = true;
2611 /* read config file */
2612 /* But only report errors if we failed, and the file existed. */
2613 if ((SUCCESS != read_rcfile(configfile)) && configfile_exists) {
2614 a.i = Error;
2615 a.s = g_strdup_printf("Error in config file '%s'", configfile);
2616 echo(&a);
2617 g_free(a.s);
2618 g_free(configfile);
2621 /* command line argument: URL */
2622 if (argc > 1) {
2623 strncpy(url, argv[argc - 1], 255);
2624 } else {
2625 strncpy(url, startpage, 255);
2628 a.i = TargetCurrent;
2629 a.s = url;
2630 open_arg(&a);
2631 gtk_main();
2633 mop_up();
2635 return EXIT_SUCCESS;