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>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 char *version
= XXXTERM_VERSION
;
28 uint32_t swm_debug
= 0
51 GCRY_THREAD_OPTION_PTHREAD_IMPL
;
63 TAILQ_ENTRY(session
) entry
;
66 TAILQ_HEAD(session_list
, session
);
69 TAILQ_ENTRY(undo
) entry
;
72 int back
; /* Keeps track of how many back
73 * history items there are. */
75 TAILQ_HEAD(undo_tailq
, undo
);
77 struct command_entry
{
79 TAILQ_ENTRY(command_entry
) entry
;
81 TAILQ_HEAD(command_list
, command_entry
);
84 #define XT_DIR (".xxxterm")
85 #define XT_CACHE_DIR ("cache")
86 #define XT_CERT_DIR ("certs")
87 #define XT_JS_DIR ("js")
88 #define XT_SESSIONS_DIR ("sessions")
89 #define XT_TEMP_DIR ("tmp")
90 #define XT_CONF_FILE ("xxxterm.conf")
91 #define XT_QMARKS_FILE ("quickmarks")
92 #define XT_SAVED_TABS_FILE ("main_session")
93 #define XT_RESTART_TABS_FILE ("restart_tabs")
94 #define XT_SOCKET_FILE ("socket")
95 #define XT_SAVE_SESSION_ID ("SESSION_NAME=")
96 #define XT_SEARCH_FILE ("search_history")
97 #define XT_COMMAND_FILE ("command_history")
98 #define XT_DLMAN_REFRESH "10"
99 #define XT_MAX_URL_LENGTH (4096) /* 1 page is atomic, don't make bigger */
100 #define XT_MAX_UNDO_CLOSE_TAB (32)
101 #define XT_RESERVED_CHARS "$&+,/:;=?@ \"<>#%%{}|^~[]`"
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 enable_hints(struct tab
*t
)
1035 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
1038 run_script(t
, "hints.createHints('', 'F');");
1040 run_script(t
, "hints.createHints('', 'f');");
1041 t
->mode
= XT_MODE_HINT
;
1045 disable_hints(struct tab
*t
)
1047 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
1049 run_script(t
, "hints.clearHints();");
1050 t
->mode
= XT_MODE_COMMAND
;
1055 passthrough(struct tab
*t
, struct karg
*args
)
1057 t
->mode
= XT_MODE_PASSTHROUGH
;
1062 modurl(struct tab
*t
, struct karg
*args
)
1064 const gchar
*uri
= NULL
;
1067 /* XXX kind of a bad hack, but oh well */
1068 if (GTK_WIDGET_HAS_FOCUS(t
->uri_entry
)) {
1069 if ((uri
= gtk_entry_get_text(GTK_ENTRY(t
->uri_entry
))) &&
1071 u
= g_strdup_printf("www.%s.com", uri
);
1072 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), u
);
1074 activate_uri_entry_cb(t
->uri_entry
, t
);
1081 hint(struct tab
*t
, struct karg
*args
)
1084 DNPRINTF(XT_D_JS
, "hint: tab %d args %d\n", t
->tab_id
, args
->i
);
1086 if (t
->mode
== XT_MODE_HINT
) {
1087 if (args
->i
== XT_HINT_NEWTAB
)
1097 apply_style(struct tab
*t
)
1100 g_object_set(G_OBJECT(t
->settings
),
1101 "user-stylesheet-uri", t
->stylesheet
, (char *)NULL
);
1105 remove_style(struct tab
*t
)
1108 g_object_set(G_OBJECT(t
->settings
),
1109 "user-stylesheet-uri", NULL
, (char *)NULL
);
1113 userstyle(struct tab
*t
, struct karg
*args
)
1117 DNPRINTF(XT_D_JS
, "userstyle: tab %d\n", t
->tab_id
);
1120 case XT_STYLE_CURRENT_TAB
:
1126 case XT_STYLE_GLOBAL
:
1127 if (userstyle_global
) {
1128 userstyle_global
= 0;
1129 TAILQ_FOREACH(tt
, &tabs
, entry
)
1132 userstyle_global
= 1;
1133 TAILQ_FOREACH(tt
, &tabs
, entry
)
1143 quit(struct tab
*t
, struct karg
*args
)
1145 if (save_global_history
)
1146 save_global_history_to_disk(t
);
1154 restore_sessions_list(void)
1157 struct dirent
*dp
= NULL
;
1161 sdir
= opendir(sessions_dir
);
1163 while ((dp
= readdir(sdir
)) != NULL
) {
1164 #if defined __MINGW32__
1165 reg
= 1; /* windows only has regular files */
1167 reg
= dp
->d_type
== DT_REG
;
1170 s
= g_malloc(sizeof(struct session
));
1171 s
->name
= g_strdup(dp
->d_name
);
1172 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
1180 open_tabs(struct tab
*t
, struct karg
*a
)
1182 char file
[PATH_MAX
];
1186 struct tab
*ti
, *tt
;
1191 ti
= TAILQ_LAST(&tabs
, tab_list
);
1193 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, a
->s
);
1194 if ((f
= fopen(file
, "r")) == NULL
)
1198 if ((uri
= fparseln(f
, NULL
, NULL
, "\0\0\0", 0)) == NULL
)
1199 if (feof(f
) || ferror(f
))
1202 /* retrieve session name */
1203 if (uri
&& g_str_has_prefix(uri
, XT_SAVE_SESSION_ID
)) {
1204 strlcpy(named_session
,
1205 &uri
[strlen(XT_SAVE_SESSION_ID
)],
1206 sizeof named_session
);
1210 if (uri
&& strlen(uri
))
1211 create_new_tab(uri
, NULL
, 1, -1);
1217 /* close open tabs */
1218 if (a
->i
== XT_SES_CLOSETABS
&& ti
!= NULL
) {
1220 tt
= TAILQ_FIRST(&tabs
);
1241 restore_saved_tabs(void)
1243 char file
[PATH_MAX
];
1244 int unlink_file
= 0;
1249 snprintf(file
, sizeof file
, "%s" PS
"%s",
1250 sessions_dir
, XT_RESTART_TABS_FILE
);
1251 if (stat(file
, &sb
) == -1)
1252 a
.s
= XT_SAVED_TABS_FILE
;
1255 a
.s
= XT_RESTART_TABS_FILE
;
1258 a
.i
= XT_SES_DONOTHING
;
1259 rv
= open_tabs(NULL
, &a
);
1268 save_tabs(struct tab
*t
, struct karg
*a
)
1270 char file
[PATH_MAX
];
1272 int num_tabs
= 0, i
;
1273 struct tab
**stabs
= NULL
;
1275 /* tab may be null here */
1280 snprintf(file
, sizeof file
, "%s" PS
"%s",
1281 sessions_dir
, named_session
);
1283 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, a
->s
);
1285 if ((f
= fopen(file
, "w")) == NULL
) {
1286 show_oops(t
, "Can't open save_tabs file: %s", strerror(errno
));
1290 /* save session name */
1291 fprintf(f
, "%s%s\n", XT_SAVE_SESSION_ID
, named_session
);
1293 /* Save tabs, in the order they are arranged in the notebook. */
1294 num_tabs
= sort_tabs_by_page_num(&stabs
);
1296 for (i
= 0; i
< num_tabs
; i
++)
1298 if (get_uri(stabs
[i
]) != NULL
)
1299 fprintf(f
, "%s\n", get_uri(stabs
[i
]));
1300 else if (gtk_entry_get_text(GTK_ENTRY(
1301 stabs
[i
]->uri_entry
)))
1302 fprintf(f
, "%s\n", gtk_entry_get_text(GTK_ENTRY(
1303 stabs
[i
]->uri_entry
)));
1308 /* try and make sure this gets to disk NOW. XXX Backup first? */
1309 if (fflush(f
) != 0 || fsync(fileno(f
)) != 0) {
1310 show_oops(t
, "May not have managed to save session: %s",
1320 save_tabs_and_quit(struct tab
*t
, struct karg
*args
)
1332 run_page_script(struct tab
*t
, struct karg
*args
)
1335 char *tmp
, script
[PATH_MAX
];
1337 tmp
= args
->s
!= NULL
&& strlen(args
->s
) > 0 ? args
->s
: default_script
;
1338 if (tmp
[0] == '\0') {
1339 show_oops(t
, "no script specified");
1343 if ((uri
= get_uri(t
)) == NULL
) {
1344 show_oops(t
, "tab is empty, not running script");
1349 snprintf(script
, sizeof script
, "%s" PS
"%s",
1350 pwd
->pw_dir
, &tmp
[1]);
1352 strlcpy(script
, tmp
, sizeof script
);
1354 return (fork_exec(t
, script
, uri
, "can't launch external script", 1));
1358 yank_uri(struct tab
*t
, struct karg
*args
)
1361 GtkClipboard
*clipboard
;
1363 if ((uri
= get_uri(t
)) == NULL
)
1366 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1367 gtk_clipboard_set_text(clipboard
, uri
, -1);
1373 paste_uri(struct tab
*t
, struct karg
*args
)
1375 GtkClipboard
*clipboard
;
1376 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
1378 gchar
*p
= NULL
, *uri
;
1380 /* try primary clipboard first */
1381 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1382 p
= gtk_clipboard_wait_for_text(clipboard
);
1384 /* if it failed get whatever text is in cut_buffer0 */
1385 if (p
== NULL
&& xterm_workaround
)
1386 if (gdk_property_get(gdk_get_default_root_window(),
1388 gdk_atom_intern("STRING", FALSE
),
1390 1024 * 1024 /* picked out of my butt */,
1398 /* yes sir, we need to NUL the string */
1404 while (*uri
&& isspace(*uri
))
1406 if (strlen(uri
) == 0) {
1407 show_oops(t
, "empty paste buffer");
1410 if (guess_search
== 0 && valid_url_type(uri
)) {
1411 /* we can be clever and paste this in search box */
1412 show_oops(t
, "not a valid URL");
1416 if (args
->i
== XT_PASTE_CURRENT_TAB
)
1418 else if (args
->i
== XT_PASTE_NEW_TAB
)
1419 create_new_tab(uri
, NULL
, 1, -1);
1430 js_toggle_cb(GtkWidget
*w
, struct tab
*t
)
1435 g_object_get(G_OBJECT(t
->settings
),
1436 "enable-scripts", &es
, (char *)NULL
);
1441 set
= XT_WL_DISABLE
;
1443 a
.i
= set
| XT_WL_TOPLEVEL
;
1446 a
.i
= set
| XT_WL_TOPLEVEL
;
1449 a
.i
= XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
;
1454 toggle_src(struct tab
*t
, struct karg
*args
)
1461 mode
= webkit_web_view_get_view_source_mode(t
->wv
);
1462 webkit_web_view_set_view_source_mode(t
->wv
, !mode
);
1463 webkit_web_view_reload(t
->wv
);
1469 focus_webview(struct tab
*t
)
1474 /* only grab focus if we are visible */
1475 if (gtk_notebook_get_current_page(notebook
) == t
->tab_id
)
1476 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
1480 focus(struct tab
*t
, struct karg
*args
)
1482 if (t
== NULL
|| args
== NULL
)
1488 if (args
->i
== XT_FOCUS_URI
)
1489 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
1490 else if (args
->i
== XT_FOCUS_SEARCH
)
1491 gtk_widget_grab_focus(GTK_WIDGET(t
->search_entry
));
1497 connect_socket_from_uri(const gchar
*uri
, const gchar
**error_str
, char *domain
,
1501 struct addrinfo hints
, *res
= NULL
, *ai
;
1502 int rv
= -1, s
= -1, on
, error
;
1504 static gchar myerror
[256]; /* this is not thread safe */
1507 *error_str
= myerror
;
1508 if (uri
&& !g_str_has_prefix(uri
, "https://")) {
1509 *error_str
= "invalid URI";
1513 su
= soup_uri_new(uri
);
1515 *error_str
= "invalid soup URI";
1518 if (!SOUP_URI_VALID_FOR_HTTP(su
)) {
1519 *error_str
= "invalid HTTPS URI";
1523 snprintf(port
, sizeof port
, "%d", su
->port
);
1524 bzero(&hints
, sizeof(struct addrinfo
));
1525 hints
.ai_flags
= AI_CANONNAME
;
1526 hints
.ai_family
= AF_UNSPEC
;
1527 hints
.ai_socktype
= SOCK_STREAM
;
1529 if ((error
= getaddrinfo(su
->host
, port
, &hints
, &res
))) {
1530 snprintf(myerror
, sizeof myerror
, "getaddrinfo failed: %s",
1531 gai_strerror(errno
));
1535 for (ai
= res
; ai
; ai
= ai
->ai_next
) {
1541 if (ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
)
1543 s
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
1546 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &on
,
1549 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) == 0)
1553 snprintf(myerror
, sizeof myerror
,
1554 "could not obtain certificates from: %s",
1560 strlcpy(domain
, su
->host
, domain_sz
);
1567 if (rv
== -1 && s
!= -1)
1575 custom_gnutls_push(void *s
, const void *buf
, size_t len
)
1577 return send((size_t)s
, buf
, len
, 0);
1581 custom_gnutls_pull(void *s
, void *buf
, size_t len
)
1583 return recv((size_t)s
, buf
, len
, 0);
1588 stop_tls(gnutls_session_t gsession
, gnutls_certificate_credentials_t xcred
)
1591 gnutls_deinit(gsession
);
1593 gnutls_certificate_free_credentials(xcred
);
1599 start_tls(const gchar
**error_str
, int s
, gnutls_session_t
*gs
,
1600 gnutls_certificate_credentials_t
*xc
)
1602 gnutls_certificate_credentials_t xcred
;
1603 gnutls_session_t gsession
;
1605 static gchar myerror
[1024]; /* this is not thread safe */
1607 if (gs
== NULL
|| xc
== NULL
)
1614 gnutls_certificate_allocate_credentials(&xcred
);
1615 gnutls_certificate_set_x509_trust_file(xcred
, ssl_ca_file
,
1616 GNUTLS_X509_FMT_PEM
);
1618 gnutls_init(&gsession
, GNUTLS_CLIENT
);
1619 gnutls_priority_set_direct(gsession
, "PERFORMANCE", NULL
);
1620 gnutls_credentials_set(gsession
, GNUTLS_CRD_CERTIFICATE
, xcred
);
1621 gnutls_transport_set_ptr(gsession
, (gnutls_transport_ptr_t
)(long)s
);
1623 /* sockets on windows don't use file descriptors */
1624 gnutls_transport_set_push_function(gsession
, custom_gnutls_push
);
1625 gnutls_transport_set_pull_function(gsession
, custom_gnutls_pull
);
1627 if ((rv
= gnutls_handshake(gsession
)) < 0) {
1628 snprintf(myerror
, sizeof myerror
,
1629 "gnutls_handshake failed %d fatal %d %s",
1631 gnutls_error_is_fatal(rv
),
1632 #if LIBGNUTLS_VERSION_MAJOR >= 2 && LIBGNUTLS_VERSION_MINOR >= 6
1633 gnutls_strerror_name(rv
));
1635 "GNUTLS version is too old to provide human readable error");
1637 stop_tls(gsession
, xcred
);
1641 gnutls_credentials_type_t cred
;
1642 cred
= gnutls_auth_get_type(gsession
);
1643 if (cred
!= GNUTLS_CRD_CERTIFICATE
) {
1644 snprintf(myerror
, sizeof myerror
,
1645 "gnutls_auth_get_type failed %d",
1647 stop_tls(gsession
, xcred
);
1655 *error_str
= myerror
;
1660 get_connection_certs(gnutls_session_t gsession
, gnutls_x509_crt_t
**certs
,
1664 const gnutls_datum_t
*cl
;
1665 gnutls_x509_crt_t
*all_certs
;
1668 if (certs
== NULL
|| cert_count
== NULL
)
1670 if (gnutls_certificate_type_get(gsession
) != GNUTLS_CRT_X509
)
1672 cl
= gnutls_certificate_get_peers(gsession
, &len
);
1676 all_certs
= g_malloc(sizeof(gnutls_x509_crt_t
) * len
);
1677 for (i
= 0; i
< len
; i
++) {
1678 gnutls_x509_crt_init(&all_certs
[i
]);
1679 if (gnutls_x509_crt_import(all_certs
[i
], &cl
[i
],
1680 GNUTLS_X509_FMT_PEM
< 0)) {
1694 free_connection_certs(gnutls_x509_crt_t
*certs
, size_t cert_count
)
1698 for (i
= 0; i
< cert_count
; i
++)
1699 gnutls_x509_crt_deinit(certs
[i
]);
1704 statusbar_modify_attr(struct tab
*t
, const char *text
, const char *base
)
1706 GdkColor c_text
, c_base
;
1708 gdk_color_parse(text
, &c_text
);
1709 gdk_color_parse(base
, &c_base
);
1711 gtk_widget_modify_text(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_text
);
1712 gtk_widget_modify_text(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_text
);
1713 gtk_widget_modify_text(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_text
);
1714 gtk_widget_modify_text(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_text
);
1716 gtk_widget_modify_base(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_base
);
1717 gtk_widget_modify_base(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_base
);
1718 gtk_widget_modify_base(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_base
);
1719 gtk_widget_modify_base(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_base
);
1723 save_certs(struct tab
*t
, gnutls_x509_crt_t
*certs
,
1724 size_t cert_count
, char *domain
)
1727 char cert_buf
[64 * 1024], file
[PATH_MAX
];
1732 if (t
== NULL
|| certs
== NULL
|| cert_count
<= 0 || domain
== NULL
)
1735 snprintf(file
, sizeof file
, "%s" PS
"%s", certs_dir
, domain
);
1736 if ((f
= fopen(file
, "w")) == NULL
) {
1737 show_oops(t
, "Can't create cert file %s %s",
1738 file
, strerror(errno
));
1742 for (i
= 0; i
< cert_count
; i
++) {
1743 cert_buf_sz
= sizeof cert_buf
;
1744 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1745 cert_buf
, &cert_buf_sz
)) {
1746 show_oops(t
, "gnutls_x509_crt_export failed");
1749 if (fwrite(cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1750 show_oops(t
, "Can't write certs: %s", strerror(errno
));
1755 /* not the best spot but oh well */
1756 gdk_color_parse(XT_COLOR_BLUE
, &color
);
1757 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
1758 statusbar_modify_attr(t
, XT_COLOR_BLACK
, XT_COLOR_BLUE
);
1771 load_compare_cert(const gchar
*uri
, const gchar
**error_str
)
1773 char domain
[8182], file
[PATH_MAX
];
1774 char cert_buf
[64 * 1024], r_cert_buf
[64 * 1024];
1776 unsigned int error
= 0;
1778 size_t cert_buf_sz
, cert_count
;
1779 enum cert_trust rv
= CERT_UNTRUSTED
;
1780 static gchar serr
[80]; /* this isn't thread safe */
1781 gnutls_session_t gsession
;
1782 gnutls_x509_crt_t
*certs
;
1783 gnutls_certificate_credentials_t xcred
;
1785 DNPRINTF(XT_D_URL
, "%s: %s\n", __func__
, uri
);
1789 if ((s
= connect_socket_from_uri(uri
, error_str
, domain
,
1790 sizeof domain
)) == -1)
1793 DNPRINTF(XT_D_URL
, "%s: fd %d\n", __func__
, s
);
1796 if (start_tls(error_str
, s
, &gsession
, &xcred
))
1798 DNPRINTF(XT_D_URL
, "%s: got tls\n", __func__
);
1800 /* verify certs in case cert file doesn't exist */
1801 if (gnutls_certificate_verify_peers2(gsession
, &error
) !=
1803 *error_str
= "Invalid certificates";
1808 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1809 *error_str
= "Can't get connection certificates";
1813 snprintf(file
, sizeof file
, "%s" PS
"%s", certs_dir
, domain
);
1814 if ((f
= fopen(file
, "r")) == NULL
) {
1820 for (i
= 0; i
< cert_count
; i
++) {
1821 cert_buf_sz
= sizeof cert_buf
;
1822 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1823 cert_buf
, &cert_buf_sz
)) {
1826 if (fread(r_cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1827 rv
= CERT_BAD
; /* critical */
1830 if (bcmp(r_cert_buf
, cert_buf
, sizeof cert_buf_sz
)) {
1831 rv
= CERT_BAD
; /* critical */
1840 free_connection_certs(certs
, cert_count
);
1842 /* we close the socket first for speed */
1846 /* only complain if we didn't save it locally */
1847 if (error
&& rv
!= CERT_LOCAL
) {
1848 strlcpy(serr
, "Certificate exception(s): ", sizeof serr
);
1849 if (error
& GNUTLS_CERT_INVALID
)
1850 strlcat(serr
, "invalid, ", sizeof serr
);
1851 if (error
& GNUTLS_CERT_REVOKED
)
1852 strlcat(serr
, "revoked, ", sizeof serr
);
1853 if (error
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
1854 strlcat(serr
, "signer not found, ", sizeof serr
);
1855 if (error
& GNUTLS_CERT_SIGNER_NOT_CA
)
1856 strlcat(serr
, "not signed by CA, ", sizeof serr
);
1857 if (error
& GNUTLS_CERT_INSECURE_ALGORITHM
)
1858 strlcat(serr
, "insecure algorithm, ", sizeof serr
);
1859 #if LIBGNUTLS_VERSION_MAJOR >= 2 && LIBGNUTLS_VERSION_MINOR >= 6
1860 if (error
& GNUTLS_CERT_NOT_ACTIVATED
)
1861 strlcat(serr
, "not activated, ", sizeof serr
);
1862 if (error
& GNUTLS_CERT_EXPIRED
)
1863 strlcat(serr
, "expired, ", sizeof serr
);
1865 for (i
= strlen(serr
) - 1; i
> 0; i
--)
1866 if (serr
[i
] == ',') {
1873 stop_tls(gsession
, xcred
);
1879 cert_cmd(struct tab
*t
, struct karg
*args
)
1881 const gchar
*uri
, *error_str
= NULL
;
1885 gnutls_session_t gsession
;
1886 gnutls_x509_crt_t
*certs
;
1887 gnutls_certificate_credentials_t xcred
;
1892 if (ssl_ca_file
== NULL
) {
1893 show_oops(t
, "Can't open CA file: %s", ssl_ca_file
);
1897 if ((uri
= get_uri(t
)) == NULL
) {
1898 show_oops(t
, "Invalid URI");
1902 if ((s
= connect_socket_from_uri(uri
, &error_str
, domain
,
1903 sizeof domain
)) == -1) {
1904 show_oops(t
, "%s", error_str
);
1909 if (start_tls(&error_str
, s
, &gsession
, &xcred
))
1913 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1914 show_oops(t
, "get_connection_certs failed");
1918 if (args
->i
& XT_SHOW
)
1919 show_certs(t
, certs
, cert_count
, "Certificate Chain");
1920 else if (args
->i
& XT_SAVE
)
1921 save_certs(t
, certs
, cert_count
, domain
);
1923 free_connection_certs(certs
, cert_count
);
1925 /* we close the socket first for speed */
1928 stop_tls(gsession
, xcred
);
1929 if (error_str
&& strlen(error_str
))
1930 show_oops(t
, "%s", error_str
);
1935 remove_cookie(int index
)
1941 DNPRINTF(XT_D_COOKIE
, "remove_cookie: %d\n", index
);
1943 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
1945 for (i
= 1; cf
; cf
= cf
->next
, i
++) {
1949 print_cookie("remove cookie", c
);
1950 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
1955 soup_cookies_free(cf
);
1961 remove_cookie_domain(int domain_id
)
1963 int domain_count
, rv
= 1;
1968 DNPRINTF(XT_D_COOKIE
, "remove_cookie_domain: %d\n", domain_id
);
1971 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
1973 for (domain_count
= 0; cf
; cf
= cf
->next
) {
1976 if (strcmp(last_domain
, c
->domain
) != 0) {
1978 last_domain
= c
->domain
;
1981 if (domain_count
< domain_id
)
1983 else if (domain_count
> domain_id
)
1986 print_cookie("remove cookie", c
);
1987 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
1991 soup_cookies_free(cf
);
2003 DNPRINTF(XT_D_COOKIE
, "remove_cookie_all\n");
2005 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
2007 for (; cf
; cf
= cf
->next
) {
2010 print_cookie("remove cookie", c
);
2011 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2015 soup_cookies_free(cf
);
2021 toplevel_cmd(struct tab
*t
, struct karg
*args
)
2023 js_toggle_cb(t
->js_toggle
, t
);
2029 can_go_back_for_real(struct tab
*t
)
2032 WebKitWebHistoryItem
*item
;
2038 /* rely on webkit to make sure we can go backward when on an about page */
2040 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
2041 return (webkit_web_view_can_go_back(t
->wv
));
2043 /* the back/forwars list is stupid so help determine if we can go back */
2044 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2046 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2047 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
2055 can_go_forward_for_real(struct tab
*t
)
2058 WebKitWebHistoryItem
*item
;
2064 /* rely on webkit to make sure we can go forward when on an about page */
2066 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
2067 return (webkit_web_view_can_go_forward(t
->wv
));
2069 /* the back/forwars list is stupid so help selecting a different item */
2070 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2072 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2073 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
2081 go_back_for_real(struct tab
*t
)
2084 WebKitWebHistoryItem
*item
;
2092 webkit_web_view_go_back(t
->wv
);
2095 /* the back/forwars list is stupid so help selecting a different item */
2096 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2098 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2099 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2100 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2107 go_forward_for_real(struct tab
*t
)
2110 WebKitWebHistoryItem
*item
;
2118 webkit_web_view_go_forward(t
->wv
);
2121 /* the back/forwars list is stupid so help selecting a different item */
2122 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2124 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2125 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2126 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2133 navaction(struct tab
*t
, struct karg
*args
)
2135 WebKitWebHistoryItem
*item
;
2136 WebKitWebFrame
*frame
;
2138 DNPRINTF(XT_D_NAV
, "navaction: tab %d opcode %d\n",
2139 t
->tab_id
, args
->i
);
2141 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
2143 if (args
->i
== XT_NAV_BACK
)
2144 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2146 item
= webkit_web_back_forward_list_get_forward_item(t
->bfl
);
2148 return (XT_CB_PASSTHROUGH
);
2149 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2151 return (XT_CB_PASSTHROUGH
);
2157 go_back_for_real(t
);
2159 case XT_NAV_FORWARD
:
2161 go_forward_for_real(t
);
2164 frame
= webkit_web_view_get_main_frame(t
->wv
);
2165 webkit_web_frame_reload(frame
);
2168 frame
= webkit_web_view_get_main_frame(t
->wv
);
2169 webkit_web_frame_stop_loading(frame
);
2172 return (XT_CB_PASSTHROUGH
);
2176 move(struct tab
*t
, struct karg
*args
)
2178 GtkAdjustment
*adjust
;
2179 double pi
, si
, pos
, ps
, upper
, lower
, max
;
2185 case XT_MOVE_BOTTOM
:
2187 case XT_MOVE_PAGEDOWN
:
2188 case XT_MOVE_PAGEUP
:
2189 case XT_MOVE_HALFDOWN
:
2190 case XT_MOVE_HALFUP
:
2191 case XT_MOVE_PERCENT
:
2192 case XT_MOVE_CENTER
:
2193 adjust
= t
->adjust_v
;
2196 adjust
= t
->adjust_h
;
2200 pos
= gtk_adjustment_get_value(adjust
);
2201 ps
= gtk_adjustment_get_page_size(adjust
);
2202 upper
= gtk_adjustment_get_upper(adjust
);
2203 lower
= gtk_adjustment_get_lower(adjust
);
2204 si
= gtk_adjustment_get_step_increment(adjust
);
2205 pi
= gtk_adjustment_get_page_increment(adjust
);
2208 DNPRINTF(XT_D_MOVE
, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2209 "max %f si %f pi %f\n",
2210 args
->i
, adjust
== t
->adjust_h
? "horizontal" : "vertical",
2211 pos
, ps
, upper
, lower
, max
, si
, pi
);
2217 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2222 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2224 case XT_MOVE_BOTTOM
:
2225 case XT_MOVE_FARRIGHT
:
2226 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2227 gtk_adjustment_set_value(adjust
, max
);
2230 case XT_MOVE_FARLEFT
:
2231 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2232 gtk_adjustment_set_value(adjust
, lower
);
2234 case XT_MOVE_PAGEDOWN
:
2236 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2238 case XT_MOVE_PAGEUP
:
2240 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2242 case XT_MOVE_HALFDOWN
:
2244 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2246 case XT_MOVE_HALFUP
:
2248 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2250 case XT_MOVE_CENTER
:
2251 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2252 args
->s
= g_strdup("50.0");
2254 case XT_MOVE_PERCENT
:
2255 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2256 percent
= atoi(args
->s
) / 100.0;
2257 pos
= max
* percent
;
2258 if (pos
< 0.0 || pos
> max
)
2260 gtk_adjustment_set_value(adjust
, pos
);
2263 return (XT_CB_PASSTHROUGH
);
2266 DNPRINTF(XT_D_MOVE
, "move: new pos %f %f\n", pos
, MIN(pos
, max
));
2268 return (XT_CB_HANDLED
);
2272 url_set_visibility(void)
2276 TAILQ_FOREACH(t
, &tabs
, entry
)
2277 if (show_url
== 0) {
2278 gtk_widget_hide(t
->toolbar
);
2281 gtk_widget_show(t
->toolbar
);
2285 notebook_tab_set_visibility(void)
2287 if (show_tabs
== 0) {
2288 gtk_widget_hide(tab_bar
);
2289 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2291 if (tab_style
== XT_TABS_NORMAL
) {
2292 gtk_widget_hide(tab_bar
);
2293 gtk_notebook_set_show_tabs(notebook
, TRUE
);
2294 } else if (tab_style
== XT_TABS_COMPACT
) {
2295 gtk_widget_show(tab_bar
);
2296 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2302 statusbar_set_visibility(void)
2306 TAILQ_FOREACH(t
, &tabs
, entry
){
2307 if (show_statusbar
== 0)
2308 gtk_widget_hide(t
->statusbar_box
);
2310 gtk_widget_show(t
->statusbar_box
);
2317 url_set(struct tab
*t
, int enable_url_entry
)
2322 show_url
= enable_url_entry
;
2324 if (enable_url_entry
) {
2325 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
2326 GTK_ENTRY_ICON_PRIMARY
, NULL
);
2327 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
), 0);
2329 pixbuf
= gtk_entry_get_icon_pixbuf(GTK_ENTRY(t
->uri_entry
),
2330 GTK_ENTRY_ICON_PRIMARY
);
2332 gtk_entry_get_progress_fraction(GTK_ENTRY(t
->uri_entry
));
2333 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
2334 GTK_ENTRY_ICON_PRIMARY
, pixbuf
);
2335 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
2341 fullscreen(struct tab
*t
, struct karg
*args
)
2343 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2346 return (XT_CB_PASSTHROUGH
);
2348 if (show_url
== 0) {
2356 url_set_visibility();
2357 notebook_tab_set_visibility();
2359 return (XT_CB_HANDLED
);
2363 statustoggle(struct tab
*t
, struct karg
*args
)
2365 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2367 if (show_statusbar
== 1) {
2369 statusbar_set_visibility();
2370 } else if (show_statusbar
== 0) {
2372 statusbar_set_visibility();
2374 return (XT_CB_HANDLED
);
2378 urlaction(struct tab
*t
, struct karg
*args
)
2380 int rv
= XT_CB_HANDLED
;
2382 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2385 return (XT_CB_PASSTHROUGH
);
2389 if (show_url
== 0) {
2391 url_set_visibility();
2395 if (show_url
== 1) {
2397 url_set_visibility();
2405 tabaction(struct tab
*t
, struct karg
*args
)
2407 int rv
= XT_CB_HANDLED
;
2408 char *url
= args
->s
;
2412 DNPRINTF(XT_D_TAB
, "tabaction: %p %d\n", t
, args
->i
);
2415 return (XT_CB_PASSTHROUGH
);
2419 if (strlen(url
) > 0)
2420 create_new_tab(url
, NULL
, 1, args
->precount
);
2422 create_new_tab(NULL
, NULL
, 1, args
->precount
);
2425 if (args
->precount
< 0)
2428 TAILQ_FOREACH(tt
, &tabs
, entry
)
2429 if (tt
->tab_id
== args
->precount
- 1) {
2434 case XT_TAB_DELQUIT
:
2435 if (gtk_notebook_get_n_pages(notebook
) > 1)
2441 if (strlen(url
) > 0)
2444 rv
= XT_CB_PASSTHROUGH
;
2450 if (show_tabs
== 0) {
2452 notebook_tab_set_visibility();
2456 if (show_tabs
== 1) {
2458 notebook_tab_set_visibility();
2461 case XT_TAB_NEXTSTYLE
:
2462 if (tab_style
== XT_TABS_NORMAL
) {
2463 tab_style
= XT_TABS_COMPACT
;
2464 recolor_compact_tabs();
2467 tab_style
= XT_TABS_NORMAL
;
2468 notebook_tab_set_visibility();
2470 case XT_TAB_UNDO_CLOSE
:
2471 if (undo_count
== 0) {
2472 DNPRINTF(XT_D_TAB
, "%s: no tabs to undo close",
2477 u
= TAILQ_FIRST(&undos
);
2478 create_new_tab(u
->uri
, u
, 1, -1);
2480 TAILQ_REMOVE(&undos
, u
, entry
);
2482 /* u->history is freed in create_new_tab() */
2486 case XT_TAB_LOAD_IMAGES
:
2488 if (!auto_load_images
) {
2490 /* Enable auto-load images (this will load all
2491 * previously unloaded images). */
2492 g_object_set(G_OBJECT(t
->settings
),
2493 "auto-load-images", TRUE
, (char *)NULL
);
2494 webkit_web_view_set_settings(t
->wv
, t
->settings
);
2496 webkit_web_view_reload(t
->wv
);
2498 /* Webkit triggers an event when we change the setting,
2499 * so we can't disable the auto-loading at once.
2501 * Unfortunately, webkit does not tell us when it's done.
2502 * Instead, we wait until the next request, and then
2503 * disable autoloading again.
2505 t
->load_images
= TRUE
;
2509 rv
= XT_CB_PASSTHROUGH
;
2523 resizetab(struct tab
*t
, struct karg
*args
)
2525 if (t
== NULL
|| args
== NULL
) {
2526 show_oops(NULL
, "resizetab invalid parameters");
2527 return (XT_CB_PASSTHROUGH
);
2530 DNPRINTF(XT_D_TAB
, "resizetab: tab %d %d\n",
2531 t
->tab_id
, args
->i
);
2533 setzoom_webkit(t
, args
->i
);
2535 return (XT_CB_HANDLED
);
2539 movetab(struct tab
*t
, struct karg
*args
)
2543 if (t
== NULL
|| args
== NULL
) {
2544 show_oops(NULL
, "movetab invalid parameters");
2545 return (XT_CB_PASSTHROUGH
);
2548 DNPRINTF(XT_D_TAB
, "movetab: tab %d opcode %d\n",
2549 t
->tab_id
, args
->i
);
2551 if (args
->i
>= XT_TAB_INVALID
)
2552 return (XT_CB_PASSTHROUGH
);
2554 if (TAILQ_EMPTY(&tabs
))
2555 return (XT_CB_PASSTHROUGH
);
2557 n
= gtk_notebook_get_n_pages(notebook
);
2558 dest
= gtk_notebook_get_current_page(notebook
);
2562 if (args
->precount
< 0)
2563 dest
= dest
== n
- 1 ? 0 : dest
+ 1;
2565 dest
= args
->precount
- 1;
2569 if (args
->precount
< 0)
2572 dest
-= args
->precount
% n
;
2585 return (XT_CB_PASSTHROUGH
);
2588 if (dest
< 0 || dest
>= n
)
2589 return (XT_CB_PASSTHROUGH
);
2590 if (t
->tab_id
== dest
) {
2591 DNPRINTF(XT_D_TAB
, "movetab: do nothing\n");
2592 return (XT_CB_HANDLED
);
2595 set_current_tab(dest
);
2597 return (XT_CB_HANDLED
);
2604 command(struct tab
*t
, struct karg
*args
)
2606 char *s
= NULL
, *ss
= NULL
;
2611 if (t
== NULL
|| args
== NULL
) {
2612 show_oops(NULL
, "command invalid parameters");
2613 return (XT_CB_PASSTHROUGH
);
2624 if (cmd_prefix
== 0)
2627 ss
= g_strdup_printf(":%d", cmd_prefix
);
2633 t
->mode
= XT_MODE_HINT
;
2634 bzero(&a
, sizeof a
);
2640 t
->mode
= XT_MODE_HINT
;
2641 bzero(&a
, sizeof a
);
2642 a
.i
= XT_HINT_NEWTAB
;
2652 case XT_CMD_OPEN_CURRENT
:
2655 case XT_CMD_TABNEW_CURRENT
:
2656 if (!s
) /* FALL THROUGH? */
2658 if ((uri
= get_uri(t
)) != NULL
) {
2659 ss
= g_strdup_printf("%s%s", s
, uri
);
2664 show_oops(t
, "command: invalid opcode %d", args
->i
);
2665 return (XT_CB_PASSTHROUGH
);
2668 DNPRINTF(XT_D_CMD
, "%s: tab %d type %s\n", __func__
, t
->tab_id
, s
);
2670 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), s
);
2671 text
= gdk_color_to_string(&t
->default_style
->text
[GTK_STATE_NORMAL
]);
2672 base
= gdk_color_to_string(&t
->default_style
->base
[GTK_STATE_NORMAL
]);
2673 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
2674 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
2675 statusbar_modify_attr(t
, text
, base
);
2679 gtk_widget_grab_focus(GTK_WIDGET(t
->cmd
));
2680 gtk_editable_set_position(GTK_EDITABLE(t
->cmd
), -1);
2685 return (XT_CB_HANDLED
);
2689 search(struct tab
*t
, struct karg
*args
)
2693 if (t
== NULL
|| args
== NULL
) {
2694 show_oops(NULL
, "search invalid parameters");
2699 case XT_SEARCH_NEXT
:
2700 d
= t
->search_forward
;
2702 case XT_SEARCH_PREV
:
2703 d
= !t
->search_forward
;
2706 return (XT_CB_PASSTHROUGH
);
2709 if (t
->search_text
== NULL
) {
2710 if (global_search
== NULL
)
2711 return (XT_CB_PASSTHROUGH
);
2713 d
= t
->search_forward
= TRUE
;
2714 t
->search_text
= g_strdup(global_search
);
2715 webkit_web_view_mark_text_matches(t
->wv
, global_search
, FALSE
, 0);
2716 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
2720 DNPRINTF(XT_D_CMD
, "search: tab %d opc %d forw %d text %s\n",
2721 t
->tab_id
, args
->i
, t
->search_forward
, t
->search_text
);
2723 webkit_web_view_search_text(t
->wv
, t
->search_text
, FALSE
, d
, TRUE
);
2725 return (XT_CB_HANDLED
);
2729 session_save(struct tab
*t
, char *filename
)
2735 if (strlen(filename
) == 0)
2738 if (filename
[0] == '.' || filename
[0] == '/')
2742 if (save_tabs(t
, &a
))
2744 strlcpy(named_session
, filename
, sizeof named_session
);
2746 /* add the new session to the list of sessions */
2747 s
= g_malloc(sizeof(struct session
));
2748 s
->name
= g_strdup(filename
);
2749 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
2757 session_open(struct tab
*t
, char *filename
)
2762 if (strlen(filename
) == 0)
2765 if (filename
[0] == '.' || filename
[0] == '/')
2769 a
.i
= XT_SES_CLOSETABS
;
2770 if (open_tabs(t
, &a
))
2773 strlcpy(named_session
, filename
, sizeof named_session
);
2781 session_delete(struct tab
*t
, char *filename
)
2783 char file
[PATH_MAX
];
2787 if (strlen(filename
) == 0)
2790 if (filename
[0] == '.' || filename
[0] == '/')
2793 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, filename
);
2797 if (!strcmp(filename
, named_session
))
2798 strlcpy(named_session
, XT_SAVED_TABS_FILE
,
2799 sizeof named_session
);
2801 /* remove session from sessions list */
2802 TAILQ_FOREACH(s
, &sessions
, entry
) {
2803 if (!strcmp(s
->name
, filename
))
2808 TAILQ_REMOVE(&sessions
, s
, entry
);
2809 g_free((gpointer
) s
->name
);
2818 session_cmd(struct tab
*t
, struct karg
*args
)
2820 char *filename
= args
->s
;
2825 if (args
->i
& XT_SHOW
)
2826 show_oops(t
, "Current session: %s", named_session
[0] == '\0' ?
2827 XT_SAVED_TABS_FILE
: named_session
);
2828 else if (args
->i
& XT_SAVE
) {
2829 if (session_save(t
, filename
)) {
2830 show_oops(t
, "Can't save session: %s",
2831 filename
? filename
: "INVALID");
2834 } else if (args
->i
& XT_OPEN
) {
2835 if (session_open(t
, filename
)) {
2836 show_oops(t
, "Can't open session: %s",
2837 filename
? filename
: "INVALID");
2840 } else if (args
->i
& XT_DELETE
) {
2841 if (session_delete(t
, filename
)) {
2842 show_oops(t
, "Can't delete session: %s",
2843 filename
? filename
: "INVALID");
2848 return (XT_CB_PASSTHROUGH
);
2852 script_cmd(struct tab
*t
, struct karg
*args
)
2861 if ((f
= fopen(args
->s
, "r")) == NULL
) {
2862 show_oops(t
, "Can't open script file: %s", args
->s
);
2866 if (fstat(fileno(f
), &sb
) == -1) {
2867 show_oops(t
, "Can't stat script file: %s", args
->s
);
2871 buf
= g_malloc0(sb
.st_size
+ 1);
2872 if (fread(buf
, 1, sb
.st_size
, f
) != sb
.st_size
) {
2873 show_oops(t
, "Can't read script file: %s", args
->s
);
2877 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
2886 return (XT_CB_PASSTHROUGH
);
2890 * Make a hardcopy of the page
2893 print_page(struct tab
*t
, struct karg
*args
)
2895 WebKitWebFrame
*frame
;
2897 GtkPrintOperation
*op
;
2898 GtkPrintOperationAction action
;
2899 GtkPrintOperationResult print_res
;
2900 GError
*g_err
= NULL
;
2901 int marg_l
, marg_r
, marg_t
, marg_b
;
2903 DNPRINTF(XT_D_PRINTING
, "%s:", __func__
);
2905 ps
= gtk_page_setup_new();
2906 op
= gtk_print_operation_new();
2907 action
= GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
;
2908 frame
= webkit_web_view_get_main_frame(t
->wv
);
2910 /* the default margins are too small, so we will bump them */
2911 marg_l
= gtk_page_setup_get_left_margin(ps
, GTK_UNIT_MM
) +
2912 XT_PRINT_EXTRA_MARGIN
;
2913 marg_r
= gtk_page_setup_get_right_margin(ps
, GTK_UNIT_MM
) +
2914 XT_PRINT_EXTRA_MARGIN
;
2915 marg_t
= gtk_page_setup_get_top_margin(ps
, GTK_UNIT_MM
) +
2916 XT_PRINT_EXTRA_MARGIN
;
2917 marg_b
= gtk_page_setup_get_bottom_margin(ps
, GTK_UNIT_MM
) +
2918 XT_PRINT_EXTRA_MARGIN
;
2921 gtk_page_setup_set_left_margin(ps
, marg_l
, GTK_UNIT_MM
);
2922 gtk_page_setup_set_right_margin(ps
, marg_r
, GTK_UNIT_MM
);
2923 gtk_page_setup_set_top_margin(ps
, marg_t
, GTK_UNIT_MM
);
2924 gtk_page_setup_set_bottom_margin(ps
, marg_b
, GTK_UNIT_MM
);
2926 gtk_print_operation_set_default_page_setup(op
, ps
);
2928 /* this appears to free 'op' and 'ps' */
2929 print_res
= webkit_web_frame_print_full(frame
, op
, action
, &g_err
);
2931 /* check it worked */
2932 if (print_res
== GTK_PRINT_OPERATION_RESULT_ERROR
) {
2933 show_oops(NULL
, "can't print: %s", g_err
->message
);
2934 g_error_free (g_err
);
2942 go_home(struct tab
*t
, struct karg
*args
)
2949 set_encoding(struct tab
*t
, struct karg
*args
)
2953 if (args
->s
&& strlen(g_strstrip(args
->s
)) == 0) {
2954 e
= webkit_web_view_get_custom_encoding(t
->wv
);
2956 e
= webkit_web_view_get_encoding(t
->wv
);
2957 show_oops(t
, "encoding: %s", e
? e
: "N/A");
2959 webkit_web_view_set_custom_encoding(t
->wv
, args
->s
);
2965 restart(struct tab
*t
, struct karg
*args
)
2969 a
.s
= XT_RESTART_TABS_FILE
;
2971 execvp(start_argv
[0], start_argv
);
2977 char *http_proxy_save
; /* not a setting, used to toggle */
2980 proxy_cmd(struct tab
*t
, struct karg
*args
)
2982 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
2989 if (http_proxy_save
)
2990 g_free(http_proxy_save
);
2991 http_proxy_save
= g_strdup(http_proxy
);
2994 if (args
->i
& XT_PRXY_SHOW
) {
2996 show_oops(t
, "http_proxy = %s", http_proxy
);
2998 show_oops(t
, "proxy is currently disabled");
2999 } else if (args
->i
& XT_PRXY_TOGGLE
) {
3000 if (http_proxy_save
== NULL
&& http_proxy
== NULL
) {
3001 show_oops(t
, "can't toggle proxy");
3007 setup_proxy(http_proxy_save
);
3010 return (XT_CB_PASSTHROUGH
);
3015 int (*func
)(struct tab
*, struct karg
*);
3019 { "command_mode", 0, command_mode
, XT_MODE_COMMAND
, 0 },
3020 { "insert_mode", 0, command_mode
, XT_MODE_INSERT
, 0 },
3021 { "command", 0, command
, ':', 0 },
3022 { "search", 0, command
, '/', 0 },
3023 { "searchb", 0, command
, '?', 0 },
3024 { "hinting", 0, command
, '.', 0 },
3025 { "hinting_newtab", 0, command
, ',', 0 },
3026 { "togglesrc", 0, toggle_src
, 0, 0 },
3027 { "editsrc", 0, edit_src
, 0, 0 },
3028 { "editelement", 0, edit_element
, 0, 0 },
3029 { "passthrough", 0, passthrough
, 0, 0 },
3030 { "modurl", 0, modurl
, 0, 0 },
3032 /* yanking and pasting */
3033 { "yankuri", 0, yank_uri
, 0, 0 },
3034 /* XXX: pasteuri{cur,new} do not work from the cmd_entry? */
3035 { "pasteuricur", 0, paste_uri
, XT_PASTE_CURRENT_TAB
, 0 },
3036 { "pasteurinew", 0, paste_uri
, XT_PASTE_NEW_TAB
, 0 },
3039 { "searchnext", 0, search
, XT_SEARCH_NEXT
, 0 },
3040 { "searchprevious", 0, search
, XT_SEARCH_PREV
, 0 },
3043 { "focusaddress", 0, focus
, XT_FOCUS_URI
, 0 },
3044 { "focussearch", 0, focus
, XT_FOCUS_SEARCH
, 0 },
3047 { "hinting", 0, hint
, 0, 0 },
3048 { "hinting_newtab", 0, hint
, XT_HINT_NEWTAB
, 0 },
3050 /* custom stylesheet */
3051 { "userstyle", 0, userstyle
, XT_STYLE_CURRENT_TAB
, 0 },
3052 { "userstyle_global", 0, userstyle
, XT_STYLE_GLOBAL
, 0 },
3055 { "goback", 0, navaction
, XT_NAV_BACK
, 0 },
3056 { "goforward", 0, navaction
, XT_NAV_FORWARD
, 0 },
3057 { "reload", 0, navaction
, XT_NAV_RELOAD
, 0 },
3058 { "stop", 0, navaction
, XT_NAV_STOP
, 0 },
3060 /* vertical movement */
3061 { "scrolldown", 0, move
, XT_MOVE_DOWN
, 0 },
3062 { "scrollup", 0, move
, XT_MOVE_UP
, 0 },
3063 { "scrollbottom", 0, move
, XT_MOVE_BOTTOM
, 0 },
3064 { "scrolltop", 0, move
, XT_MOVE_TOP
, 0 },
3065 { "1", 0, move
, XT_MOVE_TOP
, 0 },
3066 { "scrollhalfdown", 0, move
, XT_MOVE_HALFDOWN
, 0 },
3067 { "scrollhalfup", 0, move
, XT_MOVE_HALFUP
, 0 },
3068 { "scrollpagedown", 0, move
, XT_MOVE_PAGEDOWN
, 0 },
3069 { "scrollpageup", 0, move
, XT_MOVE_PAGEUP
, 0 },
3070 /* horizontal movement */
3071 { "scrollright", 0, move
, XT_MOVE_RIGHT
, 0 },
3072 { "scrollleft", 0, move
, XT_MOVE_LEFT
, 0 },
3073 { "scrollfarright", 0, move
, XT_MOVE_FARRIGHT
, 0 },
3074 { "scrollfarleft", 0, move
, XT_MOVE_FARLEFT
, 0 },
3076 { "favorites", 0, xtp_page_fl
, 0, 0 },
3077 { "fav", 0, xtp_page_fl
, 0, 0 },
3078 { "favadd", 0, add_favorite
, 0, 0 },
3080 { "qall", 0, quit
, 0, 0 },
3081 { "quitall", 0, quit
, 0, 0 },
3082 { "w", 0, save_tabs
, 0, 0 },
3083 { "wq", 0, save_tabs_and_quit
, 0, 0 },
3084 { "help", 0, help
, 0, 0 },
3085 { "about", 0, about
, 0, 0 },
3086 { "stats", 0, stats
, 0, 0 },
3087 { "version", 0, about
, 0, 0 },
3090 { "js", 0, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3091 { "save", 1, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3092 { "domain", 2, js_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3093 { "fqdn", 2, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3094 { "show", 1, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3095 { "all", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3096 { "persistent", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3097 { "session", 2, js_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3098 { "toggle", 1, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3099 { "domain", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3100 { "fqdn", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3102 /* cookie command */
3103 { "cookie", 0, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3104 { "save", 1, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3105 { "domain", 2, cookie_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3106 { "fqdn", 2, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3107 { "show", 1, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3108 { "all", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3109 { "persistent", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3110 { "session", 2, cookie_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3111 { "toggle", 1, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3112 { "domain", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3113 { "fqdn", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3114 { "purge", 1, cookie_cmd
, XT_DELETE
, 0 },
3116 /* plugin command */
3117 { "plugin", 0, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3118 { "save", 1, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3119 { "domain", 2, pl_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3120 { "fqdn", 2, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3121 { "show", 1, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3122 { "all", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3123 { "persistent", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3124 { "session", 2, pl_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3125 { "toggle", 1, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3126 { "domain", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3127 { "fqdn", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3129 /* toplevel (domain) command */
3130 { "toplevel", 0, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3131 { "toggle", 1, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3134 { "cookiejar", 0, xtp_page_cl
, 0, 0 },
3137 { "cert", 0, cert_cmd
, XT_SHOW
, 0 },
3138 { "save", 1, cert_cmd
, XT_SAVE
, 0 },
3139 { "show", 1, cert_cmd
, XT_SHOW
, 0 },
3141 { "ca", 0, ca_cmd
, 0, 0 },
3142 { "downloadmgr", 0, xtp_page_dl
, 0, 0 },
3143 { "dl", 0, xtp_page_dl
, 0, 0 },
3144 { "h", 0, xtp_page_hl
, 0, 0 },
3145 { "history", 0, xtp_page_hl
, 0, 0 },
3146 { "home", 0, go_home
, 0, 0 },
3147 { "restart", 0, restart
, 0, 0 },
3148 { "urlhide", 0, urlaction
, XT_URL_HIDE
, 0 },
3149 { "urlshow", 0, urlaction
, XT_URL_SHOW
, 0 },
3150 { "statustoggle", 0, statustoggle
, 0, 0 },
3151 { "run_script", 0, run_page_script
, 0, XT_USERARG
},
3153 { "print", 0, print_page
, 0, 0 },
3156 { "focusin", 0, resizetab
, XT_ZOOM_IN
, 0 },
3157 { "focusout", 0, resizetab
, XT_ZOOM_OUT
, 0 },
3158 { "focusreset", 0, resizetab
, XT_ZOOM_NORMAL
, 0 },
3159 { "q", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3160 { "quit", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3161 { "open", 0, tabaction
, XT_TAB_OPEN
, XT_URLARG
},
3162 { "tabclose", 0, tabaction
, XT_TAB_DELETE
, XT_PREFIX
| XT_INTARG
},
3163 { "tabedit", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3164 { "tabfirst", 0, movetab
, XT_TAB_FIRST
, 0 },
3165 { "tabhide", 0, tabaction
, XT_TAB_HIDE
, 0 },
3166 { "tablast", 0, movetab
, XT_TAB_LAST
, 0 },
3167 { "tabnew", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3168 { "tabnext", 0, movetab
, XT_TAB_NEXT
, XT_PREFIX
| XT_INTARG
},
3169 { "tabnextstyle", 0, tabaction
, XT_TAB_NEXTSTYLE
, 0 },
3170 { "tabprevious", 0, movetab
, XT_TAB_PREV
, XT_PREFIX
| XT_INTARG
},
3171 { "tabrewind", 0, movetab
, XT_TAB_FIRST
, 0 },
3172 { "tabshow", 0, tabaction
, XT_TAB_SHOW
, 0 },
3173 { "tabs", 0, buffers
, 0, 0 },
3174 { "tabundoclose", 0, tabaction
, XT_TAB_UNDO_CLOSE
, 0 },
3175 { "buffers", 0, buffers
, 0, 0 },
3176 { "ls", 0, buffers
, 0, 0 },
3177 { "encoding", 0, set_encoding
, 0, XT_USERARG
},
3178 { "loadimages", 0, tabaction
, XT_TAB_LOAD_IMAGES
, 0 },
3180 /* command aliases (handy when -S flag is used) */
3181 { "promptopen", 0, command
, XT_CMD_OPEN
, 0 },
3182 { "promptopencurrent", 0, command
, XT_CMD_OPEN_CURRENT
, 0 },
3183 { "prompttabnew", 0, command
, XT_CMD_TABNEW
, 0 },
3184 { "prompttabnewcurrent",0, command
, XT_CMD_TABNEW_CURRENT
, 0 },
3187 { "set", 0, set
, 0, XT_SETARG
},
3189 { "fullscreen", 0, fullscreen
, 0, 0 },
3190 { "f", 0, fullscreen
, 0, 0 },
3193 { "session", 0, session_cmd
, XT_SHOW
, 0 },
3194 { "delete", 1, session_cmd
, XT_DELETE
, XT_SESSARG
},
3195 { "open", 1, session_cmd
, XT_OPEN
, XT_SESSARG
},
3196 { "save", 1, session_cmd
, XT_SAVE
, XT_USERARG
},
3197 { "show", 1, session_cmd
, XT_SHOW
, 0 },
3199 /* external javascript */
3200 { "script", 0, script_cmd
, XT_EJS_SHOW
, XT_USERARG
},
3203 { "inspector", 0, inspector_cmd
, XT_INS_SHOW
, 0 },
3204 { "show", 1, inspector_cmd
, XT_INS_SHOW
, 0 },
3205 { "hide", 1, inspector_cmd
, XT_INS_HIDE
, 0 },
3208 { "proxy", 0, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3209 { "show", 1, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3210 { "toggle", 1, proxy_cmd
, XT_PRXY_TOGGLE
, 0 },
3217 } cmd_status
= {-1, 0};
3220 wv_release_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3223 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 1)
3230 wv_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3233 WebKitHitTestResult
*hit_test_result
;
3236 hit_test_result
= webkit_web_view_get_hit_test_result(t
->wv
, e
);
3237 g_object_get(hit_test_result
, "context", &context
, NULL
);
3242 if (context
& WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE
)
3243 t
->mode
= XT_MODE_INSERT
;
3245 t
->mode
= XT_MODE_COMMAND
;
3247 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3249 else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 8 /* btn 4 */) {
3255 } else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 9 /* btn 5 */) {
3257 a
.i
= XT_NAV_FORWARD
;
3267 tab_close_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3269 DNPRINTF(XT_D_TAB
, "tab_close_cb: tab %d\n", t
->tab_id
);
3271 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3280 activate_uri_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3282 const gchar
*uri
= gtk_entry_get_text(GTK_ENTRY(entry
));
3284 DNPRINTF(XT_D_URL
, "activate_uri_entry_cb: %s\n", uri
);
3287 show_oops(NULL
, "activate_uri_entry_cb invalid parameters");
3292 show_oops(t
, "activate_uri_entry_cb no uri");
3296 uri
+= strspn(uri
, "\t ");
3298 /* if xxxt:// treat specially */
3299 if (parse_xtp_url(t
, uri
))
3302 /* otherwise continue to load page normally */
3303 load_uri(t
, (gchar
*)uri
);
3308 activate_search_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3310 const gchar
*search
= gtk_entry_get_text(GTK_ENTRY(entry
));
3311 char *newuri
= NULL
;
3314 DNPRINTF(XT_D_URL
, "activate_search_entry_cb: %s\n", search
);
3317 show_oops(NULL
, "activate_search_entry_cb invalid parameters");
3321 if (search_string
== NULL
|| strlen(search_string
) == 0) {
3322 show_oops(t
, "no search_string");
3326 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
3328 enc_search
= soup_uri_encode(search
, XT_RESERVED_CHARS
);
3329 newuri
= g_strdup_printf(search_string
, enc_search
);
3333 webkit_web_view_load_uri(t
->wv
, newuri
);
3341 check_and_set_cookie(const gchar
*uri
, struct tab
*t
)
3343 struct domain
*d
= NULL
;
3346 if (uri
== NULL
|| t
== NULL
)
3349 if ((d
= wl_find_uri(uri
, &c_wl
)) == NULL
)
3354 DNPRINTF(XT_D_COOKIE
, "check_and_set_cookie: %s %s\n",
3355 es
? "enable" : "disable", uri
);
3357 g_object_set(G_OBJECT(t
->settings
),
3358 "enable-html5-local-storage", es
, (char *)NULL
);
3359 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3363 check_and_set_js(const gchar
*uri
, struct tab
*t
)
3365 struct domain
*d
= NULL
;
3368 if (uri
== NULL
|| t
== NULL
)
3371 if ((d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3376 DNPRINTF(XT_D_JS
, "check_and_set_js: %s %s\n",
3377 es
? "enable" : "disable", uri
);
3379 g_object_set(G_OBJECT(t
->settings
),
3380 "enable-scripts", es
, (char *)NULL
);
3381 g_object_set(G_OBJECT(t
->settings
),
3382 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
3383 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3385 button_set_stockid(t
->js_toggle
,
3386 es
? GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
);
3390 check_and_set_pl(const gchar
*uri
, struct tab
*t
)
3392 struct domain
*d
= NULL
;
3395 if (uri
== NULL
|| t
== NULL
)
3398 if ((d
= wl_find_uri(uri
, &pl_wl
)) == NULL
)
3403 DNPRINTF(XT_D_JS
, "check_and_set_pl: %s %s\n",
3404 es
? "enable" : "disable", uri
);
3406 g_object_set(G_OBJECT(t
->settings
),
3407 "enable-plugins", es
, (char *)NULL
);
3408 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3412 color_address_bar(gpointer p
)
3415 struct tab
*tt
, *t
= p
;
3416 gchar
*col_str
= XT_COLOR_WHITE
, *text
, *base
;
3417 const gchar
*uri
, *u
= NULL
, *error_str
= NULL
;
3420 gdk_threads_enter();
3422 DNPRINTF(XT_D_URL
, "%s:\n", __func__
);
3424 /* make sure t still exists */
3427 TAILQ_FOREACH(tt
, &tabs
, entry
)
3433 if ((uri
= get_uri(t
)) == NULL
)
3439 gdk_threads_leave();
3442 col_str
= XT_COLOR_YELLOW
;
3443 switch (load_compare_cert(u
, &error_str
)) {
3445 col_str
= XT_COLOR_BLUE
;
3448 col_str
= XT_COLOR_GREEN
;
3450 case CERT_UNTRUSTED
:
3451 col_str
= XT_COLOR_YELLOW
;
3454 col_str
= XT_COLOR_RED
;
3459 gdk_threads_enter();
3461 /* make sure t isn't deleted */
3462 TAILQ_FOREACH(tt
, &tabs
, entry
)
3469 /* test to see if the user navigated away and canceled the thread */
3470 if (t
->thread
!= g_thread_self())
3472 if ((uri
= get_uri(t
)) == NULL
) {
3476 if (strcmp(uri
, u
)) {
3477 /* make sure we are still the same url */
3484 if (!strcmp(col_str
, XT_COLOR_WHITE
)) {
3485 text
= gdk_color_to_string(
3486 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3487 base
= gdk_color_to_string(
3488 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3489 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3490 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3491 statusbar_modify_attr(t
, text
, base
);
3495 gdk_color_parse(col_str
, &color
);
3496 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3497 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3500 if (error_str
&& error_str
[0] != '\0')
3501 show_oops(t
, "%s", error_str
);
3506 /* t is invalid at this point */
3508 g_free((gpointer
)u
);
3511 gdk_threads_leave();
3516 show_ca_status(struct tab
*t
, const char *uri
)
3519 gchar
*col_str
= XT_COLOR_WHITE
, *text
, *base
;
3521 DNPRINTF(XT_D_URL
, "show_ca_status: %d %s %s\n",
3522 ssl_strict_certs
, ssl_ca_file
, uri
);
3529 if (ssl_ca_file
== NULL
) {
3530 if (g_str_has_prefix(uri
, "http://"))
3532 if (g_str_has_prefix(uri
, "https://")) {
3533 col_str
= XT_COLOR_RED
;
3538 if (g_str_has_prefix(uri
, "http://") ||
3539 !g_str_has_prefix(uri
, "https://"))
3543 * It is not necessary to see if the thread is already running.
3544 * If the thread is in progress setting it to something else aborts it
3548 /* thread the coloring of the address bar */
3549 t
->thread
= g_thread_create((GThreadFunc
)color_address_bar
, t
, TRUE
, NULL
);
3551 color_address_bar(t
);
3558 if (!strcmp(col_str
, XT_COLOR_WHITE
)) {
3559 text
= gdk_color_to_string(
3560 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3561 base
= gdk_color_to_string(
3562 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3563 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3564 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3565 statusbar_modify_attr(t
, text
, base
);
3569 gdk_color_parse(col_str
, &color
);
3570 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3571 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3577 free_favicon(struct tab
*t
)
3579 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p req %p\n",
3580 __func__
, t
->icon_download
, t
->icon_request
);
3582 if (t
->icon_request
)
3583 g_object_unref(t
->icon_request
);
3584 if (t
->icon_dest_uri
)
3585 g_free(t
->icon_dest_uri
);
3587 t
->icon_request
= NULL
;
3588 t
->icon_dest_uri
= NULL
;
3592 xt_icon_from_name(struct tab
*t
, gchar
*name
)
3594 if (!enable_favicon_entry
)
3597 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->uri_entry
),
3598 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3600 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3601 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3603 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3604 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3608 xt_icon_from_pixbuf(struct tab
*t
, GdkPixbuf
*pb
)
3610 GdkPixbuf
*pb_scaled
;
3612 if (gdk_pixbuf_get_width(pb
) > 16 || gdk_pixbuf_get_height(pb
) > 16)
3613 pb_scaled
= gdk_pixbuf_scale_simple(pb
, 16, 16,
3614 GDK_INTERP_BILINEAR
);
3618 if (enable_favicon_entry
) {
3621 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
3622 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3625 if (show_url
== 0) {
3626 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
3627 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3629 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3630 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3633 /* XXX: Only supports the minimal tabs atm. */
3634 if (enable_favicon_tabs
)
3635 gtk_image_set_from_pixbuf(GTK_IMAGE(t
->tab_elems
.favicon
),
3638 if (pb_scaled
!= pb
)
3639 g_object_unref(pb_scaled
);
3643 xt_icon_from_file(struct tab
*t
, char *file
)
3647 if (g_str_has_prefix(file
, "file://"))
3648 file
+= strlen("file://");
3650 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
3652 xt_icon_from_pixbuf(t
, pb
);
3655 xt_icon_from_name(t
, "text-html");
3659 is_valid_icon(char *file
)
3662 const char *mime_type
;
3666 gf
= g_file_new_for_path(file
);
3667 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
3669 mime_type
= g_file_info_get_content_type(fi
);
3670 valid
= g_strcmp0(mime_type
, "image/x-ico") == 0 ||
3671 g_strcmp0(mime_type
, "image/vnd.microsoft.icon") == 0 ||
3672 g_strcmp0(mime_type
, "image/png") == 0 ||
3673 g_strcmp0(mime_type
, "image/gif") == 0 ||
3674 g_strcmp0(mime_type
, "application/octet-stream") == 0;
3682 set_favicon_from_file(struct tab
*t
, char *file
)
3686 if (t
== NULL
|| file
== NULL
)
3689 if (g_str_has_prefix(file
, "file://"))
3690 file
+= strlen("file://");
3691 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading %s\n", __func__
, file
);
3693 if (!stat(file
, &sb
)) {
3694 if (sb
.st_size
== 0 || !is_valid_icon(file
)) {
3695 /* corrupt icon so trash it */
3696 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3699 /* no need to set icon to default here */
3703 xt_icon_from_file(t
, file
);
3707 favicon_download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
3710 WebKitDownloadStatus status
= webkit_download_get_status(download
);
3711 struct tab
*tt
= NULL
, *t
= NULL
;
3714 * find the webview instead of passing in the tab as it could have been
3715 * deleted from underneath us.
3717 TAILQ_FOREACH(tt
, &tabs
, entry
) {
3726 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d status %d\n",
3727 __func__
, t
->tab_id
, status
);
3730 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
3732 t
->icon_download
= NULL
;
3735 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
3738 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
3741 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
3743 DNPRINTF(XT_D_DOWNLOAD
, "%s: freeing favicon %d\n",
3744 __func__
, t
->tab_id
);
3745 t
->icon_download
= NULL
;
3748 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
3751 DNPRINTF(XT_D_DOWNLOAD
, "%s: setting icon to %s\n",
3752 __func__
, t
->icon_dest_uri
);
3753 set_favicon_from_file(t
, t
->icon_dest_uri
);
3754 /* these will be freed post callback */
3755 t
->icon_request
= NULL
;
3756 t
->icon_download
= NULL
;
3764 abort_favicon_download(struct tab
*t
)
3766 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p\n", __func__
, t
->icon_download
);
3768 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
3769 if (t
->icon_download
) {
3770 g_signal_handlers_disconnect_by_func(G_OBJECT(t
->icon_download
),
3771 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3772 webkit_download_cancel(t
->icon_download
);
3773 t
->icon_download
= NULL
;
3778 xt_icon_from_name(t
, "text-html");
3782 notify_icon_loaded_cb(WebKitWebView
*wv
, gchar
*uri
, struct tab
*t
)
3784 DNPRINTF(XT_D_DOWNLOAD
, "%s %s\n", __func__
, uri
);
3786 if (uri
== NULL
|| t
== NULL
)
3789 #if WEBKIT_CHECK_VERSION(1, 4, 0)
3790 /* take icon from WebKitIconDatabase */
3793 pb
= webkit_web_view_get_icon_pixbuf(wv
);
3795 xt_icon_from_pixbuf(t
, pb
);
3798 xt_icon_from_name(t
, "text-html");
3799 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
3800 /* download icon to cache dir */
3801 gchar
*name_hash
, file
[PATH_MAX
];
3804 if (t
->icon_request
) {
3805 DNPRINTF(XT_D_DOWNLOAD
, "%s: download in progress\n", __func__
);
3809 /* check to see if we got the icon in cache */
3810 name_hash
= g_compute_checksum_for_string(G_CHECKSUM_SHA256
, uri
, -1);
3811 snprintf(file
, sizeof file
, "%s" PS
"%s.ico", cache_dir
, name_hash
);
3814 if (!stat(file
, &sb
)) {
3815 if (sb
.st_size
> 0) {
3816 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading from cache %s\n",
3818 set_favicon_from_file(t
, file
);
3822 /* corrupt icon so trash it */
3823 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3828 /* create download for icon */
3829 t
->icon_request
= webkit_network_request_new(uri
);
3830 if (t
->icon_request
== NULL
) {
3831 DNPRINTF(XT_D_DOWNLOAD
, "%s: invalid uri %s\n",
3836 t
->icon_download
= webkit_download_new(t
->icon_request
);
3837 if (t
->icon_download
== NULL
)
3840 /* we have to free icon_dest_uri later */
3841 t
->icon_dest_uri
= g_strdup_printf("file://%s", file
);
3842 webkit_download_set_destination_uri(t
->icon_download
,
3845 if (webkit_download_get_status(t
->icon_download
) ==
3846 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
3847 g_object_unref(t
->icon_request
);
3848 g_free(t
->icon_dest_uri
);
3849 t
->icon_request
= NULL
;
3850 t
->icon_dest_uri
= NULL
;
3854 g_signal_connect(G_OBJECT(t
->icon_download
), "notify::status",
3855 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3857 webkit_download_start(t
->icon_download
);
3862 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3864 const gchar
*uri
= NULL
;
3865 struct history
*h
, find
;
3869 DNPRINTF(XT_D_URL
, "notify_load_status_cb: %d %s\n",
3870 webkit_web_view_get_load_status(wview
),
3871 get_uri(t
) ? get_uri(t
) : "NOTHING");
3874 show_oops(NULL
, "notify_load_status_cb invalid parameters");
3878 switch (webkit_web_view_get_load_status(wview
)) {
3879 case WEBKIT_LOAD_PROVISIONAL
:
3881 abort_favicon_download(t
);
3882 #if GTK_CHECK_VERSION(2, 20, 0)
3883 gtk_widget_show(t
->spinner
);
3884 gtk_spinner_start(GTK_SPINNER(t
->spinner
));
3886 gtk_label_set_text(GTK_LABEL(t
->label
), "Loading");
3888 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), TRUE
);
3890 /* assume we are a new address */
3891 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3892 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3893 text
= gdk_color_to_string(
3894 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3895 base
= gdk_color_to_string(
3896 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3897 statusbar_modify_attr(t
, text
, base
);
3901 /* take focus if we are visible */
3907 /* kill color thread */
3912 case WEBKIT_LOAD_COMMITTED
:
3917 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
3923 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
3925 /* check if js white listing is enabled */
3926 if (enable_plugin_whitelist
)
3927 check_and_set_pl(uri
, t
);
3928 if (enable_cookie_whitelist
)
3929 check_and_set_cookie(uri
, t
);
3930 if (enable_js_whitelist
)
3931 check_and_set_js(uri
, t
);
3937 /* we know enough to autosave the session */
3938 if (session_autosave
) {
3943 show_ca_status(t
, uri
);
3944 run_script(t
, JS_HINTING
);
3945 if (enable_autoscroll
)
3946 run_script(t
, JS_AUTOSCROLL
);
3949 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
3951 if (color_visited_uris
) {
3952 color_visited(t
, color_visited_helper());
3954 /* This colors the links you middle-click (open in new
3955 * tab) in the current tab. */
3956 if (t
->tab_id
!= gtk_notebook_get_current_page(notebook
) &&
3957 (uri
= get_uri(t
)) != NULL
)
3958 color_visited(get_current_tab(),
3959 g_strdup_printf("{'%s' : 'dummy'}", uri
));
3963 case WEBKIT_LOAD_FINISHED
:
3965 if ((uri
= get_uri(t
)) == NULL
)
3968 if (!strncmp(uri
, "http://", strlen("http://")) ||
3969 !strncmp(uri
, "https://", strlen("https://")) ||
3970 !strncmp(uri
, "file://", strlen("file://"))) {
3971 find
.uri
= (gchar
*)uri
;
3972 h
= RB_FIND(history_list
, &hl
, &find
);
3974 insert_history_item(uri
,
3975 get_title(t
, FALSE
), time(NULL
));
3977 h
->time
= time(NULL
);
3980 set_status(t
, (char *)uri
, XT_STATUS_URI
);
3981 #if WEBKIT_CHECK_VERSION(1, 1, 18)
3982 case WEBKIT_LOAD_FAILED
:
3985 #if GTK_CHECK_VERSION(2, 20, 0)
3986 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
3987 gtk_widget_hide(t
->spinner
);
3990 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
3995 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
), TRUE
);
3997 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
),
3998 can_go_back_for_real(t
));
4000 gtk_widget_set_sensitive(GTK_WIDGET(t
->forward
),
4001 can_go_forward_for_real(t
));
4005 notify_title_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
4007 const gchar
*title
= NULL
, *win_title
= NULL
;
4009 title
= get_title(t
, FALSE
);
4010 win_title
= get_title(t
, TRUE
);
4012 gtk_label_set_text(GTK_LABEL(t
->label
), title
);
4013 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
), title
);
4016 if (win_title
&& t
->tab_id
== gtk_notebook_get_current_page(notebook
))
4017 gtk_window_set_title(GTK_WINDOW(main_window
), win_title
);
4021 get_domain(const gchar
*host
)
4026 /* handle silly domains like .co.uk */
4028 if ((x
= strlen(host
)) <= 6)
4029 return (g_strdup(host
));
4031 if (host
[x
- 3] == '.' && host
[x
- 6] == '.') {
4037 return (g_strdup(&host
[x
+ 1]));
4041 p
= g_strrstr(host
, ".");
4043 return (g_strdup(""));
4049 return (g_strdup(p
+ 1));
4051 return (g_strdup(host
));
4055 js_autorun(struct tab
*t
)
4059 size_t got_default
= 0, got_host
= 0;
4061 char deff
[PATH_MAX
], hostf
[PATH_MAX
];
4062 char *js
= NULL
, *jsat
, *domain
= NULL
;
4063 FILE *deffile
= NULL
, *hostfile
= NULL
;
4065 if (enable_js_autorun
== 0)
4070 !(g_str_has_prefix(uri
, "http://") ||
4071 g_str_has_prefix(uri
, "https://")))
4074 su
= soup_uri_new(uri
);
4077 if (!SOUP_URI_VALID_FOR_HTTP(su
))
4080 DNPRINTF(XT_D_JS
, "%s: host: %s domain: %s\n", __func__
,
4082 domain
= get_domain(su
->host
);
4084 snprintf(deff
, sizeof deff
, "%s" PS
"default.js", js_dir
);
4085 if ((deffile
= fopen(deff
, "r")) != NULL
) {
4086 if (fstat(fileno(deffile
), &sb
) == -1) {
4087 show_oops(t
, "can't stat default JS file");
4090 got_default
= sb
.st_size
;
4093 /* try host first followed by domain */
4094 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, su
->host
);
4095 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4096 if ((hostfile
= fopen(hostf
, "r")) == NULL
) {
4097 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, domain
);
4098 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4099 if ((hostfile
= fopen(hostf
, "r")) == NULL
)
4102 DNPRINTF(XT_D_JS
, "file: %s\n", hostf
);
4103 if (fstat(fileno(hostfile
), &sb
) == -1) {
4104 show_oops(t
, "can't stat %s JS file", hostf
);
4107 got_host
= sb
.st_size
;
4110 if (got_default
+ got_host
== 0)
4113 js
= g_malloc0(got_default
+ got_host
+ 1);
4117 if (fread(js
, got_default
, 1, deffile
) != 1) {
4118 show_oops(t
, "default file read error");
4121 jsat
= js
+ got_default
;
4125 if (fread(jsat
, got_host
, 1, hostfile
) != 1) {
4126 show_oops(t
, "host file read error");
4131 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
4148 webview_load_finished_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4150 /* autorun some js if enabled */
4158 webview_progress_changed_cb(WebKitWebView
*wv
, int progress
, struct tab
*t
)
4160 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->uri_entry
),
4161 progress
== 100 ? 0 : (double)progress
/ 100);
4162 if (show_url
== 0) {
4163 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
4164 progress
== 100 ? 0 : (double)progress
/ 100);
4167 update_statusbar_position(NULL
, NULL
);
4171 strict_transport_rb_cmp(struct strict_transport
*a
, struct strict_transport
*b
)
4176 /* compare strings from the end */
4177 l1
= strlen(a
->host
);
4178 l2
= strlen(b
->host
);
4182 for (; *p1
== *p2
&& p1
> a
->host
&& p2
> b
->host
;
4187 * Check if we need to do pattern expansion,
4188 * or if we're just keeping the tree in order
4190 if (a
->flags
& XT_STS_FLAGS_EXPAND
&&
4191 b
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) {
4192 /* Check if we're matching the
4193 * 'host.xyz' part in '*.host.xyz'
4195 if (p2
== b
->host
&& (p1
== a
->host
|| *(p1
-1) == '.')) {
4200 if (p1
== a
->host
&& p2
== b
->host
)
4214 RB_GENERATE(strict_transport_tree
, strict_transport
, entry
,
4215 strict_transport_rb_cmp
);
4218 strict_transport_add(const char *domain
, time_t timeout
, int subdomains
)
4220 struct strict_transport
*d
, find
;
4224 if (enable_strict_transport
== FALSE
)
4227 DPRINTF("strict_transport_add(%s,%lld,%d)\n", domain
,
4228 (long long)timeout
, subdomains
);
4234 find
.host
= (char *)domain
;
4236 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4240 /* check if update is needed */
4241 if (d
->timeout
== timeout
&&
4242 (d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) == subdomains
)
4245 d
->timeout
= timeout
;
4247 d
->flags
|= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4249 /* We're still initializing */
4250 if (strict_transport_file
== NULL
)
4253 if ((f
= fopen(strict_transport_file
, "w")) == NULL
) {
4255 "can't open strict-transport rules file");
4259 fprintf(f
, "# Generated file - do not update unless you know "
4260 "what you're doing\n");
4261 RB_FOREACH(d
, strict_transport_tree
, &st_tree
) {
4262 if (d
->timeout
< now
)
4264 fprintf(f
, "%s\t%lld\t%d\n", d
->host
, (long long)d
->timeout
,
4265 d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
);
4269 d
= g_malloc(sizeof *d
);
4270 d
->host
= g_strdup(domain
);
4271 d
->timeout
= timeout
;
4273 d
->flags
= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4276 RB_INSERT(strict_transport_tree
, &st_tree
, d
);
4278 /* We're still initializing */
4279 if (strict_transport_file
== NULL
)
4282 if ((f
= fopen(strict_transport_file
, "a+")) == NULL
) {
4284 "can't open strict-transport rules file");
4288 fseek(f
, 0, SEEK_END
);
4289 fprintf(f
,"%s\t%lld\t%d\n", d
->host
, (long long)timeout
, subdomains
);
4296 strict_transport_check(const char *host
)
4298 static struct strict_transport
*d
= NULL
;
4299 struct strict_transport find
;
4301 if (enable_strict_transport
== FALSE
)
4304 find
.host
= (char *)host
;
4306 /* match for domains that include subdomains */
4307 find
.flags
= XT_STS_FLAGS_EXPAND
;
4309 /* First, check if we're already at the right node */
4310 if (d
!= NULL
&& strict_transport_rb_cmp(&find
, d
) == 0) {
4314 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4322 strict_transport_init()
4324 char file
[PATH_MAX
];
4330 time_t timeout
, now
;
4333 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_STS_FILE
);
4334 if ((f
= fopen(file
, "r")) == NULL
) {
4335 strict_transport_file
= g_strdup(file
);
4346 if ((rule
= fparseln(f
, &len
, NULL
, delim
, 0)) == NULL
) {
4347 if (!feof(f
) || ferror(f
))
4353 /* get second entry */
4354 if ((ptr
= strpbrk(rule
, " \t")) == NULL
)
4358 timeout
= atoi(ptr
);
4360 /* get third entry */
4361 if ((ptr
= strpbrk(ptr
, " \t")) == NULL
)
4365 subdomains
= atoi(ptr
);
4368 strict_transport_add(rule
, timeout
, subdomains
);
4373 strict_transport_file
= g_strdup(file
);
4377 startpage_add("strict-transport rules file ('%s') is corrupt", file
);
4385 strict_transport_security_cb(SoupMessage
*msg
, gpointer data
)
4391 int subdomains
= FALSE
;
4396 sts
= soup_message_headers_get_one(msg
->response_headers
,
4397 "Strict-Transport-Security");
4398 uri
= soup_message_get_uri(msg
);
4400 if (sts
== NULL
|| uri
== NULL
)
4403 if ((ptr
= strcasestr(sts
, "max-age="))) {
4404 ptr
+= strlen("max-age=");
4405 timeout
= atoll(ptr
);
4407 return; /* malformed header - max-age must be included */
4409 if ((ptr
= strcasestr(sts
, "includeSubDomains")))
4412 strict_transport_add(uri
->host
, timeout
+ time(NULL
), subdomains
);
4416 session_rq_cb(SoupSession
*s
, SoupMessage
*msg
, SoupSocket
*socket
,
4426 if (s
== NULL
|| msg
== NULL
)
4429 if (referer_mode
== XT_REFERER_ALWAYS
)
4432 /* Check if referer is set - and what the user requested for referers */
4433 ref
= soup_message_headers_get_one(msg
->request_headers
, "Referer");
4435 DNPRINTF(XT_D_NAV
, "session_rq_cb: Referer: %s\n", ref
);
4436 switch (referer_mode
) {
4437 case XT_REFERER_NEVER
:
4438 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing referer\n");
4439 soup_message_headers_remove(msg
->request_headers
,
4442 case XT_REFERER_SAME_DOMAIN
:
4443 ref_uri
= soup_uri_new(ref
);
4444 dest
= soup_message_get_uri(msg
);
4446 ref_suffix
= tld_get_suffix(ref_uri
->host
);
4447 dest_suffix
= tld_get_suffix(dest
->host
);
4449 if (dest
&& ref_suffix
&& dest_suffix
&&
4450 strcmp(ref_suffix
, dest_suffix
) != 0) {
4451 soup_message_headers_remove(msg
->request_headers
,
4453 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4454 "referer (not same domain) (suffixes: %s - %s)\n",
4455 ref_suffix
, dest_suffix
);
4457 soup_uri_free(ref_uri
);
4459 case XT_REFERER_SAME_FQDN
:
4460 ref_uri
= soup_uri_new(ref
);
4461 dest
= soup_message_get_uri(msg
);
4462 if (dest
&& strcmp(ref_uri
->host
, dest
->host
) != 0) {
4463 soup_message_headers_remove(msg
->request_headers
,
4465 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4466 "referer (not same fqdn) (should be %s)\n",
4469 soup_uri_free(ref_uri
);
4471 case XT_REFERER_CUSTOM
:
4472 DNPRINTF(XT_D_NAV
, "session_rq_cb: setting referer "
4473 "to %s\n", referer_custom
);
4474 soup_message_headers_replace(msg
->request_headers
,
4475 "Referer", referer_custom
);
4480 if (enable_strict_transport
) {
4481 soup_message_add_header_handler(msg
, "finished",
4482 "Strict-Transport-Security",
4483 G_CALLBACK(strict_transport_security_cb
), NULL
);
4488 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
4489 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
4490 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
4493 WebKitWebNavigationReason reason
;
4494 struct domain
*d
= NULL
;
4497 show_oops(NULL
, "webview_npd_cb invalid parameters");
4501 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
4503 webkit_network_request_get_uri(request
));
4505 uri
= (char *)webkit_network_request_get_uri(request
);
4507 if (!auto_load_images
&& t
->load_images
) {
4509 /* Disable autoloading of images, now that we're done loading
4511 g_object_set(G_OBJECT(t
->settings
),
4512 "auto-load-images", FALSE
, (char *)NULL
);
4513 webkit_web_view_set_settings(t
->wv
, t
->settings
);
4515 t
->load_images
= FALSE
;
4518 /* If this is an xtp url, we don't load anything else. */
4519 if (parse_xtp_url(t
, uri
))
4522 if ((t
->mode
== XT_MODE_HINT
&& t
->new_tab
) || t
->ctrl_click
) {
4524 create_new_tab(uri
, NULL
, ctrl_click_focus
, -1);
4525 webkit_web_policy_decision_ignore(pd
);
4526 return (TRUE
); /* we made the decission */
4529 /* Change user agent if more than one has been given. */
4530 if (user_agent_count
> 1) {
4531 struct user_agent
*ua
;
4533 if ((ua
= TAILQ_NEXT(user_agent
, entry
)) == NULL
)
4534 user_agent
= TAILQ_FIRST(&ua_list
);
4538 free(t
->user_agent
);
4539 t
->user_agent
= g_strdup(user_agent
->value
);
4541 DNPRINTF(XT_D_NAV
, "user-agent: %s\n", t
->user_agent
);
4543 g_object_set(G_OBJECT(t
->settings
),
4544 "user-agent", t
->user_agent
, (char *)NULL
);
4546 webkit_web_view_set_settings(wv
, t
->settings
);
4550 * This is a little hairy but it comes down to this:
4551 * when we run in whitelist mode we have to assist the browser in
4552 * opening the URL that it would have opened in a new tab.
4554 reason
= webkit_web_navigation_action_get_reason(na
);
4555 if (reason
== WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED
) {
4556 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
4557 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1)
4558 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4560 webkit_web_policy_decision_use(pd
);
4561 return (TRUE
); /* we made the decision */
4568 webview_rrs_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, WebKitWebResource
*res
,
4569 WebKitNetworkRequest
*request
, WebKitNetworkResponse
*response
,
4575 msg
= webkit_network_request_get_message(request
);
4578 uri
= soup_message_get_uri(msg
);
4581 if (strcmp(soup_uri_get_scheme(uri
), SOUP_URI_SCHEME_HTTP
) == 0) {
4582 if (strict_transport_check(uri
->host
)) {
4583 DNPRINTF(XT_D_NAV
, "webview_rrs_cb: force https for %s\n",
4585 soup_uri_set_scheme(uri
, SOUP_URI_SCHEME_HTTPS
);
4591 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4594 struct domain
*d
= NULL
;
4596 WebKitWebView
*webview
= NULL
;
4599 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
4600 webkit_web_view_get_uri(wv
));
4603 /* open in current tab */
4605 } else if (enable_scripts
== 0 && enable_js_whitelist
== 1) {
4606 uri
= webkit_web_view_get_uri(wv
);
4607 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4610 if (t
->ctrl_click
) {
4611 x
= ctrl_click_focus
;
4614 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4616 } else if (enable_scripts
== 1) {
4617 if (t
->ctrl_click
) {
4618 x
= ctrl_click_focus
;
4621 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4629 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
4632 struct domain
*d
= NULL
;
4634 DNPRINTF(XT_D_NAV
, "webview_close_cb: %d\n", t
->tab_id
);
4636 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
4637 uri
= webkit_web_view_get_uri(wv
);
4638 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4642 } else if (enable_scripts
== 1)
4649 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
4651 /* we can not eat the event without throwing gtk off so defer it */
4653 /* catch middle click */
4654 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 2) {
4659 /* catch ctrl click */
4660 if (e
->type
== GDK_BUTTON_RELEASE
&&
4661 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
4666 return (XT_CB_PASSTHROUGH
);
4670 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
4672 struct mime_type
*m
;
4674 m
= find_mime_type(mime_type
);
4680 return (fork_exec(t
, m
->mt_action
,
4681 webkit_network_request_get_uri(request
),
4682 "can't launch MIME handler", 0));
4686 get_mime_type(const char *file
)
4689 char *mime_type
= NULL
;
4693 if (g_str_has_prefix(file
, "file://"))
4694 file
+= strlen("file://");
4696 gf
= g_file_new_for_path(file
);
4697 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
4699 if ((m
= g_file_info_get_content_type(fi
)) != NULL
)
4700 mime_type
= g_strdup(m
);
4708 run_download_mimehandler(char *mime_type
, char *file
)
4710 struct mime_type
*m
;
4712 m
= find_mime_type(mime_type
);
4716 return (fork_exec(NULL
, m
->mt_action
, file
,
4717 "can't launch download MIME handler", 0));
4721 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
4724 WebKitDownloadStatus status
;
4725 const char *file
= NULL
;
4728 if (download
== NULL
)
4730 status
= webkit_download_get_status(download
);
4731 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
4734 file
= webkit_download_get_destination_uri(download
);
4737 mime
= get_mime_type(file
);
4741 if (g_str_has_prefix(file
, "file://"))
4742 file
+= strlen("file://");
4743 run_download_mimehandler((char *)mime
, (char *)file
);
4748 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
4749 WebKitNetworkRequest
*request
, char *mime_type
,
4750 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
4753 show_oops(NULL
, "webview_mimetype_cb invalid parameters");
4757 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
4758 t
->tab_id
, mime_type
);
4760 if (run_mimehandler(t
, mime_type
, request
) == 0) {
4761 webkit_web_policy_decision_ignore(decision
);
4766 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
4767 webkit_web_policy_decision_download(decision
);
4775 download_start(struct tab
*t
, struct download
*d
, int flag
)
4777 WebKitNetworkRequest
*req
;
4779 const gchar
*suggested_name
;
4780 gchar
*filename
= NULL
;
4785 if (d
== NULL
|| t
== NULL
) {
4786 show_oops(NULL
, "%s invalid parameters", __func__
);
4790 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
4791 if (suggested_name
== NULL
)
4792 return (FALSE
); /* abort download */
4803 filename
= g_strdup_printf("%d%s", i
, suggested_name
);
4806 uri
= g_strdup_printf("%s\\%s", download_dir
, i
?
4807 filename
: suggested_name
);
4809 uri
= g_strdup_printf("file://%s/%s", download_dir
, i
?
4810 filename
: suggested_name
);
4814 } while (!stat(uri
, &sb
));
4816 } while (!stat(uri
+ strlen("file://"), &sb
)); /* XXX is the + strlen right? */
4819 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d filename %s "
4820 "local %s\n", __func__
, t
->tab_id
, filename
, uri
);
4822 /* if we're restarting the download, or starting
4823 * it after doing something else, we need to recreate
4824 * the download request.
4826 if (flag
== XT_DL_RESTART
) {
4827 req
= webkit_network_request_new(webkit_download_get_uri(d
->download
));
4828 webkit_download_cancel(d
->download
);
4829 g_object_unref(d
->download
);
4830 d
->download
= webkit_download_new(req
);
4833 webkit_download_set_destination_uri(d
->download
, uri
);
4835 if (webkit_download_get_status(d
->download
) ==
4836 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
4837 show_oops(t
, "%s: download failed to start", __func__
);
4839 gtk_label_set_text(GTK_LABEL(t
->label
), "Download Failed");
4841 /* connect "download first" mime handler */
4842 g_signal_connect(G_OBJECT(d
->download
), "notify::status",
4843 G_CALLBACK(download_status_changed_cb
), NULL
);
4845 /* get from history */
4846 g_object_ref(d
->download
);
4847 gtk_label_set_text(GTK_LABEL(t
->label
), "Downloading");
4848 show_oops(t
, "Download of '%s' started...",
4849 basename((char *)webkit_download_get_destination_uri(d
->download
)));
4852 if (flag
!= XT_DL_START
)
4853 webkit_download_start(d
->download
);
4855 DNPRINTF(XT_D_DOWNLOAD
, "download status : %d",
4856 webkit_download_get_status(d
->download
));
4858 /* sync other download manager tabs */
4859 update_download_tabs(NULL
);
4871 download_ask_cb(struct tab
*t
, GdkEventKey
*e
, gpointer data
)
4873 struct download
*d
= data
;
4877 t
->mode_cb_data
= NULL
;
4880 e
->keyval
= GDK_Escape
;
4881 return (XT_CB_PASSTHROUGH
);
4884 DPRINTF("download_ask_cb: User pressed %c\n", e
->keyval
);
4885 if (e
->keyval
== 'y' || e
->keyval
== 'Y' || e
->keyval
== GDK_Return
)
4886 /* We need to do a RESTART, because we're not calling from
4887 * webview_download_cb
4889 download_start(t
, d
, XT_DL_RESTART
);
4891 /* for all other keyvals, we just let the download be */
4892 e
->keyval
= GDK_Escape
;
4893 return (XT_CB_HANDLED
);
4897 download_ask(struct tab
*t
, struct download
*d
)
4899 const gchar
*suggested_name
;
4901 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
4902 if (suggested_name
== NULL
)
4903 return (FALSE
); /* abort download */
4905 show_oops(t
, "download file %s [y/n] ?", suggested_name
);
4906 t
->mode_cb
= download_ask_cb
;
4907 t
->mode_cb_data
= d
;
4913 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
4916 const gchar
*suggested_name
;
4917 struct download
*download_entry
;
4920 if (wk_download
== NULL
|| t
== NULL
) {
4921 show_oops(NULL
, "%s invalid parameters", __func__
);
4925 suggested_name
= webkit_download_get_suggested_filename(wk_download
);
4926 if (suggested_name
== NULL
)
4927 return (FALSE
); /* abort download */
4929 download_entry
= g_malloc(sizeof(struct download
));
4930 download_entry
->download
= wk_download
;
4931 download_entry
->tab
= t
;
4932 download_entry
->id
= next_download_id
++;
4933 RB_INSERT(download_list
, &downloads
, download_entry
);
4935 if (download_mode
== XT_DM_START
)
4936 ret
= download_start(t
, download_entry
, XT_DL_START
);
4937 else if (download_mode
== XT_DM_ASK
)
4938 ret
= download_ask(t
, download_entry
);
4939 else if (download_mode
== XT_DM_ADD
)
4940 show_oops(t
, "added %s to download manager",
4943 /* sync other download manager tabs */
4944 update_download_tabs(NULL
);
4947 * NOTE: never redirect/render the current tab before this
4948 * function returns. This will cause the download to never start.
4950 return (ret
); /* start download */
4954 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
4956 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
4959 show_oops(NULL
, "webview_hover_cb");
4964 set_status(t
, uri
, XT_STATUS_LINK
);
4967 set_status(t
, t
->status
, XT_STATUS_NOTHING
);
4972 mark(struct tab
*t
, struct karg
*arg
)
4979 if ((index
= marktoindex(mark
)) == -1)
4982 if (arg
->i
== XT_MARK_SET
)
4983 t
->mark
[index
] = gtk_adjustment_get_value(t
->adjust_v
);
4984 else if (arg
->i
== XT_MARK_GOTO
) {
4985 if (t
->mark
[index
] == XT_INVALID_MARK
) {
4986 show_oops(t
, "mark '%c' does not exist", mark
);
4989 /* XXX t->mark[index] can be bigger than the maximum if ajax or
4990 something changes the document size */
4991 pos
= gtk_adjustment_get_value(t
->adjust_v
);
4992 gtk_adjustment_set_value(t
->adjust_v
, t
->mark
[index
]);
4993 t
->mark
[marktoindex('\'')] = pos
;
5000 marks_clear(struct tab
*t
)
5004 for (i
= 0; i
< LENGTH(t
->mark
); i
++)
5005 t
->mark
[i
] = XT_INVALID_MARK
;
5011 char file
[PATH_MAX
];
5012 char *line
= NULL
, *p
;
5017 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
5018 if ((f
= fopen(file
, "r+")) == NULL
) {
5019 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
5023 for (i
= 1; ; i
++) {
5024 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
5026 if (strlen(line
) == 0 || line
[0] == '#') {
5032 p
= strtok(line
, " \t");
5034 if (p
== NULL
|| strlen(p
) != 1 ||
5035 (index
= qmarktoindex(*p
)) == -1) {
5036 warnx("corrupt quickmarks file, line %d", i
);
5040 p
= strtok(NULL
, " \t");
5041 if (qmarks
[index
] != NULL
)
5042 g_free(qmarks
[index
]);
5043 qmarks
[index
] = g_strdup(p
);
5054 char file
[PATH_MAX
];
5058 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
5059 if ((f
= fopen(file
, "r+")) == NULL
) {
5060 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
5064 for (i
= 0; i
< XT_NOQMARKS
; i
++)
5065 if (qmarks
[i
] != NULL
)
5066 fprintf(f
, "%c %s\n", indextoqmark(i
), qmarks
[i
]);
5074 qmark(struct tab
*t
, struct karg
*arg
)
5079 mark
= arg
->s
[strlen(arg
->s
)-1];
5080 index
= qmarktoindex(mark
);
5086 if (qmarks
[index
] != NULL
) {
5087 g_free(qmarks
[index
]);
5088 qmarks
[index
] = NULL
;
5091 qmarks_load(); /* sync if multiple instances */
5092 qmarks
[index
] = g_strdup(get_uri(t
));
5096 if (qmarks
[index
] != NULL
)
5097 load_uri(t
, qmarks
[index
]);
5099 show_oops(t
, "quickmark \"%c\" does not exist",
5105 if (qmarks
[index
] != NULL
)
5106 create_new_tab(qmarks
[index
], NULL
, 1, -1);
5108 show_oops(t
, "quickmark \"%c\" does not exist",
5119 go_up(struct tab
*t
, struct karg
*args
)
5127 if (args
->i
== XT_GO_UP_ROOT
)
5128 levels
= XT_GO_UP_ROOT
;
5129 else if ((levels
= atoi(args
->s
)) == 0)
5132 uri
= g_strdup(get_uri(t
));
5136 if ((tmp
= strstr(uri
, XT_PROTO_DELIM
)) == NULL
)
5139 tmp
+= strlen(XT_PROTO_DELIM
);
5141 /* it makes no sense to strip the last slash from ".../dir/", skip it */
5142 lastidx
= strlen(tmp
) - 1;
5144 if (tmp
[lastidx
] == '/')
5145 tmp
[lastidx
] = '\0';
5149 p
= strrchr(tmp
, '/');
5150 if (p
== tmp
) { /* Are we at the root of a file://-path? */
5153 } else if (p
!= NULL
)
5166 gototab(struct tab
*t
, struct karg
*args
)
5169 struct karg arg
= {0, NULL
, -1};
5171 tab
= atoi(args
->s
);
5174 arg
.i
= XT_TAB_NEXT
;
5186 zoom_amount(struct tab
*t
, struct karg
*arg
)
5188 struct karg narg
= {0, NULL
, -1};
5190 narg
.i
= atoi(arg
->s
);
5191 resizetab(t
, &narg
);
5197 flip_colon(struct tab
*t
, struct karg
*arg
)
5199 struct karg narg
= {0, NULL
, -1};
5202 if (t
== NULL
|| arg
== NULL
)
5205 p
= strstr(arg
->s
, ":");
5217 /* buffer commands receive the regex that triggered them in arg.s */
5218 char bcmd
[XT_BUFCMD_SZ
];
5222 #define XT_PRE_NO (0)
5223 #define XT_PRE_YES (1)
5224 #define XT_PRE_MAYBE (2)
5226 int (*func
)(struct tab
*, struct karg
*);
5230 { "^[0-9]*gu$", XT_PRE_MAYBE
, "gu", go_up
, 0 },
5231 { "^gU$", XT_PRE_NO
, "gU", go_up
, XT_GO_UP_ROOT
},
5232 { "^gg$", XT_PRE_NO
, "gg", move
, XT_MOVE_TOP
},
5233 { "^gG$", XT_PRE_NO
, "gG", move
, XT_MOVE_BOTTOM
},
5234 { "^[0-9]+%$", XT_PRE_YES
, "%", move
, XT_MOVE_PERCENT
},
5235 { "^zz$", XT_PRE_NO
, "zz", move
, XT_MOVE_CENTER
},
5236 { "^gh$", XT_PRE_NO
, "gh", go_home
, 0 },
5237 { "^m[a-zA-Z0-9]$", XT_PRE_NO
, "m", mark
, XT_MARK_SET
},
5238 { "^['][a-zA-Z0-9']$", XT_PRE_NO
, "'", mark
, XT_MARK_GOTO
},
5239 { "^[0-9]+t$", XT_PRE_YES
, "t", gototab
, 0 },
5240 { "^g0$", XT_PRE_YES
, "g0", movetab
, XT_TAB_FIRST
},
5241 { "^g[$]$", XT_PRE_YES
, "g$", movetab
, XT_TAB_LAST
},
5242 { "^[0-9]*gt$", XT_PRE_YES
, "t", movetab
, XT_TAB_NEXT
},
5243 { "^[0-9]*gT$", XT_PRE_YES
, "T", movetab
, XT_TAB_PREV
},
5244 { "^M[a-zA-Z0-9]$", XT_PRE_NO
, "M", qmark
, XT_QMARK_SET
},
5245 { "^go[a-zA-Z0-9]$", XT_PRE_NO
, "go", qmark
, XT_QMARK_OPEN
},
5246 { "^gn[a-zA-Z0-9]$", XT_PRE_NO
, "gn", qmark
, XT_QMARK_TAB
},
5247 { "^ZR$", XT_PRE_NO
, "ZR", restart
, 0 },
5248 { "^ZZ$", XT_PRE_NO
, "ZZ", quit
, 0 },
5249 { "^zi$", XT_PRE_NO
, "zi", resizetab
, XT_ZOOM_IN
},
5250 { "^zo$", XT_PRE_NO
, "zo", resizetab
, XT_ZOOM_OUT
},
5251 { "^z0$", XT_PRE_NO
, "z0", resizetab
, XT_ZOOM_NORMAL
},
5252 { "^[0-9]+Z$", XT_PRE_YES
, "Z", zoom_amount
, 0 },
5253 { "^[0-9]+:$", XT_PRE_YES
, ":", flip_colon
, 0 },
5257 buffercmd_init(void)
5261 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5262 if (regcomp(&buffercmds
[i
].cregex
, buffercmds
[i
].regex
,
5263 REG_EXTENDED
| REG_NOSUB
))
5264 startpage_add("invalid buffercmd regex %s",
5265 buffercmds
[i
].regex
);
5269 buffercmd_abort(struct tab
*t
)
5276 DNPRINTF(XT_D_BUFFERCMD
, "%s: clearing buffer\n", __func__
);
5278 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5281 cmd_prefix
= 0; /* clear prefix for non-buffer commands */
5282 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
5286 buffercmd_execute(struct tab
*t
, struct buffercmd
*cmd
)
5288 struct karg arg
= {0, NULL
, -1};
5291 arg
.s
= g_strdup(bcmd
);
5293 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_execute: buffer \"%s\" "
5294 "matches regex \"%s\", executing\n", bcmd
, cmd
->regex
);
5304 buffercmd_addkey(struct tab
*t
, guint keyval
)
5307 char s
[XT_BUFCMD_SZ
];
5309 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
))) {
5311 return (XT_CB_PASSTHROUGH
);
5314 if (keyval
== GDK_Escape
) {
5316 return (XT_CB_HANDLED
);
5319 /* key with modifier or non-ascii character */
5320 if (!isascii(keyval
)) {
5322 * XXX this looks wrong but fixes some sites like
5323 * http://www.seslisozluk.com/
5324 * that eat a shift or ctrl and end putting default focus in js
5325 * instead of ignoring the keystroke
5326 * so instead of return (XT_CB_PASSTHROUGH); eat the key
5328 return (XT_CB_HANDLED
);
5331 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: adding key \"%c\" "
5332 "to buffer \"%s\"\n", keyval
, bcmd
);
5334 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5335 if (bcmd
[i
] == '\0') {
5340 /* buffer full, ignore input */
5341 if (i
>= LENGTH(bcmd
) -1) {
5342 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: buffer full\n");
5344 return (XT_CB_HANDLED
);
5347 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
5349 /* find exact match */
5350 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5351 if (regexec(&buffercmds
[i
].cregex
, bcmd
,
5352 (size_t) 0, NULL
, 0) == 0) {
5353 buffercmd_execute(t
, &buffercmds
[i
]);
5357 /* find non exact matches to see if we need to abort ot not */
5358 for (i
= 0, match
= 0; i
< LENGTH(buffercmds
); i
++) {
5359 DNPRINTF(XT_D_BUFFERCMD
, "trying: %s\n", bcmd
);
5362 if (buffercmds
[i
].precount
== XT_PRE_MAYBE
) {
5363 if (isdigit(bcmd
[0])) {
5364 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5368 if (sscanf(bcmd
, "%s", s
) == 0)
5371 } else if (buffercmds
[i
].precount
== XT_PRE_YES
) {
5372 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5375 if (sscanf(bcmd
, "%s", s
) == 0)
5378 if (c
== -1 && buffercmds
[i
].precount
)
5380 if (!strncmp(s
, buffercmds
[i
].cmd
, strlen(s
)))
5383 DNPRINTF(XT_D_BUFFERCMD
, "got[%d] %d <%s>: %d %s\n",
5384 i
, match
, buffercmds
[i
].cmd
, c
, s
);
5387 DNPRINTF(XT_D_BUFFERCMD
, "aborting: %s\n", bcmd
);
5392 return (XT_CB_HANDLED
);
5396 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
5398 struct key_binding
*k
;
5400 /* handle keybindings if buffercmd is empty.
5401 if not empty, allow commands like C-n */
5402 if (bcmd
[0] == '\0' || ((e
->state
& (CTRL
| MOD1
)) != 0))
5403 TAILQ_FOREACH(k
, &kbl
, entry
)
5404 if (e
->keyval
== k
->key
5405 && (entry
? k
->use_in_entry
: 1)) {
5406 /* when we are edditing eat ctrl/mod keys */
5407 if (edit_mode
== XT_EM_VI
&&
5408 t
->mode
== XT_MODE_INSERT
&&
5409 (e
->state
& CTRL
|| e
->state
& MOD1
))
5410 return (XT_CB_PASSTHROUGH
);
5413 if ((e
->state
& (CTRL
| MOD1
)) == 0)
5414 return (cmd_execute(t
, k
->cmd
));
5415 } else if ((e
->state
& k
->mask
) == k
->mask
) {
5416 return (cmd_execute(t
, k
->cmd
));
5420 if (!entry
&& ((e
->state
& (CTRL
| MOD1
)) == 0))
5421 return buffercmd_addkey(t
, e
->keyval
);
5423 return (XT_CB_PASSTHROUGH
);
5427 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5431 /* don't use w directly; use t->whatever instead */
5434 show_oops(NULL
, "wv_keypress_cb");
5435 return (XT_CB_PASSTHROUGH
);
5441 return t
->mode_cb(t
, e
, t
->mode_cb_data
);
5443 DNPRINTF(XT_D_KEY
, "wv_keypress_cb: mode %d keyval 0x%x mask "
5444 "0x%x tab %d\n", t
->mode
, e
->keyval
, e
->state
, t
->tab_id
);
5446 /* Hide buffers, if they are visible, with escape. */
5447 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)) &&
5448 CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
5449 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5451 return (XT_CB_HANDLED
);
5454 if (t
->mode
== XT_MODE_HINT
)
5455 return (XT_CB_HANDLED
);
5457 if ((CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Tab
) ||
5458 (CLEAN(e
->state
) == SHFT
&& e
->keyval
== GDK_Tab
))
5459 /* something focussy is about to happen */
5460 return (XT_CB_PASSTHROUGH
);
5462 /* check if we are some sort of text input thing in the dom */
5463 input_check_mode(t
);
5465 if (t
->mode
== XT_MODE_HINT
) {
5466 /* XXX make sure cmd entry is enabled */
5467 return (XT_CB_HANDLED
);
5468 } else if (t
->mode
== XT_MODE_PASSTHROUGH
) {
5469 if (CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
)
5470 t
->mode
= XT_MODE_COMMAND
;
5471 return (XT_CB_PASSTHROUGH
);
5472 } else if (t
->mode
== XT_MODE_COMMAND
) {
5474 snprintf(s
, sizeof s
, "%c", e
->keyval
);
5475 if (CLEAN(e
->state
) == 0 && isdigit(s
[0]))
5476 cmd_prefix
= 10 * cmd_prefix
+ atoi(s
);
5477 return (handle_keypress(t
, e
, 0));
5480 return (handle_keypress(t
, e
, 1));
5484 return (XT_CB_PASSTHROUGH
);
5488 hint_continue(struct tab
*t
)
5490 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5492 const gchar
*errstr
= NULL
;
5496 if (!(c
[0] == '.' || c
[0] == ','))
5498 if (strlen(c
) == 1) {
5499 /* XXX should not happen */
5504 if (isdigit(c
[1])) {
5506 i
= strtonum(&c
[1], 1, 4096, &errstr
);
5508 show_oops(t
, "invalid numerical hint %s", &c
[1]);
5511 s
= g_strdup_printf("hints.updateHints(%d);", i
);
5515 /* alphanumeric input */
5516 s
= g_strdup_printf("hints.createHints('%s', '%c');",
5517 &c
[1], c
[0] == '.' ? 'f' : 'F');
5528 search_continue(struct tab
*t
)
5530 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5531 gboolean rv
= FALSE
;
5533 if (c
[0] == ':' || c
[0] == '.' || c
[0] == ',')
5535 if (strlen(c
) == 1) {
5536 webkit_web_view_unmark_text_matches(t
->wv
);
5541 t
->search_forward
= TRUE
;
5542 else if (c
[0] == '?')
5543 t
->search_forward
= FALSE
;
5553 search_cb(struct tab
*t
)
5555 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5558 if (search_continue(t
) == FALSE
)
5562 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, t
->search_forward
,
5564 /* not found, mark red */
5565 gdk_color_parse(XT_COLOR_RED
, &color
);
5566 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
5567 /* unmark and remove selection */
5568 webkit_web_view_unmark_text_matches(t
->wv
);
5569 /* my kingdom for a way to unselect text in webview */
5571 /* found, highlight all */
5572 webkit_web_view_unmark_text_matches(t
->wv
);
5573 webkit_web_view_mark_text_matches(t
->wv
, &c
[1], FALSE
, 0);
5574 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
5575 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
5576 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
5584 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5586 const gchar
*c
= gtk_entry_get_text(w
);
5589 show_oops(NULL
, "cmd_keyrelease_cb invalid parameters");
5590 return (XT_CB_PASSTHROUGH
);
5593 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
5594 e
->keyval
, e
->state
, t
->tab_id
);
5597 if (!(e
->keyval
== GDK_Tab
|| e
->keyval
== GDK_ISO_Left_Tab
)) {
5598 if (hint_continue(t
) == FALSE
)
5603 if (search_continue(t
) == FALSE
)
5606 /* if search length is > 4 then no longer play timeout games */
5607 if (strlen(c
) > 4) {
5609 g_source_remove(t
->search_id
);
5616 /* reestablish a new timer if the user types fast */
5618 g_source_remove(t
->search_id
);
5619 t
->search_id
= g_timeout_add(250, (GSourceFunc
)search_cb
, (gpointer
)t
);
5622 return (XT_CB_PASSTHROUGH
);
5626 match_uri(const gchar
*uri
, const gchar
*key
) {
5629 gboolean match
= FALSE
;
5633 if (!strncmp(key
, uri
, len
))
5636 voffset
= strstr(uri
, "/") + 2;
5637 if (!strncmp(key
, voffset
, len
))
5639 else if (g_str_has_prefix(voffset
, "www.")) {
5640 voffset
= voffset
+ strlen("www.");
5641 if (!strncmp(key
, voffset
, len
))
5650 match_session(const gchar
*name
, const gchar
*key
) {
5653 sub
= strcasestr(name
, key
);
5659 cmd_getlist(int id
, char *key
)
5666 if (cmds
[id
].type
& XT_URLARG
) {
5667 RB_FOREACH_REVERSE(h
, history_list
, &hl
)
5668 if (match_uri(h
->uri
, key
)) {
5669 cmd_status
.list
[c
] = (char *)h
->uri
;
5675 } else if (cmds
[id
].type
& XT_SESSARG
) {
5676 TAILQ_FOREACH(s
, &sessions
, entry
)
5677 if (match_session(s
->name
, key
)) {
5678 cmd_status
.list
[c
] = (char *)s
->name
;
5684 } else if (cmds
[id
].type
& XT_SETARG
) {
5685 for (i
= 0; i
< get_settings_size(); i
++)
5686 if (!strncmp(key
, get_setting_name(i
),
5688 cmd_status
.list
[c
++] =
5689 get_setting_name(i
);
5695 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
5697 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
5698 if (cmds
[i
].level
< dep
)
5700 if (cmds
[i
].level
== dep
&& !strncmp(key
, cmds
[i
].cmd
,
5701 strlen(key
)) && !isdigit(cmds
[i
].cmd
[0]))
5702 cmd_status
.list
[c
++] = cmds
[i
].cmd
;
5710 cmd_getnext(int dir
)
5712 cmd_status
.index
+= dir
;
5714 if (cmd_status
.index
< 0)
5715 cmd_status
.index
= cmd_status
.len
- 1;
5716 else if (cmd_status
.index
>= cmd_status
.len
)
5717 cmd_status
.index
= 0;
5719 return cmd_status
.list
[cmd_status
.index
];
5723 cmd_tokenize(char *s
, char *tokens
[])
5726 char *tok
, *last
= NULL
;
5727 size_t len
= strlen(s
);
5730 blank
= len
== 0 || (len
> 0 && s
[len
- 1] == ' ');
5731 for (tok
= strtok_r(s
, " ", &last
); tok
&& i
< 3;
5732 tok
= strtok_r(NULL
, " ", &last
), i
++)
5742 cmd_complete(struct tab
*t
, char *str
, int dir
)
5744 GtkEntry
*w
= GTK_ENTRY(t
->cmd
);
5745 int i
, j
, levels
, c
= 0, dep
= 0, parent
= -1;
5747 char *tok
, *match
, *s
= g_strdup(str
);
5749 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
5752 DNPRINTF(XT_D_CMD
, "%s: complete %s\n", __func__
, str
);
5755 for (i
= 0; isdigit(s
[i
]); i
++)
5758 for (; isspace(s
[i
]); i
++)
5763 levels
= cmd_tokenize(s
, tokens
);
5765 for (i
= 0; i
< levels
- 1; i
++) {
5768 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5769 if (cmds
[j
].level
< dep
)
5771 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
,
5775 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
5782 if (matchcount
== 1) {
5783 strlcat(res
, tok
, sizeof res
);
5784 strlcat(res
, " ", sizeof res
);
5794 if (cmd_status
.index
== -1)
5795 cmd_getlist(parent
, tokens
[i
]);
5797 if (cmd_status
.len
> 0) {
5798 match
= cmd_getnext(dir
);
5799 strlcat(res
, match
, sizeof res
);
5800 gtk_entry_set_text(w
, res
);
5801 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5808 cmd_execute(struct tab
*t
, char *str
)
5810 struct cmd
*cmd
= NULL
;
5811 char *tok
, *last
= NULL
, *s
= g_strdup(str
), *sc
;
5813 int j
, len
, c
= 0, dep
= 0, matchcount
= 0;
5814 int prefix
= -1, rv
= XT_CB_PASSTHROUGH
;
5815 struct karg arg
= {0, NULL
, -1};
5820 for (j
= 0; j
<3 && isdigit(s
[j
]); j
++)
5826 while (isspace(s
[0]))
5829 if (strlen(s
) > 0 && strlen(prefixstr
) > 0)
5830 prefix
= atoi(prefixstr
);
5834 for (tok
= strtok_r(s
, " ", &last
); tok
;
5835 tok
= strtok_r(NULL
, " ", &last
)) {
5837 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5838 if (cmds
[j
].level
< dep
)
5840 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1 :
5842 if (cmds
[j
].level
== dep
&&
5843 !strncmp(tok
, cmds
[j
].cmd
, len
)) {
5847 if (len
== strlen(cmds
[j
].cmd
)) {
5853 if (matchcount
== 1) {
5858 show_oops(t
, "Invalid command: %s", str
);
5864 show_oops(t
, "Empty command");
5870 arg
.precount
= prefix
;
5871 else if (cmd_prefix
> 0)
5872 arg
.precount
= cmd_prefix
;
5874 if (j
> 0 && !(cmd
->type
& XT_PREFIX
) && arg
.precount
> -1) {
5875 show_oops(t
, "No prefix allowed: %s", str
);
5879 arg
.s
= last
? g_strdup(last
) : g_strdup("");
5880 if (cmd
->type
& XT_INTARG
&& last
&& strlen(last
) > 0) {
5881 if (arg
.s
== NULL
) {
5882 show_oops(t
, "Invalid command");
5885 arg
.precount
= atoi(arg
.s
);
5886 if (arg
.precount
<= 0) {
5887 if (arg
.s
[0] == '0')
5888 show_oops(t
, "Zero count");
5890 show_oops(t
, "Trailing characters");
5895 DNPRINTF(XT_D_CMD
, "%s: prefix %d arg %s\n",
5896 __func__
, arg
.precount
, arg
.s
);
5912 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5915 show_oops(NULL
, "entry_key_cb invalid parameters");
5916 return (XT_CB_PASSTHROUGH
);
5919 DNPRINTF(XT_D_CMD
, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
5920 e
->keyval
, e
->state
, t
->tab_id
);
5924 if (e
->keyval
== GDK_Escape
) {
5925 /* don't use focus_webview(t) because we want to type :cmds */
5926 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5929 return (handle_keypress(t
, e
, 1));
5932 struct command_entry
*
5933 history_prev(struct command_list
*l
, struct command_entry
*at
)
5936 at
= TAILQ_LAST(l
, command_list
);
5938 at
= TAILQ_PREV(at
, command_list
, entry
);
5940 at
= TAILQ_LAST(l
, command_list
);
5946 struct command_entry
*
5947 history_next(struct command_list
*l
, struct command_entry
*at
)
5950 at
= TAILQ_FIRST(l
);
5952 at
= TAILQ_NEXT(at
, entry
);
5954 at
= TAILQ_FIRST(l
);
5961 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5963 int rv
= XT_CB_HANDLED
;
5964 const gchar
*c
= gtk_entry_get_text(w
);
5968 show_oops(NULL
, "cmd_keypress_cb parameters");
5969 return (XT_CB_PASSTHROUGH
);
5972 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
5973 e
->keyval
, e
->state
, t
->tab_id
);
5977 e
->keyval
= GDK_Escape
;
5978 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5979 c
[0] == '.' || c
[0] == ','))
5980 e
->keyval
= GDK_Escape
;
5982 if (e
->keyval
!= GDK_Tab
&& e
->keyval
!= GDK_Shift_L
&&
5983 e
->keyval
!= GDK_ISO_Left_Tab
)
5984 cmd_status
.index
= -1;
5986 switch (e
->keyval
) {
5989 cmd_complete(t
, (char *)&c
[1], 1);
5990 else if (c
[0] == '.' || c
[0] == ',')
5991 run_script(t
, "hints.focusNextHint();");
5993 case GDK_ISO_Left_Tab
:
5995 cmd_complete(t
, (char *)&c
[1], -1);
5996 else if (c
[0] == '.' || c
[0] == ',')
5997 run_script(t
, "hints.focusPreviousHint();");
6001 if ((search_at
= history_next(&shl
, search_at
))) {
6002 search_at
->line
[0] = c
[0];
6003 gtk_entry_set_text(w
, search_at
->line
);
6004 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6006 } else if (c
[0] == '/') {
6007 if ((search_at
= history_prev(&shl
, search_at
))) {
6008 search_at
->line
[0] = c
[0];
6009 gtk_entry_set_text(w
, search_at
->line
);
6010 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6012 } if (c
[0] == ':') {
6013 if ((history_at
= history_prev(&chl
, history_at
))) {
6014 history_at
->line
[0] = c
[0];
6015 gtk_entry_set_text(w
, history_at
->line
);
6016 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6022 if ((search_at
= history_next(&shl
, search_at
))) {
6023 search_at
->line
[0] = c
[0];
6024 gtk_entry_set_text(w
, search_at
->line
);
6025 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6027 } else if (c
[0] == '?') {
6028 if ((search_at
= history_prev(&shl
, search_at
))) {
6029 search_at
->line
[0] = c
[0];
6030 gtk_entry_set_text(w
, search_at
->line
);
6031 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6033 } if (c
[0] == ':') {
6034 if ((history_at
= history_next(&chl
, history_at
))) {
6035 history_at
->line
[0] = c
[0];
6036 gtk_entry_set_text(w
, history_at
->line
);
6037 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6042 if (!(!strcmp(c
, ":") || !strcmp(c
, "/") || !strcmp(c
, "?") ||
6043 !strcmp(c
, ".") || !strcmp(c
, ","))) {
6044 /* see if we are doing hinting and reset it */
6045 if (c
[0] == '.' || c
[0] == ',') {
6046 /* recreate hints */
6047 s
= g_strdup_printf("hints.createHints('', "
6048 "'%c');", c
[0] == '.' ? 'f' : 'F');
6061 if (c
!= NULL
&& (c
[0] == '/' || c
[0] == '?'))
6062 webkit_web_view_unmark_text_matches(t
->wv
);
6064 /* no need to cancel hints */
6068 rv
= XT_CB_PASSTHROUGH
;
6074 wv_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
6076 DNPRINTF(XT_D_CMD
, "wv_popup_cb: tab %d\n", t
->tab_id
);
6080 cmd_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
6082 /* popup menu enabled */
6087 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
6090 show_oops(NULL
, "cmd_focusout_cb invalid parameters");
6091 return (XT_CB_PASSTHROUGH
);
6094 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d popup %d\n",
6095 t
->tab_id
, t
->popup
);
6097 /* if popup is enabled don't lose focus */
6100 return (XT_CB_PASSTHROUGH
);
6107 if (show_url
== 0 || t
->focus_wv
)
6110 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6112 return (XT_CB_PASSTHROUGH
);
6116 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
6119 const gchar
*c
= gtk_entry_get_text(entry
);
6122 show_oops(NULL
, "cmd_activate_cb invalid parameters");
6126 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
6131 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
6132 c
[0] == '.' || c
[0] == ','))
6138 if (c
[0] == '/' || c
[0] == '?') {
6139 /* see if there is a timer pending */
6141 g_source_remove(t
->search_id
);
6146 if (t
->search_text
) {
6147 g_free(t
->search_text
);
6148 t
->search_text
= NULL
;
6151 t
->search_text
= g_strdup(s
);
6153 g_free(global_search
);
6154 global_search
= g_strdup(s
);
6155 t
->search_forward
= c
[0] == '/';
6157 history_add(&shl
, search_file
, s
, &search_history_count
);
6158 } else if (c
[0] == '.' || c
[0] == ',') {
6159 run_script(t
, "hints.fire();");
6160 /* XXX history for link following? */
6161 } else if (c
[0] == ':') {
6162 history_add(&chl
, command_file
, s
, &cmd_history_count
);
6163 /* can't call hide_cmd after cmd_execute */
6174 backward_cb(GtkWidget
*w
, struct tab
*t
)
6179 show_oops(NULL
, "backward_cb invalid parameters");
6183 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
6190 forward_cb(GtkWidget
*w
, struct tab
*t
)
6195 show_oops(NULL
, "forward_cb invalid parameters");
6199 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
6201 a
.i
= XT_NAV_FORWARD
;
6206 home_cb(GtkWidget
*w
, struct tab
*t
)
6209 show_oops(NULL
, "home_cb invalid parameters");
6213 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
6219 stop_cb(GtkWidget
*w
, struct tab
*t
)
6221 WebKitWebFrame
*frame
;
6224 show_oops(NULL
, "stop_cb invalid parameters");
6228 DNPRINTF(XT_D_NAV
, "stop_cb: tab %d\n", t
->tab_id
);
6230 frame
= webkit_web_view_get_main_frame(t
->wv
);
6231 if (frame
== NULL
) {
6232 show_oops(t
, "stop_cb: no frame");
6236 webkit_web_frame_stop_loading(frame
);
6237 abort_favicon_download(t
);
6241 setup_webkit(struct tab
*t
)
6243 if (is_g_object_setting(G_OBJECT(t
->settings
), "enable-dns-prefetching"))
6244 g_object_set(G_OBJECT(t
->settings
), "enable-dns-prefetching",
6245 FALSE
, (char *)NULL
);
6247 warnx("webkit does not have \"enable-dns-prefetching\" property");
6248 g_object_set(G_OBJECT(t
->settings
),
6249 "user-agent", t
->user_agent
, (char *)NULL
);
6250 g_object_set(G_OBJECT(t
->settings
),
6251 "enable-scripts", enable_scripts
, (char *)NULL
);
6252 g_object_set(G_OBJECT(t
->settings
),
6253 "enable-plugins", enable_plugins
, (char *)NULL
);
6254 g_object_set(G_OBJECT(t
->settings
),
6255 "javascript-can-open-windows-automatically", enable_scripts
,
6257 g_object_set(G_OBJECT(t
->settings
),
6258 "enable-html5-database", FALSE
, (char *)NULL
);
6259 g_object_set(G_OBJECT(t
->settings
),
6260 "enable-html5-local-storage", enable_localstorage
, (char *)NULL
);
6261 g_object_set(G_OBJECT(t
->settings
),
6262 "enable_spell_checking", enable_spell_checking
, (char *)NULL
);
6263 g_object_set(G_OBJECT(t
->settings
),
6264 "spell_checking_languages", spell_check_languages
, (char *)NULL
);
6265 g_object_set(G_OBJECT(t
->settings
),
6266 "enable-developer-extras", TRUE
, (char *)NULL
);
6267 g_object_set(G_OBJECT(t
->wv
),
6268 "full-content-zoom", TRUE
, (char *)NULL
);
6269 g_object_set(G_OBJECT(t
->settings
),
6270 "auto-load-images", auto_load_images
, (char *)NULL
);
6272 webkit_web_view_set_settings(t
->wv
, t
->settings
);
6276 update_statusbar_position(GtkAdjustment
* adjustment
, gpointer data
)
6278 struct tab
*ti
, *t
= NULL
;
6279 gdouble view_size
, value
, max
;
6282 TAILQ_FOREACH(ti
, &tabs
, entry
)
6283 if (ti
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
6291 if (adjustment
== NULL
)
6292 adjustment
= gtk_scrolled_window_get_vadjustment(
6293 GTK_SCROLLED_WINDOW(t
->browser_win
));
6295 view_size
= gtk_adjustment_get_page_size(adjustment
);
6296 value
= gtk_adjustment_get_value(adjustment
);
6297 max
= gtk_adjustment_get_upper(adjustment
) - view_size
;
6300 position
= g_strdup("All");
6301 else if (value
== max
)
6302 position
= g_strdup("Bot");
6303 else if (value
== 0)
6304 position
= g_strdup("Top");
6306 position
= g_strdup_printf("%d%%", (int) ((value
/ max
) * 100));
6308 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.position
), position
);
6315 create_window(const gchar
*name
)
6319 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
6320 if (window_maximize
)
6321 gtk_window_maximize(GTK_WINDOW(w
));
6323 gtk_window_set_default_size(GTK_WINDOW(w
), window_width
, window_height
);
6324 gtk_widget_set_name(w
, name
);
6325 gtk_window_set_wmclass(GTK_WINDOW(w
), name
, "XXXTerm");
6331 create_browser(struct tab
*t
)
6335 GtkAdjustment
*adjustment
;
6338 show_oops(NULL
, "create_browser invalid parameters");
6342 t
->sb_h
= GTK_SCROLLBAR(gtk_hscrollbar_new(NULL
));
6343 t
->sb_v
= GTK_SCROLLBAR(gtk_vscrollbar_new(NULL
));
6344 t
->adjust_h
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_h
));
6345 t
->adjust_v
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_v
));
6347 w
= gtk_scrolled_window_new(t
->adjust_h
, t
->adjust_v
);
6348 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
6349 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
6351 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
6352 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
6355 t
->settings
= webkit_web_settings_new();
6357 g_object_set(t
->settings
, "default-encoding", encoding
, (char *)NULL
);
6359 if (user_agent
== NULL
) {
6360 g_object_get(G_OBJECT(t
->settings
), "user-agent", &strval
,
6362 t
->user_agent
= g_strdup_printf("%s %s+", strval
, version
);
6365 t
->user_agent
= g_strdup(user_agent
->value
);
6367 t
->stylesheet
= g_strdup_printf("file://%s/style.css", resource_dir
);
6368 t
->load_images
= auto_load_images
;
6371 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w
));
6372 g_signal_connect(G_OBJECT(adjustment
), "value-changed",
6373 G_CALLBACK(update_statusbar_position
), NULL
);
6382 create_kiosk_toolbar(struct tab
*t
)
6384 GtkWidget
*toolbar
= NULL
, *b
;
6386 b
= gtk_hbox_new(FALSE
, 0);
6388 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
6390 /* backward button */
6391 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
6392 gtk_widget_set_sensitive(t
->backward
, FALSE
);
6393 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
6394 G_CALLBACK(backward_cb
), t
);
6395 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, TRUE
, TRUE
, 0);
6397 /* forward button */
6398 t
->forward
= create_button("Forward", GTK_STOCK_GO_FORWARD
, 0);
6399 gtk_widget_set_sensitive(t
->forward
, FALSE
);
6400 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
6401 G_CALLBACK(forward_cb
), t
);
6402 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, TRUE
, TRUE
, 0);
6405 t
->gohome
= create_button("Home", GTK_STOCK_HOME
, 0);
6406 gtk_widget_set_sensitive(t
->gohome
, true);
6407 g_signal_connect(G_OBJECT(t
->gohome
), "clicked",
6408 G_CALLBACK(home_cb
), t
);
6409 gtk_box_pack_start(GTK_BOX(b
), t
->gohome
, TRUE
, TRUE
, 0);
6411 /* create widgets but don't use them */
6412 t
->uri_entry
= gtk_entry_new();
6413 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
6414 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
6415 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
6416 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
6422 create_toolbar(struct tab
*t
)
6424 GtkWidget
*toolbar
= NULL
, *b
, *eb1
;
6426 b
= gtk_hbox_new(FALSE
, 0);
6428 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
6430 /* backward button */
6431 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
6432 gtk_widget_set_sensitive(t
->backward
, FALSE
);
6433 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
6434 G_CALLBACK(backward_cb
), t
);
6435 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, FALSE
, FALSE
, 0);
6437 /* forward button */
6438 t
->forward
= create_button("Forward",GTK_STOCK_GO_FORWARD
, 0);
6439 gtk_widget_set_sensitive(t
->forward
, FALSE
);
6440 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
6441 G_CALLBACK(forward_cb
), t
);
6442 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, FALSE
,
6446 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
6447 gtk_widget_set_sensitive(t
->stop
, FALSE
);
6448 g_signal_connect(G_OBJECT(t
->stop
), "clicked",
6449 G_CALLBACK(stop_cb
), t
);
6450 gtk_box_pack_start(GTK_BOX(b
), t
->stop
, FALSE
,
6454 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
6455 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
6456 gtk_widget_set_sensitive(t
->js_toggle
, TRUE
);
6457 g_signal_connect(G_OBJECT(t
->js_toggle
), "clicked",
6458 G_CALLBACK(js_toggle_cb
), t
);
6459 gtk_box_pack_start(GTK_BOX(b
), t
->js_toggle
, FALSE
, FALSE
, 0);
6461 t
->uri_entry
= gtk_entry_new();
6462 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
6463 G_CALLBACK(activate_uri_entry_cb
), t
);
6464 g_signal_connect(G_OBJECT(t
->uri_entry
), "key-press-event",
6465 G_CALLBACK(entry_key_cb
), t
);
6467 eb1
= gtk_hbox_new(FALSE
, 0);
6468 gtk_container_set_border_width(GTK_CONTAINER(eb1
), 1);
6469 gtk_box_pack_start(GTK_BOX(eb1
), t
->uri_entry
, TRUE
, TRUE
, 0);
6470 gtk_box_pack_start(GTK_BOX(b
), eb1
, TRUE
, TRUE
, 0);
6473 if (search_string
!= NULL
&& strlen(search_string
) != 0) {
6475 t
->search_entry
= gtk_entry_new();
6476 gtk_entry_set_width_chars(GTK_ENTRY(t
->search_entry
), 30);
6477 g_signal_connect(G_OBJECT(t
->search_entry
), "activate",
6478 G_CALLBACK(activate_search_entry_cb
), t
);
6479 g_signal_connect(G_OBJECT(t
->search_entry
), "key-press-event",
6480 G_CALLBACK(entry_key_cb
), t
);
6481 gtk_widget_set_size_request(t
->search_entry
, -1, -1);
6482 eb2
= gtk_hbox_new(FALSE
, 0);
6483 gtk_container_set_border_width(GTK_CONTAINER(eb2
), 1);
6484 gtk_box_pack_start(GTK_BOX(eb2
), t
->search_entry
, TRUE
, TRUE
,
6486 gtk_box_pack_start(GTK_BOX(b
), eb2
, FALSE
, FALSE
, 0);
6488 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
6494 create_buffers(struct tab
*t
)
6496 GtkCellRenderer
*renderer
;
6499 view
= gtk_tree_view_new();
6501 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view
), FALSE
);
6503 renderer
= gtk_cell_renderer_text_new();
6504 gtk_tree_view_insert_column_with_attributes
6505 (GTK_TREE_VIEW(view
), -1, "Id", renderer
, "text", COL_ID
, (char *)NULL
);
6507 renderer
= gtk_cell_renderer_pixbuf_new();
6508 gtk_tree_view_insert_column_with_attributes
6509 (GTK_TREE_VIEW(view
), -1, "Favicon", renderer
, "pixbuf", COL_FAVICON
,
6512 renderer
= gtk_cell_renderer_text_new();
6513 gtk_tree_view_insert_column_with_attributes
6514 (GTK_TREE_VIEW(view
), -1, "Title", renderer
, "text", COL_TITLE
,
6517 gtk_tree_view_set_model
6518 (GTK_TREE_VIEW(view
), GTK_TREE_MODEL(buffers_store
));
6524 row_activated_cb(GtkTreeView
*view
, GtkTreePath
*path
,
6525 GtkTreeViewColumn
*col
, struct tab
*t
)
6530 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6532 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
,
6535 (GTK_TREE_MODEL(buffers_store
), &iter
, COL_ID
, &id
, -1);
6536 set_current_tab(id
- 1);
6542 /* after tab reordering/creation/removal */
6549 TAILQ_FOREACH(t
, &tabs
, entry
) {
6550 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
6551 if (t
->tab_id
> maxid
)
6554 gtk_widget_show(t
->tab_elems
.sep
);
6557 TAILQ_FOREACH(t
, &tabs
, entry
) {
6558 if (t
->tab_id
== maxid
) {
6559 gtk_widget_hide(t
->tab_elems
.sep
);
6565 /* after active tab change */
6567 recolor_compact_tabs(void)
6573 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6574 TAILQ_FOREACH(t
, &tabs
, entry
)
6575 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
,
6578 curid
= gtk_notebook_get_current_page(notebook
);
6579 TAILQ_FOREACH(t
, &tabs
, entry
)
6580 if (t
->tab_id
== curid
) {
6581 gdk_color_parse(XT_COLOR_CT_ACTIVE
, &color
);
6582 gtk_widget_modify_fg(t
->tab_elems
.label
,
6583 GTK_STATE_NORMAL
, &color
);
6589 set_current_tab(int page_num
)
6591 buffercmd_abort(get_current_tab());
6592 gtk_notebook_set_current_page(notebook
, page_num
);
6593 recolor_compact_tabs();
6597 undo_close_tab_save(struct tab
*t
)
6601 struct undo
*u1
, *u2
;
6603 WebKitWebHistoryItem
*item
;
6605 if ((uri
= get_uri(t
)) == NULL
)
6608 u1
= g_malloc0(sizeof(struct undo
));
6609 u1
->uri
= g_strdup(uri
);
6611 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
6613 m
= webkit_web_back_forward_list_get_forward_length(t
->bfl
);
6614 n
= webkit_web_back_forward_list_get_back_length(t
->bfl
);
6617 /* forward history */
6618 items
= webkit_web_back_forward_list_get_forward_list_with_limit(t
->bfl
, m
);
6622 u1
->history
= g_list_prepend(u1
->history
,
6623 webkit_web_history_item_copy(item
));
6624 items
= g_list_next(items
);
6629 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
6630 u1
->history
= g_list_prepend(u1
->history
,
6631 webkit_web_history_item_copy(item
));
6635 items
= webkit_web_back_forward_list_get_back_list_with_limit(t
->bfl
, n
);
6639 u1
->history
= g_list_prepend(u1
->history
,
6640 webkit_web_history_item_copy(item
));
6641 items
= g_list_next(items
);
6644 TAILQ_INSERT_HEAD(&undos
, u1
, entry
);
6646 if (undo_count
> XT_MAX_UNDO_CLOSE_TAB
) {
6647 u2
= TAILQ_LAST(&undos
, undo_tailq
);
6648 TAILQ_REMOVE(&undos
, u2
, entry
);
6650 g_list_free(u2
->history
);
6659 delete_tab(struct tab
*t
)
6663 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
6669 * no need to join thread here because it won't access t on completion
6672 TAILQ_REMOVE(&tabs
, t
, entry
);
6675 /* Halt all webkit activity. */
6676 abort_favicon_download(t
);
6677 webkit_web_view_stop_loading(t
->wv
);
6679 /* Save the tab, so we can undo the close. */
6680 undo_close_tab_save(t
);
6684 g_source_remove(t
->search_id
);
6687 bzero(&a
, sizeof a
);
6689 inspector_cmd(t
, &a
);
6691 if (browser_mode
== XT_BM_KIOSK
) {
6692 gtk_widget_destroy(t
->uri_entry
);
6693 gtk_widget_destroy(t
->stop
);
6694 gtk_widget_destroy(t
->js_toggle
);
6697 gtk_widget_destroy(t
->tab_elems
.eventbox
);
6698 gtk_widget_destroy(t
->vbox
);
6700 g_free(t
->user_agent
);
6701 g_free(t
->stylesheet
);
6706 if (TAILQ_EMPTY(&tabs
)) {
6707 if (browser_mode
== XT_BM_KIOSK
)
6708 create_new_tab(home
, NULL
, 1, -1);
6710 create_new_tab(NULL
, NULL
, 1, -1);
6713 /* recreate session */
6714 if (session_autosave
) {
6715 bzero(&a
, sizeof a
);
6717 save_tabs(NULL
, &a
);
6721 recolor_compact_tabs();
6725 update_statusbar_zoom(struct tab
*t
)
6728 char s
[16] = { '\0' };
6730 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6731 if ((zoom
<= 0.99 || zoom
>= 1.01))
6732 snprintf(s
, sizeof s
, "%d%%", (int)(zoom
* 100));
6733 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.zoom
), s
);
6737 setzoom_webkit(struct tab
*t
, int adjust
)
6739 #define XT_ZOOMPERCENT 0.04
6744 show_oops(NULL
, "setzoom_webkit invalid parameters");
6748 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6749 if (adjust
== XT_ZOOM_IN
)
6750 zoom
+= XT_ZOOMPERCENT
;
6751 else if (adjust
== XT_ZOOM_OUT
)
6752 zoom
-= XT_ZOOMPERCENT
;
6753 else if (adjust
> 0)
6754 zoom
= default_zoom_level
+ adjust
/ 100.0 - 1.0;
6756 show_oops(t
, "setzoom_webkit invalid zoom value");
6760 if (zoom
< XT_ZOOMPERCENT
)
6761 zoom
= XT_ZOOMPERCENT
;
6762 g_object_set(G_OBJECT(t
->wv
), "zoom-level", zoom
, (char *)NULL
);
6763 update_statusbar_zoom(t
);
6767 tab_clicked_cb(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
6769 struct tab
*t
= (struct tab
*) data
;
6771 DNPRINTF(XT_D_TAB
, "tab_clicked_cb: tab: %d\n", t
->tab_id
);
6773 switch (event
->button
) {
6775 set_current_tab(t
->tab_id
);
6786 append_tab(struct tab
*t
)
6791 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
6792 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
6796 create_sbe(int width
)
6800 sbe
= gtk_entry_new();
6801 gtk_entry_set_inner_border(GTK_ENTRY(sbe
), NULL
);
6802 gtk_entry_set_has_frame(GTK_ENTRY(sbe
), FALSE
);
6803 gtk_widget_set_can_focus(GTK_WIDGET(sbe
), FALSE
);
6804 gtk_widget_modify_font(GTK_WIDGET(sbe
), statusbar_font
);
6805 gtk_entry_set_alignment(GTK_ENTRY(sbe
), 1.0);
6806 gtk_widget_set_size_request(sbe
, width
, -1);
6812 create_new_tab(char *title
, struct undo
*u
, int focus
, int position
)
6817 WebKitWebHistoryItem
*item
;
6821 int sbe_p
= 0, sbe_b
= 0,
6824 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
6826 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
6827 DNPRINTF(XT_D_TAB
, "create_new_tab: new tab rejected\n");
6831 t
= g_malloc0(sizeof *t
);
6833 if (title
== NULL
) {
6834 title
= "(untitled)";
6838 t
->vbox
= gtk_vbox_new(FALSE
, 0);
6840 /* label + button for tab */
6841 b
= gtk_hbox_new(FALSE
, 0);
6844 #if GTK_CHECK_VERSION(2, 20, 0)
6845 t
->spinner
= gtk_spinner_new();
6847 t
->label
= gtk_label_new(title
);
6848 bb
= create_button("Close", GTK_STOCK_CLOSE
, 1);
6849 gtk_widget_set_size_request(t
->label
, 100, 0);
6850 gtk_label_set_max_width_chars(GTK_LABEL(t
->label
), 20);
6851 gtk_label_set_ellipsize(GTK_LABEL(t
->label
), PANGO_ELLIPSIZE_END
);
6852 gtk_widget_set_size_request(b
, 130, 0);
6854 gtk_box_pack_start(GTK_BOX(b
), bb
, FALSE
, FALSE
, 0);
6855 gtk_box_pack_start(GTK_BOX(b
), t
->label
, FALSE
, FALSE
, 0);
6856 #if GTK_CHECK_VERSION(2, 20, 0)
6857 gtk_box_pack_start(GTK_BOX(b
), t
->spinner
, FALSE
, FALSE
, 0);
6861 if (browser_mode
== XT_BM_KIOSK
) {
6862 t
->toolbar
= create_kiosk_toolbar(t
);
6863 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
6866 t
->toolbar
= create_toolbar(t
);
6868 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
,
6876 t
->browser_win
= create_browser(t
);
6877 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
6879 /* oops message for user feedback */
6880 t
->oops
= gtk_entry_new();
6881 gtk_entry_set_inner_border(GTK_ENTRY(t
->oops
), NULL
);
6882 gtk_entry_set_has_frame(GTK_ENTRY(t
->oops
), FALSE
);
6883 gtk_widget_set_can_focus(GTK_WIDGET(t
->oops
), FALSE
);
6884 gdk_color_parse(XT_COLOR_RED
, &color
);
6885 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
6886 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
6887 gtk_widget_modify_font(GTK_WIDGET(t
->oops
), oops_font
);
6890 t
->cmd
= gtk_entry_new();
6891 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
6892 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
6893 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
6894 gtk_widget_modify_font(GTK_WIDGET(t
->cmd
), cmd_font
);
6897 t
->statusbar_box
= gtk_hbox_new(FALSE
, 0);
6899 t
->sbe
.statusbar
= gtk_entry_new();
6900 gtk_entry_set_inner_border(GTK_ENTRY(t
->sbe
.statusbar
), NULL
);
6901 gtk_entry_set_has_frame(GTK_ENTRY(t
->sbe
.statusbar
), FALSE
);
6902 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.statusbar
), FALSE
);
6903 gtk_widget_modify_font(GTK_WIDGET(t
->sbe
.statusbar
), statusbar_font
);
6905 /* create these widgets only if specified in statusbar_elems */
6907 t
->sbe
.position
= create_sbe(40);
6908 t
->sbe
.zoom
= create_sbe(40);
6909 t
->sbe
.buffercmd
= create_sbe(60);
6911 statusbar_modify_attr(t
, XT_COLOR_WHITE
, XT_COLOR_BLACK
);
6913 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), t
->sbe
.statusbar
, TRUE
,
6916 /* gtk widgets cannot be added to a box twice. sbe_* variables
6917 make sure of this */
6918 for (p
= statusbar_elems
; *p
!= '\0'; p
++) {
6922 GtkWidget
*sep
= gtk_vseparator_new();
6924 gdk_color_parse(XT_COLOR_SB_SEPARATOR
, &color
);
6925 gtk_widget_modify_bg(sep
, GTK_STATE_NORMAL
, &color
);
6926 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), sep
,
6927 FALSE
, FALSE
, FALSE
);
6932 warnx("flag \"%c\" specified more than "
6933 "once in statusbar_elems\n", *p
);
6937 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6938 t
->sbe
.position
, FALSE
, FALSE
, FALSE
);
6942 warnx("flag \"%c\" specified more than "
6943 "once in statusbar_elems\n", *p
);
6947 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6948 t
->sbe
.buffercmd
, FALSE
, FALSE
, FALSE
);
6952 warnx("flag \"%c\" specified more than "
6953 "once in statusbar_elems\n", *p
);
6957 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6958 t
->sbe
.zoom
, FALSE
, FALSE
, FALSE
);
6961 warnx("illegal flag \"%c\" in statusbar_elems\n", *p
);
6966 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar_box
, FALSE
, FALSE
, 0);
6969 t
->buffers
= create_buffers(t
);
6970 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->buffers
, FALSE
, FALSE
, 0);
6972 /* xtp meaning is normal by default */
6973 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
6975 /* set empty favicon */
6976 xt_icon_from_name(t
, "text-html");
6978 /* and show it all */
6979 gtk_widget_show_all(b
);
6980 gtk_widget_show_all(t
->vbox
);
6982 /* compact tab bar */
6983 t
->tab_elems
.label
= gtk_label_new(title
);
6984 t
->tab_elems
.favicon
= gtk_image_new();
6985 gtk_label_set_width_chars(GTK_LABEL(t
->tab_elems
.label
), 1.0);
6986 gtk_misc_set_alignment(GTK_MISC(t
->tab_elems
.label
), 0.0, 0.0);
6987 gtk_misc_set_padding(GTK_MISC(t
->tab_elems
.label
), 4.0, 4.0);
6988 gtk_widget_modify_font(GTK_WIDGET(t
->tab_elems
.label
), tabbar_font
);
6990 t
->tab_elems
.eventbox
= gtk_event_box_new();
6991 t
->tab_elems
.box
= gtk_hbox_new(FALSE
, 0);
6992 t
->tab_elems
.sep
= gtk_vseparator_new();
6994 gdk_color_parse(XT_COLOR_CT_BACKGROUND
, &color
);
6995 gtk_widget_modify_bg(t
->tab_elems
.eventbox
, GTK_STATE_NORMAL
, &color
);
6996 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6997 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
, &color
);
6998 gdk_color_parse(XT_COLOR_CT_SEPARATOR
, &color
);
6999 gtk_widget_modify_bg(t
->tab_elems
.sep
, GTK_STATE_NORMAL
, &color
);
7001 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.favicon
, FALSE
,
7003 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.label
, TRUE
,
7005 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.sep
, FALSE
,
7007 gtk_container_add(GTK_CONTAINER(t
->tab_elems
.eventbox
),
7010 gtk_box_pack_start(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
, TRUE
,
7012 gtk_widget_show_all(t
->tab_elems
.eventbox
);
7014 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
7017 id
= position
>= 0 ? position
:
7018 gtk_notebook_get_current_page(notebook
) + 1;
7019 if (id
> gtk_notebook_get_n_pages(notebook
))
7022 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
7023 gtk_notebook_insert_page(notebook
, t
->vbox
, b
, id
);
7024 gtk_box_reorder_child(GTK_BOX(tab_bar
),
7025 t
->tab_elems
.eventbox
, id
);
7030 #if GTK_CHECK_VERSION(2, 20, 0)
7031 /* turn spinner off if we are a new tab without uri */
7033 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
7034 gtk_widget_hide(t
->spinner
);
7037 /* make notebook tabs reorderable */
7038 gtk_notebook_set_tab_reorderable(notebook
, t
->vbox
, TRUE
);
7040 /* compact tabs clickable */
7041 g_signal_connect(G_OBJECT(t
->tab_elems
.eventbox
),
7042 "button_press_event", G_CALLBACK(tab_clicked_cb
), t
);
7044 g_object_connect(G_OBJECT(t
->cmd
),
7045 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb
), t
,
7046 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
7047 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb
), t
,
7048 "signal::activate", G_CALLBACK(cmd_activate_cb
), t
,
7049 "signal::populate-popup", G_CALLBACK(cmd_popup_cb
), t
,
7052 /* reuse wv_button_cb to hide oops */
7053 g_object_connect(G_OBJECT(t
->oops
),
7054 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7057 g_signal_connect(t
->buffers
,
7058 "row-activated", G_CALLBACK(row_activated_cb
), t
);
7059 g_object_connect(G_OBJECT(t
->buffers
),
7060 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
, (char *)NULL
);
7062 g_object_connect(G_OBJECT(t
->wv
),
7063 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
,
7064 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb
), t
,
7065 "signal::download-requested", G_CALLBACK(webview_download_cb
), t
,
7066 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), t
,
7067 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7068 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7069 "signal::resource-request-starting", G_CALLBACK(webview_rrs_cb
), t
,
7070 "signal::create-web-view", G_CALLBACK(webview_cwv_cb
), t
,
7071 "signal::close-web-view", G_CALLBACK(webview_closewv_cb
), t
,
7072 "signal::event", G_CALLBACK(webview_event_cb
), t
,
7073 "signal::load-finished", G_CALLBACK(webview_load_finished_cb
), t
,
7074 "signal::load-progress-changed", G_CALLBACK(webview_progress_changed_cb
), t
,
7075 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb
), t
,
7076 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7077 "signal::button_release_event", G_CALLBACK(wv_release_button_cb
), t
,
7078 "signal::populate-popup", G_CALLBACK(wv_popup_cb
), t
,
7080 g_signal_connect(t
->wv
,
7081 "notify::load-status", G_CALLBACK(notify_load_status_cb
), t
);
7082 g_signal_connect(t
->wv
,
7083 "notify::title", G_CALLBACK(notify_title_cb
), t
);
7085 /* hijack the unused keys as if we were the browser */
7086 //g_object_connect(G_OBJECT(t->toolbar),
7087 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
7090 g_signal_connect(G_OBJECT(bb
), "button_press_event",
7091 G_CALLBACK(tab_close_cb
), t
);
7094 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
7095 /* restore the tab's history */
7096 if (u
&& u
->history
) {
7100 webkit_web_back_forward_list_add_item(t
->bfl
, item
);
7101 items
= g_list_next(items
);
7104 item
= g_list_nth_data(u
->history
, u
->back
);
7106 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
7109 g_list_free(u
->history
);
7111 webkit_web_back_forward_list_clear(t
->bfl
);
7117 url_set_visibility();
7118 statusbar_set_visibility();
7121 set_current_tab(t
->tab_id
);
7122 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
7126 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
7130 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
7137 if (userstyle_global
)
7140 recolor_compact_tabs();
7141 setzoom_webkit(t
, XT_ZOOM_NORMAL
);
7146 notebook_switchpage_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7152 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
7154 if (gtk_notebook_get_current_page(notebook
) == -1)
7157 TAILQ_FOREACH(t
, &tabs
, entry
) {
7158 if (t
->tab_id
== pn
) {
7159 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
7162 uri
= get_title(t
, TRUE
);
7163 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
7169 /* can't use focus_webview here */
7170 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
7177 notebook_pagereordered_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7180 struct tab
*t
= NULL
, *tt
;
7184 TAILQ_FOREACH(tt
, &tabs
, entry
)
7185 if (tt
->tab_id
== pn
) {
7191 DNPRINTF(XT_D_TAB
, "page_reordered_cb: tab: %d\n", t
->tab_id
);
7193 gtk_box_reorder_child(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
,
7198 menuitem_response(struct tab
*t
)
7200 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
7204 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
7206 GtkWidget
*menu
, *menu_items
;
7207 GdkEventButton
*bevent
;
7211 if (event
->type
== GDK_BUTTON_PRESS
) {
7212 bevent
= (GdkEventButton
*) event
;
7213 menu
= gtk_menu_new();
7215 TAILQ_FOREACH(ti
, &tabs
, entry
) {
7216 if ((uri
= get_uri(ti
)) == NULL
)
7217 /* XXX make sure there is something to print */
7218 /* XXX add gui pages in here to look purdy */
7220 menu_items
= gtk_menu_item_new_with_label(uri
);
7221 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), menu_items
);
7222 gtk_widget_show(menu_items
);
7224 g_signal_connect_swapped((menu_items
),
7225 "activate", G_CALLBACK(menuitem_response
),
7229 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
,
7230 bevent
->button
, bevent
->time
);
7232 /* unref object so it'll free itself when popped down */
7233 #if !GTK_CHECK_VERSION(3, 0, 0)
7234 /* XXX does not need unref with gtk+3? */
7235 g_object_ref_sink(menu
);
7236 g_object_unref(menu
);
7239 return (TRUE
/* eat event */);
7242 return (FALSE
/* propagate */);
7246 icon_size_map(int iconsz
)
7248 if (iconsz
<= GTK_ICON_SIZE_INVALID
||
7249 iconsz
> GTK_ICON_SIZE_DIALOG
)
7250 return (GTK_ICON_SIZE_SMALL_TOOLBAR
);
7256 create_button(char *name
, char *stockid
, int size
)
7258 GtkWidget
*button
, *image
;
7262 rcstring
= g_strdup_printf(
7263 "style \"%s-style\"\n"
7265 " GtkWidget::focus-padding = 0\n"
7266 " GtkWidget::focus-line-width = 0\n"
7270 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
7271 gtk_rc_parse_string(rcstring
);
7273 button
= gtk_button_new();
7274 gtk_button_set_focus_on_click(GTK_BUTTON(button
), FALSE
);
7275 gtk_icon_size
= icon_size_map(size
? size
: icon_size
);
7277 image
= gtk_image_new_from_stock(stockid
, gtk_icon_size
);
7278 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
7279 gtk_container_set_border_width(GTK_CONTAINER(button
), 1);
7280 gtk_container_add(GTK_CONTAINER(button
), GTK_WIDGET(image
));
7281 gtk_widget_set_name(button
, name
);
7282 gtk_button_set_relief(GTK_BUTTON(button
), GTK_RELIEF_NONE
);
7288 button_set_stockid(GtkWidget
*button
, char *stockid
)
7292 image
= gtk_image_new_from_stock(stockid
, icon_size_map(icon_size
));
7293 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
7294 gtk_button_set_image(GTK_BUTTON(button
), image
);
7298 clipb_primary_cb(GtkClipboard
*primary
, GdkEvent
*event
, gpointer notused
)
7301 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
7304 if (xterm_workaround
== 0)
7308 * xterm doesn't play nice with clipboards because it clears the
7309 * primary when clicked. We rely on primary being set to properly
7310 * handle middle mouse button clicks (paste). So when someone clears
7311 * primary copy whatever is in CUT_BUFFER0 into primary to simualte
7312 * other application behavior (as in DON'T clear primary).
7315 p
= gtk_clipboard_wait_for_text(primary
);
7317 if (gdk_property_get(gdk_get_default_root_window(),
7319 gdk_atom_intern("STRING", FALSE
),
7321 1024 * 1024 /* picked out of my butt */,
7329 /* yes sir, we need to NUL the string */
7331 gtk_clipboard_set_text(primary
, p
, -1);
7345 char file
[PATH_MAX
];
7348 vbox
= gtk_vbox_new(FALSE
, 0);
7349 gtk_box_set_spacing(GTK_BOX(vbox
), 0);
7350 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
7351 #if !GTK_CHECK_VERSION(3, 0, 0)
7352 /* XXX seems to be needed with gtk+2 */
7353 gtk_notebook_set_tab_hborder(notebook
, 0);
7354 gtk_notebook_set_tab_vborder(notebook
, 0);
7356 gtk_notebook_set_scrollable(notebook
, TRUE
);
7357 gtk_notebook_set_show_border(notebook
, FALSE
);
7358 gtk_widget_set_can_focus(GTK_WIDGET(notebook
), FALSE
);
7360 abtn
= gtk_button_new();
7361 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
7362 gtk_widget_set_size_request(arrow
, -1, -1);
7363 gtk_container_add(GTK_CONTAINER(abtn
), arrow
);
7364 gtk_widget_set_size_request(abtn
, -1, 20);
7366 #if GTK_CHECK_VERSION(2, 20, 0)
7367 gtk_notebook_set_action_widget(notebook
, abtn
, GTK_PACK_END
);
7369 gtk_widget_set_size_request(GTK_WIDGET(notebook
), -1, -1);
7371 /* compact tab bar */
7372 tab_bar
= gtk_hbox_new(TRUE
, 0);
7374 gtk_box_pack_start(GTK_BOX(vbox
), tab_bar
, FALSE
, FALSE
, 0);
7375 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
7376 gtk_widget_set_size_request(vbox
, -1, -1);
7378 g_object_connect(G_OBJECT(notebook
),
7379 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb
), NULL
,
7381 g_object_connect(G_OBJECT(notebook
),
7382 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb
),
7383 NULL
, (char *)NULL
);
7384 g_signal_connect(G_OBJECT(abtn
), "button_press_event",
7385 G_CALLBACK(arrow_cb
), NULL
);
7387 main_window
= create_window("xxxterm");
7388 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
7389 g_signal_connect(G_OBJECT(main_window
), "delete_event",
7390 G_CALLBACK(gtk_main_quit
), NULL
);
7393 for (i
= 0; i
< LENGTH(icons
); i
++) {
7394 snprintf(file
, sizeof file
, "%s" PS
"%s", resource_dir
, icons
[i
]);
7395 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
7396 l
= g_list_append(l
, pb
);
7398 gtk_window_set_default_icon_list(l
);
7400 /* clipboard work around */
7401 if (xterm_workaround
)
7403 G_OBJECT(gtk_clipboard_get(GDK_SELECTION_PRIMARY
)),
7404 "owner-change", G_CALLBACK(clipb_primary_cb
), NULL
);
7406 gtk_widget_show_all(abtn
);
7407 gtk_widget_show_all(main_window
);
7408 notebook_tab_set_visibility();
7411 #ifndef XT_SOCKET_DISABLE
7413 send_cmd_to_socket(char *cmd
)
7416 struct sockaddr_un sa
;
7418 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7419 warnx("%s: socket", __func__
);
7423 sa
.sun_family
= AF_UNIX
;
7424 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
7425 work_dir
, XT_SOCKET_FILE
);
7428 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7429 warnx("%s: connect", __func__
);
7433 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
7434 warnx("%s: send", __func__
);
7445 socket_watcher(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
7448 char str
[XT_MAX_URL_LENGTH
];
7449 socklen_t t
= sizeof(struct sockaddr_un
);
7450 struct sockaddr_un sa
;
7455 gint fd
= g_io_channel_unix_get_fd(source
);
7457 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
7462 if (getpeereid(s
, &uid
, &gid
) == -1) {
7466 if (uid
!= getuid() || gid
!= getgid()) {
7467 warnx("unauthorized user");
7473 warnx("not a valid user");
7477 n
= recv(s
, str
, sizeof(str
), 0);
7481 tt
= TAILQ_LAST(&tabs
, tab_list
);
7482 cmd_execute(tt
, str
);
7490 struct sockaddr_un sa
;
7492 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7493 warn("is_running: socket");
7497 sa
.sun_family
= AF_UNIX
;
7498 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
7499 work_dir
, XT_SOCKET_FILE
);
7502 /* connect to see if there is a listener */
7503 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
7504 rv
= 0; /* not running */
7506 rv
= 1; /* already running */
7517 struct sockaddr_un sa
;
7519 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7520 warn("build_socket: socket");
7524 sa
.sun_family
= AF_UNIX
;
7525 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
7526 work_dir
, XT_SOCKET_FILE
);
7529 /* connect to see if there is a listener */
7530 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7531 /* no listener so we will */
7532 unlink(sa
.sun_path
);
7534 if (bind(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7535 warn("build_socket: bind");
7539 if (listen(s
, 1) == -1) {
7540 warn("build_socket: listen");
7558 if (stat(dir
, &sb
)) {
7559 #if defined __MINGW32__
7560 printf("making: %s\n", dir
);
7561 if (mkdir(dir
) == -1)
7563 if (mkdir(dir
, S_IRWXU
) == -1)
7565 err(1, "mkdir %s", dir
);
7567 err(1, "stat %s", dir
);
7569 if (S_ISDIR(sb
.st_mode
) == 0)
7570 errx(1, "%s not a dir", dir
);
7571 #if !defined __MINGW32__
7572 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
7573 warnx("fixing invalid permissions on %s", dir
);
7574 if (chmod(dir
, S_IRWXU
) == -1)
7575 err(1, "chmod %s", dir
);
7584 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
7588 GStaticRecMutex my_gdk_mtx
= G_STATIC_REC_MUTEX_INIT
;
7589 volatile int mtx_depth
;
7593 * The linux flash plugin violates the gdk locking mechanism.
7594 * Work around the issue by using a recursive mutex with some match applied
7595 * to see if we hit a buggy condition.
7597 * The following code is painful so just don't read it. It really doesn't
7598 * make much sense but seems to work.
7603 g_static_rec_mutex_lock(&my_gdk_mtx
);
7606 if (mtx_depth
<= 0) {
7607 /* should not happen */
7608 show_oops(NULL
, "negative mutex locking bug, trying to "
7610 fprintf(stderr
, "negative mutex locking bug, trying to "
7612 g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
7613 g_static_rec_mutex_lock(&my_gdk_mtx
);
7618 if (mtx_depth
!= 1) {
7619 /* decrease mutext depth to 1 */
7621 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7623 } while (mtx_depth
> 1);
7632 /* if mutex depth isn't 1 then something went bad */
7633 if (mtx_depth
!= 1) {
7634 x
= g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
7636 /* should not happen */
7637 show_oops(NULL
, "mutex unlocking bug, trying to "
7639 fprintf(stderr
, "mutex unlocking bug, trying to "
7643 if (mtx_complain
== 0) {
7644 show_oops(NULL
, "buggy mutex implementation detected, "
7645 "work around implemented");
7646 fprintf(stderr
, "buggy mutex implementation detected, "
7647 "work around implemented");
7654 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7660 startpage_add("<b>Welcome to xxxterm %s!</b><p>", version
);
7661 startpage_add("Beware that this is the final version that will use the"
7662 " xxxterm name.<br>Moving forward the browser will be called "
7663 "xombrero.<p>", version
);
7664 startpage_add("Details will soon appear on the "
7665 "<a href=https://opensource.conformal.com/wiki/xombrero>xombrero "
7666 "wiki page</a><p>");
7667 startpage_add("Unfortunately scroogle has shut it's doors and due to "
7668 "that one has to edit search_string in ~/.xxxterm.conf.<br>"
7669 "There are various examples in the configuration file.<br>"
7670 "The authors of xxxterm are not in a position to suggest a search "
7675 main(int argc
, char **argv
)
7678 int c
, optn
= 0, opte
= 0, focus
= 1;
7679 char conf
[PATH_MAX
] = { '\0' };
7680 char file
[PATH_MAX
];
7681 char sodversion
[32];
7682 char *env_proxy
= NULL
;
7686 start_argv
= (char * const *)argv
;
7690 /* http://web.archiveorange.com/archive/v/UsPjxkX5PsaXBIoOjqxf */
7693 /* http://developer.gnome.org/gdk/stable/gdk-Threads.html */
7694 g_thread_init(NULL
);
7695 gdk_threads_set_lock_functions(mtx_lock
, mtx_unlock
);
7697 gdk_threads_enter();
7699 /* http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html */
7700 gcry_control (GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
7702 gtk_init(&argc
, &argv
);
7704 gnutls_global_init();
7706 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
7711 RB_INIT(&downloads
);
7714 TAILQ_INIT(&sessions
);
7717 TAILQ_INIT(&aliases
);
7723 TAILQ_INIT(&ua_list
);
7725 #ifndef XT_RESOURCE_LIMITS_DISABLE
7729 GIOChannel
*channel
;
7731 /* fiddle with ulimits */
7732 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7735 /* just use them all */
7736 rlp
.rlim_cur
= rlp
.rlim_max
;
7737 if (setrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7739 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7741 else if (rlp
.rlim_cur
< 1024)
7742 startpage_add("%s requires at least 1024 "
7743 "(2048 recommended) file " "descriptors, "
7744 "currently it has up to %d available",
7745 __progname
, rlp
.rlim_cur
);
7749 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
7758 #ifdef XXXTERM_BUILDSTR
7759 errx(0 , "Version: %s Build: %s",
7760 version
, XXXTERM_BUILDSTR
);
7762 errx(0 , "Version: %s", version
);
7766 strlcpy(conf
, optarg
, sizeof(conf
));
7769 strlcpy(named_session
, optarg
, sizeof(named_session
));
7790 xtp_generate_keys();
7792 /* XXX this hammer is way too big but treat all signaling the same */
7793 #ifndef XT_SIGNALS_DISABLE
7795 struct sigaction sact
;
7797 bzero(&sact
, sizeof(sact
));
7798 sigemptyset(&sact
.sa_mask
);
7799 sact
.sa_handler
= sigchild
;
7800 sact
.sa_flags
= SA_NOCLDSTOP
;
7801 sigaction(SIGCHLD
, &sact
, NULL
);
7804 /* set download dir */
7805 pwd
= getpwuid(getuid());
7807 errx(1, "invalid user %d", getuid());
7808 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
7810 /* compile buffer command regexes */
7813 /* set default string settings */
7814 home
= g_strdup("https://www.cyphertite.com");
7815 search_string
= g_strdup("https://ssl.scroogle.org/cgi-bin/nbbwssl.cgi?Gw=%s");
7816 resource_dir
= g_strdup("/usr/local/share/xxxterm/");
7817 strlcpy(runtime_settings
, "runtime", sizeof runtime_settings
);
7818 cmd_font_name
= g_strdup("monospace normal 9");
7819 oops_font_name
= g_strdup("monospace normal 9");
7820 statusbar_font_name
= g_strdup("monospace normal 9");
7821 tabbar_font_name
= g_strdup("monospace normal 9");
7822 statusbar_elems
= g_strdup("BP");
7823 spell_check_languages
= g_strdup("en_US");
7824 encoding
= g_strdup("UTF-8");
7826 /* read config file */
7827 if (strlen(conf
) == 0)
7828 snprintf(conf
, sizeof conf
, "%s" PS
".%s",
7829 pwd
->pw_dir
, XT_CONF_FILE
);
7830 config_parse(conf
, 0);
7833 cmd_font
= pango_font_description_from_string(cmd_font_name
);
7834 oops_font
= pango_font_description_from_string(oops_font_name
);
7835 statusbar_font
= pango_font_description_from_string(statusbar_font_name
);
7836 tabbar_font
= pango_font_description_from_string(tabbar_font_name
);
7838 /* working directory */
7839 if (strlen(work_dir
) == 0)
7840 snprintf(work_dir
, sizeof work_dir
, "%s" PS
"%s",
7841 pwd
->pw_dir
, XT_DIR
);
7844 /* icon cache dir */
7845 snprintf(cache_dir
, sizeof cache_dir
, "%s" PS
"%s", work_dir
, XT_CACHE_DIR
);
7849 snprintf(certs_dir
, sizeof certs_dir
, "%s" PS
"%s", work_dir
, XT_CERT_DIR
);
7853 snprintf(sessions_dir
, sizeof sessions_dir
, "%s" PS
"%s",
7854 work_dir
, XT_SESSIONS_DIR
);
7855 xxx_dir(sessions_dir
);
7858 snprintf(js_dir
, sizeof js_dir
, "%s" PS
"%s", work_dir
, XT_JS_DIR
);
7862 snprintf(temp_dir
, sizeof temp_dir
, "%s" PS
"%s", work_dir
, XT_TEMP_DIR
);
7865 /* runtime settings that can override config file */
7866 if (runtime_settings
[0] != '\0')
7867 config_parse(runtime_settings
, 1);
7870 if (!strcmp(download_dir
, pwd
->pw_dir
))
7871 strlcat(download_dir
, PS
"downloads", sizeof download_dir
);
7872 xxx_dir(download_dir
);
7874 /* first start file */
7875 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_SOD_FILE
);
7876 if (stat(file
, &sb
)) {
7877 warnx("start of day file doesn't exist, creating it");
7878 if ((f
= fopen(file
, "w")) == NULL
)
7879 err(1, "startofday");
7880 if (fputs(version
, f
) == EOF
)
7887 if ((f
= fopen(file
, "r+")) == NULL
)
7888 err(1, "startofday");
7889 if (fgets(sodversion
, sizeof sodversion
, f
) == NULL
)
7891 sodversion
[strcspn(sodversion
, "\n")] = '\0';
7892 if (strcmp(version
, sodversion
)) {
7894 if (fputs(version
, f
) == EOF
)
7897 /* upgrade, say something smart */
7903 /* favorites file */
7904 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_FAVS_FILE
);
7905 if (stat(file
, &sb
)) {
7906 warnx("favorites file doesn't exist, creating it");
7907 if ((f
= fopen(file
, "w")) == NULL
)
7908 err(1, "favorites");
7912 /* quickmarks file */
7913 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
7914 if (stat(file
, &sb
)) {
7915 warnx("quickmarks file doesn't exist, creating it");
7916 if ((f
= fopen(file
, "w")) == NULL
)
7917 err(1, "quickmarks");
7921 /* search history */
7922 if (history_autosave
) {
7923 snprintf(search_file
, sizeof search_file
, "%s" PS
"%s",
7924 work_dir
, XT_SEARCH_FILE
);
7925 if (stat(search_file
, &sb
)) {
7926 warnx("search history file doesn't exist, creating it");
7927 if ((f
= fopen(search_file
, "w")) == NULL
)
7928 err(1, "search_history");
7931 history_read(&shl
, search_file
, &search_history_count
);
7934 /* command history */
7935 if (history_autosave
) {
7936 snprintf(command_file
, sizeof command_file
, "%s" PS
"%s",
7937 work_dir
, XT_COMMAND_FILE
);
7938 if (stat(command_file
, &sb
)) {
7939 warnx("command history file doesn't exist, creating it");
7940 if ((f
= fopen(command_file
, "w")) == NULL
)
7941 err(1, "command_history");
7944 history_read(&chl
, command_file
, &cmd_history_count
);
7948 session
= webkit_get_default_session();
7952 set_ssl_ca_file(ssl_ca_file
);
7954 /* guess_search regex */
7955 if (url_regex
== NULL
)
7956 url_regex
= g_strdup(XT_URL_REGEX
);
7958 if (regcomp(&url_re
, url_regex
, REG_EXTENDED
| REG_NOSUB
))
7959 startpage_add("invalid url regex %s", url_regex
);
7962 env_proxy
= getenv("http_proxy");
7964 setup_proxy(env_proxy
);
7966 env_proxy
= getenv("HTTP_PROXY");
7968 setup_proxy(env_proxy
);
7970 setup_proxy(http_proxy
);
7972 #ifndef XT_SOCKET_DISABLE
7974 send_cmd_to_socket(argv
[0]);
7978 opte
= opte
; /* shut mingw up */
7980 /* set some connection parameters */
7981 g_object_set(session
, "max-conns", max_connections
, (char *)NULL
);
7982 g_object_set(session
, "max-conns-per-host", max_host_connections
,
7985 g_signal_connect(session
, "request-queued", G_CALLBACK(session_rq_cb
),
7988 #ifndef XT_SOCKET_DISABLE
7989 /* see if there is already an xxxterm running */
7990 if (single_instance
&& is_running()) {
7992 warnx("already running");
7997 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
7998 send_cmd_to_socket(cmd
);
8008 optn
= optn
; /* shut mingw up */
8013 if (enable_strict_transport
)
8014 strict_transport_init();
8016 /* uri completion */
8017 completion_model
= gtk_list_store_new(1, G_TYPE_STRING
);
8020 buffers_store
= gtk_list_store_new
8021 (NUM_COLS
, G_TYPE_UINT
, GDK_TYPE_PIXBUF
, G_TYPE_STRING
);
8027 notebook_tab_set_visibility();
8029 if (save_global_history
)
8030 restore_global_history();
8032 /* restore session list */
8033 restore_sessions_list();
8035 if (!strcmp(named_session
, XT_SAVED_TABS_FILE
))
8036 restore_saved_tabs();
8038 a
.s
= named_session
;
8039 a
.i
= XT_SES_DONOTHING
;
8040 open_tabs(NULL
, &a
);
8043 /* see if we have an exception */
8044 if (!TAILQ_EMPTY(&spl
)) {
8045 create_new_tab("about:startpage", NULL
, focus
, -1);
8050 create_new_tab(argv
[0], NULL
, focus
, -1);
8057 if (TAILQ_EMPTY(&tabs
))
8058 create_new_tab(home
, NULL
, 1, -1);
8059 #ifndef XT_SOCKET_DISABLE
8061 if ((s
= build_socket()) != -1) {
8062 channel
= g_io_channel_unix_new(s
);
8063 g_io_add_watch(channel
, G_IO_IN
, socket_watcher
, NULL
);
8071 gdk_threads_leave();
8072 g_static_rec_mutex_unlock_full(&my_gdk_mtx
); /* just in case */
8075 gnutls_global_deinit();