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>
9 * Copyright (c) 2013 David Hill <dhill@mindcry.org>
11 * Permission to use, copy, modify, and distribute this software for any
12 * purpose with or without fee is hereby granted, provided that the above
13 * copyright notice and this permission notice appear in all copies.
15 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 char *version
= XOMBRERO_VERSION
;
30 uint32_t swm_debug
= 0
61 TAILQ_ENTRY(session
) entry
;
64 TAILQ_HEAD(session_list
, session
);
67 TAILQ_ENTRY(undo
) entry
;
70 int back
; /* Keeps track of how many back
71 * history items there are. */
73 TAILQ_HEAD(undo_tailq
, undo
);
75 struct command_entry
{
77 TAILQ_ENTRY(command_entry
) entry
;
79 TAILQ_HEAD(command_list
, command_entry
);
82 #define XT_CACHE_DIR ("cache")
83 #define XT_CERT_DIR ("certs")
84 #define XT_CERT_CACHE_DIR ("certs_cache")
85 #define XT_JS_DIR ("js")
86 #define XT_SESSIONS_DIR ("sessions")
87 #define XT_TEMP_DIR ("tmp")
88 #define XT_QMARKS_FILE ("quickmarks")
89 #define XT_SAVED_TABS_FILE ("main_session")
90 #define XT_RESTART_TABS_FILE ("restart_tabs")
91 #define XT_SOCKET_FILE ("socket")
92 #define XT_SAVE_SESSION_ID ("SESSION_NAME=")
93 #define XT_SEARCH_FILE ("search_history")
94 #define XT_COMMAND_FILE ("command_history")
95 #define XT_DLMAN_REFRESH "10"
96 #define XT_MAX_URL_LENGTH (4096) /* 1 page is atomic, don't make bigger */
97 #define XT_MAX_UNDO_CLOSE_TAB (32)
98 #define XT_PRINT_EXTRA_MARGIN 10
99 #define XT_URL_REGEX ("^[[:blank:]]*[^[:blank:]]*([[:alnum:]-]+\\.)+[[:alnum:]-][^[:blank:]]*[[:blank:]]*$")
100 #define XT_INVALID_MARK (-1) /* XXX this is a double, maybe use something else, like a nan */
101 #define XT_MAX_CERTS (32)
104 #define XT_COLOR_RED "#cc0000"
105 #define XT_COLOR_YELLOW "#ffff66"
106 #define XT_COLOR_BLUE "lightblue"
107 #define XT_COLOR_GREEN "#99ff66"
108 #define XT_COLOR_WHITE "white"
109 #define XT_COLOR_BLACK "black"
111 #define XT_COLOR_CT_BACKGROUND "#000000"
112 #define XT_COLOR_CT_INACTIVE "#dddddd"
113 #define XT_COLOR_CT_ACTIVE "#bbbb00"
114 #define XT_COLOR_CT_SEPARATOR "#555555"
116 #define XT_COLOR_SB_SEPARATOR "#555555"
118 /* CSS element names */
119 #define XT_CSS_NORMAL ""
120 #define XT_CSS_RED "red"
121 #define XT_CSS_YELLOW "yellow"
122 #define XT_CSS_GREEN "green"
123 #define XT_CSS_BLUE "blue"
124 #define XT_CSS_HIDDEN "hidden"
125 #define XT_CSS_ACTIVE "active"
127 #define XT_PROTO_DELIM "://"
130 #define XT_MOVE_INVALID (0)
131 #define XT_MOVE_DOWN (1)
132 #define XT_MOVE_UP (2)
133 #define XT_MOVE_BOTTOM (3)
134 #define XT_MOVE_TOP (4)
135 #define XT_MOVE_PAGEDOWN (5)
136 #define XT_MOVE_PAGEUP (6)
137 #define XT_MOVE_HALFDOWN (7)
138 #define XT_MOVE_HALFUP (8)
139 #define XT_MOVE_LEFT (9)
140 #define XT_MOVE_FARLEFT (10)
141 #define XT_MOVE_RIGHT (11)
142 #define XT_MOVE_FARRIGHT (12)
143 #define XT_MOVE_PERCENT (13)
144 #define XT_MOVE_CENTER (14)
146 #define XT_QMARK_SET (0)
147 #define XT_QMARK_OPEN (1)
148 #define XT_QMARK_TAB (2)
150 #define XT_MARK_SET (0)
151 #define XT_MARK_GOTO (1)
153 #define XT_GO_UP_ROOT (999)
155 #define XT_NAV_INVALID (0)
156 #define XT_NAV_BACK (1)
157 #define XT_NAV_FORWARD (2)
158 #define XT_NAV_RELOAD (3)
159 #define XT_NAV_STOP (4)
161 #define XT_FOCUS_INVALID (0)
162 #define XT_FOCUS_URI (1)
163 #define XT_FOCUS_SEARCH (2)
165 #define XT_SEARCH_INVALID (0)
166 #define XT_SEARCH_NEXT (1)
167 #define XT_SEARCH_PREV (2)
169 #define XT_PASTE_CURRENT_TAB (0)
170 #define XT_PASTE_NEW_TAB (1)
172 #define XT_ZOOM_IN (-1)
173 #define XT_ZOOM_OUT (-2)
174 #define XT_ZOOM_NORMAL (100)
176 #define XT_SES_DONOTHING (0)
177 #define XT_SES_CLOSETABS (1)
179 #define XT_PREFIX (1<<0)
180 #define XT_USERARG (1<<1)
181 #define XT_URLARG (1<<2)
182 #define XT_INTARG (1<<3)
183 #define XT_SESSARG (1<<4)
184 #define XT_SETARG (1<<5)
186 #define XT_HINT_NEWTAB (1<<0)
188 #define XT_BUFCMD_SZ (8)
190 #define XT_EJS_SHOW (1<<0)
192 GtkWidget
* create_button(const char *, const char *, int);
194 void recalc_tabs(void);
195 void recolor_compact_tabs(void);
196 void set_current_tab(int page_num
);
197 gboolean
update_statusbar_position(GtkAdjustment
*, gpointer
);
198 void marks_clear(struct tab
*t
);
201 extern char *__progname
;
202 char * const *start_argv
;
204 GtkWidget
*main_window
;
205 GtkNotebook
*notebook
;
207 GtkWidget
*tab_bar_box
;
208 GtkWidget
*arrow
, *abtn
;
209 GdkEvent
*fevent
= NULL
;
210 struct tab_list tabs
;
211 struct history_list hl
;
212 int hl_purge_count
= 0;
213 struct session_list sessions
;
215 struct wl_list js_wl
;
216 struct wl_list pl_wl
;
217 struct wl_list force_https
;
219 struct strict_transport_tree st_tree
;
220 struct undo_tailq undos
;
221 struct keybinding_list kbl
;
223 struct user_agent_list ua_list
;
224 struct http_accept_list ha_list
;
225 struct domain_id_list di_list
;
226 struct cmd_alias_list cal
;
227 struct custom_uri_list cul
;
228 struct command_list chl
;
229 struct command_list shl
;
230 struct command_entry
*history_at
;
231 struct command_entry
*search_at
;
232 struct secviolation_list svl
;
233 struct set_reject_list srl
;
235 int cmd_history_count
= 0;
236 int search_history_count
= 0;
238 uint64_t blocked_cookies
= 0;
239 char named_session
[PATH_MAX
];
240 GtkListStore
*completion_model
;
241 GtkListStore
*buffers_store
;
244 char *qmarks
[XT_NOQMARKS
];
245 int btn_down
; /* M1 down in any wv */
246 regex_t url_re
; /* guess_search regex */
248 /* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
249 int next_download_id
= 1;
251 void xxx_dir(char *);
252 int icon_size_map(int);
253 void activate_uri_entry_cb(GtkWidget
*, struct tab
*);
256 history_delete(struct command_list
*l
, int *counter
)
258 struct command_entry
*c
;
260 if (l
== NULL
|| counter
== NULL
)
263 c
= TAILQ_LAST(l
, command_list
);
267 TAILQ_REMOVE(l
, c
, entry
);
274 history_add(struct command_list
*list
, char *file
, char *l
, int *counter
)
276 struct command_entry
*c
;
279 if (list
== NULL
|| l
== NULL
|| counter
== NULL
)
282 /* don't add the same line */
283 c
= TAILQ_FIRST(list
);
285 if (!strcmp(c
->line
+ 1 /* skip space */, l
))
288 c
= g_malloc0(sizeof *c
);
289 c
->line
= g_strdup_printf(" %s", l
);
292 TAILQ_INSERT_HEAD(list
, c
, entry
);
295 history_delete(list
, counter
);
297 if (history_autosave
&& file
) {
298 f
= fopen(file
, "w");
300 show_oops(NULL
, "couldn't write history %s", file
);
304 TAILQ_FOREACH_REVERSE(c
, list
, command_list
, entry
) {
306 fprintf(f
, "%s\n", c
->line
);
314 history_read(struct command_list
*list
, char *file
, int *counter
)
317 char *s
, line
[65536];
319 if (list
== NULL
|| file
== NULL
)
322 f
= fopen(file
, "r");
324 startpage_add("couldn't open history file %s", file
);
329 s
= fgets(line
, sizeof line
, f
);
330 if (s
== NULL
|| feof(f
) || ferror(f
))
332 if ((s
= strchr(line
, '\n')) == NULL
) {
333 startpage_add("invalid history file %s", file
);
339 history_add(list
, NULL
, line
+ 1, counter
);
347 /* marks array storage. */
351 if (i
< 0 || i
>= XT_NOMARKS
)
362 if ((ret
= strchr(XT_MARKS
, m
)) != NULL
)
363 return ret
- XT_MARKS
;
368 /* quickmarks array storage. */
372 if (i
< 0 || i
>= XT_NOQMARKS
)
383 if ((ret
= strchr(XT_QMARKS
, m
)) != NULL
)
384 return ret
- XT_QMARKS
;
390 is_g_object_setting(GObject
*o
, char *str
)
392 guint n_props
= 0, i
;
393 GParamSpec
**proplist
;
399 proplist
= g_object_class_list_properties(G_OBJECT_GET_CLASS(o
),
402 for (i
= 0; i
< n_props
; i
++) {
403 if (! strcmp(proplist
[i
]->name
, str
)) {
414 get_current_tab(void)
418 TAILQ_FOREACH(t
, &tabs
, entry
) {
419 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
423 warnx("%s: no current tab", __func__
);
429 set_ssl_ca_file(struct settings
*s
, char *file
)
433 if (file
== NULL
|| strlen(file
) == 0)
435 if (stat(file
, &sb
)) {
436 warnx("no CA file: %s", file
);
439 expand_tilde(ssl_ca_file
, sizeof ssl_ca_file
, file
);
440 g_object_set(session
,
441 SOUP_SESSION_SSL_CA_FILE
, ssl_ca_file
,
442 SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
448 set_status(struct tab
*t
, gchar
*fmt
, ...)
455 status
= g_strdup_vprintf(fmt
, ap
);
457 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.uri
), status
);
461 else if (strcmp(t
->status
, status
)) {
472 hide_cmd(struct tab
*t
)
474 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
476 history_at
= NULL
; /* just in case */
477 search_at
= NULL
; /* just in case */
478 gtk_widget_set_can_focus(t
->cmd
, FALSE
);
479 gtk_widget_hide(t
->cmd
);
483 show_cmd(struct tab
*t
, const char *s
)
485 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
487 /* without this you can't middle click in t->cmd to paste */
488 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), "");
492 gtk_widget_hide(t
->oops
);
493 gtk_widget_set_can_focus(t
->cmd
, TRUE
);
494 gtk_widget_show(t
->cmd
);
496 gtk_widget_grab_focus(GTK_WIDGET(t
->cmd
));
497 #if GTK_CHECK_VERSION(3, 0, 0)
498 gtk_widget_set_name(t
->cmd
, XT_CSS_NORMAL
);
500 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
501 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
503 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), s
);
504 gtk_editable_set_position(GTK_EDITABLE(t
->cmd
), -1);
508 hide_buffers(struct tab
*t
)
510 gtk_widget_hide(t
->buffers
);
511 gtk_widget_set_can_focus(t
->buffers
, FALSE
);
512 gtk_list_store_clear(buffers_store
);
523 sort_tabs_by_page_num(struct tab
***stabs
)
528 num_tabs
= gtk_notebook_get_n_pages(notebook
);
530 *stabs
= g_malloc0(num_tabs
* sizeof(struct tab
*));
532 TAILQ_FOREACH(t
, &tabs
, entry
)
533 (*stabs
)[gtk_notebook_page_num(notebook
, t
->vbox
)] = t
;
539 buffers_make_list(void)
542 const gchar
*title
= NULL
;
543 struct tab
**stabs
= NULL
;
546 num_tabs
= sort_tabs_by_page_num(&stabs
);
548 for (i
= 0; i
< num_tabs
; i
++)
550 gtk_list_store_append(buffers_store
, &iter
);
551 title
= get_title(stabs
[i
], FALSE
);
552 gtk_list_store_set(buffers_store
, &iter
,
553 COL_ID
, i
+ 1, /* Enumerate the tabs starting from 1
555 COL_FAVICON
, gtk_image_get_pixbuf
556 (GTK_IMAGE(stabs
[i
]->tab_elems
.favicon
)),
565 show_buffers(struct tab
*t
)
568 GtkTreeSelection
*sel
;
572 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)))
577 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(t
->buffers
));
578 index
= gtk_notebook_get_current_page(notebook
);
579 path
= gtk_tree_path_new_from_indices(index
, -1);
580 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
, path
))
581 gtk_tree_selection_select_iter(sel
, &iter
);
582 gtk_tree_path_free(path
);
584 gtk_widget_show(t
->buffers
);
585 gtk_widget_set_can_focus(t
->buffers
, TRUE
);
586 gtk_widget_grab_focus(GTK_WIDGET(t
->buffers
));
590 toggle_buffers(struct tab
*t
)
592 if (gtk_widget_get_visible(t
->buffers
))
599 buffers(struct tab
*t
, struct karg
*args
)
607 set_scrollbar_visibility(struct tab
*t
, int visible
)
609 #if GTK_CHECK_VERSION(3, 0, 0)
610 GtkWidget
*h_scrollbar
, *v_scrollbar
;
612 h_scrollbar
= gtk_scrolled_window_get_hscrollbar(
613 GTK_SCROLLED_WINDOW(t
->browser_win
));
614 v_scrollbar
= gtk_scrolled_window_get_vscrollbar(
615 GTK_SCROLLED_WINDOW(t
->browser_win
));
618 gtk_widget_set_name(h_scrollbar
, XT_CSS_HIDDEN
);
619 gtk_widget_set_name(v_scrollbar
, XT_CSS_HIDDEN
);
621 gtk_widget_set_name(h_scrollbar
, "");
622 gtk_widget_set_name(v_scrollbar
, "");
627 return (visible
== 0);
632 hide_oops(struct tab
*t
)
634 gtk_widget_hide(t
->oops
);
638 show_oops(struct tab
*at
, const char *fmt
, ...)
642 struct tab
*t
= NULL
;
648 if ((t
= get_current_tab()) == NULL
)
654 if ((msg
= g_strdup_vprintf(fmt
, ap
)) == NULL
)
655 errx(1, "show_oops failed");
658 gtk_entry_set_text(GTK_ENTRY(t
->oops
), msg
);
659 gtk_widget_hide(t
->cmd
);
660 gtk_widget_show(t
->oops
);
666 char work_dir
[PATH_MAX
];
667 char certs_dir
[PATH_MAX
];
668 char certs_cache_dir
[PATH_MAX
];
669 char js_dir
[PATH_MAX
];
670 char cache_dir
[PATH_MAX
];
671 char sessions_dir
[PATH_MAX
];
672 char temp_dir
[PATH_MAX
];
673 char cookie_file
[PATH_MAX
];
674 char *strict_transport_file
= NULL
;
675 SoupSession
*session
;
676 SoupCookieJar
*s_cookiejar
;
677 SoupCookieJar
*p_cookiejar
;
678 char rc_fname
[PATH_MAX
];
680 struct mime_type_list mtl
;
681 struct alias_list aliases
;
684 struct tab
*create_new_tab(const char *, struct undo
*, int, int);
685 void delete_tab(struct tab
*);
686 void setzoom_webkit(struct tab
*, int);
687 int download_rb_cmp(struct download
*, struct download
*);
688 gboolean
cmd_execute(struct tab
*t
, char *str
);
691 history_rb_cmp(struct history
*h1
, struct history
*h2
)
693 return (strcmp(h1
->uri
, h2
->uri
));
695 RB_GENERATE(history_list
, history
, entry
, history_rb_cmp
);
698 download_rb_cmp(struct download
*e1
, struct download
*e2
)
700 return (e1
->id
< e2
->id
? -1 : e1
->id
> e2
->id
);
702 RB_GENERATE(download_list
, download
, entry
, download_rb_cmp
);
705 secviolation_rb_cmp(struct secviolation
*s1
, struct secviolation
*s2
)
707 return (s1
->xtp_arg
< s2
->xtp_arg
? -1 : s1
->xtp_arg
> s2
->xtp_arg
);
709 RB_GENERATE(secviolation_list
, secviolation
, entry
, secviolation_rb_cmp
);
712 user_agent_rb_cmp(struct user_agent
*ua1
, struct user_agent
*ua2
)
714 return (ua1
->id
< ua2
->id
? -1 : ua1
->id
> ua2
->id
);
716 RB_GENERATE(user_agent_list
, user_agent
, entry
, user_agent_rb_cmp
);
719 http_accept_rb_cmp(struct http_accept
*ha1
, struct http_accept
*ha2
)
721 return (ha1
->id
< ha2
->id
? -1 : ha1
->id
> ha2
->id
);
723 RB_GENERATE(http_accept_list
, http_accept
, entry
, http_accept_rb_cmp
);
726 domain_id_rb_cmp(struct domain_id
*d1
, struct domain_id
*d2
)
728 return (strcmp(d1
->domain
, d2
->domain
));
730 RB_GENERATE(domain_id_list
, domain_id
, entry
, domain_id_rb_cmp
);
732 struct valid_url_types
{
744 valid_url_type(const char *url
)
748 for (i
= 0; i
< LENGTH(vut
); i
++)
749 if (!strncasecmp(vut
[i
].type
, url
, strlen(vut
[i
].type
)))
756 match_alias(const char *url_in
)
760 char *url_out
= NULL
, *search
, *enc_arg
;
763 search
= g_strdup(url_in
);
765 if (strsep(&arg
, " \t") == NULL
) {
766 show_oops(NULL
, "match_alias: NULL URL");
770 TAILQ_FOREACH(a
, &aliases
, entry
) {
771 if (!strcmp(search
, a
->a_name
))
776 DNPRINTF(XT_D_URL
, "match_alias: matched alias %s\n",
780 enc_arg
= soup_uri_encode(arg
, XT_RESERVED_CHARS
);
781 sv
= g_strsplit(a
->a_uri
, "%s", 2);
783 url_out
= g_strjoinv(enc_arg
, sv
);
785 url_out
= g_strjoinv("", sv
);
795 guess_url_type(const char *url_in
)
798 char cwd
[PATH_MAX
] = {0};
799 char *url_out
= NULL
, *enc_search
= NULL
;
805 /* substitute aliases */
806 url_out
= match_alias(url_in
);
810 /* see if we are an about page */
811 if (!strncmp(url_in
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
))
812 for (i
= 0; i
< about_list_size(); i
++)
813 if (!strcmp(&url_in
[XT_URI_ABOUT_LEN
],
814 about_list
[i
].name
)) {
815 url_out
= g_strdup(url_in
);
819 if (guess_search
&& url_regex
&&
820 !(g_str_has_prefix(url_in
, "http://") ||
821 g_str_has_prefix(url_in
, "https://"))) {
822 if (regexec(&url_re
, url_in
, 0, NULL
, 0)) {
823 /* invalid URI so search instead */
824 enc_search
= soup_uri_encode(url_in
, XT_RESERVED_CHARS
);
825 sv
= g_strsplit(search_string
, "%s", 2);
826 url_out
= g_strjoinv(enc_search
, sv
);
833 /* XXX not sure about this heuristic */
834 if (stat(url_in
, &sb
) == 0) {
835 if (url_in
[0] == '/')
836 url_out
= g_filename_to_uri(url_in
, NULL
, NULL
);
838 if (getcwd(cwd
, PATH_MAX
) != NULL
) {
839 path
= g_strdup_printf("%s" PS
"%s", cwd
,
841 url_out
= g_filename_to_uri(path
, NULL
, NULL
);
846 url_out
= g_strdup_printf("http://%s", url_in
); /* guess http */
848 DNPRINTF(XT_D_URL
, "guess_url_type: guessed %s\n", url_out
);
854 set_normal_tab_meaning(struct tab
*t
)
859 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
860 if (t
->session_key
!= NULL
) {
861 g_free(t
->session_key
);
862 t
->session_key
= NULL
;
867 load_uri(struct tab
*t
, const gchar
*uri
)
870 gchar
*newuri
= NULL
;
876 /* Strip leading spaces. */
877 while (*uri
&& isspace((unsigned char)*uri
))
880 if (strlen(uri
) == 0) {
885 set_normal_tab_meaning(t
);
887 if (valid_url_type(uri
)) {
888 if ((newuri
= guess_url_type(uri
)) != NULL
)
894 /* clear :cert show host */
895 if (t
->about_cert_host
) {
896 g_free(t
->about_cert_host
);
897 t
->about_cert_host
= NULL
;
900 if (!strncmp(uri
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
)) {
901 for (i
= 0; i
< about_list_size(); i
++)
902 if (!strcmp(&uri
[XT_URI_ABOUT_LEN
], about_list
[i
].name
) &&
903 about_list
[i
].func
!= NULL
) {
904 bzero(&args
, sizeof args
);
905 about_list
[i
].func(t
, &args
);
906 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
),
910 show_oops(t
, "invalid about page");
914 /* remove old HTTPS cert chain (if any) */
920 set_status(t
, "Loading: %s", (char *)uri
);
922 webkit_web_view_load_uri(t
->wv
, uri
);
929 get_uri(struct tab
*t
)
931 const gchar
*uri
= NULL
;
933 if (webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
&&
934 !t
->download_requested
)
936 if (t
->xtp_meaning
== XT_XTP_TAB_MEANING_NORMAL
)
937 uri
= webkit_web_view_get_uri(t
->wv
);
939 /* use tmp_uri to make sure it is g_freed */
942 t
->tmp_uri
= g_strdup_printf("%s%s", XT_URI_ABOUT
,
943 about_list
[t
->xtp_meaning
].name
);
950 get_title(struct tab
*t
, bool window
)
952 const gchar
*set
= NULL
, *title
= NULL
;
953 WebKitLoadStatus status
;
955 status
= webkit_web_view_get_load_status(t
->wv
);
956 if (status
== WEBKIT_LOAD_PROVISIONAL
||
957 (status
== WEBKIT_LOAD_FAILED
&& !t
->download_requested
) ||
958 t
->xtp_meaning
== XT_XTP_TAB_MEANING_BL
)
961 title
= webkit_web_view_get_title(t
->wv
);
962 if ((set
= title
? title
: get_uri(t
)))
966 set
= window
? XT_NAME
: "(untitled)";
972 find_mime_type(char *mime_type
)
974 struct mime_type
*m
, *def
= NULL
, *rv
= NULL
;
976 TAILQ_FOREACH(m
, &mtl
, entry
) {
978 !strncmp(mime_type
, m
->mt_type
, strlen(m
->mt_type
)))
981 if (m
->mt_default
== 0 && !strcmp(mime_type
, m
->mt_type
)) {
994 * This only escapes the & and < characters, as per the discussion found here:
995 * http://lists.apple.com/archives/Webkitsdk-dev/2007/May/msg00056.html
998 html_escape(const char *val
)
1006 sv
= g_strsplit(val
, "&", -1);
1007 s
= g_strjoinv("&", sv
);
1010 sv
= g_strsplit(val
, "<", -1);
1011 s
= g_strjoinv("<", sv
);
1018 wl_find_uri(const gchar
*s
, struct wl_list
*wl
)
1024 if (s
== NULL
|| wl
== NULL
)
1027 if (!strncmp(s
, "http://", strlen("http://")))
1028 s
= &s
[strlen("http://")];
1029 else if (!strncmp(s
, "https://", strlen("https://")))
1030 s
= &s
[strlen("https://")];
1035 for (i
= 0; i
< strlen(s
) + 1 /* yes er need this */; i
++)
1036 /* chop string at first slash */
1037 if (s
[i
] == '/' || s
[i
] == ':' || s
[i
] == '\0') {
1040 w
= wl_find(ss
, wl
);
1049 js_ref_to_string(JSContextRef context
, JSValueRef ref
)
1055 jsref
= JSValueToStringCopy(context
, ref
, NULL
);
1059 l
= JSStringGetMaximumUTF8CStringSize(jsref
);
1062 JSStringGetUTF8CString(jsref
, s
, l
);
1063 JSStringRelease(jsref
);
1068 #define XT_JS_DONE ("done;")
1069 #define XT_JS_DONE_LEN (strlen(XT_JS_DONE))
1070 #define XT_JS_INSERT ("insert;")
1071 #define XT_JS_INSERT_LEN (strlen(XT_JS_INSERT))
1074 run_script(struct tab
*t
, char *s
)
1076 JSGlobalContextRef ctx
;
1077 WebKitWebFrame
*frame
;
1079 JSValueRef val
, exception
;
1082 DNPRINTF(XT_D_JS
, "%s: tab %d %s\n", __func__
,
1083 t
->tab_id
, s
== (char *)JS_HINTING
? "JS_HINTING" : s
);
1085 frame
= webkit_web_view_get_main_frame(t
->wv
);
1086 ctx
= webkit_web_frame_get_global_context(frame
);
1088 str
= JSStringCreateWithUTF8CString(s
);
1089 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
1090 NULL
, 0, &exception
);
1091 JSStringRelease(str
);
1093 DNPRINTF(XT_D_JS
, "%s: val %p\n", __func__
, val
);
1095 es
= js_ref_to_string(ctx
, exception
);
1097 DNPRINTF(XT_D_JS
, "%s: exception %s\n", __func__
, es
);
1102 es
= js_ref_to_string(ctx
, val
);
1105 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
1107 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
1111 DNPRINTF(XT_D_JS
, "%s: val %s\n", __func__
, es
);
1120 enable_hints(struct tab
*t
)
1122 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
1125 run_script(t
, "hints.createHints('', 'F');");
1127 run_script(t
, "hints.createHints('', 'f');");
1128 t
->mode
= XT_MODE_HINT
;
1132 disable_hints(struct tab
*t
)
1134 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
1136 run_script(t
, "hints.clearHints();");
1137 t
->mode
= XT_MODE_COMMAND
;
1142 passthrough(struct tab
*t
, struct karg
*args
)
1144 t
->mode
= XT_MODE_PASSTHROUGH
;
1149 modurl(struct tab
*t
, struct karg
*args
)
1151 const gchar
*uri
= NULL
;
1154 /* XXX kind of a bad hack, but oh well */
1155 if (gtk_widget_has_focus(t
->uri_entry
)) {
1156 if ((uri
= gtk_entry_get_text(GTK_ENTRY(t
->uri_entry
))) &&
1158 u
= g_strdup_printf("www.%s.com", uri
);
1159 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), u
);
1161 activate_uri_entry_cb(t
->uri_entry
, t
);
1168 hint(struct tab
*t
, struct karg
*args
)
1171 DNPRINTF(XT_D_JS
, "hint: tab %d args %d\n", t
->tab_id
, args
->i
);
1173 if (t
->mode
== XT_MODE_HINT
) {
1174 if (args
->i
== XT_HINT_NEWTAB
)
1184 apply_style(struct tab
*t
)
1187 g_object_set(G_OBJECT(t
->settings
),
1188 "user-stylesheet-uri", t
->stylesheet
, (char *)NULL
);
1192 remove_style(struct tab
*t
)
1195 g_object_set(G_OBJECT(t
->settings
),
1196 "user-stylesheet-uri", NULL
, (char *)NULL
);
1200 userstyle_cmd(struct tab
*t
, struct karg
*args
)
1202 char script
[PATH_MAX
] = {'\0'};
1206 DNPRINTF(XT_D_JS
, "userstyle_cmd: tab %d\n", t
->tab_id
);
1208 if (args
->s
!= NULL
&& strlen(args
->s
)) {
1209 expand_tilde(script
, sizeof script
, args
->s
);
1210 script_uri
= g_filename_to_uri(script
, NULL
, NULL
);
1212 script_uri
= g_strdup(userstyle
);
1214 if (script_uri
== NULL
)
1218 case XT_STYLE_CURRENT_TAB
:
1219 if (t
->styled
&& !strcmp(script_uri
, t
->stylesheet
))
1223 g_free(t
->stylesheet
);
1224 t
->stylesheet
= g_strdup(script_uri
);
1228 case XT_STYLE_GLOBAL
:
1229 if (userstyle_global
&& !strcmp(script_uri
, t
->stylesheet
)) {
1230 userstyle_global
= 0;
1231 TAILQ_FOREACH(tt
, &tabs
, entry
)
1234 userstyle_global
= 1;
1236 /* need to save this stylesheet for new tabs */
1239 stylesheet
= g_strdup(script_uri
);
1241 TAILQ_FOREACH(tt
, &tabs
, entry
) {
1243 g_free(tt
->stylesheet
);
1244 tt
->stylesheet
= g_strdup(script_uri
);
1257 quit(struct tab
*t
, struct karg
*args
)
1259 if (save_global_history
)
1260 save_global_history_to_disk(t
);
1268 restore_sessions_list(void)
1271 struct dirent
*dp
= NULL
;
1275 sdir
= opendir(sessions_dir
);
1277 while ((dp
= readdir(sdir
)) != NULL
) {
1278 #if defined __MINGW32__
1279 reg
= 1; /* windows only has regular files */
1281 reg
= dp
->d_type
== DT_REG
;
1284 s
= g_malloc(sizeof(struct session
));
1285 s
->name
= g_strdup(dp
->d_name
);
1286 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
1294 open_tabs(struct tab
*t
, struct karg
*a
)
1296 char file
[PATH_MAX
];
1300 struct tab
*ti
, *tt
;
1305 ti
= TAILQ_LAST(&tabs
, tab_list
);
1307 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, a
->s
);
1308 if ((f
= fopen(file
, "r")) == NULL
)
1312 if ((uri
= fparseln(f
, NULL
, NULL
, "\0\0\0", 0)) == NULL
) {
1313 if (feof(f
) || ferror(f
))
1316 /* retrieve session name */
1317 if (g_str_has_prefix(uri
, XT_SAVE_SESSION_ID
)) {
1318 strlcpy(named_session
,
1319 &uri
[strlen(XT_SAVE_SESSION_ID
)],
1320 sizeof named_session
);
1325 create_new_tab(uri
, NULL
, 1, -1);
1331 /* close open tabs */
1332 if (a
->i
== XT_SES_CLOSETABS
&& ti
!= NULL
) {
1334 tt
= TAILQ_FIRST(&tabs
);
1355 restore_saved_tabs(void)
1357 char file
[PATH_MAX
];
1358 int unlink_file
= 0;
1363 snprintf(file
, sizeof file
, "%s" PS
"%s",
1364 sessions_dir
, XT_RESTART_TABS_FILE
);
1365 if (stat(file
, &sb
) == -1)
1366 a
.s
= XT_SAVED_TABS_FILE
;
1369 a
.s
= XT_RESTART_TABS_FILE
;
1372 a
.i
= XT_SES_DONOTHING
;
1373 rv
= open_tabs(NULL
, &a
);
1382 save_tabs(struct tab
*t
, struct karg
*a
)
1384 char file
[PATH_MAX
];
1386 int num_tabs
= 0, i
;
1387 struct tab
**stabs
= NULL
;
1389 /* tab may be null here */
1394 snprintf(file
, sizeof file
, "%s" PS
"%s",
1395 sessions_dir
, named_session
);
1397 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, a
->s
);
1399 if ((f
= fopen(file
, "w")) == NULL
) {
1400 show_oops(t
, "Can't open save_tabs file: %s", strerror(errno
));
1404 /* save session name */
1405 fprintf(f
, "%s%s\n", XT_SAVE_SESSION_ID
, named_session
);
1407 /* Save tabs, in the order they are arranged in the notebook. */
1408 num_tabs
= sort_tabs_by_page_num(&stabs
);
1410 for (i
= 0; i
< num_tabs
; i
++)
1412 if (get_uri(stabs
[i
]) != NULL
)
1413 fprintf(f
, "%s\n", get_uri(stabs
[i
]));
1414 else if (gtk_entry_get_text(GTK_ENTRY(
1415 stabs
[i
]->uri_entry
)))
1416 fprintf(f
, "%s\n", gtk_entry_get_text(GTK_ENTRY(
1417 stabs
[i
]->uri_entry
)));
1422 /* try and make sure this gets to disk NOW. XXX Backup first? */
1423 if (fflush(f
) != 0 || fsync(fileno(f
)) != 0) {
1424 show_oops(t
, "May not have managed to save session: %s",
1434 save_tabs_and_quit(struct tab
*t
, struct karg
*args
)
1446 expand_tilde(char *path
, size_t len
, const char *s
)
1450 char user
[LOGIN_NAME_MAX
];
1453 if (path
== NULL
|| s
== NULL
)
1454 errx(1, "expand_tilde");
1457 strlcpy(path
, sc
, len
);
1462 for (i
= 0; s
[i
] != PSC
&& s
[i
] != '\0'; ++i
)
1467 pwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1469 strlcpy(path
, sc
, len
);
1471 snprintf(path
, len
, "%s%s", pwd
->pw_dir
, s
);
1475 run_page_script(struct tab
*t
, struct karg
*args
)
1478 char *tmp
, script
[PATH_MAX
];
1481 tmp
= args
->s
!= NULL
&& strlen(args
->s
) > 0 ? args
->s
: default_script
;
1482 if (tmp
[0] == '\0') {
1483 show_oops(t
, "no script specified");
1487 if ((uri
= get_uri(t
)) == NULL
) {
1488 show_oops(t
, "tab is empty, not running script");
1492 expand_tilde(script
, sizeof script
, tmp
);
1495 sv
[1] = (char *)uri
;
1497 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
, NULL
,
1499 show_oops(t
, "%s: could not spawn process: %s %s", __func__
,
1503 show_oops(t
, "running: %s %s", sv
[0], sv
[1]);
1509 yank_uri(struct tab
*t
, struct karg
*args
)
1512 GtkClipboard
*clipboard
, *primary
;
1514 if ((uri
= get_uri(t
)) == NULL
)
1517 primary
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1518 clipboard
= gtk_clipboard_get(GDK_SELECTION_CLIPBOARD
);
1519 gtk_clipboard_set_text(primary
, uri
, -1);
1520 gtk_clipboard_set_text(clipboard
, uri
, -1);
1526 paste_uri(struct tab
*t
, struct karg
*args
)
1528 GtkClipboard
*clipboard
, *primary
;
1529 gchar
*c
= NULL
, *p
= NULL
, *uri
;
1532 clipboard
= gtk_clipboard_get(GDK_SELECTION_CLIPBOARD
);
1533 primary
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1534 c
= gtk_clipboard_wait_for_text(clipboard
);
1535 p
= gtk_clipboard_wait_for_text(primary
);
1539 /* Windows try clipboard first */
1542 /* UNIX try primary first */
1545 /* replace all newlines with spaces */
1546 for (i
= 0; uri
[i
] != '\0'; ++i
)
1550 while (*uri
&& isspace((unsigned char)*uri
))
1552 if (strlen(uri
) == 0) {
1553 show_oops(t
, "empty paste buffer");
1556 if (guess_search
== 0 && valid_url_type(uri
)) {
1557 /* we can be clever and paste this in search box */
1558 show_oops(t
, "not a valid URL");
1562 if (args
->i
== XT_PASTE_CURRENT_TAB
)
1564 else if (args
->i
== XT_PASTE_NEW_TAB
)
1565 create_new_tab(uri
, NULL
, 1, -1);
1578 js_toggle_cb(GtkWidget
*w
, struct tab
*t
)
1583 g_object_get(G_OBJECT(t
->settings
),
1584 "enable-scripts", &es
, (char *)NULL
);
1589 set
= XT_WL_DISABLE
;
1591 a
.i
= set
| XT_WL_TOPLEVEL
;
1594 a
.i
= set
| XT_WL_TOPLEVEL
;
1597 a
.i
= XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
;
1602 proxy_toggle_cb(GtkWidget
*w
, struct tab
*t
)
1604 struct karg args
= {0};
1606 args
.i
= XT_PRXY_TOGGLE
;
1607 proxy_cmd(t
, &args
);
1611 toggle_src(struct tab
*t
, struct karg
*args
)
1618 mode
= webkit_web_view_get_view_source_mode(t
->wv
);
1619 webkit_web_view_set_view_source_mode(t
->wv
, !mode
);
1620 webkit_web_view_reload(t
->wv
);
1626 focus_webview(struct tab
*t
)
1631 /* only grab focus if we are visible */
1632 if (gtk_notebook_get_current_page(notebook
) == t
->tab_id
)
1633 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
1637 focus(struct tab
*t
, struct karg
*args
)
1639 if (t
== NULL
|| args
== NULL
)
1645 if (args
->i
== XT_FOCUS_URI
)
1646 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
1647 else if (args
->i
== XT_FOCUS_SEARCH
)
1648 gtk_widget_grab_focus(GTK_WIDGET(t
->search_entry
));
1654 free_connection_certs(gnutls_x509_crt_t
*certs
, size_t cert_count
)
1658 for (i
= 0; i
< cert_count
; i
++)
1659 gnutls_x509_crt_deinit(certs
[i
]);
1663 #if GTK_CHECK_VERSION(3, 0, 0)
1665 statusbar_modify_attr(struct tab
*t
, const char *css_name
)
1667 gtk_widget_set_name(t
->sbe
.ebox
, css_name
);
1671 statusbar_modify_attr(struct tab
*t
, const char *text
, const char *base
)
1673 GdkColor c_text
, c_base
;
1675 gdk_color_parse(text
, &c_text
);
1676 gdk_color_parse(base
, &c_base
);
1678 gtk_widget_modify_bg(t
->sbe
.ebox
, GTK_STATE_NORMAL
, &c_base
);
1679 gtk_widget_modify_base(t
->sbe
.uri
, GTK_STATE_NORMAL
, &c_base
);
1680 gtk_widget_modify_text(t
->sbe
.uri
, GTK_STATE_NORMAL
, &c_text
);
1685 save_certs(struct tab
*t
, gnutls_x509_crt_t
*certs
,
1686 size_t cert_count
, const char *domain
, const char *dir
)
1689 char file
[PATH_MAX
];
1690 char *cert_buf
= NULL
;
1695 if (t
== NULL
|| certs
== NULL
|| cert_count
<= 0 || domain
== NULL
)
1698 snprintf(file
, sizeof file
, "%s" PS
"%s", dir
, domain
);
1699 if ((f
= fopen(file
, "w")) == NULL
) {
1700 show_oops(t
, "Can't create cert file %s %s",
1701 file
, strerror(errno
));
1705 for (i
= 0; i
< cert_count
; i
++) {
1707 * Because we support old crap and can't use
1708 * gnutls_x509_crt_export2(), we intentionally use an empty
1709 * buffer and then make a second call with the known size
1713 gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1714 NULL
, &cert_buf_sz
);
1715 if (cert_buf_sz
== 0) {
1716 show_oops(t
, "no certs found");
1719 cert_buf
= gnutls_malloc(cert_buf_sz
* sizeof(char));
1720 if (cert_buf
== NULL
) {
1721 show_oops(t
, "gnutls_x509_crt_export failed");
1724 rv
= gnutls_x509_crt_export(certs
[i
],
1725 GNUTLS_X509_FMT_PEM
, cert_buf
, &cert_buf_sz
);
1727 show_oops(t
, "gnutls_x509_crt_export failure: %s",
1728 gnutls_strerror(rv
));
1731 cert_buf
[cert_buf_sz
] = '\0';
1732 if (fwrite(cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1733 show_oops(t
, "Can't write certs: %s", strerror(errno
));
1736 gnutls_free(cert_buf
);
1742 gnutls_free(cert_buf
);
1754 get_local_cert_chain(const char *uri
, size_t *ncerts
, const char **error_str
,
1758 unsigned char cert_buf
[64 * 1024] = {0};
1759 gnutls_datum_t data
;
1760 unsigned int max_certs
= XT_MAX_CERTS
;
1762 char file
[PATH_MAX
];
1764 gnutls_x509_crt_t
*certs
;
1766 if ((su
= soup_uri_new(uri
)) == NULL
) {
1767 *error_str
= "Invalid URI";
1771 snprintf(file
, sizeof file
, "%s" PS
"%s", dir
, su
->host
);
1773 if ((f
= fopen(file
, "r")) == NULL
) {
1774 *error_str
= "Could not read local cert";
1778 bytes_read
= fread(cert_buf
, sizeof *cert_buf
, sizeof cert_buf
, f
);
1779 if (bytes_read
== 0) {
1780 *error_str
= "Could not read local cert";
1784 certs
= gnutls_malloc(sizeof(*certs
) * max_certs
);
1785 if (certs
== NULL
) {
1786 *error_str
= "Error allocating memory";
1789 data
.data
= cert_buf
;
1790 data
.size
= bytes_read
;
1791 if (gnutls_x509_crt_list_import(certs
, &max_certs
, &data
,
1792 GNUTLS_X509_FMT_PEM
, 0) < 0) {
1794 *error_str
= "Error reading local cert chain";
1798 *ncerts
= max_certs
;
1803 * This uses the pem-encoded cert chain saved in t->pem instead of
1804 * grabbing the remote cert. We save it beforehand and read it here
1805 * so as to not open a side channel that ignores proxy settings.
1808 get_chain_for_pem(char *pem
, size_t *ncerts
, const char **error_str
)
1810 gnutls_datum_t data
;
1811 unsigned int max_certs
= XT_MAX_CERTS
;
1812 gnutls_x509_crt_t
*certs
;
1815 *error_str
= "Error reading remote cert chain";
1819 certs
= gnutls_malloc(sizeof(*certs
) * max_certs
);
1820 if (certs
== NULL
) {
1821 *error_str
= "Error allocating memory";
1824 data
.data
= (unsigned char *)pem
;
1825 data
.size
= strlen(pem
);
1826 if (gnutls_x509_crt_list_import(certs
, &max_certs
, &data
,
1827 GNUTLS_X509_FMT_PEM
, 0) < 0) {
1829 *error_str
= "Error reading remote cert chain";
1833 *ncerts
= max_certs
;
1839 cert_cmd(struct tab
*t
, struct karg
*args
)
1841 const gchar
*uri
, *error_str
= NULL
;
1843 gnutls_x509_crt_t
*certs
;
1846 #if !GTK_CHECK_VERSION(3, 0, 0)
1853 if (args
->s
!= NULL
) {
1855 } else if ((uri
= get_uri(t
)) == NULL
) {
1856 show_oops(t
, "Invalid URI");
1859 if ((su
= soup_uri_new(uri
)) == NULL
) {
1860 show_oops(t
, "Invalid URI");
1865 * if we're only showing the local certs, don't open a socket and get
1868 if (args
->i
& XT_SHOW
&& args
->i
& XT_CACHE
) {
1869 certs
= get_local_cert_chain(uri
, &cert_count
, &error_str
,
1871 if (error_str
== NULL
) {
1872 t
->about_cert_host
= g_strdup(su
->host
);
1873 show_certs(t
, certs
, cert_count
, "Certificate Chain");
1874 free_connection_certs(certs
, cert_count
);
1876 show_oops(t
, "%s", error_str
);
1884 certs
= get_chain_for_pem(t
->pem
, &cert_count
, &error_str
);
1888 if (args
->i
& XT_SHOW
) {
1889 t
->about_cert_host
= g_strdup(su
->host
);
1890 show_certs(t
, certs
, cert_count
, "Certificate Chain");
1891 } else if (args
->i
& XT_SAVE
) {
1892 host
= t
->about_cert_host
? t
->about_cert_host
: su
->host
;
1893 save_certs(t
, certs
, cert_count
, host
, certs_dir
);
1894 #if GTK_CHECK_VERSION(3, 0, 0)
1895 gtk_widget_set_name(t
->uri_entry
, XT_CSS_BLUE
);
1896 statusbar_modify_attr(t
, XT_CSS_BLUE
);
1898 gdk_color_parse(XT_COLOR_BLUE
, &color
);
1899 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
1900 statusbar_modify_attr(t
, XT_COLOR_BLACK
, XT_COLOR_BLUE
);
1902 } else if (args
->i
& XT_CACHE
)
1903 save_certs(t
, certs
, cert_count
, su
->host
, certs_cache_dir
);
1905 free_connection_certs(certs
, cert_count
);
1910 if (error_str
&& strlen(error_str
)) {
1911 show_oops(t
, "%s", error_str
);
1918 * Checks whether the remote cert is identical to the local saved
1919 * cert. Returns CERT_LOCAL if unchanged, CERT_UNTRUSTED if local
1920 * cert does not exist, and CERT_BAD if different.
1922 * Saves entire cert chain in pem encoding to chain for it to be
1923 * cached later, if needed.
1926 check_local_certs(const char *file
, GTlsCertificate
*cert
, char **chain
)
1928 char r_cert_buf
[64 * 1024];
1930 GTlsCertificate
*tmpcert
= NULL
;
1931 GTlsCertificate
*issuer
= NULL
;
1934 enum cert_trust rv
= CERT_LOCAL
;
1937 if ((f
= fopen(file
, "r")) == NULL
) {
1938 /* no local cert to check */
1939 rv
= CERT_UNTRUSTED
;
1944 g_object_get(G_OBJECT(cert
), "certificate-pem", &pem
,
1946 g_object_get(G_OBJECT(cert
), "issuer", &issuer
, NULL
);
1950 g_object_get(G_OBJECT(tmpcert
), "issuer", &issuer
,
1955 g_object_get(G_OBJECT(tmpcert
), "certificate-pem", &pem
,
1960 if (tmpcert
!= NULL
)
1961 g_object_unref(G_OBJECT(tmpcert
));
1964 if (fread(r_cert_buf
, strlen(pem
), 1, f
) != 1 && !feof(f
))
1966 if (bcmp(r_cert_buf
, pem
, strlen(pem
)))
1969 tmp
= g_strdup_printf("%s%s", *chain
, pem
);
1976 g_object_unref(G_OBJECT(issuer
));
1983 check_cert_changes(struct tab
*t
, GTlsCertificate
*cert
, const char *file
, const char *uri
)
1985 SoupURI
*soupuri
= NULL
;
1986 struct karg args
= {0};
1987 struct wl_entry
*w
= NULL
;
1991 chain
= g_strdup("");
1992 switch(check_local_certs(file
, cert
, &chain
)) {
1994 /* The cached certificate is identical */
1996 case CERT_TRUSTED
: /* FALLTHROUGH */
1997 case CERT_UNTRUSTED
:
1998 /* cache new certificate */
2003 if ((soupuri
= soup_uri_new(uri
)) == NULL
||
2004 soupuri
->host
== NULL
)
2006 if ((w
= wl_find(soupuri
->host
, &svil
)) != NULL
)
2008 t
->xtp_meaning
= XT_XTP_TAB_MEANING_SV
;
2009 args
.s
= g_strdup((char *)uri
);
2010 xtp_page_sv(t
, &args
);
2016 soup_uri_free(soupuri
);
2023 remove_cookie(int index
)
2029 DNPRINTF(XT_D_COOKIE
, "remove_cookie: %d\n", index
);
2031 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
2033 for (i
= 1; cf
; cf
= cf
->next
, i
++) {
2037 print_cookie("remove cookie", c
);
2038 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2043 soup_cookies_free(cf
);
2049 remove_cookie_domain(int domain_id
)
2051 int domain_count
, rv
= 1;
2056 DNPRINTF(XT_D_COOKIE
, "remove_cookie_domain: %d\n", domain_id
);
2059 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
2061 for (domain_count
= 0; cf
; cf
= cf
->next
) {
2064 if (strcmp(last_domain
, c
->domain
) != 0) {
2066 last_domain
= c
->domain
;
2069 if (domain_count
< domain_id
)
2071 else if (domain_count
> domain_id
)
2074 print_cookie("remove cookie", c
);
2075 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2079 soup_cookies_free(cf
);
2091 DNPRINTF(XT_D_COOKIE
, "remove_cookie_all\n");
2093 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
2095 for (; cf
; cf
= cf
->next
) {
2098 print_cookie("remove cookie", c
);
2099 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2103 soup_cookies_free(cf
);
2109 toplevel_cmd(struct tab
*t
, struct karg
*args
)
2111 js_toggle_cb(t
->js_toggle
, t
);
2117 can_go_back_for_real(struct tab
*t
)
2120 WebKitWebHistoryItem
*item
;
2126 if (t
->item
!= NULL
)
2129 /* rely on webkit to make sure we can go backward when on an about page */
2131 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:") ||
2132 g_str_has_prefix(uri
, "xxxt://"))
2133 return (webkit_web_view_can_go_back(t
->wv
));
2135 /* the back/forward list is stupid so help determine if we can go back */
2136 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2138 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2139 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
2147 can_go_forward_for_real(struct tab
*t
)
2150 WebKitWebHistoryItem
*item
;
2156 /* rely on webkit to make sure we can go forward when on an about page */
2158 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:") ||
2159 g_str_has_prefix(uri
, "xxxt://"))
2160 return (webkit_web_view_can_go_forward(t
->wv
));
2162 /* the back/forwars list is stupid so help selecting a different item */
2163 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2165 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2166 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
2174 go_back_for_real(struct tab
*t
)
2177 WebKitWebHistoryItem
*item
;
2184 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:") ||
2185 g_str_has_prefix(uri
, "xxxt://")) {
2186 webkit_web_view_go_back(t
->wv
);
2189 /* the back/forwars list is stupid so help selecting a different item */
2190 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2192 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2193 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2194 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2201 go_forward_for_real(struct tab
*t
)
2204 WebKitWebHistoryItem
*item
;
2211 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:") ||
2212 g_str_has_prefix(uri
, "xxxt://")) {
2213 webkit_web_view_go_forward(t
->wv
);
2216 /* the back/forwars list is stupid so help selecting a different item */
2217 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2219 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2220 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2221 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2228 navaction(struct tab
*t
, struct karg
*args
)
2230 WebKitWebHistoryItem
*item
= NULL
;
2231 WebKitWebFrame
*frame
;
2233 DNPRINTF(XT_D_NAV
, "navaction: tab %d opcode %d\n",
2234 t
->tab_id
, args
->i
);
2237 set_normal_tab_meaning(t
);
2239 if (args
->i
== XT_NAV_BACK
)
2240 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2242 item
= webkit_web_back_forward_list_get_forward_item(t
->bfl
);
2249 return (XT_CB_PASSTHROUGH
);
2250 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2252 return (XT_CB_PASSTHROUGH
);
2255 go_back_for_real(t
);
2257 case XT_NAV_FORWARD
:
2260 return (XT_CB_PASSTHROUGH
);
2261 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2263 return (XT_CB_PASSTHROUGH
);
2266 go_forward_for_real(t
);
2269 frame
= webkit_web_view_get_main_frame(t
->wv
);
2270 webkit_web_frame_reload(frame
);
2273 frame
= webkit_web_view_get_main_frame(t
->wv
);
2274 webkit_web_frame_stop_loading(frame
);
2277 return (XT_CB_PASSTHROUGH
);
2281 move(struct tab
*t
, struct karg
*args
)
2283 GtkAdjustment
*adjust
;
2284 double pi
, si
, pos
, ps
, upper
, lower
, max
;
2290 case XT_MOVE_BOTTOM
:
2292 case XT_MOVE_PAGEDOWN
:
2293 case XT_MOVE_PAGEUP
:
2294 case XT_MOVE_HALFDOWN
:
2295 case XT_MOVE_HALFUP
:
2296 case XT_MOVE_PERCENT
:
2297 case XT_MOVE_CENTER
:
2298 adjust
= t
->adjust_v
;
2301 adjust
= t
->adjust_h
;
2305 pos
= gtk_adjustment_get_value(adjust
);
2306 ps
= gtk_adjustment_get_page_size(adjust
);
2307 upper
= gtk_adjustment_get_upper(adjust
);
2308 lower
= gtk_adjustment_get_lower(adjust
);
2309 si
= gtk_adjustment_get_step_increment(adjust
);
2310 pi
= gtk_adjustment_get_page_increment(adjust
);
2313 DNPRINTF(XT_D_MOVE
, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2314 "max %f si %f pi %f\n",
2315 args
->i
, adjust
== t
->adjust_h
? "horizontal" : "vertical",
2316 pos
, ps
, upper
, lower
, max
, si
, pi
);
2322 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2327 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2329 case XT_MOVE_BOTTOM
:
2330 case XT_MOVE_FARRIGHT
:
2331 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2332 gtk_adjustment_set_value(adjust
, max
);
2335 case XT_MOVE_FARLEFT
:
2336 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2337 gtk_adjustment_set_value(adjust
, lower
);
2339 case XT_MOVE_PAGEDOWN
:
2341 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2343 case XT_MOVE_PAGEUP
:
2345 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2347 case XT_MOVE_HALFDOWN
:
2349 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2351 case XT_MOVE_HALFUP
:
2353 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2355 case XT_MOVE_CENTER
:
2356 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2357 args
->s
= g_strdup("50.0");
2359 case XT_MOVE_PERCENT
:
2360 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2361 percent
= atoi(args
->s
) / 100.0;
2362 pos
= max
* percent
;
2363 if (pos
< 0.0 || pos
> max
)
2365 gtk_adjustment_set_value(adjust
, pos
);
2368 return (XT_CB_PASSTHROUGH
);
2371 DNPRINTF(XT_D_MOVE
, "move: new pos %f %f\n", pos
, MIN(pos
, max
));
2373 return (XT_CB_HANDLED
);
2377 url_set_visibility(void)
2381 TAILQ_FOREACH(t
, &tabs
, entry
)
2382 if (show_url
== 0) {
2383 gtk_widget_hide(t
->toolbar
);
2386 gtk_widget_show(t
->toolbar
);
2390 notebook_tab_set_visibility(void)
2392 if (show_tabs
== 0) {
2393 gtk_widget_hide(tab_bar
);
2394 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2396 if (tab_style
== XT_TABS_NORMAL
) {
2397 gtk_widget_hide(tab_bar
);
2398 gtk_notebook_set_show_tabs(notebook
, TRUE
);
2399 } else if (tab_style
== XT_TABS_COMPACT
) {
2400 gtk_widget_show(tab_bar
);
2401 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2407 statusbar_set_visibility(void)
2411 TAILQ_FOREACH(t
, &tabs
, entry
){
2412 if (show_statusbar
== 0)
2413 gtk_widget_hide(t
->statusbar
);
2415 gtk_widget_show(t
->statusbar
);
2422 url_set(struct tab
*t
, int enable_url_entry
)
2427 show_url
= enable_url_entry
;
2429 if (enable_url_entry
) {
2430 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.uri
),
2431 GTK_ENTRY_ICON_PRIMARY
, NULL
);
2432 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.uri
), 0);
2434 pixbuf
= gtk_entry_get_icon_pixbuf(GTK_ENTRY(t
->uri_entry
),
2435 GTK_ENTRY_ICON_PRIMARY
);
2437 gtk_entry_get_progress_fraction(GTK_ENTRY(t
->uri_entry
));
2438 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.uri
),
2439 GTK_ENTRY_ICON_PRIMARY
, pixbuf
);
2440 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.uri
),
2446 fullscreen(struct tab
*t
, struct karg
*args
)
2448 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2451 return (XT_CB_PASSTHROUGH
);
2453 if (show_url
== 0) {
2461 url_set_visibility();
2462 notebook_tab_set_visibility();
2464 return (XT_CB_HANDLED
);
2468 statustoggle(struct tab
*t
, struct karg
*args
)
2470 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2472 if (show_statusbar
== 1) {
2474 statusbar_set_visibility();
2475 } else if (show_statusbar
== 0) {
2477 statusbar_set_visibility();
2479 return (XT_CB_HANDLED
);
2483 urlaction(struct tab
*t
, struct karg
*args
)
2485 int rv
= XT_CB_HANDLED
;
2487 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2490 return (XT_CB_PASSTHROUGH
);
2494 if (show_url
== 0) {
2496 url_set_visibility();
2500 if (show_url
== 1) {
2502 url_set_visibility();
2510 tabaction(struct tab
*t
, struct karg
*args
)
2512 int rv
= XT_CB_HANDLED
;
2513 char *url
= args
->s
;
2515 struct tab
*tt
, *tv
;
2517 DNPRINTF(XT_D_TAB
, "tabaction: %p %d\n", t
, args
->i
);
2520 return (XT_CB_PASSTHROUGH
);
2524 if (strlen(url
) > 0)
2525 create_new_tab(url
, NULL
, 1, args
->precount
);
2527 create_new_tab(NULL
, NULL
, 1, args
->precount
);
2530 if (args
->precount
< 0)
2533 TAILQ_FOREACH(tt
, &tabs
, entry
)
2534 if (tt
->tab_id
== args
->precount
- 1) {
2539 case XT_TAB_DELQUIT
:
2540 if (gtk_notebook_get_n_pages(notebook
) > 1)
2546 TAILQ_FOREACH_SAFE(tt
, &tabs
, entry
, tv
)
2551 if (strlen(url
) > 0)
2554 rv
= XT_CB_PASSTHROUGH
;
2560 if (show_tabs
== 0) {
2562 notebook_tab_set_visibility();
2566 if (show_tabs
== 1) {
2568 notebook_tab_set_visibility();
2571 case XT_TAB_NEXTSTYLE
:
2572 if (tab_style
== XT_TABS_NORMAL
) {
2573 tab_style
= XT_TABS_COMPACT
;
2574 recolor_compact_tabs();
2577 tab_style
= XT_TABS_NORMAL
;
2578 notebook_tab_set_visibility();
2580 case XT_TAB_UNDO_CLOSE
:
2581 if (undo_count
== 0) {
2582 DNPRINTF(XT_D_TAB
, "%s: no tabs to undo close",
2587 u
= TAILQ_FIRST(&undos
);
2588 create_new_tab(u
->uri
, u
, 1, -1);
2590 TAILQ_REMOVE(&undos
, u
, entry
);
2592 /* u->history is freed in create_new_tab() */
2596 case XT_TAB_LOAD_IMAGES
:
2598 if (!auto_load_images
) {
2600 /* Enable auto-load images (this will load all
2601 * previously unloaded images). */
2602 g_object_set(G_OBJECT(t
->settings
),
2603 "auto-load-images", TRUE
, (char *)NULL
);
2604 webkit_web_view_set_settings(t
->wv
, t
->settings
);
2606 webkit_web_view_reload(t
->wv
);
2608 /* Webkit triggers an event when we change the setting,
2609 * so we can't disable the auto-loading at once.
2611 * Unfortunately, webkit does not tell us when it's done.
2612 * Instead, we wait until the next request, and then
2613 * disable autoloading again.
2615 t
->load_images
= TRUE
;
2619 rv
= XT_CB_PASSTHROUGH
;
2633 resizetab(struct tab
*t
, struct karg
*args
)
2635 if (t
== NULL
|| args
== NULL
) {
2636 show_oops(NULL
, "resizetab invalid parameters");
2637 return (XT_CB_PASSTHROUGH
);
2640 DNPRINTF(XT_D_TAB
, "resizetab: tab %d %d\n",
2641 t
->tab_id
, args
->i
);
2643 setzoom_webkit(t
, args
->i
);
2645 return (XT_CB_HANDLED
);
2649 movetab(struct tab
*t
, struct karg
*args
)
2653 if (t
== NULL
|| args
== NULL
) {
2654 show_oops(NULL
, "movetab invalid parameters");
2655 return (XT_CB_PASSTHROUGH
);
2658 DNPRINTF(XT_D_TAB
, "movetab: tab %d opcode %d\n",
2659 t
->tab_id
, args
->i
);
2661 if (args
->i
>= XT_TAB_INVALID
)
2662 return (XT_CB_PASSTHROUGH
);
2664 if (TAILQ_EMPTY(&tabs
))
2665 return (XT_CB_PASSTHROUGH
);
2667 n
= gtk_notebook_get_n_pages(notebook
);
2668 dest
= gtk_notebook_get_current_page(notebook
);
2672 if (args
->precount
< 0)
2673 dest
= dest
== n
- 1 ? 0 : dest
+ 1;
2675 dest
= args
->precount
- 1;
2679 if (args
->precount
< 0)
2682 dest
-= args
->precount
% n
;
2695 return (XT_CB_PASSTHROUGH
);
2698 if (dest
< 0 || dest
>= n
)
2699 return (XT_CB_PASSTHROUGH
);
2700 if (t
->tab_id
== dest
) {
2701 DNPRINTF(XT_D_TAB
, "movetab: do nothing\n");
2702 return (XT_CB_HANDLED
);
2705 set_current_tab(dest
);
2707 return (XT_CB_HANDLED
);
2714 const char *(*f
)(struct tab
*);
2716 { "<uri>", get_uri
},
2720 command(struct tab
*t
, struct karg
*args
)
2722 struct karg a
= {0};
2723 int i
, cmd_setup
= 0;
2724 char *s
= NULL
, *sp
= NULL
, *sl
= NULL
;
2727 if (t
== NULL
|| args
== NULL
) {
2728 show_oops(NULL
, "command invalid parameters");
2729 return (XT_CB_PASSTHROUGH
);
2740 if (cmd_prefix
== 0) {
2741 if (args
->s
!= NULL
&& strlen(args
->s
) != 0) {
2742 sp
= g_strdup_printf(":%s", args
->s
);
2747 sp
= g_strdup_printf(":%d", cmd_prefix
);
2757 for (i
= 0; i
< LENGTH(subs
); ++i
) {
2758 sv
= g_strsplit(sl
, subs
[i
].s
, -1);
2761 sl
= g_strjoinv(subs
[i
].f(t
), sv
);
2767 t
->mode
= XT_MODE_HINT
;
2771 * js code will auto fire() if a single link is visible,
2772 * causing the focus-out-event cb function to be called. Setup
2773 * the cmd _before_ triggering hinting code so the cmd can get
2774 * killed by the cb in this case.
2781 t
->mode
= XT_MODE_HINT
;
2782 a
.i
= XT_HINT_NEWTAB
;
2789 show_oops(t
, "command: invalid opcode %d", args
->i
);
2790 return (XT_CB_PASSTHROUGH
);
2793 DNPRINTF(XT_D_CMD
, "%s: tab %d type %s\n", __func__
, t
->tab_id
, s
);
2803 return (XT_CB_HANDLED
);
2807 search(struct tab
*t
, struct karg
*args
)
2811 if (t
== NULL
|| args
== NULL
) {
2812 show_oops(NULL
, "search invalid parameters");
2817 case XT_SEARCH_NEXT
:
2818 d
= t
->search_forward
;
2820 case XT_SEARCH_PREV
:
2821 d
= !t
->search_forward
;
2824 return (XT_CB_PASSTHROUGH
);
2827 if (t
->search_text
== NULL
) {
2828 if (global_search
== NULL
)
2829 return (XT_CB_PASSTHROUGH
);
2831 d
= t
->search_forward
= TRUE
;
2832 t
->search_text
= g_strdup(global_search
);
2833 webkit_web_view_mark_text_matches(t
->wv
, global_search
, FALSE
, 0);
2834 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
2838 DNPRINTF(XT_D_CMD
, "search: tab %d opc %d forw %d text %s\n",
2839 t
->tab_id
, args
->i
, t
->search_forward
, t
->search_text
);
2841 webkit_web_view_search_text(t
->wv
, t
->search_text
, FALSE
, d
, TRUE
);
2843 return (XT_CB_HANDLED
);
2847 session_save(struct tab
*t
, char *filename
)
2853 if (strlen(filename
) == 0)
2856 if (filename
[0] == '.' || filename
[0] == '/')
2860 if (save_tabs(t
, &a
))
2862 strlcpy(named_session
, filename
, sizeof named_session
);
2864 /* add the new session to the list of sessions */
2865 s
= g_malloc(sizeof(struct session
));
2866 s
->name
= g_strdup(filename
);
2867 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
2875 session_open(struct tab
*t
, char *filename
)
2880 if (strlen(filename
) == 0)
2883 if (filename
[0] == '.' || filename
[0] == '/')
2887 a
.i
= XT_SES_CLOSETABS
;
2888 if (open_tabs(t
, &a
))
2891 strlcpy(named_session
, filename
, sizeof named_session
);
2899 session_delete(struct tab
*t
, char *filename
)
2901 char file
[PATH_MAX
];
2905 if (strlen(filename
) == 0)
2908 if (filename
[0] == '.' || filename
[0] == '/')
2911 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, filename
);
2915 if (!strcmp(filename
, named_session
))
2916 strlcpy(named_session
, XT_SAVED_TABS_FILE
,
2917 sizeof named_session
);
2919 /* remove session from sessions list */
2920 TAILQ_FOREACH(s
, &sessions
, entry
) {
2921 if (!strcmp(s
->name
, filename
))
2926 TAILQ_REMOVE(&sessions
, s
, entry
);
2927 g_free((gpointer
) s
->name
);
2936 session_cmd(struct tab
*t
, struct karg
*args
)
2938 char *filename
= args
->s
;
2943 if (args
->i
& XT_SHOW
)
2944 show_oops(t
, "Current session: %s", named_session
[0] == '\0' ?
2945 XT_SAVED_TABS_FILE
: named_session
);
2946 else if (args
->i
& XT_SAVE
) {
2947 if (session_save(t
, filename
)) {
2948 show_oops(t
, "Can't save session: %s",
2949 filename
? filename
: "INVALID");
2952 } else if (args
->i
& XT_OPEN
) {
2953 if (session_open(t
, filename
)) {
2954 show_oops(t
, "Can't open session: %s",
2955 filename
? filename
: "INVALID");
2958 } else if (args
->i
& XT_DELETE
) {
2959 if (session_delete(t
, filename
)) {
2960 show_oops(t
, "Can't delete session: %s",
2961 filename
? filename
: "INVALID");
2966 return (XT_CB_PASSTHROUGH
);
2970 script_cmd(struct tab
*t
, struct karg
*args
)
2979 if ((f
= fopen(args
->s
, "r")) == NULL
) {
2980 show_oops(t
, "Can't open script file: %s", args
->s
);
2984 if (fstat(fileno(f
), &sb
) == -1) {
2985 show_oops(t
, "Can't stat script file: %s", args
->s
);
2989 buf
= g_malloc0(sb
.st_size
+ 1);
2990 if (fread(buf
, 1, sb
.st_size
, f
) != sb
.st_size
) {
2991 show_oops(t
, "Can't read script file: %s", args
->s
);
2995 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
3004 return (XT_CB_PASSTHROUGH
);
3008 * Make a hardcopy of the page
3011 print_page(struct tab
*t
, struct karg
*args
)
3013 WebKitWebFrame
*frame
;
3015 GtkPrintOperation
*op
;
3016 GtkPrintOperationAction action
;
3017 GtkPrintOperationResult print_res
;
3018 GError
*g_err
= NULL
;
3019 int marg_l
, marg_r
, marg_t
, marg_b
;
3022 DNPRINTF(XT_D_PRINTING
, "%s:", __func__
);
3024 ps
= gtk_page_setup_new();
3025 op
= gtk_print_operation_new();
3026 action
= GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
;
3027 frame
= webkit_web_view_get_main_frame(t
->wv
);
3029 /* the default margins are too small, so we will bump them */
3030 marg_l
= gtk_page_setup_get_left_margin(ps
, GTK_UNIT_MM
) +
3031 XT_PRINT_EXTRA_MARGIN
;
3032 marg_r
= gtk_page_setup_get_right_margin(ps
, GTK_UNIT_MM
) +
3033 XT_PRINT_EXTRA_MARGIN
;
3034 marg_t
= gtk_page_setup_get_top_margin(ps
, GTK_UNIT_MM
) +
3035 XT_PRINT_EXTRA_MARGIN
;
3036 marg_b
= gtk_page_setup_get_bottom_margin(ps
, GTK_UNIT_MM
) +
3037 XT_PRINT_EXTRA_MARGIN
;
3040 gtk_page_setup_set_left_margin(ps
, marg_l
, GTK_UNIT_MM
);
3041 gtk_page_setup_set_right_margin(ps
, marg_r
, GTK_UNIT_MM
);
3042 gtk_page_setup_set_top_margin(ps
, marg_t
, GTK_UNIT_MM
);
3043 gtk_page_setup_set_bottom_margin(ps
, marg_b
, GTK_UNIT_MM
);
3045 gtk_print_operation_set_default_page_setup(op
, ps
);
3047 print_res
= webkit_web_frame_print_full(frame
, op
, action
, &g_err
);
3049 /* check it worked */
3050 if (print_res
== GTK_PRINT_OPERATION_RESULT_ERROR
) {
3051 show_oops(NULL
, "can't print: %s", g_err
->message
);
3052 g_error_free (g_err
);
3056 g_object_unref(G_OBJECT(ps
));
3057 g_object_unref(G_OBJECT(op
));
3062 go_home(struct tab
*t
, struct karg
*args
)
3069 set_encoding(struct tab
*t
, struct karg
*args
)
3073 if (args
->s
&& strlen(g_strstrip(args
->s
)) == 0) {
3074 e
= webkit_web_view_get_custom_encoding(t
->wv
);
3076 e
= webkit_web_view_get_encoding(t
->wv
);
3077 show_oops(t
, "encoding: %s", e
? e
: "N/A");
3079 webkit_web_view_set_custom_encoding(t
->wv
, args
->s
);
3085 restart(struct tab
*t
, struct karg
*args
)
3089 a
.s
= XT_RESTART_TABS_FILE
;
3091 execvp(start_argv
[0], start_argv
);
3097 char *http_proxy_save
; /* not a setting, used to toggle */
3100 proxy_cmd(struct tab
*t
, struct karg
*args
)
3104 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
3111 TAILQ_FOREACH(tt
, &tabs
, entry
)
3112 gtk_widget_show(t
->proxy_toggle
);
3113 if (http_proxy_save
)
3114 g_free(http_proxy_save
);
3115 http_proxy_save
= g_strdup(http_proxy
);
3118 if (args
->i
& XT_PRXY_SHOW
) {
3120 show_oops(t
, "http_proxy = %s", http_proxy
);
3122 show_oops(t
, "proxy is currently disabled");
3123 } else if (args
->i
& XT_PRXY_TOGGLE
) {
3124 if (http_proxy_save
== NULL
&& http_proxy
== NULL
) {
3125 show_oops(t
, "can't toggle proxy");
3128 TAILQ_FOREACH(tt
, &tabs
, entry
)
3129 gtk_widget_show(t
->proxy_toggle
);
3131 if (setup_proxy(NULL
) == 0)
3132 button_set_file(t
->proxy_toggle
,
3134 show_oops(t
, "http proxy disabled");
3136 if (setup_proxy(http_proxy_save
) == 0 && http_proxy
) {
3137 button_set_file(t
->proxy_toggle
,
3139 show_oops(t
, "http_proxy = %s", http_proxy
);
3141 show_oops(t
, "invalid proxy: %s", http_proxy_save
);
3145 return (XT_CB_PASSTHROUGH
);
3149 * If you can read this functionthen you are a sick and twisted individual.
3150 * I hope we never meet, it'll be violent.
3153 eval_cb(const GMatchInfo
*info
, GString
*res
, gpointer data
)
3156 gint start
= -1, end
= -1, i
;
3157 struct karg
*args
= data
;
3160 * match contains the string UP TO the match.
3162 * res is what is returned, note that whatever remains in the sent in
3163 * string is appended on the way out.
3165 * for example /123/456/789/moo came in
3166 * match contains /123/456/789/
3167 * we assign that to res and replace /789/ with the replacement text
3168 * then g_regex_replace_eval on the way out has /123/456/replacement/moo
3171 match
= g_match_info_fetch(info
, 0);
3175 if (g_match_info_fetch_pos(info
, 1, &start
, &end
) == FALSE
)
3178 g_string_assign(res
, match
);
3180 i
= atoi(&match
[start
+ 1]);
3181 if (args
->i
== XT_URL_PLUS
)
3186 /* preserve whitespace when likely */
3187 num
= g_strdup_printf("%0*d", end
- start
- 2, i
);
3188 g_string_overwrite_len(res
, start
+ 1, num
, end
- start
- 2);
3194 return (FALSE
); /* doesn't matter */
3198 urlmod_cmd(struct tab
*t
, struct karg
*args
)
3206 if ((uri
= gtk_entry_get_text(GTK_ENTRY(t
->uri_entry
))) == NULL
)
3208 if (strlen(uri
) == 0)
3211 reg
= g_regex_new(".*(/[0-9]+/)", 0, 0, NULL
);
3214 res
= g_regex_replace_eval(reg
, uri
, -1, 0, 0, eval_cb
, args
, NULL
);
3218 if (!strcmp(res
, uri
))
3221 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), res
);
3222 activate_uri_entry_cb(t
->uri_entry
, t
);
3229 return (XT_CB_PASSTHROUGH
);
3235 int (*func
)(struct tab
*, struct karg
*);
3239 { "command_mode", 0, command_mode
, XT_MODE_COMMAND
, 0 },
3240 { "insert_mode", 0, command_mode
, XT_MODE_INSERT
, 0 },
3241 { "command", 0, command
, ':', 0 },
3242 { "search", 0, command
, '/', 0 },
3243 { "searchb", 0, command
, '?', 0 },
3244 { "hinting", 0, command
, '.', 0 },
3245 { "hinting_newtab", 0, command
, ',', 0 },
3246 { "togglesrc", 0, toggle_src
, 0, 0 },
3247 { "editsrc", 0, edit_src
, 0, 0 },
3248 { "editelement", 0, edit_element
, 0, 0 },
3249 { "passthrough", 0, passthrough
, 0, 0 },
3250 { "modurl", 0, modurl
, 0, 0 },
3252 /* yanking and pasting */
3253 { "yankuri", 0, yank_uri
, 0, 0 },
3254 { "pasteuricur", 0, paste_uri
, XT_PASTE_CURRENT_TAB
, 0 },
3255 { "pasteurinew", 0, paste_uri
, XT_PASTE_NEW_TAB
, 0 },
3258 { "searchnext", 0, search
, XT_SEARCH_NEXT
, 0 },
3259 { "searchprevious", 0, search
, XT_SEARCH_PREV
, 0 },
3262 { "focusaddress", 0, focus
, XT_FOCUS_URI
, 0 },
3263 { "focussearch", 0, focus
, XT_FOCUS_SEARCH
, 0 },
3266 { "hinting", 0, hint
, 0, 0 },
3267 { "hinting_newtab", 0, hint
, XT_HINT_NEWTAB
, 0 },
3269 /* custom stylesheet */
3270 { "userstyle", 0, userstyle_cmd
, XT_STYLE_CURRENT_TAB
, XT_USERARG
},
3271 { "userstyle_global", 0, userstyle_cmd
, XT_STYLE_GLOBAL
, XT_USERARG
},
3274 { "goback", 0, navaction
, XT_NAV_BACK
, 0 },
3275 { "goforward", 0, navaction
, XT_NAV_FORWARD
, 0 },
3276 { "reload", 0, navaction
, XT_NAV_RELOAD
, 0 },
3277 { "stop", 0, navaction
, XT_NAV_STOP
, 0 },
3279 /* vertical movement */
3280 { "scrolldown", 0, move
, XT_MOVE_DOWN
, 0 },
3281 { "scrollup", 0, move
, XT_MOVE_UP
, 0 },
3282 { "scrollbottom", 0, move
, XT_MOVE_BOTTOM
, 0 },
3283 { "scrolltop", 0, move
, XT_MOVE_TOP
, 0 },
3284 { "1", 0, move
, XT_MOVE_TOP
, 0 },
3285 { "scrollhalfdown", 0, move
, XT_MOVE_HALFDOWN
, 0 },
3286 { "scrollhalfup", 0, move
, XT_MOVE_HALFUP
, 0 },
3287 { "scrollpagedown", 0, move
, XT_MOVE_PAGEDOWN
, 0 },
3288 { "scrollpageup", 0, move
, XT_MOVE_PAGEUP
, 0 },
3289 /* horizontal movement */
3290 { "scrollright", 0, move
, XT_MOVE_RIGHT
, 0 },
3291 { "scrollleft", 0, move
, XT_MOVE_LEFT
, 0 },
3292 { "scrollfarright", 0, move
, XT_MOVE_FARRIGHT
, 0 },
3293 { "scrollfarleft", 0, move
, XT_MOVE_FARLEFT
, 0 },
3295 { "favorites", 0, xtp_page_fl
, XT_SHOW
, 0 },
3296 { "fav", 0, xtp_page_fl
, XT_SHOW
, 0 },
3297 { "favedit", 0, xtp_page_fl
, XT_SHOW
|XT_DELETE
, 0 },
3298 { "favadd", 0, add_favorite
, 0, XT_USERARG
},
3300 { "qall", 0, quit
, 0, 0 },
3301 { "quitall", 0, quit
, 0, 0 },
3302 { "w", 0, save_tabs
, 0, 0 },
3303 { "wq", 0, save_tabs_and_quit
, 0, 0 },
3304 { "help", 0, help
, 0, 0 },
3305 { "about", 0, xtp_page_ab
, 0, 0 },
3306 { "stats", 0, stats
, 0, 0 },
3307 { "version", 0, xtp_page_ab
, 0, 0 },
3310 { "js", 0, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3311 { "save", 1, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3312 { "domain", 2, js_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3313 { "fqdn", 2, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3314 { "show", 1, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3315 { "all", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3316 { "persistent", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3317 { "session", 2, js_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3318 { "toggle", 1, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3319 { "domain", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3320 { "fqdn", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3322 /* cookie command */
3323 { "cookie", 0, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3324 { "save", 1, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3325 { "domain", 2, cookie_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3326 { "fqdn", 2, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3327 { "show", 1, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3328 { "all", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3329 { "persistent", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3330 { "session", 2, cookie_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3331 { "toggle", 1, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3332 { "domain", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3333 { "fqdn", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3334 { "purge", 1, cookie_cmd
, XT_DELETE
, 0 },
3336 /* plugin command */
3337 { "plugin", 0, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3338 { "save", 1, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3339 { "domain", 2, pl_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3340 { "fqdn", 2, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3341 { "show", 1, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3342 { "all", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3343 { "persistent", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3344 { "session", 2, pl_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3345 { "toggle", 1, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3346 { "domain", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3347 { "fqdn", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3350 { "https", 0, https_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3351 { "save", 1, https_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3352 { "domain", 2, https_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3353 { "fqdn", 2, https_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3354 { "show", 1, https_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3355 { "all", 2, https_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3356 { "persistent", 2, https_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3357 { "session", 2, https_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3358 { "toggle", 1, https_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3359 { "domain", 2, https_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3360 { "fqdn", 2, https_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3362 /* toplevel (domain) command */
3363 { "toplevel", 0, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3364 { "toggle", 1, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3367 { "cookiejar", 0, xtp_page_cl
, 0, 0 },
3370 { "cert", 0, cert_cmd
, XT_SHOW
, 0 },
3371 { "save", 1, cert_cmd
, XT_SAVE
, 0 },
3372 { "show", 1, cert_cmd
, XT_SHOW
, 0 },
3374 { "ca", 0, ca_cmd
, 0, 0 },
3375 { "downloadmgr", 0, xtp_page_dl
, 0, 0 },
3376 { "dl", 0, xtp_page_dl
, 0, 0 },
3377 { "h", 0, xtp_page_hl
, 0, 0 },
3378 { "history", 0, xtp_page_hl
, 0, 0 },
3379 { "home", 0, go_home
, 0, 0 },
3380 { "restart", 0, restart
, 0, 0 },
3381 { "urlhide", 0, urlaction
, XT_URL_HIDE
, 0 },
3382 { "urlshow", 0, urlaction
, XT_URL_SHOW
, 0 },
3383 { "statustoggle", 0, statustoggle
, 0, 0 },
3384 { "run_script", 0, run_page_script
, 0, XT_USERARG
},
3386 { "print", 0, print_page
, 0, 0 },
3389 { "focusin", 0, resizetab
, XT_ZOOM_IN
, 0 },
3390 { "focusout", 0, resizetab
, XT_ZOOM_OUT
, 0 },
3391 { "focusreset", 0, resizetab
, XT_ZOOM_NORMAL
, 0 },
3392 { "q", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3393 { "quit", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3394 { "open", 0, tabaction
, XT_TAB_OPEN
, XT_URLARG
},
3395 { "tabclose", 0, tabaction
, XT_TAB_DELETE
, XT_PREFIX
| XT_INTARG
},
3396 { "tabedit", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3397 { "tabfirst", 0, movetab
, XT_TAB_FIRST
, 0 },
3398 { "tabhide", 0, tabaction
, XT_TAB_HIDE
, 0 },
3399 { "tablast", 0, movetab
, XT_TAB_LAST
, 0 },
3400 { "tabnew", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3401 { "tabnext", 0, movetab
, XT_TAB_NEXT
, XT_PREFIX
| XT_INTARG
},
3402 { "tabnextstyle", 0, tabaction
, XT_TAB_NEXTSTYLE
, 0 },
3403 { "tabonly", 0, tabaction
, XT_TAB_ONLY
, 0 },
3404 { "tabprevious", 0, movetab
, XT_TAB_PREV
, XT_PREFIX
| XT_INTARG
},
3405 { "tabrewind", 0, movetab
, XT_TAB_FIRST
, 0 },
3406 { "tabshow", 0, tabaction
, XT_TAB_SHOW
, 0 },
3407 { "tabs", 0, buffers
, 0, 0 },
3408 { "tabundoclose", 0, tabaction
, XT_TAB_UNDO_CLOSE
, 0 },
3409 { "buffers", 0, buffers
, 0, 0 },
3410 { "ls", 0, buffers
, 0, 0 },
3411 { "encoding", 0, set_encoding
, 0, XT_USERARG
},
3412 { "loadimages", 0, tabaction
, XT_TAB_LOAD_IMAGES
, 0 },
3415 { "set", 0, set
, 0, XT_SETARG
},
3416 { "runtime", 0, xtp_page_rt
, 0, 0 },
3418 { "fullscreen", 0, fullscreen
, 0, 0 },
3419 { "f", 0, fullscreen
, 0, 0 },
3422 { "session", 0, session_cmd
, XT_SHOW
, 0 },
3423 { "delete", 1, session_cmd
, XT_DELETE
, XT_SESSARG
},
3424 { "open", 1, session_cmd
, XT_OPEN
, XT_SESSARG
},
3425 { "save", 1, session_cmd
, XT_SAVE
, XT_USERARG
},
3426 { "show", 1, session_cmd
, XT_SHOW
, 0 },
3428 /* external javascript */
3429 { "script", 0, script_cmd
, XT_EJS_SHOW
, XT_USERARG
},
3432 { "inspector", 0, inspector_cmd
, XT_INS_SHOW
, 0 },
3433 { "show", 1, inspector_cmd
, XT_INS_SHOW
, 0 },
3434 { "hide", 1, inspector_cmd
, XT_INS_HIDE
, 0 },
3437 { "proxy", 0, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3438 { "show", 1, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3439 { "toggle", 1, proxy_cmd
, XT_PRXY_TOGGLE
, 0 },
3442 { "urlmod", 0, urlmod_cmd
, XT_URL
, 0 },
3443 { "plus", 1, urlmod_cmd
, XT_URL_PLUS
, 0 },
3444 { "min", 1, urlmod_cmd
, XT_URL_MIN
, 0 },
3451 } cmd_status
= {-1, 0};
3454 wv_release_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3457 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 1)
3464 wv_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3467 WebKitHitTestResult
*hit_test_result
;
3470 hit_test_result
= webkit_web_view_get_hit_test_result(t
->wv
, e
);
3471 g_object_get(hit_test_result
, "context", &context
, NULL
);
3472 g_object_unref(G_OBJECT(hit_test_result
));
3477 if (context
& WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE
)
3478 t
->mode
= XT_MODE_INSERT
;
3480 t
->mode
= XT_MODE_COMMAND
;
3482 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3484 else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 8 /* btn 4 */) {
3490 } else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 9 /* btn 5 */) {
3492 a
.i
= XT_NAV_FORWARD
;
3502 tab_close_cb(GtkWidget
*btn
, struct tab
*t
)
3504 DNPRINTF(XT_D_TAB
, "tab_close_cb: tab %d\n", t
->tab_id
);
3510 parse_custom_uri(struct tab
*t
, const char *uri
)
3512 struct custom_uri
*u
;
3516 TAILQ_FOREACH(u
, &cul
, entry
) {
3517 if (strncmp(uri
, u
->uri
, strlen(u
->uri
)))
3522 sv
[1] = (char *)uri
;
3524 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
,
3526 show_oops(t
, "%s: could not spawn process", __func__
);
3533 activate_uri_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3535 const gchar
*uri
= gtk_entry_get_text(GTK_ENTRY(entry
));
3537 DNPRINTF(XT_D_URL
, "activate_uri_entry_cb: %s\n", uri
);
3540 show_oops(NULL
, "activate_uri_entry_cb invalid parameters");
3545 show_oops(t
, "activate_uri_entry_cb no uri");
3549 uri
+= strspn(uri
, "\t ");
3551 if (parse_custom_uri(t
, uri
))
3554 /* otherwise continue to load page normally */
3555 load_uri(t
, (gchar
*)uri
);
3560 activate_search_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3562 const gchar
*search
= gtk_entry_get_text(GTK_ENTRY(entry
));
3563 char *newuri
= NULL
;
3567 DNPRINTF(XT_D_URL
, "activate_search_entry_cb: %s\n", search
);
3570 show_oops(NULL
, "activate_search_entry_cb invalid parameters");
3574 if (search_string
== NULL
|| strlen(search_string
) == 0) {
3575 show_oops(t
, "no search_string");
3579 set_normal_tab_meaning(t
);
3581 enc_search
= soup_uri_encode(search
, XT_RESERVED_CHARS
);
3582 sv
= g_strsplit(search_string
, "%s", 2);
3583 newuri
= g_strjoinv(enc_search
, sv
);
3588 load_uri(t
, newuri
);
3596 check_and_set_cookie(const gchar
*uri
, struct tab
*t
)
3598 struct wl_entry
*w
= NULL
;
3601 if (uri
== NULL
|| t
== NULL
)
3604 if ((w
= wl_find_uri(uri
, &c_wl
)) == NULL
)
3609 DNPRINTF(XT_D_COOKIE
, "check_and_set_cookie: %s %s\n",
3610 es
? "enable" : "disable", uri
);
3612 g_object_set(G_OBJECT(t
->settings
),
3613 "enable-html5-local-storage", es
, (char *)NULL
);
3614 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3618 check_and_set_js(const gchar
*uri
, struct tab
*t
)
3620 struct wl_entry
*w
= NULL
;
3623 if (uri
== NULL
|| t
== NULL
)
3626 if ((w
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3631 DNPRINTF(XT_D_JS
, "check_and_set_js: %s %s\n",
3632 es
? "enable" : "disable", uri
);
3634 g_object_set(G_OBJECT(t
->settings
),
3635 "enable-scripts", es
, (char *)NULL
);
3636 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3638 button_set_icon_name(t
->js_toggle
,
3639 es
? "media-playback-start" : "media-playback-pause");
3643 check_and_set_pl(const gchar
*uri
, struct tab
*t
)
3645 struct wl_entry
*w
= NULL
;
3648 if (uri
== NULL
|| t
== NULL
)
3651 if ((w
= wl_find_uri(uri
, &pl_wl
)) == NULL
)
3656 DNPRINTF(XT_D_JS
, "check_and_set_pl: %s %s\n",
3657 es
? "enable" : "disable", uri
);
3659 g_object_set(G_OBJECT(t
->settings
),
3660 "enable-plugins", es
, (char *)NULL
);
3661 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3664 #if GTK_CHECK_VERSION(3, 0, 0)
3665 /* A lot of this can be removed when gtk2 is dropped on the floor */
3667 get_css_name(const char *col_str
)
3671 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3672 name
= g_strdup(XT_CSS_NORMAL
);
3673 else if (!strcmp(col_str
, XT_COLOR_RED
))
3674 name
= g_strdup(XT_CSS_RED
);
3675 else if (!strcmp(col_str
, XT_COLOR_YELLOW
))
3676 name
= g_strdup(XT_CSS_YELLOW
);
3677 else if (!strcmp(col_str
, XT_COLOR_GREEN
))
3678 name
= g_strdup(XT_CSS_GREEN
);
3679 else if (!strcmp(col_str
, XT_COLOR_BLUE
))
3680 name
= g_strdup(XT_CSS_BLUE
);
3686 show_ca_status(struct tab
*t
, const char *uri
)
3688 char domain
[8182], file
[PATH_MAX
];
3689 SoupMessage
*msg
= NULL
;
3690 GTlsCertificate
*cert
= NULL
;
3691 GTlsCertificateFlags flags
= 0;
3692 gchar
*col_str
= XT_COLOR_RED
;
3696 #if GTK_CHECK_VERSION(3, 0, 0)
3702 enum cert_trust trust
;
3706 DNPRINTF(XT_D_URL
, "show_ca_status: %d %s %s\n",
3707 ssl_strict_certs
, ssl_ca_file
, uri
);
3712 if (uri
== NULL
|| g_str_has_prefix(uri
, "http://") ||
3713 !g_str_has_prefix(uri
, "https://"))
3717 * Cut the uri to get the certs off the homepage. We can't use the
3718 * full URI here since it may include arguments and we don't want to make
3719 * these requests multiple times.
3721 cut_uri
= g_strdup(uri
);
3723 for (i
= 0; i
< 3; ++i
)
3724 s
= strchr(&(s
[1]), '/');
3727 msg
= soup_message_new("HEAD", cut_uri
);
3731 soup_message_set_flags(msg
, SOUP_MESSAGE_NO_REDIRECT
);
3732 soup_session_send_message(session
, msg
);
3733 if (msg
->status_code
== SOUP_STATUS_SSL_FAILED
||
3734 msg
->status_code
== SOUP_STATUS_TLS_FAILED
) {
3735 DNPRINTF(XT_D_URL
, "%s: status not ok: %d\n", uri
,
3739 if (!soup_message_get_https_status(msg
, &cert
, &flags
)) {
3740 DNPRINTF(XT_D_URL
, "%s: invalid response\n", uri
);
3743 if (!G_IS_TLS_CERTIFICATE(cert
)) {
3744 DNPRINTF(XT_D_URL
, "%s: no cert\n", uri
);
3749 col_str
= XT_COLOR_GREEN
;
3751 col_str
= XT_COLOR_YELLOW
;
3753 strlcpy(domain
, uri
+ strlen("https://"), sizeof domain
);
3754 for (i
= 0; i
< strlen(domain
); i
++)
3755 if (domain
[i
] == '/') {
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
;
3768 snprintf(file
, sizeof file
, "%s" PS
"%s", certs_cache_dir
, domain
);
3769 if (warn_cert_changes
) {
3770 if (check_cert_changes(t
, cert
, file
, uri
))
3775 g_object_unref(msg
);
3776 if (!strcmp(col_str
, XT_COLOR_WHITE
) || nocolor
) {
3777 #if GTK_CHECK_VERSION(3, 0, 0)
3778 gtk_widget_set_name(t
->uri_entry
, XT_CSS_NORMAL
);
3779 statusbar_modify_attr(t
, XT_CSS_NORMAL
);
3781 text
= gdk_color_to_string(
3782 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3783 base
= gdk_color_to_string(
3784 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3785 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3786 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3787 statusbar_modify_attr(t
, text
, base
);
3792 #if GTK_CHECK_VERSION(3, 0, 0)
3793 name
= get_css_name(col_str
);
3794 gtk_widget_set_name(t
->uri_entry
, name
);
3795 statusbar_modify_attr(t
, name
);
3798 gdk_color_parse(col_str
, &color
);
3799 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3800 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3806 free_favicon(struct tab
*t
)
3808 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p req %p\n",
3809 __func__
, t
->icon_download
, t
->icon_request
);
3811 if (t
->icon_request
)
3812 g_object_unref(t
->icon_request
);
3813 if (t
->icon_dest_uri
)
3814 g_free(t
->icon_dest_uri
);
3816 t
->icon_request
= NULL
;
3817 t
->icon_dest_uri
= NULL
;
3821 xt_icon_from_name(struct tab
*t
, gchar
*name
)
3823 if (!enable_favicon_entry
)
3826 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->uri_entry
),
3827 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3829 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.uri
),
3830 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3832 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.uri
),
3833 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3837 xt_icon_from_pixbuf(struct tab
*t
, GdkPixbuf
*pb
)
3839 GdkPixbuf
*pb_scaled
;
3841 if (gdk_pixbuf_get_width(pb
) > 16 || gdk_pixbuf_get_height(pb
) > 16)
3842 pb_scaled
= gdk_pixbuf_scale_simple(pb
, 16, 16,
3843 GDK_INTERP_BILINEAR
);
3847 if (enable_favicon_entry
) {
3850 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
3851 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3854 if (show_url
== 0) {
3855 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.uri
),
3856 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3858 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.uri
),
3859 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3862 /* XXX: Only supports the minimal tabs atm. */
3863 if (enable_favicon_tabs
)
3864 gtk_image_set_from_pixbuf(GTK_IMAGE(t
->tab_elems
.favicon
),
3867 if (pb_scaled
!= pb
)
3868 g_object_unref(pb_scaled
);
3872 xt_icon_from_file(struct tab
*t
, char *uri
)
3877 if (g_str_has_prefix(uri
, "file://"))
3878 file
= g_filename_from_uri(uri
, NULL
, NULL
);
3880 file
= g_strdup(uri
);
3885 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
3887 xt_icon_from_pixbuf(t
, pb
);
3890 xt_icon_from_name(t
, "text-html");
3896 is_valid_icon(char *file
)
3899 const char *mime_type
;
3903 gf
= g_file_new_for_path(file
);
3904 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
3906 mime_type
= g_file_info_get_content_type(fi
);
3907 valid
= g_strcmp0(mime_type
, "image/x-ico") == 0 ||
3908 g_strcmp0(mime_type
, "image/vnd.microsoft.icon") == 0 ||
3909 g_strcmp0(mime_type
, "image/png") == 0 ||
3910 g_strcmp0(mime_type
, "image/gif") == 0 ||
3911 g_strcmp0(mime_type
, "application/octet-stream") == 0;
3919 set_favicon_from_file(struct tab
*t
, char *uri
)
3924 if (t
== NULL
|| uri
== NULL
)
3927 if (g_str_has_prefix(uri
, "file://"))
3928 file
= g_filename_from_uri(uri
, NULL
, NULL
);
3930 file
= g_strdup(uri
);
3935 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading %s\n", __func__
, file
);
3937 if (!stat(file
, &sb
)) {
3938 if (sb
.st_size
== 0 || !is_valid_icon(file
)) {
3939 /* corrupt icon so trash it */
3940 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3943 /* no need to set icon to default here */
3947 xt_icon_from_file(t
, file
);
3953 favicon_download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
3956 WebKitDownloadStatus status
= webkit_download_get_status(download
);
3957 struct tab
*tt
= NULL
, *t
= NULL
;
3960 * find the webview instead of passing in the tab as it could have been
3961 * deleted from underneath us.
3963 TAILQ_FOREACH(tt
, &tabs
, entry
) {
3972 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d status %d\n",
3973 __func__
, t
->tab_id
, status
);
3976 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
3978 t
->icon_download
= NULL
;
3981 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
3984 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
3987 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
3989 DNPRINTF(XT_D_DOWNLOAD
, "%s: freeing favicon %d\n",
3990 __func__
, t
->tab_id
);
3991 t
->icon_download
= NULL
;
3994 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
3997 DNPRINTF(XT_D_DOWNLOAD
, "%s: setting icon to %s\n",
3998 __func__
, t
->icon_dest_uri
);
3999 set_favicon_from_file(t
, t
->icon_dest_uri
);
4000 /* these will be freed post callback */
4001 t
->icon_request
= NULL
;
4002 t
->icon_download
= NULL
;
4010 abort_favicon_download(struct tab
*t
)
4012 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p\n", __func__
, t
->icon_download
);
4014 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
4015 if (t
->icon_download
) {
4016 g_signal_handlers_disconnect_by_func(G_OBJECT(t
->icon_download
),
4017 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
4018 webkit_download_cancel(t
->icon_download
);
4019 t
->icon_download
= NULL
;
4024 xt_icon_from_name(t
, "text-html");
4028 notify_icon_loaded_cb(WebKitWebView
*wv
, gchar
*uri
, struct tab
*t
)
4030 DNPRINTF(XT_D_DOWNLOAD
, "%s %s\n", __func__
, uri
);
4032 if (uri
== NULL
|| t
== NULL
)
4035 #if WEBKIT_CHECK_VERSION(1, 4, 0)
4036 /* take icon from WebKitIconDatabase */
4037 GdkPixbuf
*pb
= NULL
;
4039 /* webkit_web_view_get_icon_pixbuf is depreciated in 1.8 */
4040 #if WEBKIT_CHECK_VERSION(1, 8, 0)
4042 * If the page was not loaded (for example, via ssl_strict_certs), do
4043 * not attempt to get the webview's pixbuf. This prevents a CRITICAL
4046 if (wv
&& webkit_web_view_get_uri(wv
))
4047 pb
= webkit_web_view_try_get_favicon_pixbuf(wv
, 0, 0);
4049 if (wv
&& webkit_web_view_get_uri(wv
))
4050 pb
= webkit_web_view_get_icon_pixbuf(wv
);
4053 xt_icon_from_pixbuf(t
, pb
);
4056 xt_icon_from_name(t
, "text-html");
4057 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
4058 /* download icon to cache dir */
4059 gchar
*name_hash
, file
[PATH_MAX
];
4062 if (t
->icon_request
) {
4063 DNPRINTF(XT_D_DOWNLOAD
, "%s: download in progress\n", __func__
);
4067 /* check to see if we got the icon in cache */
4068 name_hash
= g_compute_checksum_for_string(G_CHECKSUM_SHA256
, uri
, -1);
4069 snprintf(file
, sizeof file
, "%s" PS
"%s.ico", cache_dir
, name_hash
);
4072 if (!stat(file
, &sb
)) {
4073 if (sb
.st_size
> 0) {
4074 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading from cache %s\n",
4076 set_favicon_from_file(t
, file
);
4080 /* corrupt icon so trash it */
4081 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
4086 /* create download for icon */
4087 t
->icon_request
= webkit_network_request_new(uri
);
4088 if (t
->icon_request
== NULL
) {
4089 DNPRINTF(XT_D_DOWNLOAD
, "%s: invalid uri %s\n",
4094 t
->icon_download
= webkit_download_new(t
->icon_request
);
4095 if (t
->icon_download
== NULL
)
4098 /* we have to free icon_dest_uri later */
4099 if ((t
->icon_dest_uri
= g_filename_to_uri(file
, NULL
, NULL
)) == NULL
)
4101 webkit_download_set_destination_uri(t
->icon_download
,
4104 if (webkit_download_get_status(t
->icon_download
) ==
4105 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
4106 g_object_unref(t
->icon_request
);
4107 g_free(t
->icon_dest_uri
);
4108 t
->icon_request
= NULL
;
4109 t
->icon_dest_uri
= NULL
;
4113 g_signal_connect(G_OBJECT(t
->icon_download
), "notify::status",
4114 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
4116 webkit_download_start(t
->icon_download
);
4121 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
4123 const gchar
*uri
= NULL
;
4124 struct history
*h
, find
;
4126 gchar
*tmp_uri
= NULL
;
4127 #if !GTK_CHECK_VERSION(3, 0, 0)
4131 DNPRINTF(XT_D_URL
, "notify_load_status_cb: %d %s\n",
4132 webkit_web_view_get_load_status(wview
),
4133 get_uri(t
) ? get_uri(t
) : "NOTHING");
4136 show_oops(NULL
, "notify_load_status_cb invalid parameters");
4140 switch (webkit_web_view_get_load_status(wview
)) {
4141 case WEBKIT_LOAD_PROVISIONAL
:
4143 abort_favicon_download(t
);
4144 #if GTK_CHECK_VERSION(2, 20, 0)
4145 gtk_widget_show(t
->spinner
);
4146 gtk_spinner_start(GTK_SPINNER(t
->spinner
));
4148 t
->download_requested
= 0;
4150 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), TRUE
);
4152 /* assume we are a new address */
4153 #if GTK_CHECK_VERSION(3, 0, 0)
4154 gtk_widget_set_name(t
->uri_entry
, XT_CSS_NORMAL
);
4155 statusbar_modify_attr(t
, XT_CSS_NORMAL
);
4157 text
= gdk_color_to_string(
4158 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
4159 base
= gdk_color_to_string(
4160 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
4161 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
4162 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
4163 statusbar_modify_attr(t
, text
, base
);
4168 /* DOM is changing, unreference the previous focused element */
4169 #if WEBKIT_CHECK_VERSION(1, 5, 0)
4171 g_object_unref(t
->active
);
4173 if (t
->active_text
) {
4174 g_free(t
->active_text
);
4175 t
->active_text
= NULL
;
4179 /* take focus if we are visible */
4186 case WEBKIT_LOAD_COMMITTED
:
4191 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
4197 set_status(t
, "Loading: %s", (char *)uri
);
4199 /* clear t->item, except if we're switching to an about: page */
4200 if (t
->item
&& !g_str_has_prefix(uri
, "xxxt://") &&
4201 !g_str_has_prefix(uri
, "about:")) {
4202 g_object_unref(t
->item
);
4206 /* check if js white listing is enabled */
4207 if (enable_plugin_whitelist
)
4208 check_and_set_pl(uri
, t
);
4209 if (enable_cookie_whitelist
)
4210 check_and_set_cookie(uri
, t
);
4211 if (enable_js_whitelist
)
4212 check_and_set_js(uri
, t
);
4218 /* we know enough to autosave the session */
4219 if (session_autosave
) {
4224 show_ca_status(t
, uri
);
4225 run_script(t
, JS_HINTING
);
4226 if (enable_autoscroll
)
4227 run_script(t
, JS_AUTOSCROLL
);
4230 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
4232 if (color_visited_uris
) {
4233 color_visited(t
, color_visited_helper());
4236 * This colors the links you middle-click (open in new
4237 * tab) in the current tab.
4239 if (t
->tab_id
!= gtk_notebook_get_current_page(notebook
) &&
4240 (uri
= get_uri(t
)) != NULL
)
4241 color_visited(get_current_tab(),
4242 g_strdup_printf("{'%s' : 'dummy'}", uri
));
4246 case WEBKIT_LOAD_FINISHED
:
4248 if ((uri
= get_uri(t
)) == NULL
)
4251 * js_autorun calls get_uri which frees t->tmp_uri if on an
4252 * "about:" page. On "about:" pages, uri points to t->tmp_uri.
4253 * I.e. we will use freed memory. Prevent that.
4255 tmp_uri
= g_strdup(uri
);
4257 /* autorun some js if enabled */
4262 if (!strncmp(tmp_uri
, "http://", strlen("http://")) ||
4263 !strncmp(tmp_uri
, "https://", strlen("https://")) ||
4264 !strncmp(tmp_uri
, "file://", strlen("file://"))) {
4265 find
.uri
= (gchar
*)tmp_uri
;
4266 h
= RB_FIND(history_list
, &hl
, &find
);
4268 insert_history_item(tmp_uri
,
4269 get_title(t
, FALSE
), time(NULL
));
4271 h
->time
= time(NULL
);
4274 if (statusbar_style
== XT_STATUSBAR_URL
)
4275 set_status(t
, "%s", (char *)tmp_uri
);
4277 set_status(t
, "%s", get_title(t
, FALSE
));
4278 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
4279 #if GTK_CHECK_VERSION(2, 20, 0)
4280 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
4281 gtk_widget_hide(t
->spinner
);
4286 #if WEBKIT_CHECK_VERSION(1, 1, 18)
4287 case WEBKIT_LOAD_FAILED
:
4289 if (!t
->download_requested
) {
4290 gtk_label_set_text(GTK_LABEL(t
->label
),
4291 get_title(t
, FALSE
));
4292 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
),
4293 get_title(t
, FALSE
));
4294 set_status(t
, "%s", (char *)get_title(t
, FALSE
));
4295 gtk_window_set_title(GTK_WINDOW(main_window
),
4296 get_title(t
, TRUE
));
4302 #if GTK_CHECK_VERSION(2, 20, 0)
4303 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
4304 gtk_widget_hide(t
->spinner
);
4306 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
4310 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
),
4311 can_go_back_for_real(t
));
4313 gtk_widget_set_sensitive(GTK_WIDGET(t
->forward
),
4314 can_go_forward_for_real(t
));
4318 notify_title_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
4320 const gchar
*title
= NULL
, *win_title
= NULL
;
4322 title
= get_title(t
, FALSE
);
4323 win_title
= get_title(t
, TRUE
);
4325 gtk_label_set_text(GTK_LABEL(t
->label
), title
);
4326 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
), title
);
4329 if (win_title
&& t
->tab_id
== gtk_notebook_get_current_page(notebook
))
4330 gtk_window_set_title(GTK_WINDOW(main_window
), win_title
);
4334 get_domain(const gchar
*host
)
4339 /* handle silly domains like .co.uk */
4341 if ((x
= strlen(host
)) <= 6)
4342 return (g_strdup(host
));
4344 if (host
[x
- 3] == '.' && host
[x
- 6] == '.') {
4350 return (g_strdup(&host
[x
+ 1]));
4354 p
= g_strrstr(host
, ".");
4356 return (g_strdup(""));
4362 return (g_strdup(p
+ 1));
4364 return (g_strdup(host
));
4368 js_autorun(struct tab
*t
)
4372 size_t got_default
= 0, got_host
= 0;
4374 char deff
[PATH_MAX
], hostf
[PATH_MAX
];
4375 char *js
= NULL
, *jsat
, *domain
= NULL
;
4376 FILE *deffile
= NULL
, *hostfile
= NULL
;
4378 if (enable_js_autorun
== 0)
4383 !(g_str_has_prefix(uri
, "http://") ||
4384 g_str_has_prefix(uri
, "https://")))
4387 su
= soup_uri_new(uri
);
4390 if (!SOUP_URI_VALID_FOR_HTTP(su
))
4393 DNPRINTF(XT_D_JS
, "%s: host: %s domain: %s\n", __func__
,
4395 domain
= get_domain(su
->host
);
4397 snprintf(deff
, sizeof deff
, "%s" PS
"default.js", js_dir
);
4398 if ((deffile
= fopen(deff
, "r")) != NULL
) {
4399 if (fstat(fileno(deffile
), &sb
) == -1) {
4400 show_oops(t
, "can't stat default JS file");
4403 got_default
= sb
.st_size
;
4406 /* try host first followed by domain */
4407 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, su
->host
);
4408 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4409 if ((hostfile
= fopen(hostf
, "r")) == NULL
) {
4410 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, domain
);
4411 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4412 if ((hostfile
= fopen(hostf
, "r")) == NULL
)
4415 DNPRINTF(XT_D_JS
, "file: %s\n", hostf
);
4416 if (fstat(fileno(hostfile
), &sb
) == -1) {
4417 show_oops(t
, "can't stat %s JS file", hostf
);
4420 got_host
= sb
.st_size
;
4423 if (got_default
+ got_host
== 0)
4426 js
= g_malloc0(got_default
+ got_host
+ 1);
4430 if (fread(js
, got_default
, 1, deffile
) != 1) {
4431 show_oops(t
, "default file read error");
4434 jsat
= js
+ got_default
;
4438 if (fread(jsat
, got_host
, 1, hostfile
) != 1) {
4439 show_oops(t
, "host file read error");
4444 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
4461 webview_progress_changed_cb(WebKitWebView
*wv
, GParamSpec
*pspec
, struct tab
*t
)
4465 progress
= webkit_web_view_get_progress(wv
);
4466 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.uri
),
4467 progress
> (1.0 - 0.0001) ? 0 : progress
);
4468 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->uri_entry
),
4469 progress
> (1.0 - 0.0001) ? 0 : progress
);
4471 update_statusbar_position(NULL
, NULL
);
4475 strict_transport_rb_cmp(struct strict_transport
*a
, struct strict_transport
*b
)
4480 /* compare strings from the end */
4481 l1
= strlen(a
->host
);
4482 l2
= strlen(b
->host
);
4486 for (; *p1
== *p2
&& p1
> a
->host
&& p2
> b
->host
;
4491 * Check if we need to do pattern expansion,
4492 * or if we're just keeping the tree in order
4494 if (a
->flags
& XT_STS_FLAGS_EXPAND
&&
4495 b
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) {
4496 /* Check if we're matching the
4497 * 'host.xyz' part in '*.host.xyz'
4499 if (p2
== b
->host
&& (p1
== a
->host
|| *(p1
-1) == '.')) {
4504 if (p1
== a
->host
&& p2
== b
->host
)
4518 RB_GENERATE(strict_transport_tree
, strict_transport
, entry
,
4519 strict_transport_rb_cmp
);
4522 strict_transport_add(const char *domain
, time_t timeout
, int subdomains
)
4524 struct strict_transport
*d
, find
;
4528 if (enable_strict_transport
== FALSE
)
4531 DPRINTF("strict_transport_add(%s,%" PRIi64
",%d)\n", domain
,
4532 (uint64_t)timeout
, subdomains
);
4538 find
.host
= (char *)domain
;
4540 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4544 /* check if update is needed */
4545 if (d
->timeout
== timeout
&&
4546 (d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) == subdomains
)
4549 d
->timeout
= timeout
;
4551 d
->flags
|= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4553 /* We're still initializing */
4554 if (strict_transport_file
== NULL
)
4557 if ((f
= fopen(strict_transport_file
, "w")) == NULL
) {
4559 "can't open strict-transport rules file");
4563 fprintf(f
, "# Generated file - do not update unless you know "
4564 "what you're doing\n");
4565 RB_FOREACH(d
, strict_transport_tree
, &st_tree
) {
4566 if (d
->timeout
< now
)
4568 fprintf(f
, "%s\t%" PRIi64
"\t%d\n", d
->host
,
4569 (uint64_t)d
->timeout
,
4570 d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
);
4574 d
= g_malloc(sizeof *d
);
4575 d
->host
= g_strdup(domain
);
4576 d
->timeout
= timeout
;
4578 d
->flags
= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4581 RB_INSERT(strict_transport_tree
, &st_tree
, d
);
4583 /* We're still initializing */
4584 if (strict_transport_file
== NULL
)
4587 if ((f
= fopen(strict_transport_file
, "a+")) == NULL
) {
4589 "can't open strict-transport rules file");
4593 fseek(f
, 0, SEEK_END
);
4594 fprintf(f
,"%s\t%" PRIi64
"\t%d\n", d
->host
, (uint64_t)timeout
,
4602 strict_transport_check(const char *host
)
4604 static struct strict_transport
*d
= NULL
;
4605 struct strict_transport find
;
4607 if (enable_strict_transport
== FALSE
)
4610 find
.host
= (char *)host
;
4612 /* match for domains that include subdomains */
4613 find
.flags
= XT_STS_FLAGS_EXPAND
;
4615 /* First, check if we're already at the right node */
4616 if (d
!= NULL
&& strict_transport_rb_cmp(&find
, d
) == 0) {
4620 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4628 strict_transport_init()
4630 char file
[PATH_MAX
];
4636 time_t timeout
, now
;
4639 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_STS_FILE
);
4640 if ((f
= fopen(file
, "r")) == NULL
) {
4641 strict_transport_file
= g_strdup(file
);
4652 if ((rule
= fparseln(f
, &len
, NULL
, delim
, 0)) == NULL
) {
4653 if (!feof(f
) || ferror(f
))
4659 /* get second entry */
4660 if ((ptr
= strpbrk(rule
, " \t")) == NULL
)
4664 timeout
= atoi(ptr
);
4666 /* get third entry */
4667 if ((ptr
= strpbrk(ptr
, " \t")) == NULL
)
4671 subdomains
= atoi(ptr
);
4674 strict_transport_add(rule
, timeout
, subdomains
);
4679 strict_transport_file
= g_strdup(file
);
4683 startpage_add("strict-transport rules file ('%s') is corrupt", file
);
4691 force_https_check(const char *uri
)
4693 struct wl_entry
*w
= NULL
;
4698 if ((w
= wl_find_uri(uri
, &force_https
)) == NULL
)
4705 strict_transport_security_cb(SoupMessage
*msg
, gpointer data
)
4711 int subdomains
= FALSE
;
4716 sts
= soup_message_headers_get_one(msg
->response_headers
,
4717 "Strict-Transport-Security");
4718 uri
= soup_message_get_uri(msg
);
4720 if (sts
== NULL
|| uri
== NULL
)
4723 if ((ptr
= strcasestr(sts
, "max-age="))) {
4724 ptr
+= strlen("max-age=");
4725 timeout
= atoll(ptr
);
4727 return; /* malformed header - max-age must be included */
4729 if ((ptr
= strcasestr(sts
, "includeSubDomains")))
4732 strict_transport_add(uri
->host
, timeout
+ time(NULL
), subdomains
);
4736 session_rq_cb(SoupSession
*s
, SoupMessage
*msg
, SoupSocket
*socket
,
4746 if (s
== NULL
|| msg
== NULL
)
4749 if (enable_strict_transport
) {
4750 soup_message_add_header_handler(msg
, "finished",
4751 "Strict-Transport-Security",
4752 G_CALLBACK(strict_transport_security_cb
), NULL
);
4755 if (referer_mode
== XT_REFERER_ALWAYS
)
4758 /* Check if referer is set - and what the user requested for referers */
4759 ref
= soup_message_headers_get_one(msg
->request_headers
, "Referer");
4761 DNPRINTF(XT_D_NAV
, "session_rq_cb: Referer: %s\n", ref
);
4762 switch (referer_mode
) {
4763 case XT_REFERER_NEVER
:
4764 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing referer\n");
4765 soup_message_headers_remove(msg
->request_headers
,
4768 case XT_REFERER_SAME_DOMAIN
:
4769 ref_uri
= soup_uri_new(ref
);
4770 dest
= soup_message_get_uri(msg
);
4772 if (ref_uri
== NULL
|| dest
== NULL
)
4775 ref_suffix
= tld_get_suffix(ref_uri
->host
);
4776 dest_suffix
= tld_get_suffix(dest
->host
);
4778 if (ref_suffix
&& dest_suffix
&&
4779 strcmp(ref_suffix
, dest_suffix
) != 0) {
4780 soup_message_headers_remove(msg
->request_headers
,
4782 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4783 "referer (not same domain) (suffixes: %s - %s)\n",
4784 ref_suffix
, dest_suffix
);
4786 soup_uri_free(ref_uri
);
4788 case XT_REFERER_SAME_FQDN
:
4789 ref_uri
= soup_uri_new(ref
);
4790 dest
= soup_message_get_uri(msg
);
4792 if (ref_uri
== NULL
|| dest
== NULL
)
4795 if (strcmp(ref_uri
->host
, dest
->host
) != 0) {
4796 soup_message_headers_remove(msg
->request_headers
,
4798 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4799 "referer (not same fqdn) (should be %s)\n",
4802 soup_uri_free(ref_uri
);
4804 case XT_REFERER_CUSTOM
:
4805 DNPRINTF(XT_D_NAV
, "session_rq_cb: setting referer "
4806 "to %s\n", referer_custom
);
4807 soup_message_headers_replace(msg
->request_headers
,
4808 "Referer", referer_custom
);
4815 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
4816 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
4817 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
4819 WebKitWebNavigationReason reason
;
4823 show_oops(NULL
, "webview_npd_cb invalid parameters");
4827 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
4829 webkit_network_request_get_uri(request
));
4831 uri
= (char *)webkit_network_request_get_uri(request
);
4833 if (!auto_load_images
&& t
->load_images
) {
4835 /* Disable autoloading of images, now that we're done loading
4837 g_object_set(G_OBJECT(t
->settings
),
4838 "auto-load-images", FALSE
, (char *)NULL
);
4839 webkit_web_view_set_settings(t
->wv
, t
->settings
);
4841 t
->load_images
= FALSE
;
4844 /* If this is an xtp url, we don't load anything else. */
4845 if (parse_xtp_url(t
, uri
)) {
4846 webkit_web_policy_decision_ignore(pd
);
4850 if (parse_custom_uri(t
, uri
)) {
4851 webkit_web_policy_decision_ignore(pd
);
4855 if (valid_url_type(uri
)) {
4856 show_oops(t
, "Stopping attempt to load an invalid URI (possible"
4857 " bait and switch attack)");
4858 webkit_web_policy_decision_ignore(pd
);
4862 if ((t
->mode
== XT_MODE_HINT
&& t
->new_tab
) || t
->ctrl_click
) {
4864 create_new_tab(uri
, NULL
, ctrl_click_focus
, -1);
4865 webkit_web_policy_decision_ignore(pd
);
4866 return (TRUE
); /* we made the decission */
4870 * This is a little hairy but it comes down to this:
4871 * when we run in whitelist mode we have to assist the browser in
4872 * opening the URL that it would have opened in a new tab.
4874 reason
= webkit_web_navigation_action_get_reason(na
);
4875 if (reason
== WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED
) {
4876 set_normal_tab_meaning(t
);
4877 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1)
4879 webkit_web_policy_decision_use(pd
);
4880 return (TRUE
); /* we made the decision */
4887 webview_rrs_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, WebKitWebResource
*res
,
4888 WebKitNetworkRequest
*request
, WebKitNetworkResponse
*response
,
4891 SoupMessage
*msg
= NULL
;
4892 SoupURI
*uri
= NULL
;
4893 struct http_accept ha_find
, *ha
= NULL
;
4894 struct user_agent ua_find
, *ua
= NULL
;
4895 struct domain_id di_find
, *di
= NULL
;
4898 msg
= webkit_network_request_get_message(request
);
4902 uri
= soup_message_get_uri(msg
);
4905 uri_s
= soup_uri_to_string(uri
, FALSE
);
4907 if (strcmp(uri
->scheme
, SOUP_URI_SCHEME_HTTP
) == 0) {
4908 if (strict_transport_check(uri
->host
) ||
4909 force_https_check(uri_s
)) {
4910 DNPRINTF(XT_D_NAV
, "webview_rrs_cb: force https for %s\n",
4912 soup_uri_set_scheme(uri
, SOUP_URI_SCHEME_HTTPS
);
4917 soup_message_headers_append(msg
->request_headers
, "DNT", "1");
4920 * Check if resources on this domain have been loaded before. If
4921 * not, add the current tab's http-accept and user-agent id's to a
4922 * new domain_id and insert into the RB tree. Use these http headers
4923 * for all resources loaded from this domain for the lifetime of the
4926 if ((di_find
.domain
= uri
->host
) == NULL
)
4928 if ((di
= RB_FIND(domain_id_list
, &di_list
, &di_find
)) == NULL
) {
4929 di
= g_malloc(sizeof *di
);
4930 di
->domain
= g_strdup(uri
->host
);
4931 di
->ua_id
= t
->user_agent_id
++;
4932 di
->ha_id
= t
->http_accept_id
++;
4933 RB_INSERT(domain_id_list
, &di_list
, di
);
4935 ua_find
.id
= t
->user_agent_id
;
4936 ua
= RB_FIND(user_agent_list
, &ua_list
, &ua_find
);
4938 t
->user_agent_id
= 0;
4940 ha_find
.id
= t
->http_accept_id
;
4941 ha
= RB_FIND(http_accept_list
, &ha_list
, &ha_find
);
4943 t
->http_accept_id
= 0;
4946 ua_find
.id
= di
->ua_id
;
4947 ua
= RB_FIND(user_agent_list
, &ua_list
, &ua_find
);
4948 ha_find
.id
= di
->ha_id
;
4949 ha
= RB_FIND(http_accept_list
, &ha_list
, &ha_find
);
4952 soup_message_headers_replace(msg
->request_headers
,
4953 "User-Agent", ua
->value
);
4955 soup_message_headers_replace(msg
->request_headers
,
4956 "Accept", ha
->value
);
4964 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4967 struct wl_entry
*w
= NULL
;
4969 WebKitWebView
*webview
= NULL
;
4972 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
4973 webkit_web_view_get_uri(wv
));
4976 /* open in current tab */
4978 } else if (enable_scripts
== 0 && enable_js_whitelist
== 1) {
4979 uri
= webkit_web_view_get_uri(wv
);
4980 if (uri
&& (w
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4983 if (t
->ctrl_click
) {
4984 x
= ctrl_click_focus
;
4987 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4989 } else if (enable_scripts
== 1) {
4990 if (t
->ctrl_click
) {
4991 x
= ctrl_click_focus
;
4994 tt
= create_new_tab(NULL
, NULL
, x
, -1);
5002 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
5005 struct wl_entry
*w
= NULL
;
5007 DNPRINTF(XT_D_NAV
, "webview_close_cb: %d\n", t
->tab_id
);
5009 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
5010 uri
= webkit_web_view_get_uri(wv
);
5011 if (uri
&& (w
= wl_find_uri(uri
, &js_wl
)) == NULL
)
5015 } else if (enable_scripts
== 1)
5022 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
5024 /* we can not eat the event without throwing gtk off so defer it */
5026 /* catch middle click */
5027 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 2) {
5032 /* catch ctrl click */
5033 if (e
->type
== GDK_BUTTON_RELEASE
&&
5034 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
5039 return (XT_CB_PASSTHROUGH
);
5043 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
5045 struct mime_type
*m
;
5047 GError
*gerr
= NULL
;
5049 m
= find_mime_type(mime_type
);
5055 sv
[0] = m
->mt_action
;
5056 sv
[1] = (char *)webkit_network_request_get_uri(request
);
5059 /* ignore donothing from example config */
5060 if (m
->mt_action
&& !strcmp(m
->mt_action
, "donothing"))
5063 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
, NULL
,
5065 show_oops(t
, "%s: could not spawn process (%s)", __func__
,
5066 gerr
? gerr
->message
: "N/A");
5071 get_mime_type(const char *uri
)
5076 char *mime_type
= NULL
;
5080 show_oops(NULL
, "%s: invalid parameters", __func__
);
5084 if (g_str_has_prefix(uri
, "file://"))
5085 file
= g_filename_from_uri(uri
, NULL
, NULL
);
5087 file
= g_strdup(uri
);
5092 gf
= g_file_new_for_path(file
);
5093 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
5095 if ((m
= g_file_info_get_content_type(fi
)) != NULL
)
5096 mime_type
= g_strdup(m
);
5105 run_download_mimehandler(char *mime_type
, char *file
)
5107 struct mime_type
*m
;
5110 m
= find_mime_type(mime_type
);
5114 sv
[0] = m
->mt_action
;
5117 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
, NULL
,
5119 show_oops(NULL
, "%s: could not spawn process: %s %s", __func__
,
5127 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
5130 WebKitDownloadStatus status
;
5136 if (download
== NULL
)
5138 status
= webkit_download_get_status(download
);
5139 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
5142 if (download_notifications
) {
5143 /* because basename() takes a char * on linux */
5144 destination
= g_strdup(
5145 webkit_download_get_destination_uri(download
));
5146 show_oops(NULL
, "Download of '%s' finished",
5147 basename(destination
));
5148 g_free(destination
);
5150 uri
= webkit_download_get_destination_uri(download
);
5153 mime
= get_mime_type(uri
);
5157 if (g_str_has_prefix(uri
, "file://"))
5158 file
= g_filename_from_uri(uri
, NULL
, NULL
);
5160 file
= g_strdup(uri
);
5165 run_download_mimehandler((char *)mime
, file
);
5172 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
5173 WebKitNetworkRequest
*request
, char *mime_type
,
5174 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
5177 show_oops(NULL
, "webview_mimetype_cb invalid parameters");
5181 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
5182 t
->tab_id
, mime_type
);
5184 if (run_mimehandler(t
, mime_type
, request
) == 0) {
5185 webkit_web_policy_decision_ignore(decision
);
5190 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
5191 webkit_web_policy_decision_download(decision
);
5199 download_start(struct tab
*t
, struct download
*d
, int flag
)
5201 WebKitNetworkRequest
*req
;
5203 const gchar
*suggested_name
;
5204 gchar
*filename
= NULL
;
5211 if (d
== NULL
|| t
== NULL
) {
5212 show_oops(NULL
, "%s invalid parameters", __func__
);
5216 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
5217 if (suggested_name
== NULL
)
5218 return (FALSE
); /* abort download */
5229 filename
= g_strdup_printf("%d%s", i
, suggested_name
);
5232 /* XXX using urls doesn't work properly in windows? */
5233 uri
= g_strdup_printf("%s\\%s", download_dir
, i
?
5234 filename
: suggested_name
);
5236 path
= g_strdup_printf("%s" PS
"%s", download_dir
, i
?
5237 filename
: suggested_name
);
5238 if ((uri
= g_filename_to_uri(path
, NULL
, NULL
)) == NULL
)
5243 } while (!stat(uri
, &sb
));
5245 } while (!stat(path
, &sb
));
5248 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d filename %s "
5249 "local %s\n", __func__
, t
->tab_id
, filename
, uri
);
5251 /* if we're restarting the download, or starting
5252 * it after doing something else, we need to recreate
5253 * the download request.
5255 if (flag
== XT_DL_RESTART
) {
5256 req
= webkit_network_request_new(webkit_download_get_uri(d
->download
));
5257 webkit_download_cancel(d
->download
);
5258 g_object_unref(d
->download
);
5259 d
->download
= webkit_download_new(req
);
5260 g_object_unref(req
);
5263 webkit_download_set_destination_uri(d
->download
, uri
);
5265 if (webkit_download_get_status(d
->download
) ==
5266 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
5267 show_oops(t
, "%s: download failed to start", __func__
);
5269 show_oops(t
, "Download Failed");
5271 /* connect "download first" mime handler */
5272 g_signal_connect(G_OBJECT(d
->download
), "notify::status",
5273 G_CALLBACK(download_status_changed_cb
), NULL
);
5275 /* get from history */
5276 g_object_ref(d
->download
);
5277 /* because basename() takes a char * on linux */
5278 destination
= g_strdup(
5279 webkit_download_get_destination_uri(d
->download
));
5280 show_oops(t
, "Download of '%s' started...",
5281 basename(destination
));
5282 g_free(destination
);
5285 if (flag
!= XT_DL_START
)
5286 webkit_download_start(d
->download
);
5288 DNPRINTF(XT_D_DOWNLOAD
, "download status : %d",
5289 webkit_download_get_status(d
->download
));
5291 /* sync other download manager tabs */
5292 update_download_tabs(NULL
);
5305 download_ask_cb(struct tab
*t
, GdkEventKey
*e
, gpointer data
)
5307 struct download
*d
= data
;
5311 t
->mode_cb_data
= NULL
;
5315 e
->keyval
= GDK_Escape
;
5316 return (XT_CB_PASSTHROUGH
);
5319 DPRINTF("download_ask_cb: User pressed %c\n", e
->keyval
);
5320 if (e
->keyval
== 'y' || e
->keyval
== 'Y' || e
->keyval
== GDK_Return
)
5321 /* We need to do a RESTART, because we're not calling from
5322 * webview_download_cb
5324 download_start(t
, d
, XT_DL_RESTART
);
5326 /* for all other keyvals, we just let the download be */
5327 e
->keyval
= GDK_Escape
;
5328 return (XT_CB_HANDLED
);
5332 download_ask(struct tab
*t
, struct download
*d
)
5334 const gchar
*suggested_name
;
5336 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
5337 if (suggested_name
== NULL
)
5338 return (FALSE
); /* abort download */
5340 show_oops(t
, "download file %s [y/n] ?", suggested_name
);
5341 t
->mode_cb
= download_ask_cb
;
5342 t
->mode_cb_data
= d
;
5348 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
5351 const gchar
*suggested_name
;
5352 struct download
*download_entry
;
5355 if (wk_download
== NULL
|| t
== NULL
) {
5356 show_oops(NULL
, "%s invalid parameters", __func__
);
5360 suggested_name
= webkit_download_get_suggested_filename(wk_download
);
5361 if (suggested_name
== NULL
)
5362 return (FALSE
); /* abort download */
5364 download_entry
= g_malloc(sizeof(struct download
));
5365 download_entry
->download
= wk_download
;
5366 download_entry
->tab
= t
;
5367 download_entry
->id
= next_download_id
++;
5368 RB_INSERT(download_list
, &downloads
, download_entry
);
5369 t
->download_requested
= 1;
5371 if (download_mode
== XT_DM_START
)
5372 ret
= download_start(t
, download_entry
, XT_DL_START
);
5373 else if (download_mode
== XT_DM_ASK
)
5374 ret
= download_ask(t
, download_entry
);
5375 else if (download_mode
== XT_DM_ADD
)
5376 show_oops(t
, "added %s to download manager",
5379 /* sync other download manager tabs */
5380 update_download_tabs(NULL
);
5383 * NOTE: never redirect/render the current tab before this
5384 * function returns. This will cause the download to never start.
5386 return (ret
); /* start download */
5390 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
5392 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
5395 show_oops(NULL
, "webview_hover_cb");
5400 set_status(t
, "Link: %s", uri
);
5402 if (statusbar_style
== XT_STATUSBAR_URL
) {
5403 const gchar
*page_uri
;
5405 if ((page_uri
= get_uri(t
)) != NULL
)
5406 set_status(t
, "%s", page_uri
);
5408 set_status(t
, "%s", (char *)get_title(t
, FALSE
));
5413 mark(struct tab
*t
, struct karg
*arg
)
5420 if ((index
= marktoindex(mark
)) == -1)
5423 if (arg
->i
== XT_MARK_SET
)
5424 t
->mark
[index
] = gtk_adjustment_get_value(t
->adjust_v
);
5425 else if (arg
->i
== XT_MARK_GOTO
) {
5426 if (t
->mark
[index
] > (XT_INVALID_MARK
- 0.001) &&
5427 t
->mark
[index
] < (XT_INVALID_MARK
+ 0.001)) {
5428 show_oops(t
, "mark '%c' does not exist", mark
);
5431 /* XXX t->mark[index] can be bigger than the maximum if ajax or
5432 something changes the document size */
5433 pos
= gtk_adjustment_get_value(t
->adjust_v
);
5434 gtk_adjustment_set_value(t
->adjust_v
, t
->mark
[index
]);
5435 t
->mark
[marktoindex('\'')] = pos
;
5442 marks_clear(struct tab
*t
)
5446 for (i
= 0; i
< LENGTH(t
->mark
); i
++)
5447 t
->mark
[i
] = XT_INVALID_MARK
;
5453 char file
[PATH_MAX
];
5454 char *line
= NULL
, *p
;
5459 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
5460 if ((f
= fopen(file
, "r+")) == NULL
) {
5461 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
5465 for (i
= 1; ; i
++) {
5466 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
5468 if (strlen(line
) == 0 || line
[0] == '#') {
5474 p
= strtok(line
, " \t");
5476 if (p
== NULL
|| strlen(p
) != 1 ||
5477 (index
= qmarktoindex(*p
)) == -1) {
5478 warnx("corrupt quickmarks file, line %d", i
);
5482 p
= strtok(NULL
, " \t");
5483 if (qmarks
[index
] != NULL
)
5484 g_free(qmarks
[index
]);
5485 qmarks
[index
] = g_strdup(p
);
5496 char file
[PATH_MAX
];
5500 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
5501 if ((f
= fopen(file
, "r+")) == NULL
) {
5502 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
5506 for (i
= 0; i
< XT_NOQMARKS
; i
++)
5507 if (qmarks
[i
] != NULL
)
5508 fprintf(f
, "%c %s\n", indextoqmark(i
), qmarks
[i
]);
5516 qmark(struct tab
*t
, struct karg
*arg
)
5521 mark
= arg
->s
[strlen(arg
->s
)-1];
5522 index
= qmarktoindex(mark
);
5528 if (qmarks
[index
] != NULL
) {
5529 g_free(qmarks
[index
]);
5530 qmarks
[index
] = NULL
;
5533 qmarks_load(); /* sync if multiple instances */
5534 qmarks
[index
] = g_strdup(get_uri(t
));
5538 if (qmarks
[index
] != NULL
)
5539 load_uri(t
, qmarks
[index
]);
5541 show_oops(t
, "quickmark \"%c\" does not exist",
5547 if (qmarks
[index
] != NULL
)
5548 create_new_tab(qmarks
[index
], NULL
, 1, -1);
5550 show_oops(t
, "quickmark \"%c\" does not exist",
5561 go_up(struct tab
*t
, struct karg
*args
)
5569 if (args
->i
== XT_GO_UP_ROOT
)
5570 levels
= XT_GO_UP_ROOT
;
5571 else if ((levels
= atoi(args
->s
)) == 0)
5574 uri
= g_strdup(get_uri(t
));
5578 if ((tmp
= strstr(uri
, XT_PROTO_DELIM
)) == NULL
)
5581 tmp
+= strlen(XT_PROTO_DELIM
);
5583 /* it makes no sense to strip the last slash from ".../dir/", skip it */
5584 lastidx
= strlen(tmp
) - 1;
5586 if (tmp
[lastidx
] == '/')
5587 tmp
[lastidx
] = '\0';
5591 p
= strrchr(tmp
, '/');
5592 if (p
== tmp
) { /* Are we at the root of a file://-path? */
5595 } else if (p
!= NULL
)
5608 gototab(struct tab
*t
, struct karg
*args
)
5611 struct karg arg
= {0, NULL
, -1};
5613 tab
= atoi(args
->s
);
5616 arg
.i
= XT_TAB_NEXT
;
5628 zoom_amount(struct tab
*t
, struct karg
*arg
)
5630 struct karg narg
= {0, NULL
, -1};
5632 narg
.i
= atoi(arg
->s
);
5633 resizetab(t
, &narg
);
5639 flip_colon(struct tab
*t
, struct karg
*arg
)
5641 struct karg narg
= {0, NULL
, -1};
5644 if (t
== NULL
|| arg
== NULL
)
5647 p
= strstr(arg
->s
, ":");
5659 /* buffer commands receive the regex that triggered them in arg.s */
5660 char bcmd
[XT_BUFCMD_SZ
];
5664 #define XT_PRE_NO (0)
5665 #define XT_PRE_YES (1)
5666 #define XT_PRE_MAYBE (2)
5668 int (*func
)(struct tab
*, struct karg
*);
5672 { "^[0-9]*gu$", XT_PRE_MAYBE
, "gu", go_up
, 0 },
5673 { "^gU$", XT_PRE_NO
, "gU", go_up
, XT_GO_UP_ROOT
},
5674 { "^gg$", XT_PRE_NO
, "gg", move
, XT_MOVE_TOP
},
5675 { "^gG$", XT_PRE_NO
, "gG", move
, XT_MOVE_BOTTOM
},
5676 { "^[0-9]+%$", XT_PRE_YES
, "%", move
, XT_MOVE_PERCENT
},
5677 { "^zz$", XT_PRE_NO
, "zz", move
, XT_MOVE_CENTER
},
5678 { "^gh$", XT_PRE_NO
, "gh", go_home
, 0 },
5679 { "^m[a-zA-Z0-9]$", XT_PRE_NO
, "m", mark
, XT_MARK_SET
},
5680 { "^['][a-zA-Z0-9']$", XT_PRE_NO
, "'", mark
, XT_MARK_GOTO
},
5681 { "^[0-9]+t$", XT_PRE_YES
, "t", gototab
, 0 },
5682 { "^g0$", XT_PRE_YES
, "g0", movetab
, XT_TAB_FIRST
},
5683 { "^g[$]$", XT_PRE_YES
, "g$", movetab
, XT_TAB_LAST
},
5684 { "^[0-9]*gt$", XT_PRE_YES
, "t", movetab
, XT_TAB_NEXT
},
5685 { "^[0-9]*gT$", XT_PRE_YES
, "T", movetab
, XT_TAB_PREV
},
5686 { "^M[a-zA-Z0-9]$", XT_PRE_NO
, "M", qmark
, XT_QMARK_SET
},
5687 { "^go[a-zA-Z0-9]$", XT_PRE_NO
, "go", qmark
, XT_QMARK_OPEN
},
5688 { "^gn[a-zA-Z0-9]$", XT_PRE_NO
, "gn", qmark
, XT_QMARK_TAB
},
5689 { "^ZR$", XT_PRE_NO
, "ZR", restart
, 0 },
5690 { "^ZZ$", XT_PRE_NO
, "ZZ", quit
, 0 },
5691 { "^zi$", XT_PRE_NO
, "zi", resizetab
, XT_ZOOM_IN
},
5692 { "^zo$", XT_PRE_NO
, "zo", resizetab
, XT_ZOOM_OUT
},
5693 { "^z0$", XT_PRE_NO
, "z0", resizetab
, XT_ZOOM_NORMAL
},
5694 { "^[0-9]+Z$", XT_PRE_YES
, "Z", zoom_amount
, 0 },
5695 { "^[0-9]+:$", XT_PRE_YES
, ":", flip_colon
, 0 },
5699 buffercmd_init(void)
5703 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5704 if (regcomp(&buffercmds
[i
].cregex
, buffercmds
[i
].regex
,
5705 REG_EXTENDED
| REG_NOSUB
))
5706 startpage_add("invalid buffercmd regex %s",
5707 buffercmds
[i
].regex
);
5711 buffercmd_abort(struct tab
*t
)
5718 DNPRINTF(XT_D_BUFFERCMD
, "%s: clearing buffer\n", __func__
);
5720 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5723 cmd_prefix
= 0; /* clear prefix for non-buffer commands */
5724 if (t
->sbe
.buffercmd
)
5725 gtk_label_set_text(GTK_LABEL(t
->sbe
.buffercmd
), bcmd
);
5729 buffercmd_execute(struct tab
*t
, struct buffercmd
*cmd
)
5731 struct karg arg
= {0, NULL
, -1};
5734 arg
.s
= g_strdup(bcmd
);
5736 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_execute: buffer \"%s\" "
5737 "matches regex \"%s\", executing\n", bcmd
, cmd
->regex
);
5747 buffercmd_addkey(struct tab
*t
, guint keyval
)
5750 char s
[XT_BUFCMD_SZ
];
5752 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
))) {
5754 return (XT_CB_PASSTHROUGH
);
5757 if (keyval
== GDK_Escape
) {
5759 return (XT_CB_HANDLED
);
5762 /* key with modifier or non-ascii character */
5763 if (!isascii(keyval
)) {
5765 * XXX this looks wrong but fixes some sites like
5766 * http://www.seslisozluk.com/
5767 * that eat a shift or ctrl and end putting default focus in js
5768 * instead of ignoring the keystroke
5769 * so instead of return (XT_CB_PASSTHROUGH); eat the key
5771 return (XT_CB_HANDLED
);
5774 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: adding key \"%c\" "
5775 "to buffer \"%s\"\n", keyval
, bcmd
);
5777 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5778 if (bcmd
[i
] == '\0') {
5783 /* buffer full, ignore input */
5784 if (i
>= LENGTH(bcmd
) -1) {
5785 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: buffer full\n");
5787 return (XT_CB_HANDLED
);
5790 if (t
->sbe
.buffercmd
!= NULL
)
5791 gtk_label_set_text(GTK_LABEL(t
->sbe
.buffercmd
), bcmd
);
5793 /* find exact match */
5794 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5795 if (regexec(&buffercmds
[i
].cregex
, bcmd
,
5796 (size_t) 0, NULL
, 0) == 0) {
5797 buffercmd_execute(t
, &buffercmds
[i
]);
5801 /* find non exact matches to see if we need to abort ot not */
5802 for (i
= 0, match
= 0; i
< LENGTH(buffercmds
); i
++) {
5803 DNPRINTF(XT_D_BUFFERCMD
, "trying: %s\n", bcmd
);
5806 if (buffercmds
[i
].precount
== XT_PRE_MAYBE
) {
5807 if (isdigit((unsigned char)bcmd
[0])) {
5808 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5812 if (sscanf(bcmd
, "%s", s
) == 0)
5815 } else if (buffercmds
[i
].precount
== XT_PRE_YES
) {
5816 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5819 if (sscanf(bcmd
, "%s", s
) == 0)
5822 if (c
== -1 && buffercmds
[i
].precount
)
5824 if (!strncmp(s
, buffercmds
[i
].cmd
, strlen(s
)))
5827 DNPRINTF(XT_D_BUFFERCMD
, "got[%d] %d <%s>: %d %s\n",
5828 i
, match
, buffercmds
[i
].cmd
, c
, s
);
5831 DNPRINTF(XT_D_BUFFERCMD
, "aborting: %s\n", bcmd
);
5836 return (XT_CB_HANDLED
);
5840 * XXX we were seeing a bunch of focus issues with the toplevel
5841 * main_window losing its is-active and has-toplevel-focus properties.
5842 * This is the most correct and portable solution we could come up with
5843 * without relying on calling internal GTK functions (which we
5844 * couldn't link to in Linux).
5846 #if GTK_CHECK_VERSION(3, 0, 0)
5848 fake_focus_in(GtkWidget
*w
)
5850 if (fevent
== NULL
) {
5851 fevent
= gdk_event_new(GDK_FOCUS_CHANGE
);
5852 fevent
->focus_change
.window
=
5853 gtk_widget_get_window(main_window
);
5854 fevent
->focus_change
.type
= GDK_FOCUS_CHANGE
;
5855 fevent
->focus_change
.in
= TRUE
;
5857 gtk_widget_send_focus_change(main_window
, fevent
);
5862 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
5865 struct key_binding
*k
;
5868 * This sometimes gets randomly unset for whatever reason in GTK3.
5869 * If we're handling a keypress, the main window's is-active propery
5870 * *must* be true, or else many things will break.
5872 #if GTK_CHECK_VERSION(3, 0, 0)
5873 fake_focus_in(main_window
);
5876 /* handle keybindings if buffercmd is empty.
5877 if not empty, allow commands like C-n */
5878 if (bcmd
[0] == '\0' || ((e
->state
& (CTRL
| MOD1
)) != 0))
5879 TAILQ_FOREACH(k
, &kbl
, entry
)
5880 if (e
->keyval
== k
->key
5881 && (entry
? k
->use_in_entry
: 1)) {
5882 /* when we are edditing eat ctrl/mod keys */
5883 if (edit_mode
== XT_EM_VI
&&
5884 t
->mode
== XT_MODE_INSERT
&&
5885 (e
->state
& CTRL
|| e
->state
& MOD1
))
5886 return (XT_CB_PASSTHROUGH
);
5889 if ((e
->state
& (CTRL
| MOD1
)) == 0)
5891 } else if ((e
->state
& k
->mask
) == k
->mask
) {
5896 if (!entry
&& ((e
->state
& (CTRL
| MOD1
)) == 0))
5897 return (buffercmd_addkey(t
, e
->keyval
));
5899 return (XT_CB_PASSTHROUGH
);
5902 if (k
->cmd
[0] == ':') {
5904 args
.s
= &k
->cmd
[1];
5905 return (command(t
, &args
));
5907 return (cmd_execute(t
, k
->cmd
));
5911 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5915 /* don't use w directly; use t->whatever instead */
5918 show_oops(NULL
, "wv_keypress_cb");
5919 return (XT_CB_PASSTHROUGH
);
5925 return (t
->mode_cb(t
, e
, t
->mode_cb_data
));
5927 DNPRINTF(XT_D_KEY
, "wv_keypress_cb: mode %d keyval 0x%x mask "
5928 "0x%x tab %d\n", t
->mode
, e
->keyval
, e
->state
, t
->tab_id
);
5930 /* Hide buffers, if they are visible, with escape. */
5931 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)) &&
5932 CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
5933 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5935 return (XT_CB_HANDLED
);
5938 if ((CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Tab
) ||
5939 (CLEAN(e
->state
) == SHFT
&& e
->keyval
== GDK_Tab
))
5940 /* something focussy is about to happen */
5941 return (XT_CB_PASSTHROUGH
);
5943 /* check if we are some sort of text input thing in the dom */
5944 input_check_mode(t
);
5946 if (t
->mode
== XT_MODE_PASSTHROUGH
) {
5947 if (CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
)
5948 t
->mode
= XT_MODE_COMMAND
;
5949 return (XT_CB_PASSTHROUGH
);
5950 } else if (t
->mode
== XT_MODE_COMMAND
|| t
->mode
== XT_MODE_HINT
) {
5952 snprintf(s
, sizeof s
, "%c", e
->keyval
);
5953 if (CLEAN(e
->state
) == 0 && isdigit((unsigned char)s
[0]))
5954 cmd_prefix
= 10 * cmd_prefix
+ atoi(s
);
5955 return (handle_keypress(t
, e
, 0));
5958 return (handle_keypress(t
, e
, 1));
5962 return (XT_CB_PASSTHROUGH
);
5966 hint_continue(struct tab
*t
)
5968 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5970 const gchar
*errstr
= NULL
;
5974 if (!(c
[0] == '.' || c
[0] == ','))
5976 if (strlen(c
) == 1) {
5977 /* XXX should not happen */
5982 if (isdigit((unsigned char)c
[1])) {
5984 i
= strtonum(&c
[1], 1, 4096, &errstr
);
5986 show_oops(t
, "invalid numerical hint %s", &c
[1]);
5989 s
= g_strdup_printf("hints.updateHints(%d);", i
);
5993 /* alphanumeric input */
5994 s
= g_strdup_printf("hints.createHints('%s', '%c');",
5995 &c
[1], c
[0] == '.' ? 'f' : 'F');
6006 search_continue(struct tab
*t
)
6008 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
6009 gboolean rv
= FALSE
;
6011 if (c
[0] == ':' || c
[0] == '.' || c
[0] == ',')
6013 if (strlen(c
) == 1) {
6014 webkit_web_view_unmark_text_matches(t
->wv
);
6019 t
->search_forward
= TRUE
;
6020 else if (c
[0] == '?')
6021 t
->search_forward
= FALSE
;
6031 search_cb(struct tab
*t
)
6033 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
6034 #if !GTK_CHECK_VERSION(3, 0, 0)
6038 if (search_continue(t
) == FALSE
)
6042 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, t
->search_forward
,
6044 /* not found, mark red */
6045 #if GTK_CHECK_VERSION(3, 0, 0)
6046 gtk_widget_set_name(t
->cmd
, XT_CSS_RED
);
6048 gdk_color_parse(XT_COLOR_RED
, &color
);
6049 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
6051 /* unmark and remove selection */
6052 webkit_web_view_unmark_text_matches(t
->wv
);
6053 /* my kingdom for a way to unselect text in webview */
6055 /* found, highlight all */
6056 webkit_web_view_unmark_text_matches(t
->wv
);
6057 webkit_web_view_mark_text_matches(t
->wv
, &c
[1], FALSE
, 0);
6058 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
6059 #if GTK_CHECK_VERSION(3, 0, 0)
6060 gtk_widget_set_name(t
->cmd
, XT_CSS_NORMAL
);
6062 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
6063 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
6072 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
6074 const gchar
*c
= gtk_entry_get_text(w
);
6077 show_oops(NULL
, "cmd_keyrelease_cb invalid parameters");
6078 return (XT_CB_PASSTHROUGH
);
6081 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
6082 e
->keyval
, e
->state
, t
->tab_id
);
6085 if (!(e
->keyval
== GDK_Tab
|| e
->keyval
== GDK_ISO_Left_Tab
)) {
6086 if (hint_continue(t
) == FALSE
)
6091 if (search_continue(t
) == FALSE
)
6094 /* if search length is > 4 then no longer play timeout games */
6095 if (strlen(c
) > 4) {
6097 g_source_remove(t
->search_id
);
6104 /* reestablish a new timer if the user types fast */
6106 g_source_remove(t
->search_id
);
6107 t
->search_id
= g_timeout_add(250, (GSourceFunc
)search_cb
, (gpointer
)t
);
6110 return (XT_CB_PASSTHROUGH
);
6114 match_uri(const gchar
*uri
, const gchar
*key
) {
6117 gboolean match
= FALSE
;
6121 if (!strncmp(key
, uri
, len
))
6124 voffset
= strstr(uri
, "/") + 2;
6125 if (!strncmp(key
, voffset
, len
))
6127 else if (g_str_has_prefix(voffset
, "www.")) {
6128 voffset
= voffset
+ strlen("www.");
6129 if (!strncmp(key
, voffset
, len
))
6138 match_session(const gchar
*name
, const gchar
*key
) {
6141 sub
= strcasestr(name
, key
);
6147 cmd_getlist(int id
, char *key
)
6157 if (cmds
[id
].type
& XT_URLARG
) {
6158 RB_FOREACH_REVERSE(h
, history_list
, &hl
)
6159 if (match_uri(h
->uri
, key
)) {
6160 cmd_status
.list
[c
] = (char *)h
->uri
;
6166 } else if (cmds
[id
].type
& XT_SESSARG
) {
6167 TAILQ_FOREACH(s
, &sessions
, entry
)
6168 if (match_session(s
->name
, key
)) {
6169 cmd_status
.list
[c
] = (char *)s
->name
;
6175 } else if (cmds
[id
].type
& XT_SETARG
) {
6176 for (i
= 0; i
< get_settings_size(); i
++)
6177 if (!strncmp(key
, get_setting_name(i
),
6179 cmd_status
.list
[c
++] =
6180 get_setting_name(i
);
6186 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
6188 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
6189 if (cmds
[i
].level
< dep
)
6191 if (cmds
[i
].level
== dep
&& !strncmp(key
, cmds
[i
].cmd
,
6192 strlen(key
)) && !isdigit((unsigned char)cmds
[i
].cmd
[0]))
6193 cmd_status
.list
[c
++] = cmds
[i
].cmd
;
6201 cmd_getnext(int dir
)
6203 cmd_status
.index
+= dir
;
6205 if (cmd_status
.index
< 0)
6206 cmd_status
.index
= cmd_status
.len
- 1;
6207 else if (cmd_status
.index
>= cmd_status
.len
)
6208 cmd_status
.index
= 0;
6210 return cmd_status
.list
[cmd_status
.index
];
6214 cmd_tokenize(char *s
, char *tokens
[])
6217 char *tok
, *last
= NULL
;
6218 size_t len
= strlen(s
);
6221 blank
= len
== 0 || (len
> 0 && s
[len
- 1] == ' ');
6222 for (tok
= strtok_r(s
, " ", &last
); tok
&& i
< 3;
6223 tok
= strtok_r(NULL
, " ", &last
), i
++)
6233 cmd_complete(struct tab
*t
, char *str
, int dir
)
6235 GtkEntry
*w
= GTK_ENTRY(t
->cmd
);
6236 int i
, j
, levels
, c
= 0, dep
= 0, parent
= -1;
6238 char *tok
, *match
, *s
= g_strdup(str
);
6239 char *tokens
[3] = {0};
6240 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
6243 DNPRINTF(XT_D_CMD
, "%s: complete %s\n", __func__
, str
);
6246 for (i
= 0; isdigit((unsigned char)s
[i
]); i
++)
6249 for (; isspace((unsigned char)s
[i
]); i
++)
6254 levels
= cmd_tokenize(s
, tokens
);
6256 for (i
= 0; i
< levels
- 1; i
++) {
6259 for (j
= c
; j
< LENGTH(cmds
); j
++) {
6260 if (cmds
[j
].level
< dep
)
6262 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
,
6266 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
6273 if (matchcount
== 1) {
6274 strlcat(res
, tok
, sizeof res
);
6275 strlcat(res
, " ", sizeof res
);
6285 if (cmd_status
.index
== -1)
6286 cmd_getlist(parent
, tokens
[i
]);
6288 if (cmd_status
.len
> 0) {
6289 match
= cmd_getnext(dir
);
6290 strlcat(res
, match
, sizeof res
);
6291 gtk_entry_set_text(w
, res
);
6292 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6299 parse_prefix_and_alias(const char *str
, int *prefix
)
6301 struct cmd_alias
*c
;
6302 char *s
= g_strdup(str
), *sc
;
6307 if (isdigit((unsigned char)s
[0])) {
6308 sscanf(s
, "%d", prefix
);
6309 while (isdigit((unsigned char)s
[0]) ||
6310 isspace((unsigned char)s
[0]))
6314 TAILQ_FOREACH(c
, &cal
, entry
) {
6315 if (strncmp(s
, c
->alias
, strlen(c
->alias
)))
6318 if (strlen(s
) == strlen(c
->alias
)) {
6320 return (g_strdup(c
->cmd
));
6323 if (!isspace((unsigned char)s
[strlen(c
->alias
)]))
6326 s
= g_strdup_printf("%s %s", c
->cmd
, &s
[strlen(c
->alias
) + 1]);
6336 cmd_execute(struct tab
*t
, char *str
)
6338 struct cmd
*cmd
= NULL
;
6339 char *tok
, *last
= NULL
, *s
= str
;
6340 int j
= 0, len
, c
= 0, dep
= 0, matchcount
= 0;
6341 int prefix
= -1, rv
= XT_CB_PASSTHROUGH
;
6342 struct karg arg
= {0, NULL
, -1};
6344 s
= parse_prefix_and_alias(s
, &prefix
);
6346 for (tok
= strtok_r(s
, " ", &last
); tok
;
6347 tok
= strtok_r(NULL
, " ", &last
)) {
6349 for (j
= c
; j
< LENGTH(cmds
); j
++) {
6350 if (cmds
[j
].level
< dep
)
6352 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1 :
6354 if (cmds
[j
].level
== dep
&&
6355 !strncmp(tok
, cmds
[j
].cmd
, len
)) {
6359 if (len
== strlen(cmds
[j
].cmd
)) {
6365 if (matchcount
== 1) {
6370 show_oops(t
, "Invalid command: %s", str
);
6376 show_oops(t
, "Empty command");
6382 arg
.precount
= prefix
;
6383 else if (cmd_prefix
> 0)
6384 arg
.precount
= cmd_prefix
;
6386 if (j
> 0 && !(cmd
->type
& XT_PREFIX
) && arg
.precount
> -1) {
6387 show_oops(t
, "No prefix allowed: %s", str
);
6391 arg
.s
= last
? g_strdup(last
) : g_strdup("");
6392 if (cmd
->type
& XT_INTARG
&& last
&& strlen(last
) > 0) {
6393 if (arg
.s
== NULL
) {
6394 show_oops(t
, "Invalid command");
6397 arg
.precount
= atoi(arg
.s
);
6398 if (arg
.precount
<= 0) {
6399 if (arg
.s
[0] == '0')
6400 show_oops(t
, "Zero count");
6402 show_oops(t
, "Trailing characters");
6407 DNPRINTF(XT_D_CMD
, "%s: prefix %d arg %s\n",
6408 __func__
, arg
.precount
, arg
.s
);
6424 save_runtime_setting(const char *name
, const char *val
)
6430 char file
[PATH_MAX
];
6431 char delim
[3] = { '\0', '\0', '\0' };
6432 char *line
, *lt
, *start
;
6433 char *contents
, *tmp
;
6435 if (runtime_settings
== NULL
|| strlen(runtime_settings
) == 0)
6438 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, runtime_settings
);
6439 if (stat(file
, &sb
) || (f
= fopen(file
, "r+")) == NULL
)
6441 lt
= g_strdup_printf("%s=%s", name
, val
);
6442 contents
= g_strdup("");
6444 line
= fparseln(f
, &linelen
, NULL
, delim
, 0);
6445 if (line
== NULL
|| linelen
== 0)
6448 start
= g_strdup_printf("%s=", name
);
6449 if (strstr(line
, start
) == NULL
)
6450 contents
= g_strdup_printf("%s%s\n", contents
, line
);
6453 contents
= g_strdup_printf("%s%s\n", contents
, lt
);
6462 contents
= g_strdup_printf("%s%s\n", contents
, lt
);
6465 if ((f
= freopen(file
, "w", f
)) == NULL
)
6478 entry_focus_cb(GtkWidget
*w
, GdkEvent e
, struct tab
*t
)
6481 * This sometimes gets randomly unset for whatever reason in GTK3,
6482 * causing a GtkEntry's text cursor becomes invisible. When we focus
6483 * a GtkEntry, be sure to manually reset the main window's is-active
6484 * property so the cursor is shown correctly.
6486 #if GTK_CHECK_VERSION(3, 0, 0)
6487 fake_focus_in(main_window
);
6489 return (XT_CB_PASSTHROUGH
);
6493 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
6496 show_oops(NULL
, "entry_key_cb invalid parameters");
6497 return (XT_CB_PASSTHROUGH
);
6500 DNPRINTF(XT_D_CMD
, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
6501 e
->keyval
, e
->state
, t
->tab_id
);
6505 if (e
->keyval
== GDK_Escape
) {
6506 /* don't use focus_webview(t) because we want to type :cmds */
6507 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6510 return (handle_keypress(t
, e
, 1));
6513 struct command_entry
*
6514 history_prev(struct command_list
*l
, struct command_entry
*at
)
6517 at
= TAILQ_LAST(l
, command_list
);
6519 at
= TAILQ_PREV(at
, command_list
, entry
);
6521 at
= TAILQ_LAST(l
, command_list
);
6527 struct command_entry
*
6528 history_next(struct command_list
*l
, struct command_entry
*at
)
6531 at
= TAILQ_FIRST(l
);
6533 at
= TAILQ_NEXT(at
, entry
);
6535 at
= TAILQ_FIRST(l
);
6542 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
6544 int rv
= XT_CB_HANDLED
;
6545 const gchar
*c
= gtk_entry_get_text(w
);
6549 show_oops(NULL
, "cmd_keypress_cb parameters");
6550 return (XT_CB_PASSTHROUGH
);
6553 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
6554 e
->keyval
, e
->state
, t
->tab_id
);
6558 e
->keyval
= GDK_Escape
;
6559 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
6560 c
[0] == '.' || c
[0] == ','))
6561 e
->keyval
= GDK_Escape
;
6563 if (e
->keyval
!= GDK_Tab
&& e
->keyval
!= GDK_Shift_L
&&
6564 e
->keyval
!= GDK_ISO_Left_Tab
)
6565 cmd_status
.index
= -1;
6567 switch (e
->keyval
) {
6570 cmd_complete(t
, (char *)&c
[1], 1);
6571 else if (c
[0] == '.' || c
[0] == ',')
6572 run_script(t
, "hints.focusNextHint();");
6574 case GDK_ISO_Left_Tab
:
6576 cmd_complete(t
, (char *)&c
[1], -1);
6577 else if (c
[0] == '.' || c
[0] == ',')
6578 run_script(t
, "hints.focusPreviousHint();");
6582 if ((search_at
= history_next(&shl
, search_at
))) {
6583 search_at
->line
[0] = c
[0];
6584 gtk_entry_set_text(w
, search_at
->line
);
6585 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6587 } else if (c
[0] == '/') {
6588 if ((search_at
= history_prev(&shl
, search_at
))) {
6589 search_at
->line
[0] = c
[0];
6590 gtk_entry_set_text(w
, search_at
->line
);
6591 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6593 } else if (c
[0] == ':') {
6594 if ((history_at
= history_prev(&chl
, history_at
))) {
6595 history_at
->line
[0] = c
[0];
6596 gtk_entry_set_text(w
, history_at
->line
);
6597 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6603 if ((search_at
= history_next(&shl
, search_at
))) {
6604 search_at
->line
[0] = c
[0];
6605 gtk_entry_set_text(w
, search_at
->line
);
6606 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6608 } else if (c
[0] == '?') {
6609 if ((search_at
= history_prev(&shl
, search_at
))) {
6610 search_at
->line
[0] = c
[0];
6611 gtk_entry_set_text(w
, search_at
->line
);
6612 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6614 } if (c
[0] == ':') {
6615 if ((history_at
= history_next(&chl
, history_at
))) {
6616 history_at
->line
[0] = c
[0];
6617 gtk_entry_set_text(w
, history_at
->line
);
6618 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6623 if (!(!strcmp(c
, ":") || !strcmp(c
, "/") || !strcmp(c
, "?") ||
6624 !strcmp(c
, ".") || !strcmp(c
, ","))) {
6625 /* see if we are doing hinting and reset it */
6626 if (c
[0] == '.' || c
[0] == ',') {
6627 /* recreate hints */
6628 s
= g_strdup_printf("hints.createHints('', "
6629 "'%c');", c
[0] == '.' ? 'f' : 'F');
6641 if (c
!= NULL
&& (c
[0] == '/' || c
[0] == '?'))
6642 webkit_web_view_unmark_text_matches(t
->wv
);
6644 /* no need to cancel hints */
6648 rv
= XT_CB_PASSTHROUGH
;
6654 wv_popup_activ_cb(GtkMenuItem
*menu
, struct tab
*t
)
6656 GtkClipboard
*clipboard
, *primary
;
6657 const gchar
*name
, *uri
;
6658 #if !GTK_CHECK_VERSION(3, 4, 0)
6661 a
= gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu
));
6664 name
= gtk_action_get_name(a
);
6666 name
= gtk_actionable_get_action_name(GTK_ACTIONABLE(menu
));
6671 DNPRINTF(XT_D_CMD
, "wv_popup_activ_cb: tab %d action %s\n",
6675 * context-menu-action-3 copy link location
6676 * context-menu-action-7 copy image address
6677 * context-menu-action-2030 copy video link location
6681 if ((g_strcmp0(name
, "context-menu-action-3") == 0) ||
6682 (g_strcmp0(name
, "context-menu-action-7") == 0) ||
6683 (g_strcmp0(name
, "context-menu-action-2030") == 0)) {
6684 clipboard
= gtk_clipboard_get(GDK_SELECTION_CLIPBOARD
);
6685 primary
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
6686 uri
= gtk_clipboard_wait_for_text(clipboard
);
6689 gtk_clipboard_set_text(primary
, uri
, -1);
6694 wv_popup_cb(WebKitWebView
*wview
, GtkMenu
*menu
, struct tab
*t
)
6698 DNPRINTF(XT_D_CMD
, "wv_popup_cb: tab %d\n", t
->tab_id
);
6700 items
= gtk_container_get_children(GTK_CONTAINER(menu
));
6701 for (l
= items
; l
; l
= l
->next
)
6702 g_signal_connect(l
->data
, "activate",
6703 G_CALLBACK(wv_popup_activ_cb
), t
);
6708 wv_deny_location_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
6709 WebKitGeolocationPolicyDecision
*pd
, struct tab
*t
)
6711 webkit_geolocation_policy_deny(pd
);
6715 cmd_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
6717 /* popup menu enabled */
6722 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
6725 show_oops(NULL
, "cmd_focusout_cb invalid parameters");
6726 return (XT_CB_PASSTHROUGH
);
6729 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d popup %d\n",
6730 t
->tab_id
, t
->popup
);
6732 /* if popup is enabled don't lose focus */
6735 return (XT_CB_PASSTHROUGH
);
6742 return (XT_CB_PASSTHROUGH
);
6746 cmd_hide_cb(GtkWidget
*w
, struct tab
*t
)
6749 show_oops(NULL
, "%s: invalid parameters", __func__
);
6753 if (show_url
== 0 || t
->focus_wv
)
6756 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6760 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
6763 const gchar
*c
= gtk_entry_get_text(entry
);
6766 show_oops(NULL
, "cmd_activate_cb invalid parameters");
6770 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
6775 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
6776 c
[0] == '.' || c
[0] == ','))
6782 if (c
[0] == '/' || c
[0] == '?') {
6783 /* see if there is a timer pending */
6785 g_source_remove(t
->search_id
);
6790 if (t
->search_text
) {
6791 g_free(t
->search_text
);
6792 t
->search_text
= NULL
;
6795 t
->search_text
= g_strdup(s
);
6797 g_free(global_search
);
6798 global_search
= g_strdup(s
);
6799 t
->search_forward
= c
[0] == '/';
6801 history_add(&shl
, search_file
, s
, &search_history_count
);
6802 } else if (c
[0] == '.' || c
[0] == ',') {
6803 run_script(t
, "hints.fire();");
6804 /* XXX history for link following? */
6805 } else if (c
[0] == ':') {
6806 history_add(&chl
, command_file
, s
, &cmd_history_count
);
6807 /* can't call hide_cmd after cmd_execute */
6818 backward_cb(GtkWidget
*w
, struct tab
*t
)
6823 show_oops(NULL
, "backward_cb invalid parameters");
6827 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
6834 forward_cb(GtkWidget
*w
, struct tab
*t
)
6839 show_oops(NULL
, "forward_cb invalid parameters");
6843 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
6845 a
.i
= XT_NAV_FORWARD
;
6850 home_cb(GtkWidget
*w
, struct tab
*t
)
6853 show_oops(NULL
, "home_cb invalid parameters");
6857 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
6863 stop_cb(GtkWidget
*w
, struct tab
*t
)
6865 WebKitWebFrame
*frame
;
6868 show_oops(NULL
, "stop_cb invalid parameters");
6872 DNPRINTF(XT_D_NAV
, "stop_cb: tab %d\n", t
->tab_id
);
6874 frame
= webkit_web_view_get_main_frame(t
->wv
);
6875 if (frame
== NULL
) {
6876 show_oops(t
, "stop_cb: no frame");
6880 webkit_web_frame_stop_loading(frame
);
6881 abort_favicon_download(t
);
6885 setup_webkit(struct tab
*t
)
6887 if (is_g_object_setting(G_OBJECT(t
->settings
), "enable-dns-prefetching"))
6888 g_object_set(G_OBJECT(t
->settings
), "enable-dns-prefetching",
6889 FALSE
, (char *)NULL
);
6891 warnx("webkit does not have \"enable-dns-prefetching\" property");
6892 g_object_set(G_OBJECT(t
->settings
), "default-encoding", encoding
,
6894 g_object_set(G_OBJECT(t
->settings
),
6895 "enable-scripts", enable_scripts
, (char *)NULL
);
6896 g_object_set(G_OBJECT(t
->settings
),
6897 "enable-plugins", enable_plugins
, (char *)NULL
);
6898 g_object_set(G_OBJECT(t
->settings
),
6899 "javascript-can-open-windows-automatically",
6900 js_auto_open_windows
, (char *)NULL
);
6901 g_object_set(G_OBJECT(t
->settings
),
6902 "enable-html5-database", FALSE
, (char *)NULL
);
6903 g_object_set(G_OBJECT(t
->settings
),
6904 "enable-html5-local-storage", enable_localstorage
, (char *)NULL
);
6905 g_object_set(G_OBJECT(t
->settings
),
6906 "enable_spell_checking", enable_spell_checking
, (char *)NULL
);
6907 g_object_set(G_OBJECT(t
->settings
),
6908 "spell_checking_languages", spell_check_languages
, (char *)NULL
);
6909 g_object_set(G_OBJECT(t
->settings
),
6910 "enable-developer-extras", TRUE
, (char *)NULL
);
6911 g_object_set(G_OBJECT(t
->wv
),
6912 "full-content-zoom", TRUE
, (char *)NULL
);
6913 g_object_set(G_OBJECT(t
->settings
),
6914 "auto-load-images", auto_load_images
, (char *)NULL
);
6915 if (is_g_object_setting(G_OBJECT(t
->settings
),
6916 "enable-display-of-insecure-content"))
6917 g_object_set(G_OBJECT(t
->settings
),
6918 "enable-display-of-insecure-content",
6919 allow_insecure_content
, (char *)NULL
);
6920 if (is_g_object_setting(G_OBJECT(t
->settings
),
6921 "enable-running-of-insecure-content"))
6922 g_object_set(G_OBJECT(t
->settings
),
6923 "enable-running-of-insecure-content",
6924 allow_insecure_scripts
, (char *)NULL
);
6926 webkit_web_view_set_settings(t
->wv
, t
->settings
);
6930 update_statusbar_position(GtkAdjustment
* adjustment
, gpointer data
)
6932 struct tab
*ti
, *t
= NULL
;
6933 gdouble view_size
, value
, max
;
6936 TAILQ_FOREACH(ti
, &tabs
, entry
)
6937 if (ti
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
6945 if (adjustment
== NULL
)
6946 adjustment
= gtk_scrolled_window_get_vadjustment(
6947 GTK_SCROLLED_WINDOW(t
->browser_win
));
6949 view_size
= gtk_adjustment_get_page_size(adjustment
);
6950 value
= gtk_adjustment_get_value(adjustment
);
6951 max
= gtk_adjustment_get_upper(adjustment
) - view_size
;
6953 if (max
> -0.0001 && max
< 0.0001)
6954 position
= g_strdup("All");
6955 else if (value
> (max
- 0.0001) && value
< (max
+ 0.0001))
6956 position
= g_strdup("Bot");
6957 else if (value
> -0.0001 && value
< 0.0001)
6958 position
= g_strdup("Top");
6960 position
= g_strdup_printf("%d%%", (int) ((value
/ max
) * 100));
6962 if (t
->sbe
.position
!= NULL
)
6963 gtk_label_set_text(GTK_LABEL(t
->sbe
.position
), position
);
6970 create_window(const gchar
*name
)
6974 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
6975 if (window_maximize
)
6976 gtk_window_maximize(GTK_WINDOW(w
));
6978 gtk_window_set_default_size(GTK_WINDOW(w
), window_width
, window_height
);
6979 gtk_widget_set_name(w
, name
);
6980 gtk_window_set_wmclass(GTK_WINDOW(w
), name
, "Xombrero");
6986 create_browser(struct tab
*t
)
6989 GtkAdjustment
*adjustment
;
6992 show_oops(NULL
, "create_browser invalid parameters");
6996 w
= gtk_scrolled_window_new(NULL
, NULL
);
6997 gtk_widget_set_can_focus(w
, FALSE
);
6998 t
->adjust_h
= gtk_scrolled_window_get_hadjustment(
6999 GTK_SCROLLED_WINDOW(w
));
7000 t
->adjust_v
= gtk_scrolled_window_get_vadjustment(
7001 GTK_SCROLLED_WINDOW(w
));
7002 #if !GTK_CHECK_VERSION(3, 0, 0)
7003 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
7004 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
7008 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
7009 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
7012 t
->settings
= webkit_web_view_get_settings(t
->wv
);
7013 t
->stylesheet
= g_strdup(stylesheet
);
7014 t
->load_images
= auto_load_images
;
7017 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w
));
7018 g_signal_connect(G_OBJECT(adjustment
), "value-changed",
7019 G_CALLBACK(update_statusbar_position
), NULL
);
7028 create_kiosk_toolbar(struct tab
*t
)
7030 GtkWidget
*toolbar
= NULL
;
7033 #if GTK_CHECK_VERSION(3, 0, 0)
7034 toolbar
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 1);
7035 gtk_widget_set_name(toolbar
, "toolbar");
7037 toolbar
= gtk_hbox_new(FALSE
, 1);
7040 b
= GTK_BOX(toolbar
);
7041 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
7043 /* backward button */
7044 t
->backward
= create_button("Back", "go-previous", 0);
7045 gtk_widget_set_sensitive(t
->backward
, FALSE
);
7046 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
7047 G_CALLBACK(backward_cb
), t
);
7048 gtk_box_pack_start(b
, t
->backward
, TRUE
, TRUE
, 0);
7050 /* forward button */
7051 t
->forward
= create_button("Forward", "go-next", 0);
7052 gtk_widget_set_sensitive(t
->forward
, FALSE
);
7053 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
7054 G_CALLBACK(forward_cb
), t
);
7055 gtk_box_pack_start(b
, t
->forward
, TRUE
, TRUE
, 0);
7058 t
->gohome
= create_button("Home", "go-home", 0);
7059 gtk_widget_set_sensitive(t
->gohome
, true);
7060 g_signal_connect(G_OBJECT(t
->gohome
), "clicked",
7061 G_CALLBACK(home_cb
), t
);
7062 gtk_box_pack_start(b
, t
->gohome
, TRUE
, TRUE
, 0);
7065 * Create widgets but don't use them. This is just so we don't get
7066 * loads of errors when trying to do stuff with them. Sink the floating
7067 * reference here, and do a manual unreference when the tab is deleted.
7069 t
->uri_entry
= gtk_entry_new();
7070 g_object_ref_sink(G_OBJECT(t
->uri_entry
));
7071 g_signal_connect(G_OBJECT(t
->uri_entry
), "focus-in-event",
7072 G_CALLBACK(entry_focus_cb
), t
);
7073 t
->stop
= create_button("Stop", "process-stop", 0);
7074 g_object_ref_sink(G_OBJECT(t
->stop
));
7075 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
7076 "media-playback-start" : "media-playback-pause", 0);
7077 g_object_ref_sink(G_OBJECT(t
->stop
));
7079 #if !GTK_CHECK_VERSION(3, 0, 0)
7080 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
7087 create_toolbar(struct tab
*t
)
7089 GtkWidget
*toolbar
= NULL
;
7093 * t->stop, t->js_toggle, and t->uri_entry are shared by the kiosk
7094 * toolbar, but not shown or used. We still create them there so we can
7095 * avoid loads of warnings when trying to use them. Instead, we sink
7096 * the floating reference here, and do the final unreference when
7100 #if GTK_CHECK_VERSION(3, 0, 0)
7101 toolbar
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 1);
7102 gtk_widget_set_name(toolbar
, "toolbar");
7104 toolbar
= gtk_hbox_new(FALSE
, 1);
7107 b
= GTK_BOX(toolbar
);
7108 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
7110 /* backward button */
7111 t
->backward
= create_button("Back", "go-previous", 0);
7112 gtk_widget_set_sensitive(t
->backward
, FALSE
);
7113 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
7114 G_CALLBACK(backward_cb
), t
);
7115 gtk_box_pack_start(b
, t
->backward
, FALSE
, FALSE
, 0);
7117 /* forward button */
7118 t
->forward
= create_button("Forward", "go-next", 0);
7119 gtk_widget_set_sensitive(t
->forward
, FALSE
);
7120 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
7121 G_CALLBACK(forward_cb
), t
);
7122 gtk_box_pack_start(b
, t
->forward
, FALSE
, FALSE
, 0);
7125 t
->stop
= create_button("Stop", "process-stop", 0);
7126 g_object_ref_sink(G_OBJECT(t
->stop
));
7127 gtk_widget_set_sensitive(t
->stop
, FALSE
);
7128 g_signal_connect(G_OBJECT(t
->stop
), "clicked", G_CALLBACK(stop_cb
), t
);
7129 gtk_box_pack_start(b
, t
->stop
, FALSE
, FALSE
, 0);
7132 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
7133 "media-playback-start" : "media-playback-pause", 0);
7134 g_object_ref_sink(G_OBJECT(t
->js_toggle
));
7135 gtk_widget_set_sensitive(t
->js_toggle
, TRUE
);
7136 g_signal_connect(G_OBJECT(t
->js_toggle
), "clicked",
7137 G_CALLBACK(js_toggle_cb
), t
);
7138 gtk_box_pack_start(b
, t
->js_toggle
, FALSE
, FALSE
, 0);
7140 /* toggle proxy button */
7141 t
->proxy_toggle
= create_button("Proxy-Toggle", proxy_uri
?
7142 "gtk-connect" : "gtk-disconnect", 0);
7143 /* override icons */
7145 button_set_file(t
->proxy_toggle
, "torenabled.ico");
7147 button_set_file(t
->proxy_toggle
, "tordisabled.ico");
7148 gtk_widget_set_sensitive(t
->proxy_toggle
, TRUE
);
7149 g_signal_connect(G_OBJECT(t
->proxy_toggle
), "clicked",
7150 G_CALLBACK(proxy_toggle_cb
), t
);
7151 gtk_box_pack_start(b
, t
->proxy_toggle
, FALSE
, FALSE
, 0);
7153 t
->uri_entry
= gtk_entry_new();
7154 g_object_ref_sink(G_OBJECT(t
->uri_entry
));
7155 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
7156 G_CALLBACK(activate_uri_entry_cb
), t
);
7157 g_signal_connect(G_OBJECT(t
->uri_entry
), "key-press-event",
7158 G_CALLBACK(entry_key_cb
), t
);
7159 g_signal_connect(G_OBJECT(t
->uri_entry
), "focus-in-event",
7160 G_CALLBACK(entry_focus_cb
), t
);
7162 gtk_box_pack_start(b
, t
->uri_entry
, TRUE
, TRUE
, 0);
7165 t
->search_entry
= gtk_entry_new();
7166 gtk_entry_set_width_chars(GTK_ENTRY(t
->search_entry
), 30);
7167 g_signal_connect(G_OBJECT(t
->search_entry
), "activate",
7168 G_CALLBACK(activate_search_entry_cb
), t
);
7169 g_signal_connect(G_OBJECT(t
->search_entry
), "key-press-event",
7170 G_CALLBACK(entry_key_cb
), t
);
7171 g_signal_connect(G_OBJECT(t
->search_entry
), "focus-in-event",
7172 G_CALLBACK(entry_focus_cb
), t
);
7173 gtk_box_pack_start(b
, t
->search_entry
, FALSE
, FALSE
, 0);
7175 #if !GTK_CHECK_VERSION(3, 0, 0)
7176 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
7183 create_buffers(struct tab
*t
)
7185 GtkCellRenderer
*renderer
;
7188 view
= gtk_tree_view_new();
7190 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view
), FALSE
);
7192 renderer
= gtk_cell_renderer_text_new();
7193 gtk_tree_view_insert_column_with_attributes
7194 (GTK_TREE_VIEW(view
), -1, "Id", renderer
, "text", COL_ID
, (char *)NULL
);
7196 renderer
= gtk_cell_renderer_pixbuf_new();
7197 gtk_tree_view_insert_column_with_attributes
7198 (GTK_TREE_VIEW(view
), -1, "Favicon", renderer
, "pixbuf", COL_FAVICON
,
7201 renderer
= gtk_cell_renderer_text_new();
7202 gtk_tree_view_insert_column_with_attributes
7203 (GTK_TREE_VIEW(view
), -1, "Title", renderer
, "text", COL_TITLE
,
7206 gtk_tree_view_set_model
7207 (GTK_TREE_VIEW(view
), GTK_TREE_MODEL(buffers_store
));
7213 row_activated_cb(GtkTreeView
*view
, GtkTreePath
*path
,
7214 GtkTreeViewColumn
*col
, struct tab
*t
)
7219 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
7221 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
,
7224 (GTK_TREE_MODEL(buffers_store
), &iter
, COL_ID
, &id
, -1);
7225 set_current_tab(id
- 1);
7231 /* after tab reordering/creation/removal */
7237 TAILQ_FOREACH(t
, &tabs
, entry
)
7238 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
7242 update_statusbar_tabs(struct tab
*t
)
7244 int tab_id
, max_tab_id
;
7250 tab_id
= gtk_notebook_get_current_page(notebook
);
7252 max_tab_id
= gtk_notebook_get_n_pages(notebook
);
7253 snprintf(s
, sizeof s
, "%d/%d", tab_id
+ 1, max_tab_id
);
7256 t
= get_current_tab();
7258 if (t
!= NULL
&& t
->sbe
.tabs
!= NULL
)
7259 gtk_label_set_text(GTK_LABEL(t
->sbe
.tabs
), s
);
7262 /* after active tab change */
7264 recolor_compact_tabs(void)
7268 #if !GTK_CHECK_VERSION(3, 0, 0)
7269 GdkColor color_active
, color_inactive
;
7271 gdk_color_parse(XT_COLOR_CT_ACTIVE
, &color_active
);
7272 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color_inactive
);
7274 curid
= gtk_notebook_get_current_page(notebook
);
7276 TAILQ_FOREACH(t
, &tabs
, entry
) {
7277 #if GTK_CHECK_VERSION(3, 0, 0)
7278 if (t
->tab_id
== curid
)
7279 gtk_widget_set_name(t
->tab_elems
.label
, XT_CSS_ACTIVE
);
7281 gtk_widget_set_name(t
->tab_elems
.label
, "");
7283 if (t
->tab_id
== curid
)
7284 gtk_widget_modify_fg(t
->tab_elems
.label
,
7285 GTK_STATE_NORMAL
, &color_active
);
7287 gtk_widget_modify_fg(t
->tab_elems
.label
,
7288 GTK_STATE_NORMAL
, &color_inactive
);
7294 set_current_tab(int page_num
)
7296 buffercmd_abort(get_current_tab());
7297 gtk_notebook_set_current_page(notebook
, page_num
);
7298 recolor_compact_tabs();
7299 update_statusbar_tabs(NULL
);
7303 undo_close_tab_save(struct tab
*t
)
7307 struct undo
*u1
, *u2
;
7310 WebKitWebHistoryItem
*el
;
7312 if ((uri
= get_uri(t
)) == NULL
)
7315 u1
= g_malloc0(sizeof(struct undo
));
7316 u1
->uri
= g_strdup(uri
);
7318 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
7320 m
= webkit_web_back_forward_list_get_forward_length(t
->bfl
);
7321 n
= webkit_web_back_forward_list_get_back_length(t
->bfl
);
7324 /* forward history */
7325 items
= webkit_web_back_forward_list_get_forward_list_with_limit(t
->bfl
, m
);
7326 for (item
= g_list_first(items
); item
; item
= item
->next
) {
7327 u1
->history
= g_list_prepend(u1
->history
,
7328 webkit_web_history_item_copy(item
->data
));
7334 el
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
7335 u1
->history
= g_list_prepend(u1
->history
,
7336 webkit_web_history_item_copy(el
));
7340 items
= webkit_web_back_forward_list_get_back_list_with_limit(t
->bfl
, n
);
7341 for (item
= g_list_first(items
); item
; item
= item
->next
) {
7342 u1
->history
= g_list_prepend(u1
->history
,
7343 webkit_web_history_item_copy(item
->data
));
7347 TAILQ_INSERT_HEAD(&undos
, u1
, entry
);
7349 if (undo_count
> XT_MAX_UNDO_CLOSE_TAB
) {
7350 u2
= TAILQ_LAST(&undos
, undo_tailq
);
7351 TAILQ_REMOVE(&undos
, u2
, entry
);
7353 g_list_free(u2
->history
);
7362 delete_tab(struct tab
*t
)
7366 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
7371 TAILQ_REMOVE(&tabs
, t
, entry
);
7374 /* Halt all webkit activity. */
7375 abort_favicon_download(t
);
7376 webkit_web_view_stop_loading(t
->wv
);
7378 /* Save the tab, so we can undo the close. */
7379 undo_close_tab_save(t
);
7383 g_source_remove(t
->search_id
);
7387 g_free(t
->session_key
);
7390 bzero(&a
, sizeof a
);
7392 inspector_cmd(t
, &a
);
7394 if (browser_mode
== XT_BM_KIOSK
) {
7395 gtk_widget_destroy(t
->uri_entry
);
7396 gtk_widget_destroy(t
->stop
);
7397 gtk_widget_destroy(t
->js_toggle
);
7400 /* widgets not shown in the kiosk toolbar */
7401 gtk_widget_destroy(t
->stop
);
7402 g_object_unref(G_OBJECT(t
->stop
));
7403 gtk_widget_destroy(t
->js_toggle
);
7404 g_object_unref(G_OBJECT(t
->js_toggle
));
7405 gtk_widget_destroy(t
->uri_entry
);
7406 g_object_unref(G_OBJECT(t
->uri_entry
));
7408 g_object_unref(G_OBJECT(t
->completion
));
7410 g_object_unref(t
->item
);
7412 gtk_widget_destroy(t
->tab_elems
.eventbox
);
7413 gtk_widget_destroy(t
->vbox
);
7415 g_free(t
->stylesheet
);
7421 if (TAILQ_EMPTY(&tabs
)) {
7422 if (browser_mode
== XT_BM_KIOSK
)
7423 create_new_tab(home
, NULL
, 1, -1);
7425 create_new_tab(NULL
, NULL
, 1, -1);
7428 /* recreate session */
7429 if (session_autosave
) {
7430 bzero(&a
, sizeof a
);
7432 save_tabs(NULL
, &a
);
7436 recolor_compact_tabs();
7440 update_statusbar_zoom(struct tab
*t
)
7443 char s
[16] = { '\0' };
7445 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
7446 if ((zoom
<= 0.99 || zoom
>= 1.01))
7447 snprintf(s
, sizeof s
, "%d%%", (int)(zoom
* 100));
7448 if (t
->sbe
.zoom
!= NULL
)
7449 gtk_label_set_text(GTK_LABEL(t
->sbe
.zoom
), s
);
7453 setzoom_webkit(struct tab
*t
, int adjust
)
7455 #define XT_ZOOMPERCENT 0.04
7460 show_oops(NULL
, "setzoom_webkit invalid parameters");
7464 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
7465 if (adjust
== XT_ZOOM_IN
)
7466 zoom
+= XT_ZOOMPERCENT
;
7467 else if (adjust
== XT_ZOOM_OUT
)
7468 zoom
-= XT_ZOOMPERCENT
;
7469 else if (adjust
> 0)
7470 zoom
= default_zoom_level
+ adjust
/ 100.0 - 1.0;
7472 show_oops(t
, "setzoom_webkit invalid zoom value");
7476 if (zoom
< XT_ZOOMPERCENT
)
7477 zoom
= XT_ZOOMPERCENT
;
7478 g_object_set(G_OBJECT(t
->wv
), "zoom-level", zoom
, (char *)NULL
);
7479 update_statusbar_zoom(t
);
7483 tab_clicked_cb(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
7485 struct tab
*t
= (struct tab
*) data
;
7487 DNPRINTF(XT_D_TAB
, "tab_clicked_cb: tab: %d\n", t
->tab_id
);
7489 switch (event
->button
) {
7491 set_current_tab(t
->tab_id
);
7502 append_tab(struct tab
*t
)
7507 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
7508 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
7516 sbe
= gtk_label_new(NULL
);
7517 gtk_widget_set_can_focus(GTK_WIDGET(sbe
), FALSE
);
7518 modify_font(GTK_WIDGET(sbe
), statusbar_font
);
7523 statusbar_create(struct tab
*t
)
7525 GtkWidget
*box
; /* container for statusbar elems */
7529 #if !GTK_CHECK_VERSION(3, 0, 0)
7534 DPRINTF("%s: invalid parameters", __func__
);
7538 #if GTK_CHECK_VERSION(3, 0, 0)
7539 t
->statusbar
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7540 box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7541 gtk_widget_set_name(GTK_WIDGET(t
->statusbar
), "statusbar");
7543 t
->statusbar
= gtk_hbox_new(FALSE
, 0);
7544 box
= gtk_hbox_new(FALSE
, 0);
7546 t
->sbe
.ebox
= gtk_event_box_new();
7548 gtk_widget_set_can_focus(GTK_WIDGET(t
->statusbar
), FALSE
);
7549 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.ebox
), FALSE
);
7550 gtk_widget_set_can_focus(GTK_WIDGET(box
), FALSE
);
7552 gtk_box_set_spacing(GTK_BOX(box
), 10);
7553 gtk_box_pack_start(GTK_BOX(t
->statusbar
), t
->sbe
.ebox
, TRUE
, TRUE
, 0);
7554 gtk_container_add(GTK_CONTAINER(t
->sbe
.ebox
), box
);
7556 t
->sbe
.uri
= gtk_entry_new();
7557 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.uri
), FALSE
);
7558 modify_font(GTK_WIDGET(t
->sbe
.uri
), statusbar_font
);
7559 #if !GTK_CHECK_VERSION(3, 0, 0)
7560 gtk_entry_set_inner_border(GTK_ENTRY(t
->sbe
.uri
), NULL
);
7561 gtk_entry_set_has_frame(GTK_ENTRY(t
->sbe
.uri
), FALSE
);
7564 #if GTK_CHECK_VERSION(3, 0, 0)
7565 statusbar_modify_attr(t
, XT_CSS_NORMAL
);
7567 statusbar_modify_attr(t
, XT_COLOR_WHITE
, XT_COLOR_BLACK
);
7571 gtk_box_pack_start(b
, t
->sbe
.uri
, TRUE
, TRUE
, 0);
7573 for (p
= statusbar_elems
; *p
!= '\0'; p
++) {
7576 #if GTK_CHECK_VERSION(3, 0, 0)
7577 sep
= gtk_separator_new(GTK_ORIENTATION_VERTICAL
);
7579 sep
= gtk_vseparator_new();
7580 gdk_color_parse(XT_COLOR_SB_SEPARATOR
, &color
);
7581 gtk_widget_modify_bg(sep
, GTK_STATE_NORMAL
, &color
);
7583 gtk_box_pack_start(b
, sep
, FALSE
, FALSE
, 0);
7586 if (t
->sbe
.position
== NULL
) {
7587 t
->sbe
.position
= create_sbe();
7588 gtk_box_pack_start(b
, t
->sbe
.position
, FALSE
,
7593 if (t
->sbe
.buffercmd
== NULL
) {
7594 t
->sbe
.buffercmd
= create_sbe();
7595 gtk_box_pack_start(b
, t
->sbe
.buffercmd
, FALSE
,
7600 if (t
->sbe
.zoom
== NULL
) {
7601 t
->sbe
.zoom
= create_sbe();
7602 gtk_box_pack_start(b
, t
->sbe
.zoom
, FALSE
, FALSE
,
7607 if (t
->sbe
.tabs
== NULL
) {
7608 t
->sbe
.tabs
= create_sbe();
7609 gtk_box_pack_start(b
, t
->sbe
.tabs
, FALSE
, FALSE
,
7614 if (t
->sbe
.proxy
== NULL
) {
7615 t
->sbe
.proxy
= create_sbe();
7616 gtk_box_pack_start(b
, t
->sbe
.proxy
, FALSE
,
7620 GTK_LABEL(t
->sbe
.proxy
), "proxy");
7624 warnx("illegal flag \"%c\" in statusbar_elems\n", *p
);
7629 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar
, FALSE
, FALSE
, 0);
7635 create_new_tab(const char *title
, struct undo
*u
, int focus
, int position
)
7640 WebKitWebHistoryItem
*item
;
7643 #if !GTK_CHECK_VERSION(3, 0, 0)
7647 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
7649 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
7650 if (single_instance
) {
7652 "create_new_tab: new tab rejected\n");
7655 sv
[0] = start_argv
[0];
7656 sv
[1] = (char *)title
;
7657 sv
[2] = (char *)NULL
;
7658 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
,
7659 NULL
, NULL
, NULL
, NULL
))
7660 show_oops(NULL
, "%s: could not spawn process",
7665 t
= g_malloc0(sizeof *t
);
7667 if (title
== NULL
) {
7668 title
= "(untitled)";
7672 #if GTK_CHECK_VERSION(3, 0, 0)
7673 t
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
7674 b
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7675 gtk_widget_set_name(t
->vbox
, "vbox");
7677 t
->vbox
= gtk_vbox_new(FALSE
, 0);
7678 b
= gtk_hbox_new(FALSE
, 0);
7680 gtk_widget_set_can_focus(t
->vbox
, FALSE
);
7682 /* label + button for tab */
7684 gtk_widget_set_can_focus(t
->tab_content
, FALSE
);
7686 t
->user_agent_id
= 0;
7687 t
->http_accept_id
= 0;
7689 #if WEBKIT_CHECK_VERSION(1, 5, 0)
7693 #if GTK_CHECK_VERSION(2, 20, 0)
7694 t
->spinner
= gtk_spinner_new();
7696 t
->label
= gtk_label_new(title
);
7697 bb
= create_button("Close", "window-close", 1);
7698 gtk_label_set_max_width_chars(GTK_LABEL(t
->label
), 20);
7699 gtk_label_set_ellipsize(GTK_LABEL(t
->label
), PANGO_ELLIPSIZE_END
);
7700 gtk_label_set_line_wrap(GTK_LABEL(t
->label
), FALSE
);
7701 gtk_widget_set_size_request(t
->tab_content
, 130, 0);
7704 * this is a total hack and most likely breaks with other styles but
7705 * is necessary so the text doesn't bounce around when the spinner is
7708 #if GTK_CHECK_VERSION(3, 0, 0)
7709 gtk_widget_set_size_request(t
->label
, 95, 0);
7711 gtk_widget_set_size_request(t
->label
, 100, 0);
7714 gtk_box_pack_start(GTK_BOX(t
->tab_content
), bb
, FALSE
, FALSE
, 0);
7715 gtk_box_pack_start(GTK_BOX(t
->tab_content
), t
->label
, FALSE
, FALSE
, 0);
7716 #if GTK_CHECK_VERSION(2, 20, 0)
7717 gtk_box_pack_end(GTK_BOX(b
), t
->spinner
, FALSE
, FALSE
, 0);
7721 if (browser_mode
== XT_BM_KIOSK
) {
7722 t
->toolbar
= create_kiosk_toolbar(t
);
7723 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
7726 t
->toolbar
= create_toolbar(t
);
7727 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
7735 t
->browser_win
= create_browser(t
);
7736 set_scrollbar_visibility(t
, show_scrollbars
);
7737 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
7739 /* oops message for user feedback */
7740 t
->oops
= gtk_entry_new();
7741 gtk_entry_set_inner_border(GTK_ENTRY(t
->oops
), NULL
);
7742 gtk_entry_set_has_frame(GTK_ENTRY(t
->oops
), FALSE
);
7743 gtk_widget_set_can_focus(GTK_WIDGET(t
->oops
), FALSE
);
7744 #if GTK_CHECK_VERSION(3, 0, 0)
7745 gtk_widget_set_name(t
->oops
, XT_CSS_RED
);
7747 gdk_color_parse(XT_COLOR_RED
, &color
);
7748 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
7750 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
7751 modify_font(GTK_WIDGET(t
->oops
), oops_font
);
7754 t
->cmd
= gtk_entry_new();
7755 g_signal_connect(G_OBJECT(t
->cmd
), "focus-in-event",
7756 G_CALLBACK(entry_focus_cb
), t
);
7757 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
7758 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
7759 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
7760 modify_font(GTK_WIDGET(t
->cmd
), cmd_font
);
7763 statusbar_create(t
);
7766 t
->buffers
= create_buffers(t
);
7767 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->buffers
, FALSE
, FALSE
, 0);
7769 /* xtp meaning is normal by default */
7770 set_normal_tab_meaning(t
);
7772 /* set empty favicon */
7773 xt_icon_from_name(t
, "text-html");
7775 /* and show it all */
7776 gtk_widget_show_all(b
);
7777 gtk_widget_show_all(t
->vbox
);
7780 gtk_widget_hide(t
->backward
);
7781 gtk_widget_hide(t
->forward
);
7782 gtk_widget_hide(t
->stop
);
7783 gtk_widget_hide(t
->js_toggle
);
7785 if (!fancy_bar
|| (search_string
== NULL
|| strlen(search_string
) == 0))
7786 gtk_widget_hide(t
->search_entry
);
7787 if (http_proxy
== NULL
&& http_proxy_save
== NULL
)
7788 gtk_widget_hide(t
->proxy_toggle
);
7790 /* compact tab bar */
7791 t
->tab_elems
.label
= gtk_label_new(title
);
7792 t
->tab_elems
.favicon
= gtk_image_new();
7794 t
->tab_elems
.eventbox
= gtk_event_box_new();
7795 gtk_widget_set_name(t
->tab_elems
.eventbox
, "compact_tab");
7796 #if GTK_CHECK_VERSION(3, 0, 0)
7797 t
->tab_elems
.box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7799 gtk_label_set_ellipsize(GTK_LABEL(t
->tab_elems
.label
),
7800 PANGO_ELLIPSIZE_END
);
7801 gtk_widget_override_font(t
->tab_elems
.label
, tabbar_font
);
7802 gtk_widget_set_halign(t
->tab_elems
.label
, GTK_ALIGN_START
);
7803 gtk_widget_set_valign(t
->tab_elems
.label
, GTK_ALIGN_START
);
7805 t
->tab_elems
.box
= gtk_hbox_new(FALSE
, 0);
7807 gtk_label_set_width_chars(GTK_LABEL(t
->tab_elems
.label
), 1);
7808 gtk_misc_set_alignment(GTK_MISC(t
->tab_elems
.label
), 0.0, 0.0);
7809 gtk_misc_set_padding(GTK_MISC(t
->tab_elems
.label
), 4.0, 4.0);
7810 modify_font(GTK_WIDGET(t
->tab_elems
.label
), tabbar_font
);
7812 gdk_color_parse(XT_COLOR_CT_BACKGROUND
, &color
);
7813 gtk_widget_modify_bg(t
->tab_elems
.eventbox
, GTK_STATE_NORMAL
, &color
);
7814 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
7815 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
, &color
);
7818 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.favicon
, FALSE
,
7820 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.label
, TRUE
,
7822 gtk_container_add(GTK_CONTAINER(t
->tab_elems
.eventbox
),
7825 gtk_box_pack_start(GTK_BOX(tab_bar_box
), t
->tab_elems
.eventbox
, TRUE
,
7827 gtk_widget_show_all(t
->tab_elems
.eventbox
);
7829 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
7832 id
= position
>= 0 ? position
:
7833 gtk_notebook_get_current_page(notebook
) + 1;
7834 if (id
> gtk_notebook_get_n_pages(notebook
))
7837 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
7838 gtk_notebook_insert_page(notebook
, t
->vbox
, t
->tab_content
,
7840 gtk_box_reorder_child(GTK_BOX(tab_bar_box
),
7841 t
->tab_elems
.eventbox
, id
);
7846 #if GTK_CHECK_VERSION(2, 20, 0)
7847 /* turn spinner off if we are a new tab without uri */
7849 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
7850 gtk_widget_hide(t
->spinner
);
7853 /* make notebook tabs reorderable */
7854 gtk_notebook_set_tab_reorderable(notebook
, t
->vbox
, TRUE
);
7856 /* compact tabs clickable */
7857 g_signal_connect(G_OBJECT(t
->tab_elems
.eventbox
),
7858 "button_press_event", G_CALLBACK(tab_clicked_cb
), t
);
7860 g_object_connect(G_OBJECT(t
->cmd
),
7861 "signal::button-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
7862 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb
), t
,
7863 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
7864 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb
), t
,
7865 "signal::activate", G_CALLBACK(cmd_activate_cb
), t
,
7866 "signal::populate-popup", G_CALLBACK(cmd_popup_cb
), t
,
7867 "signal::hide", G_CALLBACK(cmd_hide_cb
), t
,
7870 /* reuse wv_button_cb to hide oops */
7871 g_object_connect(G_OBJECT(t
->oops
),
7872 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7875 g_signal_connect(t
->buffers
,
7876 "row-activated", G_CALLBACK(row_activated_cb
), t
);
7877 g_object_connect(G_OBJECT(t
->buffers
),
7878 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
, (char *)NULL
);
7880 g_object_connect(G_OBJECT(t
->wv
),
7881 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
,
7882 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb
), t
,
7883 "signal::download-requested", G_CALLBACK(webview_download_cb
), t
,
7884 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), t
,
7885 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7886 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7887 "signal::resource-request-starting", G_CALLBACK(webview_rrs_cb
), t
,
7888 "signal::create-web-view", G_CALLBACK(webview_cwv_cb
), t
,
7889 "signal::close-web-view", G_CALLBACK(webview_closewv_cb
), t
,
7890 "signal::event", G_CALLBACK(webview_event_cb
), t
,
7891 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb
), t
,
7892 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7893 "signal::button_release_event", G_CALLBACK(wv_release_button_cb
), t
,
7894 "signal::populate-popup", G_CALLBACK(wv_popup_cb
), t
,
7895 "signal::geolocation-policy-decision-requested", G_CALLBACK(wv_deny_location_cb
), t
,
7897 g_signal_connect(t
->wv
,
7898 "notify::load-status", G_CALLBACK(notify_load_status_cb
), t
);
7899 g_signal_connect(t
->wv
,
7900 "notify::title", G_CALLBACK(notify_title_cb
), t
);
7901 t
->progress_handle
= g_signal_connect(t
->wv
,
7902 "notify::progress", G_CALLBACK(webview_progress_changed_cb
), t
);
7904 /* hijack the unused keys as if we were the browser */
7905 //g_object_connect(G_OBJECT(t->toolbar),
7906 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
7909 g_signal_connect(G_OBJECT(bb
), "clicked", G_CALLBACK(tab_close_cb
), t
);
7912 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
7913 /* restore the tab's history */
7914 if (u
&& u
->history
) {
7918 webkit_web_back_forward_list_add_item(t
->bfl
, item
);
7919 items
= g_list_next(items
);
7922 item
= g_list_nth_data(u
->history
, u
->back
);
7924 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
7927 g_list_free(u
->history
);
7929 webkit_web_back_forward_list_clear(t
->bfl
);
7931 /* check and show url and statusbar */
7932 url_set_visibility();
7933 statusbar_set_visibility();
7936 set_current_tab(t
->tab_id
);
7937 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
7941 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
7945 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
7952 if (userstyle_global
)
7955 recolor_compact_tabs();
7956 setzoom_webkit(t
, XT_ZOOM_NORMAL
);
7961 notebook_switchpage_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7967 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
7969 if (gtk_notebook_get_current_page(notebook
) == -1)
7972 TAILQ_FOREACH(t
, &tabs
, entry
) {
7973 if (t
->tab_id
== pn
) {
7974 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
7977 uri
= get_title(t
, TRUE
);
7978 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
7985 /* can't use focus_webview here */
7986 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
7988 update_statusbar_tabs(t
);
7995 notebook_pagereordered_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7998 struct tab
*t
= NULL
, *tt
;
8002 TAILQ_FOREACH(tt
, &tabs
, entry
)
8003 if (tt
->tab_id
== pn
) {
8009 DNPRINTF(XT_D_TAB
, "page_reordered_cb: tab: %d\n", t
->tab_id
);
8011 gtk_box_reorder_child(GTK_BOX(tab_bar_box
), t
->tab_elems
.eventbox
,
8014 update_statusbar_tabs(t
);
8018 menuitem_response(struct tab
*t
)
8020 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
8024 destroy_menu(GtkMenuShell
*m
, void *notused
)
8026 gtk_widget_destroy(GTK_WIDGET(m
));
8027 g_object_unref(G_OBJECT(m
));
8028 return (XT_CB_PASSTHROUGH
);
8032 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
8034 GtkWidget
*menu
= NULL
, *menu_items
;
8035 GdkEventButton
*bevent
;
8036 struct tab
**stabs
= NULL
;
8040 if (event
->type
== GDK_BUTTON_PRESS
) {
8041 bevent
= (GdkEventButton
*) event
;
8042 menu
= gtk_menu_new();
8043 g_object_ref_sink(G_OBJECT(menu
));
8045 num_tabs
= sort_tabs_by_page_num(&stabs
);
8046 for (i
= 0; i
< num_tabs
; ++i
) {
8047 if (stabs
[i
] == NULL
)
8049 if ((uri
= get_uri(stabs
[i
])) == NULL
)
8050 /* XXX make sure there is something to print */
8051 /* XXX add gui pages in here to look purdy */
8053 menu_items
= gtk_menu_item_new_with_label(uri
);
8054 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), menu_items
);
8055 gtk_widget_show(menu_items
);
8057 g_signal_connect_swapped(menu_items
,
8058 "activate", G_CALLBACK(menuitem_response
),
8059 (gpointer
)stabs
[i
]);
8063 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
,
8064 bevent
->button
, bevent
->time
);
8066 g_object_connect(G_OBJECT(menu
),
8067 "signal::selection-done", G_CALLBACK(destroy_menu
), NULL
,
8070 return (TRUE
/* eat event */);
8073 return (FALSE
/* propagate */);
8077 icon_size_map(int iconsz
)
8079 if (iconsz
<= GTK_ICON_SIZE_INVALID
||
8080 iconsz
> GTK_ICON_SIZE_DIALOG
)
8081 return (GTK_ICON_SIZE_SMALL_TOOLBAR
);
8087 create_button(const char *name
, const char *icon_name
, int size
)
8089 GtkWidget
*button
, *image
;
8091 #if !GTK_CHECK_VERSION(3, 0, 0)
8095 #if !GTK_CHECK_VERSION(3, 0, 0)
8096 newstyle
= g_strdup_printf(
8097 "style \"%s-style\"\n"
8099 " GtkWidget::focus-padding = 0\n"
8100 " GtkWidget::focus-line-width = 0\n"
8104 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
8105 gtk_rc_parse_string(newstyle
);
8108 button
= gtk_button_new();
8109 gtk_widget_set_can_focus(button
, FALSE
);
8110 gtk_button_set_focus_on_click(GTK_BUTTON(button
), FALSE
);
8111 gtk_icon_size
= icon_size_map(size
? size
: icon_size
);
8113 image
= gtk_image_new_from_icon_name(icon_name
, gtk_icon_size
);
8114 gtk_container_set_border_width(GTK_CONTAINER(button
), 0);
8115 gtk_button_set_image(GTK_BUTTON(button
), GTK_WIDGET(image
));
8116 gtk_widget_set_name(button
, name
);
8117 gtk_button_set_relief(GTK_BUTTON(button
), GTK_RELIEF_NONE
);
8123 button_set_file(GtkWidget
*button
, char *filename
)
8126 char file
[PATH_MAX
];
8128 snprintf(file
, sizeof file
, "%s" PS
"%s", resource_dir
, filename
);
8129 image
= gtk_image_new_from_file(file
);
8130 gtk_button_set_image(GTK_BUTTON(button
), image
);
8134 button_set_icon_name(GtkWidget
*button
, char *icon_name
)
8136 GtkWidget
*image
= NULL
;
8138 image
= gtk_image_new_from_icon_name(icon_name
,
8139 icon_size_map(icon_size
));
8140 gtk_button_set_image(GTK_BUTTON(button
), image
);
8149 char file
[PATH_MAX
];
8151 #if !GTK_CHECK_VERSION(3, 0, 0)
8155 #if GTK_CHECK_VERSION(3, 0, 0)
8156 vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
8158 vbox
= gtk_vbox_new(FALSE
, 0);
8160 gtk_box_set_spacing(GTK_BOX(vbox
), 0);
8161 gtk_widget_set_can_focus(vbox
, FALSE
);
8162 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
8163 #if !GTK_CHECK_VERSION(3, 0, 0)
8164 /* XXX seems to be needed with gtk+2 */
8165 g_object_set(G_OBJECT(notebook
), "tab-border", 0, NULL
);
8167 gtk_notebook_set_scrollable(notebook
, TRUE
);
8168 gtk_notebook_set_show_border(notebook
, FALSE
);
8169 gtk_widget_set_can_focus(GTK_WIDGET(notebook
), FALSE
);
8171 abtn
= gtk_button_new();
8172 gtk_widget_set_can_focus(abtn
, FALSE
);
8173 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
8174 gtk_widget_set_name(abtn
, "Arrow");
8175 gtk_button_set_image(GTK_BUTTON(abtn
), arrow
);
8176 gtk_widget_set_size_request(abtn
, -1, 20);
8178 #if GTK_CHECK_VERSION(2, 20, 0)
8179 gtk_notebook_set_action_widget(notebook
, abtn
, GTK_PACK_END
);
8181 /* compact tab bar */
8182 tab_bar
= gtk_event_box_new();
8183 #if GTK_CHECK_VERSION(3, 0, 0)
8184 gtk_widget_set_name(tab_bar
, "tab_bar");
8185 tab_bar_box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
8187 gdk_color_parse(XT_COLOR_CT_SEPARATOR
, &color
);
8188 gtk_widget_modify_bg(tab_bar
, GTK_STATE_NORMAL
, &color
);
8189 tab_bar_box
= gtk_hbox_new(TRUE
, 0);
8191 gtk_container_add(GTK_CONTAINER(tab_bar
), tab_bar_box
);
8192 gtk_box_set_homogeneous(GTK_BOX(tab_bar_box
), TRUE
);
8193 gtk_box_set_spacing(GTK_BOX(tab_bar_box
), 2);
8195 gtk_box_pack_start(GTK_BOX(vbox
), tab_bar
, FALSE
, FALSE
, 0);
8196 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
8198 g_object_connect(G_OBJECT(notebook
),
8199 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb
), NULL
,
8201 g_object_connect(G_OBJECT(notebook
),
8202 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb
),
8203 NULL
, (char *)NULL
);
8204 g_signal_connect(G_OBJECT(abtn
), "button_press_event",
8205 G_CALLBACK(arrow_cb
), NULL
);
8207 main_window
= create_window("xombrero");
8208 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
8209 g_signal_connect(G_OBJECT(main_window
), "delete_event",
8210 G_CALLBACK(gtk_main_quit
), NULL
);
8211 #if GTK_CHECK_VERSION(3, 0, 0)
8212 gtk_window_set_has_resize_grip(GTK_WINDOW(main_window
), FALSE
);
8216 for (i
= 0; i
< LENGTH(icons
); i
++) {
8217 snprintf(file
, sizeof file
, "%s" PS
"%s", resource_dir
, icons
[i
]);
8218 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
8219 l
= g_list_append(l
, pb
);
8221 gtk_window_set_default_icon_list(l
);
8223 for (; l
; l
= l
->next
)
8224 g_object_unref(G_OBJECT(l
->data
));
8226 gtk_widget_show_all(abtn
);
8227 gtk_widget_show_all(main_window
);
8228 notebook_tab_set_visibility();
8231 #ifndef XT_SOCKET_DISABLE
8233 send_cmd_to_socket(char *cmd
)
8236 struct sockaddr_un sa
;
8238 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
8239 warnx("%s: socket", __func__
);
8243 sa
.sun_family
= AF_UNIX
;
8244 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
8245 work_dir
, XT_SOCKET_FILE
);
8248 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
8249 warnx("%s: connect", __func__
);
8253 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
8254 warnx("%s: send", __func__
);
8265 socket_watcher(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
8268 char str
[XT_MAX_URL_LENGTH
];
8269 socklen_t t
= sizeof(struct sockaddr_un
);
8270 struct sockaddr_un sa
;
8275 gint fd
= g_io_channel_unix_get_fd(source
);
8277 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
8282 if (getpeereid(s
, &uid
, &gid
) == -1) {
8286 if (uid
!= getuid() || gid
!= getgid()) {
8287 warnx("unauthorized user");
8293 warnx("not a valid user");
8297 n
= recv(s
, str
, sizeof(str
), 0);
8301 tt
= get_current_tab();
8302 cmd_execute(tt
, str
);
8310 struct sockaddr_un sa
;
8312 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
8313 warn("is_running: socket");
8317 sa
.sun_family
= AF_UNIX
;
8318 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
8319 work_dir
, XT_SOCKET_FILE
);
8322 /* connect to see if there is a listener */
8323 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
8324 rv
= 0; /* not running */
8326 rv
= 1; /* already running */
8337 struct sockaddr_un sa
;
8339 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
8340 warn("build_socket: socket");
8344 sa
.sun_family
= AF_UNIX
;
8345 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
8346 work_dir
, XT_SOCKET_FILE
);
8349 /* connect to see if there is a listener */
8350 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
8351 /* no listener so we will */
8352 unlink(sa
.sun_path
);
8354 if (bind(s
, (struct sockaddr
*)&sa
, len
) == -1) {
8355 warn("build_socket: bind");
8359 if (listen(s
, 1) == -1) {
8360 warn("build_socket: listen");
8378 if (stat(dir
, &sb
)) {
8379 #if defined __MINGW32__
8380 if (mkdir(dir
) == -1)
8382 if (mkdir(dir
, S_IRWXU
) == -1)
8384 err(1, "mkdir %s", dir
);
8386 err(1, "stat %s", dir
);
8388 if (S_ISDIR(sb
.st_mode
) == 0)
8389 errx(1, "%s not a dir", dir
);
8390 #if !defined __MINGW32__
8391 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
8392 warnx("fixing invalid permissions on %s", dir
);
8393 if (chmod(dir
, S_IRWXU
) == -1)
8394 err(1, "chmod %s", dir
);
8403 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
8407 #if GTK_CHECK_VERSION(3, 0, 0)
8411 GtkCssProvider
*provider
;
8412 GdkDisplay
*display
;
8415 char path
[PATH_MAX
];
8416 #if defined __MINGW32__
8417 GtkCssProvider
*windows_hacks
;
8420 provider
= gtk_css_provider_new();
8421 display
= gdk_display_get_default();
8422 screen
= gdk_display_get_default_screen(display
);
8423 snprintf(path
, sizeof path
, "%s" PS
"%s", resource_dir
, XT_CSS_FILE
);
8424 file
= g_file_new_for_path(path
);
8425 gtk_css_provider_load_from_file(provider
, file
, NULL
);
8426 gtk_style_context_add_provider_for_screen(screen
,
8427 GTK_STYLE_PROVIDER(provider
),
8428 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
8429 g_object_unref(G_OBJECT(provider
));
8430 #if defined __MINGW32__
8431 windows_hacks
= gtk_css_provider_new();
8432 gtk_css_provider_load_from_data(windows_hacks
,
8434 " border-width: 0px;\n"
8436 gtk_style_context_add_provider_for_screen(screen
,
8437 GTK_STYLE_PROVIDER(windows_hacks
),
8438 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
8439 g_object_unref(G_OBJECT(windows_hacks
));
8441 g_object_unref(G_OBJECT(file
));
8448 startpage_add("<b>Welcome to xombrero %s!</b><p>", version
);
8449 startpage_add("Details at "
8450 "<a href=https://opensource.conformal.com/wiki/xombrero>xombrero "
8451 "wiki page</a><p>");
8455 main(int argc
, char **argv
)
8458 int c
, optn
= 0, opte
= 0, focus
= 1;
8459 char conf
[PATH_MAX
] = { '\0' };
8460 char file
[PATH_MAX
];
8461 char sodversion
[32];
8462 char *env_proxy
= NULL
;
8467 start_argv
= (char * const *)argv
;
8473 gtk_init(&argc
, &argv
);
8475 gnutls_global_init();
8477 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
8480 RB_INIT(&downloads
);
8487 TAILQ_INIT(&sessions
);
8490 TAILQ_INIT(&aliases
);
8501 TAILQ_INIT(&force_https
);
8504 #ifndef XT_RESOURCE_LIMITS_DISABLE
8508 GIOChannel
*channel
;
8510 /* fiddle with ulimits */
8511 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
8514 /* just use them all */
8516 rlp
.rlim_cur
= OPEN_MAX
;
8518 rlp
.rlim_cur
= rlp
.rlim_max
;
8520 if (setrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
8522 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
8524 else if (rlp
.rlim_cur
< 1024)
8525 startpage_add("%s requires at least 1024 "
8526 "(2048 recommended) file " "descriptors, "
8527 "currently it has up to %d available",
8528 __progname
, rlp
.rlim_cur
);
8532 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
8541 #ifdef XOMBRERO_BUILDSTR
8542 errx(0 , "Version: %s Build: %s",
8543 version
, XOMBRERO_BUILDSTR
);
8545 errx(0 , "Version: %s", version
);
8549 strlcpy(conf
, optarg
, sizeof(conf
));
8552 strlcpy(named_session
, optarg
, sizeof(named_session
));
8573 pwd
= getpwuid(getuid());
8575 errx(1, "invalid user %d", getuid());
8577 /* set download dir */
8578 if (strlen(download_dir
) == 0)
8579 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
8581 /* compile buffer command regexes */
8584 /* set default dynamic string settings */
8585 home
= g_strdup(XT_DS_HOME
);
8586 search_string
= g_strdup(XT_DS_SEARCH_STRING
);
8587 strlcpy(runtime_settings
, "runtime", sizeof runtime_settings
);
8588 cmd_font_name
= g_strdup(XT_DS_CMD_FONT_NAME
);
8589 oops_font_name
= g_strdup(XT_DS_OOPS_FONT_NAME
);
8590 statusbar_font_name
= g_strdup(XT_DS_STATUSBAR_FONT_NAME
);
8591 tabbar_font_name
= g_strdup(XT_DS_TABBAR_FONT_NAME
);
8592 statusbar_elems
= g_strdup("BP");
8593 spell_check_languages
= g_strdup(XT_DS_SPELL_CHECK_LANGUAGES
);
8594 encoding
= g_strdup(XT_DS_ENCODING
);
8595 spell_check_languages
= g_strdup(XT_DS_SPELL_CHECK_LANGUAGES
);
8596 path
= g_strdup_printf("%s" PS
"style.css", resource_dir
);
8597 userstyle
= g_filename_to_uri(path
, NULL
, NULL
);
8599 stylesheet
= g_strdup(userstyle
);
8601 /* set statically allocated (struct special) settings */
8602 if (strlen(default_script
) == 0)
8603 expand_tilde(default_script
, sizeof default_script
,
8604 XT_DS_DEFAULT_SCRIPT
);
8605 if (strlen(ssl_ca_file
) == 0)
8606 expand_tilde(ssl_ca_file
, sizeof ssl_ca_file
,
8610 session
= webkit_get_default_session();
8612 /* read config file */
8613 if (strlen(conf
) == 0)
8614 snprintf(conf
, sizeof conf
, "%s" PS
".%s",
8615 pwd
->pw_dir
, XT_CONF_FILE
);
8616 config_parse(conf
, 0);
8618 /* read preloaded HSTS list */
8619 if (preload_strict_transport
) {
8620 snprintf(conf
, sizeof conf
, "%s" PS
"%s",
8621 resource_dir
, XT_HSTS_PRELOAD_FILE
);
8622 config_parse(conf
, 0);
8625 /* check whether to read in a crapton of additional http headers */
8626 if (anonymize_headers
) {
8627 snprintf(conf
, sizeof conf
, "%s" PS
"%s",
8628 resource_dir
, XT_USER_AGENT_FILE
);
8629 config_parse(conf
, 0);
8630 snprintf(conf
, sizeof conf
, "%s" PS
"%s",
8631 resource_dir
, XT_HTTP_ACCEPT_FILE
);
8632 config_parse(conf
, 0);
8636 cmd_font
= pango_font_description_from_string(cmd_font_name
);
8637 oops_font
= pango_font_description_from_string(oops_font_name
);
8638 statusbar_font
= pango_font_description_from_string(statusbar_font_name
);
8639 tabbar_font
= pango_font_description_from_string(tabbar_font_name
);
8641 /* working directory */
8642 if (strlen(work_dir
) == 0)
8643 snprintf(work_dir
, sizeof work_dir
, "%s" PS
"%s",
8644 pwd
->pw_dir
, XT_DIR
);
8647 /* icon cache dir */
8648 snprintf(cache_dir
, sizeof cache_dir
, "%s" PS
"%s", work_dir
, XT_CACHE_DIR
);
8652 snprintf(certs_dir
, sizeof certs_dir
, "%s" PS
"%s", work_dir
, XT_CERT_DIR
);
8655 /* cert changes dir */
8656 snprintf(certs_cache_dir
, sizeof certs_cache_dir
, "%s" PS
"%s",
8657 work_dir
, XT_CERT_CACHE_DIR
);
8658 xxx_dir(certs_cache_dir
);
8661 snprintf(sessions_dir
, sizeof sessions_dir
, "%s" PS
"%s",
8662 work_dir
, XT_SESSIONS_DIR
);
8663 xxx_dir(sessions_dir
);
8666 snprintf(js_dir
, sizeof js_dir
, "%s" PS
"%s", work_dir
, XT_JS_DIR
);
8670 snprintf(temp_dir
, sizeof temp_dir
, "%s" PS
"%s", work_dir
, XT_TEMP_DIR
);
8673 /* runtime settings that can override config file */
8674 if (runtime_settings
[0] != '\0')
8675 config_parse(runtime_settings
, 1);
8678 if (!strcmp(download_dir
, pwd
->pw_dir
))
8679 strlcat(download_dir
, PS
"downloads", sizeof download_dir
);
8680 xxx_dir(download_dir
);
8682 /* first start file */
8683 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_SOD_FILE
);
8684 if (stat(file
, &sb
)) {
8685 warnx("start of day file doesn't exist, creating it");
8686 if ((f
= fopen(file
, "w")) == NULL
)
8687 err(1, "startofday");
8688 if (fputs(version
, f
) == EOF
)
8695 if ((f
= fopen(file
, "r+")) == NULL
)
8696 err(1, "startofday");
8697 if (fgets(sodversion
, sizeof sodversion
, f
) == NULL
)
8699 sodversion
[strcspn(sodversion
, "\n")] = '\0';
8700 if (strcmp(version
, sodversion
)) {
8701 if ((f
= freopen(file
, "w", f
)) == NULL
)
8702 err(1, "startofday");
8703 if (fputs(version
, f
) == EOF
)
8706 /* upgrade, say something smart */
8712 /* favorites file */
8713 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_FAVS_FILE
);
8714 if (stat(file
, &sb
)) {
8715 warnx("favorites file doesn't exist, creating it");
8716 if ((f
= fopen(file
, "w")) == NULL
)
8717 err(1, "favorites");
8721 /* quickmarks file */
8722 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
8723 if (stat(file
, &sb
)) {
8724 warnx("quickmarks file doesn't exist, creating it");
8725 if ((f
= fopen(file
, "w")) == NULL
)
8726 err(1, "quickmarks");
8730 /* search history */
8731 if (history_autosave
) {
8732 snprintf(search_file
, sizeof search_file
, "%s" PS
"%s",
8733 work_dir
, XT_SEARCH_FILE
);
8734 if (stat(search_file
, &sb
)) {
8735 warnx("search history file doesn't exist, creating it");
8736 if ((f
= fopen(search_file
, "w")) == NULL
)
8737 err(1, "search_history");
8740 history_read(&shl
, search_file
, &search_history_count
);
8743 /* command history */
8744 if (history_autosave
) {
8745 snprintf(command_file
, sizeof command_file
, "%s" PS
"%s",
8746 work_dir
, XT_COMMAND_FILE
);
8747 if (stat(command_file
, &sb
)) {
8748 warnx("command history file doesn't exist, creating it");
8749 if ((f
= fopen(command_file
, "w")) == NULL
)
8750 err(1, "command_history");
8753 history_read(&chl
, command_file
, &cmd_history_count
);
8759 /* guess_search regex */
8760 if (url_regex
== NULL
)
8761 url_regex
= g_strdup(XT_URL_REGEX
);
8763 if (regcomp(&url_re
, url_regex
, REG_EXTENDED
| REG_NOSUB
))
8764 startpage_add("invalid url regex %s", url_regex
);
8767 env_proxy
= getenv("http_proxy");
8769 setup_proxy(env_proxy
);
8771 env_proxy
= getenv("HTTP_PROXY");
8773 setup_proxy(env_proxy
);
8775 setup_proxy(http_proxy
);
8778 /* the user can optionally have the proxy disabled at startup */
8779 if ((http_proxy_starts_enabled
== 0) && (http_proxy
!= NULL
)) {
8780 http_proxy_save
= g_strdup(http_proxy
);
8784 #ifndef XT_SOCKET_DISABLE
8786 send_cmd_to_socket(argv
[0]);
8790 opte
= opte
; /* shut mingw up */
8792 /* set some connection parameters */
8793 g_object_set(session
, "max-conns", max_connections
, (char *)NULL
);
8794 g_object_set(session
, "max-conns-per-host", max_host_connections
,
8796 g_object_set(session
, SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
8799 g_signal_connect(session
, "request-queued", G_CALLBACK(session_rq_cb
),
8802 #ifndef XT_SOCKET_DISABLE
8803 /* see if there is already a xombrero running */
8804 if (single_instance
&& is_running()) {
8806 warnx("already running");
8811 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
8812 send_cmd_to_socket(cmd
);
8822 optn
= optn
; /* shut mingw up */
8829 webkit_set_cache_model(WEBKIT_CACHE_MODEL_WEB_BROWSER
);
8831 webkit_set_cache_model(WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER
);
8833 if (enable_strict_transport
)
8834 strict_transport_init();
8836 /* uri completion */
8837 completion_model
= gtk_list_store_new(1, G_TYPE_STRING
);
8840 buffers_store
= gtk_list_store_new
8841 (NUM_COLS
, G_TYPE_UINT
, GDK_TYPE_PIXBUF
, G_TYPE_STRING
);
8847 notebook_tab_set_visibility();
8849 if (save_global_history
)
8850 restore_global_history();
8852 /* restore session list */
8853 restore_sessions_list();
8855 if (!strcmp(named_session
, XT_SAVED_TABS_FILE
))
8856 restore_saved_tabs();
8858 a
.s
= named_session
;
8859 a
.i
= XT_SES_DONOTHING
;
8860 open_tabs(NULL
, &a
);
8863 /* see if we have an exception */
8864 if (!TAILQ_EMPTY(&spl
)) {
8865 create_new_tab("about:startpage", NULL
, focus
, -1);
8870 create_new_tab(argv
[0], NULL
, focus
, -1);
8877 if (TAILQ_EMPTY(&tabs
))
8878 create_new_tab(home
, NULL
, 1, -1);
8879 #ifndef XT_SOCKET_DISABLE
8881 if ((s
= build_socket()) != -1) {
8882 channel
= g_io_channel_unix_new(s
);
8883 g_io_add_watch(channel
, G_IO_IN
, socket_watcher
, NULL
);
8887 #if GTK_CHECK_VERSION(3, 0, 0)
8893 gnutls_global_deinit();