2 * Copyright (c) 2010, 2011 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2011 Stevan Andjelkovic <stevan@student.chalmers.se>
4 * Copyright (c) 2010, 2011, 2012 Edd Barrett <vext01@gmail.com>
5 * Copyright (c) 2011 Todd T. Fries <todd@fries.net>
6 * Copyright (c) 2011 Raphael Graf <r@undefined.ch>
7 * Copyright (c) 2011 Michal Mazurek <akfaew@jasminek.net>
8 * Copyright (c) 2012 Josh Rickmar <jrick@devio.us>
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 char *version
= XOMBRERO_VERSION
;
29 uint32_t swm_debug
= 0
52 GCRY_THREAD_OPTION_PTHREAD_IMPL
;
64 TAILQ_ENTRY(session
) entry
;
67 TAILQ_HEAD(session_list
, session
);
70 TAILQ_ENTRY(undo
) entry
;
73 int back
; /* Keeps track of how many back
74 * history items there are. */
76 TAILQ_HEAD(undo_tailq
, undo
);
78 struct command_entry
{
80 TAILQ_ENTRY(command_entry
) entry
;
82 TAILQ_HEAD(command_list
, command_entry
);
85 #define XT_DIR (".xombrero")
86 #define XT_CACHE_DIR ("cache")
87 #define XT_CERT_DIR ("certs")
88 #define XT_JS_DIR ("js")
89 #define XT_SESSIONS_DIR ("sessions")
90 #define XT_TEMP_DIR ("tmp")
91 #define XT_CONF_FILE ("xombrero.conf")
92 #define XT_QMARKS_FILE ("quickmarks")
93 #define XT_SAVED_TABS_FILE ("main_session")
94 #define XT_RESTART_TABS_FILE ("restart_tabs")
95 #define XT_SOCKET_FILE ("socket")
96 #define XT_SAVE_SESSION_ID ("SESSION_NAME=")
97 #define XT_SEARCH_FILE ("search_history")
98 #define XT_COMMAND_FILE ("command_history")
99 #define XT_DLMAN_REFRESH "10"
100 #define XT_MAX_URL_LENGTH (4096) /* 1 page is atomic, don't make bigger */
101 #define XT_MAX_UNDO_CLOSE_TAB (32)
102 #define XT_PRINT_EXTRA_MARGIN 10
103 #define XT_URL_REGEX ("^[[:blank:]]*[^[:blank:]]*([[:alnum:]-]+\\.)+[[:alnum:]-][^[:blank:]]*[[:blank:]]*$")
104 #define XT_INVALID_MARK (-1) /* XXX this is a double, maybe use something else, like a nan */
107 #define XT_COLOR_RED "#cc0000"
108 #define XT_COLOR_YELLOW "#ffff66"
109 #define XT_COLOR_BLUE "lightblue"
110 #define XT_COLOR_GREEN "#99ff66"
111 #define XT_COLOR_WHITE "white"
112 #define XT_COLOR_BLACK "black"
114 #define XT_COLOR_CT_BACKGROUND "#000000"
115 #define XT_COLOR_CT_INACTIVE "#dddddd"
116 #define XT_COLOR_CT_ACTIVE "#bbbb00"
117 #define XT_COLOR_CT_SEPARATOR "#555555"
119 #define XT_COLOR_SB_SEPARATOR "#555555"
121 #define XT_PROTO_DELIM "://"
124 #define XT_MOVE_INVALID (0)
125 #define XT_MOVE_DOWN (1)
126 #define XT_MOVE_UP (2)
127 #define XT_MOVE_BOTTOM (3)
128 #define XT_MOVE_TOP (4)
129 #define XT_MOVE_PAGEDOWN (5)
130 #define XT_MOVE_PAGEUP (6)
131 #define XT_MOVE_HALFDOWN (7)
132 #define XT_MOVE_HALFUP (8)
133 #define XT_MOVE_LEFT (9)
134 #define XT_MOVE_FARLEFT (10)
135 #define XT_MOVE_RIGHT (11)
136 #define XT_MOVE_FARRIGHT (12)
137 #define XT_MOVE_PERCENT (13)
138 #define XT_MOVE_CENTER (14)
140 #define XT_QMARK_SET (0)
141 #define XT_QMARK_OPEN (1)
142 #define XT_QMARK_TAB (2)
144 #define XT_MARK_SET (0)
145 #define XT_MARK_GOTO (1)
147 #define XT_GO_UP_ROOT (999)
149 #define XT_NAV_INVALID (0)
150 #define XT_NAV_BACK (1)
151 #define XT_NAV_FORWARD (2)
152 #define XT_NAV_RELOAD (3)
153 #define XT_NAV_STOP (4)
155 #define XT_FOCUS_INVALID (0)
156 #define XT_FOCUS_URI (1)
157 #define XT_FOCUS_SEARCH (2)
159 #define XT_SEARCH_INVALID (0)
160 #define XT_SEARCH_NEXT (1)
161 #define XT_SEARCH_PREV (2)
163 #define XT_PASTE_CURRENT_TAB (0)
164 #define XT_PASTE_NEW_TAB (1)
166 #define XT_ZOOM_IN (-1)
167 #define XT_ZOOM_OUT (-2)
168 #define XT_ZOOM_NORMAL (100)
170 #define XT_CMD_OPEN (0)
171 #define XT_CMD_OPEN_CURRENT (1)
172 #define XT_CMD_TABNEW (2)
173 #define XT_CMD_TABNEW_CURRENT (3)
175 #define XT_STATUS_NOTHING (0)
176 #define XT_STATUS_LINK (1)
177 #define XT_STATUS_URI (2)
178 #define XT_STATUS_LOADING (3)
180 #define XT_SES_DONOTHING (0)
181 #define XT_SES_CLOSETABS (1)
183 #define XT_PREFIX (1<<0)
184 #define XT_USERARG (1<<1)
185 #define XT_URLARG (1<<2)
186 #define XT_INTARG (1<<3)
187 #define XT_SESSARG (1<<4)
188 #define XT_SETARG (1<<5)
190 #define XT_HINT_NEWTAB (1<<0)
192 #define XT_BUFCMD_SZ (8)
194 #define XT_EJS_SHOW (1<<0)
196 GtkWidget
* create_button(char *, char *, int);
198 void recalc_tabs(void);
199 void recolor_compact_tabs(void);
200 void set_current_tab(int page_num
);
201 gboolean
update_statusbar_position(GtkAdjustment
*, gpointer
);
202 void marks_clear(struct tab
*t
);
205 extern char *__progname
;
206 char * const *start_argv
;
208 GtkWidget
*main_window
;
209 GtkNotebook
*notebook
;
211 GtkWidget
*arrow
, *abtn
;
212 struct tab_list tabs
;
213 struct history_list hl
;
214 int hl_purge_count
= 0;
215 struct session_list sessions
;
216 struct domain_list c_wl
;
217 struct domain_list js_wl
;
218 struct domain_list pl_wl
;
219 struct strict_transport_tree st_tree
;
220 struct undo_tailq undos
;
221 struct keybinding_list kbl
;
223 struct user_agent_list ua_list
;
224 int user_agent_count
= 0;
225 struct command_list chl
;
226 struct command_list shl
;
227 struct command_entry
*history_at
;
228 struct command_entry
*search_at
;
230 int cmd_history_count
= 0;
231 int search_history_count
= 0;
233 uint64_t blocked_cookies
= 0;
234 char named_session
[PATH_MAX
];
235 GtkListStore
*completion_model
;
236 GtkListStore
*buffers_store
;
238 char *qmarks
[XT_NOQMARKS
];
239 int btn_down
; /* M1 down in any wv */
240 regex_t url_re
; /* guess_search regex */
242 /* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
243 int next_download_id
= 1;
245 void xxx_dir(char *);
246 int icon_size_map(int);
247 void activate_uri_entry_cb(GtkWidget
*, struct tab
*);
250 history_delete(struct command_list
*l
, int *counter
)
252 struct command_entry
*c
;
254 if (l
== NULL
|| counter
== NULL
)
257 c
= TAILQ_LAST(l
, command_list
);
261 TAILQ_REMOVE(l
, c
, entry
);
268 history_add(struct command_list
*list
, char *file
, char *l
, int *counter
)
270 struct command_entry
*c
;
273 if (list
== NULL
|| l
== NULL
|| counter
== NULL
)
276 /* don't add the same line */
277 c
= TAILQ_FIRST(list
);
279 if (!strcmp(c
->line
+ 1 /* skip space */, l
))
282 c
= g_malloc0(sizeof *c
);
283 c
->line
= g_strdup_printf(" %s", l
);
286 TAILQ_INSERT_HEAD(list
, c
, entry
);
289 history_delete(list
, counter
);
291 if (history_autosave
&& file
) {
292 f
= fopen(file
, "w");
294 show_oops(NULL
, "couldn't write history %s", file
);
298 TAILQ_FOREACH_REVERSE(c
, list
, command_list
, entry
) {
300 fprintf(f
, "%s\n", c
->line
);
308 history_read(struct command_list
*list
, char *file
, int *counter
)
311 char *s
, line
[65536];
313 if (list
== NULL
|| file
== NULL
)
316 f
= fopen(file
, "r");
318 startpage_add("couldn't open history file %s", file
);
323 s
= fgets(line
, sizeof line
, f
);
324 if (s
== NULL
|| feof(f
) || ferror(f
))
326 if ((s
= strchr(line
, '\n')) == NULL
) {
327 startpage_add("invalid history file %s", file
);
333 history_add(list
, NULL
, line
+ 1, counter
);
341 /* marks array storage. */
345 if (i
< 0 || i
>= XT_NOMARKS
)
356 if ((ret
= strchr(XT_MARKS
, m
)) != NULL
)
357 return ret
- XT_MARKS
;
362 /* quickmarks array storage. */
366 if (i
< 0 || i
>= XT_NOQMARKS
)
377 if ((ret
= strchr(XT_QMARKS
, m
)) != NULL
)
378 return ret
- XT_QMARKS
;
383 #ifndef XT_SIGNALS_DISABLE
387 int saved_errno
, status
;
392 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
396 if (errno
!= ECHILD
) {
398 clog_warn("sigchild: waitpid:");
404 if (WIFEXITED(status
)) {
405 if (WEXITSTATUS(status
) != 0) {
407 clog_warnx("sigchild: child exit status: %d",
408 WEXITSTATUS(status));
413 clog_warnx("sigchild: child is terminated abnormally");
423 is_g_object_setting(GObject
*o
, char *str
)
425 guint n_props
= 0, i
;
426 GParamSpec
**proplist
;
432 proplist
= g_object_class_list_properties(G_OBJECT_GET_CLASS(o
),
435 for (i
= 0; i
< n_props
; i
++) {
436 if (! strcmp(proplist
[i
]->name
, str
)) {
447 get_current_tab(void)
451 TAILQ_FOREACH(t
, &tabs
, entry
) {
452 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
456 warnx("%s: no current tab", __func__
);
462 set_ssl_ca_file(char *file
)
466 if (file
== NULL
|| strlen(file
) == 0)
468 if (stat(file
, &sb
)) {
469 warnx("no CA file: %s", file
);
470 if (ssl_ca_file
== file
) { /* check and fix */
476 if (ssl_ca_file
!= file
) { /* set dynamically */
479 ssl_ca_file
= g_strdup(file
);
481 g_object_set(session
,
482 SOUP_SESSION_SSL_CA_FILE
, ssl_ca_file
,
483 SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
489 set_status(struct tab
*t
, gchar
*s
, int status
)
497 case XT_STATUS_LOADING
:
498 type
= g_strdup_printf("Loading: %s", s
);
502 type
= g_strdup_printf("Link: %s", s
);
504 t
->status
= g_strdup(gtk_entry_get_text(
505 GTK_ENTRY(t
->sbe
.statusbar
)));
509 type
= g_strdup_printf("%s", s
);
511 t
->status
= g_strdup(type
);
515 t
->status
= g_strdup(s
);
517 case XT_STATUS_NOTHING
:
522 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.statusbar
), s
);
528 hide_cmd(struct tab
*t
)
530 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
532 history_at
= NULL
; /* just in case */
533 search_at
= NULL
; /* just in case */
534 gtk_widget_hide(t
->cmd
);
538 show_cmd(struct tab
*t
)
540 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
544 gtk_widget_hide(t
->oops
);
545 gtk_widget_show(t
->cmd
);
549 hide_buffers(struct tab
*t
)
551 gtk_widget_hide(t
->buffers
);
552 gtk_list_store_clear(buffers_store
);
563 sort_tabs_by_page_num(struct tab
***stabs
)
568 num_tabs
= gtk_notebook_get_n_pages(notebook
);
570 *stabs
= g_malloc0(num_tabs
* sizeof(struct tab
*));
572 TAILQ_FOREACH(t
, &tabs
, entry
)
573 (*stabs
)[gtk_notebook_page_num(notebook
, t
->vbox
)] = t
;
579 buffers_make_list(void)
582 const gchar
*title
= NULL
;
584 struct tab
**stabs
= NULL
;
586 num_tabs
= sort_tabs_by_page_num(&stabs
);
588 for (i
= 0; i
< num_tabs
; i
++)
590 gtk_list_store_append(buffers_store
, &iter
);
591 title
= get_title(stabs
[i
], FALSE
);
592 gtk_list_store_set(buffers_store
, &iter
,
593 COL_ID
, i
+ 1, /* Enumerate the tabs starting from 1
595 COL_FAVICON
, gtk_image_get_pixbuf
596 (GTK_IMAGE(stabs
[i
]->tab_elems
.favicon
)),
605 show_buffers(struct tab
*t
)
607 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)))
611 gtk_widget_show(t
->buffers
);
612 gtk_widget_grab_focus(GTK_WIDGET(t
->buffers
));
616 toggle_buffers(struct tab
*t
)
618 if (gtk_widget_get_visible(t
->buffers
))
625 buffers(struct tab
*t
, struct karg
*args
)
633 hide_oops(struct tab
*t
)
635 gtk_widget_hide(t
->oops
);
639 show_oops(struct tab
*at
, const char *fmt
, ...)
643 struct tab
*t
= NULL
;
649 if ((t
= get_current_tab()) == NULL
)
655 if (vasprintf(&msg
, fmt
, ap
) == -1)
656 errx(1, "show_oops failed");
659 gtk_entry_set_text(GTK_ENTRY(t
->oops
), msg
);
660 gtk_widget_hide(t
->cmd
);
661 gtk_widget_show(t
->oops
);
667 char work_dir
[PATH_MAX
];
668 char certs_dir
[PATH_MAX
];
669 char js_dir
[PATH_MAX
];
670 char cache_dir
[PATH_MAX
];
671 char sessions_dir
[PATH_MAX
];
672 char temp_dir
[PATH_MAX
];
673 char cookie_file
[PATH_MAX
];
674 char *strict_transport_file
= NULL
;
675 SoupSession
*session
;
676 SoupCookieJar
*s_cookiejar
;
677 SoupCookieJar
*p_cookiejar
;
678 char rc_fname
[PATH_MAX
];
680 struct mime_type_list mtl
;
681 struct alias_list aliases
;
684 struct tab
*create_new_tab(char *, struct undo
*, int, int);
685 void delete_tab(struct tab
*);
686 void setzoom_webkit(struct tab
*, int);
687 int download_rb_cmp(struct download
*, struct download
*);
688 gboolean
cmd_execute(struct tab
*t
, char *str
);
691 history_rb_cmp(struct history
*h1
, struct history
*h2
)
693 return (strcmp(h1
->uri
, h2
->uri
));
695 RB_GENERATE(history_list
, history
, entry
, history_rb_cmp
);
698 domain_rb_cmp(struct domain
*d1
, struct domain
*d2
)
700 return (strcmp(d1
->d
, d2
->d
));
702 RB_GENERATE(domain_list
, domain
, entry
, domain_rb_cmp
);
705 download_rb_cmp(struct download
*e1
, struct download
*e2
)
707 return (e1
->id
< e2
->id
? -1 : e1
->id
> e2
->id
);
709 RB_GENERATE(download_list
, download
, entry
, download_rb_cmp
);
711 struct valid_url_types
{
722 valid_url_type(char *url
)
726 for (i
= 0; i
< LENGTH(vut
); i
++)
727 if (!strncasecmp(vut
[i
].type
, url
, strlen(vut
[i
].type
)))
734 match_alias(char *url_in
)
738 char *url_out
= NULL
, *search
, *enc_arg
;
740 search
= g_strdup(url_in
);
742 if (strsep(&arg
, " \t") == NULL
) {
743 show_oops(NULL
, "match_alias: NULL URL");
747 TAILQ_FOREACH(a
, &aliases
, entry
) {
748 if (!strcmp(search
, a
->a_name
))
753 DNPRINTF(XT_D_URL
, "match_alias: matched alias %s\n",
756 enc_arg
= soup_uri_encode(arg
, XT_RESERVED_CHARS
);
757 url_out
= g_strdup_printf(a
->a_uri
, enc_arg
);
760 url_out
= g_strdup_printf(a
->a_uri
, "");
768 guess_url_type(char *url_in
)
771 char *url_out
= NULL
, *enc_search
= NULL
;
776 /* substitute aliases */
777 url_out
= match_alias(url_in
);
781 /* see if we are an about page */
782 if (!strncmp(url_in
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
))
783 for (i
= 0; i
< about_list_size(); i
++)
784 if (!strcmp(&url_in
[XT_URI_ABOUT_LEN
],
785 about_list
[i
].name
)) {
786 url_out
= g_strdup(url_in
);
790 if (guess_search
&& url_regex
&&
791 !(g_str_has_prefix(url_in
, "http://") ||
792 g_str_has_prefix(url_in
, "https://"))) {
793 if (regexec(&url_re
, url_in
, 0, NULL
, 0)) {
794 /* invalid URI so search instead */
795 enc_search
= soup_uri_encode(url_in
, XT_RESERVED_CHARS
);
796 url_out
= g_strdup_printf(search_string
, enc_search
);
802 /* XXX not sure about this heuristic */
803 if (stat(url_in
, &sb
) == 0) {
804 if (url_in
[0] == '/')
805 url_out
= g_strdup_printf("file://%s", url_in
);
807 cwd
= malloc(PATH_MAX
);
808 if (getcwd(cwd
, PATH_MAX
) != NULL
) {
809 url_out
= g_strdup_printf("file://%s/%s",cwd
, url_in
);
814 url_out
= g_strdup_printf("http://%s", url_in
); /* guess http */
816 DNPRINTF(XT_D_URL
, "guess_url_type: guessed %s\n", url_out
);
822 load_uri(struct tab
*t
, gchar
*uri
)
825 gchar
*newuri
= NULL
;
831 /* Strip leading spaces. */
832 while (*uri
&& isspace(*uri
))
835 if (strlen(uri
) == 0) {
840 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
842 if (valid_url_type(uri
)) {
843 newuri
= guess_url_type(uri
);
847 if (!strncmp(uri
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
)) {
848 for (i
= 0; i
< about_list_size(); i
++)
849 if (!strcmp(&uri
[XT_URI_ABOUT_LEN
], about_list
[i
].name
)) {
850 bzero(&args
, sizeof args
);
851 about_list
[i
].func(t
, &args
);
852 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
),
856 show_oops(t
, "invalid about page");
860 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
862 webkit_web_view_load_uri(t
->wv
, uri
);
869 get_uri(struct tab
*t
)
871 const gchar
*uri
= NULL
;
873 if (webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
)
875 if (t
->xtp_meaning
== XT_XTP_TAB_MEANING_NORMAL
) {
876 uri
= webkit_web_view_get_uri(t
->wv
);
878 /* use tmp_uri to make sure it is g_freed */
881 t
->tmp_uri
=g_strdup_printf("%s%s", XT_URI_ABOUT
,
882 about_list
[t
->xtp_meaning
].name
);
889 get_title(struct tab
*t
, bool window
)
891 const gchar
*set
= NULL
, *title
= NULL
;
892 WebKitLoadStatus status
= webkit_web_view_get_load_status(t
->wv
);
894 if (status
== WEBKIT_LOAD_PROVISIONAL
|| status
== WEBKIT_LOAD_FAILED
||
895 t
->xtp_meaning
== XT_XTP_TAB_MEANING_BL
)
898 title
= webkit_web_view_get_title(t
->wv
);
899 if ((set
= title
? title
: get_uri(t
)))
903 set
= window
? XT_NAME
: "(untitled)";
909 find_mime_type(char *mime_type
)
911 struct mime_type
*m
, *def
= NULL
, *rv
= NULL
;
913 TAILQ_FOREACH(m
, &mtl
, entry
) {
915 !strncmp(mime_type
, m
->mt_type
, strlen(m
->mt_type
)))
918 if (m
->mt_default
== 0 && !strcmp(mime_type
, m
->mt_type
)) {
931 wl_find_uri(const gchar
*s
, struct domain_list
*wl
)
937 if (s
== NULL
|| wl
== NULL
)
940 if (!strncmp(s
, "http://", strlen("http://")))
941 s
= &s
[strlen("http://")];
942 else if (!strncmp(s
, "https://", strlen("https://")))
943 s
= &s
[strlen("https://")];
948 for (i
= 0; i
< strlen(s
) + 1 /* yes er need this */; i
++)
949 /* chop string at first slash */
950 if (s
[i
] == '/' || s
[i
] == ':' || s
[i
] == '\0') {
962 js_ref_to_string(JSContextRef context
, JSValueRef ref
)
968 jsref
= JSValueToStringCopy(context
, ref
, NULL
);
972 l
= JSStringGetMaximumUTF8CStringSize(jsref
);
975 JSStringGetUTF8CString(jsref
, s
, l
);
976 JSStringRelease(jsref
);
981 #define XT_JS_DONE ("done;")
982 #define XT_JS_DONE_LEN (strlen(XT_JS_DONE))
983 #define XT_JS_INSERT ("insert;")
984 #define XT_JS_INSERT_LEN (strlen(XT_JS_INSERT))
987 run_script(struct tab
*t
, char *s
)
989 JSGlobalContextRef ctx
;
990 WebKitWebFrame
*frame
;
992 JSValueRef val
, exception
;
995 DNPRINTF(XT_D_JS
, "%s: tab %d %s\n", __func__
,
996 t
->tab_id
, s
== (char *)JS_HINTING
? "JS_HINTING" : s
);
998 frame
= webkit_web_view_get_main_frame(t
->wv
);
999 ctx
= webkit_web_frame_get_global_context(frame
);
1001 str
= JSStringCreateWithUTF8CString(s
);
1002 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
1003 NULL
, 0, &exception
);
1004 JSStringRelease(str
);
1006 DNPRINTF(XT_D_JS
, "%s: val %p\n", __func__
, val
);
1008 es
= js_ref_to_string(ctx
, exception
);
1010 DNPRINTF(XT_D_JS
, "%s: exception %s\n", __func__
, es
);
1015 es
= js_ref_to_string(ctx
, val
);
1018 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
1020 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
1024 DNPRINTF(XT_D_JS
, "%s: val %s\n", __func__
, es
);
1033 run_script_locked(struct tab
*t
, char *s
)
1037 gdk_threads_enter();
1039 rv
= run_script(t
, s
);
1042 gdk_threads_leave();
1048 enable_hints(struct tab
*t
)
1050 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
1053 run_script(t
, "hints.createHints('', 'F');");
1055 run_script(t
, "hints.createHints('', 'f');");
1056 t
->mode
= XT_MODE_HINT
;
1060 disable_hints(struct tab
*t
)
1062 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
1064 run_script(t
, "hints.clearHints();");
1065 t
->mode
= XT_MODE_COMMAND
;
1070 passthrough(struct tab
*t
, struct karg
*args
)
1072 t
->mode
= XT_MODE_PASSTHROUGH
;
1077 modurl(struct tab
*t
, struct karg
*args
)
1079 const gchar
*uri
= NULL
;
1082 /* XXX kind of a bad hack, but oh well */
1083 if (GTK_WIDGET_HAS_FOCUS(t
->uri_entry
)) {
1084 if ((uri
= gtk_entry_get_text(GTK_ENTRY(t
->uri_entry
))) &&
1086 u
= g_strdup_printf("www.%s.com", uri
);
1087 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), u
);
1089 activate_uri_entry_cb(t
->uri_entry
, t
);
1096 hint(struct tab
*t
, struct karg
*args
)
1099 DNPRINTF(XT_D_JS
, "hint: tab %d args %d\n", t
->tab_id
, args
->i
);
1101 if (t
->mode
== XT_MODE_HINT
) {
1102 if (args
->i
== XT_HINT_NEWTAB
)
1112 apply_style(struct tab
*t
)
1115 g_object_set(G_OBJECT(t
->settings
),
1116 "user-stylesheet-uri", t
->stylesheet
, (char *)NULL
);
1120 remove_style(struct tab
*t
)
1123 g_object_set(G_OBJECT(t
->settings
),
1124 "user-stylesheet-uri", NULL
, (char *)NULL
);
1128 userstyle(struct tab
*t
, struct karg
*args
)
1132 DNPRINTF(XT_D_JS
, "userstyle: tab %d\n", t
->tab_id
);
1135 case XT_STYLE_CURRENT_TAB
:
1141 case XT_STYLE_GLOBAL
:
1142 if (userstyle_global
) {
1143 userstyle_global
= 0;
1144 TAILQ_FOREACH(tt
, &tabs
, entry
)
1147 userstyle_global
= 1;
1148 TAILQ_FOREACH(tt
, &tabs
, entry
)
1158 quit(struct tab
*t
, struct karg
*args
)
1160 if (save_global_history
)
1161 save_global_history_to_disk(t
);
1169 restore_sessions_list(void)
1172 struct dirent
*dp
= NULL
;
1176 sdir
= opendir(sessions_dir
);
1178 while ((dp
= readdir(sdir
)) != NULL
) {
1179 #if defined __MINGW32__
1180 reg
= 1; /* windows only has regular files */
1182 reg
= dp
->d_type
== DT_REG
;
1185 s
= g_malloc(sizeof(struct session
));
1186 s
->name
= g_strdup(dp
->d_name
);
1187 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
1195 open_tabs(struct tab
*t
, struct karg
*a
)
1197 char file
[PATH_MAX
];
1201 struct tab
*ti
, *tt
;
1206 ti
= TAILQ_LAST(&tabs
, tab_list
);
1208 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, a
->s
);
1209 if ((f
= fopen(file
, "r")) == NULL
)
1213 if ((uri
= fparseln(f
, NULL
, NULL
, "\0\0\0", 0)) == NULL
)
1214 if (feof(f
) || ferror(f
))
1217 /* retrieve session name */
1218 if (uri
&& g_str_has_prefix(uri
, XT_SAVE_SESSION_ID
)) {
1219 strlcpy(named_session
,
1220 &uri
[strlen(XT_SAVE_SESSION_ID
)],
1221 sizeof named_session
);
1225 if (uri
&& strlen(uri
))
1226 create_new_tab(uri
, NULL
, 1, -1);
1232 /* close open tabs */
1233 if (a
->i
== XT_SES_CLOSETABS
&& ti
!= NULL
) {
1235 tt
= TAILQ_FIRST(&tabs
);
1256 restore_saved_tabs(void)
1258 char file
[PATH_MAX
];
1259 int unlink_file
= 0;
1264 snprintf(file
, sizeof file
, "%s" PS
"%s",
1265 sessions_dir
, XT_RESTART_TABS_FILE
);
1266 if (stat(file
, &sb
) == -1)
1267 a
.s
= XT_SAVED_TABS_FILE
;
1270 a
.s
= XT_RESTART_TABS_FILE
;
1273 a
.i
= XT_SES_DONOTHING
;
1274 rv
= open_tabs(NULL
, &a
);
1283 save_tabs(struct tab
*t
, struct karg
*a
)
1285 char file
[PATH_MAX
];
1287 int num_tabs
= 0, i
;
1288 struct tab
**stabs
= NULL
;
1290 /* tab may be null here */
1295 snprintf(file
, sizeof file
, "%s" PS
"%s",
1296 sessions_dir
, named_session
);
1298 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, a
->s
);
1300 if ((f
= fopen(file
, "w")) == NULL
) {
1301 show_oops(t
, "Can't open save_tabs file: %s", strerror(errno
));
1305 /* save session name */
1306 fprintf(f
, "%s%s\n", XT_SAVE_SESSION_ID
, named_session
);
1308 /* Save tabs, in the order they are arranged in the notebook. */
1309 num_tabs
= sort_tabs_by_page_num(&stabs
);
1311 for (i
= 0; i
< num_tabs
; i
++)
1313 if (get_uri(stabs
[i
]) != NULL
)
1314 fprintf(f
, "%s\n", get_uri(stabs
[i
]));
1315 else if (gtk_entry_get_text(GTK_ENTRY(
1316 stabs
[i
]->uri_entry
)))
1317 fprintf(f
, "%s\n", gtk_entry_get_text(GTK_ENTRY(
1318 stabs
[i
]->uri_entry
)));
1323 /* try and make sure this gets to disk NOW. XXX Backup first? */
1324 if (fflush(f
) != 0 || fsync(fileno(f
)) != 0) {
1325 show_oops(t
, "May not have managed to save session: %s",
1335 save_tabs_and_quit(struct tab
*t
, struct karg
*args
)
1347 run_page_script(struct tab
*t
, struct karg
*args
)
1350 char *tmp
, script
[PATH_MAX
];
1352 tmp
= args
->s
!= NULL
&& strlen(args
->s
) > 0 ? args
->s
: default_script
;
1353 if (tmp
[0] == '\0') {
1354 show_oops(t
, "no script specified");
1358 if ((uri
= get_uri(t
)) == NULL
) {
1359 show_oops(t
, "tab is empty, not running script");
1364 snprintf(script
, sizeof script
, "%s" PS
"%s",
1365 pwd
->pw_dir
, &tmp
[1]);
1367 strlcpy(script
, tmp
, sizeof script
);
1369 return (fork_exec(t
, script
, uri
, "can't launch external script", 1));
1373 yank_uri(struct tab
*t
, struct karg
*args
)
1376 GtkClipboard
*clipboard
;
1378 if ((uri
= get_uri(t
)) == NULL
)
1381 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1382 gtk_clipboard_set_text(clipboard
, uri
, -1);
1388 paste_uri(struct tab
*t
, struct karg
*args
)
1390 GtkClipboard
*clipboard
;
1391 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
1393 gchar
*p
= NULL
, *uri
;
1395 /* try primary clipboard first */
1396 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1397 p
= gtk_clipboard_wait_for_text(clipboard
);
1399 /* if it failed get whatever text is in cut_buffer0 */
1400 if (p
== NULL
&& xterm_workaround
)
1401 if (gdk_property_get(gdk_get_default_root_window(),
1403 gdk_atom_intern("STRING", FALSE
),
1405 1024 * 1024 /* picked out of my butt */,
1413 /* yes sir, we need to NUL the string */
1419 while (*uri
&& isspace(*uri
))
1421 if (strlen(uri
) == 0) {
1422 show_oops(t
, "empty paste buffer");
1425 if (guess_search
== 0 && valid_url_type(uri
)) {
1426 /* we can be clever and paste this in search box */
1427 show_oops(t
, "not a valid URL");
1431 if (args
->i
== XT_PASTE_CURRENT_TAB
)
1433 else if (args
->i
== XT_PASTE_NEW_TAB
)
1434 create_new_tab(uri
, NULL
, 1, -1);
1445 js_toggle_cb(GtkWidget
*w
, struct tab
*t
)
1450 g_object_get(G_OBJECT(t
->settings
),
1451 "enable-scripts", &es
, (char *)NULL
);
1456 set
= XT_WL_DISABLE
;
1458 a
.i
= set
| XT_WL_TOPLEVEL
;
1461 a
.i
= set
| XT_WL_TOPLEVEL
;
1464 a
.i
= XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
;
1469 toggle_src(struct tab
*t
, struct karg
*args
)
1476 mode
= webkit_web_view_get_view_source_mode(t
->wv
);
1477 webkit_web_view_set_view_source_mode(t
->wv
, !mode
);
1478 webkit_web_view_reload(t
->wv
);
1484 focus_webview(struct tab
*t
)
1489 /* only grab focus if we are visible */
1490 if (gtk_notebook_get_current_page(notebook
) == t
->tab_id
)
1491 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
1495 focus(struct tab
*t
, struct karg
*args
)
1497 if (t
== NULL
|| args
== NULL
)
1503 if (args
->i
== XT_FOCUS_URI
)
1504 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
1505 else if (args
->i
== XT_FOCUS_SEARCH
)
1506 gtk_widget_grab_focus(GTK_WIDGET(t
->search_entry
));
1512 connect_socket_from_uri(const gchar
*uri
, const gchar
**error_str
, char *domain
,
1516 struct addrinfo hints
, *res
= NULL
, *ai
;
1517 int rv
= -1, s
= -1, on
, error
;
1519 static gchar myerror
[256]; /* this is not thread safe */
1522 *error_str
= myerror
;
1523 if (uri
&& !g_str_has_prefix(uri
, "https://")) {
1524 *error_str
= "invalid URI";
1528 su
= soup_uri_new(uri
);
1530 *error_str
= "invalid soup URI";
1533 if (!SOUP_URI_VALID_FOR_HTTP(su
)) {
1534 *error_str
= "invalid HTTPS URI";
1538 snprintf(port
, sizeof port
, "%d", su
->port
);
1539 bzero(&hints
, sizeof(struct addrinfo
));
1540 hints
.ai_flags
= AI_CANONNAME
;
1541 hints
.ai_family
= AF_UNSPEC
;
1542 hints
.ai_socktype
= SOCK_STREAM
;
1544 if ((error
= getaddrinfo(su
->host
, port
, &hints
, &res
))) {
1545 snprintf(myerror
, sizeof myerror
, "getaddrinfo failed: %s",
1546 gai_strerror(errno
));
1550 for (ai
= res
; ai
; ai
= ai
->ai_next
) {
1556 if (ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
)
1558 s
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
1561 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &on
,
1564 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) == 0)
1568 snprintf(myerror
, sizeof myerror
,
1569 "could not obtain certificates from: %s",
1575 strlcpy(domain
, su
->host
, domain_sz
);
1582 if (rv
== -1 && s
!= -1)
1590 custom_gnutls_push(void *s
, const void *buf
, size_t len
)
1592 return send((size_t)s
, buf
, len
, 0);
1596 custom_gnutls_pull(void *s
, void *buf
, size_t len
)
1598 return recv((size_t)s
, buf
, len
, 0);
1603 stop_tls(gnutls_session_t gsession
, gnutls_certificate_credentials_t xcred
)
1606 gnutls_deinit(gsession
);
1608 gnutls_certificate_free_credentials(xcred
);
1614 start_tls(const gchar
**error_str
, int s
, gnutls_session_t
*gs
,
1615 gnutls_certificate_credentials_t
*xc
)
1617 gnutls_certificate_credentials_t xcred
;
1618 gnutls_session_t gsession
;
1620 static gchar myerror
[1024]; /* this is not thread safe */
1622 if (gs
== NULL
|| xc
== NULL
)
1629 gnutls_certificate_allocate_credentials(&xcred
);
1630 gnutls_certificate_set_x509_trust_file(xcred
, ssl_ca_file
,
1631 GNUTLS_X509_FMT_PEM
);
1633 gnutls_init(&gsession
, GNUTLS_CLIENT
);
1634 gnutls_priority_set_direct(gsession
, "PERFORMANCE", NULL
);
1635 gnutls_credentials_set(gsession
, GNUTLS_CRD_CERTIFICATE
, xcred
);
1636 gnutls_transport_set_ptr(gsession
, (gnutls_transport_ptr_t
)(long)s
);
1638 /* sockets on windows don't use file descriptors */
1639 gnutls_transport_set_push_function(gsession
, custom_gnutls_push
);
1640 gnutls_transport_set_pull_function(gsession
, custom_gnutls_pull
);
1642 if ((rv
= gnutls_handshake(gsession
)) < 0) {
1643 snprintf(myerror
, sizeof myerror
,
1644 "gnutls_handshake failed %d fatal %d %s",
1646 gnutls_error_is_fatal(rv
),
1647 #if LIBGNUTLS_VERSION_MAJOR >= 2 && LIBGNUTLS_VERSION_MINOR >= 6
1648 gnutls_strerror_name(rv
));
1650 "GNUTLS version is too old to provide human readable error");
1652 stop_tls(gsession
, xcred
);
1656 gnutls_credentials_type_t cred
;
1657 cred
= gnutls_auth_get_type(gsession
);
1658 if (cred
!= GNUTLS_CRD_CERTIFICATE
) {
1659 snprintf(myerror
, sizeof myerror
,
1660 "gnutls_auth_get_type failed %d",
1662 stop_tls(gsession
, xcred
);
1670 *error_str
= myerror
;
1675 get_connection_certs(gnutls_session_t gsession
, gnutls_x509_crt_t
**certs
,
1679 const gnutls_datum_t
*cl
;
1680 gnutls_x509_crt_t
*all_certs
;
1683 if (certs
== NULL
|| cert_count
== NULL
)
1685 if (gnutls_certificate_type_get(gsession
) != GNUTLS_CRT_X509
)
1687 cl
= gnutls_certificate_get_peers(gsession
, &len
);
1691 all_certs
= g_malloc(sizeof(gnutls_x509_crt_t
) * len
);
1692 for (i
= 0; i
< len
; i
++) {
1693 gnutls_x509_crt_init(&all_certs
[i
]);
1694 if (gnutls_x509_crt_import(all_certs
[i
], &cl
[i
],
1695 GNUTLS_X509_FMT_PEM
< 0)) {
1709 free_connection_certs(gnutls_x509_crt_t
*certs
, size_t cert_count
)
1713 for (i
= 0; i
< cert_count
; i
++)
1714 gnutls_x509_crt_deinit(certs
[i
]);
1719 statusbar_modify_attr(struct tab
*t
, const char *text
, const char *base
)
1721 GdkColor c_text
, c_base
;
1723 gdk_color_parse(text
, &c_text
);
1724 gdk_color_parse(base
, &c_base
);
1726 gtk_widget_modify_text(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_text
);
1727 gtk_widget_modify_text(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_text
);
1728 gtk_widget_modify_text(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_text
);
1729 gtk_widget_modify_text(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_text
);
1731 gtk_widget_modify_base(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_base
);
1732 gtk_widget_modify_base(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_base
);
1733 gtk_widget_modify_base(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_base
);
1734 gtk_widget_modify_base(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_base
);
1738 save_certs(struct tab
*t
, gnutls_x509_crt_t
*certs
,
1739 size_t cert_count
, char *domain
)
1742 char cert_buf
[64 * 1024], file
[PATH_MAX
];
1747 if (t
== NULL
|| certs
== NULL
|| cert_count
<= 0 || domain
== NULL
)
1750 snprintf(file
, sizeof file
, "%s" PS
"%s", certs_dir
, domain
);
1751 if ((f
= fopen(file
, "w")) == NULL
) {
1752 show_oops(t
, "Can't create cert file %s %s",
1753 file
, strerror(errno
));
1757 for (i
= 0; i
< cert_count
; i
++) {
1758 cert_buf_sz
= sizeof cert_buf
;
1759 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1760 cert_buf
, &cert_buf_sz
)) {
1761 show_oops(t
, "gnutls_x509_crt_export failed");
1764 if (fwrite(cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1765 show_oops(t
, "Can't write certs: %s", strerror(errno
));
1770 /* not the best spot but oh well */
1771 gdk_color_parse(XT_COLOR_BLUE
, &color
);
1772 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
1773 statusbar_modify_attr(t
, XT_COLOR_BLACK
, XT_COLOR_BLUE
);
1786 load_compare_cert(const gchar
*uri
, const gchar
**error_str
)
1788 char domain
[8182], file
[PATH_MAX
];
1789 char cert_buf
[64 * 1024], r_cert_buf
[64 * 1024];
1791 unsigned int error
= 0;
1793 size_t cert_buf_sz
, cert_count
;
1794 enum cert_trust rv
= CERT_UNTRUSTED
;
1795 static gchar serr
[80]; /* this isn't thread safe */
1796 gnutls_session_t gsession
;
1797 gnutls_x509_crt_t
*certs
;
1798 gnutls_certificate_credentials_t xcred
;
1800 DNPRINTF(XT_D_URL
, "%s: %s\n", __func__
, uri
);
1804 if ((s
= connect_socket_from_uri(uri
, error_str
, domain
,
1805 sizeof domain
)) == -1)
1808 DNPRINTF(XT_D_URL
, "%s: fd %d\n", __func__
, s
);
1811 if (start_tls(error_str
, s
, &gsession
, &xcred
))
1813 DNPRINTF(XT_D_URL
, "%s: got tls\n", __func__
);
1815 /* verify certs in case cert file doesn't exist */
1816 if (gnutls_certificate_verify_peers2(gsession
, &error
) !=
1818 *error_str
= "Invalid certificates";
1823 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1824 *error_str
= "Can't get connection certificates";
1828 snprintf(file
, sizeof file
, "%s" PS
"%s", certs_dir
, domain
);
1829 if ((f
= fopen(file
, "r")) == NULL
) {
1835 for (i
= 0; i
< cert_count
; i
++) {
1836 cert_buf_sz
= sizeof cert_buf
;
1837 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1838 cert_buf
, &cert_buf_sz
)) {
1841 if (fread(r_cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1842 rv
= CERT_BAD
; /* critical */
1845 if (bcmp(r_cert_buf
, cert_buf
, cert_buf_sz
)) {
1846 rv
= CERT_BAD
; /* critical */
1855 free_connection_certs(certs
, cert_count
);
1857 /* we close the socket first for speed */
1861 /* only complain if we didn't save it locally */
1862 if (error
&& rv
!= CERT_LOCAL
) {
1863 strlcpy(serr
, "Certificate exception(s): ", sizeof serr
);
1864 if (error
& GNUTLS_CERT_INVALID
)
1865 strlcat(serr
, "invalid, ", sizeof serr
);
1866 if (error
& GNUTLS_CERT_REVOKED
)
1867 strlcat(serr
, "revoked, ", sizeof serr
);
1868 if (error
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
1869 strlcat(serr
, "signer not found, ", sizeof serr
);
1870 if (error
& GNUTLS_CERT_SIGNER_NOT_CA
)
1871 strlcat(serr
, "not signed by CA, ", sizeof serr
);
1872 if (error
& GNUTLS_CERT_INSECURE_ALGORITHM
)
1873 strlcat(serr
, "insecure algorithm, ", sizeof serr
);
1874 #if LIBGNUTLS_VERSION_MAJOR >= 2 && LIBGNUTLS_VERSION_MINOR >= 6
1875 if (error
& GNUTLS_CERT_NOT_ACTIVATED
)
1876 strlcat(serr
, "not activated, ", sizeof serr
);
1877 if (error
& GNUTLS_CERT_EXPIRED
)
1878 strlcat(serr
, "expired, ", sizeof serr
);
1880 for (i
= strlen(serr
) - 1; i
> 0; i
--)
1881 if (serr
[i
] == ',') {
1888 stop_tls(gsession
, xcred
);
1894 cert_cmd(struct tab
*t
, struct karg
*args
)
1896 const gchar
*uri
, *error_str
= NULL
;
1900 gnutls_session_t gsession
;
1901 gnutls_x509_crt_t
*certs
;
1902 gnutls_certificate_credentials_t xcred
;
1907 if (ssl_ca_file
== NULL
) {
1908 show_oops(t
, "Can't open CA file: %s", ssl_ca_file
);
1912 if ((uri
= get_uri(t
)) == NULL
) {
1913 show_oops(t
, "Invalid URI");
1917 if ((s
= connect_socket_from_uri(uri
, &error_str
, domain
,
1918 sizeof domain
)) == -1) {
1919 show_oops(t
, "%s", error_str
);
1924 if (start_tls(&error_str
, s
, &gsession
, &xcred
))
1928 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1929 show_oops(t
, "get_connection_certs failed");
1933 if (args
->i
& XT_SHOW
)
1934 show_certs(t
, certs
, cert_count
, "Certificate Chain");
1935 else if (args
->i
& XT_SAVE
)
1936 save_certs(t
, certs
, cert_count
, domain
);
1938 free_connection_certs(certs
, cert_count
);
1940 /* we close the socket first for speed */
1943 stop_tls(gsession
, xcred
);
1944 if (error_str
&& strlen(error_str
))
1945 show_oops(t
, "%s", error_str
);
1950 remove_cookie(int index
)
1956 DNPRINTF(XT_D_COOKIE
, "remove_cookie: %d\n", index
);
1958 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
1960 for (i
= 1; cf
; cf
= cf
->next
, i
++) {
1964 print_cookie("remove cookie", c
);
1965 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
1970 soup_cookies_free(cf
);
1976 remove_cookie_domain(int domain_id
)
1978 int domain_count
, rv
= 1;
1983 DNPRINTF(XT_D_COOKIE
, "remove_cookie_domain: %d\n", domain_id
);
1986 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
1988 for (domain_count
= 0; cf
; cf
= cf
->next
) {
1991 if (strcmp(last_domain
, c
->domain
) != 0) {
1993 last_domain
= c
->domain
;
1996 if (domain_count
< domain_id
)
1998 else if (domain_count
> domain_id
)
2001 print_cookie("remove cookie", c
);
2002 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2006 soup_cookies_free(cf
);
2018 DNPRINTF(XT_D_COOKIE
, "remove_cookie_all\n");
2020 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
2022 for (; cf
; cf
= cf
->next
) {
2025 print_cookie("remove cookie", c
);
2026 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2030 soup_cookies_free(cf
);
2036 toplevel_cmd(struct tab
*t
, struct karg
*args
)
2038 js_toggle_cb(t
->js_toggle
, t
);
2044 can_go_back_for_real(struct tab
*t
)
2047 WebKitWebHistoryItem
*item
;
2053 /* rely on webkit to make sure we can go backward when on an about page */
2055 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
2056 return (webkit_web_view_can_go_back(t
->wv
));
2058 /* the back/forwars list is stupid so help determine if we can go back */
2059 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2061 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2062 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
2070 can_go_forward_for_real(struct tab
*t
)
2073 WebKitWebHistoryItem
*item
;
2079 /* rely on webkit to make sure we can go forward when on an about page */
2081 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
2082 return (webkit_web_view_can_go_forward(t
->wv
));
2084 /* the back/forwars list is stupid so help selecting a different item */
2085 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2087 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2088 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
2096 go_back_for_real(struct tab
*t
)
2099 WebKitWebHistoryItem
*item
;
2107 webkit_web_view_go_back(t
->wv
);
2110 /* the back/forwars list is stupid so help selecting a different item */
2111 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2113 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2114 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2115 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2122 go_forward_for_real(struct tab
*t
)
2125 WebKitWebHistoryItem
*item
;
2133 webkit_web_view_go_forward(t
->wv
);
2136 /* the back/forwars list is stupid so help selecting a different item */
2137 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2139 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2140 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2141 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2148 navaction(struct tab
*t
, struct karg
*args
)
2150 WebKitWebHistoryItem
*item
;
2151 WebKitWebFrame
*frame
;
2153 DNPRINTF(XT_D_NAV
, "navaction: tab %d opcode %d\n",
2154 t
->tab_id
, args
->i
);
2157 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
2159 if (args
->i
== XT_NAV_BACK
)
2160 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2162 item
= webkit_web_back_forward_list_get_forward_item(t
->bfl
);
2164 return (XT_CB_PASSTHROUGH
);
2165 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2167 return (XT_CB_PASSTHROUGH
);
2173 go_back_for_real(t
);
2175 case XT_NAV_FORWARD
:
2177 go_forward_for_real(t
);
2180 frame
= webkit_web_view_get_main_frame(t
->wv
);
2181 webkit_web_frame_reload(frame
);
2184 frame
= webkit_web_view_get_main_frame(t
->wv
);
2185 webkit_web_frame_stop_loading(frame
);
2188 return (XT_CB_PASSTHROUGH
);
2192 move(struct tab
*t
, struct karg
*args
)
2194 GtkAdjustment
*adjust
;
2195 double pi
, si
, pos
, ps
, upper
, lower
, max
;
2201 case XT_MOVE_BOTTOM
:
2203 case XT_MOVE_PAGEDOWN
:
2204 case XT_MOVE_PAGEUP
:
2205 case XT_MOVE_HALFDOWN
:
2206 case XT_MOVE_HALFUP
:
2207 case XT_MOVE_PERCENT
:
2208 case XT_MOVE_CENTER
:
2209 adjust
= t
->adjust_v
;
2212 adjust
= t
->adjust_h
;
2216 pos
= gtk_adjustment_get_value(adjust
);
2217 ps
= gtk_adjustment_get_page_size(adjust
);
2218 upper
= gtk_adjustment_get_upper(adjust
);
2219 lower
= gtk_adjustment_get_lower(adjust
);
2220 si
= gtk_adjustment_get_step_increment(adjust
);
2221 pi
= gtk_adjustment_get_page_increment(adjust
);
2224 DNPRINTF(XT_D_MOVE
, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2225 "max %f si %f pi %f\n",
2226 args
->i
, adjust
== t
->adjust_h
? "horizontal" : "vertical",
2227 pos
, ps
, upper
, lower
, max
, si
, pi
);
2233 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2238 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2240 case XT_MOVE_BOTTOM
:
2241 case XT_MOVE_FARRIGHT
:
2242 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2243 gtk_adjustment_set_value(adjust
, max
);
2246 case XT_MOVE_FARLEFT
:
2247 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2248 gtk_adjustment_set_value(adjust
, lower
);
2250 case XT_MOVE_PAGEDOWN
:
2252 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2254 case XT_MOVE_PAGEUP
:
2256 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2258 case XT_MOVE_HALFDOWN
:
2260 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2262 case XT_MOVE_HALFUP
:
2264 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2266 case XT_MOVE_CENTER
:
2267 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2268 args
->s
= g_strdup("50.0");
2270 case XT_MOVE_PERCENT
:
2271 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2272 percent
= atoi(args
->s
) / 100.0;
2273 pos
= max
* percent
;
2274 if (pos
< 0.0 || pos
> max
)
2276 gtk_adjustment_set_value(adjust
, pos
);
2279 return (XT_CB_PASSTHROUGH
);
2282 DNPRINTF(XT_D_MOVE
, "move: new pos %f %f\n", pos
, MIN(pos
, max
));
2284 return (XT_CB_HANDLED
);
2288 url_set_visibility(void)
2292 TAILQ_FOREACH(t
, &tabs
, entry
)
2293 if (show_url
== 0) {
2294 gtk_widget_hide(t
->toolbar
);
2297 gtk_widget_show(t
->toolbar
);
2301 notebook_tab_set_visibility(void)
2303 if (show_tabs
== 0) {
2304 gtk_widget_hide(tab_bar
);
2305 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2307 if (tab_style
== XT_TABS_NORMAL
) {
2308 gtk_widget_hide(tab_bar
);
2309 gtk_notebook_set_show_tabs(notebook
, TRUE
);
2310 } else if (tab_style
== XT_TABS_COMPACT
) {
2311 gtk_widget_show(tab_bar
);
2312 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2318 statusbar_set_visibility(void)
2322 TAILQ_FOREACH(t
, &tabs
, entry
){
2323 if (show_statusbar
== 0)
2324 gtk_widget_hide(t
->statusbar_box
);
2326 gtk_widget_show(t
->statusbar_box
);
2333 url_set(struct tab
*t
, int enable_url_entry
)
2338 show_url
= enable_url_entry
;
2340 if (enable_url_entry
) {
2341 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
2342 GTK_ENTRY_ICON_PRIMARY
, NULL
);
2343 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
), 0);
2345 pixbuf
= gtk_entry_get_icon_pixbuf(GTK_ENTRY(t
->uri_entry
),
2346 GTK_ENTRY_ICON_PRIMARY
);
2348 gtk_entry_get_progress_fraction(GTK_ENTRY(t
->uri_entry
));
2349 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
2350 GTK_ENTRY_ICON_PRIMARY
, pixbuf
);
2351 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
2357 fullscreen(struct tab
*t
, struct karg
*args
)
2359 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2362 return (XT_CB_PASSTHROUGH
);
2364 if (show_url
== 0) {
2372 url_set_visibility();
2373 notebook_tab_set_visibility();
2375 return (XT_CB_HANDLED
);
2379 statustoggle(struct tab
*t
, struct karg
*args
)
2381 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2383 if (show_statusbar
== 1) {
2385 statusbar_set_visibility();
2386 } else if (show_statusbar
== 0) {
2388 statusbar_set_visibility();
2390 return (XT_CB_HANDLED
);
2394 urlaction(struct tab
*t
, struct karg
*args
)
2396 int rv
= XT_CB_HANDLED
;
2398 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2401 return (XT_CB_PASSTHROUGH
);
2405 if (show_url
== 0) {
2407 url_set_visibility();
2411 if (show_url
== 1) {
2413 url_set_visibility();
2421 tabaction(struct tab
*t
, struct karg
*args
)
2423 int rv
= XT_CB_HANDLED
;
2424 char *url
= args
->s
;
2428 DNPRINTF(XT_D_TAB
, "tabaction: %p %d\n", t
, args
->i
);
2431 return (XT_CB_PASSTHROUGH
);
2435 if (strlen(url
) > 0)
2436 create_new_tab(url
, NULL
, 1, args
->precount
);
2438 create_new_tab(NULL
, NULL
, 1, args
->precount
);
2441 if (args
->precount
< 0)
2444 TAILQ_FOREACH(tt
, &tabs
, entry
)
2445 if (tt
->tab_id
== args
->precount
- 1) {
2450 case XT_TAB_DELQUIT
:
2451 if (gtk_notebook_get_n_pages(notebook
) > 1)
2457 if (strlen(url
) > 0)
2460 rv
= XT_CB_PASSTHROUGH
;
2466 if (show_tabs
== 0) {
2468 notebook_tab_set_visibility();
2472 if (show_tabs
== 1) {
2474 notebook_tab_set_visibility();
2477 case XT_TAB_NEXTSTYLE
:
2478 if (tab_style
== XT_TABS_NORMAL
) {
2479 tab_style
= XT_TABS_COMPACT
;
2480 recolor_compact_tabs();
2483 tab_style
= XT_TABS_NORMAL
;
2484 notebook_tab_set_visibility();
2486 case XT_TAB_UNDO_CLOSE
:
2487 if (undo_count
== 0) {
2488 DNPRINTF(XT_D_TAB
, "%s: no tabs to undo close",
2493 u
= TAILQ_FIRST(&undos
);
2494 create_new_tab(u
->uri
, u
, 1, -1);
2496 TAILQ_REMOVE(&undos
, u
, entry
);
2498 /* u->history is freed in create_new_tab() */
2502 case XT_TAB_LOAD_IMAGES
:
2504 if (!auto_load_images
) {
2506 /* Enable auto-load images (this will load all
2507 * previously unloaded images). */
2508 g_object_set(G_OBJECT(t
->settings
),
2509 "auto-load-images", TRUE
, (char *)NULL
);
2510 webkit_web_view_set_settings(t
->wv
, t
->settings
);
2512 webkit_web_view_reload(t
->wv
);
2514 /* Webkit triggers an event when we change the setting,
2515 * so we can't disable the auto-loading at once.
2517 * Unfortunately, webkit does not tell us when it's done.
2518 * Instead, we wait until the next request, and then
2519 * disable autoloading again.
2521 t
->load_images
= TRUE
;
2525 rv
= XT_CB_PASSTHROUGH
;
2539 resizetab(struct tab
*t
, struct karg
*args
)
2541 if (t
== NULL
|| args
== NULL
) {
2542 show_oops(NULL
, "resizetab invalid parameters");
2543 return (XT_CB_PASSTHROUGH
);
2546 DNPRINTF(XT_D_TAB
, "resizetab: tab %d %d\n",
2547 t
->tab_id
, args
->i
);
2549 setzoom_webkit(t
, args
->i
);
2551 return (XT_CB_HANDLED
);
2555 movetab(struct tab
*t
, struct karg
*args
)
2559 if (t
== NULL
|| args
== NULL
) {
2560 show_oops(NULL
, "movetab invalid parameters");
2561 return (XT_CB_PASSTHROUGH
);
2564 DNPRINTF(XT_D_TAB
, "movetab: tab %d opcode %d\n",
2565 t
->tab_id
, args
->i
);
2567 if (args
->i
>= XT_TAB_INVALID
)
2568 return (XT_CB_PASSTHROUGH
);
2570 if (TAILQ_EMPTY(&tabs
))
2571 return (XT_CB_PASSTHROUGH
);
2573 n
= gtk_notebook_get_n_pages(notebook
);
2574 dest
= gtk_notebook_get_current_page(notebook
);
2578 if (args
->precount
< 0)
2579 dest
= dest
== n
- 1 ? 0 : dest
+ 1;
2581 dest
= args
->precount
- 1;
2585 if (args
->precount
< 0)
2588 dest
-= args
->precount
% n
;
2601 return (XT_CB_PASSTHROUGH
);
2604 if (dest
< 0 || dest
>= n
)
2605 return (XT_CB_PASSTHROUGH
);
2606 if (t
->tab_id
== dest
) {
2607 DNPRINTF(XT_D_TAB
, "movetab: do nothing\n");
2608 return (XT_CB_HANDLED
);
2611 set_current_tab(dest
);
2613 return (XT_CB_HANDLED
);
2620 command(struct tab
*t
, struct karg
*args
)
2622 char *s
= NULL
, *ss
= NULL
;
2627 if (t
== NULL
|| args
== NULL
) {
2628 show_oops(NULL
, "command invalid parameters");
2629 return (XT_CB_PASSTHROUGH
);
2640 if (cmd_prefix
== 0)
2643 ss
= g_strdup_printf(":%d", cmd_prefix
);
2649 t
->mode
= XT_MODE_HINT
;
2650 bzero(&a
, sizeof a
);
2656 t
->mode
= XT_MODE_HINT
;
2657 bzero(&a
, sizeof a
);
2658 a
.i
= XT_HINT_NEWTAB
;
2668 case XT_CMD_OPEN_CURRENT
:
2671 case XT_CMD_TABNEW_CURRENT
:
2672 if (!s
) /* FALL THROUGH? */
2674 if ((uri
= get_uri(t
)) != NULL
) {
2675 ss
= g_strdup_printf("%s%s", s
, uri
);
2680 show_oops(t
, "command: invalid opcode %d", args
->i
);
2681 return (XT_CB_PASSTHROUGH
);
2684 DNPRINTF(XT_D_CMD
, "%s: tab %d type %s\n", __func__
, t
->tab_id
, s
);
2686 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), s
);
2687 text
= gdk_color_to_string(&t
->default_style
->text
[GTK_STATE_NORMAL
]);
2688 base
= gdk_color_to_string(&t
->default_style
->base
[GTK_STATE_NORMAL
]);
2689 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
2690 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
2691 statusbar_modify_attr(t
, text
, base
);
2695 gtk_widget_grab_focus(GTK_WIDGET(t
->cmd
));
2696 gtk_editable_set_position(GTK_EDITABLE(t
->cmd
), -1);
2701 return (XT_CB_HANDLED
);
2705 search(struct tab
*t
, struct karg
*args
)
2709 if (t
== NULL
|| args
== NULL
) {
2710 show_oops(NULL
, "search invalid parameters");
2715 case XT_SEARCH_NEXT
:
2716 d
= t
->search_forward
;
2718 case XT_SEARCH_PREV
:
2719 d
= !t
->search_forward
;
2722 return (XT_CB_PASSTHROUGH
);
2725 if (t
->search_text
== NULL
) {
2726 if (global_search
== NULL
)
2727 return (XT_CB_PASSTHROUGH
);
2729 d
= t
->search_forward
= TRUE
;
2730 t
->search_text
= g_strdup(global_search
);
2731 webkit_web_view_mark_text_matches(t
->wv
, global_search
, FALSE
, 0);
2732 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
2736 DNPRINTF(XT_D_CMD
, "search: tab %d opc %d forw %d text %s\n",
2737 t
->tab_id
, args
->i
, t
->search_forward
, t
->search_text
);
2739 webkit_web_view_search_text(t
->wv
, t
->search_text
, FALSE
, d
, TRUE
);
2741 return (XT_CB_HANDLED
);
2745 session_save(struct tab
*t
, char *filename
)
2751 if (strlen(filename
) == 0)
2754 if (filename
[0] == '.' || filename
[0] == '/')
2758 if (save_tabs(t
, &a
))
2760 strlcpy(named_session
, filename
, sizeof named_session
);
2762 /* add the new session to the list of sessions */
2763 s
= g_malloc(sizeof(struct session
));
2764 s
->name
= g_strdup(filename
);
2765 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
2773 session_open(struct tab
*t
, char *filename
)
2778 if (strlen(filename
) == 0)
2781 if (filename
[0] == '.' || filename
[0] == '/')
2785 a
.i
= XT_SES_CLOSETABS
;
2786 if (open_tabs(t
, &a
))
2789 strlcpy(named_session
, filename
, sizeof named_session
);
2797 session_delete(struct tab
*t
, char *filename
)
2799 char file
[PATH_MAX
];
2803 if (strlen(filename
) == 0)
2806 if (filename
[0] == '.' || filename
[0] == '/')
2809 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, filename
);
2813 if (!strcmp(filename
, named_session
))
2814 strlcpy(named_session
, XT_SAVED_TABS_FILE
,
2815 sizeof named_session
);
2817 /* remove session from sessions list */
2818 TAILQ_FOREACH(s
, &sessions
, entry
) {
2819 if (!strcmp(s
->name
, filename
))
2824 TAILQ_REMOVE(&sessions
, s
, entry
);
2825 g_free((gpointer
) s
->name
);
2834 session_cmd(struct tab
*t
, struct karg
*args
)
2836 char *filename
= args
->s
;
2841 if (args
->i
& XT_SHOW
)
2842 show_oops(t
, "Current session: %s", named_session
[0] == '\0' ?
2843 XT_SAVED_TABS_FILE
: named_session
);
2844 else if (args
->i
& XT_SAVE
) {
2845 if (session_save(t
, filename
)) {
2846 show_oops(t
, "Can't save session: %s",
2847 filename
? filename
: "INVALID");
2850 } else if (args
->i
& XT_OPEN
) {
2851 if (session_open(t
, filename
)) {
2852 show_oops(t
, "Can't open session: %s",
2853 filename
? filename
: "INVALID");
2856 } else if (args
->i
& XT_DELETE
) {
2857 if (session_delete(t
, filename
)) {
2858 show_oops(t
, "Can't delete session: %s",
2859 filename
? filename
: "INVALID");
2864 return (XT_CB_PASSTHROUGH
);
2868 script_cmd(struct tab
*t
, struct karg
*args
)
2877 if ((f
= fopen(args
->s
, "r")) == NULL
) {
2878 show_oops(t
, "Can't open script file: %s", args
->s
);
2882 if (fstat(fileno(f
), &sb
) == -1) {
2883 show_oops(t
, "Can't stat script file: %s", args
->s
);
2887 buf
= g_malloc0(sb
.st_size
+ 1);
2888 if (fread(buf
, 1, sb
.st_size
, f
) != sb
.st_size
) {
2889 show_oops(t
, "Can't read script file: %s", args
->s
);
2893 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
2902 return (XT_CB_PASSTHROUGH
);
2906 * Make a hardcopy of the page
2909 print_page(struct tab
*t
, struct karg
*args
)
2911 WebKitWebFrame
*frame
;
2913 GtkPrintOperation
*op
;
2914 GtkPrintOperationAction action
;
2915 GtkPrintOperationResult print_res
;
2916 GError
*g_err
= NULL
;
2917 int marg_l
, marg_r
, marg_t
, marg_b
;
2919 DNPRINTF(XT_D_PRINTING
, "%s:", __func__
);
2921 ps
= gtk_page_setup_new();
2922 op
= gtk_print_operation_new();
2923 action
= GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
;
2924 frame
= webkit_web_view_get_main_frame(t
->wv
);
2926 /* the default margins are too small, so we will bump them */
2927 marg_l
= gtk_page_setup_get_left_margin(ps
, GTK_UNIT_MM
) +
2928 XT_PRINT_EXTRA_MARGIN
;
2929 marg_r
= gtk_page_setup_get_right_margin(ps
, GTK_UNIT_MM
) +
2930 XT_PRINT_EXTRA_MARGIN
;
2931 marg_t
= gtk_page_setup_get_top_margin(ps
, GTK_UNIT_MM
) +
2932 XT_PRINT_EXTRA_MARGIN
;
2933 marg_b
= gtk_page_setup_get_bottom_margin(ps
, GTK_UNIT_MM
) +
2934 XT_PRINT_EXTRA_MARGIN
;
2937 gtk_page_setup_set_left_margin(ps
, marg_l
, GTK_UNIT_MM
);
2938 gtk_page_setup_set_right_margin(ps
, marg_r
, GTK_UNIT_MM
);
2939 gtk_page_setup_set_top_margin(ps
, marg_t
, GTK_UNIT_MM
);
2940 gtk_page_setup_set_bottom_margin(ps
, marg_b
, GTK_UNIT_MM
);
2942 gtk_print_operation_set_default_page_setup(op
, ps
);
2944 /* this appears to free 'op' and 'ps' */
2945 print_res
= webkit_web_frame_print_full(frame
, op
, action
, &g_err
);
2947 /* check it worked */
2948 if (print_res
== GTK_PRINT_OPERATION_RESULT_ERROR
) {
2949 show_oops(NULL
, "can't print: %s", g_err
->message
);
2950 g_error_free (g_err
);
2958 go_home(struct tab
*t
, struct karg
*args
)
2965 set_encoding(struct tab
*t
, struct karg
*args
)
2969 if (args
->s
&& strlen(g_strstrip(args
->s
)) == 0) {
2970 e
= webkit_web_view_get_custom_encoding(t
->wv
);
2972 e
= webkit_web_view_get_encoding(t
->wv
);
2973 show_oops(t
, "encoding: %s", e
? e
: "N/A");
2975 webkit_web_view_set_custom_encoding(t
->wv
, args
->s
);
2981 restart(struct tab
*t
, struct karg
*args
)
2985 a
.s
= XT_RESTART_TABS_FILE
;
2987 execvp(start_argv
[0], start_argv
);
2993 char *http_proxy_save
; /* not a setting, used to toggle */
2996 proxy_cmd(struct tab
*t
, struct karg
*args
)
2998 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
3005 if (http_proxy_save
)
3006 g_free(http_proxy_save
);
3007 http_proxy_save
= g_strdup(http_proxy
);
3010 if (args
->i
& XT_PRXY_SHOW
) {
3012 show_oops(t
, "http_proxy = %s", http_proxy
);
3014 show_oops(t
, "proxy is currently disabled");
3015 } else if (args
->i
& XT_PRXY_TOGGLE
) {
3016 if (http_proxy_save
== NULL
&& http_proxy
== NULL
) {
3017 show_oops(t
, "can't toggle proxy");
3023 setup_proxy(http_proxy_save
);
3026 return (XT_CB_PASSTHROUGH
);
3031 int (*func
)(struct tab
*, struct karg
*);
3035 { "command_mode", 0, command_mode
, XT_MODE_COMMAND
, 0 },
3036 { "insert_mode", 0, command_mode
, XT_MODE_INSERT
, 0 },
3037 { "command", 0, command
, ':', 0 },
3038 { "search", 0, command
, '/', 0 },
3039 { "searchb", 0, command
, '?', 0 },
3040 { "hinting", 0, command
, '.', 0 },
3041 { "hinting_newtab", 0, command
, ',', 0 },
3042 { "togglesrc", 0, toggle_src
, 0, 0 },
3043 { "editsrc", 0, edit_src
, 0, 0 },
3044 { "editelement", 0, edit_element
, 0, 0 },
3045 { "passthrough", 0, passthrough
, 0, 0 },
3046 { "modurl", 0, modurl
, 0, 0 },
3048 /* yanking and pasting */
3049 { "yankuri", 0, yank_uri
, 0, 0 },
3050 /* XXX: pasteuri{cur,new} do not work from the cmd_entry? */
3051 { "pasteuricur", 0, paste_uri
, XT_PASTE_CURRENT_TAB
, 0 },
3052 { "pasteurinew", 0, paste_uri
, XT_PASTE_NEW_TAB
, 0 },
3055 { "searchnext", 0, search
, XT_SEARCH_NEXT
, 0 },
3056 { "searchprevious", 0, search
, XT_SEARCH_PREV
, 0 },
3059 { "focusaddress", 0, focus
, XT_FOCUS_URI
, 0 },
3060 { "focussearch", 0, focus
, XT_FOCUS_SEARCH
, 0 },
3063 { "hinting", 0, hint
, 0, 0 },
3064 { "hinting_newtab", 0, hint
, XT_HINT_NEWTAB
, 0 },
3066 /* custom stylesheet */
3067 { "userstyle", 0, userstyle
, XT_STYLE_CURRENT_TAB
, 0 },
3068 { "userstyle_global", 0, userstyle
, XT_STYLE_GLOBAL
, 0 },
3071 { "goback", 0, navaction
, XT_NAV_BACK
, 0 },
3072 { "goforward", 0, navaction
, XT_NAV_FORWARD
, 0 },
3073 { "reload", 0, navaction
, XT_NAV_RELOAD
, 0 },
3074 { "stop", 0, navaction
, XT_NAV_STOP
, 0 },
3076 /* vertical movement */
3077 { "scrolldown", 0, move
, XT_MOVE_DOWN
, 0 },
3078 { "scrollup", 0, move
, XT_MOVE_UP
, 0 },
3079 { "scrollbottom", 0, move
, XT_MOVE_BOTTOM
, 0 },
3080 { "scrolltop", 0, move
, XT_MOVE_TOP
, 0 },
3081 { "1", 0, move
, XT_MOVE_TOP
, 0 },
3082 { "scrollhalfdown", 0, move
, XT_MOVE_HALFDOWN
, 0 },
3083 { "scrollhalfup", 0, move
, XT_MOVE_HALFUP
, 0 },
3084 { "scrollpagedown", 0, move
, XT_MOVE_PAGEDOWN
, 0 },
3085 { "scrollpageup", 0, move
, XT_MOVE_PAGEUP
, 0 },
3086 /* horizontal movement */
3087 { "scrollright", 0, move
, XT_MOVE_RIGHT
, 0 },
3088 { "scrollleft", 0, move
, XT_MOVE_LEFT
, 0 },
3089 { "scrollfarright", 0, move
, XT_MOVE_FARRIGHT
, 0 },
3090 { "scrollfarleft", 0, move
, XT_MOVE_FARLEFT
, 0 },
3092 { "favorites", 0, xtp_page_fl
, 0, 0 },
3093 { "fav", 0, xtp_page_fl
, 0, 0 },
3094 { "favadd", 0, add_favorite
, 0, 0 },
3096 { "qall", 0, quit
, 0, 0 },
3097 { "quitall", 0, quit
, 0, 0 },
3098 { "w", 0, save_tabs
, 0, 0 },
3099 { "wq", 0, save_tabs_and_quit
, 0, 0 },
3100 { "help", 0, help
, 0, 0 },
3101 { "about", 0, about
, 0, 0 },
3102 { "stats", 0, stats
, 0, 0 },
3103 { "version", 0, about
, 0, 0 },
3106 { "js", 0, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3107 { "save", 1, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3108 { "domain", 2, js_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3109 { "fqdn", 2, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3110 { "show", 1, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3111 { "all", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3112 { "persistent", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3113 { "session", 2, js_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3114 { "toggle", 1, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3115 { "domain", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3116 { "fqdn", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3118 /* cookie command */
3119 { "cookie", 0, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3120 { "save", 1, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3121 { "domain", 2, cookie_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3122 { "fqdn", 2, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3123 { "show", 1, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3124 { "all", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3125 { "persistent", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3126 { "session", 2, cookie_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3127 { "toggle", 1, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3128 { "domain", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3129 { "fqdn", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3130 { "purge", 1, cookie_cmd
, XT_DELETE
, 0 },
3132 /* plugin command */
3133 { "plugin", 0, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3134 { "save", 1, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3135 { "domain", 2, pl_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3136 { "fqdn", 2, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3137 { "show", 1, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3138 { "all", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3139 { "persistent", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3140 { "session", 2, pl_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3141 { "toggle", 1, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3142 { "domain", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3143 { "fqdn", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3145 /* toplevel (domain) command */
3146 { "toplevel", 0, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3147 { "toggle", 1, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3150 { "cookiejar", 0, xtp_page_cl
, 0, 0 },
3153 { "cert", 0, cert_cmd
, XT_SHOW
, 0 },
3154 { "save", 1, cert_cmd
, XT_SAVE
, 0 },
3155 { "show", 1, cert_cmd
, XT_SHOW
, 0 },
3157 { "ca", 0, ca_cmd
, 0, 0 },
3158 { "downloadmgr", 0, xtp_page_dl
, 0, 0 },
3159 { "dl", 0, xtp_page_dl
, 0, 0 },
3160 { "h", 0, xtp_page_hl
, 0, 0 },
3161 { "history", 0, xtp_page_hl
, 0, 0 },
3162 { "home", 0, go_home
, 0, 0 },
3163 { "restart", 0, restart
, 0, 0 },
3164 { "urlhide", 0, urlaction
, XT_URL_HIDE
, 0 },
3165 { "urlshow", 0, urlaction
, XT_URL_SHOW
, 0 },
3166 { "statustoggle", 0, statustoggle
, 0, 0 },
3167 { "run_script", 0, run_page_script
, 0, XT_USERARG
},
3169 { "print", 0, print_page
, 0, 0 },
3172 { "focusin", 0, resizetab
, XT_ZOOM_IN
, 0 },
3173 { "focusout", 0, resizetab
, XT_ZOOM_OUT
, 0 },
3174 { "focusreset", 0, resizetab
, XT_ZOOM_NORMAL
, 0 },
3175 { "q", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3176 { "quit", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3177 { "open", 0, tabaction
, XT_TAB_OPEN
, XT_URLARG
},
3178 { "tabclose", 0, tabaction
, XT_TAB_DELETE
, XT_PREFIX
| XT_INTARG
},
3179 { "tabedit", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3180 { "tabfirst", 0, movetab
, XT_TAB_FIRST
, 0 },
3181 { "tabhide", 0, tabaction
, XT_TAB_HIDE
, 0 },
3182 { "tablast", 0, movetab
, XT_TAB_LAST
, 0 },
3183 { "tabnew", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3184 { "tabnext", 0, movetab
, XT_TAB_NEXT
, XT_PREFIX
| XT_INTARG
},
3185 { "tabnextstyle", 0, tabaction
, XT_TAB_NEXTSTYLE
, 0 },
3186 { "tabprevious", 0, movetab
, XT_TAB_PREV
, XT_PREFIX
| XT_INTARG
},
3187 { "tabrewind", 0, movetab
, XT_TAB_FIRST
, 0 },
3188 { "tabshow", 0, tabaction
, XT_TAB_SHOW
, 0 },
3189 { "tabs", 0, buffers
, 0, 0 },
3190 { "tabundoclose", 0, tabaction
, XT_TAB_UNDO_CLOSE
, 0 },
3191 { "buffers", 0, buffers
, 0, 0 },
3192 { "ls", 0, buffers
, 0, 0 },
3193 { "encoding", 0, set_encoding
, 0, XT_USERARG
},
3194 { "loadimages", 0, tabaction
, XT_TAB_LOAD_IMAGES
, 0 },
3196 /* command aliases (handy when -S flag is used) */
3197 { "promptopen", 0, command
, XT_CMD_OPEN
, 0 },
3198 { "promptopencurrent", 0, command
, XT_CMD_OPEN_CURRENT
, 0 },
3199 { "prompttabnew", 0, command
, XT_CMD_TABNEW
, 0 },
3200 { "prompttabnewcurrent",0, command
, XT_CMD_TABNEW_CURRENT
, 0 },
3203 { "set", 0, set
, 0, XT_SETARG
},
3205 { "fullscreen", 0, fullscreen
, 0, 0 },
3206 { "f", 0, fullscreen
, 0, 0 },
3209 { "session", 0, session_cmd
, XT_SHOW
, 0 },
3210 { "delete", 1, session_cmd
, XT_DELETE
, XT_SESSARG
},
3211 { "open", 1, session_cmd
, XT_OPEN
, XT_SESSARG
},
3212 { "save", 1, session_cmd
, XT_SAVE
, XT_USERARG
},
3213 { "show", 1, session_cmd
, XT_SHOW
, 0 },
3215 /* external javascript */
3216 { "script", 0, script_cmd
, XT_EJS_SHOW
, XT_USERARG
},
3219 { "inspector", 0, inspector_cmd
, XT_INS_SHOW
, 0 },
3220 { "show", 1, inspector_cmd
, XT_INS_SHOW
, 0 },
3221 { "hide", 1, inspector_cmd
, XT_INS_HIDE
, 0 },
3224 { "proxy", 0, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3225 { "show", 1, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3226 { "toggle", 1, proxy_cmd
, XT_PRXY_TOGGLE
, 0 },
3233 } cmd_status
= {-1, 0};
3236 wv_release_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3239 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 1)
3246 wv_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3249 WebKitHitTestResult
*hit_test_result
;
3252 hit_test_result
= webkit_web_view_get_hit_test_result(t
->wv
, e
);
3253 g_object_get(hit_test_result
, "context", &context
, NULL
);
3258 if (context
& WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE
)
3259 t
->mode
= XT_MODE_INSERT
;
3261 t
->mode
= XT_MODE_COMMAND
;
3263 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3265 else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 8 /* btn 4 */) {
3271 } else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 9 /* btn 5 */) {
3273 a
.i
= XT_NAV_FORWARD
;
3283 tab_close_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3285 DNPRINTF(XT_D_TAB
, "tab_close_cb: tab %d\n", t
->tab_id
);
3287 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3296 activate_uri_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3298 const gchar
*uri
= gtk_entry_get_text(GTK_ENTRY(entry
));
3300 DNPRINTF(XT_D_URL
, "activate_uri_entry_cb: %s\n", uri
);
3303 show_oops(NULL
, "activate_uri_entry_cb invalid parameters");
3308 show_oops(t
, "activate_uri_entry_cb no uri");
3312 uri
+= strspn(uri
, "\t ");
3314 /* if xxxt:// treat specially */
3315 if (parse_xtp_url(t
, uri
))
3318 /* otherwise continue to load page normally */
3319 load_uri(t
, (gchar
*)uri
);
3324 activate_search_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3326 const gchar
*search
= gtk_entry_get_text(GTK_ENTRY(entry
));
3327 char *newuri
= NULL
;
3330 DNPRINTF(XT_D_URL
, "activate_search_entry_cb: %s\n", search
);
3333 show_oops(NULL
, "activate_search_entry_cb invalid parameters");
3337 if (search_string
== NULL
|| strlen(search_string
) == 0) {
3338 show_oops(t
, "no search_string");
3342 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
3344 enc_search
= soup_uri_encode(search
, XT_RESERVED_CHARS
);
3345 newuri
= g_strdup_printf(search_string
, enc_search
);
3349 load_uri(t
, newuri
);
3357 check_and_set_cookie(const gchar
*uri
, struct tab
*t
)
3359 struct domain
*d
= NULL
;
3362 if (uri
== NULL
|| t
== NULL
)
3365 if ((d
= wl_find_uri(uri
, &c_wl
)) == NULL
)
3370 DNPRINTF(XT_D_COOKIE
, "check_and_set_cookie: %s %s\n",
3371 es
? "enable" : "disable", uri
);
3373 g_object_set(G_OBJECT(t
->settings
),
3374 "enable-html5-local-storage", es
, (char *)NULL
);
3375 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3379 check_and_set_js(const gchar
*uri
, struct tab
*t
)
3381 struct domain
*d
= NULL
;
3384 if (uri
== NULL
|| t
== NULL
)
3387 if ((d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3392 DNPRINTF(XT_D_JS
, "check_and_set_js: %s %s\n",
3393 es
? "enable" : "disable", uri
);
3395 g_object_set(G_OBJECT(t
->settings
),
3396 "enable-scripts", es
, (char *)NULL
);
3397 g_object_set(G_OBJECT(t
->settings
),
3398 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
3399 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3401 button_set_stockid(t
->js_toggle
,
3402 es
? GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
);
3406 check_and_set_pl(const gchar
*uri
, struct tab
*t
)
3408 struct domain
*d
= NULL
;
3411 if (uri
== NULL
|| t
== NULL
)
3414 if ((d
= wl_find_uri(uri
, &pl_wl
)) == NULL
)
3419 DNPRINTF(XT_D_JS
, "check_and_set_pl: %s %s\n",
3420 es
? "enable" : "disable", uri
);
3422 g_object_set(G_OBJECT(t
->settings
),
3423 "enable-plugins", es
, (char *)NULL
);
3424 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3428 color_address_bar(gpointer p
)
3431 struct tab
*tt
, *t
= p
;
3432 gchar
*col_str
= XT_COLOR_WHITE
, *text
, *base
;
3433 const gchar
*uri
, *u
= NULL
, *error_str
= NULL
;
3436 gdk_threads_enter();
3438 DNPRINTF(XT_D_URL
, "%s:\n", __func__
);
3440 /* make sure t still exists */
3443 TAILQ_FOREACH(tt
, &tabs
, entry
)
3449 if ((uri
= get_uri(t
)) == NULL
)
3455 gdk_threads_leave();
3458 col_str
= XT_COLOR_YELLOW
;
3459 switch (load_compare_cert(u
, &error_str
)) {
3461 col_str
= XT_COLOR_BLUE
;
3464 col_str
= XT_COLOR_GREEN
;
3466 case CERT_UNTRUSTED
:
3467 col_str
= XT_COLOR_YELLOW
;
3470 col_str
= XT_COLOR_RED
;
3475 gdk_threads_enter();
3477 /* make sure t isn't deleted */
3478 TAILQ_FOREACH(tt
, &tabs
, entry
)
3485 /* test to see if the user navigated away and canceled the thread */
3486 if (t
->thread
!= g_thread_self())
3488 if ((uri
= get_uri(t
)) == NULL
) {
3492 if (strcmp(uri
, u
)) {
3493 /* make sure we are still the same url */
3500 if (!strcmp(col_str
, XT_COLOR_WHITE
)) {
3501 text
= gdk_color_to_string(
3502 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3503 base
= gdk_color_to_string(
3504 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3505 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3506 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3507 statusbar_modify_attr(t
, text
, base
);
3511 gdk_color_parse(col_str
, &color
);
3512 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3513 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3516 if (error_str
&& error_str
[0] != '\0')
3517 show_oops(t
, "%s", error_str
);
3522 /* t is invalid at this point */
3524 g_free((gpointer
)u
);
3527 gdk_threads_leave();
3532 show_ca_status(struct tab
*t
, const char *uri
)
3535 gchar
*col_str
= XT_COLOR_WHITE
, *text
, *base
;
3537 DNPRINTF(XT_D_URL
, "show_ca_status: %d %s %s\n",
3538 ssl_strict_certs
, ssl_ca_file
, uri
);
3545 if (ssl_ca_file
== NULL
) {
3546 if (g_str_has_prefix(uri
, "http://"))
3548 if (g_str_has_prefix(uri
, "https://")) {
3549 col_str
= XT_COLOR_RED
;
3554 if (g_str_has_prefix(uri
, "http://") ||
3555 !g_str_has_prefix(uri
, "https://"))
3559 * It is not necessary to see if the thread is already running.
3560 * If the thread is in progress setting it to something else aborts it
3564 /* thread the coloring of the address bar */
3565 t
->thread
= g_thread_create((GThreadFunc
)color_address_bar
, t
, TRUE
, NULL
);
3567 color_address_bar(t
);
3574 if (!strcmp(col_str
, XT_COLOR_WHITE
)) {
3575 text
= gdk_color_to_string(
3576 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3577 base
= gdk_color_to_string(
3578 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3579 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3580 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3581 statusbar_modify_attr(t
, text
, base
);
3585 gdk_color_parse(col_str
, &color
);
3586 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3587 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3593 free_favicon(struct tab
*t
)
3595 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p req %p\n",
3596 __func__
, t
->icon_download
, t
->icon_request
);
3598 if (t
->icon_request
)
3599 g_object_unref(t
->icon_request
);
3600 if (t
->icon_dest_uri
)
3601 g_free(t
->icon_dest_uri
);
3603 t
->icon_request
= NULL
;
3604 t
->icon_dest_uri
= NULL
;
3608 xt_icon_from_name(struct tab
*t
, gchar
*name
)
3610 if (!enable_favicon_entry
)
3613 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->uri_entry
),
3614 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3616 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3617 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3619 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3620 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3624 xt_icon_from_pixbuf(struct tab
*t
, GdkPixbuf
*pb
)
3626 GdkPixbuf
*pb_scaled
;
3628 if (gdk_pixbuf_get_width(pb
) > 16 || gdk_pixbuf_get_height(pb
) > 16)
3629 pb_scaled
= gdk_pixbuf_scale_simple(pb
, 16, 16,
3630 GDK_INTERP_BILINEAR
);
3634 if (enable_favicon_entry
) {
3637 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
3638 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3641 if (show_url
== 0) {
3642 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
3643 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3645 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3646 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3649 /* XXX: Only supports the minimal tabs atm. */
3650 if (enable_favicon_tabs
)
3651 gtk_image_set_from_pixbuf(GTK_IMAGE(t
->tab_elems
.favicon
),
3654 if (pb_scaled
!= pb
)
3655 g_object_unref(pb_scaled
);
3659 xt_icon_from_file(struct tab
*t
, char *file
)
3663 if (g_str_has_prefix(file
, "file://"))
3664 file
+= strlen("file://");
3666 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
3668 xt_icon_from_pixbuf(t
, pb
);
3671 xt_icon_from_name(t
, "text-html");
3675 is_valid_icon(char *file
)
3678 const char *mime_type
;
3682 gf
= g_file_new_for_path(file
);
3683 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
3685 mime_type
= g_file_info_get_content_type(fi
);
3686 valid
= g_strcmp0(mime_type
, "image/x-ico") == 0 ||
3687 g_strcmp0(mime_type
, "image/vnd.microsoft.icon") == 0 ||
3688 g_strcmp0(mime_type
, "image/png") == 0 ||
3689 g_strcmp0(mime_type
, "image/gif") == 0 ||
3690 g_strcmp0(mime_type
, "application/octet-stream") == 0;
3698 set_favicon_from_file(struct tab
*t
, char *file
)
3702 if (t
== NULL
|| file
== NULL
)
3705 if (g_str_has_prefix(file
, "file://"))
3706 file
+= strlen("file://");
3707 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading %s\n", __func__
, file
);
3709 if (!stat(file
, &sb
)) {
3710 if (sb
.st_size
== 0 || !is_valid_icon(file
)) {
3711 /* corrupt icon so trash it */
3712 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3715 /* no need to set icon to default here */
3719 xt_icon_from_file(t
, file
);
3723 favicon_download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
3726 WebKitDownloadStatus status
= webkit_download_get_status(download
);
3727 struct tab
*tt
= NULL
, *t
= NULL
;
3730 * find the webview instead of passing in the tab as it could have been
3731 * deleted from underneath us.
3733 TAILQ_FOREACH(tt
, &tabs
, entry
) {
3742 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d status %d\n",
3743 __func__
, t
->tab_id
, status
);
3746 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
3748 t
->icon_download
= NULL
;
3751 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
3754 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
3757 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
3759 DNPRINTF(XT_D_DOWNLOAD
, "%s: freeing favicon %d\n",
3760 __func__
, t
->tab_id
);
3761 t
->icon_download
= NULL
;
3764 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
3767 DNPRINTF(XT_D_DOWNLOAD
, "%s: setting icon to %s\n",
3768 __func__
, t
->icon_dest_uri
);
3769 set_favicon_from_file(t
, t
->icon_dest_uri
);
3770 /* these will be freed post callback */
3771 t
->icon_request
= NULL
;
3772 t
->icon_download
= NULL
;
3780 abort_favicon_download(struct tab
*t
)
3782 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p\n", __func__
, t
->icon_download
);
3784 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
3785 if (t
->icon_download
) {
3786 g_signal_handlers_disconnect_by_func(G_OBJECT(t
->icon_download
),
3787 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3788 webkit_download_cancel(t
->icon_download
);
3789 t
->icon_download
= NULL
;
3794 xt_icon_from_name(t
, "text-html");
3798 notify_icon_loaded_cb(WebKitWebView
*wv
, gchar
*uri
, struct tab
*t
)
3800 DNPRINTF(XT_D_DOWNLOAD
, "%s %s\n", __func__
, uri
);
3802 if (uri
== NULL
|| t
== NULL
)
3805 #if WEBKIT_CHECK_VERSION(1, 4, 0)
3806 /* take icon from WebKitIconDatabase */
3809 pb
= webkit_web_view_get_icon_pixbuf(wv
);
3811 xt_icon_from_pixbuf(t
, pb
);
3814 xt_icon_from_name(t
, "text-html");
3815 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
3816 /* download icon to cache dir */
3817 gchar
*name_hash
, file
[PATH_MAX
];
3820 if (t
->icon_request
) {
3821 DNPRINTF(XT_D_DOWNLOAD
, "%s: download in progress\n", __func__
);
3825 /* check to see if we got the icon in cache */
3826 name_hash
= g_compute_checksum_for_string(G_CHECKSUM_SHA256
, uri
, -1);
3827 snprintf(file
, sizeof file
, "%s" PS
"%s.ico", cache_dir
, name_hash
);
3830 if (!stat(file
, &sb
)) {
3831 if (sb
.st_size
> 0) {
3832 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading from cache %s\n",
3834 set_favicon_from_file(t
, file
);
3838 /* corrupt icon so trash it */
3839 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3844 /* create download for icon */
3845 t
->icon_request
= webkit_network_request_new(uri
);
3846 if (t
->icon_request
== NULL
) {
3847 DNPRINTF(XT_D_DOWNLOAD
, "%s: invalid uri %s\n",
3852 t
->icon_download
= webkit_download_new(t
->icon_request
);
3853 if (t
->icon_download
== NULL
)
3856 /* we have to free icon_dest_uri later */
3857 t
->icon_dest_uri
= g_strdup_printf("file://%s", file
);
3858 webkit_download_set_destination_uri(t
->icon_download
,
3861 if (webkit_download_get_status(t
->icon_download
) ==
3862 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
3863 g_object_unref(t
->icon_request
);
3864 g_free(t
->icon_dest_uri
);
3865 t
->icon_request
= NULL
;
3866 t
->icon_dest_uri
= NULL
;
3870 g_signal_connect(G_OBJECT(t
->icon_download
), "notify::status",
3871 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3873 webkit_download_start(t
->icon_download
);
3878 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3880 const gchar
*uri
= NULL
;
3881 struct history
*h
, find
;
3885 DNPRINTF(XT_D_URL
, "notify_load_status_cb: %d %s\n",
3886 webkit_web_view_get_load_status(wview
),
3887 get_uri(t
) ? get_uri(t
) : "NOTHING");
3890 show_oops(NULL
, "notify_load_status_cb invalid parameters");
3894 switch (webkit_web_view_get_load_status(wview
)) {
3895 case WEBKIT_LOAD_PROVISIONAL
:
3897 abort_favicon_download(t
);
3898 #if GTK_CHECK_VERSION(2, 20, 0)
3899 gtk_widget_show(t
->spinner
);
3900 gtk_spinner_start(GTK_SPINNER(t
->spinner
));
3902 gtk_label_set_text(GTK_LABEL(t
->label
), "Loading");
3904 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), TRUE
);
3906 /* assume we are a new address */
3907 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3908 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3909 text
= gdk_color_to_string(
3910 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3911 base
= gdk_color_to_string(
3912 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3913 statusbar_modify_attr(t
, text
, base
);
3917 /* take focus if we are visible */
3923 /* kill color thread */
3928 case WEBKIT_LOAD_COMMITTED
:
3933 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
3939 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
3941 /* check if js white listing is enabled */
3942 if (enable_plugin_whitelist
)
3943 check_and_set_pl(uri
, t
);
3944 if (enable_cookie_whitelist
)
3945 check_and_set_cookie(uri
, t
);
3946 if (enable_js_whitelist
)
3947 check_and_set_js(uri
, t
);
3953 /* we know enough to autosave the session */
3954 if (session_autosave
) {
3959 show_ca_status(t
, uri
);
3960 run_script(t
, JS_HINTING
);
3961 if (enable_autoscroll
)
3962 run_script(t
, JS_AUTOSCROLL
);
3965 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
3967 if (color_visited_uris
) {
3968 color_visited(t
, color_visited_helper());
3970 /* This colors the links you middle-click (open in new
3971 * tab) in the current tab. */
3972 if (t
->tab_id
!= gtk_notebook_get_current_page(notebook
) &&
3973 (uri
= get_uri(t
)) != NULL
)
3974 color_visited(get_current_tab(),
3975 g_strdup_printf("{'%s' : 'dummy'}", uri
));
3979 case WEBKIT_LOAD_FINISHED
:
3981 if ((uri
= get_uri(t
)) == NULL
)
3984 if (!strncmp(uri
, "http://", strlen("http://")) ||
3985 !strncmp(uri
, "https://", strlen("https://")) ||
3986 !strncmp(uri
, "file://", strlen("file://"))) {
3987 find
.uri
= (gchar
*)uri
;
3988 h
= RB_FIND(history_list
, &hl
, &find
);
3990 insert_history_item(uri
,
3991 get_title(t
, FALSE
), time(NULL
));
3993 h
->time
= time(NULL
);
3996 set_status(t
, (char *)uri
, XT_STATUS_URI
);
3997 #if WEBKIT_CHECK_VERSION(1, 1, 18)
3998 case WEBKIT_LOAD_FAILED
:
4001 #if GTK_CHECK_VERSION(2, 20, 0)
4002 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
4003 gtk_widget_hide(t
->spinner
);
4006 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
4011 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
), TRUE
);
4013 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
),
4014 can_go_back_for_real(t
));
4016 gtk_widget_set_sensitive(GTK_WIDGET(t
->forward
),
4017 can_go_forward_for_real(t
));
4021 notify_title_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
4023 const gchar
*title
= NULL
, *win_title
= NULL
;
4025 title
= get_title(t
, FALSE
);
4026 win_title
= get_title(t
, TRUE
);
4028 gtk_label_set_text(GTK_LABEL(t
->label
), title
);
4029 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
), title
);
4032 if (win_title
&& t
->tab_id
== gtk_notebook_get_current_page(notebook
))
4033 gtk_window_set_title(GTK_WINDOW(main_window
), win_title
);
4037 get_domain(const gchar
*host
)
4042 /* handle silly domains like .co.uk */
4044 if ((x
= strlen(host
)) <= 6)
4045 return (g_strdup(host
));
4047 if (host
[x
- 3] == '.' && host
[x
- 6] == '.') {
4053 return (g_strdup(&host
[x
+ 1]));
4057 p
= g_strrstr(host
, ".");
4059 return (g_strdup(""));
4065 return (g_strdup(p
+ 1));
4067 return (g_strdup(host
));
4071 js_autorun(struct tab
*t
)
4075 size_t got_default
= 0, got_host
= 0;
4077 char deff
[PATH_MAX
], hostf
[PATH_MAX
];
4078 char *js
= NULL
, *jsat
, *domain
= NULL
;
4079 FILE *deffile
= NULL
, *hostfile
= NULL
;
4081 if (enable_js_autorun
== 0)
4086 !(g_str_has_prefix(uri
, "http://") ||
4087 g_str_has_prefix(uri
, "https://")))
4090 su
= soup_uri_new(uri
);
4093 if (!SOUP_URI_VALID_FOR_HTTP(su
))
4096 DNPRINTF(XT_D_JS
, "%s: host: %s domain: %s\n", __func__
,
4098 domain
= get_domain(su
->host
);
4100 snprintf(deff
, sizeof deff
, "%s" PS
"default.js", js_dir
);
4101 if ((deffile
= fopen(deff
, "r")) != NULL
) {
4102 if (fstat(fileno(deffile
), &sb
) == -1) {
4103 show_oops(t
, "can't stat default JS file");
4106 got_default
= sb
.st_size
;
4109 /* try host first followed by domain */
4110 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, su
->host
);
4111 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4112 if ((hostfile
= fopen(hostf
, "r")) == NULL
) {
4113 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, domain
);
4114 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4115 if ((hostfile
= fopen(hostf
, "r")) == NULL
)
4118 DNPRINTF(XT_D_JS
, "file: %s\n", hostf
);
4119 if (fstat(fileno(hostfile
), &sb
) == -1) {
4120 show_oops(t
, "can't stat %s JS file", hostf
);
4123 got_host
= sb
.st_size
;
4126 if (got_default
+ got_host
== 0)
4129 js
= g_malloc0(got_default
+ got_host
+ 1);
4133 if (fread(js
, got_default
, 1, deffile
) != 1) {
4134 show_oops(t
, "default file read error");
4137 jsat
= js
+ got_default
;
4141 if (fread(jsat
, got_host
, 1, hostfile
) != 1) {
4142 show_oops(t
, "host file read error");
4147 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
4148 run_script_locked(t
, js
);
4164 webview_load_finished_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4166 /* autorun some js if enabled */
4174 webview_progress_changed_cb(WebKitWebView
*wv
, int progress
, struct tab
*t
)
4176 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->uri_entry
),
4177 progress
== 100 ? 0 : (double)progress
/ 100);
4178 if (show_url
== 0) {
4179 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
4180 progress
== 100 ? 0 : (double)progress
/ 100);
4183 update_statusbar_position(NULL
, NULL
);
4187 strict_transport_rb_cmp(struct strict_transport
*a
, struct strict_transport
*b
)
4192 /* compare strings from the end */
4193 l1
= strlen(a
->host
);
4194 l2
= strlen(b
->host
);
4198 for (; *p1
== *p2
&& p1
> a
->host
&& p2
> b
->host
;
4203 * Check if we need to do pattern expansion,
4204 * or if we're just keeping the tree in order
4206 if (a
->flags
& XT_STS_FLAGS_EXPAND
&&
4207 b
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) {
4208 /* Check if we're matching the
4209 * 'host.xyz' part in '*.host.xyz'
4211 if (p2
== b
->host
&& (p1
== a
->host
|| *(p1
-1) == '.')) {
4216 if (p1
== a
->host
&& p2
== b
->host
)
4230 RB_GENERATE(strict_transport_tree
, strict_transport
, entry
,
4231 strict_transport_rb_cmp
);
4234 strict_transport_add(const char *domain
, time_t timeout
, int subdomains
)
4236 struct strict_transport
*d
, find
;
4240 if (enable_strict_transport
== FALSE
)
4243 DPRINTF("strict_transport_add(%s,%" PRIi64
",%d)\n", domain
,
4244 (uint64_t)timeout
, subdomains
);
4250 find
.host
= (char *)domain
;
4252 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4256 /* check if update is needed */
4257 if (d
->timeout
== timeout
&&
4258 (d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) == subdomains
)
4261 d
->timeout
= timeout
;
4263 d
->flags
|= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4265 /* We're still initializing */
4266 if (strict_transport_file
== NULL
)
4269 if ((f
= fopen(strict_transport_file
, "w")) == NULL
) {
4271 "can't open strict-transport rules file");
4275 fprintf(f
, "# Generated file - do not update unless you know "
4276 "what you're doing\n");
4277 RB_FOREACH(d
, strict_transport_tree
, &st_tree
) {
4278 if (d
->timeout
< now
)
4280 fprintf(f
, "%s\t%" PRIi64
"\t%d\n", d
->host
,
4281 (uint64_t)d
->timeout
,
4282 d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
);
4286 d
= g_malloc(sizeof *d
);
4287 d
->host
= g_strdup(domain
);
4288 d
->timeout
= timeout
;
4290 d
->flags
= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4293 RB_INSERT(strict_transport_tree
, &st_tree
, d
);
4295 /* We're still initializing */
4296 if (strict_transport_file
== NULL
)
4299 if ((f
= fopen(strict_transport_file
, "a+")) == NULL
) {
4301 "can't open strict-transport rules file");
4305 fseek(f
, 0, SEEK_END
);
4306 fprintf(f
,"%s\t%" PRIi64
"\t%d\n", d
->host
, (uint64_t)timeout
,
4314 strict_transport_check(const char *host
)
4316 static struct strict_transport
*d
= NULL
;
4317 struct strict_transport find
;
4319 if (enable_strict_transport
== FALSE
)
4322 find
.host
= (char *)host
;
4324 /* match for domains that include subdomains */
4325 find
.flags
= XT_STS_FLAGS_EXPAND
;
4327 /* First, check if we're already at the right node */
4328 if (d
!= NULL
&& strict_transport_rb_cmp(&find
, d
) == 0) {
4332 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4340 strict_transport_init()
4342 char file
[PATH_MAX
];
4348 time_t timeout
, now
;
4351 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_STS_FILE
);
4352 if ((f
= fopen(file
, "r")) == NULL
) {
4353 strict_transport_file
= g_strdup(file
);
4364 if ((rule
= fparseln(f
, &len
, NULL
, delim
, 0)) == NULL
) {
4365 if (!feof(f
) || ferror(f
))
4371 /* get second entry */
4372 if ((ptr
= strpbrk(rule
, " \t")) == NULL
)
4376 timeout
= atoi(ptr
);
4378 /* get third entry */
4379 if ((ptr
= strpbrk(ptr
, " \t")) == NULL
)
4383 subdomains
= atoi(ptr
);
4386 strict_transport_add(rule
, timeout
, subdomains
);
4391 strict_transport_file
= g_strdup(file
);
4395 startpage_add("strict-transport rules file ('%s') is corrupt", file
);
4403 strict_transport_security_cb(SoupMessage
*msg
, gpointer data
)
4409 int subdomains
= FALSE
;
4414 sts
= soup_message_headers_get_one(msg
->response_headers
,
4415 "Strict-Transport-Security");
4416 uri
= soup_message_get_uri(msg
);
4418 if (sts
== NULL
|| uri
== NULL
)
4421 if ((ptr
= strcasestr(sts
, "max-age="))) {
4422 ptr
+= strlen("max-age=");
4423 timeout
= atoll(ptr
);
4425 return; /* malformed header - max-age must be included */
4427 if ((ptr
= strcasestr(sts
, "includeSubDomains")))
4430 strict_transport_add(uri
->host
, timeout
+ time(NULL
), subdomains
);
4434 session_rq_cb(SoupSession
*s
, SoupMessage
*msg
, SoupSocket
*socket
,
4444 if (s
== NULL
|| msg
== NULL
)
4447 if (referer_mode
== XT_REFERER_ALWAYS
)
4450 /* Check if referer is set - and what the user requested for referers */
4451 ref
= soup_message_headers_get_one(msg
->request_headers
, "Referer");
4453 DNPRINTF(XT_D_NAV
, "session_rq_cb: Referer: %s\n", ref
);
4454 switch (referer_mode
) {
4455 case XT_REFERER_NEVER
:
4456 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing referer\n");
4457 soup_message_headers_remove(msg
->request_headers
,
4460 case XT_REFERER_SAME_DOMAIN
:
4461 ref_uri
= soup_uri_new(ref
);
4462 dest
= soup_message_get_uri(msg
);
4464 ref_suffix
= tld_get_suffix(ref_uri
->host
);
4465 dest_suffix
= tld_get_suffix(dest
->host
);
4467 if (dest
&& ref_suffix
&& dest_suffix
&&
4468 strcmp(ref_suffix
, dest_suffix
) != 0) {
4469 soup_message_headers_remove(msg
->request_headers
,
4471 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4472 "referer (not same domain) (suffixes: %s - %s)\n",
4473 ref_suffix
, dest_suffix
);
4475 soup_uri_free(ref_uri
);
4477 case XT_REFERER_SAME_FQDN
:
4478 ref_uri
= soup_uri_new(ref
);
4479 dest
= soup_message_get_uri(msg
);
4480 if (dest
&& strcmp(ref_uri
->host
, dest
->host
) != 0) {
4481 soup_message_headers_remove(msg
->request_headers
,
4483 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4484 "referer (not same fqdn) (should be %s)\n",
4487 soup_uri_free(ref_uri
);
4489 case XT_REFERER_CUSTOM
:
4490 DNPRINTF(XT_D_NAV
, "session_rq_cb: setting referer "
4491 "to %s\n", referer_custom
);
4492 soup_message_headers_replace(msg
->request_headers
,
4493 "Referer", referer_custom
);
4498 if (enable_strict_transport
) {
4499 soup_message_add_header_handler(msg
, "finished",
4500 "Strict-Transport-Security",
4501 G_CALLBACK(strict_transport_security_cb
), NULL
);
4506 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
4507 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
4508 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
4511 WebKitWebNavigationReason reason
;
4512 struct domain
*d
= NULL
;
4515 show_oops(NULL
, "webview_npd_cb invalid parameters");
4519 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
4521 webkit_network_request_get_uri(request
));
4523 uri
= (char *)webkit_network_request_get_uri(request
);
4525 if (!auto_load_images
&& t
->load_images
) {
4527 /* Disable autoloading of images, now that we're done loading
4529 g_object_set(G_OBJECT(t
->settings
),
4530 "auto-load-images", FALSE
, (char *)NULL
);
4531 webkit_web_view_set_settings(t
->wv
, t
->settings
);
4533 t
->load_images
= FALSE
;
4536 /* If this is an xtp url, we don't load anything else. */
4537 if (parse_xtp_url(t
, uri
))
4540 if ((t
->mode
== XT_MODE_HINT
&& t
->new_tab
) || t
->ctrl_click
) {
4542 create_new_tab(uri
, NULL
, ctrl_click_focus
, -1);
4543 webkit_web_policy_decision_ignore(pd
);
4544 return (TRUE
); /* we made the decission */
4547 /* Change user agent if more than one has been given. */
4548 if (user_agent_count
> 1) {
4549 struct user_agent
*ua
;
4551 if ((ua
= TAILQ_NEXT(user_agent
, entry
)) == NULL
)
4552 user_agent
= TAILQ_FIRST(&ua_list
);
4556 free(t
->user_agent
);
4557 t
->user_agent
= g_strdup(user_agent
->value
);
4559 DNPRINTF(XT_D_NAV
, "user-agent: %s\n", t
->user_agent
);
4561 g_object_set(G_OBJECT(t
->settings
),
4562 "user-agent", t
->user_agent
, (char *)NULL
);
4564 webkit_web_view_set_settings(wv
, t
->settings
);
4568 * This is a little hairy but it comes down to this:
4569 * when we run in whitelist mode we have to assist the browser in
4570 * opening the URL that it would have opened in a new tab.
4572 reason
= webkit_web_navigation_action_get_reason(na
);
4573 if (reason
== WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED
) {
4574 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
4575 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1)
4576 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4578 webkit_web_policy_decision_use(pd
);
4579 return (TRUE
); /* we made the decision */
4586 webview_rrs_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, WebKitWebResource
*res
,
4587 WebKitNetworkRequest
*request
, WebKitNetworkResponse
*response
,
4593 msg
= webkit_network_request_get_message(request
);
4596 uri
= soup_message_get_uri(msg
);
4599 if (strcmp(soup_uri_get_scheme(uri
), SOUP_URI_SCHEME_HTTP
) == 0) {
4600 if (strict_transport_check(uri
->host
)) {
4601 DNPRINTF(XT_D_NAV
, "webview_rrs_cb: force https for %s\n",
4603 soup_uri_set_scheme(uri
, SOUP_URI_SCHEME_HTTPS
);
4609 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4612 struct domain
*d
= NULL
;
4614 WebKitWebView
*webview
= NULL
;
4617 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
4618 webkit_web_view_get_uri(wv
));
4621 /* open in current tab */
4623 } else if (enable_scripts
== 0 && enable_js_whitelist
== 1) {
4624 uri
= webkit_web_view_get_uri(wv
);
4625 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4628 if (t
->ctrl_click
) {
4629 x
= ctrl_click_focus
;
4632 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4634 } else if (enable_scripts
== 1) {
4635 if (t
->ctrl_click
) {
4636 x
= ctrl_click_focus
;
4639 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4647 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
4650 struct domain
*d
= NULL
;
4652 DNPRINTF(XT_D_NAV
, "webview_close_cb: %d\n", t
->tab_id
);
4654 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
4655 uri
= webkit_web_view_get_uri(wv
);
4656 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4660 } else if (enable_scripts
== 1)
4667 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
4669 /* we can not eat the event without throwing gtk off so defer it */
4671 /* catch middle click */
4672 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 2) {
4677 /* catch ctrl click */
4678 if (e
->type
== GDK_BUTTON_RELEASE
&&
4679 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
4684 return (XT_CB_PASSTHROUGH
);
4688 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
4690 struct mime_type
*m
;
4692 m
= find_mime_type(mime_type
);
4698 return (fork_exec(t
, m
->mt_action
,
4699 webkit_network_request_get_uri(request
),
4700 "can't launch MIME handler", 0));
4704 get_mime_type(const char *file
)
4707 char *mime_type
= NULL
;
4711 if (g_str_has_prefix(file
, "file://"))
4712 file
+= strlen("file://");
4714 gf
= g_file_new_for_path(file
);
4715 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
4717 if ((m
= g_file_info_get_content_type(fi
)) != NULL
)
4718 mime_type
= g_strdup(m
);
4726 run_download_mimehandler(char *mime_type
, char *file
)
4728 struct mime_type
*m
;
4730 m
= find_mime_type(mime_type
);
4734 return (fork_exec(NULL
, m
->mt_action
, file
,
4735 "can't launch download MIME handler", 0));
4739 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
4742 WebKitDownloadStatus status
;
4743 const char *file
= NULL
;
4746 if (download
== NULL
)
4748 status
= webkit_download_get_status(download
);
4749 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
4752 if (download_notifications
)
4753 show_oops(NULL
, "Download of '%s' finished",
4754 basename((char *)webkit_download_get_destination_uri(download
)));
4755 file
= webkit_download_get_destination_uri(download
);
4758 mime
= get_mime_type(file
);
4762 if (g_str_has_prefix(file
, "file://"))
4763 file
+= strlen("file://");
4764 run_download_mimehandler((char *)mime
, (char *)file
);
4769 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
4770 WebKitNetworkRequest
*request
, char *mime_type
,
4771 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
4774 show_oops(NULL
, "webview_mimetype_cb invalid parameters");
4778 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
4779 t
->tab_id
, mime_type
);
4781 if (run_mimehandler(t
, mime_type
, request
) == 0) {
4782 webkit_web_policy_decision_ignore(decision
);
4787 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
4788 webkit_web_policy_decision_download(decision
);
4796 download_start(struct tab
*t
, struct download
*d
, int flag
)
4798 WebKitNetworkRequest
*req
;
4800 const gchar
*suggested_name
;
4801 gchar
*filename
= NULL
;
4806 if (d
== NULL
|| t
== NULL
) {
4807 show_oops(NULL
, "%s invalid parameters", __func__
);
4811 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
4812 if (suggested_name
== NULL
)
4813 return (FALSE
); /* abort download */
4824 filename
= g_strdup_printf("%d%s", i
, suggested_name
);
4827 uri
= g_strdup_printf("%s\\%s", download_dir
, i
?
4828 filename
: suggested_name
);
4830 uri
= g_strdup_printf("file://%s/%s", download_dir
, i
?
4831 filename
: suggested_name
);
4835 } while (!stat(uri
, &sb
));
4837 } while (!stat(uri
+ strlen("file://"), &sb
)); /* XXX is the + strlen right? */
4840 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d filename %s "
4841 "local %s\n", __func__
, t
->tab_id
, filename
, uri
);
4843 /* if we're restarting the download, or starting
4844 * it after doing something else, we need to recreate
4845 * the download request.
4847 if (flag
== XT_DL_RESTART
) {
4848 req
= webkit_network_request_new(webkit_download_get_uri(d
->download
));
4849 webkit_download_cancel(d
->download
);
4850 g_object_unref(d
->download
);
4851 d
->download
= webkit_download_new(req
);
4854 webkit_download_set_destination_uri(d
->download
, uri
);
4856 if (webkit_download_get_status(d
->download
) ==
4857 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
4858 show_oops(t
, "%s: download failed to start", __func__
);
4860 gtk_label_set_text(GTK_LABEL(t
->label
), "Download Failed");
4862 /* connect "download first" mime handler */
4863 g_signal_connect(G_OBJECT(d
->download
), "notify::status",
4864 G_CALLBACK(download_status_changed_cb
), NULL
);
4866 /* get from history */
4867 g_object_ref(d
->download
);
4868 gtk_label_set_text(GTK_LABEL(t
->label
), "Downloading");
4869 if (download_notifications
)
4870 show_oops(t
, "Download of '%s' started...",
4871 basename((char *)webkit_download_get_destination_uri(d
->download
)));
4874 if (flag
!= XT_DL_START
)
4875 webkit_download_start(d
->download
);
4877 DNPRINTF(XT_D_DOWNLOAD
, "download status : %d",
4878 webkit_download_get_status(d
->download
));
4880 /* sync other download manager tabs */
4881 update_download_tabs(NULL
);
4893 download_ask_cb(struct tab
*t
, GdkEventKey
*e
, gpointer data
)
4895 struct download
*d
= data
;
4899 t
->mode_cb_data
= NULL
;
4902 e
->keyval
= GDK_Escape
;
4903 return (XT_CB_PASSTHROUGH
);
4906 DPRINTF("download_ask_cb: User pressed %c\n", e
->keyval
);
4907 if (e
->keyval
== 'y' || e
->keyval
== 'Y' || e
->keyval
== GDK_Return
)
4908 /* We need to do a RESTART, because we're not calling from
4909 * webview_download_cb
4911 download_start(t
, d
, XT_DL_RESTART
);
4913 /* for all other keyvals, we just let the download be */
4914 e
->keyval
= GDK_Escape
;
4915 return (XT_CB_HANDLED
);
4919 download_ask(struct tab
*t
, struct download
*d
)
4921 const gchar
*suggested_name
;
4923 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
4924 if (suggested_name
== NULL
)
4925 return (FALSE
); /* abort download */
4927 show_oops(t
, "download file %s [y/n] ?", suggested_name
);
4928 t
->mode_cb
= download_ask_cb
;
4929 t
->mode_cb_data
= d
;
4935 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
4938 const gchar
*suggested_name
;
4939 struct download
*download_entry
;
4942 if (wk_download
== NULL
|| t
== NULL
) {
4943 show_oops(NULL
, "%s invalid parameters", __func__
);
4947 suggested_name
= webkit_download_get_suggested_filename(wk_download
);
4948 if (suggested_name
== NULL
)
4949 return (FALSE
); /* abort download */
4951 download_entry
= g_malloc(sizeof(struct download
));
4952 download_entry
->download
= wk_download
;
4953 download_entry
->tab
= t
;
4954 download_entry
->id
= next_download_id
++;
4955 RB_INSERT(download_list
, &downloads
, download_entry
);
4957 if (download_mode
== XT_DM_START
)
4958 ret
= download_start(t
, download_entry
, XT_DL_START
);
4959 else if (download_mode
== XT_DM_ASK
)
4960 ret
= download_ask(t
, download_entry
);
4961 else if (download_mode
== XT_DM_ADD
)
4962 show_oops(t
, "added %s to download manager",
4965 /* sync other download manager tabs */
4966 update_download_tabs(NULL
);
4969 * NOTE: never redirect/render the current tab before this
4970 * function returns. This will cause the download to never start.
4972 return (ret
); /* start download */
4976 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
4978 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
4981 show_oops(NULL
, "webview_hover_cb");
4986 set_status(t
, uri
, XT_STATUS_LINK
);
4989 set_status(t
, t
->status
, XT_STATUS_NOTHING
);
4994 mark(struct tab
*t
, struct karg
*arg
)
5001 if ((index
= marktoindex(mark
)) == -1)
5004 if (arg
->i
== XT_MARK_SET
)
5005 t
->mark
[index
] = gtk_adjustment_get_value(t
->adjust_v
);
5006 else if (arg
->i
== XT_MARK_GOTO
) {
5007 if (t
->mark
[index
] == XT_INVALID_MARK
) {
5008 show_oops(t
, "mark '%c' does not exist", mark
);
5011 /* XXX t->mark[index] can be bigger than the maximum if ajax or
5012 something changes the document size */
5013 pos
= gtk_adjustment_get_value(t
->adjust_v
);
5014 gtk_adjustment_set_value(t
->adjust_v
, t
->mark
[index
]);
5015 t
->mark
[marktoindex('\'')] = pos
;
5022 marks_clear(struct tab
*t
)
5026 for (i
= 0; i
< LENGTH(t
->mark
); i
++)
5027 t
->mark
[i
] = XT_INVALID_MARK
;
5033 char file
[PATH_MAX
];
5034 char *line
= NULL
, *p
;
5039 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
5040 if ((f
= fopen(file
, "r+")) == NULL
) {
5041 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
5045 for (i
= 1; ; i
++) {
5046 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
5048 if (strlen(line
) == 0 || line
[0] == '#') {
5054 p
= strtok(line
, " \t");
5056 if (p
== NULL
|| strlen(p
) != 1 ||
5057 (index
= qmarktoindex(*p
)) == -1) {
5058 warnx("corrupt quickmarks file, line %d", i
);
5062 p
= strtok(NULL
, " \t");
5063 if (qmarks
[index
] != NULL
)
5064 g_free(qmarks
[index
]);
5065 qmarks
[index
] = g_strdup(p
);
5076 char file
[PATH_MAX
];
5080 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
5081 if ((f
= fopen(file
, "r+")) == NULL
) {
5082 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
5086 for (i
= 0; i
< XT_NOQMARKS
; i
++)
5087 if (qmarks
[i
] != NULL
)
5088 fprintf(f
, "%c %s\n", indextoqmark(i
), qmarks
[i
]);
5096 qmark(struct tab
*t
, struct karg
*arg
)
5101 mark
= arg
->s
[strlen(arg
->s
)-1];
5102 index
= qmarktoindex(mark
);
5108 if (qmarks
[index
] != NULL
) {
5109 g_free(qmarks
[index
]);
5110 qmarks
[index
] = NULL
;
5113 qmarks_load(); /* sync if multiple instances */
5114 qmarks
[index
] = g_strdup(get_uri(t
));
5118 if (qmarks
[index
] != NULL
)
5119 load_uri(t
, qmarks
[index
]);
5121 show_oops(t
, "quickmark \"%c\" does not exist",
5127 if (qmarks
[index
] != NULL
)
5128 create_new_tab(qmarks
[index
], NULL
, 1, -1);
5130 show_oops(t
, "quickmark \"%c\" does not exist",
5141 go_up(struct tab
*t
, struct karg
*args
)
5149 if (args
->i
== XT_GO_UP_ROOT
)
5150 levels
= XT_GO_UP_ROOT
;
5151 else if ((levels
= atoi(args
->s
)) == 0)
5154 uri
= g_strdup(get_uri(t
));
5158 if ((tmp
= strstr(uri
, XT_PROTO_DELIM
)) == NULL
)
5161 tmp
+= strlen(XT_PROTO_DELIM
);
5163 /* it makes no sense to strip the last slash from ".../dir/", skip it */
5164 lastidx
= strlen(tmp
) - 1;
5166 if (tmp
[lastidx
] == '/')
5167 tmp
[lastidx
] = '\0';
5171 p
= strrchr(tmp
, '/');
5172 if (p
== tmp
) { /* Are we at the root of a file://-path? */
5175 } else if (p
!= NULL
)
5188 gototab(struct tab
*t
, struct karg
*args
)
5191 struct karg arg
= {0, NULL
, -1};
5193 tab
= atoi(args
->s
);
5196 arg
.i
= XT_TAB_NEXT
;
5208 zoom_amount(struct tab
*t
, struct karg
*arg
)
5210 struct karg narg
= {0, NULL
, -1};
5212 narg
.i
= atoi(arg
->s
);
5213 resizetab(t
, &narg
);
5219 flip_colon(struct tab
*t
, struct karg
*arg
)
5221 struct karg narg
= {0, NULL
, -1};
5224 if (t
== NULL
|| arg
== NULL
)
5227 p
= strstr(arg
->s
, ":");
5239 /* buffer commands receive the regex that triggered them in arg.s */
5240 char bcmd
[XT_BUFCMD_SZ
];
5244 #define XT_PRE_NO (0)
5245 #define XT_PRE_YES (1)
5246 #define XT_PRE_MAYBE (2)
5248 int (*func
)(struct tab
*, struct karg
*);
5252 { "^[0-9]*gu$", XT_PRE_MAYBE
, "gu", go_up
, 0 },
5253 { "^gU$", XT_PRE_NO
, "gU", go_up
, XT_GO_UP_ROOT
},
5254 { "^gg$", XT_PRE_NO
, "gg", move
, XT_MOVE_TOP
},
5255 { "^gG$", XT_PRE_NO
, "gG", move
, XT_MOVE_BOTTOM
},
5256 { "^[0-9]+%$", XT_PRE_YES
, "%", move
, XT_MOVE_PERCENT
},
5257 { "^zz$", XT_PRE_NO
, "zz", move
, XT_MOVE_CENTER
},
5258 { "^gh$", XT_PRE_NO
, "gh", go_home
, 0 },
5259 { "^m[a-zA-Z0-9]$", XT_PRE_NO
, "m", mark
, XT_MARK_SET
},
5260 { "^['][a-zA-Z0-9']$", XT_PRE_NO
, "'", mark
, XT_MARK_GOTO
},
5261 { "^[0-9]+t$", XT_PRE_YES
, "t", gototab
, 0 },
5262 { "^g0$", XT_PRE_YES
, "g0", movetab
, XT_TAB_FIRST
},
5263 { "^g[$]$", XT_PRE_YES
, "g$", movetab
, XT_TAB_LAST
},
5264 { "^[0-9]*gt$", XT_PRE_YES
, "t", movetab
, XT_TAB_NEXT
},
5265 { "^[0-9]*gT$", XT_PRE_YES
, "T", movetab
, XT_TAB_PREV
},
5266 { "^M[a-zA-Z0-9]$", XT_PRE_NO
, "M", qmark
, XT_QMARK_SET
},
5267 { "^go[a-zA-Z0-9]$", XT_PRE_NO
, "go", qmark
, XT_QMARK_OPEN
},
5268 { "^gn[a-zA-Z0-9]$", XT_PRE_NO
, "gn", qmark
, XT_QMARK_TAB
},
5269 { "^ZR$", XT_PRE_NO
, "ZR", restart
, 0 },
5270 { "^ZZ$", XT_PRE_NO
, "ZZ", quit
, 0 },
5271 { "^zi$", XT_PRE_NO
, "zi", resizetab
, XT_ZOOM_IN
},
5272 { "^zo$", XT_PRE_NO
, "zo", resizetab
, XT_ZOOM_OUT
},
5273 { "^z0$", XT_PRE_NO
, "z0", resizetab
, XT_ZOOM_NORMAL
},
5274 { "^[0-9]+Z$", XT_PRE_YES
, "Z", zoom_amount
, 0 },
5275 { "^[0-9]+:$", XT_PRE_YES
, ":", flip_colon
, 0 },
5279 buffercmd_init(void)
5283 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5284 if (regcomp(&buffercmds
[i
].cregex
, buffercmds
[i
].regex
,
5285 REG_EXTENDED
| REG_NOSUB
))
5286 startpage_add("invalid buffercmd regex %s",
5287 buffercmds
[i
].regex
);
5291 buffercmd_abort(struct tab
*t
)
5298 DNPRINTF(XT_D_BUFFERCMD
, "%s: clearing buffer\n", __func__
);
5300 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5303 cmd_prefix
= 0; /* clear prefix for non-buffer commands */
5304 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
5308 buffercmd_execute(struct tab
*t
, struct buffercmd
*cmd
)
5310 struct karg arg
= {0, NULL
, -1};
5313 arg
.s
= g_strdup(bcmd
);
5315 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_execute: buffer \"%s\" "
5316 "matches regex \"%s\", executing\n", bcmd
, cmd
->regex
);
5326 buffercmd_addkey(struct tab
*t
, guint keyval
)
5329 char s
[XT_BUFCMD_SZ
];
5331 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
))) {
5333 return (XT_CB_PASSTHROUGH
);
5336 if (keyval
== GDK_Escape
) {
5338 return (XT_CB_HANDLED
);
5341 /* key with modifier or non-ascii character */
5342 if (!isascii(keyval
)) {
5344 * XXX this looks wrong but fixes some sites like
5345 * http://www.seslisozluk.com/
5346 * that eat a shift or ctrl and end putting default focus in js
5347 * instead of ignoring the keystroke
5348 * so instead of return (XT_CB_PASSTHROUGH); eat the key
5350 return (XT_CB_HANDLED
);
5353 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: adding key \"%c\" "
5354 "to buffer \"%s\"\n", keyval
, bcmd
);
5356 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5357 if (bcmd
[i
] == '\0') {
5362 /* buffer full, ignore input */
5363 if (i
>= LENGTH(bcmd
) -1) {
5364 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: buffer full\n");
5366 return (XT_CB_HANDLED
);
5369 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
5371 /* find exact match */
5372 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5373 if (regexec(&buffercmds
[i
].cregex
, bcmd
,
5374 (size_t) 0, NULL
, 0) == 0) {
5375 buffercmd_execute(t
, &buffercmds
[i
]);
5379 /* find non exact matches to see if we need to abort ot not */
5380 for (i
= 0, match
= 0; i
< LENGTH(buffercmds
); i
++) {
5381 DNPRINTF(XT_D_BUFFERCMD
, "trying: %s\n", bcmd
);
5384 if (buffercmds
[i
].precount
== XT_PRE_MAYBE
) {
5385 if (isdigit(bcmd
[0])) {
5386 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5390 if (sscanf(bcmd
, "%s", s
) == 0)
5393 } else if (buffercmds
[i
].precount
== XT_PRE_YES
) {
5394 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5397 if (sscanf(bcmd
, "%s", s
) == 0)
5400 if (c
== -1 && buffercmds
[i
].precount
)
5402 if (!strncmp(s
, buffercmds
[i
].cmd
, strlen(s
)))
5405 DNPRINTF(XT_D_BUFFERCMD
, "got[%d] %d <%s>: %d %s\n",
5406 i
, match
, buffercmds
[i
].cmd
, c
, s
);
5409 DNPRINTF(XT_D_BUFFERCMD
, "aborting: %s\n", bcmd
);
5414 return (XT_CB_HANDLED
);
5418 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
5420 struct key_binding
*k
;
5422 /* handle keybindings if buffercmd is empty.
5423 if not empty, allow commands like C-n */
5424 if (bcmd
[0] == '\0' || ((e
->state
& (CTRL
| MOD1
)) != 0))
5425 TAILQ_FOREACH(k
, &kbl
, entry
)
5426 if (e
->keyval
== k
->key
5427 && (entry
? k
->use_in_entry
: 1)) {
5428 /* when we are edditing eat ctrl/mod keys */
5429 if (edit_mode
== XT_EM_VI
&&
5430 t
->mode
== XT_MODE_INSERT
&&
5431 (e
->state
& CTRL
|| e
->state
& MOD1
))
5432 return (XT_CB_PASSTHROUGH
);
5435 if ((e
->state
& (CTRL
| MOD1
)) == 0)
5436 return (cmd_execute(t
, k
->cmd
));
5437 } else if ((e
->state
& k
->mask
) == k
->mask
) {
5438 return (cmd_execute(t
, k
->cmd
));
5442 if (!entry
&& ((e
->state
& (CTRL
| MOD1
)) == 0))
5443 return buffercmd_addkey(t
, e
->keyval
);
5445 return (XT_CB_PASSTHROUGH
);
5449 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5453 /* don't use w directly; use t->whatever instead */
5456 show_oops(NULL
, "wv_keypress_cb");
5457 return (XT_CB_PASSTHROUGH
);
5463 return t
->mode_cb(t
, e
, t
->mode_cb_data
);
5465 DNPRINTF(XT_D_KEY
, "wv_keypress_cb: mode %d keyval 0x%x mask "
5466 "0x%x tab %d\n", t
->mode
, e
->keyval
, e
->state
, t
->tab_id
);
5468 /* Hide buffers, if they are visible, with escape. */
5469 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)) &&
5470 CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
5471 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5473 return (XT_CB_HANDLED
);
5476 if (t
->mode
== XT_MODE_HINT
)
5477 return (XT_CB_HANDLED
);
5479 if ((CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Tab
) ||
5480 (CLEAN(e
->state
) == SHFT
&& e
->keyval
== GDK_Tab
))
5481 /* something focussy is about to happen */
5482 return (XT_CB_PASSTHROUGH
);
5484 /* check if we are some sort of text input thing in the dom */
5485 input_check_mode(t
);
5487 if (t
->mode
== XT_MODE_HINT
) {
5488 /* XXX make sure cmd entry is enabled */
5489 return (XT_CB_HANDLED
);
5490 } else if (t
->mode
== XT_MODE_PASSTHROUGH
) {
5491 if (CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
)
5492 t
->mode
= XT_MODE_COMMAND
;
5493 return (XT_CB_PASSTHROUGH
);
5494 } else if (t
->mode
== XT_MODE_COMMAND
) {
5496 snprintf(s
, sizeof s
, "%c", e
->keyval
);
5497 if (CLEAN(e
->state
) == 0 && isdigit(s
[0]))
5498 cmd_prefix
= 10 * cmd_prefix
+ atoi(s
);
5499 return (handle_keypress(t
, e
, 0));
5502 return (handle_keypress(t
, e
, 1));
5506 return (XT_CB_PASSTHROUGH
);
5510 hint_continue(struct tab
*t
)
5512 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5514 const gchar
*errstr
= NULL
;
5518 if (!(c
[0] == '.' || c
[0] == ','))
5520 if (strlen(c
) == 1) {
5521 /* XXX should not happen */
5526 if (isdigit(c
[1])) {
5528 i
= strtonum(&c
[1], 1, 4096, &errstr
);
5530 show_oops(t
, "invalid numerical hint %s", &c
[1]);
5533 s
= g_strdup_printf("hints.updateHints(%d);", i
);
5537 /* alphanumeric input */
5538 s
= g_strdup_printf("hints.createHints('%s', '%c');",
5539 &c
[1], c
[0] == '.' ? 'f' : 'F');
5550 search_continue(struct tab
*t
)
5552 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5553 gboolean rv
= FALSE
;
5555 if (c
[0] == ':' || c
[0] == '.' || c
[0] == ',')
5557 if (strlen(c
) == 1) {
5558 webkit_web_view_unmark_text_matches(t
->wv
);
5563 t
->search_forward
= TRUE
;
5564 else if (c
[0] == '?')
5565 t
->search_forward
= FALSE
;
5575 search_cb(struct tab
*t
)
5577 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5580 if (search_continue(t
) == FALSE
)
5584 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, t
->search_forward
,
5586 /* not found, mark red */
5587 gdk_color_parse(XT_COLOR_RED
, &color
);
5588 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
5589 /* unmark and remove selection */
5590 webkit_web_view_unmark_text_matches(t
->wv
);
5591 /* my kingdom for a way to unselect text in webview */
5593 /* found, highlight all */
5594 webkit_web_view_unmark_text_matches(t
->wv
);
5595 webkit_web_view_mark_text_matches(t
->wv
, &c
[1], FALSE
, 0);
5596 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
5597 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
5598 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
5606 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5608 const gchar
*c
= gtk_entry_get_text(w
);
5611 show_oops(NULL
, "cmd_keyrelease_cb invalid parameters");
5612 return (XT_CB_PASSTHROUGH
);
5615 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
5616 e
->keyval
, e
->state
, t
->tab_id
);
5619 if (!(e
->keyval
== GDK_Tab
|| e
->keyval
== GDK_ISO_Left_Tab
)) {
5620 if (hint_continue(t
) == FALSE
)
5625 if (search_continue(t
) == FALSE
)
5628 /* if search length is > 4 then no longer play timeout games */
5629 if (strlen(c
) > 4) {
5631 g_source_remove(t
->search_id
);
5638 /* reestablish a new timer if the user types fast */
5640 g_source_remove(t
->search_id
);
5641 t
->search_id
= g_timeout_add(250, (GSourceFunc
)search_cb
, (gpointer
)t
);
5644 return (XT_CB_PASSTHROUGH
);
5648 match_uri(const gchar
*uri
, const gchar
*key
) {
5651 gboolean match
= FALSE
;
5655 if (!strncmp(key
, uri
, len
))
5658 voffset
= strstr(uri
, "/") + 2;
5659 if (!strncmp(key
, voffset
, len
))
5661 else if (g_str_has_prefix(voffset
, "www.")) {
5662 voffset
= voffset
+ strlen("www.");
5663 if (!strncmp(key
, voffset
, len
))
5672 match_session(const gchar
*name
, const gchar
*key
) {
5675 sub
= strcasestr(name
, key
);
5681 cmd_getlist(int id
, char *key
)
5688 if (cmds
[id
].type
& XT_URLARG
) {
5689 RB_FOREACH_REVERSE(h
, history_list
, &hl
)
5690 if (match_uri(h
->uri
, key
)) {
5691 cmd_status
.list
[c
] = (char *)h
->uri
;
5697 } else if (cmds
[id
].type
& XT_SESSARG
) {
5698 TAILQ_FOREACH(s
, &sessions
, entry
)
5699 if (match_session(s
->name
, key
)) {
5700 cmd_status
.list
[c
] = (char *)s
->name
;
5706 } else if (cmds
[id
].type
& XT_SETARG
) {
5707 for (i
= 0; i
< get_settings_size(); i
++)
5708 if (!strncmp(key
, get_setting_name(i
),
5710 cmd_status
.list
[c
++] =
5711 get_setting_name(i
);
5717 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
5719 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
5720 if (cmds
[i
].level
< dep
)
5722 if (cmds
[i
].level
== dep
&& !strncmp(key
, cmds
[i
].cmd
,
5723 strlen(key
)) && !isdigit(cmds
[i
].cmd
[0]))
5724 cmd_status
.list
[c
++] = cmds
[i
].cmd
;
5732 cmd_getnext(int dir
)
5734 cmd_status
.index
+= dir
;
5736 if (cmd_status
.index
< 0)
5737 cmd_status
.index
= cmd_status
.len
- 1;
5738 else if (cmd_status
.index
>= cmd_status
.len
)
5739 cmd_status
.index
= 0;
5741 return cmd_status
.list
[cmd_status
.index
];
5745 cmd_tokenize(char *s
, char *tokens
[])
5748 char *tok
, *last
= NULL
;
5749 size_t len
= strlen(s
);
5752 blank
= len
== 0 || (len
> 0 && s
[len
- 1] == ' ');
5753 for (tok
= strtok_r(s
, " ", &last
); tok
&& i
< 3;
5754 tok
= strtok_r(NULL
, " ", &last
), i
++)
5764 cmd_complete(struct tab
*t
, char *str
, int dir
)
5766 GtkEntry
*w
= GTK_ENTRY(t
->cmd
);
5767 int i
, j
, levels
, c
= 0, dep
= 0, parent
= -1;
5769 char *tok
, *match
, *s
= g_strdup(str
);
5771 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
5774 DNPRINTF(XT_D_CMD
, "%s: complete %s\n", __func__
, str
);
5777 for (i
= 0; isdigit(s
[i
]); i
++)
5780 for (; isspace(s
[i
]); i
++)
5785 levels
= cmd_tokenize(s
, tokens
);
5787 for (i
= 0; i
< levels
- 1; i
++) {
5790 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5791 if (cmds
[j
].level
< dep
)
5793 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
,
5797 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
5804 if (matchcount
== 1) {
5805 strlcat(res
, tok
, sizeof res
);
5806 strlcat(res
, " ", sizeof res
);
5816 if (cmd_status
.index
== -1)
5817 cmd_getlist(parent
, tokens
[i
]);
5819 if (cmd_status
.len
> 0) {
5820 match
= cmd_getnext(dir
);
5821 strlcat(res
, match
, sizeof res
);
5822 gtk_entry_set_text(w
, res
);
5823 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5830 cmd_execute(struct tab
*t
, char *str
)
5832 struct cmd
*cmd
= NULL
;
5833 char *tok
, *last
= NULL
, *s
= g_strdup(str
), *sc
;
5835 int j
, len
, c
= 0, dep
= 0, matchcount
= 0;
5836 int prefix
= -1, rv
= XT_CB_PASSTHROUGH
;
5837 struct karg arg
= {0, NULL
, -1};
5842 for (j
= 0; j
<3 && isdigit(s
[j
]); j
++)
5848 while (isspace(s
[0]))
5851 if (strlen(s
) > 0 && strlen(prefixstr
) > 0)
5852 prefix
= atoi(prefixstr
);
5856 for (tok
= strtok_r(s
, " ", &last
); tok
;
5857 tok
= strtok_r(NULL
, " ", &last
)) {
5859 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5860 if (cmds
[j
].level
< dep
)
5862 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1 :
5864 if (cmds
[j
].level
== dep
&&
5865 !strncmp(tok
, cmds
[j
].cmd
, len
)) {
5869 if (len
== strlen(cmds
[j
].cmd
)) {
5875 if (matchcount
== 1) {
5880 show_oops(t
, "Invalid command: %s", str
);
5886 show_oops(t
, "Empty command");
5892 arg
.precount
= prefix
;
5893 else if (cmd_prefix
> 0)
5894 arg
.precount
= cmd_prefix
;
5896 if (j
> 0 && !(cmd
->type
& XT_PREFIX
) && arg
.precount
> -1) {
5897 show_oops(t
, "No prefix allowed: %s", str
);
5901 arg
.s
= last
? g_strdup(last
) : g_strdup("");
5902 if (cmd
->type
& XT_INTARG
&& last
&& strlen(last
) > 0) {
5903 if (arg
.s
== NULL
) {
5904 show_oops(t
, "Invalid command");
5907 arg
.precount
= atoi(arg
.s
);
5908 if (arg
.precount
<= 0) {
5909 if (arg
.s
[0] == '0')
5910 show_oops(t
, "Zero count");
5912 show_oops(t
, "Trailing characters");
5917 DNPRINTF(XT_D_CMD
, "%s: prefix %d arg %s\n",
5918 __func__
, arg
.precount
, arg
.s
);
5934 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5937 show_oops(NULL
, "entry_key_cb invalid parameters");
5938 return (XT_CB_PASSTHROUGH
);
5941 DNPRINTF(XT_D_CMD
, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
5942 e
->keyval
, e
->state
, t
->tab_id
);
5946 if (e
->keyval
== GDK_Escape
) {
5947 /* don't use focus_webview(t) because we want to type :cmds */
5948 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5951 return (handle_keypress(t
, e
, 1));
5954 struct command_entry
*
5955 history_prev(struct command_list
*l
, struct command_entry
*at
)
5958 at
= TAILQ_LAST(l
, command_list
);
5960 at
= TAILQ_PREV(at
, command_list
, entry
);
5962 at
= TAILQ_LAST(l
, command_list
);
5968 struct command_entry
*
5969 history_next(struct command_list
*l
, struct command_entry
*at
)
5972 at
= TAILQ_FIRST(l
);
5974 at
= TAILQ_NEXT(at
, entry
);
5976 at
= TAILQ_FIRST(l
);
5983 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5985 int rv
= XT_CB_HANDLED
;
5986 const gchar
*c
= gtk_entry_get_text(w
);
5990 show_oops(NULL
, "cmd_keypress_cb parameters");
5991 return (XT_CB_PASSTHROUGH
);
5994 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
5995 e
->keyval
, e
->state
, t
->tab_id
);
5999 e
->keyval
= GDK_Escape
;
6000 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
6001 c
[0] == '.' || c
[0] == ','))
6002 e
->keyval
= GDK_Escape
;
6004 if (e
->keyval
!= GDK_Tab
&& e
->keyval
!= GDK_Shift_L
&&
6005 e
->keyval
!= GDK_ISO_Left_Tab
)
6006 cmd_status
.index
= -1;
6008 switch (e
->keyval
) {
6011 cmd_complete(t
, (char *)&c
[1], 1);
6012 else if (c
[0] == '.' || c
[0] == ',')
6013 run_script(t
, "hints.focusNextHint();");
6015 case GDK_ISO_Left_Tab
:
6017 cmd_complete(t
, (char *)&c
[1], -1);
6018 else if (c
[0] == '.' || c
[0] == ',')
6019 run_script(t
, "hints.focusPreviousHint();");
6023 if ((search_at
= history_next(&shl
, search_at
))) {
6024 search_at
->line
[0] = c
[0];
6025 gtk_entry_set_text(w
, search_at
->line
);
6026 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6028 } else if (c
[0] == '/') {
6029 if ((search_at
= history_prev(&shl
, search_at
))) {
6030 search_at
->line
[0] = c
[0];
6031 gtk_entry_set_text(w
, search_at
->line
);
6032 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6034 } if (c
[0] == ':') {
6035 if ((history_at
= history_prev(&chl
, history_at
))) {
6036 history_at
->line
[0] = c
[0];
6037 gtk_entry_set_text(w
, history_at
->line
);
6038 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6044 if ((search_at
= history_next(&shl
, search_at
))) {
6045 search_at
->line
[0] = c
[0];
6046 gtk_entry_set_text(w
, search_at
->line
);
6047 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6049 } else if (c
[0] == '?') {
6050 if ((search_at
= history_prev(&shl
, search_at
))) {
6051 search_at
->line
[0] = c
[0];
6052 gtk_entry_set_text(w
, search_at
->line
);
6053 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6055 } if (c
[0] == ':') {
6056 if ((history_at
= history_next(&chl
, history_at
))) {
6057 history_at
->line
[0] = c
[0];
6058 gtk_entry_set_text(w
, history_at
->line
);
6059 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6064 if (!(!strcmp(c
, ":") || !strcmp(c
, "/") || !strcmp(c
, "?") ||
6065 !strcmp(c
, ".") || !strcmp(c
, ","))) {
6066 /* see if we are doing hinting and reset it */
6067 if (c
[0] == '.' || c
[0] == ',') {
6068 /* recreate hints */
6069 s
= g_strdup_printf("hints.createHints('', "
6070 "'%c');", c
[0] == '.' ? 'f' : 'F');
6083 if (c
!= NULL
&& (c
[0] == '/' || c
[0] == '?'))
6084 webkit_web_view_unmark_text_matches(t
->wv
);
6086 /* no need to cancel hints */
6090 rv
= XT_CB_PASSTHROUGH
;
6096 wv_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
6098 DNPRINTF(XT_D_CMD
, "wv_popup_cb: tab %d\n", t
->tab_id
);
6102 cmd_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
6104 /* popup menu enabled */
6109 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
6112 show_oops(NULL
, "cmd_focusout_cb invalid parameters");
6113 return (XT_CB_PASSTHROUGH
);
6116 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d popup %d\n",
6117 t
->tab_id
, t
->popup
);
6119 /* if popup is enabled don't lose focus */
6122 return (XT_CB_PASSTHROUGH
);
6129 if (show_url
== 0 || t
->focus_wv
)
6132 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6134 return (XT_CB_PASSTHROUGH
);
6138 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
6141 const gchar
*c
= gtk_entry_get_text(entry
);
6144 show_oops(NULL
, "cmd_activate_cb invalid parameters");
6148 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
6153 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
6154 c
[0] == '.' || c
[0] == ','))
6160 if (c
[0] == '/' || c
[0] == '?') {
6161 /* see if there is a timer pending */
6163 g_source_remove(t
->search_id
);
6168 if (t
->search_text
) {
6169 g_free(t
->search_text
);
6170 t
->search_text
= NULL
;
6173 t
->search_text
= g_strdup(s
);
6175 g_free(global_search
);
6176 global_search
= g_strdup(s
);
6177 t
->search_forward
= c
[0] == '/';
6179 history_add(&shl
, search_file
, s
, &search_history_count
);
6180 } else if (c
[0] == '.' || c
[0] == ',') {
6181 run_script(t
, "hints.fire();");
6182 /* XXX history for link following? */
6183 } else if (c
[0] == ':') {
6184 history_add(&chl
, command_file
, s
, &cmd_history_count
);
6185 /* can't call hide_cmd after cmd_execute */
6196 backward_cb(GtkWidget
*w
, struct tab
*t
)
6201 show_oops(NULL
, "backward_cb invalid parameters");
6205 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
6212 forward_cb(GtkWidget
*w
, struct tab
*t
)
6217 show_oops(NULL
, "forward_cb invalid parameters");
6221 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
6223 a
.i
= XT_NAV_FORWARD
;
6228 home_cb(GtkWidget
*w
, struct tab
*t
)
6231 show_oops(NULL
, "home_cb invalid parameters");
6235 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
6241 stop_cb(GtkWidget
*w
, struct tab
*t
)
6243 WebKitWebFrame
*frame
;
6246 show_oops(NULL
, "stop_cb invalid parameters");
6250 DNPRINTF(XT_D_NAV
, "stop_cb: tab %d\n", t
->tab_id
);
6252 frame
= webkit_web_view_get_main_frame(t
->wv
);
6253 if (frame
== NULL
) {
6254 show_oops(t
, "stop_cb: no frame");
6258 webkit_web_frame_stop_loading(frame
);
6259 abort_favicon_download(t
);
6263 setup_webkit(struct tab
*t
)
6265 if (is_g_object_setting(G_OBJECT(t
->settings
), "enable-dns-prefetching"))
6266 g_object_set(G_OBJECT(t
->settings
), "enable-dns-prefetching",
6267 FALSE
, (char *)NULL
);
6269 warnx("webkit does not have \"enable-dns-prefetching\" property");
6270 g_object_set(G_OBJECT(t
->settings
),
6271 "user-agent", t
->user_agent
, (char *)NULL
);
6272 g_object_set(G_OBJECT(t
->settings
),
6273 "enable-scripts", enable_scripts
, (char *)NULL
);
6274 g_object_set(G_OBJECT(t
->settings
),
6275 "enable-plugins", enable_plugins
, (char *)NULL
);
6276 g_object_set(G_OBJECT(t
->settings
),
6277 "javascript-can-open-windows-automatically", enable_scripts
,
6279 g_object_set(G_OBJECT(t
->settings
),
6280 "enable-html5-database", FALSE
, (char *)NULL
);
6281 g_object_set(G_OBJECT(t
->settings
),
6282 "enable-html5-local-storage", enable_localstorage
, (char *)NULL
);
6283 g_object_set(G_OBJECT(t
->settings
),
6284 "enable_spell_checking", enable_spell_checking
, (char *)NULL
);
6285 g_object_set(G_OBJECT(t
->settings
),
6286 "spell_checking_languages", spell_check_languages
, (char *)NULL
);
6287 g_object_set(G_OBJECT(t
->settings
),
6288 "enable-developer-extras", TRUE
, (char *)NULL
);
6289 g_object_set(G_OBJECT(t
->wv
),
6290 "full-content-zoom", TRUE
, (char *)NULL
);
6291 g_object_set(G_OBJECT(t
->settings
),
6292 "auto-load-images", auto_load_images
, (char *)NULL
);
6294 webkit_web_view_set_settings(t
->wv
, t
->settings
);
6298 update_statusbar_position(GtkAdjustment
* adjustment
, gpointer data
)
6300 struct tab
*ti
, *t
= NULL
;
6301 gdouble view_size
, value
, max
;
6304 TAILQ_FOREACH(ti
, &tabs
, entry
)
6305 if (ti
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
6313 if (adjustment
== NULL
)
6314 adjustment
= gtk_scrolled_window_get_vadjustment(
6315 GTK_SCROLLED_WINDOW(t
->browser_win
));
6317 view_size
= gtk_adjustment_get_page_size(adjustment
);
6318 value
= gtk_adjustment_get_value(adjustment
);
6319 max
= gtk_adjustment_get_upper(adjustment
) - view_size
;
6322 position
= g_strdup("All");
6323 else if (value
== max
)
6324 position
= g_strdup("Bot");
6325 else if (value
== 0)
6326 position
= g_strdup("Top");
6328 position
= g_strdup_printf("%d%%", (int) ((value
/ max
) * 100));
6330 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.position
), position
);
6337 create_window(const gchar
*name
)
6341 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
6342 if (window_maximize
)
6343 gtk_window_maximize(GTK_WINDOW(w
));
6345 gtk_window_set_default_size(GTK_WINDOW(w
), window_width
, window_height
);
6346 gtk_widget_set_name(w
, name
);
6347 gtk_window_set_wmclass(GTK_WINDOW(w
), name
, "Xombrero");
6353 create_browser(struct tab
*t
)
6357 GtkAdjustment
*adjustment
;
6360 show_oops(NULL
, "create_browser invalid parameters");
6364 t
->sb_h
= GTK_SCROLLBAR(gtk_hscrollbar_new(NULL
));
6365 t
->sb_v
= GTK_SCROLLBAR(gtk_vscrollbar_new(NULL
));
6366 t
->adjust_h
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_h
));
6367 t
->adjust_v
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_v
));
6369 w
= gtk_scrolled_window_new(t
->adjust_h
, t
->adjust_v
);
6370 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
6371 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
6373 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
6374 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
6377 t
->settings
= webkit_web_settings_new();
6379 g_object_set(t
->settings
, "default-encoding", encoding
, (char *)NULL
);
6381 if (user_agent
== NULL
) {
6382 g_object_get(G_OBJECT(t
->settings
), "user-agent", &strval
,
6384 t
->user_agent
= g_strdup_printf("%s %s+", strval
, version
);
6387 t
->user_agent
= g_strdup(user_agent
->value
);
6389 t
->stylesheet
= g_strdup_printf("file://%s/style.css", resource_dir
);
6390 t
->load_images
= auto_load_images
;
6393 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w
));
6394 g_signal_connect(G_OBJECT(adjustment
), "value-changed",
6395 G_CALLBACK(update_statusbar_position
), NULL
);
6404 create_kiosk_toolbar(struct tab
*t
)
6406 GtkWidget
*toolbar
= NULL
, *b
;
6408 b
= gtk_hbox_new(FALSE
, 0);
6410 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
6412 /* backward button */
6413 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
6414 gtk_widget_set_sensitive(t
->backward
, FALSE
);
6415 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
6416 G_CALLBACK(backward_cb
), t
);
6417 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, TRUE
, TRUE
, 0);
6419 /* forward button */
6420 t
->forward
= create_button("Forward", GTK_STOCK_GO_FORWARD
, 0);
6421 gtk_widget_set_sensitive(t
->forward
, FALSE
);
6422 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
6423 G_CALLBACK(forward_cb
), t
);
6424 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, TRUE
, TRUE
, 0);
6427 t
->gohome
= create_button("Home", GTK_STOCK_HOME
, 0);
6428 gtk_widget_set_sensitive(t
->gohome
, true);
6429 g_signal_connect(G_OBJECT(t
->gohome
), "clicked",
6430 G_CALLBACK(home_cb
), t
);
6431 gtk_box_pack_start(GTK_BOX(b
), t
->gohome
, TRUE
, TRUE
, 0);
6433 /* create widgets but don't use them */
6434 t
->uri_entry
= gtk_entry_new();
6435 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
6436 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
6437 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
6438 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
6444 create_toolbar(struct tab
*t
)
6446 GtkWidget
*toolbar
= NULL
, *b
, *eb1
;
6448 b
= gtk_hbox_new(FALSE
, 0);
6450 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
6452 /* backward button */
6453 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
6454 gtk_widget_set_sensitive(t
->backward
, FALSE
);
6455 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
6456 G_CALLBACK(backward_cb
), t
);
6457 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, FALSE
, FALSE
, 0);
6459 /* forward button */
6460 t
->forward
= create_button("Forward",GTK_STOCK_GO_FORWARD
, 0);
6461 gtk_widget_set_sensitive(t
->forward
, FALSE
);
6462 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
6463 G_CALLBACK(forward_cb
), t
);
6464 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, FALSE
,
6468 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
6469 gtk_widget_set_sensitive(t
->stop
, FALSE
);
6470 g_signal_connect(G_OBJECT(t
->stop
), "clicked",
6471 G_CALLBACK(stop_cb
), t
);
6472 gtk_box_pack_start(GTK_BOX(b
), t
->stop
, FALSE
,
6476 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
6477 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
6478 gtk_widget_set_sensitive(t
->js_toggle
, TRUE
);
6479 g_signal_connect(G_OBJECT(t
->js_toggle
), "clicked",
6480 G_CALLBACK(js_toggle_cb
), t
);
6481 gtk_box_pack_start(GTK_BOX(b
), t
->js_toggle
, FALSE
, FALSE
, 0);
6483 t
->uri_entry
= gtk_entry_new();
6484 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
6485 G_CALLBACK(activate_uri_entry_cb
), t
);
6486 g_signal_connect(G_OBJECT(t
->uri_entry
), "key-press-event",
6487 G_CALLBACK(entry_key_cb
), t
);
6489 eb1
= gtk_hbox_new(FALSE
, 0);
6490 gtk_container_set_border_width(GTK_CONTAINER(eb1
), 1);
6491 gtk_box_pack_start(GTK_BOX(eb1
), t
->uri_entry
, TRUE
, TRUE
, 0);
6492 gtk_box_pack_start(GTK_BOX(b
), eb1
, TRUE
, TRUE
, 0);
6495 if (search_string
!= NULL
&& strlen(search_string
) != 0) {
6497 t
->search_entry
= gtk_entry_new();
6498 gtk_entry_set_width_chars(GTK_ENTRY(t
->search_entry
), 30);
6499 g_signal_connect(G_OBJECT(t
->search_entry
), "activate",
6500 G_CALLBACK(activate_search_entry_cb
), t
);
6501 g_signal_connect(G_OBJECT(t
->search_entry
), "key-press-event",
6502 G_CALLBACK(entry_key_cb
), t
);
6503 gtk_widget_set_size_request(t
->search_entry
, -1, -1);
6504 eb2
= gtk_hbox_new(FALSE
, 0);
6505 gtk_container_set_border_width(GTK_CONTAINER(eb2
), 1);
6506 gtk_box_pack_start(GTK_BOX(eb2
), t
->search_entry
, TRUE
, TRUE
,
6508 gtk_box_pack_start(GTK_BOX(b
), eb2
, FALSE
, FALSE
, 0);
6510 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
6516 create_buffers(struct tab
*t
)
6518 GtkCellRenderer
*renderer
;
6521 view
= gtk_tree_view_new();
6523 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view
), FALSE
);
6525 renderer
= gtk_cell_renderer_text_new();
6526 gtk_tree_view_insert_column_with_attributes
6527 (GTK_TREE_VIEW(view
), -1, "Id", renderer
, "text", COL_ID
, (char *)NULL
);
6529 renderer
= gtk_cell_renderer_pixbuf_new();
6530 gtk_tree_view_insert_column_with_attributes
6531 (GTK_TREE_VIEW(view
), -1, "Favicon", renderer
, "pixbuf", COL_FAVICON
,
6534 renderer
= gtk_cell_renderer_text_new();
6535 gtk_tree_view_insert_column_with_attributes
6536 (GTK_TREE_VIEW(view
), -1, "Title", renderer
, "text", COL_TITLE
,
6539 gtk_tree_view_set_model
6540 (GTK_TREE_VIEW(view
), GTK_TREE_MODEL(buffers_store
));
6546 row_activated_cb(GtkTreeView
*view
, GtkTreePath
*path
,
6547 GtkTreeViewColumn
*col
, struct tab
*t
)
6552 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6554 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
,
6557 (GTK_TREE_MODEL(buffers_store
), &iter
, COL_ID
, &id
, -1);
6558 set_current_tab(id
- 1);
6564 /* after tab reordering/creation/removal */
6571 TAILQ_FOREACH(t
, &tabs
, entry
) {
6572 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
6573 if (t
->tab_id
> maxid
)
6576 gtk_widget_show(t
->tab_elems
.sep
);
6579 TAILQ_FOREACH(t
, &tabs
, entry
) {
6580 if (t
->tab_id
== maxid
) {
6581 gtk_widget_hide(t
->tab_elems
.sep
);
6587 /* after active tab change */
6589 recolor_compact_tabs(void)
6595 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6596 TAILQ_FOREACH(t
, &tabs
, entry
)
6597 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
,
6600 curid
= gtk_notebook_get_current_page(notebook
);
6601 TAILQ_FOREACH(t
, &tabs
, entry
)
6602 if (t
->tab_id
== curid
) {
6603 gdk_color_parse(XT_COLOR_CT_ACTIVE
, &color
);
6604 gtk_widget_modify_fg(t
->tab_elems
.label
,
6605 GTK_STATE_NORMAL
, &color
);
6611 set_current_tab(int page_num
)
6613 buffercmd_abort(get_current_tab());
6614 gtk_notebook_set_current_page(notebook
, page_num
);
6615 recolor_compact_tabs();
6619 undo_close_tab_save(struct tab
*t
)
6623 struct undo
*u1
, *u2
;
6625 WebKitWebHistoryItem
*item
;
6627 if ((uri
= get_uri(t
)) == NULL
)
6630 u1
= g_malloc0(sizeof(struct undo
));
6631 u1
->uri
= g_strdup(uri
);
6633 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
6635 m
= webkit_web_back_forward_list_get_forward_length(t
->bfl
);
6636 n
= webkit_web_back_forward_list_get_back_length(t
->bfl
);
6639 /* forward history */
6640 items
= webkit_web_back_forward_list_get_forward_list_with_limit(t
->bfl
, m
);
6644 u1
->history
= g_list_prepend(u1
->history
,
6645 webkit_web_history_item_copy(item
));
6646 items
= g_list_next(items
);
6651 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
6652 u1
->history
= g_list_prepend(u1
->history
,
6653 webkit_web_history_item_copy(item
));
6657 items
= webkit_web_back_forward_list_get_back_list_with_limit(t
->bfl
, n
);
6661 u1
->history
= g_list_prepend(u1
->history
,
6662 webkit_web_history_item_copy(item
));
6663 items
= g_list_next(items
);
6666 TAILQ_INSERT_HEAD(&undos
, u1
, entry
);
6668 if (undo_count
> XT_MAX_UNDO_CLOSE_TAB
) {
6669 u2
= TAILQ_LAST(&undos
, undo_tailq
);
6670 TAILQ_REMOVE(&undos
, u2
, entry
);
6672 g_list_free(u2
->history
);
6681 delete_tab(struct tab
*t
)
6685 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
6691 * no need to join thread here because it won't access t on completion
6694 TAILQ_REMOVE(&tabs
, t
, entry
);
6697 /* Halt all webkit activity. */
6698 abort_favicon_download(t
);
6699 webkit_web_view_stop_loading(t
->wv
);
6701 /* Save the tab, so we can undo the close. */
6702 undo_close_tab_save(t
);
6706 g_source_remove(t
->search_id
);
6709 bzero(&a
, sizeof a
);
6711 inspector_cmd(t
, &a
);
6713 if (browser_mode
== XT_BM_KIOSK
) {
6714 gtk_widget_destroy(t
->uri_entry
);
6715 gtk_widget_destroy(t
->stop
);
6716 gtk_widget_destroy(t
->js_toggle
);
6719 gtk_widget_destroy(t
->tab_elems
.eventbox
);
6720 gtk_widget_destroy(t
->vbox
);
6722 g_free(t
->user_agent
);
6723 g_free(t
->stylesheet
);
6728 if (TAILQ_EMPTY(&tabs
)) {
6729 if (browser_mode
== XT_BM_KIOSK
)
6730 create_new_tab(home
, NULL
, 1, -1);
6732 create_new_tab(NULL
, NULL
, 1, -1);
6735 /* recreate session */
6736 if (session_autosave
) {
6737 bzero(&a
, sizeof a
);
6739 save_tabs(NULL
, &a
);
6743 recolor_compact_tabs();
6747 update_statusbar_zoom(struct tab
*t
)
6750 char s
[16] = { '\0' };
6752 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6753 if ((zoom
<= 0.99 || zoom
>= 1.01))
6754 snprintf(s
, sizeof s
, "%d%%", (int)(zoom
* 100));
6755 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.zoom
), s
);
6759 setzoom_webkit(struct tab
*t
, int adjust
)
6761 #define XT_ZOOMPERCENT 0.04
6766 show_oops(NULL
, "setzoom_webkit invalid parameters");
6770 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6771 if (adjust
== XT_ZOOM_IN
)
6772 zoom
+= XT_ZOOMPERCENT
;
6773 else if (adjust
== XT_ZOOM_OUT
)
6774 zoom
-= XT_ZOOMPERCENT
;
6775 else if (adjust
> 0)
6776 zoom
= default_zoom_level
+ adjust
/ 100.0 - 1.0;
6778 show_oops(t
, "setzoom_webkit invalid zoom value");
6782 if (zoom
< XT_ZOOMPERCENT
)
6783 zoom
= XT_ZOOMPERCENT
;
6784 g_object_set(G_OBJECT(t
->wv
), "zoom-level", zoom
, (char *)NULL
);
6785 update_statusbar_zoom(t
);
6789 tab_clicked_cb(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
6791 struct tab
*t
= (struct tab
*) data
;
6793 DNPRINTF(XT_D_TAB
, "tab_clicked_cb: tab: %d\n", t
->tab_id
);
6795 switch (event
->button
) {
6797 set_current_tab(t
->tab_id
);
6808 append_tab(struct tab
*t
)
6813 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
6814 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
6818 create_sbe(int width
)
6822 sbe
= gtk_entry_new();
6823 gtk_entry_set_inner_border(GTK_ENTRY(sbe
), NULL
);
6824 gtk_entry_set_has_frame(GTK_ENTRY(sbe
), FALSE
);
6825 gtk_widget_set_can_focus(GTK_WIDGET(sbe
), FALSE
);
6826 gtk_widget_modify_font(GTK_WIDGET(sbe
), statusbar_font
);
6827 gtk_entry_set_alignment(GTK_ENTRY(sbe
), 1.0);
6828 gtk_widget_set_size_request(sbe
, width
, -1);
6834 create_new_tab(char *title
, struct undo
*u
, int focus
, int position
)
6839 WebKitWebHistoryItem
*item
;
6843 int sbe_p
= 0, sbe_b
= 0,
6846 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
6848 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
6849 DNPRINTF(XT_D_TAB
, "create_new_tab: new tab rejected\n");
6853 t
= g_malloc0(sizeof *t
);
6855 if (title
== NULL
) {
6856 title
= "(untitled)";
6860 t
->vbox
= gtk_vbox_new(FALSE
, 0);
6862 /* label + button for tab */
6863 b
= gtk_hbox_new(FALSE
, 0);
6866 #if GTK_CHECK_VERSION(2, 20, 0)
6867 t
->spinner
= gtk_spinner_new();
6869 t
->label
= gtk_label_new(title
);
6870 bb
= create_button("Close", GTK_STOCK_CLOSE
, 1);
6871 gtk_widget_set_size_request(t
->label
, 100, 0);
6872 gtk_label_set_max_width_chars(GTK_LABEL(t
->label
), 20);
6873 gtk_label_set_ellipsize(GTK_LABEL(t
->label
), PANGO_ELLIPSIZE_END
);
6874 gtk_widget_set_size_request(b
, 130, 0);
6876 gtk_box_pack_start(GTK_BOX(b
), bb
, FALSE
, FALSE
, 0);
6877 gtk_box_pack_start(GTK_BOX(b
), t
->label
, FALSE
, FALSE
, 0);
6878 #if GTK_CHECK_VERSION(2, 20, 0)
6879 gtk_box_pack_start(GTK_BOX(b
), t
->spinner
, FALSE
, FALSE
, 0);
6883 if (browser_mode
== XT_BM_KIOSK
) {
6884 t
->toolbar
= create_kiosk_toolbar(t
);
6885 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
6888 t
->toolbar
= create_toolbar(t
);
6890 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
,
6898 t
->browser_win
= create_browser(t
);
6899 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
6901 /* oops message for user feedback */
6902 t
->oops
= gtk_entry_new();
6903 gtk_entry_set_inner_border(GTK_ENTRY(t
->oops
), NULL
);
6904 gtk_entry_set_has_frame(GTK_ENTRY(t
->oops
), FALSE
);
6905 gtk_widget_set_can_focus(GTK_WIDGET(t
->oops
), FALSE
);
6906 gdk_color_parse(XT_COLOR_RED
, &color
);
6907 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
6908 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
6909 gtk_widget_modify_font(GTK_WIDGET(t
->oops
), oops_font
);
6912 t
->cmd
= gtk_entry_new();
6913 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
6914 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
6915 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
6916 gtk_widget_modify_font(GTK_WIDGET(t
->cmd
), cmd_font
);
6919 t
->statusbar_box
= gtk_hbox_new(FALSE
, 0);
6921 t
->sbe
.statusbar
= gtk_entry_new();
6922 gtk_entry_set_inner_border(GTK_ENTRY(t
->sbe
.statusbar
), NULL
);
6923 gtk_entry_set_has_frame(GTK_ENTRY(t
->sbe
.statusbar
), FALSE
);
6924 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.statusbar
), FALSE
);
6925 gtk_widget_modify_font(GTK_WIDGET(t
->sbe
.statusbar
), statusbar_font
);
6927 /* create these widgets only if specified in statusbar_elems */
6929 t
->sbe
.position
= create_sbe(40);
6930 t
->sbe
.zoom
= create_sbe(40);
6931 t
->sbe
.buffercmd
= create_sbe(60);
6933 statusbar_modify_attr(t
, XT_COLOR_WHITE
, XT_COLOR_BLACK
);
6935 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), t
->sbe
.statusbar
, TRUE
,
6938 /* gtk widgets cannot be added to a box twice. sbe_* variables
6939 make sure of this */
6940 for (p
= statusbar_elems
; *p
!= '\0'; p
++) {
6944 GtkWidget
*sep
= gtk_vseparator_new();
6946 gdk_color_parse(XT_COLOR_SB_SEPARATOR
, &color
);
6947 gtk_widget_modify_bg(sep
, GTK_STATE_NORMAL
, &color
);
6948 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), sep
,
6949 FALSE
, FALSE
, FALSE
);
6954 warnx("flag \"%c\" specified more than "
6955 "once in statusbar_elems\n", *p
);
6959 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6960 t
->sbe
.position
, FALSE
, FALSE
, FALSE
);
6964 warnx("flag \"%c\" specified more than "
6965 "once in statusbar_elems\n", *p
);
6969 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6970 t
->sbe
.buffercmd
, FALSE
, FALSE
, FALSE
);
6974 warnx("flag \"%c\" specified more than "
6975 "once in statusbar_elems\n", *p
);
6979 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6980 t
->sbe
.zoom
, FALSE
, FALSE
, FALSE
);
6983 warnx("illegal flag \"%c\" in statusbar_elems\n", *p
);
6988 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar_box
, FALSE
, FALSE
, 0);
6991 t
->buffers
= create_buffers(t
);
6992 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->buffers
, FALSE
, FALSE
, 0);
6994 /* xtp meaning is normal by default */
6995 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
6997 /* set empty favicon */
6998 xt_icon_from_name(t
, "text-html");
7000 /* and show it all */
7001 gtk_widget_show_all(b
);
7002 gtk_widget_show_all(t
->vbox
);
7004 /* compact tab bar */
7005 t
->tab_elems
.label
= gtk_label_new(title
);
7006 t
->tab_elems
.favicon
= gtk_image_new();
7007 gtk_label_set_width_chars(GTK_LABEL(t
->tab_elems
.label
), 1.0);
7008 gtk_misc_set_alignment(GTK_MISC(t
->tab_elems
.label
), 0.0, 0.0);
7009 gtk_misc_set_padding(GTK_MISC(t
->tab_elems
.label
), 4.0, 4.0);
7010 gtk_widget_modify_font(GTK_WIDGET(t
->tab_elems
.label
), tabbar_font
);
7012 t
->tab_elems
.eventbox
= gtk_event_box_new();
7013 t
->tab_elems
.box
= gtk_hbox_new(FALSE
, 0);
7014 t
->tab_elems
.sep
= gtk_vseparator_new();
7016 gdk_color_parse(XT_COLOR_CT_BACKGROUND
, &color
);
7017 gtk_widget_modify_bg(t
->tab_elems
.eventbox
, GTK_STATE_NORMAL
, &color
);
7018 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
7019 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
, &color
);
7020 gdk_color_parse(XT_COLOR_CT_SEPARATOR
, &color
);
7021 gtk_widget_modify_bg(t
->tab_elems
.sep
, GTK_STATE_NORMAL
, &color
);
7023 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.favicon
, FALSE
,
7025 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.label
, TRUE
,
7027 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.sep
, FALSE
,
7029 gtk_container_add(GTK_CONTAINER(t
->tab_elems
.eventbox
),
7032 gtk_box_pack_start(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
, TRUE
,
7034 gtk_widget_show_all(t
->tab_elems
.eventbox
);
7036 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
7039 id
= position
>= 0 ? position
:
7040 gtk_notebook_get_current_page(notebook
) + 1;
7041 if (id
> gtk_notebook_get_n_pages(notebook
))
7044 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
7045 gtk_notebook_insert_page(notebook
, t
->vbox
, b
, id
);
7046 gtk_box_reorder_child(GTK_BOX(tab_bar
),
7047 t
->tab_elems
.eventbox
, id
);
7052 #if GTK_CHECK_VERSION(2, 20, 0)
7053 /* turn spinner off if we are a new tab without uri */
7055 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
7056 gtk_widget_hide(t
->spinner
);
7059 /* make notebook tabs reorderable */
7060 gtk_notebook_set_tab_reorderable(notebook
, t
->vbox
, TRUE
);
7062 /* compact tabs clickable */
7063 g_signal_connect(G_OBJECT(t
->tab_elems
.eventbox
),
7064 "button_press_event", G_CALLBACK(tab_clicked_cb
), t
);
7066 g_object_connect(G_OBJECT(t
->cmd
),
7067 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb
), t
,
7068 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
7069 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb
), t
,
7070 "signal::activate", G_CALLBACK(cmd_activate_cb
), t
,
7071 "signal::populate-popup", G_CALLBACK(cmd_popup_cb
), t
,
7074 /* reuse wv_button_cb to hide oops */
7075 g_object_connect(G_OBJECT(t
->oops
),
7076 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7079 g_signal_connect(t
->buffers
,
7080 "row-activated", G_CALLBACK(row_activated_cb
), t
);
7081 g_object_connect(G_OBJECT(t
->buffers
),
7082 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
, (char *)NULL
);
7084 g_object_connect(G_OBJECT(t
->wv
),
7085 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
,
7086 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb
), t
,
7087 "signal::download-requested", G_CALLBACK(webview_download_cb
), t
,
7088 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), t
,
7089 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7090 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7091 "signal::resource-request-starting", G_CALLBACK(webview_rrs_cb
), t
,
7092 "signal::create-web-view", G_CALLBACK(webview_cwv_cb
), t
,
7093 "signal::close-web-view", G_CALLBACK(webview_closewv_cb
), t
,
7094 "signal::event", G_CALLBACK(webview_event_cb
), t
,
7095 "signal::load-finished", G_CALLBACK(webview_load_finished_cb
), t
,
7096 "signal::load-progress-changed", G_CALLBACK(webview_progress_changed_cb
), t
,
7097 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb
), t
,
7098 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7099 "signal::button_release_event", G_CALLBACK(wv_release_button_cb
), t
,
7100 "signal::populate-popup", G_CALLBACK(wv_popup_cb
), t
,
7102 g_signal_connect(t
->wv
,
7103 "notify::load-status", G_CALLBACK(notify_load_status_cb
), t
);
7104 g_signal_connect(t
->wv
,
7105 "notify::title", G_CALLBACK(notify_title_cb
), t
);
7107 /* hijack the unused keys as if we were the browser */
7108 //g_object_connect(G_OBJECT(t->toolbar),
7109 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
7112 g_signal_connect(G_OBJECT(bb
), "button_press_event",
7113 G_CALLBACK(tab_close_cb
), t
);
7116 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
7117 /* restore the tab's history */
7118 if (u
&& u
->history
) {
7122 webkit_web_back_forward_list_add_item(t
->bfl
, item
);
7123 items
= g_list_next(items
);
7126 item
= g_list_nth_data(u
->history
, u
->back
);
7128 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
7131 g_list_free(u
->history
);
7133 webkit_web_back_forward_list_clear(t
->bfl
);
7139 url_set_visibility();
7140 statusbar_set_visibility();
7143 set_current_tab(t
->tab_id
);
7144 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
7148 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
7152 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
7159 if (userstyle_global
)
7162 recolor_compact_tabs();
7163 setzoom_webkit(t
, XT_ZOOM_NORMAL
);
7168 notebook_switchpage_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7174 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
7176 if (gtk_notebook_get_current_page(notebook
) == -1)
7179 TAILQ_FOREACH(t
, &tabs
, entry
) {
7180 if (t
->tab_id
== pn
) {
7181 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
7184 uri
= get_title(t
, TRUE
);
7185 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
7191 /* can't use focus_webview here */
7192 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
7199 notebook_pagereordered_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7202 struct tab
*t
= NULL
, *tt
;
7206 TAILQ_FOREACH(tt
, &tabs
, entry
)
7207 if (tt
->tab_id
== pn
) {
7213 DNPRINTF(XT_D_TAB
, "page_reordered_cb: tab: %d\n", t
->tab_id
);
7215 gtk_box_reorder_child(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
,
7220 menuitem_response(struct tab
*t
)
7222 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
7226 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
7228 GtkWidget
*menu
, *menu_items
;
7229 GdkEventButton
*bevent
;
7233 if (event
->type
== GDK_BUTTON_PRESS
) {
7234 bevent
= (GdkEventButton
*) event
;
7235 menu
= gtk_menu_new();
7237 TAILQ_FOREACH(ti
, &tabs
, entry
) {
7238 if ((uri
= get_uri(ti
)) == NULL
)
7239 /* XXX make sure there is something to print */
7240 /* XXX add gui pages in here to look purdy */
7242 menu_items
= gtk_menu_item_new_with_label(uri
);
7243 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), menu_items
);
7244 gtk_widget_show(menu_items
);
7246 g_signal_connect_swapped((menu_items
),
7247 "activate", G_CALLBACK(menuitem_response
),
7251 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
,
7252 bevent
->button
, bevent
->time
);
7254 /* unref object so it'll free itself when popped down */
7255 #if !GTK_CHECK_VERSION(3, 0, 0)
7256 /* XXX does not need unref with gtk+3? */
7257 g_object_ref_sink(menu
);
7258 g_object_unref(menu
);
7261 return (TRUE
/* eat event */);
7264 return (FALSE
/* propagate */);
7268 icon_size_map(int iconsz
)
7270 if (iconsz
<= GTK_ICON_SIZE_INVALID
||
7271 iconsz
> GTK_ICON_SIZE_DIALOG
)
7272 return (GTK_ICON_SIZE_SMALL_TOOLBAR
);
7278 create_button(char *name
, char *stockid
, int size
)
7280 GtkWidget
*button
, *image
;
7284 rcstring
= g_strdup_printf(
7285 "style \"%s-style\"\n"
7287 " GtkWidget::focus-padding = 0\n"
7288 " GtkWidget::focus-line-width = 0\n"
7292 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
7293 gtk_rc_parse_string(rcstring
);
7295 button
= gtk_button_new();
7296 gtk_button_set_focus_on_click(GTK_BUTTON(button
), FALSE
);
7297 gtk_icon_size
= icon_size_map(size
? size
: icon_size
);
7299 image
= gtk_image_new_from_stock(stockid
, gtk_icon_size
);
7300 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
7301 gtk_container_set_border_width(GTK_CONTAINER(button
), 1);
7302 gtk_container_add(GTK_CONTAINER(button
), GTK_WIDGET(image
));
7303 gtk_widget_set_name(button
, name
);
7304 gtk_button_set_relief(GTK_BUTTON(button
), GTK_RELIEF_NONE
);
7310 button_set_stockid(GtkWidget
*button
, char *stockid
)
7314 image
= gtk_image_new_from_stock(stockid
, icon_size_map(icon_size
));
7315 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
7316 gtk_button_set_image(GTK_BUTTON(button
), image
);
7320 clipb_primary_cb(GtkClipboard
*primary
, GdkEvent
*event
, gpointer notused
)
7323 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
7326 if (xterm_workaround
== 0)
7330 * xterm doesn't play nice with clipboards because it clears the
7331 * primary when clicked. We rely on primary being set to properly
7332 * handle middle mouse button clicks (paste). So when someone clears
7333 * primary copy whatever is in CUT_BUFFER0 into primary to simualte
7334 * other application behavior (as in DON'T clear primary).
7337 p
= gtk_clipboard_wait_for_text(primary
);
7339 if (gdk_property_get(gdk_get_default_root_window(),
7341 gdk_atom_intern("STRING", FALSE
),
7343 1024 * 1024 /* picked out of my butt */,
7351 /* yes sir, we need to NUL the string */
7353 gtk_clipboard_set_text(primary
, p
, -1);
7367 char file
[PATH_MAX
];
7370 vbox
= gtk_vbox_new(FALSE
, 0);
7371 gtk_box_set_spacing(GTK_BOX(vbox
), 0);
7372 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
7373 #if !GTK_CHECK_VERSION(3, 0, 0)
7374 /* XXX seems to be needed with gtk+2 */
7375 gtk_notebook_set_tab_hborder(notebook
, 0);
7376 gtk_notebook_set_tab_vborder(notebook
, 0);
7378 gtk_notebook_set_scrollable(notebook
, TRUE
);
7379 gtk_notebook_set_show_border(notebook
, FALSE
);
7380 gtk_widget_set_can_focus(GTK_WIDGET(notebook
), FALSE
);
7382 abtn
= gtk_button_new();
7383 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
7384 gtk_widget_set_size_request(arrow
, -1, -1);
7385 gtk_container_add(GTK_CONTAINER(abtn
), arrow
);
7386 gtk_widget_set_size_request(abtn
, -1, 20);
7388 #if GTK_CHECK_VERSION(2, 20, 0)
7389 gtk_notebook_set_action_widget(notebook
, abtn
, GTK_PACK_END
);
7391 gtk_widget_set_size_request(GTK_WIDGET(notebook
), -1, -1);
7393 /* compact tab bar */
7394 tab_bar
= gtk_hbox_new(TRUE
, 0);
7396 gtk_box_pack_start(GTK_BOX(vbox
), tab_bar
, FALSE
, FALSE
, 0);
7397 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
7398 gtk_widget_set_size_request(vbox
, -1, -1);
7400 g_object_connect(G_OBJECT(notebook
),
7401 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb
), NULL
,
7403 g_object_connect(G_OBJECT(notebook
),
7404 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb
),
7405 NULL
, (char *)NULL
);
7406 g_signal_connect(G_OBJECT(abtn
), "button_press_event",
7407 G_CALLBACK(arrow_cb
), NULL
);
7409 main_window
= create_window("xombrero");
7410 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
7411 g_signal_connect(G_OBJECT(main_window
), "delete_event",
7412 G_CALLBACK(gtk_main_quit
), NULL
);
7415 for (i
= 0; i
< LENGTH(icons
); i
++) {
7416 snprintf(file
, sizeof file
, "%s" PS
"%s", resource_dir
, icons
[i
]);
7417 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
7418 l
= g_list_append(l
, pb
);
7420 gtk_window_set_default_icon_list(l
);
7422 /* clipboard work around */
7423 if (xterm_workaround
)
7425 G_OBJECT(gtk_clipboard_get(GDK_SELECTION_PRIMARY
)),
7426 "owner-change", G_CALLBACK(clipb_primary_cb
), NULL
);
7428 gtk_widget_show_all(abtn
);
7429 gtk_widget_show_all(main_window
);
7430 notebook_tab_set_visibility();
7433 #ifndef XT_SOCKET_DISABLE
7435 send_cmd_to_socket(char *cmd
)
7438 struct sockaddr_un sa
;
7440 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7441 warnx("%s: socket", __func__
);
7445 sa
.sun_family
= AF_UNIX
;
7446 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
7447 work_dir
, XT_SOCKET_FILE
);
7450 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7451 warnx("%s: connect", __func__
);
7455 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
7456 warnx("%s: send", __func__
);
7467 socket_watcher(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
7470 char str
[XT_MAX_URL_LENGTH
];
7471 socklen_t t
= sizeof(struct sockaddr_un
);
7472 struct sockaddr_un sa
;
7477 gint fd
= g_io_channel_unix_get_fd(source
);
7479 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
7484 if (getpeereid(s
, &uid
, &gid
) == -1) {
7488 if (uid
!= getuid() || gid
!= getgid()) {
7489 warnx("unauthorized user");
7495 warnx("not a valid user");
7499 n
= recv(s
, str
, sizeof(str
), 0);
7503 tt
= TAILQ_LAST(&tabs
, tab_list
);
7504 cmd_execute(tt
, str
);
7512 struct sockaddr_un sa
;
7514 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7515 warn("is_running: socket");
7519 sa
.sun_family
= AF_UNIX
;
7520 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
7521 work_dir
, XT_SOCKET_FILE
);
7524 /* connect to see if there is a listener */
7525 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
7526 rv
= 0; /* not running */
7528 rv
= 1; /* already running */
7539 struct sockaddr_un sa
;
7541 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7542 warn("build_socket: socket");
7546 sa
.sun_family
= AF_UNIX
;
7547 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
7548 work_dir
, XT_SOCKET_FILE
);
7551 /* connect to see if there is a listener */
7552 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7553 /* no listener so we will */
7554 unlink(sa
.sun_path
);
7556 if (bind(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7557 warn("build_socket: bind");
7561 if (listen(s
, 1) == -1) {
7562 warn("build_socket: listen");
7580 if (stat(dir
, &sb
)) {
7581 #if defined __MINGW32__
7582 printf("making: %s\n", dir
);
7583 if (mkdir(dir
) == -1)
7585 if (mkdir(dir
, S_IRWXU
) == -1)
7587 err(1, "mkdir %s", dir
);
7589 err(1, "stat %s", dir
);
7591 if (S_ISDIR(sb
.st_mode
) == 0)
7592 errx(1, "%s not a dir", dir
);
7593 #if !defined __MINGW32__
7594 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
7595 warnx("fixing invalid permissions on %s", dir
);
7596 if (chmod(dir
, S_IRWXU
) == -1)
7597 err(1, "chmod %s", dir
);
7606 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
7610 GStaticRecMutex my_gdk_mtx
= G_STATIC_REC_MUTEX_INIT
;
7611 volatile int mtx_depth
;
7615 * The linux flash plugin violates the gdk locking mechanism.
7616 * Work around the issue by using a recursive mutex with some match applied
7617 * to see if we hit a buggy condition.
7619 * The following code is painful so just don't read it. It really doesn't
7620 * make much sense but seems to work.
7626 g_static_rec_mutex_lock(&my_gdk_mtx
);
7627 if (my_gdk_mtx
.depth
<= 0) {
7629 g_static_rec_mutex_lock(&my_gdk_mtx
);
7631 } else if (my_gdk_mtx
.depth
!= 1) {
7634 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7635 } while (my_gdk_mtx
.depth
> 1);
7641 if (mtx_complain
== 0) {
7642 show_oops(NULL
, "buggy mutex implementation detected(%s), "
7643 "work around implemented", s
);
7653 if (my_gdk_mtx
.depth
<= 0) {
7656 } else if (my_gdk_mtx
.depth
!= 1) {
7658 g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
7661 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7665 if (mtx_complain
== 0) {
7666 show_oops(NULL
, "buggy mutex implementation detected(%s), "
7667 "work around implemented", s
);
7675 startpage_add("<b>Welcome to xombrero %s!</b><p>", version
);
7676 startpage_add("Details at "
7677 "<a href=https://opensource.conformal.com/wiki/xombrero>xombrero "
7678 "wiki page</a><p>");
7682 main(int argc
, char **argv
)
7685 int c
, optn
= 0, opte
= 0, focus
= 1;
7686 char conf
[PATH_MAX
] = { '\0' };
7687 char file
[PATH_MAX
];
7688 char sodversion
[32];
7689 char *env_proxy
= NULL
;
7693 start_argv
= (char * const *)argv
;
7697 #if !defined __MINGW32__
7698 /* http://web.archiveorange.com/archive/v/UsPjxkX5PsaXBIoOjqxf */
7701 /* http://developer.gnome.org/gdk/stable/gdk-Threads.html */
7702 g_thread_init(NULL
);
7703 gdk_threads_set_lock_functions(mtx_lock
, mtx_unlock
);
7705 gdk_threads_enter();
7707 /* http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html */
7708 gcry_control (GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
7710 gtk_init(&argc
, &argv
);
7712 gnutls_global_init();
7714 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
7719 RB_INIT(&downloads
);
7722 TAILQ_INIT(&sessions
);
7725 TAILQ_INIT(&aliases
);
7731 TAILQ_INIT(&ua_list
);
7733 #ifndef XT_RESOURCE_LIMITS_DISABLE
7737 GIOChannel
*channel
;
7739 /* fiddle with ulimits */
7740 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7743 /* just use them all */
7744 rlp
.rlim_cur
= rlp
.rlim_max
;
7745 if (setrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7747 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7749 else if (rlp
.rlim_cur
< 1024)
7750 startpage_add("%s requires at least 1024 "
7751 "(2048 recommended) file " "descriptors, "
7752 "currently it has up to %d available",
7753 __progname
, rlp
.rlim_cur
);
7757 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
7766 #ifdef XOMBRERO_BUILDSTR
7767 errx(0 , "Version: %s Build: %s",
7768 version
, XOMBRERO_BUILDSTR
);
7770 errx(0 , "Version: %s", version
);
7774 strlcpy(conf
, optarg
, sizeof(conf
));
7777 strlcpy(named_session
, optarg
, sizeof(named_session
));
7798 xtp_generate_keys();
7800 /* XXX this hammer is way too big but treat all signaling the same */
7801 #ifndef XT_SIGNALS_DISABLE
7803 struct sigaction sact
;
7805 bzero(&sact
, sizeof(sact
));
7806 sigemptyset(&sact
.sa_mask
);
7807 sact
.sa_handler
= sigchild
;
7808 sact
.sa_flags
= SA_NOCLDSTOP
;
7809 sigaction(SIGCHLD
, &sact
, NULL
);
7812 /* set download dir */
7813 pwd
= getpwuid(getuid());
7815 errx(1, "invalid user %d", getuid());
7816 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
7818 /* compile buffer command regexes */
7821 /* set default string settings */
7822 home
= g_strdup("https://www.cyphertite.com");
7823 search_string
= g_strdup("about:search");
7824 resource_dir
= g_strdup("/usr/local/share/xombrero/");
7825 strlcpy(runtime_settings
, "runtime", sizeof runtime_settings
);
7826 cmd_font_name
= g_strdup("monospace normal 9");
7827 oops_font_name
= g_strdup("monospace normal 9");
7828 statusbar_font_name
= g_strdup("monospace normal 9");
7829 tabbar_font_name
= g_strdup("monospace normal 9");
7830 statusbar_elems
= g_strdup("BP");
7831 spell_check_languages
= g_strdup("en_US");
7832 encoding
= g_strdup("UTF-8");
7834 /* override os specific settings */
7838 /* read config file */
7839 if (strlen(conf
) == 0)
7840 snprintf(conf
, sizeof conf
, "%s" PS
".%s",
7841 pwd
->pw_dir
, XT_CONF_FILE
);
7842 config_parse(conf
, 0);
7845 cmd_font
= pango_font_description_from_string(cmd_font_name
);
7846 oops_font
= pango_font_description_from_string(oops_font_name
);
7847 statusbar_font
= pango_font_description_from_string(statusbar_font_name
);
7848 tabbar_font
= pango_font_description_from_string(tabbar_font_name
);
7850 /* working directory */
7851 if (strlen(work_dir
) == 0)
7852 snprintf(work_dir
, sizeof work_dir
, "%s" PS
"%s",
7853 pwd
->pw_dir
, XT_DIR
);
7856 /* icon cache dir */
7857 snprintf(cache_dir
, sizeof cache_dir
, "%s" PS
"%s", work_dir
, XT_CACHE_DIR
);
7861 snprintf(certs_dir
, sizeof certs_dir
, "%s" PS
"%s", work_dir
, XT_CERT_DIR
);
7865 snprintf(sessions_dir
, sizeof sessions_dir
, "%s" PS
"%s",
7866 work_dir
, XT_SESSIONS_DIR
);
7867 xxx_dir(sessions_dir
);
7870 snprintf(js_dir
, sizeof js_dir
, "%s" PS
"%s", work_dir
, XT_JS_DIR
);
7874 snprintf(temp_dir
, sizeof temp_dir
, "%s" PS
"%s", work_dir
, XT_TEMP_DIR
);
7877 /* runtime settings that can override config file */
7878 if (runtime_settings
[0] != '\0')
7879 config_parse(runtime_settings
, 1);
7882 if (!strcmp(download_dir
, pwd
->pw_dir
))
7883 strlcat(download_dir
, PS
"downloads", sizeof download_dir
);
7884 xxx_dir(download_dir
);
7886 /* first start file */
7887 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_SOD_FILE
);
7888 if (stat(file
, &sb
)) {
7889 warnx("start of day file doesn't exist, creating it");
7890 if ((f
= fopen(file
, "w")) == NULL
)
7891 err(1, "startofday");
7892 if (fputs(version
, f
) == EOF
)
7899 if ((f
= fopen(file
, "r+")) == NULL
)
7900 err(1, "startofday");
7901 if (fgets(sodversion
, sizeof sodversion
, f
) == NULL
)
7903 sodversion
[strcspn(sodversion
, "\n")] = '\0';
7904 if (strcmp(version
, sodversion
)) {
7905 if ((f
= freopen(file
, "w", f
)) == NULL
)
7906 err(1, "startofday");
7907 if (fputs(version
, f
) == EOF
)
7910 /* upgrade, say something smart */
7916 /* favorites file */
7917 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_FAVS_FILE
);
7918 if (stat(file
, &sb
)) {
7919 warnx("favorites file doesn't exist, creating it");
7920 if ((f
= fopen(file
, "w")) == NULL
)
7921 err(1, "favorites");
7925 /* quickmarks file */
7926 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
7927 if (stat(file
, &sb
)) {
7928 warnx("quickmarks file doesn't exist, creating it");
7929 if ((f
= fopen(file
, "w")) == NULL
)
7930 err(1, "quickmarks");
7934 /* search history */
7935 if (history_autosave
) {
7936 snprintf(search_file
, sizeof search_file
, "%s" PS
"%s",
7937 work_dir
, XT_SEARCH_FILE
);
7938 if (stat(search_file
, &sb
)) {
7939 warnx("search history file doesn't exist, creating it");
7940 if ((f
= fopen(search_file
, "w")) == NULL
)
7941 err(1, "search_history");
7944 history_read(&shl
, search_file
, &search_history_count
);
7947 /* command history */
7948 if (history_autosave
) {
7949 snprintf(command_file
, sizeof command_file
, "%s" PS
"%s",
7950 work_dir
, XT_COMMAND_FILE
);
7951 if (stat(command_file
, &sb
)) {
7952 warnx("command history file doesn't exist, creating it");
7953 if ((f
= fopen(command_file
, "w")) == NULL
)
7954 err(1, "command_history");
7957 history_read(&chl
, command_file
, &cmd_history_count
);
7961 session
= webkit_get_default_session();
7965 set_ssl_ca_file(ssl_ca_file
);
7967 /* guess_search regex */
7968 if (url_regex
== NULL
)
7969 url_regex
= g_strdup(XT_URL_REGEX
);
7971 if (regcomp(&url_re
, url_regex
, REG_EXTENDED
| REG_NOSUB
))
7972 startpage_add("invalid url regex %s", url_regex
);
7975 env_proxy
= getenv("http_proxy");
7977 setup_proxy(env_proxy
);
7979 env_proxy
= getenv("HTTP_PROXY");
7981 setup_proxy(env_proxy
);
7983 setup_proxy(http_proxy
);
7985 #ifndef XT_SOCKET_DISABLE
7987 send_cmd_to_socket(argv
[0]);
7991 opte
= opte
; /* shut mingw up */
7993 /* set some connection parameters */
7994 g_object_set(session
, "max-conns", max_connections
, (char *)NULL
);
7995 g_object_set(session
, "max-conns-per-host", max_host_connections
,
7998 g_signal_connect(session
, "request-queued", G_CALLBACK(session_rq_cb
),
8001 #ifndef XT_SOCKET_DISABLE
8002 /* see if there is already a xombrero running */
8003 if (single_instance
&& is_running()) {
8005 warnx("already running");
8010 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
8011 send_cmd_to_socket(cmd
);
8021 optn
= optn
; /* shut mingw up */
8026 if (enable_strict_transport
)
8027 strict_transport_init();
8029 /* uri completion */
8030 completion_model
= gtk_list_store_new(1, G_TYPE_STRING
);
8033 buffers_store
= gtk_list_store_new
8034 (NUM_COLS
, G_TYPE_UINT
, GDK_TYPE_PIXBUF
, G_TYPE_STRING
);
8040 notebook_tab_set_visibility();
8042 if (save_global_history
)
8043 restore_global_history();
8045 /* restore session list */
8046 restore_sessions_list();
8048 if (!strcmp(named_session
, XT_SAVED_TABS_FILE
))
8049 restore_saved_tabs();
8051 a
.s
= named_session
;
8052 a
.i
= XT_SES_DONOTHING
;
8053 open_tabs(NULL
, &a
);
8056 /* see if we have an exception */
8057 if (!TAILQ_EMPTY(&spl
)) {
8058 create_new_tab("about:startpage", NULL
, focus
, -1);
8063 create_new_tab(argv
[0], NULL
, focus
, -1);
8070 if (TAILQ_EMPTY(&tabs
))
8071 create_new_tab(home
, NULL
, 1, -1);
8072 #ifndef XT_SOCKET_DISABLE
8074 if ((s
= build_socket()) != -1) {
8075 channel
= g_io_channel_unix_new(s
);
8076 g_io_add_watch(channel
, G_IO_IN
, socket_watcher
, NULL
);
8084 gdk_threads_leave();
8085 g_static_rec_mutex_unlock_full(&my_gdk_mtx
); /* just in case */
8088 gnutls_global_deinit();