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 Josh Rickmar <jrick@devio.us>
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 char *version
= XOMBRERO_VERSION
;
29 uint32_t swm_debug
= 0
52 GCRY_THREAD_OPTION_PTHREAD_IMPL
;
64 TAILQ_ENTRY(session
) entry
;
67 TAILQ_HEAD(session_list
, session
);
70 TAILQ_ENTRY(undo
) entry
;
73 int back
; /* Keeps track of how many back
74 * history items there are. */
76 TAILQ_HEAD(undo_tailq
, undo
);
78 struct command_entry
{
80 TAILQ_ENTRY(command_entry
) entry
;
82 TAILQ_HEAD(command_list
, command_entry
);
85 #define XT_CACHE_DIR ("cache")
86 #define XT_CERT_DIR ("certs")
87 #define XT_CERT_CACHE_DIR ("certs_cache")
88 #define XT_JS_DIR ("js")
89 #define XT_SESSIONS_DIR ("sessions")
90 #define XT_TEMP_DIR ("tmp")
91 #define XT_QMARKS_FILE ("quickmarks")
92 #define XT_SAVED_TABS_FILE ("main_session")
93 #define XT_RESTART_TABS_FILE ("restart_tabs")
94 #define XT_SOCKET_FILE ("socket")
95 #define XT_SAVE_SESSION_ID ("SESSION_NAME=")
96 #define XT_SEARCH_FILE ("search_history")
97 #define XT_COMMAND_FILE ("command_history")
98 #define XT_DLMAN_REFRESH "10"
99 #define XT_MAX_URL_LENGTH (4096) /* 1 page is atomic, don't make bigger */
100 #define XT_MAX_UNDO_CLOSE_TAB (32)
101 #define XT_PRINT_EXTRA_MARGIN 10
102 #define XT_URL_REGEX ("^[[:blank:]]*[^[:blank:]]*([[:alnum:]-]+\\.)+[[:alnum:]-][^[:blank:]]*[[:blank:]]*$")
103 #define XT_INVALID_MARK (-1) /* XXX this is a double, maybe use something else, like a nan */
106 #define XT_COLOR_RED "#cc0000"
107 #define XT_COLOR_YELLOW "#ffff66"
108 #define XT_COLOR_BLUE "lightblue"
109 #define XT_COLOR_GREEN "#99ff66"
110 #define XT_COLOR_WHITE "white"
111 #define XT_COLOR_BLACK "black"
113 #define XT_COLOR_CT_BACKGROUND "#000000"
114 #define XT_COLOR_CT_INACTIVE "#dddddd"
115 #define XT_COLOR_CT_ACTIVE "#bbbb00"
116 #define XT_COLOR_CT_SEPARATOR "#555555"
118 #define XT_COLOR_SB_SEPARATOR "#555555"
120 /* CSS element names */
121 #define XT_CSS_NORMAL ""
122 #define XT_CSS_RED "red"
123 #define XT_CSS_YELLOW "yellow"
124 #define XT_CSS_GREEN "green"
125 #define XT_CSS_BLUE "blue"
126 #define XT_CSS_HIDDEN "hidden"
127 #define XT_CSS_ACTIVE "active"
129 #define XT_PROTO_DELIM "://"
132 #define XT_MOVE_INVALID (0)
133 #define XT_MOVE_DOWN (1)
134 #define XT_MOVE_UP (2)
135 #define XT_MOVE_BOTTOM (3)
136 #define XT_MOVE_TOP (4)
137 #define XT_MOVE_PAGEDOWN (5)
138 #define XT_MOVE_PAGEUP (6)
139 #define XT_MOVE_HALFDOWN (7)
140 #define XT_MOVE_HALFUP (8)
141 #define XT_MOVE_LEFT (9)
142 #define XT_MOVE_FARLEFT (10)
143 #define XT_MOVE_RIGHT (11)
144 #define XT_MOVE_FARRIGHT (12)
145 #define XT_MOVE_PERCENT (13)
146 #define XT_MOVE_CENTER (14)
148 #define XT_QMARK_SET (0)
149 #define XT_QMARK_OPEN (1)
150 #define XT_QMARK_TAB (2)
152 #define XT_MARK_SET (0)
153 #define XT_MARK_GOTO (1)
155 #define XT_GO_UP_ROOT (999)
157 #define XT_NAV_INVALID (0)
158 #define XT_NAV_BACK (1)
159 #define XT_NAV_FORWARD (2)
160 #define XT_NAV_RELOAD (3)
161 #define XT_NAV_STOP (4)
163 #define XT_FOCUS_INVALID (0)
164 #define XT_FOCUS_URI (1)
165 #define XT_FOCUS_SEARCH (2)
167 #define XT_SEARCH_INVALID (0)
168 #define XT_SEARCH_NEXT (1)
169 #define XT_SEARCH_PREV (2)
171 #define XT_PASTE_CURRENT_TAB (0)
172 #define XT_PASTE_NEW_TAB (1)
174 #define XT_ZOOM_IN (-1)
175 #define XT_ZOOM_OUT (-2)
176 #define XT_ZOOM_NORMAL (100)
178 #define XT_SES_DONOTHING (0)
179 #define XT_SES_CLOSETABS (1)
181 #define XT_PREFIX (1<<0)
182 #define XT_USERARG (1<<1)
183 #define XT_URLARG (1<<2)
184 #define XT_INTARG (1<<3)
185 #define XT_SESSARG (1<<4)
186 #define XT_SETARG (1<<5)
188 #define XT_HINT_NEWTAB (1<<0)
190 #define XT_BUFCMD_SZ (8)
192 #define XT_EJS_SHOW (1<<0)
194 GtkWidget
* create_button(char *, char *, int);
196 void recalc_tabs(void);
197 void recolor_compact_tabs(void);
198 void set_current_tab(int page_num
);
199 gboolean
update_statusbar_position(GtkAdjustment
*, gpointer
);
200 void marks_clear(struct tab
*t
);
203 extern char *__progname
;
204 char * const *start_argv
;
206 GtkWidget
*main_window
;
207 GtkNotebook
*notebook
;
209 GtkWidget
*tab_bar_box
;
210 GtkWidget
*arrow
, *abtn
;
211 GdkEvent
*fevent
= NULL
;
212 struct tab_list tabs
;
213 struct history_list hl
;
214 int hl_purge_count
= 0;
215 struct session_list sessions
;
216 struct domain_list c_wl
;
217 struct domain_list js_wl
;
218 struct domain_list pl_wl
;
219 struct domain_list force_https
;
220 struct domain_list svil
;
221 struct strict_transport_tree st_tree
;
222 struct undo_tailq undos
;
223 struct keybinding_list kbl
;
225 struct user_agent_list ua_list
;
226 struct http_accept_list ha_list
;
227 struct cmd_alias_list cal
;
228 struct custom_uri_list cul
;
229 struct command_list chl
;
230 struct command_list shl
;
231 struct command_entry
*history_at
;
232 struct command_entry
*search_at
;
233 struct secviolation_list svl
;
234 struct set_reject_list srl
;
236 int cmd_history_count
= 0;
237 int search_history_count
= 0;
239 uint64_t blocked_cookies
= 0;
240 char named_session
[PATH_MAX
];
241 GtkListStore
*completion_model
;
242 GtkListStore
*buffers_store
;
245 char *qmarks
[XT_NOQMARKS
];
246 int btn_down
; /* M1 down in any wv */
247 regex_t url_re
; /* guess_search regex */
249 /* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
250 int next_download_id
= 1;
252 void xxx_dir(char *);
253 int icon_size_map(int);
254 void activate_uri_entry_cb(GtkWidget
*, struct tab
*);
257 history_delete(struct command_list
*l
, int *counter
)
259 struct command_entry
*c
;
261 if (l
== NULL
|| counter
== NULL
)
264 c
= TAILQ_LAST(l
, command_list
);
268 TAILQ_REMOVE(l
, c
, entry
);
275 history_add(struct command_list
*list
, char *file
, char *l
, int *counter
)
277 struct command_entry
*c
;
280 if (list
== NULL
|| l
== NULL
|| counter
== NULL
)
283 /* don't add the same line */
284 c
= TAILQ_FIRST(list
);
286 if (!strcmp(c
->line
+ 1 /* skip space */, l
))
289 c
= g_malloc0(sizeof *c
);
290 c
->line
= g_strdup_printf(" %s", l
);
293 TAILQ_INSERT_HEAD(list
, c
, entry
);
296 history_delete(list
, counter
);
298 if (history_autosave
&& file
) {
299 f
= fopen(file
, "w");
301 show_oops(NULL
, "couldn't write history %s", file
);
305 TAILQ_FOREACH_REVERSE(c
, list
, command_list
, entry
) {
307 fprintf(f
, "%s\n", c
->line
);
315 history_read(struct command_list
*list
, char *file
, int *counter
)
318 char *s
, line
[65536];
320 if (list
== NULL
|| file
== NULL
)
323 f
= fopen(file
, "r");
325 startpage_add("couldn't open history file %s", file
);
330 s
= fgets(line
, sizeof line
, f
);
331 if (s
== NULL
|| feof(f
) || ferror(f
))
333 if ((s
= strchr(line
, '\n')) == NULL
) {
334 startpage_add("invalid history file %s", file
);
340 history_add(list
, NULL
, line
+ 1, counter
);
348 /* marks array storage. */
352 if (i
< 0 || i
>= XT_NOMARKS
)
363 if ((ret
= strchr(XT_MARKS
, m
)) != NULL
)
364 return ret
- XT_MARKS
;
369 /* quickmarks array storage. */
373 if (i
< 0 || i
>= XT_NOQMARKS
)
384 if ((ret
= strchr(XT_QMARKS
, m
)) != NULL
)
385 return ret
- XT_QMARKS
;
391 is_g_object_setting(GObject
*o
, char *str
)
393 guint n_props
= 0, i
;
394 GParamSpec
**proplist
;
400 proplist
= g_object_class_list_properties(G_OBJECT_GET_CLASS(o
),
403 for (i
= 0; i
< n_props
; i
++) {
404 if (! strcmp(proplist
[i
]->name
, str
)) {
415 get_current_tab(void)
419 TAILQ_FOREACH(t
, &tabs
, entry
) {
420 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
424 warnx("%s: no current tab", __func__
);
430 set_ssl_ca_file(struct settings
*s
, char *file
)
434 if (file
== NULL
|| strlen(file
) == 0)
436 if (stat(file
, &sb
)) {
437 warnx("no CA file: %s", file
);
440 expand_tilde(ssl_ca_file
, sizeof ssl_ca_file
, file
);
441 g_object_set(session
,
442 SOUP_SESSION_SSL_CA_FILE
, ssl_ca_file
,
443 SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
449 set_status(struct tab
*t
, gchar
*fmt
, ...)
456 status
= g_strdup_vprintf(fmt
, ap
);
458 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.uri
), status
);
462 else if (strcmp(t
->status
, status
)) {
473 hide_cmd(struct tab
*t
)
475 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
477 history_at
= NULL
; /* just in case */
478 search_at
= NULL
; /* just in case */
479 gtk_widget_set_can_focus(t
->cmd
, FALSE
);
480 gtk_widget_hide(t
->cmd
);
484 show_cmd(struct tab
*t
)
486 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
488 /* without this you can't middle click in t->cmd to paste */
489 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), "");
493 gtk_widget_hide(t
->oops
);
494 gtk_widget_set_can_focus(t
->cmd
, TRUE
);
495 gtk_widget_show(t
->cmd
);
499 hide_buffers(struct tab
*t
)
501 gtk_widget_hide(t
->buffers
);
502 gtk_widget_set_can_focus(t
->buffers
, FALSE
);
503 gtk_list_store_clear(buffers_store
);
514 sort_tabs_by_page_num(struct tab
***stabs
)
519 num_tabs
= gtk_notebook_get_n_pages(notebook
);
521 *stabs
= g_malloc0(num_tabs
* sizeof(struct tab
*));
523 TAILQ_FOREACH(t
, &tabs
, entry
)
524 (*stabs
)[gtk_notebook_page_num(notebook
, t
->vbox
)] = t
;
530 buffers_make_list(void)
533 const gchar
*title
= NULL
;
535 struct tab
**stabs
= NULL
;
537 num_tabs
= sort_tabs_by_page_num(&stabs
);
539 for (i
= 0; i
< num_tabs
; i
++)
541 gtk_list_store_append(buffers_store
, &iter
);
542 title
= get_title(stabs
[i
], FALSE
);
543 gtk_list_store_set(buffers_store
, &iter
,
544 COL_ID
, i
+ 1, /* Enumerate the tabs starting from 1
546 COL_FAVICON
, gtk_image_get_pixbuf
547 (GTK_IMAGE(stabs
[i
]->tab_elems
.favicon
)),
556 show_buffers(struct tab
*t
)
559 GtkTreeSelection
*sel
;
563 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)))
568 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(t
->buffers
));
569 index
= gtk_notebook_get_current_page(notebook
);
570 path
= gtk_tree_path_new_from_indices(index
, -1);
571 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
, path
))
572 gtk_tree_selection_select_iter(sel
, &iter
);
573 gtk_tree_path_free(path
);
575 gtk_widget_show(t
->buffers
);
576 gtk_widget_set_can_focus(t
->buffers
, TRUE
);
577 gtk_widget_grab_focus(GTK_WIDGET(t
->buffers
));
581 toggle_buffers(struct tab
*t
)
583 if (gtk_widget_get_visible(t
->buffers
))
590 buffers(struct tab
*t
, struct karg
*args
)
598 set_scrollbar_visibility(struct tab
*t
, int visible
)
600 #if GTK_CHECK_VERSION(3, 0, 0)
601 GtkWidget
*h_scrollbar
, *v_scrollbar
;
603 h_scrollbar
= gtk_scrolled_window_get_hscrollbar(
604 GTK_SCROLLED_WINDOW(t
->browser_win
));
605 v_scrollbar
= gtk_scrolled_window_get_vscrollbar(
606 GTK_SCROLLED_WINDOW(t
->browser_win
));
609 gtk_widget_set_name(h_scrollbar
, XT_CSS_HIDDEN
);
610 gtk_widget_set_name(v_scrollbar
, XT_CSS_HIDDEN
);
612 gtk_widget_set_name(h_scrollbar
, "");
613 gtk_widget_set_name(v_scrollbar
, "");
618 return (visible
== 0);
623 hide_oops(struct tab
*t
)
625 gtk_widget_hide(t
->oops
);
629 show_oops(struct tab
*at
, const char *fmt
, ...)
633 struct tab
*t
= NULL
;
639 if ((t
= get_current_tab()) == NULL
)
645 if ((msg
= g_strdup_vprintf(fmt
, ap
)) == NULL
)
646 errx(1, "show_oops failed");
649 gtk_entry_set_text(GTK_ENTRY(t
->oops
), msg
);
650 gtk_widget_hide(t
->cmd
);
651 gtk_widget_show(t
->oops
);
657 char work_dir
[PATH_MAX
];
658 char certs_dir
[PATH_MAX
];
659 char certs_cache_dir
[PATH_MAX
];
660 char js_dir
[PATH_MAX
];
661 char cache_dir
[PATH_MAX
];
662 char sessions_dir
[PATH_MAX
];
663 char temp_dir
[PATH_MAX
];
664 char cookie_file
[PATH_MAX
];
665 char *strict_transport_file
= NULL
;
666 SoupSession
*session
;
667 SoupCookieJar
*s_cookiejar
;
668 SoupCookieJar
*p_cookiejar
;
669 char rc_fname
[PATH_MAX
];
671 struct mime_type_list mtl
;
672 struct alias_list aliases
;
675 struct tab
*create_new_tab(char *, struct undo
*, int, int);
676 void delete_tab(struct tab
*);
677 void setzoom_webkit(struct tab
*, int);
678 int download_rb_cmp(struct download
*, struct download
*);
679 gboolean
cmd_execute(struct tab
*t
, char *str
);
682 history_rb_cmp(struct history
*h1
, struct history
*h2
)
684 return (strcmp(h1
->uri
, h2
->uri
));
686 RB_GENERATE(history_list
, history
, entry
, history_rb_cmp
);
689 domain_rb_cmp(struct domain
*d1
, struct domain
*d2
)
691 return (strcmp(d1
->d
, d2
->d
));
693 RB_GENERATE(domain_list
, domain
, entry
, domain_rb_cmp
);
696 download_rb_cmp(struct download
*e1
, struct download
*e2
)
698 return (e1
->id
< e2
->id
? -1 : e1
->id
> e2
->id
);
700 RB_GENERATE(download_list
, download
, entry
, download_rb_cmp
);
703 secviolation_rb_cmp(struct secviolation
*s1
, struct secviolation
*s2
)
705 return (s1
->xtp_arg
< s2
->xtp_arg
? -1 : s1
->xtp_arg
> s2
->xtp_arg
);
707 RB_GENERATE(secviolation_list
, secviolation
, entry
, secviolation_rb_cmp
);
710 user_agent_rb_cmp(struct user_agent
*ua1
, struct user_agent
*ua2
)
712 return (ua1
->id
< ua2
->id
? -1 : ua1
->id
> ua2
->id
);
714 RB_GENERATE(user_agent_list
, user_agent
, entry
, user_agent_rb_cmp
);
717 http_accept_rb_cmp(struct http_accept
*ha1
, struct http_accept
*ha2
)
719 return (ha1
->id
< ha2
->id
? -1 : ha1
->id
> ha2
->id
);
721 RB_GENERATE(http_accept_list
, http_accept
, entry
, http_accept_rb_cmp
);
723 struct valid_url_types
{
735 valid_url_type(char *url
)
739 for (i
= 0; i
< LENGTH(vut
); i
++)
740 if (!strncasecmp(vut
[i
].type
, url
, strlen(vut
[i
].type
)))
747 match_alias(char *url_in
)
751 char *url_out
= NULL
, *search
, *enc_arg
;
754 search
= g_strdup(url_in
);
756 if (strsep(&arg
, " \t") == NULL
) {
757 show_oops(NULL
, "match_alias: NULL URL");
761 TAILQ_FOREACH(a
, &aliases
, entry
) {
762 if (!strcmp(search
, a
->a_name
))
767 DNPRINTF(XT_D_URL
, "match_alias: matched alias %s\n",
769 enc_arg
= soup_uri_encode(arg
, XT_RESERVED_CHARS
);
770 sv
= g_strsplit(a
->a_uri
, "%s", 2);
772 url_out
= g_strjoinv(enc_arg
, sv
);
774 url_out
= g_strjoinv("", sv
);
784 guess_url_type(char *url_in
)
787 char cwd
[PATH_MAX
] = {0};
788 char *url_out
= NULL
, *enc_search
= NULL
;
794 /* substitute aliases */
795 url_out
= match_alias(url_in
);
799 /* see if we are an about page */
800 if (!strncmp(url_in
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
))
801 for (i
= 0; i
< about_list_size(); i
++)
802 if (!strcmp(&url_in
[XT_URI_ABOUT_LEN
],
803 about_list
[i
].name
)) {
804 url_out
= g_strdup(url_in
);
808 if (guess_search
&& url_regex
&&
809 !(g_str_has_prefix(url_in
, "http://") ||
810 g_str_has_prefix(url_in
, "https://"))) {
811 if (regexec(&url_re
, url_in
, 0, NULL
, 0)) {
812 /* invalid URI so search instead */
813 enc_search
= soup_uri_encode(url_in
, XT_RESERVED_CHARS
);
814 sv
= g_strsplit(search_string
, "%s", 2);
815 url_out
= g_strjoinv(enc_search
, sv
);
822 /* XXX not sure about this heuristic */
823 if (stat(url_in
, &sb
) == 0) {
824 if (url_in
[0] == '/')
825 url_out
= g_filename_to_uri(url_in
, NULL
, NULL
);
827 if (getcwd(cwd
, PATH_MAX
) != NULL
) {
828 path
= g_strdup_printf("%s" PS
"%s", cwd
,
830 url_out
= g_filename_to_uri(path
, NULL
, NULL
);
835 url_out
= g_strdup_printf("http://%s", url_in
); /* guess http */
837 DNPRINTF(XT_D_URL
, "guess_url_type: guessed %s\n", url_out
);
843 set_normal_tab_meaning(struct tab
*t
)
848 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
849 if (t
->session_key
!= NULL
) {
850 g_free(t
->session_key
);
851 t
->session_key
= NULL
;
856 load_uri(struct tab
*t
, gchar
*uri
)
859 gchar
*newuri
= NULL
;
865 /* Strip leading spaces. */
866 while (*uri
&& isspace(*uri
))
869 if (strlen(uri
) == 0) {
874 set_normal_tab_meaning(t
);
876 if (valid_url_type(uri
)) {
877 newuri
= guess_url_type(uri
);
881 if (!strncmp(uri
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
)) {
882 for (i
= 0; i
< about_list_size(); i
++)
883 if (!strcmp(&uri
[XT_URI_ABOUT_LEN
], about_list
[i
].name
) &&
884 about_list
[i
].func
!= NULL
) {
885 bzero(&args
, sizeof args
);
886 about_list
[i
].func(t
, &args
);
887 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
),
891 show_oops(t
, "invalid about page");
895 set_status(t
, "Loading: %s", (char *)uri
);
897 webkit_web_view_load_uri(t
->wv
, uri
);
904 get_uri(struct tab
*t
)
906 const gchar
*uri
= NULL
;
908 if (webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
&&
909 !t
->download_requested
)
911 if (t
->xtp_meaning
== XT_XTP_TAB_MEANING_NORMAL
) {
912 uri
= webkit_web_view_get_uri(t
->wv
);
914 /* use tmp_uri to make sure it is g_freed */
917 t
->tmp_uri
= g_strdup_printf("%s%s", XT_URI_ABOUT
,
918 about_list
[t
->xtp_meaning
].name
);
925 get_title(struct tab
*t
, bool window
)
927 const gchar
*set
= NULL
, *title
= NULL
;
928 WebKitLoadStatus status
= webkit_web_view_get_load_status(t
->wv
);
930 if (status
== WEBKIT_LOAD_PROVISIONAL
||
931 (status
== WEBKIT_LOAD_FAILED
&& !t
->download_requested
) ||
932 t
->xtp_meaning
== XT_XTP_TAB_MEANING_BL
)
935 title
= webkit_web_view_get_title(t
->wv
);
936 if ((set
= title
? title
: get_uri(t
)))
940 set
= window
? XT_NAME
: "(untitled)";
946 find_mime_type(char *mime_type
)
948 struct mime_type
*m
, *def
= NULL
, *rv
= NULL
;
950 TAILQ_FOREACH(m
, &mtl
, entry
) {
952 !strncmp(mime_type
, m
->mt_type
, strlen(m
->mt_type
)))
955 if (m
->mt_default
== 0 && !strcmp(mime_type
, m
->mt_type
)) {
968 * This only escapes the & and < characters, as per the discussion found here:
969 * http://lists.apple.com/archives/Webkitsdk-dev/2007/May/msg00056.html
972 html_escape(const char *val
)
980 sv
= g_strsplit(val
, "&", -1);
981 s
= g_strjoinv("&", sv
);
984 sv
= g_strsplit(val
, "<", -1);
985 s
= g_strjoinv("<", sv
);
992 wl_find_uri(const gchar
*s
, struct domain_list
*wl
)
998 if (s
== NULL
|| wl
== NULL
)
1001 if (!strncmp(s
, "http://", strlen("http://")))
1002 s
= &s
[strlen("http://")];
1003 else if (!strncmp(s
, "https://", strlen("https://")))
1004 s
= &s
[strlen("https://")];
1009 for (i
= 0; i
< strlen(s
) + 1 /* yes er need this */; i
++)
1010 /* chop string at first slash */
1011 if (s
[i
] == '/' || s
[i
] == ':' || s
[i
] == '\0') {
1014 r
= wl_find(ss
, wl
);
1023 js_ref_to_string(JSContextRef context
, JSValueRef ref
)
1029 jsref
= JSValueToStringCopy(context
, ref
, NULL
);
1033 l
= JSStringGetMaximumUTF8CStringSize(jsref
);
1036 JSStringGetUTF8CString(jsref
, s
, l
);
1037 JSStringRelease(jsref
);
1042 #define XT_JS_DONE ("done;")
1043 #define XT_JS_DONE_LEN (strlen(XT_JS_DONE))
1044 #define XT_JS_INSERT ("insert;")
1045 #define XT_JS_INSERT_LEN (strlen(XT_JS_INSERT))
1048 run_script(struct tab
*t
, char *s
)
1050 JSGlobalContextRef ctx
;
1051 WebKitWebFrame
*frame
;
1053 JSValueRef val
, exception
;
1056 DNPRINTF(XT_D_JS
, "%s: tab %d %s\n", __func__
,
1057 t
->tab_id
, s
== (char *)JS_HINTING
? "JS_HINTING" : s
);
1059 frame
= webkit_web_view_get_main_frame(t
->wv
);
1060 ctx
= webkit_web_frame_get_global_context(frame
);
1062 str
= JSStringCreateWithUTF8CString(s
);
1063 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
1064 NULL
, 0, &exception
);
1065 JSStringRelease(str
);
1067 DNPRINTF(XT_D_JS
, "%s: val %p\n", __func__
, val
);
1069 es
= js_ref_to_string(ctx
, exception
);
1071 DNPRINTF(XT_D_JS
, "%s: exception %s\n", __func__
, es
);
1076 es
= js_ref_to_string(ctx
, val
);
1079 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
1081 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
1085 DNPRINTF(XT_D_JS
, "%s: val %s\n", __func__
, es
);
1094 run_script_locked(struct tab
*t
, char *s
)
1098 gdk_threads_enter();
1100 rv
= run_script(t
, s
);
1103 gdk_threads_leave();
1109 enable_hints(struct tab
*t
)
1111 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
1114 run_script(t
, "hints.createHints('', 'F');");
1116 run_script(t
, "hints.createHints('', 'f');");
1117 t
->mode
= XT_MODE_HINT
;
1121 disable_hints(struct tab
*t
)
1123 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
1125 run_script(t
, "hints.clearHints();");
1126 t
->mode
= XT_MODE_COMMAND
;
1131 passthrough(struct tab
*t
, struct karg
*args
)
1133 t
->mode
= XT_MODE_PASSTHROUGH
;
1138 modurl(struct tab
*t
, struct karg
*args
)
1140 const gchar
*uri
= NULL
;
1143 /* XXX kind of a bad hack, but oh well */
1144 if (gtk_widget_has_focus(t
->uri_entry
)) {
1145 if ((uri
= gtk_entry_get_text(GTK_ENTRY(t
->uri_entry
))) &&
1147 u
= g_strdup_printf("www.%s.com", uri
);
1148 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), u
);
1150 activate_uri_entry_cb(t
->uri_entry
, t
);
1157 hint(struct tab
*t
, struct karg
*args
)
1160 DNPRINTF(XT_D_JS
, "hint: tab %d args %d\n", t
->tab_id
, args
->i
);
1162 if (t
->mode
== XT_MODE_HINT
) {
1163 if (args
->i
== XT_HINT_NEWTAB
)
1173 apply_style(struct tab
*t
)
1176 g_object_set(G_OBJECT(t
->settings
),
1177 "user-stylesheet-uri", t
->stylesheet
, (char *)NULL
);
1181 remove_style(struct tab
*t
)
1184 g_object_set(G_OBJECT(t
->settings
),
1185 "user-stylesheet-uri", NULL
, (char *)NULL
);
1189 userstyle_cmd(struct tab
*t
, struct karg
*args
)
1191 char script
[PATH_MAX
] = {'\0'};
1195 DNPRINTF(XT_D_JS
, "userstyle_cmd: tab %d\n", t
->tab_id
);
1197 if (args
->s
!= NULL
&& strlen(args
->s
)) {
1198 expand_tilde(script
, sizeof script
, args
->s
);
1199 script_uri
= g_filename_to_uri(script
, NULL
, NULL
);
1201 script_uri
= g_strdup(userstyle
);
1203 if (script_uri
== NULL
)
1207 case XT_STYLE_CURRENT_TAB
:
1208 if (t
->styled
&& !strcmp(script_uri
, t
->stylesheet
))
1212 g_free(t
->stylesheet
);
1213 t
->stylesheet
= g_strdup(script_uri
);
1217 case XT_STYLE_GLOBAL
:
1218 if (userstyle_global
&& !strcmp(script_uri
, t
->stylesheet
)) {
1219 userstyle_global
= 0;
1220 TAILQ_FOREACH(tt
, &tabs
, entry
)
1223 userstyle_global
= 1;
1225 /* need to save this stylesheet for new tabs */
1228 stylesheet
= g_strdup(script_uri
);
1230 TAILQ_FOREACH(tt
, &tabs
, entry
) {
1232 g_free(tt
->stylesheet
);
1233 tt
->stylesheet
= g_strdup(script_uri
);
1246 quit(struct tab
*t
, struct karg
*args
)
1248 if (save_global_history
)
1249 save_global_history_to_disk(t
);
1257 restore_sessions_list(void)
1260 struct dirent
*dp
= NULL
;
1264 sdir
= opendir(sessions_dir
);
1266 while ((dp
= readdir(sdir
)) != NULL
) {
1267 #if defined __MINGW32__
1268 reg
= 1; /* windows only has regular files */
1270 reg
= dp
->d_type
== DT_REG
;
1273 s
= g_malloc(sizeof(struct session
));
1274 s
->name
= g_strdup(dp
->d_name
);
1275 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
1283 open_tabs(struct tab
*t
, struct karg
*a
)
1285 char file
[PATH_MAX
];
1289 struct tab
*ti
, *tt
;
1294 ti
= TAILQ_LAST(&tabs
, tab_list
);
1296 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, a
->s
);
1297 if ((f
= fopen(file
, "r")) == NULL
)
1301 if ((uri
= fparseln(f
, NULL
, NULL
, "\0\0\0", 0)) == NULL
)
1302 if (feof(f
) || ferror(f
))
1305 /* retrieve session name */
1306 if (uri
&& g_str_has_prefix(uri
, XT_SAVE_SESSION_ID
)) {
1307 strlcpy(named_session
,
1308 &uri
[strlen(XT_SAVE_SESSION_ID
)],
1309 sizeof named_session
);
1313 if (uri
&& strlen(uri
))
1314 create_new_tab(uri
, NULL
, 1, -1);
1320 /* close open tabs */
1321 if (a
->i
== XT_SES_CLOSETABS
&& ti
!= NULL
) {
1323 tt
= TAILQ_FIRST(&tabs
);
1344 restore_saved_tabs(void)
1346 char file
[PATH_MAX
];
1347 int unlink_file
= 0;
1352 snprintf(file
, sizeof file
, "%s" PS
"%s",
1353 sessions_dir
, XT_RESTART_TABS_FILE
);
1354 if (stat(file
, &sb
) == -1)
1355 a
.s
= XT_SAVED_TABS_FILE
;
1358 a
.s
= XT_RESTART_TABS_FILE
;
1361 a
.i
= XT_SES_DONOTHING
;
1362 rv
= open_tabs(NULL
, &a
);
1371 save_tabs(struct tab
*t
, struct karg
*a
)
1373 char file
[PATH_MAX
];
1375 int num_tabs
= 0, i
;
1376 struct tab
**stabs
= NULL
;
1378 /* tab may be null here */
1383 snprintf(file
, sizeof file
, "%s" PS
"%s",
1384 sessions_dir
, named_session
);
1386 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, a
->s
);
1388 if ((f
= fopen(file
, "w")) == NULL
) {
1389 show_oops(t
, "Can't open save_tabs file: %s", strerror(errno
));
1393 /* save session name */
1394 fprintf(f
, "%s%s\n", XT_SAVE_SESSION_ID
, named_session
);
1396 /* Save tabs, in the order they are arranged in the notebook. */
1397 num_tabs
= sort_tabs_by_page_num(&stabs
);
1399 for (i
= 0; i
< num_tabs
; i
++)
1401 if (get_uri(stabs
[i
]) != NULL
)
1402 fprintf(f
, "%s\n", get_uri(stabs
[i
]));
1403 else if (gtk_entry_get_text(GTK_ENTRY(
1404 stabs
[i
]->uri_entry
)))
1405 fprintf(f
, "%s\n", gtk_entry_get_text(GTK_ENTRY(
1406 stabs
[i
]->uri_entry
)));
1411 /* try and make sure this gets to disk NOW. XXX Backup first? */
1412 if (fflush(f
) != 0 || fsync(fileno(f
)) != 0) {
1413 show_oops(t
, "May not have managed to save session: %s",
1423 save_tabs_and_quit(struct tab
*t
, struct karg
*args
)
1435 expand_tilde(char *path
, size_t len
, const char *s
)
1439 char user
[LOGIN_NAME_MAX
];
1442 if (path
== NULL
|| s
== NULL
)
1443 errx(1, "expand_tilde");
1446 strlcpy(path
, sc
, len
);
1451 for (i
= 0; s
[i
] != PSC
&& s
[i
] != '\0'; ++i
)
1456 pwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1458 strlcpy(path
, sc
, len
);
1460 snprintf(path
, len
, "%s%s", pwd
->pw_dir
, s
);
1464 run_page_script(struct tab
*t
, struct karg
*args
)
1467 char *tmp
, script
[PATH_MAX
];
1470 tmp
= args
->s
!= NULL
&& strlen(args
->s
) > 0 ? args
->s
: default_script
;
1471 if (tmp
[0] == '\0') {
1472 show_oops(t
, "no script specified");
1476 if ((uri
= get_uri(t
)) == NULL
) {
1477 show_oops(t
, "tab is empty, not running script");
1481 expand_tilde(script
, sizeof script
, tmp
);
1484 sv
[1] = (char *)uri
;
1486 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
, NULL
,
1488 show_oops(t
, "%s: could not spawn process: %s %s", __func__
,
1492 show_oops(t
, "running: %s %s", sv
[0], sv
[1]);
1498 yank_uri(struct tab
*t
, struct karg
*args
)
1501 GtkClipboard
*clipboard
, *primary
;
1503 if ((uri
= get_uri(t
)) == NULL
)
1506 primary
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1507 clipboard
= gtk_clipboard_get(GDK_SELECTION_CLIPBOARD
);
1508 gtk_clipboard_set_text(primary
, uri
, -1);
1509 gtk_clipboard_set_text(clipboard
, uri
, -1);
1515 paste_uri(struct tab
*t
, struct karg
*args
)
1517 GtkClipboard
*clipboard
, *primary
;
1518 gchar
*c
= NULL
, *p
= NULL
, *uri
;
1521 clipboard
= gtk_clipboard_get(GDK_SELECTION_CLIPBOARD
);
1522 primary
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1523 c
= gtk_clipboard_wait_for_text(clipboard
);
1524 p
= gtk_clipboard_wait_for_text(primary
);
1528 /* Windows try clipboard first */
1531 /* UNIX try primary first */
1534 /* replace all newlines with spaces */
1535 for (i
= 0; uri
[i
] != '\0'; ++i
)
1539 while (*uri
&& isspace(*uri
))
1541 if (strlen(uri
) == 0) {
1542 show_oops(t
, "empty paste buffer");
1545 if (guess_search
== 0 && valid_url_type(uri
)) {
1546 /* we can be clever and paste this in search box */
1547 show_oops(t
, "not a valid URL");
1551 if (args
->i
== XT_PASTE_CURRENT_TAB
)
1553 else if (args
->i
== XT_PASTE_NEW_TAB
)
1554 create_new_tab(uri
, NULL
, 1, -1);
1567 js_toggle_cb(GtkWidget
*w
, struct tab
*t
)
1572 g_object_get(G_OBJECT(t
->settings
),
1573 "enable-scripts", &es
, (char *)NULL
);
1578 set
= XT_WL_DISABLE
;
1580 a
.i
= set
| XT_WL_TOPLEVEL
;
1583 a
.i
= set
| XT_WL_TOPLEVEL
;
1586 a
.i
= XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
;
1591 toggle_src(struct tab
*t
, struct karg
*args
)
1598 mode
= webkit_web_view_get_view_source_mode(t
->wv
);
1599 webkit_web_view_set_view_source_mode(t
->wv
, !mode
);
1600 webkit_web_view_reload(t
->wv
);
1606 focus_webview(struct tab
*t
)
1611 /* only grab focus if we are visible */
1612 if (gtk_notebook_get_current_page(notebook
) == t
->tab_id
)
1613 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
1617 focus(struct tab
*t
, struct karg
*args
)
1619 if (t
== NULL
|| args
== NULL
)
1625 if (args
->i
== XT_FOCUS_URI
)
1626 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
1627 else if (args
->i
== XT_FOCUS_SEARCH
)
1628 gtk_widget_grab_focus(GTK_WIDGET(t
->search_entry
));
1634 connect_socket_from_uri(const gchar
*uri
, const gchar
**error_str
, char *domain
,
1638 struct addrinfo hints
, *res
= NULL
, *ai
;
1639 int rv
= -1, s
= -1, on
, error
;
1641 static gchar myerror
[256]; /* this is not thread safe */
1644 *error_str
= myerror
;
1645 if (uri
&& !g_str_has_prefix(uri
, "https://")) {
1646 *error_str
= "invalid URI";
1650 su
= soup_uri_new(uri
);
1652 *error_str
= "invalid soup URI";
1655 if (!SOUP_URI_VALID_FOR_HTTP(su
)) {
1656 *error_str
= "invalid HTTPS URI";
1660 snprintf(port
, sizeof port
, "%d", su
->port
);
1661 bzero(&hints
, sizeof(struct addrinfo
));
1662 hints
.ai_flags
= AI_CANONNAME
;
1663 hints
.ai_family
= AF_UNSPEC
;
1664 hints
.ai_socktype
= SOCK_STREAM
;
1666 if ((error
= getaddrinfo(su
->host
, port
, &hints
, &res
))) {
1667 snprintf(myerror
, sizeof myerror
, "getaddrinfo failed: %s",
1668 gai_strerror(errno
));
1672 for (ai
= res
; ai
; ai
= ai
->ai_next
) {
1678 if (ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
)
1680 s
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
1683 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &on
,
1686 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) == 0)
1690 snprintf(myerror
, sizeof myerror
,
1691 "could not obtain certificates from: %s",
1697 strlcpy(domain
, su
->host
, domain_sz
);
1704 if (rv
== -1 && s
!= -1)
1712 custom_gnutls_push(void *s
, const void *buf
, size_t len
)
1714 return send((size_t)s
, buf
, len
, 0);
1718 custom_gnutls_pull(void *s
, void *buf
, size_t len
)
1720 return recv((size_t)s
, buf
, len
, 0);
1725 stop_tls(gnutls_session_t gsession
, gnutls_certificate_credentials_t xcred
)
1728 gnutls_deinit(gsession
);
1730 gnutls_certificate_free_credentials(xcred
);
1736 start_tls(const gchar
**error_str
, int s
, gnutls_session_t
*gs
,
1737 gnutls_certificate_credentials_t
*xc
)
1739 gnutls_certificate_credentials_t xcred
;
1740 gnutls_session_t gsession
;
1742 static gchar myerror
[1024]; /* this is not thread safe */
1744 if (gs
== NULL
|| xc
== NULL
)
1751 gnutls_certificate_allocate_credentials(&xcred
);
1752 gnutls_certificate_set_x509_trust_file(xcred
, ssl_ca_file
,
1753 GNUTLS_X509_FMT_PEM
);
1755 gnutls_init(&gsession
, GNUTLS_CLIENT
);
1756 gnutls_priority_set_direct(gsession
, "PERFORMANCE", NULL
);
1757 gnutls_credentials_set(gsession
, GNUTLS_CRD_CERTIFICATE
, xcred
);
1758 gnutls_transport_set_ptr(gsession
, (gnutls_transport_ptr_t
)(long)s
);
1760 /* sockets on windows don't use file descriptors */
1761 gnutls_transport_set_push_function(gsession
, custom_gnutls_push
);
1762 gnutls_transport_set_pull_function(gsession
, custom_gnutls_pull
);
1764 if ((rv
= gnutls_handshake(gsession
)) < 0) {
1765 snprintf(myerror
, sizeof myerror
,
1766 "gnutls_handshake failed %d fatal %d %s",
1768 gnutls_error_is_fatal(rv
),
1769 #if LIBGNUTLS_VERSION_MAJOR >= 2 && LIBGNUTLS_VERSION_MINOR >= 6
1770 gnutls_strerror_name(rv
));
1772 "GNUTLS version is too old to provide human readable error");
1774 stop_tls(gsession
, xcred
);
1778 gnutls_credentials_type_t cred
;
1779 cred
= gnutls_auth_get_type(gsession
);
1780 if (cred
!= GNUTLS_CRD_CERTIFICATE
) {
1781 snprintf(myerror
, sizeof myerror
,
1782 "gnutls_auth_get_type failed %d",
1784 stop_tls(gsession
, xcred
);
1792 *error_str
= myerror
;
1797 get_connection_certs(gnutls_session_t gsession
, gnutls_x509_crt_t
**certs
,
1801 const gnutls_datum_t
*cl
;
1802 gnutls_x509_crt_t
*all_certs
;
1805 if (certs
== NULL
|| cert_count
== NULL
)
1807 if (gnutls_certificate_type_get(gsession
) != GNUTLS_CRT_X509
)
1809 cl
= gnutls_certificate_get_peers(gsession
, &len
);
1813 all_certs
= g_malloc(sizeof(gnutls_x509_crt_t
) * len
);
1814 for (i
= 0; i
< len
; i
++) {
1815 gnutls_x509_crt_init(&all_certs
[i
]);
1816 if (gnutls_x509_crt_import(all_certs
[i
], &cl
[i
],
1817 GNUTLS_X509_FMT_PEM
< 0)) {
1831 free_connection_certs(gnutls_x509_crt_t
*certs
, size_t cert_count
)
1835 for (i
= 0; i
< cert_count
; i
++)
1836 gnutls_x509_crt_deinit(certs
[i
]);
1840 #if GTK_CHECK_VERSION(3, 0, 0)
1842 statusbar_modify_attr(struct tab
*t
, const char *css_name
)
1844 gtk_widget_set_name(t
->sbe
.ebox
, css_name
);
1848 statusbar_modify_attr(struct tab
*t
, const char *text
, const char *base
)
1850 GdkColor c_text
, c_base
;
1852 gdk_color_parse(text
, &c_text
);
1853 gdk_color_parse(base
, &c_base
);
1855 gtk_widget_modify_bg(t
->sbe
.ebox
, GTK_STATE_NORMAL
, &c_base
);
1856 gtk_widget_modify_base(t
->sbe
.uri
, GTK_STATE_NORMAL
, &c_base
);
1857 gtk_widget_modify_text(t
->sbe
.uri
, GTK_STATE_NORMAL
, &c_text
);
1862 save_certs(struct tab
*t
, gnutls_x509_crt_t
*certs
,
1863 size_t cert_count
, const char *domain
, const char *dir
)
1866 char cert_buf
[64 * 1024], file
[PATH_MAX
];
1870 if (t
== NULL
|| certs
== NULL
|| cert_count
<= 0 || domain
== NULL
)
1873 snprintf(file
, sizeof file
, "%s" PS
"%s", dir
, domain
);
1874 if ((f
= fopen(file
, "w")) == NULL
) {
1875 show_oops(t
, "Can't create cert file %s %s",
1876 file
, strerror(errno
));
1880 for (i
= 0; i
< cert_count
; i
++) {
1881 cert_buf_sz
= sizeof cert_buf
;
1882 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1883 cert_buf
, &cert_buf_sz
)) {
1884 show_oops(t
, "gnutls_x509_crt_export failed");
1887 if (fwrite(cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1888 show_oops(t
, "Can't write certs: %s", strerror(errno
));
1905 load_compare_cert(const gchar
*uri
, const gchar
**error_str
, const char *dir
)
1907 char domain
[8182], file
[PATH_MAX
];
1908 char cert_buf
[64 * 1024], r_cert_buf
[64 * 1024];
1910 unsigned int error
= 0;
1912 size_t cert_buf_sz
, cert_count
;
1913 enum cert_trust rv
= CERT_UNTRUSTED
;
1914 static gchar serr
[80]; /* this isn't thread safe */
1915 gnutls_session_t gsession
;
1916 gnutls_x509_crt_t
*certs
;
1917 gnutls_certificate_credentials_t xcred
;
1919 DNPRINTF(XT_D_URL
, "%s: %s\n", __func__
, uri
);
1923 if ((s
= connect_socket_from_uri(uri
, error_str
, domain
,
1924 sizeof domain
)) == -1)
1927 DNPRINTF(XT_D_URL
, "%s: fd %d\n", __func__
, s
);
1930 if (start_tls(error_str
, s
, &gsession
, &xcred
))
1932 DNPRINTF(XT_D_URL
, "%s: got tls\n", __func__
);
1934 /* verify certs in case cert file doesn't exist */
1935 if (gnutls_certificate_verify_peers2(gsession
, &error
) !=
1937 *error_str
= "Invalid certificates";
1942 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1943 *error_str
= "Can't get connection certificates";
1947 snprintf(file
, sizeof file
, "%s" PS
"%s", dir
, domain
);
1948 if ((f
= fopen(file
, "r")) == NULL
) {
1954 for (i
= 0; i
< cert_count
; i
++) {
1955 cert_buf_sz
= sizeof cert_buf
;
1956 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1957 cert_buf
, &cert_buf_sz
)) {
1960 if (fread(r_cert_buf
, cert_buf_sz
, 1, f
) != 1 && !feof(f
)) {
1961 rv
= CERT_BAD
; /* critical */
1964 if (bcmp(r_cert_buf
, cert_buf
, cert_buf_sz
)) {
1965 rv
= CERT_BAD
; /* critical */
1974 free_connection_certs(certs
, cert_count
);
1976 /* we close the socket first for speed */
1980 /* only complain if we didn't save it locally */
1981 if (strlen(ssl_ca_file
) != 0 && error
&& rv
!= CERT_LOCAL
) {
1982 strlcpy(serr
, "Certificate exception(s): ", sizeof serr
);
1983 if (error
& GNUTLS_CERT_INVALID
)
1984 strlcat(serr
, "invalid, ", sizeof serr
);
1985 if (error
& GNUTLS_CERT_REVOKED
)
1986 strlcat(serr
, "revoked, ", sizeof serr
);
1987 if (error
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
1988 strlcat(serr
, "signer not found, ", sizeof serr
);
1989 if (error
& GNUTLS_CERT_SIGNER_NOT_CA
)
1990 strlcat(serr
, "not signed by CA, ", sizeof serr
);
1991 if (error
& GNUTLS_CERT_INSECURE_ALGORITHM
)
1992 strlcat(serr
, "insecure algorithm, ", sizeof serr
);
1993 #if LIBGNUTLS_VERSION_MAJOR >= 2 && LIBGNUTLS_VERSION_MINOR >= 6
1994 if (error
& GNUTLS_CERT_NOT_ACTIVATED
)
1995 strlcat(serr
, "not activated, ", sizeof serr
);
1996 if (error
& GNUTLS_CERT_EXPIRED
)
1997 strlcat(serr
, "expired, ", sizeof serr
);
1999 for (i
= strlen(serr
) - 1; i
> 0; i
--)
2000 if (serr
[i
] == ',') {
2007 stop_tls(gsession
, xcred
);
2013 get_local_cert_chain(const char *uri
, size_t *ncerts
, const char **error_str
,
2017 unsigned char cert_buf
[64 * 1024] = {0};
2018 gnutls_datum_t data
;
2019 unsigned int len
= UINT_MAX
;
2021 char file
[PATH_MAX
];
2023 gnutls_x509_crt_t
*certs
;
2025 if ((su
= soup_uri_new(uri
)) == NULL
) {
2026 *error_str
= "Invalid URI";
2030 snprintf(file
, sizeof file
, "%s" PS
"%s", dir
, su
->host
);
2031 if ((f
= fopen(file
, "r")) == NULL
) {
2032 *error_str
= "Could not read local cert";
2036 bytes_read
= fread(cert_buf
, sizeof *cert_buf
, sizeof cert_buf
, f
);
2037 if (bytes_read
== 0) {
2038 *error_str
= "Could not read local cert";
2042 data
.data
= cert_buf
;
2043 data
.size
= bytes_read
;
2044 certs
= g_malloc(sizeof *certs
);
2046 if (gnutls_x509_crt_list_import(certs
, &len
, &data
,
2047 GNUTLS_X509_FMT_PEM
, 0) < 0) {
2048 *error_str
= "Error reading local cert chain";
2058 cert_cmd(struct tab
*t
, struct karg
*args
)
2060 const gchar
*uri
, *error_str
= NULL
;
2064 gnutls_session_t gsession
;
2065 gnutls_x509_crt_t
*certs
;
2066 gnutls_certificate_credentials_t xcred
;
2067 #if !GTK_CHECK_VERSION(3, 0, 0)
2074 if (args
->s
!= NULL
)
2076 else if ((uri
= get_uri(t
)) == NULL
) {
2077 show_oops(t
, "Invalid URI");
2082 * if we're only showing the local certs, don't open a socket and get
2085 if (args
->i
& XT_SHOW
&& args
->i
& XT_CACHE
) {
2086 certs
= get_local_cert_chain(uri
, &cert_count
, &error_str
,
2088 if (error_str
== NULL
) {
2089 show_certs(t
, certs
, cert_count
, "Certificate Chain");
2090 free_connection_certs(certs
, cert_count
);
2092 show_oops(t
, "%s", error_str
);
2098 if ((s
= connect_socket_from_uri(uri
, &error_str
, domain
,
2099 sizeof domain
)) == -1) {
2100 show_oops(t
, "%s", error_str
);
2105 if (start_tls(&error_str
, s
, &gsession
, &xcred
))
2109 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
2110 show_oops(t
, "get_connection_certs failed");
2114 if (args
->i
& XT_SHOW
)
2115 show_certs(t
, certs
, cert_count
, "Certificate Chain");
2116 else if (args
->i
& XT_SAVE
) {
2117 save_certs(t
, certs
, cert_count
, domain
, certs_dir
);
2118 #if GTK_CHECK_VERSION(3, 0, 0)
2119 gtk_widget_set_name(t
->uri_entry
, XT_CSS_BLUE
);
2120 statusbar_modify_attr(t
, XT_CSS_BLUE
);
2122 gdk_color_parse(XT_COLOR_BLUE
, &color
);
2123 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
2124 statusbar_modify_attr(t
, XT_COLOR_BLACK
, XT_COLOR_BLUE
);
2126 } else if (args
->i
& XT_CACHE
)
2127 save_certs(t
, certs
, cert_count
, domain
, certs_cache_dir
);
2129 free_connection_certs(certs
, cert_count
);
2131 /* we close the socket first for speed */
2134 stop_tls(gsession
, xcred
);
2135 if (error_str
&& strlen(error_str
))
2136 show_oops(t
, "%s", error_str
);
2141 * args must be allocated dynamically as the thread that added this function
2142 * to the idle loop no longer exists
2145 warn_cert_cache_differs_idle(struct karg
*args
)
2148 show_oops(NULL
, "%s: invalid parameters", __func__
);
2149 /* return 0 to not re-add function to the idle loop */
2152 xtp_page_sv((struct tab
*)args
->ptr
, args
);
2158 check_cert_changes(struct tab
*t
, const char *uri
)
2160 SoupURI
*soupuri
= NULL
;
2161 struct karg args
= {0};
2162 struct domain
*d
= NULL
;
2163 const char *errstr
= NULL
;
2166 if (!(warn_cert_changes
&& uri
&& g_str_has_prefix(uri
, "https://")))
2169 switch (load_compare_cert(uri
, &errstr
, certs_cache_dir
)) {
2171 /* The cached certificate is identical */
2173 case CERT_TRUSTED
: /* FALLTHROUGH */
2174 case CERT_UNTRUSTED
:
2175 /* cache new certificate */
2180 if ((soupuri
= soup_uri_new(uri
)) == NULL
||
2181 soupuri
->host
== NULL
)
2183 if ((d
= wl_find(soupuri
->host
, &svil
)) != NULL
)
2185 t
->xtp_meaning
= XT_XTP_TAB_MEANING_SV
;
2186 argsp
= g_malloc0(sizeof(struct karg
));
2187 argsp
->s
= g_strdup((char *)uri
);
2188 argsp
->ptr
= (void *)t
;
2189 g_idle_add((GSourceFunc
)warn_cert_cache_differs_idle
, argsp
);
2194 soup_uri_free(soupuri
);
2199 remove_cookie(int index
)
2205 DNPRINTF(XT_D_COOKIE
, "remove_cookie: %d\n", index
);
2207 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
2209 for (i
= 1; cf
; cf
= cf
->next
, i
++) {
2213 print_cookie("remove cookie", c
);
2214 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2219 soup_cookies_free(cf
);
2225 remove_cookie_domain(int domain_id
)
2227 int domain_count
, rv
= 1;
2232 DNPRINTF(XT_D_COOKIE
, "remove_cookie_domain: %d\n", domain_id
);
2235 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
2237 for (domain_count
= 0; cf
; cf
= cf
->next
) {
2240 if (strcmp(last_domain
, c
->domain
) != 0) {
2242 last_domain
= c
->domain
;
2245 if (domain_count
< domain_id
)
2247 else if (domain_count
> domain_id
)
2250 print_cookie("remove cookie", c
);
2251 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2255 soup_cookies_free(cf
);
2267 DNPRINTF(XT_D_COOKIE
, "remove_cookie_all\n");
2269 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
2271 for (; cf
; cf
= cf
->next
) {
2274 print_cookie("remove cookie", c
);
2275 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2279 soup_cookies_free(cf
);
2285 toplevel_cmd(struct tab
*t
, struct karg
*args
)
2287 js_toggle_cb(t
->js_toggle
, t
);
2293 navaction(struct tab
*t
, struct karg
*args
)
2295 WebKitWebFrame
*frame
;
2297 DNPRINTF(XT_D_NAV
, "navaction: tab %d opcode %d\n",
2298 t
->tab_id
, args
->i
);
2301 set_normal_tab_meaning(t
);
2307 webkit_web_view_go_to_back_forward_item(t
->wv
, t
->item
);
2309 webkit_web_view_go_back(t
->wv
);
2311 case XT_NAV_FORWARD
:
2313 webkit_web_view_go_forward(t
->wv
);
2316 frame
= webkit_web_view_get_main_frame(t
->wv
);
2317 webkit_web_frame_reload(frame
);
2320 frame
= webkit_web_view_get_main_frame(t
->wv
);
2321 webkit_web_frame_stop_loading(frame
);
2324 return (XT_CB_PASSTHROUGH
);
2328 move(struct tab
*t
, struct karg
*args
)
2330 GtkAdjustment
*adjust
;
2331 double pi
, si
, pos
, ps
, upper
, lower
, max
;
2337 case XT_MOVE_BOTTOM
:
2339 case XT_MOVE_PAGEDOWN
:
2340 case XT_MOVE_PAGEUP
:
2341 case XT_MOVE_HALFDOWN
:
2342 case XT_MOVE_HALFUP
:
2343 case XT_MOVE_PERCENT
:
2344 case XT_MOVE_CENTER
:
2345 adjust
= t
->adjust_v
;
2348 adjust
= t
->adjust_h
;
2352 pos
= gtk_adjustment_get_value(adjust
);
2353 ps
= gtk_adjustment_get_page_size(adjust
);
2354 upper
= gtk_adjustment_get_upper(adjust
);
2355 lower
= gtk_adjustment_get_lower(adjust
);
2356 si
= gtk_adjustment_get_step_increment(adjust
);
2357 pi
= gtk_adjustment_get_page_increment(adjust
);
2360 DNPRINTF(XT_D_MOVE
, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2361 "max %f si %f pi %f\n",
2362 args
->i
, adjust
== t
->adjust_h
? "horizontal" : "vertical",
2363 pos
, ps
, upper
, lower
, max
, si
, pi
);
2369 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2374 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2376 case XT_MOVE_BOTTOM
:
2377 case XT_MOVE_FARRIGHT
:
2378 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2379 gtk_adjustment_set_value(adjust
, max
);
2382 case XT_MOVE_FARLEFT
:
2383 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2384 gtk_adjustment_set_value(adjust
, lower
);
2386 case XT_MOVE_PAGEDOWN
:
2388 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2390 case XT_MOVE_PAGEUP
:
2392 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2394 case XT_MOVE_HALFDOWN
:
2396 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2398 case XT_MOVE_HALFUP
:
2400 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2402 case XT_MOVE_CENTER
:
2403 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2404 args
->s
= g_strdup("50.0");
2406 case XT_MOVE_PERCENT
:
2407 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2408 percent
= atoi(args
->s
) / 100.0;
2409 pos
= max
* percent
;
2410 if (pos
< 0.0 || pos
> max
)
2412 gtk_adjustment_set_value(adjust
, pos
);
2415 return (XT_CB_PASSTHROUGH
);
2418 DNPRINTF(XT_D_MOVE
, "move: new pos %f %f\n", pos
, MIN(pos
, max
));
2420 return (XT_CB_HANDLED
);
2424 url_set_visibility(void)
2428 TAILQ_FOREACH(t
, &tabs
, entry
)
2429 if (show_url
== 0) {
2430 gtk_widget_hide(t
->toolbar
);
2433 gtk_widget_show(t
->toolbar
);
2437 notebook_tab_set_visibility(void)
2439 if (show_tabs
== 0) {
2440 gtk_widget_hide(tab_bar
);
2441 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2443 if (tab_style
== XT_TABS_NORMAL
) {
2444 gtk_widget_hide(tab_bar
);
2445 gtk_notebook_set_show_tabs(notebook
, TRUE
);
2446 } else if (tab_style
== XT_TABS_COMPACT
) {
2447 gtk_widget_show(tab_bar
);
2448 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2454 statusbar_set_visibility(void)
2458 TAILQ_FOREACH(t
, &tabs
, entry
){
2459 if (show_statusbar
== 0)
2460 gtk_widget_hide(t
->statusbar
);
2462 gtk_widget_show(t
->statusbar
);
2469 url_set(struct tab
*t
, int enable_url_entry
)
2474 show_url
= enable_url_entry
;
2476 if (enable_url_entry
) {
2477 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.uri
),
2478 GTK_ENTRY_ICON_PRIMARY
, NULL
);
2479 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.uri
), 0);
2481 pixbuf
= gtk_entry_get_icon_pixbuf(GTK_ENTRY(t
->uri_entry
),
2482 GTK_ENTRY_ICON_PRIMARY
);
2484 gtk_entry_get_progress_fraction(GTK_ENTRY(t
->uri_entry
));
2485 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.uri
),
2486 GTK_ENTRY_ICON_PRIMARY
, pixbuf
);
2487 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.uri
),
2493 fullscreen(struct tab
*t
, struct karg
*args
)
2495 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2498 return (XT_CB_PASSTHROUGH
);
2500 if (show_url
== 0) {
2508 url_set_visibility();
2509 notebook_tab_set_visibility();
2511 return (XT_CB_HANDLED
);
2515 statustoggle(struct tab
*t
, struct karg
*args
)
2517 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2519 if (show_statusbar
== 1) {
2521 statusbar_set_visibility();
2522 } else if (show_statusbar
== 0) {
2524 statusbar_set_visibility();
2526 return (XT_CB_HANDLED
);
2530 urlaction(struct tab
*t
, struct karg
*args
)
2532 int rv
= XT_CB_HANDLED
;
2534 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2537 return (XT_CB_PASSTHROUGH
);
2541 if (show_url
== 0) {
2543 url_set_visibility();
2547 if (show_url
== 1) {
2549 url_set_visibility();
2557 tabaction(struct tab
*t
, struct karg
*args
)
2559 int rv
= XT_CB_HANDLED
;
2560 char *url
= args
->s
;
2562 struct tab
*tt
, *tv
;
2564 DNPRINTF(XT_D_TAB
, "tabaction: %p %d\n", t
, args
->i
);
2567 return (XT_CB_PASSTHROUGH
);
2571 if (strlen(url
) > 0)
2572 create_new_tab(url
, NULL
, 1, args
->precount
);
2574 create_new_tab(NULL
, NULL
, 1, args
->precount
);
2577 if (args
->precount
< 0)
2580 TAILQ_FOREACH(tt
, &tabs
, entry
)
2581 if (tt
->tab_id
== args
->precount
- 1) {
2586 case XT_TAB_DELQUIT
:
2587 if (gtk_notebook_get_n_pages(notebook
) > 1)
2593 TAILQ_FOREACH_SAFE(tt
, &tabs
, entry
, tv
)
2598 if (strlen(url
) > 0)
2601 rv
= XT_CB_PASSTHROUGH
;
2607 if (show_tabs
== 0) {
2609 notebook_tab_set_visibility();
2613 if (show_tabs
== 1) {
2615 notebook_tab_set_visibility();
2618 case XT_TAB_NEXTSTYLE
:
2619 if (tab_style
== XT_TABS_NORMAL
) {
2620 tab_style
= XT_TABS_COMPACT
;
2621 recolor_compact_tabs();
2624 tab_style
= XT_TABS_NORMAL
;
2625 notebook_tab_set_visibility();
2627 case XT_TAB_UNDO_CLOSE
:
2628 if (undo_count
== 0) {
2629 DNPRINTF(XT_D_TAB
, "%s: no tabs to undo close",
2634 u
= TAILQ_FIRST(&undos
);
2635 create_new_tab(u
->uri
, u
, 1, -1);
2637 TAILQ_REMOVE(&undos
, u
, entry
);
2639 /* u->history is freed in create_new_tab() */
2643 case XT_TAB_LOAD_IMAGES
:
2645 if (!auto_load_images
) {
2647 /* Enable auto-load images (this will load all
2648 * previously unloaded images). */
2649 g_object_set(G_OBJECT(t
->settings
),
2650 "auto-load-images", TRUE
, (char *)NULL
);
2651 webkit_web_view_set_settings(t
->wv
, t
->settings
);
2653 webkit_web_view_reload(t
->wv
);
2655 /* Webkit triggers an event when we change the setting,
2656 * so we can't disable the auto-loading at once.
2658 * Unfortunately, webkit does not tell us when it's done.
2659 * Instead, we wait until the next request, and then
2660 * disable autoloading again.
2662 t
->load_images
= TRUE
;
2666 rv
= XT_CB_PASSTHROUGH
;
2680 resizetab(struct tab
*t
, struct karg
*args
)
2682 if (t
== NULL
|| args
== NULL
) {
2683 show_oops(NULL
, "resizetab invalid parameters");
2684 return (XT_CB_PASSTHROUGH
);
2687 DNPRINTF(XT_D_TAB
, "resizetab: tab %d %d\n",
2688 t
->tab_id
, args
->i
);
2690 setzoom_webkit(t
, args
->i
);
2692 return (XT_CB_HANDLED
);
2696 movetab(struct tab
*t
, struct karg
*args
)
2700 if (t
== NULL
|| args
== NULL
) {
2701 show_oops(NULL
, "movetab invalid parameters");
2702 return (XT_CB_PASSTHROUGH
);
2705 DNPRINTF(XT_D_TAB
, "movetab: tab %d opcode %d\n",
2706 t
->tab_id
, args
->i
);
2708 if (args
->i
>= XT_TAB_INVALID
)
2709 return (XT_CB_PASSTHROUGH
);
2711 if (TAILQ_EMPTY(&tabs
))
2712 return (XT_CB_PASSTHROUGH
);
2714 n
= gtk_notebook_get_n_pages(notebook
);
2715 dest
= gtk_notebook_get_current_page(notebook
);
2719 if (args
->precount
< 0)
2720 dest
= dest
== n
- 1 ? 0 : dest
+ 1;
2722 dest
= args
->precount
- 1;
2726 if (args
->precount
< 0)
2729 dest
-= args
->precount
% n
;
2742 return (XT_CB_PASSTHROUGH
);
2745 if (dest
< 0 || dest
>= n
)
2746 return (XT_CB_PASSTHROUGH
);
2747 if (t
->tab_id
== dest
) {
2748 DNPRINTF(XT_D_TAB
, "movetab: do nothing\n");
2749 return (XT_CB_HANDLED
);
2752 set_current_tab(dest
);
2754 return (XT_CB_HANDLED
);
2761 const char *(*f
)(struct tab
*);
2763 { "<uri>", get_uri
},
2767 command(struct tab
*t
, struct karg
*args
)
2769 struct karg a
= {0};
2771 char *s
= NULL
, *sp
= NULL
, *sl
= NULL
;
2774 if (t
== NULL
|| args
== NULL
) {
2775 show_oops(NULL
, "command invalid parameters");
2776 return (XT_CB_PASSTHROUGH
);
2787 if (cmd_prefix
== 0) {
2788 if (args
->s
!= NULL
&& strlen(args
->s
) != 0) {
2789 sp
= g_strdup_printf(":%s", args
->s
);
2794 sp
= g_strdup_printf(":%d", cmd_prefix
);
2804 for (i
= 0; i
< LENGTH(subs
); ++i
) {
2805 sv
= g_strsplit(sl
, subs
[i
].s
, -1);
2808 sl
= g_strjoinv(subs
[i
].f(t
), sv
);
2814 t
->mode
= XT_MODE_HINT
;
2820 t
->mode
= XT_MODE_HINT
;
2821 a
.i
= XT_HINT_NEWTAB
;
2826 show_oops(t
, "command: invalid opcode %d", args
->i
);
2827 return (XT_CB_PASSTHROUGH
);
2830 DNPRINTF(XT_D_CMD
, "%s: tab %d type %s\n", __func__
, t
->tab_id
, s
);
2833 gtk_widget_grab_focus(GTK_WIDGET(t
->cmd
));
2834 #if GTK_CHECK_VERSION(3, 0, 0)
2835 gtk_widget_set_name(t
->cmd
, XT_CSS_NORMAL
);
2837 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
2838 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
2840 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), s
);
2841 gtk_editable_set_position(GTK_EDITABLE(t
->cmd
), -1);
2848 return (XT_CB_HANDLED
);
2852 search(struct tab
*t
, struct karg
*args
)
2856 if (t
== NULL
|| args
== NULL
) {
2857 show_oops(NULL
, "search invalid parameters");
2862 case XT_SEARCH_NEXT
:
2863 d
= t
->search_forward
;
2865 case XT_SEARCH_PREV
:
2866 d
= !t
->search_forward
;
2869 return (XT_CB_PASSTHROUGH
);
2872 if (t
->search_text
== NULL
) {
2873 if (global_search
== NULL
)
2874 return (XT_CB_PASSTHROUGH
);
2876 d
= t
->search_forward
= TRUE
;
2877 t
->search_text
= g_strdup(global_search
);
2878 webkit_web_view_mark_text_matches(t
->wv
, global_search
, FALSE
, 0);
2879 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
2883 DNPRINTF(XT_D_CMD
, "search: tab %d opc %d forw %d text %s\n",
2884 t
->tab_id
, args
->i
, t
->search_forward
, t
->search_text
);
2886 webkit_web_view_search_text(t
->wv
, t
->search_text
, FALSE
, d
, TRUE
);
2888 return (XT_CB_HANDLED
);
2892 session_save(struct tab
*t
, char *filename
)
2898 if (strlen(filename
) == 0)
2901 if (filename
[0] == '.' || filename
[0] == '/')
2905 if (save_tabs(t
, &a
))
2907 strlcpy(named_session
, filename
, sizeof named_session
);
2909 /* add the new session to the list of sessions */
2910 s
= g_malloc(sizeof(struct session
));
2911 s
->name
= g_strdup(filename
);
2912 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
2920 session_open(struct tab
*t
, char *filename
)
2925 if (strlen(filename
) == 0)
2928 if (filename
[0] == '.' || filename
[0] == '/')
2932 a
.i
= XT_SES_CLOSETABS
;
2933 if (open_tabs(t
, &a
))
2936 strlcpy(named_session
, filename
, sizeof named_session
);
2944 session_delete(struct tab
*t
, char *filename
)
2946 char file
[PATH_MAX
];
2950 if (strlen(filename
) == 0)
2953 if (filename
[0] == '.' || filename
[0] == '/')
2956 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, filename
);
2960 if (!strcmp(filename
, named_session
))
2961 strlcpy(named_session
, XT_SAVED_TABS_FILE
,
2962 sizeof named_session
);
2964 /* remove session from sessions list */
2965 TAILQ_FOREACH(s
, &sessions
, entry
) {
2966 if (!strcmp(s
->name
, filename
))
2971 TAILQ_REMOVE(&sessions
, s
, entry
);
2972 g_free((gpointer
) s
->name
);
2981 session_cmd(struct tab
*t
, struct karg
*args
)
2983 char *filename
= args
->s
;
2988 if (args
->i
& XT_SHOW
)
2989 show_oops(t
, "Current session: %s", named_session
[0] == '\0' ?
2990 XT_SAVED_TABS_FILE
: named_session
);
2991 else if (args
->i
& XT_SAVE
) {
2992 if (session_save(t
, filename
)) {
2993 show_oops(t
, "Can't save session: %s",
2994 filename
? filename
: "INVALID");
2997 } else if (args
->i
& XT_OPEN
) {
2998 if (session_open(t
, filename
)) {
2999 show_oops(t
, "Can't open session: %s",
3000 filename
? filename
: "INVALID");
3003 } else if (args
->i
& XT_DELETE
) {
3004 if (session_delete(t
, filename
)) {
3005 show_oops(t
, "Can't delete session: %s",
3006 filename
? filename
: "INVALID");
3011 return (XT_CB_PASSTHROUGH
);
3015 script_cmd(struct tab
*t
, struct karg
*args
)
3024 if ((f
= fopen(args
->s
, "r")) == NULL
) {
3025 show_oops(t
, "Can't open script file: %s", args
->s
);
3029 if (fstat(fileno(f
), &sb
) == -1) {
3030 show_oops(t
, "Can't stat script file: %s", args
->s
);
3034 buf
= g_malloc0(sb
.st_size
+ 1);
3035 if (fread(buf
, 1, sb
.st_size
, f
) != sb
.st_size
) {
3036 show_oops(t
, "Can't read script file: %s", args
->s
);
3040 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
3049 return (XT_CB_PASSTHROUGH
);
3053 * Make a hardcopy of the page
3056 print_page(struct tab
*t
, struct karg
*args
)
3058 WebKitWebFrame
*frame
;
3060 GtkPrintOperation
*op
;
3061 GtkPrintOperationAction action
;
3062 GtkPrintOperationResult print_res
;
3063 GError
*g_err
= NULL
;
3064 int marg_l
, marg_r
, marg_t
, marg_b
;
3066 DNPRINTF(XT_D_PRINTING
, "%s:", __func__
);
3068 ps
= gtk_page_setup_new();
3069 op
= gtk_print_operation_new();
3070 action
= GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
;
3071 frame
= webkit_web_view_get_main_frame(t
->wv
);
3073 /* the default margins are too small, so we will bump them */
3074 marg_l
= gtk_page_setup_get_left_margin(ps
, GTK_UNIT_MM
) +
3075 XT_PRINT_EXTRA_MARGIN
;
3076 marg_r
= gtk_page_setup_get_right_margin(ps
, GTK_UNIT_MM
) +
3077 XT_PRINT_EXTRA_MARGIN
;
3078 marg_t
= gtk_page_setup_get_top_margin(ps
, GTK_UNIT_MM
) +
3079 XT_PRINT_EXTRA_MARGIN
;
3080 marg_b
= gtk_page_setup_get_bottom_margin(ps
, GTK_UNIT_MM
) +
3081 XT_PRINT_EXTRA_MARGIN
;
3084 gtk_page_setup_set_left_margin(ps
, marg_l
, GTK_UNIT_MM
);
3085 gtk_page_setup_set_right_margin(ps
, marg_r
, GTK_UNIT_MM
);
3086 gtk_page_setup_set_top_margin(ps
, marg_t
, GTK_UNIT_MM
);
3087 gtk_page_setup_set_bottom_margin(ps
, marg_b
, GTK_UNIT_MM
);
3089 gtk_print_operation_set_default_page_setup(op
, ps
);
3091 /* this appears to free 'op' and 'ps' */
3092 print_res
= webkit_web_frame_print_full(frame
, op
, action
, &g_err
);
3094 /* check it worked */
3095 if (print_res
== GTK_PRINT_OPERATION_RESULT_ERROR
) {
3096 show_oops(NULL
, "can't print: %s", g_err
->message
);
3097 g_error_free (g_err
);
3105 go_home(struct tab
*t
, struct karg
*args
)
3112 set_encoding(struct tab
*t
, struct karg
*args
)
3116 if (args
->s
&& strlen(g_strstrip(args
->s
)) == 0) {
3117 e
= webkit_web_view_get_custom_encoding(t
->wv
);
3119 e
= webkit_web_view_get_encoding(t
->wv
);
3120 show_oops(t
, "encoding: %s", e
? e
: "N/A");
3122 webkit_web_view_set_custom_encoding(t
->wv
, args
->s
);
3128 restart(struct tab
*t
, struct karg
*args
)
3132 a
.s
= XT_RESTART_TABS_FILE
;
3134 execvp(start_argv
[0], start_argv
);
3140 char *http_proxy_save
; /* not a setting, used to toggle */
3143 proxy_cmd(struct tab
*t
, struct karg
*args
)
3145 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
3152 if (http_proxy_save
)
3153 g_free(http_proxy_save
);
3154 http_proxy_save
= g_strdup(http_proxy
);
3157 if (args
->i
& XT_PRXY_SHOW
) {
3159 show_oops(t
, "http_proxy = %s", http_proxy
);
3161 show_oops(t
, "proxy is currently disabled");
3162 } else if (args
->i
& XT_PRXY_TOGGLE
) {
3163 if (http_proxy_save
== NULL
&& http_proxy
== NULL
) {
3164 show_oops(t
, "can't toggle proxy");
3169 show_oops(t
, "http proxy disabled");
3171 setup_proxy(http_proxy_save
);
3172 show_oops(t
, "http_proxy = %s", http_proxy
);
3176 return (XT_CB_PASSTHROUGH
);
3181 int (*func
)(struct tab
*, struct karg
*);
3185 { "command_mode", 0, command_mode
, XT_MODE_COMMAND
, 0 },
3186 { "insert_mode", 0, command_mode
, XT_MODE_INSERT
, 0 },
3187 { "command", 0, command
, ':', 0 },
3188 { "search", 0, command
, '/', 0 },
3189 { "searchb", 0, command
, '?', 0 },
3190 { "hinting", 0, command
, '.', 0 },
3191 { "hinting_newtab", 0, command
, ',', 0 },
3192 { "togglesrc", 0, toggle_src
, 0, 0 },
3193 { "editsrc", 0, edit_src
, 0, 0 },
3194 { "editelement", 0, edit_element
, 0, 0 },
3195 { "passthrough", 0, passthrough
, 0, 0 },
3196 { "modurl", 0, modurl
, 0, 0 },
3198 /* yanking and pasting */
3199 { "yankuri", 0, yank_uri
, 0, 0 },
3200 { "pasteuricur", 0, paste_uri
, XT_PASTE_CURRENT_TAB
, 0 },
3201 { "pasteurinew", 0, paste_uri
, XT_PASTE_NEW_TAB
, 0 },
3204 { "searchnext", 0, search
, XT_SEARCH_NEXT
, 0 },
3205 { "searchprevious", 0, search
, XT_SEARCH_PREV
, 0 },
3208 { "focusaddress", 0, focus
, XT_FOCUS_URI
, 0 },
3209 { "focussearch", 0, focus
, XT_FOCUS_SEARCH
, 0 },
3212 { "hinting", 0, hint
, 0, 0 },
3213 { "hinting_newtab", 0, hint
, XT_HINT_NEWTAB
, 0 },
3215 /* custom stylesheet */
3216 { "userstyle", 0, userstyle_cmd
, XT_STYLE_CURRENT_TAB
, XT_USERARG
},
3217 { "userstyle_global", 0, userstyle_cmd
, XT_STYLE_GLOBAL
, XT_USERARG
},
3220 { "goback", 0, navaction
, XT_NAV_BACK
, 0 },
3221 { "goforward", 0, navaction
, XT_NAV_FORWARD
, 0 },
3222 { "reload", 0, navaction
, XT_NAV_RELOAD
, 0 },
3223 { "stop", 0, navaction
, XT_NAV_STOP
, 0 },
3225 /* vertical movement */
3226 { "scrolldown", 0, move
, XT_MOVE_DOWN
, 0 },
3227 { "scrollup", 0, move
, XT_MOVE_UP
, 0 },
3228 { "scrollbottom", 0, move
, XT_MOVE_BOTTOM
, 0 },
3229 { "scrolltop", 0, move
, XT_MOVE_TOP
, 0 },
3230 { "1", 0, move
, XT_MOVE_TOP
, 0 },
3231 { "scrollhalfdown", 0, move
, XT_MOVE_HALFDOWN
, 0 },
3232 { "scrollhalfup", 0, move
, XT_MOVE_HALFUP
, 0 },
3233 { "scrollpagedown", 0, move
, XT_MOVE_PAGEDOWN
, 0 },
3234 { "scrollpageup", 0, move
, XT_MOVE_PAGEUP
, 0 },
3235 /* horizontal movement */
3236 { "scrollright", 0, move
, XT_MOVE_RIGHT
, 0 },
3237 { "scrollleft", 0, move
, XT_MOVE_LEFT
, 0 },
3238 { "scrollfarright", 0, move
, XT_MOVE_FARRIGHT
, 0 },
3239 { "scrollfarleft", 0, move
, XT_MOVE_FARLEFT
, 0 },
3241 { "favorites", 0, xtp_page_fl
, XT_SHOW
, 0 },
3242 { "fav", 0, xtp_page_fl
, XT_SHOW
, 0 },
3243 { "favedit", 0, xtp_page_fl
, XT_SHOW
|XT_DELETE
, 0 },
3244 { "favadd", 0, add_favorite
, 0, 0 },
3246 { "qall", 0, quit
, 0, 0 },
3247 { "quitall", 0, quit
, 0, 0 },
3248 { "w", 0, save_tabs
, 0, 0 },
3249 { "wq", 0, save_tabs_and_quit
, 0, 0 },
3250 { "help", 0, help
, 0, 0 },
3251 { "about", 0, xtp_page_ab
, 0, 0 },
3252 { "stats", 0, stats
, 0, 0 },
3253 { "version", 0, xtp_page_ab
, 0, 0 },
3256 { "js", 0, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3257 { "save", 1, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3258 { "domain", 2, js_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3259 { "fqdn", 2, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3260 { "show", 1, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3261 { "all", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3262 { "persistent", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3263 { "session", 2, js_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3264 { "toggle", 1, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3265 { "domain", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3266 { "fqdn", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3268 /* cookie command */
3269 { "cookie", 0, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3270 { "save", 1, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3271 { "domain", 2, cookie_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3272 { "fqdn", 2, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3273 { "show", 1, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3274 { "all", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3275 { "persistent", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3276 { "session", 2, cookie_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3277 { "toggle", 1, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3278 { "domain", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3279 { "fqdn", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3280 { "purge", 1, cookie_cmd
, XT_DELETE
, 0 },
3282 /* plugin command */
3283 { "plugin", 0, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3284 { "save", 1, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3285 { "domain", 2, pl_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3286 { "fqdn", 2, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3287 { "show", 1, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3288 { "all", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3289 { "persistent", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3290 { "session", 2, pl_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3291 { "toggle", 1, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3292 { "domain", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3293 { "fqdn", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3296 { "https", 0, https_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3297 { "save", 1, https_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3298 { "domain", 2, https_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3299 { "fqdn", 2, https_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3300 { "show", 1, https_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3301 { "all", 2, https_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3302 { "persistent", 2, https_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3303 { "session", 2, https_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3304 { "toggle", 1, https_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3305 { "domain", 2, https_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3306 { "fqdn", 2, https_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3308 /* toplevel (domain) command */
3309 { "toplevel", 0, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3310 { "toggle", 1, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3313 { "cookiejar", 0, xtp_page_cl
, 0, 0 },
3316 { "cert", 0, cert_cmd
, XT_SHOW
, 0 },
3317 { "save", 1, cert_cmd
, XT_SAVE
, 0 },
3318 { "show", 1, cert_cmd
, XT_SHOW
, 0 },
3320 { "ca", 0, ca_cmd
, 0, 0 },
3321 { "downloadmgr", 0, xtp_page_dl
, 0, 0 },
3322 { "dl", 0, xtp_page_dl
, 0, 0 },
3323 { "h", 0, xtp_page_hl
, 0, 0 },
3324 { "history", 0, xtp_page_hl
, 0, 0 },
3325 { "home", 0, go_home
, 0, 0 },
3326 { "restart", 0, restart
, 0, 0 },
3327 { "urlhide", 0, urlaction
, XT_URL_HIDE
, 0 },
3328 { "urlshow", 0, urlaction
, XT_URL_SHOW
, 0 },
3329 { "statustoggle", 0, statustoggle
, 0, 0 },
3330 { "run_script", 0, run_page_script
, 0, XT_USERARG
},
3332 { "print", 0, print_page
, 0, 0 },
3335 { "focusin", 0, resizetab
, XT_ZOOM_IN
, 0 },
3336 { "focusout", 0, resizetab
, XT_ZOOM_OUT
, 0 },
3337 { "focusreset", 0, resizetab
, XT_ZOOM_NORMAL
, 0 },
3338 { "q", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3339 { "quit", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3340 { "open", 0, tabaction
, XT_TAB_OPEN
, XT_URLARG
},
3341 { "tabclose", 0, tabaction
, XT_TAB_DELETE
, XT_PREFIX
| XT_INTARG
},
3342 { "tabedit", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3343 { "tabfirst", 0, movetab
, XT_TAB_FIRST
, 0 },
3344 { "tabhide", 0, tabaction
, XT_TAB_HIDE
, 0 },
3345 { "tablast", 0, movetab
, XT_TAB_LAST
, 0 },
3346 { "tabnew", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3347 { "tabnext", 0, movetab
, XT_TAB_NEXT
, XT_PREFIX
| XT_INTARG
},
3348 { "tabnextstyle", 0, tabaction
, XT_TAB_NEXTSTYLE
, 0 },
3349 { "tabonly", 0, tabaction
, XT_TAB_ONLY
, 0 },
3350 { "tabprevious", 0, movetab
, XT_TAB_PREV
, XT_PREFIX
| XT_INTARG
},
3351 { "tabrewind", 0, movetab
, XT_TAB_FIRST
, 0 },
3352 { "tabshow", 0, tabaction
, XT_TAB_SHOW
, 0 },
3353 { "tabs", 0, buffers
, 0, 0 },
3354 { "tabundoclose", 0, tabaction
, XT_TAB_UNDO_CLOSE
, 0 },
3355 { "buffers", 0, buffers
, 0, 0 },
3356 { "ls", 0, buffers
, 0, 0 },
3357 { "encoding", 0, set_encoding
, 0, XT_USERARG
},
3358 { "loadimages", 0, tabaction
, XT_TAB_LOAD_IMAGES
, 0 },
3361 { "set", 0, set
, 0, XT_SETARG
},
3362 { "runtime", 0, xtp_page_rt
, 0, 0 },
3364 { "fullscreen", 0, fullscreen
, 0, 0 },
3365 { "f", 0, fullscreen
, 0, 0 },
3368 { "session", 0, session_cmd
, XT_SHOW
, 0 },
3369 { "delete", 1, session_cmd
, XT_DELETE
, XT_SESSARG
},
3370 { "open", 1, session_cmd
, XT_OPEN
, XT_SESSARG
},
3371 { "save", 1, session_cmd
, XT_SAVE
, XT_USERARG
},
3372 { "show", 1, session_cmd
, XT_SHOW
, 0 },
3374 /* external javascript */
3375 { "script", 0, script_cmd
, XT_EJS_SHOW
, XT_USERARG
},
3378 { "inspector", 0, inspector_cmd
, XT_INS_SHOW
, 0 },
3379 { "show", 1, inspector_cmd
, XT_INS_SHOW
, 0 },
3380 { "hide", 1, inspector_cmd
, XT_INS_HIDE
, 0 },
3383 { "proxy", 0, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3384 { "show", 1, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3385 { "toggle", 1, proxy_cmd
, XT_PRXY_TOGGLE
, 0 },
3392 } cmd_status
= {-1, 0};
3395 wv_release_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3398 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 1)
3405 wv_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3408 WebKitHitTestResult
*hit_test_result
;
3411 hit_test_result
= webkit_web_view_get_hit_test_result(t
->wv
, e
);
3412 g_object_get(hit_test_result
, "context", &context
, NULL
);
3417 if (context
& WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE
)
3418 t
->mode
= XT_MODE_INSERT
;
3420 t
->mode
= XT_MODE_COMMAND
;
3422 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3424 else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 8 /* btn 4 */) {
3430 } else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 9 /* btn 5 */) {
3432 a
.i
= XT_NAV_FORWARD
;
3442 tab_close_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3444 DNPRINTF(XT_D_TAB
, "tab_close_cb: tab %d\n", t
->tab_id
);
3446 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3453 parse_custom_uri(struct tab
*t
, const char *uri
)
3455 struct custom_uri
*u
;
3459 TAILQ_FOREACH(u
, &cul
, entry
) {
3460 if (strncmp(uri
, u
->uri
, strlen(u
->uri
)))
3465 sv
[1] = (char *)uri
;
3467 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
,
3469 show_oops(t
, "%s: could not spawn process", __func__
);
3476 activate_uri_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3478 const gchar
*uri
= gtk_entry_get_text(GTK_ENTRY(entry
));
3480 DNPRINTF(XT_D_URL
, "activate_uri_entry_cb: %s\n", uri
);
3483 show_oops(NULL
, "activate_uri_entry_cb invalid parameters");
3488 show_oops(t
, "activate_uri_entry_cb no uri");
3492 uri
+= strspn(uri
, "\t ");
3494 if (parse_custom_uri(t
, uri
))
3497 /* otherwise continue to load page normally */
3498 load_uri(t
, (gchar
*)uri
);
3503 activate_search_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3505 const gchar
*search
= gtk_entry_get_text(GTK_ENTRY(entry
));
3506 char *newuri
= NULL
;
3510 DNPRINTF(XT_D_URL
, "activate_search_entry_cb: %s\n", search
);
3513 show_oops(NULL
, "activate_search_entry_cb invalid parameters");
3517 if (search_string
== NULL
|| strlen(search_string
) == 0) {
3518 show_oops(t
, "no search_string");
3522 set_normal_tab_meaning(t
);
3524 enc_search
= soup_uri_encode(search
, XT_RESERVED_CHARS
);
3525 sv
= g_strsplit(search_string
, "%s", 2);
3526 newuri
= g_strjoinv(enc_search
, sv
);
3531 load_uri(t
, newuri
);
3539 check_and_set_cookie(const gchar
*uri
, struct tab
*t
)
3541 struct domain
*d
= NULL
;
3544 if (uri
== NULL
|| t
== NULL
)
3547 if ((d
= wl_find_uri(uri
, &c_wl
)) == NULL
)
3552 DNPRINTF(XT_D_COOKIE
, "check_and_set_cookie: %s %s\n",
3553 es
? "enable" : "disable", uri
);
3555 g_object_set(G_OBJECT(t
->settings
),
3556 "enable-html5-local-storage", es
, (char *)NULL
);
3557 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3561 check_and_set_js(const gchar
*uri
, struct tab
*t
)
3563 struct domain
*d
= NULL
;
3566 if (uri
== NULL
|| t
== NULL
)
3569 if ((d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3574 DNPRINTF(XT_D_JS
, "check_and_set_js: %s %s\n",
3575 es
? "enable" : "disable", uri
);
3577 g_object_set(G_OBJECT(t
->settings
),
3578 "enable-scripts", es
, (char *)NULL
);
3579 g_object_set(G_OBJECT(t
->settings
),
3580 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
3581 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3583 button_set_stockid(t
->js_toggle
,
3584 es
? GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
);
3588 check_and_set_pl(const gchar
*uri
, struct tab
*t
)
3590 struct domain
*d
= NULL
;
3593 if (uri
== NULL
|| t
== NULL
)
3596 if ((d
= wl_find_uri(uri
, &pl_wl
)) == NULL
)
3601 DNPRINTF(XT_D_JS
, "check_and_set_pl: %s %s\n",
3602 es
? "enable" : "disable", uri
);
3604 g_object_set(G_OBJECT(t
->settings
),
3605 "enable-plugins", es
, (char *)NULL
);
3606 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3609 #if GTK_CHECK_VERSION(3, 0, 0)
3610 /* A lot of this can be removed when gtk2 is dropped on the floor */
3612 get_css_name(const char *col_str
)
3616 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3617 name
= g_strdup(XT_CSS_NORMAL
);
3618 else if (!strcmp(col_str
, XT_COLOR_RED
))
3619 name
= g_strdup(XT_CSS_RED
);
3620 else if (!strcmp(col_str
, XT_COLOR_YELLOW
))
3621 name
= g_strdup(XT_CSS_YELLOW
);
3622 else if (!strcmp(col_str
, XT_COLOR_GREEN
))
3623 name
= g_strdup(XT_CSS_GREEN
);
3624 else if (!strcmp(col_str
, XT_COLOR_BLUE
))
3625 name
= g_strdup(XT_CSS_BLUE
);
3631 color_address_bar(gpointer p
)
3633 struct tab
*tt
, *t
= p
;
3634 gchar
*col_str
= XT_COLOR_WHITE
;
3635 const gchar
*uri
, *u
= NULL
, *error_str
= NULL
;
3636 #if GTK_CHECK_VERSION(3, 0, 0)
3644 gdk_threads_enter();
3646 DNPRINTF(XT_D_URL
, "%s:\n", __func__
);
3648 /* make sure t still exists */
3651 TAILQ_FOREACH(tt
, &tabs
, entry
)
3657 if ((uri
= get_uri(t
)) == NULL
)
3663 gdk_threads_leave();
3666 col_str
= XT_COLOR_YELLOW
;
3667 switch (load_compare_cert(u
, &error_str
, certs_dir
)) {
3669 col_str
= XT_COLOR_BLUE
;
3672 col_str
= (strlen(ssl_ca_file
) == 0) ? XT_COLOR_RED
3675 case CERT_UNTRUSTED
:
3676 col_str
= (strlen(ssl_ca_file
) == 0) ? XT_COLOR_RED
3680 col_str
= XT_COLOR_RED
;
3685 gdk_threads_enter();
3687 /* make sure t isn't deleted */
3688 TAILQ_FOREACH(tt
, &tabs
, entry
)
3695 /* test to see if the user navigated away and canceled the thread */
3696 if (t
->thread
!= g_thread_self())
3698 if ((uri
= get_uri(t
)) == NULL
) {
3702 if (strcmp(uri
, u
)) {
3703 /* make sure we are still the same url */
3710 if (!strcmp(col_str
, XT_COLOR_WHITE
)) {
3711 #if GTK_CHECK_VERSION(3, 0, 0)
3712 gtk_widget_set_name(t
->uri_entry
, XT_CSS_NORMAL
);
3713 statusbar_modify_attr(t
, XT_CSS_NORMAL
);
3715 text
= gdk_color_to_string(
3716 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3717 base
= gdk_color_to_string(
3718 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3719 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3720 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3721 statusbar_modify_attr(t
, text
, base
);
3726 #if GTK_CHECK_VERSION(3, 0, 0)
3727 name
= get_css_name(col_str
);
3728 gtk_widget_set_name(t
->uri_entry
, name
);
3729 statusbar_modify_attr(t
, name
);
3732 gdk_color_parse(col_str
, &color
);
3733 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3734 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3738 if (error_str
&& error_str
[0] != '\0')
3739 show_oops(t
, "%s", error_str
);
3744 /* t is invalid at this point */
3746 g_free((gpointer
)u
);
3749 gdk_threads_leave();
3754 check_certs(gpointer p
)
3756 color_address_bar((struct tab
*)p
);
3757 check_cert_changes((struct tab
*)p
, get_uri((struct tab
*)p
));
3761 show_ca_status(struct tab
*t
, const char *uri
)
3763 gchar
*col_str
= XT_COLOR_WHITE
;
3764 #if GTK_CHECK_VERSION(3, 0, 0)
3771 DNPRINTF(XT_D_URL
, "show_ca_status: %d %s %s\n",
3772 ssl_strict_certs
, ssl_ca_file
, uri
);
3777 if (uri
== NULL
|| g_str_has_prefix(uri
, "http://") ||
3778 !g_str_has_prefix(uri
, "https://"))
3783 * It is not necessary to see if the thread is already running.
3784 * If the thread is in progress setting it to something else aborts it
3788 /* thread the coloring of the address bar */
3789 t
->thread
= g_thread_create((GThreadFunc
)check_certs
, t
, TRUE
, NULL
);
3797 if (!strcmp(col_str
, XT_COLOR_WHITE
)) {
3798 #if GTK_CHECK_VERSION(3, 0, 0)
3799 gtk_widget_set_name(t
->uri_entry
, XT_CSS_NORMAL
);
3800 statusbar_modify_attr(t
, XT_CSS_NORMAL
);
3802 text
= gdk_color_to_string(
3803 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3804 base
= gdk_color_to_string(
3805 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3806 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3807 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3808 statusbar_modify_attr(t
, text
, base
);
3813 #if GTK_CHECK_VERSION(3, 0, 0)
3814 name
= get_css_name(col_str
);
3815 gtk_widget_set_name(t
->uri_entry
, name
);
3816 statusbar_modify_attr(t
, name
);
3819 gdk_color_parse(col_str
, &color
);
3820 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3821 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3828 free_favicon(struct tab
*t
)
3830 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p req %p\n",
3831 __func__
, t
->icon_download
, t
->icon_request
);
3833 if (t
->icon_request
)
3834 g_object_unref(t
->icon_request
);
3835 if (t
->icon_dest_uri
)
3836 g_free(t
->icon_dest_uri
);
3838 t
->icon_request
= NULL
;
3839 t
->icon_dest_uri
= NULL
;
3843 xt_icon_from_name(struct tab
*t
, gchar
*name
)
3845 if (!enable_favicon_entry
)
3848 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->uri_entry
),
3849 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3851 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.uri
),
3852 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3854 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.uri
),
3855 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3859 xt_icon_from_pixbuf(struct tab
*t
, GdkPixbuf
*pb
)
3861 GdkPixbuf
*pb_scaled
;
3863 if (gdk_pixbuf_get_width(pb
) > 16 || gdk_pixbuf_get_height(pb
) > 16)
3864 pb_scaled
= gdk_pixbuf_scale_simple(pb
, 16, 16,
3865 GDK_INTERP_BILINEAR
);
3869 if (enable_favicon_entry
) {
3872 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
3873 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3876 if (show_url
== 0) {
3877 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.uri
),
3878 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3880 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.uri
),
3881 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3884 /* XXX: Only supports the minimal tabs atm. */
3885 if (enable_favicon_tabs
)
3886 gtk_image_set_from_pixbuf(GTK_IMAGE(t
->tab_elems
.favicon
),
3889 if (pb_scaled
!= pb
)
3890 g_object_unref(pb_scaled
);
3894 xt_icon_from_file(struct tab
*t
, char *uri
)
3899 if (uri
!= NULL
&& g_str_has_prefix(uri
, "file://"))
3900 file
= g_filename_from_uri(uri
, NULL
, NULL
);
3902 file
= g_strdup(uri
);
3907 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
3909 xt_icon_from_pixbuf(t
, pb
);
3912 xt_icon_from_name(t
, "text-html");
3918 is_valid_icon(char *file
)
3921 const char *mime_type
;
3925 gf
= g_file_new_for_path(file
);
3926 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
3928 mime_type
= g_file_info_get_content_type(fi
);
3929 valid
= g_strcmp0(mime_type
, "image/x-ico") == 0 ||
3930 g_strcmp0(mime_type
, "image/vnd.microsoft.icon") == 0 ||
3931 g_strcmp0(mime_type
, "image/png") == 0 ||
3932 g_strcmp0(mime_type
, "image/gif") == 0 ||
3933 g_strcmp0(mime_type
, "application/octet-stream") == 0;
3941 set_favicon_from_file(struct tab
*t
, char *uri
)
3946 if (t
== NULL
|| uri
== NULL
)
3949 if (uri
!= NULL
&& g_str_has_prefix(uri
, "file://"))
3950 file
= g_filename_from_uri(uri
, NULL
, NULL
);
3952 file
= g_strdup(uri
);
3957 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading %s\n", __func__
, file
);
3959 if (!stat(file
, &sb
)) {
3960 if (sb
.st_size
== 0 || !is_valid_icon(file
)) {
3961 /* corrupt icon so trash it */
3962 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3965 /* no need to set icon to default here */
3969 xt_icon_from_file(t
, file
);
3975 favicon_download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
3978 WebKitDownloadStatus status
= webkit_download_get_status(download
);
3979 struct tab
*tt
= NULL
, *t
= NULL
;
3982 * find the webview instead of passing in the tab as it could have been
3983 * deleted from underneath us.
3985 TAILQ_FOREACH(tt
, &tabs
, entry
) {
3994 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d status %d\n",
3995 __func__
, t
->tab_id
, status
);
3998 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
4000 t
->icon_download
= NULL
;
4003 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
4006 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
4009 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
4011 DNPRINTF(XT_D_DOWNLOAD
, "%s: freeing favicon %d\n",
4012 __func__
, t
->tab_id
);
4013 t
->icon_download
= NULL
;
4016 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
4019 DNPRINTF(XT_D_DOWNLOAD
, "%s: setting icon to %s\n",
4020 __func__
, t
->icon_dest_uri
);
4021 set_favicon_from_file(t
, t
->icon_dest_uri
);
4022 /* these will be freed post callback */
4023 t
->icon_request
= NULL
;
4024 t
->icon_download
= NULL
;
4032 abort_favicon_download(struct tab
*t
)
4034 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p\n", __func__
, t
->icon_download
);
4036 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
4037 if (t
->icon_download
) {
4038 g_signal_handlers_disconnect_by_func(G_OBJECT(t
->icon_download
),
4039 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
4040 webkit_download_cancel(t
->icon_download
);
4041 t
->icon_download
= NULL
;
4046 xt_icon_from_name(t
, "text-html");
4050 notify_icon_loaded_cb(WebKitWebView
*wv
, gchar
*uri
, struct tab
*t
)
4052 DNPRINTF(XT_D_DOWNLOAD
, "%s %s\n", __func__
, uri
);
4054 if (uri
== NULL
|| t
== NULL
)
4057 #if WEBKIT_CHECK_VERSION(1, 4, 0)
4058 /* take icon from WebKitIconDatabase */
4059 GdkPixbuf
*pb
= NULL
;
4061 /* webkit_web_view_get_icon_pixbuf is depreciated in 1.8 */
4062 #if WEBKIT_CHECK_VERSION(1, 8, 0)
4064 * If the page was not loaded (for example, via ssl_strict_certs), do
4065 * not attempt to get the webview's pixbuf. This prevents a CRITICAL
4068 if (wv
&& webkit_web_view_get_uri(wv
))
4069 pb
= webkit_web_view_try_get_favicon_pixbuf(wv
, 0, 0);
4071 if (wv
&& webkit_web_view_get_uri(wv
))
4072 pb
= webkit_web_view_get_icon_pixbuf(wv
);
4075 xt_icon_from_pixbuf(t
, pb
);
4078 xt_icon_from_name(t
, "text-html");
4079 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
4080 /* download icon to cache dir */
4081 gchar
*name_hash
, file
[PATH_MAX
];
4084 if (t
->icon_request
) {
4085 DNPRINTF(XT_D_DOWNLOAD
, "%s: download in progress\n", __func__
);
4089 /* check to see if we got the icon in cache */
4090 name_hash
= g_compute_checksum_for_string(G_CHECKSUM_SHA256
, uri
, -1);
4091 snprintf(file
, sizeof file
, "%s" PS
"%s.ico", cache_dir
, name_hash
);
4094 if (!stat(file
, &sb
)) {
4095 if (sb
.st_size
> 0) {
4096 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading from cache %s\n",
4098 set_favicon_from_file(t
, file
);
4102 /* corrupt icon so trash it */
4103 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
4108 /* create download for icon */
4109 t
->icon_request
= webkit_network_request_new(uri
);
4110 if (t
->icon_request
== NULL
) {
4111 DNPRINTF(XT_D_DOWNLOAD
, "%s: invalid uri %s\n",
4116 t
->icon_download
= webkit_download_new(t
->icon_request
);
4117 if (t
->icon_download
== NULL
)
4120 /* we have to free icon_dest_uri later */
4121 if ((t
->icon_dest_uri
= g_filename_to_uri(file
, NULL
, NULL
)) == NULL
)
4123 webkit_download_set_destination_uri(t
->icon_download
,
4126 if (webkit_download_get_status(t
->icon_download
) ==
4127 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
4128 g_object_unref(t
->icon_request
);
4129 g_free(t
->icon_dest_uri
);
4130 t
->icon_request
= NULL
;
4131 t
->icon_dest_uri
= NULL
;
4135 g_signal_connect(G_OBJECT(t
->icon_download
), "notify::status",
4136 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
4138 webkit_download_start(t
->icon_download
);
4143 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
4145 const gchar
*uri
= NULL
;
4146 struct history
*h
, find
;
4148 #if !GTK_CHECK_VERSION(3, 0, 0)
4152 DNPRINTF(XT_D_URL
, "notify_load_status_cb: %d %s\n",
4153 webkit_web_view_get_load_status(wview
),
4154 get_uri(t
) ? get_uri(t
) : "NOTHING");
4157 show_oops(NULL
, "notify_load_status_cb invalid parameters");
4161 switch (webkit_web_view_get_load_status(wview
)) {
4162 case WEBKIT_LOAD_PROVISIONAL
:
4164 abort_favicon_download(t
);
4165 #if GTK_CHECK_VERSION(2, 20, 0)
4166 gtk_widget_show(t
->spinner
);
4167 gtk_spinner_start(GTK_SPINNER(t
->spinner
));
4169 t
->download_requested
= 0;
4171 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), TRUE
);
4173 /* assume we are a new address */
4174 #if GTK_CHECK_VERSION(3, 0, 0)
4175 gtk_widget_set_name(t
->uri_entry
, XT_CSS_NORMAL
);
4176 statusbar_modify_attr(t
, XT_CSS_NORMAL
);
4178 text
= gdk_color_to_string(
4179 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
4180 base
= gdk_color_to_string(
4181 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
4182 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
4183 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
4184 statusbar_modify_attr(t
, text
, base
);
4189 /* DOM is changing, unreference the previous focused element */
4190 #if WEBKIT_CHECK_VERSION(1, 5, 0)
4192 g_object_unref(t
->active
);
4194 if (t
->active_text
) {
4195 g_free(t
->active_text
);
4196 t
->active_text
= NULL
;
4200 /* take focus if we are visible */
4206 /* kill color thread */
4211 case WEBKIT_LOAD_COMMITTED
:
4216 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
4222 set_status(t
, "Loading: %s", (char *)uri
);
4224 /* check if js white listing is enabled */
4225 if (enable_plugin_whitelist
)
4226 check_and_set_pl(uri
, t
);
4227 if (enable_cookie_whitelist
)
4228 check_and_set_cookie(uri
, t
);
4229 if (enable_js_whitelist
)
4230 check_and_set_js(uri
, t
);
4236 /* we know enough to autosave the session */
4237 if (session_autosave
) {
4242 show_ca_status(t
, uri
);
4243 run_script(t
, JS_HINTING
);
4244 if (enable_autoscroll
)
4245 run_script(t
, JS_AUTOSCROLL
);
4248 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
4250 if (color_visited_uris
) {
4251 color_visited(t
, color_visited_helper());
4253 /* This colors the links you middle-click (open in new
4254 * tab) in the current tab. */
4255 if (t
->tab_id
!= gtk_notebook_get_current_page(notebook
) &&
4256 (uri
= get_uri(t
)) != NULL
)
4257 color_visited(get_current_tab(),
4258 g_strdup_printf("{'%s' : 'dummy'}", uri
));
4262 case WEBKIT_LOAD_FINISHED
:
4264 if ((uri
= get_uri(t
)) == NULL
)
4267 if (!strncmp(uri
, "http://", strlen("http://")) ||
4268 !strncmp(uri
, "https://", strlen("https://")) ||
4269 !strncmp(uri
, "file://", strlen("file://"))) {
4270 find
.uri
= (gchar
*)uri
;
4271 h
= RB_FIND(history_list
, &hl
, &find
);
4273 insert_history_item(uri
,
4274 get_title(t
, FALSE
), time(NULL
));
4276 h
->time
= time(NULL
);
4279 if (statusbar_style
== XT_STATUSBAR_URL
)
4280 set_status(t
, "%s", (char *)uri
);
4282 set_status(t
, "%s", (char *)get_title(t
, FALSE
));
4283 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
4284 #if GTK_CHECK_VERSION(2, 20, 0)
4285 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
4286 gtk_widget_hide(t
->spinner
);
4290 #if WEBKIT_CHECK_VERSION(1, 1, 18)
4291 case WEBKIT_LOAD_FAILED
:
4293 if (!t
->download_requested
) {
4294 gtk_label_set_text(GTK_LABEL(t
->label
),
4295 get_title(t
, FALSE
));
4296 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
),
4297 get_title(t
, FALSE
));
4298 set_status(t
, "%s", (char *)get_title(t
, FALSE
));
4299 gtk_window_set_title(GTK_WINDOW(main_window
),
4300 get_title(t
, TRUE
));
4306 #if GTK_CHECK_VERSION(2, 20, 0)
4307 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
4308 gtk_widget_hide(t
->spinner
);
4310 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
4314 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
),
4315 t
->item
|| webkit_web_view_can_go_back(t
->wv
));
4317 gtk_widget_set_sensitive(GTK_WIDGET(t
->forward
),
4318 webkit_web_view_can_go_forward(t
->wv
));
4322 notify_title_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
4324 const gchar
*title
= NULL
, *win_title
= NULL
;
4326 title
= get_title(t
, FALSE
);
4327 win_title
= get_title(t
, TRUE
);
4329 gtk_label_set_text(GTK_LABEL(t
->label
), title
);
4330 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
), title
);
4333 if (win_title
&& t
->tab_id
== gtk_notebook_get_current_page(notebook
))
4334 gtk_window_set_title(GTK_WINDOW(main_window
), win_title
);
4338 get_domain(const gchar
*host
)
4343 /* handle silly domains like .co.uk */
4345 if ((x
= strlen(host
)) <= 6)
4346 return (g_strdup(host
));
4348 if (host
[x
- 3] == '.' && host
[x
- 6] == '.') {
4354 return (g_strdup(&host
[x
+ 1]));
4358 p
= g_strrstr(host
, ".");
4360 return (g_strdup(""));
4366 return (g_strdup(p
+ 1));
4368 return (g_strdup(host
));
4372 js_autorun(struct tab
*t
)
4376 size_t got_default
= 0, got_host
= 0;
4378 char deff
[PATH_MAX
], hostf
[PATH_MAX
];
4379 char *js
= NULL
, *jsat
, *domain
= NULL
;
4380 FILE *deffile
= NULL
, *hostfile
= NULL
;
4382 if (enable_js_autorun
== 0)
4387 !(g_str_has_prefix(uri
, "http://") ||
4388 g_str_has_prefix(uri
, "https://")))
4391 su
= soup_uri_new(uri
);
4394 if (!SOUP_URI_VALID_FOR_HTTP(su
))
4397 DNPRINTF(XT_D_JS
, "%s: host: %s domain: %s\n", __func__
,
4399 domain
= get_domain(su
->host
);
4401 snprintf(deff
, sizeof deff
, "%s" PS
"default.js", js_dir
);
4402 if ((deffile
= fopen(deff
, "r")) != NULL
) {
4403 if (fstat(fileno(deffile
), &sb
) == -1) {
4404 show_oops(t
, "can't stat default JS file");
4407 got_default
= sb
.st_size
;
4410 /* try host first followed by domain */
4411 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, su
->host
);
4412 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4413 if ((hostfile
= fopen(hostf
, "r")) == NULL
) {
4414 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, domain
);
4415 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4416 if ((hostfile
= fopen(hostf
, "r")) == NULL
)
4419 DNPRINTF(XT_D_JS
, "file: %s\n", hostf
);
4420 if (fstat(fileno(hostfile
), &sb
) == -1) {
4421 show_oops(t
, "can't stat %s JS file", hostf
);
4424 got_host
= sb
.st_size
;
4427 if (got_default
+ got_host
== 0)
4430 js
= g_malloc0(got_default
+ got_host
+ 1);
4434 if (fread(js
, got_default
, 1, deffile
) != 1) {
4435 show_oops(t
, "default file read error");
4438 jsat
= js
+ got_default
;
4442 if (fread(jsat
, got_host
, 1, hostfile
) != 1) {
4443 show_oops(t
, "host file read error");
4448 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
4449 run_script_locked(t
, js
);
4465 webview_load_finished_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4467 /* autorun some js if enabled */
4475 webview_progress_changed_cb(WebKitWebView
*wv
, int progress
, struct tab
*t
)
4477 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.uri
),
4478 progress
== 100 ? 0 : (double)progress
/ 100);
4479 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->uri_entry
),
4480 progress
== 100 ? 0 : (double)progress
/ 100);
4482 update_statusbar_position(NULL
, NULL
);
4486 strict_transport_rb_cmp(struct strict_transport
*a
, struct strict_transport
*b
)
4491 /* compare strings from the end */
4492 l1
= strlen(a
->host
);
4493 l2
= strlen(b
->host
);
4497 for (; *p1
== *p2
&& p1
> a
->host
&& p2
> b
->host
;
4502 * Check if we need to do pattern expansion,
4503 * or if we're just keeping the tree in order
4505 if (a
->flags
& XT_STS_FLAGS_EXPAND
&&
4506 b
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) {
4507 /* Check if we're matching the
4508 * 'host.xyz' part in '*.host.xyz'
4510 if (p2
== b
->host
&& (p1
== a
->host
|| *(p1
-1) == '.')) {
4515 if (p1
== a
->host
&& p2
== b
->host
)
4529 RB_GENERATE(strict_transport_tree
, strict_transport
, entry
,
4530 strict_transport_rb_cmp
);
4533 strict_transport_add(const char *domain
, time_t timeout
, int subdomains
)
4535 struct strict_transport
*d
, find
;
4539 if (enable_strict_transport
== FALSE
)
4542 DPRINTF("strict_transport_add(%s,%" PRIi64
",%d)\n", domain
,
4543 (uint64_t)timeout
, subdomains
);
4549 find
.host
= (char *)domain
;
4551 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4555 /* check if update is needed */
4556 if (d
->timeout
== timeout
&&
4557 (d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) == subdomains
)
4560 d
->timeout
= timeout
;
4562 d
->flags
|= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4564 /* We're still initializing */
4565 if (strict_transport_file
== NULL
)
4568 if ((f
= fopen(strict_transport_file
, "w")) == NULL
) {
4570 "can't open strict-transport rules file");
4574 fprintf(f
, "# Generated file - do not update unless you know "
4575 "what you're doing\n");
4576 RB_FOREACH(d
, strict_transport_tree
, &st_tree
) {
4577 if (d
->timeout
< now
)
4579 fprintf(f
, "%s\t%" PRIi64
"\t%d\n", d
->host
,
4580 (uint64_t)d
->timeout
,
4581 d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
);
4585 d
= g_malloc(sizeof *d
);
4586 d
->host
= g_strdup(domain
);
4587 d
->timeout
= timeout
;
4589 d
->flags
= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4592 RB_INSERT(strict_transport_tree
, &st_tree
, d
);
4594 /* We're still initializing */
4595 if (strict_transport_file
== NULL
)
4598 if ((f
= fopen(strict_transport_file
, "a+")) == NULL
) {
4600 "can't open strict-transport rules file");
4604 fseek(f
, 0, SEEK_END
);
4605 fprintf(f
,"%s\t%" PRIi64
"\t%d\n", d
->host
, (uint64_t)timeout
,
4613 strict_transport_check(const char *host
)
4615 static struct strict_transport
*d
= NULL
;
4616 struct strict_transport find
;
4618 if (enable_strict_transport
== FALSE
)
4621 find
.host
= (char *)host
;
4623 /* match for domains that include subdomains */
4624 find
.flags
= XT_STS_FLAGS_EXPAND
;
4626 /* First, check if we're already at the right node */
4627 if (d
!= NULL
&& strict_transport_rb_cmp(&find
, d
) == 0) {
4631 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4639 strict_transport_init()
4641 char file
[PATH_MAX
];
4647 time_t timeout
, now
;
4650 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_STS_FILE
);
4651 if ((f
= fopen(file
, "r")) == NULL
) {
4652 strict_transport_file
= g_strdup(file
);
4663 if ((rule
= fparseln(f
, &len
, NULL
, delim
, 0)) == NULL
) {
4664 if (!feof(f
) || ferror(f
))
4670 /* get second entry */
4671 if ((ptr
= strpbrk(rule
, " \t")) == NULL
)
4675 timeout
= atoi(ptr
);
4677 /* get third entry */
4678 if ((ptr
= strpbrk(ptr
, " \t")) == NULL
)
4682 subdomains
= atoi(ptr
);
4685 strict_transport_add(rule
, timeout
, subdomains
);
4690 strict_transport_file
= g_strdup(file
);
4694 startpage_add("strict-transport rules file ('%s') is corrupt", file
);
4702 force_https_check(const char *uri
)
4704 struct domain
*d
= NULL
;
4709 if ((d
= wl_find_uri(uri
, &force_https
)) == NULL
)
4716 strict_transport_security_cb(SoupMessage
*msg
, gpointer data
)
4722 int subdomains
= FALSE
;
4727 sts
= soup_message_headers_get_one(msg
->response_headers
,
4728 "Strict-Transport-Security");
4729 uri
= soup_message_get_uri(msg
);
4731 if (sts
== NULL
|| uri
== NULL
)
4734 if ((ptr
= strcasestr(sts
, "max-age="))) {
4735 ptr
+= strlen("max-age=");
4736 timeout
= atoll(ptr
);
4738 return; /* malformed header - max-age must be included */
4740 if ((ptr
= strcasestr(sts
, "includeSubDomains")))
4743 strict_transport_add(uri
->host
, timeout
+ time(NULL
), subdomains
);
4747 session_rq_cb(SoupSession
*s
, SoupMessage
*msg
, SoupSocket
*socket
,
4757 if (s
== NULL
|| msg
== NULL
)
4760 if (enable_strict_transport
) {
4761 soup_message_add_header_handler(msg
, "finished",
4762 "Strict-Transport-Security",
4763 G_CALLBACK(strict_transport_security_cb
), NULL
);
4766 if (referer_mode
== XT_REFERER_ALWAYS
)
4769 /* Check if referer is set - and what the user requested for referers */
4770 ref
= soup_message_headers_get_one(msg
->request_headers
, "Referer");
4772 DNPRINTF(XT_D_NAV
, "session_rq_cb: Referer: %s\n", ref
);
4773 switch (referer_mode
) {
4774 case XT_REFERER_NEVER
:
4775 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing referer\n");
4776 soup_message_headers_remove(msg
->request_headers
,
4779 case XT_REFERER_SAME_DOMAIN
:
4780 ref_uri
= soup_uri_new(ref
);
4781 dest
= soup_message_get_uri(msg
);
4783 ref_suffix
= tld_get_suffix(ref_uri
->host
);
4784 dest_suffix
= tld_get_suffix(dest
->host
);
4786 if (dest
&& ref_suffix
&& dest_suffix
&&
4787 strcmp(ref_suffix
, dest_suffix
) != 0) {
4788 soup_message_headers_remove(msg
->request_headers
,
4790 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4791 "referer (not same domain) (suffixes: %s - %s)\n",
4792 ref_suffix
, dest_suffix
);
4794 soup_uri_free(ref_uri
);
4796 case XT_REFERER_SAME_FQDN
:
4797 ref_uri
= soup_uri_new(ref
);
4798 dest
= soup_message_get_uri(msg
);
4799 if (dest
&& strcmp(ref_uri
->host
, dest
->host
) != 0) {
4800 soup_message_headers_remove(msg
->request_headers
,
4802 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4803 "referer (not same fqdn) (should be %s)\n",
4806 soup_uri_free(ref_uri
);
4808 case XT_REFERER_CUSTOM
:
4809 DNPRINTF(XT_D_NAV
, "session_rq_cb: setting referer "
4810 "to %s\n", referer_custom
);
4811 soup_message_headers_replace(msg
->request_headers
,
4812 "Referer", referer_custom
);
4819 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
4820 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
4821 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
4823 WebKitWebNavigationReason reason
;
4824 struct user_agent ua_find
, *ua
;
4825 struct domain
*d
= NULL
;
4829 show_oops(NULL
, "webview_npd_cb invalid parameters");
4833 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
4835 webkit_network_request_get_uri(request
));
4837 uri
= (char *)webkit_network_request_get_uri(request
);
4839 /* clear t->item, except if we're switching to an about: page */
4840 if (t
->item
&& !g_str_has_prefix(uri
, "xxxt://")) {
4841 g_object_unref(t
->item
);
4845 if (!auto_load_images
&& t
->load_images
) {
4847 /* Disable autoloading of images, now that we're done loading
4849 g_object_set(G_OBJECT(t
->settings
),
4850 "auto-load-images", FALSE
, (char *)NULL
);
4851 webkit_web_view_set_settings(t
->wv
, t
->settings
);
4853 t
->load_images
= FALSE
;
4856 /* If this is an xtp url, we don't load anything else. */
4857 if (parse_xtp_url(t
, uri
))
4860 if (parse_custom_uri(t
, uri
))
4863 if ((t
->mode
== XT_MODE_HINT
&& t
->new_tab
) || t
->ctrl_click
) {
4865 create_new_tab(uri
, NULL
, ctrl_click_focus
, -1);
4866 webkit_web_policy_decision_ignore(pd
);
4867 return (TRUE
); /* we made the decission */
4870 /* Change user agent if more than one has been given. */
4871 if (!RB_EMPTY(&ua_list
)) {
4872 ua_find
.id
= t
->user_agent_id
;
4874 if ((ua
= RB_FIND(user_agent_list
, &ua_list
, &ua_find
)) == NULL
) {
4876 t
->user_agent_id
= 1;
4877 user_agent
= RB_FIND(user_agent_list
, &ua_list
, &ua_find
);
4883 g_free(t
->user_agent
);
4884 t
->user_agent
= g_strdup(user_agent
->value
);
4886 DNPRINTF(XT_D_NAV
, "user-agent: %s\n", t
->user_agent
);
4888 g_object_set(G_OBJECT(t
->settings
),
4889 "user-agent", t
->user_agent
, (char *)NULL
);
4891 webkit_web_view_set_settings(wv
, t
->settings
);
4895 * This is a little hairy but it comes down to this:
4896 * when we run in whitelist mode we have to assist the browser in
4897 * opening the URL that it would have opened in a new tab.
4899 reason
= webkit_web_navigation_action_get_reason(na
);
4900 if (reason
== WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED
) {
4901 set_normal_tab_meaning(t
);
4902 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1)
4903 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4905 webkit_web_policy_decision_use(pd
);
4906 return (TRUE
); /* we made the decision */
4913 webview_rrs_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, WebKitWebResource
*res
,
4914 WebKitNetworkRequest
*request
, WebKitNetworkResponse
*response
,
4917 SoupMessage
*msg
= NULL
;
4918 SoupURI
*uri
= NULL
;
4919 struct http_accept ha_find
, *ha
= NULL
;
4920 const char *accept
= NULL
;
4923 msg
= webkit_network_request_get_message(request
);
4926 uri
= soup_message_get_uri(msg
);
4929 uri_s
= soup_uri_to_string(uri
, FALSE
);
4931 if (strcmp(uri
->scheme
, SOUP_URI_SCHEME_HTTP
) == 0) {
4932 if (strict_transport_check(uri
->host
) ||
4933 force_https_check(uri_s
)) {
4934 DNPRINTF(XT_D_NAV
, "webview_rrs_cb: force https for %s\n",
4936 soup_uri_set_scheme(uri
, SOUP_URI_SCHEME_HTTPS
);
4941 soup_message_headers_append(msg
->request_headers
, "DNT", "1");
4943 /* Round-robin through HTTP Accept headers if any have been set */
4944 if (!RB_EMPTY(&ha_list
)) {
4945 accept
= soup_message_headers_get_list(msg
->request_headers
,
4947 if (accept
== NULL
||
4948 strncmp(accept
, "text/html", strlen("text/html")))
4951 ha_find
.id
= t
->http_accept_id
;
4952 ha
= RB_FIND(http_accept_list
, &ha_list
, &ha_find
);
4955 t
->http_accept_id
= 1;
4956 http_accept
= RB_FIND(http_accept_list
, &ha_list
,
4959 ++t
->http_accept_id
;
4963 soup_message_headers_replace(msg
->request_headers
, "Accept",
4964 http_accept
->value
);
4972 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4975 struct domain
*d
= NULL
;
4977 WebKitWebView
*webview
= NULL
;
4980 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
4981 webkit_web_view_get_uri(wv
));
4984 /* open in current tab */
4986 } else if (enable_scripts
== 0 && enable_js_whitelist
== 1) {
4987 uri
= webkit_web_view_get_uri(wv
);
4988 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4991 if (t
->ctrl_click
) {
4992 x
= ctrl_click_focus
;
4995 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4997 } else if (enable_scripts
== 1) {
4998 if (t
->ctrl_click
) {
4999 x
= ctrl_click_focus
;
5002 tt
= create_new_tab(NULL
, NULL
, x
, -1);
5010 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
5013 struct domain
*d
= NULL
;
5015 DNPRINTF(XT_D_NAV
, "webview_close_cb: %d\n", t
->tab_id
);
5017 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
5018 uri
= webkit_web_view_get_uri(wv
);
5019 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
5023 } else if (enable_scripts
== 1)
5030 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
5032 /* we can not eat the event without throwing gtk off so defer it */
5034 /* catch middle click */
5035 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 2) {
5040 /* catch ctrl click */
5041 if (e
->type
== GDK_BUTTON_RELEASE
&&
5042 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
5047 return (XT_CB_PASSTHROUGH
);
5051 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
5053 struct mime_type
*m
;
5055 GError
*gerr
= NULL
;
5057 m
= find_mime_type(mime_type
);
5063 sv
[0] = m
->mt_action
;
5064 sv
[1] = (char *)webkit_network_request_get_uri(request
);
5067 /* ignore donothing from example config */
5068 if (m
->mt_action
&& !strcmp(m
->mt_action
, "donothing"))
5071 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
, NULL
,
5073 show_oops(t
, "%s: could not spawn process (%s)", __func__
,
5074 gerr
? gerr
->message
: "N/A");
5079 get_mime_type(const char *uri
)
5084 char *mime_type
= NULL
;
5088 show_oops(NULL
, "%s: invalid parameters", __func__
);
5092 if (g_str_has_prefix(uri
, "file://"))
5093 file
= g_filename_from_uri(uri
, NULL
, NULL
);
5095 file
= g_strdup(uri
);
5100 gf
= g_file_new_for_path(file
);
5101 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
5103 if ((m
= g_file_info_get_content_type(fi
)) != NULL
)
5104 mime_type
= g_strdup(m
);
5113 run_download_mimehandler(char *mime_type
, char *file
)
5115 struct mime_type
*m
;
5118 m
= find_mime_type(mime_type
);
5122 sv
[0] = m
->mt_action
;
5125 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
, NULL
,
5127 show_oops(NULL
, "%s: could not spawn process: %s %s", __func__
,
5135 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
5138 WebKitDownloadStatus status
;
5143 if (download
== NULL
)
5145 status
= webkit_download_get_status(download
);
5146 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
5149 if (download_notifications
)
5150 show_oops(NULL
, "Download of '%s' finished",
5151 basename((char *)webkit_download_get_destination_uri(download
)));
5152 uri
= webkit_download_get_destination_uri(download
);
5155 mime
= get_mime_type(uri
);
5159 if (g_str_has_prefix(uri
, "file://"))
5160 file
= g_filename_from_uri(uri
, NULL
, NULL
);
5162 file
= g_strdup(uri
);
5167 run_download_mimehandler((char *)mime
, file
);
5174 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
5175 WebKitNetworkRequest
*request
, char *mime_type
,
5176 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
5179 show_oops(NULL
, "webview_mimetype_cb invalid parameters");
5183 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
5184 t
->tab_id
, mime_type
);
5186 if (run_mimehandler(t
, mime_type
, request
) == 0) {
5187 webkit_web_policy_decision_ignore(decision
);
5192 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
5193 webkit_web_policy_decision_download(decision
);
5201 download_start(struct tab
*t
, struct download
*d
, int flag
)
5203 WebKitNetworkRequest
*req
;
5205 const gchar
*suggested_name
;
5206 gchar
*filename
= NULL
;
5212 if (d
== NULL
|| t
== NULL
) {
5213 show_oops(NULL
, "%s invalid parameters", __func__
);
5217 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
5218 if (suggested_name
== NULL
)
5219 return (FALSE
); /* abort download */
5230 filename
= g_strdup_printf("%d%s", i
, suggested_name
);
5233 /* XXX using urls doesn't work properly in windows? */
5234 uri
= g_strdup_printf("%s\\%s", download_dir
, i
?
5235 filename
: suggested_name
);
5237 path
= g_strdup_printf("%s" PS
"%s", download_dir
, i
?
5238 filename
: suggested_name
);
5239 if ((uri
= g_filename_to_uri(path
, NULL
, NULL
)) == NULL
)
5244 } while (!stat(uri
, &sb
));
5246 } while (!stat(path
, &sb
));
5249 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d filename %s "
5250 "local %s\n", __func__
, t
->tab_id
, filename
, uri
);
5252 /* if we're restarting the download, or starting
5253 * it after doing something else, we need to recreate
5254 * the download request.
5256 if (flag
== XT_DL_RESTART
) {
5257 req
= webkit_network_request_new(webkit_download_get_uri(d
->download
));
5258 webkit_download_cancel(d
->download
);
5259 g_object_unref(d
->download
);
5260 d
->download
= webkit_download_new(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 show_oops(t
, "Download of '%s' started...",
5278 basename((char *)webkit_download_get_destination_uri(d
->download
)));
5281 if (flag
!= XT_DL_START
)
5282 webkit_download_start(d
->download
);
5284 DNPRINTF(XT_D_DOWNLOAD
, "download status : %d",
5285 webkit_download_get_status(d
->download
));
5287 /* sync other download manager tabs */
5288 update_download_tabs(NULL
);
5301 download_ask_cb(struct tab
*t
, GdkEventKey
*e
, gpointer data
)
5303 struct download
*d
= data
;
5307 t
->mode_cb_data
= NULL
;
5310 e
->keyval
= GDK_Escape
;
5311 return (XT_CB_PASSTHROUGH
);
5314 DPRINTF("download_ask_cb: User pressed %c\n", e
->keyval
);
5315 if (e
->keyval
== 'y' || e
->keyval
== 'Y' || e
->keyval
== GDK_Return
)
5316 /* We need to do a RESTART, because we're not calling from
5317 * webview_download_cb
5319 download_start(t
, d
, XT_DL_RESTART
);
5321 /* for all other keyvals, we just let the download be */
5322 e
->keyval
= GDK_Escape
;
5323 return (XT_CB_HANDLED
);
5327 download_ask(struct tab
*t
, struct download
*d
)
5329 const gchar
*suggested_name
;
5331 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
5332 if (suggested_name
== NULL
)
5333 return (FALSE
); /* abort download */
5335 show_oops(t
, "download file %s [y/n] ?", suggested_name
);
5336 t
->mode_cb
= download_ask_cb
;
5337 t
->mode_cb_data
= d
;
5343 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
5346 const gchar
*suggested_name
;
5347 struct download
*download_entry
;
5350 if (wk_download
== NULL
|| t
== NULL
) {
5351 show_oops(NULL
, "%s invalid parameters", __func__
);
5355 suggested_name
= webkit_download_get_suggested_filename(wk_download
);
5356 if (suggested_name
== NULL
)
5357 return (FALSE
); /* abort download */
5359 download_entry
= g_malloc(sizeof(struct download
));
5360 download_entry
->download
= wk_download
;
5361 download_entry
->tab
= t
;
5362 download_entry
->id
= next_download_id
++;
5363 RB_INSERT(download_list
, &downloads
, download_entry
);
5364 t
->download_requested
= 1;
5366 if (download_mode
== XT_DM_START
)
5367 ret
= download_start(t
, download_entry
, XT_DL_START
);
5368 else if (download_mode
== XT_DM_ASK
)
5369 ret
= download_ask(t
, download_entry
);
5370 else if (download_mode
== XT_DM_ADD
)
5371 show_oops(t
, "added %s to download manager",
5374 /* sync other download manager tabs */
5375 update_download_tabs(NULL
);
5378 * NOTE: never redirect/render the current tab before this
5379 * function returns. This will cause the download to never start.
5381 return (ret
); /* start download */
5385 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
5387 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
5390 show_oops(NULL
, "webview_hover_cb");
5395 set_status(t
, "Link: %s", uri
);
5397 if (statusbar_style
== XT_STATUSBAR_URL
) {
5398 const gchar
*page_uri
;
5400 if ((page_uri
= get_uri(t
)) != NULL
)
5401 set_status(t
, "%s", page_uri
);
5403 set_status(t
, "%s", (char *)get_title(t
, FALSE
));
5408 mark(struct tab
*t
, struct karg
*arg
)
5415 if ((index
= marktoindex(mark
)) == -1)
5418 if (arg
->i
== XT_MARK_SET
)
5419 t
->mark
[index
] = gtk_adjustment_get_value(t
->adjust_v
);
5420 else if (arg
->i
== XT_MARK_GOTO
) {
5421 if (t
->mark
[index
] == XT_INVALID_MARK
) {
5422 show_oops(t
, "mark '%c' does not exist", mark
);
5425 /* XXX t->mark[index] can be bigger than the maximum if ajax or
5426 something changes the document size */
5427 pos
= gtk_adjustment_get_value(t
->adjust_v
);
5428 gtk_adjustment_set_value(t
->adjust_v
, t
->mark
[index
]);
5429 t
->mark
[marktoindex('\'')] = pos
;
5436 marks_clear(struct tab
*t
)
5440 for (i
= 0; i
< LENGTH(t
->mark
); i
++)
5441 t
->mark
[i
] = XT_INVALID_MARK
;
5447 char file
[PATH_MAX
];
5448 char *line
= NULL
, *p
;
5453 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
5454 if ((f
= fopen(file
, "r+")) == NULL
) {
5455 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
5459 for (i
= 1; ; i
++) {
5460 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
5462 if (strlen(line
) == 0 || line
[0] == '#') {
5468 p
= strtok(line
, " \t");
5470 if (p
== NULL
|| strlen(p
) != 1 ||
5471 (index
= qmarktoindex(*p
)) == -1) {
5472 warnx("corrupt quickmarks file, line %d", i
);
5476 p
= strtok(NULL
, " \t");
5477 if (qmarks
[index
] != NULL
)
5478 g_free(qmarks
[index
]);
5479 qmarks
[index
] = g_strdup(p
);
5490 char file
[PATH_MAX
];
5494 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
5495 if ((f
= fopen(file
, "r+")) == NULL
) {
5496 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
5500 for (i
= 0; i
< XT_NOQMARKS
; i
++)
5501 if (qmarks
[i
] != NULL
)
5502 fprintf(f
, "%c %s\n", indextoqmark(i
), qmarks
[i
]);
5510 qmark(struct tab
*t
, struct karg
*arg
)
5515 mark
= arg
->s
[strlen(arg
->s
)-1];
5516 index
= qmarktoindex(mark
);
5522 if (qmarks
[index
] != NULL
) {
5523 g_free(qmarks
[index
]);
5524 qmarks
[index
] = NULL
;
5527 qmarks_load(); /* sync if multiple instances */
5528 qmarks
[index
] = g_strdup(get_uri(t
));
5532 if (qmarks
[index
] != NULL
)
5533 load_uri(t
, qmarks
[index
]);
5535 show_oops(t
, "quickmark \"%c\" does not exist",
5541 if (qmarks
[index
] != NULL
)
5542 create_new_tab(qmarks
[index
], NULL
, 1, -1);
5544 show_oops(t
, "quickmark \"%c\" does not exist",
5555 go_up(struct tab
*t
, struct karg
*args
)
5563 if (args
->i
== XT_GO_UP_ROOT
)
5564 levels
= XT_GO_UP_ROOT
;
5565 else if ((levels
= atoi(args
->s
)) == 0)
5568 uri
= g_strdup(get_uri(t
));
5572 if ((tmp
= strstr(uri
, XT_PROTO_DELIM
)) == NULL
)
5575 tmp
+= strlen(XT_PROTO_DELIM
);
5577 /* it makes no sense to strip the last slash from ".../dir/", skip it */
5578 lastidx
= strlen(tmp
) - 1;
5580 if (tmp
[lastidx
] == '/')
5581 tmp
[lastidx
] = '\0';
5585 p
= strrchr(tmp
, '/');
5586 if (p
== tmp
) { /* Are we at the root of a file://-path? */
5589 } else if (p
!= NULL
)
5602 gototab(struct tab
*t
, struct karg
*args
)
5605 struct karg arg
= {0, NULL
, -1};
5607 tab
= atoi(args
->s
);
5610 arg
.i
= XT_TAB_NEXT
;
5622 zoom_amount(struct tab
*t
, struct karg
*arg
)
5624 struct karg narg
= {0, NULL
, -1};
5626 narg
.i
= atoi(arg
->s
);
5627 resizetab(t
, &narg
);
5633 flip_colon(struct tab
*t
, struct karg
*arg
)
5635 struct karg narg
= {0, NULL
, -1};
5638 if (t
== NULL
|| arg
== NULL
)
5641 p
= strstr(arg
->s
, ":");
5653 /* buffer commands receive the regex that triggered them in arg.s */
5654 char bcmd
[XT_BUFCMD_SZ
];
5658 #define XT_PRE_NO (0)
5659 #define XT_PRE_YES (1)
5660 #define XT_PRE_MAYBE (2)
5662 int (*func
)(struct tab
*, struct karg
*);
5666 { "^[0-9]*gu$", XT_PRE_MAYBE
, "gu", go_up
, 0 },
5667 { "^gU$", XT_PRE_NO
, "gU", go_up
, XT_GO_UP_ROOT
},
5668 { "^gg$", XT_PRE_NO
, "gg", move
, XT_MOVE_TOP
},
5669 { "^gG$", XT_PRE_NO
, "gG", move
, XT_MOVE_BOTTOM
},
5670 { "^[0-9]+%$", XT_PRE_YES
, "%", move
, XT_MOVE_PERCENT
},
5671 { "^zz$", XT_PRE_NO
, "zz", move
, XT_MOVE_CENTER
},
5672 { "^gh$", XT_PRE_NO
, "gh", go_home
, 0 },
5673 { "^m[a-zA-Z0-9]$", XT_PRE_NO
, "m", mark
, XT_MARK_SET
},
5674 { "^['][a-zA-Z0-9']$", XT_PRE_NO
, "'", mark
, XT_MARK_GOTO
},
5675 { "^[0-9]+t$", XT_PRE_YES
, "t", gototab
, 0 },
5676 { "^g0$", XT_PRE_YES
, "g0", movetab
, XT_TAB_FIRST
},
5677 { "^g[$]$", XT_PRE_YES
, "g$", movetab
, XT_TAB_LAST
},
5678 { "^[0-9]*gt$", XT_PRE_YES
, "t", movetab
, XT_TAB_NEXT
},
5679 { "^[0-9]*gT$", XT_PRE_YES
, "T", movetab
, XT_TAB_PREV
},
5680 { "^M[a-zA-Z0-9]$", XT_PRE_NO
, "M", qmark
, XT_QMARK_SET
},
5681 { "^go[a-zA-Z0-9]$", XT_PRE_NO
, "go", qmark
, XT_QMARK_OPEN
},
5682 { "^gn[a-zA-Z0-9]$", XT_PRE_NO
, "gn", qmark
, XT_QMARK_TAB
},
5683 { "^ZR$", XT_PRE_NO
, "ZR", restart
, 0 },
5684 { "^ZZ$", XT_PRE_NO
, "ZZ", quit
, 0 },
5685 { "^zi$", XT_PRE_NO
, "zi", resizetab
, XT_ZOOM_IN
},
5686 { "^zo$", XT_PRE_NO
, "zo", resizetab
, XT_ZOOM_OUT
},
5687 { "^z0$", XT_PRE_NO
, "z0", resizetab
, XT_ZOOM_NORMAL
},
5688 { "^[0-9]+Z$", XT_PRE_YES
, "Z", zoom_amount
, 0 },
5689 { "^[0-9]+:$", XT_PRE_YES
, ":", flip_colon
, 0 },
5693 buffercmd_init(void)
5697 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5698 if (regcomp(&buffercmds
[i
].cregex
, buffercmds
[i
].regex
,
5699 REG_EXTENDED
| REG_NOSUB
))
5700 startpage_add("invalid buffercmd regex %s",
5701 buffercmds
[i
].regex
);
5705 buffercmd_abort(struct tab
*t
)
5712 DNPRINTF(XT_D_BUFFERCMD
, "%s: clearing buffer\n", __func__
);
5714 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5717 cmd_prefix
= 0; /* clear prefix for non-buffer commands */
5718 gtk_label_set_text(GTK_LABEL(t
->sbe
.buffercmd
), bcmd
);
5722 buffercmd_execute(struct tab
*t
, struct buffercmd
*cmd
)
5724 struct karg arg
= {0, NULL
, -1};
5727 arg
.s
= g_strdup(bcmd
);
5729 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_execute: buffer \"%s\" "
5730 "matches regex \"%s\", executing\n", bcmd
, cmd
->regex
);
5740 buffercmd_addkey(struct tab
*t
, guint keyval
)
5743 char s
[XT_BUFCMD_SZ
];
5745 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
))) {
5747 return (XT_CB_PASSTHROUGH
);
5750 if (keyval
== GDK_Escape
) {
5752 return (XT_CB_HANDLED
);
5755 /* key with modifier or non-ascii character */
5756 if (!isascii(keyval
)) {
5758 * XXX this looks wrong but fixes some sites like
5759 * http://www.seslisozluk.com/
5760 * that eat a shift or ctrl and end putting default focus in js
5761 * instead of ignoring the keystroke
5762 * so instead of return (XT_CB_PASSTHROUGH); eat the key
5764 return (XT_CB_HANDLED
);
5767 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: adding key \"%c\" "
5768 "to buffer \"%s\"\n", keyval
, bcmd
);
5770 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5771 if (bcmd
[i
] == '\0') {
5776 /* buffer full, ignore input */
5777 if (i
>= LENGTH(bcmd
) -1) {
5778 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: buffer full\n");
5780 return (XT_CB_HANDLED
);
5783 gtk_label_set_text(GTK_LABEL(t
->sbe
.buffercmd
), bcmd
);
5785 /* find exact match */
5786 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5787 if (regexec(&buffercmds
[i
].cregex
, bcmd
,
5788 (size_t) 0, NULL
, 0) == 0) {
5789 buffercmd_execute(t
, &buffercmds
[i
]);
5793 /* find non exact matches to see if we need to abort ot not */
5794 for (i
= 0, match
= 0; i
< LENGTH(buffercmds
); i
++) {
5795 DNPRINTF(XT_D_BUFFERCMD
, "trying: %s\n", bcmd
);
5798 if (buffercmds
[i
].precount
== XT_PRE_MAYBE
) {
5799 if (isdigit(bcmd
[0])) {
5800 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5804 if (sscanf(bcmd
, "%s", s
) == 0)
5807 } else if (buffercmds
[i
].precount
== XT_PRE_YES
) {
5808 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5811 if (sscanf(bcmd
, "%s", s
) == 0)
5814 if (c
== -1 && buffercmds
[i
].precount
)
5816 if (!strncmp(s
, buffercmds
[i
].cmd
, strlen(s
)))
5819 DNPRINTF(XT_D_BUFFERCMD
, "got[%d] %d <%s>: %d %s\n",
5820 i
, match
, buffercmds
[i
].cmd
, c
, s
);
5823 DNPRINTF(XT_D_BUFFERCMD
, "aborting: %s\n", bcmd
);
5828 return (XT_CB_HANDLED
);
5832 * XXX we were seeing a bunch of focus issues with the toplevel
5833 * main_window losing its is-active and has-toplevel-focus properties.
5834 * This is the most correct and portable solution we could come up with
5835 * without relying on calling internal GTK functions (which we
5836 * couldn't link to in Linux).
5839 fake_focus_in(GtkWidget
*w
)
5841 if (fevent
== NULL
) {
5842 fevent
= gdk_event_new(GDK_FOCUS_CHANGE
);
5843 fevent
->focus_change
.window
=
5844 gtk_widget_get_window(main_window
);
5845 fevent
->focus_change
.type
= GDK_FOCUS_CHANGE
;
5846 fevent
->focus_change
.in
= TRUE
;
5848 gtk_widget_send_focus_change(main_window
, fevent
);
5852 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
5855 struct key_binding
*k
;
5858 * This sometimes gets randomly unset for whatever reason in GTK3.
5859 * If we're handling a keypress, the main window's is-active propery
5860 * *must* be true, or else many things will break.
5862 #if GTK_CHECK_VERSION(3, 0, 0)
5863 fake_focus_in(main_window
);
5866 /* handle keybindings if buffercmd is empty.
5867 if not empty, allow commands like C-n */
5868 if (bcmd
[0] == '\0' || ((e
->state
& (CTRL
| MOD1
)) != 0))
5869 TAILQ_FOREACH(k
, &kbl
, entry
)
5870 if (e
->keyval
== k
->key
5871 && (entry
? k
->use_in_entry
: 1)) {
5872 /* when we are edditing eat ctrl/mod keys */
5873 if (edit_mode
== XT_EM_VI
&&
5874 t
->mode
== XT_MODE_INSERT
&&
5875 (e
->state
& CTRL
|| e
->state
& MOD1
))
5876 return (XT_CB_PASSTHROUGH
);
5879 if ((e
->state
& (CTRL
| MOD1
)) == 0)
5881 } else if ((e
->state
& k
->mask
) == k
->mask
) {
5886 if (!entry
&& ((e
->state
& (CTRL
| MOD1
)) == 0))
5887 return (buffercmd_addkey(t
, e
->keyval
));
5889 return (XT_CB_PASSTHROUGH
);
5892 if (k
->cmd
[0] == ':') {
5894 args
.s
= &k
->cmd
[1];
5895 return (command(t
, &args
));
5897 return (cmd_execute(t
, k
->cmd
));
5901 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5905 /* don't use w directly; use t->whatever instead */
5908 show_oops(NULL
, "wv_keypress_cb");
5909 return (XT_CB_PASSTHROUGH
);
5915 return (t
->mode_cb(t
, e
, t
->mode_cb_data
));
5917 DNPRINTF(XT_D_KEY
, "wv_keypress_cb: mode %d keyval 0x%x mask "
5918 "0x%x tab %d\n", t
->mode
, e
->keyval
, e
->state
, t
->tab_id
);
5920 /* Hide buffers, if they are visible, with escape. */
5921 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)) &&
5922 CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
5923 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5925 return (XT_CB_HANDLED
);
5928 if (t
->mode
== XT_MODE_HINT
)
5929 return (XT_CB_HANDLED
);
5931 if ((CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Tab
) ||
5932 (CLEAN(e
->state
) == SHFT
&& e
->keyval
== GDK_Tab
))
5933 /* something focussy is about to happen */
5934 return (XT_CB_PASSTHROUGH
);
5936 /* check if we are some sort of text input thing in the dom */
5937 input_check_mode(t
);
5939 if (t
->mode
== XT_MODE_HINT
) {
5940 /* XXX make sure cmd entry is enabled */
5941 return (XT_CB_HANDLED
);
5942 } else if (t
->mode
== XT_MODE_PASSTHROUGH
) {
5943 if (CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
)
5944 t
->mode
= XT_MODE_COMMAND
;
5945 return (XT_CB_PASSTHROUGH
);
5946 } else if (t
->mode
== XT_MODE_COMMAND
) {
5948 snprintf(s
, sizeof s
, "%c", e
->keyval
);
5949 if (CLEAN(e
->state
) == 0 && isdigit(s
[0]))
5950 cmd_prefix
= 10 * cmd_prefix
+ atoi(s
);
5951 return (handle_keypress(t
, e
, 0));
5954 return (handle_keypress(t
, e
, 1));
5958 return (XT_CB_PASSTHROUGH
);
5962 hint_continue(struct tab
*t
)
5964 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5966 const gchar
*errstr
= NULL
;
5970 if (!(c
[0] == '.' || c
[0] == ','))
5972 if (strlen(c
) == 1) {
5973 /* XXX should not happen */
5978 if (isdigit(c
[1])) {
5980 i
= strtonum(&c
[1], 1, 4096, &errstr
);
5982 show_oops(t
, "invalid numerical hint %s", &c
[1]);
5985 s
= g_strdup_printf("hints.updateHints(%d);", i
);
5989 /* alphanumeric input */
5990 s
= g_strdup_printf("hints.createHints('%s', '%c');",
5991 &c
[1], c
[0] == '.' ? 'f' : 'F');
6002 search_continue(struct tab
*t
)
6004 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
6005 gboolean rv
= FALSE
;
6007 if (c
[0] == ':' || c
[0] == '.' || c
[0] == ',')
6009 if (strlen(c
) == 1) {
6010 webkit_web_view_unmark_text_matches(t
->wv
);
6015 t
->search_forward
= TRUE
;
6016 else if (c
[0] == '?')
6017 t
->search_forward
= FALSE
;
6027 search_cb(struct tab
*t
)
6029 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
6030 #if !GTK_CHECK_VERSION(3, 0, 0)
6034 if (search_continue(t
) == FALSE
)
6038 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, t
->search_forward
,
6040 /* not found, mark red */
6041 #if GTK_CHECK_VERSION(3, 0, 0)
6042 gtk_widget_set_name(t
->cmd
, XT_CSS_RED
);
6044 gdk_color_parse(XT_COLOR_RED
, &color
);
6045 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
6047 /* unmark and remove selection */
6048 webkit_web_view_unmark_text_matches(t
->wv
);
6049 /* my kingdom for a way to unselect text in webview */
6051 /* found, highlight all */
6052 webkit_web_view_unmark_text_matches(t
->wv
);
6053 webkit_web_view_mark_text_matches(t
->wv
, &c
[1], FALSE
, 0);
6054 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
6055 #if GTK_CHECK_VERSION(3, 0, 0)
6056 gtk_widget_set_name(t
->cmd
, XT_CSS_NORMAL
);
6058 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
6059 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
6068 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
6070 const gchar
*c
= gtk_entry_get_text(w
);
6073 show_oops(NULL
, "cmd_keyrelease_cb invalid parameters");
6074 return (XT_CB_PASSTHROUGH
);
6077 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
6078 e
->keyval
, e
->state
, t
->tab_id
);
6081 if (!(e
->keyval
== GDK_Tab
|| e
->keyval
== GDK_ISO_Left_Tab
)) {
6082 if (hint_continue(t
) == FALSE
)
6087 if (search_continue(t
) == FALSE
)
6090 /* if search length is > 4 then no longer play timeout games */
6091 if (strlen(c
) > 4) {
6093 g_source_remove(t
->search_id
);
6100 /* reestablish a new timer if the user types fast */
6102 g_source_remove(t
->search_id
);
6103 t
->search_id
= g_timeout_add(250, (GSourceFunc
)search_cb
, (gpointer
)t
);
6106 return (XT_CB_PASSTHROUGH
);
6110 match_uri(const gchar
*uri
, const gchar
*key
) {
6113 gboolean match
= FALSE
;
6117 if (!strncmp(key
, uri
, len
))
6120 voffset
= strstr(uri
, "/") + 2;
6121 if (!strncmp(key
, voffset
, len
))
6123 else if (g_str_has_prefix(voffset
, "www.")) {
6124 voffset
= voffset
+ strlen("www.");
6125 if (!strncmp(key
, voffset
, len
))
6134 match_session(const gchar
*name
, const gchar
*key
) {
6137 sub
= strcasestr(name
, key
);
6143 cmd_getlist(int id
, char *key
)
6150 if (cmds
[id
].type
& XT_URLARG
) {
6151 RB_FOREACH_REVERSE(h
, history_list
, &hl
)
6152 if (match_uri(h
->uri
, key
)) {
6153 cmd_status
.list
[c
] = (char *)h
->uri
;
6159 } else if (cmds
[id
].type
& XT_SESSARG
) {
6160 TAILQ_FOREACH(s
, &sessions
, entry
)
6161 if (match_session(s
->name
, key
)) {
6162 cmd_status
.list
[c
] = (char *)s
->name
;
6168 } else if (cmds
[id
].type
& XT_SETARG
) {
6169 for (i
= 0; i
< get_settings_size(); i
++)
6170 if (!strncmp(key
, get_setting_name(i
),
6172 cmd_status
.list
[c
++] =
6173 get_setting_name(i
);
6179 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
6181 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
6182 if (cmds
[i
].level
< dep
)
6184 if (cmds
[i
].level
== dep
&& !strncmp(key
, cmds
[i
].cmd
,
6185 strlen(key
)) && !isdigit(cmds
[i
].cmd
[0]))
6186 cmd_status
.list
[c
++] = cmds
[i
].cmd
;
6194 cmd_getnext(int dir
)
6196 cmd_status
.index
+= dir
;
6198 if (cmd_status
.index
< 0)
6199 cmd_status
.index
= cmd_status
.len
- 1;
6200 else if (cmd_status
.index
>= cmd_status
.len
)
6201 cmd_status
.index
= 0;
6203 return cmd_status
.list
[cmd_status
.index
];
6207 cmd_tokenize(char *s
, char *tokens
[])
6210 char *tok
, *last
= NULL
;
6211 size_t len
= strlen(s
);
6214 blank
= len
== 0 || (len
> 0 && s
[len
- 1] == ' ');
6215 for (tok
= strtok_r(s
, " ", &last
); tok
&& i
< 3;
6216 tok
= strtok_r(NULL
, " ", &last
), i
++)
6226 cmd_complete(struct tab
*t
, char *str
, int dir
)
6228 GtkEntry
*w
= GTK_ENTRY(t
->cmd
);
6229 int i
, j
, levels
, c
= 0, dep
= 0, parent
= -1;
6231 char *tok
, *match
, *s
= g_strdup(str
);
6233 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
6236 DNPRINTF(XT_D_CMD
, "%s: complete %s\n", __func__
, str
);
6239 for (i
= 0; isdigit(s
[i
]); i
++)
6242 for (; isspace(s
[i
]); i
++)
6247 levels
= cmd_tokenize(s
, tokens
);
6249 for (i
= 0; i
< levels
- 1; i
++) {
6252 for (j
= c
; j
< LENGTH(cmds
); j
++) {
6253 if (cmds
[j
].level
< dep
)
6255 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
,
6259 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
6266 if (matchcount
== 1) {
6267 strlcat(res
, tok
, sizeof res
);
6268 strlcat(res
, " ", sizeof res
);
6278 if (cmd_status
.index
== -1)
6279 cmd_getlist(parent
, tokens
[i
]);
6281 if (cmd_status
.len
> 0) {
6282 match
= cmd_getnext(dir
);
6283 strlcat(res
, match
, sizeof res
);
6284 gtk_entry_set_text(w
, res
);
6285 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6292 parse_prefix_and_alias(const char *str
, int *prefix
)
6294 struct cmd_alias
*c
;
6295 char *s
= g_strdup(str
), *sc
;
6300 if (isdigit(s
[0])) {
6301 sscanf(s
, "%d", prefix
);
6302 while (isdigit(s
[0]) || isspace(s
[0]))
6306 TAILQ_FOREACH(c
, &cal
, entry
) {
6307 if (strncmp(s
, c
->alias
, strlen(c
->alias
)))
6310 if (strlen(s
) == strlen(c
->alias
)) {
6312 return (g_strdup(c
->cmd
));
6315 if (!isspace(s
[strlen(c
->alias
)]))
6318 s
= g_strdup_printf("%s %s", c
->cmd
, &s
[strlen(c
->alias
) + 1]);
6328 cmd_execute(struct tab
*t
, char *str
)
6330 struct cmd
*cmd
= NULL
;
6331 char *tok
, *last
= NULL
, *s
= str
;
6332 int j
= 0, len
, c
= 0, dep
= 0, matchcount
= 0;
6333 int prefix
= -1, rv
= XT_CB_PASSTHROUGH
;
6334 struct karg arg
= {0, NULL
, -1};
6336 s
= parse_prefix_and_alias(s
, &prefix
);
6338 for (tok
= strtok_r(s
, " ", &last
); tok
;
6339 tok
= strtok_r(NULL
, " ", &last
)) {
6341 for (j
= c
; j
< LENGTH(cmds
); j
++) {
6342 if (cmds
[j
].level
< dep
)
6344 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1 :
6346 if (cmds
[j
].level
== dep
&&
6347 !strncmp(tok
, cmds
[j
].cmd
, len
)) {
6351 if (len
== strlen(cmds
[j
].cmd
)) {
6357 if (matchcount
== 1) {
6362 show_oops(t
, "Invalid command: %s", str
);
6368 show_oops(t
, "Empty command");
6374 arg
.precount
= prefix
;
6375 else if (cmd_prefix
> 0)
6376 arg
.precount
= cmd_prefix
;
6378 if (j
> 0 && !(cmd
->type
& XT_PREFIX
) && arg
.precount
> -1) {
6379 show_oops(t
, "No prefix allowed: %s", str
);
6383 arg
.s
= last
? g_strdup(last
) : g_strdup("");
6384 if (cmd
->type
& XT_INTARG
&& last
&& strlen(last
) > 0) {
6385 if (arg
.s
== NULL
) {
6386 show_oops(t
, "Invalid command");
6389 arg
.precount
= atoi(arg
.s
);
6390 if (arg
.precount
<= 0) {
6391 if (arg
.s
[0] == '0')
6392 show_oops(t
, "Zero count");
6394 show_oops(t
, "Trailing characters");
6399 DNPRINTF(XT_D_CMD
, "%s: prefix %d arg %s\n",
6400 __func__
, arg
.precount
, arg
.s
);
6416 save_runtime_setting(const char *name
, const char *val
)
6422 char file
[PATH_MAX
];
6423 char delim
[3] = { '\0', '\0', '\0' };
6424 char *line
, *lt
, *start
;
6425 char *contents
, *tmp
;
6427 if (runtime_settings
== NULL
|| strlen(runtime_settings
) == 0)
6430 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, runtime_settings
);
6431 if (stat(file
, &sb
) || (f
= fopen(file
, "r+")) == NULL
)
6433 lt
= g_strdup_printf("%s=%s", name
, val
);
6434 contents
= g_strdup("");
6436 line
= fparseln(f
, &linelen
, NULL
, delim
, 0);
6437 if (line
== NULL
|| linelen
== 0)
6440 start
= g_strdup_printf("%s=", name
);
6441 if (strstr(line
, start
) == NULL
)
6442 contents
= g_strdup_printf("%s%s\n", contents
, line
);
6445 contents
= g_strdup_printf("%s%s\n", contents
, lt
);
6454 contents
= g_strdup_printf("%s%s\n", contents
, lt
);
6457 if ((f
= freopen(file
, "w", f
)) == NULL
)
6470 entry_focus_cb(GtkWidget
*w
, GdkEvent e
, struct tab
*t
)
6473 * This sometimes gets randomly unset for whatever reason in GTK3,
6474 * causing a GtkEntry's text cursor becomes invisible. When we focus
6475 * a GtkEntry, be sure to manually reset the main window's is-active
6476 * property so the cursor is shown correctly.
6478 #if GTK_CHECK_VERSION(3, 0, 0)
6479 fake_focus_in(main_window
);
6481 return (XT_CB_PASSTHROUGH
);
6485 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
6488 show_oops(NULL
, "entry_key_cb invalid parameters");
6489 return (XT_CB_PASSTHROUGH
);
6492 DNPRINTF(XT_D_CMD
, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
6493 e
->keyval
, e
->state
, t
->tab_id
);
6497 if (e
->keyval
== GDK_Escape
) {
6498 /* don't use focus_webview(t) because we want to type :cmds */
6499 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6502 return (handle_keypress(t
, e
, 1));
6505 struct command_entry
*
6506 history_prev(struct command_list
*l
, struct command_entry
*at
)
6509 at
= TAILQ_LAST(l
, command_list
);
6511 at
= TAILQ_PREV(at
, command_list
, entry
);
6513 at
= TAILQ_LAST(l
, command_list
);
6519 struct command_entry
*
6520 history_next(struct command_list
*l
, struct command_entry
*at
)
6523 at
= TAILQ_FIRST(l
);
6525 at
= TAILQ_NEXT(at
, entry
);
6527 at
= TAILQ_FIRST(l
);
6534 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
6536 int rv
= XT_CB_HANDLED
;
6537 const gchar
*c
= gtk_entry_get_text(w
);
6541 show_oops(NULL
, "cmd_keypress_cb parameters");
6542 return (XT_CB_PASSTHROUGH
);
6545 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
6546 e
->keyval
, e
->state
, t
->tab_id
);
6550 e
->keyval
= GDK_Escape
;
6551 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
6552 c
[0] == '.' || c
[0] == ','))
6553 e
->keyval
= GDK_Escape
;
6555 if (e
->keyval
!= GDK_Tab
&& e
->keyval
!= GDK_Shift_L
&&
6556 e
->keyval
!= GDK_ISO_Left_Tab
)
6557 cmd_status
.index
= -1;
6559 switch (e
->keyval
) {
6562 cmd_complete(t
, (char *)&c
[1], 1);
6563 else if (c
[0] == '.' || c
[0] == ',')
6564 run_script(t
, "hints.focusNextHint();");
6566 case GDK_ISO_Left_Tab
:
6568 cmd_complete(t
, (char *)&c
[1], -1);
6569 else if (c
[0] == '.' || c
[0] == ',')
6570 run_script(t
, "hints.focusPreviousHint();");
6574 if ((search_at
= history_next(&shl
, search_at
))) {
6575 search_at
->line
[0] = c
[0];
6576 gtk_entry_set_text(w
, search_at
->line
);
6577 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6579 } else if (c
[0] == '/') {
6580 if ((search_at
= history_prev(&shl
, search_at
))) {
6581 search_at
->line
[0] = c
[0];
6582 gtk_entry_set_text(w
, search_at
->line
);
6583 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6585 } else if (c
[0] == ':') {
6586 if ((history_at
= history_prev(&chl
, history_at
))) {
6587 history_at
->line
[0] = c
[0];
6588 gtk_entry_set_text(w
, history_at
->line
);
6589 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6595 if ((search_at
= history_next(&shl
, search_at
))) {
6596 search_at
->line
[0] = c
[0];
6597 gtk_entry_set_text(w
, search_at
->line
);
6598 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6600 } else if (c
[0] == '?') {
6601 if ((search_at
= history_prev(&shl
, search_at
))) {
6602 search_at
->line
[0] = c
[0];
6603 gtk_entry_set_text(w
, search_at
->line
);
6604 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6606 } if (c
[0] == ':') {
6607 if ((history_at
= history_next(&chl
, history_at
))) {
6608 history_at
->line
[0] = c
[0];
6609 gtk_entry_set_text(w
, history_at
->line
);
6610 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6615 if (!(!strcmp(c
, ":") || !strcmp(c
, "/") || !strcmp(c
, "?") ||
6616 !strcmp(c
, ".") || !strcmp(c
, ","))) {
6617 /* see if we are doing hinting and reset it */
6618 if (c
[0] == '.' || c
[0] == ',') {
6619 /* recreate hints */
6620 s
= g_strdup_printf("hints.createHints('', "
6621 "'%c');", c
[0] == '.' ? 'f' : 'F');
6633 if (c
!= NULL
&& (c
[0] == '/' || c
[0] == '?'))
6634 webkit_web_view_unmark_text_matches(t
->wv
);
6636 /* no need to cancel hints */
6640 rv
= XT_CB_PASSTHROUGH
;
6646 wv_popup_activ_cb(GtkMenuItem
*menu
, struct tab
*t
)
6648 GtkAction
*a
= NULL
;
6649 GtkClipboard
*clipboard
, *primary
;
6650 const gchar
*name
, *uri
;
6652 a
= gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu
));
6655 name
= gtk_action_get_name(a
);
6657 DNPRINTF(XT_D_CMD
, "wv_popup_activ_cb: tab %d action %s\n",
6661 * context-menu-action-3 copy link location
6662 * context-menu-action-7 copy image address
6663 * context-menu-action-2030 copy video link location
6667 if ((g_strcmp0(name
, "context-menu-action-3") == 0) ||
6668 (g_strcmp0(name
, "context-menu-action-7") == 0) ||
6669 (g_strcmp0(name
, "context-menu-action-2030") == 0)) {
6670 clipboard
= gtk_clipboard_get(GDK_SELECTION_CLIPBOARD
);
6671 primary
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
6672 uri
= gtk_clipboard_wait_for_text(clipboard
);
6675 gtk_clipboard_set_text(primary
, uri
, -1);
6680 wv_popup_cb(WebKitWebView
*wview
, GtkMenu
*menu
, struct tab
*t
)
6684 DNPRINTF(XT_D_CMD
, "wv_popup_cb: tab %d\n", t
->tab_id
);
6686 items
= gtk_container_get_children(GTK_CONTAINER(menu
));
6687 for (l
= items
; l
; l
= l
->next
)
6688 g_signal_connect(l
->data
, "activate",
6689 G_CALLBACK(wv_popup_activ_cb
), t
);
6694 cmd_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
6696 /* popup menu enabled */
6701 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
6704 show_oops(NULL
, "cmd_focusout_cb invalid parameters");
6705 return (XT_CB_PASSTHROUGH
);
6708 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d popup %d\n",
6709 t
->tab_id
, t
->popup
);
6711 /* if popup is enabled don't lose focus */
6714 return (XT_CB_PASSTHROUGH
);
6721 return (XT_CB_PASSTHROUGH
);
6725 cmd_hide_cb(GtkWidget
*w
, struct tab
*t
)
6728 show_oops(NULL
, "%s: invalid parameters", __func__
);
6732 if (show_url
== 0 || t
->focus_wv
)
6735 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6739 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
6742 const gchar
*c
= gtk_entry_get_text(entry
);
6745 show_oops(NULL
, "cmd_activate_cb invalid parameters");
6749 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
6754 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
6755 c
[0] == '.' || c
[0] == ','))
6761 if (c
[0] == '/' || c
[0] == '?') {
6762 /* see if there is a timer pending */
6764 g_source_remove(t
->search_id
);
6769 if (t
->search_text
) {
6770 g_free(t
->search_text
);
6771 t
->search_text
= NULL
;
6774 t
->search_text
= g_strdup(s
);
6776 g_free(global_search
);
6777 global_search
= g_strdup(s
);
6778 t
->search_forward
= c
[0] == '/';
6780 history_add(&shl
, search_file
, s
, &search_history_count
);
6781 } else if (c
[0] == '.' || c
[0] == ',') {
6782 run_script(t
, "hints.fire();");
6783 /* XXX history for link following? */
6784 } else if (c
[0] == ':') {
6785 history_add(&chl
, command_file
, s
, &cmd_history_count
);
6786 /* can't call hide_cmd after cmd_execute */
6797 backward_cb(GtkWidget
*w
, struct tab
*t
)
6802 show_oops(NULL
, "backward_cb invalid parameters");
6806 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
6813 forward_cb(GtkWidget
*w
, struct tab
*t
)
6818 show_oops(NULL
, "forward_cb invalid parameters");
6822 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
6824 a
.i
= XT_NAV_FORWARD
;
6829 home_cb(GtkWidget
*w
, struct tab
*t
)
6832 show_oops(NULL
, "home_cb invalid parameters");
6836 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
6842 stop_cb(GtkWidget
*w
, struct tab
*t
)
6844 WebKitWebFrame
*frame
;
6847 show_oops(NULL
, "stop_cb invalid parameters");
6851 DNPRINTF(XT_D_NAV
, "stop_cb: tab %d\n", t
->tab_id
);
6853 frame
= webkit_web_view_get_main_frame(t
->wv
);
6854 if (frame
== NULL
) {
6855 show_oops(t
, "stop_cb: no frame");
6859 webkit_web_frame_stop_loading(frame
);
6860 abort_favicon_download(t
);
6864 setup_webkit(struct tab
*t
)
6866 if (is_g_object_setting(G_OBJECT(t
->settings
), "enable-dns-prefetching"))
6867 g_object_set(G_OBJECT(t
->settings
), "enable-dns-prefetching",
6868 FALSE
, (char *)NULL
);
6870 warnx("webkit does not have \"enable-dns-prefetching\" property");
6871 g_object_set(G_OBJECT(t
->settings
),
6872 "user-agent", t
->user_agent
, (char *)NULL
);
6873 g_object_set(G_OBJECT(t
->settings
),
6874 "enable-scripts", enable_scripts
, (char *)NULL
);
6875 g_object_set(G_OBJECT(t
->settings
),
6876 "enable-plugins", enable_plugins
, (char *)NULL
);
6877 g_object_set(G_OBJECT(t
->settings
),
6878 "javascript-can-open-windows-automatically", enable_scripts
,
6880 g_object_set(G_OBJECT(t
->settings
),
6881 "enable-html5-database", FALSE
, (char *)NULL
);
6882 g_object_set(G_OBJECT(t
->settings
),
6883 "enable-html5-local-storage", enable_localstorage
, (char *)NULL
);
6884 g_object_set(G_OBJECT(t
->settings
),
6885 "enable_spell_checking", enable_spell_checking
, (char *)NULL
);
6886 g_object_set(G_OBJECT(t
->settings
),
6887 "spell_checking_languages", spell_check_languages
, (char *)NULL
);
6888 g_object_set(G_OBJECT(t
->settings
),
6889 "enable-developer-extras", TRUE
, (char *)NULL
);
6890 g_object_set(G_OBJECT(t
->wv
),
6891 "full-content-zoom", TRUE
, (char *)NULL
);
6892 g_object_set(G_OBJECT(t
->settings
),
6893 "auto-load-images", auto_load_images
, (char *)NULL
);
6894 if (is_g_object_setting(G_OBJECT(t
->settings
),
6895 "enable-display-of-insecure-content"))
6896 g_object_set(G_OBJECT(t
->settings
),
6897 "enable-display-of-insecure-content",
6898 allow_insecure_content
, (char *)NULL
);
6899 if (is_g_object_setting(G_OBJECT(t
->settings
),
6900 "enable-running-of-insecure-content"))
6901 g_object_set(G_OBJECT(t
->settings
),
6902 "enable-running-of-insecure-content",
6903 allow_insecure_scripts
, (char *)NULL
);
6905 webkit_web_view_set_settings(t
->wv
, t
->settings
);
6909 update_statusbar_position(GtkAdjustment
* adjustment
, gpointer data
)
6911 struct tab
*ti
, *t
= NULL
;
6912 gdouble view_size
, value
, max
;
6915 TAILQ_FOREACH(ti
, &tabs
, entry
)
6916 if (ti
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
6924 if (adjustment
== NULL
)
6925 adjustment
= gtk_scrolled_window_get_vadjustment(
6926 GTK_SCROLLED_WINDOW(t
->browser_win
));
6928 view_size
= gtk_adjustment_get_page_size(adjustment
);
6929 value
= gtk_adjustment_get_value(adjustment
);
6930 max
= gtk_adjustment_get_upper(adjustment
) - view_size
;
6933 position
= g_strdup("All");
6934 else if (value
== max
)
6935 position
= g_strdup("Bot");
6936 else if (value
== 0)
6937 position
= g_strdup("Top");
6939 position
= g_strdup_printf("%d%%", (int) ((value
/ max
) * 100));
6941 gtk_label_set_text(GTK_LABEL(t
->sbe
.position
), position
);
6948 create_window(const gchar
*name
)
6952 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
6953 if (window_maximize
)
6954 gtk_window_maximize(GTK_WINDOW(w
));
6956 gtk_window_set_default_size(GTK_WINDOW(w
), window_width
, window_height
);
6957 gtk_widget_set_name(w
, name
);
6958 gtk_window_set_wmclass(GTK_WINDOW(w
), name
, "Xombrero");
6964 create_browser(struct tab
*t
)
6968 GtkAdjustment
*adjustment
;
6971 show_oops(NULL
, "create_browser invalid parameters");
6975 w
= gtk_scrolled_window_new(NULL
, NULL
);
6976 gtk_widget_set_can_focus(w
, FALSE
);
6977 t
->adjust_h
= gtk_scrolled_window_get_hadjustment(
6978 GTK_SCROLLED_WINDOW(w
));
6979 t
->adjust_v
= gtk_scrolled_window_get_vadjustment(
6980 GTK_SCROLLED_WINDOW(w
));
6981 #if !GTK_CHECK_VERSION(3, 0, 0)
6982 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
6983 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
6987 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
6988 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
6991 t
->settings
= webkit_web_settings_new();
6993 g_object_set(t
->settings
, "default-encoding", encoding
, (char *)NULL
);
6995 if (user_agent
== NULL
) {
6996 g_object_get(G_OBJECT(t
->settings
), "user-agent", &strval
,
6998 t
->user_agent
= g_strdup_printf("%s %s+", strval
, version
);
7001 t
->user_agent
= g_strdup(user_agent
->value
);
7003 t
->stylesheet
= g_strdup(stylesheet
);
7004 t
->load_images
= auto_load_images
;
7007 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w
));
7008 g_signal_connect(G_OBJECT(adjustment
), "value-changed",
7009 G_CALLBACK(update_statusbar_position
), NULL
);
7018 create_kiosk_toolbar(struct tab
*t
)
7020 GtkWidget
*toolbar
= NULL
, *b
;
7022 #if GTK_CHECK_VERSION(3, 0, 0)
7023 b
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7024 gtk_widget_set_name(GTK_WIDGET(b
), "toolbar");
7026 b
= gtk_hbox_new(FALSE
, 0);
7029 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
7031 /* backward button */
7032 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
7033 gtk_widget_set_sensitive(t
->backward
, FALSE
);
7034 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
7035 G_CALLBACK(backward_cb
), t
);
7036 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, TRUE
, TRUE
, 0);
7038 /* forward button */
7039 t
->forward
= create_button("Forward", GTK_STOCK_GO_FORWARD
, 0);
7040 gtk_widget_set_sensitive(t
->forward
, FALSE
);
7041 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
7042 G_CALLBACK(forward_cb
), t
);
7043 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, TRUE
, TRUE
, 0);
7046 t
->gohome
= create_button("Home", GTK_STOCK_HOME
, 0);
7047 gtk_widget_set_sensitive(t
->gohome
, true);
7048 g_signal_connect(G_OBJECT(t
->gohome
), "clicked",
7049 G_CALLBACK(home_cb
), t
);
7050 gtk_box_pack_start(GTK_BOX(b
), t
->gohome
, TRUE
, TRUE
, 0);
7052 /* create widgets but don't use them */
7053 t
->uri_entry
= gtk_entry_new();
7054 g_signal_connect(G_OBJECT(t
->uri_entry
), "focus-in-event",
7055 G_CALLBACK(entry_focus_cb
), t
);
7056 #if !GTK_CHECK_VERSION(3, 0, 0)
7057 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
7059 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
7060 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
7061 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
7067 create_toolbar(struct tab
*t
)
7069 GtkWidget
*toolbar
= NULL
, *b
;
7071 #if GTK_CHECK_VERSION(3, 0, 0)
7072 b
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7073 gtk_widget_set_name(GTK_WIDGET(b
), "toolbar");
7075 b
= gtk_hbox_new(FALSE
, 0);
7078 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
7080 /* backward button */
7081 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
7082 gtk_widget_set_sensitive(t
->backward
, FALSE
);
7083 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
7084 G_CALLBACK(backward_cb
), t
);
7085 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, FALSE
, FALSE
, 0);
7087 /* forward button */
7088 t
->forward
= create_button("Forward",GTK_STOCK_GO_FORWARD
, 0);
7089 gtk_widget_set_sensitive(t
->forward
, FALSE
);
7090 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
7091 G_CALLBACK(forward_cb
), t
);
7092 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, FALSE
, FALSE
, 0);
7095 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
7096 gtk_widget_set_sensitive(t
->stop
, FALSE
);
7097 g_signal_connect(G_OBJECT(t
->stop
), "clicked", G_CALLBACK(stop_cb
), t
);
7098 gtk_box_pack_start(GTK_BOX(b
), t
->stop
, FALSE
, FALSE
, 0);
7101 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
7102 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
7103 gtk_widget_set_sensitive(t
->js_toggle
, TRUE
);
7104 g_signal_connect(G_OBJECT(t
->js_toggle
), "clicked",
7105 G_CALLBACK(js_toggle_cb
), t
);
7106 gtk_box_pack_start(GTK_BOX(b
), t
->js_toggle
, FALSE
, FALSE
, 0);
7108 t
->uri_entry
= gtk_entry_new();
7109 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
7110 G_CALLBACK(activate_uri_entry_cb
), t
);
7111 g_signal_connect(G_OBJECT(t
->uri_entry
), "key-press-event",
7112 G_CALLBACK(entry_key_cb
), t
);
7113 g_signal_connect(G_OBJECT(t
->uri_entry
), "focus-in-event",
7114 G_CALLBACK(entry_focus_cb
), t
);
7116 gtk_box_pack_start(GTK_BOX(b
), t
->uri_entry
, TRUE
, TRUE
, 0);
7119 t
->search_entry
= gtk_entry_new();
7120 gtk_entry_set_width_chars(GTK_ENTRY(t
->search_entry
), 30);
7121 g_signal_connect(G_OBJECT(t
->search_entry
), "activate",
7122 G_CALLBACK(activate_search_entry_cb
), t
);
7123 g_signal_connect(G_OBJECT(t
->search_entry
), "key-press-event",
7124 G_CALLBACK(entry_key_cb
), t
);
7125 g_signal_connect(G_OBJECT(t
->search_entry
), "focus-in-event",
7126 G_CALLBACK(entry_focus_cb
), t
);
7127 gtk_box_pack_start(GTK_BOX(b
), t
->search_entry
, FALSE
, FALSE
, 0);
7129 #if !GTK_CHECK_VERSION(3, 0, 0)
7130 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
7137 create_buffers(struct tab
*t
)
7139 GtkCellRenderer
*renderer
;
7142 view
= gtk_tree_view_new();
7144 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view
), FALSE
);
7146 renderer
= gtk_cell_renderer_text_new();
7147 gtk_tree_view_insert_column_with_attributes
7148 (GTK_TREE_VIEW(view
), -1, "Id", renderer
, "text", COL_ID
, (char *)NULL
);
7150 renderer
= gtk_cell_renderer_pixbuf_new();
7151 gtk_tree_view_insert_column_with_attributes
7152 (GTK_TREE_VIEW(view
), -1, "Favicon", renderer
, "pixbuf", COL_FAVICON
,
7155 renderer
= gtk_cell_renderer_text_new();
7156 gtk_tree_view_insert_column_with_attributes
7157 (GTK_TREE_VIEW(view
), -1, "Title", renderer
, "text", COL_TITLE
,
7160 gtk_tree_view_set_model
7161 (GTK_TREE_VIEW(view
), GTK_TREE_MODEL(buffers_store
));
7167 row_activated_cb(GtkTreeView
*view
, GtkTreePath
*path
,
7168 GtkTreeViewColumn
*col
, struct tab
*t
)
7173 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
7175 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
,
7178 (GTK_TREE_MODEL(buffers_store
), &iter
, COL_ID
, &id
, -1);
7179 set_current_tab(id
- 1);
7185 /* after tab reordering/creation/removal */
7191 TAILQ_FOREACH(t
, &tabs
, entry
)
7192 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
7196 update_statusbar_tabs(struct tab
*t
)
7198 int tab_id
, max_tab_id
;
7204 tab_id
= gtk_notebook_get_current_page(notebook
);
7206 max_tab_id
= gtk_notebook_get_n_pages(notebook
);
7207 snprintf(s
, sizeof s
, "%d/%d", tab_id
+ 1, max_tab_id
);
7210 t
= get_current_tab();
7213 gtk_label_set_text(GTK_LABEL(t
->sbe
.tabs
), s
);
7216 /* after active tab change */
7218 recolor_compact_tabs(void)
7222 #if !GTK_CHECK_VERSION(3, 0, 0)
7223 GdkColor color_active
, color_inactive
;
7225 gdk_color_parse(XT_COLOR_CT_ACTIVE
, &color_active
);
7226 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color_inactive
);
7228 curid
= gtk_notebook_get_current_page(notebook
);
7230 TAILQ_FOREACH(t
, &tabs
, entry
) {
7231 #if GTK_CHECK_VERSION(3, 0, 0)
7232 if (t
->tab_id
== curid
)
7233 gtk_widget_set_name(t
->tab_elems
.label
, XT_CSS_ACTIVE
);
7235 gtk_widget_set_name(t
->tab_elems
.label
, "");
7237 if (t
->tab_id
== curid
)
7238 gtk_widget_modify_fg(t
->tab_elems
.label
,
7239 GTK_STATE_NORMAL
, &color_active
);
7241 gtk_widget_modify_fg(t
->tab_elems
.label
,
7242 GTK_STATE_NORMAL
, &color_inactive
);
7248 set_current_tab(int page_num
)
7250 buffercmd_abort(get_current_tab());
7251 gtk_notebook_set_current_page(notebook
, page_num
);
7252 recolor_compact_tabs();
7253 update_statusbar_tabs(NULL
);
7257 undo_close_tab_save(struct tab
*t
)
7261 struct undo
*u1
, *u2
;
7263 WebKitWebHistoryItem
*item
;
7265 if ((uri
= get_uri(t
)) == NULL
)
7268 u1
= g_malloc0(sizeof(struct undo
));
7269 u1
->uri
= g_strdup(uri
);
7271 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
7273 m
= webkit_web_back_forward_list_get_forward_length(t
->bfl
);
7274 n
= webkit_web_back_forward_list_get_back_length(t
->bfl
);
7277 /* forward history */
7278 items
= webkit_web_back_forward_list_get_forward_list_with_limit(t
->bfl
, m
);
7282 u1
->history
= g_list_prepend(u1
->history
,
7283 webkit_web_history_item_copy(item
));
7284 items
= g_list_next(items
);
7289 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
7290 u1
->history
= g_list_prepend(u1
->history
,
7291 webkit_web_history_item_copy(item
));
7295 items
= webkit_web_back_forward_list_get_back_list_with_limit(t
->bfl
, n
);
7299 u1
->history
= g_list_prepend(u1
->history
,
7300 webkit_web_history_item_copy(item
));
7301 items
= g_list_next(items
);
7304 TAILQ_INSERT_HEAD(&undos
, u1
, entry
);
7306 if (undo_count
> XT_MAX_UNDO_CLOSE_TAB
) {
7307 u2
= TAILQ_LAST(&undos
, undo_tailq
);
7308 TAILQ_REMOVE(&undos
, u2
, entry
);
7310 g_list_free(u2
->history
);
7319 delete_tab(struct tab
*t
)
7323 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
7329 * no need to join thread here because it won't access t on completion
7332 TAILQ_REMOVE(&tabs
, t
, entry
);
7335 /* Halt all webkit activity. */
7336 abort_favicon_download(t
);
7337 webkit_web_view_stop_loading(t
->wv
);
7339 /* Save the tab, so we can undo the close. */
7340 undo_close_tab_save(t
);
7344 g_source_remove(t
->search_id
);
7348 g_free(t
->session_key
);
7351 bzero(&a
, sizeof a
);
7353 inspector_cmd(t
, &a
);
7355 if (browser_mode
== XT_BM_KIOSK
) {
7356 gtk_widget_destroy(t
->uri_entry
);
7357 gtk_widget_destroy(t
->stop
);
7358 gtk_widget_destroy(t
->js_toggle
);
7361 g_object_unref(t
->completion
);
7363 g_object_unref(t
->item
);
7365 gtk_widget_destroy(t
->tab_elems
.eventbox
);
7366 gtk_widget_destroy(t
->vbox
);
7368 g_free(t
->user_agent
);
7369 g_free(t
->stylesheet
);
7375 if (TAILQ_EMPTY(&tabs
)) {
7376 if (browser_mode
== XT_BM_KIOSK
)
7377 create_new_tab(home
, NULL
, 1, -1);
7379 create_new_tab(NULL
, NULL
, 1, -1);
7382 /* recreate session */
7383 if (session_autosave
) {
7384 bzero(&a
, sizeof a
);
7386 save_tabs(NULL
, &a
);
7390 recolor_compact_tabs();
7394 update_statusbar_zoom(struct tab
*t
)
7397 char s
[16] = { '\0' };
7399 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
7400 if ((zoom
<= 0.99 || zoom
>= 1.01))
7401 snprintf(s
, sizeof s
, "%d%%", (int)(zoom
* 100));
7402 gtk_label_set_text(GTK_LABEL(t
->sbe
.zoom
), s
);
7406 setzoom_webkit(struct tab
*t
, int adjust
)
7408 #define XT_ZOOMPERCENT 0.04
7413 show_oops(NULL
, "setzoom_webkit invalid parameters");
7417 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
7418 if (adjust
== XT_ZOOM_IN
)
7419 zoom
+= XT_ZOOMPERCENT
;
7420 else if (adjust
== XT_ZOOM_OUT
)
7421 zoom
-= XT_ZOOMPERCENT
;
7422 else if (adjust
> 0)
7423 zoom
= default_zoom_level
+ adjust
/ 100.0 - 1.0;
7425 show_oops(t
, "setzoom_webkit invalid zoom value");
7429 if (zoom
< XT_ZOOMPERCENT
)
7430 zoom
= XT_ZOOMPERCENT
;
7431 g_object_set(G_OBJECT(t
->wv
), "zoom-level", zoom
, (char *)NULL
);
7432 update_statusbar_zoom(t
);
7436 tab_clicked_cb(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
7438 struct tab
*t
= (struct tab
*) data
;
7440 DNPRINTF(XT_D_TAB
, "tab_clicked_cb: tab: %d\n", t
->tab_id
);
7442 switch (event
->button
) {
7444 set_current_tab(t
->tab_id
);
7455 append_tab(struct tab
*t
)
7460 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
7461 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
7469 sbe
= gtk_label_new(NULL
);
7470 gtk_widget_set_can_focus(GTK_WIDGET(sbe
), FALSE
);
7471 gtk_widget_modify_font(GTK_WIDGET(sbe
), statusbar_font
);
7476 add_sbe(GtkWidget
*box
, char flag
, int *used
, GtkWidget
*sbe
)
7478 if (box
== NULL
|| used
== NULL
|| sbe
== NULL
) {
7479 DPRINTF("%s: invalid parameters", __func__
);
7484 warnx("flag \"%c\" specified more than "
7485 "once in statusbar_elems\n", flag
);
7489 gtk_box_pack_start(GTK_BOX(box
), sbe
, FALSE
, FALSE
, 0);
7496 statusbar_create(struct tab
*t
)
7498 GtkWidget
*box
; /* container for statusbar elems */
7501 int sbe_P
= 0, sbe_B
= 0, sbe_Z
= 0, sbe_T
= 0,
7503 #if !GTK_CHECK_VERSION(3, 0, 0)
7508 DPRINTF("%s: invalid parameters", __func__
);
7512 #if GTK_CHECK_VERSION(3, 0, 0)
7513 t
->statusbar
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7514 box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7515 gtk_widget_set_name(GTK_WIDGET(t
->statusbar
), "statusbar");
7517 t
->statusbar
= gtk_hbox_new(FALSE
, 0);
7518 box
= gtk_hbox_new(FALSE
, 0);
7520 t
->sbe
.ebox
= gtk_event_box_new();
7522 gtk_widget_set_can_focus(GTK_WIDGET(t
->statusbar
), FALSE
);
7523 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.ebox
), FALSE
);
7524 gtk_widget_set_can_focus(GTK_WIDGET(box
), FALSE
);
7526 gtk_box_set_spacing(GTK_BOX(box
), 10);
7527 gtk_box_pack_start(GTK_BOX(t
->statusbar
), t
->sbe
.ebox
, TRUE
, TRUE
, 0);
7528 gtk_container_add(GTK_CONTAINER(t
->sbe
.ebox
), box
);
7530 /* create these widgets only if specified in statusbar_elems */
7531 t
->sbe
.uri
= gtk_entry_new();
7532 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.uri
), FALSE
);
7533 gtk_widget_modify_font(GTK_WIDGET(t
->sbe
.uri
), statusbar_font
);
7534 #if !GTK_CHECK_VERSION(3, 0, 0)
7535 gtk_entry_set_inner_border(GTK_ENTRY(t
->sbe
.uri
), NULL
);
7536 gtk_entry_set_has_frame(GTK_ENTRY(t
->sbe
.uri
), FALSE
);
7538 t
->sbe
.position
= create_sbe();
7539 t
->sbe
.zoom
= create_sbe();
7540 t
->sbe
.buffercmd
= create_sbe();
7541 t
->sbe
.tabs
= create_sbe();
7542 t
->sbe
.proxy
= create_sbe();
7544 #if GTK_CHECK_VERSION(3, 0, 0)
7545 statusbar_modify_attr(t
, XT_CSS_NORMAL
);
7547 statusbar_modify_attr(t
, XT_COLOR_WHITE
, XT_COLOR_BLACK
);
7550 gtk_box_pack_start(GTK_BOX(box
), t
->sbe
.uri
, TRUE
, TRUE
, 0);
7553 * gtk widgets cannot be added to a box twice. The sbe_* variables
7556 for (p
= statusbar_elems
; *p
!= '\0'; p
++) {
7559 #if GTK_CHECK_VERSION(3, 0, 0)
7560 sep
= gtk_separator_new(GTK_ORIENTATION_VERTICAL
);
7562 sep
= gtk_vseparator_new();
7563 gdk_color_parse(XT_COLOR_SB_SEPARATOR
, &color
);
7564 gtk_widget_modify_bg(sep
, GTK_STATE_NORMAL
, &color
);
7566 gtk_box_pack_start(GTK_BOX(box
), sep
, FALSE
, FALSE
, 0);
7569 add_sbe(box
, *p
, &sbe_P
, t
->sbe
.position
);
7572 add_sbe(box
, *p
, &sbe_B
, t
->sbe
.buffercmd
);
7575 add_sbe(box
, *p
, &sbe_Z
, t
->sbe
.zoom
);
7578 add_sbe(box
, *p
, &sbe_T
, t
->sbe
.tabs
);
7581 if (add_sbe(box
, *p
, &sbe_p
, t
->sbe
.proxy
) == 0)
7584 GTK_ENTRY(t
->sbe
.proxy
), "proxy");
7587 warnx("illegal flag \"%c\" in statusbar_elems\n", *p
);
7592 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar
, FALSE
, FALSE
, 0);
7598 create_new_tab(char *title
, struct undo
*u
, int focus
, int position
)
7603 WebKitWebHistoryItem
*item
;
7605 #if !GTK_CHECK_VERSION(3, 0, 0)
7609 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
7611 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
7612 DNPRINTF(XT_D_TAB
, "create_new_tab: new tab rejected\n");
7616 t
= g_malloc0(sizeof *t
);
7618 if (title
== NULL
) {
7619 title
= "(untitled)";
7623 #if GTK_CHECK_VERSION(3, 0, 0)
7624 t
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
7625 b
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7626 gtk_widget_set_name(t
->vbox
, "vbox");
7628 t
->vbox
= gtk_vbox_new(FALSE
, 0);
7629 b
= gtk_hbox_new(FALSE
, 0);
7631 gtk_widget_set_can_focus(t
->vbox
, FALSE
);
7633 /* label + button for tab */
7635 gtk_widget_set_can_focus(t
->tab_content
, FALSE
);
7637 t
->user_agent_id
= 0;
7638 t
->http_accept_id
= 0;
7640 #if WEBKIT_CHECK_VERSION(1, 5, 0)
7644 #if GTK_CHECK_VERSION(2, 20, 0)
7645 t
->spinner
= gtk_spinner_new();
7647 t
->label
= gtk_label_new(title
);
7648 bb
= create_button("Close", GTK_STOCK_CLOSE
, 1);
7649 gtk_label_set_max_width_chars(GTK_LABEL(t
->label
), 20);
7650 gtk_label_set_ellipsize(GTK_LABEL(t
->label
), PANGO_ELLIPSIZE_END
);
7651 gtk_label_set_line_wrap(GTK_LABEL(t
->label
), FALSE
);
7652 gtk_widget_set_size_request(b
, 130, 0);
7655 * this is a total hack and most likely breaks with other styles but
7656 * is necessary so the text doesn't bounce around when the spinner is
7659 #if GTK_CHECK_VERSION(3, 0, 0)
7660 gtk_widget_set_size_request(t
->label
, 95, 0);
7662 gtk_widget_set_size_request(t
->label
, 100, 0);
7665 gtk_box_pack_start(GTK_BOX(b
), bb
, FALSE
, FALSE
, 0);
7666 gtk_box_pack_start(GTK_BOX(b
), t
->label
, FALSE
, FALSE
, 0);
7667 #if GTK_CHECK_VERSION(2, 20, 0)
7668 gtk_box_pack_end(GTK_BOX(b
), t
->spinner
, FALSE
, FALSE
, 0);
7672 if (browser_mode
== XT_BM_KIOSK
) {
7673 t
->toolbar
= create_kiosk_toolbar(t
);
7674 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
7677 t
->toolbar
= create_toolbar(t
);
7678 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
7686 t
->browser_win
= create_browser(t
);
7687 set_scrollbar_visibility(t
, show_scrollbars
);
7688 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
7690 /* oops message for user feedback */
7691 t
->oops
= gtk_entry_new();
7692 gtk_entry_set_inner_border(GTK_ENTRY(t
->oops
), NULL
);
7693 gtk_entry_set_has_frame(GTK_ENTRY(t
->oops
), FALSE
);
7694 gtk_widget_set_can_focus(GTK_WIDGET(t
->oops
), FALSE
);
7695 #if GTK_CHECK_VERSION(3, 0, 0)
7696 gtk_widget_set_name(t
->oops
, XT_CSS_RED
);
7698 gdk_color_parse(XT_COLOR_RED
, &color
);
7699 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
7701 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
7702 gtk_widget_modify_font(GTK_WIDGET(t
->oops
), oops_font
);
7705 t
->cmd
= gtk_entry_new();
7706 g_signal_connect(G_OBJECT(t
->cmd
), "focus-in-event",
7707 G_CALLBACK(entry_focus_cb
), t
);
7708 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
7709 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
7710 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
7711 gtk_widget_modify_font(GTK_WIDGET(t
->cmd
), cmd_font
);
7714 statusbar_create(t
);
7717 t
->buffers
= create_buffers(t
);
7718 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->buffers
, FALSE
, FALSE
, 0);
7720 /* xtp meaning is normal by default */
7721 set_normal_tab_meaning(t
);
7723 /* set empty favicon */
7724 xt_icon_from_name(t
, "text-html");
7726 /* and show it all */
7727 gtk_widget_show_all(b
);
7728 gtk_widget_show_all(t
->vbox
);
7731 gtk_widget_hide(t
->backward
);
7732 gtk_widget_hide(t
->forward
);
7733 gtk_widget_hide(t
->stop
);
7734 gtk_widget_hide(t
->js_toggle
);
7736 if (!fancy_bar
|| (search_string
== NULL
|| strlen(search_string
) == 0))
7737 gtk_widget_hide(t
->search_entry
);
7739 /* compact tab bar */
7740 t
->tab_elems
.label
= gtk_label_new(title
);
7741 t
->tab_elems
.favicon
= gtk_image_new();
7743 t
->tab_elems
.eventbox
= gtk_event_box_new();
7744 gtk_widget_set_name(t
->tab_elems
.eventbox
, "compact_tab");
7745 #if GTK_CHECK_VERSION(3, 0, 0)
7746 t
->tab_elems
.box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7748 gtk_label_set_ellipsize(GTK_LABEL(t
->tab_elems
.label
),
7749 PANGO_ELLIPSIZE_END
);
7750 gtk_widget_override_font(t
->tab_elems
.label
, tabbar_font
);
7751 gtk_widget_set_halign(t
->tab_elems
.label
, GTK_ALIGN_START
);
7752 gtk_widget_set_valign(t
->tab_elems
.label
, GTK_ALIGN_START
);
7754 t
->tab_elems
.box
= gtk_hbox_new(FALSE
, 0);
7756 gtk_label_set_width_chars(GTK_LABEL(t
->tab_elems
.label
), 1);
7757 gtk_misc_set_alignment(GTK_MISC(t
->tab_elems
.label
), 0.0, 0.0);
7758 gtk_misc_set_padding(GTK_MISC(t
->tab_elems
.label
), 4.0, 4.0);
7759 gtk_widget_modify_font(GTK_WIDGET(t
->tab_elems
.label
), tabbar_font
);
7761 gdk_color_parse(XT_COLOR_CT_BACKGROUND
, &color
);
7762 gtk_widget_modify_bg(t
->tab_elems
.eventbox
, GTK_STATE_NORMAL
, &color
);
7763 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
7764 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
, &color
);
7767 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.favicon
, FALSE
,
7769 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.label
, TRUE
,
7771 gtk_container_add(GTK_CONTAINER(t
->tab_elems
.eventbox
),
7774 gtk_box_pack_start(GTK_BOX(tab_bar_box
), t
->tab_elems
.eventbox
, TRUE
,
7776 gtk_widget_show_all(t
->tab_elems
.eventbox
);
7778 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
7781 id
= position
>= 0 ? position
:
7782 gtk_notebook_get_current_page(notebook
) + 1;
7783 if (id
> gtk_notebook_get_n_pages(notebook
))
7786 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
7787 gtk_notebook_insert_page(notebook
, t
->vbox
, b
, id
);
7788 gtk_box_reorder_child(GTK_BOX(tab_bar_box
),
7789 t
->tab_elems
.eventbox
, id
);
7794 #if GTK_CHECK_VERSION(2, 20, 0)
7795 /* turn spinner off if we are a new tab without uri */
7797 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
7798 gtk_widget_hide(t
->spinner
);
7801 /* make notebook tabs reorderable */
7802 gtk_notebook_set_tab_reorderable(notebook
, t
->vbox
, TRUE
);
7804 /* compact tabs clickable */
7805 g_signal_connect(G_OBJECT(t
->tab_elems
.eventbox
),
7806 "button_press_event", G_CALLBACK(tab_clicked_cb
), t
);
7808 g_object_connect(G_OBJECT(t
->cmd
),
7809 "signal::button-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
7810 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb
), t
,
7811 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
7812 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb
), t
,
7813 "signal::activate", G_CALLBACK(cmd_activate_cb
), t
,
7814 "signal::populate-popup", G_CALLBACK(cmd_popup_cb
), t
,
7815 "signal::hide", G_CALLBACK(cmd_hide_cb
), t
,
7818 /* reuse wv_button_cb to hide oops */
7819 g_object_connect(G_OBJECT(t
->oops
),
7820 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7823 g_signal_connect(t
->buffers
,
7824 "row-activated", G_CALLBACK(row_activated_cb
), t
);
7825 g_object_connect(G_OBJECT(t
->buffers
),
7826 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
, (char *)NULL
);
7828 g_object_connect(G_OBJECT(t
->wv
),
7829 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
,
7830 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb
), t
,
7831 "signal::download-requested", G_CALLBACK(webview_download_cb
), t
,
7832 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), t
,
7833 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7834 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7835 "signal::resource-request-starting", G_CALLBACK(webview_rrs_cb
), t
,
7836 "signal::create-web-view", G_CALLBACK(webview_cwv_cb
), t
,
7837 "signal::close-web-view", G_CALLBACK(webview_closewv_cb
), t
,
7838 "signal::event", G_CALLBACK(webview_event_cb
), t
,
7839 "signal::load-finished", G_CALLBACK(webview_load_finished_cb
), t
,
7840 "signal::load-progress-changed", G_CALLBACK(webview_progress_changed_cb
), t
,
7841 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb
), t
,
7842 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7843 "signal::button_release_event", G_CALLBACK(wv_release_button_cb
), t
,
7844 "signal::populate-popup", G_CALLBACK(wv_popup_cb
), t
,
7846 g_signal_connect(t
->wv
,
7847 "notify::load-status", G_CALLBACK(notify_load_status_cb
), t
);
7848 g_signal_connect(t
->wv
,
7849 "notify::title", G_CALLBACK(notify_title_cb
), t
);
7851 /* hijack the unused keys as if we were the browser */
7852 //g_object_connect(G_OBJECT(t->toolbar),
7853 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
7856 g_signal_connect(G_OBJECT(bb
), "button_press_event",
7857 G_CALLBACK(tab_close_cb
), t
);
7860 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
7861 /* restore the tab's history */
7862 if (u
&& u
->history
) {
7866 webkit_web_back_forward_list_add_item(t
->bfl
, item
);
7867 items
= g_list_next(items
);
7870 item
= g_list_nth_data(u
->history
, u
->back
);
7872 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
7875 g_list_free(u
->history
);
7877 webkit_web_back_forward_list_clear(t
->bfl
);
7879 /* check and show url and statusbar */
7880 url_set_visibility();
7881 statusbar_set_visibility();
7884 set_current_tab(t
->tab_id
);
7885 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
7889 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
7893 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
7900 if (userstyle_global
)
7903 recolor_compact_tabs();
7904 setzoom_webkit(t
, XT_ZOOM_NORMAL
);
7909 notebook_switchpage_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7915 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
7917 if (gtk_notebook_get_current_page(notebook
) == -1)
7920 TAILQ_FOREACH(t
, &tabs
, entry
) {
7921 if (t
->tab_id
== pn
) {
7922 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
7925 uri
= get_title(t
, TRUE
);
7926 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
7933 /* can't use focus_webview here */
7934 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
7936 update_statusbar_tabs(t
);
7943 notebook_pagereordered_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7946 struct tab
*t
= NULL
, *tt
;
7950 TAILQ_FOREACH(tt
, &tabs
, entry
)
7951 if (tt
->tab_id
== pn
) {
7957 DNPRINTF(XT_D_TAB
, "page_reordered_cb: tab: %d\n", t
->tab_id
);
7959 gtk_box_reorder_child(GTK_BOX(tab_bar_box
), t
->tab_elems
.eventbox
,
7962 update_statusbar_tabs(t
);
7966 menuitem_response(struct tab
*t
)
7968 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
7972 destroy_menu(GtkWidget
*w
, GdkEventFocus
*e
, void *notused
)
7974 gtk_widget_destroy(w
);
7975 return (XT_CB_PASSTHROUGH
);
7979 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
7981 GtkWidget
*menu
, *menu_items
;
7982 GdkEventButton
*bevent
;
7983 struct tab
**stabs
= NULL
;
7987 if (event
->type
== GDK_BUTTON_PRESS
) {
7988 bevent
= (GdkEventButton
*) event
;
7989 menu
= gtk_menu_new();
7991 num_tabs
= sort_tabs_by_page_num(&stabs
);
7992 for (i
= 0; i
< num_tabs
; ++i
) {
7993 if (stabs
[i
] == NULL
)
7995 if ((uri
= get_uri(stabs
[i
])) == NULL
)
7996 /* XXX make sure there is something to print */
7997 /* XXX add gui pages in here to look purdy */
7999 menu_items
= gtk_menu_item_new_with_label(uri
);
8000 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), menu_items
);
8001 gtk_widget_show(menu_items
);
8003 g_signal_connect_swapped((menu_items
),
8004 "activate", G_CALLBACK(menuitem_response
),
8005 (gpointer
)stabs
[i
]);
8009 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
,
8010 bevent
->button
, bevent
->time
);
8012 g_object_connect(G_OBJECT(menu
),
8013 "signal::hide", G_CALLBACK(destroy_menu
), NULL
,
8016 return (TRUE
/* eat event */);
8019 return (FALSE
/* propagate */);
8023 icon_size_map(int iconsz
)
8025 if (iconsz
<= GTK_ICON_SIZE_INVALID
||
8026 iconsz
> GTK_ICON_SIZE_DIALOG
)
8027 return (GTK_ICON_SIZE_SMALL_TOOLBAR
);
8033 create_button(char *name
, char *stockid
, int size
)
8035 GtkWidget
*button
, *image
;
8037 #if !GTK_CHECK_VERSION(3, 0, 0)
8041 #if !GTK_CHECK_VERSION(3, 0, 0)
8042 newstyle
= g_strdup_printf(
8043 "style \"%s-style\"\n"
8045 " GtkWidget::focus-padding = 0\n"
8046 " GtkWidget::focus-line-width = 0\n"
8050 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
8051 gtk_rc_parse_string(newstyle
);
8054 button
= gtk_button_new();
8055 gtk_widget_set_can_focus(button
, FALSE
);
8056 gtk_button_set_focus_on_click(GTK_BUTTON(button
), FALSE
);
8057 gtk_icon_size
= icon_size_map(size
? size
: icon_size
);
8059 image
= gtk_image_new_from_stock(stockid
, gtk_icon_size
);
8060 gtk_container_set_border_width(GTK_CONTAINER(button
), 1);
8061 gtk_container_add(GTK_CONTAINER(button
), GTK_WIDGET(image
));
8062 gtk_widget_set_name(button
, name
);
8063 gtk_button_set_relief(GTK_BUTTON(button
), GTK_RELIEF_NONE
);
8069 button_set_stockid(GtkWidget
*button
, char *stockid
)
8073 image
= gtk_image_new_from_stock(stockid
, icon_size_map(icon_size
));
8074 gtk_button_set_image(GTK_BUTTON(button
), image
);
8083 char file
[PATH_MAX
];
8085 #if !GTK_CHECK_VERSION(3, 0, 0)
8089 #if GTK_CHECK_VERSION(3, 0, 0)
8090 vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
8092 vbox
= gtk_vbox_new(FALSE
, 0);
8094 gtk_box_set_spacing(GTK_BOX(vbox
), 0);
8095 gtk_widget_set_can_focus(vbox
, FALSE
);
8096 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
8097 #if !GTK_CHECK_VERSION(3, 0, 0)
8098 /* XXX seems to be needed with gtk+2 */
8099 g_object_set(G_OBJECT(notebook
), "tab-border", 0, NULL
);
8101 gtk_notebook_set_scrollable(notebook
, TRUE
);
8102 gtk_notebook_set_show_border(notebook
, FALSE
);
8103 gtk_widget_set_can_focus(GTK_WIDGET(notebook
), FALSE
);
8105 abtn
= gtk_button_new();
8106 gtk_widget_set_can_focus(abtn
, FALSE
);
8107 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
8108 gtk_widget_set_name(abtn
, "Arrow");
8109 gtk_container_add(GTK_CONTAINER(abtn
), arrow
);
8110 gtk_widget_set_size_request(abtn
, -1, 20);
8112 #if GTK_CHECK_VERSION(2, 20, 0)
8113 gtk_notebook_set_action_widget(notebook
, abtn
, GTK_PACK_END
);
8115 /* compact tab bar */
8116 tab_bar
= gtk_event_box_new();
8117 #if GTK_CHECK_VERSION(3, 0, 0)
8118 gtk_widget_set_name(tab_bar
, "tab_bar");
8119 tab_bar_box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
8121 gdk_color_parse(XT_COLOR_CT_SEPARATOR
, &color
);
8122 gtk_widget_modify_bg(tab_bar
, GTK_STATE_NORMAL
, &color
);
8123 tab_bar_box
= gtk_hbox_new(TRUE
, 0);
8125 gtk_container_add(GTK_CONTAINER(tab_bar
), tab_bar_box
);
8126 gtk_box_set_homogeneous(GTK_BOX(tab_bar_box
), TRUE
);
8127 gtk_box_set_spacing(GTK_BOX(tab_bar_box
), 2);
8129 gtk_box_pack_start(GTK_BOX(vbox
), tab_bar
, FALSE
, FALSE
, 0);
8130 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
8132 g_object_connect(G_OBJECT(notebook
),
8133 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb
), NULL
,
8135 g_object_connect(G_OBJECT(notebook
),
8136 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb
),
8137 NULL
, (char *)NULL
);
8138 g_signal_connect(G_OBJECT(abtn
), "button_press_event",
8139 G_CALLBACK(arrow_cb
), NULL
);
8141 main_window
= create_window("xombrero");
8142 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
8143 g_signal_connect(G_OBJECT(main_window
), "delete_event",
8144 G_CALLBACK(gtk_main_quit
), NULL
);
8145 #if GTK_CHECK_VERSION(3, 0, 0)
8146 gtk_window_set_has_resize_grip(GTK_WINDOW(main_window
), FALSE
);
8150 for (i
= 0; i
< LENGTH(icons
); i
++) {
8151 snprintf(file
, sizeof file
, "%s" PS
"%s", resource_dir
, icons
[i
]);
8152 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
8153 l
= g_list_append(l
, pb
);
8155 gtk_window_set_default_icon_list(l
);
8157 gtk_widget_show_all(abtn
);
8158 gtk_widget_show_all(main_window
);
8159 notebook_tab_set_visibility();
8162 #ifndef XT_SOCKET_DISABLE
8164 send_cmd_to_socket(char *cmd
)
8167 struct sockaddr_un sa
;
8169 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
8170 warnx("%s: socket", __func__
);
8174 sa
.sun_family
= AF_UNIX
;
8175 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
8176 work_dir
, XT_SOCKET_FILE
);
8179 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
8180 warnx("%s: connect", __func__
);
8184 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
8185 warnx("%s: send", __func__
);
8196 socket_watcher(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
8199 char str
[XT_MAX_URL_LENGTH
];
8200 socklen_t t
= sizeof(struct sockaddr_un
);
8201 struct sockaddr_un sa
;
8206 gint fd
= g_io_channel_unix_get_fd(source
);
8208 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
8213 if (getpeereid(s
, &uid
, &gid
) == -1) {
8217 if (uid
!= getuid() || gid
!= getgid()) {
8218 warnx("unauthorized user");
8224 warnx("not a valid user");
8228 n
= recv(s
, str
, sizeof(str
), 0);
8232 tt
= get_current_tab();
8233 cmd_execute(tt
, str
);
8241 struct sockaddr_un sa
;
8243 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
8244 warn("is_running: socket");
8248 sa
.sun_family
= AF_UNIX
;
8249 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
8250 work_dir
, XT_SOCKET_FILE
);
8253 /* connect to see if there is a listener */
8254 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
8255 rv
= 0; /* not running */
8257 rv
= 1; /* already running */
8268 struct sockaddr_un sa
;
8270 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
8271 warn("build_socket: socket");
8275 sa
.sun_family
= AF_UNIX
;
8276 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
8277 work_dir
, XT_SOCKET_FILE
);
8280 /* connect to see if there is a listener */
8281 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
8282 /* no listener so we will */
8283 unlink(sa
.sun_path
);
8285 if (bind(s
, (struct sockaddr
*)&sa
, len
) == -1) {
8286 warn("build_socket: bind");
8290 if (listen(s
, 1) == -1) {
8291 warn("build_socket: listen");
8309 if (stat(dir
, &sb
)) {
8310 #if defined __MINGW32__
8311 if (mkdir(dir
) == -1)
8313 if (mkdir(dir
, S_IRWXU
) == -1)
8315 err(1, "mkdir %s", dir
);
8317 err(1, "stat %s", dir
);
8319 if (S_ISDIR(sb
.st_mode
) == 0)
8320 errx(1, "%s not a dir", dir
);
8321 #if !defined __MINGW32__
8322 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
8323 warnx("fixing invalid permissions on %s", dir
);
8324 if (chmod(dir
, S_IRWXU
) == -1)
8325 err(1, "chmod %s", dir
);
8334 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
8338 GStaticRecMutex my_gdk_mtx
= G_STATIC_REC_MUTEX_INIT
;
8339 volatile int mtx_depth
;
8343 * The linux flash plugin violates the gdk locking mechanism.
8344 * Work around the issue by using a recursive mutex with some match applied
8345 * to see if we hit a buggy condition.
8347 * The following code is painful so just don't read it. It really doesn't
8348 * make much sense but seems to work.
8357 g_static_rec_mutex_lock(&my_gdk_mtx
);
8358 if (my_gdk_mtx
.depth
<= 0) {
8362 g_static_rec_mutex_lock(&my_gdk_mtx
);
8364 } else if (my_gdk_mtx
.depth
!= 1) {
8369 g_static_rec_mutex_unlock(&my_gdk_mtx
);
8370 } while (my_gdk_mtx
.depth
> 1);
8376 if (mtx_complain
== 0) {
8377 DNPRINTF(XT_D_MTX
, "buggy mutex implementation detected(%s), "
8378 "work around implemented", s
);
8390 if (my_gdk_mtx
.depth
<= 0) {
8395 } else if (my_gdk_mtx
.depth
!= 1) {
8399 g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
8402 g_static_rec_mutex_unlock(&my_gdk_mtx
);
8406 if (mtx_complain
== 0) {
8407 DNPRINTF(XT_D_MTX
, "buggy mutex implementation detected(%s), "
8408 "work around implemented", s
);
8413 #if GTK_CHECK_VERSION(3, 0, 0)
8417 GtkCssProvider
*provider
;
8418 GdkDisplay
*display
;
8421 char path
[PATH_MAX
];
8422 #if defined __MINGW32__
8423 GtkCssProvider
*windows_hacks
;
8426 provider
= gtk_css_provider_new();
8427 display
= gdk_display_get_default();
8428 screen
= gdk_display_get_default_screen(display
);
8429 snprintf(path
, sizeof path
, "%s" PS
"%s", resource_dir
, XT_CSS_FILE
);
8430 file
= g_file_new_for_path(path
);
8431 gtk_css_provider_load_from_file(provider
, file
, NULL
);
8432 gtk_style_context_add_provider_for_screen(screen
,
8433 GTK_STYLE_PROVIDER(provider
),
8434 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
8435 #if defined __MINGW32__
8436 windows_hacks
= gtk_css_provider_new();
8437 gtk_css_provider_load_from_data(windows_hacks
,
8439 " border-width: 0px;\n"
8441 gtk_style_context_add_provider_for_screen(screen
,
8442 GTK_STYLE_PROVIDER(windows_hacks
),
8443 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
8445 g_object_unref(file
);
8452 startpage_add("<b>Welcome to xombrero %s!</b><p>", version
);
8453 startpage_add("Details at "
8454 "<a href=https://opensource.conformal.com/wiki/xombrero>xombrero "
8455 "wiki page</a><p>");
8459 main(int argc
, char **argv
)
8462 int c
, optn
= 0, opte
= 0, focus
= 1;
8463 char conf
[PATH_MAX
] = { '\0' };
8464 char file
[PATH_MAX
];
8465 char sodversion
[32];
8466 char *env_proxy
= NULL
;
8471 start_argv
= (char * const *)argv
;
8478 #if !defined __MINGW32__
8479 /* http://web.archiveorange.com/archive/v/UsPjxkX5PsaXBIoOjqxf */
8482 /* http://developer.gnome.org/gdk/stable/gdk-Threads.html */
8483 g_thread_init(NULL
);
8484 gdk_threads_set_lock_functions(mtx_lock
, mtx_unlock
);
8486 gdk_threads_enter();
8488 /* http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html */
8489 gcry_control (GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
8491 gtk_init(&argc
, &argv
);
8493 gnutls_global_init();
8495 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
8500 RB_INIT(&force_https
);
8501 RB_INIT(&downloads
);
8507 TAILQ_INIT(&sessions
);
8510 TAILQ_INIT(&aliases
);
8519 #ifndef XT_RESOURCE_LIMITS_DISABLE
8523 GIOChannel
*channel
;
8525 /* fiddle with ulimits */
8526 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
8529 /* just use them all */
8530 rlp
.rlim_cur
= rlp
.rlim_max
;
8531 if (setrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
8533 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
8535 else if (rlp
.rlim_cur
< 1024)
8536 startpage_add("%s requires at least 1024 "
8537 "(2048 recommended) file " "descriptors, "
8538 "currently it has up to %d available",
8539 __progname
, rlp
.rlim_cur
);
8543 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
8552 #ifdef XOMBRERO_BUILDSTR
8553 errx(0 , "Version: %s Build: %s",
8554 version
, XOMBRERO_BUILDSTR
);
8556 errx(0 , "Version: %s", version
);
8560 strlcpy(conf
, optarg
, sizeof(conf
));
8563 strlcpy(named_session
, optarg
, sizeof(named_session
));
8584 pwd
= getpwuid(getuid());
8586 errx(1, "invalid user %d", getuid());
8588 /* set download dir */
8589 if (strlen(download_dir
) == 0)
8590 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
8592 /* compile buffer command regexes */
8595 /* set default dynamic string settings */
8596 home
= g_strdup(XT_DS_HOME
);
8597 search_string
= g_strdup(XT_DS_SEARCH_STRING
);
8598 strlcpy(runtime_settings
, "runtime", sizeof runtime_settings
);
8599 cmd_font_name
= g_strdup(XT_DS_CMD_FONT_NAME
);
8600 oops_font_name
= g_strdup(XT_DS_OOPS_FONT_NAME
);
8601 statusbar_font_name
= g_strdup(XT_DS_STATUSBAR_FONT_NAME
);
8602 tabbar_font_name
= g_strdup(XT_DS_TABBAR_FONT_NAME
);
8603 statusbar_elems
= g_strdup("BP");
8604 spell_check_languages
= g_strdup(XT_DS_SPELL_CHECK_LANGUAGES
);
8605 encoding
= g_strdup(XT_DS_ENCODING
);
8606 spell_check_languages
= g_strdup(XT_DS_SPELL_CHECK_LANGUAGES
);
8607 path
= g_strdup_printf("%s" PS
"style.css", resource_dir
);
8608 userstyle
= g_filename_to_uri(path
, NULL
, NULL
);
8610 stylesheet
= g_strdup(userstyle
);
8612 /* set statically allocated (struct special) settings */
8613 if (strlen(default_script
) == 0)
8614 expand_tilde(default_script
, sizeof default_script
,
8615 XT_DS_DEFAULT_SCRIPT
);
8616 if (strlen(ssl_ca_file
) == 0)
8617 expand_tilde(ssl_ca_file
, sizeof ssl_ca_file
,
8621 session
= webkit_get_default_session();
8623 /* read config file */
8624 if (strlen(conf
) == 0)
8625 snprintf(conf
, sizeof conf
, "%s" PS
".%s",
8626 pwd
->pw_dir
, XT_CONF_FILE
);
8627 config_parse(conf
, 0);
8629 /* read preloaded HSTS list */
8630 if (preload_strict_transport
) {
8631 snprintf(conf
, sizeof conf
, "%s" PS
"%s",
8632 resource_dir
, XT_HSTS_PRELOAD_FILE
);
8633 config_parse(conf
, 0);
8637 cmd_font
= pango_font_description_from_string(cmd_font_name
);
8638 oops_font
= pango_font_description_from_string(oops_font_name
);
8639 statusbar_font
= pango_font_description_from_string(statusbar_font_name
);
8640 tabbar_font
= pango_font_description_from_string(tabbar_font_name
);
8642 /* working directory */
8643 if (strlen(work_dir
) == 0)
8644 snprintf(work_dir
, sizeof work_dir
, "%s" PS
"%s",
8645 pwd
->pw_dir
, XT_DIR
);
8648 /* icon cache dir */
8649 snprintf(cache_dir
, sizeof cache_dir
, "%s" PS
"%s", work_dir
, XT_CACHE_DIR
);
8653 snprintf(certs_dir
, sizeof certs_dir
, "%s" PS
"%s", work_dir
, XT_CERT_DIR
);
8656 /* cert changes dir */
8657 snprintf(certs_cache_dir
, sizeof certs_cache_dir
, "%s" PS
"%s",
8658 work_dir
, XT_CERT_CACHE_DIR
);
8659 xxx_dir(certs_cache_dir
);
8662 snprintf(sessions_dir
, sizeof sessions_dir
, "%s" PS
"%s",
8663 work_dir
, XT_SESSIONS_DIR
);
8664 xxx_dir(sessions_dir
);
8667 snprintf(js_dir
, sizeof js_dir
, "%s" PS
"%s", work_dir
, XT_JS_DIR
);
8671 snprintf(temp_dir
, sizeof temp_dir
, "%s" PS
"%s", work_dir
, XT_TEMP_DIR
);
8674 /* runtime settings that can override config file */
8675 if (runtime_settings
[0] != '\0')
8676 config_parse(runtime_settings
, 1);
8679 if (!strcmp(download_dir
, pwd
->pw_dir
))
8680 strlcat(download_dir
, PS
"downloads", sizeof download_dir
);
8681 xxx_dir(download_dir
);
8683 /* first start file */
8684 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_SOD_FILE
);
8685 if (stat(file
, &sb
)) {
8686 warnx("start of day file doesn't exist, creating it");
8687 if ((f
= fopen(file
, "w")) == NULL
)
8688 err(1, "startofday");
8689 if (fputs(version
, f
) == EOF
)
8696 if ((f
= fopen(file
, "r+")) == NULL
)
8697 err(1, "startofday");
8698 if (fgets(sodversion
, sizeof sodversion
, f
) == NULL
)
8700 sodversion
[strcspn(sodversion
, "\n")] = '\0';
8701 if (strcmp(version
, sodversion
)) {
8702 if ((f
= freopen(file
, "w", f
)) == NULL
)
8703 err(1, "startofday");
8704 if (fputs(version
, f
) == EOF
)
8707 /* upgrade, say something smart */
8713 /* favorites file */
8714 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_FAVS_FILE
);
8715 if (stat(file
, &sb
)) {
8716 warnx("favorites file doesn't exist, creating it");
8717 if ((f
= fopen(file
, "w")) == NULL
)
8718 err(1, "favorites");
8722 /* quickmarks file */
8723 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
8724 if (stat(file
, &sb
)) {
8725 warnx("quickmarks file doesn't exist, creating it");
8726 if ((f
= fopen(file
, "w")) == NULL
)
8727 err(1, "quickmarks");
8731 /* search history */
8732 if (history_autosave
) {
8733 snprintf(search_file
, sizeof search_file
, "%s" PS
"%s",
8734 work_dir
, XT_SEARCH_FILE
);
8735 if (stat(search_file
, &sb
)) {
8736 warnx("search history file doesn't exist, creating it");
8737 if ((f
= fopen(search_file
, "w")) == NULL
)
8738 err(1, "search_history");
8741 history_read(&shl
, search_file
, &search_history_count
);
8744 /* command history */
8745 if (history_autosave
) {
8746 snprintf(command_file
, sizeof command_file
, "%s" PS
"%s",
8747 work_dir
, XT_COMMAND_FILE
);
8748 if (stat(command_file
, &sb
)) {
8749 warnx("command history file doesn't exist, creating it");
8750 if ((f
= fopen(command_file
, "w")) == NULL
)
8751 err(1, "command_history");
8754 history_read(&chl
, command_file
, &cmd_history_count
);
8760 /* guess_search regex */
8761 if (url_regex
== NULL
)
8762 url_regex
= g_strdup(XT_URL_REGEX
);
8764 if (regcomp(&url_re
, url_regex
, REG_EXTENDED
| REG_NOSUB
))
8765 startpage_add("invalid url regex %s", url_regex
);
8768 env_proxy
= getenv("http_proxy");
8770 setup_proxy(env_proxy
);
8772 env_proxy
= getenv("HTTP_PROXY");
8774 setup_proxy(env_proxy
);
8776 setup_proxy(http_proxy
);
8779 /* the user can optionally have the proxy disabled at startup */
8780 if ((http_proxy_starts_enabled
== 0) && (http_proxy
!= NULL
)) {
8781 http_proxy_save
= g_strdup(http_proxy
);
8785 #ifndef XT_SOCKET_DISABLE
8787 send_cmd_to_socket(argv
[0]);
8791 opte
= opte
; /* shut mingw up */
8793 /* set some connection parameters */
8794 g_object_set(session
, "max-conns", max_connections
, (char *)NULL
);
8795 g_object_set(session
, "max-conns-per-host", max_host_connections
,
8797 g_object_set(session
, SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
8800 g_signal_connect(session
, "request-queued", G_CALLBACK(session_rq_cb
),
8803 #ifndef XT_SOCKET_DISABLE
8804 /* see if there is already a xombrero running */
8805 if (single_instance
&& is_running()) {
8807 warnx("already running");
8812 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
8813 send_cmd_to_socket(cmd
);
8823 optn
= optn
; /* shut mingw up */
8828 if (enable_strict_transport
)
8829 strict_transport_init();
8831 /* uri completion */
8832 completion_model
= gtk_list_store_new(1, G_TYPE_STRING
);
8835 buffers_store
= gtk_list_store_new
8836 (NUM_COLS
, G_TYPE_UINT
, GDK_TYPE_PIXBUF
, G_TYPE_STRING
);
8842 notebook_tab_set_visibility();
8844 if (save_global_history
)
8845 restore_global_history();
8847 /* restore session list */
8848 restore_sessions_list();
8850 if (!strcmp(named_session
, XT_SAVED_TABS_FILE
))
8851 restore_saved_tabs();
8853 a
.s
= named_session
;
8854 a
.i
= XT_SES_DONOTHING
;
8855 open_tabs(NULL
, &a
);
8858 /* see if we have an exception */
8859 if (!TAILQ_EMPTY(&spl
)) {
8860 create_new_tab("about:startpage", NULL
, focus
, -1);
8865 create_new_tab(argv
[0], NULL
, focus
, -1);
8872 if (TAILQ_EMPTY(&tabs
))
8873 create_new_tab(home
, NULL
, 1, -1);
8874 #ifndef XT_SOCKET_DISABLE
8876 if ((s
= build_socket()) != -1) {
8877 channel
= g_io_channel_unix_new(s
);
8878 g_io_add_watch(channel
, G_IO_IN
, socket_watcher
, NULL
);
8882 #if GTK_CHECK_VERSION(3, 0, 0)
8890 gdk_threads_leave();
8891 g_static_rec_mutex_unlock_full(&my_gdk_mtx
); /* just in case */
8894 gnutls_global_deinit();