2 * Copyright (c) 2010, 2011 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2011 Stevan Andjelkovic <stevan@student.chalmers.se>
4 * Copyright (c) 2010, 2011, 2012 Edd Barrett <vext01@gmail.com>
5 * Copyright (c) 2011 Todd T. Fries <todd@fries.net>
6 * Copyright (c) 2011 Raphael Graf <r@undefined.ch>
7 * Copyright (c) 2011 Michal Mazurek <akfaew@jasminek.net>
8 * Copyright (c) 2012, 2013 Josh Rickmar <jrick@devio.us>
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 char *version
= XOMBRERO_VERSION
;
29 uint32_t swm_debug
= 0
60 TAILQ_ENTRY(session
) entry
;
63 TAILQ_HEAD(session_list
, session
);
66 TAILQ_ENTRY(undo
) entry
;
69 int back
; /* Keeps track of how many back
70 * history items there are. */
72 TAILQ_HEAD(undo_tailq
, undo
);
74 struct command_entry
{
76 TAILQ_ENTRY(command_entry
) entry
;
78 TAILQ_HEAD(command_list
, command_entry
);
81 #define XT_CACHE_DIR ("cache")
82 #define XT_CERT_DIR ("certs")
83 #define XT_CERT_CACHE_DIR ("certs_cache")
84 #define XT_JS_DIR ("js")
85 #define XT_SESSIONS_DIR ("sessions")
86 #define XT_TEMP_DIR ("tmp")
87 #define XT_QMARKS_FILE ("quickmarks")
88 #define XT_SAVED_TABS_FILE ("main_session")
89 #define XT_RESTART_TABS_FILE ("restart_tabs")
90 #define XT_SOCKET_FILE ("socket")
91 #define XT_SAVE_SESSION_ID ("SESSION_NAME=")
92 #define XT_SEARCH_FILE ("search_history")
93 #define XT_COMMAND_FILE ("command_history")
94 #define XT_DLMAN_REFRESH "10"
95 #define XT_MAX_URL_LENGTH (4096) /* 1 page is atomic, don't make bigger */
96 #define XT_MAX_UNDO_CLOSE_TAB (32)
97 #define XT_PRINT_EXTRA_MARGIN 10
98 #define XT_URL_REGEX ("^[[:blank:]]*[^[:blank:]]*([[:alnum:]-]+\\.)+[[:alnum:]-][^[:blank:]]*[[:blank:]]*$")
99 #define XT_INVALID_MARK (-1) /* XXX this is a double, maybe use something else, like a nan */
100 #define XT_MAX_CERTS (32)
103 #define XT_COLOR_RED "#cc0000"
104 #define XT_COLOR_YELLOW "#ffff66"
105 #define XT_COLOR_BLUE "lightblue"
106 #define XT_COLOR_GREEN "#99ff66"
107 #define XT_COLOR_WHITE "white"
108 #define XT_COLOR_BLACK "black"
110 #define XT_COLOR_CT_BACKGROUND "#000000"
111 #define XT_COLOR_CT_INACTIVE "#dddddd"
112 #define XT_COLOR_CT_ACTIVE "#bbbb00"
113 #define XT_COLOR_CT_SEPARATOR "#555555"
115 #define XT_COLOR_SB_SEPARATOR "#555555"
117 /* CSS element names */
118 #define XT_CSS_NORMAL ""
119 #define XT_CSS_RED "red"
120 #define XT_CSS_YELLOW "yellow"
121 #define XT_CSS_GREEN "green"
122 #define XT_CSS_BLUE "blue"
123 #define XT_CSS_HIDDEN "hidden"
124 #define XT_CSS_ACTIVE "active"
126 #define XT_PROTO_DELIM "://"
129 #define XT_MOVE_INVALID (0)
130 #define XT_MOVE_DOWN (1)
131 #define XT_MOVE_UP (2)
132 #define XT_MOVE_BOTTOM (3)
133 #define XT_MOVE_TOP (4)
134 #define XT_MOVE_PAGEDOWN (5)
135 #define XT_MOVE_PAGEUP (6)
136 #define XT_MOVE_HALFDOWN (7)
137 #define XT_MOVE_HALFUP (8)
138 #define XT_MOVE_LEFT (9)
139 #define XT_MOVE_FARLEFT (10)
140 #define XT_MOVE_RIGHT (11)
141 #define XT_MOVE_FARRIGHT (12)
142 #define XT_MOVE_PERCENT (13)
143 #define XT_MOVE_CENTER (14)
145 #define XT_QMARK_SET (0)
146 #define XT_QMARK_OPEN (1)
147 #define XT_QMARK_TAB (2)
149 #define XT_MARK_SET (0)
150 #define XT_MARK_GOTO (1)
152 #define XT_GO_UP_ROOT (999)
154 #define XT_NAV_INVALID (0)
155 #define XT_NAV_BACK (1)
156 #define XT_NAV_FORWARD (2)
157 #define XT_NAV_RELOAD (3)
158 #define XT_NAV_STOP (4)
160 #define XT_FOCUS_INVALID (0)
161 #define XT_FOCUS_URI (1)
162 #define XT_FOCUS_SEARCH (2)
164 #define XT_SEARCH_INVALID (0)
165 #define XT_SEARCH_NEXT (1)
166 #define XT_SEARCH_PREV (2)
168 #define XT_PASTE_CURRENT_TAB (0)
169 #define XT_PASTE_NEW_TAB (1)
171 #define XT_ZOOM_IN (-1)
172 #define XT_ZOOM_OUT (-2)
173 #define XT_ZOOM_NORMAL (100)
175 #define XT_SES_DONOTHING (0)
176 #define XT_SES_CLOSETABS (1)
178 #define XT_PREFIX (1<<0)
179 #define XT_USERARG (1<<1)
180 #define XT_URLARG (1<<2)
181 #define XT_INTARG (1<<3)
182 #define XT_SESSARG (1<<4)
183 #define XT_SETARG (1<<5)
185 #define XT_HINT_NEWTAB (1<<0)
187 #define XT_BUFCMD_SZ (8)
189 #define XT_EJS_SHOW (1<<0)
191 GtkWidget
* create_button(const char *, const char *, int);
193 void recalc_tabs(void);
194 void recolor_compact_tabs(void);
195 void set_current_tab(int page_num
);
196 gboolean
update_statusbar_position(GtkAdjustment
*, gpointer
);
197 void marks_clear(struct tab
*t
);
200 extern char *__progname
;
201 char * const *start_argv
;
203 GtkWidget
*main_window
;
204 GtkNotebook
*notebook
;
206 GtkWidget
*tab_bar_box
;
207 GtkWidget
*arrow
, *abtn
;
208 GdkEvent
*fevent
= NULL
;
209 struct tab_list tabs
;
210 struct history_list hl
;
211 int hl_purge_count
= 0;
212 struct session_list sessions
;
214 struct wl_list js_wl
;
215 struct wl_list pl_wl
;
216 struct wl_list force_https
;
218 struct strict_transport_tree st_tree
;
219 struct undo_tailq undos
;
220 struct keybinding_list kbl
;
222 struct user_agent_list ua_list
;
223 struct http_accept_list ha_list
;
224 struct domain_id_list di_list
;
225 struct cmd_alias_list cal
;
226 struct custom_uri_list cul
;
227 struct command_list chl
;
228 struct command_list shl
;
229 struct command_entry
*history_at
;
230 struct command_entry
*search_at
;
231 struct secviolation_list svl
;
232 struct set_reject_list srl
;
234 int cmd_history_count
= 0;
235 int search_history_count
= 0;
237 uint64_t blocked_cookies
= 0;
238 char named_session
[PATH_MAX
];
239 GtkListStore
*completion_model
;
240 GtkListStore
*buffers_store
;
243 char *qmarks
[XT_NOQMARKS
];
244 int btn_down
; /* M1 down in any wv */
245 regex_t url_re
; /* guess_search regex */
247 /* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
248 int next_download_id
= 1;
250 void xxx_dir(char *);
251 int icon_size_map(int);
252 void activate_uri_entry_cb(GtkWidget
*, struct tab
*);
255 history_delete(struct command_list
*l
, int *counter
)
257 struct command_entry
*c
;
259 if (l
== NULL
|| counter
== NULL
)
262 c
= TAILQ_LAST(l
, command_list
);
266 TAILQ_REMOVE(l
, c
, entry
);
273 history_add(struct command_list
*list
, char *file
, char *l
, int *counter
)
275 struct command_entry
*c
;
278 if (list
== NULL
|| l
== NULL
|| counter
== NULL
)
281 /* don't add the same line */
282 c
= TAILQ_FIRST(list
);
284 if (!strcmp(c
->line
+ 1 /* skip space */, l
))
287 c
= g_malloc0(sizeof *c
);
288 c
->line
= g_strdup_printf(" %s", l
);
291 TAILQ_INSERT_HEAD(list
, c
, entry
);
294 history_delete(list
, counter
);
296 if (history_autosave
&& file
) {
297 f
= fopen(file
, "w");
299 show_oops(NULL
, "couldn't write history %s", file
);
303 TAILQ_FOREACH_REVERSE(c
, list
, command_list
, entry
) {
305 fprintf(f
, "%s\n", c
->line
);
313 history_read(struct command_list
*list
, char *file
, int *counter
)
316 char *s
, line
[65536];
318 if (list
== NULL
|| file
== NULL
)
321 f
= fopen(file
, "r");
323 startpage_add("couldn't open history file %s", file
);
328 s
= fgets(line
, sizeof line
, f
);
329 if (s
== NULL
|| feof(f
) || ferror(f
))
331 if ((s
= strchr(line
, '\n')) == NULL
) {
332 startpage_add("invalid history file %s", file
);
338 history_add(list
, NULL
, line
+ 1, counter
);
346 /* marks array storage. */
350 if (i
< 0 || i
>= XT_NOMARKS
)
361 if ((ret
= strchr(XT_MARKS
, m
)) != NULL
)
362 return ret
- XT_MARKS
;
367 /* quickmarks array storage. */
371 if (i
< 0 || i
>= XT_NOQMARKS
)
382 if ((ret
= strchr(XT_QMARKS
, m
)) != NULL
)
383 return ret
- XT_QMARKS
;
389 is_g_object_setting(GObject
*o
, char *str
)
391 guint n_props
= 0, i
;
392 GParamSpec
**proplist
;
398 proplist
= g_object_class_list_properties(G_OBJECT_GET_CLASS(o
),
401 for (i
= 0; i
< n_props
; i
++) {
402 if (! strcmp(proplist
[i
]->name
, str
)) {
413 get_current_tab(void)
417 TAILQ_FOREACH(t
, &tabs
, entry
) {
418 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
422 warnx("%s: no current tab", __func__
);
428 set_ssl_ca_file(struct settings
*s
, char *file
)
432 if (file
== NULL
|| strlen(file
) == 0)
434 if (stat(file
, &sb
)) {
435 warnx("no CA file: %s", file
);
438 expand_tilde(ssl_ca_file
, sizeof ssl_ca_file
, file
);
439 g_object_set(session
,
440 SOUP_SESSION_SSL_CA_FILE
, ssl_ca_file
,
441 SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
447 set_status(struct tab
*t
, gchar
*fmt
, ...)
454 status
= g_strdup_vprintf(fmt
, ap
);
456 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.uri
), status
);
460 else if (strcmp(t
->status
, status
)) {
471 hide_cmd(struct tab
*t
)
473 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
475 history_at
= NULL
; /* just in case */
476 search_at
= NULL
; /* just in case */
477 gtk_widget_set_can_focus(t
->cmd
, FALSE
);
478 gtk_widget_hide(t
->cmd
);
482 show_cmd(struct tab
*t
, const char *s
)
484 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
486 /* without this you can't middle click in t->cmd to paste */
487 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), "");
491 gtk_widget_hide(t
->oops
);
492 gtk_widget_set_can_focus(t
->cmd
, TRUE
);
493 gtk_widget_show(t
->cmd
);
495 gtk_widget_grab_focus(GTK_WIDGET(t
->cmd
));
496 #if GTK_CHECK_VERSION(3, 0, 0)
497 gtk_widget_set_name(t
->cmd
, XT_CSS_NORMAL
);
499 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
500 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
502 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), s
);
503 gtk_editable_set_position(GTK_EDITABLE(t
->cmd
), -1);
507 hide_buffers(struct tab
*t
)
509 gtk_widget_hide(t
->buffers
);
510 gtk_widget_set_can_focus(t
->buffers
, FALSE
);
511 gtk_list_store_clear(buffers_store
);
522 sort_tabs_by_page_num(struct tab
***stabs
)
527 num_tabs
= gtk_notebook_get_n_pages(notebook
);
529 *stabs
= g_malloc0(num_tabs
* sizeof(struct tab
*));
531 TAILQ_FOREACH(t
, &tabs
, entry
)
532 (*stabs
)[gtk_notebook_page_num(notebook
, t
->vbox
)] = t
;
538 buffers_make_list(void)
541 const gchar
*title
= NULL
;
542 struct tab
**stabs
= NULL
;
545 num_tabs
= sort_tabs_by_page_num(&stabs
);
547 for (i
= 0; i
< num_tabs
; i
++)
549 gtk_list_store_append(buffers_store
, &iter
);
550 title
= get_title(stabs
[i
], FALSE
);
551 gtk_list_store_set(buffers_store
, &iter
,
552 COL_ID
, i
+ 1, /* Enumerate the tabs starting from 1
554 COL_FAVICON
, gtk_image_get_pixbuf
555 (GTK_IMAGE(stabs
[i
]->tab_elems
.favicon
)),
564 show_buffers(struct tab
*t
)
567 GtkTreeSelection
*sel
;
571 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)))
576 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(t
->buffers
));
577 index
= gtk_notebook_get_current_page(notebook
);
578 path
= gtk_tree_path_new_from_indices(index
, -1);
579 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
, path
))
580 gtk_tree_selection_select_iter(sel
, &iter
);
581 gtk_tree_path_free(path
);
583 gtk_widget_show(t
->buffers
);
584 gtk_widget_set_can_focus(t
->buffers
, TRUE
);
585 gtk_widget_grab_focus(GTK_WIDGET(t
->buffers
));
589 toggle_buffers(struct tab
*t
)
591 if (gtk_widget_get_visible(t
->buffers
))
598 buffers(struct tab
*t
, struct karg
*args
)
606 set_scrollbar_visibility(struct tab
*t
, int visible
)
608 #if GTK_CHECK_VERSION(3, 0, 0)
609 GtkWidget
*h_scrollbar
, *v_scrollbar
;
611 h_scrollbar
= gtk_scrolled_window_get_hscrollbar(
612 GTK_SCROLLED_WINDOW(t
->browser_win
));
613 v_scrollbar
= gtk_scrolled_window_get_vscrollbar(
614 GTK_SCROLLED_WINDOW(t
->browser_win
));
617 gtk_widget_set_name(h_scrollbar
, XT_CSS_HIDDEN
);
618 gtk_widget_set_name(v_scrollbar
, XT_CSS_HIDDEN
);
620 gtk_widget_set_name(h_scrollbar
, "");
621 gtk_widget_set_name(v_scrollbar
, "");
626 return (visible
== 0);
631 hide_oops(struct tab
*t
)
633 gtk_widget_hide(t
->oops
);
637 show_oops(struct tab
*at
, const char *fmt
, ...)
641 struct tab
*t
= NULL
;
647 if ((t
= get_current_tab()) == NULL
)
653 if ((msg
= g_strdup_vprintf(fmt
, ap
)) == NULL
)
654 errx(1, "show_oops failed");
657 gtk_entry_set_text(GTK_ENTRY(t
->oops
), msg
);
658 gtk_widget_hide(t
->cmd
);
659 gtk_widget_show(t
->oops
);
665 char work_dir
[PATH_MAX
];
666 char certs_dir
[PATH_MAX
];
667 char certs_cache_dir
[PATH_MAX
];
668 char js_dir
[PATH_MAX
];
669 char cache_dir
[PATH_MAX
];
670 char sessions_dir
[PATH_MAX
];
671 char temp_dir
[PATH_MAX
];
672 char cookie_file
[PATH_MAX
];
673 char *strict_transport_file
= NULL
;
674 SoupSession
*session
;
675 SoupCookieJar
*s_cookiejar
;
676 SoupCookieJar
*p_cookiejar
;
677 char rc_fname
[PATH_MAX
];
679 struct mime_type_list mtl
;
680 struct alias_list aliases
;
683 struct tab
*create_new_tab(const char *, struct undo
*, int, int);
684 void delete_tab(struct tab
*);
685 void setzoom_webkit(struct tab
*, int);
686 int download_rb_cmp(struct download
*, struct download
*);
687 gboolean
cmd_execute(struct tab
*t
, char *str
);
690 history_rb_cmp(struct history
*h1
, struct history
*h2
)
692 return (strcmp(h1
->uri
, h2
->uri
));
694 RB_GENERATE(history_list
, history
, entry
, history_rb_cmp
);
697 download_rb_cmp(struct download
*e1
, struct download
*e2
)
699 return (e1
->id
< e2
->id
? -1 : e1
->id
> e2
->id
);
701 RB_GENERATE(download_list
, download
, entry
, download_rb_cmp
);
704 secviolation_rb_cmp(struct secviolation
*s1
, struct secviolation
*s2
)
706 return (s1
->xtp_arg
< s2
->xtp_arg
? -1 : s1
->xtp_arg
> s2
->xtp_arg
);
708 RB_GENERATE(secviolation_list
, secviolation
, entry
, secviolation_rb_cmp
);
711 user_agent_rb_cmp(struct user_agent
*ua1
, struct user_agent
*ua2
)
713 return (ua1
->id
< ua2
->id
? -1 : ua1
->id
> ua2
->id
);
715 RB_GENERATE(user_agent_list
, user_agent
, entry
, user_agent_rb_cmp
);
718 http_accept_rb_cmp(struct http_accept
*ha1
, struct http_accept
*ha2
)
720 return (ha1
->id
< ha2
->id
? -1 : ha1
->id
> ha2
->id
);
722 RB_GENERATE(http_accept_list
, http_accept
, entry
, http_accept_rb_cmp
);
725 domain_id_rb_cmp(struct domain_id
*d1
, struct domain_id
*d2
)
727 return (strcmp(d1
->domain
, d2
->domain
));
729 RB_GENERATE(domain_id_list
, domain_id
, entry
, domain_id_rb_cmp
);
731 struct valid_url_types
{
743 valid_url_type(const char *url
)
747 for (i
= 0; i
< LENGTH(vut
); i
++)
748 if (!strncasecmp(vut
[i
].type
, url
, strlen(vut
[i
].type
)))
755 match_alias(const char *url_in
)
759 char *url_out
= NULL
, *search
, *enc_arg
;
762 search
= g_strdup(url_in
);
764 if (strsep(&arg
, " \t") == NULL
) {
765 show_oops(NULL
, "match_alias: NULL URL");
769 TAILQ_FOREACH(a
, &aliases
, entry
) {
770 if (!strcmp(search
, a
->a_name
))
775 DNPRINTF(XT_D_URL
, "match_alias: matched alias %s\n",
779 enc_arg
= soup_uri_encode(arg
, XT_RESERVED_CHARS
);
780 sv
= g_strsplit(a
->a_uri
, "%s", 2);
782 url_out
= g_strjoinv(enc_arg
, sv
);
784 url_out
= g_strjoinv("", sv
);
794 guess_url_type(const char *url_in
)
797 char cwd
[PATH_MAX
] = {0};
798 char *url_out
= NULL
, *enc_search
= NULL
;
804 /* substitute aliases */
805 url_out
= match_alias(url_in
);
809 /* see if we are an about page */
810 if (!strncmp(url_in
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
))
811 for (i
= 0; i
< about_list_size(); i
++)
812 if (!strcmp(&url_in
[XT_URI_ABOUT_LEN
],
813 about_list
[i
].name
)) {
814 url_out
= g_strdup(url_in
);
818 if (guess_search
&& url_regex
&&
819 !(g_str_has_prefix(url_in
, "http://") ||
820 g_str_has_prefix(url_in
, "https://"))) {
821 if (regexec(&url_re
, url_in
, 0, NULL
, 0)) {
822 /* invalid URI so search instead */
823 enc_search
= soup_uri_encode(url_in
, XT_RESERVED_CHARS
);
824 sv
= g_strsplit(search_string
, "%s", 2);
825 url_out
= g_strjoinv(enc_search
, sv
);
832 /* XXX not sure about this heuristic */
833 if (stat(url_in
, &sb
) == 0) {
834 if (url_in
[0] == '/')
835 url_out
= g_filename_to_uri(url_in
, NULL
, NULL
);
837 if (getcwd(cwd
, PATH_MAX
) != NULL
) {
838 path
= g_strdup_printf("%s" PS
"%s", cwd
,
840 url_out
= g_filename_to_uri(path
, NULL
, NULL
);
845 url_out
= g_strdup_printf("http://%s", url_in
); /* guess http */
847 DNPRINTF(XT_D_URL
, "guess_url_type: guessed %s\n", url_out
);
853 set_normal_tab_meaning(struct tab
*t
)
858 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
859 if (t
->session_key
!= NULL
) {
860 g_free(t
->session_key
);
861 t
->session_key
= NULL
;
866 load_uri(struct tab
*t
, const gchar
*uri
)
869 gchar
*newuri
= NULL
;
875 /* Strip leading spaces. */
876 while (*uri
&& isspace(*uri
))
879 if (strlen(uri
) == 0) {
884 set_normal_tab_meaning(t
);
886 if (valid_url_type(uri
)) {
887 newuri
= guess_url_type(uri
);
891 /* clear :cert show host */
892 if (t
->about_cert_host
) {
893 g_free(t
->about_cert_host
);
894 t
->about_cert_host
= NULL
;
897 if (!strncmp(uri
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
)) {
898 for (i
= 0; i
< about_list_size(); i
++)
899 if (!strcmp(&uri
[XT_URI_ABOUT_LEN
], about_list
[i
].name
) &&
900 about_list
[i
].func
!= NULL
) {
901 bzero(&args
, sizeof args
);
902 about_list
[i
].func(t
, &args
);
903 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
),
907 show_oops(t
, "invalid about page");
911 /* remove old HTTPS cert chain (if any) */
917 set_status(t
, "Loading: %s", (char *)uri
);
919 webkit_web_view_load_uri(t
->wv
, uri
);
926 get_uri(struct tab
*t
)
928 const gchar
*uri
= NULL
;
930 if (webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
&&
931 !t
->download_requested
)
933 if (t
->xtp_meaning
== XT_XTP_TAB_MEANING_NORMAL
)
934 uri
= webkit_web_view_get_uri(t
->wv
);
936 /* use tmp_uri to make sure it is g_freed */
939 t
->tmp_uri
= g_strdup_printf("%s%s", XT_URI_ABOUT
,
940 about_list
[t
->xtp_meaning
].name
);
947 get_title(struct tab
*t
, bool window
)
949 const gchar
*set
= NULL
, *title
= NULL
;
950 WebKitLoadStatus status
;
952 status
= webkit_web_view_get_load_status(t
->wv
);
953 if (status
== WEBKIT_LOAD_PROVISIONAL
||
954 (status
== WEBKIT_LOAD_FAILED
&& !t
->download_requested
) ||
955 t
->xtp_meaning
== XT_XTP_TAB_MEANING_BL
)
958 title
= webkit_web_view_get_title(t
->wv
);
959 if ((set
= title
? title
: get_uri(t
)))
963 set
= window
? XT_NAME
: "(untitled)";
969 find_mime_type(char *mime_type
)
971 struct mime_type
*m
, *def
= NULL
, *rv
= NULL
;
973 TAILQ_FOREACH(m
, &mtl
, entry
) {
975 !strncmp(mime_type
, m
->mt_type
, strlen(m
->mt_type
)))
978 if (m
->mt_default
== 0 && !strcmp(mime_type
, m
->mt_type
)) {
991 * This only escapes the & and < characters, as per the discussion found here:
992 * http://lists.apple.com/archives/Webkitsdk-dev/2007/May/msg00056.html
995 html_escape(const char *val
)
1003 sv
= g_strsplit(val
, "&", -1);
1004 s
= g_strjoinv("&", sv
);
1007 sv
= g_strsplit(val
, "<", -1);
1008 s
= g_strjoinv("<", sv
);
1015 wl_find_uri(const gchar
*s
, struct wl_list
*wl
)
1021 if (s
== NULL
|| wl
== NULL
)
1024 if (!strncmp(s
, "http://", strlen("http://")))
1025 s
= &s
[strlen("http://")];
1026 else if (!strncmp(s
, "https://", strlen("https://")))
1027 s
= &s
[strlen("https://")];
1032 for (i
= 0; i
< strlen(s
) + 1 /* yes er need this */; i
++)
1033 /* chop string at first slash */
1034 if (s
[i
] == '/' || s
[i
] == ':' || s
[i
] == '\0') {
1037 w
= wl_find(ss
, wl
);
1046 js_ref_to_string(JSContextRef context
, JSValueRef ref
)
1052 jsref
= JSValueToStringCopy(context
, ref
, NULL
);
1056 l
= JSStringGetMaximumUTF8CStringSize(jsref
);
1059 JSStringGetUTF8CString(jsref
, s
, l
);
1060 JSStringRelease(jsref
);
1065 #define XT_JS_DONE ("done;")
1066 #define XT_JS_DONE_LEN (strlen(XT_JS_DONE))
1067 #define XT_JS_INSERT ("insert;")
1068 #define XT_JS_INSERT_LEN (strlen(XT_JS_INSERT))
1071 run_script(struct tab
*t
, char *s
)
1073 JSGlobalContextRef ctx
;
1074 WebKitWebFrame
*frame
;
1076 JSValueRef val
, exception
;
1079 DNPRINTF(XT_D_JS
, "%s: tab %d %s\n", __func__
,
1080 t
->tab_id
, s
== (char *)JS_HINTING
? "JS_HINTING" : s
);
1082 frame
= webkit_web_view_get_main_frame(t
->wv
);
1083 ctx
= webkit_web_frame_get_global_context(frame
);
1085 str
= JSStringCreateWithUTF8CString(s
);
1086 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
1087 NULL
, 0, &exception
);
1088 JSStringRelease(str
);
1090 DNPRINTF(XT_D_JS
, "%s: val %p\n", __func__
, val
);
1092 es
= js_ref_to_string(ctx
, exception
);
1094 DNPRINTF(XT_D_JS
, "%s: exception %s\n", __func__
, es
);
1099 es
= js_ref_to_string(ctx
, val
);
1102 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
1104 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
1108 DNPRINTF(XT_D_JS
, "%s: val %s\n", __func__
, es
);
1117 enable_hints(struct tab
*t
)
1119 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
1122 run_script(t
, "hints.createHints('', 'F');");
1124 run_script(t
, "hints.createHints('', 'f');");
1125 t
->mode
= XT_MODE_HINT
;
1129 disable_hints(struct tab
*t
)
1131 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
1133 run_script(t
, "hints.clearHints();");
1134 t
->mode
= XT_MODE_COMMAND
;
1139 passthrough(struct tab
*t
, struct karg
*args
)
1141 t
->mode
= XT_MODE_PASSTHROUGH
;
1146 modurl(struct tab
*t
, struct karg
*args
)
1148 const gchar
*uri
= NULL
;
1151 /* XXX kind of a bad hack, but oh well */
1152 if (gtk_widget_has_focus(t
->uri_entry
)) {
1153 if ((uri
= gtk_entry_get_text(GTK_ENTRY(t
->uri_entry
))) &&
1155 u
= g_strdup_printf("www.%s.com", uri
);
1156 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), u
);
1158 activate_uri_entry_cb(t
->uri_entry
, t
);
1165 hint(struct tab
*t
, struct karg
*args
)
1168 DNPRINTF(XT_D_JS
, "hint: tab %d args %d\n", t
->tab_id
, args
->i
);
1170 if (t
->mode
== XT_MODE_HINT
) {
1171 if (args
->i
== XT_HINT_NEWTAB
)
1181 apply_style(struct tab
*t
)
1184 g_object_set(G_OBJECT(t
->settings
),
1185 "user-stylesheet-uri", t
->stylesheet
, (char *)NULL
);
1189 remove_style(struct tab
*t
)
1192 g_object_set(G_OBJECT(t
->settings
),
1193 "user-stylesheet-uri", NULL
, (char *)NULL
);
1197 userstyle_cmd(struct tab
*t
, struct karg
*args
)
1199 char script
[PATH_MAX
] = {'\0'};
1203 DNPRINTF(XT_D_JS
, "userstyle_cmd: tab %d\n", t
->tab_id
);
1205 if (args
->s
!= NULL
&& strlen(args
->s
)) {
1206 expand_tilde(script
, sizeof script
, args
->s
);
1207 script_uri
= g_filename_to_uri(script
, NULL
, NULL
);
1209 script_uri
= g_strdup(userstyle
);
1211 if (script_uri
== NULL
)
1215 case XT_STYLE_CURRENT_TAB
:
1216 if (t
->styled
&& !strcmp(script_uri
, t
->stylesheet
))
1220 g_free(t
->stylesheet
);
1221 t
->stylesheet
= g_strdup(script_uri
);
1225 case XT_STYLE_GLOBAL
:
1226 if (userstyle_global
&& !strcmp(script_uri
, t
->stylesheet
)) {
1227 userstyle_global
= 0;
1228 TAILQ_FOREACH(tt
, &tabs
, entry
)
1231 userstyle_global
= 1;
1233 /* need to save this stylesheet for new tabs */
1236 stylesheet
= g_strdup(script_uri
);
1238 TAILQ_FOREACH(tt
, &tabs
, entry
) {
1240 g_free(tt
->stylesheet
);
1241 tt
->stylesheet
= g_strdup(script_uri
);
1254 quit(struct tab
*t
, struct karg
*args
)
1256 if (save_global_history
)
1257 save_global_history_to_disk(t
);
1265 restore_sessions_list(void)
1268 struct dirent
*dp
= NULL
;
1272 sdir
= opendir(sessions_dir
);
1274 while ((dp
= readdir(sdir
)) != NULL
) {
1275 #if defined __MINGW32__
1276 reg
= 1; /* windows only has regular files */
1278 reg
= dp
->d_type
== DT_REG
;
1281 s
= g_malloc(sizeof(struct session
));
1282 s
->name
= g_strdup(dp
->d_name
);
1283 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
1291 open_tabs(struct tab
*t
, struct karg
*a
)
1293 char file
[PATH_MAX
];
1297 struct tab
*ti
, *tt
;
1302 ti
= TAILQ_LAST(&tabs
, tab_list
);
1304 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, a
->s
);
1305 if ((f
= fopen(file
, "r")) == NULL
)
1309 if ((uri
= fparseln(f
, NULL
, NULL
, "\0\0\0", 0)) == NULL
) {
1310 if (feof(f
) || ferror(f
))
1313 /* retrieve session name */
1314 if (g_str_has_prefix(uri
, XT_SAVE_SESSION_ID
)) {
1315 strlcpy(named_session
,
1316 &uri
[strlen(XT_SAVE_SESSION_ID
)],
1317 sizeof named_session
);
1322 create_new_tab(uri
, NULL
, 1, -1);
1328 /* close open tabs */
1329 if (a
->i
== XT_SES_CLOSETABS
&& ti
!= NULL
) {
1331 tt
= TAILQ_FIRST(&tabs
);
1352 restore_saved_tabs(void)
1354 char file
[PATH_MAX
];
1355 int unlink_file
= 0;
1360 snprintf(file
, sizeof file
, "%s" PS
"%s",
1361 sessions_dir
, XT_RESTART_TABS_FILE
);
1362 if (stat(file
, &sb
) == -1)
1363 a
.s
= XT_SAVED_TABS_FILE
;
1366 a
.s
= XT_RESTART_TABS_FILE
;
1369 a
.i
= XT_SES_DONOTHING
;
1370 rv
= open_tabs(NULL
, &a
);
1379 save_tabs(struct tab
*t
, struct karg
*a
)
1381 char file
[PATH_MAX
];
1383 int num_tabs
= 0, i
;
1384 struct tab
**stabs
= NULL
;
1386 /* tab may be null here */
1391 snprintf(file
, sizeof file
, "%s" PS
"%s",
1392 sessions_dir
, named_session
);
1394 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, a
->s
);
1396 if ((f
= fopen(file
, "w")) == NULL
) {
1397 show_oops(t
, "Can't open save_tabs file: %s", strerror(errno
));
1401 /* save session name */
1402 fprintf(f
, "%s%s\n", XT_SAVE_SESSION_ID
, named_session
);
1404 /* Save tabs, in the order they are arranged in the notebook. */
1405 num_tabs
= sort_tabs_by_page_num(&stabs
);
1407 for (i
= 0; i
< num_tabs
; i
++)
1409 if (get_uri(stabs
[i
]) != NULL
)
1410 fprintf(f
, "%s\n", get_uri(stabs
[i
]));
1411 else if (gtk_entry_get_text(GTK_ENTRY(
1412 stabs
[i
]->uri_entry
)))
1413 fprintf(f
, "%s\n", gtk_entry_get_text(GTK_ENTRY(
1414 stabs
[i
]->uri_entry
)));
1419 /* try and make sure this gets to disk NOW. XXX Backup first? */
1420 if (fflush(f
) != 0 || fsync(fileno(f
)) != 0) {
1421 show_oops(t
, "May not have managed to save session: %s",
1431 save_tabs_and_quit(struct tab
*t
, struct karg
*args
)
1443 expand_tilde(char *path
, size_t len
, const char *s
)
1447 char user
[LOGIN_NAME_MAX
];
1450 if (path
== NULL
|| s
== NULL
)
1451 errx(1, "expand_tilde");
1454 strlcpy(path
, sc
, len
);
1459 for (i
= 0; s
[i
] != PSC
&& s
[i
] != '\0'; ++i
)
1464 pwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1466 strlcpy(path
, sc
, len
);
1468 snprintf(path
, len
, "%s%s", pwd
->pw_dir
, s
);
1472 run_page_script(struct tab
*t
, struct karg
*args
)
1475 char *tmp
, script
[PATH_MAX
];
1478 tmp
= args
->s
!= NULL
&& strlen(args
->s
) > 0 ? args
->s
: default_script
;
1479 if (tmp
[0] == '\0') {
1480 show_oops(t
, "no script specified");
1484 if ((uri
= get_uri(t
)) == NULL
) {
1485 show_oops(t
, "tab is empty, not running script");
1489 expand_tilde(script
, sizeof script
, tmp
);
1492 sv
[1] = (char *)uri
;
1494 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
, NULL
,
1496 show_oops(t
, "%s: could not spawn process: %s %s", __func__
,
1500 show_oops(t
, "running: %s %s", sv
[0], sv
[1]);
1506 yank_uri(struct tab
*t
, struct karg
*args
)
1509 GtkClipboard
*clipboard
, *primary
;
1511 if ((uri
= get_uri(t
)) == NULL
)
1514 primary
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1515 clipboard
= gtk_clipboard_get(GDK_SELECTION_CLIPBOARD
);
1516 gtk_clipboard_set_text(primary
, uri
, -1);
1517 gtk_clipboard_set_text(clipboard
, uri
, -1);
1523 paste_uri(struct tab
*t
, struct karg
*args
)
1525 GtkClipboard
*clipboard
, *primary
;
1526 gchar
*c
= NULL
, *p
= NULL
, *uri
;
1529 clipboard
= gtk_clipboard_get(GDK_SELECTION_CLIPBOARD
);
1530 primary
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1531 c
= gtk_clipboard_wait_for_text(clipboard
);
1532 p
= gtk_clipboard_wait_for_text(primary
);
1536 /* Windows try clipboard first */
1539 /* UNIX try primary first */
1542 /* replace all newlines with spaces */
1543 for (i
= 0; uri
[i
] != '\0'; ++i
)
1547 while (*uri
&& isspace(*uri
))
1549 if (strlen(uri
) == 0) {
1550 show_oops(t
, "empty paste buffer");
1553 if (guess_search
== 0 && valid_url_type(uri
)) {
1554 /* we can be clever and paste this in search box */
1555 show_oops(t
, "not a valid URL");
1559 if (args
->i
== XT_PASTE_CURRENT_TAB
)
1561 else if (args
->i
== XT_PASTE_NEW_TAB
)
1562 create_new_tab(uri
, NULL
, 1, -1);
1575 js_toggle_cb(GtkWidget
*w
, struct tab
*t
)
1580 g_object_get(G_OBJECT(t
->settings
),
1581 "enable-scripts", &es
, (char *)NULL
);
1586 set
= XT_WL_DISABLE
;
1588 a
.i
= set
| XT_WL_TOPLEVEL
;
1591 a
.i
= set
| XT_WL_TOPLEVEL
;
1594 a
.i
= XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
;
1599 proxy_toggle_cb(GtkWidget
*w
, struct tab
*t
)
1601 struct karg args
= {0};
1603 args
.i
= XT_PRXY_TOGGLE
;
1604 proxy_cmd(t
, &args
);
1608 toggle_src(struct tab
*t
, struct karg
*args
)
1615 mode
= webkit_web_view_get_view_source_mode(t
->wv
);
1616 webkit_web_view_set_view_source_mode(t
->wv
, !mode
);
1617 webkit_web_view_reload(t
->wv
);
1623 focus_webview(struct tab
*t
)
1628 /* only grab focus if we are visible */
1629 if (gtk_notebook_get_current_page(notebook
) == t
->tab_id
)
1630 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
1634 focus(struct tab
*t
, struct karg
*args
)
1636 if (t
== NULL
|| args
== NULL
)
1642 if (args
->i
== XT_FOCUS_URI
)
1643 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
1644 else if (args
->i
== XT_FOCUS_SEARCH
)
1645 gtk_widget_grab_focus(GTK_WIDGET(t
->search_entry
));
1651 free_connection_certs(gnutls_x509_crt_t
*certs
, size_t cert_count
)
1655 for (i
= 0; i
< cert_count
; i
++)
1656 gnutls_x509_crt_deinit(certs
[i
]);
1660 #if GTK_CHECK_VERSION(3, 0, 0)
1662 statusbar_modify_attr(struct tab
*t
, const char *css_name
)
1664 gtk_widget_set_name(t
->sbe
.ebox
, css_name
);
1668 statusbar_modify_attr(struct tab
*t
, const char *text
, const char *base
)
1670 GdkColor c_text
, c_base
;
1672 gdk_color_parse(text
, &c_text
);
1673 gdk_color_parse(base
, &c_base
);
1675 gtk_widget_modify_bg(t
->sbe
.ebox
, GTK_STATE_NORMAL
, &c_base
);
1676 gtk_widget_modify_base(t
->sbe
.uri
, GTK_STATE_NORMAL
, &c_base
);
1677 gtk_widget_modify_text(t
->sbe
.uri
, GTK_STATE_NORMAL
, &c_text
);
1682 save_certs(struct tab
*t
, gnutls_x509_crt_t
*certs
,
1683 size_t cert_count
, const char *domain
, const char *dir
)
1686 char file
[PATH_MAX
];
1687 char *cert_buf
= NULL
;
1692 if (t
== NULL
|| certs
== NULL
|| cert_count
<= 0 || domain
== NULL
)
1695 snprintf(file
, sizeof file
, "%s" PS
"%s", dir
, domain
);
1696 if ((f
= fopen(file
, "w")) == NULL
) {
1697 show_oops(t
, "Can't create cert file %s %s",
1698 file
, strerror(errno
));
1702 for (i
= 0; i
< cert_count
; i
++) {
1704 * Because we support old crap and can't use
1705 * gnutls_x509_crt_export2(), we intentionally use an empty
1706 * buffer and then make a second call with the known size
1710 rv
= gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1711 cert_buf
, &cert_buf_sz
);
1712 if (rv
== GNUTLS_E_SHORT_MEMORY_BUFFER
) {
1713 cert_buf
= gnutls_malloc(cert_buf_sz
* sizeof(char));
1714 if (cert_buf
== NULL
) {
1715 show_oops(t
, "gnutls_x509_crt_export failed");
1718 rv
= gnutls_x509_crt_export(certs
[i
],
1719 GNUTLS_X509_FMT_PEM
, cert_buf
, &cert_buf_sz
);
1722 show_oops(t
, "gnutls_x509_crt_export failure: %s",
1723 gnutls_strerror(rv
));
1726 cert_buf
[cert_buf_sz
] = '\0';
1727 if (fwrite(cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1728 show_oops(t
, "Can't write certs: %s", strerror(errno
));
1731 gnutls_free(cert_buf
);
1737 gnutls_free(cert_buf
);
1749 get_local_cert_chain(const char *uri
, size_t *ncerts
, const char **error_str
,
1753 unsigned char cert_buf
[64 * 1024] = {0};
1754 gnutls_datum_t data
;
1755 unsigned int max_certs
= XT_MAX_CERTS
;
1757 char file
[PATH_MAX
];
1759 gnutls_x509_crt_t
*certs
;
1761 if ((su
= soup_uri_new(uri
)) == NULL
) {
1762 *error_str
= "Invalid URI";
1766 snprintf(file
, sizeof file
, "%s" PS
"%s", dir
, su
->host
);
1768 if ((f
= fopen(file
, "r")) == NULL
) {
1769 *error_str
= "Could not read local cert";
1773 bytes_read
= fread(cert_buf
, sizeof *cert_buf
, sizeof cert_buf
, f
);
1774 if (bytes_read
== 0) {
1775 *error_str
= "Could not read local cert";
1779 certs
= gnutls_malloc(sizeof(*certs
) * max_certs
);
1780 if (certs
== NULL
) {
1781 *error_str
= "Error allocating memory";
1784 data
.data
= cert_buf
;
1785 data
.size
= bytes_read
;
1786 if (gnutls_x509_crt_list_import(certs
, &max_certs
, &data
,
1787 GNUTLS_X509_FMT_PEM
, 0) < 0) {
1789 *error_str
= "Error reading local cert chain";
1793 *ncerts
= max_certs
;
1798 * This uses the pem-encoded cert chain saved in t->pem instead of
1799 * grabbing the remote cert. We save it beforehand and read it here
1800 * so as to not open a side channel that ignores proxy settings.
1803 get_chain_for_pem(char *pem
, size_t *ncerts
, const char **error_str
)
1805 gnutls_datum_t data
;
1806 unsigned int max_certs
= XT_MAX_CERTS
;
1807 gnutls_x509_crt_t
*certs
;
1810 *error_str
= "Error reading remote cert chain";
1814 certs
= gnutls_malloc(sizeof(*certs
) * max_certs
);
1815 if (certs
== NULL
) {
1816 *error_str
= "Error allocating memory";
1819 data
.data
= (unsigned char *)pem
;
1820 data
.size
= strlen(pem
);
1821 if (gnutls_x509_crt_list_import(certs
, &max_certs
, &data
,
1822 GNUTLS_X509_FMT_PEM
, 0) < 0) {
1824 *error_str
= "Error reading remote cert chain";
1828 *ncerts
= max_certs
;
1834 cert_cmd(struct tab
*t
, struct karg
*args
)
1836 const gchar
*uri
, *error_str
= NULL
;
1838 gnutls_x509_crt_t
*certs
;
1841 #if !GTK_CHECK_VERSION(3, 0, 0)
1848 if (args
->s
!= NULL
) {
1850 } else if ((uri
= get_uri(t
)) == NULL
) {
1851 show_oops(t
, "Invalid URI");
1854 if ((su
= soup_uri_new(uri
)) == NULL
) {
1855 show_oops(t
, "Invalid URI");
1860 * if we're only showing the local certs, don't open a socket and get
1863 if (args
->i
& XT_SHOW
&& args
->i
& XT_CACHE
) {
1864 certs
= get_local_cert_chain(uri
, &cert_count
, &error_str
,
1866 if (error_str
== NULL
) {
1867 t
->about_cert_host
= g_strdup(su
->host
);
1868 show_certs(t
, certs
, cert_count
, "Certificate Chain");
1869 free_connection_certs(certs
, cert_count
);
1871 show_oops(t
, "%s", error_str
);
1879 certs
= get_chain_for_pem(t
->pem
, &cert_count
, &error_str
);
1883 if (args
->i
& XT_SHOW
) {
1884 t
->about_cert_host
= g_strdup(su
->host
);
1885 show_certs(t
, certs
, cert_count
, "Certificate Chain");
1886 } else if (args
->i
& XT_SAVE
) {
1887 host
= t
->about_cert_host
? t
->about_cert_host
: su
->host
;
1888 save_certs(t
, certs
, cert_count
, host
, certs_dir
);
1889 #if GTK_CHECK_VERSION(3, 0, 0)
1890 gtk_widget_set_name(t
->uri_entry
, XT_CSS_BLUE
);
1891 statusbar_modify_attr(t
, XT_CSS_BLUE
);
1893 gdk_color_parse(XT_COLOR_BLUE
, &color
);
1894 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
1895 statusbar_modify_attr(t
, XT_COLOR_BLACK
, XT_COLOR_BLUE
);
1897 } else if (args
->i
& XT_CACHE
)
1898 save_certs(t
, certs
, cert_count
, su
->host
, certs_cache_dir
);
1900 free_connection_certs(certs
, cert_count
);
1905 if (error_str
&& strlen(error_str
))
1906 show_oops(t
, "%s", error_str
);
1911 * Checks whether the remote cert is identical to the local saved
1912 * cert. Returns CERT_LOCAL if unchanged, CERT_UNTRUSTED if local
1913 * cert does not exist, and CERT_BAD if different.
1915 * Saves entire cert chain in pem encoding to chain for it to be
1916 * cached later, if needed.
1919 check_local_certs(const char *file
, GTlsCertificate
*cert
, char **chain
)
1921 char r_cert_buf
[64 * 1024];
1923 GTlsCertificate
*tmpcert
= NULL
;
1924 GTlsCertificate
*issuer
= NULL
;
1927 enum cert_trust rv
= CERT_LOCAL
;
1930 if ((f
= fopen(file
, "r")) == NULL
) {
1931 /* no local cert to check */
1932 rv
= CERT_UNTRUSTED
;
1937 g_object_get(G_OBJECT(cert
), "certificate-pem", &pem
,
1939 g_object_get(G_OBJECT(cert
), "issuer", &issuer
, NULL
);
1943 g_object_get(G_OBJECT(tmpcert
), "issuer", &issuer
,
1948 g_object_get(G_OBJECT(tmpcert
), "certificate-pem", &pem
,
1953 if (tmpcert
!= NULL
)
1954 g_object_unref(G_OBJECT(tmpcert
));
1957 if (fread(r_cert_buf
, strlen(pem
), 1, f
) != 1 && !feof(f
))
1959 if (bcmp(r_cert_buf
, pem
, strlen(pem
)))
1962 tmp
= g_strdup_printf("%s%s", *chain
, pem
);
1969 g_object_unref(G_OBJECT(issuer
));
1976 check_cert_changes(struct tab
*t
, GTlsCertificate
*cert
, const char *file
, const char *uri
)
1978 SoupURI
*soupuri
= NULL
;
1979 struct karg args
= {0};
1980 struct wl_entry
*w
= NULL
;
1984 chain
= g_strdup("");
1985 switch(check_local_certs(file
, cert
, &chain
)) {
1987 /* The cached certificate is identical */
1989 case CERT_TRUSTED
: /* FALLTHROUGH */
1990 case CERT_UNTRUSTED
:
1991 /* cache new certificate */
1996 if ((soupuri
= soup_uri_new(uri
)) == NULL
||
1997 soupuri
->host
== NULL
)
1999 if ((w
= wl_find(soupuri
->host
, &svil
)) != NULL
)
2001 t
->xtp_meaning
= XT_XTP_TAB_MEANING_SV
;
2002 args
.s
= g_strdup((char *)uri
);
2003 xtp_page_sv(t
, &args
);
2009 soup_uri_free(soupuri
);
2016 remove_cookie(int index
)
2022 DNPRINTF(XT_D_COOKIE
, "remove_cookie: %d\n", index
);
2024 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
2026 for (i
= 1; cf
; cf
= cf
->next
, i
++) {
2030 print_cookie("remove cookie", c
);
2031 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2036 soup_cookies_free(cf
);
2042 remove_cookie_domain(int domain_id
)
2044 int domain_count
, rv
= 1;
2049 DNPRINTF(XT_D_COOKIE
, "remove_cookie_domain: %d\n", domain_id
);
2052 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
2054 for (domain_count
= 0; cf
; cf
= cf
->next
) {
2057 if (strcmp(last_domain
, c
->domain
) != 0) {
2059 last_domain
= c
->domain
;
2062 if (domain_count
< domain_id
)
2064 else if (domain_count
> domain_id
)
2067 print_cookie("remove cookie", c
);
2068 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2072 soup_cookies_free(cf
);
2084 DNPRINTF(XT_D_COOKIE
, "remove_cookie_all\n");
2086 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
2088 for (; cf
; cf
= cf
->next
) {
2091 print_cookie("remove cookie", c
);
2092 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2096 soup_cookies_free(cf
);
2102 toplevel_cmd(struct tab
*t
, struct karg
*args
)
2104 js_toggle_cb(t
->js_toggle
, t
);
2110 can_go_back_for_real(struct tab
*t
)
2113 WebKitWebHistoryItem
*item
;
2119 if (t
->item
!= NULL
)
2122 /* rely on webkit to make sure we can go backward when on an about page */
2124 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:") ||
2125 g_str_has_prefix(uri
, "xxxt://"))
2126 return (webkit_web_view_can_go_back(t
->wv
));
2128 /* the back/forward list is stupid so help determine if we can go back */
2129 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2131 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2132 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
2140 can_go_forward_for_real(struct tab
*t
)
2143 WebKitWebHistoryItem
*item
;
2149 /* rely on webkit to make sure we can go forward when on an about page */
2151 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:") ||
2152 g_str_has_prefix(uri
, "xxxt://"))
2153 return (webkit_web_view_can_go_forward(t
->wv
));
2155 /* the back/forwars list is stupid so help selecting a different item */
2156 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2158 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2159 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
2167 go_back_for_real(struct tab
*t
)
2170 WebKitWebHistoryItem
*item
;
2177 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:") ||
2178 g_str_has_prefix(uri
, "xxxt://")) {
2179 webkit_web_view_go_back(t
->wv
);
2182 /* the back/forwars list is stupid so help selecting a different item */
2183 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2185 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2186 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2187 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2194 go_forward_for_real(struct tab
*t
)
2197 WebKitWebHistoryItem
*item
;
2204 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:") ||
2205 g_str_has_prefix(uri
, "xxxt://")) {
2206 webkit_web_view_go_forward(t
->wv
);
2209 /* the back/forwars list is stupid so help selecting a different item */
2210 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2212 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2213 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2214 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2221 navaction(struct tab
*t
, struct karg
*args
)
2223 WebKitWebHistoryItem
*item
= NULL
;
2224 WebKitWebFrame
*frame
;
2226 DNPRINTF(XT_D_NAV
, "navaction: tab %d opcode %d\n",
2227 t
->tab_id
, args
->i
);
2230 set_normal_tab_meaning(t
);
2232 if (args
->i
== XT_NAV_BACK
)
2233 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2235 item
= webkit_web_back_forward_list_get_forward_item(t
->bfl
);
2242 return (XT_CB_PASSTHROUGH
);
2243 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2245 return (XT_CB_PASSTHROUGH
);
2248 go_back_for_real(t
);
2250 case XT_NAV_FORWARD
:
2253 return (XT_CB_PASSTHROUGH
);
2254 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2256 return (XT_CB_PASSTHROUGH
);
2259 go_forward_for_real(t
);
2262 frame
= webkit_web_view_get_main_frame(t
->wv
);
2263 webkit_web_frame_reload(frame
);
2266 frame
= webkit_web_view_get_main_frame(t
->wv
);
2267 webkit_web_frame_stop_loading(frame
);
2270 return (XT_CB_PASSTHROUGH
);
2274 move(struct tab
*t
, struct karg
*args
)
2276 GtkAdjustment
*adjust
;
2277 double pi
, si
, pos
, ps
, upper
, lower
, max
;
2283 case XT_MOVE_BOTTOM
:
2285 case XT_MOVE_PAGEDOWN
:
2286 case XT_MOVE_PAGEUP
:
2287 case XT_MOVE_HALFDOWN
:
2288 case XT_MOVE_HALFUP
:
2289 case XT_MOVE_PERCENT
:
2290 case XT_MOVE_CENTER
:
2291 adjust
= t
->adjust_v
;
2294 adjust
= t
->adjust_h
;
2298 pos
= gtk_adjustment_get_value(adjust
);
2299 ps
= gtk_adjustment_get_page_size(adjust
);
2300 upper
= gtk_adjustment_get_upper(adjust
);
2301 lower
= gtk_adjustment_get_lower(adjust
);
2302 si
= gtk_adjustment_get_step_increment(adjust
);
2303 pi
= gtk_adjustment_get_page_increment(adjust
);
2306 DNPRINTF(XT_D_MOVE
, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2307 "max %f si %f pi %f\n",
2308 args
->i
, adjust
== t
->adjust_h
? "horizontal" : "vertical",
2309 pos
, ps
, upper
, lower
, max
, si
, pi
);
2315 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2320 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2322 case XT_MOVE_BOTTOM
:
2323 case XT_MOVE_FARRIGHT
:
2324 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2325 gtk_adjustment_set_value(adjust
, max
);
2328 case XT_MOVE_FARLEFT
:
2329 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2330 gtk_adjustment_set_value(adjust
, lower
);
2332 case XT_MOVE_PAGEDOWN
:
2334 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2336 case XT_MOVE_PAGEUP
:
2338 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2340 case XT_MOVE_HALFDOWN
:
2342 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2344 case XT_MOVE_HALFUP
:
2346 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2348 case XT_MOVE_CENTER
:
2349 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2350 args
->s
= g_strdup("50.0");
2352 case XT_MOVE_PERCENT
:
2353 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2354 percent
= atoi(args
->s
) / 100.0;
2355 pos
= max
* percent
;
2356 if (pos
< 0.0 || pos
> max
)
2358 gtk_adjustment_set_value(adjust
, pos
);
2361 return (XT_CB_PASSTHROUGH
);
2364 DNPRINTF(XT_D_MOVE
, "move: new pos %f %f\n", pos
, MIN(pos
, max
));
2366 return (XT_CB_HANDLED
);
2370 url_set_visibility(void)
2374 TAILQ_FOREACH(t
, &tabs
, entry
)
2375 if (show_url
== 0) {
2376 gtk_widget_hide(t
->toolbar
);
2379 gtk_widget_show(t
->toolbar
);
2383 notebook_tab_set_visibility(void)
2385 if (show_tabs
== 0) {
2386 gtk_widget_hide(tab_bar
);
2387 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2389 if (tab_style
== XT_TABS_NORMAL
) {
2390 gtk_widget_hide(tab_bar
);
2391 gtk_notebook_set_show_tabs(notebook
, TRUE
);
2392 } else if (tab_style
== XT_TABS_COMPACT
) {
2393 gtk_widget_show(tab_bar
);
2394 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2400 statusbar_set_visibility(void)
2404 TAILQ_FOREACH(t
, &tabs
, entry
){
2405 if (show_statusbar
== 0)
2406 gtk_widget_hide(t
->statusbar
);
2408 gtk_widget_show(t
->statusbar
);
2415 url_set(struct tab
*t
, int enable_url_entry
)
2420 show_url
= enable_url_entry
;
2422 if (enable_url_entry
) {
2423 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.uri
),
2424 GTK_ENTRY_ICON_PRIMARY
, NULL
);
2425 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.uri
), 0);
2427 pixbuf
= gtk_entry_get_icon_pixbuf(GTK_ENTRY(t
->uri_entry
),
2428 GTK_ENTRY_ICON_PRIMARY
);
2430 gtk_entry_get_progress_fraction(GTK_ENTRY(t
->uri_entry
));
2431 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.uri
),
2432 GTK_ENTRY_ICON_PRIMARY
, pixbuf
);
2433 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.uri
),
2439 fullscreen(struct tab
*t
, struct karg
*args
)
2441 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2444 return (XT_CB_PASSTHROUGH
);
2446 if (show_url
== 0) {
2454 url_set_visibility();
2455 notebook_tab_set_visibility();
2457 return (XT_CB_HANDLED
);
2461 statustoggle(struct tab
*t
, struct karg
*args
)
2463 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2465 if (show_statusbar
== 1) {
2467 statusbar_set_visibility();
2468 } else if (show_statusbar
== 0) {
2470 statusbar_set_visibility();
2472 return (XT_CB_HANDLED
);
2476 urlaction(struct tab
*t
, struct karg
*args
)
2478 int rv
= XT_CB_HANDLED
;
2480 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2483 return (XT_CB_PASSTHROUGH
);
2487 if (show_url
== 0) {
2489 url_set_visibility();
2493 if (show_url
== 1) {
2495 url_set_visibility();
2503 tabaction(struct tab
*t
, struct karg
*args
)
2505 int rv
= XT_CB_HANDLED
;
2506 char *url
= args
->s
;
2508 struct tab
*tt
, *tv
;
2510 DNPRINTF(XT_D_TAB
, "tabaction: %p %d\n", t
, args
->i
);
2513 return (XT_CB_PASSTHROUGH
);
2517 if (strlen(url
) > 0)
2518 create_new_tab(url
, NULL
, 1, args
->precount
);
2520 create_new_tab(NULL
, NULL
, 1, args
->precount
);
2523 if (args
->precount
< 0)
2526 TAILQ_FOREACH(tt
, &tabs
, entry
)
2527 if (tt
->tab_id
== args
->precount
- 1) {
2532 case XT_TAB_DELQUIT
:
2533 if (gtk_notebook_get_n_pages(notebook
) > 1)
2539 TAILQ_FOREACH_SAFE(tt
, &tabs
, entry
, tv
)
2544 if (strlen(url
) > 0)
2547 rv
= XT_CB_PASSTHROUGH
;
2553 if (show_tabs
== 0) {
2555 notebook_tab_set_visibility();
2559 if (show_tabs
== 1) {
2561 notebook_tab_set_visibility();
2564 case XT_TAB_NEXTSTYLE
:
2565 if (tab_style
== XT_TABS_NORMAL
) {
2566 tab_style
= XT_TABS_COMPACT
;
2567 recolor_compact_tabs();
2570 tab_style
= XT_TABS_NORMAL
;
2571 notebook_tab_set_visibility();
2573 case XT_TAB_UNDO_CLOSE
:
2574 if (undo_count
== 0) {
2575 DNPRINTF(XT_D_TAB
, "%s: no tabs to undo close",
2580 u
= TAILQ_FIRST(&undos
);
2581 create_new_tab(u
->uri
, u
, 1, -1);
2583 TAILQ_REMOVE(&undos
, u
, entry
);
2585 /* u->history is freed in create_new_tab() */
2589 case XT_TAB_LOAD_IMAGES
:
2591 if (!auto_load_images
) {
2593 /* Enable auto-load images (this will load all
2594 * previously unloaded images). */
2595 g_object_set(G_OBJECT(t
->settings
),
2596 "auto-load-images", TRUE
, (char *)NULL
);
2597 webkit_web_view_set_settings(t
->wv
, t
->settings
);
2599 webkit_web_view_reload(t
->wv
);
2601 /* Webkit triggers an event when we change the setting,
2602 * so we can't disable the auto-loading at once.
2604 * Unfortunately, webkit does not tell us when it's done.
2605 * Instead, we wait until the next request, and then
2606 * disable autoloading again.
2608 t
->load_images
= TRUE
;
2612 rv
= XT_CB_PASSTHROUGH
;
2626 resizetab(struct tab
*t
, struct karg
*args
)
2628 if (t
== NULL
|| args
== NULL
) {
2629 show_oops(NULL
, "resizetab invalid parameters");
2630 return (XT_CB_PASSTHROUGH
);
2633 DNPRINTF(XT_D_TAB
, "resizetab: tab %d %d\n",
2634 t
->tab_id
, args
->i
);
2636 setzoom_webkit(t
, args
->i
);
2638 return (XT_CB_HANDLED
);
2642 movetab(struct tab
*t
, struct karg
*args
)
2646 if (t
== NULL
|| args
== NULL
) {
2647 show_oops(NULL
, "movetab invalid parameters");
2648 return (XT_CB_PASSTHROUGH
);
2651 DNPRINTF(XT_D_TAB
, "movetab: tab %d opcode %d\n",
2652 t
->tab_id
, args
->i
);
2654 if (args
->i
>= XT_TAB_INVALID
)
2655 return (XT_CB_PASSTHROUGH
);
2657 if (TAILQ_EMPTY(&tabs
))
2658 return (XT_CB_PASSTHROUGH
);
2660 n
= gtk_notebook_get_n_pages(notebook
);
2661 dest
= gtk_notebook_get_current_page(notebook
);
2665 if (args
->precount
< 0)
2666 dest
= dest
== n
- 1 ? 0 : dest
+ 1;
2668 dest
= args
->precount
- 1;
2672 if (args
->precount
< 0)
2675 dest
-= args
->precount
% n
;
2688 return (XT_CB_PASSTHROUGH
);
2691 if (dest
< 0 || dest
>= n
)
2692 return (XT_CB_PASSTHROUGH
);
2693 if (t
->tab_id
== dest
) {
2694 DNPRINTF(XT_D_TAB
, "movetab: do nothing\n");
2695 return (XT_CB_HANDLED
);
2698 set_current_tab(dest
);
2700 return (XT_CB_HANDLED
);
2707 const char *(*f
)(struct tab
*);
2709 { "<uri>", get_uri
},
2713 command(struct tab
*t
, struct karg
*args
)
2715 struct karg a
= {0};
2716 int i
, cmd_setup
= 0;
2717 char *s
= NULL
, *sp
= NULL
, *sl
= NULL
;
2720 if (t
== NULL
|| args
== NULL
) {
2721 show_oops(NULL
, "command invalid parameters");
2722 return (XT_CB_PASSTHROUGH
);
2733 if (cmd_prefix
== 0) {
2734 if (args
->s
!= NULL
&& strlen(args
->s
) != 0) {
2735 sp
= g_strdup_printf(":%s", args
->s
);
2740 sp
= g_strdup_printf(":%d", cmd_prefix
);
2750 for (i
= 0; i
< LENGTH(subs
); ++i
) {
2751 sv
= g_strsplit(sl
, subs
[i
].s
, -1);
2754 sl
= g_strjoinv(subs
[i
].f(t
), sv
);
2760 t
->mode
= XT_MODE_HINT
;
2764 * js code will auto fire() if a single link is visible,
2765 * causing the focus-out-event cb function to be called. Setup
2766 * the cmd _before_ triggering hinting code so the cmd can get
2767 * killed by the cb in this case.
2774 t
->mode
= XT_MODE_HINT
;
2775 a
.i
= XT_HINT_NEWTAB
;
2782 show_oops(t
, "command: invalid opcode %d", args
->i
);
2783 return (XT_CB_PASSTHROUGH
);
2786 DNPRINTF(XT_D_CMD
, "%s: tab %d type %s\n", __func__
, t
->tab_id
, s
);
2796 return (XT_CB_HANDLED
);
2800 search(struct tab
*t
, struct karg
*args
)
2804 if (t
== NULL
|| args
== NULL
) {
2805 show_oops(NULL
, "search invalid parameters");
2810 case XT_SEARCH_NEXT
:
2811 d
= t
->search_forward
;
2813 case XT_SEARCH_PREV
:
2814 d
= !t
->search_forward
;
2817 return (XT_CB_PASSTHROUGH
);
2820 if (t
->search_text
== NULL
) {
2821 if (global_search
== NULL
)
2822 return (XT_CB_PASSTHROUGH
);
2824 d
= t
->search_forward
= TRUE
;
2825 t
->search_text
= g_strdup(global_search
);
2826 webkit_web_view_mark_text_matches(t
->wv
, global_search
, FALSE
, 0);
2827 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
2831 DNPRINTF(XT_D_CMD
, "search: tab %d opc %d forw %d text %s\n",
2832 t
->tab_id
, args
->i
, t
->search_forward
, t
->search_text
);
2834 webkit_web_view_search_text(t
->wv
, t
->search_text
, FALSE
, d
, TRUE
);
2836 return (XT_CB_HANDLED
);
2840 session_save(struct tab
*t
, char *filename
)
2846 if (strlen(filename
) == 0)
2849 if (filename
[0] == '.' || filename
[0] == '/')
2853 if (save_tabs(t
, &a
))
2855 strlcpy(named_session
, filename
, sizeof named_session
);
2857 /* add the new session to the list of sessions */
2858 s
= g_malloc(sizeof(struct session
));
2859 s
->name
= g_strdup(filename
);
2860 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
2868 session_open(struct tab
*t
, char *filename
)
2873 if (strlen(filename
) == 0)
2876 if (filename
[0] == '.' || filename
[0] == '/')
2880 a
.i
= XT_SES_CLOSETABS
;
2881 if (open_tabs(t
, &a
))
2884 strlcpy(named_session
, filename
, sizeof named_session
);
2892 session_delete(struct tab
*t
, char *filename
)
2894 char file
[PATH_MAX
];
2898 if (strlen(filename
) == 0)
2901 if (filename
[0] == '.' || filename
[0] == '/')
2904 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, filename
);
2908 if (!strcmp(filename
, named_session
))
2909 strlcpy(named_session
, XT_SAVED_TABS_FILE
,
2910 sizeof named_session
);
2912 /* remove session from sessions list */
2913 TAILQ_FOREACH(s
, &sessions
, entry
) {
2914 if (!strcmp(s
->name
, filename
))
2919 TAILQ_REMOVE(&sessions
, s
, entry
);
2920 g_free((gpointer
) s
->name
);
2929 session_cmd(struct tab
*t
, struct karg
*args
)
2931 char *filename
= args
->s
;
2936 if (args
->i
& XT_SHOW
)
2937 show_oops(t
, "Current session: %s", named_session
[0] == '\0' ?
2938 XT_SAVED_TABS_FILE
: named_session
);
2939 else if (args
->i
& XT_SAVE
) {
2940 if (session_save(t
, filename
)) {
2941 show_oops(t
, "Can't save session: %s",
2942 filename
? filename
: "INVALID");
2945 } else if (args
->i
& XT_OPEN
) {
2946 if (session_open(t
, filename
)) {
2947 show_oops(t
, "Can't open session: %s",
2948 filename
? filename
: "INVALID");
2951 } else if (args
->i
& XT_DELETE
) {
2952 if (session_delete(t
, filename
)) {
2953 show_oops(t
, "Can't delete session: %s",
2954 filename
? filename
: "INVALID");
2959 return (XT_CB_PASSTHROUGH
);
2963 script_cmd(struct tab
*t
, struct karg
*args
)
2972 if ((f
= fopen(args
->s
, "r")) == NULL
) {
2973 show_oops(t
, "Can't open script file: %s", args
->s
);
2977 if (fstat(fileno(f
), &sb
) == -1) {
2978 show_oops(t
, "Can't stat script file: %s", args
->s
);
2982 buf
= g_malloc0(sb
.st_size
+ 1);
2983 if (fread(buf
, 1, sb
.st_size
, f
) != sb
.st_size
) {
2984 show_oops(t
, "Can't read script file: %s", args
->s
);
2988 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
2997 return (XT_CB_PASSTHROUGH
);
3001 * Make a hardcopy of the page
3004 print_page(struct tab
*t
, struct karg
*args
)
3006 WebKitWebFrame
*frame
;
3008 GtkPrintOperation
*op
;
3009 GtkPrintOperationAction action
;
3010 GtkPrintOperationResult print_res
;
3011 GError
*g_err
= NULL
;
3012 int marg_l
, marg_r
, marg_t
, marg_b
;
3015 DNPRINTF(XT_D_PRINTING
, "%s:", __func__
);
3017 ps
= gtk_page_setup_new();
3018 op
= gtk_print_operation_new();
3019 action
= GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
;
3020 frame
= webkit_web_view_get_main_frame(t
->wv
);
3022 /* the default margins are too small, so we will bump them */
3023 marg_l
= gtk_page_setup_get_left_margin(ps
, GTK_UNIT_MM
) +
3024 XT_PRINT_EXTRA_MARGIN
;
3025 marg_r
= gtk_page_setup_get_right_margin(ps
, GTK_UNIT_MM
) +
3026 XT_PRINT_EXTRA_MARGIN
;
3027 marg_t
= gtk_page_setup_get_top_margin(ps
, GTK_UNIT_MM
) +
3028 XT_PRINT_EXTRA_MARGIN
;
3029 marg_b
= gtk_page_setup_get_bottom_margin(ps
, GTK_UNIT_MM
) +
3030 XT_PRINT_EXTRA_MARGIN
;
3033 gtk_page_setup_set_left_margin(ps
, marg_l
, GTK_UNIT_MM
);
3034 gtk_page_setup_set_right_margin(ps
, marg_r
, GTK_UNIT_MM
);
3035 gtk_page_setup_set_top_margin(ps
, marg_t
, GTK_UNIT_MM
);
3036 gtk_page_setup_set_bottom_margin(ps
, marg_b
, GTK_UNIT_MM
);
3038 gtk_print_operation_set_default_page_setup(op
, ps
);
3040 print_res
= webkit_web_frame_print_full(frame
, op
, action
, &g_err
);
3042 /* check it worked */
3043 if (print_res
== GTK_PRINT_OPERATION_RESULT_ERROR
) {
3044 show_oops(NULL
, "can't print: %s", g_err
->message
);
3045 g_error_free (g_err
);
3049 g_object_unref(G_OBJECT(ps
));
3050 g_object_unref(G_OBJECT(op
));
3055 go_home(struct tab
*t
, struct karg
*args
)
3062 set_encoding(struct tab
*t
, struct karg
*args
)
3066 if (args
->s
&& strlen(g_strstrip(args
->s
)) == 0) {
3067 e
= webkit_web_view_get_custom_encoding(t
->wv
);
3069 e
= webkit_web_view_get_encoding(t
->wv
);
3070 show_oops(t
, "encoding: %s", e
? e
: "N/A");
3072 webkit_web_view_set_custom_encoding(t
->wv
, args
->s
);
3078 restart(struct tab
*t
, struct karg
*args
)
3082 a
.s
= XT_RESTART_TABS_FILE
;
3084 execvp(start_argv
[0], start_argv
);
3090 char *http_proxy_save
; /* not a setting, used to toggle */
3093 proxy_cmd(struct tab
*t
, struct karg
*args
)
3097 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
3104 TAILQ_FOREACH(tt
, &tabs
, entry
)
3105 gtk_widget_show(t
->proxy_toggle
);
3106 if (http_proxy_save
)
3107 g_free(http_proxy_save
);
3108 http_proxy_save
= g_strdup(http_proxy
);
3111 if (args
->i
& XT_PRXY_SHOW
) {
3113 show_oops(t
, "http_proxy = %s", http_proxy
);
3115 show_oops(t
, "proxy is currently disabled");
3116 } else if (args
->i
& XT_PRXY_TOGGLE
) {
3117 if (http_proxy_save
== NULL
&& http_proxy
== NULL
) {
3118 show_oops(t
, "can't toggle proxy");
3121 TAILQ_FOREACH(tt
, &tabs
, entry
)
3122 gtk_widget_show(t
->proxy_toggle
);
3125 button_set_file(t
->proxy_toggle
, "tordisabled.ico");
3126 show_oops(t
, "http proxy disabled");
3128 setup_proxy(http_proxy_save
);
3129 button_set_file(t
->proxy_toggle
, "torenabled.ico");
3130 show_oops(t
, "http_proxy = %s", http_proxy
);
3134 return (XT_CB_PASSTHROUGH
);
3138 * If you can read this functionthen you are a sick and twisted individual.
3139 * I hope we never meet, it'll be violent.
3142 eval_cb(const GMatchInfo
*info
, GString
*res
, gpointer data
)
3145 gint start
= -1, end
= -1, i
;
3146 struct karg
*args
= data
;
3149 * match contains the string UP TO the match.
3151 * res is what is returned, note that whatever remains in the sent in
3152 * string is appended on the way out.
3154 * for example /123/456/789/moo came in
3155 * match contains /123/456/789/
3156 * we assign that to res and replace /789/ with the replacement text
3157 * then g_regex_replace_eval on the way out has /123/456/replacement/moo
3160 match
= g_match_info_fetch(info
, 0);
3164 if (g_match_info_fetch_pos(info
, 1, &start
, &end
) == FALSE
)
3167 g_string_assign(res
, match
);
3169 i
= atoi(&match
[start
+ 1]);
3170 if (args
->i
== XT_URL_PLUS
)
3175 /* preserve whitespace when likely */
3176 num
= g_strdup_printf("%0*d", end
- start
- 2, i
);
3177 g_string_overwrite_len(res
, start
+ 1, num
, end
- start
- 2);
3183 return (FALSE
); /* doesn't matter */
3187 urlmod_cmd(struct tab
*t
, struct karg
*args
)
3195 if ((uri
= gtk_entry_get_text(GTK_ENTRY(t
->uri_entry
))) == NULL
)
3197 if (strlen(uri
) == 0)
3200 reg
= g_regex_new(".*(/[0-9]+/)", 0, 0, NULL
);
3203 res
= g_regex_replace_eval(reg
, uri
, -1, 0, 0, eval_cb
, args
, NULL
);
3207 if (!strcmp(res
, uri
))
3210 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), res
);
3211 activate_uri_entry_cb(t
->uri_entry
, t
);
3218 return (XT_CB_PASSTHROUGH
);
3224 int (*func
)(struct tab
*, struct karg
*);
3228 { "command_mode", 0, command_mode
, XT_MODE_COMMAND
, 0 },
3229 { "insert_mode", 0, command_mode
, XT_MODE_INSERT
, 0 },
3230 { "command", 0, command
, ':', 0 },
3231 { "search", 0, command
, '/', 0 },
3232 { "searchb", 0, command
, '?', 0 },
3233 { "hinting", 0, command
, '.', 0 },
3234 { "hinting_newtab", 0, command
, ',', 0 },
3235 { "togglesrc", 0, toggle_src
, 0, 0 },
3236 { "editsrc", 0, edit_src
, 0, 0 },
3237 { "editelement", 0, edit_element
, 0, 0 },
3238 { "passthrough", 0, passthrough
, 0, 0 },
3239 { "modurl", 0, modurl
, 0, 0 },
3241 /* yanking and pasting */
3242 { "yankuri", 0, yank_uri
, 0, 0 },
3243 { "pasteuricur", 0, paste_uri
, XT_PASTE_CURRENT_TAB
, 0 },
3244 { "pasteurinew", 0, paste_uri
, XT_PASTE_NEW_TAB
, 0 },
3247 { "searchnext", 0, search
, XT_SEARCH_NEXT
, 0 },
3248 { "searchprevious", 0, search
, XT_SEARCH_PREV
, 0 },
3251 { "focusaddress", 0, focus
, XT_FOCUS_URI
, 0 },
3252 { "focussearch", 0, focus
, XT_FOCUS_SEARCH
, 0 },
3255 { "hinting", 0, hint
, 0, 0 },
3256 { "hinting_newtab", 0, hint
, XT_HINT_NEWTAB
, 0 },
3258 /* custom stylesheet */
3259 { "userstyle", 0, userstyle_cmd
, XT_STYLE_CURRENT_TAB
, XT_USERARG
},
3260 { "userstyle_global", 0, userstyle_cmd
, XT_STYLE_GLOBAL
, XT_USERARG
},
3263 { "goback", 0, navaction
, XT_NAV_BACK
, 0 },
3264 { "goforward", 0, navaction
, XT_NAV_FORWARD
, 0 },
3265 { "reload", 0, navaction
, XT_NAV_RELOAD
, 0 },
3266 { "stop", 0, navaction
, XT_NAV_STOP
, 0 },
3268 /* vertical movement */
3269 { "scrolldown", 0, move
, XT_MOVE_DOWN
, 0 },
3270 { "scrollup", 0, move
, XT_MOVE_UP
, 0 },
3271 { "scrollbottom", 0, move
, XT_MOVE_BOTTOM
, 0 },
3272 { "scrolltop", 0, move
, XT_MOVE_TOP
, 0 },
3273 { "1", 0, move
, XT_MOVE_TOP
, 0 },
3274 { "scrollhalfdown", 0, move
, XT_MOVE_HALFDOWN
, 0 },
3275 { "scrollhalfup", 0, move
, XT_MOVE_HALFUP
, 0 },
3276 { "scrollpagedown", 0, move
, XT_MOVE_PAGEDOWN
, 0 },
3277 { "scrollpageup", 0, move
, XT_MOVE_PAGEUP
, 0 },
3278 /* horizontal movement */
3279 { "scrollright", 0, move
, XT_MOVE_RIGHT
, 0 },
3280 { "scrollleft", 0, move
, XT_MOVE_LEFT
, 0 },
3281 { "scrollfarright", 0, move
, XT_MOVE_FARRIGHT
, 0 },
3282 { "scrollfarleft", 0, move
, XT_MOVE_FARLEFT
, 0 },
3284 { "favorites", 0, xtp_page_fl
, XT_SHOW
, 0 },
3285 { "fav", 0, xtp_page_fl
, XT_SHOW
, 0 },
3286 { "favedit", 0, xtp_page_fl
, XT_SHOW
|XT_DELETE
, 0 },
3287 { "favadd", 0, add_favorite
, 0, 0 },
3289 { "qall", 0, quit
, 0, 0 },
3290 { "quitall", 0, quit
, 0, 0 },
3291 { "w", 0, save_tabs
, 0, 0 },
3292 { "wq", 0, save_tabs_and_quit
, 0, 0 },
3293 { "help", 0, help
, 0, 0 },
3294 { "about", 0, xtp_page_ab
, 0, 0 },
3295 { "stats", 0, stats
, 0, 0 },
3296 { "version", 0, xtp_page_ab
, 0, 0 },
3299 { "js", 0, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3300 { "save", 1, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3301 { "domain", 2, js_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3302 { "fqdn", 2, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3303 { "show", 1, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3304 { "all", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3305 { "persistent", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3306 { "session", 2, js_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3307 { "toggle", 1, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3308 { "domain", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3309 { "fqdn", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3311 /* cookie command */
3312 { "cookie", 0, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3313 { "save", 1, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3314 { "domain", 2, cookie_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3315 { "fqdn", 2, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3316 { "show", 1, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3317 { "all", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3318 { "persistent", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3319 { "session", 2, cookie_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3320 { "toggle", 1, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3321 { "domain", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3322 { "fqdn", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3323 { "purge", 1, cookie_cmd
, XT_DELETE
, 0 },
3325 /* plugin command */
3326 { "plugin", 0, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3327 { "save", 1, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3328 { "domain", 2, pl_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3329 { "fqdn", 2, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3330 { "show", 1, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3331 { "all", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3332 { "persistent", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3333 { "session", 2, pl_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3334 { "toggle", 1, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3335 { "domain", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3336 { "fqdn", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3339 { "https", 0, https_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3340 { "save", 1, https_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3341 { "domain", 2, https_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3342 { "fqdn", 2, https_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3343 { "show", 1, https_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3344 { "all", 2, https_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3345 { "persistent", 2, https_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3346 { "session", 2, https_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3347 { "toggle", 1, https_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3348 { "domain", 2, https_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3349 { "fqdn", 2, https_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3351 /* toplevel (domain) command */
3352 { "toplevel", 0, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3353 { "toggle", 1, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3356 { "cookiejar", 0, xtp_page_cl
, 0, 0 },
3359 { "cert", 0, cert_cmd
, XT_SHOW
, 0 },
3360 { "save", 1, cert_cmd
, XT_SAVE
, 0 },
3361 { "show", 1, cert_cmd
, XT_SHOW
, 0 },
3363 { "ca", 0, ca_cmd
, 0, 0 },
3364 { "downloadmgr", 0, xtp_page_dl
, 0, 0 },
3365 { "dl", 0, xtp_page_dl
, 0, 0 },
3366 { "h", 0, xtp_page_hl
, 0, 0 },
3367 { "history", 0, xtp_page_hl
, 0, 0 },
3368 { "home", 0, go_home
, 0, 0 },
3369 { "restart", 0, restart
, 0, 0 },
3370 { "urlhide", 0, urlaction
, XT_URL_HIDE
, 0 },
3371 { "urlshow", 0, urlaction
, XT_URL_SHOW
, 0 },
3372 { "statustoggle", 0, statustoggle
, 0, 0 },
3373 { "run_script", 0, run_page_script
, 0, XT_USERARG
},
3375 { "print", 0, print_page
, 0, 0 },
3378 { "focusin", 0, resizetab
, XT_ZOOM_IN
, 0 },
3379 { "focusout", 0, resizetab
, XT_ZOOM_OUT
, 0 },
3380 { "focusreset", 0, resizetab
, XT_ZOOM_NORMAL
, 0 },
3381 { "q", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3382 { "quit", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3383 { "open", 0, tabaction
, XT_TAB_OPEN
, XT_URLARG
},
3384 { "tabclose", 0, tabaction
, XT_TAB_DELETE
, XT_PREFIX
| XT_INTARG
},
3385 { "tabedit", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3386 { "tabfirst", 0, movetab
, XT_TAB_FIRST
, 0 },
3387 { "tabhide", 0, tabaction
, XT_TAB_HIDE
, 0 },
3388 { "tablast", 0, movetab
, XT_TAB_LAST
, 0 },
3389 { "tabnew", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3390 { "tabnext", 0, movetab
, XT_TAB_NEXT
, XT_PREFIX
| XT_INTARG
},
3391 { "tabnextstyle", 0, tabaction
, XT_TAB_NEXTSTYLE
, 0 },
3392 { "tabonly", 0, tabaction
, XT_TAB_ONLY
, 0 },
3393 { "tabprevious", 0, movetab
, XT_TAB_PREV
, XT_PREFIX
| XT_INTARG
},
3394 { "tabrewind", 0, movetab
, XT_TAB_FIRST
, 0 },
3395 { "tabshow", 0, tabaction
, XT_TAB_SHOW
, 0 },
3396 { "tabs", 0, buffers
, 0, 0 },
3397 { "tabundoclose", 0, tabaction
, XT_TAB_UNDO_CLOSE
, 0 },
3398 { "buffers", 0, buffers
, 0, 0 },
3399 { "ls", 0, buffers
, 0, 0 },
3400 { "encoding", 0, set_encoding
, 0, XT_USERARG
},
3401 { "loadimages", 0, tabaction
, XT_TAB_LOAD_IMAGES
, 0 },
3404 { "set", 0, set
, 0, XT_SETARG
},
3405 { "runtime", 0, xtp_page_rt
, 0, 0 },
3407 { "fullscreen", 0, fullscreen
, 0, 0 },
3408 { "f", 0, fullscreen
, 0, 0 },
3411 { "session", 0, session_cmd
, XT_SHOW
, 0 },
3412 { "delete", 1, session_cmd
, XT_DELETE
, XT_SESSARG
},
3413 { "open", 1, session_cmd
, XT_OPEN
, XT_SESSARG
},
3414 { "save", 1, session_cmd
, XT_SAVE
, XT_USERARG
},
3415 { "show", 1, session_cmd
, XT_SHOW
, 0 },
3417 /* external javascript */
3418 { "script", 0, script_cmd
, XT_EJS_SHOW
, XT_USERARG
},
3421 { "inspector", 0, inspector_cmd
, XT_INS_SHOW
, 0 },
3422 { "show", 1, inspector_cmd
, XT_INS_SHOW
, 0 },
3423 { "hide", 1, inspector_cmd
, XT_INS_HIDE
, 0 },
3426 { "proxy", 0, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3427 { "show", 1, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3428 { "toggle", 1, proxy_cmd
, XT_PRXY_TOGGLE
, 0 },
3431 { "urlmod", 0, urlmod_cmd
, XT_URL
, 0 },
3432 { "plus", 1, urlmod_cmd
, XT_URL_PLUS
, 0 },
3433 { "min", 1, urlmod_cmd
, XT_URL_MIN
, 0 },
3440 } cmd_status
= {-1, 0};
3443 wv_release_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3446 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 1)
3453 wv_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3456 WebKitHitTestResult
*hit_test_result
;
3459 hit_test_result
= webkit_web_view_get_hit_test_result(t
->wv
, e
);
3460 g_object_get(hit_test_result
, "context", &context
, NULL
);
3461 g_object_unref(G_OBJECT(hit_test_result
));
3466 if (context
& WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE
)
3467 t
->mode
= XT_MODE_INSERT
;
3469 t
->mode
= XT_MODE_COMMAND
;
3471 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3473 else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 8 /* btn 4 */) {
3479 } else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 9 /* btn 5 */) {
3481 a
.i
= XT_NAV_FORWARD
;
3491 tab_close_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3493 DNPRINTF(XT_D_TAB
, "tab_close_cb: tab %d\n", t
->tab_id
);
3495 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3502 parse_custom_uri(struct tab
*t
, const char *uri
)
3504 struct custom_uri
*u
;
3508 TAILQ_FOREACH(u
, &cul
, entry
) {
3509 if (strncmp(uri
, u
->uri
, strlen(u
->uri
)))
3514 sv
[1] = (char *)uri
;
3516 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
,
3518 show_oops(t
, "%s: could not spawn process", __func__
);
3525 activate_uri_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3527 const gchar
*uri
= gtk_entry_get_text(GTK_ENTRY(entry
));
3529 DNPRINTF(XT_D_URL
, "activate_uri_entry_cb: %s\n", uri
);
3532 show_oops(NULL
, "activate_uri_entry_cb invalid parameters");
3537 show_oops(t
, "activate_uri_entry_cb no uri");
3541 uri
+= strspn(uri
, "\t ");
3543 if (parse_custom_uri(t
, uri
))
3546 /* otherwise continue to load page normally */
3547 load_uri(t
, (gchar
*)uri
);
3552 activate_search_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3554 const gchar
*search
= gtk_entry_get_text(GTK_ENTRY(entry
));
3555 char *newuri
= NULL
;
3559 DNPRINTF(XT_D_URL
, "activate_search_entry_cb: %s\n", search
);
3562 show_oops(NULL
, "activate_search_entry_cb invalid parameters");
3566 if (search_string
== NULL
|| strlen(search_string
) == 0) {
3567 show_oops(t
, "no search_string");
3571 set_normal_tab_meaning(t
);
3573 enc_search
= soup_uri_encode(search
, XT_RESERVED_CHARS
);
3574 sv
= g_strsplit(search_string
, "%s", 2);
3575 newuri
= g_strjoinv(enc_search
, sv
);
3580 load_uri(t
, newuri
);
3588 check_and_set_cookie(const gchar
*uri
, struct tab
*t
)
3590 struct wl_entry
*w
= NULL
;
3593 if (uri
== NULL
|| t
== NULL
)
3596 if ((w
= wl_find_uri(uri
, &c_wl
)) == NULL
)
3601 DNPRINTF(XT_D_COOKIE
, "check_and_set_cookie: %s %s\n",
3602 es
? "enable" : "disable", uri
);
3604 g_object_set(G_OBJECT(t
->settings
),
3605 "enable-html5-local-storage", es
, (char *)NULL
);
3606 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3610 check_and_set_js(const gchar
*uri
, struct tab
*t
)
3612 struct wl_entry
*w
= NULL
;
3615 if (uri
== NULL
|| t
== NULL
)
3618 if ((w
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3623 DNPRINTF(XT_D_JS
, "check_and_set_js: %s %s\n",
3624 es
? "enable" : "disable", uri
);
3626 g_object_set(G_OBJECT(t
->settings
),
3627 "enable-scripts", es
, (char *)NULL
);
3628 g_object_set(G_OBJECT(t
->settings
),
3629 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
3630 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3632 button_set_stockid(t
->js_toggle
,
3633 es
? GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
);
3637 check_and_set_pl(const gchar
*uri
, struct tab
*t
)
3639 struct wl_entry
*w
= NULL
;
3642 if (uri
== NULL
|| t
== NULL
)
3645 if ((w
= wl_find_uri(uri
, &pl_wl
)) == NULL
)
3650 DNPRINTF(XT_D_JS
, "check_and_set_pl: %s %s\n",
3651 es
? "enable" : "disable", uri
);
3653 g_object_set(G_OBJECT(t
->settings
),
3654 "enable-plugins", es
, (char *)NULL
);
3655 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3658 #if GTK_CHECK_VERSION(3, 0, 0)
3659 /* A lot of this can be removed when gtk2 is dropped on the floor */
3661 get_css_name(const char *col_str
)
3665 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3666 name
= g_strdup(XT_CSS_NORMAL
);
3667 else if (!strcmp(col_str
, XT_COLOR_RED
))
3668 name
= g_strdup(XT_CSS_RED
);
3669 else if (!strcmp(col_str
, XT_COLOR_YELLOW
))
3670 name
= g_strdup(XT_CSS_YELLOW
);
3671 else if (!strcmp(col_str
, XT_COLOR_GREEN
))
3672 name
= g_strdup(XT_CSS_GREEN
);
3673 else if (!strcmp(col_str
, XT_COLOR_BLUE
))
3674 name
= g_strdup(XT_CSS_BLUE
);
3680 show_ca_status(struct tab
*t
, const char *uri
)
3682 char domain
[8182], file
[PATH_MAX
];
3683 SoupMessage
*msg
= NULL
;
3684 GTlsCertificate
*cert
= NULL
;
3685 GTlsCertificateFlags flags
= 0;
3686 gchar
*col_str
= XT_COLOR_RED
;
3690 #if GTK_CHECK_VERSION(3, 0, 0)
3696 enum cert_trust trust
;
3700 DNPRINTF(XT_D_URL
, "show_ca_status: %d %s %s\n",
3701 ssl_strict_certs
, ssl_ca_file
, uri
);
3706 if (uri
== NULL
|| g_str_has_prefix(uri
, "http://") ||
3707 !g_str_has_prefix(uri
, "https://"))
3711 * Cut the uri to get the certs off the homepage. We can't use the
3712 * full URI here since it may include arguments and we don't want to make
3713 * these requests multiple times.
3715 cut_uri
= g_strdup(uri
);
3717 for (i
= 0; i
< 3; ++i
)
3718 s
= strchr(&(s
[1]), '/');
3721 msg
= soup_message_new("HEAD", cut_uri
);
3725 soup_message_set_flags(msg
, SOUP_MESSAGE_NO_REDIRECT
);
3726 soup_session_send_message(session
, msg
);
3727 if (msg
->status_code
== SOUP_STATUS_SSL_FAILED
||
3728 msg
->status_code
== SOUP_STATUS_TLS_FAILED
) {
3729 DNPRINTF(XT_D_URL
, "%s: status not ok: %d\n", uri
,
3733 if (!soup_message_get_https_status(msg
, &cert
, &flags
)) {
3734 DNPRINTF(XT_D_URL
, "%s: invalid response\n", uri
);
3737 if (!G_IS_TLS_CERTIFICATE(cert
)) {
3738 DNPRINTF(XT_D_URL
, "%s: no cert\n", uri
);
3743 col_str
= XT_COLOR_GREEN
;
3745 col_str
= XT_COLOR_YELLOW
;
3747 strlcpy(domain
, uri
+ strlen("https://"), sizeof domain
);
3748 for (i
= 0; i
< strlen(domain
); i
++)
3749 if (domain
[i
] == '/') {
3754 snprintf(file
, sizeof file
, "%s" PS
"%s", certs_cache_dir
, domain
);
3755 if (warn_cert_changes
) {
3756 if (check_cert_changes(t
, cert
, file
, uri
))
3760 snprintf(file
, sizeof file
, "%s" PS
"%s", certs_dir
, domain
);
3761 chain
= g_strdup("");
3762 if ((trust
= check_local_certs(file
, cert
, &chain
)) == CERT_LOCAL
)
3763 col_str
= XT_COLOR_BLUE
;
3769 g_object_unref(msg
);
3770 if (!strcmp(col_str
, XT_COLOR_WHITE
) || nocolor
) {
3771 #if GTK_CHECK_VERSION(3, 0, 0)
3772 gtk_widget_set_name(t
->uri_entry
, XT_CSS_NORMAL
);
3773 statusbar_modify_attr(t
, XT_CSS_NORMAL
);
3775 text
= gdk_color_to_string(
3776 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3777 base
= gdk_color_to_string(
3778 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3779 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3780 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3781 statusbar_modify_attr(t
, text
, base
);
3786 #if GTK_CHECK_VERSION(3, 0, 0)
3787 name
= get_css_name(col_str
);
3788 gtk_widget_set_name(t
->uri_entry
, name
);
3789 statusbar_modify_attr(t
, name
);
3792 gdk_color_parse(col_str
, &color
);
3793 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3794 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3800 free_favicon(struct tab
*t
)
3802 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p req %p\n",
3803 __func__
, t
->icon_download
, t
->icon_request
);
3805 if (t
->icon_request
)
3806 g_object_unref(t
->icon_request
);
3807 if (t
->icon_dest_uri
)
3808 g_free(t
->icon_dest_uri
);
3810 t
->icon_request
= NULL
;
3811 t
->icon_dest_uri
= NULL
;
3815 xt_icon_from_name(struct tab
*t
, gchar
*name
)
3817 if (!enable_favicon_entry
)
3820 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->uri_entry
),
3821 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3823 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.uri
),
3824 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3826 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.uri
),
3827 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3831 xt_icon_from_pixbuf(struct tab
*t
, GdkPixbuf
*pb
)
3833 GdkPixbuf
*pb_scaled
;
3835 if (gdk_pixbuf_get_width(pb
) > 16 || gdk_pixbuf_get_height(pb
) > 16)
3836 pb_scaled
= gdk_pixbuf_scale_simple(pb
, 16, 16,
3837 GDK_INTERP_BILINEAR
);
3841 if (enable_favicon_entry
) {
3844 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
3845 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3848 if (show_url
== 0) {
3849 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.uri
),
3850 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3852 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.uri
),
3853 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3856 /* XXX: Only supports the minimal tabs atm. */
3857 if (enable_favicon_tabs
)
3858 gtk_image_set_from_pixbuf(GTK_IMAGE(t
->tab_elems
.favicon
),
3861 if (pb_scaled
!= pb
)
3862 g_object_unref(pb_scaled
);
3866 xt_icon_from_file(struct tab
*t
, char *uri
)
3871 if (g_str_has_prefix(uri
, "file://"))
3872 file
= g_filename_from_uri(uri
, NULL
, NULL
);
3874 file
= g_strdup(uri
);
3879 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
3881 xt_icon_from_pixbuf(t
, pb
);
3884 xt_icon_from_name(t
, "text-html");
3890 is_valid_icon(char *file
)
3893 const char *mime_type
;
3897 gf
= g_file_new_for_path(file
);
3898 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
3900 mime_type
= g_file_info_get_content_type(fi
);
3901 valid
= g_strcmp0(mime_type
, "image/x-ico") == 0 ||
3902 g_strcmp0(mime_type
, "image/vnd.microsoft.icon") == 0 ||
3903 g_strcmp0(mime_type
, "image/png") == 0 ||
3904 g_strcmp0(mime_type
, "image/gif") == 0 ||
3905 g_strcmp0(mime_type
, "application/octet-stream") == 0;
3913 set_favicon_from_file(struct tab
*t
, char *uri
)
3918 if (t
== NULL
|| uri
== NULL
)
3921 if (g_str_has_prefix(uri
, "file://"))
3922 file
= g_filename_from_uri(uri
, NULL
, NULL
);
3924 file
= g_strdup(uri
);
3929 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading %s\n", __func__
, file
);
3931 if (!stat(file
, &sb
)) {
3932 if (sb
.st_size
== 0 || !is_valid_icon(file
)) {
3933 /* corrupt icon so trash it */
3934 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3937 /* no need to set icon to default here */
3941 xt_icon_from_file(t
, file
);
3947 favicon_download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
3950 WebKitDownloadStatus status
= webkit_download_get_status(download
);
3951 struct tab
*tt
= NULL
, *t
= NULL
;
3954 * find the webview instead of passing in the tab as it could have been
3955 * deleted from underneath us.
3957 TAILQ_FOREACH(tt
, &tabs
, entry
) {
3966 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d status %d\n",
3967 __func__
, t
->tab_id
, status
);
3970 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
3972 t
->icon_download
= NULL
;
3975 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
3978 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
3981 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
3983 DNPRINTF(XT_D_DOWNLOAD
, "%s: freeing favicon %d\n",
3984 __func__
, t
->tab_id
);
3985 t
->icon_download
= NULL
;
3988 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
3991 DNPRINTF(XT_D_DOWNLOAD
, "%s: setting icon to %s\n",
3992 __func__
, t
->icon_dest_uri
);
3993 set_favicon_from_file(t
, t
->icon_dest_uri
);
3994 /* these will be freed post callback */
3995 t
->icon_request
= NULL
;
3996 t
->icon_download
= NULL
;
4004 abort_favicon_download(struct tab
*t
)
4006 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p\n", __func__
, t
->icon_download
);
4008 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
4009 if (t
->icon_download
) {
4010 g_signal_handlers_disconnect_by_func(G_OBJECT(t
->icon_download
),
4011 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
4012 webkit_download_cancel(t
->icon_download
);
4013 t
->icon_download
= NULL
;
4018 xt_icon_from_name(t
, "text-html");
4022 notify_icon_loaded_cb(WebKitWebView
*wv
, gchar
*uri
, struct tab
*t
)
4024 DNPRINTF(XT_D_DOWNLOAD
, "%s %s\n", __func__
, uri
);
4026 if (uri
== NULL
|| t
== NULL
)
4029 #if WEBKIT_CHECK_VERSION(1, 4, 0)
4030 /* take icon from WebKitIconDatabase */
4031 GdkPixbuf
*pb
= NULL
;
4033 /* webkit_web_view_get_icon_pixbuf is depreciated in 1.8 */
4034 #if WEBKIT_CHECK_VERSION(1, 8, 0)
4036 * If the page was not loaded (for example, via ssl_strict_certs), do
4037 * not attempt to get the webview's pixbuf. This prevents a CRITICAL
4040 if (wv
&& webkit_web_view_get_uri(wv
))
4041 pb
= webkit_web_view_try_get_favicon_pixbuf(wv
, 0, 0);
4043 if (wv
&& webkit_web_view_get_uri(wv
))
4044 pb
= webkit_web_view_get_icon_pixbuf(wv
);
4047 xt_icon_from_pixbuf(t
, pb
);
4050 xt_icon_from_name(t
, "text-html");
4051 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
4052 /* download icon to cache dir */
4053 gchar
*name_hash
, file
[PATH_MAX
];
4056 if (t
->icon_request
) {
4057 DNPRINTF(XT_D_DOWNLOAD
, "%s: download in progress\n", __func__
);
4061 /* check to see if we got the icon in cache */
4062 name_hash
= g_compute_checksum_for_string(G_CHECKSUM_SHA256
, uri
, -1);
4063 snprintf(file
, sizeof file
, "%s" PS
"%s.ico", cache_dir
, name_hash
);
4066 if (!stat(file
, &sb
)) {
4067 if (sb
.st_size
> 0) {
4068 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading from cache %s\n",
4070 set_favicon_from_file(t
, file
);
4074 /* corrupt icon so trash it */
4075 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
4080 /* create download for icon */
4081 t
->icon_request
= webkit_network_request_new(uri
);
4082 if (t
->icon_request
== NULL
) {
4083 DNPRINTF(XT_D_DOWNLOAD
, "%s: invalid uri %s\n",
4088 t
->icon_download
= webkit_download_new(t
->icon_request
);
4089 if (t
->icon_download
== NULL
)
4092 /* we have to free icon_dest_uri later */
4093 if ((t
->icon_dest_uri
= g_filename_to_uri(file
, NULL
, NULL
)) == NULL
)
4095 webkit_download_set_destination_uri(t
->icon_download
,
4098 if (webkit_download_get_status(t
->icon_download
) ==
4099 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
4100 g_object_unref(t
->icon_request
);
4101 g_free(t
->icon_dest_uri
);
4102 t
->icon_request
= NULL
;
4103 t
->icon_dest_uri
= NULL
;
4107 g_signal_connect(G_OBJECT(t
->icon_download
), "notify::status",
4108 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
4110 webkit_download_start(t
->icon_download
);
4115 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
4117 const gchar
*uri
= NULL
;
4118 struct history
*h
, find
;
4120 gchar
*tmp_uri
= NULL
;
4121 #if !GTK_CHECK_VERSION(3, 0, 0)
4125 DNPRINTF(XT_D_URL
, "notify_load_status_cb: %d %s\n",
4126 webkit_web_view_get_load_status(wview
),
4127 get_uri(t
) ? get_uri(t
) : "NOTHING");
4130 show_oops(NULL
, "notify_load_status_cb invalid parameters");
4134 switch (webkit_web_view_get_load_status(wview
)) {
4135 case WEBKIT_LOAD_PROVISIONAL
:
4137 abort_favicon_download(t
);
4138 #if GTK_CHECK_VERSION(2, 20, 0)
4139 gtk_widget_show(t
->spinner
);
4140 gtk_spinner_start(GTK_SPINNER(t
->spinner
));
4142 t
->download_requested
= 0;
4144 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), TRUE
);
4146 /* assume we are a new address */
4147 #if GTK_CHECK_VERSION(3, 0, 0)
4148 gtk_widget_set_name(t
->uri_entry
, XT_CSS_NORMAL
);
4149 statusbar_modify_attr(t
, XT_CSS_NORMAL
);
4151 text
= gdk_color_to_string(
4152 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
4153 base
= gdk_color_to_string(
4154 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
4155 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
4156 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
4157 statusbar_modify_attr(t
, text
, base
);
4162 /* DOM is changing, unreference the previous focused element */
4163 #if WEBKIT_CHECK_VERSION(1, 5, 0)
4165 g_object_unref(t
->active
);
4167 if (t
->active_text
) {
4168 g_free(t
->active_text
);
4169 t
->active_text
= NULL
;
4173 /* take focus if we are visible */
4180 case WEBKIT_LOAD_COMMITTED
:
4185 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
4191 set_status(t
, "Loading: %s", (char *)uri
);
4193 /* clear t->item, except if we're switching to an about: page */
4194 if (t
->item
&& !g_str_has_prefix(uri
, "xxxt://") &&
4195 !g_str_has_prefix(uri
, "about:")) {
4196 g_object_unref(t
->item
);
4200 /* check if js white listing is enabled */
4201 if (enable_plugin_whitelist
)
4202 check_and_set_pl(uri
, t
);
4203 if (enable_cookie_whitelist
)
4204 check_and_set_cookie(uri
, t
);
4205 if (enable_js_whitelist
)
4206 check_and_set_js(uri
, t
);
4212 /* we know enough to autosave the session */
4213 if (session_autosave
) {
4218 show_ca_status(t
, uri
);
4219 run_script(t
, JS_HINTING
);
4220 if (enable_autoscroll
)
4221 run_script(t
, JS_AUTOSCROLL
);
4224 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
4226 if (color_visited_uris
) {
4227 color_visited(t
, color_visited_helper());
4230 * This colors the links you middle-click (open in new
4231 * tab) in the current tab.
4233 if (t
->tab_id
!= gtk_notebook_get_current_page(notebook
) &&
4234 (uri
= get_uri(t
)) != NULL
)
4235 color_visited(get_current_tab(),
4236 g_strdup_printf("{'%s' : 'dummy'}", uri
));
4240 case WEBKIT_LOAD_FINISHED
:
4242 if ((uri
= get_uri(t
)) == NULL
)
4245 * js_autorun calls get_uri which frees t->tmp_uri if on an
4246 * "about:" page. On "about:" pages, uri points to t->tmp_uri.
4247 * I.e. we will use freed memory. Prevent that.
4249 tmp_uri
= g_strdup(uri
);
4251 /* autorun some js if enabled */
4256 if (!strncmp(tmp_uri
, "http://", strlen("http://")) ||
4257 !strncmp(tmp_uri
, "https://", strlen("https://")) ||
4258 !strncmp(tmp_uri
, "file://", strlen("file://"))) {
4259 find
.uri
= (gchar
*)tmp_uri
;
4260 h
= RB_FIND(history_list
, &hl
, &find
);
4262 insert_history_item(tmp_uri
,
4263 get_title(t
, FALSE
), time(NULL
));
4265 h
->time
= time(NULL
);
4268 if (statusbar_style
== XT_STATUSBAR_URL
)
4269 set_status(t
, "%s", (char *)tmp_uri
);
4271 set_status(t
, "%s", get_title(t
, FALSE
));
4272 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
4273 #if GTK_CHECK_VERSION(2, 20, 0)
4274 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
4275 gtk_widget_hide(t
->spinner
);
4280 #if WEBKIT_CHECK_VERSION(1, 1, 18)
4281 case WEBKIT_LOAD_FAILED
:
4283 if (!t
->download_requested
) {
4284 gtk_label_set_text(GTK_LABEL(t
->label
),
4285 get_title(t
, FALSE
));
4286 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
),
4287 get_title(t
, FALSE
));
4288 set_status(t
, "%s", (char *)get_title(t
, FALSE
));
4289 gtk_window_set_title(GTK_WINDOW(main_window
),
4290 get_title(t
, TRUE
));
4296 #if GTK_CHECK_VERSION(2, 20, 0)
4297 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
4298 gtk_widget_hide(t
->spinner
);
4300 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
4304 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
),
4305 can_go_back_for_real(t
));
4307 gtk_widget_set_sensitive(GTK_WIDGET(t
->forward
),
4308 can_go_forward_for_real(t
));
4312 notify_title_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
4314 const gchar
*title
= NULL
, *win_title
= NULL
;
4316 title
= get_title(t
, FALSE
);
4317 win_title
= get_title(t
, TRUE
);
4319 gtk_label_set_text(GTK_LABEL(t
->label
), title
);
4320 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
), title
);
4323 if (win_title
&& t
->tab_id
== gtk_notebook_get_current_page(notebook
))
4324 gtk_window_set_title(GTK_WINDOW(main_window
), win_title
);
4328 get_domain(const gchar
*host
)
4333 /* handle silly domains like .co.uk */
4335 if ((x
= strlen(host
)) <= 6)
4336 return (g_strdup(host
));
4338 if (host
[x
- 3] == '.' && host
[x
- 6] == '.') {
4344 return (g_strdup(&host
[x
+ 1]));
4348 p
= g_strrstr(host
, ".");
4350 return (g_strdup(""));
4356 return (g_strdup(p
+ 1));
4358 return (g_strdup(host
));
4362 js_autorun(struct tab
*t
)
4366 size_t got_default
= 0, got_host
= 0;
4368 char deff
[PATH_MAX
], hostf
[PATH_MAX
];
4369 char *js
= NULL
, *jsat
, *domain
= NULL
;
4370 FILE *deffile
= NULL
, *hostfile
= NULL
;
4372 if (enable_js_autorun
== 0)
4377 !(g_str_has_prefix(uri
, "http://") ||
4378 g_str_has_prefix(uri
, "https://")))
4381 su
= soup_uri_new(uri
);
4384 if (!SOUP_URI_VALID_FOR_HTTP(su
))
4387 DNPRINTF(XT_D_JS
, "%s: host: %s domain: %s\n", __func__
,
4389 domain
= get_domain(su
->host
);
4391 snprintf(deff
, sizeof deff
, "%s" PS
"default.js", js_dir
);
4392 if ((deffile
= fopen(deff
, "r")) != NULL
) {
4393 if (fstat(fileno(deffile
), &sb
) == -1) {
4394 show_oops(t
, "can't stat default JS file");
4397 got_default
= sb
.st_size
;
4400 /* try host first followed by domain */
4401 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, su
->host
);
4402 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4403 if ((hostfile
= fopen(hostf
, "r")) == NULL
) {
4404 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, domain
);
4405 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4406 if ((hostfile
= fopen(hostf
, "r")) == NULL
)
4409 DNPRINTF(XT_D_JS
, "file: %s\n", hostf
);
4410 if (fstat(fileno(hostfile
), &sb
) == -1) {
4411 show_oops(t
, "can't stat %s JS file", hostf
);
4414 got_host
= sb
.st_size
;
4417 if (got_default
+ got_host
== 0)
4420 js
= g_malloc0(got_default
+ got_host
+ 1);
4424 if (fread(js
, got_default
, 1, deffile
) != 1) {
4425 show_oops(t
, "default file read error");
4428 jsat
= js
+ got_default
;
4432 if (fread(jsat
, got_host
, 1, hostfile
) != 1) {
4433 show_oops(t
, "host file read error");
4438 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
4455 webview_progress_changed_cb(WebKitWebView
*wv
, GParamSpec
*pspec
, struct tab
*t
)
4459 progress
= webkit_web_view_get_progress(wv
);
4460 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.uri
),
4461 progress
== 1.0 ? 0 : progress
);
4462 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->uri_entry
),
4463 progress
== 1.0 ? 0 : progress
);
4465 update_statusbar_position(NULL
, NULL
);
4469 strict_transport_rb_cmp(struct strict_transport
*a
, struct strict_transport
*b
)
4474 /* compare strings from the end */
4475 l1
= strlen(a
->host
);
4476 l2
= strlen(b
->host
);
4480 for (; *p1
== *p2
&& p1
> a
->host
&& p2
> b
->host
;
4485 * Check if we need to do pattern expansion,
4486 * or if we're just keeping the tree in order
4488 if (a
->flags
& XT_STS_FLAGS_EXPAND
&&
4489 b
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) {
4490 /* Check if we're matching the
4491 * 'host.xyz' part in '*.host.xyz'
4493 if (p2
== b
->host
&& (p1
== a
->host
|| *(p1
-1) == '.')) {
4498 if (p1
== a
->host
&& p2
== b
->host
)
4512 RB_GENERATE(strict_transport_tree
, strict_transport
, entry
,
4513 strict_transport_rb_cmp
);
4516 strict_transport_add(const char *domain
, time_t timeout
, int subdomains
)
4518 struct strict_transport
*d
, find
;
4522 if (enable_strict_transport
== FALSE
)
4525 DPRINTF("strict_transport_add(%s,%" PRIi64
",%d)\n", domain
,
4526 (uint64_t)timeout
, subdomains
);
4532 find
.host
= (char *)domain
;
4534 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4538 /* check if update is needed */
4539 if (d
->timeout
== timeout
&&
4540 (d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) == subdomains
)
4543 d
->timeout
= timeout
;
4545 d
->flags
|= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4547 /* We're still initializing */
4548 if (strict_transport_file
== NULL
)
4551 if ((f
= fopen(strict_transport_file
, "w")) == NULL
) {
4553 "can't open strict-transport rules file");
4557 fprintf(f
, "# Generated file - do not update unless you know "
4558 "what you're doing\n");
4559 RB_FOREACH(d
, strict_transport_tree
, &st_tree
) {
4560 if (d
->timeout
< now
)
4562 fprintf(f
, "%s\t%" PRIi64
"\t%d\n", d
->host
,
4563 (uint64_t)d
->timeout
,
4564 d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
);
4568 d
= g_malloc(sizeof *d
);
4569 d
->host
= g_strdup(domain
);
4570 d
->timeout
= timeout
;
4572 d
->flags
= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4575 RB_INSERT(strict_transport_tree
, &st_tree
, d
);
4577 /* We're still initializing */
4578 if (strict_transport_file
== NULL
)
4581 if ((f
= fopen(strict_transport_file
, "a+")) == NULL
) {
4583 "can't open strict-transport rules file");
4587 fseek(f
, 0, SEEK_END
);
4588 fprintf(f
,"%s\t%" PRIi64
"\t%d\n", d
->host
, (uint64_t)timeout
,
4596 strict_transport_check(const char *host
)
4598 static struct strict_transport
*d
= NULL
;
4599 struct strict_transport find
;
4601 if (enable_strict_transport
== FALSE
)
4604 find
.host
= (char *)host
;
4606 /* match for domains that include subdomains */
4607 find
.flags
= XT_STS_FLAGS_EXPAND
;
4609 /* First, check if we're already at the right node */
4610 if (d
!= NULL
&& strict_transport_rb_cmp(&find
, d
) == 0) {
4614 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4622 strict_transport_init()
4624 char file
[PATH_MAX
];
4630 time_t timeout
, now
;
4633 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_STS_FILE
);
4634 if ((f
= fopen(file
, "r")) == NULL
) {
4635 strict_transport_file
= g_strdup(file
);
4646 if ((rule
= fparseln(f
, &len
, NULL
, delim
, 0)) == NULL
) {
4647 if (!feof(f
) || ferror(f
))
4653 /* get second entry */
4654 if ((ptr
= strpbrk(rule
, " \t")) == NULL
)
4658 timeout
= atoi(ptr
);
4660 /* get third entry */
4661 if ((ptr
= strpbrk(ptr
, " \t")) == NULL
)
4665 subdomains
= atoi(ptr
);
4668 strict_transport_add(rule
, timeout
, subdomains
);
4673 strict_transport_file
= g_strdup(file
);
4677 startpage_add("strict-transport rules file ('%s') is corrupt", file
);
4685 force_https_check(const char *uri
)
4687 struct wl_entry
*w
= NULL
;
4692 if ((w
= wl_find_uri(uri
, &force_https
)) == NULL
)
4699 strict_transport_security_cb(SoupMessage
*msg
, gpointer data
)
4705 int subdomains
= FALSE
;
4710 sts
= soup_message_headers_get_one(msg
->response_headers
,
4711 "Strict-Transport-Security");
4712 uri
= soup_message_get_uri(msg
);
4714 if (sts
== NULL
|| uri
== NULL
)
4717 if ((ptr
= strcasestr(sts
, "max-age="))) {
4718 ptr
+= strlen("max-age=");
4719 timeout
= atoll(ptr
);
4721 return; /* malformed header - max-age must be included */
4723 if ((ptr
= strcasestr(sts
, "includeSubDomains")))
4726 strict_transport_add(uri
->host
, timeout
+ time(NULL
), subdomains
);
4730 session_rq_cb(SoupSession
*s
, SoupMessage
*msg
, SoupSocket
*socket
,
4740 if (s
== NULL
|| msg
== NULL
)
4743 if (enable_strict_transport
) {
4744 soup_message_add_header_handler(msg
, "finished",
4745 "Strict-Transport-Security",
4746 G_CALLBACK(strict_transport_security_cb
), NULL
);
4749 if (referer_mode
== XT_REFERER_ALWAYS
)
4752 /* Check if referer is set - and what the user requested for referers */
4753 ref
= soup_message_headers_get_one(msg
->request_headers
, "Referer");
4755 DNPRINTF(XT_D_NAV
, "session_rq_cb: Referer: %s\n", ref
);
4756 switch (referer_mode
) {
4757 case XT_REFERER_NEVER
:
4758 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing referer\n");
4759 soup_message_headers_remove(msg
->request_headers
,
4762 case XT_REFERER_SAME_DOMAIN
:
4763 ref_uri
= soup_uri_new(ref
);
4764 dest
= soup_message_get_uri(msg
);
4766 ref_suffix
= tld_get_suffix(ref_uri
->host
);
4767 dest_suffix
= tld_get_suffix(dest
->host
);
4769 if (dest
&& ref_suffix
&& dest_suffix
&&
4770 strcmp(ref_suffix
, dest_suffix
) != 0) {
4771 soup_message_headers_remove(msg
->request_headers
,
4773 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4774 "referer (not same domain) (suffixes: %s - %s)\n",
4775 ref_suffix
, dest_suffix
);
4777 soup_uri_free(ref_uri
);
4779 case XT_REFERER_SAME_FQDN
:
4780 ref_uri
= soup_uri_new(ref
);
4781 dest
= soup_message_get_uri(msg
);
4782 if (dest
&& strcmp(ref_uri
->host
, dest
->host
) != 0) {
4783 soup_message_headers_remove(msg
->request_headers
,
4785 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4786 "referer (not same fqdn) (should be %s)\n",
4789 soup_uri_free(ref_uri
);
4791 case XT_REFERER_CUSTOM
:
4792 DNPRINTF(XT_D_NAV
, "session_rq_cb: setting referer "
4793 "to %s\n", referer_custom
);
4794 soup_message_headers_replace(msg
->request_headers
,
4795 "Referer", referer_custom
);
4802 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
4803 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
4804 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
4806 WebKitWebNavigationReason reason
;
4810 show_oops(NULL
, "webview_npd_cb invalid parameters");
4814 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
4816 webkit_network_request_get_uri(request
));
4818 uri
= (char *)webkit_network_request_get_uri(request
);
4820 if (!auto_load_images
&& t
->load_images
) {
4822 /* Disable autoloading of images, now that we're done loading
4824 g_object_set(G_OBJECT(t
->settings
),
4825 "auto-load-images", FALSE
, (char *)NULL
);
4826 webkit_web_view_set_settings(t
->wv
, t
->settings
);
4828 t
->load_images
= FALSE
;
4831 /* If this is an xtp url, we don't load anything else. */
4832 if (parse_xtp_url(t
, uri
)) {
4833 webkit_web_policy_decision_ignore(pd
);
4837 if (parse_custom_uri(t
, uri
)) {
4838 webkit_web_policy_decision_ignore(pd
);
4842 if (valid_url_type(uri
)) {
4843 show_oops(t
, "Stopping attempt to load an invalid URI (possible"
4844 " bait and switch attack)");
4845 webkit_web_policy_decision_ignore(pd
);
4849 if ((t
->mode
== XT_MODE_HINT
&& t
->new_tab
) || t
->ctrl_click
) {
4851 create_new_tab(uri
, NULL
, ctrl_click_focus
, -1);
4852 webkit_web_policy_decision_ignore(pd
);
4853 return (TRUE
); /* we made the decission */
4857 * This is a little hairy but it comes down to this:
4858 * when we run in whitelist mode we have to assist the browser in
4859 * opening the URL that it would have opened in a new tab.
4861 reason
= webkit_web_navigation_action_get_reason(na
);
4862 if (reason
== WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED
) {
4863 set_normal_tab_meaning(t
);
4864 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1)
4866 webkit_web_policy_decision_use(pd
);
4867 return (TRUE
); /* we made the decision */
4874 webview_rrs_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, WebKitWebResource
*res
,
4875 WebKitNetworkRequest
*request
, WebKitNetworkResponse
*response
,
4878 SoupMessage
*msg
= NULL
;
4879 SoupURI
*uri
= NULL
;
4880 struct http_accept ha_find
, *ha
= NULL
;
4881 struct user_agent ua_find
, *ua
= NULL
;
4882 struct domain_id di_find
, *di
= NULL
;
4885 msg
= webkit_network_request_get_message(request
);
4889 uri
= soup_message_get_uri(msg
);
4892 uri_s
= soup_uri_to_string(uri
, FALSE
);
4894 if (strcmp(uri
->scheme
, SOUP_URI_SCHEME_HTTP
) == 0) {
4895 if (strict_transport_check(uri
->host
) ||
4896 force_https_check(uri_s
)) {
4897 DNPRINTF(XT_D_NAV
, "webview_rrs_cb: force https for %s\n",
4899 soup_uri_set_scheme(uri
, SOUP_URI_SCHEME_HTTPS
);
4904 soup_message_headers_append(msg
->request_headers
, "DNT", "1");
4907 * Check if resources on this domain have been loaded before. If
4908 * not, add the current tab's http-accept and user-agent id's to a
4909 * new domain_id and insert into the RB tree. Use these http headers
4910 * for all resources loaded from this domain for the lifetime of the
4913 if ((di_find
.domain
= uri
->host
) == NULL
)
4915 if ((di
= RB_FIND(domain_id_list
, &di_list
, &di_find
)) == NULL
) {
4916 di
= g_malloc(sizeof *di
);
4917 di
->domain
= g_strdup(uri
->host
);
4918 di
->ua_id
= t
->user_agent_id
++;
4919 di
->ha_id
= t
->http_accept_id
++;
4920 RB_INSERT(domain_id_list
, &di_list
, di
);
4922 ua_find
.id
= t
->user_agent_id
;
4923 ua
= RB_FIND(user_agent_list
, &ua_list
, &ua_find
);
4925 t
->user_agent_id
= 0;
4927 ha_find
.id
= t
->http_accept_id
;
4928 ha
= RB_FIND(http_accept_list
, &ha_list
, &ha_find
);
4930 t
->http_accept_id
= 0;
4933 ua_find
.id
= di
->ua_id
;
4934 ua
= RB_FIND(user_agent_list
, &ua_list
, &ua_find
);
4935 ha_find
.id
= di
->ha_id
;
4936 ha
= RB_FIND(http_accept_list
, &ha_list
, &ha_find
);
4939 soup_message_headers_replace(msg
->request_headers
,
4940 "User-Agent", ua
->value
);
4942 soup_message_headers_replace(msg
->request_headers
,
4943 "Accept", ha
->value
);
4951 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4954 struct wl_entry
*w
= NULL
;
4956 WebKitWebView
*webview
= NULL
;
4959 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
4960 webkit_web_view_get_uri(wv
));
4963 /* open in current tab */
4965 } else if (enable_scripts
== 0 && enable_js_whitelist
== 1) {
4966 uri
= webkit_web_view_get_uri(wv
);
4967 if (uri
&& (w
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4970 if (t
->ctrl_click
) {
4971 x
= ctrl_click_focus
;
4974 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4976 } else if (enable_scripts
== 1) {
4977 if (t
->ctrl_click
) {
4978 x
= ctrl_click_focus
;
4981 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4989 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
4992 struct wl_entry
*w
= NULL
;
4994 DNPRINTF(XT_D_NAV
, "webview_close_cb: %d\n", t
->tab_id
);
4996 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
4997 uri
= webkit_web_view_get_uri(wv
);
4998 if (uri
&& (w
= wl_find_uri(uri
, &js_wl
)) == NULL
)
5002 } else if (enable_scripts
== 1)
5009 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
5011 /* we can not eat the event without throwing gtk off so defer it */
5013 /* catch middle click */
5014 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 2) {
5019 /* catch ctrl click */
5020 if (e
->type
== GDK_BUTTON_RELEASE
&&
5021 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
5026 return (XT_CB_PASSTHROUGH
);
5030 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
5032 struct mime_type
*m
;
5034 GError
*gerr
= NULL
;
5036 m
= find_mime_type(mime_type
);
5042 sv
[0] = m
->mt_action
;
5043 sv
[1] = (char *)webkit_network_request_get_uri(request
);
5046 /* ignore donothing from example config */
5047 if (m
->mt_action
&& !strcmp(m
->mt_action
, "donothing"))
5050 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
, NULL
,
5052 show_oops(t
, "%s: could not spawn process (%s)", __func__
,
5053 gerr
? gerr
->message
: "N/A");
5058 get_mime_type(const char *uri
)
5063 char *mime_type
= NULL
;
5067 show_oops(NULL
, "%s: invalid parameters", __func__
);
5071 if (g_str_has_prefix(uri
, "file://"))
5072 file
= g_filename_from_uri(uri
, NULL
, NULL
);
5074 file
= g_strdup(uri
);
5079 gf
= g_file_new_for_path(file
);
5080 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
5082 if ((m
= g_file_info_get_content_type(fi
)) != NULL
)
5083 mime_type
= g_strdup(m
);
5092 run_download_mimehandler(char *mime_type
, char *file
)
5094 struct mime_type
*m
;
5097 m
= find_mime_type(mime_type
);
5101 sv
[0] = m
->mt_action
;
5104 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
, NULL
,
5106 show_oops(NULL
, "%s: could not spawn process: %s %s", __func__
,
5114 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
5117 WebKitDownloadStatus status
;
5122 if (download
== NULL
)
5124 status
= webkit_download_get_status(download
);
5125 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
5128 if (download_notifications
)
5129 show_oops(NULL
, "Download of '%s' finished",
5130 basename(webkit_download_get_destination_uri(download
)));
5131 uri
= webkit_download_get_destination_uri(download
);
5134 mime
= get_mime_type(uri
);
5138 if (g_str_has_prefix(uri
, "file://"))
5139 file
= g_filename_from_uri(uri
, NULL
, NULL
);
5141 file
= g_strdup(uri
);
5146 run_download_mimehandler((char *)mime
, file
);
5153 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
5154 WebKitNetworkRequest
*request
, char *mime_type
,
5155 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
5158 show_oops(NULL
, "webview_mimetype_cb invalid parameters");
5162 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
5163 t
->tab_id
, mime_type
);
5165 if (run_mimehandler(t
, mime_type
, request
) == 0) {
5166 webkit_web_policy_decision_ignore(decision
);
5171 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
5172 webkit_web_policy_decision_download(decision
);
5180 download_start(struct tab
*t
, struct download
*d
, int flag
)
5182 WebKitNetworkRequest
*req
;
5184 const gchar
*suggested_name
;
5185 gchar
*filename
= NULL
;
5191 if (d
== NULL
|| t
== NULL
) {
5192 show_oops(NULL
, "%s invalid parameters", __func__
);
5196 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
5197 if (suggested_name
== NULL
)
5198 return (FALSE
); /* abort download */
5209 filename
= g_strdup_printf("%d%s", i
, suggested_name
);
5212 /* XXX using urls doesn't work properly in windows? */
5213 uri
= g_strdup_printf("%s\\%s", download_dir
, i
?
5214 filename
: suggested_name
);
5216 path
= g_strdup_printf("%s" PS
"%s", download_dir
, i
?
5217 filename
: suggested_name
);
5218 if ((uri
= g_filename_to_uri(path
, NULL
, NULL
)) == NULL
)
5223 } while (!stat(uri
, &sb
));
5225 } while (!stat(path
, &sb
));
5228 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d filename %s "
5229 "local %s\n", __func__
, t
->tab_id
, filename
, uri
);
5231 /* if we're restarting the download, or starting
5232 * it after doing something else, we need to recreate
5233 * the download request.
5235 if (flag
== XT_DL_RESTART
) {
5236 req
= webkit_network_request_new(webkit_download_get_uri(d
->download
));
5237 webkit_download_cancel(d
->download
);
5238 g_object_unref(d
->download
);
5239 d
->download
= webkit_download_new(req
);
5240 g_object_unref(req
);
5243 webkit_download_set_destination_uri(d
->download
, uri
);
5245 if (webkit_download_get_status(d
->download
) ==
5246 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
5247 show_oops(t
, "%s: download failed to start", __func__
);
5249 show_oops(t
, "Download Failed");
5251 /* connect "download first" mime handler */
5252 g_signal_connect(G_OBJECT(d
->download
), "notify::status",
5253 G_CALLBACK(download_status_changed_cb
), NULL
);
5255 /* get from history */
5256 g_object_ref(d
->download
);
5257 show_oops(t
, "Download of '%s' started...",
5258 basename(webkit_download_get_destination_uri(d
->download
)));
5261 if (flag
!= XT_DL_START
)
5262 webkit_download_start(d
->download
);
5264 DNPRINTF(XT_D_DOWNLOAD
, "download status : %d",
5265 webkit_download_get_status(d
->download
));
5267 /* sync other download manager tabs */
5268 update_download_tabs(NULL
);
5281 download_ask_cb(struct tab
*t
, GdkEventKey
*e
, gpointer data
)
5283 struct download
*d
= data
;
5287 t
->mode_cb_data
= NULL
;
5290 e
->keyval
= GDK_Escape
;
5291 return (XT_CB_PASSTHROUGH
);
5294 DPRINTF("download_ask_cb: User pressed %c\n", e
->keyval
);
5295 if (e
->keyval
== 'y' || e
->keyval
== 'Y' || e
->keyval
== GDK_Return
)
5296 /* We need to do a RESTART, because we're not calling from
5297 * webview_download_cb
5299 download_start(t
, d
, XT_DL_RESTART
);
5301 /* for all other keyvals, we just let the download be */
5302 e
->keyval
= GDK_Escape
;
5303 return (XT_CB_HANDLED
);
5307 download_ask(struct tab
*t
, struct download
*d
)
5309 const gchar
*suggested_name
;
5311 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
5312 if (suggested_name
== NULL
)
5313 return (FALSE
); /* abort download */
5315 show_oops(t
, "download file %s [y/n] ?", suggested_name
);
5316 t
->mode_cb
= download_ask_cb
;
5317 t
->mode_cb_data
= d
;
5323 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
5326 const gchar
*suggested_name
;
5327 struct download
*download_entry
;
5330 if (wk_download
== NULL
|| t
== NULL
) {
5331 show_oops(NULL
, "%s invalid parameters", __func__
);
5335 suggested_name
= webkit_download_get_suggested_filename(wk_download
);
5336 if (suggested_name
== NULL
)
5337 return (FALSE
); /* abort download */
5339 download_entry
= g_malloc(sizeof(struct download
));
5340 download_entry
->download
= wk_download
;
5341 download_entry
->tab
= t
;
5342 download_entry
->id
= next_download_id
++;
5343 RB_INSERT(download_list
, &downloads
, download_entry
);
5344 t
->download_requested
= 1;
5346 if (download_mode
== XT_DM_START
)
5347 ret
= download_start(t
, download_entry
, XT_DL_START
);
5348 else if (download_mode
== XT_DM_ASK
)
5349 ret
= download_ask(t
, download_entry
);
5350 else if (download_mode
== XT_DM_ADD
)
5351 show_oops(t
, "added %s to download manager",
5354 /* sync other download manager tabs */
5355 update_download_tabs(NULL
);
5358 * NOTE: never redirect/render the current tab before this
5359 * function returns. This will cause the download to never start.
5361 return (ret
); /* start download */
5365 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
5367 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
5370 show_oops(NULL
, "webview_hover_cb");
5375 set_status(t
, "Link: %s", uri
);
5377 if (statusbar_style
== XT_STATUSBAR_URL
) {
5378 const gchar
*page_uri
;
5380 if ((page_uri
= get_uri(t
)) != NULL
)
5381 set_status(t
, "%s", page_uri
);
5383 set_status(t
, "%s", (char *)get_title(t
, FALSE
));
5388 mark(struct tab
*t
, struct karg
*arg
)
5395 if ((index
= marktoindex(mark
)) == -1)
5398 if (arg
->i
== XT_MARK_SET
)
5399 t
->mark
[index
] = gtk_adjustment_get_value(t
->adjust_v
);
5400 else if (arg
->i
== XT_MARK_GOTO
) {
5401 if (t
->mark
[index
] == XT_INVALID_MARK
) {
5402 show_oops(t
, "mark '%c' does not exist", mark
);
5405 /* XXX t->mark[index] can be bigger than the maximum if ajax or
5406 something changes the document size */
5407 pos
= gtk_adjustment_get_value(t
->adjust_v
);
5408 gtk_adjustment_set_value(t
->adjust_v
, t
->mark
[index
]);
5409 t
->mark
[marktoindex('\'')] = pos
;
5416 marks_clear(struct tab
*t
)
5420 for (i
= 0; i
< LENGTH(t
->mark
); i
++)
5421 t
->mark
[i
] = XT_INVALID_MARK
;
5427 char file
[PATH_MAX
];
5428 char *line
= NULL
, *p
;
5433 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
5434 if ((f
= fopen(file
, "r+")) == NULL
) {
5435 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
5439 for (i
= 1; ; i
++) {
5440 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
5442 if (strlen(line
) == 0 || line
[0] == '#') {
5448 p
= strtok(line
, " \t");
5450 if (p
== NULL
|| strlen(p
) != 1 ||
5451 (index
= qmarktoindex(*p
)) == -1) {
5452 warnx("corrupt quickmarks file, line %d", i
);
5456 p
= strtok(NULL
, " \t");
5457 if (qmarks
[index
] != NULL
)
5458 g_free(qmarks
[index
]);
5459 qmarks
[index
] = g_strdup(p
);
5470 char file
[PATH_MAX
];
5474 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
5475 if ((f
= fopen(file
, "r+")) == NULL
) {
5476 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
5480 for (i
= 0; i
< XT_NOQMARKS
; i
++)
5481 if (qmarks
[i
] != NULL
)
5482 fprintf(f
, "%c %s\n", indextoqmark(i
), qmarks
[i
]);
5490 qmark(struct tab
*t
, struct karg
*arg
)
5495 mark
= arg
->s
[strlen(arg
->s
)-1];
5496 index
= qmarktoindex(mark
);
5502 if (qmarks
[index
] != NULL
) {
5503 g_free(qmarks
[index
]);
5504 qmarks
[index
] = NULL
;
5507 qmarks_load(); /* sync if multiple instances */
5508 qmarks
[index
] = g_strdup(get_uri(t
));
5512 if (qmarks
[index
] != NULL
)
5513 load_uri(t
, qmarks
[index
]);
5515 show_oops(t
, "quickmark \"%c\" does not exist",
5521 if (qmarks
[index
] != NULL
)
5522 create_new_tab(qmarks
[index
], NULL
, 1, -1);
5524 show_oops(t
, "quickmark \"%c\" does not exist",
5535 go_up(struct tab
*t
, struct karg
*args
)
5543 if (args
->i
== XT_GO_UP_ROOT
)
5544 levels
= XT_GO_UP_ROOT
;
5545 else if ((levels
= atoi(args
->s
)) == 0)
5548 uri
= g_strdup(get_uri(t
));
5552 if ((tmp
= strstr(uri
, XT_PROTO_DELIM
)) == NULL
)
5555 tmp
+= strlen(XT_PROTO_DELIM
);
5557 /* it makes no sense to strip the last slash from ".../dir/", skip it */
5558 lastidx
= strlen(tmp
) - 1;
5560 if (tmp
[lastidx
] == '/')
5561 tmp
[lastidx
] = '\0';
5565 p
= strrchr(tmp
, '/');
5566 if (p
== tmp
) { /* Are we at the root of a file://-path? */
5569 } else if (p
!= NULL
)
5582 gototab(struct tab
*t
, struct karg
*args
)
5585 struct karg arg
= {0, NULL
, -1};
5587 tab
= atoi(args
->s
);
5590 arg
.i
= XT_TAB_NEXT
;
5602 zoom_amount(struct tab
*t
, struct karg
*arg
)
5604 struct karg narg
= {0, NULL
, -1};
5606 narg
.i
= atoi(arg
->s
);
5607 resizetab(t
, &narg
);
5613 flip_colon(struct tab
*t
, struct karg
*arg
)
5615 struct karg narg
= {0, NULL
, -1};
5618 if (t
== NULL
|| arg
== NULL
)
5621 p
= strstr(arg
->s
, ":");
5633 /* buffer commands receive the regex that triggered them in arg.s */
5634 char bcmd
[XT_BUFCMD_SZ
];
5638 #define XT_PRE_NO (0)
5639 #define XT_PRE_YES (1)
5640 #define XT_PRE_MAYBE (2)
5642 int (*func
)(struct tab
*, struct karg
*);
5646 { "^[0-9]*gu$", XT_PRE_MAYBE
, "gu", go_up
, 0 },
5647 { "^gU$", XT_PRE_NO
, "gU", go_up
, XT_GO_UP_ROOT
},
5648 { "^gg$", XT_PRE_NO
, "gg", move
, XT_MOVE_TOP
},
5649 { "^gG$", XT_PRE_NO
, "gG", move
, XT_MOVE_BOTTOM
},
5650 { "^[0-9]+%$", XT_PRE_YES
, "%", move
, XT_MOVE_PERCENT
},
5651 { "^zz$", XT_PRE_NO
, "zz", move
, XT_MOVE_CENTER
},
5652 { "^gh$", XT_PRE_NO
, "gh", go_home
, 0 },
5653 { "^m[a-zA-Z0-9]$", XT_PRE_NO
, "m", mark
, XT_MARK_SET
},
5654 { "^['][a-zA-Z0-9']$", XT_PRE_NO
, "'", mark
, XT_MARK_GOTO
},
5655 { "^[0-9]+t$", XT_PRE_YES
, "t", gototab
, 0 },
5656 { "^g0$", XT_PRE_YES
, "g0", movetab
, XT_TAB_FIRST
},
5657 { "^g[$]$", XT_PRE_YES
, "g$", movetab
, XT_TAB_LAST
},
5658 { "^[0-9]*gt$", XT_PRE_YES
, "t", movetab
, XT_TAB_NEXT
},
5659 { "^[0-9]*gT$", XT_PRE_YES
, "T", movetab
, XT_TAB_PREV
},
5660 { "^M[a-zA-Z0-9]$", XT_PRE_NO
, "M", qmark
, XT_QMARK_SET
},
5661 { "^go[a-zA-Z0-9]$", XT_PRE_NO
, "go", qmark
, XT_QMARK_OPEN
},
5662 { "^gn[a-zA-Z0-9]$", XT_PRE_NO
, "gn", qmark
, XT_QMARK_TAB
},
5663 { "^ZR$", XT_PRE_NO
, "ZR", restart
, 0 },
5664 { "^ZZ$", XT_PRE_NO
, "ZZ", quit
, 0 },
5665 { "^zi$", XT_PRE_NO
, "zi", resizetab
, XT_ZOOM_IN
},
5666 { "^zo$", XT_PRE_NO
, "zo", resizetab
, XT_ZOOM_OUT
},
5667 { "^z0$", XT_PRE_NO
, "z0", resizetab
, XT_ZOOM_NORMAL
},
5668 { "^[0-9]+Z$", XT_PRE_YES
, "Z", zoom_amount
, 0 },
5669 { "^[0-9]+:$", XT_PRE_YES
, ":", flip_colon
, 0 },
5673 buffercmd_init(void)
5677 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5678 if (regcomp(&buffercmds
[i
].cregex
, buffercmds
[i
].regex
,
5679 REG_EXTENDED
| REG_NOSUB
))
5680 startpage_add("invalid buffercmd regex %s",
5681 buffercmds
[i
].regex
);
5685 buffercmd_abort(struct tab
*t
)
5692 DNPRINTF(XT_D_BUFFERCMD
, "%s: clearing buffer\n", __func__
);
5694 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5697 cmd_prefix
= 0; /* clear prefix for non-buffer commands */
5698 if (t
->sbe
.buffercmd
)
5699 gtk_label_set_text(GTK_LABEL(t
->sbe
.buffercmd
), bcmd
);
5703 buffercmd_execute(struct tab
*t
, struct buffercmd
*cmd
)
5705 struct karg arg
= {0, NULL
, -1};
5708 arg
.s
= g_strdup(bcmd
);
5710 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_execute: buffer \"%s\" "
5711 "matches regex \"%s\", executing\n", bcmd
, cmd
->regex
);
5721 buffercmd_addkey(struct tab
*t
, guint keyval
)
5724 char s
[XT_BUFCMD_SZ
];
5726 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
))) {
5728 return (XT_CB_PASSTHROUGH
);
5731 if (keyval
== GDK_Escape
) {
5733 return (XT_CB_HANDLED
);
5736 /* key with modifier or non-ascii character */
5737 if (!isascii(keyval
)) {
5739 * XXX this looks wrong but fixes some sites like
5740 * http://www.seslisozluk.com/
5741 * that eat a shift or ctrl and end putting default focus in js
5742 * instead of ignoring the keystroke
5743 * so instead of return (XT_CB_PASSTHROUGH); eat the key
5745 return (XT_CB_HANDLED
);
5748 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: adding key \"%c\" "
5749 "to buffer \"%s\"\n", keyval
, bcmd
);
5751 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5752 if (bcmd
[i
] == '\0') {
5757 /* buffer full, ignore input */
5758 if (i
>= LENGTH(bcmd
) -1) {
5759 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: buffer full\n");
5761 return (XT_CB_HANDLED
);
5764 if (t
->sbe
.buffercmd
!= NULL
)
5765 gtk_label_set_text(GTK_LABEL(t
->sbe
.buffercmd
), bcmd
);
5767 /* find exact match */
5768 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5769 if (regexec(&buffercmds
[i
].cregex
, bcmd
,
5770 (size_t) 0, NULL
, 0) == 0) {
5771 buffercmd_execute(t
, &buffercmds
[i
]);
5775 /* find non exact matches to see if we need to abort ot not */
5776 for (i
= 0, match
= 0; i
< LENGTH(buffercmds
); i
++) {
5777 DNPRINTF(XT_D_BUFFERCMD
, "trying: %s\n", bcmd
);
5780 if (buffercmds
[i
].precount
== XT_PRE_MAYBE
) {
5781 if (isdigit(bcmd
[0])) {
5782 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5786 if (sscanf(bcmd
, "%s", s
) == 0)
5789 } else if (buffercmds
[i
].precount
== XT_PRE_YES
) {
5790 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5793 if (sscanf(bcmd
, "%s", s
) == 0)
5796 if (c
== -1 && buffercmds
[i
].precount
)
5798 if (!strncmp(s
, buffercmds
[i
].cmd
, strlen(s
)))
5801 DNPRINTF(XT_D_BUFFERCMD
, "got[%d] %d <%s>: %d %s\n",
5802 i
, match
, buffercmds
[i
].cmd
, c
, s
);
5805 DNPRINTF(XT_D_BUFFERCMD
, "aborting: %s\n", bcmd
);
5810 return (XT_CB_HANDLED
);
5814 * XXX we were seeing a bunch of focus issues with the toplevel
5815 * main_window losing its is-active and has-toplevel-focus properties.
5816 * This is the most correct and portable solution we could come up with
5817 * without relying on calling internal GTK functions (which we
5818 * couldn't link to in Linux).
5820 #if GTK_CHECK_VERSION(3, 0, 0)
5822 fake_focus_in(GtkWidget
*w
)
5824 if (fevent
== NULL
) {
5825 fevent
= gdk_event_new(GDK_FOCUS_CHANGE
);
5826 fevent
->focus_change
.window
=
5827 gtk_widget_get_window(main_window
);
5828 fevent
->focus_change
.type
= GDK_FOCUS_CHANGE
;
5829 fevent
->focus_change
.in
= TRUE
;
5831 gtk_widget_send_focus_change(main_window
, fevent
);
5836 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
5839 struct key_binding
*k
;
5842 * This sometimes gets randomly unset for whatever reason in GTK3.
5843 * If we're handling a keypress, the main window's is-active propery
5844 * *must* be true, or else many things will break.
5846 #if GTK_CHECK_VERSION(3, 0, 0)
5847 fake_focus_in(main_window
);
5850 /* handle keybindings if buffercmd is empty.
5851 if not empty, allow commands like C-n */
5852 if (bcmd
[0] == '\0' || ((e
->state
& (CTRL
| MOD1
)) != 0))
5853 TAILQ_FOREACH(k
, &kbl
, entry
)
5854 if (e
->keyval
== k
->key
5855 && (entry
? k
->use_in_entry
: 1)) {
5856 /* when we are edditing eat ctrl/mod keys */
5857 if (edit_mode
== XT_EM_VI
&&
5858 t
->mode
== XT_MODE_INSERT
&&
5859 (e
->state
& CTRL
|| e
->state
& MOD1
))
5860 return (XT_CB_PASSTHROUGH
);
5863 if ((e
->state
& (CTRL
| MOD1
)) == 0)
5865 } else if ((e
->state
& k
->mask
) == k
->mask
) {
5870 if (!entry
&& ((e
->state
& (CTRL
| MOD1
)) == 0))
5871 return (buffercmd_addkey(t
, e
->keyval
));
5873 return (XT_CB_PASSTHROUGH
);
5876 if (k
->cmd
[0] == ':') {
5878 args
.s
= &k
->cmd
[1];
5879 return (command(t
, &args
));
5881 return (cmd_execute(t
, k
->cmd
));
5885 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5889 /* don't use w directly; use t->whatever instead */
5892 show_oops(NULL
, "wv_keypress_cb");
5893 return (XT_CB_PASSTHROUGH
);
5899 return (t
->mode_cb(t
, e
, t
->mode_cb_data
));
5901 DNPRINTF(XT_D_KEY
, "wv_keypress_cb: mode %d keyval 0x%x mask "
5902 "0x%x tab %d\n", t
->mode
, e
->keyval
, e
->state
, t
->tab_id
);
5904 /* Hide buffers, if they are visible, with escape. */
5905 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)) &&
5906 CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
5907 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5909 return (XT_CB_HANDLED
);
5912 if ((CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Tab
) ||
5913 (CLEAN(e
->state
) == SHFT
&& e
->keyval
== GDK_Tab
))
5914 /* something focussy is about to happen */
5915 return (XT_CB_PASSTHROUGH
);
5917 /* check if we are some sort of text input thing in the dom */
5918 input_check_mode(t
);
5920 if (t
->mode
== XT_MODE_PASSTHROUGH
) {
5921 if (CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
)
5922 t
->mode
= XT_MODE_COMMAND
;
5923 return (XT_CB_PASSTHROUGH
);
5924 } else if (t
->mode
== XT_MODE_COMMAND
|| t
->mode
== XT_MODE_HINT
) {
5926 snprintf(s
, sizeof s
, "%c", e
->keyval
);
5927 if (CLEAN(e
->state
) == 0 && isdigit(s
[0]))
5928 cmd_prefix
= 10 * cmd_prefix
+ atoi(s
);
5929 return (handle_keypress(t
, e
, 0));
5932 return (handle_keypress(t
, e
, 1));
5936 return (XT_CB_PASSTHROUGH
);
5940 hint_continue(struct tab
*t
)
5942 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5944 const gchar
*errstr
= NULL
;
5948 if (!(c
[0] == '.' || c
[0] == ','))
5950 if (strlen(c
) == 1) {
5951 /* XXX should not happen */
5956 if (isdigit(c
[1])) {
5958 i
= strtonum(&c
[1], 1, 4096, &errstr
);
5960 show_oops(t
, "invalid numerical hint %s", &c
[1]);
5963 s
= g_strdup_printf("hints.updateHints(%d);", i
);
5967 /* alphanumeric input */
5968 s
= g_strdup_printf("hints.createHints('%s', '%c');",
5969 &c
[1], c
[0] == '.' ? 'f' : 'F');
5980 search_continue(struct tab
*t
)
5982 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5983 gboolean rv
= FALSE
;
5985 if (c
[0] == ':' || c
[0] == '.' || c
[0] == ',')
5987 if (strlen(c
) == 1) {
5988 webkit_web_view_unmark_text_matches(t
->wv
);
5993 t
->search_forward
= TRUE
;
5994 else if (c
[0] == '?')
5995 t
->search_forward
= FALSE
;
6005 search_cb(struct tab
*t
)
6007 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
6008 #if !GTK_CHECK_VERSION(3, 0, 0)
6012 if (search_continue(t
) == FALSE
)
6016 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, t
->search_forward
,
6018 /* not found, mark red */
6019 #if GTK_CHECK_VERSION(3, 0, 0)
6020 gtk_widget_set_name(t
->cmd
, XT_CSS_RED
);
6022 gdk_color_parse(XT_COLOR_RED
, &color
);
6023 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
6025 /* unmark and remove selection */
6026 webkit_web_view_unmark_text_matches(t
->wv
);
6027 /* my kingdom for a way to unselect text in webview */
6029 /* found, highlight all */
6030 webkit_web_view_unmark_text_matches(t
->wv
);
6031 webkit_web_view_mark_text_matches(t
->wv
, &c
[1], FALSE
, 0);
6032 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
6033 #if GTK_CHECK_VERSION(3, 0, 0)
6034 gtk_widget_set_name(t
->cmd
, XT_CSS_NORMAL
);
6036 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
6037 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
6046 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
6048 const gchar
*c
= gtk_entry_get_text(w
);
6051 show_oops(NULL
, "cmd_keyrelease_cb invalid parameters");
6052 return (XT_CB_PASSTHROUGH
);
6055 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
6056 e
->keyval
, e
->state
, t
->tab_id
);
6059 if (!(e
->keyval
== GDK_Tab
|| e
->keyval
== GDK_ISO_Left_Tab
)) {
6060 if (hint_continue(t
) == FALSE
)
6065 if (search_continue(t
) == FALSE
)
6068 /* if search length is > 4 then no longer play timeout games */
6069 if (strlen(c
) > 4) {
6071 g_source_remove(t
->search_id
);
6078 /* reestablish a new timer if the user types fast */
6080 g_source_remove(t
->search_id
);
6081 t
->search_id
= g_timeout_add(250, (GSourceFunc
)search_cb
, (gpointer
)t
);
6084 return (XT_CB_PASSTHROUGH
);
6088 match_uri(const gchar
*uri
, const gchar
*key
) {
6091 gboolean match
= FALSE
;
6095 if (!strncmp(key
, uri
, len
))
6098 voffset
= strstr(uri
, "/") + 2;
6099 if (!strncmp(key
, voffset
, len
))
6101 else if (g_str_has_prefix(voffset
, "www.")) {
6102 voffset
= voffset
+ strlen("www.");
6103 if (!strncmp(key
, voffset
, len
))
6112 match_session(const gchar
*name
, const gchar
*key
) {
6115 sub
= strcasestr(name
, key
);
6121 cmd_getlist(int id
, char *key
)
6128 if (cmds
[id
].type
& XT_URLARG
) {
6129 RB_FOREACH_REVERSE(h
, history_list
, &hl
)
6130 if (match_uri(h
->uri
, key
)) {
6131 cmd_status
.list
[c
] = (char *)h
->uri
;
6137 } else if (cmds
[id
].type
& XT_SESSARG
) {
6138 TAILQ_FOREACH(s
, &sessions
, entry
)
6139 if (match_session(s
->name
, key
)) {
6140 cmd_status
.list
[c
] = (char *)s
->name
;
6146 } else if (cmds
[id
].type
& XT_SETARG
) {
6147 for (i
= 0; i
< get_settings_size(); i
++)
6148 if (!strncmp(key
, get_setting_name(i
),
6150 cmd_status
.list
[c
++] =
6151 get_setting_name(i
);
6157 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
6159 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
6160 if (cmds
[i
].level
< dep
)
6162 if (cmds
[i
].level
== dep
&& !strncmp(key
, cmds
[i
].cmd
,
6163 strlen(key
)) && !isdigit(cmds
[i
].cmd
[0]))
6164 cmd_status
.list
[c
++] = cmds
[i
].cmd
;
6172 cmd_getnext(int dir
)
6174 cmd_status
.index
+= dir
;
6176 if (cmd_status
.index
< 0)
6177 cmd_status
.index
= cmd_status
.len
- 1;
6178 else if (cmd_status
.index
>= cmd_status
.len
)
6179 cmd_status
.index
= 0;
6181 return cmd_status
.list
[cmd_status
.index
];
6185 cmd_tokenize(char *s
, char *tokens
[])
6188 char *tok
, *last
= NULL
;
6189 size_t len
= strlen(s
);
6192 blank
= len
== 0 || (len
> 0 && s
[len
- 1] == ' ');
6193 for (tok
= strtok_r(s
, " ", &last
); tok
&& i
< 3;
6194 tok
= strtok_r(NULL
, " ", &last
), i
++)
6204 cmd_complete(struct tab
*t
, char *str
, int dir
)
6206 GtkEntry
*w
= GTK_ENTRY(t
->cmd
);
6207 int i
, j
, levels
, c
= 0, dep
= 0, parent
= -1;
6209 char *tok
, *match
, *s
= g_strdup(str
);
6211 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
6214 DNPRINTF(XT_D_CMD
, "%s: complete %s\n", __func__
, str
);
6217 for (i
= 0; isdigit(s
[i
]); i
++)
6220 for (; isspace(s
[i
]); i
++)
6225 levels
= cmd_tokenize(s
, tokens
);
6227 for (i
= 0; i
< levels
- 1; i
++) {
6230 for (j
= c
; j
< LENGTH(cmds
); j
++) {
6231 if (cmds
[j
].level
< dep
)
6233 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
,
6237 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
6244 if (matchcount
== 1) {
6245 strlcat(res
, tok
, sizeof res
);
6246 strlcat(res
, " ", sizeof res
);
6256 if (cmd_status
.index
== -1)
6257 cmd_getlist(parent
, tokens
[i
]);
6259 if (cmd_status
.len
> 0) {
6260 match
= cmd_getnext(dir
);
6261 strlcat(res
, match
, sizeof res
);
6262 gtk_entry_set_text(w
, res
);
6263 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6270 parse_prefix_and_alias(const char *str
, int *prefix
)
6272 struct cmd_alias
*c
;
6273 char *s
= g_strdup(str
), *sc
;
6278 if (isdigit(s
[0])) {
6279 sscanf(s
, "%d", prefix
);
6280 while (isdigit(s
[0]) || isspace(s
[0]))
6284 TAILQ_FOREACH(c
, &cal
, entry
) {
6285 if (strncmp(s
, c
->alias
, strlen(c
->alias
)))
6288 if (strlen(s
) == strlen(c
->alias
)) {
6290 return (g_strdup(c
->cmd
));
6293 if (!isspace(s
[strlen(c
->alias
)]))
6296 s
= g_strdup_printf("%s %s", c
->cmd
, &s
[strlen(c
->alias
) + 1]);
6306 cmd_execute(struct tab
*t
, char *str
)
6308 struct cmd
*cmd
= NULL
;
6309 char *tok
, *last
= NULL
, *s
= str
;
6310 int j
= 0, len
, c
= 0, dep
= 0, matchcount
= 0;
6311 int prefix
= -1, rv
= XT_CB_PASSTHROUGH
;
6312 struct karg arg
= {0, NULL
, -1};
6314 s
= parse_prefix_and_alias(s
, &prefix
);
6316 for (tok
= strtok_r(s
, " ", &last
); tok
;
6317 tok
= strtok_r(NULL
, " ", &last
)) {
6319 for (j
= c
; j
< LENGTH(cmds
); j
++) {
6320 if (cmds
[j
].level
< dep
)
6322 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1 :
6324 if (cmds
[j
].level
== dep
&&
6325 !strncmp(tok
, cmds
[j
].cmd
, len
)) {
6329 if (len
== strlen(cmds
[j
].cmd
)) {
6335 if (matchcount
== 1) {
6340 show_oops(t
, "Invalid command: %s", str
);
6346 show_oops(t
, "Empty command");
6352 arg
.precount
= prefix
;
6353 else if (cmd_prefix
> 0)
6354 arg
.precount
= cmd_prefix
;
6356 if (j
> 0 && !(cmd
->type
& XT_PREFIX
) && arg
.precount
> -1) {
6357 show_oops(t
, "No prefix allowed: %s", str
);
6361 arg
.s
= last
? g_strdup(last
) : g_strdup("");
6362 if (cmd
->type
& XT_INTARG
&& last
&& strlen(last
) > 0) {
6363 if (arg
.s
== NULL
) {
6364 show_oops(t
, "Invalid command");
6367 arg
.precount
= atoi(arg
.s
);
6368 if (arg
.precount
<= 0) {
6369 if (arg
.s
[0] == '0')
6370 show_oops(t
, "Zero count");
6372 show_oops(t
, "Trailing characters");
6377 DNPRINTF(XT_D_CMD
, "%s: prefix %d arg %s\n",
6378 __func__
, arg
.precount
, arg
.s
);
6394 save_runtime_setting(const char *name
, const char *val
)
6400 char file
[PATH_MAX
];
6401 char delim
[3] = { '\0', '\0', '\0' };
6402 char *line
, *lt
, *start
;
6403 char *contents
, *tmp
;
6405 if (runtime_settings
== NULL
|| strlen(runtime_settings
) == 0)
6408 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, runtime_settings
);
6409 if (stat(file
, &sb
) || (f
= fopen(file
, "r+")) == NULL
)
6411 lt
= g_strdup_printf("%s=%s", name
, val
);
6412 contents
= g_strdup("");
6414 line
= fparseln(f
, &linelen
, NULL
, delim
, 0);
6415 if (line
== NULL
|| linelen
== 0)
6418 start
= g_strdup_printf("%s=", name
);
6419 if (strstr(line
, start
) == NULL
)
6420 contents
= g_strdup_printf("%s%s\n", contents
, line
);
6423 contents
= g_strdup_printf("%s%s\n", contents
, lt
);
6432 contents
= g_strdup_printf("%s%s\n", contents
, lt
);
6435 if ((f
= freopen(file
, "w", f
)) == NULL
)
6448 entry_focus_cb(GtkWidget
*w
, GdkEvent e
, struct tab
*t
)
6451 * This sometimes gets randomly unset for whatever reason in GTK3,
6452 * causing a GtkEntry's text cursor becomes invisible. When we focus
6453 * a GtkEntry, be sure to manually reset the main window's is-active
6454 * property so the cursor is shown correctly.
6456 #if GTK_CHECK_VERSION(3, 0, 0)
6457 fake_focus_in(main_window
);
6459 return (XT_CB_PASSTHROUGH
);
6463 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
6466 show_oops(NULL
, "entry_key_cb invalid parameters");
6467 return (XT_CB_PASSTHROUGH
);
6470 DNPRINTF(XT_D_CMD
, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
6471 e
->keyval
, e
->state
, t
->tab_id
);
6475 if (e
->keyval
== GDK_Escape
) {
6476 /* don't use focus_webview(t) because we want to type :cmds */
6477 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6480 return (handle_keypress(t
, e
, 1));
6483 struct command_entry
*
6484 history_prev(struct command_list
*l
, struct command_entry
*at
)
6487 at
= TAILQ_LAST(l
, command_list
);
6489 at
= TAILQ_PREV(at
, command_list
, entry
);
6491 at
= TAILQ_LAST(l
, command_list
);
6497 struct command_entry
*
6498 history_next(struct command_list
*l
, struct command_entry
*at
)
6501 at
= TAILQ_FIRST(l
);
6503 at
= TAILQ_NEXT(at
, entry
);
6505 at
= TAILQ_FIRST(l
);
6512 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
6514 int rv
= XT_CB_HANDLED
;
6515 const gchar
*c
= gtk_entry_get_text(w
);
6519 show_oops(NULL
, "cmd_keypress_cb parameters");
6520 return (XT_CB_PASSTHROUGH
);
6523 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
6524 e
->keyval
, e
->state
, t
->tab_id
);
6528 e
->keyval
= GDK_Escape
;
6529 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
6530 c
[0] == '.' || c
[0] == ','))
6531 e
->keyval
= GDK_Escape
;
6533 if (e
->keyval
!= GDK_Tab
&& e
->keyval
!= GDK_Shift_L
&&
6534 e
->keyval
!= GDK_ISO_Left_Tab
)
6535 cmd_status
.index
= -1;
6537 switch (e
->keyval
) {
6540 cmd_complete(t
, (char *)&c
[1], 1);
6541 else if (c
[0] == '.' || c
[0] == ',')
6542 run_script(t
, "hints.focusNextHint();");
6544 case GDK_ISO_Left_Tab
:
6546 cmd_complete(t
, (char *)&c
[1], -1);
6547 else if (c
[0] == '.' || c
[0] == ',')
6548 run_script(t
, "hints.focusPreviousHint();");
6552 if ((search_at
= history_next(&shl
, search_at
))) {
6553 search_at
->line
[0] = c
[0];
6554 gtk_entry_set_text(w
, search_at
->line
);
6555 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6557 } else if (c
[0] == '/') {
6558 if ((search_at
= history_prev(&shl
, search_at
))) {
6559 search_at
->line
[0] = c
[0];
6560 gtk_entry_set_text(w
, search_at
->line
);
6561 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6563 } else if (c
[0] == ':') {
6564 if ((history_at
= history_prev(&chl
, history_at
))) {
6565 history_at
->line
[0] = c
[0];
6566 gtk_entry_set_text(w
, history_at
->line
);
6567 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6573 if ((search_at
= history_next(&shl
, search_at
))) {
6574 search_at
->line
[0] = c
[0];
6575 gtk_entry_set_text(w
, search_at
->line
);
6576 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6578 } else if (c
[0] == '?') {
6579 if ((search_at
= history_prev(&shl
, search_at
))) {
6580 search_at
->line
[0] = c
[0];
6581 gtk_entry_set_text(w
, search_at
->line
);
6582 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6584 } if (c
[0] == ':') {
6585 if ((history_at
= history_next(&chl
, history_at
))) {
6586 history_at
->line
[0] = c
[0];
6587 gtk_entry_set_text(w
, history_at
->line
);
6588 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6593 if (!(!strcmp(c
, ":") || !strcmp(c
, "/") || !strcmp(c
, "?") ||
6594 !strcmp(c
, ".") || !strcmp(c
, ","))) {
6595 /* see if we are doing hinting and reset it */
6596 if (c
[0] == '.' || c
[0] == ',') {
6597 /* recreate hints */
6598 s
= g_strdup_printf("hints.createHints('', "
6599 "'%c');", c
[0] == '.' ? 'f' : 'F');
6611 if (c
!= NULL
&& (c
[0] == '/' || c
[0] == '?'))
6612 webkit_web_view_unmark_text_matches(t
->wv
);
6614 /* no need to cancel hints */
6618 rv
= XT_CB_PASSTHROUGH
;
6624 wv_popup_activ_cb(GtkMenuItem
*menu
, struct tab
*t
)
6626 GtkAction
*a
= NULL
;
6627 GtkClipboard
*clipboard
, *primary
;
6628 const gchar
*name
, *uri
;
6630 a
= gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu
));
6633 name
= gtk_action_get_name(a
);
6635 DNPRINTF(XT_D_CMD
, "wv_popup_activ_cb: tab %d action %s\n",
6639 * context-menu-action-3 copy link location
6640 * context-menu-action-7 copy image address
6641 * context-menu-action-2030 copy video link location
6645 if ((g_strcmp0(name
, "context-menu-action-3") == 0) ||
6646 (g_strcmp0(name
, "context-menu-action-7") == 0) ||
6647 (g_strcmp0(name
, "context-menu-action-2030") == 0)) {
6648 clipboard
= gtk_clipboard_get(GDK_SELECTION_CLIPBOARD
);
6649 primary
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
6650 uri
= gtk_clipboard_wait_for_text(clipboard
);
6653 gtk_clipboard_set_text(primary
, uri
, -1);
6658 wv_popup_cb(WebKitWebView
*wview
, GtkMenu
*menu
, struct tab
*t
)
6662 DNPRINTF(XT_D_CMD
, "wv_popup_cb: tab %d\n", t
->tab_id
);
6664 items
= gtk_container_get_children(GTK_CONTAINER(menu
));
6665 for (l
= items
; l
; l
= l
->next
)
6666 g_signal_connect(l
->data
, "activate",
6667 G_CALLBACK(wv_popup_activ_cb
), t
);
6672 cmd_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
6674 /* popup menu enabled */
6679 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
6682 show_oops(NULL
, "cmd_focusout_cb invalid parameters");
6683 return (XT_CB_PASSTHROUGH
);
6686 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d popup %d\n",
6687 t
->tab_id
, t
->popup
);
6689 /* if popup is enabled don't lose focus */
6692 return (XT_CB_PASSTHROUGH
);
6699 return (XT_CB_PASSTHROUGH
);
6703 cmd_hide_cb(GtkWidget
*w
, struct tab
*t
)
6706 show_oops(NULL
, "%s: invalid parameters", __func__
);
6710 if (show_url
== 0 || t
->focus_wv
)
6713 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6717 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
6720 const gchar
*c
= gtk_entry_get_text(entry
);
6723 show_oops(NULL
, "cmd_activate_cb invalid parameters");
6727 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
6732 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
6733 c
[0] == '.' || c
[0] == ','))
6739 if (c
[0] == '/' || c
[0] == '?') {
6740 /* see if there is a timer pending */
6742 g_source_remove(t
->search_id
);
6747 if (t
->search_text
) {
6748 g_free(t
->search_text
);
6749 t
->search_text
= NULL
;
6752 t
->search_text
= g_strdup(s
);
6754 g_free(global_search
);
6755 global_search
= g_strdup(s
);
6756 t
->search_forward
= c
[0] == '/';
6758 history_add(&shl
, search_file
, s
, &search_history_count
);
6759 } else if (c
[0] == '.' || c
[0] == ',') {
6760 run_script(t
, "hints.fire();");
6761 /* XXX history for link following? */
6762 } else if (c
[0] == ':') {
6763 history_add(&chl
, command_file
, s
, &cmd_history_count
);
6764 /* can't call hide_cmd after cmd_execute */
6775 backward_cb(GtkWidget
*w
, struct tab
*t
)
6780 show_oops(NULL
, "backward_cb invalid parameters");
6784 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
6791 forward_cb(GtkWidget
*w
, struct tab
*t
)
6796 show_oops(NULL
, "forward_cb invalid parameters");
6800 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
6802 a
.i
= XT_NAV_FORWARD
;
6807 home_cb(GtkWidget
*w
, struct tab
*t
)
6810 show_oops(NULL
, "home_cb invalid parameters");
6814 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
6820 stop_cb(GtkWidget
*w
, struct tab
*t
)
6822 WebKitWebFrame
*frame
;
6825 show_oops(NULL
, "stop_cb invalid parameters");
6829 DNPRINTF(XT_D_NAV
, "stop_cb: tab %d\n", t
->tab_id
);
6831 frame
= webkit_web_view_get_main_frame(t
->wv
);
6832 if (frame
== NULL
) {
6833 show_oops(t
, "stop_cb: no frame");
6837 webkit_web_frame_stop_loading(frame
);
6838 abort_favicon_download(t
);
6842 setup_webkit(struct tab
*t
)
6844 if (is_g_object_setting(G_OBJECT(t
->settings
), "enable-dns-prefetching"))
6845 g_object_set(G_OBJECT(t
->settings
), "enable-dns-prefetching",
6846 FALSE
, (char *)NULL
);
6848 warnx("webkit does not have \"enable-dns-prefetching\" property");
6849 g_object_set(G_OBJECT(t
->settings
), "default-encoding", encoding
,
6851 g_object_set(G_OBJECT(t
->settings
),
6852 "enable-scripts", enable_scripts
, (char *)NULL
);
6853 g_object_set(G_OBJECT(t
->settings
),
6854 "enable-plugins", enable_plugins
, (char *)NULL
);
6855 g_object_set(G_OBJECT(t
->settings
),
6856 "javascript-can-open-windows-automatically", enable_scripts
,
6858 g_object_set(G_OBJECT(t
->settings
),
6859 "enable-html5-database", FALSE
, (char *)NULL
);
6860 g_object_set(G_OBJECT(t
->settings
),
6861 "enable-html5-local-storage", enable_localstorage
, (char *)NULL
);
6862 g_object_set(G_OBJECT(t
->settings
),
6863 "enable_spell_checking", enable_spell_checking
, (char *)NULL
);
6864 g_object_set(G_OBJECT(t
->settings
),
6865 "spell_checking_languages", spell_check_languages
, (char *)NULL
);
6866 g_object_set(G_OBJECT(t
->settings
),
6867 "enable-developer-extras", TRUE
, (char *)NULL
);
6868 g_object_set(G_OBJECT(t
->wv
),
6869 "full-content-zoom", TRUE
, (char *)NULL
);
6870 g_object_set(G_OBJECT(t
->settings
),
6871 "auto-load-images", auto_load_images
, (char *)NULL
);
6872 if (is_g_object_setting(G_OBJECT(t
->settings
),
6873 "enable-display-of-insecure-content"))
6874 g_object_set(G_OBJECT(t
->settings
),
6875 "enable-display-of-insecure-content",
6876 allow_insecure_content
, (char *)NULL
);
6877 if (is_g_object_setting(G_OBJECT(t
->settings
),
6878 "enable-running-of-insecure-content"))
6879 g_object_set(G_OBJECT(t
->settings
),
6880 "enable-running-of-insecure-content",
6881 allow_insecure_scripts
, (char *)NULL
);
6883 webkit_web_view_set_settings(t
->wv
, t
->settings
);
6887 update_statusbar_position(GtkAdjustment
* adjustment
, gpointer data
)
6889 struct tab
*ti
, *t
= NULL
;
6890 gdouble view_size
, value
, max
;
6893 TAILQ_FOREACH(ti
, &tabs
, entry
)
6894 if (ti
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
6902 if (adjustment
== NULL
)
6903 adjustment
= gtk_scrolled_window_get_vadjustment(
6904 GTK_SCROLLED_WINDOW(t
->browser_win
));
6906 view_size
= gtk_adjustment_get_page_size(adjustment
);
6907 value
= gtk_adjustment_get_value(adjustment
);
6908 max
= gtk_adjustment_get_upper(adjustment
) - view_size
;
6911 position
= g_strdup("All");
6912 else if (value
== max
)
6913 position
= g_strdup("Bot");
6914 else if (value
== 0)
6915 position
= g_strdup("Top");
6917 position
= g_strdup_printf("%d%%", (int) ((value
/ max
) * 100));
6919 if (t
->sbe
.position
!= NULL
)
6920 gtk_label_set_text(GTK_LABEL(t
->sbe
.position
), position
);
6927 create_window(const gchar
*name
)
6931 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
6932 if (window_maximize
)
6933 gtk_window_maximize(GTK_WINDOW(w
));
6935 gtk_window_set_default_size(GTK_WINDOW(w
), window_width
, window_height
);
6936 gtk_widget_set_name(w
, name
);
6937 gtk_window_set_wmclass(GTK_WINDOW(w
), name
, "Xombrero");
6943 create_browser(struct tab
*t
)
6946 GtkAdjustment
*adjustment
;
6949 show_oops(NULL
, "create_browser invalid parameters");
6953 w
= gtk_scrolled_window_new(NULL
, NULL
);
6954 gtk_widget_set_can_focus(w
, FALSE
);
6955 t
->adjust_h
= gtk_scrolled_window_get_hadjustment(
6956 GTK_SCROLLED_WINDOW(w
));
6957 t
->adjust_v
= gtk_scrolled_window_get_vadjustment(
6958 GTK_SCROLLED_WINDOW(w
));
6959 #if !GTK_CHECK_VERSION(3, 0, 0)
6960 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
6961 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
6965 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
6966 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
6969 t
->settings
= webkit_web_view_get_settings(t
->wv
);
6970 t
->stylesheet
= g_strdup(stylesheet
);
6971 t
->load_images
= auto_load_images
;
6974 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w
));
6975 g_signal_connect(G_OBJECT(adjustment
), "value-changed",
6976 G_CALLBACK(update_statusbar_position
), NULL
);
6985 create_kiosk_toolbar(struct tab
*t
)
6987 GtkWidget
*toolbar
= NULL
;
6990 #if GTK_CHECK_VERSION(3, 0, 0)
6991 toolbar
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 1);
6992 gtk_widget_set_name(toolbar
, "toolbar");
6994 toolbar
= gtk_hbox_new(FALSE
, 1);
6997 b
= GTK_BOX(toolbar
);
6998 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
7000 /* backward button */
7001 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
7002 gtk_widget_set_sensitive(t
->backward
, FALSE
);
7003 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
7004 G_CALLBACK(backward_cb
), t
);
7005 gtk_box_pack_start(b
, t
->backward
, TRUE
, TRUE
, 0);
7007 /* forward button */
7008 t
->forward
= create_button("Forward", GTK_STOCK_GO_FORWARD
, 0);
7009 gtk_widget_set_sensitive(t
->forward
, FALSE
);
7010 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
7011 G_CALLBACK(forward_cb
), t
);
7012 gtk_box_pack_start(b
, t
->forward
, TRUE
, TRUE
, 0);
7015 t
->gohome
= create_button("Home", GTK_STOCK_HOME
, 0);
7016 gtk_widget_set_sensitive(t
->gohome
, true);
7017 g_signal_connect(G_OBJECT(t
->gohome
), "clicked",
7018 G_CALLBACK(home_cb
), t
);
7019 gtk_box_pack_start(b
, t
->gohome
, TRUE
, TRUE
, 0);
7022 * Create widgets but don't use them. This is just so we don't get
7023 * loads of errors when trying to do stuff with them. Sink the floating
7024 * reference here, and do a manual unreference when the tab is deleted.
7026 t
->uri_entry
= gtk_entry_new();
7027 g_object_ref_sink(G_OBJECT(t
->uri_entry
));
7028 g_signal_connect(G_OBJECT(t
->uri_entry
), "focus-in-event",
7029 G_CALLBACK(entry_focus_cb
), t
);
7030 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
7031 g_object_ref_sink(G_OBJECT(t
->stop
));
7032 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
7033 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
7034 g_object_ref_sink(G_OBJECT(t
->stop
));
7036 #if !GTK_CHECK_VERSION(3, 0, 0)
7037 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
7044 create_toolbar(struct tab
*t
)
7046 GtkWidget
*toolbar
= NULL
;
7050 * t->stop, t->js_toggle, and t->uri_entry are shared by the kiosk
7051 * toolbar, but not shown or used. We still create them there so we can
7052 * avoid loads of warnings when trying to use them. Instead, we sink
7053 * the floating reference here, and do the final unreference when
7057 #if GTK_CHECK_VERSION(3, 0, 0)
7058 toolbar
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 1);
7059 gtk_widget_set_name(toolbar
, "toolbar");
7061 b
= gtk_hbox_new(FALSE
, 1);
7064 b
= GTK_BOX(toolbar
);
7065 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
7067 /* backward button */
7068 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
7069 gtk_widget_set_sensitive(t
->backward
, FALSE
);
7070 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
7071 G_CALLBACK(backward_cb
), t
);
7072 gtk_box_pack_start(b
, t
->backward
, FALSE
, FALSE
, 0);
7074 /* forward button */
7075 t
->forward
= create_button("Forward",GTK_STOCK_GO_FORWARD
, 0);
7076 gtk_widget_set_sensitive(t
->forward
, FALSE
);
7077 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
7078 G_CALLBACK(forward_cb
), t
);
7079 gtk_box_pack_start(b
, t
->forward
, FALSE
, FALSE
, 0);
7082 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
7083 g_object_ref_sink(G_OBJECT(t
->stop
));
7084 gtk_widget_set_sensitive(t
->stop
, FALSE
);
7085 g_signal_connect(G_OBJECT(t
->stop
), "clicked", G_CALLBACK(stop_cb
), t
);
7086 gtk_box_pack_start(b
, t
->stop
, FALSE
, FALSE
, 0);
7089 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
7090 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
7091 g_object_ref_sink(G_OBJECT(t
->js_toggle
));
7092 gtk_widget_set_sensitive(t
->js_toggle
, TRUE
);
7093 g_signal_connect(G_OBJECT(t
->js_toggle
), "clicked",
7094 G_CALLBACK(js_toggle_cb
), t
);
7095 gtk_box_pack_start(b
, t
->js_toggle
, FALSE
, FALSE
, 0);
7097 /* toggle proxy button */
7098 t
->proxy_toggle
= create_button("Proxy-Toggle", proxy_uri
?
7099 GTK_STOCK_CONNECT
: GTK_STOCK_DISCONNECT
, 0);
7100 /* override icons */
7102 button_set_file(t
->proxy_toggle
, "torenabled.ico");
7104 button_set_file(t
->proxy_toggle
, "tordisabled.ico");
7105 gtk_widget_set_sensitive(t
->proxy_toggle
, TRUE
);
7106 g_signal_connect(G_OBJECT(t
->proxy_toggle
), "clicked",
7107 G_CALLBACK(proxy_toggle_cb
), t
);
7108 gtk_box_pack_start(b
, t
->proxy_toggle
, FALSE
, FALSE
, 0);
7110 t
->uri_entry
= gtk_entry_new();
7111 g_object_ref_sink(G_OBJECT(t
->uri_entry
));
7112 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
7113 G_CALLBACK(activate_uri_entry_cb
), t
);
7114 g_signal_connect(G_OBJECT(t
->uri_entry
), "key-press-event",
7115 G_CALLBACK(entry_key_cb
), t
);
7116 g_signal_connect(G_OBJECT(t
->uri_entry
), "focus-in-event",
7117 G_CALLBACK(entry_focus_cb
), t
);
7119 gtk_box_pack_start(b
, t
->uri_entry
, TRUE
, TRUE
, 0);
7122 t
->search_entry
= gtk_entry_new();
7123 gtk_entry_set_width_chars(GTK_ENTRY(t
->search_entry
), 30);
7124 g_signal_connect(G_OBJECT(t
->search_entry
), "activate",
7125 G_CALLBACK(activate_search_entry_cb
), t
);
7126 g_signal_connect(G_OBJECT(t
->search_entry
), "key-press-event",
7127 G_CALLBACK(entry_key_cb
), t
);
7128 g_signal_connect(G_OBJECT(t
->search_entry
), "focus-in-event",
7129 G_CALLBACK(entry_focus_cb
), t
);
7130 gtk_box_pack_start(b
, t
->search_entry
, FALSE
, FALSE
, 0);
7132 #if !GTK_CHECK_VERSION(3, 0, 0)
7133 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
7140 create_buffers(struct tab
*t
)
7142 GtkCellRenderer
*renderer
;
7145 view
= gtk_tree_view_new();
7147 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view
), FALSE
);
7149 renderer
= gtk_cell_renderer_text_new();
7150 gtk_tree_view_insert_column_with_attributes
7151 (GTK_TREE_VIEW(view
), -1, "Id", renderer
, "text", COL_ID
, (char *)NULL
);
7153 renderer
= gtk_cell_renderer_pixbuf_new();
7154 gtk_tree_view_insert_column_with_attributes
7155 (GTK_TREE_VIEW(view
), -1, "Favicon", renderer
, "pixbuf", COL_FAVICON
,
7158 renderer
= gtk_cell_renderer_text_new();
7159 gtk_tree_view_insert_column_with_attributes
7160 (GTK_TREE_VIEW(view
), -1, "Title", renderer
, "text", COL_TITLE
,
7163 gtk_tree_view_set_model
7164 (GTK_TREE_VIEW(view
), GTK_TREE_MODEL(buffers_store
));
7170 row_activated_cb(GtkTreeView
*view
, GtkTreePath
*path
,
7171 GtkTreeViewColumn
*col
, struct tab
*t
)
7176 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
7178 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
,
7181 (GTK_TREE_MODEL(buffers_store
), &iter
, COL_ID
, &id
, -1);
7182 set_current_tab(id
- 1);
7188 /* after tab reordering/creation/removal */
7194 TAILQ_FOREACH(t
, &tabs
, entry
)
7195 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
7199 update_statusbar_tabs(struct tab
*t
)
7201 int tab_id
, max_tab_id
;
7207 tab_id
= gtk_notebook_get_current_page(notebook
);
7209 max_tab_id
= gtk_notebook_get_n_pages(notebook
);
7210 snprintf(s
, sizeof s
, "%d/%d", tab_id
+ 1, max_tab_id
);
7213 t
= get_current_tab();
7215 if (t
!= NULL
&& t
->sbe
.tabs
!= NULL
)
7216 gtk_label_set_text(GTK_LABEL(t
->sbe
.tabs
), s
);
7219 /* after active tab change */
7221 recolor_compact_tabs(void)
7225 #if !GTK_CHECK_VERSION(3, 0, 0)
7226 GdkColor color_active
, color_inactive
;
7228 gdk_color_parse(XT_COLOR_CT_ACTIVE
, &color_active
);
7229 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color_inactive
);
7231 curid
= gtk_notebook_get_current_page(notebook
);
7233 TAILQ_FOREACH(t
, &tabs
, entry
) {
7234 #if GTK_CHECK_VERSION(3, 0, 0)
7235 if (t
->tab_id
== curid
)
7236 gtk_widget_set_name(t
->tab_elems
.label
, XT_CSS_ACTIVE
);
7238 gtk_widget_set_name(t
->tab_elems
.label
, "");
7240 if (t
->tab_id
== curid
)
7241 gtk_widget_modify_fg(t
->tab_elems
.label
,
7242 GTK_STATE_NORMAL
, &color_active
);
7244 gtk_widget_modify_fg(t
->tab_elems
.label
,
7245 GTK_STATE_NORMAL
, &color_inactive
);
7251 set_current_tab(int page_num
)
7253 buffercmd_abort(get_current_tab());
7254 gtk_notebook_set_current_page(notebook
, page_num
);
7255 recolor_compact_tabs();
7256 update_statusbar_tabs(NULL
);
7260 undo_close_tab_save(struct tab
*t
)
7264 struct undo
*u1
, *u2
;
7267 WebKitWebHistoryItem
*el
;
7269 if ((uri
= get_uri(t
)) == NULL
)
7272 u1
= g_malloc0(sizeof(struct undo
));
7273 u1
->uri
= g_strdup(uri
);
7275 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
7277 m
= webkit_web_back_forward_list_get_forward_length(t
->bfl
);
7278 n
= webkit_web_back_forward_list_get_back_length(t
->bfl
);
7281 /* forward history */
7282 items
= webkit_web_back_forward_list_get_forward_list_with_limit(t
->bfl
, m
);
7283 for (item
= g_list_first(items
); item
; item
= item
->next
) {
7284 u1
->history
= g_list_prepend(u1
->history
,
7285 webkit_web_history_item_copy(item
->data
));
7291 el
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
7292 u1
->history
= g_list_prepend(u1
->history
,
7293 webkit_web_history_item_copy(el
));
7297 items
= webkit_web_back_forward_list_get_back_list_with_limit(t
->bfl
, n
);
7298 for (item
= g_list_first(items
); item
; item
= item
->next
) {
7299 u1
->history
= g_list_prepend(u1
->history
,
7300 webkit_web_history_item_copy(item
->data
));
7304 TAILQ_INSERT_HEAD(&undos
, u1
, entry
);
7306 if (undo_count
> XT_MAX_UNDO_CLOSE_TAB
) {
7307 u2
= TAILQ_LAST(&undos
, undo_tailq
);
7308 TAILQ_REMOVE(&undos
, u2
, entry
);
7310 g_list_free(u2
->history
);
7319 delete_tab(struct tab
*t
)
7323 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
7328 TAILQ_REMOVE(&tabs
, t
, entry
);
7331 /* Halt all webkit activity. */
7332 abort_favicon_download(t
);
7333 webkit_web_view_stop_loading(t
->wv
);
7335 /* Save the tab, so we can undo the close. */
7336 undo_close_tab_save(t
);
7340 g_source_remove(t
->search_id
);
7344 g_free(t
->session_key
);
7347 bzero(&a
, sizeof a
);
7349 inspector_cmd(t
, &a
);
7351 if (browser_mode
== XT_BM_KIOSK
) {
7352 gtk_widget_destroy(t
->uri_entry
);
7353 gtk_widget_destroy(t
->stop
);
7354 gtk_widget_destroy(t
->js_toggle
);
7357 /* widgets not shown in the kiosk toolbar */
7358 gtk_widget_destroy(t
->stop
);
7359 g_object_unref(G_OBJECT(t
->stop
));
7360 gtk_widget_destroy(t
->js_toggle
);
7361 g_object_unref(G_OBJECT(t
->js_toggle
));
7362 gtk_widget_destroy(t
->uri_entry
);
7363 g_object_unref(G_OBJECT(t
->uri_entry
));
7365 g_object_unref(G_OBJECT(t
->completion
));
7367 g_object_unref(t
->item
);
7369 gtk_widget_destroy(t
->tab_elems
.eventbox
);
7370 gtk_widget_destroy(t
->vbox
);
7372 g_free(t
->stylesheet
);
7378 if (TAILQ_EMPTY(&tabs
)) {
7379 if (browser_mode
== XT_BM_KIOSK
)
7380 create_new_tab(home
, NULL
, 1, -1);
7382 create_new_tab(NULL
, NULL
, 1, -1);
7385 /* recreate session */
7386 if (session_autosave
) {
7387 bzero(&a
, sizeof a
);
7389 save_tabs(NULL
, &a
);
7393 recolor_compact_tabs();
7397 update_statusbar_zoom(struct tab
*t
)
7400 char s
[16] = { '\0' };
7402 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
7403 if ((zoom
<= 0.99 || zoom
>= 1.01))
7404 snprintf(s
, sizeof s
, "%d%%", (int)(zoom
* 100));
7405 if (t
->sbe
.zoom
!= NULL
)
7406 gtk_label_set_text(GTK_LABEL(t
->sbe
.zoom
), s
);
7410 setzoom_webkit(struct tab
*t
, int adjust
)
7412 #define XT_ZOOMPERCENT 0.04
7417 show_oops(NULL
, "setzoom_webkit invalid parameters");
7421 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
7422 if (adjust
== XT_ZOOM_IN
)
7423 zoom
+= XT_ZOOMPERCENT
;
7424 else if (adjust
== XT_ZOOM_OUT
)
7425 zoom
-= XT_ZOOMPERCENT
;
7426 else if (adjust
> 0)
7427 zoom
= default_zoom_level
+ adjust
/ 100.0 - 1.0;
7429 show_oops(t
, "setzoom_webkit invalid zoom value");
7433 if (zoom
< XT_ZOOMPERCENT
)
7434 zoom
= XT_ZOOMPERCENT
;
7435 g_object_set(G_OBJECT(t
->wv
), "zoom-level", zoom
, (char *)NULL
);
7436 update_statusbar_zoom(t
);
7440 tab_clicked_cb(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
7442 struct tab
*t
= (struct tab
*) data
;
7444 DNPRINTF(XT_D_TAB
, "tab_clicked_cb: tab: %d\n", t
->tab_id
);
7446 switch (event
->button
) {
7448 set_current_tab(t
->tab_id
);
7459 append_tab(struct tab
*t
)
7464 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
7465 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
7473 sbe
= gtk_label_new(NULL
);
7474 gtk_widget_set_can_focus(GTK_WIDGET(sbe
), FALSE
);
7475 modify_font(GTK_WIDGET(sbe
), statusbar_font
);
7480 statusbar_create(struct tab
*t
)
7482 GtkWidget
*box
; /* container for statusbar elems */
7486 #if !GTK_CHECK_VERSION(3, 0, 0)
7491 DPRINTF("%s: invalid parameters", __func__
);
7495 #if GTK_CHECK_VERSION(3, 0, 0)
7496 t
->statusbar
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7497 box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7498 gtk_widget_set_name(GTK_WIDGET(t
->statusbar
), "statusbar");
7500 t
->statusbar
= gtk_hbox_new(FALSE
, 0);
7501 box
= gtk_hbox_new(FALSE
, 0);
7503 t
->sbe
.ebox
= gtk_event_box_new();
7505 gtk_widget_set_can_focus(GTK_WIDGET(t
->statusbar
), FALSE
);
7506 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.ebox
), FALSE
);
7507 gtk_widget_set_can_focus(GTK_WIDGET(box
), FALSE
);
7509 gtk_box_set_spacing(GTK_BOX(box
), 10);
7510 gtk_box_pack_start(GTK_BOX(t
->statusbar
), t
->sbe
.ebox
, TRUE
, TRUE
, 0);
7511 gtk_container_add(GTK_CONTAINER(t
->sbe
.ebox
), box
);
7513 t
->sbe
.uri
= gtk_entry_new();
7514 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.uri
), FALSE
);
7515 modify_font(GTK_WIDGET(t
->sbe
.uri
), statusbar_font
);
7516 #if !GTK_CHECK_VERSION(3, 0, 0)
7517 gtk_entry_set_inner_border(GTK_ENTRY(t
->sbe
.uri
), NULL
);
7518 gtk_entry_set_has_frame(GTK_ENTRY(t
->sbe
.uri
), FALSE
);
7521 #if GTK_CHECK_VERSION(3, 0, 0)
7522 statusbar_modify_attr(t
, XT_CSS_NORMAL
);
7524 statusbar_modify_attr(t
, XT_COLOR_WHITE
, XT_COLOR_BLACK
);
7528 gtk_box_pack_start(b
, t
->sbe
.uri
, TRUE
, TRUE
, 0);
7530 for (p
= statusbar_elems
; *p
!= '\0'; p
++) {
7533 #if GTK_CHECK_VERSION(3, 0, 0)
7534 sep
= gtk_separator_new(GTK_ORIENTATION_VERTICAL
);
7536 sep
= gtk_vseparator_new();
7537 gdk_color_parse(XT_COLOR_SB_SEPARATOR
, &color
);
7538 gtk_widget_modify_bg(sep
, GTK_STATE_NORMAL
, &color
);
7540 gtk_box_pack_start(b
, sep
, FALSE
, FALSE
, 0);
7543 if (t
->sbe
.position
== NULL
) {
7544 t
->sbe
.position
= create_sbe();
7545 gtk_box_pack_start(b
, t
->sbe
.position
, FALSE
,
7550 if (t
->sbe
.buffercmd
== NULL
) {
7551 t
->sbe
.buffercmd
= create_sbe();
7552 gtk_box_pack_start(b
, t
->sbe
.buffercmd
, FALSE
,
7557 if (t
->sbe
.zoom
== NULL
) {
7558 t
->sbe
.zoom
= create_sbe();
7559 gtk_box_pack_start(b
, t
->sbe
.zoom
, FALSE
, FALSE
,
7564 if (t
->sbe
.tabs
== NULL
) {
7565 t
->sbe
.tabs
= create_sbe();
7566 gtk_box_pack_start(b
, t
->sbe
.tabs
, FALSE
, FALSE
,
7571 if (t
->sbe
.proxy
== NULL
) {
7572 t
->sbe
.proxy
= create_sbe();
7573 gtk_box_pack_start(b
, t
->sbe
.proxy
, FALSE
,
7577 GTK_ENTRY(t
->sbe
.proxy
), "proxy");
7581 warnx("illegal flag \"%c\" in statusbar_elems\n", *p
);
7586 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar
, FALSE
, FALSE
, 0);
7592 create_new_tab(const char *title
, struct undo
*u
, int focus
, int position
)
7597 WebKitWebHistoryItem
*item
;
7600 #if !GTK_CHECK_VERSION(3, 0, 0)
7604 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
7606 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
7607 if (single_instance
) {
7609 "create_new_tab: new tab rejected\n");
7612 sv
[0] = start_argv
[0];
7613 sv
[1] = (char *)title
;
7614 sv
[2] = (char *)NULL
;
7615 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
,
7616 NULL
, NULL
, NULL
, NULL
))
7617 show_oops(NULL
, "%s: could not spawn process",
7622 t
= g_malloc0(sizeof *t
);
7624 if (title
== NULL
) {
7625 title
= "(untitled)";
7629 #if GTK_CHECK_VERSION(3, 0, 0)
7630 t
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
7631 b
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7632 gtk_widget_set_name(t
->vbox
, "vbox");
7634 t
->vbox
= gtk_vbox_new(FALSE
, 0);
7635 b
= gtk_hbox_new(FALSE
, 0);
7637 gtk_widget_set_can_focus(t
->vbox
, FALSE
);
7639 /* label + button for tab */
7641 gtk_widget_set_can_focus(t
->tab_content
, FALSE
);
7643 t
->user_agent_id
= 0;
7644 t
->http_accept_id
= 0;
7646 #if WEBKIT_CHECK_VERSION(1, 5, 0)
7650 #if GTK_CHECK_VERSION(2, 20, 0)
7651 t
->spinner
= gtk_spinner_new();
7653 t
->label
= gtk_label_new(title
);
7654 bb
= create_button("Close", GTK_STOCK_CLOSE
, 1);
7655 gtk_label_set_max_width_chars(GTK_LABEL(t
->label
), 20);
7656 gtk_label_set_ellipsize(GTK_LABEL(t
->label
), PANGO_ELLIPSIZE_END
);
7657 gtk_label_set_line_wrap(GTK_LABEL(t
->label
), FALSE
);
7658 gtk_widget_set_size_request(t
->tab_content
, 130, 0);
7661 * this is a total hack and most likely breaks with other styles but
7662 * is necessary so the text doesn't bounce around when the spinner is
7665 #if GTK_CHECK_VERSION(3, 0, 0)
7666 gtk_widget_set_size_request(t
->label
, 95, 0);
7668 gtk_widget_set_size_request(t
->label
, 100, 0);
7671 gtk_box_pack_start(GTK_BOX(t
->tab_content
), bb
, FALSE
, FALSE
, 0);
7672 gtk_box_pack_start(GTK_BOX(t
->tab_content
), t
->label
, FALSE
, FALSE
, 0);
7673 #if GTK_CHECK_VERSION(2, 20, 0)
7674 gtk_box_pack_end(GTK_BOX(b
), t
->spinner
, FALSE
, FALSE
, 0);
7678 if (browser_mode
== XT_BM_KIOSK
) {
7679 t
->toolbar
= create_kiosk_toolbar(t
);
7680 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
7683 t
->toolbar
= create_toolbar(t
);
7684 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
7692 t
->browser_win
= create_browser(t
);
7693 set_scrollbar_visibility(t
, show_scrollbars
);
7694 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
7696 /* oops message for user feedback */
7697 t
->oops
= gtk_entry_new();
7698 gtk_entry_set_inner_border(GTK_ENTRY(t
->oops
), NULL
);
7699 gtk_entry_set_has_frame(GTK_ENTRY(t
->oops
), FALSE
);
7700 gtk_widget_set_can_focus(GTK_WIDGET(t
->oops
), FALSE
);
7701 #if GTK_CHECK_VERSION(3, 0, 0)
7702 gtk_widget_set_name(t
->oops
, XT_CSS_RED
);
7704 gdk_color_parse(XT_COLOR_RED
, &color
);
7705 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
7707 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
7708 modify_font(GTK_WIDGET(t
->oops
), oops_font
);
7711 t
->cmd
= gtk_entry_new();
7712 g_signal_connect(G_OBJECT(t
->cmd
), "focus-in-event",
7713 G_CALLBACK(entry_focus_cb
), t
);
7714 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
7715 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
7716 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
7717 modify_font(GTK_WIDGET(t
->cmd
), cmd_font
);
7720 statusbar_create(t
);
7723 t
->buffers
= create_buffers(t
);
7724 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->buffers
, FALSE
, FALSE
, 0);
7726 /* xtp meaning is normal by default */
7727 set_normal_tab_meaning(t
);
7729 /* set empty favicon */
7730 xt_icon_from_name(t
, "text-html");
7732 /* and show it all */
7733 gtk_widget_show_all(b
);
7734 gtk_widget_show_all(t
->vbox
);
7737 gtk_widget_hide(t
->backward
);
7738 gtk_widget_hide(t
->forward
);
7739 gtk_widget_hide(t
->stop
);
7740 gtk_widget_hide(t
->js_toggle
);
7742 if (!fancy_bar
|| (search_string
== NULL
|| strlen(search_string
) == 0))
7743 gtk_widget_hide(t
->search_entry
);
7744 if (http_proxy
== NULL
&& http_proxy_save
== NULL
)
7745 gtk_widget_hide(t
->proxy_toggle
);
7747 /* compact tab bar */
7748 t
->tab_elems
.label
= gtk_label_new(title
);
7749 t
->tab_elems
.favicon
= gtk_image_new();
7751 t
->tab_elems
.eventbox
= gtk_event_box_new();
7752 gtk_widget_set_name(t
->tab_elems
.eventbox
, "compact_tab");
7753 #if GTK_CHECK_VERSION(3, 0, 0)
7754 t
->tab_elems
.box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7756 gtk_label_set_ellipsize(GTK_LABEL(t
->tab_elems
.label
),
7757 PANGO_ELLIPSIZE_END
);
7758 gtk_widget_override_font(t
->tab_elems
.label
, tabbar_font
);
7759 gtk_widget_set_halign(t
->tab_elems
.label
, GTK_ALIGN_START
);
7760 gtk_widget_set_valign(t
->tab_elems
.label
, GTK_ALIGN_START
);
7762 t
->tab_elems
.box
= gtk_hbox_new(FALSE
, 0);
7764 gtk_label_set_width_chars(GTK_LABEL(t
->tab_elems
.label
), 1);
7765 gtk_misc_set_alignment(GTK_MISC(t
->tab_elems
.label
), 0.0, 0.0);
7766 gtk_misc_set_padding(GTK_MISC(t
->tab_elems
.label
), 4.0, 4.0);
7767 modify_font(GTK_WIDGET(t
->tab_elems
.label
), tabbar_font
);
7769 gdk_color_parse(XT_COLOR_CT_BACKGROUND
, &color
);
7770 gtk_widget_modify_bg(t
->tab_elems
.eventbox
, GTK_STATE_NORMAL
, &color
);
7771 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
7772 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
, &color
);
7775 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.favicon
, FALSE
,
7777 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.label
, TRUE
,
7779 gtk_container_add(GTK_CONTAINER(t
->tab_elems
.eventbox
),
7782 gtk_box_pack_start(GTK_BOX(tab_bar_box
), t
->tab_elems
.eventbox
, TRUE
,
7784 gtk_widget_show_all(t
->tab_elems
.eventbox
);
7786 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
7789 id
= position
>= 0 ? position
:
7790 gtk_notebook_get_current_page(notebook
) + 1;
7791 if (id
> gtk_notebook_get_n_pages(notebook
))
7794 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
7795 gtk_notebook_insert_page(notebook
, t
->vbox
, t
->tab_content
,
7797 gtk_box_reorder_child(GTK_BOX(tab_bar_box
),
7798 t
->tab_elems
.eventbox
, id
);
7803 #if GTK_CHECK_VERSION(2, 20, 0)
7804 /* turn spinner off if we are a new tab without uri */
7806 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
7807 gtk_widget_hide(t
->spinner
);
7810 /* make notebook tabs reorderable */
7811 gtk_notebook_set_tab_reorderable(notebook
, t
->vbox
, TRUE
);
7813 /* compact tabs clickable */
7814 g_signal_connect(G_OBJECT(t
->tab_elems
.eventbox
),
7815 "button_press_event", G_CALLBACK(tab_clicked_cb
), t
);
7817 g_object_connect(G_OBJECT(t
->cmd
),
7818 "signal::button-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
7819 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb
), t
,
7820 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
7821 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb
), t
,
7822 "signal::activate", G_CALLBACK(cmd_activate_cb
), t
,
7823 "signal::populate-popup", G_CALLBACK(cmd_popup_cb
), t
,
7824 "signal::hide", G_CALLBACK(cmd_hide_cb
), t
,
7827 /* reuse wv_button_cb to hide oops */
7828 g_object_connect(G_OBJECT(t
->oops
),
7829 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7832 g_signal_connect(t
->buffers
,
7833 "row-activated", G_CALLBACK(row_activated_cb
), t
);
7834 g_object_connect(G_OBJECT(t
->buffers
),
7835 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
, (char *)NULL
);
7837 g_object_connect(G_OBJECT(t
->wv
),
7838 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
,
7839 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb
), t
,
7840 "signal::download-requested", G_CALLBACK(webview_download_cb
), t
,
7841 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), t
,
7842 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7843 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7844 "signal::resource-request-starting", G_CALLBACK(webview_rrs_cb
), t
,
7845 "signal::create-web-view", G_CALLBACK(webview_cwv_cb
), t
,
7846 "signal::close-web-view", G_CALLBACK(webview_closewv_cb
), t
,
7847 "signal::event", G_CALLBACK(webview_event_cb
), t
,
7848 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb
), t
,
7849 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7850 "signal::button_release_event", G_CALLBACK(wv_release_button_cb
), t
,
7851 "signal::populate-popup", G_CALLBACK(wv_popup_cb
), t
,
7853 g_signal_connect(t
->wv
,
7854 "notify::load-status", G_CALLBACK(notify_load_status_cb
), t
);
7855 g_signal_connect(t
->wv
,
7856 "notify::title", G_CALLBACK(notify_title_cb
), t
);
7857 t
->progress_handle
= g_signal_connect(t
->wv
,
7858 "notify::progress", G_CALLBACK(webview_progress_changed_cb
), t
);
7860 /* hijack the unused keys as if we were the browser */
7861 //g_object_connect(G_OBJECT(t->toolbar),
7862 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
7865 g_signal_connect(G_OBJECT(bb
), "button_press_event",
7866 G_CALLBACK(tab_close_cb
), t
);
7869 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
7870 /* restore the tab's history */
7871 if (u
&& u
->history
) {
7875 webkit_web_back_forward_list_add_item(t
->bfl
, item
);
7876 items
= g_list_next(items
);
7879 item
= g_list_nth_data(u
->history
, u
->back
);
7881 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
7884 g_list_free(u
->history
);
7886 webkit_web_back_forward_list_clear(t
->bfl
);
7888 /* check and show url and statusbar */
7889 url_set_visibility();
7890 statusbar_set_visibility();
7893 set_current_tab(t
->tab_id
);
7894 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
7898 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
7902 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
7909 if (userstyle_global
)
7912 recolor_compact_tabs();
7913 setzoom_webkit(t
, XT_ZOOM_NORMAL
);
7918 notebook_switchpage_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7924 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
7926 if (gtk_notebook_get_current_page(notebook
) == -1)
7929 TAILQ_FOREACH(t
, &tabs
, entry
) {
7930 if (t
->tab_id
== pn
) {
7931 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
7934 uri
= get_title(t
, TRUE
);
7935 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
7942 /* can't use focus_webview here */
7943 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
7945 update_statusbar_tabs(t
);
7952 notebook_pagereordered_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7955 struct tab
*t
= NULL
, *tt
;
7959 TAILQ_FOREACH(tt
, &tabs
, entry
)
7960 if (tt
->tab_id
== pn
) {
7966 DNPRINTF(XT_D_TAB
, "page_reordered_cb: tab: %d\n", t
->tab_id
);
7968 gtk_box_reorder_child(GTK_BOX(tab_bar_box
), t
->tab_elems
.eventbox
,
7971 update_statusbar_tabs(t
);
7975 menuitem_response(struct tab
*t
)
7977 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
7981 destroy_menu(GtkMenuShell
*m
, void *notused
)
7983 gtk_widget_destroy(GTK_WIDGET(m
));
7984 g_object_unref(G_OBJECT(m
));
7985 return (XT_CB_PASSTHROUGH
);
7989 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
7991 GtkWidget
*menu
= NULL
, *menu_items
;
7992 GdkEventButton
*bevent
;
7993 struct tab
**stabs
= NULL
;
7997 if (event
->type
== GDK_BUTTON_PRESS
) {
7998 bevent
= (GdkEventButton
*) event
;
7999 menu
= gtk_menu_new();
8000 g_object_ref_sink(G_OBJECT(menu
));
8002 num_tabs
= sort_tabs_by_page_num(&stabs
);
8003 for (i
= 0; i
< num_tabs
; ++i
) {
8004 if (stabs
[i
] == NULL
)
8006 if ((uri
= get_uri(stabs
[i
])) == NULL
)
8007 /* XXX make sure there is something to print */
8008 /* XXX add gui pages in here to look purdy */
8010 menu_items
= gtk_menu_item_new_with_label(uri
);
8011 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), menu_items
);
8012 gtk_widget_show(menu_items
);
8014 g_signal_connect_swapped(menu_items
,
8015 "activate", G_CALLBACK(menuitem_response
),
8016 (gpointer
)stabs
[i
]);
8020 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
,
8021 bevent
->button
, bevent
->time
);
8023 g_object_connect(G_OBJECT(menu
),
8024 "signal::selection-done", G_CALLBACK(destroy_menu
), NULL
,
8027 return (TRUE
/* eat event */);
8030 return (FALSE
/* propagate */);
8034 icon_size_map(int iconsz
)
8036 if (iconsz
<= GTK_ICON_SIZE_INVALID
||
8037 iconsz
> GTK_ICON_SIZE_DIALOG
)
8038 return (GTK_ICON_SIZE_SMALL_TOOLBAR
);
8044 create_button(const char *name
, const char *stockid
, int size
)
8046 GtkWidget
*button
, *image
;
8048 #if !GTK_CHECK_VERSION(3, 0, 0)
8052 #if !GTK_CHECK_VERSION(3, 0, 0)
8053 newstyle
= g_strdup_printf(
8054 "style \"%s-style\"\n"
8056 " GtkWidget::focus-padding = 0\n"
8057 " GtkWidget::focus-line-width = 0\n"
8061 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
8062 gtk_rc_parse_string(newstyle
);
8065 button
= gtk_button_new();
8066 gtk_widget_set_can_focus(button
, FALSE
);
8067 gtk_button_set_focus_on_click(GTK_BUTTON(button
), FALSE
);
8068 gtk_icon_size
= icon_size_map(size
? size
: icon_size
);
8070 image
= gtk_image_new_from_stock(stockid
, gtk_icon_size
);
8071 gtk_container_set_border_width(GTK_CONTAINER(button
), 0);
8072 gtk_button_set_image(GTK_BUTTON(button
), GTK_WIDGET(image
));
8073 gtk_widget_set_name(button
, name
);
8074 gtk_button_set_relief(GTK_BUTTON(button
), GTK_RELIEF_NONE
);
8080 button_set_file(GtkWidget
*button
, char *filename
)
8083 char file
[PATH_MAX
];
8085 snprintf(file
, sizeof file
, "%s" PS
"%s", resource_dir
, filename
);
8086 image
= gtk_image_new_from_file(file
);
8087 gtk_button_set_image(GTK_BUTTON(button
), image
);
8091 button_set_stockid(GtkWidget
*button
, char *stockid
)
8095 image
= gtk_image_new_from_stock(stockid
, icon_size_map(icon_size
));
8096 gtk_button_set_image(GTK_BUTTON(button
), image
);
8105 char file
[PATH_MAX
];
8107 #if !GTK_CHECK_VERSION(3, 0, 0)
8111 #if GTK_CHECK_VERSION(3, 0, 0)
8112 vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
8114 vbox
= gtk_vbox_new(FALSE
, 0);
8116 gtk_box_set_spacing(GTK_BOX(vbox
), 0);
8117 gtk_widget_set_can_focus(vbox
, FALSE
);
8118 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
8119 #if !GTK_CHECK_VERSION(3, 0, 0)
8120 /* XXX seems to be needed with gtk+2 */
8121 g_object_set(G_OBJECT(notebook
), "tab-border", 0, NULL
);
8123 gtk_notebook_set_scrollable(notebook
, TRUE
);
8124 gtk_notebook_set_show_border(notebook
, FALSE
);
8125 gtk_widget_set_can_focus(GTK_WIDGET(notebook
), FALSE
);
8127 abtn
= gtk_button_new();
8128 gtk_widget_set_can_focus(abtn
, FALSE
);
8129 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
8130 gtk_widget_set_name(abtn
, "Arrow");
8131 gtk_button_set_image(GTK_BUTTON(abtn
), arrow
);
8132 gtk_widget_set_size_request(abtn
, -1, 20);
8134 #if GTK_CHECK_VERSION(2, 20, 0)
8135 gtk_notebook_set_action_widget(notebook
, abtn
, GTK_PACK_END
);
8137 /* compact tab bar */
8138 tab_bar
= gtk_event_box_new();
8139 #if GTK_CHECK_VERSION(3, 0, 0)
8140 gtk_widget_set_name(tab_bar
, "tab_bar");
8141 tab_bar_box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
8143 gdk_color_parse(XT_COLOR_CT_SEPARATOR
, &color
);
8144 gtk_widget_modify_bg(tab_bar
, GTK_STATE_NORMAL
, &color
);
8145 tab_bar_box
= gtk_hbox_new(TRUE
, 0);
8147 gtk_container_add(GTK_CONTAINER(tab_bar
), tab_bar_box
);
8148 gtk_box_set_homogeneous(GTK_BOX(tab_bar_box
), TRUE
);
8149 gtk_box_set_spacing(GTK_BOX(tab_bar_box
), 2);
8151 gtk_box_pack_start(GTK_BOX(vbox
), tab_bar
, FALSE
, FALSE
, 0);
8152 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
8154 g_object_connect(G_OBJECT(notebook
),
8155 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb
), NULL
,
8157 g_object_connect(G_OBJECT(notebook
),
8158 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb
),
8159 NULL
, (char *)NULL
);
8160 g_signal_connect(G_OBJECT(abtn
), "button_press_event",
8161 G_CALLBACK(arrow_cb
), NULL
);
8163 main_window
= create_window("xombrero");
8164 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
8165 g_signal_connect(G_OBJECT(main_window
), "delete_event",
8166 G_CALLBACK(gtk_main_quit
), NULL
);
8167 #if GTK_CHECK_VERSION(3, 0, 0)
8168 gtk_window_set_has_resize_grip(GTK_WINDOW(main_window
), FALSE
);
8172 for (i
= 0; i
< LENGTH(icons
); i
++) {
8173 snprintf(file
, sizeof file
, "%s" PS
"%s", resource_dir
, icons
[i
]);
8174 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
8175 l
= g_list_append(l
, pb
);
8177 gtk_window_set_default_icon_list(l
);
8179 for (; l
; l
= l
->next
)
8180 g_object_unref(G_OBJECT(l
->data
));
8182 gtk_widget_show_all(abtn
);
8183 gtk_widget_show_all(main_window
);
8184 notebook_tab_set_visibility();
8187 #ifndef XT_SOCKET_DISABLE
8189 send_cmd_to_socket(char *cmd
)
8192 struct sockaddr_un sa
;
8194 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
8195 warnx("%s: socket", __func__
);
8199 sa
.sun_family
= AF_UNIX
;
8200 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
8201 work_dir
, XT_SOCKET_FILE
);
8204 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
8205 warnx("%s: connect", __func__
);
8209 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
8210 warnx("%s: send", __func__
);
8221 socket_watcher(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
8224 char str
[XT_MAX_URL_LENGTH
];
8225 socklen_t t
= sizeof(struct sockaddr_un
);
8226 struct sockaddr_un sa
;
8231 gint fd
= g_io_channel_unix_get_fd(source
);
8233 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
8238 if (getpeereid(s
, &uid
, &gid
) == -1) {
8242 if (uid
!= getuid() || gid
!= getgid()) {
8243 warnx("unauthorized user");
8249 warnx("not a valid user");
8253 n
= recv(s
, str
, sizeof(str
), 0);
8257 tt
= get_current_tab();
8258 cmd_execute(tt
, str
);
8266 struct sockaddr_un sa
;
8268 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
8269 warn("is_running: socket");
8273 sa
.sun_family
= AF_UNIX
;
8274 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
8275 work_dir
, XT_SOCKET_FILE
);
8278 /* connect to see if there is a listener */
8279 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
8280 rv
= 0; /* not running */
8282 rv
= 1; /* already running */
8293 struct sockaddr_un sa
;
8295 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
8296 warn("build_socket: socket");
8300 sa
.sun_family
= AF_UNIX
;
8301 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
8302 work_dir
, XT_SOCKET_FILE
);
8305 /* connect to see if there is a listener */
8306 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
8307 /* no listener so we will */
8308 unlink(sa
.sun_path
);
8310 if (bind(s
, (struct sockaddr
*)&sa
, len
) == -1) {
8311 warn("build_socket: bind");
8315 if (listen(s
, 1) == -1) {
8316 warn("build_socket: listen");
8334 if (stat(dir
, &sb
)) {
8335 #if defined __MINGW32__
8336 if (mkdir(dir
) == -1)
8338 if (mkdir(dir
, S_IRWXU
) == -1)
8340 err(1, "mkdir %s", dir
);
8342 err(1, "stat %s", dir
);
8344 if (S_ISDIR(sb
.st_mode
) == 0)
8345 errx(1, "%s not a dir", dir
);
8346 #if !defined __MINGW32__
8347 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
8348 warnx("fixing invalid permissions on %s", dir
);
8349 if (chmod(dir
, S_IRWXU
) == -1)
8350 err(1, "chmod %s", dir
);
8359 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
8363 #if GTK_CHECK_VERSION(3, 0, 0)
8367 GtkCssProvider
*provider
;
8368 GdkDisplay
*display
;
8371 char path
[PATH_MAX
];
8372 #if defined __MINGW32__
8373 GtkCssProvider
*windows_hacks
;
8376 provider
= gtk_css_provider_new();
8377 display
= gdk_display_get_default();
8378 screen
= gdk_display_get_default_screen(display
);
8379 snprintf(path
, sizeof path
, "%s" PS
"%s", resource_dir
, XT_CSS_FILE
);
8380 file
= g_file_new_for_path(path
);
8381 gtk_css_provider_load_from_file(provider
, file
, NULL
);
8382 gtk_style_context_add_provider_for_screen(screen
,
8383 GTK_STYLE_PROVIDER(provider
),
8384 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
8385 g_object_unref(G_OBJECT(provider
));
8386 #if defined __MINGW32__
8387 windows_hacks
= gtk_css_provider_new();
8388 gtk_css_provider_load_from_data(windows_hacks
,
8390 " border-width: 0px;\n"
8392 gtk_style_context_add_provider_for_screen(screen
,
8393 GTK_STYLE_PROVIDER(windows_hacks
),
8394 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
8395 g_object_unref(G_OBJECT(windows_hacks
));
8397 g_object_unref(G_OBJECT(file
));
8404 startpage_add("<b>Welcome to xombrero %s!</b><p>", version
);
8405 startpage_add("Details at "
8406 "<a href=https://opensource.conformal.com/wiki/xombrero>xombrero "
8407 "wiki page</a><p>");
8411 main(int argc
, char **argv
)
8414 int c
, optn
= 0, opte
= 0, focus
= 1;
8415 char conf
[PATH_MAX
] = { '\0' };
8416 char file
[PATH_MAX
];
8417 char sodversion
[32];
8418 char *env_proxy
= NULL
;
8423 start_argv
= (char * const *)argv
;
8429 gtk_init(&argc
, &argv
);
8431 gnutls_global_init();
8433 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
8436 RB_INIT(&downloads
);
8443 TAILQ_INIT(&sessions
);
8446 TAILQ_INIT(&aliases
);
8457 TAILQ_INIT(&force_https
);
8460 #ifndef XT_RESOURCE_LIMITS_DISABLE
8464 GIOChannel
*channel
;
8466 /* fiddle with ulimits */
8467 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
8470 /* just use them all */
8472 rlp
.rlim_cur
= OPEN_MAX
;
8474 rlp
.rlim_cur
= rlp
.rlim_max
;
8476 if (setrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
8478 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
8480 else if (rlp
.rlim_cur
< 1024)
8481 startpage_add("%s requires at least 1024 "
8482 "(2048 recommended) file " "descriptors, "
8483 "currently it has up to %d available",
8484 __progname
, rlp
.rlim_cur
);
8488 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
8497 #ifdef XOMBRERO_BUILDSTR
8498 errx(0 , "Version: %s Build: %s",
8499 version
, XOMBRERO_BUILDSTR
);
8501 errx(0 , "Version: %s", version
);
8505 strlcpy(conf
, optarg
, sizeof(conf
));
8508 strlcpy(named_session
, optarg
, sizeof(named_session
));
8529 pwd
= getpwuid(getuid());
8531 errx(1, "invalid user %d", getuid());
8533 /* set download dir */
8534 if (strlen(download_dir
) == 0)
8535 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
8537 /* compile buffer command regexes */
8540 /* set default dynamic string settings */
8541 home
= g_strdup(XT_DS_HOME
);
8542 search_string
= g_strdup(XT_DS_SEARCH_STRING
);
8543 strlcpy(runtime_settings
, "runtime", sizeof runtime_settings
);
8544 cmd_font_name
= g_strdup(XT_DS_CMD_FONT_NAME
);
8545 oops_font_name
= g_strdup(XT_DS_OOPS_FONT_NAME
);
8546 statusbar_font_name
= g_strdup(XT_DS_STATUSBAR_FONT_NAME
);
8547 tabbar_font_name
= g_strdup(XT_DS_TABBAR_FONT_NAME
);
8548 statusbar_elems
= g_strdup("BP");
8549 spell_check_languages
= g_strdup(XT_DS_SPELL_CHECK_LANGUAGES
);
8550 encoding
= g_strdup(XT_DS_ENCODING
);
8551 spell_check_languages
= g_strdup(XT_DS_SPELL_CHECK_LANGUAGES
);
8552 path
= g_strdup_printf("%s" PS
"style.css", resource_dir
);
8553 userstyle
= g_filename_to_uri(path
, NULL
, NULL
);
8555 stylesheet
= g_strdup(userstyle
);
8557 /* set statically allocated (struct special) settings */
8558 if (strlen(default_script
) == 0)
8559 expand_tilde(default_script
, sizeof default_script
,
8560 XT_DS_DEFAULT_SCRIPT
);
8561 if (strlen(ssl_ca_file
) == 0)
8562 expand_tilde(ssl_ca_file
, sizeof ssl_ca_file
,
8566 session
= webkit_get_default_session();
8568 /* read config file */
8569 if (strlen(conf
) == 0)
8570 snprintf(conf
, sizeof conf
, "%s" PS
".%s",
8571 pwd
->pw_dir
, XT_CONF_FILE
);
8572 config_parse(conf
, 0);
8574 /* read preloaded HSTS list */
8575 if (preload_strict_transport
) {
8576 snprintf(conf
, sizeof conf
, "%s" PS
"%s",
8577 resource_dir
, XT_HSTS_PRELOAD_FILE
);
8578 config_parse(conf
, 0);
8581 /* check whether to read in a crapton of additional http headers */
8582 if (anonymize_headers
) {
8583 snprintf(conf
, sizeof conf
, "%s" PS
"%s",
8584 resource_dir
, XT_USER_AGENT_FILE
);
8585 config_parse(conf
, 0);
8586 snprintf(conf
, sizeof conf
, "%s" PS
"%s",
8587 resource_dir
, XT_HTTP_ACCEPT_FILE
);
8588 config_parse(conf
, 0);
8592 cmd_font
= pango_font_description_from_string(cmd_font_name
);
8593 oops_font
= pango_font_description_from_string(oops_font_name
);
8594 statusbar_font
= pango_font_description_from_string(statusbar_font_name
);
8595 tabbar_font
= pango_font_description_from_string(tabbar_font_name
);
8597 /* working directory */
8598 if (strlen(work_dir
) == 0)
8599 snprintf(work_dir
, sizeof work_dir
, "%s" PS
"%s",
8600 pwd
->pw_dir
, XT_DIR
);
8603 /* icon cache dir */
8604 snprintf(cache_dir
, sizeof cache_dir
, "%s" PS
"%s", work_dir
, XT_CACHE_DIR
);
8608 snprintf(certs_dir
, sizeof certs_dir
, "%s" PS
"%s", work_dir
, XT_CERT_DIR
);
8611 /* cert changes dir */
8612 snprintf(certs_cache_dir
, sizeof certs_cache_dir
, "%s" PS
"%s",
8613 work_dir
, XT_CERT_CACHE_DIR
);
8614 xxx_dir(certs_cache_dir
);
8617 snprintf(sessions_dir
, sizeof sessions_dir
, "%s" PS
"%s",
8618 work_dir
, XT_SESSIONS_DIR
);
8619 xxx_dir(sessions_dir
);
8622 snprintf(js_dir
, sizeof js_dir
, "%s" PS
"%s", work_dir
, XT_JS_DIR
);
8626 snprintf(temp_dir
, sizeof temp_dir
, "%s" PS
"%s", work_dir
, XT_TEMP_DIR
);
8629 /* runtime settings that can override config file */
8630 if (runtime_settings
[0] != '\0')
8631 config_parse(runtime_settings
, 1);
8634 if (!strcmp(download_dir
, pwd
->pw_dir
))
8635 strlcat(download_dir
, PS
"downloads", sizeof download_dir
);
8636 xxx_dir(download_dir
);
8638 /* first start file */
8639 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_SOD_FILE
);
8640 if (stat(file
, &sb
)) {
8641 warnx("start of day file doesn't exist, creating it");
8642 if ((f
= fopen(file
, "w")) == NULL
)
8643 err(1, "startofday");
8644 if (fputs(version
, f
) == EOF
)
8651 if ((f
= fopen(file
, "r+")) == NULL
)
8652 err(1, "startofday");
8653 if (fgets(sodversion
, sizeof sodversion
, f
) == NULL
)
8655 sodversion
[strcspn(sodversion
, "\n")] = '\0';
8656 if (strcmp(version
, sodversion
)) {
8657 if ((f
= freopen(file
, "w", f
)) == NULL
)
8658 err(1, "startofday");
8659 if (fputs(version
, f
) == EOF
)
8662 /* upgrade, say something smart */
8668 /* favorites file */
8669 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_FAVS_FILE
);
8670 if (stat(file
, &sb
)) {
8671 warnx("favorites file doesn't exist, creating it");
8672 if ((f
= fopen(file
, "w")) == NULL
)
8673 err(1, "favorites");
8677 /* quickmarks file */
8678 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
8679 if (stat(file
, &sb
)) {
8680 warnx("quickmarks file doesn't exist, creating it");
8681 if ((f
= fopen(file
, "w")) == NULL
)
8682 err(1, "quickmarks");
8686 /* search history */
8687 if (history_autosave
) {
8688 snprintf(search_file
, sizeof search_file
, "%s" PS
"%s",
8689 work_dir
, XT_SEARCH_FILE
);
8690 if (stat(search_file
, &sb
)) {
8691 warnx("search history file doesn't exist, creating it");
8692 if ((f
= fopen(search_file
, "w")) == NULL
)
8693 err(1, "search_history");
8696 history_read(&shl
, search_file
, &search_history_count
);
8699 /* command history */
8700 if (history_autosave
) {
8701 snprintf(command_file
, sizeof command_file
, "%s" PS
"%s",
8702 work_dir
, XT_COMMAND_FILE
);
8703 if (stat(command_file
, &sb
)) {
8704 warnx("command history file doesn't exist, creating it");
8705 if ((f
= fopen(command_file
, "w")) == NULL
)
8706 err(1, "command_history");
8709 history_read(&chl
, command_file
, &cmd_history_count
);
8715 /* guess_search regex */
8716 if (url_regex
== NULL
)
8717 url_regex
= g_strdup(XT_URL_REGEX
);
8719 if (regcomp(&url_re
, url_regex
, REG_EXTENDED
| REG_NOSUB
))
8720 startpage_add("invalid url regex %s", url_regex
);
8723 env_proxy
= getenv("http_proxy");
8725 setup_proxy(env_proxy
);
8727 env_proxy
= getenv("HTTP_PROXY");
8729 setup_proxy(env_proxy
);
8731 setup_proxy(http_proxy
);
8734 /* the user can optionally have the proxy disabled at startup */
8735 if ((http_proxy_starts_enabled
== 0) && (http_proxy
!= NULL
)) {
8736 http_proxy_save
= g_strdup(http_proxy
);
8740 #ifndef XT_SOCKET_DISABLE
8742 send_cmd_to_socket(argv
[0]);
8746 opte
= opte
; /* shut mingw up */
8748 /* set some connection parameters */
8749 g_object_set(session
, "max-conns", max_connections
, (char *)NULL
);
8750 g_object_set(session
, "max-conns-per-host", max_host_connections
,
8752 g_object_set(session
, SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
8755 g_signal_connect(session
, "request-queued", G_CALLBACK(session_rq_cb
),
8758 #ifndef XT_SOCKET_DISABLE
8759 /* see if there is already a xombrero running */
8760 if (single_instance
&& is_running()) {
8762 warnx("already running");
8767 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
8768 send_cmd_to_socket(cmd
);
8778 optn
= optn
; /* shut mingw up */
8785 webkit_set_cache_model(WEBKIT_CACHE_MODEL_WEB_BROWSER
);
8787 webkit_set_cache_model(WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER
);
8789 if (enable_strict_transport
)
8790 strict_transport_init();
8792 /* uri completion */
8793 completion_model
= gtk_list_store_new(1, G_TYPE_STRING
);
8796 buffers_store
= gtk_list_store_new
8797 (NUM_COLS
, G_TYPE_UINT
, GDK_TYPE_PIXBUF
, G_TYPE_STRING
);
8803 notebook_tab_set_visibility();
8805 if (save_global_history
)
8806 restore_global_history();
8808 /* restore session list */
8809 restore_sessions_list();
8811 if (!strcmp(named_session
, XT_SAVED_TABS_FILE
))
8812 restore_saved_tabs();
8814 a
.s
= named_session
;
8815 a
.i
= XT_SES_DONOTHING
;
8816 open_tabs(NULL
, &a
);
8819 /* see if we have an exception */
8820 if (!TAILQ_EMPTY(&spl
)) {
8821 create_new_tab("about:startpage", NULL
, focus
, -1);
8826 create_new_tab(argv
[0], NULL
, focus
, -1);
8833 if (TAILQ_EMPTY(&tabs
))
8834 create_new_tab(home
, NULL
, 1, -1);
8835 #ifndef XT_SOCKET_DISABLE
8837 if ((s
= build_socket()) != -1) {
8838 channel
= g_io_channel_unix_new(s
);
8839 g_io_add_watch(channel
, G_IO_IN
, socket_watcher
, NULL
);
8843 #if GTK_CHECK_VERSION(3, 0, 0)
8849 gnutls_global_deinit();