2 * Copyright (c) 2010, 2011 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2011 Stevan Andjelkovic <stevan@student.chalmers.se>
4 * Copyright (c) 2010, 2011, 2012 Edd Barrett <vext01@gmail.com>
5 * Copyright (c) 2011 Todd T. Fries <todd@fries.net>
6 * Copyright (c) 2011 Raphael Graf <r@undefined.ch>
7 * Copyright (c) 2011 Michal Mazurek <akfaew@jasminek.net>
8 * Copyright (c) 2012, 2013 Josh Rickmar <jrick@devio.us>
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 char *version
= XOMBRERO_VERSION
;
29 uint32_t swm_debug
= 0
60 TAILQ_ENTRY(session
) entry
;
63 TAILQ_HEAD(session_list
, session
);
66 TAILQ_ENTRY(undo
) entry
;
69 int back
; /* Keeps track of how many back
70 * history items there are. */
72 TAILQ_HEAD(undo_tailq
, undo
);
74 struct command_entry
{
76 TAILQ_ENTRY(command_entry
) entry
;
78 TAILQ_HEAD(command_list
, command_entry
);
81 #define XT_CACHE_DIR ("cache")
82 #define XT_CERT_DIR ("certs")
83 #define XT_CERT_CACHE_DIR ("certs_cache")
84 #define XT_JS_DIR ("js")
85 #define XT_SESSIONS_DIR ("sessions")
86 #define XT_TEMP_DIR ("tmp")
87 #define XT_QMARKS_FILE ("quickmarks")
88 #define XT_SAVED_TABS_FILE ("main_session")
89 #define XT_RESTART_TABS_FILE ("restart_tabs")
90 #define XT_SOCKET_FILE ("socket")
91 #define XT_SAVE_SESSION_ID ("SESSION_NAME=")
92 #define XT_SEARCH_FILE ("search_history")
93 #define XT_COMMAND_FILE ("command_history")
94 #define XT_DLMAN_REFRESH "10"
95 #define XT_MAX_URL_LENGTH (4096) /* 1 page is atomic, don't make bigger */
96 #define XT_MAX_UNDO_CLOSE_TAB (32)
97 #define XT_PRINT_EXTRA_MARGIN 10
98 #define XT_URL_REGEX ("^[[:blank:]]*[^[:blank:]]*([[:alnum:]-]+\\.)+[[:alnum:]-][^[:blank:]]*[[:blank:]]*$")
99 #define XT_INVALID_MARK (-1) /* XXX this is a double, maybe use something else, like a nan */
102 #define XT_COLOR_RED "#cc0000"
103 #define XT_COLOR_YELLOW "#ffff66"
104 #define XT_COLOR_BLUE "lightblue"
105 #define XT_COLOR_GREEN "#99ff66"
106 #define XT_COLOR_WHITE "white"
107 #define XT_COLOR_BLACK "black"
109 #define XT_COLOR_CT_BACKGROUND "#000000"
110 #define XT_COLOR_CT_INACTIVE "#dddddd"
111 #define XT_COLOR_CT_ACTIVE "#bbbb00"
112 #define XT_COLOR_CT_SEPARATOR "#555555"
114 #define XT_COLOR_SB_SEPARATOR "#555555"
116 /* CSS element names */
117 #define XT_CSS_NORMAL ""
118 #define XT_CSS_RED "red"
119 #define XT_CSS_YELLOW "yellow"
120 #define XT_CSS_GREEN "green"
121 #define XT_CSS_BLUE "blue"
122 #define XT_CSS_HIDDEN "hidden"
123 #define XT_CSS_ACTIVE "active"
125 #define XT_PROTO_DELIM "://"
128 #define XT_MOVE_INVALID (0)
129 #define XT_MOVE_DOWN (1)
130 #define XT_MOVE_UP (2)
131 #define XT_MOVE_BOTTOM (3)
132 #define XT_MOVE_TOP (4)
133 #define XT_MOVE_PAGEDOWN (5)
134 #define XT_MOVE_PAGEUP (6)
135 #define XT_MOVE_HALFDOWN (7)
136 #define XT_MOVE_HALFUP (8)
137 #define XT_MOVE_LEFT (9)
138 #define XT_MOVE_FARLEFT (10)
139 #define XT_MOVE_RIGHT (11)
140 #define XT_MOVE_FARRIGHT (12)
141 #define XT_MOVE_PERCENT (13)
142 #define XT_MOVE_CENTER (14)
144 #define XT_QMARK_SET (0)
145 #define XT_QMARK_OPEN (1)
146 #define XT_QMARK_TAB (2)
148 #define XT_MARK_SET (0)
149 #define XT_MARK_GOTO (1)
151 #define XT_GO_UP_ROOT (999)
153 #define XT_NAV_INVALID (0)
154 #define XT_NAV_BACK (1)
155 #define XT_NAV_FORWARD (2)
156 #define XT_NAV_RELOAD (3)
157 #define XT_NAV_STOP (4)
159 #define XT_FOCUS_INVALID (0)
160 #define XT_FOCUS_URI (1)
161 #define XT_FOCUS_SEARCH (2)
163 #define XT_SEARCH_INVALID (0)
164 #define XT_SEARCH_NEXT (1)
165 #define XT_SEARCH_PREV (2)
167 #define XT_PASTE_CURRENT_TAB (0)
168 #define XT_PASTE_NEW_TAB (1)
170 #define XT_ZOOM_IN (-1)
171 #define XT_ZOOM_OUT (-2)
172 #define XT_ZOOM_NORMAL (100)
174 #define XT_SES_DONOTHING (0)
175 #define XT_SES_CLOSETABS (1)
177 #define XT_PREFIX (1<<0)
178 #define XT_USERARG (1<<1)
179 #define XT_URLARG (1<<2)
180 #define XT_INTARG (1<<3)
181 #define XT_SESSARG (1<<4)
182 #define XT_SETARG (1<<5)
184 #define XT_HINT_NEWTAB (1<<0)
186 #define XT_BUFCMD_SZ (8)
188 #define XT_EJS_SHOW (1<<0)
190 GtkWidget
* create_button(char *, char *, int);
192 void recalc_tabs(void);
193 void recolor_compact_tabs(void);
194 void set_current_tab(int page_num
);
195 gboolean
update_statusbar_position(GtkAdjustment
*, gpointer
);
196 void marks_clear(struct tab
*t
);
199 extern char *__progname
;
200 char * const *start_argv
;
202 GtkWidget
*main_window
;
203 GtkNotebook
*notebook
;
205 GtkWidget
*tab_bar_box
;
206 GtkWidget
*arrow
, *abtn
;
207 GdkEvent
*fevent
= NULL
;
208 struct tab_list tabs
;
209 struct history_list hl
;
210 int hl_purge_count
= 0;
211 struct session_list sessions
;
213 struct wl_list js_wl
;
214 struct wl_list pl_wl
;
215 struct wl_list force_https
;
217 struct strict_transport_tree st_tree
;
218 struct undo_tailq undos
;
219 struct keybinding_list kbl
;
221 struct user_agent_list ua_list
;
222 struct http_accept_list ha_list
;
223 struct domain_id_list di_list
;
224 struct cmd_alias_list cal
;
225 struct custom_uri_list cul
;
226 struct command_list chl
;
227 struct command_list shl
;
228 struct command_entry
*history_at
;
229 struct command_entry
*search_at
;
230 struct secviolation_list svl
;
231 struct set_reject_list srl
;
233 int cmd_history_count
= 0;
234 int search_history_count
= 0;
236 uint64_t blocked_cookies
= 0;
237 char named_session
[PATH_MAX
];
238 GtkListStore
*completion_model
;
239 GtkListStore
*buffers_store
;
242 char *qmarks
[XT_NOQMARKS
];
243 int btn_down
; /* M1 down in any wv */
244 regex_t url_re
; /* guess_search regex */
246 /* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
247 int next_download_id
= 1;
249 void xxx_dir(char *);
250 int icon_size_map(int);
251 void activate_uri_entry_cb(GtkWidget
*, struct tab
*);
254 history_delete(struct command_list
*l
, int *counter
)
256 struct command_entry
*c
;
258 if (l
== NULL
|| counter
== NULL
)
261 c
= TAILQ_LAST(l
, command_list
);
265 TAILQ_REMOVE(l
, c
, entry
);
272 history_add(struct command_list
*list
, char *file
, char *l
, int *counter
)
274 struct command_entry
*c
;
277 if (list
== NULL
|| l
== NULL
|| counter
== NULL
)
280 /* don't add the same line */
281 c
= TAILQ_FIRST(list
);
283 if (!strcmp(c
->line
+ 1 /* skip space */, l
))
286 c
= g_malloc0(sizeof *c
);
287 c
->line
= g_strdup_printf(" %s", l
);
290 TAILQ_INSERT_HEAD(list
, c
, entry
);
293 history_delete(list
, counter
);
295 if (history_autosave
&& file
) {
296 f
= fopen(file
, "w");
298 show_oops(NULL
, "couldn't write history %s", file
);
302 TAILQ_FOREACH_REVERSE(c
, list
, command_list
, entry
) {
304 fprintf(f
, "%s\n", c
->line
);
312 history_read(struct command_list
*list
, char *file
, int *counter
)
315 char *s
, line
[65536];
317 if (list
== NULL
|| file
== NULL
)
320 f
= fopen(file
, "r");
322 startpage_add("couldn't open history file %s", file
);
327 s
= fgets(line
, sizeof line
, f
);
328 if (s
== NULL
|| feof(f
) || ferror(f
))
330 if ((s
= strchr(line
, '\n')) == NULL
) {
331 startpage_add("invalid history file %s", file
);
337 history_add(list
, NULL
, line
+ 1, counter
);
345 /* marks array storage. */
349 if (i
< 0 || i
>= XT_NOMARKS
)
360 if ((ret
= strchr(XT_MARKS
, m
)) != NULL
)
361 return ret
- XT_MARKS
;
366 /* quickmarks array storage. */
370 if (i
< 0 || i
>= XT_NOQMARKS
)
381 if ((ret
= strchr(XT_QMARKS
, m
)) != NULL
)
382 return ret
- XT_QMARKS
;
388 is_g_object_setting(GObject
*o
, char *str
)
390 guint n_props
= 0, i
;
391 GParamSpec
**proplist
;
397 proplist
= g_object_class_list_properties(G_OBJECT_GET_CLASS(o
),
400 for (i
= 0; i
< n_props
; i
++) {
401 if (! strcmp(proplist
[i
]->name
, str
)) {
412 get_current_tab(void)
416 TAILQ_FOREACH(t
, &tabs
, entry
) {
417 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
421 warnx("%s: no current tab", __func__
);
427 set_ssl_ca_file(struct settings
*s
, char *file
)
431 if (file
== NULL
|| strlen(file
) == 0)
433 if (stat(file
, &sb
)) {
434 warnx("no CA file: %s", file
);
437 expand_tilde(ssl_ca_file
, sizeof ssl_ca_file
, file
);
438 g_object_set(session
,
439 SOUP_SESSION_SSL_CA_FILE
, ssl_ca_file
,
440 SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
446 set_status(struct tab
*t
, gchar
*fmt
, ...)
453 status
= g_strdup_vprintf(fmt
, ap
);
455 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.uri
), status
);
459 else if (strcmp(t
->status
, status
)) {
470 hide_cmd(struct tab
*t
)
472 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
474 history_at
= NULL
; /* just in case */
475 search_at
= NULL
; /* just in case */
476 gtk_widget_set_can_focus(t
->cmd
, FALSE
);
477 gtk_widget_hide(t
->cmd
);
481 show_cmd(struct tab
*t
, const char *s
)
483 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
485 /* without this you can't middle click in t->cmd to paste */
486 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), "");
490 gtk_widget_hide(t
->oops
);
491 gtk_widget_set_can_focus(t
->cmd
, TRUE
);
492 gtk_widget_show(t
->cmd
);
494 gtk_widget_grab_focus(GTK_WIDGET(t
->cmd
));
495 #if GTK_CHECK_VERSION(3, 0, 0)
496 gtk_widget_set_name(t
->cmd
, XT_CSS_NORMAL
);
498 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
499 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
501 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), s
);
502 gtk_editable_set_position(GTK_EDITABLE(t
->cmd
), -1);
506 hide_buffers(struct tab
*t
)
508 gtk_widget_hide(t
->buffers
);
509 gtk_widget_set_can_focus(t
->buffers
, FALSE
);
510 gtk_list_store_clear(buffers_store
);
521 sort_tabs_by_page_num(struct tab
***stabs
)
526 num_tabs
= gtk_notebook_get_n_pages(notebook
);
528 *stabs
= g_malloc0(num_tabs
* sizeof(struct tab
*));
530 TAILQ_FOREACH(t
, &tabs
, entry
)
531 (*stabs
)[gtk_notebook_page_num(notebook
, t
->vbox
)] = t
;
537 buffers_make_list(void)
540 const gchar
*title
= NULL
;
542 struct tab
**stabs
= NULL
;
544 num_tabs
= sort_tabs_by_page_num(&stabs
);
546 for (i
= 0; i
< num_tabs
; i
++)
548 gtk_list_store_append(buffers_store
, &iter
);
549 title
= get_title(stabs
[i
], FALSE
);
550 gtk_list_store_set(buffers_store
, &iter
,
551 COL_ID
, i
+ 1, /* Enumerate the tabs starting from 1
553 COL_FAVICON
, gtk_image_get_pixbuf
554 (GTK_IMAGE(stabs
[i
]->tab_elems
.favicon
)),
563 show_buffers(struct tab
*t
)
566 GtkTreeSelection
*sel
;
570 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)))
575 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(t
->buffers
));
576 index
= gtk_notebook_get_current_page(notebook
);
577 path
= gtk_tree_path_new_from_indices(index
, -1);
578 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
, path
))
579 gtk_tree_selection_select_iter(sel
, &iter
);
580 gtk_tree_path_free(path
);
582 gtk_widget_show(t
->buffers
);
583 gtk_widget_set_can_focus(t
->buffers
, TRUE
);
584 gtk_widget_grab_focus(GTK_WIDGET(t
->buffers
));
588 toggle_buffers(struct tab
*t
)
590 if (gtk_widget_get_visible(t
->buffers
))
597 buffers(struct tab
*t
, struct karg
*args
)
605 set_scrollbar_visibility(struct tab
*t
, int visible
)
607 #if GTK_CHECK_VERSION(3, 0, 0)
608 GtkWidget
*h_scrollbar
, *v_scrollbar
;
610 h_scrollbar
= gtk_scrolled_window_get_hscrollbar(
611 GTK_SCROLLED_WINDOW(t
->browser_win
));
612 v_scrollbar
= gtk_scrolled_window_get_vscrollbar(
613 GTK_SCROLLED_WINDOW(t
->browser_win
));
616 gtk_widget_set_name(h_scrollbar
, XT_CSS_HIDDEN
);
617 gtk_widget_set_name(v_scrollbar
, XT_CSS_HIDDEN
);
619 gtk_widget_set_name(h_scrollbar
, "");
620 gtk_widget_set_name(v_scrollbar
, "");
625 return (visible
== 0);
630 hide_oops(struct tab
*t
)
632 gtk_widget_hide(t
->oops
);
636 show_oops(struct tab
*at
, const char *fmt
, ...)
640 struct tab
*t
= NULL
;
646 if ((t
= get_current_tab()) == NULL
)
652 if ((msg
= g_strdup_vprintf(fmt
, ap
)) == NULL
)
653 errx(1, "show_oops failed");
656 gtk_entry_set_text(GTK_ENTRY(t
->oops
), msg
);
657 gtk_widget_hide(t
->cmd
);
658 gtk_widget_show(t
->oops
);
664 char work_dir
[PATH_MAX
];
665 char certs_dir
[PATH_MAX
];
666 char certs_cache_dir
[PATH_MAX
];
667 char js_dir
[PATH_MAX
];
668 char cache_dir
[PATH_MAX
];
669 char sessions_dir
[PATH_MAX
];
670 char temp_dir
[PATH_MAX
];
671 char cookie_file
[PATH_MAX
];
672 char *strict_transport_file
= NULL
;
673 SoupSession
*session
;
674 SoupCookieJar
*s_cookiejar
;
675 SoupCookieJar
*p_cookiejar
;
676 char rc_fname
[PATH_MAX
];
678 struct mime_type_list mtl
;
679 struct alias_list aliases
;
682 struct tab
*create_new_tab(char *, struct undo
*, int, int);
683 void delete_tab(struct tab
*);
684 void setzoom_webkit(struct tab
*, int);
685 int download_rb_cmp(struct download
*, struct download
*);
686 gboolean
cmd_execute(struct tab
*t
, char *str
);
689 history_rb_cmp(struct history
*h1
, struct history
*h2
)
691 return (strcmp(h1
->uri
, h2
->uri
));
693 RB_GENERATE(history_list
, history
, entry
, history_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
);
724 domain_id_rb_cmp(struct domain_id
*d1
, struct domain_id
*d2
)
726 return (strcmp(d1
->domain
, d2
->domain
));
728 RB_GENERATE(domain_id_list
, domain_id
, entry
, domain_id_rb_cmp
);
730 struct valid_url_types
{
742 valid_url_type(char *url
)
746 for (i
= 0; i
< LENGTH(vut
); i
++)
747 if (!strncasecmp(vut
[i
].type
, url
, strlen(vut
[i
].type
)))
754 match_alias(char *url_in
)
758 char *url_out
= NULL
, *search
, *enc_arg
;
761 search
= g_strdup(url_in
);
763 if (strsep(&arg
, " \t") == NULL
) {
764 show_oops(NULL
, "match_alias: NULL URL");
768 TAILQ_FOREACH(a
, &aliases
, entry
) {
769 if (!strcmp(search
, a
->a_name
))
774 DNPRINTF(XT_D_URL
, "match_alias: matched alias %s\n",
778 enc_arg
= soup_uri_encode(arg
, XT_RESERVED_CHARS
);
779 sv
= g_strsplit(a
->a_uri
, "%s", 2);
781 url_out
= g_strjoinv(enc_arg
, sv
);
783 url_out
= g_strjoinv("", sv
);
793 guess_url_type(char *url_in
)
796 char cwd
[PATH_MAX
] = {0};
797 char *url_out
= NULL
, *enc_search
= NULL
;
803 /* substitute aliases */
804 url_out
= match_alias(url_in
);
808 /* see if we are an about page */
809 if (!strncmp(url_in
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
))
810 for (i
= 0; i
< about_list_size(); i
++)
811 if (!strcmp(&url_in
[XT_URI_ABOUT_LEN
],
812 about_list
[i
].name
)) {
813 url_out
= g_strdup(url_in
);
817 if (guess_search
&& url_regex
&&
818 !(g_str_has_prefix(url_in
, "http://") ||
819 g_str_has_prefix(url_in
, "https://"))) {
820 if (regexec(&url_re
, url_in
, 0, NULL
, 0)) {
821 /* invalid URI so search instead */
822 enc_search
= soup_uri_encode(url_in
, XT_RESERVED_CHARS
);
823 sv
= g_strsplit(search_string
, "%s", 2);
824 url_out
= g_strjoinv(enc_search
, sv
);
831 /* XXX not sure about this heuristic */
832 if (stat(url_in
, &sb
) == 0) {
833 if (url_in
[0] == '/')
834 url_out
= g_filename_to_uri(url_in
, NULL
, NULL
);
836 if (getcwd(cwd
, PATH_MAX
) != NULL
) {
837 path
= g_strdup_printf("%s" PS
"%s", cwd
,
839 url_out
= g_filename_to_uri(path
, NULL
, NULL
);
844 url_out
= g_strdup_printf("http://%s", url_in
); /* guess http */
846 DNPRINTF(XT_D_URL
, "guess_url_type: guessed %s\n", url_out
);
852 set_normal_tab_meaning(struct tab
*t
)
857 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
858 if (t
->session_key
!= NULL
) {
859 g_free(t
->session_key
);
860 t
->session_key
= NULL
;
865 load_uri(struct tab
*t
, gchar
*uri
)
868 gchar
*newuri
= NULL
;
874 /* Strip leading spaces. */
875 while (*uri
&& isspace(*uri
))
878 if (strlen(uri
) == 0) {
883 set_normal_tab_meaning(t
);
885 if (valid_url_type(uri
)) {
886 newuri
= guess_url_type(uri
);
890 if (!strncmp(uri
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
)) {
891 for (i
= 0; i
< about_list_size(); i
++)
892 if (!strcmp(&uri
[XT_URI_ABOUT_LEN
], about_list
[i
].name
) &&
893 about_list
[i
].func
!= NULL
) {
894 bzero(&args
, sizeof args
);
895 about_list
[i
].func(t
, &args
);
896 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
),
900 show_oops(t
, "invalid about page");
904 /* remove old HTTPS cert chain (if any) */
906 g_free(t
->cert_chain
);
907 t
->cert_chain
= NULL
;
910 set_status(t
, "Loading: %s", (char *)uri
);
912 webkit_web_view_load_uri(t
->wv
, uri
);
919 get_uri(struct tab
*t
)
921 const gchar
*uri
= NULL
;
923 if (webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
&&
924 !t
->download_requested
)
926 if (t
->xtp_meaning
== XT_XTP_TAB_MEANING_NORMAL
)
927 uri
= webkit_web_view_get_uri(t
->wv
);
929 /* use tmp_uri to make sure it is g_freed */
932 t
->tmp_uri
= g_strdup_printf("%s%s", XT_URI_ABOUT
,
933 about_list
[t
->xtp_meaning
].name
);
940 get_title(struct tab
*t
, bool window
)
942 const gchar
*set
= NULL
, *title
= NULL
;
943 WebKitLoadStatus status
= webkit_web_view_get_load_status(t
->wv
);
945 if (status
== WEBKIT_LOAD_PROVISIONAL
||
946 (status
== WEBKIT_LOAD_FAILED
&& !t
->download_requested
) ||
947 t
->xtp_meaning
== XT_XTP_TAB_MEANING_BL
)
950 title
= webkit_web_view_get_title(t
->wv
);
951 if ((set
= title
? title
: get_uri(t
)))
955 set
= window
? XT_NAME
: "(untitled)";
961 find_mime_type(char *mime_type
)
963 struct mime_type
*m
, *def
= NULL
, *rv
= NULL
;
965 TAILQ_FOREACH(m
, &mtl
, entry
) {
967 !strncmp(mime_type
, m
->mt_type
, strlen(m
->mt_type
)))
970 if (m
->mt_default
== 0 && !strcmp(mime_type
, m
->mt_type
)) {
983 * This only escapes the & and < characters, as per the discussion found here:
984 * http://lists.apple.com/archives/Webkitsdk-dev/2007/May/msg00056.html
987 html_escape(const char *val
)
995 sv
= g_strsplit(val
, "&", -1);
996 s
= g_strjoinv("&", sv
);
999 sv
= g_strsplit(val
, "<", -1);
1000 s
= g_strjoinv("<", sv
);
1007 wl_find_uri(const gchar
*s
, struct wl_list
*wl
)
1013 if (s
== NULL
|| wl
== NULL
)
1016 if (!strncmp(s
, "http://", strlen("http://")))
1017 s
= &s
[strlen("http://")];
1018 else if (!strncmp(s
, "https://", strlen("https://")))
1019 s
= &s
[strlen("https://")];
1024 for (i
= 0; i
< strlen(s
) + 1 /* yes er need this */; i
++)
1025 /* chop string at first slash */
1026 if (s
[i
] == '/' || s
[i
] == ':' || s
[i
] == '\0') {
1029 w
= wl_find(ss
, wl
);
1038 js_ref_to_string(JSContextRef context
, JSValueRef ref
)
1044 jsref
= JSValueToStringCopy(context
, ref
, NULL
);
1048 l
= JSStringGetMaximumUTF8CStringSize(jsref
);
1051 JSStringGetUTF8CString(jsref
, s
, l
);
1052 JSStringRelease(jsref
);
1057 #define XT_JS_DONE ("done;")
1058 #define XT_JS_DONE_LEN (strlen(XT_JS_DONE))
1059 #define XT_JS_INSERT ("insert;")
1060 #define XT_JS_INSERT_LEN (strlen(XT_JS_INSERT))
1063 run_script(struct tab
*t
, char *s
)
1065 JSGlobalContextRef ctx
;
1066 WebKitWebFrame
*frame
;
1068 JSValueRef val
, exception
;
1071 DNPRINTF(XT_D_JS
, "%s: tab %d %s\n", __func__
,
1072 t
->tab_id
, s
== (char *)JS_HINTING
? "JS_HINTING" : s
);
1074 frame
= webkit_web_view_get_main_frame(t
->wv
);
1075 ctx
= webkit_web_frame_get_global_context(frame
);
1077 str
= JSStringCreateWithUTF8CString(s
);
1078 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
1079 NULL
, 0, &exception
);
1080 JSStringRelease(str
);
1082 DNPRINTF(XT_D_JS
, "%s: val %p\n", __func__
, val
);
1084 es
= js_ref_to_string(ctx
, exception
);
1086 DNPRINTF(XT_D_JS
, "%s: exception %s\n", __func__
, es
);
1091 es
= js_ref_to_string(ctx
, val
);
1094 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
1096 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
1100 DNPRINTF(XT_D_JS
, "%s: val %s\n", __func__
, es
);
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 (g_str_has_prefix(uri
, XT_SAVE_SESSION_ID
)) {
1307 strlcpy(named_session
,
1308 &uri
[strlen(XT_SAVE_SESSION_ID
)],
1309 sizeof named_session
);
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 proxy_toggle_cb(GtkWidget
*w
, struct tab
*t
)
1593 struct karg args
= {0};
1595 args
.i
= XT_PRXY_TOGGLE
;
1596 proxy_cmd(t
, &args
);
1600 toggle_src(struct tab
*t
, struct karg
*args
)
1607 mode
= webkit_web_view_get_view_source_mode(t
->wv
);
1608 webkit_web_view_set_view_source_mode(t
->wv
, !mode
);
1609 webkit_web_view_reload(t
->wv
);
1615 focus_webview(struct tab
*t
)
1620 /* only grab focus if we are visible */
1621 if (gtk_notebook_get_current_page(notebook
) == t
->tab_id
)
1622 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
1626 focus(struct tab
*t
, struct karg
*args
)
1628 if (t
== NULL
|| args
== NULL
)
1634 if (args
->i
== XT_FOCUS_URI
)
1635 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
1636 else if (args
->i
== XT_FOCUS_SEARCH
)
1637 gtk_widget_grab_focus(GTK_WIDGET(t
->search_entry
));
1643 free_connection_certs(gnutls_x509_crt_t
*certs
, size_t cert_count
)
1647 for (i
= 0; i
< cert_count
; i
++)
1648 gnutls_x509_crt_deinit(certs
[i
]);
1652 #if GTK_CHECK_VERSION(3, 0, 0)
1654 statusbar_modify_attr(struct tab
*t
, const char *css_name
)
1656 gtk_widget_set_name(t
->sbe
.ebox
, css_name
);
1660 statusbar_modify_attr(struct tab
*t
, const char *text
, const char *base
)
1662 GdkColor c_text
, c_base
;
1664 gdk_color_parse(text
, &c_text
);
1665 gdk_color_parse(base
, &c_base
);
1667 gtk_widget_modify_bg(t
->sbe
.ebox
, GTK_STATE_NORMAL
, &c_base
);
1668 gtk_widget_modify_base(t
->sbe
.uri
, GTK_STATE_NORMAL
, &c_base
);
1669 gtk_widget_modify_text(t
->sbe
.uri
, GTK_STATE_NORMAL
, &c_text
);
1674 save_certs(struct tab
*t
, gnutls_x509_crt_t
*certs
,
1675 size_t cert_count
, const char *domain
, const char *dir
)
1678 char cert_buf
[64 * 1024], file
[PATH_MAX
];
1682 if (t
== NULL
|| certs
== NULL
|| cert_count
<= 0 || domain
== NULL
)
1685 snprintf(file
, sizeof file
, "%s" PS
"%s", dir
, domain
);
1686 if ((f
= fopen(file
, "w")) == NULL
) {
1687 show_oops(t
, "Can't create cert file %s %s",
1688 file
, strerror(errno
));
1692 for (i
= 0; i
< cert_count
; i
++) {
1693 cert_buf_sz
= sizeof cert_buf
;
1694 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1695 cert_buf
, &cert_buf_sz
)) {
1696 show_oops(t
, "gnutls_x509_crt_export failed");
1699 if (fwrite(cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1700 show_oops(t
, "Can't write certs: %s", strerror(errno
));
1717 get_local_cert_chain(const char *uri
, size_t *ncerts
, const char **error_str
,
1721 unsigned char cert_buf
[64 * 1024] = {0};
1722 gnutls_datum_t data
;
1723 unsigned int len
= UINT_MAX
;
1725 char file
[PATH_MAX
];
1727 gnutls_x509_crt_t
*certs
;
1729 if ((su
= soup_uri_new(uri
)) == NULL
) {
1730 *error_str
= "Invalid URI";
1734 snprintf(file
, sizeof file
, "%s" PS
"%s", dir
, su
->host
);
1736 if ((f
= fopen(file
, "r")) == NULL
) {
1737 *error_str
= "Could not read local cert";
1741 bytes_read
= fread(cert_buf
, sizeof *cert_buf
, sizeof cert_buf
, f
);
1742 if (bytes_read
== 0) {
1743 *error_str
= "Could not read local cert";
1747 data
.data
= cert_buf
;
1748 data
.size
= bytes_read
;
1749 certs
= g_malloc(sizeof *certs
);
1751 if (gnutls_x509_crt_list_import(certs
, &len
, &data
,
1752 GNUTLS_X509_FMT_PEM
, 0) < 0) {
1754 *error_str
= "Error reading local cert chain";
1763 * This uses the pem-encoded cert chain saved in t->cert_chain instead
1764 * of grabbing the remote cert. We save it beforehand and read it
1765 * here so as to not open a side channel that ignores proxy settings.
1768 get_remote_cert_chain(const char *uri
, size_t *ncerts
, const char **error_str
,
1771 gnutls_datum_t data
;
1772 unsigned int len
= UINT_MAX
;
1773 gnutls_x509_crt_t
*certs
;
1775 if (chain
== NULL
) {
1776 *error_str
= "Error reading remote cert chain";
1780 data
.data
= (unsigned char *)chain
;
1781 data
.size
= strlen(chain
);
1782 certs
= g_malloc(sizeof *certs
);
1784 if (gnutls_x509_crt_list_import(certs
, &len
, &data
,
1785 GNUTLS_X509_FMT_PEM
, 0) < 0) {
1787 *error_str
= "Error reading remote cert chain";
1797 cert_cmd(struct tab
*t
, struct karg
*args
)
1799 const gchar
*uri
, *error_str
= NULL
;
1801 gnutls_x509_crt_t
*certs
;
1803 #if !GTK_CHECK_VERSION(3, 0, 0)
1810 if (args
->s
!= NULL
) {
1812 } else if ((uri
= get_uri(t
)) == NULL
) {
1813 show_oops(t
, "Invalid URI");
1816 if ((su
= soup_uri_new(uri
)) == NULL
) {
1817 show_oops(t
, "Invalid URI");
1822 * if we're only showing the local certs, don't open a socket and get
1825 if (args
->i
& XT_SHOW
&& args
->i
& XT_CACHE
) {
1826 certs
= get_local_cert_chain(uri
, &cert_count
, &error_str
,
1828 if (error_str
== NULL
) {
1829 show_certs(t
, certs
, cert_count
, "Certificate Chain");
1830 free_connection_certs(certs
, cert_count
);
1832 show_oops(t
, "%s", error_str
);
1838 certs
= get_remote_cert_chain(uri
, &cert_count
, &error_str
,
1843 if (args
->i
& XT_SHOW
)
1844 show_certs(t
, certs
, cert_count
, "Certificate Chain");
1845 else if (args
->i
& XT_SAVE
) {
1846 save_certs(t
, certs
, cert_count
, su
->host
, certs_dir
);
1847 #if GTK_CHECK_VERSION(3, 0, 0)
1848 gtk_widget_set_name(t
->uri_entry
, XT_CSS_BLUE
);
1849 statusbar_modify_attr(t
, XT_CSS_BLUE
);
1851 gdk_color_parse(XT_COLOR_BLUE
, &color
);
1852 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
1853 statusbar_modify_attr(t
, XT_COLOR_BLACK
, XT_COLOR_BLUE
);
1855 } else if (args
->i
& XT_CACHE
)
1856 save_certs(t
, certs
, cert_count
, su
->host
, certs_cache_dir
);
1858 free_connection_certs(certs
, cert_count
);
1863 if (error_str
&& strlen(error_str
))
1864 show_oops(t
, "%s", error_str
);
1869 * Checks whether the remote cert is identical to the local saved
1870 * cert. Returns CERT_LOCAL if unchanged, CERT_UNTRUSTED if local
1871 * cert does not exist, and CERT_BAD if different.
1873 * Saves entire cert chain in pem encoding to chain for it to be
1874 * cached later, if needed.
1877 check_local_certs(const char *file
, GTlsCertificate
*cert
, char **chain
)
1879 char r_cert_buf
[64 * 1024];
1881 GTlsCertificate
*tmpcert
= NULL
;
1882 GTlsCertificate
*issuer
;
1885 enum cert_trust rv
= CERT_LOCAL
;
1888 if ((f
= fopen(file
, "r")) == NULL
) {
1889 /* no local cert to check */
1890 rv
= CERT_UNTRUSTED
;
1895 g_object_get(G_OBJECT(cert
), "certificate-pem", &pem
,
1897 g_object_get(G_OBJECT(cert
), "issuer", &issuer
,NULL
);
1901 g_object_get(G_OBJECT(tmpcert
), "issuer", &issuer
,
1906 g_object_get(G_OBJECT(tmpcert
), "certificate-pem", &pem
,
1913 if (fread(r_cert_buf
, strlen(pem
), 1, f
) != 1 && !feof(f
))
1915 if (bcmp(r_cert_buf
, pem
, strlen(pem
)))
1918 tmp
= g_strdup_printf("%s%s", *chain
, pem
);
1929 check_cert_changes(struct tab
*t
, GTlsCertificate
*cert
, const char *file
, const char *uri
)
1931 SoupURI
*soupuri
= NULL
;
1932 struct karg args
= {0};
1933 struct wl_entry
*w
= NULL
;
1937 chain
= g_strdup("");
1938 switch(check_local_certs(file
, cert
, &chain
)) {
1940 /* The cached certificate is identical */
1942 case CERT_TRUSTED
: /* FALLTHROUGH */
1943 case CERT_UNTRUSTED
:
1944 /* cache new certificate */
1950 if ((soupuri
= soup_uri_new(uri
)) == NULL
||
1951 soupuri
->host
== NULL
)
1953 if ((w
= wl_find(soupuri
->host
, &svil
)) != NULL
)
1955 t
->xtp_meaning
= XT_XTP_TAB_MEANING_SV
;
1956 args
.s
= g_strdup((char *)uri
);
1957 xtp_page_sv(t
, &args
);
1962 soup_uri_free(soupuri
);
1969 remove_cookie(int index
)
1975 DNPRINTF(XT_D_COOKIE
, "remove_cookie: %d\n", index
);
1977 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
1979 for (i
= 1; cf
; cf
= cf
->next
, i
++) {
1983 print_cookie("remove cookie", c
);
1984 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
1989 soup_cookies_free(cf
);
1995 remove_cookie_domain(int domain_id
)
1997 int domain_count
, rv
= 1;
2002 DNPRINTF(XT_D_COOKIE
, "remove_cookie_domain: %d\n", domain_id
);
2005 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
2007 for (domain_count
= 0; cf
; cf
= cf
->next
) {
2010 if (strcmp(last_domain
, c
->domain
) != 0) {
2012 last_domain
= c
->domain
;
2015 if (domain_count
< domain_id
)
2017 else if (domain_count
> domain_id
)
2020 print_cookie("remove cookie", c
);
2021 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2025 soup_cookies_free(cf
);
2037 DNPRINTF(XT_D_COOKIE
, "remove_cookie_all\n");
2039 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
2041 for (; cf
; cf
= cf
->next
) {
2044 print_cookie("remove cookie", c
);
2045 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2049 soup_cookies_free(cf
);
2055 toplevel_cmd(struct tab
*t
, struct karg
*args
)
2057 js_toggle_cb(t
->js_toggle
, t
);
2063 can_go_back_for_real(struct tab
*t
)
2066 WebKitWebHistoryItem
*item
;
2072 if (t
->item
!= NULL
)
2075 /* rely on webkit to make sure we can go backward when on an about page */
2077 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:") ||
2078 g_str_has_prefix(uri
, "xxxt://"))
2079 return (webkit_web_view_can_go_back(t
->wv
));
2081 /* the back/forward list is stupid so help determine if we can go back */
2082 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2084 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2085 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
2093 can_go_forward_for_real(struct tab
*t
)
2096 WebKitWebHistoryItem
*item
;
2102 /* rely on webkit to make sure we can go forward when on an about page */
2104 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:") ||
2105 g_str_has_prefix(uri
, "xxxt://"))
2106 return (webkit_web_view_can_go_forward(t
->wv
));
2108 /* the back/forwars list is stupid so help selecting a different item */
2109 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2111 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2112 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
2120 go_back_for_real(struct tab
*t
)
2123 WebKitWebHistoryItem
*item
;
2130 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:") ||
2131 g_str_has_prefix(uri
, "xxxt://")) {
2132 webkit_web_view_go_back(t
->wv
);
2135 /* the back/forwars list is stupid so help selecting a different item */
2136 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2138 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2139 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2140 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2147 go_forward_for_real(struct tab
*t
)
2150 WebKitWebHistoryItem
*item
;
2157 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:") ||
2158 g_str_has_prefix(uri
, "xxxt://")) {
2159 webkit_web_view_go_forward(t
->wv
);
2162 /* the back/forwars list is stupid so help selecting a different item */
2163 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2165 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2166 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2167 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2174 navaction(struct tab
*t
, struct karg
*args
)
2176 WebKitWebHistoryItem
*item
= NULL
;
2177 WebKitWebFrame
*frame
;
2179 DNPRINTF(XT_D_NAV
, "navaction: tab %d opcode %d\n",
2180 t
->tab_id
, args
->i
);
2183 set_normal_tab_meaning(t
);
2185 if (args
->i
== XT_NAV_BACK
)
2186 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2188 item
= webkit_web_back_forward_list_get_forward_item(t
->bfl
);
2195 return (XT_CB_PASSTHROUGH
);
2196 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2198 return (XT_CB_PASSTHROUGH
);
2201 go_back_for_real(t
);
2203 case XT_NAV_FORWARD
:
2206 return (XT_CB_PASSTHROUGH
);
2207 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2209 return (XT_CB_PASSTHROUGH
);
2212 go_forward_for_real(t
);
2215 frame
= webkit_web_view_get_main_frame(t
->wv
);
2216 webkit_web_frame_reload(frame
);
2219 frame
= webkit_web_view_get_main_frame(t
->wv
);
2220 webkit_web_frame_stop_loading(frame
);
2223 return (XT_CB_PASSTHROUGH
);
2227 move(struct tab
*t
, struct karg
*args
)
2229 GtkAdjustment
*adjust
;
2230 double pi
, si
, pos
, ps
, upper
, lower
, max
;
2236 case XT_MOVE_BOTTOM
:
2238 case XT_MOVE_PAGEDOWN
:
2239 case XT_MOVE_PAGEUP
:
2240 case XT_MOVE_HALFDOWN
:
2241 case XT_MOVE_HALFUP
:
2242 case XT_MOVE_PERCENT
:
2243 case XT_MOVE_CENTER
:
2244 adjust
= t
->adjust_v
;
2247 adjust
= t
->adjust_h
;
2251 pos
= gtk_adjustment_get_value(adjust
);
2252 ps
= gtk_adjustment_get_page_size(adjust
);
2253 upper
= gtk_adjustment_get_upper(adjust
);
2254 lower
= gtk_adjustment_get_lower(adjust
);
2255 si
= gtk_adjustment_get_step_increment(adjust
);
2256 pi
= gtk_adjustment_get_page_increment(adjust
);
2259 DNPRINTF(XT_D_MOVE
, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2260 "max %f si %f pi %f\n",
2261 args
->i
, adjust
== t
->adjust_h
? "horizontal" : "vertical",
2262 pos
, ps
, upper
, lower
, max
, si
, pi
);
2268 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2273 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2275 case XT_MOVE_BOTTOM
:
2276 case XT_MOVE_FARRIGHT
:
2277 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2278 gtk_adjustment_set_value(adjust
, max
);
2281 case XT_MOVE_FARLEFT
:
2282 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2283 gtk_adjustment_set_value(adjust
, lower
);
2285 case XT_MOVE_PAGEDOWN
:
2287 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2289 case XT_MOVE_PAGEUP
:
2291 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2293 case XT_MOVE_HALFDOWN
:
2295 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2297 case XT_MOVE_HALFUP
:
2299 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2301 case XT_MOVE_CENTER
:
2302 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2303 args
->s
= g_strdup("50.0");
2305 case XT_MOVE_PERCENT
:
2306 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2307 percent
= atoi(args
->s
) / 100.0;
2308 pos
= max
* percent
;
2309 if (pos
< 0.0 || pos
> max
)
2311 gtk_adjustment_set_value(adjust
, pos
);
2314 return (XT_CB_PASSTHROUGH
);
2317 DNPRINTF(XT_D_MOVE
, "move: new pos %f %f\n", pos
, MIN(pos
, max
));
2319 return (XT_CB_HANDLED
);
2323 url_set_visibility(void)
2327 TAILQ_FOREACH(t
, &tabs
, entry
)
2328 if (show_url
== 0) {
2329 gtk_widget_hide(t
->toolbar
);
2332 gtk_widget_show(t
->toolbar
);
2336 notebook_tab_set_visibility(void)
2338 if (show_tabs
== 0) {
2339 gtk_widget_hide(tab_bar
);
2340 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2342 if (tab_style
== XT_TABS_NORMAL
) {
2343 gtk_widget_hide(tab_bar
);
2344 gtk_notebook_set_show_tabs(notebook
, TRUE
);
2345 } else if (tab_style
== XT_TABS_COMPACT
) {
2346 gtk_widget_show(tab_bar
);
2347 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2353 statusbar_set_visibility(void)
2357 TAILQ_FOREACH(t
, &tabs
, entry
){
2358 if (show_statusbar
== 0)
2359 gtk_widget_hide(t
->statusbar
);
2361 gtk_widget_show(t
->statusbar
);
2368 url_set(struct tab
*t
, int enable_url_entry
)
2373 show_url
= enable_url_entry
;
2375 if (enable_url_entry
) {
2376 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.uri
),
2377 GTK_ENTRY_ICON_PRIMARY
, NULL
);
2378 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.uri
), 0);
2380 pixbuf
= gtk_entry_get_icon_pixbuf(GTK_ENTRY(t
->uri_entry
),
2381 GTK_ENTRY_ICON_PRIMARY
);
2383 gtk_entry_get_progress_fraction(GTK_ENTRY(t
->uri_entry
));
2384 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.uri
),
2385 GTK_ENTRY_ICON_PRIMARY
, pixbuf
);
2386 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.uri
),
2392 fullscreen(struct tab
*t
, struct karg
*args
)
2394 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2397 return (XT_CB_PASSTHROUGH
);
2399 if (show_url
== 0) {
2407 url_set_visibility();
2408 notebook_tab_set_visibility();
2410 return (XT_CB_HANDLED
);
2414 statustoggle(struct tab
*t
, struct karg
*args
)
2416 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2418 if (show_statusbar
== 1) {
2420 statusbar_set_visibility();
2421 } else if (show_statusbar
== 0) {
2423 statusbar_set_visibility();
2425 return (XT_CB_HANDLED
);
2429 urlaction(struct tab
*t
, struct karg
*args
)
2431 int rv
= XT_CB_HANDLED
;
2433 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2436 return (XT_CB_PASSTHROUGH
);
2440 if (show_url
== 0) {
2442 url_set_visibility();
2446 if (show_url
== 1) {
2448 url_set_visibility();
2456 tabaction(struct tab
*t
, struct karg
*args
)
2458 int rv
= XT_CB_HANDLED
;
2459 char *url
= args
->s
;
2461 struct tab
*tt
, *tv
;
2463 DNPRINTF(XT_D_TAB
, "tabaction: %p %d\n", t
, args
->i
);
2466 return (XT_CB_PASSTHROUGH
);
2470 if (strlen(url
) > 0)
2471 create_new_tab(url
, NULL
, 1, args
->precount
);
2473 create_new_tab(NULL
, NULL
, 1, args
->precount
);
2476 if (args
->precount
< 0)
2479 TAILQ_FOREACH(tt
, &tabs
, entry
)
2480 if (tt
->tab_id
== args
->precount
- 1) {
2485 case XT_TAB_DELQUIT
:
2486 if (gtk_notebook_get_n_pages(notebook
) > 1)
2492 TAILQ_FOREACH_SAFE(tt
, &tabs
, entry
, tv
)
2497 if (strlen(url
) > 0)
2500 rv
= XT_CB_PASSTHROUGH
;
2506 if (show_tabs
== 0) {
2508 notebook_tab_set_visibility();
2512 if (show_tabs
== 1) {
2514 notebook_tab_set_visibility();
2517 case XT_TAB_NEXTSTYLE
:
2518 if (tab_style
== XT_TABS_NORMAL
) {
2519 tab_style
= XT_TABS_COMPACT
;
2520 recolor_compact_tabs();
2523 tab_style
= XT_TABS_NORMAL
;
2524 notebook_tab_set_visibility();
2526 case XT_TAB_UNDO_CLOSE
:
2527 if (undo_count
== 0) {
2528 DNPRINTF(XT_D_TAB
, "%s: no tabs to undo close",
2533 u
= TAILQ_FIRST(&undos
);
2534 create_new_tab(u
->uri
, u
, 1, -1);
2536 TAILQ_REMOVE(&undos
, u
, entry
);
2538 /* u->history is freed in create_new_tab() */
2542 case XT_TAB_LOAD_IMAGES
:
2544 if (!auto_load_images
) {
2546 /* Enable auto-load images (this will load all
2547 * previously unloaded images). */
2548 g_object_set(G_OBJECT(t
->settings
),
2549 "auto-load-images", TRUE
, (char *)NULL
);
2550 webkit_web_view_set_settings(t
->wv
, t
->settings
);
2552 webkit_web_view_reload(t
->wv
);
2554 /* Webkit triggers an event when we change the setting,
2555 * so we can't disable the auto-loading at once.
2557 * Unfortunately, webkit does not tell us when it's done.
2558 * Instead, we wait until the next request, and then
2559 * disable autoloading again.
2561 t
->load_images
= TRUE
;
2565 rv
= XT_CB_PASSTHROUGH
;
2579 resizetab(struct tab
*t
, struct karg
*args
)
2581 if (t
== NULL
|| args
== NULL
) {
2582 show_oops(NULL
, "resizetab invalid parameters");
2583 return (XT_CB_PASSTHROUGH
);
2586 DNPRINTF(XT_D_TAB
, "resizetab: tab %d %d\n",
2587 t
->tab_id
, args
->i
);
2589 setzoom_webkit(t
, args
->i
);
2591 return (XT_CB_HANDLED
);
2595 movetab(struct tab
*t
, struct karg
*args
)
2599 if (t
== NULL
|| args
== NULL
) {
2600 show_oops(NULL
, "movetab invalid parameters");
2601 return (XT_CB_PASSTHROUGH
);
2604 DNPRINTF(XT_D_TAB
, "movetab: tab %d opcode %d\n",
2605 t
->tab_id
, args
->i
);
2607 if (args
->i
>= XT_TAB_INVALID
)
2608 return (XT_CB_PASSTHROUGH
);
2610 if (TAILQ_EMPTY(&tabs
))
2611 return (XT_CB_PASSTHROUGH
);
2613 n
= gtk_notebook_get_n_pages(notebook
);
2614 dest
= gtk_notebook_get_current_page(notebook
);
2618 if (args
->precount
< 0)
2619 dest
= dest
== n
- 1 ? 0 : dest
+ 1;
2621 dest
= args
->precount
- 1;
2625 if (args
->precount
< 0)
2628 dest
-= args
->precount
% n
;
2641 return (XT_CB_PASSTHROUGH
);
2644 if (dest
< 0 || dest
>= n
)
2645 return (XT_CB_PASSTHROUGH
);
2646 if (t
->tab_id
== dest
) {
2647 DNPRINTF(XT_D_TAB
, "movetab: do nothing\n");
2648 return (XT_CB_HANDLED
);
2651 set_current_tab(dest
);
2653 return (XT_CB_HANDLED
);
2660 const char *(*f
)(struct tab
*);
2662 { "<uri>", get_uri
},
2666 command(struct tab
*t
, struct karg
*args
)
2668 struct karg a
= {0};
2669 int i
, cmd_setup
= 0;
2670 char *s
= NULL
, *sp
= NULL
, *sl
= NULL
;
2673 if (t
== NULL
|| args
== NULL
) {
2674 show_oops(NULL
, "command invalid parameters");
2675 return (XT_CB_PASSTHROUGH
);
2686 if (cmd_prefix
== 0) {
2687 if (args
->s
!= NULL
&& strlen(args
->s
) != 0) {
2688 sp
= g_strdup_printf(":%s", args
->s
);
2693 sp
= g_strdup_printf(":%d", cmd_prefix
);
2703 for (i
= 0; i
< LENGTH(subs
); ++i
) {
2704 sv
= g_strsplit(sl
, subs
[i
].s
, -1);
2707 sl
= g_strjoinv(subs
[i
].f(t
), sv
);
2713 t
->mode
= XT_MODE_HINT
;
2717 * js code will auto fire() if a single link is visible,
2718 * causing the focus-out-event cb function to be called. Setup
2719 * the cmd _before_ triggering hinting code so the cmd can get
2720 * killed by the cb in this case.
2727 t
->mode
= XT_MODE_HINT
;
2728 a
.i
= XT_HINT_NEWTAB
;
2735 show_oops(t
, "command: invalid opcode %d", args
->i
);
2736 return (XT_CB_PASSTHROUGH
);
2739 DNPRINTF(XT_D_CMD
, "%s: tab %d type %s\n", __func__
, t
->tab_id
, s
);
2749 return (XT_CB_HANDLED
);
2753 search(struct tab
*t
, struct karg
*args
)
2757 if (t
== NULL
|| args
== NULL
) {
2758 show_oops(NULL
, "search invalid parameters");
2763 case XT_SEARCH_NEXT
:
2764 d
= t
->search_forward
;
2766 case XT_SEARCH_PREV
:
2767 d
= !t
->search_forward
;
2770 return (XT_CB_PASSTHROUGH
);
2773 if (t
->search_text
== NULL
) {
2774 if (global_search
== NULL
)
2775 return (XT_CB_PASSTHROUGH
);
2777 d
= t
->search_forward
= TRUE
;
2778 t
->search_text
= g_strdup(global_search
);
2779 webkit_web_view_mark_text_matches(t
->wv
, global_search
, FALSE
, 0);
2780 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
2784 DNPRINTF(XT_D_CMD
, "search: tab %d opc %d forw %d text %s\n",
2785 t
->tab_id
, args
->i
, t
->search_forward
, t
->search_text
);
2787 webkit_web_view_search_text(t
->wv
, t
->search_text
, FALSE
, d
, TRUE
);
2789 return (XT_CB_HANDLED
);
2793 session_save(struct tab
*t
, char *filename
)
2799 if (strlen(filename
) == 0)
2802 if (filename
[0] == '.' || filename
[0] == '/')
2806 if (save_tabs(t
, &a
))
2808 strlcpy(named_session
, filename
, sizeof named_session
);
2810 /* add the new session to the list of sessions */
2811 s
= g_malloc(sizeof(struct session
));
2812 s
->name
= g_strdup(filename
);
2813 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
2821 session_open(struct tab
*t
, char *filename
)
2826 if (strlen(filename
) == 0)
2829 if (filename
[0] == '.' || filename
[0] == '/')
2833 a
.i
= XT_SES_CLOSETABS
;
2834 if (open_tabs(t
, &a
))
2837 strlcpy(named_session
, filename
, sizeof named_session
);
2845 session_delete(struct tab
*t
, char *filename
)
2847 char file
[PATH_MAX
];
2851 if (strlen(filename
) == 0)
2854 if (filename
[0] == '.' || filename
[0] == '/')
2857 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, filename
);
2861 if (!strcmp(filename
, named_session
))
2862 strlcpy(named_session
, XT_SAVED_TABS_FILE
,
2863 sizeof named_session
);
2865 /* remove session from sessions list */
2866 TAILQ_FOREACH(s
, &sessions
, entry
) {
2867 if (!strcmp(s
->name
, filename
))
2872 TAILQ_REMOVE(&sessions
, s
, entry
);
2873 g_free((gpointer
) s
->name
);
2882 session_cmd(struct tab
*t
, struct karg
*args
)
2884 char *filename
= args
->s
;
2889 if (args
->i
& XT_SHOW
)
2890 show_oops(t
, "Current session: %s", named_session
[0] == '\0' ?
2891 XT_SAVED_TABS_FILE
: named_session
);
2892 else if (args
->i
& XT_SAVE
) {
2893 if (session_save(t
, filename
)) {
2894 show_oops(t
, "Can't save session: %s",
2895 filename
? filename
: "INVALID");
2898 } else if (args
->i
& XT_OPEN
) {
2899 if (session_open(t
, filename
)) {
2900 show_oops(t
, "Can't open session: %s",
2901 filename
? filename
: "INVALID");
2904 } else if (args
->i
& XT_DELETE
) {
2905 if (session_delete(t
, filename
)) {
2906 show_oops(t
, "Can't delete session: %s",
2907 filename
? filename
: "INVALID");
2912 return (XT_CB_PASSTHROUGH
);
2916 script_cmd(struct tab
*t
, struct karg
*args
)
2925 if ((f
= fopen(args
->s
, "r")) == NULL
) {
2926 show_oops(t
, "Can't open script file: %s", args
->s
);
2930 if (fstat(fileno(f
), &sb
) == -1) {
2931 show_oops(t
, "Can't stat script file: %s", args
->s
);
2935 buf
= g_malloc0(sb
.st_size
+ 1);
2936 if (fread(buf
, 1, sb
.st_size
, f
) != sb
.st_size
) {
2937 show_oops(t
, "Can't read script file: %s", args
->s
);
2941 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
2950 return (XT_CB_PASSTHROUGH
);
2954 * Make a hardcopy of the page
2957 print_page(struct tab
*t
, struct karg
*args
)
2959 WebKitWebFrame
*frame
;
2961 GtkPrintOperation
*op
;
2962 GtkPrintOperationAction action
;
2963 GtkPrintOperationResult print_res
;
2964 GError
*g_err
= NULL
;
2965 int marg_l
, marg_r
, marg_t
, marg_b
;
2967 DNPRINTF(XT_D_PRINTING
, "%s:", __func__
);
2969 ps
= gtk_page_setup_new();
2970 op
= gtk_print_operation_new();
2971 action
= GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
;
2972 frame
= webkit_web_view_get_main_frame(t
->wv
);
2974 /* the default margins are too small, so we will bump them */
2975 marg_l
= gtk_page_setup_get_left_margin(ps
, GTK_UNIT_MM
) +
2976 XT_PRINT_EXTRA_MARGIN
;
2977 marg_r
= gtk_page_setup_get_right_margin(ps
, GTK_UNIT_MM
) +
2978 XT_PRINT_EXTRA_MARGIN
;
2979 marg_t
= gtk_page_setup_get_top_margin(ps
, GTK_UNIT_MM
) +
2980 XT_PRINT_EXTRA_MARGIN
;
2981 marg_b
= gtk_page_setup_get_bottom_margin(ps
, GTK_UNIT_MM
) +
2982 XT_PRINT_EXTRA_MARGIN
;
2985 gtk_page_setup_set_left_margin(ps
, marg_l
, GTK_UNIT_MM
);
2986 gtk_page_setup_set_right_margin(ps
, marg_r
, GTK_UNIT_MM
);
2987 gtk_page_setup_set_top_margin(ps
, marg_t
, GTK_UNIT_MM
);
2988 gtk_page_setup_set_bottom_margin(ps
, marg_b
, GTK_UNIT_MM
);
2990 gtk_print_operation_set_default_page_setup(op
, ps
);
2992 /* this appears to free 'op' and 'ps' */
2993 print_res
= webkit_web_frame_print_full(frame
, op
, action
, &g_err
);
2995 /* check it worked */
2996 if (print_res
== GTK_PRINT_OPERATION_RESULT_ERROR
) {
2997 show_oops(NULL
, "can't print: %s", g_err
->message
);
2998 g_error_free (g_err
);
3006 go_home(struct tab
*t
, struct karg
*args
)
3013 set_encoding(struct tab
*t
, struct karg
*args
)
3017 if (args
->s
&& strlen(g_strstrip(args
->s
)) == 0) {
3018 e
= webkit_web_view_get_custom_encoding(t
->wv
);
3020 e
= webkit_web_view_get_encoding(t
->wv
);
3021 show_oops(t
, "encoding: %s", e
? e
: "N/A");
3023 webkit_web_view_set_custom_encoding(t
->wv
, args
->s
);
3029 restart(struct tab
*t
, struct karg
*args
)
3033 a
.s
= XT_RESTART_TABS_FILE
;
3035 execvp(start_argv
[0], start_argv
);
3041 char *http_proxy_save
; /* not a setting, used to toggle */
3044 proxy_cmd(struct tab
*t
, struct karg
*args
)
3048 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
3055 TAILQ_FOREACH(tt
, &tabs
, entry
)
3056 gtk_widget_show(t
->proxy_toggle
);
3057 if (http_proxy_save
)
3058 g_free(http_proxy_save
);
3059 http_proxy_save
= g_strdup(http_proxy
);
3062 if (args
->i
& XT_PRXY_SHOW
) {
3064 show_oops(t
, "http_proxy = %s", http_proxy
);
3066 show_oops(t
, "proxy is currently disabled");
3067 } else if (args
->i
& XT_PRXY_TOGGLE
) {
3068 if (http_proxy_save
== NULL
&& http_proxy
== NULL
) {
3069 show_oops(t
, "can't toggle proxy");
3072 TAILQ_FOREACH(tt
, &tabs
, entry
)
3073 gtk_widget_show(t
->proxy_toggle
);
3076 button_set_file(t
->proxy_toggle
, "tordisabled.ico");
3077 show_oops(t
, "http proxy disabled");
3079 setup_proxy(http_proxy_save
);
3080 button_set_file(t
->proxy_toggle
, "torenabled.ico");
3081 show_oops(t
, "http_proxy = %s", http_proxy
);
3085 return (XT_CB_PASSTHROUGH
);
3089 * If you can read this functionthen you are a sick and twisted individual.
3090 * I hope we never meet, it'll be violent.
3093 eval_cb(const GMatchInfo
*info
, GString
*res
, gpointer data
)
3096 gint start
= -1, end
= -1, i
;
3097 struct karg
*args
= data
;
3100 * match contains the string UP TO the match.
3102 * res is what is returned, note that whatever remains in the sent in
3103 * string is appended on the way out.
3105 * for example /123/456/789/moo came in
3106 * match contains /123/456/789/
3107 * we assign that to res and replace /789/ with the replacement text
3108 * then g_regex_replace_eval on the way out has /123/456/replacement/moo
3111 match
= g_match_info_fetch(info
, 0);
3115 if (g_match_info_fetch_pos(info
, 1, &start
, &end
) == FALSE
)
3118 g_string_assign(res
, match
);
3120 i
= atoi(&match
[start
+ 1]);
3121 if (args
->i
== XT_URL_PLUS
)
3126 /* preserve whitespace when likely */
3127 num
= g_strdup_printf("%0*d", end
- start
- 2, i
);
3128 g_string_overwrite_len(res
, start
+ 1, num
, end
- start
- 2);
3134 return (FALSE
); /* doesn't matter */
3138 urlmod_cmd(struct tab
*t
, struct karg
*args
)
3146 if ((uri
= gtk_entry_get_text(GTK_ENTRY(t
->uri_entry
))) == NULL
)
3148 if (strlen(uri
) == 0)
3151 reg
= g_regex_new(".*(/[0-9]+/)", 0, 0, NULL
);
3154 res
= g_regex_replace_eval(reg
, uri
, -1, 0, 0, eval_cb
, args
, NULL
);
3158 if (!strcmp(res
, uri
))
3161 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), res
);
3162 activate_uri_entry_cb(t
->uri_entry
, t
);
3169 return (XT_CB_PASSTHROUGH
);
3175 int (*func
)(struct tab
*, struct karg
*);
3179 { "command_mode", 0, command_mode
, XT_MODE_COMMAND
, 0 },
3180 { "insert_mode", 0, command_mode
, XT_MODE_INSERT
, 0 },
3181 { "command", 0, command
, ':', 0 },
3182 { "search", 0, command
, '/', 0 },
3183 { "searchb", 0, command
, '?', 0 },
3184 { "hinting", 0, command
, '.', 0 },
3185 { "hinting_newtab", 0, command
, ',', 0 },
3186 { "togglesrc", 0, toggle_src
, 0, 0 },
3187 { "editsrc", 0, edit_src
, 0, 0 },
3188 { "editelement", 0, edit_element
, 0, 0 },
3189 { "passthrough", 0, passthrough
, 0, 0 },
3190 { "modurl", 0, modurl
, 0, 0 },
3192 /* yanking and pasting */
3193 { "yankuri", 0, yank_uri
, 0, 0 },
3194 { "pasteuricur", 0, paste_uri
, XT_PASTE_CURRENT_TAB
, 0 },
3195 { "pasteurinew", 0, paste_uri
, XT_PASTE_NEW_TAB
, 0 },
3198 { "searchnext", 0, search
, XT_SEARCH_NEXT
, 0 },
3199 { "searchprevious", 0, search
, XT_SEARCH_PREV
, 0 },
3202 { "focusaddress", 0, focus
, XT_FOCUS_URI
, 0 },
3203 { "focussearch", 0, focus
, XT_FOCUS_SEARCH
, 0 },
3206 { "hinting", 0, hint
, 0, 0 },
3207 { "hinting_newtab", 0, hint
, XT_HINT_NEWTAB
, 0 },
3209 /* custom stylesheet */
3210 { "userstyle", 0, userstyle_cmd
, XT_STYLE_CURRENT_TAB
, XT_USERARG
},
3211 { "userstyle_global", 0, userstyle_cmd
, XT_STYLE_GLOBAL
, XT_USERARG
},
3214 { "goback", 0, navaction
, XT_NAV_BACK
, 0 },
3215 { "goforward", 0, navaction
, XT_NAV_FORWARD
, 0 },
3216 { "reload", 0, navaction
, XT_NAV_RELOAD
, 0 },
3217 { "stop", 0, navaction
, XT_NAV_STOP
, 0 },
3219 /* vertical movement */
3220 { "scrolldown", 0, move
, XT_MOVE_DOWN
, 0 },
3221 { "scrollup", 0, move
, XT_MOVE_UP
, 0 },
3222 { "scrollbottom", 0, move
, XT_MOVE_BOTTOM
, 0 },
3223 { "scrolltop", 0, move
, XT_MOVE_TOP
, 0 },
3224 { "1", 0, move
, XT_MOVE_TOP
, 0 },
3225 { "scrollhalfdown", 0, move
, XT_MOVE_HALFDOWN
, 0 },
3226 { "scrollhalfup", 0, move
, XT_MOVE_HALFUP
, 0 },
3227 { "scrollpagedown", 0, move
, XT_MOVE_PAGEDOWN
, 0 },
3228 { "scrollpageup", 0, move
, XT_MOVE_PAGEUP
, 0 },
3229 /* horizontal movement */
3230 { "scrollright", 0, move
, XT_MOVE_RIGHT
, 0 },
3231 { "scrollleft", 0, move
, XT_MOVE_LEFT
, 0 },
3232 { "scrollfarright", 0, move
, XT_MOVE_FARRIGHT
, 0 },
3233 { "scrollfarleft", 0, move
, XT_MOVE_FARLEFT
, 0 },
3235 { "favorites", 0, xtp_page_fl
, XT_SHOW
, 0 },
3236 { "fav", 0, xtp_page_fl
, XT_SHOW
, 0 },
3237 { "favedit", 0, xtp_page_fl
, XT_SHOW
|XT_DELETE
, 0 },
3238 { "favadd", 0, add_favorite
, 0, 0 },
3240 { "qall", 0, quit
, 0, 0 },
3241 { "quitall", 0, quit
, 0, 0 },
3242 { "w", 0, save_tabs
, 0, 0 },
3243 { "wq", 0, save_tabs_and_quit
, 0, 0 },
3244 { "help", 0, help
, 0, 0 },
3245 { "about", 0, xtp_page_ab
, 0, 0 },
3246 { "stats", 0, stats
, 0, 0 },
3247 { "version", 0, xtp_page_ab
, 0, 0 },
3250 { "js", 0, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3251 { "save", 1, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3252 { "domain", 2, js_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3253 { "fqdn", 2, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3254 { "show", 1, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3255 { "all", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3256 { "persistent", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3257 { "session", 2, js_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3258 { "toggle", 1, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3259 { "domain", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3260 { "fqdn", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3262 /* cookie command */
3263 { "cookie", 0, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3264 { "save", 1, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3265 { "domain", 2, cookie_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3266 { "fqdn", 2, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3267 { "show", 1, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3268 { "all", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3269 { "persistent", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3270 { "session", 2, cookie_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3271 { "toggle", 1, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3272 { "domain", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3273 { "fqdn", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3274 { "purge", 1, cookie_cmd
, XT_DELETE
, 0 },
3276 /* plugin command */
3277 { "plugin", 0, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3278 { "save", 1, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3279 { "domain", 2, pl_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3280 { "fqdn", 2, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3281 { "show", 1, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3282 { "all", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3283 { "persistent", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3284 { "session", 2, pl_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3285 { "toggle", 1, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3286 { "domain", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3287 { "fqdn", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3290 { "https", 0, https_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3291 { "save", 1, https_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3292 { "domain", 2, https_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3293 { "fqdn", 2, https_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3294 { "show", 1, https_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3295 { "all", 2, https_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3296 { "persistent", 2, https_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3297 { "session", 2, https_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3298 { "toggle", 1, https_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3299 { "domain", 2, https_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3300 { "fqdn", 2, https_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3302 /* toplevel (domain) command */
3303 { "toplevel", 0, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3304 { "toggle", 1, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3307 { "cookiejar", 0, xtp_page_cl
, 0, 0 },
3310 { "cert", 0, cert_cmd
, XT_SHOW
, 0 },
3311 { "save", 1, cert_cmd
, XT_SAVE
, 0 },
3312 { "show", 1, cert_cmd
, XT_SHOW
, 0 },
3314 { "ca", 0, ca_cmd
, 0, 0 },
3315 { "downloadmgr", 0, xtp_page_dl
, 0, 0 },
3316 { "dl", 0, xtp_page_dl
, 0, 0 },
3317 { "h", 0, xtp_page_hl
, 0, 0 },
3318 { "history", 0, xtp_page_hl
, 0, 0 },
3319 { "home", 0, go_home
, 0, 0 },
3320 { "restart", 0, restart
, 0, 0 },
3321 { "urlhide", 0, urlaction
, XT_URL_HIDE
, 0 },
3322 { "urlshow", 0, urlaction
, XT_URL_SHOW
, 0 },
3323 { "statustoggle", 0, statustoggle
, 0, 0 },
3324 { "run_script", 0, run_page_script
, 0, XT_USERARG
},
3326 { "print", 0, print_page
, 0, 0 },
3329 { "focusin", 0, resizetab
, XT_ZOOM_IN
, 0 },
3330 { "focusout", 0, resizetab
, XT_ZOOM_OUT
, 0 },
3331 { "focusreset", 0, resizetab
, XT_ZOOM_NORMAL
, 0 },
3332 { "q", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3333 { "quit", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3334 { "open", 0, tabaction
, XT_TAB_OPEN
, XT_URLARG
},
3335 { "tabclose", 0, tabaction
, XT_TAB_DELETE
, XT_PREFIX
| XT_INTARG
},
3336 { "tabedit", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3337 { "tabfirst", 0, movetab
, XT_TAB_FIRST
, 0 },
3338 { "tabhide", 0, tabaction
, XT_TAB_HIDE
, 0 },
3339 { "tablast", 0, movetab
, XT_TAB_LAST
, 0 },
3340 { "tabnew", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3341 { "tabnext", 0, movetab
, XT_TAB_NEXT
, XT_PREFIX
| XT_INTARG
},
3342 { "tabnextstyle", 0, tabaction
, XT_TAB_NEXTSTYLE
, 0 },
3343 { "tabonly", 0, tabaction
, XT_TAB_ONLY
, 0 },
3344 { "tabprevious", 0, movetab
, XT_TAB_PREV
, XT_PREFIX
| XT_INTARG
},
3345 { "tabrewind", 0, movetab
, XT_TAB_FIRST
, 0 },
3346 { "tabshow", 0, tabaction
, XT_TAB_SHOW
, 0 },
3347 { "tabs", 0, buffers
, 0, 0 },
3348 { "tabundoclose", 0, tabaction
, XT_TAB_UNDO_CLOSE
, 0 },
3349 { "buffers", 0, buffers
, 0, 0 },
3350 { "ls", 0, buffers
, 0, 0 },
3351 { "encoding", 0, set_encoding
, 0, XT_USERARG
},
3352 { "loadimages", 0, tabaction
, XT_TAB_LOAD_IMAGES
, 0 },
3355 { "set", 0, set
, 0, XT_SETARG
},
3356 { "runtime", 0, xtp_page_rt
, 0, 0 },
3358 { "fullscreen", 0, fullscreen
, 0, 0 },
3359 { "f", 0, fullscreen
, 0, 0 },
3362 { "session", 0, session_cmd
, XT_SHOW
, 0 },
3363 { "delete", 1, session_cmd
, XT_DELETE
, XT_SESSARG
},
3364 { "open", 1, session_cmd
, XT_OPEN
, XT_SESSARG
},
3365 { "save", 1, session_cmd
, XT_SAVE
, XT_USERARG
},
3366 { "show", 1, session_cmd
, XT_SHOW
, 0 },
3368 /* external javascript */
3369 { "script", 0, script_cmd
, XT_EJS_SHOW
, XT_USERARG
},
3372 { "inspector", 0, inspector_cmd
, XT_INS_SHOW
, 0 },
3373 { "show", 1, inspector_cmd
, XT_INS_SHOW
, 0 },
3374 { "hide", 1, inspector_cmd
, XT_INS_HIDE
, 0 },
3377 { "proxy", 0, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3378 { "show", 1, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3379 { "toggle", 1, proxy_cmd
, XT_PRXY_TOGGLE
, 0 },
3382 { "urlmod", 0, urlmod_cmd
, XT_URL
, 0 },
3383 { "plus", 1, urlmod_cmd
, XT_URL_PLUS
, 0 },
3384 { "min", 1, urlmod_cmd
, XT_URL_MIN
, 0 },
3391 } cmd_status
= {-1, 0};
3394 wv_release_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3397 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 1)
3404 wv_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3407 WebKitHitTestResult
*hit_test_result
;
3410 hit_test_result
= webkit_web_view_get_hit_test_result(t
->wv
, e
);
3411 g_object_get(hit_test_result
, "context", &context
, NULL
);
3416 if (context
& WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE
)
3417 t
->mode
= XT_MODE_INSERT
;
3419 t
->mode
= XT_MODE_COMMAND
;
3421 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3423 else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 8 /* btn 4 */) {
3429 } else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 9 /* btn 5 */) {
3431 a
.i
= XT_NAV_FORWARD
;
3441 tab_close_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3443 DNPRINTF(XT_D_TAB
, "tab_close_cb: tab %d\n", t
->tab_id
);
3445 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3452 parse_custom_uri(struct tab
*t
, const char *uri
)
3454 struct custom_uri
*u
;
3458 TAILQ_FOREACH(u
, &cul
, entry
) {
3459 if (strncmp(uri
, u
->uri
, strlen(u
->uri
)))
3464 sv
[1] = (char *)uri
;
3466 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
,
3468 show_oops(t
, "%s: could not spawn process", __func__
);
3475 activate_uri_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3477 const gchar
*uri
= gtk_entry_get_text(GTK_ENTRY(entry
));
3479 DNPRINTF(XT_D_URL
, "activate_uri_entry_cb: %s\n", uri
);
3482 show_oops(NULL
, "activate_uri_entry_cb invalid parameters");
3487 show_oops(t
, "activate_uri_entry_cb no uri");
3491 uri
+= strspn(uri
, "\t ");
3493 if (parse_custom_uri(t
, uri
))
3496 /* otherwise continue to load page normally */
3497 load_uri(t
, (gchar
*)uri
);
3502 activate_search_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3504 const gchar
*search
= gtk_entry_get_text(GTK_ENTRY(entry
));
3505 char *newuri
= NULL
;
3509 DNPRINTF(XT_D_URL
, "activate_search_entry_cb: %s\n", search
);
3512 show_oops(NULL
, "activate_search_entry_cb invalid parameters");
3516 if (search_string
== NULL
|| strlen(search_string
) == 0) {
3517 show_oops(t
, "no search_string");
3521 set_normal_tab_meaning(t
);
3523 enc_search
= soup_uri_encode(search
, XT_RESERVED_CHARS
);
3524 sv
= g_strsplit(search_string
, "%s", 2);
3525 newuri
= g_strjoinv(enc_search
, sv
);
3530 load_uri(t
, newuri
);
3538 check_and_set_cookie(const gchar
*uri
, struct tab
*t
)
3540 struct wl_entry
*w
= NULL
;
3543 if (uri
== NULL
|| t
== NULL
)
3546 if ((w
= wl_find_uri(uri
, &c_wl
)) == NULL
)
3551 DNPRINTF(XT_D_COOKIE
, "check_and_set_cookie: %s %s\n",
3552 es
? "enable" : "disable", uri
);
3554 g_object_set(G_OBJECT(t
->settings
),
3555 "enable-html5-local-storage", es
, (char *)NULL
);
3556 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3560 check_and_set_js(const gchar
*uri
, struct tab
*t
)
3562 struct wl_entry
*w
= NULL
;
3565 if (uri
== NULL
|| t
== NULL
)
3568 if ((w
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3573 DNPRINTF(XT_D_JS
, "check_and_set_js: %s %s\n",
3574 es
? "enable" : "disable", uri
);
3576 g_object_set(G_OBJECT(t
->settings
),
3577 "enable-scripts", es
, (char *)NULL
);
3578 g_object_set(G_OBJECT(t
->settings
),
3579 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
3580 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3582 button_set_stockid(t
->js_toggle
,
3583 es
? GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
);
3587 check_and_set_pl(const gchar
*uri
, struct tab
*t
)
3589 struct wl_entry
*w
= NULL
;
3592 if (uri
== NULL
|| t
== NULL
)
3595 if ((w
= wl_find_uri(uri
, &pl_wl
)) == NULL
)
3600 DNPRINTF(XT_D_JS
, "check_and_set_pl: %s %s\n",
3601 es
? "enable" : "disable", uri
);
3603 g_object_set(G_OBJECT(t
->settings
),
3604 "enable-plugins", es
, (char *)NULL
);
3605 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3608 #if GTK_CHECK_VERSION(3, 0, 0)
3609 /* A lot of this can be removed when gtk2 is dropped on the floor */
3611 get_css_name(const char *col_str
)
3615 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3616 name
= g_strdup(XT_CSS_NORMAL
);
3617 else if (!strcmp(col_str
, XT_COLOR_RED
))
3618 name
= g_strdup(XT_CSS_RED
);
3619 else if (!strcmp(col_str
, XT_COLOR_YELLOW
))
3620 name
= g_strdup(XT_CSS_YELLOW
);
3621 else if (!strcmp(col_str
, XT_COLOR_GREEN
))
3622 name
= g_strdup(XT_CSS_GREEN
);
3623 else if (!strcmp(col_str
, XT_COLOR_BLUE
))
3624 name
= g_strdup(XT_CSS_BLUE
);
3630 show_ca_status(struct tab
*t
, const char *uri
)
3632 char domain
[8182], file
[PATH_MAX
];
3633 SoupMessage
*msg
= NULL
;
3634 GTlsCertificate
*cert
= NULL
;
3635 GTlsCertificateFlags flags
= 0;
3636 gchar
*col_str
= XT_COLOR_RED
;
3638 #if GTK_CHECK_VERSION(3, 0, 0)
3644 enum cert_trust trust
;
3648 DNPRINTF(XT_D_URL
, "show_ca_status: %d %s %s\n",
3649 ssl_strict_certs
, ssl_ca_file
, uri
);
3654 if (uri
== NULL
|| g_str_has_prefix(uri
, "http://") ||
3655 !g_str_has_prefix(uri
, "https://"))
3658 msg
= soup_message_new("GET", uri
);
3661 soup_session_send_message(session
, msg
);
3662 if (msg
->status_code
== SOUP_STATUS_SSL_FAILED
||
3663 msg
->status_code
== SOUP_STATUS_TLS_FAILED
) {
3664 DNPRINTF(XT_D_URL
, "%s: status not ok: %d\n", uri
,
3668 if (!soup_message_get_https_status(msg
, &cert
, &flags
)) {
3669 DNPRINTF(XT_D_URL
, "%s: invalid response\n", uri
);
3672 if (!G_IS_TLS_CERTIFICATE(cert
)) {
3673 DNPRINTF(XT_D_URL
, "%s: no cert\n", uri
);
3678 col_str
= XT_COLOR_GREEN
;
3680 col_str
= XT_COLOR_YELLOW
;
3682 strlcpy(domain
, uri
+ strlen("https://"), sizeof domain
);
3683 for (i
= 0; i
< strlen(domain
); i
++)
3684 if (domain
[i
] == '/') {
3689 snprintf(file
, sizeof file
, "%s" PS
"%s", certs_cache_dir
, domain
);
3690 if (warn_cert_changes
) {
3691 if (check_cert_changes(t
, cert
, file
, uri
))
3695 snprintf(file
, sizeof file
, "%s" PS
"%s", certs_dir
, domain
);
3696 chain
= g_strdup("");
3697 if ((trust
= check_local_certs(file
, cert
, &chain
)) == CERT_LOCAL
)
3698 col_str
= XT_COLOR_BLUE
;
3700 g_free(t
->cert_chain
);
3701 t
->cert_chain
= chain
;
3704 g_object_unref(msg
);
3705 if (!strcmp(col_str
, XT_COLOR_WHITE
) || nocolor
) {
3706 #if GTK_CHECK_VERSION(3, 0, 0)
3707 gtk_widget_set_name(t
->uri_entry
, XT_CSS_NORMAL
);
3708 statusbar_modify_attr(t
, XT_CSS_NORMAL
);
3710 text
= gdk_color_to_string(
3711 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3712 base
= gdk_color_to_string(
3713 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3714 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3715 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3716 statusbar_modify_attr(t
, text
, base
);
3721 #if GTK_CHECK_VERSION(3, 0, 0)
3722 name
= get_css_name(col_str
);
3723 gtk_widget_set_name(t
->uri_entry
, name
);
3724 statusbar_modify_attr(t
, name
);
3727 gdk_color_parse(col_str
, &color
);
3728 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3729 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3735 free_favicon(struct tab
*t
)
3737 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p req %p\n",
3738 __func__
, t
->icon_download
, t
->icon_request
);
3740 if (t
->icon_request
)
3741 g_object_unref(t
->icon_request
);
3742 if (t
->icon_dest_uri
)
3743 g_free(t
->icon_dest_uri
);
3745 t
->icon_request
= NULL
;
3746 t
->icon_dest_uri
= NULL
;
3750 xt_icon_from_name(struct tab
*t
, gchar
*name
)
3752 if (!enable_favicon_entry
)
3755 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->uri_entry
),
3756 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3758 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.uri
),
3759 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3761 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.uri
),
3762 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3766 xt_icon_from_pixbuf(struct tab
*t
, GdkPixbuf
*pb
)
3768 GdkPixbuf
*pb_scaled
;
3770 if (gdk_pixbuf_get_width(pb
) > 16 || gdk_pixbuf_get_height(pb
) > 16)
3771 pb_scaled
= gdk_pixbuf_scale_simple(pb
, 16, 16,
3772 GDK_INTERP_BILINEAR
);
3776 if (enable_favicon_entry
) {
3779 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
3780 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3783 if (show_url
== 0) {
3784 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.uri
),
3785 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3787 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.uri
),
3788 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3791 /* XXX: Only supports the minimal tabs atm. */
3792 if (enable_favicon_tabs
)
3793 gtk_image_set_from_pixbuf(GTK_IMAGE(t
->tab_elems
.favicon
),
3796 if (pb_scaled
!= pb
)
3797 g_object_unref(pb_scaled
);
3801 xt_icon_from_file(struct tab
*t
, char *uri
)
3806 if (g_str_has_prefix(uri
, "file://"))
3807 file
= g_filename_from_uri(uri
, NULL
, NULL
);
3809 file
= g_strdup(uri
);
3814 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
3816 xt_icon_from_pixbuf(t
, pb
);
3819 xt_icon_from_name(t
, "text-html");
3825 is_valid_icon(char *file
)
3828 const char *mime_type
;
3832 gf
= g_file_new_for_path(file
);
3833 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
3835 mime_type
= g_file_info_get_content_type(fi
);
3836 valid
= g_strcmp0(mime_type
, "image/x-ico") == 0 ||
3837 g_strcmp0(mime_type
, "image/vnd.microsoft.icon") == 0 ||
3838 g_strcmp0(mime_type
, "image/png") == 0 ||
3839 g_strcmp0(mime_type
, "image/gif") == 0 ||
3840 g_strcmp0(mime_type
, "application/octet-stream") == 0;
3848 set_favicon_from_file(struct tab
*t
, char *uri
)
3853 if (t
== NULL
|| uri
== NULL
)
3856 if (g_str_has_prefix(uri
, "file://"))
3857 file
= g_filename_from_uri(uri
, NULL
, NULL
);
3859 file
= g_strdup(uri
);
3864 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading %s\n", __func__
, file
);
3866 if (!stat(file
, &sb
)) {
3867 if (sb
.st_size
== 0 || !is_valid_icon(file
)) {
3868 /* corrupt icon so trash it */
3869 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3872 /* no need to set icon to default here */
3876 xt_icon_from_file(t
, file
);
3882 favicon_download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
3885 WebKitDownloadStatus status
= webkit_download_get_status(download
);
3886 struct tab
*tt
= NULL
, *t
= NULL
;
3889 * find the webview instead of passing in the tab as it could have been
3890 * deleted from underneath us.
3892 TAILQ_FOREACH(tt
, &tabs
, entry
) {
3901 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d status %d\n",
3902 __func__
, t
->tab_id
, status
);
3905 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
3907 t
->icon_download
= NULL
;
3910 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
3913 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
3916 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
3918 DNPRINTF(XT_D_DOWNLOAD
, "%s: freeing favicon %d\n",
3919 __func__
, t
->tab_id
);
3920 t
->icon_download
= NULL
;
3923 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
3926 DNPRINTF(XT_D_DOWNLOAD
, "%s: setting icon to %s\n",
3927 __func__
, t
->icon_dest_uri
);
3928 set_favicon_from_file(t
, t
->icon_dest_uri
);
3929 /* these will be freed post callback */
3930 t
->icon_request
= NULL
;
3931 t
->icon_download
= NULL
;
3939 abort_favicon_download(struct tab
*t
)
3941 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p\n", __func__
, t
->icon_download
);
3943 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
3944 if (t
->icon_download
) {
3945 g_signal_handlers_disconnect_by_func(G_OBJECT(t
->icon_download
),
3946 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3947 webkit_download_cancel(t
->icon_download
);
3948 t
->icon_download
= NULL
;
3953 xt_icon_from_name(t
, "text-html");
3957 notify_icon_loaded_cb(WebKitWebView
*wv
, gchar
*uri
, struct tab
*t
)
3959 DNPRINTF(XT_D_DOWNLOAD
, "%s %s\n", __func__
, uri
);
3961 if (uri
== NULL
|| t
== NULL
)
3964 #if WEBKIT_CHECK_VERSION(1, 4, 0)
3965 /* take icon from WebKitIconDatabase */
3966 GdkPixbuf
*pb
= NULL
;
3968 /* webkit_web_view_get_icon_pixbuf is depreciated in 1.8 */
3969 #if WEBKIT_CHECK_VERSION(1, 8, 0)
3971 * If the page was not loaded (for example, via ssl_strict_certs), do
3972 * not attempt to get the webview's pixbuf. This prevents a CRITICAL
3975 if (wv
&& webkit_web_view_get_uri(wv
))
3976 pb
= webkit_web_view_try_get_favicon_pixbuf(wv
, 0, 0);
3978 if (wv
&& webkit_web_view_get_uri(wv
))
3979 pb
= webkit_web_view_get_icon_pixbuf(wv
);
3982 xt_icon_from_pixbuf(t
, pb
);
3985 xt_icon_from_name(t
, "text-html");
3986 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
3987 /* download icon to cache dir */
3988 gchar
*name_hash
, file
[PATH_MAX
];
3991 if (t
->icon_request
) {
3992 DNPRINTF(XT_D_DOWNLOAD
, "%s: download in progress\n", __func__
);
3996 /* check to see if we got the icon in cache */
3997 name_hash
= g_compute_checksum_for_string(G_CHECKSUM_SHA256
, uri
, -1);
3998 snprintf(file
, sizeof file
, "%s" PS
"%s.ico", cache_dir
, name_hash
);
4001 if (!stat(file
, &sb
)) {
4002 if (sb
.st_size
> 0) {
4003 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading from cache %s\n",
4005 set_favicon_from_file(t
, file
);
4009 /* corrupt icon so trash it */
4010 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
4015 /* create download for icon */
4016 t
->icon_request
= webkit_network_request_new(uri
);
4017 if (t
->icon_request
== NULL
) {
4018 DNPRINTF(XT_D_DOWNLOAD
, "%s: invalid uri %s\n",
4023 t
->icon_download
= webkit_download_new(t
->icon_request
);
4024 if (t
->icon_download
== NULL
)
4027 /* we have to free icon_dest_uri later */
4028 if ((t
->icon_dest_uri
= g_filename_to_uri(file
, NULL
, NULL
)) == NULL
)
4030 webkit_download_set_destination_uri(t
->icon_download
,
4033 if (webkit_download_get_status(t
->icon_download
) ==
4034 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
4035 g_object_unref(t
->icon_request
);
4036 g_free(t
->icon_dest_uri
);
4037 t
->icon_request
= NULL
;
4038 t
->icon_dest_uri
= NULL
;
4042 g_signal_connect(G_OBJECT(t
->icon_download
), "notify::status",
4043 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
4045 webkit_download_start(t
->icon_download
);
4050 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
4052 const gchar
*uri
= NULL
;
4053 struct history
*h
, find
;
4055 gchar
*tmp_uri
= NULL
;
4056 #if !GTK_CHECK_VERSION(3, 0, 0)
4060 DNPRINTF(XT_D_URL
, "notify_load_status_cb: %d %s\n",
4061 webkit_web_view_get_load_status(wview
),
4062 get_uri(t
) ? get_uri(t
) : "NOTHING");
4065 show_oops(NULL
, "notify_load_status_cb invalid parameters");
4069 switch (webkit_web_view_get_load_status(wview
)) {
4070 case WEBKIT_LOAD_PROVISIONAL
:
4072 abort_favicon_download(t
);
4073 #if GTK_CHECK_VERSION(2, 20, 0)
4074 gtk_widget_show(t
->spinner
);
4075 gtk_spinner_start(GTK_SPINNER(t
->spinner
));
4077 t
->download_requested
= 0;
4079 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), TRUE
);
4081 /* assume we are a new address */
4082 #if GTK_CHECK_VERSION(3, 0, 0)
4083 gtk_widget_set_name(t
->uri_entry
, XT_CSS_NORMAL
);
4084 statusbar_modify_attr(t
, XT_CSS_NORMAL
);
4086 text
= gdk_color_to_string(
4087 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
4088 base
= gdk_color_to_string(
4089 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
4090 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
4091 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
4092 statusbar_modify_attr(t
, text
, base
);
4097 /* DOM is changing, unreference the previous focused element */
4098 #if WEBKIT_CHECK_VERSION(1, 5, 0)
4100 g_object_unref(t
->active
);
4102 if (t
->active_text
) {
4103 g_free(t
->active_text
);
4104 t
->active_text
= NULL
;
4108 /* take focus if we are visible */
4115 case WEBKIT_LOAD_COMMITTED
:
4120 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
4126 set_status(t
, "Loading: %s", (char *)uri
);
4128 /* clear t->item, except if we're switching to an about: page */
4129 if (t
->item
&& !g_str_has_prefix(uri
, "xxxt://") &&
4130 !g_str_has_prefix(uri
, "about:")) {
4131 g_object_unref(t
->item
);
4135 /* check if js white listing is enabled */
4136 if (enable_plugin_whitelist
)
4137 check_and_set_pl(uri
, t
);
4138 if (enable_cookie_whitelist
)
4139 check_and_set_cookie(uri
, t
);
4140 if (enable_js_whitelist
)
4141 check_and_set_js(uri
, t
);
4147 /* we know enough to autosave the session */
4148 if (session_autosave
) {
4153 show_ca_status(t
, uri
);
4154 run_script(t
, JS_HINTING
);
4155 if (enable_autoscroll
)
4156 run_script(t
, JS_AUTOSCROLL
);
4159 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
4161 if (color_visited_uris
) {
4162 color_visited(t
, color_visited_helper());
4165 * This colors the links you middle-click (open in new
4166 * tab) in the current tab.
4168 if (t
->tab_id
!= gtk_notebook_get_current_page(notebook
) &&
4169 (uri
= get_uri(t
)) != NULL
)
4170 color_visited(get_current_tab(),
4171 g_strdup_printf("{'%s' : 'dummy'}", uri
));
4175 case WEBKIT_LOAD_FINISHED
:
4177 if ((uri
= get_uri(t
)) == NULL
)
4180 * js_autorun calls get_uri which frees t->tmp_uri if on an
4181 * "about:" page. On "about:" pages, uri points to t->tmp_uri.
4182 * I.e. we will use freed memory. Prevent that.
4184 tmp_uri
= g_strdup(uri
);
4186 /* autorun some js if enabled */
4191 if (!strncmp(tmp_uri
, "http://", strlen("http://")) ||
4192 !strncmp(tmp_uri
, "https://", strlen("https://")) ||
4193 !strncmp(tmp_uri
, "file://", strlen("file://"))) {
4194 find
.uri
= (gchar
*)tmp_uri
;
4195 h
= RB_FIND(history_list
, &hl
, &find
);
4197 insert_history_item(tmp_uri
,
4198 get_title(t
, FALSE
), time(NULL
));
4200 h
->time
= time(NULL
);
4203 if (statusbar_style
== XT_STATUSBAR_URL
)
4204 set_status(t
, "%s", (char *)tmp_uri
);
4206 set_status(t
, "%s", (char *)get_title(t
, FALSE
));
4207 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
4208 #if GTK_CHECK_VERSION(2, 20, 0)
4209 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
4210 gtk_widget_hide(t
->spinner
);
4215 #if WEBKIT_CHECK_VERSION(1, 1, 18)
4216 case WEBKIT_LOAD_FAILED
:
4218 if (!t
->download_requested
) {
4219 gtk_label_set_text(GTK_LABEL(t
->label
),
4220 get_title(t
, FALSE
));
4221 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
),
4222 get_title(t
, FALSE
));
4223 set_status(t
, "%s", (char *)get_title(t
, FALSE
));
4224 gtk_window_set_title(GTK_WINDOW(main_window
),
4225 get_title(t
, TRUE
));
4231 #if GTK_CHECK_VERSION(2, 20, 0)
4232 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
4233 gtk_widget_hide(t
->spinner
);
4235 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
4239 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
),
4240 can_go_back_for_real(t
));
4242 gtk_widget_set_sensitive(GTK_WIDGET(t
->forward
),
4243 can_go_forward_for_real(t
));
4247 notify_title_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
4249 const gchar
*title
= NULL
, *win_title
= NULL
;
4251 title
= get_title(t
, FALSE
);
4252 win_title
= get_title(t
, TRUE
);
4254 gtk_label_set_text(GTK_LABEL(t
->label
), title
);
4255 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
), title
);
4258 if (win_title
&& t
->tab_id
== gtk_notebook_get_current_page(notebook
))
4259 gtk_window_set_title(GTK_WINDOW(main_window
), win_title
);
4263 get_domain(const gchar
*host
)
4268 /* handle silly domains like .co.uk */
4270 if ((x
= strlen(host
)) <= 6)
4271 return (g_strdup(host
));
4273 if (host
[x
- 3] == '.' && host
[x
- 6] == '.') {
4279 return (g_strdup(&host
[x
+ 1]));
4283 p
= g_strrstr(host
, ".");
4285 return (g_strdup(""));
4291 return (g_strdup(p
+ 1));
4293 return (g_strdup(host
));
4297 js_autorun(struct tab
*t
)
4301 size_t got_default
= 0, got_host
= 0;
4303 char deff
[PATH_MAX
], hostf
[PATH_MAX
];
4304 char *js
= NULL
, *jsat
, *domain
= NULL
;
4305 FILE *deffile
= NULL
, *hostfile
= NULL
;
4307 if (enable_js_autorun
== 0)
4312 !(g_str_has_prefix(uri
, "http://") ||
4313 g_str_has_prefix(uri
, "https://")))
4316 su
= soup_uri_new(uri
);
4319 if (!SOUP_URI_VALID_FOR_HTTP(su
))
4322 DNPRINTF(XT_D_JS
, "%s: host: %s domain: %s\n", __func__
,
4324 domain
= get_domain(su
->host
);
4326 snprintf(deff
, sizeof deff
, "%s" PS
"default.js", js_dir
);
4327 if ((deffile
= fopen(deff
, "r")) != NULL
) {
4328 if (fstat(fileno(deffile
), &sb
) == -1) {
4329 show_oops(t
, "can't stat default JS file");
4332 got_default
= sb
.st_size
;
4335 /* try host first followed by domain */
4336 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, su
->host
);
4337 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4338 if ((hostfile
= fopen(hostf
, "r")) == NULL
) {
4339 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, domain
);
4340 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4341 if ((hostfile
= fopen(hostf
, "r")) == NULL
)
4344 DNPRINTF(XT_D_JS
, "file: %s\n", hostf
);
4345 if (fstat(fileno(hostfile
), &sb
) == -1) {
4346 show_oops(t
, "can't stat %s JS file", hostf
);
4349 got_host
= sb
.st_size
;
4352 if (got_default
+ got_host
== 0)
4355 js
= g_malloc0(got_default
+ got_host
+ 1);
4359 if (fread(js
, got_default
, 1, deffile
) != 1) {
4360 show_oops(t
, "default file read error");
4363 jsat
= js
+ got_default
;
4367 if (fread(jsat
, got_host
, 1, hostfile
) != 1) {
4368 show_oops(t
, "host file read error");
4373 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
4390 webview_progress_changed_cb(WebKitWebView
*wv
, GParamSpec
*pspec
, struct tab
*t
)
4394 progress
= webkit_web_view_get_progress(wv
);
4395 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.uri
),
4396 progress
== 1.0 ? 0 : progress
);
4397 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->uri_entry
),
4398 progress
== 1.0 ? 0 : progress
);
4400 update_statusbar_position(NULL
, NULL
);
4404 strict_transport_rb_cmp(struct strict_transport
*a
, struct strict_transport
*b
)
4409 /* compare strings from the end */
4410 l1
= strlen(a
->host
);
4411 l2
= strlen(b
->host
);
4415 for (; *p1
== *p2
&& p1
> a
->host
&& p2
> b
->host
;
4420 * Check if we need to do pattern expansion,
4421 * or if we're just keeping the tree in order
4423 if (a
->flags
& XT_STS_FLAGS_EXPAND
&&
4424 b
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) {
4425 /* Check if we're matching the
4426 * 'host.xyz' part in '*.host.xyz'
4428 if (p2
== b
->host
&& (p1
== a
->host
|| *(p1
-1) == '.')) {
4433 if (p1
== a
->host
&& p2
== b
->host
)
4447 RB_GENERATE(strict_transport_tree
, strict_transport
, entry
,
4448 strict_transport_rb_cmp
);
4451 strict_transport_add(const char *domain
, time_t timeout
, int subdomains
)
4453 struct strict_transport
*d
, find
;
4457 if (enable_strict_transport
== FALSE
)
4460 DPRINTF("strict_transport_add(%s,%" PRIi64
",%d)\n", domain
,
4461 (uint64_t)timeout
, subdomains
);
4467 find
.host
= (char *)domain
;
4469 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4473 /* check if update is needed */
4474 if (d
->timeout
== timeout
&&
4475 (d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) == subdomains
)
4478 d
->timeout
= timeout
;
4480 d
->flags
|= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4482 /* We're still initializing */
4483 if (strict_transport_file
== NULL
)
4486 if ((f
= fopen(strict_transport_file
, "w")) == NULL
) {
4488 "can't open strict-transport rules file");
4492 fprintf(f
, "# Generated file - do not update unless you know "
4493 "what you're doing\n");
4494 RB_FOREACH(d
, strict_transport_tree
, &st_tree
) {
4495 if (d
->timeout
< now
)
4497 fprintf(f
, "%s\t%" PRIi64
"\t%d\n", d
->host
,
4498 (uint64_t)d
->timeout
,
4499 d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
);
4503 d
= g_malloc(sizeof *d
);
4504 d
->host
= g_strdup(domain
);
4505 d
->timeout
= timeout
;
4507 d
->flags
= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4510 RB_INSERT(strict_transport_tree
, &st_tree
, d
);
4512 /* We're still initializing */
4513 if (strict_transport_file
== NULL
)
4516 if ((f
= fopen(strict_transport_file
, "a+")) == NULL
) {
4518 "can't open strict-transport rules file");
4522 fseek(f
, 0, SEEK_END
);
4523 fprintf(f
,"%s\t%" PRIi64
"\t%d\n", d
->host
, (uint64_t)timeout
,
4531 strict_transport_check(const char *host
)
4533 static struct strict_transport
*d
= NULL
;
4534 struct strict_transport find
;
4536 if (enable_strict_transport
== FALSE
)
4539 find
.host
= (char *)host
;
4541 /* match for domains that include subdomains */
4542 find
.flags
= XT_STS_FLAGS_EXPAND
;
4544 /* First, check if we're already at the right node */
4545 if (d
!= NULL
&& strict_transport_rb_cmp(&find
, d
) == 0) {
4549 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4557 strict_transport_init()
4559 char file
[PATH_MAX
];
4565 time_t timeout
, now
;
4568 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_STS_FILE
);
4569 if ((f
= fopen(file
, "r")) == NULL
) {
4570 strict_transport_file
= g_strdup(file
);
4581 if ((rule
= fparseln(f
, &len
, NULL
, delim
, 0)) == NULL
) {
4582 if (!feof(f
) || ferror(f
))
4588 /* get second entry */
4589 if ((ptr
= strpbrk(rule
, " \t")) == NULL
)
4593 timeout
= atoi(ptr
);
4595 /* get third entry */
4596 if ((ptr
= strpbrk(ptr
, " \t")) == NULL
)
4600 subdomains
= atoi(ptr
);
4603 strict_transport_add(rule
, timeout
, subdomains
);
4608 strict_transport_file
= g_strdup(file
);
4612 startpage_add("strict-transport rules file ('%s') is corrupt", file
);
4620 force_https_check(const char *uri
)
4622 struct wl_entry
*w
= NULL
;
4627 if ((w
= wl_find_uri(uri
, &force_https
)) == NULL
)
4634 strict_transport_security_cb(SoupMessage
*msg
, gpointer data
)
4640 int subdomains
= FALSE
;
4645 sts
= soup_message_headers_get_one(msg
->response_headers
,
4646 "Strict-Transport-Security");
4647 uri
= soup_message_get_uri(msg
);
4649 if (sts
== NULL
|| uri
== NULL
)
4652 if ((ptr
= strcasestr(sts
, "max-age="))) {
4653 ptr
+= strlen("max-age=");
4654 timeout
= atoll(ptr
);
4656 return; /* malformed header - max-age must be included */
4658 if ((ptr
= strcasestr(sts
, "includeSubDomains")))
4661 strict_transport_add(uri
->host
, timeout
+ time(NULL
), subdomains
);
4665 session_rq_cb(SoupSession
*s
, SoupMessage
*msg
, SoupSocket
*socket
,
4675 if (s
== NULL
|| msg
== NULL
)
4678 if (enable_strict_transport
) {
4679 soup_message_add_header_handler(msg
, "finished",
4680 "Strict-Transport-Security",
4681 G_CALLBACK(strict_transport_security_cb
), NULL
);
4684 if (referer_mode
== XT_REFERER_ALWAYS
)
4687 /* Check if referer is set - and what the user requested for referers */
4688 ref
= soup_message_headers_get_one(msg
->request_headers
, "Referer");
4690 DNPRINTF(XT_D_NAV
, "session_rq_cb: Referer: %s\n", ref
);
4691 switch (referer_mode
) {
4692 case XT_REFERER_NEVER
:
4693 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing referer\n");
4694 soup_message_headers_remove(msg
->request_headers
,
4697 case XT_REFERER_SAME_DOMAIN
:
4698 ref_uri
= soup_uri_new(ref
);
4699 dest
= soup_message_get_uri(msg
);
4701 ref_suffix
= tld_get_suffix(ref_uri
->host
);
4702 dest_suffix
= tld_get_suffix(dest
->host
);
4704 if (dest
&& ref_suffix
&& dest_suffix
&&
4705 strcmp(ref_suffix
, dest_suffix
) != 0) {
4706 soup_message_headers_remove(msg
->request_headers
,
4708 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4709 "referer (not same domain) (suffixes: %s - %s)\n",
4710 ref_suffix
, dest_suffix
);
4712 soup_uri_free(ref_uri
);
4714 case XT_REFERER_SAME_FQDN
:
4715 ref_uri
= soup_uri_new(ref
);
4716 dest
= soup_message_get_uri(msg
);
4717 if (dest
&& strcmp(ref_uri
->host
, dest
->host
) != 0) {
4718 soup_message_headers_remove(msg
->request_headers
,
4720 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4721 "referer (not same fqdn) (should be %s)\n",
4724 soup_uri_free(ref_uri
);
4726 case XT_REFERER_CUSTOM
:
4727 DNPRINTF(XT_D_NAV
, "session_rq_cb: setting referer "
4728 "to %s\n", referer_custom
);
4729 soup_message_headers_replace(msg
->request_headers
,
4730 "Referer", referer_custom
);
4737 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
4738 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
4739 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
4741 WebKitWebNavigationReason reason
;
4745 show_oops(NULL
, "webview_npd_cb invalid parameters");
4749 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
4751 webkit_network_request_get_uri(request
));
4753 uri
= (char *)webkit_network_request_get_uri(request
);
4755 if (!auto_load_images
&& t
->load_images
) {
4757 /* Disable autoloading of images, now that we're done loading
4759 g_object_set(G_OBJECT(t
->settings
),
4760 "auto-load-images", FALSE
, (char *)NULL
);
4761 webkit_web_view_set_settings(t
->wv
, t
->settings
);
4763 t
->load_images
= FALSE
;
4766 /* If this is an xtp url, we don't load anything else. */
4767 if (parse_xtp_url(t
, uri
)) {
4768 webkit_web_policy_decision_ignore(pd
);
4772 if (parse_custom_uri(t
, uri
)) {
4773 webkit_web_policy_decision_ignore(pd
);
4777 if (valid_url_type(uri
)) {
4778 show_oops(t
, "Stopping attempt to load an invalid URI (possible"
4779 " bait and switch attack)");
4780 webkit_web_policy_decision_ignore(pd
);
4784 if ((t
->mode
== XT_MODE_HINT
&& t
->new_tab
) || t
->ctrl_click
) {
4786 create_new_tab(uri
, NULL
, ctrl_click_focus
, -1);
4787 webkit_web_policy_decision_ignore(pd
);
4788 return (TRUE
); /* we made the decission */
4792 * This is a little hairy but it comes down to this:
4793 * when we run in whitelist mode we have to assist the browser in
4794 * opening the URL that it would have opened in a new tab.
4796 reason
= webkit_web_navigation_action_get_reason(na
);
4797 if (reason
== WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED
) {
4798 set_normal_tab_meaning(t
);
4799 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1)
4801 webkit_web_policy_decision_use(pd
);
4802 return (TRUE
); /* we made the decision */
4809 webview_rrs_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, WebKitWebResource
*res
,
4810 WebKitNetworkRequest
*request
, WebKitNetworkResponse
*response
,
4813 SoupMessage
*msg
= NULL
;
4814 SoupURI
*uri
= NULL
;
4815 struct http_accept ha_find
, *ha
= NULL
;
4816 struct user_agent ua_find
, *ua
= NULL
;
4817 struct domain_id di_find
, *di
= NULL
;
4820 msg
= webkit_network_request_get_message(request
);
4823 uri
= soup_message_get_uri(msg
);
4826 uri_s
= soup_uri_to_string(uri
, FALSE
);
4828 if (strcmp(uri
->scheme
, SOUP_URI_SCHEME_HTTP
) == 0) {
4829 if (strict_transport_check(uri
->host
) ||
4830 force_https_check(uri_s
)) {
4831 DNPRINTF(XT_D_NAV
, "webview_rrs_cb: force https for %s\n",
4833 soup_uri_set_scheme(uri
, SOUP_URI_SCHEME_HTTPS
);
4838 soup_message_headers_append(msg
->request_headers
, "DNT", "1");
4841 * Check if resources on this domain have been loaded before. If
4842 * not, add the current tab's http-accept and user-agent id's to a
4843 * new domain_id and insert into the RB tree. Use these http headers
4844 * for all resources loaded from this domain for the lifetime of the
4847 if ((di_find
.domain
= uri
->host
) == NULL
)
4849 if ((di
= RB_FIND(domain_id_list
, &di_list
, &di_find
)) == NULL
) {
4850 di
= g_malloc(sizeof *di
);
4851 di
->domain
= g_strdup(uri
->host
);
4852 di
->ua_id
= t
->user_agent_id
++;
4853 di
->ha_id
= t
->http_accept_id
++;
4854 RB_INSERT(domain_id_list
, &di_list
, di
);
4856 ua_find
.id
= t
->user_agent_id
;
4857 ua
= RB_FIND(user_agent_list
, &ua_list
, &ua_find
);
4859 t
->user_agent_id
= 0;
4861 ha_find
.id
= t
->http_accept_id
;
4862 ha
= RB_FIND(http_accept_list
, &ha_list
, &ha_find
);
4864 t
->http_accept_id
= 0;
4867 ua_find
.id
= di
->ua_id
;
4868 ua
= RB_FIND(user_agent_list
, &ua_list
, &ua_find
);
4869 ha_find
.id
= di
->ha_id
;
4870 ha
= RB_FIND(http_accept_list
, &ha_list
, &ha_find
);
4873 soup_message_headers_replace(msg
->request_headers
,
4874 "User-Agent", ua
->value
);
4876 soup_message_headers_replace(msg
->request_headers
,
4877 "Accept", ha
->value
);
4885 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4888 struct wl_entry
*w
= NULL
;
4890 WebKitWebView
*webview
= NULL
;
4893 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
4894 webkit_web_view_get_uri(wv
));
4897 /* open in current tab */
4899 } else if (enable_scripts
== 0 && enable_js_whitelist
== 1) {
4900 uri
= webkit_web_view_get_uri(wv
);
4901 if (uri
&& (w
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4904 if (t
->ctrl_click
) {
4905 x
= ctrl_click_focus
;
4908 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4910 } else if (enable_scripts
== 1) {
4911 if (t
->ctrl_click
) {
4912 x
= ctrl_click_focus
;
4915 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4923 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
4926 struct wl_entry
*w
= NULL
;
4928 DNPRINTF(XT_D_NAV
, "webview_close_cb: %d\n", t
->tab_id
);
4930 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
4931 uri
= webkit_web_view_get_uri(wv
);
4932 if (uri
&& (w
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4936 } else if (enable_scripts
== 1)
4943 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
4945 /* we can not eat the event without throwing gtk off so defer it */
4947 /* catch middle click */
4948 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 2) {
4953 /* catch ctrl click */
4954 if (e
->type
== GDK_BUTTON_RELEASE
&&
4955 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
4960 return (XT_CB_PASSTHROUGH
);
4964 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
4966 struct mime_type
*m
;
4968 GError
*gerr
= NULL
;
4970 m
= find_mime_type(mime_type
);
4976 sv
[0] = m
->mt_action
;
4977 sv
[1] = (char *)webkit_network_request_get_uri(request
);
4980 /* ignore donothing from example config */
4981 if (m
->mt_action
&& !strcmp(m
->mt_action
, "donothing"))
4984 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
, NULL
,
4986 show_oops(t
, "%s: could not spawn process (%s)", __func__
,
4987 gerr
? gerr
->message
: "N/A");
4992 get_mime_type(const char *uri
)
4997 char *mime_type
= NULL
;
5001 show_oops(NULL
, "%s: invalid parameters", __func__
);
5005 if (g_str_has_prefix(uri
, "file://"))
5006 file
= g_filename_from_uri(uri
, NULL
, NULL
);
5008 file
= g_strdup(uri
);
5013 gf
= g_file_new_for_path(file
);
5014 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
5016 if ((m
= g_file_info_get_content_type(fi
)) != NULL
)
5017 mime_type
= g_strdup(m
);
5026 run_download_mimehandler(char *mime_type
, char *file
)
5028 struct mime_type
*m
;
5031 m
= find_mime_type(mime_type
);
5035 sv
[0] = m
->mt_action
;
5038 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
, NULL
,
5040 show_oops(NULL
, "%s: could not spawn process: %s %s", __func__
,
5048 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
5051 WebKitDownloadStatus status
;
5056 if (download
== NULL
)
5058 status
= webkit_download_get_status(download
);
5059 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
5062 if (download_notifications
)
5063 show_oops(NULL
, "Download of '%s' finished",
5064 basename((char *)webkit_download_get_destination_uri(download
)));
5065 uri
= webkit_download_get_destination_uri(download
);
5068 mime
= get_mime_type(uri
);
5072 if (g_str_has_prefix(uri
, "file://"))
5073 file
= g_filename_from_uri(uri
, NULL
, NULL
);
5075 file
= g_strdup(uri
);
5080 run_download_mimehandler((char *)mime
, file
);
5087 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
5088 WebKitNetworkRequest
*request
, char *mime_type
,
5089 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
5092 show_oops(NULL
, "webview_mimetype_cb invalid parameters");
5096 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
5097 t
->tab_id
, mime_type
);
5099 if (run_mimehandler(t
, mime_type
, request
) == 0) {
5100 webkit_web_policy_decision_ignore(decision
);
5105 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
5106 webkit_web_policy_decision_download(decision
);
5114 download_start(struct tab
*t
, struct download
*d
, int flag
)
5116 WebKitNetworkRequest
*req
;
5118 const gchar
*suggested_name
;
5119 gchar
*filename
= NULL
;
5125 if (d
== NULL
|| t
== NULL
) {
5126 show_oops(NULL
, "%s invalid parameters", __func__
);
5130 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
5131 if (suggested_name
== NULL
)
5132 return (FALSE
); /* abort download */
5143 filename
= g_strdup_printf("%d%s", i
, suggested_name
);
5146 /* XXX using urls doesn't work properly in windows? */
5147 uri
= g_strdup_printf("%s\\%s", download_dir
, i
?
5148 filename
: suggested_name
);
5150 path
= g_strdup_printf("%s" PS
"%s", download_dir
, i
?
5151 filename
: suggested_name
);
5152 if ((uri
= g_filename_to_uri(path
, NULL
, NULL
)) == NULL
)
5157 } while (!stat(uri
, &sb
));
5159 } while (!stat(path
, &sb
));
5162 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d filename %s "
5163 "local %s\n", __func__
, t
->tab_id
, filename
, uri
);
5165 /* if we're restarting the download, or starting
5166 * it after doing something else, we need to recreate
5167 * the download request.
5169 if (flag
== XT_DL_RESTART
) {
5170 req
= webkit_network_request_new(webkit_download_get_uri(d
->download
));
5171 webkit_download_cancel(d
->download
);
5172 g_object_unref(d
->download
);
5173 d
->download
= webkit_download_new(req
);
5176 webkit_download_set_destination_uri(d
->download
, uri
);
5178 if (webkit_download_get_status(d
->download
) ==
5179 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
5180 show_oops(t
, "%s: download failed to start", __func__
);
5182 show_oops(t
, "Download Failed");
5184 /* connect "download first" mime handler */
5185 g_signal_connect(G_OBJECT(d
->download
), "notify::status",
5186 G_CALLBACK(download_status_changed_cb
), NULL
);
5188 /* get from history */
5189 g_object_ref(d
->download
);
5190 show_oops(t
, "Download of '%s' started...",
5191 basename((char *)webkit_download_get_destination_uri(d
->download
)));
5194 if (flag
!= XT_DL_START
)
5195 webkit_download_start(d
->download
);
5197 DNPRINTF(XT_D_DOWNLOAD
, "download status : %d",
5198 webkit_download_get_status(d
->download
));
5200 /* sync other download manager tabs */
5201 update_download_tabs(NULL
);
5214 download_ask_cb(struct tab
*t
, GdkEventKey
*e
, gpointer data
)
5216 struct download
*d
= data
;
5220 t
->mode_cb_data
= NULL
;
5223 e
->keyval
= GDK_Escape
;
5224 return (XT_CB_PASSTHROUGH
);
5227 DPRINTF("download_ask_cb: User pressed %c\n", e
->keyval
);
5228 if (e
->keyval
== 'y' || e
->keyval
== 'Y' || e
->keyval
== GDK_Return
)
5229 /* We need to do a RESTART, because we're not calling from
5230 * webview_download_cb
5232 download_start(t
, d
, XT_DL_RESTART
);
5234 /* for all other keyvals, we just let the download be */
5235 e
->keyval
= GDK_Escape
;
5236 return (XT_CB_HANDLED
);
5240 download_ask(struct tab
*t
, struct download
*d
)
5242 const gchar
*suggested_name
;
5244 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
5245 if (suggested_name
== NULL
)
5246 return (FALSE
); /* abort download */
5248 show_oops(t
, "download file %s [y/n] ?", suggested_name
);
5249 t
->mode_cb
= download_ask_cb
;
5250 t
->mode_cb_data
= d
;
5256 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
5259 const gchar
*suggested_name
;
5260 struct download
*download_entry
;
5263 if (wk_download
== NULL
|| t
== NULL
) {
5264 show_oops(NULL
, "%s invalid parameters", __func__
);
5268 suggested_name
= webkit_download_get_suggested_filename(wk_download
);
5269 if (suggested_name
== NULL
)
5270 return (FALSE
); /* abort download */
5272 download_entry
= g_malloc(sizeof(struct download
));
5273 download_entry
->download
= wk_download
;
5274 download_entry
->tab
= t
;
5275 download_entry
->id
= next_download_id
++;
5276 RB_INSERT(download_list
, &downloads
, download_entry
);
5277 t
->download_requested
= 1;
5279 if (download_mode
== XT_DM_START
)
5280 ret
= download_start(t
, download_entry
, XT_DL_START
);
5281 else if (download_mode
== XT_DM_ASK
)
5282 ret
= download_ask(t
, download_entry
);
5283 else if (download_mode
== XT_DM_ADD
)
5284 show_oops(t
, "added %s to download manager",
5287 /* sync other download manager tabs */
5288 update_download_tabs(NULL
);
5291 * NOTE: never redirect/render the current tab before this
5292 * function returns. This will cause the download to never start.
5294 return (ret
); /* start download */
5298 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
5300 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
5303 show_oops(NULL
, "webview_hover_cb");
5308 set_status(t
, "Link: %s", uri
);
5310 if (statusbar_style
== XT_STATUSBAR_URL
) {
5311 const gchar
*page_uri
;
5313 if ((page_uri
= get_uri(t
)) != NULL
)
5314 set_status(t
, "%s", page_uri
);
5316 set_status(t
, "%s", (char *)get_title(t
, FALSE
));
5321 mark(struct tab
*t
, struct karg
*arg
)
5328 if ((index
= marktoindex(mark
)) == -1)
5331 if (arg
->i
== XT_MARK_SET
)
5332 t
->mark
[index
] = gtk_adjustment_get_value(t
->adjust_v
);
5333 else if (arg
->i
== XT_MARK_GOTO
) {
5334 if (t
->mark
[index
] == XT_INVALID_MARK
) {
5335 show_oops(t
, "mark '%c' does not exist", mark
);
5338 /* XXX t->mark[index] can be bigger than the maximum if ajax or
5339 something changes the document size */
5340 pos
= gtk_adjustment_get_value(t
->adjust_v
);
5341 gtk_adjustment_set_value(t
->adjust_v
, t
->mark
[index
]);
5342 t
->mark
[marktoindex('\'')] = pos
;
5349 marks_clear(struct tab
*t
)
5353 for (i
= 0; i
< LENGTH(t
->mark
); i
++)
5354 t
->mark
[i
] = XT_INVALID_MARK
;
5360 char file
[PATH_MAX
];
5361 char *line
= NULL
, *p
;
5366 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
5367 if ((f
= fopen(file
, "r+")) == NULL
) {
5368 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
5372 for (i
= 1; ; i
++) {
5373 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
5375 if (strlen(line
) == 0 || line
[0] == '#') {
5381 p
= strtok(line
, " \t");
5383 if (p
== NULL
|| strlen(p
) != 1 ||
5384 (index
= qmarktoindex(*p
)) == -1) {
5385 warnx("corrupt quickmarks file, line %d", i
);
5389 p
= strtok(NULL
, " \t");
5390 if (qmarks
[index
] != NULL
)
5391 g_free(qmarks
[index
]);
5392 qmarks
[index
] = g_strdup(p
);
5403 char file
[PATH_MAX
];
5407 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
5408 if ((f
= fopen(file
, "r+")) == NULL
) {
5409 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
5413 for (i
= 0; i
< XT_NOQMARKS
; i
++)
5414 if (qmarks
[i
] != NULL
)
5415 fprintf(f
, "%c %s\n", indextoqmark(i
), qmarks
[i
]);
5423 qmark(struct tab
*t
, struct karg
*arg
)
5428 mark
= arg
->s
[strlen(arg
->s
)-1];
5429 index
= qmarktoindex(mark
);
5435 if (qmarks
[index
] != NULL
) {
5436 g_free(qmarks
[index
]);
5437 qmarks
[index
] = NULL
;
5440 qmarks_load(); /* sync if multiple instances */
5441 qmarks
[index
] = g_strdup(get_uri(t
));
5445 if (qmarks
[index
] != NULL
)
5446 load_uri(t
, qmarks
[index
]);
5448 show_oops(t
, "quickmark \"%c\" does not exist",
5454 if (qmarks
[index
] != NULL
)
5455 create_new_tab(qmarks
[index
], NULL
, 1, -1);
5457 show_oops(t
, "quickmark \"%c\" does not exist",
5468 go_up(struct tab
*t
, struct karg
*args
)
5476 if (args
->i
== XT_GO_UP_ROOT
)
5477 levels
= XT_GO_UP_ROOT
;
5478 else if ((levels
= atoi(args
->s
)) == 0)
5481 uri
= g_strdup(get_uri(t
));
5485 if ((tmp
= strstr(uri
, XT_PROTO_DELIM
)) == NULL
)
5488 tmp
+= strlen(XT_PROTO_DELIM
);
5490 /* it makes no sense to strip the last slash from ".../dir/", skip it */
5491 lastidx
= strlen(tmp
) - 1;
5493 if (tmp
[lastidx
] == '/')
5494 tmp
[lastidx
] = '\0';
5498 p
= strrchr(tmp
, '/');
5499 if (p
== tmp
) { /* Are we at the root of a file://-path? */
5502 } else if (p
!= NULL
)
5515 gototab(struct tab
*t
, struct karg
*args
)
5518 struct karg arg
= {0, NULL
, -1};
5520 tab
= atoi(args
->s
);
5523 arg
.i
= XT_TAB_NEXT
;
5535 zoom_amount(struct tab
*t
, struct karg
*arg
)
5537 struct karg narg
= {0, NULL
, -1};
5539 narg
.i
= atoi(arg
->s
);
5540 resizetab(t
, &narg
);
5546 flip_colon(struct tab
*t
, struct karg
*arg
)
5548 struct karg narg
= {0, NULL
, -1};
5551 if (t
== NULL
|| arg
== NULL
)
5554 p
= strstr(arg
->s
, ":");
5566 /* buffer commands receive the regex that triggered them in arg.s */
5567 char bcmd
[XT_BUFCMD_SZ
];
5571 #define XT_PRE_NO (0)
5572 #define XT_PRE_YES (1)
5573 #define XT_PRE_MAYBE (2)
5575 int (*func
)(struct tab
*, struct karg
*);
5579 { "^[0-9]*gu$", XT_PRE_MAYBE
, "gu", go_up
, 0 },
5580 { "^gU$", XT_PRE_NO
, "gU", go_up
, XT_GO_UP_ROOT
},
5581 { "^gg$", XT_PRE_NO
, "gg", move
, XT_MOVE_TOP
},
5582 { "^gG$", XT_PRE_NO
, "gG", move
, XT_MOVE_BOTTOM
},
5583 { "^[0-9]+%$", XT_PRE_YES
, "%", move
, XT_MOVE_PERCENT
},
5584 { "^zz$", XT_PRE_NO
, "zz", move
, XT_MOVE_CENTER
},
5585 { "^gh$", XT_PRE_NO
, "gh", go_home
, 0 },
5586 { "^m[a-zA-Z0-9]$", XT_PRE_NO
, "m", mark
, XT_MARK_SET
},
5587 { "^['][a-zA-Z0-9']$", XT_PRE_NO
, "'", mark
, XT_MARK_GOTO
},
5588 { "^[0-9]+t$", XT_PRE_YES
, "t", gototab
, 0 },
5589 { "^g0$", XT_PRE_YES
, "g0", movetab
, XT_TAB_FIRST
},
5590 { "^g[$]$", XT_PRE_YES
, "g$", movetab
, XT_TAB_LAST
},
5591 { "^[0-9]*gt$", XT_PRE_YES
, "t", movetab
, XT_TAB_NEXT
},
5592 { "^[0-9]*gT$", XT_PRE_YES
, "T", movetab
, XT_TAB_PREV
},
5593 { "^M[a-zA-Z0-9]$", XT_PRE_NO
, "M", qmark
, XT_QMARK_SET
},
5594 { "^go[a-zA-Z0-9]$", XT_PRE_NO
, "go", qmark
, XT_QMARK_OPEN
},
5595 { "^gn[a-zA-Z0-9]$", XT_PRE_NO
, "gn", qmark
, XT_QMARK_TAB
},
5596 { "^ZR$", XT_PRE_NO
, "ZR", restart
, 0 },
5597 { "^ZZ$", XT_PRE_NO
, "ZZ", quit
, 0 },
5598 { "^zi$", XT_PRE_NO
, "zi", resizetab
, XT_ZOOM_IN
},
5599 { "^zo$", XT_PRE_NO
, "zo", resizetab
, XT_ZOOM_OUT
},
5600 { "^z0$", XT_PRE_NO
, "z0", resizetab
, XT_ZOOM_NORMAL
},
5601 { "^[0-9]+Z$", XT_PRE_YES
, "Z", zoom_amount
, 0 },
5602 { "^[0-9]+:$", XT_PRE_YES
, ":", flip_colon
, 0 },
5606 buffercmd_init(void)
5610 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5611 if (regcomp(&buffercmds
[i
].cregex
, buffercmds
[i
].regex
,
5612 REG_EXTENDED
| REG_NOSUB
))
5613 startpage_add("invalid buffercmd regex %s",
5614 buffercmds
[i
].regex
);
5618 buffercmd_abort(struct tab
*t
)
5625 DNPRINTF(XT_D_BUFFERCMD
, "%s: clearing buffer\n", __func__
);
5627 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5630 cmd_prefix
= 0; /* clear prefix for non-buffer commands */
5631 gtk_label_set_text(GTK_LABEL(t
->sbe
.buffercmd
), bcmd
);
5635 buffercmd_execute(struct tab
*t
, struct buffercmd
*cmd
)
5637 struct karg arg
= {0, NULL
, -1};
5640 arg
.s
= g_strdup(bcmd
);
5642 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_execute: buffer \"%s\" "
5643 "matches regex \"%s\", executing\n", bcmd
, cmd
->regex
);
5653 buffercmd_addkey(struct tab
*t
, guint keyval
)
5656 char s
[XT_BUFCMD_SZ
];
5658 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
))) {
5660 return (XT_CB_PASSTHROUGH
);
5663 if (keyval
== GDK_Escape
) {
5665 return (XT_CB_HANDLED
);
5668 /* key with modifier or non-ascii character */
5669 if (!isascii(keyval
)) {
5671 * XXX this looks wrong but fixes some sites like
5672 * http://www.seslisozluk.com/
5673 * that eat a shift or ctrl and end putting default focus in js
5674 * instead of ignoring the keystroke
5675 * so instead of return (XT_CB_PASSTHROUGH); eat the key
5677 return (XT_CB_HANDLED
);
5680 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: adding key \"%c\" "
5681 "to buffer \"%s\"\n", keyval
, bcmd
);
5683 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5684 if (bcmd
[i
] == '\0') {
5689 /* buffer full, ignore input */
5690 if (i
>= LENGTH(bcmd
) -1) {
5691 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: buffer full\n");
5693 return (XT_CB_HANDLED
);
5696 gtk_label_set_text(GTK_LABEL(t
->sbe
.buffercmd
), bcmd
);
5698 /* find exact match */
5699 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5700 if (regexec(&buffercmds
[i
].cregex
, bcmd
,
5701 (size_t) 0, NULL
, 0) == 0) {
5702 buffercmd_execute(t
, &buffercmds
[i
]);
5706 /* find non exact matches to see if we need to abort ot not */
5707 for (i
= 0, match
= 0; i
< LENGTH(buffercmds
); i
++) {
5708 DNPRINTF(XT_D_BUFFERCMD
, "trying: %s\n", bcmd
);
5711 if (buffercmds
[i
].precount
== XT_PRE_MAYBE
) {
5712 if (isdigit(bcmd
[0])) {
5713 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5717 if (sscanf(bcmd
, "%s", s
) == 0)
5720 } else if (buffercmds
[i
].precount
== XT_PRE_YES
) {
5721 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5724 if (sscanf(bcmd
, "%s", s
) == 0)
5727 if (c
== -1 && buffercmds
[i
].precount
)
5729 if (!strncmp(s
, buffercmds
[i
].cmd
, strlen(s
)))
5732 DNPRINTF(XT_D_BUFFERCMD
, "got[%d] %d <%s>: %d %s\n",
5733 i
, match
, buffercmds
[i
].cmd
, c
, s
);
5736 DNPRINTF(XT_D_BUFFERCMD
, "aborting: %s\n", bcmd
);
5741 return (XT_CB_HANDLED
);
5745 * XXX we were seeing a bunch of focus issues with the toplevel
5746 * main_window losing its is-active and has-toplevel-focus properties.
5747 * This is the most correct and portable solution we could come up with
5748 * without relying on calling internal GTK functions (which we
5749 * couldn't link to in Linux).
5751 #if GTK_CHECK_VERSION(3, 0, 0)
5753 fake_focus_in(GtkWidget
*w
)
5755 if (fevent
== NULL
) {
5756 fevent
= gdk_event_new(GDK_FOCUS_CHANGE
);
5757 fevent
->focus_change
.window
=
5758 gtk_widget_get_window(main_window
);
5759 fevent
->focus_change
.type
= GDK_FOCUS_CHANGE
;
5760 fevent
->focus_change
.in
= TRUE
;
5762 gtk_widget_send_focus_change(main_window
, fevent
);
5767 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
5770 struct key_binding
*k
;
5773 * This sometimes gets randomly unset for whatever reason in GTK3.
5774 * If we're handling a keypress, the main window's is-active propery
5775 * *must* be true, or else many things will break.
5777 #if GTK_CHECK_VERSION(3, 0, 0)
5778 fake_focus_in(main_window
);
5781 /* handle keybindings if buffercmd is empty.
5782 if not empty, allow commands like C-n */
5783 if (bcmd
[0] == '\0' || ((e
->state
& (CTRL
| MOD1
)) != 0))
5784 TAILQ_FOREACH(k
, &kbl
, entry
)
5785 if (e
->keyval
== k
->key
5786 && (entry
? k
->use_in_entry
: 1)) {
5787 /* when we are edditing eat ctrl/mod keys */
5788 if (edit_mode
== XT_EM_VI
&&
5789 t
->mode
== XT_MODE_INSERT
&&
5790 (e
->state
& CTRL
|| e
->state
& MOD1
))
5791 return (XT_CB_PASSTHROUGH
);
5794 if ((e
->state
& (CTRL
| MOD1
)) == 0)
5796 } else if ((e
->state
& k
->mask
) == k
->mask
) {
5801 if (!entry
&& ((e
->state
& (CTRL
| MOD1
)) == 0))
5802 return (buffercmd_addkey(t
, e
->keyval
));
5804 return (XT_CB_PASSTHROUGH
);
5807 if (k
->cmd
[0] == ':') {
5809 args
.s
= &k
->cmd
[1];
5810 return (command(t
, &args
));
5812 return (cmd_execute(t
, k
->cmd
));
5816 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5820 /* don't use w directly; use t->whatever instead */
5823 show_oops(NULL
, "wv_keypress_cb");
5824 return (XT_CB_PASSTHROUGH
);
5830 return (t
->mode_cb(t
, e
, t
->mode_cb_data
));
5832 DNPRINTF(XT_D_KEY
, "wv_keypress_cb: mode %d keyval 0x%x mask "
5833 "0x%x tab %d\n", t
->mode
, e
->keyval
, e
->state
, t
->tab_id
);
5835 /* Hide buffers, if they are visible, with escape. */
5836 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)) &&
5837 CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
5838 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5840 return (XT_CB_HANDLED
);
5843 if ((CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Tab
) ||
5844 (CLEAN(e
->state
) == SHFT
&& e
->keyval
== GDK_Tab
))
5845 /* something focussy is about to happen */
5846 return (XT_CB_PASSTHROUGH
);
5848 /* check if we are some sort of text input thing in the dom */
5849 input_check_mode(t
);
5851 if (t
->mode
== XT_MODE_PASSTHROUGH
) {
5852 if (CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
)
5853 t
->mode
= XT_MODE_COMMAND
;
5854 return (XT_CB_PASSTHROUGH
);
5855 } else if (t
->mode
== XT_MODE_COMMAND
|| t
->mode
== XT_MODE_HINT
) {
5857 snprintf(s
, sizeof s
, "%c", e
->keyval
);
5858 if (CLEAN(e
->state
) == 0 && isdigit(s
[0]))
5859 cmd_prefix
= 10 * cmd_prefix
+ atoi(s
);
5860 return (handle_keypress(t
, e
, 0));
5863 return (handle_keypress(t
, e
, 1));
5867 return (XT_CB_PASSTHROUGH
);
5871 hint_continue(struct tab
*t
)
5873 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5875 const gchar
*errstr
= NULL
;
5879 if (!(c
[0] == '.' || c
[0] == ','))
5881 if (strlen(c
) == 1) {
5882 /* XXX should not happen */
5887 if (isdigit(c
[1])) {
5889 i
= strtonum(&c
[1], 1, 4096, &errstr
);
5891 show_oops(t
, "invalid numerical hint %s", &c
[1]);
5894 s
= g_strdup_printf("hints.updateHints(%d);", i
);
5898 /* alphanumeric input */
5899 s
= g_strdup_printf("hints.createHints('%s', '%c');",
5900 &c
[1], c
[0] == '.' ? 'f' : 'F');
5911 search_continue(struct tab
*t
)
5913 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5914 gboolean rv
= FALSE
;
5916 if (c
[0] == ':' || c
[0] == '.' || c
[0] == ',')
5918 if (strlen(c
) == 1) {
5919 webkit_web_view_unmark_text_matches(t
->wv
);
5924 t
->search_forward
= TRUE
;
5925 else if (c
[0] == '?')
5926 t
->search_forward
= FALSE
;
5936 search_cb(struct tab
*t
)
5938 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5939 #if !GTK_CHECK_VERSION(3, 0, 0)
5943 if (search_continue(t
) == FALSE
)
5947 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, t
->search_forward
,
5949 /* not found, mark red */
5950 #if GTK_CHECK_VERSION(3, 0, 0)
5951 gtk_widget_set_name(t
->cmd
, XT_CSS_RED
);
5953 gdk_color_parse(XT_COLOR_RED
, &color
);
5954 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
5956 /* unmark and remove selection */
5957 webkit_web_view_unmark_text_matches(t
->wv
);
5958 /* my kingdom for a way to unselect text in webview */
5960 /* found, highlight all */
5961 webkit_web_view_unmark_text_matches(t
->wv
);
5962 webkit_web_view_mark_text_matches(t
->wv
, &c
[1], FALSE
, 0);
5963 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
5964 #if GTK_CHECK_VERSION(3, 0, 0)
5965 gtk_widget_set_name(t
->cmd
, XT_CSS_NORMAL
);
5967 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
5968 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
5977 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5979 const gchar
*c
= gtk_entry_get_text(w
);
5982 show_oops(NULL
, "cmd_keyrelease_cb invalid parameters");
5983 return (XT_CB_PASSTHROUGH
);
5986 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
5987 e
->keyval
, e
->state
, t
->tab_id
);
5990 if (!(e
->keyval
== GDK_Tab
|| e
->keyval
== GDK_ISO_Left_Tab
)) {
5991 if (hint_continue(t
) == FALSE
)
5996 if (search_continue(t
) == FALSE
)
5999 /* if search length is > 4 then no longer play timeout games */
6000 if (strlen(c
) > 4) {
6002 g_source_remove(t
->search_id
);
6009 /* reestablish a new timer if the user types fast */
6011 g_source_remove(t
->search_id
);
6012 t
->search_id
= g_timeout_add(250, (GSourceFunc
)search_cb
, (gpointer
)t
);
6015 return (XT_CB_PASSTHROUGH
);
6019 match_uri(const gchar
*uri
, const gchar
*key
) {
6022 gboolean match
= FALSE
;
6026 if (!strncmp(key
, uri
, len
))
6029 voffset
= strstr(uri
, "/") + 2;
6030 if (!strncmp(key
, voffset
, len
))
6032 else if (g_str_has_prefix(voffset
, "www.")) {
6033 voffset
= voffset
+ strlen("www.");
6034 if (!strncmp(key
, voffset
, len
))
6043 match_session(const gchar
*name
, const gchar
*key
) {
6046 sub
= strcasestr(name
, key
);
6052 cmd_getlist(int id
, char *key
)
6059 if (cmds
[id
].type
& XT_URLARG
) {
6060 RB_FOREACH_REVERSE(h
, history_list
, &hl
)
6061 if (match_uri(h
->uri
, key
)) {
6062 cmd_status
.list
[c
] = (char *)h
->uri
;
6068 } else if (cmds
[id
].type
& XT_SESSARG
) {
6069 TAILQ_FOREACH(s
, &sessions
, entry
)
6070 if (match_session(s
->name
, key
)) {
6071 cmd_status
.list
[c
] = (char *)s
->name
;
6077 } else if (cmds
[id
].type
& XT_SETARG
) {
6078 for (i
= 0; i
< get_settings_size(); i
++)
6079 if (!strncmp(key
, get_setting_name(i
),
6081 cmd_status
.list
[c
++] =
6082 get_setting_name(i
);
6088 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
6090 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
6091 if (cmds
[i
].level
< dep
)
6093 if (cmds
[i
].level
== dep
&& !strncmp(key
, cmds
[i
].cmd
,
6094 strlen(key
)) && !isdigit(cmds
[i
].cmd
[0]))
6095 cmd_status
.list
[c
++] = cmds
[i
].cmd
;
6103 cmd_getnext(int dir
)
6105 cmd_status
.index
+= dir
;
6107 if (cmd_status
.index
< 0)
6108 cmd_status
.index
= cmd_status
.len
- 1;
6109 else if (cmd_status
.index
>= cmd_status
.len
)
6110 cmd_status
.index
= 0;
6112 return cmd_status
.list
[cmd_status
.index
];
6116 cmd_tokenize(char *s
, char *tokens
[])
6119 char *tok
, *last
= NULL
;
6120 size_t len
= strlen(s
);
6123 blank
= len
== 0 || (len
> 0 && s
[len
- 1] == ' ');
6124 for (tok
= strtok_r(s
, " ", &last
); tok
&& i
< 3;
6125 tok
= strtok_r(NULL
, " ", &last
), i
++)
6135 cmd_complete(struct tab
*t
, char *str
, int dir
)
6137 GtkEntry
*w
= GTK_ENTRY(t
->cmd
);
6138 int i
, j
, levels
, c
= 0, dep
= 0, parent
= -1;
6140 char *tok
, *match
, *s
= g_strdup(str
);
6142 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
6145 DNPRINTF(XT_D_CMD
, "%s: complete %s\n", __func__
, str
);
6148 for (i
= 0; isdigit(s
[i
]); i
++)
6151 for (; isspace(s
[i
]); i
++)
6156 levels
= cmd_tokenize(s
, tokens
);
6158 for (i
= 0; i
< levels
- 1; i
++) {
6161 for (j
= c
; j
< LENGTH(cmds
); j
++) {
6162 if (cmds
[j
].level
< dep
)
6164 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
,
6168 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
6175 if (matchcount
== 1) {
6176 strlcat(res
, tok
, sizeof res
);
6177 strlcat(res
, " ", sizeof res
);
6187 if (cmd_status
.index
== -1)
6188 cmd_getlist(parent
, tokens
[i
]);
6190 if (cmd_status
.len
> 0) {
6191 match
= cmd_getnext(dir
);
6192 strlcat(res
, match
, sizeof res
);
6193 gtk_entry_set_text(w
, res
);
6194 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6201 parse_prefix_and_alias(const char *str
, int *prefix
)
6203 struct cmd_alias
*c
;
6204 char *s
= g_strdup(str
), *sc
;
6209 if (isdigit(s
[0])) {
6210 sscanf(s
, "%d", prefix
);
6211 while (isdigit(s
[0]) || isspace(s
[0]))
6215 TAILQ_FOREACH(c
, &cal
, entry
) {
6216 if (strncmp(s
, c
->alias
, strlen(c
->alias
)))
6219 if (strlen(s
) == strlen(c
->alias
)) {
6221 return (g_strdup(c
->cmd
));
6224 if (!isspace(s
[strlen(c
->alias
)]))
6227 s
= g_strdup_printf("%s %s", c
->cmd
, &s
[strlen(c
->alias
) + 1]);
6237 cmd_execute(struct tab
*t
, char *str
)
6239 struct cmd
*cmd
= NULL
;
6240 char *tok
, *last
= NULL
, *s
= str
;
6241 int j
= 0, len
, c
= 0, dep
= 0, matchcount
= 0;
6242 int prefix
= -1, rv
= XT_CB_PASSTHROUGH
;
6243 struct karg arg
= {0, NULL
, -1};
6245 s
= parse_prefix_and_alias(s
, &prefix
);
6247 for (tok
= strtok_r(s
, " ", &last
); tok
;
6248 tok
= strtok_r(NULL
, " ", &last
)) {
6250 for (j
= c
; j
< LENGTH(cmds
); j
++) {
6251 if (cmds
[j
].level
< dep
)
6253 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1 :
6255 if (cmds
[j
].level
== dep
&&
6256 !strncmp(tok
, cmds
[j
].cmd
, len
)) {
6260 if (len
== strlen(cmds
[j
].cmd
)) {
6266 if (matchcount
== 1) {
6271 show_oops(t
, "Invalid command: %s", str
);
6277 show_oops(t
, "Empty command");
6283 arg
.precount
= prefix
;
6284 else if (cmd_prefix
> 0)
6285 arg
.precount
= cmd_prefix
;
6287 if (j
> 0 && !(cmd
->type
& XT_PREFIX
) && arg
.precount
> -1) {
6288 show_oops(t
, "No prefix allowed: %s", str
);
6292 arg
.s
= last
? g_strdup(last
) : g_strdup("");
6293 if (cmd
->type
& XT_INTARG
&& last
&& strlen(last
) > 0) {
6294 if (arg
.s
== NULL
) {
6295 show_oops(t
, "Invalid command");
6298 arg
.precount
= atoi(arg
.s
);
6299 if (arg
.precount
<= 0) {
6300 if (arg
.s
[0] == '0')
6301 show_oops(t
, "Zero count");
6303 show_oops(t
, "Trailing characters");
6308 DNPRINTF(XT_D_CMD
, "%s: prefix %d arg %s\n",
6309 __func__
, arg
.precount
, arg
.s
);
6325 save_runtime_setting(const char *name
, const char *val
)
6331 char file
[PATH_MAX
];
6332 char delim
[3] = { '\0', '\0', '\0' };
6333 char *line
, *lt
, *start
;
6334 char *contents
, *tmp
;
6336 if (runtime_settings
== NULL
|| strlen(runtime_settings
) == 0)
6339 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, runtime_settings
);
6340 if (stat(file
, &sb
) || (f
= fopen(file
, "r+")) == NULL
)
6342 lt
= g_strdup_printf("%s=%s", name
, val
);
6343 contents
= g_strdup("");
6345 line
= fparseln(f
, &linelen
, NULL
, delim
, 0);
6346 if (line
== NULL
|| linelen
== 0)
6349 start
= g_strdup_printf("%s=", name
);
6350 if (strstr(line
, start
) == NULL
)
6351 contents
= g_strdup_printf("%s%s\n", contents
, line
);
6354 contents
= g_strdup_printf("%s%s\n", contents
, lt
);
6363 contents
= g_strdup_printf("%s%s\n", contents
, lt
);
6366 if ((f
= freopen(file
, "w", f
)) == NULL
)
6379 entry_focus_cb(GtkWidget
*w
, GdkEvent e
, struct tab
*t
)
6382 * This sometimes gets randomly unset for whatever reason in GTK3,
6383 * causing a GtkEntry's text cursor becomes invisible. When we focus
6384 * a GtkEntry, be sure to manually reset the main window's is-active
6385 * property so the cursor is shown correctly.
6387 #if GTK_CHECK_VERSION(3, 0, 0)
6388 fake_focus_in(main_window
);
6390 return (XT_CB_PASSTHROUGH
);
6394 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
6397 show_oops(NULL
, "entry_key_cb invalid parameters");
6398 return (XT_CB_PASSTHROUGH
);
6401 DNPRINTF(XT_D_CMD
, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
6402 e
->keyval
, e
->state
, t
->tab_id
);
6406 if (e
->keyval
== GDK_Escape
) {
6407 /* don't use focus_webview(t) because we want to type :cmds */
6408 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6411 return (handle_keypress(t
, e
, 1));
6414 struct command_entry
*
6415 history_prev(struct command_list
*l
, struct command_entry
*at
)
6418 at
= TAILQ_LAST(l
, command_list
);
6420 at
= TAILQ_PREV(at
, command_list
, entry
);
6422 at
= TAILQ_LAST(l
, command_list
);
6428 struct command_entry
*
6429 history_next(struct command_list
*l
, struct command_entry
*at
)
6432 at
= TAILQ_FIRST(l
);
6434 at
= TAILQ_NEXT(at
, entry
);
6436 at
= TAILQ_FIRST(l
);
6443 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
6445 int rv
= XT_CB_HANDLED
;
6446 const gchar
*c
= gtk_entry_get_text(w
);
6450 show_oops(NULL
, "cmd_keypress_cb parameters");
6451 return (XT_CB_PASSTHROUGH
);
6454 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
6455 e
->keyval
, e
->state
, t
->tab_id
);
6459 e
->keyval
= GDK_Escape
;
6460 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
6461 c
[0] == '.' || c
[0] == ','))
6462 e
->keyval
= GDK_Escape
;
6464 if (e
->keyval
!= GDK_Tab
&& e
->keyval
!= GDK_Shift_L
&&
6465 e
->keyval
!= GDK_ISO_Left_Tab
)
6466 cmd_status
.index
= -1;
6468 switch (e
->keyval
) {
6471 cmd_complete(t
, (char *)&c
[1], 1);
6472 else if (c
[0] == '.' || c
[0] == ',')
6473 run_script(t
, "hints.focusNextHint();");
6475 case GDK_ISO_Left_Tab
:
6477 cmd_complete(t
, (char *)&c
[1], -1);
6478 else if (c
[0] == '.' || c
[0] == ',')
6479 run_script(t
, "hints.focusPreviousHint();");
6483 if ((search_at
= history_next(&shl
, search_at
))) {
6484 search_at
->line
[0] = c
[0];
6485 gtk_entry_set_text(w
, search_at
->line
);
6486 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6488 } else if (c
[0] == '/') {
6489 if ((search_at
= history_prev(&shl
, search_at
))) {
6490 search_at
->line
[0] = c
[0];
6491 gtk_entry_set_text(w
, search_at
->line
);
6492 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6494 } else if (c
[0] == ':') {
6495 if ((history_at
= history_prev(&chl
, history_at
))) {
6496 history_at
->line
[0] = c
[0];
6497 gtk_entry_set_text(w
, history_at
->line
);
6498 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6504 if ((search_at
= history_next(&shl
, search_at
))) {
6505 search_at
->line
[0] = c
[0];
6506 gtk_entry_set_text(w
, search_at
->line
);
6507 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6509 } else if (c
[0] == '?') {
6510 if ((search_at
= history_prev(&shl
, search_at
))) {
6511 search_at
->line
[0] = c
[0];
6512 gtk_entry_set_text(w
, search_at
->line
);
6513 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6515 } if (c
[0] == ':') {
6516 if ((history_at
= history_next(&chl
, history_at
))) {
6517 history_at
->line
[0] = c
[0];
6518 gtk_entry_set_text(w
, history_at
->line
);
6519 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6524 if (!(!strcmp(c
, ":") || !strcmp(c
, "/") || !strcmp(c
, "?") ||
6525 !strcmp(c
, ".") || !strcmp(c
, ","))) {
6526 /* see if we are doing hinting and reset it */
6527 if (c
[0] == '.' || c
[0] == ',') {
6528 /* recreate hints */
6529 s
= g_strdup_printf("hints.createHints('', "
6530 "'%c');", c
[0] == '.' ? 'f' : 'F');
6542 if (c
!= NULL
&& (c
[0] == '/' || c
[0] == '?'))
6543 webkit_web_view_unmark_text_matches(t
->wv
);
6545 /* no need to cancel hints */
6549 rv
= XT_CB_PASSTHROUGH
;
6555 wv_popup_activ_cb(GtkMenuItem
*menu
, struct tab
*t
)
6557 GtkAction
*a
= NULL
;
6558 GtkClipboard
*clipboard
, *primary
;
6559 const gchar
*name
, *uri
;
6561 a
= gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu
));
6564 name
= gtk_action_get_name(a
);
6566 DNPRINTF(XT_D_CMD
, "wv_popup_activ_cb: tab %d action %s\n",
6570 * context-menu-action-3 copy link location
6571 * context-menu-action-7 copy image address
6572 * context-menu-action-2030 copy video link location
6576 if ((g_strcmp0(name
, "context-menu-action-3") == 0) ||
6577 (g_strcmp0(name
, "context-menu-action-7") == 0) ||
6578 (g_strcmp0(name
, "context-menu-action-2030") == 0)) {
6579 clipboard
= gtk_clipboard_get(GDK_SELECTION_CLIPBOARD
);
6580 primary
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
6581 uri
= gtk_clipboard_wait_for_text(clipboard
);
6584 gtk_clipboard_set_text(primary
, uri
, -1);
6589 wv_popup_cb(WebKitWebView
*wview
, GtkMenu
*menu
, struct tab
*t
)
6593 DNPRINTF(XT_D_CMD
, "wv_popup_cb: tab %d\n", t
->tab_id
);
6595 items
= gtk_container_get_children(GTK_CONTAINER(menu
));
6596 for (l
= items
; l
; l
= l
->next
)
6597 g_signal_connect(l
->data
, "activate",
6598 G_CALLBACK(wv_popup_activ_cb
), t
);
6603 cmd_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
6605 /* popup menu enabled */
6610 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
6613 show_oops(NULL
, "cmd_focusout_cb invalid parameters");
6614 return (XT_CB_PASSTHROUGH
);
6617 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d popup %d\n",
6618 t
->tab_id
, t
->popup
);
6620 /* if popup is enabled don't lose focus */
6623 return (XT_CB_PASSTHROUGH
);
6630 return (XT_CB_PASSTHROUGH
);
6634 cmd_hide_cb(GtkWidget
*w
, struct tab
*t
)
6637 show_oops(NULL
, "%s: invalid parameters", __func__
);
6641 if (show_url
== 0 || t
->focus_wv
)
6644 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6648 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
6651 const gchar
*c
= gtk_entry_get_text(entry
);
6654 show_oops(NULL
, "cmd_activate_cb invalid parameters");
6658 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
6663 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
6664 c
[0] == '.' || c
[0] == ','))
6670 if (c
[0] == '/' || c
[0] == '?') {
6671 /* see if there is a timer pending */
6673 g_source_remove(t
->search_id
);
6678 if (t
->search_text
) {
6679 g_free(t
->search_text
);
6680 t
->search_text
= NULL
;
6683 t
->search_text
= g_strdup(s
);
6685 g_free(global_search
);
6686 global_search
= g_strdup(s
);
6687 t
->search_forward
= c
[0] == '/';
6689 history_add(&shl
, search_file
, s
, &search_history_count
);
6690 } else if (c
[0] == '.' || c
[0] == ',') {
6691 run_script(t
, "hints.fire();");
6692 /* XXX history for link following? */
6693 } else if (c
[0] == ':') {
6694 history_add(&chl
, command_file
, s
, &cmd_history_count
);
6695 /* can't call hide_cmd after cmd_execute */
6706 backward_cb(GtkWidget
*w
, struct tab
*t
)
6711 show_oops(NULL
, "backward_cb invalid parameters");
6715 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
6722 forward_cb(GtkWidget
*w
, struct tab
*t
)
6727 show_oops(NULL
, "forward_cb invalid parameters");
6731 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
6733 a
.i
= XT_NAV_FORWARD
;
6738 home_cb(GtkWidget
*w
, struct tab
*t
)
6741 show_oops(NULL
, "home_cb invalid parameters");
6745 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
6751 stop_cb(GtkWidget
*w
, struct tab
*t
)
6753 WebKitWebFrame
*frame
;
6756 show_oops(NULL
, "stop_cb invalid parameters");
6760 DNPRINTF(XT_D_NAV
, "stop_cb: tab %d\n", t
->tab_id
);
6762 frame
= webkit_web_view_get_main_frame(t
->wv
);
6763 if (frame
== NULL
) {
6764 show_oops(t
, "stop_cb: no frame");
6768 webkit_web_frame_stop_loading(frame
);
6769 abort_favicon_download(t
);
6773 setup_webkit(struct tab
*t
)
6775 if (is_g_object_setting(G_OBJECT(t
->settings
), "enable-dns-prefetching"))
6776 g_object_set(G_OBJECT(t
->settings
), "enable-dns-prefetching",
6777 FALSE
, (char *)NULL
);
6779 warnx("webkit does not have \"enable-dns-prefetching\" property");
6780 g_object_set(G_OBJECT(t
->settings
),
6781 "enable-scripts", enable_scripts
, (char *)NULL
);
6782 g_object_set(G_OBJECT(t
->settings
),
6783 "enable-plugins", enable_plugins
, (char *)NULL
);
6784 g_object_set(G_OBJECT(t
->settings
),
6785 "javascript-can-open-windows-automatically", enable_scripts
,
6787 g_object_set(G_OBJECT(t
->settings
),
6788 "enable-html5-database", FALSE
, (char *)NULL
);
6789 g_object_set(G_OBJECT(t
->settings
),
6790 "enable-html5-local-storage", enable_localstorage
, (char *)NULL
);
6791 g_object_set(G_OBJECT(t
->settings
),
6792 "enable_spell_checking", enable_spell_checking
, (char *)NULL
);
6793 g_object_set(G_OBJECT(t
->settings
),
6794 "spell_checking_languages", spell_check_languages
, (char *)NULL
);
6795 g_object_set(G_OBJECT(t
->settings
),
6796 "enable-developer-extras", TRUE
, (char *)NULL
);
6797 g_object_set(G_OBJECT(t
->wv
),
6798 "full-content-zoom", TRUE
, (char *)NULL
);
6799 g_object_set(G_OBJECT(t
->settings
),
6800 "auto-load-images", auto_load_images
, (char *)NULL
);
6801 if (is_g_object_setting(G_OBJECT(t
->settings
),
6802 "enable-display-of-insecure-content"))
6803 g_object_set(G_OBJECT(t
->settings
),
6804 "enable-display-of-insecure-content",
6805 allow_insecure_content
, (char *)NULL
);
6806 if (is_g_object_setting(G_OBJECT(t
->settings
),
6807 "enable-running-of-insecure-content"))
6808 g_object_set(G_OBJECT(t
->settings
),
6809 "enable-running-of-insecure-content",
6810 allow_insecure_scripts
, (char *)NULL
);
6812 webkit_web_view_set_settings(t
->wv
, t
->settings
);
6816 update_statusbar_position(GtkAdjustment
* adjustment
, gpointer data
)
6818 struct tab
*ti
, *t
= NULL
;
6819 gdouble view_size
, value
, max
;
6822 TAILQ_FOREACH(ti
, &tabs
, entry
)
6823 if (ti
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
6831 if (adjustment
== NULL
)
6832 adjustment
= gtk_scrolled_window_get_vadjustment(
6833 GTK_SCROLLED_WINDOW(t
->browser_win
));
6835 view_size
= gtk_adjustment_get_page_size(adjustment
);
6836 value
= gtk_adjustment_get_value(adjustment
);
6837 max
= gtk_adjustment_get_upper(adjustment
) - view_size
;
6840 position
= g_strdup("All");
6841 else if (value
== max
)
6842 position
= g_strdup("Bot");
6843 else if (value
== 0)
6844 position
= g_strdup("Top");
6846 position
= g_strdup_printf("%d%%", (int) ((value
/ max
) * 100));
6848 gtk_label_set_text(GTK_LABEL(t
->sbe
.position
), position
);
6855 create_window(const gchar
*name
)
6859 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
6860 if (window_maximize
)
6861 gtk_window_maximize(GTK_WINDOW(w
));
6863 gtk_window_set_default_size(GTK_WINDOW(w
), window_width
, window_height
);
6864 gtk_widget_set_name(w
, name
);
6865 gtk_window_set_wmclass(GTK_WINDOW(w
), name
, "Xombrero");
6871 create_browser(struct tab
*t
)
6874 GtkAdjustment
*adjustment
;
6877 show_oops(NULL
, "create_browser invalid parameters");
6881 w
= gtk_scrolled_window_new(NULL
, NULL
);
6882 gtk_widget_set_can_focus(w
, FALSE
);
6883 t
->adjust_h
= gtk_scrolled_window_get_hadjustment(
6884 GTK_SCROLLED_WINDOW(w
));
6885 t
->adjust_v
= gtk_scrolled_window_get_vadjustment(
6886 GTK_SCROLLED_WINDOW(w
));
6887 #if !GTK_CHECK_VERSION(3, 0, 0)
6888 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
6889 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
6893 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
6894 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
6897 t
->settings
= webkit_web_settings_new();
6899 g_object_set(t
->settings
, "default-encoding", encoding
, (char *)NULL
);
6901 t
->stylesheet
= g_strdup(stylesheet
);
6902 t
->load_images
= auto_load_images
;
6905 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w
));
6906 g_signal_connect(G_OBJECT(adjustment
), "value-changed",
6907 G_CALLBACK(update_statusbar_position
), NULL
);
6916 create_kiosk_toolbar(struct tab
*t
)
6918 GtkWidget
*toolbar
= NULL
, *b
;
6920 #if GTK_CHECK_VERSION(3, 0, 0)
6921 b
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
6922 gtk_widget_set_name(GTK_WIDGET(b
), "toolbar");
6924 b
= gtk_hbox_new(FALSE
, 0);
6927 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
6929 /* backward button */
6930 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
6931 gtk_widget_set_sensitive(t
->backward
, FALSE
);
6932 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
6933 G_CALLBACK(backward_cb
), t
);
6934 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, TRUE
, TRUE
, 0);
6936 /* forward button */
6937 t
->forward
= create_button("Forward", GTK_STOCK_GO_FORWARD
, 0);
6938 gtk_widget_set_sensitive(t
->forward
, FALSE
);
6939 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
6940 G_CALLBACK(forward_cb
), t
);
6941 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, TRUE
, TRUE
, 0);
6944 t
->gohome
= create_button("Home", GTK_STOCK_HOME
, 0);
6945 gtk_widget_set_sensitive(t
->gohome
, true);
6946 g_signal_connect(G_OBJECT(t
->gohome
), "clicked",
6947 G_CALLBACK(home_cb
), t
);
6948 gtk_box_pack_start(GTK_BOX(b
), t
->gohome
, TRUE
, TRUE
, 0);
6950 /* create widgets but don't use them */
6951 t
->uri_entry
= gtk_entry_new();
6952 g_signal_connect(G_OBJECT(t
->uri_entry
), "focus-in-event",
6953 G_CALLBACK(entry_focus_cb
), t
);
6954 #if !GTK_CHECK_VERSION(3, 0, 0)
6955 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
6957 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
6958 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
6959 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
6965 create_toolbar(struct tab
*t
)
6967 GtkWidget
*toolbar
= NULL
, *b
;
6969 #if GTK_CHECK_VERSION(3, 0, 0)
6970 b
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
6971 gtk_widget_set_name(GTK_WIDGET(b
), "toolbar");
6973 b
= gtk_hbox_new(FALSE
, 0);
6976 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
6978 /* backward button */
6979 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
6980 gtk_widget_set_sensitive(t
->backward
, FALSE
);
6981 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
6982 G_CALLBACK(backward_cb
), t
);
6983 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, FALSE
, FALSE
, 0);
6985 /* forward button */
6986 t
->forward
= create_button("Forward",GTK_STOCK_GO_FORWARD
, 0);
6987 gtk_widget_set_sensitive(t
->forward
, FALSE
);
6988 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
6989 G_CALLBACK(forward_cb
), t
);
6990 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, FALSE
, FALSE
, 0);
6993 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
6994 gtk_widget_set_sensitive(t
->stop
, FALSE
);
6995 g_signal_connect(G_OBJECT(t
->stop
), "clicked", G_CALLBACK(stop_cb
), t
);
6996 gtk_box_pack_start(GTK_BOX(b
), t
->stop
, FALSE
, FALSE
, 0);
6999 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
7000 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
7001 gtk_widget_set_sensitive(t
->js_toggle
, TRUE
);
7002 g_signal_connect(G_OBJECT(t
->js_toggle
), "clicked",
7003 G_CALLBACK(js_toggle_cb
), t
);
7004 gtk_box_pack_start(GTK_BOX(b
), t
->js_toggle
, FALSE
, FALSE
, 0);
7006 /* toggle proxy button */
7007 t
->proxy_toggle
= create_button("Proxy-Toggle", proxy_uri
?
7008 GTK_STOCK_CONNECT
: GTK_STOCK_DISCONNECT
, 0);
7009 /* override icons */
7011 button_set_file(t
->proxy_toggle
, "torenabled.ico");
7013 button_set_file(t
->proxy_toggle
, "tordisabled.ico");
7014 gtk_widget_set_sensitive(t
->proxy_toggle
, TRUE
);
7015 g_signal_connect(G_OBJECT(t
->proxy_toggle
), "clicked",
7016 G_CALLBACK(proxy_toggle_cb
), t
);
7017 gtk_box_pack_start(GTK_BOX(b
), t
->proxy_toggle
, FALSE
, FALSE
, 0);
7019 t
->uri_entry
= gtk_entry_new();
7020 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
7021 G_CALLBACK(activate_uri_entry_cb
), t
);
7022 g_signal_connect(G_OBJECT(t
->uri_entry
), "key-press-event",
7023 G_CALLBACK(entry_key_cb
), t
);
7024 g_signal_connect(G_OBJECT(t
->uri_entry
), "focus-in-event",
7025 G_CALLBACK(entry_focus_cb
), t
);
7027 gtk_box_pack_start(GTK_BOX(b
), t
->uri_entry
, TRUE
, TRUE
, 0);
7030 t
->search_entry
= gtk_entry_new();
7031 gtk_entry_set_width_chars(GTK_ENTRY(t
->search_entry
), 30);
7032 g_signal_connect(G_OBJECT(t
->search_entry
), "activate",
7033 G_CALLBACK(activate_search_entry_cb
), t
);
7034 g_signal_connect(G_OBJECT(t
->search_entry
), "key-press-event",
7035 G_CALLBACK(entry_key_cb
), t
);
7036 g_signal_connect(G_OBJECT(t
->search_entry
), "focus-in-event",
7037 G_CALLBACK(entry_focus_cb
), t
);
7038 gtk_box_pack_start(GTK_BOX(b
), t
->search_entry
, FALSE
, FALSE
, 0);
7040 #if !GTK_CHECK_VERSION(3, 0, 0)
7041 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
7048 create_buffers(struct tab
*t
)
7050 GtkCellRenderer
*renderer
;
7053 view
= gtk_tree_view_new();
7055 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view
), FALSE
);
7057 renderer
= gtk_cell_renderer_text_new();
7058 gtk_tree_view_insert_column_with_attributes
7059 (GTK_TREE_VIEW(view
), -1, "Id", renderer
, "text", COL_ID
, (char *)NULL
);
7061 renderer
= gtk_cell_renderer_pixbuf_new();
7062 gtk_tree_view_insert_column_with_attributes
7063 (GTK_TREE_VIEW(view
), -1, "Favicon", renderer
, "pixbuf", COL_FAVICON
,
7066 renderer
= gtk_cell_renderer_text_new();
7067 gtk_tree_view_insert_column_with_attributes
7068 (GTK_TREE_VIEW(view
), -1, "Title", renderer
, "text", COL_TITLE
,
7071 gtk_tree_view_set_model
7072 (GTK_TREE_VIEW(view
), GTK_TREE_MODEL(buffers_store
));
7078 row_activated_cb(GtkTreeView
*view
, GtkTreePath
*path
,
7079 GtkTreeViewColumn
*col
, struct tab
*t
)
7084 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
7086 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
,
7089 (GTK_TREE_MODEL(buffers_store
), &iter
, COL_ID
, &id
, -1);
7090 set_current_tab(id
- 1);
7096 /* after tab reordering/creation/removal */
7102 TAILQ_FOREACH(t
, &tabs
, entry
)
7103 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
7107 update_statusbar_tabs(struct tab
*t
)
7109 int tab_id
, max_tab_id
;
7115 tab_id
= gtk_notebook_get_current_page(notebook
);
7117 max_tab_id
= gtk_notebook_get_n_pages(notebook
);
7118 snprintf(s
, sizeof s
, "%d/%d", tab_id
+ 1, max_tab_id
);
7121 t
= get_current_tab();
7124 gtk_label_set_text(GTK_LABEL(t
->sbe
.tabs
), s
);
7127 /* after active tab change */
7129 recolor_compact_tabs(void)
7133 #if !GTK_CHECK_VERSION(3, 0, 0)
7134 GdkColor color_active
, color_inactive
;
7136 gdk_color_parse(XT_COLOR_CT_ACTIVE
, &color_active
);
7137 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color_inactive
);
7139 curid
= gtk_notebook_get_current_page(notebook
);
7141 TAILQ_FOREACH(t
, &tabs
, entry
) {
7142 #if GTK_CHECK_VERSION(3, 0, 0)
7143 if (t
->tab_id
== curid
)
7144 gtk_widget_set_name(t
->tab_elems
.label
, XT_CSS_ACTIVE
);
7146 gtk_widget_set_name(t
->tab_elems
.label
, "");
7148 if (t
->tab_id
== curid
)
7149 gtk_widget_modify_fg(t
->tab_elems
.label
,
7150 GTK_STATE_NORMAL
, &color_active
);
7152 gtk_widget_modify_fg(t
->tab_elems
.label
,
7153 GTK_STATE_NORMAL
, &color_inactive
);
7159 set_current_tab(int page_num
)
7161 buffercmd_abort(get_current_tab());
7162 gtk_notebook_set_current_page(notebook
, page_num
);
7163 recolor_compact_tabs();
7164 update_statusbar_tabs(NULL
);
7168 undo_close_tab_save(struct tab
*t
)
7172 struct undo
*u1
, *u2
;
7174 WebKitWebHistoryItem
*item
;
7176 if ((uri
= get_uri(t
)) == NULL
)
7179 u1
= g_malloc0(sizeof(struct undo
));
7180 u1
->uri
= g_strdup(uri
);
7182 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
7184 m
= webkit_web_back_forward_list_get_forward_length(t
->bfl
);
7185 n
= webkit_web_back_forward_list_get_back_length(t
->bfl
);
7188 /* forward history */
7189 items
= webkit_web_back_forward_list_get_forward_list_with_limit(t
->bfl
, m
);
7193 u1
->history
= g_list_prepend(u1
->history
,
7194 webkit_web_history_item_copy(item
));
7195 items
= g_list_next(items
);
7200 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
7201 u1
->history
= g_list_prepend(u1
->history
,
7202 webkit_web_history_item_copy(item
));
7206 items
= webkit_web_back_forward_list_get_back_list_with_limit(t
->bfl
, n
);
7210 u1
->history
= g_list_prepend(u1
->history
,
7211 webkit_web_history_item_copy(item
));
7212 items
= g_list_next(items
);
7215 TAILQ_INSERT_HEAD(&undos
, u1
, entry
);
7217 if (undo_count
> XT_MAX_UNDO_CLOSE_TAB
) {
7218 u2
= TAILQ_LAST(&undos
, undo_tailq
);
7219 TAILQ_REMOVE(&undos
, u2
, entry
);
7221 g_list_free(u2
->history
);
7230 delete_tab(struct tab
*t
)
7234 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
7239 TAILQ_REMOVE(&tabs
, t
, entry
);
7242 /* Halt all webkit activity. */
7243 abort_favicon_download(t
);
7244 webkit_web_view_stop_loading(t
->wv
);
7246 /* Save the tab, so we can undo the close. */
7247 undo_close_tab_save(t
);
7251 g_source_remove(t
->search_id
);
7255 g_free(t
->session_key
);
7258 bzero(&a
, sizeof a
);
7260 inspector_cmd(t
, &a
);
7262 if (browser_mode
== XT_BM_KIOSK
) {
7263 gtk_widget_destroy(t
->uri_entry
);
7264 gtk_widget_destroy(t
->stop
);
7265 gtk_widget_destroy(t
->js_toggle
);
7268 g_object_unref(t
->completion
);
7270 g_object_unref(t
->item
);
7272 gtk_widget_destroy(t
->tab_elems
.eventbox
);
7273 gtk_widget_destroy(t
->vbox
);
7275 g_free(t
->stylesheet
);
7281 if (TAILQ_EMPTY(&tabs
)) {
7282 if (browser_mode
== XT_BM_KIOSK
)
7283 create_new_tab(home
, NULL
, 1, -1);
7285 create_new_tab(NULL
, NULL
, 1, -1);
7288 /* recreate session */
7289 if (session_autosave
) {
7290 bzero(&a
, sizeof a
);
7292 save_tabs(NULL
, &a
);
7296 recolor_compact_tabs();
7300 update_statusbar_zoom(struct tab
*t
)
7303 char s
[16] = { '\0' };
7305 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
7306 if ((zoom
<= 0.99 || zoom
>= 1.01))
7307 snprintf(s
, sizeof s
, "%d%%", (int)(zoom
* 100));
7308 gtk_label_set_text(GTK_LABEL(t
->sbe
.zoom
), s
);
7312 setzoom_webkit(struct tab
*t
, int adjust
)
7314 #define XT_ZOOMPERCENT 0.04
7319 show_oops(NULL
, "setzoom_webkit invalid parameters");
7323 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
7324 if (adjust
== XT_ZOOM_IN
)
7325 zoom
+= XT_ZOOMPERCENT
;
7326 else if (adjust
== XT_ZOOM_OUT
)
7327 zoom
-= XT_ZOOMPERCENT
;
7328 else if (adjust
> 0)
7329 zoom
= default_zoom_level
+ adjust
/ 100.0 - 1.0;
7331 show_oops(t
, "setzoom_webkit invalid zoom value");
7335 if (zoom
< XT_ZOOMPERCENT
)
7336 zoom
= XT_ZOOMPERCENT
;
7337 g_object_set(G_OBJECT(t
->wv
), "zoom-level", zoom
, (char *)NULL
);
7338 update_statusbar_zoom(t
);
7342 tab_clicked_cb(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
7344 struct tab
*t
= (struct tab
*) data
;
7346 DNPRINTF(XT_D_TAB
, "tab_clicked_cb: tab: %d\n", t
->tab_id
);
7348 switch (event
->button
) {
7350 set_current_tab(t
->tab_id
);
7361 append_tab(struct tab
*t
)
7366 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
7367 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
7375 sbe
= gtk_label_new(NULL
);
7376 gtk_widget_set_can_focus(GTK_WIDGET(sbe
), FALSE
);
7377 gtk_widget_modify_font(GTK_WIDGET(sbe
), statusbar_font
);
7382 add_sbe(GtkWidget
*box
, char flag
, int *used
, GtkWidget
*sbe
)
7384 if (box
== NULL
|| used
== NULL
|| sbe
== NULL
) {
7385 DPRINTF("%s: invalid parameters", __func__
);
7390 warnx("flag \"%c\" specified more than "
7391 "once in statusbar_elems\n", flag
);
7395 gtk_box_pack_start(GTK_BOX(box
), sbe
, FALSE
, FALSE
, 0);
7402 statusbar_create(struct tab
*t
)
7404 GtkWidget
*box
; /* container for statusbar elems */
7407 int sbe_P
= 0, sbe_B
= 0, sbe_Z
= 0, sbe_T
= 0,
7409 #if !GTK_CHECK_VERSION(3, 0, 0)
7414 DPRINTF("%s: invalid parameters", __func__
);
7418 #if GTK_CHECK_VERSION(3, 0, 0)
7419 t
->statusbar
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7420 box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7421 gtk_widget_set_name(GTK_WIDGET(t
->statusbar
), "statusbar");
7423 t
->statusbar
= gtk_hbox_new(FALSE
, 0);
7424 box
= gtk_hbox_new(FALSE
, 0);
7426 t
->sbe
.ebox
= gtk_event_box_new();
7428 gtk_widget_set_can_focus(GTK_WIDGET(t
->statusbar
), FALSE
);
7429 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.ebox
), FALSE
);
7430 gtk_widget_set_can_focus(GTK_WIDGET(box
), FALSE
);
7432 gtk_box_set_spacing(GTK_BOX(box
), 10);
7433 gtk_box_pack_start(GTK_BOX(t
->statusbar
), t
->sbe
.ebox
, TRUE
, TRUE
, 0);
7434 gtk_container_add(GTK_CONTAINER(t
->sbe
.ebox
), box
);
7436 /* create these widgets only if specified in statusbar_elems */
7437 t
->sbe
.uri
= gtk_entry_new();
7438 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.uri
), FALSE
);
7439 gtk_widget_modify_font(GTK_WIDGET(t
->sbe
.uri
), statusbar_font
);
7440 #if !GTK_CHECK_VERSION(3, 0, 0)
7441 gtk_entry_set_inner_border(GTK_ENTRY(t
->sbe
.uri
), NULL
);
7442 gtk_entry_set_has_frame(GTK_ENTRY(t
->sbe
.uri
), FALSE
);
7444 t
->sbe
.position
= create_sbe();
7445 t
->sbe
.zoom
= create_sbe();
7446 t
->sbe
.buffercmd
= create_sbe();
7447 t
->sbe
.tabs
= create_sbe();
7448 t
->sbe
.proxy
= create_sbe();
7450 #if GTK_CHECK_VERSION(3, 0, 0)
7451 statusbar_modify_attr(t
, XT_CSS_NORMAL
);
7453 statusbar_modify_attr(t
, XT_COLOR_WHITE
, XT_COLOR_BLACK
);
7456 gtk_box_pack_start(GTK_BOX(box
), t
->sbe
.uri
, TRUE
, TRUE
, 0);
7459 * gtk widgets cannot be added to a box twice. The sbe_* variables
7462 for (p
= statusbar_elems
; *p
!= '\0'; p
++) {
7465 #if GTK_CHECK_VERSION(3, 0, 0)
7466 sep
= gtk_separator_new(GTK_ORIENTATION_VERTICAL
);
7468 sep
= gtk_vseparator_new();
7469 gdk_color_parse(XT_COLOR_SB_SEPARATOR
, &color
);
7470 gtk_widget_modify_bg(sep
, GTK_STATE_NORMAL
, &color
);
7472 gtk_box_pack_start(GTK_BOX(box
), sep
, FALSE
, FALSE
, 0);
7475 add_sbe(box
, *p
, &sbe_P
, t
->sbe
.position
);
7478 add_sbe(box
, *p
, &sbe_B
, t
->sbe
.buffercmd
);
7481 add_sbe(box
, *p
, &sbe_Z
, t
->sbe
.zoom
);
7484 add_sbe(box
, *p
, &sbe_T
, t
->sbe
.tabs
);
7487 if (add_sbe(box
, *p
, &sbe_p
, t
->sbe
.proxy
) == 0)
7490 GTK_ENTRY(t
->sbe
.proxy
), "proxy");
7493 warnx("illegal flag \"%c\" in statusbar_elems\n", *p
);
7498 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar
, FALSE
, FALSE
, 0);
7504 create_new_tab(char *title
, struct undo
*u
, int focus
, int position
)
7509 WebKitWebHistoryItem
*item
;
7512 #if !GTK_CHECK_VERSION(3, 0, 0)
7516 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
7518 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
7519 if (single_instance
) {
7521 "create_new_tab: new tab rejected\n");
7524 sv
[0] = start_argv
[0];
7526 sv
[2] = (char *)NULL
;
7527 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
,
7528 NULL
, NULL
, NULL
, NULL
))
7529 show_oops(NULL
, "%s: could not spawn process",
7534 t
= g_malloc0(sizeof *t
);
7536 if (title
== NULL
) {
7537 title
= "(untitled)";
7541 #if GTK_CHECK_VERSION(3, 0, 0)
7542 t
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
7543 b
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7544 gtk_widget_set_name(t
->vbox
, "vbox");
7546 t
->vbox
= gtk_vbox_new(FALSE
, 0);
7547 b
= gtk_hbox_new(FALSE
, 0);
7549 gtk_widget_set_can_focus(t
->vbox
, FALSE
);
7551 /* label + button for tab */
7553 gtk_widget_set_can_focus(t
->tab_content
, FALSE
);
7555 t
->user_agent_id
= 0;
7556 t
->http_accept_id
= 0;
7558 #if WEBKIT_CHECK_VERSION(1, 5, 0)
7562 #if GTK_CHECK_VERSION(2, 20, 0)
7563 t
->spinner
= gtk_spinner_new();
7565 t
->label
= gtk_label_new(title
);
7566 bb
= create_button("Close", GTK_STOCK_CLOSE
, 1);
7567 gtk_label_set_max_width_chars(GTK_LABEL(t
->label
), 20);
7568 gtk_label_set_ellipsize(GTK_LABEL(t
->label
), PANGO_ELLIPSIZE_END
);
7569 gtk_label_set_line_wrap(GTK_LABEL(t
->label
), FALSE
);
7570 gtk_widget_set_size_request(b
, 130, 0);
7573 * this is a total hack and most likely breaks with other styles but
7574 * is necessary so the text doesn't bounce around when the spinner is
7577 #if GTK_CHECK_VERSION(3, 0, 0)
7578 gtk_widget_set_size_request(t
->label
, 95, 0);
7580 gtk_widget_set_size_request(t
->label
, 100, 0);
7583 gtk_box_pack_start(GTK_BOX(b
), bb
, FALSE
, FALSE
, 0);
7584 gtk_box_pack_start(GTK_BOX(b
), t
->label
, FALSE
, FALSE
, 0);
7585 #if GTK_CHECK_VERSION(2, 20, 0)
7586 gtk_box_pack_end(GTK_BOX(b
), t
->spinner
, FALSE
, FALSE
, 0);
7590 if (browser_mode
== XT_BM_KIOSK
) {
7591 t
->toolbar
= create_kiosk_toolbar(t
);
7592 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
7595 t
->toolbar
= create_toolbar(t
);
7596 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
7604 t
->browser_win
= create_browser(t
);
7605 set_scrollbar_visibility(t
, show_scrollbars
);
7606 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
7608 /* oops message for user feedback */
7609 t
->oops
= gtk_entry_new();
7610 gtk_entry_set_inner_border(GTK_ENTRY(t
->oops
), NULL
);
7611 gtk_entry_set_has_frame(GTK_ENTRY(t
->oops
), FALSE
);
7612 gtk_widget_set_can_focus(GTK_WIDGET(t
->oops
), FALSE
);
7613 #if GTK_CHECK_VERSION(3, 0, 0)
7614 gtk_widget_set_name(t
->oops
, XT_CSS_RED
);
7616 gdk_color_parse(XT_COLOR_RED
, &color
);
7617 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
7619 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
7620 gtk_widget_modify_font(GTK_WIDGET(t
->oops
), oops_font
);
7623 t
->cmd
= gtk_entry_new();
7624 g_signal_connect(G_OBJECT(t
->cmd
), "focus-in-event",
7625 G_CALLBACK(entry_focus_cb
), t
);
7626 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
7627 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
7628 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
7629 gtk_widget_modify_font(GTK_WIDGET(t
->cmd
), cmd_font
);
7632 statusbar_create(t
);
7635 t
->buffers
= create_buffers(t
);
7636 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->buffers
, FALSE
, FALSE
, 0);
7638 /* xtp meaning is normal by default */
7639 set_normal_tab_meaning(t
);
7641 /* set empty favicon */
7642 xt_icon_from_name(t
, "text-html");
7644 /* and show it all */
7645 gtk_widget_show_all(b
);
7646 gtk_widget_show_all(t
->vbox
);
7649 gtk_widget_hide(t
->backward
);
7650 gtk_widget_hide(t
->forward
);
7651 gtk_widget_hide(t
->stop
);
7652 gtk_widget_hide(t
->js_toggle
);
7654 if (!fancy_bar
|| (search_string
== NULL
|| strlen(search_string
) == 0))
7655 gtk_widget_hide(t
->search_entry
);
7656 if (http_proxy
== NULL
&& http_proxy_save
== NULL
)
7657 gtk_widget_hide(t
->proxy_toggle
);
7659 /* compact tab bar */
7660 t
->tab_elems
.label
= gtk_label_new(title
);
7661 t
->tab_elems
.favicon
= gtk_image_new();
7663 t
->tab_elems
.eventbox
= gtk_event_box_new();
7664 gtk_widget_set_name(t
->tab_elems
.eventbox
, "compact_tab");
7665 #if GTK_CHECK_VERSION(3, 0, 0)
7666 t
->tab_elems
.box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
7668 gtk_label_set_ellipsize(GTK_LABEL(t
->tab_elems
.label
),
7669 PANGO_ELLIPSIZE_END
);
7670 gtk_widget_override_font(t
->tab_elems
.label
, tabbar_font
);
7671 gtk_widget_set_halign(t
->tab_elems
.label
, GTK_ALIGN_START
);
7672 gtk_widget_set_valign(t
->tab_elems
.label
, GTK_ALIGN_START
);
7674 t
->tab_elems
.box
= gtk_hbox_new(FALSE
, 0);
7676 gtk_label_set_width_chars(GTK_LABEL(t
->tab_elems
.label
), 1);
7677 gtk_misc_set_alignment(GTK_MISC(t
->tab_elems
.label
), 0.0, 0.0);
7678 gtk_misc_set_padding(GTK_MISC(t
->tab_elems
.label
), 4.0, 4.0);
7679 gtk_widget_modify_font(GTK_WIDGET(t
->tab_elems
.label
), tabbar_font
);
7681 gdk_color_parse(XT_COLOR_CT_BACKGROUND
, &color
);
7682 gtk_widget_modify_bg(t
->tab_elems
.eventbox
, GTK_STATE_NORMAL
, &color
);
7683 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
7684 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
, &color
);
7687 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.favicon
, FALSE
,
7689 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.label
, TRUE
,
7691 gtk_container_add(GTK_CONTAINER(t
->tab_elems
.eventbox
),
7694 gtk_box_pack_start(GTK_BOX(tab_bar_box
), t
->tab_elems
.eventbox
, TRUE
,
7696 gtk_widget_show_all(t
->tab_elems
.eventbox
);
7698 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
7701 id
= position
>= 0 ? position
:
7702 gtk_notebook_get_current_page(notebook
) + 1;
7703 if (id
> gtk_notebook_get_n_pages(notebook
))
7706 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
7707 gtk_notebook_insert_page(notebook
, t
->vbox
, b
, id
);
7708 gtk_box_reorder_child(GTK_BOX(tab_bar_box
),
7709 t
->tab_elems
.eventbox
, id
);
7714 #if GTK_CHECK_VERSION(2, 20, 0)
7715 /* turn spinner off if we are a new tab without uri */
7717 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
7718 gtk_widget_hide(t
->spinner
);
7721 /* make notebook tabs reorderable */
7722 gtk_notebook_set_tab_reorderable(notebook
, t
->vbox
, TRUE
);
7724 /* compact tabs clickable */
7725 g_signal_connect(G_OBJECT(t
->tab_elems
.eventbox
),
7726 "button_press_event", G_CALLBACK(tab_clicked_cb
), t
);
7728 g_object_connect(G_OBJECT(t
->cmd
),
7729 "signal::button-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
7730 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb
), t
,
7731 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
7732 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb
), t
,
7733 "signal::activate", G_CALLBACK(cmd_activate_cb
), t
,
7734 "signal::populate-popup", G_CALLBACK(cmd_popup_cb
), t
,
7735 "signal::hide", G_CALLBACK(cmd_hide_cb
), t
,
7738 /* reuse wv_button_cb to hide oops */
7739 g_object_connect(G_OBJECT(t
->oops
),
7740 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7743 g_signal_connect(t
->buffers
,
7744 "row-activated", G_CALLBACK(row_activated_cb
), t
);
7745 g_object_connect(G_OBJECT(t
->buffers
),
7746 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
, (char *)NULL
);
7748 g_object_connect(G_OBJECT(t
->wv
),
7749 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
,
7750 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb
), t
,
7751 "signal::download-requested", G_CALLBACK(webview_download_cb
), t
,
7752 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), t
,
7753 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7754 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7755 "signal::resource-request-starting", G_CALLBACK(webview_rrs_cb
), t
,
7756 "signal::create-web-view", G_CALLBACK(webview_cwv_cb
), t
,
7757 "signal::close-web-view", G_CALLBACK(webview_closewv_cb
), t
,
7758 "signal::event", G_CALLBACK(webview_event_cb
), t
,
7759 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb
), t
,
7760 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7761 "signal::button_release_event", G_CALLBACK(wv_release_button_cb
), t
,
7762 "signal::populate-popup", G_CALLBACK(wv_popup_cb
), t
,
7764 g_signal_connect(t
->wv
,
7765 "notify::load-status", G_CALLBACK(notify_load_status_cb
), t
);
7766 g_signal_connect(t
->wv
,
7767 "notify::title", G_CALLBACK(notify_title_cb
), t
);
7768 t
->progress_handle
= g_signal_connect(t
->wv
,
7769 "notify::progress", G_CALLBACK(webview_progress_changed_cb
), t
);
7771 /* hijack the unused keys as if we were the browser */
7772 //g_object_connect(G_OBJECT(t->toolbar),
7773 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
7776 g_signal_connect(G_OBJECT(bb
), "button_press_event",
7777 G_CALLBACK(tab_close_cb
), t
);
7780 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
7781 /* restore the tab's history */
7782 if (u
&& u
->history
) {
7786 webkit_web_back_forward_list_add_item(t
->bfl
, item
);
7787 items
= g_list_next(items
);
7790 item
= g_list_nth_data(u
->history
, u
->back
);
7792 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
7795 g_list_free(u
->history
);
7797 webkit_web_back_forward_list_clear(t
->bfl
);
7799 /* check and show url and statusbar */
7800 url_set_visibility();
7801 statusbar_set_visibility();
7804 set_current_tab(t
->tab_id
);
7805 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
7809 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
7813 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
7820 if (userstyle_global
)
7823 recolor_compact_tabs();
7824 setzoom_webkit(t
, XT_ZOOM_NORMAL
);
7829 notebook_switchpage_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7835 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
7837 if (gtk_notebook_get_current_page(notebook
) == -1)
7840 TAILQ_FOREACH(t
, &tabs
, entry
) {
7841 if (t
->tab_id
== pn
) {
7842 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
7845 uri
= get_title(t
, TRUE
);
7846 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
7853 /* can't use focus_webview here */
7854 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
7856 update_statusbar_tabs(t
);
7863 notebook_pagereordered_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7866 struct tab
*t
= NULL
, *tt
;
7870 TAILQ_FOREACH(tt
, &tabs
, entry
)
7871 if (tt
->tab_id
== pn
) {
7877 DNPRINTF(XT_D_TAB
, "page_reordered_cb: tab: %d\n", t
->tab_id
);
7879 gtk_box_reorder_child(GTK_BOX(tab_bar_box
), t
->tab_elems
.eventbox
,
7882 update_statusbar_tabs(t
);
7886 menuitem_response(struct tab
*t
)
7888 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
7892 destroy_menu(GtkMenuShell
*m
, void *notused
)
7894 gtk_widget_destroy(GTK_WIDGET(m
));
7895 return (XT_CB_PASSTHROUGH
);
7899 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
7901 GtkWidget
*menu
= NULL
, *menu_items
;
7902 GdkEventButton
*bevent
;
7903 struct tab
**stabs
= NULL
;
7907 if (event
->type
== GDK_BUTTON_PRESS
) {
7908 bevent
= (GdkEventButton
*) event
;
7909 menu
= gtk_menu_new();
7911 num_tabs
= sort_tabs_by_page_num(&stabs
);
7912 for (i
= 0; i
< num_tabs
; ++i
) {
7913 if (stabs
[i
] == NULL
)
7915 if ((uri
= get_uri(stabs
[i
])) == NULL
)
7916 /* XXX make sure there is something to print */
7917 /* XXX add gui pages in here to look purdy */
7919 menu_items
= gtk_menu_item_new_with_label(uri
);
7920 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), menu_items
);
7921 gtk_widget_show(menu_items
);
7923 g_signal_connect_swapped(menu_items
,
7924 "activate", G_CALLBACK(menuitem_response
),
7925 (gpointer
)stabs
[i
]);
7929 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
,
7930 bevent
->button
, bevent
->time
);
7932 g_object_connect(G_OBJECT(menu
),
7933 "signal::selection-done", G_CALLBACK(destroy_menu
), NULL
,
7936 return (TRUE
/* eat event */);
7939 return (FALSE
/* propagate */);
7943 icon_size_map(int iconsz
)
7945 if (iconsz
<= GTK_ICON_SIZE_INVALID
||
7946 iconsz
> GTK_ICON_SIZE_DIALOG
)
7947 return (GTK_ICON_SIZE_SMALL_TOOLBAR
);
7953 create_button(char *name
, char *stockid
, int size
)
7955 GtkWidget
*button
, *image
;
7957 #if !GTK_CHECK_VERSION(3, 0, 0)
7961 #if !GTK_CHECK_VERSION(3, 0, 0)
7962 newstyle
= g_strdup_printf(
7963 "style \"%s-style\"\n"
7965 " GtkWidget::focus-padding = 0\n"
7966 " GtkWidget::focus-line-width = 0\n"
7970 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
7971 gtk_rc_parse_string(newstyle
);
7974 button
= gtk_button_new();
7975 gtk_widget_set_can_focus(button
, FALSE
);
7976 gtk_button_set_focus_on_click(GTK_BUTTON(button
), FALSE
);
7977 gtk_icon_size
= icon_size_map(size
? size
: icon_size
);
7979 image
= gtk_image_new_from_stock(stockid
, gtk_icon_size
);
7980 gtk_container_set_border_width(GTK_CONTAINER(button
), 1);
7981 gtk_container_add(GTK_CONTAINER(button
), GTK_WIDGET(image
));
7982 gtk_widget_set_name(button
, name
);
7983 gtk_button_set_relief(GTK_BUTTON(button
), GTK_RELIEF_NONE
);
7989 button_set_file(GtkWidget
*button
, char *filename
)
7992 char file
[PATH_MAX
];
7994 snprintf(file
, sizeof file
, "%s" PS
"%s", resource_dir
, filename
);
7995 image
= gtk_image_new_from_file(file
);
7996 gtk_button_set_image(GTK_BUTTON(button
), image
);
8000 button_set_stockid(GtkWidget
*button
, char *stockid
)
8004 image
= gtk_image_new_from_stock(stockid
, icon_size_map(icon_size
));
8005 gtk_button_set_image(GTK_BUTTON(button
), image
);
8014 char file
[PATH_MAX
];
8016 #if !GTK_CHECK_VERSION(3, 0, 0)
8020 #if GTK_CHECK_VERSION(3, 0, 0)
8021 vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
8023 vbox
= gtk_vbox_new(FALSE
, 0);
8025 gtk_box_set_spacing(GTK_BOX(vbox
), 0);
8026 gtk_widget_set_can_focus(vbox
, FALSE
);
8027 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
8028 #if !GTK_CHECK_VERSION(3, 0, 0)
8029 /* XXX seems to be needed with gtk+2 */
8030 g_object_set(G_OBJECT(notebook
), "tab-border", 0, NULL
);
8032 gtk_notebook_set_scrollable(notebook
, TRUE
);
8033 gtk_notebook_set_show_border(notebook
, FALSE
);
8034 gtk_widget_set_can_focus(GTK_WIDGET(notebook
), FALSE
);
8036 abtn
= gtk_button_new();
8037 gtk_widget_set_can_focus(abtn
, FALSE
);
8038 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
8039 gtk_widget_set_name(abtn
, "Arrow");
8040 gtk_container_add(GTK_CONTAINER(abtn
), arrow
);
8041 gtk_widget_set_size_request(abtn
, -1, 20);
8043 #if GTK_CHECK_VERSION(2, 20, 0)
8044 gtk_notebook_set_action_widget(notebook
, abtn
, GTK_PACK_END
);
8046 /* compact tab bar */
8047 tab_bar
= gtk_event_box_new();
8048 #if GTK_CHECK_VERSION(3, 0, 0)
8049 gtk_widget_set_name(tab_bar
, "tab_bar");
8050 tab_bar_box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
8052 gdk_color_parse(XT_COLOR_CT_SEPARATOR
, &color
);
8053 gtk_widget_modify_bg(tab_bar
, GTK_STATE_NORMAL
, &color
);
8054 tab_bar_box
= gtk_hbox_new(TRUE
, 0);
8056 gtk_container_add(GTK_CONTAINER(tab_bar
), tab_bar_box
);
8057 gtk_box_set_homogeneous(GTK_BOX(tab_bar_box
), TRUE
);
8058 gtk_box_set_spacing(GTK_BOX(tab_bar_box
), 2);
8060 gtk_box_pack_start(GTK_BOX(vbox
), tab_bar
, FALSE
, FALSE
, 0);
8061 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
8063 g_object_connect(G_OBJECT(notebook
),
8064 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb
), NULL
,
8066 g_object_connect(G_OBJECT(notebook
),
8067 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb
),
8068 NULL
, (char *)NULL
);
8069 g_signal_connect(G_OBJECT(abtn
), "button_press_event",
8070 G_CALLBACK(arrow_cb
), NULL
);
8072 main_window
= create_window("xombrero");
8073 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
8074 g_signal_connect(G_OBJECT(main_window
), "delete_event",
8075 G_CALLBACK(gtk_main_quit
), NULL
);
8076 #if GTK_CHECK_VERSION(3, 0, 0)
8077 gtk_window_set_has_resize_grip(GTK_WINDOW(main_window
), FALSE
);
8081 for (i
= 0; i
< LENGTH(icons
); i
++) {
8082 snprintf(file
, sizeof file
, "%s" PS
"%s", resource_dir
, icons
[i
]);
8083 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
8084 l
= g_list_append(l
, pb
);
8086 gtk_window_set_default_icon_list(l
);
8088 gtk_widget_show_all(abtn
);
8089 gtk_widget_show_all(main_window
);
8090 notebook_tab_set_visibility();
8093 #ifndef XT_SOCKET_DISABLE
8095 send_cmd_to_socket(char *cmd
)
8098 struct sockaddr_un sa
;
8100 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
8101 warnx("%s: socket", __func__
);
8105 sa
.sun_family
= AF_UNIX
;
8106 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
8107 work_dir
, XT_SOCKET_FILE
);
8110 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
8111 warnx("%s: connect", __func__
);
8115 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
8116 warnx("%s: send", __func__
);
8127 socket_watcher(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
8130 char str
[XT_MAX_URL_LENGTH
];
8131 socklen_t t
= sizeof(struct sockaddr_un
);
8132 struct sockaddr_un sa
;
8137 gint fd
= g_io_channel_unix_get_fd(source
);
8139 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
8144 if (getpeereid(s
, &uid
, &gid
) == -1) {
8148 if (uid
!= getuid() || gid
!= getgid()) {
8149 warnx("unauthorized user");
8155 warnx("not a valid user");
8159 n
= recv(s
, str
, sizeof(str
), 0);
8163 tt
= get_current_tab();
8164 cmd_execute(tt
, str
);
8172 struct sockaddr_un sa
;
8174 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
8175 warn("is_running: socket");
8179 sa
.sun_family
= AF_UNIX
;
8180 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
8181 work_dir
, XT_SOCKET_FILE
);
8184 /* connect to see if there is a listener */
8185 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
8186 rv
= 0; /* not running */
8188 rv
= 1; /* already running */
8199 struct sockaddr_un sa
;
8201 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
8202 warn("build_socket: socket");
8206 sa
.sun_family
= AF_UNIX
;
8207 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
8208 work_dir
, XT_SOCKET_FILE
);
8211 /* connect to see if there is a listener */
8212 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
8213 /* no listener so we will */
8214 unlink(sa
.sun_path
);
8216 if (bind(s
, (struct sockaddr
*)&sa
, len
) == -1) {
8217 warn("build_socket: bind");
8221 if (listen(s
, 1) == -1) {
8222 warn("build_socket: listen");
8240 if (stat(dir
, &sb
)) {
8241 #if defined __MINGW32__
8242 if (mkdir(dir
) == -1)
8244 if (mkdir(dir
, S_IRWXU
) == -1)
8246 err(1, "mkdir %s", dir
);
8248 err(1, "stat %s", dir
);
8250 if (S_ISDIR(sb
.st_mode
) == 0)
8251 errx(1, "%s not a dir", dir
);
8252 #if !defined __MINGW32__
8253 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
8254 warnx("fixing invalid permissions on %s", dir
);
8255 if (chmod(dir
, S_IRWXU
) == -1)
8256 err(1, "chmod %s", dir
);
8265 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
8269 #if GTK_CHECK_VERSION(3, 0, 0)
8273 GtkCssProvider
*provider
;
8274 GdkDisplay
*display
;
8277 char path
[PATH_MAX
];
8278 #if defined __MINGW32__
8279 GtkCssProvider
*windows_hacks
;
8282 provider
= gtk_css_provider_new();
8283 display
= gdk_display_get_default();
8284 screen
= gdk_display_get_default_screen(display
);
8285 snprintf(path
, sizeof path
, "%s" PS
"%s", resource_dir
, XT_CSS_FILE
);
8286 file
= g_file_new_for_path(path
);
8287 gtk_css_provider_load_from_file(provider
, file
, NULL
);
8288 gtk_style_context_add_provider_for_screen(screen
,
8289 GTK_STYLE_PROVIDER(provider
),
8290 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
8291 #if defined __MINGW32__
8292 windows_hacks
= gtk_css_provider_new();
8293 gtk_css_provider_load_from_data(windows_hacks
,
8295 " border-width: 0px;\n"
8297 gtk_style_context_add_provider_for_screen(screen
,
8298 GTK_STYLE_PROVIDER(windows_hacks
),
8299 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
8301 g_object_unref(file
);
8308 startpage_add("<b>Welcome to xombrero %s!</b><p>", version
);
8309 startpage_add("Details at "
8310 "<a href=https://opensource.conformal.com/wiki/xombrero>xombrero "
8311 "wiki page</a><p>");
8315 main(int argc
, char **argv
)
8318 int c
, optn
= 0, opte
= 0, focus
= 1;
8319 char conf
[PATH_MAX
] = { '\0' };
8320 char file
[PATH_MAX
];
8321 char sodversion
[32];
8322 char *env_proxy
= NULL
;
8327 start_argv
= (char * const *)argv
;
8333 gtk_init(&argc
, &argv
);
8335 gnutls_global_init();
8337 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
8340 RB_INIT(&downloads
);
8347 TAILQ_INIT(&sessions
);
8350 TAILQ_INIT(&aliases
);
8361 TAILQ_INIT(&force_https
);
8364 #ifndef XT_RESOURCE_LIMITS_DISABLE
8368 GIOChannel
*channel
;
8370 /* fiddle with ulimits */
8371 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
8374 /* just use them all */
8376 rlp
.rlim_cur
= OPEN_MAX
;
8378 rlp
.rlim_cur
= rlp
.rlim_max
;
8380 if (setrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
8382 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
8384 else if (rlp
.rlim_cur
< 1024)
8385 startpage_add("%s requires at least 1024 "
8386 "(2048 recommended) file " "descriptors, "
8387 "currently it has up to %d available",
8388 __progname
, rlp
.rlim_cur
);
8392 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
8401 #ifdef XOMBRERO_BUILDSTR
8402 errx(0 , "Version: %s Build: %s",
8403 version
, XOMBRERO_BUILDSTR
);
8405 errx(0 , "Version: %s", version
);
8409 strlcpy(conf
, optarg
, sizeof(conf
));
8412 strlcpy(named_session
, optarg
, sizeof(named_session
));
8433 pwd
= getpwuid(getuid());
8435 errx(1, "invalid user %d", getuid());
8437 /* set download dir */
8438 if (strlen(download_dir
) == 0)
8439 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
8441 /* compile buffer command regexes */
8444 /* set default dynamic string settings */
8445 home
= g_strdup(XT_DS_HOME
);
8446 search_string
= g_strdup(XT_DS_SEARCH_STRING
);
8447 strlcpy(runtime_settings
, "runtime", sizeof runtime_settings
);
8448 cmd_font_name
= g_strdup(XT_DS_CMD_FONT_NAME
);
8449 oops_font_name
= g_strdup(XT_DS_OOPS_FONT_NAME
);
8450 statusbar_font_name
= g_strdup(XT_DS_STATUSBAR_FONT_NAME
);
8451 tabbar_font_name
= g_strdup(XT_DS_TABBAR_FONT_NAME
);
8452 statusbar_elems
= g_strdup("BP");
8453 spell_check_languages
= g_strdup(XT_DS_SPELL_CHECK_LANGUAGES
);
8454 encoding
= g_strdup(XT_DS_ENCODING
);
8455 spell_check_languages
= g_strdup(XT_DS_SPELL_CHECK_LANGUAGES
);
8456 path
= g_strdup_printf("%s" PS
"style.css", resource_dir
);
8457 userstyle
= g_filename_to_uri(path
, NULL
, NULL
);
8459 stylesheet
= g_strdup(userstyle
);
8461 /* set statically allocated (struct special) settings */
8462 if (strlen(default_script
) == 0)
8463 expand_tilde(default_script
, sizeof default_script
,
8464 XT_DS_DEFAULT_SCRIPT
);
8465 if (strlen(ssl_ca_file
) == 0)
8466 expand_tilde(ssl_ca_file
, sizeof ssl_ca_file
,
8470 session
= webkit_get_default_session();
8472 /* read config file */
8473 if (strlen(conf
) == 0)
8474 snprintf(conf
, sizeof conf
, "%s" PS
".%s",
8475 pwd
->pw_dir
, XT_CONF_FILE
);
8476 config_parse(conf
, 0);
8478 /* read preloaded HSTS list */
8479 if (preload_strict_transport
) {
8480 snprintf(conf
, sizeof conf
, "%s" PS
"%s",
8481 resource_dir
, XT_HSTS_PRELOAD_FILE
);
8482 config_parse(conf
, 0);
8485 /* check whether to read in a crapton of additional http headers */
8486 if (anonymize_headers
) {
8487 snprintf(conf
, sizeof conf
, "%s" PS
"%s",
8488 resource_dir
, XT_USER_AGENT_FILE
);
8489 config_parse(conf
, 0);
8490 snprintf(conf
, sizeof conf
, "%s" PS
"%s",
8491 resource_dir
, XT_HTTP_ACCEPT_FILE
);
8492 config_parse(conf
, 0);
8496 cmd_font
= pango_font_description_from_string(cmd_font_name
);
8497 oops_font
= pango_font_description_from_string(oops_font_name
);
8498 statusbar_font
= pango_font_description_from_string(statusbar_font_name
);
8499 tabbar_font
= pango_font_description_from_string(tabbar_font_name
);
8501 /* working directory */
8502 if (strlen(work_dir
) == 0)
8503 snprintf(work_dir
, sizeof work_dir
, "%s" PS
"%s",
8504 pwd
->pw_dir
, XT_DIR
);
8507 /* icon cache dir */
8508 snprintf(cache_dir
, sizeof cache_dir
, "%s" PS
"%s", work_dir
, XT_CACHE_DIR
);
8512 snprintf(certs_dir
, sizeof certs_dir
, "%s" PS
"%s", work_dir
, XT_CERT_DIR
);
8515 /* cert changes dir */
8516 snprintf(certs_cache_dir
, sizeof certs_cache_dir
, "%s" PS
"%s",
8517 work_dir
, XT_CERT_CACHE_DIR
);
8518 xxx_dir(certs_cache_dir
);
8521 snprintf(sessions_dir
, sizeof sessions_dir
, "%s" PS
"%s",
8522 work_dir
, XT_SESSIONS_DIR
);
8523 xxx_dir(sessions_dir
);
8526 snprintf(js_dir
, sizeof js_dir
, "%s" PS
"%s", work_dir
, XT_JS_DIR
);
8530 snprintf(temp_dir
, sizeof temp_dir
, "%s" PS
"%s", work_dir
, XT_TEMP_DIR
);
8533 /* runtime settings that can override config file */
8534 if (runtime_settings
[0] != '\0')
8535 config_parse(runtime_settings
, 1);
8538 if (!strcmp(download_dir
, pwd
->pw_dir
))
8539 strlcat(download_dir
, PS
"downloads", sizeof download_dir
);
8540 xxx_dir(download_dir
);
8542 /* first start file */
8543 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_SOD_FILE
);
8544 if (stat(file
, &sb
)) {
8545 warnx("start of day file doesn't exist, creating it");
8546 if ((f
= fopen(file
, "w")) == NULL
)
8547 err(1, "startofday");
8548 if (fputs(version
, f
) == EOF
)
8555 if ((f
= fopen(file
, "r+")) == NULL
)
8556 err(1, "startofday");
8557 if (fgets(sodversion
, sizeof sodversion
, f
) == NULL
)
8559 sodversion
[strcspn(sodversion
, "\n")] = '\0';
8560 if (strcmp(version
, sodversion
)) {
8561 if ((f
= freopen(file
, "w", f
)) == NULL
)
8562 err(1, "startofday");
8563 if (fputs(version
, f
) == EOF
)
8566 /* upgrade, say something smart */
8572 /* favorites file */
8573 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_FAVS_FILE
);
8574 if (stat(file
, &sb
)) {
8575 warnx("favorites file doesn't exist, creating it");
8576 if ((f
= fopen(file
, "w")) == NULL
)
8577 err(1, "favorites");
8581 /* quickmarks file */
8582 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
8583 if (stat(file
, &sb
)) {
8584 warnx("quickmarks file doesn't exist, creating it");
8585 if ((f
= fopen(file
, "w")) == NULL
)
8586 err(1, "quickmarks");
8590 /* search history */
8591 if (history_autosave
) {
8592 snprintf(search_file
, sizeof search_file
, "%s" PS
"%s",
8593 work_dir
, XT_SEARCH_FILE
);
8594 if (stat(search_file
, &sb
)) {
8595 warnx("search history file doesn't exist, creating it");
8596 if ((f
= fopen(search_file
, "w")) == NULL
)
8597 err(1, "search_history");
8600 history_read(&shl
, search_file
, &search_history_count
);
8603 /* command history */
8604 if (history_autosave
) {
8605 snprintf(command_file
, sizeof command_file
, "%s" PS
"%s",
8606 work_dir
, XT_COMMAND_FILE
);
8607 if (stat(command_file
, &sb
)) {
8608 warnx("command history file doesn't exist, creating it");
8609 if ((f
= fopen(command_file
, "w")) == NULL
)
8610 err(1, "command_history");
8613 history_read(&chl
, command_file
, &cmd_history_count
);
8619 /* guess_search regex */
8620 if (url_regex
== NULL
)
8621 url_regex
= g_strdup(XT_URL_REGEX
);
8623 if (regcomp(&url_re
, url_regex
, REG_EXTENDED
| REG_NOSUB
))
8624 startpage_add("invalid url regex %s", url_regex
);
8627 env_proxy
= getenv("http_proxy");
8629 setup_proxy(env_proxy
);
8631 env_proxy
= getenv("HTTP_PROXY");
8633 setup_proxy(env_proxy
);
8635 setup_proxy(http_proxy
);
8638 /* the user can optionally have the proxy disabled at startup */
8639 if ((http_proxy_starts_enabled
== 0) && (http_proxy
!= NULL
)) {
8640 http_proxy_save
= g_strdup(http_proxy
);
8644 #ifndef XT_SOCKET_DISABLE
8646 send_cmd_to_socket(argv
[0]);
8650 opte
= opte
; /* shut mingw up */
8652 /* set some connection parameters */
8653 g_object_set(session
, "max-conns", max_connections
, (char *)NULL
);
8654 g_object_set(session
, "max-conns-per-host", max_host_connections
,
8656 g_object_set(session
, SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
8659 g_signal_connect(session
, "request-queued", G_CALLBACK(session_rq_cb
),
8662 #ifndef XT_SOCKET_DISABLE
8663 /* see if there is already a xombrero running */
8664 if (single_instance
&& is_running()) {
8666 warnx("already running");
8671 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
8672 send_cmd_to_socket(cmd
);
8682 optn
= optn
; /* shut mingw up */
8687 if (enable_strict_transport
)
8688 strict_transport_init();
8690 /* uri completion */
8691 completion_model
= gtk_list_store_new(1, G_TYPE_STRING
);
8694 buffers_store
= gtk_list_store_new
8695 (NUM_COLS
, G_TYPE_UINT
, GDK_TYPE_PIXBUF
, G_TYPE_STRING
);
8701 notebook_tab_set_visibility();
8703 if (save_global_history
)
8704 restore_global_history();
8706 /* restore session list */
8707 restore_sessions_list();
8709 if (!strcmp(named_session
, XT_SAVED_TABS_FILE
))
8710 restore_saved_tabs();
8712 a
.s
= named_session
;
8713 a
.i
= XT_SES_DONOTHING
;
8714 open_tabs(NULL
, &a
);
8717 /* see if we have an exception */
8718 if (!TAILQ_EMPTY(&spl
)) {
8719 create_new_tab("about:startpage", NULL
, focus
, -1);
8724 create_new_tab(argv
[0], NULL
, focus
, -1);
8731 if (TAILQ_EMPTY(&tabs
))
8732 create_new_tab(home
, NULL
, 1, -1);
8733 #ifndef XT_SOCKET_DISABLE
8735 if ((s
= build_socket()) != -1) {
8736 channel
= g_io_channel_unix_new(s
);
8737 g_io_add_watch(channel
, G_IO_IN
, socket_watcher
, NULL
);
8741 #if GTK_CHECK_VERSION(3, 0, 0)
8747 gnutls_global_deinit();