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 /* autorun some js if enabled */
4272 if (!strncmp(uri
, "http://", strlen("http://")) ||
4273 !strncmp(uri
, "https://", strlen("https://")) ||
4274 !strncmp(uri
, "file://", strlen("file://"))) {
4275 find
.uri
= (gchar
*)uri
;
4276 h
= RB_FIND(history_list
, &hl
, &find
);
4278 insert_history_item(uri
,
4279 get_title(t
, FALSE
), time(NULL
));
4281 h
->time
= time(NULL
);
4284 if (statusbar_style
== XT_STATUSBAR_URL
)
4285 set_status(t
, "%s", (char *)uri
);
4287 set_status(t
, "%s", (char *)get_title(t
, FALSE
));
4288 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
4289 #if GTK_CHECK_VERSION(2, 20, 0)
4290 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
4291 gtk_widget_hide(t
->spinner
);
4295 #if WEBKIT_CHECK_VERSION(1, 1, 18)
4296 case WEBKIT_LOAD_FAILED
:
4298 if (!t
->download_requested
) {
4299 gtk_label_set_text(GTK_LABEL(t
->label
),
4300 get_title(t
, FALSE
));
4301 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
),
4302 get_title(t
, FALSE
));
4303 set_status(t
, "%s", (char *)get_title(t
, FALSE
));
4304 gtk_window_set_title(GTK_WINDOW(main_window
),
4305 get_title(t
, TRUE
));
4311 #if GTK_CHECK_VERSION(2, 20, 0)
4312 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
4313 gtk_widget_hide(t
->spinner
);
4315 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
4319 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
),
4320 t
->item
|| webkit_web_view_can_go_back(t
->wv
));
4322 gtk_widget_set_sensitive(GTK_WIDGET(t
->forward
),
4323 webkit_web_view_can_go_forward(t
->wv
));
4327 notify_title_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
4329 const gchar
*title
= NULL
, *win_title
= NULL
;
4331 title
= get_title(t
, FALSE
);
4332 win_title
= get_title(t
, TRUE
);
4334 gtk_label_set_text(GTK_LABEL(t
->label
), title
);
4335 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
), title
);
4338 if (win_title
&& t
->tab_id
== gtk_notebook_get_current_page(notebook
))
4339 gtk_window_set_title(GTK_WINDOW(main_window
), win_title
);
4343 get_domain(const gchar
*host
)
4348 /* handle silly domains like .co.uk */
4350 if ((x
= strlen(host
)) <= 6)
4351 return (g_strdup(host
));
4353 if (host
[x
- 3] == '.' && host
[x
- 6] == '.') {
4359 return (g_strdup(&host
[x
+ 1]));
4363 p
= g_strrstr(host
, ".");
4365 return (g_strdup(""));
4371 return (g_strdup(p
+ 1));
4373 return (g_strdup(host
));
4377 js_autorun(struct tab
*t
)
4381 size_t got_default
= 0, got_host
= 0;
4383 char deff
[PATH_MAX
], hostf
[PATH_MAX
];
4384 char *js
= NULL
, *jsat
, *domain
= NULL
;
4385 FILE *deffile
= NULL
, *hostfile
= NULL
;
4387 if (enable_js_autorun
== 0)
4392 !(g_str_has_prefix(uri
, "http://") ||
4393 g_str_has_prefix(uri
, "https://")))
4396 su
= soup_uri_new(uri
);
4399 if (!SOUP_URI_VALID_FOR_HTTP(su
))
4402 DNPRINTF(XT_D_JS
, "%s: host: %s domain: %s\n", __func__
,
4404 domain
= get_domain(su
->host
);
4406 snprintf(deff
, sizeof deff
, "%s" PS
"default.js", js_dir
);
4407 if ((deffile
= fopen(deff
, "r")) != NULL
) {
4408 if (fstat(fileno(deffile
), &sb
) == -1) {
4409 show_oops(t
, "can't stat default JS file");
4412 got_default
= sb
.st_size
;
4415 /* try host first followed by domain */
4416 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, su
->host
);
4417 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4418 if ((hostfile
= fopen(hostf
, "r")) == NULL
) {
4419 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, domain
);
4420 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4421 if ((hostfile
= fopen(hostf
, "r")) == NULL
)
4424 DNPRINTF(XT_D_JS
, "file: %s\n", hostf
);
4425 if (fstat(fileno(hostfile
), &sb
) == -1) {
4426 show_oops(t
, "can't stat %s JS file", hostf
);
4429 got_host
= sb
.st_size
;
4432 if (got_default
+ got_host
== 0)
4435 js
= g_malloc0(got_default
+ got_host
+ 1);
4439 if (fread(js
, got_default
, 1, deffile
) != 1) {
4440 show_oops(t
, "default file read error");
4443 jsat
= js
+ got_default
;
4447 if (fread(jsat
, got_host
, 1, hostfile
) != 1) {
4448 show_oops(t
, "host file read error");
4453 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
4454 run_script_locked(t
, js
);
4470 webview_progress_changed_cb(WebKitWebView
*wv
, GParamSpec
*pspec
, struct tab
*t
)
4474 progress
= webkit_web_view_get_progress(wv
);
4475 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.uri
),
4476 progress
== 1.0 ? 0 : progress
);
4477 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->uri_entry
),
4478 progress
== 1.0 ? 0 : progress
);
4480 update_statusbar_position(NULL
, NULL
);
4484 strict_transport_rb_cmp(struct strict_transport
*a
, struct strict_transport
*b
)
4489 /* compare strings from the end */
4490 l1
= strlen(a
->host
);
4491 l2
= strlen(b
->host
);
4495 for (; *p1
== *p2
&& p1
> a
->host
&& p2
> b
->host
;
4500 * Check if we need to do pattern expansion,
4501 * or if we're just keeping the tree in order
4503 if (a
->flags
& XT_STS_FLAGS_EXPAND
&&
4504 b
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) {
4505 /* Check if we're matching the
4506 * 'host.xyz' part in '*.host.xyz'
4508 if (p2
== b
->host
&& (p1
== a
->host
|| *(p1
-1) == '.')) {
4513 if (p1
== a
->host
&& p2
== b
->host
)
4527 RB_GENERATE(strict_transport_tree
, strict_transport
, entry
,
4528 strict_transport_rb_cmp
);
4531 strict_transport_add(const char *domain
, time_t timeout
, int subdomains
)
4533 struct strict_transport
*d
, find
;
4537 if (enable_strict_transport
== FALSE
)
4540 DPRINTF("strict_transport_add(%s,%" PRIi64
",%d)\n", domain
,
4541 (uint64_t)timeout
, subdomains
);
4547 find
.host
= (char *)domain
;
4549 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4553 /* check if update is needed */
4554 if (d
->timeout
== timeout
&&
4555 (d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) == subdomains
)
4558 d
->timeout
= timeout
;
4560 d
->flags
|= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4562 /* We're still initializing */
4563 if (strict_transport_file
== NULL
)
4566 if ((f
= fopen(strict_transport_file
, "w")) == NULL
) {
4568 "can't open strict-transport rules file");
4572 fprintf(f
, "# Generated file - do not update unless you know "
4573 "what you're doing\n");
4574 RB_FOREACH(d
, strict_transport_tree
, &st_tree
) {
4575 if (d
->timeout
< now
)
4577 fprintf(f
, "%s\t%" PRIi64
"\t%d\n", d
->host
,
4578 (uint64_t)d
->timeout
,
4579 d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
);
4583 d
= g_malloc(sizeof *d
);
4584 d
->host
= g_strdup(domain
);
4585 d
->timeout
= timeout
;
4587 d
->flags
= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4590 RB_INSERT(strict_transport_tree
, &st_tree
, d
);
4592 /* We're still initializing */
4593 if (strict_transport_file
== NULL
)
4596 if ((f
= fopen(strict_transport_file
, "a+")) == NULL
) {
4598 "can't open strict-transport rules file");
4602 fseek(f
, 0, SEEK_END
);
4603 fprintf(f
,"%s\t%" PRIi64
"\t%d\n", d
->host
, (uint64_t)timeout
,
4611 strict_transport_check(const char *host
)
4613 static struct strict_transport
*d
= NULL
;
4614 struct strict_transport find
;
4616 if (enable_strict_transport
== FALSE
)
4619 find
.host
= (char *)host
;
4621 /* match for domains that include subdomains */
4622 find
.flags
= XT_STS_FLAGS_EXPAND
;
4624 /* First, check if we're already at the right node */
4625 if (d
!= NULL
&& strict_transport_rb_cmp(&find
, d
) == 0) {
4629 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4637 strict_transport_init()
4639 char file
[PATH_MAX
];
4645 time_t timeout
, now
;
4648 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_STS_FILE
);
4649 if ((f
= fopen(file
, "r")) == NULL
) {
4650 strict_transport_file
= g_strdup(file
);
4661 if ((rule
= fparseln(f
, &len
, NULL
, delim
, 0)) == NULL
) {
4662 if (!feof(f
) || ferror(f
))
4668 /* get second entry */
4669 if ((ptr
= strpbrk(rule
, " \t")) == NULL
)
4673 timeout
= atoi(ptr
);
4675 /* get third entry */
4676 if ((ptr
= strpbrk(ptr
, " \t")) == NULL
)
4680 subdomains
= atoi(ptr
);
4683 strict_transport_add(rule
, timeout
, subdomains
);
4688 strict_transport_file
= g_strdup(file
);
4692 startpage_add("strict-transport rules file ('%s') is corrupt", file
);
4700 force_https_check(const char *uri
)
4702 struct domain
*d
= NULL
;
4707 if ((d
= wl_find_uri(uri
, &force_https
)) == NULL
)
4714 strict_transport_security_cb(SoupMessage
*msg
, gpointer data
)
4720 int subdomains
= FALSE
;
4725 sts
= soup_message_headers_get_one(msg
->response_headers
,
4726 "Strict-Transport-Security");
4727 uri
= soup_message_get_uri(msg
);
4729 if (sts
== NULL
|| uri
== NULL
)
4732 if ((ptr
= strcasestr(sts
, "max-age="))) {
4733 ptr
+= strlen("max-age=");
4734 timeout
= atoll(ptr
);
4736 return; /* malformed header - max-age must be included */
4738 if ((ptr
= strcasestr(sts
, "includeSubDomains")))
4741 strict_transport_add(uri
->host
, timeout
+ time(NULL
), subdomains
);
4745 session_rq_cb(SoupSession
*s
, SoupMessage
*msg
, SoupSocket
*socket
,
4755 if (s
== NULL
|| msg
== NULL
)
4758 if (enable_strict_transport
) {
4759 soup_message_add_header_handler(msg
, "finished",
4760 "Strict-Transport-Security",
4761 G_CALLBACK(strict_transport_security_cb
), NULL
);
4764 if (referer_mode
== XT_REFERER_ALWAYS
)
4767 /* Check if referer is set - and what the user requested for referers */
4768 ref
= soup_message_headers_get_one(msg
->request_headers
, "Referer");
4770 DNPRINTF(XT_D_NAV
, "session_rq_cb: Referer: %s\n", ref
);
4771 switch (referer_mode
) {
4772 case XT_REFERER_NEVER
:
4773 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing referer\n");
4774 soup_message_headers_remove(msg
->request_headers
,
4777 case XT_REFERER_SAME_DOMAIN
:
4778 ref_uri
= soup_uri_new(ref
);
4779 dest
= soup_message_get_uri(msg
);
4781 ref_suffix
= tld_get_suffix(ref_uri
->host
);
4782 dest_suffix
= tld_get_suffix(dest
->host
);
4784 if (dest
&& ref_suffix
&& dest_suffix
&&
4785 strcmp(ref_suffix
, dest_suffix
) != 0) {
4786 soup_message_headers_remove(msg
->request_headers
,
4788 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4789 "referer (not same domain) (suffixes: %s - %s)\n",
4790 ref_suffix
, dest_suffix
);
4792 soup_uri_free(ref_uri
);
4794 case XT_REFERER_SAME_FQDN
:
4795 ref_uri
= soup_uri_new(ref
);
4796 dest
= soup_message_get_uri(msg
);
4797 if (dest
&& strcmp(ref_uri
->host
, dest
->host
) != 0) {
4798 soup_message_headers_remove(msg
->request_headers
,
4800 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4801 "referer (not same fqdn) (should be %s)\n",
4804 soup_uri_free(ref_uri
);
4806 case XT_REFERER_CUSTOM
:
4807 DNPRINTF(XT_D_NAV
, "session_rq_cb: setting referer "
4808 "to %s\n", referer_custom
);
4809 soup_message_headers_replace(msg
->request_headers
,
4810 "Referer", referer_custom
);
4817 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
4818 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
4819 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
4821 WebKitWebNavigationReason reason
;
4822 struct user_agent ua_find
, *ua
;
4826 show_oops(NULL
, "webview_npd_cb invalid parameters");
4830 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
4832 webkit_network_request_get_uri(request
));
4834 uri
= (char *)webkit_network_request_get_uri(request
);
4836 if (valid_url_type(uri
)) {
4837 show_oops(t
, "Stopping attempt to load an invalid URI (possible"
4838 " bait and switch attack)");
4842 /* clear t->item, except if we're switching to an about: page */
4843 if (t
->item
&& !g_str_has_prefix(uri
, "xxxt://")) {
4844 g_object_unref(t
->item
);
4848 if (!auto_load_images
&& t
->load_images
) {
4850 /* Disable autoloading of images, now that we're done loading
4852 g_object_set(G_OBJECT(t
->settings
),
4853 "auto-load-images", FALSE
, (char *)NULL
);
4854 webkit_web_view_set_settings(t
->wv
, t
->settings
);
4856 t
->load_images
= FALSE
;
4859 /* If this is an xtp url, we don't load anything else. */
4860 if (parse_xtp_url(t
, uri
))
4863 if (parse_custom_uri(t
, uri
))
4866 if ((t
->mode
== XT_MODE_HINT
&& t
->new_tab
) || t
->ctrl_click
) {
4868 create_new_tab(uri
, NULL
, ctrl_click_focus
, -1);
4869 webkit_web_policy_decision_ignore(pd
);
4870 return (TRUE
); /* we made the decission */
4873 /* Change user agent if more than one has been given. */
4874 if (!RB_EMPTY(&ua_list
)) {
4875 ua_find
.id
= t
->user_agent_id
;
4877 if ((ua
= RB_FIND(user_agent_list
, &ua_list
, &ua_find
)) == NULL
) {
4879 t
->user_agent_id
= 1;
4880 user_agent
= RB_FIND(user_agent_list
, &ua_list
, &ua_find
);
4886 g_free(t
->user_agent
);
4887 t
->user_agent
= g_strdup(user_agent
->value
);
4889 DNPRINTF(XT_D_NAV
, "user-agent: %s\n", t
->user_agent
);
4891 g_object_set(G_OBJECT(t
->settings
),
4892 "user-agent", t
->user_agent
, (char *)NULL
);
4894 webkit_web_view_set_settings(wv
, t
->settings
);
4898 * This is a little hairy but it comes down to this:
4899 * when we run in whitelist mode we have to assist the browser in
4900 * opening the URL that it would have opened in a new tab.
4902 reason
= webkit_web_navigation_action_get_reason(na
);
4903 if (reason
== WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED
) {
4904 set_normal_tab_meaning(t
);
4905 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1)
4907 webkit_web_policy_decision_use(pd
);
4908 return (TRUE
); /* we made the decision */
4915 webview_rrs_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, WebKitWebResource
*res
,
4916 WebKitNetworkRequest
*request
, WebKitNetworkResponse
*response
,
4919 SoupMessage
*msg
= NULL
;
4920 SoupURI
*uri
= NULL
;
4921 struct http_accept ha_find
, *ha
= NULL
;
4922 const char *accept
= NULL
;
4925 msg
= webkit_network_request_get_message(request
);
4928 uri
= soup_message_get_uri(msg
);
4931 uri_s
= soup_uri_to_string(uri
, FALSE
);
4933 if (strcmp(uri
->scheme
, SOUP_URI_SCHEME_HTTP
) == 0) {
4934 if (strict_transport_check(uri
->host
) ||
4935 force_https_check(uri_s
)) {
4936 DNPRINTF(XT_D_NAV
, "webview_rrs_cb: force https for %s\n",
4938 soup_uri_set_scheme(uri
, SOUP_URI_SCHEME_HTTPS
);
4943 soup_message_headers_append(msg
->request_headers
, "DNT", "1");
4945 /* Round-robin through HTTP Accept headers if any have been set */
4946 if (!RB_EMPTY(&ha_list
)) {
4947 accept
= soup_message_headers_get_list(msg
->request_headers
,
4949 if (accept
== NULL
||
4950 strncmp(accept
, "text/html", strlen("text/html")))
4953 ha_find
.id
= t
->http_accept_id
;
4954 ha
= RB_FIND(http_accept_list
, &ha_list
, &ha_find
);
4957 t
->http_accept_id
= 1;
4958 http_accept
= RB_FIND(http_accept_list
, &ha_list
,
4961 ++t
->http_accept_id
;
4965 soup_message_headers_replace(msg
->request_headers
, "Accept",
4966 http_accept
->value
);
4974 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4977 struct domain
*d
= NULL
;
4979 WebKitWebView
*webview
= NULL
;
4982 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
4983 webkit_web_view_get_uri(wv
));
4986 /* open in current tab */
4988 } else if (enable_scripts
== 0 && enable_js_whitelist
== 1) {
4989 uri
= webkit_web_view_get_uri(wv
);
4990 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4993 if (t
->ctrl_click
) {
4994 x
= ctrl_click_focus
;
4997 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4999 } else if (enable_scripts
== 1) {
5000 if (t
->ctrl_click
) {
5001 x
= ctrl_click_focus
;
5004 tt
= create_new_tab(NULL
, NULL
, x
, -1);
5012 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
5015 struct domain
*d
= NULL
;
5017 DNPRINTF(XT_D_NAV
, "webview_close_cb: %d\n", t
->tab_id
);
5019 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
5020 uri
= webkit_web_view_get_uri(wv
);
5021 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
5025 } else if (enable_scripts
== 1)
5032 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
5034 /* we can not eat the event without throwing gtk off so defer it */
5036 /* catch middle click */
5037 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 2) {
5042 /* catch ctrl click */
5043 if (e
->type
== GDK_BUTTON_RELEASE
&&
5044 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
5049 return (XT_CB_PASSTHROUGH
);
5053 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
5055 struct mime_type
*m
;
5057 GError
*gerr
= NULL
;
5059 m
= find_mime_type(mime_type
);
5065 sv
[0] = m
->mt_action
;
5066 sv
[1] = (char *)webkit_network_request_get_uri(request
);
5069 /* ignore donothing from example config */
5070 if (m
->mt_action
&& !strcmp(m
->mt_action
, "donothing"))
5073 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
, NULL
,
5075 show_oops(t
, "%s: could not spawn process (%s)", __func__
,
5076 gerr
? gerr
->message
: "N/A");
5081 get_mime_type(const char *uri
)
5086 char *mime_type
= NULL
;
5090 show_oops(NULL
, "%s: invalid parameters", __func__
);
5094 if (g_str_has_prefix(uri
, "file://"))
5095 file
= g_filename_from_uri(uri
, NULL
, NULL
);
5097 file
= g_strdup(uri
);
5102 gf
= g_file_new_for_path(file
);
5103 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
5105 if ((m
= g_file_info_get_content_type(fi
)) != NULL
)
5106 mime_type
= g_strdup(m
);
5115 run_download_mimehandler(char *mime_type
, char *file
)
5117 struct mime_type
*m
;
5120 m
= find_mime_type(mime_type
);
5124 sv
[0] = m
->mt_action
;
5127 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
, NULL
,
5129 show_oops(NULL
, "%s: could not spawn process: %s %s", __func__
,
5137 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
5140 WebKitDownloadStatus status
;
5145 if (download
== NULL
)
5147 status
= webkit_download_get_status(download
);
5148 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
5151 if (download_notifications
)
5152 show_oops(NULL
, "Download of '%s' finished",
5153 basename((char *)webkit_download_get_destination_uri(download
)));
5154 uri
= webkit_download_get_destination_uri(download
);
5157 mime
= get_mime_type(uri
);
5161 if (g_str_has_prefix(uri
, "file://"))
5162 file
= g_filename_from_uri(uri
, NULL
, NULL
);
5164 file
= g_strdup(uri
);
5169 run_download_mimehandler((char *)mime
, file
);
5176 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
5177 WebKitNetworkRequest
*request
, char *mime_type
,
5178 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
5181 show_oops(NULL
, "webview_mimetype_cb invalid parameters");
5185 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
5186 t
->tab_id
, mime_type
);
5188 if (run_mimehandler(t
, mime_type
, request
) == 0) {
5189 webkit_web_policy_decision_ignore(decision
);
5194 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
5195 webkit_web_policy_decision_download(decision
);
5203 download_start(struct tab
*t
, struct download
*d
, int flag
)
5205 WebKitNetworkRequest
*req
;
5207 const gchar
*suggested_name
;
5208 gchar
*filename
= NULL
;
5214 if (d
== NULL
|| t
== NULL
) {
5215 show_oops(NULL
, "%s invalid parameters", __func__
);
5219 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
5220 if (suggested_name
== NULL
)
5221 return (FALSE
); /* abort download */
5232 filename
= g_strdup_printf("%d%s", i
, suggested_name
);
5235 /* XXX using urls doesn't work properly in windows? */
5236 uri
= g_strdup_printf("%s\\%s", download_dir
, i
?
5237 filename
: suggested_name
);
5239 path
= g_strdup_printf("%s" PS
"%s", download_dir
, i
?
5240 filename
: suggested_name
);
5241 if ((uri
= g_filename_to_uri(path
, NULL
, NULL
)) == NULL
)
5246 } while (!stat(uri
, &sb
));
5248 } while (!stat(path
, &sb
));
5251 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d filename %s "
5252 "local %s\n", __func__
, t
->tab_id
, filename
, uri
);
5254 /* if we're restarting the download, or starting
5255 * it after doing something else, we need to recreate
5256 * the download request.
5258 if (flag
== XT_DL_RESTART
) {
5259 req
= webkit_network_request_new(webkit_download_get_uri(d
->download
));
5260 webkit_download_cancel(d
->download
);
5261 g_object_unref(d
->download
);
5262 d
->download
= webkit_download_new(req
);
5265 webkit_download_set_destination_uri(d
->download
, uri
);
5267 if (webkit_download_get_status(d
->download
) ==
5268 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
5269 show_oops(t
, "%s: download failed to start", __func__
);
5271 show_oops(t
, "Download Failed");
5273 /* connect "download first" mime handler */
5274 g_signal_connect(G_OBJECT(d
->download
), "notify::status",
5275 G_CALLBACK(download_status_changed_cb
), NULL
);
5277 /* get from history */
5278 g_object_ref(d
->download
);
5279 show_oops(t
, "Download of '%s' started...",
5280 basename((char *)webkit_download_get_destination_uri(d
->download
)));
5283 if (flag
!= XT_DL_START
)
5284 webkit_download_start(d
->download
);
5286 DNPRINTF(XT_D_DOWNLOAD
, "download status : %d",
5287 webkit_download_get_status(d
->download
));
5289 /* sync other download manager tabs */
5290 update_download_tabs(NULL
);
5303 download_ask_cb(struct tab
*t
, GdkEventKey
*e
, gpointer data
)
5305 struct download
*d
= data
;
5309 t
->mode_cb_data
= NULL
;
5312 e
->keyval
= GDK_Escape
;
5313 return (XT_CB_PASSTHROUGH
);
5316 DPRINTF("download_ask_cb: User pressed %c\n", e
->keyval
);
5317 if (e
->keyval
== 'y' || e
->keyval
== 'Y' || e
->keyval
== GDK_Return
)
5318 /* We need to do a RESTART, because we're not calling from
5319 * webview_download_cb
5321 download_start(t
, d
, XT_DL_RESTART
);
5323 /* for all other keyvals, we just let the download be */
5324 e
->keyval
= GDK_Escape
;
5325 return (XT_CB_HANDLED
);
5329 download_ask(struct tab
*t
, struct download
*d
)
5331 const gchar
*suggested_name
;
5333 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
5334 if (suggested_name
== NULL
)
5335 return (FALSE
); /* abort download */
5337 show_oops(t
, "download file %s [y/n] ?", suggested_name
);
5338 t
->mode_cb
= download_ask_cb
;
5339 t
->mode_cb_data
= d
;
5345 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
5348 const gchar
*suggested_name
;
5349 struct download
*download_entry
;
5352 if (wk_download
== NULL
|| t
== NULL
) {
5353 show_oops(NULL
, "%s invalid parameters", __func__
);
5357 suggested_name
= webkit_download_get_suggested_filename(wk_download
);
5358 if (suggested_name
== NULL
)
5359 return (FALSE
); /* abort download */
5361 download_entry
= g_malloc(sizeof(struct download
));
5362 download_entry
->download
= wk_download
;
5363 download_entry
->tab
= t
;
5364 download_entry
->id
= next_download_id
++;
5365 RB_INSERT(download_list
, &downloads
, download_entry
);
5366 t
->download_requested
= 1;
5368 if (download_mode
== XT_DM_START
)
5369 ret
= download_start(t
, download_entry
, XT_DL_START
);
5370 else if (download_mode
== XT_DM_ASK
)
5371 ret
= download_ask(t
, download_entry
);
5372 else if (download_mode
== XT_DM_ADD
)
5373 show_oops(t
, "added %s to download manager",
5376 /* sync other download manager tabs */
5377 update_download_tabs(NULL
);
5380 * NOTE: never redirect/render the current tab before this
5381 * function returns. This will cause the download to never start.
5383 return (ret
); /* start download */
5387 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
5389 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
5392 show_oops(NULL
, "webview_hover_cb");
5397 set_status(t
, "Link: %s", uri
);
5399 if (statusbar_style
== XT_STATUSBAR_URL
) {
5400 const gchar
*page_uri
;
5402 if ((page_uri
= get_uri(t
)) != NULL
)
5403 set_status(t
, "%s", page_uri
);
5405 set_status(t
, "%s", (char *)get_title(t
, FALSE
));
5410 mark(struct tab
*t
, struct karg
*arg
)
5417 if ((index
= marktoindex(mark
)) == -1)
5420 if (arg
->i
== XT_MARK_SET
)
5421 t
->mark
[index
] = gtk_adjustment_get_value(t
->adjust_v
);
5422 else if (arg
->i
== XT_MARK_GOTO
) {
5423 if (t
->mark
[index
] == XT_INVALID_MARK
) {
5424 show_oops(t
, "mark '%c' does not exist", mark
);
5427 /* XXX t->mark[index] can be bigger than the maximum if ajax or
5428 something changes the document size */
5429 pos
= gtk_adjustment_get_value(t
->adjust_v
);
5430 gtk_adjustment_set_value(t
->adjust_v
, t
->mark
[index
]);
5431 t
->mark
[marktoindex('\'')] = pos
;
5438 marks_clear(struct tab
*t
)
5442 for (i
= 0; i
< LENGTH(t
->mark
); i
++)
5443 t
->mark
[i
] = XT_INVALID_MARK
;
5449 char file
[PATH_MAX
];
5450 char *line
= NULL
, *p
;
5455 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
5456 if ((f
= fopen(file
, "r+")) == NULL
) {
5457 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
5461 for (i
= 1; ; i
++) {
5462 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
5464 if (strlen(line
) == 0 || line
[0] == '#') {
5470 p
= strtok(line
, " \t");
5472 if (p
== NULL
|| strlen(p
) != 1 ||
5473 (index
= qmarktoindex(*p
)) == -1) {
5474 warnx("corrupt quickmarks file, line %d", i
);
5478 p
= strtok(NULL
, " \t");
5479 if (qmarks
[index
] != NULL
)
5480 g_free(qmarks
[index
]);
5481 qmarks
[index
] = g_strdup(p
);
5492 char file
[PATH_MAX
];
5496 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
5497 if ((f
= fopen(file
, "r+")) == NULL
) {
5498 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
5502 for (i
= 0; i
< XT_NOQMARKS
; i
++)
5503 if (qmarks
[i
] != NULL
)
5504 fprintf(f
, "%c %s\n", indextoqmark(i
), qmarks
[i
]);
5512 qmark(struct tab
*t
, struct karg
*arg
)
5517 mark
= arg
->s
[strlen(arg
->s
)-1];
5518 index
= qmarktoindex(mark
);
5524 if (qmarks
[index
] != NULL
) {
5525 g_free(qmarks
[index
]);
5526 qmarks
[index
] = NULL
;
5529 qmarks_load(); /* sync if multiple instances */
5530 qmarks
[index
] = g_strdup(get_uri(t
));
5534 if (qmarks
[index
] != NULL
)
5535 load_uri(t
, qmarks
[index
]);
5537 show_oops(t
, "quickmark \"%c\" does not exist",
5543 if (qmarks
[index
] != NULL
)
5544 create_new_tab(qmarks
[index
], NULL
, 1, -1);
5546 show_oops(t
, "quickmark \"%c\" does not exist",
5557 go_up(struct tab
*t
, struct karg
*args
)
5565 if (args
->i
== XT_GO_UP_ROOT
)
5566 levels
= XT_GO_UP_ROOT
;
5567 else if ((levels
= atoi(args
->s
)) == 0)
5570 uri
= g_strdup(get_uri(t
));
5574 if ((tmp
= strstr(uri
, XT_PROTO_DELIM
)) == NULL
)
5577 tmp
+= strlen(XT_PROTO_DELIM
);
5579 /* it makes no sense to strip the last slash from ".../dir/", skip it */
5580 lastidx
= strlen(tmp
) - 1;
5582 if (tmp
[lastidx
] == '/')
5583 tmp
[lastidx
] = '\0';
5587 p
= strrchr(tmp
, '/');
5588 if (p
== tmp
) { /* Are we at the root of a file://-path? */
5591 } else if (p
!= NULL
)
5604 gototab(struct tab
*t
, struct karg
*args
)
5607 struct karg arg
= {0, NULL
, -1};
5609 tab
= atoi(args
->s
);
5612 arg
.i
= XT_TAB_NEXT
;
5624 zoom_amount(struct tab
*t
, struct karg
*arg
)
5626 struct karg narg
= {0, NULL
, -1};
5628 narg
.i
= atoi(arg
->s
);
5629 resizetab(t
, &narg
);
5635 flip_colon(struct tab
*t
, struct karg
*arg
)
5637 struct karg narg
= {0, NULL
, -1};
5640 if (t
== NULL
|| arg
== NULL
)
5643 p
= strstr(arg
->s
, ":");
5655 /* buffer commands receive the regex that triggered them in arg.s */
5656 char bcmd
[XT_BUFCMD_SZ
];
5660 #define XT_PRE_NO (0)
5661 #define XT_PRE_YES (1)
5662 #define XT_PRE_MAYBE (2)
5664 int (*func
)(struct tab
*, struct karg
*);
5668 { "^[0-9]*gu$", XT_PRE_MAYBE
, "gu", go_up
, 0 },
5669 { "^gU$", XT_PRE_NO
, "gU", go_up
, XT_GO_UP_ROOT
},
5670 { "^gg$", XT_PRE_NO
, "gg", move
, XT_MOVE_TOP
},
5671 { "^gG$", XT_PRE_NO
, "gG", move
, XT_MOVE_BOTTOM
},
5672 { "^[0-9]+%$", XT_PRE_YES
, "%", move
, XT_MOVE_PERCENT
},
5673 { "^zz$", XT_PRE_NO
, "zz", move
, XT_MOVE_CENTER
},
5674 { "^gh$", XT_PRE_NO
, "gh", go_home
, 0 },
5675 { "^m[a-zA-Z0-9]$", XT_PRE_NO
, "m", mark
, XT_MARK_SET
},
5676 { "^['][a-zA-Z0-9']$", XT_PRE_NO
, "'", mark
, XT_MARK_GOTO
},
5677 { "^[0-9]+t$", XT_PRE_YES
, "t", gototab
, 0 },
5678 { "^g0$", XT_PRE_YES
, "g0", movetab
, XT_TAB_FIRST
},
5679 { "^g[$]$", XT_PRE_YES
, "g$", movetab
, XT_TAB_LAST
},
5680 { "^[0-9]*gt$", XT_PRE_YES
, "t", movetab
, XT_TAB_NEXT
},
5681 { "^[0-9]*gT$", XT_PRE_YES
, "T", movetab
, XT_TAB_PREV
},
5682 { "^M[a-zA-Z0-9]$", XT_PRE_NO
, "M", qmark
, XT_QMARK_SET
},
5683 { "^go[a-zA-Z0-9]$", XT_PRE_NO
, "go", qmark
, XT_QMARK_OPEN
},
5684 { "^gn[a-zA-Z0-9]$", XT_PRE_NO
, "gn", qmark
, XT_QMARK_TAB
},
5685 { "^ZR$", XT_PRE_NO
, "ZR", restart
, 0 },
5686 { "^ZZ$", XT_PRE_NO
, "ZZ", quit
, 0 },
5687 { "^zi$", XT_PRE_NO
, "zi", resizetab
, XT_ZOOM_IN
},
5688 { "^zo$", XT_PRE_NO
, "zo", resizetab
, XT_ZOOM_OUT
},
5689 { "^z0$", XT_PRE_NO
, "z0", resizetab
, XT_ZOOM_NORMAL
},
5690 { "^[0-9]+Z$", XT_PRE_YES
, "Z", zoom_amount
, 0 },
5691 { "^[0-9]+:$", XT_PRE_YES
, ":", flip_colon
, 0 },
5695 buffercmd_init(void)
5699 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5700 if (regcomp(&buffercmds
[i
].cregex
, buffercmds
[i
].regex
,
5701 REG_EXTENDED
| REG_NOSUB
))
5702 startpage_add("invalid buffercmd regex %s",
5703 buffercmds
[i
].regex
);
5707 buffercmd_abort(struct tab
*t
)
5714 DNPRINTF(XT_D_BUFFERCMD
, "%s: clearing buffer\n", __func__
);
5716 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5719 cmd_prefix
= 0; /* clear prefix for non-buffer commands */
5720 gtk_label_set_text(GTK_LABEL(t
->sbe
.buffercmd
), bcmd
);
5724 buffercmd_execute(struct tab
*t
, struct buffercmd
*cmd
)
5726 struct karg arg
= {0, NULL
, -1};
5729 arg
.s
= g_strdup(bcmd
);
5731 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_execute: buffer \"%s\" "
5732 "matches regex \"%s\", executing\n", bcmd
, cmd
->regex
);
5742 buffercmd_addkey(struct tab
*t
, guint keyval
)
5745 char s
[XT_BUFCMD_SZ
];
5747 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
))) {
5749 return (XT_CB_PASSTHROUGH
);
5752 if (keyval
== GDK_Escape
) {
5754 return (XT_CB_HANDLED
);
5757 /* key with modifier or non-ascii character */
5758 if (!isascii(keyval
)) {
5760 * XXX this looks wrong but fixes some sites like
5761 * http://www.seslisozluk.com/
5762 * that eat a shift or ctrl and end putting default focus in js
5763 * instead of ignoring the keystroke
5764 * so instead of return (XT_CB_PASSTHROUGH); eat the key
5766 return (XT_CB_HANDLED
);
5769 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: adding key \"%c\" "
5770 "to buffer \"%s\"\n", keyval
, bcmd
);
5772 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5773 if (bcmd
[i
] == '\0') {
5778 /* buffer full, ignore input */
5779 if (i
>= LENGTH(bcmd
) -1) {
5780 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: buffer full\n");
5782 return (XT_CB_HANDLED
);
5785 gtk_label_set_text(GTK_LABEL(t
->sbe
.buffercmd
), bcmd
);
5787 /* find exact match */
5788 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5789 if (regexec(&buffercmds
[i
].cregex
, bcmd
,
5790 (size_t) 0, NULL
, 0) == 0) {
5791 buffercmd_execute(t
, &buffercmds
[i
]);
5795 /* find non exact matches to see if we need to abort ot not */
5796 for (i
= 0, match
= 0; i
< LENGTH(buffercmds
); i
++) {
5797 DNPRINTF(XT_D_BUFFERCMD
, "trying: %s\n", bcmd
);
5800 if (buffercmds
[i
].precount
== XT_PRE_MAYBE
) {
5801 if (isdigit(bcmd
[0])) {
5802 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5806 if (sscanf(bcmd
, "%s", s
) == 0)
5809 } else if (buffercmds
[i
].precount
== XT_PRE_YES
) {
5810 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5813 if (sscanf(bcmd
, "%s", s
) == 0)
5816 if (c
== -1 && buffercmds
[i
].precount
)
5818 if (!strncmp(s
, buffercmds
[i
].cmd
, strlen(s
)))
5821 DNPRINTF(XT_D_BUFFERCMD
, "got[%d] %d <%s>: %d %s\n",
5822 i
, match
, buffercmds
[i
].cmd
, c
, s
);
5825 DNPRINTF(XT_D_BUFFERCMD
, "aborting: %s\n", bcmd
);
5830 return (XT_CB_HANDLED
);
5834 * XXX we were seeing a bunch of focus issues with the toplevel
5835 * main_window losing its is-active and has-toplevel-focus properties.
5836 * This is the most correct and portable solution we could come up with
5837 * without relying on calling internal GTK functions (which we
5838 * couldn't link to in Linux).
5841 fake_focus_in(GtkWidget
*w
)
5843 if (fevent
== NULL
) {
5844 fevent
= gdk_event_new(GDK_FOCUS_CHANGE
);
5845 fevent
->focus_change
.window
=
5846 gtk_widget_get_window(main_window
);
5847 fevent
->focus_change
.type
= GDK_FOCUS_CHANGE
;
5848 fevent
->focus_change
.in
= TRUE
;
5850 gtk_widget_send_focus_change(main_window
, fevent
);
5854 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
5857 struct key_binding
*k
;
5860 * This sometimes gets randomly unset for whatever reason in GTK3.
5861 * If we're handling a keypress, the main window's is-active propery
5862 * *must* be true, or else many things will break.
5864 #if GTK_CHECK_VERSION(3, 0, 0)
5865 fake_focus_in(main_window
);
5868 /* handle keybindings if buffercmd is empty.
5869 if not empty, allow commands like C-n */
5870 if (bcmd
[0] == '\0' || ((e
->state
& (CTRL
| MOD1
)) != 0))
5871 TAILQ_FOREACH(k
, &kbl
, entry
)
5872 if (e
->keyval
== k
->key
5873 && (entry
? k
->use_in_entry
: 1)) {
5874 /* when we are edditing eat ctrl/mod keys */
5875 if (edit_mode
== XT_EM_VI
&&
5876 t
->mode
== XT_MODE_INSERT
&&
5877 (e
->state
& CTRL
|| e
->state
& MOD1
))
5878 return (XT_CB_PASSTHROUGH
);
5881 if ((e
->state
& (CTRL
| MOD1
)) == 0)
5883 } else if ((e
->state
& k
->mask
) == k
->mask
) {
5888 if (!entry
&& ((e
->state
& (CTRL
| MOD1
)) == 0))
5889 return (buffercmd_addkey(t
, e
->keyval
));
5891 return (XT_CB_PASSTHROUGH
);
5894 if (k
->cmd
[0] == ':') {
5896 args
.s
= &k
->cmd
[1];
5897 return (command(t
, &args
));
5899 return (cmd_execute(t
, k
->cmd
));
5903 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5907 /* don't use w directly; use t->whatever instead */
5910 show_oops(NULL
, "wv_keypress_cb");
5911 return (XT_CB_PASSTHROUGH
);
5917 return (t
->mode_cb(t
, e
, t
->mode_cb_data
));
5919 DNPRINTF(XT_D_KEY
, "wv_keypress_cb: mode %d keyval 0x%x mask "
5920 "0x%x tab %d\n", t
->mode
, e
->keyval
, e
->state
, t
->tab_id
);
5922 /* Hide buffers, if they are visible, with escape. */
5923 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)) &&
5924 CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
5925 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5927 return (XT_CB_HANDLED
);
5930 if (t
->mode
== XT_MODE_HINT
)
5931 return (XT_CB_HANDLED
);
5933 if ((CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Tab
) ||
5934 (CLEAN(e
->state
) == SHFT
&& e
->keyval
== GDK_Tab
))
5935 /* something focussy is about to happen */
5936 return (XT_CB_PASSTHROUGH
);
5938 /* check if we are some sort of text input thing in the dom */
5939 input_check_mode(t
);
5941 if (t
->mode
== XT_MODE_HINT
) {
5942 /* XXX make sure cmd entry is enabled */
5943 return (XT_CB_HANDLED
);
5944 } else if (t
->mode
== XT_MODE_PASSTHROUGH
) {
5945 if (CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
)
5946 t
->mode
= XT_MODE_COMMAND
;
5947 return (XT_CB_PASSTHROUGH
);
5948 } else if (t
->mode
== XT_MODE_COMMAND
) {
5950 snprintf(s
, sizeof s
, "%c", e
->keyval
);
5951 if (CLEAN(e
->state
) == 0 && isdigit(s
[0]))
5952 cmd_prefix
= 10 * cmd_prefix
+ atoi(s
);
5953 return (handle_keypress(t
, e
, 0));
5956 return (handle_keypress(t
, e
, 1));
5960 return (XT_CB_PASSTHROUGH
);
5964 hint_continue(struct tab
*t
)
5966 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5968 const gchar
*errstr
= NULL
;
5972 if (!(c
[0] == '.' || c
[0] == ','))
5974 if (strlen(c
) == 1) {
5975 /* XXX should not happen */
5980 if (isdigit(c
[1])) {
5982 i
= strtonum(&c
[1], 1, 4096, &errstr
);
5984 show_oops(t
, "invalid numerical hint %s", &c
[1]);
5987 s
= g_strdup_printf("hints.updateHints(%d);", i
);
5991 /* alphanumeric input */
5992 s
= g_strdup_printf("hints.createHints('%s', '%c');",
5993 &c
[1], c
[0] == '.' ? 'f' : 'F');
6004 search_continue(struct tab
*t
)
6006 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
6007 gboolean rv
= FALSE
;
6009 if (c
[0] == ':' || c
[0] == '.' || c
[0] == ',')
6011 if (strlen(c
) == 1) {
6012 webkit_web_view_unmark_text_matches(t
->wv
);
6017 t
->search_forward
= TRUE
;
6018 else if (c
[0] == '?')
6019 t
->search_forward
= FALSE
;
6029 search_cb(struct tab
*t
)
6031 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
6032 #if !GTK_CHECK_VERSION(3, 0, 0)
6036 if (search_continue(t
) == FALSE
)
6040 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, t
->search_forward
,
6042 /* not found, mark red */
6043 #if GTK_CHECK_VERSION(3, 0, 0)
6044 gtk_widget_set_name(t
->cmd
, XT_CSS_RED
);
6046 gdk_color_parse(XT_COLOR_RED
, &color
);
6047 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
6049 /* unmark and remove selection */
6050 webkit_web_view_unmark_text_matches(t
->wv
);
6051 /* my kingdom for a way to unselect text in webview */
6053 /* found, highlight all */
6054 webkit_web_view_unmark_text_matches(t
->wv
);
6055 webkit_web_view_mark_text_matches(t
->wv
, &c
[1], FALSE
, 0);
6056 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
6057 #if GTK_CHECK_VERSION(3, 0, 0)
6058 gtk_widget_set_name(t
->cmd
, XT_CSS_NORMAL
);
6060 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
6061 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
6070 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
6072 const gchar
*c
= gtk_entry_get_text(w
);
6075 show_oops(NULL
, "cmd_keyrelease_cb invalid parameters");
6076 return (XT_CB_PASSTHROUGH
);
6079 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
6080 e
->keyval
, e
->state
, t
->tab_id
);
6083 if (!(e
->keyval
== GDK_Tab
|| e
->keyval
== GDK_ISO_Left_Tab
)) {
6084 if (hint_continue(t
) == FALSE
)
6089 if (search_continue(t
) == FALSE
)
6092 /* if search length is > 4 then no longer play timeout games */
6093 if (strlen(c
) > 4) {
6095 g_source_remove(t
->search_id
);
6102 /* reestablish a new timer if the user types fast */
6104 g_source_remove(t
->search_id
);
6105 t
->search_id
= g_timeout_add(250, (GSourceFunc
)search_cb
, (gpointer
)t
);
6108 return (XT_CB_PASSTHROUGH
);
6112 match_uri(const gchar
*uri
, const gchar
*key
) {
6115 gboolean match
= FALSE
;
6119 if (!strncmp(key
, uri
, len
))
6122 voffset
= strstr(uri
, "/") + 2;
6123 if (!strncmp(key
, voffset
, len
))
6125 else if (g_str_has_prefix(voffset
, "www.")) {
6126 voffset
= voffset
+ strlen("www.");
6127 if (!strncmp(key
, voffset
, len
))
6136 match_session(const gchar
*name
, const gchar
*key
) {
6139 sub
= strcasestr(name
, key
);
6145 cmd_getlist(int id
, char *key
)
6152 if (cmds
[id
].type
& XT_URLARG
) {
6153 RB_FOREACH_REVERSE(h
, history_list
, &hl
)
6154 if (match_uri(h
->uri
, key
)) {
6155 cmd_status
.list
[c
] = (char *)h
->uri
;
6161 } else if (cmds
[id
].type
& XT_SESSARG
) {
6162 TAILQ_FOREACH(s
, &sessions
, entry
)
6163 if (match_session(s
->name
, key
)) {
6164 cmd_status
.list
[c
] = (char *)s
->name
;
6170 } else if (cmds
[id
].type
& XT_SETARG
) {
6171 for (i
= 0; i
< get_settings_size(); i
++)
6172 if (!strncmp(key
, get_setting_name(i
),
6174 cmd_status
.list
[c
++] =
6175 get_setting_name(i
);
6181 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
6183 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
6184 if (cmds
[i
].level
< dep
)
6186 if (cmds
[i
].level
== dep
&& !strncmp(key
, cmds
[i
].cmd
,
6187 strlen(key
)) && !isdigit(cmds
[i
].cmd
[0]))
6188 cmd_status
.list
[c
++] = cmds
[i
].cmd
;
6196 cmd_getnext(int dir
)
6198 cmd_status
.index
+= dir
;
6200 if (cmd_status
.index
< 0)
6201 cmd_status
.index
= cmd_status
.len
- 1;
6202 else if (cmd_status
.index
>= cmd_status
.len
)
6203 cmd_status
.index
= 0;
6205 return cmd_status
.list
[cmd_status
.index
];
6209 cmd_tokenize(char *s
, char *tokens
[])
6212 char *tok
, *last
= NULL
;
6213 size_t len
= strlen(s
);
6216 blank
= len
== 0 || (len
> 0 && s
[len
- 1] == ' ');
6217 for (tok
= strtok_r(s
, " ", &last
); tok
&& i
< 3;
6218 tok
= strtok_r(NULL
, " ", &last
), i
++)
6228 cmd_complete(struct tab
*t
, char *str
, int dir
)
6230 GtkEntry
*w
= GTK_ENTRY(t
->cmd
);
6231 int i
, j
, levels
, c
= 0, dep
= 0, parent
= -1;
6233 char *tok
, *match
, *s
= g_strdup(str
);
6235 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
6238 DNPRINTF(XT_D_CMD
, "%s: complete %s\n", __func__
, str
);
6241 for (i
= 0; isdigit(s
[i
]); i
++)
6244 for (; isspace(s
[i
]); i
++)
6249 levels
= cmd_tokenize(s
, tokens
);
6251 for (i
= 0; i
< levels
- 1; i
++) {
6254 for (j
= c
; j
< LENGTH(cmds
); j
++) {
6255 if (cmds
[j
].level
< dep
)
6257 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
,
6261 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
6268 if (matchcount
== 1) {
6269 strlcat(res
, tok
, sizeof res
);
6270 strlcat(res
, " ", sizeof res
);
6280 if (cmd_status
.index
== -1)
6281 cmd_getlist(parent
, tokens
[i
]);
6283 if (cmd_status
.len
> 0) {
6284 match
= cmd_getnext(dir
);
6285 strlcat(res
, match
, sizeof res
);
6286 gtk_entry_set_text(w
, res
);
6287 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6294 parse_prefix_and_alias(const char *str
, int *prefix
)
6296 struct cmd_alias
*c
;
6297 char *s
= g_strdup(str
), *sc
;
6302 if (isdigit(s
[0])) {
6303 sscanf(s
, "%d", prefix
);
6304 while (isdigit(s
[0]) || isspace(s
[0]))
6308 TAILQ_FOREACH(c
, &cal
, entry
) {
6309 if (strncmp(s
, c
->alias
, strlen(c
->alias
)))
6312 if (strlen(s
) == strlen(c
->alias
)) {
6314 return (g_strdup(c
->cmd
));
6317 if (!isspace(s
[strlen(c
->alias
)]))
6320 s
= g_strdup_printf("%s %s", c
->cmd
, &s
[strlen(c
->alias
) + 1]);
6330 cmd_execute(struct tab
*t
, char *str
)
6332 struct cmd
*cmd
= NULL
;
6333 char *tok
, *last
= NULL
, *s
= str
;
6334 int j
= 0, len
, c
= 0, dep
= 0, matchcount
= 0;
6335 int prefix
= -1, rv
= XT_CB_PASSTHROUGH
;
6336 struct karg arg
= {0, NULL
, -1};
6338 s
= parse_prefix_and_alias(s
, &prefix
);
6340 for (tok
= strtok_r(s
, " ", &last
); tok
;
6341 tok
= strtok_r(NULL
, " ", &last
)) {
6343 for (j
= c
; j
< LENGTH(cmds
); j
++) {
6344 if (cmds
[j
].level
< dep
)
6346 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1 :
6348 if (cmds
[j
].level
== dep
&&
6349 !strncmp(tok
, cmds
[j
].cmd
, len
)) {
6353 if (len
== strlen(cmds
[j
].cmd
)) {
6359 if (matchcount
== 1) {
6364 show_oops(t
, "Invalid command: %s", str
);
6370 show_oops(t
, "Empty command");
6376 arg
.precount
= prefix
;
6377 else if (cmd_prefix
> 0)
6378 arg
.precount
= cmd_prefix
;
6380 if (j
> 0 && !(cmd
->type
& XT_PREFIX
) && arg
.precount
> -1) {
6381 show_oops(t
, "No prefix allowed: %s", str
);
6385 arg
.s
= last
? g_strdup(last
) : g_strdup("");
6386 if (cmd
->type
& XT_INTARG
&& last
&& strlen(last
) > 0) {
6387 if (arg
.s
== NULL
) {
6388 show_oops(t
, "Invalid command");
6391 arg
.precount
= atoi(arg
.s
);
6392 if (arg
.precount
<= 0) {
6393 if (arg
.s
[0] == '0')
6394 show_oops(t
, "Zero count");
6396 show_oops(t
, "Trailing characters");
6401 DNPRINTF(XT_D_CMD
, "%s: prefix %d arg %s\n",
6402 __func__
, arg
.precount
, arg
.s
);
6418 save_runtime_setting(const char *name
, const char *val
)
6424 char file
[PATH_MAX
];
6425 char delim
[3] = { '\0', '\0', '\0' };
6426 char *line
, *lt
, *start
;
6427 char *contents
, *tmp
;
6429 if (runtime_settings
== NULL
|| strlen(runtime_settings
) == 0)
6432 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, runtime_settings
);
6433 if (stat(file
, &sb
) || (f
= fopen(file
, "r+")) == NULL
)
6435 lt
= g_strdup_printf("%s=%s", name
, val
);
6436 contents
= g_strdup("");
6438 line
= fparseln(f
, &linelen
, NULL
, delim
, 0);
6439 if (line
== NULL
|| linelen
== 0)
6442 start
= g_strdup_printf("%s=", name
);
6443 if (strstr(line
, start
) == NULL
)
6444 contents
= g_strdup_printf("%s%s\n", contents
, line
);
6447 contents
= g_strdup_printf("%s%s\n", contents
, lt
);
6456 contents
= g_strdup_printf("%s%s\n", contents
, lt
);
6459 if ((f
= freopen(file
, "w", f
)) == NULL
)
6472 entry_focus_cb(GtkWidget
*w
, GdkEvent e
, struct tab
*t
)
6475 * This sometimes gets randomly unset for whatever reason in GTK3,
6476 * causing a GtkEntry's text cursor becomes invisible. When we focus
6477 * a GtkEntry, be sure to manually reset the main window's is-active
6478 * property so the cursor is shown correctly.
6480 #if GTK_CHECK_VERSION(3, 0, 0)
6481 fake_focus_in(main_window
);
6483 return (XT_CB_PASSTHROUGH
);
6487 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
6490 show_oops(NULL
, "entry_key_cb invalid parameters");
6491 return (XT_CB_PASSTHROUGH
);
6494 DNPRINTF(XT_D_CMD
, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
6495 e
->keyval
, e
->state
, t
->tab_id
);
6499 if (e
->keyval
== GDK_Escape
) {
6500 /* don't use focus_webview(t) because we want to type :cmds */
6501 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6504 return (handle_keypress(t
, e
, 1));
6507 struct command_entry
*
6508 history_prev(struct command_list
*l
, struct command_entry
*at
)
6511 at
= TAILQ_LAST(l
, command_list
);
6513 at
= TAILQ_PREV(at
, command_list
, entry
);
6515 at
= TAILQ_LAST(l
, command_list
);
6521 struct command_entry
*
6522 history_next(struct command_list
*l
, struct command_entry
*at
)
6525 at
= TAILQ_FIRST(l
);
6527 at
= TAILQ_NEXT(at
, entry
);
6529 at
= TAILQ_FIRST(l
);
6536 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
6538 int rv
= XT_CB_HANDLED
;
6539 const gchar
*c
= gtk_entry_get_text(w
);
6543 show_oops(NULL
, "cmd_keypress_cb parameters");
6544 return (XT_CB_PASSTHROUGH
);
6547 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
6548 e
->keyval
, e
->state
, t
->tab_id
);
6552 e
->keyval
= GDK_Escape
;
6553 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
6554 c
[0] == '.' || c
[0] == ','))
6555 e
->keyval
= GDK_Escape
;
6557 if (e
->keyval
!= GDK_Tab
&& e
->keyval
!= GDK_Shift_L
&&
6558 e
->keyval
!= GDK_ISO_Left_Tab
)
6559 cmd_status
.index
= -1;
6561 switch (e
->keyval
) {
6564 cmd_complete(t
, (char *)&c
[1], 1);
6565 else if (c
[0] == '.' || c
[0] == ',')
6566 run_script(t
, "hints.focusNextHint();");
6568 case GDK_ISO_Left_Tab
:
6570 cmd_complete(t
, (char *)&c
[1], -1);
6571 else if (c
[0] == '.' || c
[0] == ',')
6572 run_script(t
, "hints.focusPreviousHint();");
6576 if ((search_at
= history_next(&shl
, search_at
))) {
6577 search_at
->line
[0] = c
[0];
6578 gtk_entry_set_text(w
, search_at
->line
);
6579 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6581 } else if (c
[0] == '/') {
6582 if ((search_at
= history_prev(&shl
, search_at
))) {
6583 search_at
->line
[0] = c
[0];
6584 gtk_entry_set_text(w
, search_at
->line
);
6585 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6587 } else if (c
[0] == ':') {
6588 if ((history_at
= history_prev(&chl
, history_at
))) {
6589 history_at
->line
[0] = c
[0];
6590 gtk_entry_set_text(w
, history_at
->line
);
6591 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6597 if ((search_at
= history_next(&shl
, search_at
))) {
6598 search_at
->line
[0] = c
[0];
6599 gtk_entry_set_text(w
, search_at
->line
);
6600 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6602 } else if (c
[0] == '?') {
6603 if ((search_at
= history_prev(&shl
, search_at
))) {
6604 search_at
->line
[0] = c
[0];
6605 gtk_entry_set_text(w
, search_at
->line
);
6606 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6608 } if (c
[0] == ':') {
6609 if ((history_at
= history_next(&chl
, history_at
))) {
6610 history_at
->line
[0] = c
[0];
6611 gtk_entry_set_text(w
, history_at
->line
);
6612 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6617 if (!(!strcmp(c
, ":") || !strcmp(c
, "/") || !strcmp(c
, "?") ||
6618 !strcmp(c
, ".") || !strcmp(c
, ","))) {
6619 /* see if we are doing hinting and reset it */
6620 if (c
[0] == '.' || c
[0] == ',') {
6621 /* recreate hints */
6622 s
= g_strdup_printf("hints.createHints('', "
6623 "'%c');", c
[0] == '.' ? 'f' : 'F');
6635 if (c
!= NULL
&& (c
[0] == '/' || c
[0] == '?'))
6636 webkit_web_view_unmark_text_matches(t
->wv
);
6638 /* no need to cancel hints */
6642 rv
= XT_CB_PASSTHROUGH
;
6648 wv_popup_activ_cb(GtkMenuItem
*menu
, struct tab
*t
)
6650 GtkAction
*a
= NULL
;
6651 GtkClipboard
*clipboard
, *primary
;
6652 const gchar
*name
, *uri
;
6654 a
= gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu
));
6657 name
= gtk_action_get_name(a
);
6659 DNPRINTF(XT_D_CMD
, "wv_popup_activ_cb: tab %d action %s\n",
6663 * context-menu-action-3 copy link location
6664 * context-menu-action-7 copy image address
6665 * context-menu-action-2030 copy video link location
6669 if ((g_strcmp0(name
, "context-menu-action-3") == 0) ||
6670 (g_strcmp0(name
, "context-menu-action-7") == 0) ||
6671 (g_strcmp0(name
, "context-menu-action-2030") == 0)) {
6672 clipboard
= gtk_clipboard_get(GDK_SELECTION_CLIPBOARD
);
6673 primary
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
6674 uri
= gtk_clipboard_wait_for_text(clipboard
);
6677 gtk_clipboard_set_text(primary
, uri
, -1);
6682 wv_popup_cb(WebKitWebView
*wview
, GtkMenu
*menu
, struct tab
*t
)
6686 DNPRINTF(XT_D_CMD
, "wv_popup_cb: tab %d\n", t
->tab_id
);
6688 items
= gtk_container_get_children(GTK_CONTAINER(menu
));
6689 for (l
= items
; l
; l
= l
->next
)
6690 g_signal_connect(l
->data
, "activate",
6691 G_CALLBACK(wv_popup_activ_cb
), t
);
6696 cmd_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
6698 /* popup menu enabled */
6703 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
6706 show_oops(NULL
, "cmd_focusout_cb invalid parameters");
6707 return (XT_CB_PASSTHROUGH
);
6710 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d popup %d\n",
6711 t
->tab_id
, t
->popup
);
6713 /* if popup is enabled don't lose focus */
6716 return (XT_CB_PASSTHROUGH
);
6723 return (XT_CB_PASSTHROUGH
);
6727 cmd_hide_cb(GtkWidget
*w
, struct tab
*t
)
6730 show_oops(NULL
, "%s: invalid parameters", __func__
);
6734 if (show_url
== 0 || t
->focus_wv
)
6737 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6741 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
6744 const gchar
*c
= gtk_entry_get_text(entry
);
6747 show_oops(NULL
, "cmd_activate_cb invalid parameters");
6751 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
6756 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
6757 c
[0] == '.' || c
[0] == ','))
6763 if (c
[0] == '/' || c
[0] == '?') {
6764 /* see if there is a timer pending */
6766 g_source_remove(t
->search_id
);
6771 if (t
->search_text
) {
6772 g_free(t
->search_text
);
6773 t
->search_text
= NULL
;
6776 t
->search_text
= g_strdup(s
);
6778 g_free(global_search
);
6779 global_search
= g_strdup(s
);
6780 t
->search_forward
= c
[0] == '/';
6782 history_add(&shl
, search_file
, s
, &search_history_count
);
6783 } else if (c
[0] == '.' || c
[0] == ',') {
6784 run_script(t
, "hints.fire();");
6785 /* XXX history for link following? */
6786 } else if (c
[0] == ':') {
6787 history_add(&chl
, command_file
, s
, &cmd_history_count
);
6788 /* can't call hide_cmd after cmd_execute */
6799 backward_cb(GtkWidget
*w
, struct tab
*t
)
6804 show_oops(NULL
, "backward_cb invalid parameters");
6808 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
6815 forward_cb(GtkWidget
*w
, struct tab
*t
)
6820 show_oops(NULL
, "forward_cb invalid parameters");
6824 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
6826 a
.i
= XT_NAV_FORWARD
;
6831 home_cb(GtkWidget
*w
, struct tab
*t
)
6834 show_oops(NULL
, "home_cb invalid parameters");
6838 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
6844 stop_cb(GtkWidget
*w
, struct tab
*t
)
6846 WebKitWebFrame
*frame
;
6849 show_oops(NULL
, "stop_cb invalid parameters");
6853 DNPRINTF(XT_D_NAV
, "stop_cb: tab %d\n", t
->tab_id
);
6855 frame
= webkit_web_view_get_main_frame(t
->wv
);
6856 if (frame
== NULL
) {
6857 show_oops(t
, "stop_cb: no frame");
6861 webkit_web_frame_stop_loading(frame
);
6862 abort_favicon_download(t
);
6866 setup_webkit(struct tab
*t
)
6868 if (is_g_object_setting(G_OBJECT(t
->settings
), "enable-dns-prefetching"))
6869 g_object_set(G_OBJECT(t
->settings
), "enable-dns-prefetching",
6870 FALSE
, (char *)NULL
);
6872 warnx("webkit does not have \"enable-dns-prefetching\" property");
6873 g_object_set(G_OBJECT(t
->settings
),
6874 "user-agent", t
->user_agent
, (char *)NULL
);
6875 g_object_set(G_OBJECT(t
->settings
),
6876 "enable-scripts", enable_scripts
, (char *)NULL
);
6877 g_object_set(G_OBJECT(t
->settings
),
6878 "enable-plugins", enable_plugins
, (char *)NULL
);
6879 g_object_set(G_OBJECT(t
->settings
),
6880 "javascript-can-open-windows-automatically", enable_scripts
,
6882 g_object_set(G_OBJECT(t
->settings
),
6883 "enable-html5-database", FALSE
, (char *)NULL
);
6884 g_object_set(G_OBJECT(t
->settings
),
6885 "enable-html5-local-storage", enable_localstorage
, (char *)NULL
);
6886 g_object_set(G_OBJECT(t
->settings
),
6887 "enable_spell_checking", enable_spell_checking
, (char *)NULL
);
6888 g_object_set(G_OBJECT(t
->settings
),
6889 "spell_checking_languages", spell_check_languages
, (char *)NULL
);
6890 g_object_set(G_OBJECT(t
->settings
),
6891 "enable-developer-extras", TRUE
, (char *)NULL
);
6892 g_object_set(G_OBJECT(t
->wv
),
6893 "full-content-zoom", TRUE
, (char *)NULL
);
6894 g_object_set(G_OBJECT(t
->settings
),
6895 "auto-load-images", auto_load_images
, (char *)NULL
);
6896 if (is_g_object_setting(G_OBJECT(t
->settings
),
6897 "enable-display-of-insecure-content"))
6898 g_object_set(G_OBJECT(t
->settings
),
6899 "enable-display-of-insecure-content",
6900 allow_insecure_content
, (char *)NULL
);
6901 if (is_g_object_setting(G_OBJECT(t
->settings
),
6902 "enable-running-of-insecure-content"))
6903 g_object_set(G_OBJECT(t
->settings
),
6904 "enable-running-of-insecure-content",
6905 allow_insecure_scripts
, (char *)NULL
);
6907 webkit_web_view_set_settings(t
->wv
, t
->settings
);
6911 update_statusbar_position(GtkAdjustment
* adjustment
, gpointer data
)
6913 struct tab
*ti
, *t
= NULL
;
6914 gdouble view_size
, value
, max
;
6917 TAILQ_FOREACH(ti
, &tabs
, entry
)
6918 if (ti
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
6926 if (adjustment
== NULL
)
6927 adjustment
= gtk_scrolled_window_get_vadjustment(
6928 GTK_SCROLLED_WINDOW(t
->browser_win
));
6930 view_size
= gtk_adjustment_get_page_size(adjustment
);
6931 value
= gtk_adjustment_get_value(adjustment
);
6932 max
= gtk_adjustment_get_upper(adjustment
) - view_size
;
6935 position
= g_strdup("All");
6936 else if (value
== max
)
6937 position
= g_strdup("Bot");
6938 else if (value
== 0)
6939 position
= g_strdup("Top");
6941 position
= g_strdup_printf("%d%%", (int) ((value
/ max
) * 100));
6943 gtk_label_set_text(GTK_LABEL(t
->sbe
.position
), position
);
6950 create_window(const gchar
*name
)
6954 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
6955 if (window_maximize
)
6956 gtk_window_maximize(GTK_WINDOW(w
));
6958 gtk_window_set_default_size(GTK_WINDOW(w
), window_width
, window_height
);
6959 gtk_widget_set_name(w
, name
);
6960 gtk_window_set_wmclass(GTK_WINDOW(w
), name
, "Xombrero");
6966 create_browser(struct tab
*t
)
6970 GtkAdjustment
*adjustment
;
6973 show_oops(NULL
, "create_browser invalid parameters");
6977 w
= gtk_scrolled_window_new(NULL
, NULL
);
6978 gtk_widget_set_can_focus(w
, FALSE
);
6979 t
->adjust_h
= gtk_scrolled_window_get_hadjustment(
6980 GTK_SCROLLED_WINDOW(w
));
6981 t
->adjust_v
= gtk_scrolled_window_get_vadjustment(
6982 GTK_SCROLLED_WINDOW(w
));
6983 #if !GTK_CHECK_VERSION(3, 0, 0)
6984 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
6985 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
6989 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
6990 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
6993 t
->settings
= webkit_web_settings_new();
6995 g_object_set(t
->settings
, "default-encoding", encoding
, (char *)NULL
);
6997 if (user_agent
== NULL
) {
6998 g_object_get(G_OBJECT(t
->settings
), "user-agent", &strval
,
7000 t
->user_agent
= g_strdup_printf("%s %s+", strval
, version
);
7003 t
->user_agent
= g_strdup(user_agent
->value
);
7005 t
->stylesheet
= g_strdup(stylesheet
);
7006 t
->load_images
= auto_load_images
;
7009 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w
));
7010 g_signal_connect(G_OBJECT(adjustment
), "value-changed",
7011 G_CALLBACK(update_statusbar_position
), NULL
);
7020 create_kiosk_toolbar(struct tab
*t
)
7022 GtkWidget
*toolbar
= NULL
, *b
;
7024 #if GTK_CHECK_VERSION(3, 0, 0)
7025 b
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7026 gtk_widget_set_name(GTK_WIDGET(b
), "toolbar");
7028 b
= gtk_hbox_new(FALSE
, 0);
7031 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
7033 /* backward button */
7034 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
7035 gtk_widget_set_sensitive(t
->backward
, FALSE
);
7036 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
7037 G_CALLBACK(backward_cb
), t
);
7038 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, TRUE
, TRUE
, 0);
7040 /* forward button */
7041 t
->forward
= create_button("Forward", GTK_STOCK_GO_FORWARD
, 0);
7042 gtk_widget_set_sensitive(t
->forward
, FALSE
);
7043 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
7044 G_CALLBACK(forward_cb
), t
);
7045 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, TRUE
, TRUE
, 0);
7048 t
->gohome
= create_button("Home", GTK_STOCK_HOME
, 0);
7049 gtk_widget_set_sensitive(t
->gohome
, true);
7050 g_signal_connect(G_OBJECT(t
->gohome
), "clicked",
7051 G_CALLBACK(home_cb
), t
);
7052 gtk_box_pack_start(GTK_BOX(b
), t
->gohome
, TRUE
, TRUE
, 0);
7054 /* create widgets but don't use them */
7055 t
->uri_entry
= gtk_entry_new();
7056 g_signal_connect(G_OBJECT(t
->uri_entry
), "focus-in-event",
7057 G_CALLBACK(entry_focus_cb
), t
);
7058 #if !GTK_CHECK_VERSION(3, 0, 0)
7059 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
7061 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
7062 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
7063 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
7069 create_toolbar(struct tab
*t
)
7071 GtkWidget
*toolbar
= NULL
, *b
;
7073 #if GTK_CHECK_VERSION(3, 0, 0)
7074 b
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7075 gtk_widget_set_name(GTK_WIDGET(b
), "toolbar");
7077 b
= gtk_hbox_new(FALSE
, 0);
7080 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
7082 /* backward button */
7083 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
7084 gtk_widget_set_sensitive(t
->backward
, FALSE
);
7085 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
7086 G_CALLBACK(backward_cb
), t
);
7087 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, FALSE
, FALSE
, 0);
7089 /* forward button */
7090 t
->forward
= create_button("Forward",GTK_STOCK_GO_FORWARD
, 0);
7091 gtk_widget_set_sensitive(t
->forward
, FALSE
);
7092 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
7093 G_CALLBACK(forward_cb
), t
);
7094 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, FALSE
, FALSE
, 0);
7097 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
7098 gtk_widget_set_sensitive(t
->stop
, FALSE
);
7099 g_signal_connect(G_OBJECT(t
->stop
), "clicked", G_CALLBACK(stop_cb
), t
);
7100 gtk_box_pack_start(GTK_BOX(b
), t
->stop
, FALSE
, FALSE
, 0);
7103 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
7104 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
7105 gtk_widget_set_sensitive(t
->js_toggle
, TRUE
);
7106 g_signal_connect(G_OBJECT(t
->js_toggle
), "clicked",
7107 G_CALLBACK(js_toggle_cb
), t
);
7108 gtk_box_pack_start(GTK_BOX(b
), t
->js_toggle
, FALSE
, FALSE
, 0);
7110 t
->uri_entry
= gtk_entry_new();
7111 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
7112 G_CALLBACK(activate_uri_entry_cb
), t
);
7113 g_signal_connect(G_OBJECT(t
->uri_entry
), "key-press-event",
7114 G_CALLBACK(entry_key_cb
), t
);
7115 g_signal_connect(G_OBJECT(t
->uri_entry
), "focus-in-event",
7116 G_CALLBACK(entry_focus_cb
), t
);
7118 gtk_box_pack_start(GTK_BOX(b
), t
->uri_entry
, TRUE
, TRUE
, 0);
7121 t
->search_entry
= gtk_entry_new();
7122 gtk_entry_set_width_chars(GTK_ENTRY(t
->search_entry
), 30);
7123 g_signal_connect(G_OBJECT(t
->search_entry
), "activate",
7124 G_CALLBACK(activate_search_entry_cb
), t
);
7125 g_signal_connect(G_OBJECT(t
->search_entry
), "key-press-event",
7126 G_CALLBACK(entry_key_cb
), t
);
7127 g_signal_connect(G_OBJECT(t
->search_entry
), "focus-in-event",
7128 G_CALLBACK(entry_focus_cb
), t
);
7129 gtk_box_pack_start(GTK_BOX(b
), t
->search_entry
, FALSE
, FALSE
, 0);
7131 #if !GTK_CHECK_VERSION(3, 0, 0)
7132 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
7139 create_buffers(struct tab
*t
)
7141 GtkCellRenderer
*renderer
;
7144 view
= gtk_tree_view_new();
7146 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view
), FALSE
);
7148 renderer
= gtk_cell_renderer_text_new();
7149 gtk_tree_view_insert_column_with_attributes
7150 (GTK_TREE_VIEW(view
), -1, "Id", renderer
, "text", COL_ID
, (char *)NULL
);
7152 renderer
= gtk_cell_renderer_pixbuf_new();
7153 gtk_tree_view_insert_column_with_attributes
7154 (GTK_TREE_VIEW(view
), -1, "Favicon", renderer
, "pixbuf", COL_FAVICON
,
7157 renderer
= gtk_cell_renderer_text_new();
7158 gtk_tree_view_insert_column_with_attributes
7159 (GTK_TREE_VIEW(view
), -1, "Title", renderer
, "text", COL_TITLE
,
7162 gtk_tree_view_set_model
7163 (GTK_TREE_VIEW(view
), GTK_TREE_MODEL(buffers_store
));
7169 row_activated_cb(GtkTreeView
*view
, GtkTreePath
*path
,
7170 GtkTreeViewColumn
*col
, struct tab
*t
)
7175 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
7177 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
,
7180 (GTK_TREE_MODEL(buffers_store
), &iter
, COL_ID
, &id
, -1);
7181 set_current_tab(id
- 1);
7187 /* after tab reordering/creation/removal */
7193 TAILQ_FOREACH(t
, &tabs
, entry
)
7194 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
7198 update_statusbar_tabs(struct tab
*t
)
7200 int tab_id
, max_tab_id
;
7206 tab_id
= gtk_notebook_get_current_page(notebook
);
7208 max_tab_id
= gtk_notebook_get_n_pages(notebook
);
7209 snprintf(s
, sizeof s
, "%d/%d", tab_id
+ 1, max_tab_id
);
7212 t
= get_current_tab();
7215 gtk_label_set_text(GTK_LABEL(t
->sbe
.tabs
), s
);
7218 /* after active tab change */
7220 recolor_compact_tabs(void)
7224 #if !GTK_CHECK_VERSION(3, 0, 0)
7225 GdkColor color_active
, color_inactive
;
7227 gdk_color_parse(XT_COLOR_CT_ACTIVE
, &color_active
);
7228 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color_inactive
);
7230 curid
= gtk_notebook_get_current_page(notebook
);
7232 TAILQ_FOREACH(t
, &tabs
, entry
) {
7233 #if GTK_CHECK_VERSION(3, 0, 0)
7234 if (t
->tab_id
== curid
)
7235 gtk_widget_set_name(t
->tab_elems
.label
, XT_CSS_ACTIVE
);
7237 gtk_widget_set_name(t
->tab_elems
.label
, "");
7239 if (t
->tab_id
== curid
)
7240 gtk_widget_modify_fg(t
->tab_elems
.label
,
7241 GTK_STATE_NORMAL
, &color_active
);
7243 gtk_widget_modify_fg(t
->tab_elems
.label
,
7244 GTK_STATE_NORMAL
, &color_inactive
);
7250 set_current_tab(int page_num
)
7252 buffercmd_abort(get_current_tab());
7253 gtk_notebook_set_current_page(notebook
, page_num
);
7254 recolor_compact_tabs();
7255 update_statusbar_tabs(NULL
);
7259 undo_close_tab_save(struct tab
*t
)
7263 struct undo
*u1
, *u2
;
7265 WebKitWebHistoryItem
*item
;
7267 if ((uri
= get_uri(t
)) == NULL
)
7270 u1
= g_malloc0(sizeof(struct undo
));
7271 u1
->uri
= g_strdup(uri
);
7273 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
7275 m
= webkit_web_back_forward_list_get_forward_length(t
->bfl
);
7276 n
= webkit_web_back_forward_list_get_back_length(t
->bfl
);
7279 /* forward history */
7280 items
= webkit_web_back_forward_list_get_forward_list_with_limit(t
->bfl
, m
);
7284 u1
->history
= g_list_prepend(u1
->history
,
7285 webkit_web_history_item_copy(item
));
7286 items
= g_list_next(items
);
7291 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
7292 u1
->history
= g_list_prepend(u1
->history
,
7293 webkit_web_history_item_copy(item
));
7297 items
= webkit_web_back_forward_list_get_back_list_with_limit(t
->bfl
, n
);
7301 u1
->history
= g_list_prepend(u1
->history
,
7302 webkit_web_history_item_copy(item
));
7303 items
= g_list_next(items
);
7306 TAILQ_INSERT_HEAD(&undos
, u1
, entry
);
7308 if (undo_count
> XT_MAX_UNDO_CLOSE_TAB
) {
7309 u2
= TAILQ_LAST(&undos
, undo_tailq
);
7310 TAILQ_REMOVE(&undos
, u2
, entry
);
7312 g_list_free(u2
->history
);
7321 delete_tab(struct tab
*t
)
7325 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
7331 * no need to join thread here because it won't access t on completion
7334 TAILQ_REMOVE(&tabs
, t
, entry
);
7337 /* Halt all webkit activity. */
7338 abort_favicon_download(t
);
7339 webkit_web_view_stop_loading(t
->wv
);
7341 /* Save the tab, so we can undo the close. */
7342 undo_close_tab_save(t
);
7346 g_source_remove(t
->search_id
);
7350 g_free(t
->session_key
);
7353 bzero(&a
, sizeof a
);
7355 inspector_cmd(t
, &a
);
7357 if (browser_mode
== XT_BM_KIOSK
) {
7358 gtk_widget_destroy(t
->uri_entry
);
7359 gtk_widget_destroy(t
->stop
);
7360 gtk_widget_destroy(t
->js_toggle
);
7363 g_object_unref(t
->completion
);
7365 g_object_unref(t
->item
);
7367 gtk_widget_destroy(t
->tab_elems
.eventbox
);
7368 gtk_widget_destroy(t
->vbox
);
7370 g_free(t
->user_agent
);
7371 g_free(t
->stylesheet
);
7377 if (TAILQ_EMPTY(&tabs
)) {
7378 if (browser_mode
== XT_BM_KIOSK
)
7379 create_new_tab(home
, NULL
, 1, -1);
7381 create_new_tab(NULL
, NULL
, 1, -1);
7384 /* recreate session */
7385 if (session_autosave
) {
7386 bzero(&a
, sizeof a
);
7388 save_tabs(NULL
, &a
);
7392 recolor_compact_tabs();
7396 update_statusbar_zoom(struct tab
*t
)
7399 char s
[16] = { '\0' };
7401 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
7402 if ((zoom
<= 0.99 || zoom
>= 1.01))
7403 snprintf(s
, sizeof s
, "%d%%", (int)(zoom
* 100));
7404 gtk_label_set_text(GTK_LABEL(t
->sbe
.zoom
), s
);
7408 setzoom_webkit(struct tab
*t
, int adjust
)
7410 #define XT_ZOOMPERCENT 0.04
7415 show_oops(NULL
, "setzoom_webkit invalid parameters");
7419 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
7420 if (adjust
== XT_ZOOM_IN
)
7421 zoom
+= XT_ZOOMPERCENT
;
7422 else if (adjust
== XT_ZOOM_OUT
)
7423 zoom
-= XT_ZOOMPERCENT
;
7424 else if (adjust
> 0)
7425 zoom
= default_zoom_level
+ adjust
/ 100.0 - 1.0;
7427 show_oops(t
, "setzoom_webkit invalid zoom value");
7431 if (zoom
< XT_ZOOMPERCENT
)
7432 zoom
= XT_ZOOMPERCENT
;
7433 g_object_set(G_OBJECT(t
->wv
), "zoom-level", zoom
, (char *)NULL
);
7434 update_statusbar_zoom(t
);
7438 tab_clicked_cb(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
7440 struct tab
*t
= (struct tab
*) data
;
7442 DNPRINTF(XT_D_TAB
, "tab_clicked_cb: tab: %d\n", t
->tab_id
);
7444 switch (event
->button
) {
7446 set_current_tab(t
->tab_id
);
7457 append_tab(struct tab
*t
)
7462 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
7463 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
7471 sbe
= gtk_label_new(NULL
);
7472 gtk_widget_set_can_focus(GTK_WIDGET(sbe
), FALSE
);
7473 gtk_widget_modify_font(GTK_WIDGET(sbe
), statusbar_font
);
7478 add_sbe(GtkWidget
*box
, char flag
, int *used
, GtkWidget
*sbe
)
7480 if (box
== NULL
|| used
== NULL
|| sbe
== NULL
) {
7481 DPRINTF("%s: invalid parameters", __func__
);
7486 warnx("flag \"%c\" specified more than "
7487 "once in statusbar_elems\n", flag
);
7491 gtk_box_pack_start(GTK_BOX(box
), sbe
, FALSE
, FALSE
, 0);
7498 statusbar_create(struct tab
*t
)
7500 GtkWidget
*box
; /* container for statusbar elems */
7503 int sbe_P
= 0, sbe_B
= 0, sbe_Z
= 0, sbe_T
= 0,
7505 #if !GTK_CHECK_VERSION(3, 0, 0)
7510 DPRINTF("%s: invalid parameters", __func__
);
7514 #if GTK_CHECK_VERSION(3, 0, 0)
7515 t
->statusbar
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7516 box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7517 gtk_widget_set_name(GTK_WIDGET(t
->statusbar
), "statusbar");
7519 t
->statusbar
= gtk_hbox_new(FALSE
, 0);
7520 box
= gtk_hbox_new(FALSE
, 0);
7522 t
->sbe
.ebox
= gtk_event_box_new();
7524 gtk_widget_set_can_focus(GTK_WIDGET(t
->statusbar
), FALSE
);
7525 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.ebox
), FALSE
);
7526 gtk_widget_set_can_focus(GTK_WIDGET(box
), FALSE
);
7528 gtk_box_set_spacing(GTK_BOX(box
), 10);
7529 gtk_box_pack_start(GTK_BOX(t
->statusbar
), t
->sbe
.ebox
, TRUE
, TRUE
, 0);
7530 gtk_container_add(GTK_CONTAINER(t
->sbe
.ebox
), box
);
7532 /* create these widgets only if specified in statusbar_elems */
7533 t
->sbe
.uri
= gtk_entry_new();
7534 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.uri
), FALSE
);
7535 gtk_widget_modify_font(GTK_WIDGET(t
->sbe
.uri
), statusbar_font
);
7536 #if !GTK_CHECK_VERSION(3, 0, 0)
7537 gtk_entry_set_inner_border(GTK_ENTRY(t
->sbe
.uri
), NULL
);
7538 gtk_entry_set_has_frame(GTK_ENTRY(t
->sbe
.uri
), FALSE
);
7540 t
->sbe
.position
= create_sbe();
7541 t
->sbe
.zoom
= create_sbe();
7542 t
->sbe
.buffercmd
= create_sbe();
7543 t
->sbe
.tabs
= create_sbe();
7544 t
->sbe
.proxy
= create_sbe();
7546 #if GTK_CHECK_VERSION(3, 0, 0)
7547 statusbar_modify_attr(t
, XT_CSS_NORMAL
);
7549 statusbar_modify_attr(t
, XT_COLOR_WHITE
, XT_COLOR_BLACK
);
7552 gtk_box_pack_start(GTK_BOX(box
), t
->sbe
.uri
, TRUE
, TRUE
, 0);
7555 * gtk widgets cannot be added to a box twice. The sbe_* variables
7558 for (p
= statusbar_elems
; *p
!= '\0'; p
++) {
7561 #if GTK_CHECK_VERSION(3, 0, 0)
7562 sep
= gtk_separator_new(GTK_ORIENTATION_VERTICAL
);
7564 sep
= gtk_vseparator_new();
7565 gdk_color_parse(XT_COLOR_SB_SEPARATOR
, &color
);
7566 gtk_widget_modify_bg(sep
, GTK_STATE_NORMAL
, &color
);
7568 gtk_box_pack_start(GTK_BOX(box
), sep
, FALSE
, FALSE
, 0);
7571 add_sbe(box
, *p
, &sbe_P
, t
->sbe
.position
);
7574 add_sbe(box
, *p
, &sbe_B
, t
->sbe
.buffercmd
);
7577 add_sbe(box
, *p
, &sbe_Z
, t
->sbe
.zoom
);
7580 add_sbe(box
, *p
, &sbe_T
, t
->sbe
.tabs
);
7583 if (add_sbe(box
, *p
, &sbe_p
, t
->sbe
.proxy
) == 0)
7586 GTK_ENTRY(t
->sbe
.proxy
), "proxy");
7589 warnx("illegal flag \"%c\" in statusbar_elems\n", *p
);
7594 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar
, FALSE
, FALSE
, 0);
7600 create_new_tab(char *title
, struct undo
*u
, int focus
, int position
)
7605 WebKitWebHistoryItem
*item
;
7607 #if !GTK_CHECK_VERSION(3, 0, 0)
7611 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
7613 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
7614 DNPRINTF(XT_D_TAB
, "create_new_tab: new tab rejected\n");
7618 t
= g_malloc0(sizeof *t
);
7620 if (title
== NULL
) {
7621 title
= "(untitled)";
7625 #if GTK_CHECK_VERSION(3, 0, 0)
7626 t
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
7627 b
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7628 gtk_widget_set_name(t
->vbox
, "vbox");
7630 t
->vbox
= gtk_vbox_new(FALSE
, 0);
7631 b
= gtk_hbox_new(FALSE
, 0);
7633 gtk_widget_set_can_focus(t
->vbox
, FALSE
);
7635 /* label + button for tab */
7637 gtk_widget_set_can_focus(t
->tab_content
, FALSE
);
7639 t
->user_agent_id
= 0;
7640 t
->http_accept_id
= 0;
7642 #if WEBKIT_CHECK_VERSION(1, 5, 0)
7646 #if GTK_CHECK_VERSION(2, 20, 0)
7647 t
->spinner
= gtk_spinner_new();
7649 t
->label
= gtk_label_new(title
);
7650 bb
= create_button("Close", GTK_STOCK_CLOSE
, 1);
7651 gtk_label_set_max_width_chars(GTK_LABEL(t
->label
), 20);
7652 gtk_label_set_ellipsize(GTK_LABEL(t
->label
), PANGO_ELLIPSIZE_END
);
7653 gtk_label_set_line_wrap(GTK_LABEL(t
->label
), FALSE
);
7654 gtk_widget_set_size_request(b
, 130, 0);
7657 * this is a total hack and most likely breaks with other styles but
7658 * is necessary so the text doesn't bounce around when the spinner is
7661 #if GTK_CHECK_VERSION(3, 0, 0)
7662 gtk_widget_set_size_request(t
->label
, 95, 0);
7664 gtk_widget_set_size_request(t
->label
, 100, 0);
7667 gtk_box_pack_start(GTK_BOX(b
), bb
, FALSE
, FALSE
, 0);
7668 gtk_box_pack_start(GTK_BOX(b
), t
->label
, FALSE
, FALSE
, 0);
7669 #if GTK_CHECK_VERSION(2, 20, 0)
7670 gtk_box_pack_end(GTK_BOX(b
), t
->spinner
, FALSE
, FALSE
, 0);
7674 if (browser_mode
== XT_BM_KIOSK
) {
7675 t
->toolbar
= create_kiosk_toolbar(t
);
7676 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
7679 t
->toolbar
= create_toolbar(t
);
7680 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
7688 t
->browser_win
= create_browser(t
);
7689 set_scrollbar_visibility(t
, show_scrollbars
);
7690 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
7692 /* oops message for user feedback */
7693 t
->oops
= gtk_entry_new();
7694 gtk_entry_set_inner_border(GTK_ENTRY(t
->oops
), NULL
);
7695 gtk_entry_set_has_frame(GTK_ENTRY(t
->oops
), FALSE
);
7696 gtk_widget_set_can_focus(GTK_WIDGET(t
->oops
), FALSE
);
7697 #if GTK_CHECK_VERSION(3, 0, 0)
7698 gtk_widget_set_name(t
->oops
, XT_CSS_RED
);
7700 gdk_color_parse(XT_COLOR_RED
, &color
);
7701 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
7703 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
7704 gtk_widget_modify_font(GTK_WIDGET(t
->oops
), oops_font
);
7707 t
->cmd
= gtk_entry_new();
7708 g_signal_connect(G_OBJECT(t
->cmd
), "focus-in-event",
7709 G_CALLBACK(entry_focus_cb
), t
);
7710 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
7711 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
7712 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
7713 gtk_widget_modify_font(GTK_WIDGET(t
->cmd
), cmd_font
);
7716 statusbar_create(t
);
7719 t
->buffers
= create_buffers(t
);
7720 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->buffers
, FALSE
, FALSE
, 0);
7722 /* xtp meaning is normal by default */
7723 set_normal_tab_meaning(t
);
7725 /* set empty favicon */
7726 xt_icon_from_name(t
, "text-html");
7728 /* and show it all */
7729 gtk_widget_show_all(b
);
7730 gtk_widget_show_all(t
->vbox
);
7733 gtk_widget_hide(t
->backward
);
7734 gtk_widget_hide(t
->forward
);
7735 gtk_widget_hide(t
->stop
);
7736 gtk_widget_hide(t
->js_toggle
);
7738 if (!fancy_bar
|| (search_string
== NULL
|| strlen(search_string
) == 0))
7739 gtk_widget_hide(t
->search_entry
);
7741 /* compact tab bar */
7742 t
->tab_elems
.label
= gtk_label_new(title
);
7743 t
->tab_elems
.favicon
= gtk_image_new();
7745 t
->tab_elems
.eventbox
= gtk_event_box_new();
7746 gtk_widget_set_name(t
->tab_elems
.eventbox
, "compact_tab");
7747 #if GTK_CHECK_VERSION(3, 0, 0)
7748 t
->tab_elems
.box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7750 gtk_label_set_ellipsize(GTK_LABEL(t
->tab_elems
.label
),
7751 PANGO_ELLIPSIZE_END
);
7752 gtk_widget_override_font(t
->tab_elems
.label
, tabbar_font
);
7753 gtk_widget_set_halign(t
->tab_elems
.label
, GTK_ALIGN_START
);
7754 gtk_widget_set_valign(t
->tab_elems
.label
, GTK_ALIGN_START
);
7756 t
->tab_elems
.box
= gtk_hbox_new(FALSE
, 0);
7758 gtk_label_set_width_chars(GTK_LABEL(t
->tab_elems
.label
), 1);
7759 gtk_misc_set_alignment(GTK_MISC(t
->tab_elems
.label
), 0.0, 0.0);
7760 gtk_misc_set_padding(GTK_MISC(t
->tab_elems
.label
), 4.0, 4.0);
7761 gtk_widget_modify_font(GTK_WIDGET(t
->tab_elems
.label
), tabbar_font
);
7763 gdk_color_parse(XT_COLOR_CT_BACKGROUND
, &color
);
7764 gtk_widget_modify_bg(t
->tab_elems
.eventbox
, GTK_STATE_NORMAL
, &color
);
7765 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
7766 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
, &color
);
7769 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.favicon
, FALSE
,
7771 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.label
, TRUE
,
7773 gtk_container_add(GTK_CONTAINER(t
->tab_elems
.eventbox
),
7776 gtk_box_pack_start(GTK_BOX(tab_bar_box
), t
->tab_elems
.eventbox
, TRUE
,
7778 gtk_widget_show_all(t
->tab_elems
.eventbox
);
7780 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
7783 id
= position
>= 0 ? position
:
7784 gtk_notebook_get_current_page(notebook
) + 1;
7785 if (id
> gtk_notebook_get_n_pages(notebook
))
7788 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
7789 gtk_notebook_insert_page(notebook
, t
->vbox
, b
, id
);
7790 gtk_box_reorder_child(GTK_BOX(tab_bar_box
),
7791 t
->tab_elems
.eventbox
, id
);
7796 #if GTK_CHECK_VERSION(2, 20, 0)
7797 /* turn spinner off if we are a new tab without uri */
7799 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
7800 gtk_widget_hide(t
->spinner
);
7803 /* make notebook tabs reorderable */
7804 gtk_notebook_set_tab_reorderable(notebook
, t
->vbox
, TRUE
);
7806 /* compact tabs clickable */
7807 g_signal_connect(G_OBJECT(t
->tab_elems
.eventbox
),
7808 "button_press_event", G_CALLBACK(tab_clicked_cb
), t
);
7810 g_object_connect(G_OBJECT(t
->cmd
),
7811 "signal::button-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
7812 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb
), t
,
7813 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
7814 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb
), t
,
7815 "signal::activate", G_CALLBACK(cmd_activate_cb
), t
,
7816 "signal::populate-popup", G_CALLBACK(cmd_popup_cb
), t
,
7817 "signal::hide", G_CALLBACK(cmd_hide_cb
), t
,
7820 /* reuse wv_button_cb to hide oops */
7821 g_object_connect(G_OBJECT(t
->oops
),
7822 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7825 g_signal_connect(t
->buffers
,
7826 "row-activated", G_CALLBACK(row_activated_cb
), t
);
7827 g_object_connect(G_OBJECT(t
->buffers
),
7828 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
, (char *)NULL
);
7830 g_object_connect(G_OBJECT(t
->wv
),
7831 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
,
7832 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb
), t
,
7833 "signal::download-requested", G_CALLBACK(webview_download_cb
), t
,
7834 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), t
,
7835 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7836 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7837 "signal::resource-request-starting", G_CALLBACK(webview_rrs_cb
), t
,
7838 "signal::create-web-view", G_CALLBACK(webview_cwv_cb
), t
,
7839 "signal::close-web-view", G_CALLBACK(webview_closewv_cb
), t
,
7840 "signal::event", G_CALLBACK(webview_event_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
);
7850 t
->progress_handle
= g_signal_connect(t
->wv
,
7851 "notify::progress", G_CALLBACK(webview_progress_changed_cb
), t
);
7853 /* hijack the unused keys as if we were the browser */
7854 //g_object_connect(G_OBJECT(t->toolbar),
7855 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
7858 g_signal_connect(G_OBJECT(bb
), "button_press_event",
7859 G_CALLBACK(tab_close_cb
), t
);
7862 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
7863 /* restore the tab's history */
7864 if (u
&& u
->history
) {
7868 webkit_web_back_forward_list_add_item(t
->bfl
, item
);
7869 items
= g_list_next(items
);
7872 item
= g_list_nth_data(u
->history
, u
->back
);
7874 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
7877 g_list_free(u
->history
);
7879 webkit_web_back_forward_list_clear(t
->bfl
);
7881 /* check and show url and statusbar */
7882 url_set_visibility();
7883 statusbar_set_visibility();
7886 set_current_tab(t
->tab_id
);
7887 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
7891 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
7895 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
7902 if (userstyle_global
)
7905 recolor_compact_tabs();
7906 setzoom_webkit(t
, XT_ZOOM_NORMAL
);
7911 notebook_switchpage_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7917 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
7919 if (gtk_notebook_get_current_page(notebook
) == -1)
7922 TAILQ_FOREACH(t
, &tabs
, entry
) {
7923 if (t
->tab_id
== pn
) {
7924 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
7927 uri
= get_title(t
, TRUE
);
7928 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
7935 /* can't use focus_webview here */
7936 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
7938 update_statusbar_tabs(t
);
7945 notebook_pagereordered_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7948 struct tab
*t
= NULL
, *tt
;
7952 TAILQ_FOREACH(tt
, &tabs
, entry
)
7953 if (tt
->tab_id
== pn
) {
7959 DNPRINTF(XT_D_TAB
, "page_reordered_cb: tab: %d\n", t
->tab_id
);
7961 gtk_box_reorder_child(GTK_BOX(tab_bar_box
), t
->tab_elems
.eventbox
,
7964 update_statusbar_tabs(t
);
7968 menuitem_response(struct tab
*t
)
7970 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
7974 destroy_menu(GtkWidget
*w
, GdkEventFocus
*e
, void *notused
)
7976 gtk_widget_destroy(w
);
7977 return (XT_CB_PASSTHROUGH
);
7981 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
7983 GtkWidget
*menu
, *menu_items
;
7984 GdkEventButton
*bevent
;
7985 struct tab
**stabs
= NULL
;
7989 if (event
->type
== GDK_BUTTON_PRESS
) {
7990 bevent
= (GdkEventButton
*) event
;
7991 menu
= gtk_menu_new();
7993 num_tabs
= sort_tabs_by_page_num(&stabs
);
7994 for (i
= 0; i
< num_tabs
; ++i
) {
7995 if (stabs
[i
] == NULL
)
7997 if ((uri
= get_uri(stabs
[i
])) == NULL
)
7998 /* XXX make sure there is something to print */
7999 /* XXX add gui pages in here to look purdy */
8001 menu_items
= gtk_menu_item_new_with_label(uri
);
8002 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), menu_items
);
8003 gtk_widget_show(menu_items
);
8005 g_signal_connect_swapped((menu_items
),
8006 "activate", G_CALLBACK(menuitem_response
),
8007 (gpointer
)stabs
[i
]);
8011 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
,
8012 bevent
->button
, bevent
->time
);
8014 g_object_connect(G_OBJECT(menu
),
8015 "signal::hide", G_CALLBACK(destroy_menu
), NULL
,
8018 return (TRUE
/* eat event */);
8021 return (FALSE
/* propagate */);
8025 icon_size_map(int iconsz
)
8027 if (iconsz
<= GTK_ICON_SIZE_INVALID
||
8028 iconsz
> GTK_ICON_SIZE_DIALOG
)
8029 return (GTK_ICON_SIZE_SMALL_TOOLBAR
);
8035 create_button(char *name
, char *stockid
, int size
)
8037 GtkWidget
*button
, *image
;
8039 #if !GTK_CHECK_VERSION(3, 0, 0)
8043 #if !GTK_CHECK_VERSION(3, 0, 0)
8044 newstyle
= g_strdup_printf(
8045 "style \"%s-style\"\n"
8047 " GtkWidget::focus-padding = 0\n"
8048 " GtkWidget::focus-line-width = 0\n"
8052 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
8053 gtk_rc_parse_string(newstyle
);
8056 button
= gtk_button_new();
8057 gtk_widget_set_can_focus(button
, FALSE
);
8058 gtk_button_set_focus_on_click(GTK_BUTTON(button
), FALSE
);
8059 gtk_icon_size
= icon_size_map(size
? size
: icon_size
);
8061 image
= gtk_image_new_from_stock(stockid
, gtk_icon_size
);
8062 gtk_container_set_border_width(GTK_CONTAINER(button
), 1);
8063 gtk_container_add(GTK_CONTAINER(button
), GTK_WIDGET(image
));
8064 gtk_widget_set_name(button
, name
);
8065 gtk_button_set_relief(GTK_BUTTON(button
), GTK_RELIEF_NONE
);
8071 button_set_stockid(GtkWidget
*button
, char *stockid
)
8075 image
= gtk_image_new_from_stock(stockid
, icon_size_map(icon_size
));
8076 gtk_button_set_image(GTK_BUTTON(button
), image
);
8085 char file
[PATH_MAX
];
8087 #if !GTK_CHECK_VERSION(3, 0, 0)
8091 #if GTK_CHECK_VERSION(3, 0, 0)
8092 vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
8094 vbox
= gtk_vbox_new(FALSE
, 0);
8096 gtk_box_set_spacing(GTK_BOX(vbox
), 0);
8097 gtk_widget_set_can_focus(vbox
, FALSE
);
8098 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
8099 #if !GTK_CHECK_VERSION(3, 0, 0)
8100 /* XXX seems to be needed with gtk+2 */
8101 g_object_set(G_OBJECT(notebook
), "tab-border", 0, NULL
);
8103 gtk_notebook_set_scrollable(notebook
, TRUE
);
8104 gtk_notebook_set_show_border(notebook
, FALSE
);
8105 gtk_widget_set_can_focus(GTK_WIDGET(notebook
), FALSE
);
8107 abtn
= gtk_button_new();
8108 gtk_widget_set_can_focus(abtn
, FALSE
);
8109 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
8110 gtk_widget_set_name(abtn
, "Arrow");
8111 gtk_container_add(GTK_CONTAINER(abtn
), arrow
);
8112 gtk_widget_set_size_request(abtn
, -1, 20);
8114 #if GTK_CHECK_VERSION(2, 20, 0)
8115 gtk_notebook_set_action_widget(notebook
, abtn
, GTK_PACK_END
);
8117 /* compact tab bar */
8118 tab_bar
= gtk_event_box_new();
8119 #if GTK_CHECK_VERSION(3, 0, 0)
8120 gtk_widget_set_name(tab_bar
, "tab_bar");
8121 tab_bar_box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
8123 gdk_color_parse(XT_COLOR_CT_SEPARATOR
, &color
);
8124 gtk_widget_modify_bg(tab_bar
, GTK_STATE_NORMAL
, &color
);
8125 tab_bar_box
= gtk_hbox_new(TRUE
, 0);
8127 gtk_container_add(GTK_CONTAINER(tab_bar
), tab_bar_box
);
8128 gtk_box_set_homogeneous(GTK_BOX(tab_bar_box
), TRUE
);
8129 gtk_box_set_spacing(GTK_BOX(tab_bar_box
), 2);
8131 gtk_box_pack_start(GTK_BOX(vbox
), tab_bar
, FALSE
, FALSE
, 0);
8132 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
8134 g_object_connect(G_OBJECT(notebook
),
8135 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb
), NULL
,
8137 g_object_connect(G_OBJECT(notebook
),
8138 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb
),
8139 NULL
, (char *)NULL
);
8140 g_signal_connect(G_OBJECT(abtn
), "button_press_event",
8141 G_CALLBACK(arrow_cb
), NULL
);
8143 main_window
= create_window("xombrero");
8144 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
8145 g_signal_connect(G_OBJECT(main_window
), "delete_event",
8146 G_CALLBACK(gtk_main_quit
), NULL
);
8147 #if GTK_CHECK_VERSION(3, 0, 0)
8148 gtk_window_set_has_resize_grip(GTK_WINDOW(main_window
), FALSE
);
8152 for (i
= 0; i
< LENGTH(icons
); i
++) {
8153 snprintf(file
, sizeof file
, "%s" PS
"%s", resource_dir
, icons
[i
]);
8154 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
8155 l
= g_list_append(l
, pb
);
8157 gtk_window_set_default_icon_list(l
);
8159 gtk_widget_show_all(abtn
);
8160 gtk_widget_show_all(main_window
);
8161 notebook_tab_set_visibility();
8164 #ifndef XT_SOCKET_DISABLE
8166 send_cmd_to_socket(char *cmd
)
8169 struct sockaddr_un sa
;
8171 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
8172 warnx("%s: socket", __func__
);
8176 sa
.sun_family
= AF_UNIX
;
8177 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
8178 work_dir
, XT_SOCKET_FILE
);
8181 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
8182 warnx("%s: connect", __func__
);
8186 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
8187 warnx("%s: send", __func__
);
8198 socket_watcher(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
8201 char str
[XT_MAX_URL_LENGTH
];
8202 socklen_t t
= sizeof(struct sockaddr_un
);
8203 struct sockaddr_un sa
;
8208 gint fd
= g_io_channel_unix_get_fd(source
);
8210 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
8215 if (getpeereid(s
, &uid
, &gid
) == -1) {
8219 if (uid
!= getuid() || gid
!= getgid()) {
8220 warnx("unauthorized user");
8226 warnx("not a valid user");
8230 n
= recv(s
, str
, sizeof(str
), 0);
8234 tt
= get_current_tab();
8235 cmd_execute(tt
, str
);
8243 struct sockaddr_un sa
;
8245 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
8246 warn("is_running: socket");
8250 sa
.sun_family
= AF_UNIX
;
8251 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
8252 work_dir
, XT_SOCKET_FILE
);
8255 /* connect to see if there is a listener */
8256 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
8257 rv
= 0; /* not running */
8259 rv
= 1; /* already running */
8270 struct sockaddr_un sa
;
8272 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
8273 warn("build_socket: socket");
8277 sa
.sun_family
= AF_UNIX
;
8278 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
8279 work_dir
, XT_SOCKET_FILE
);
8282 /* connect to see if there is a listener */
8283 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
8284 /* no listener so we will */
8285 unlink(sa
.sun_path
);
8287 if (bind(s
, (struct sockaddr
*)&sa
, len
) == -1) {
8288 warn("build_socket: bind");
8292 if (listen(s
, 1) == -1) {
8293 warn("build_socket: listen");
8311 if (stat(dir
, &sb
)) {
8312 #if defined __MINGW32__
8313 if (mkdir(dir
) == -1)
8315 if (mkdir(dir
, S_IRWXU
) == -1)
8317 err(1, "mkdir %s", dir
);
8319 err(1, "stat %s", dir
);
8321 if (S_ISDIR(sb
.st_mode
) == 0)
8322 errx(1, "%s not a dir", dir
);
8323 #if !defined __MINGW32__
8324 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
8325 warnx("fixing invalid permissions on %s", dir
);
8326 if (chmod(dir
, S_IRWXU
) == -1)
8327 err(1, "chmod %s", dir
);
8336 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
8340 GStaticRecMutex my_gdk_mtx
= G_STATIC_REC_MUTEX_INIT
;
8341 volatile int mtx_depth
;
8345 * The linux flash plugin violates the gdk locking mechanism.
8346 * Work around the issue by using a recursive mutex with some match applied
8347 * to see if we hit a buggy condition.
8349 * The following code is painful so just don't read it. It really doesn't
8350 * make much sense but seems to work.
8359 g_static_rec_mutex_lock(&my_gdk_mtx
);
8360 if (my_gdk_mtx
.depth
<= 0) {
8364 g_static_rec_mutex_lock(&my_gdk_mtx
);
8366 } else if (my_gdk_mtx
.depth
!= 1) {
8371 g_static_rec_mutex_unlock(&my_gdk_mtx
);
8372 } while (my_gdk_mtx
.depth
> 1);
8378 if (mtx_complain
== 0) {
8379 DNPRINTF(XT_D_MTX
, "buggy mutex implementation detected(%s), "
8380 "work around implemented", s
);
8392 if (my_gdk_mtx
.depth
<= 0) {
8397 } else if (my_gdk_mtx
.depth
!= 1) {
8401 g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
8404 g_static_rec_mutex_unlock(&my_gdk_mtx
);
8408 if (mtx_complain
== 0) {
8409 DNPRINTF(XT_D_MTX
, "buggy mutex implementation detected(%s), "
8410 "work around implemented", s
);
8415 #if GTK_CHECK_VERSION(3, 0, 0)
8419 GtkCssProvider
*provider
;
8420 GdkDisplay
*display
;
8423 char path
[PATH_MAX
];
8424 #if defined __MINGW32__
8425 GtkCssProvider
*windows_hacks
;
8428 provider
= gtk_css_provider_new();
8429 display
= gdk_display_get_default();
8430 screen
= gdk_display_get_default_screen(display
);
8431 snprintf(path
, sizeof path
, "%s" PS
"%s", resource_dir
, XT_CSS_FILE
);
8432 file
= g_file_new_for_path(path
);
8433 gtk_css_provider_load_from_file(provider
, file
, NULL
);
8434 gtk_style_context_add_provider_for_screen(screen
,
8435 GTK_STYLE_PROVIDER(provider
),
8436 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
8437 #if defined __MINGW32__
8438 windows_hacks
= gtk_css_provider_new();
8439 gtk_css_provider_load_from_data(windows_hacks
,
8441 " border-width: 0px;\n"
8443 gtk_style_context_add_provider_for_screen(screen
,
8444 GTK_STYLE_PROVIDER(windows_hacks
),
8445 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
8447 g_object_unref(file
);
8454 startpage_add("<b>Welcome to xombrero %s!</b><p>", version
);
8455 startpage_add("Details at "
8456 "<a href=https://opensource.conformal.com/wiki/xombrero>xombrero "
8457 "wiki page</a><p>");
8461 main(int argc
, char **argv
)
8464 int c
, optn
= 0, opte
= 0, focus
= 1;
8465 char conf
[PATH_MAX
] = { '\0' };
8466 char file
[PATH_MAX
];
8467 char sodversion
[32];
8468 char *env_proxy
= NULL
;
8473 start_argv
= (char * const *)argv
;
8480 #if !defined __MINGW32__
8481 /* http://web.archiveorange.com/archive/v/UsPjxkX5PsaXBIoOjqxf */
8484 /* http://developer.gnome.org/gdk/stable/gdk-Threads.html */
8485 g_thread_init(NULL
);
8486 gdk_threads_set_lock_functions(mtx_lock
, mtx_unlock
);
8488 gdk_threads_enter();
8490 /* http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html */
8491 gcry_control (GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
8493 gtk_init(&argc
, &argv
);
8495 gnutls_global_init();
8497 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
8502 RB_INIT(&force_https
);
8503 RB_INIT(&downloads
);
8509 TAILQ_INIT(&sessions
);
8512 TAILQ_INIT(&aliases
);
8521 #ifndef XT_RESOURCE_LIMITS_DISABLE
8525 GIOChannel
*channel
;
8527 /* fiddle with ulimits */
8528 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
8531 /* just use them all */
8532 rlp
.rlim_cur
= rlp
.rlim_max
;
8533 if (setrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
8535 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
8537 else if (rlp
.rlim_cur
< 1024)
8538 startpage_add("%s requires at least 1024 "
8539 "(2048 recommended) file " "descriptors, "
8540 "currently it has up to %d available",
8541 __progname
, rlp
.rlim_cur
);
8545 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
8554 #ifdef XOMBRERO_BUILDSTR
8555 errx(0 , "Version: %s Build: %s",
8556 version
, XOMBRERO_BUILDSTR
);
8558 errx(0 , "Version: %s", version
);
8562 strlcpy(conf
, optarg
, sizeof(conf
));
8565 strlcpy(named_session
, optarg
, sizeof(named_session
));
8586 pwd
= getpwuid(getuid());
8588 errx(1, "invalid user %d", getuid());
8590 /* set download dir */
8591 if (strlen(download_dir
) == 0)
8592 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
8594 /* compile buffer command regexes */
8597 /* set default dynamic string settings */
8598 home
= g_strdup(XT_DS_HOME
);
8599 search_string
= g_strdup(XT_DS_SEARCH_STRING
);
8600 strlcpy(runtime_settings
, "runtime", sizeof runtime_settings
);
8601 cmd_font_name
= g_strdup(XT_DS_CMD_FONT_NAME
);
8602 oops_font_name
= g_strdup(XT_DS_OOPS_FONT_NAME
);
8603 statusbar_font_name
= g_strdup(XT_DS_STATUSBAR_FONT_NAME
);
8604 tabbar_font_name
= g_strdup(XT_DS_TABBAR_FONT_NAME
);
8605 statusbar_elems
= g_strdup("BP");
8606 spell_check_languages
= g_strdup(XT_DS_SPELL_CHECK_LANGUAGES
);
8607 encoding
= g_strdup(XT_DS_ENCODING
);
8608 spell_check_languages
= g_strdup(XT_DS_SPELL_CHECK_LANGUAGES
);
8609 path
= g_strdup_printf("%s" PS
"style.css", resource_dir
);
8610 userstyle
= g_filename_to_uri(path
, NULL
, NULL
);
8612 stylesheet
= g_strdup(userstyle
);
8614 /* set statically allocated (struct special) settings */
8615 if (strlen(default_script
) == 0)
8616 expand_tilde(default_script
, sizeof default_script
,
8617 XT_DS_DEFAULT_SCRIPT
);
8618 if (strlen(ssl_ca_file
) == 0)
8619 expand_tilde(ssl_ca_file
, sizeof ssl_ca_file
,
8623 session
= webkit_get_default_session();
8625 /* read config file */
8626 if (strlen(conf
) == 0)
8627 snprintf(conf
, sizeof conf
, "%s" PS
".%s",
8628 pwd
->pw_dir
, XT_CONF_FILE
);
8629 config_parse(conf
, 0);
8631 /* read preloaded HSTS list */
8632 if (preload_strict_transport
) {
8633 snprintf(conf
, sizeof conf
, "%s" PS
"%s",
8634 resource_dir
, XT_HSTS_PRELOAD_FILE
);
8635 config_parse(conf
, 0);
8639 cmd_font
= pango_font_description_from_string(cmd_font_name
);
8640 oops_font
= pango_font_description_from_string(oops_font_name
);
8641 statusbar_font
= pango_font_description_from_string(statusbar_font_name
);
8642 tabbar_font
= pango_font_description_from_string(tabbar_font_name
);
8644 /* working directory */
8645 if (strlen(work_dir
) == 0)
8646 snprintf(work_dir
, sizeof work_dir
, "%s" PS
"%s",
8647 pwd
->pw_dir
, XT_DIR
);
8650 /* icon cache dir */
8651 snprintf(cache_dir
, sizeof cache_dir
, "%s" PS
"%s", work_dir
, XT_CACHE_DIR
);
8655 snprintf(certs_dir
, sizeof certs_dir
, "%s" PS
"%s", work_dir
, XT_CERT_DIR
);
8658 /* cert changes dir */
8659 snprintf(certs_cache_dir
, sizeof certs_cache_dir
, "%s" PS
"%s",
8660 work_dir
, XT_CERT_CACHE_DIR
);
8661 xxx_dir(certs_cache_dir
);
8664 snprintf(sessions_dir
, sizeof sessions_dir
, "%s" PS
"%s",
8665 work_dir
, XT_SESSIONS_DIR
);
8666 xxx_dir(sessions_dir
);
8669 snprintf(js_dir
, sizeof js_dir
, "%s" PS
"%s", work_dir
, XT_JS_DIR
);
8673 snprintf(temp_dir
, sizeof temp_dir
, "%s" PS
"%s", work_dir
, XT_TEMP_DIR
);
8676 /* runtime settings that can override config file */
8677 if (runtime_settings
[0] != '\0')
8678 config_parse(runtime_settings
, 1);
8681 if (!strcmp(download_dir
, pwd
->pw_dir
))
8682 strlcat(download_dir
, PS
"downloads", sizeof download_dir
);
8683 xxx_dir(download_dir
);
8685 /* first start file */
8686 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_SOD_FILE
);
8687 if (stat(file
, &sb
)) {
8688 warnx("start of day file doesn't exist, creating it");
8689 if ((f
= fopen(file
, "w")) == NULL
)
8690 err(1, "startofday");
8691 if (fputs(version
, f
) == EOF
)
8698 if ((f
= fopen(file
, "r+")) == NULL
)
8699 err(1, "startofday");
8700 if (fgets(sodversion
, sizeof sodversion
, f
) == NULL
)
8702 sodversion
[strcspn(sodversion
, "\n")] = '\0';
8703 if (strcmp(version
, sodversion
)) {
8704 if ((f
= freopen(file
, "w", f
)) == NULL
)
8705 err(1, "startofday");
8706 if (fputs(version
, f
) == EOF
)
8709 /* upgrade, say something smart */
8715 /* favorites file */
8716 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_FAVS_FILE
);
8717 if (stat(file
, &sb
)) {
8718 warnx("favorites file doesn't exist, creating it");
8719 if ((f
= fopen(file
, "w")) == NULL
)
8720 err(1, "favorites");
8724 /* quickmarks file */
8725 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
8726 if (stat(file
, &sb
)) {
8727 warnx("quickmarks file doesn't exist, creating it");
8728 if ((f
= fopen(file
, "w")) == NULL
)
8729 err(1, "quickmarks");
8733 /* search history */
8734 if (history_autosave
) {
8735 snprintf(search_file
, sizeof search_file
, "%s" PS
"%s",
8736 work_dir
, XT_SEARCH_FILE
);
8737 if (stat(search_file
, &sb
)) {
8738 warnx("search history file doesn't exist, creating it");
8739 if ((f
= fopen(search_file
, "w")) == NULL
)
8740 err(1, "search_history");
8743 history_read(&shl
, search_file
, &search_history_count
);
8746 /* command history */
8747 if (history_autosave
) {
8748 snprintf(command_file
, sizeof command_file
, "%s" PS
"%s",
8749 work_dir
, XT_COMMAND_FILE
);
8750 if (stat(command_file
, &sb
)) {
8751 warnx("command history file doesn't exist, creating it");
8752 if ((f
= fopen(command_file
, "w")) == NULL
)
8753 err(1, "command_history");
8756 history_read(&chl
, command_file
, &cmd_history_count
);
8762 /* guess_search regex */
8763 if (url_regex
== NULL
)
8764 url_regex
= g_strdup(XT_URL_REGEX
);
8766 if (regcomp(&url_re
, url_regex
, REG_EXTENDED
| REG_NOSUB
))
8767 startpage_add("invalid url regex %s", url_regex
);
8770 env_proxy
= getenv("http_proxy");
8772 setup_proxy(env_proxy
);
8774 env_proxy
= getenv("HTTP_PROXY");
8776 setup_proxy(env_proxy
);
8778 setup_proxy(http_proxy
);
8781 /* the user can optionally have the proxy disabled at startup */
8782 if ((http_proxy_starts_enabled
== 0) && (http_proxy
!= NULL
)) {
8783 http_proxy_save
= g_strdup(http_proxy
);
8787 #ifndef XT_SOCKET_DISABLE
8789 send_cmd_to_socket(argv
[0]);
8793 opte
= opte
; /* shut mingw up */
8795 /* set some connection parameters */
8796 g_object_set(session
, "max-conns", max_connections
, (char *)NULL
);
8797 g_object_set(session
, "max-conns-per-host", max_host_connections
,
8799 g_object_set(session
, SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
8802 g_signal_connect(session
, "request-queued", G_CALLBACK(session_rq_cb
),
8805 #ifndef XT_SOCKET_DISABLE
8806 /* see if there is already a xombrero running */
8807 if (single_instance
&& is_running()) {
8809 warnx("already running");
8814 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
8815 send_cmd_to_socket(cmd
);
8825 optn
= optn
; /* shut mingw up */
8830 if (enable_strict_transport
)
8831 strict_transport_init();
8833 /* uri completion */
8834 completion_model
= gtk_list_store_new(1, G_TYPE_STRING
);
8837 buffers_store
= gtk_list_store_new
8838 (NUM_COLS
, G_TYPE_UINT
, GDK_TYPE_PIXBUF
, G_TYPE_STRING
);
8844 notebook_tab_set_visibility();
8846 if (save_global_history
)
8847 restore_global_history();
8849 /* restore session list */
8850 restore_sessions_list();
8852 if (!strcmp(named_session
, XT_SAVED_TABS_FILE
))
8853 restore_saved_tabs();
8855 a
.s
= named_session
;
8856 a
.i
= XT_SES_DONOTHING
;
8857 open_tabs(NULL
, &a
);
8860 /* see if we have an exception */
8861 if (!TAILQ_EMPTY(&spl
)) {
8862 create_new_tab("about:startpage", NULL
, focus
, -1);
8867 create_new_tab(argv
[0], NULL
, focus
, -1);
8874 if (TAILQ_EMPTY(&tabs
))
8875 create_new_tab(home
, NULL
, 1, -1);
8876 #ifndef XT_SOCKET_DISABLE
8878 if ((s
= build_socket()) != -1) {
8879 channel
= g_io_channel_unix_new(s
);
8880 g_io_add_watch(channel
, G_IO_IN
, socket_watcher
, NULL
);
8884 #if GTK_CHECK_VERSION(3, 0, 0)
8892 gdk_threads_leave();
8893 g_static_rec_mutex_unlock_full(&my_gdk_mtx
); /* just in case */
8896 gnutls_global_deinit();