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
)
3438 gdk_threads_leave();
3441 col_str
= XT_COLOR_YELLOW
;
3442 switch (load_compare_cert(u
, &error_str
)) {
3444 col_str
= XT_COLOR_BLUE
;
3447 col_str
= XT_COLOR_GREEN
;
3449 case CERT_UNTRUSTED
:
3450 col_str
= XT_COLOR_YELLOW
;
3453 col_str
= XT_COLOR_RED
;
3458 gdk_threads_enter();
3460 /* make sure t isn't deleted */
3461 TAILQ_FOREACH(tt
, &tabs
, entry
)
3468 /* test to see if the user navigated away and canceled the thread */
3469 if (t
->thread
!= g_thread_self())
3471 if ((uri
= get_uri(t
)) == NULL
) {
3475 if (strcmp(uri
, u
)) {
3476 /* make sure we are still the same url */
3483 if (!strcmp(col_str
, XT_COLOR_WHITE
)) {
3484 text
= gdk_color_to_string(
3485 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3486 base
= gdk_color_to_string(
3487 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3488 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3489 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3490 statusbar_modify_attr(t
, text
, base
);
3494 gdk_color_parse(col_str
, &color
);
3495 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3496 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3499 if (error_str
&& error_str
[0] != '\0')
3500 show_oops(t
, "%s", error_str
);
3505 /* t is invalid at this point */
3507 g_free((gpointer
)u
);
3509 gdk_threads_leave();
3514 show_ca_status(struct tab
*t
, const char *uri
)
3517 gchar
*col_str
= XT_COLOR_WHITE
, *text
, *base
;
3519 DNPRINTF(XT_D_URL
, "show_ca_status: %d %s %s\n",
3520 ssl_strict_certs
, ssl_ca_file
, uri
);
3527 if (ssl_ca_file
== NULL
) {
3528 if (g_str_has_prefix(uri
, "http://"))
3530 if (g_str_has_prefix(uri
, "https://")) {
3531 col_str
= XT_COLOR_RED
;
3536 if (g_str_has_prefix(uri
, "http://") ||
3537 !g_str_has_prefix(uri
, "https://"))
3541 * It is not necessary to see if the thread is already running.
3542 * If the thread is in progress setting it to something else aborts it
3546 /* thread the coloring of the address bar */
3547 t
->thread
= g_thread_create((GThreadFunc
)color_address_bar
, t
, TRUE
, NULL
);
3549 color_address_bar(t
);
3556 if (!strcmp(col_str
, XT_COLOR_WHITE
)) {
3557 text
= gdk_color_to_string(
3558 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3559 base
= gdk_color_to_string(
3560 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3561 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3562 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3563 statusbar_modify_attr(t
, text
, base
);
3567 gdk_color_parse(col_str
, &color
);
3568 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3569 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3575 free_favicon(struct tab
*t
)
3577 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p req %p\n",
3578 __func__
, t
->icon_download
, t
->icon_request
);
3580 if (t
->icon_request
)
3581 g_object_unref(t
->icon_request
);
3582 if (t
->icon_dest_uri
)
3583 g_free(t
->icon_dest_uri
);
3585 t
->icon_request
= NULL
;
3586 t
->icon_dest_uri
= NULL
;
3590 xt_icon_from_name(struct tab
*t
, gchar
*name
)
3592 if (!enable_favicon_entry
)
3595 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->uri_entry
),
3596 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3598 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3599 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3601 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3602 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3606 xt_icon_from_pixbuf(struct tab
*t
, GdkPixbuf
*pb
)
3608 GdkPixbuf
*pb_scaled
;
3610 if (gdk_pixbuf_get_width(pb
) > 16 || gdk_pixbuf_get_height(pb
) > 16)
3611 pb_scaled
= gdk_pixbuf_scale_simple(pb
, 16, 16,
3612 GDK_INTERP_BILINEAR
);
3616 if (enable_favicon_entry
) {
3619 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
3620 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3623 if (show_url
== 0) {
3624 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
3625 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3627 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3628 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3631 /* XXX: Only supports the minimal tabs atm. */
3632 if (enable_favicon_tabs
)
3633 gtk_image_set_from_pixbuf(GTK_IMAGE(t
->tab_elems
.favicon
),
3636 if (pb_scaled
!= pb
)
3637 g_object_unref(pb_scaled
);
3641 xt_icon_from_file(struct tab
*t
, char *file
)
3645 if (g_str_has_prefix(file
, "file://"))
3646 file
+= strlen("file://");
3648 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
3650 xt_icon_from_pixbuf(t
, pb
);
3653 xt_icon_from_name(t
, "text-html");
3657 is_valid_icon(char *file
)
3660 const char *mime_type
;
3664 gf
= g_file_new_for_path(file
);
3665 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
3667 mime_type
= g_file_info_get_content_type(fi
);
3668 valid
= g_strcmp0(mime_type
, "image/x-ico") == 0 ||
3669 g_strcmp0(mime_type
, "image/vnd.microsoft.icon") == 0 ||
3670 g_strcmp0(mime_type
, "image/png") == 0 ||
3671 g_strcmp0(mime_type
, "image/gif") == 0 ||
3672 g_strcmp0(mime_type
, "application/octet-stream") == 0;
3680 set_favicon_from_file(struct tab
*t
, char *file
)
3684 if (t
== NULL
|| file
== NULL
)
3687 if (g_str_has_prefix(file
, "file://"))
3688 file
+= strlen("file://");
3689 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading %s\n", __func__
, file
);
3691 if (!stat(file
, &sb
)) {
3692 if (sb
.st_size
== 0 || !is_valid_icon(file
)) {
3693 /* corrupt icon so trash it */
3694 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3697 /* no need to set icon to default here */
3701 xt_icon_from_file(t
, file
);
3705 favicon_download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
3708 WebKitDownloadStatus status
= webkit_download_get_status(download
);
3709 struct tab
*tt
= NULL
, *t
= NULL
;
3712 * find the webview instead of passing in the tab as it could have been
3713 * deleted from underneath us.
3715 TAILQ_FOREACH(tt
, &tabs
, entry
) {
3724 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d status %d\n",
3725 __func__
, t
->tab_id
, status
);
3728 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
3730 t
->icon_download
= NULL
;
3733 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
3736 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
3739 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
3741 DNPRINTF(XT_D_DOWNLOAD
, "%s: freeing favicon %d\n",
3742 __func__
, t
->tab_id
);
3743 t
->icon_download
= NULL
;
3746 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
3749 DNPRINTF(XT_D_DOWNLOAD
, "%s: setting icon to %s\n",
3750 __func__
, t
->icon_dest_uri
);
3751 set_favicon_from_file(t
, t
->icon_dest_uri
);
3752 /* these will be freed post callback */
3753 t
->icon_request
= NULL
;
3754 t
->icon_download
= NULL
;
3762 abort_favicon_download(struct tab
*t
)
3764 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p\n", __func__
, t
->icon_download
);
3766 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
3767 if (t
->icon_download
) {
3768 g_signal_handlers_disconnect_by_func(G_OBJECT(t
->icon_download
),
3769 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3770 webkit_download_cancel(t
->icon_download
);
3771 t
->icon_download
= NULL
;
3776 xt_icon_from_name(t
, "text-html");
3780 notify_icon_loaded_cb(WebKitWebView
*wv
, gchar
*uri
, struct tab
*t
)
3782 DNPRINTF(XT_D_DOWNLOAD
, "%s %s\n", __func__
, uri
);
3784 if (uri
== NULL
|| t
== NULL
)
3787 #if WEBKIT_CHECK_VERSION(1, 4, 0)
3788 /* take icon from WebKitIconDatabase */
3791 pb
= webkit_web_view_get_icon_pixbuf(wv
);
3793 xt_icon_from_pixbuf(t
, pb
);
3796 xt_icon_from_name(t
, "text-html");
3797 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
3798 /* download icon to cache dir */
3799 gchar
*name_hash
, file
[PATH_MAX
];
3802 if (t
->icon_request
) {
3803 DNPRINTF(XT_D_DOWNLOAD
, "%s: download in progress\n", __func__
);
3807 /* check to see if we got the icon in cache */
3808 name_hash
= g_compute_checksum_for_string(G_CHECKSUM_SHA256
, uri
, -1);
3809 snprintf(file
, sizeof file
, "%s" PS
"%s.ico", cache_dir
, name_hash
);
3812 if (!stat(file
, &sb
)) {
3813 if (sb
.st_size
> 0) {
3814 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading from cache %s\n",
3816 set_favicon_from_file(t
, file
);
3820 /* corrupt icon so trash it */
3821 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3826 /* create download for icon */
3827 t
->icon_request
= webkit_network_request_new(uri
);
3828 if (t
->icon_request
== NULL
) {
3829 DNPRINTF(XT_D_DOWNLOAD
, "%s: invalid uri %s\n",
3834 t
->icon_download
= webkit_download_new(t
->icon_request
);
3835 if (t
->icon_download
== NULL
)
3838 /* we have to free icon_dest_uri later */
3839 t
->icon_dest_uri
= g_strdup_printf("file://%s", file
);
3840 webkit_download_set_destination_uri(t
->icon_download
,
3843 if (webkit_download_get_status(t
->icon_download
) ==
3844 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
3845 g_object_unref(t
->icon_request
);
3846 g_free(t
->icon_dest_uri
);
3847 t
->icon_request
= NULL
;
3848 t
->icon_dest_uri
= NULL
;
3852 g_signal_connect(G_OBJECT(t
->icon_download
), "notify::status",
3853 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3855 webkit_download_start(t
->icon_download
);
3860 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3862 const gchar
*uri
= NULL
;
3863 struct history
*h
, find
;
3867 DNPRINTF(XT_D_URL
, "notify_load_status_cb: %d %s\n",
3868 webkit_web_view_get_load_status(wview
),
3869 get_uri(t
) ? get_uri(t
) : "NOTHING");
3872 show_oops(NULL
, "notify_load_status_cb invalid parameters");
3876 switch (webkit_web_view_get_load_status(wview
)) {
3877 case WEBKIT_LOAD_PROVISIONAL
:
3879 abort_favicon_download(t
);
3880 #if GTK_CHECK_VERSION(2, 20, 0)
3881 gtk_widget_show(t
->spinner
);
3882 gtk_spinner_start(GTK_SPINNER(t
->spinner
));
3884 gtk_label_set_text(GTK_LABEL(t
->label
), "Loading");
3886 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), TRUE
);
3888 /* assume we are a new address */
3889 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3890 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3891 text
= gdk_color_to_string(
3892 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3893 base
= gdk_color_to_string(
3894 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3895 statusbar_modify_attr(t
, text
, base
);
3899 /* take focus if we are visible */
3905 /* kill color thread */
3910 case WEBKIT_LOAD_COMMITTED
:
3915 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
3921 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
3923 /* check if js white listing is enabled */
3924 if (enable_plugin_whitelist
)
3925 check_and_set_pl(uri
, t
);
3926 if (enable_cookie_whitelist
)
3927 check_and_set_cookie(uri
, t
);
3928 if (enable_js_whitelist
)
3929 check_and_set_js(uri
, t
);
3935 /* we know enough to autosave the session */
3936 if (session_autosave
) {
3941 show_ca_status(t
, uri
);
3942 run_script(t
, JS_HINTING
);
3943 if (enable_autoscroll
)
3944 run_script(t
, JS_AUTOSCROLL
);
3947 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
3949 if (color_visited_uris
) {
3950 color_visited(t
, color_visited_helper());
3952 /* This colors the links you middle-click (open in new
3953 * tab) in the current tab. */
3954 if (t
->tab_id
!= gtk_notebook_get_current_page(notebook
) &&
3955 (uri
= get_uri(t
)) != NULL
)
3956 color_visited(get_current_tab(),
3957 g_strdup_printf("{'%s' : 'dummy'}", uri
));
3961 case WEBKIT_LOAD_FINISHED
:
3963 if ((uri
= get_uri(t
)) == NULL
)
3966 if (!strncmp(uri
, "http://", strlen("http://")) ||
3967 !strncmp(uri
, "https://", strlen("https://")) ||
3968 !strncmp(uri
, "file://", strlen("file://"))) {
3969 find
.uri
= (gchar
*)uri
;
3970 h
= RB_FIND(history_list
, &hl
, &find
);
3972 insert_history_item(uri
,
3973 get_title(t
, FALSE
), time(NULL
));
3975 h
->time
= time(NULL
);
3978 set_status(t
, (char *)uri
, XT_STATUS_URI
);
3979 #if WEBKIT_CHECK_VERSION(1, 1, 18)
3980 case WEBKIT_LOAD_FAILED
:
3983 #if GTK_CHECK_VERSION(2, 20, 0)
3984 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
3985 gtk_widget_hide(t
->spinner
);
3988 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
3993 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
), TRUE
);
3995 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
),
3996 can_go_back_for_real(t
));
3998 gtk_widget_set_sensitive(GTK_WIDGET(t
->forward
),
3999 can_go_forward_for_real(t
));
4003 notify_title_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
4005 const gchar
*title
= NULL
, *win_title
= NULL
;
4007 title
= get_title(t
, FALSE
);
4008 win_title
= get_title(t
, TRUE
);
4010 gtk_label_set_text(GTK_LABEL(t
->label
), title
);
4011 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
), title
);
4014 if (win_title
&& t
->tab_id
== gtk_notebook_get_current_page(notebook
))
4015 gtk_window_set_title(GTK_WINDOW(main_window
), win_title
);
4019 get_domain(const gchar
*host
)
4024 /* handle silly domains like .co.uk */
4026 if ((x
= strlen(host
)) <= 6)
4027 return (g_strdup(host
));
4029 if (host
[x
- 3] == '.' && host
[x
- 6] == '.') {
4035 return (g_strdup(&host
[x
+ 1]));
4039 p
= g_strrstr(host
, ".");
4041 return (g_strdup(""));
4047 return (g_strdup(p
+ 1));
4049 return (g_strdup(host
));
4053 js_autorun(struct tab
*t
)
4057 size_t got_default
= 0, got_host
= 0;
4059 char deff
[PATH_MAX
], hostf
[PATH_MAX
];
4060 char *js
= NULL
, *jsat
, *domain
= NULL
;
4061 FILE *deffile
= NULL
, *hostfile
= NULL
;
4063 if (enable_js_autorun
== 0)
4068 !(g_str_has_prefix(uri
, "http://") ||
4069 g_str_has_prefix(uri
, "https://")))
4072 su
= soup_uri_new(uri
);
4075 if (!SOUP_URI_VALID_FOR_HTTP(su
))
4078 DNPRINTF(XT_D_JS
, "%s: host: %s domain: %s\n", __func__
,
4080 domain
= get_domain(su
->host
);
4082 snprintf(deff
, sizeof deff
, "%s" PS
"default.js", js_dir
);
4083 if ((deffile
= fopen(deff
, "r")) != NULL
) {
4084 if (fstat(fileno(deffile
), &sb
) == -1) {
4085 show_oops(t
, "can't stat default JS file");
4088 got_default
= sb
.st_size
;
4091 /* try host first followed by domain */
4092 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, su
->host
);
4093 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4094 if ((hostfile
= fopen(hostf
, "r")) == NULL
) {
4095 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, domain
);
4096 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4097 if ((hostfile
= fopen(hostf
, "r")) == NULL
)
4100 DNPRINTF(XT_D_JS
, "file: %s\n", hostf
);
4101 if (fstat(fileno(hostfile
), &sb
) == -1) {
4102 show_oops(t
, "can't stat %s JS file", hostf
);
4105 got_host
= sb
.st_size
;
4108 if (got_default
+ got_host
== 0)
4111 js
= g_malloc0(got_default
+ got_host
+ 1);
4115 if (fread(js
, got_default
, 1, deffile
) != 1) {
4116 show_oops(t
, "default file read error");
4119 jsat
= js
+ got_default
;
4123 if (fread(jsat
, got_host
, 1, hostfile
) != 1) {
4124 show_oops(t
, "host file read error");
4129 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
4146 webview_load_finished_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4148 /* autorun some js if enabled */
4156 webview_progress_changed_cb(WebKitWebView
*wv
, int progress
, struct tab
*t
)
4158 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->uri_entry
),
4159 progress
== 100 ? 0 : (double)progress
/ 100);
4160 if (show_url
== 0) {
4161 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
4162 progress
== 100 ? 0 : (double)progress
/ 100);
4165 update_statusbar_position(NULL
, NULL
);
4169 strict_transport_rb_cmp(struct strict_transport
*a
, struct strict_transport
*b
)
4174 /* compare strings from the end */
4175 l1
= strlen(a
->host
);
4176 l2
= strlen(b
->host
);
4180 for (; *p1
== *p2
&& p1
> a
->host
&& p2
> b
->host
;
4185 * Check if we need to do pattern expansion,
4186 * or if we're just keeping the tree in order
4188 if (a
->flags
& XT_STS_FLAGS_EXPAND
&&
4189 b
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) {
4190 /* Check if we're matching the
4191 * 'host.xyz' part in '*.host.xyz'
4193 if (p2
== b
->host
&& (p1
== a
->host
|| *(p1
-1) == '.')) {
4198 if (p1
== a
->host
&& p2
== b
->host
)
4212 RB_GENERATE(strict_transport_tree
, strict_transport
, entry
,
4213 strict_transport_rb_cmp
);
4216 strict_transport_add(const char *domain
, time_t timeout
, int subdomains
)
4218 struct strict_transport
*d
, find
;
4222 if (enable_strict_transport
== FALSE
)
4225 DPRINTF("strict_transport_add(%s,%lld,%d)\n", domain
,
4226 (long long)timeout
, subdomains
);
4232 find
.host
= (char *)domain
;
4234 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4238 /* check if update is needed */
4239 if (d
->timeout
== timeout
&&
4240 (d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) == subdomains
)
4243 d
->timeout
= timeout
;
4245 d
->flags
|= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4247 /* We're still initializing */
4248 if (strict_transport_file
== NULL
)
4251 if ((f
= fopen(strict_transport_file
, "w")) == NULL
) {
4253 "can't open strict-transport rules file");
4257 fprintf(f
, "# Generated file - do not update unless you know "
4258 "what you're doing\n");
4259 RB_FOREACH(d
, strict_transport_tree
, &st_tree
) {
4260 if (d
->timeout
< now
)
4262 fprintf(f
, "%s\t%lld\t%d\n", d
->host
, (long long)d
->timeout
,
4263 d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
);
4267 d
= g_malloc(sizeof *d
);
4268 d
->host
= g_strdup(domain
);
4269 d
->timeout
= timeout
;
4271 d
->flags
= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4274 RB_INSERT(strict_transport_tree
, &st_tree
, d
);
4276 /* We're still initializing */
4277 if (strict_transport_file
== NULL
)
4280 if ((f
= fopen(strict_transport_file
, "a+")) == NULL
) {
4282 "can't open strict-transport rules file");
4286 fseek(f
, 0, SEEK_END
);
4287 fprintf(f
,"%s\t%lld\t%d\n", d
->host
, (long long)timeout
, subdomains
);
4294 strict_transport_check(const char *host
)
4296 static struct strict_transport
*d
= NULL
;
4297 struct strict_transport find
;
4299 if (enable_strict_transport
== FALSE
)
4302 find
.host
= (char *)host
;
4304 /* match for domains that include subdomains */
4305 find
.flags
= XT_STS_FLAGS_EXPAND
;
4307 /* First, check if we're already at the right node */
4308 if (d
!= NULL
&& strict_transport_rb_cmp(&find
, d
) == 0) {
4312 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4320 strict_transport_init()
4322 char file
[PATH_MAX
];
4328 time_t timeout
, now
;
4331 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_STS_FILE
);
4332 if ((f
= fopen(file
, "r")) == NULL
) {
4333 strict_transport_file
= g_strdup(file
);
4344 if ((rule
= fparseln(f
, &len
, NULL
, delim
, 0)) == NULL
) {
4345 if (!feof(f
) || ferror(f
))
4351 /* get second entry */
4352 if ((ptr
= strpbrk(rule
, " \t")) == NULL
)
4356 timeout
= atoi(ptr
);
4358 /* get third entry */
4359 if ((ptr
= strpbrk(ptr
, " \t")) == NULL
)
4363 subdomains
= atoi(ptr
);
4366 strict_transport_add(rule
, timeout
, subdomains
);
4371 strict_transport_file
= g_strdup(file
);
4375 startpage_add("strict-transport rules file ('%s') is corrupt", file
);
4383 strict_transport_security_cb(SoupMessage
*msg
, gpointer data
)
4389 int subdomains
= FALSE
;
4394 sts
= soup_message_headers_get_one(msg
->response_headers
,
4395 "Strict-Transport-Security");
4396 uri
= soup_message_get_uri(msg
);
4398 if (sts
== NULL
|| uri
== NULL
)
4401 if ((ptr
= strcasestr(sts
, "max-age="))) {
4402 ptr
+= strlen("max-age=");
4403 timeout
= atoll(ptr
);
4405 return; /* malformed header - max-age must be included */
4407 if ((ptr
= strcasestr(sts
, "includeSubDomains")))
4410 strict_transport_add(uri
->host
, timeout
+ time(NULL
), subdomains
);
4414 session_rq_cb(SoupSession
*s
, SoupMessage
*msg
, SoupSocket
*socket
,
4424 if (s
== NULL
|| msg
== NULL
)
4427 if (referer_mode
== XT_REFERER_ALWAYS
)
4430 /* Check if referer is set - and what the user requested for referers */
4431 ref
= soup_message_headers_get_one(msg
->request_headers
, "Referer");
4433 DNPRINTF(XT_D_NAV
, "session_rq_cb: Referer: %s\n", ref
);
4434 switch (referer_mode
) {
4435 case XT_REFERER_NEVER
:
4436 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing referer\n");
4437 soup_message_headers_remove(msg
->request_headers
,
4440 case XT_REFERER_SAME_DOMAIN
:
4441 ref_uri
= soup_uri_new(ref
);
4442 dest
= soup_message_get_uri(msg
);
4444 ref_suffix
= tld_get_suffix(ref_uri
->host
);
4445 dest_suffix
= tld_get_suffix(dest
->host
);
4447 if (dest
&& ref_suffix
&& dest_suffix
&&
4448 strcmp(ref_suffix
, dest_suffix
) != 0) {
4449 soup_message_headers_remove(msg
->request_headers
,
4451 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4452 "referer (not same domain) (suffixes: %s - %s)\n",
4453 ref_suffix
, dest_suffix
);
4455 soup_uri_free(ref_uri
);
4457 case XT_REFERER_SAME_FQDN
:
4458 ref_uri
= soup_uri_new(ref
);
4459 dest
= soup_message_get_uri(msg
);
4460 if (dest
&& strcmp(ref_uri
->host
, dest
->host
) != 0) {
4461 soup_message_headers_remove(msg
->request_headers
,
4463 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4464 "referer (not same fqdn) (should be %s)\n",
4467 soup_uri_free(ref_uri
);
4469 case XT_REFERER_CUSTOM
:
4470 DNPRINTF(XT_D_NAV
, "session_rq_cb: setting referer "
4471 "to %s\n", referer_custom
);
4472 soup_message_headers_replace(msg
->request_headers
,
4473 "Referer", referer_custom
);
4478 if (enable_strict_transport
) {
4479 soup_message_add_header_handler(msg
, "finished",
4480 "Strict-Transport-Security",
4481 G_CALLBACK(strict_transport_security_cb
), NULL
);
4486 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
4487 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
4488 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
4491 WebKitWebNavigationReason reason
;
4492 struct domain
*d
= NULL
;
4495 show_oops(NULL
, "webview_npd_cb invalid parameters");
4499 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
4501 webkit_network_request_get_uri(request
));
4503 uri
= (char *)webkit_network_request_get_uri(request
);
4505 if (!auto_load_images
&& t
->load_images
) {
4507 /* Disable autoloading of images, now that we're done loading
4509 g_object_set(G_OBJECT(t
->settings
),
4510 "auto-load-images", FALSE
, (char *)NULL
);
4511 webkit_web_view_set_settings(t
->wv
, t
->settings
);
4513 t
->load_images
= FALSE
;
4516 /* If this is an xtp url, we don't load anything else. */
4517 if (parse_xtp_url(t
, uri
))
4520 if ((t
->mode
== XT_MODE_HINT
&& t
->new_tab
) || t
->ctrl_click
) {
4522 create_new_tab(uri
, NULL
, ctrl_click_focus
, -1);
4523 webkit_web_policy_decision_ignore(pd
);
4524 return (TRUE
); /* we made the decission */
4527 /* Change user agent if more than one has been given. */
4528 if (user_agent_count
> 1) {
4529 struct user_agent
*ua
;
4531 if ((ua
= TAILQ_NEXT(user_agent
, entry
)) == NULL
)
4532 user_agent
= TAILQ_FIRST(&ua_list
);
4536 free(t
->user_agent
);
4537 t
->user_agent
= g_strdup(user_agent
->value
);
4539 DNPRINTF(XT_D_NAV
, "user-agent: %s\n", t
->user_agent
);
4541 g_object_set(G_OBJECT(t
->settings
),
4542 "user-agent", t
->user_agent
, (char *)NULL
);
4544 webkit_web_view_set_settings(wv
, t
->settings
);
4548 * This is a little hairy but it comes down to this:
4549 * when we run in whitelist mode we have to assist the browser in
4550 * opening the URL that it would have opened in a new tab.
4552 reason
= webkit_web_navigation_action_get_reason(na
);
4553 if (reason
== WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED
) {
4554 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
4555 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1)
4556 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4558 webkit_web_policy_decision_use(pd
);
4559 return (TRUE
); /* we made the decision */
4566 webview_rrs_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, WebKitWebResource
*res
,
4567 WebKitNetworkRequest
*request
, WebKitNetworkResponse
*response
,
4573 msg
= webkit_network_request_get_message(request
);
4576 uri
= soup_message_get_uri(msg
);
4579 if (strcmp(soup_uri_get_scheme(uri
), SOUP_URI_SCHEME_HTTP
) == 0) {
4580 if (strict_transport_check(uri
->host
)) {
4581 DNPRINTF(XT_D_NAV
, "webview_rrs_cb: force https for %s\n",
4583 soup_uri_set_scheme(uri
, SOUP_URI_SCHEME_HTTPS
);
4589 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4592 struct domain
*d
= NULL
;
4594 WebKitWebView
*webview
= NULL
;
4597 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
4598 webkit_web_view_get_uri(wv
));
4601 /* open in current tab */
4603 } else if (enable_scripts
== 0 && enable_js_whitelist
== 1) {
4604 uri
= webkit_web_view_get_uri(wv
);
4605 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4608 if (t
->ctrl_click
) {
4609 x
= ctrl_click_focus
;
4612 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4614 } else if (enable_scripts
== 1) {
4615 if (t
->ctrl_click
) {
4616 x
= ctrl_click_focus
;
4619 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4627 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
4630 struct domain
*d
= NULL
;
4632 DNPRINTF(XT_D_NAV
, "webview_close_cb: %d\n", t
->tab_id
);
4634 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
4635 uri
= webkit_web_view_get_uri(wv
);
4636 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4640 } else if (enable_scripts
== 1)
4647 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
4649 /* we can not eat the event without throwing gtk off so defer it */
4651 /* catch middle click */
4652 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 2) {
4657 /* catch ctrl click */
4658 if (e
->type
== GDK_BUTTON_RELEASE
&&
4659 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
4664 return (XT_CB_PASSTHROUGH
);
4668 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
4670 struct mime_type
*m
;
4672 m
= find_mime_type(mime_type
);
4678 return (fork_exec(t
, m
->mt_action
,
4679 webkit_network_request_get_uri(request
),
4680 "can't launch MIME handler", 0));
4684 get_mime_type(const char *file
)
4687 char *mime_type
= NULL
;
4691 if (g_str_has_prefix(file
, "file://"))
4692 file
+= strlen("file://");
4694 gf
= g_file_new_for_path(file
);
4695 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
4697 if ((m
= g_file_info_get_content_type(fi
)) != NULL
)
4698 mime_type
= g_strdup(m
);
4706 run_download_mimehandler(char *mime_type
, char *file
)
4708 struct mime_type
*m
;
4710 m
= find_mime_type(mime_type
);
4714 return (fork_exec(NULL
, m
->mt_action
, file
,
4715 "can't launch download MIME handler", 0));
4719 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
4722 WebKitDownloadStatus status
;
4723 const char *file
= NULL
;
4726 if (download
== NULL
)
4728 status
= webkit_download_get_status(download
);
4729 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
4732 file
= webkit_download_get_destination_uri(download
);
4735 mime
= get_mime_type(file
);
4739 if (g_str_has_prefix(file
, "file://"))
4740 file
+= strlen("file://");
4741 run_download_mimehandler((char *)mime
, (char *)file
);
4746 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
4747 WebKitNetworkRequest
*request
, char *mime_type
,
4748 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
4751 show_oops(NULL
, "webview_mimetype_cb invalid parameters");
4755 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
4756 t
->tab_id
, mime_type
);
4758 if (run_mimehandler(t
, mime_type
, request
) == 0) {
4759 webkit_web_policy_decision_ignore(decision
);
4764 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
4765 webkit_web_policy_decision_download(decision
);
4773 download_start(struct tab
*t
, struct download
*d
, int flag
)
4775 WebKitNetworkRequest
*req
;
4777 const gchar
*suggested_name
;
4778 gchar
*filename
= NULL
;
4783 if (d
== NULL
|| t
== NULL
) {
4784 show_oops(NULL
, "%s invalid parameters", __func__
);
4788 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
4789 if (suggested_name
== NULL
)
4790 return (FALSE
); /* abort download */
4801 filename
= g_strdup_printf("%d%s", i
, suggested_name
);
4804 uri
= g_strdup_printf("%s\\%s", download_dir
, i
?
4805 filename
: suggested_name
);
4807 uri
= g_strdup_printf("file://%s/%s", download_dir
, i
?
4808 filename
: suggested_name
);
4812 } while (!stat(uri
, &sb
));
4814 } while (!stat(uri
+ strlen("file://"), &sb
)); /* XXX is the + strlen right? */
4817 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d filename %s "
4818 "local %s\n", __func__
, t
->tab_id
, filename
, uri
);
4820 /* if we're restarting the download, or starting
4821 * it after doing something else, we need to recreate
4822 * the download request.
4824 if (flag
== XT_DL_RESTART
) {
4825 req
= webkit_network_request_new(webkit_download_get_uri(d
->download
));
4826 webkit_download_cancel(d
->download
);
4827 g_object_unref(d
->download
);
4828 d
->download
= webkit_download_new(req
);
4831 webkit_download_set_destination_uri(d
->download
, uri
);
4833 if (webkit_download_get_status(d
->download
) ==
4834 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
4835 show_oops(t
, "%s: download failed to start", __func__
);
4837 gtk_label_set_text(GTK_LABEL(t
->label
), "Download Failed");
4839 /* connect "download first" mime handler */
4840 g_signal_connect(G_OBJECT(d
->download
), "notify::status",
4841 G_CALLBACK(download_status_changed_cb
), NULL
);
4843 /* get from history */
4844 g_object_ref(d
->download
);
4845 gtk_label_set_text(GTK_LABEL(t
->label
), "Downloading");
4846 show_oops(t
, "Download of '%s' started...",
4847 basename((char *)webkit_download_get_destination_uri(d
->download
)));
4850 if (flag
!= XT_DL_START
)
4851 webkit_download_start(d
->download
);
4853 DNPRINTF(XT_D_DOWNLOAD
, "download status : %d",
4854 webkit_download_get_status(d
->download
));
4856 /* sync other download manager tabs */
4857 update_download_tabs(NULL
);
4869 download_ask_cb(struct tab
*t
, GdkEventKey
*e
, gpointer data
)
4871 struct download
*d
= data
;
4875 t
->mode_cb_data
= NULL
;
4878 e
->keyval
= GDK_Escape
;
4879 return (XT_CB_PASSTHROUGH
);
4882 DPRINTF("download_ask_cb: User pressed %c\n", e
->keyval
);
4883 if (e
->keyval
== 'y' || e
->keyval
== 'Y' || e
->keyval
== GDK_Return
)
4884 /* We need to do a RESTART, because we're not calling from
4885 * webview_download_cb
4887 download_start(t
, d
, XT_DL_RESTART
);
4889 /* for all other keyvals, we just let the download be */
4890 e
->keyval
= GDK_Escape
;
4891 return (XT_CB_HANDLED
);
4895 download_ask(struct tab
*t
, struct download
*d
)
4897 const gchar
*suggested_name
;
4899 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
4900 if (suggested_name
== NULL
)
4901 return (FALSE
); /* abort download */
4903 show_oops(t
, "download file %s [y/n] ?", suggested_name
);
4904 t
->mode_cb
= download_ask_cb
;
4905 t
->mode_cb_data
= d
;
4911 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
4914 const gchar
*suggested_name
;
4915 struct download
*download_entry
;
4918 if (wk_download
== NULL
|| t
== NULL
) {
4919 show_oops(NULL
, "%s invalid parameters", __func__
);
4923 suggested_name
= webkit_download_get_suggested_filename(wk_download
);
4924 if (suggested_name
== NULL
)
4925 return (FALSE
); /* abort download */
4927 download_entry
= g_malloc(sizeof(struct download
));
4928 download_entry
->download
= wk_download
;
4929 download_entry
->tab
= t
;
4930 download_entry
->id
= next_download_id
++;
4931 RB_INSERT(download_list
, &downloads
, download_entry
);
4933 if (download_mode
== XT_DM_START
)
4934 ret
= download_start(t
, download_entry
, XT_DL_START
);
4935 else if (download_mode
== XT_DM_ASK
)
4936 ret
= download_ask(t
, download_entry
);
4937 else if (download_mode
== XT_DM_ADD
)
4938 show_oops(t
, "added %s to download manager",
4941 /* sync other download manager tabs */
4942 update_download_tabs(NULL
);
4945 * NOTE: never redirect/render the current tab before this
4946 * function returns. This will cause the download to never start.
4948 return (ret
); /* start download */
4952 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
4954 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
4957 show_oops(NULL
, "webview_hover_cb");
4962 set_status(t
, uri
, XT_STATUS_LINK
);
4965 set_status(t
, t
->status
, XT_STATUS_NOTHING
);
4970 mark(struct tab
*t
, struct karg
*arg
)
4977 if ((index
= marktoindex(mark
)) == -1)
4980 if (arg
->i
== XT_MARK_SET
)
4981 t
->mark
[index
] = gtk_adjustment_get_value(t
->adjust_v
);
4982 else if (arg
->i
== XT_MARK_GOTO
) {
4983 if (t
->mark
[index
] == XT_INVALID_MARK
) {
4984 show_oops(t
, "mark '%c' does not exist", mark
);
4987 /* XXX t->mark[index] can be bigger than the maximum if ajax or
4988 something changes the document size */
4989 pos
= gtk_adjustment_get_value(t
->adjust_v
);
4990 gtk_adjustment_set_value(t
->adjust_v
, t
->mark
[index
]);
4991 t
->mark
[marktoindex('\'')] = pos
;
4998 marks_clear(struct tab
*t
)
5002 for (i
= 0; i
< LENGTH(t
->mark
); i
++)
5003 t
->mark
[i
] = XT_INVALID_MARK
;
5009 char file
[PATH_MAX
];
5010 char *line
= NULL
, *p
;
5015 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
5016 if ((f
= fopen(file
, "r+")) == NULL
) {
5017 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
5021 for (i
= 1; ; i
++) {
5022 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
5024 if (strlen(line
) == 0 || line
[0] == '#') {
5030 p
= strtok(line
, " \t");
5032 if (p
== NULL
|| strlen(p
) != 1 ||
5033 (index
= qmarktoindex(*p
)) == -1) {
5034 warnx("corrupt quickmarks file, line %d", i
);
5038 p
= strtok(NULL
, " \t");
5039 if (qmarks
[index
] != NULL
)
5040 g_free(qmarks
[index
]);
5041 qmarks
[index
] = g_strdup(p
);
5052 char file
[PATH_MAX
];
5056 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
5057 if ((f
= fopen(file
, "r+")) == NULL
) {
5058 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
5062 for (i
= 0; i
< XT_NOQMARKS
; i
++)
5063 if (qmarks
[i
] != NULL
)
5064 fprintf(f
, "%c %s\n", indextoqmark(i
), qmarks
[i
]);
5072 qmark(struct tab
*t
, struct karg
*arg
)
5077 mark
= arg
->s
[strlen(arg
->s
)-1];
5078 index
= qmarktoindex(mark
);
5084 if (qmarks
[index
] != NULL
) {
5085 g_free(qmarks
[index
]);
5086 qmarks
[index
] = NULL
;
5089 qmarks_load(); /* sync if multiple instances */
5090 qmarks
[index
] = g_strdup(get_uri(t
));
5094 if (qmarks
[index
] != NULL
)
5095 load_uri(t
, qmarks
[index
]);
5097 show_oops(t
, "quickmark \"%c\" does not exist",
5103 if (qmarks
[index
] != NULL
)
5104 create_new_tab(qmarks
[index
], NULL
, 1, -1);
5106 show_oops(t
, "quickmark \"%c\" does not exist",
5117 go_up(struct tab
*t
, struct karg
*args
)
5125 if (args
->i
== XT_GO_UP_ROOT
)
5126 levels
= XT_GO_UP_ROOT
;
5127 else if ((levels
= atoi(args
->s
)) == 0)
5130 uri
= g_strdup(get_uri(t
));
5134 if ((tmp
= strstr(uri
, XT_PROTO_DELIM
)) == NULL
)
5137 tmp
+= strlen(XT_PROTO_DELIM
);
5139 /* it makes no sense to strip the last slash from ".../dir/", skip it */
5140 lastidx
= strlen(tmp
) - 1;
5142 if (tmp
[lastidx
] == '/')
5143 tmp
[lastidx
] = '\0';
5147 p
= strrchr(tmp
, '/');
5148 if (p
== tmp
) { /* Are we at the root of a file://-path? */
5151 } else if (p
!= NULL
)
5164 gototab(struct tab
*t
, struct karg
*args
)
5167 struct karg arg
= {0, NULL
, -1};
5169 tab
= atoi(args
->s
);
5172 arg
.i
= XT_TAB_NEXT
;
5184 zoom_amount(struct tab
*t
, struct karg
*arg
)
5186 struct karg narg
= {0, NULL
, -1};
5188 narg
.i
= atoi(arg
->s
);
5189 resizetab(t
, &narg
);
5195 flip_colon(struct tab
*t
, struct karg
*arg
)
5197 struct karg narg
= {0, NULL
, -1};
5200 if (t
== NULL
|| arg
== NULL
)
5203 p
= strstr(arg
->s
, ":");
5215 /* buffer commands receive the regex that triggered them in arg.s */
5216 char bcmd
[XT_BUFCMD_SZ
];
5220 #define XT_PRE_NO (0)
5221 #define XT_PRE_YES (1)
5222 #define XT_PRE_MAYBE (2)
5224 int (*func
)(struct tab
*, struct karg
*);
5228 { "^[0-9]*gu$", XT_PRE_MAYBE
, "gu", go_up
, 0 },
5229 { "^gU$", XT_PRE_NO
, "gU", go_up
, XT_GO_UP_ROOT
},
5230 { "^gg$", XT_PRE_NO
, "gg", move
, XT_MOVE_TOP
},
5231 { "^gG$", XT_PRE_NO
, "gG", move
, XT_MOVE_BOTTOM
},
5232 { "^[0-9]+%$", XT_PRE_YES
, "%", move
, XT_MOVE_PERCENT
},
5233 { "^zz$", XT_PRE_NO
, "zz", move
, XT_MOVE_CENTER
},
5234 { "^gh$", XT_PRE_NO
, "gh", go_home
, 0 },
5235 { "^m[a-zA-Z0-9]$", XT_PRE_NO
, "m", mark
, XT_MARK_SET
},
5236 { "^['][a-zA-Z0-9']$", XT_PRE_NO
, "'", mark
, XT_MARK_GOTO
},
5237 { "^[0-9]+t$", XT_PRE_YES
, "t", gototab
, 0 },
5238 { "^g0$", XT_PRE_YES
, "g0", movetab
, XT_TAB_FIRST
},
5239 { "^g[$]$", XT_PRE_YES
, "g$", movetab
, XT_TAB_LAST
},
5240 { "^[0-9]*gt$", XT_PRE_YES
, "t", movetab
, XT_TAB_NEXT
},
5241 { "^[0-9]*gT$", XT_PRE_YES
, "T", movetab
, XT_TAB_PREV
},
5242 { "^M[a-zA-Z0-9]$", XT_PRE_NO
, "M", qmark
, XT_QMARK_SET
},
5243 { "^go[a-zA-Z0-9]$", XT_PRE_NO
, "go", qmark
, XT_QMARK_OPEN
},
5244 { "^gn[a-zA-Z0-9]$", XT_PRE_NO
, "gn", qmark
, XT_QMARK_TAB
},
5245 { "^ZR$", XT_PRE_NO
, "ZR", restart
, 0 },
5246 { "^ZZ$", XT_PRE_NO
, "ZZ", quit
, 0 },
5247 { "^zi$", XT_PRE_NO
, "zi", resizetab
, XT_ZOOM_IN
},
5248 { "^zo$", XT_PRE_NO
, "zo", resizetab
, XT_ZOOM_OUT
},
5249 { "^z0$", XT_PRE_NO
, "z0", resizetab
, XT_ZOOM_NORMAL
},
5250 { "^[0-9]+Z$", XT_PRE_YES
, "Z", zoom_amount
, 0 },
5251 { "^[0-9]+:$", XT_PRE_YES
, ":", flip_colon
, 0 },
5255 buffercmd_init(void)
5259 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5260 if (regcomp(&buffercmds
[i
].cregex
, buffercmds
[i
].regex
,
5261 REG_EXTENDED
| REG_NOSUB
))
5262 startpage_add("invalid buffercmd regex %s",
5263 buffercmds
[i
].regex
);
5267 buffercmd_abort(struct tab
*t
)
5274 DNPRINTF(XT_D_BUFFERCMD
, "%s: clearing buffer\n", __func__
);
5276 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5279 cmd_prefix
= 0; /* clear prefix for non-buffer commands */
5280 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
5284 buffercmd_execute(struct tab
*t
, struct buffercmd
*cmd
)
5286 struct karg arg
= {0, NULL
, -1};
5289 arg
.s
= g_strdup(bcmd
);
5291 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_execute: buffer \"%s\" "
5292 "matches regex \"%s\", executing\n", bcmd
, cmd
->regex
);
5302 buffercmd_addkey(struct tab
*t
, guint keyval
)
5305 char s
[XT_BUFCMD_SZ
];
5307 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
))) {
5309 return (XT_CB_PASSTHROUGH
);
5312 if (keyval
== GDK_Escape
) {
5314 return (XT_CB_HANDLED
);
5317 /* key with modifier or non-ascii character */
5318 if (!isascii(keyval
)) {
5320 * XXX this looks wrong but fixes some sites like
5321 * http://www.seslisozluk.com/
5322 * that eat a shift or ctrl and end putting default focus in js
5323 * instead of ignoring the keystroke
5324 * so instead of return (XT_CB_PASSTHROUGH); eat the key
5326 return (XT_CB_HANDLED
);
5329 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: adding key \"%c\" "
5330 "to buffer \"%s\"\n", keyval
, bcmd
);
5332 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5333 if (bcmd
[i
] == '\0') {
5338 /* buffer full, ignore input */
5339 if (i
>= LENGTH(bcmd
) -1) {
5340 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: buffer full\n");
5342 return (XT_CB_HANDLED
);
5345 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
5347 /* find exact match */
5348 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5349 if (regexec(&buffercmds
[i
].cregex
, bcmd
,
5350 (size_t) 0, NULL
, 0) == 0) {
5351 buffercmd_execute(t
, &buffercmds
[i
]);
5355 /* find non exact matches to see if we need to abort ot not */
5356 for (i
= 0, match
= 0; i
< LENGTH(buffercmds
); i
++) {
5357 DNPRINTF(XT_D_BUFFERCMD
, "trying: %s\n", bcmd
);
5360 if (buffercmds
[i
].precount
== XT_PRE_MAYBE
) {
5361 if (isdigit(bcmd
[0])) {
5362 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5366 if (sscanf(bcmd
, "%s", s
) == 0)
5369 } else if (buffercmds
[i
].precount
== XT_PRE_YES
) {
5370 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5373 if (sscanf(bcmd
, "%s", s
) == 0)
5376 if (c
== -1 && buffercmds
[i
].precount
)
5378 if (!strncmp(s
, buffercmds
[i
].cmd
, strlen(s
)))
5381 DNPRINTF(XT_D_BUFFERCMD
, "got[%d] %d <%s>: %d %s\n",
5382 i
, match
, buffercmds
[i
].cmd
, c
, s
);
5385 DNPRINTF(XT_D_BUFFERCMD
, "aborting: %s\n", bcmd
);
5390 return (XT_CB_HANDLED
);
5394 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
5396 struct key_binding
*k
;
5398 /* handle keybindings if buffercmd is empty.
5399 if not empty, allow commands like C-n */
5400 if (bcmd
[0] == '\0' || ((e
->state
& (CTRL
| MOD1
)) != 0))
5401 TAILQ_FOREACH(k
, &kbl
, entry
)
5402 if (e
->keyval
== k
->key
5403 && (entry
? k
->use_in_entry
: 1)) {
5404 /* when we are edditing eat ctrl/mod keys */
5405 if (edit_mode
== XT_EM_VI
&&
5406 t
->mode
== XT_MODE_INSERT
&&
5407 (e
->state
& CTRL
|| e
->state
& MOD1
))
5408 return (XT_CB_PASSTHROUGH
);
5411 if ((e
->state
& (CTRL
| MOD1
)) == 0)
5412 return (cmd_execute(t
, k
->cmd
));
5413 } else if ((e
->state
& k
->mask
) == k
->mask
) {
5414 return (cmd_execute(t
, k
->cmd
));
5418 if (!entry
&& ((e
->state
& (CTRL
| MOD1
)) == 0))
5419 return buffercmd_addkey(t
, e
->keyval
);
5421 return (XT_CB_PASSTHROUGH
);
5425 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5429 /* don't use w directly; use t->whatever instead */
5432 show_oops(NULL
, "wv_keypress_cb");
5433 return (XT_CB_PASSTHROUGH
);
5439 return t
->mode_cb(t
, e
, t
->mode_cb_data
);
5441 DNPRINTF(XT_D_KEY
, "wv_keypress_cb: mode %d keyval 0x%x mask "
5442 "0x%x tab %d\n", t
->mode
, e
->keyval
, e
->state
, t
->tab_id
);
5444 /* Hide buffers, if they are visible, with escape. */
5445 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)) &&
5446 CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
5447 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5449 return (XT_CB_HANDLED
);
5452 if (t
->mode
== XT_MODE_HINT
)
5453 return (XT_CB_HANDLED
);
5455 if ((CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Tab
) ||
5456 (CLEAN(e
->state
) == SHFT
&& e
->keyval
== GDK_Tab
))
5457 /* something focussy is about to happen */
5458 return (XT_CB_PASSTHROUGH
);
5460 /* check if we are some sort of text input thing in the dom */
5461 input_check_mode(t
);
5463 if (t
->mode
== XT_MODE_HINT
) {
5464 /* XXX make sure cmd entry is enabled */
5465 return (XT_CB_HANDLED
);
5466 } else if (t
->mode
== XT_MODE_PASSTHROUGH
) {
5467 if (CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
)
5468 t
->mode
= XT_MODE_COMMAND
;
5469 return (XT_CB_PASSTHROUGH
);
5470 } else if (t
->mode
== XT_MODE_COMMAND
) {
5472 snprintf(s
, sizeof s
, "%c", e
->keyval
);
5473 if (CLEAN(e
->state
) == 0 && isdigit(s
[0]))
5474 cmd_prefix
= 10 * cmd_prefix
+ atoi(s
);
5475 return (handle_keypress(t
, e
, 0));
5478 return (handle_keypress(t
, e
, 1));
5482 return (XT_CB_PASSTHROUGH
);
5486 hint_continue(struct tab
*t
)
5488 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5490 const gchar
*errstr
= NULL
;
5494 if (!(c
[0] == '.' || c
[0] == ','))
5496 if (strlen(c
) == 1) {
5497 /* XXX should not happen */
5502 if (isdigit(c
[1])) {
5504 i
= strtonum(&c
[1], 1, 4096, &errstr
);
5506 show_oops(t
, "invalid numerical hint %s", &c
[1]);
5509 s
= g_strdup_printf("hints.updateHints(%d);", i
);
5513 /* alphanumeric input */
5514 s
= g_strdup_printf("hints.createHints('%s', '%c');",
5515 &c
[1], c
[0] == '.' ? 'f' : 'F');
5526 search_continue(struct tab
*t
)
5528 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5529 gboolean rv
= FALSE
;
5531 if (c
[0] == ':' || c
[0] == '.' || c
[0] == ',')
5533 if (strlen(c
) == 1) {
5534 webkit_web_view_unmark_text_matches(t
->wv
);
5539 t
->search_forward
= TRUE
;
5540 else if (c
[0] == '?')
5541 t
->search_forward
= FALSE
;
5551 search_cb(struct tab
*t
)
5553 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5556 if (search_continue(t
) == FALSE
)
5560 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, t
->search_forward
,
5562 /* not found, mark red */
5563 gdk_color_parse(XT_COLOR_RED
, &color
);
5564 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
5565 /* unmark and remove selection */
5566 webkit_web_view_unmark_text_matches(t
->wv
);
5567 /* my kingdom for a way to unselect text in webview */
5569 /* found, highlight all */
5570 webkit_web_view_unmark_text_matches(t
->wv
);
5571 webkit_web_view_mark_text_matches(t
->wv
, &c
[1], FALSE
, 0);
5572 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
5573 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
5574 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
5582 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5584 const gchar
*c
= gtk_entry_get_text(w
);
5587 show_oops(NULL
, "cmd_keyrelease_cb invalid parameters");
5588 return (XT_CB_PASSTHROUGH
);
5591 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
5592 e
->keyval
, e
->state
, t
->tab_id
);
5595 if (!(e
->keyval
== GDK_Tab
|| e
->keyval
== GDK_ISO_Left_Tab
)) {
5596 if (hint_continue(t
) == FALSE
)
5601 if (search_continue(t
) == FALSE
)
5604 /* if search length is > 4 then no longer play timeout games */
5605 if (strlen(c
) > 4) {
5607 g_source_remove(t
->search_id
);
5614 /* reestablish a new timer if the user types fast */
5616 g_source_remove(t
->search_id
);
5617 t
->search_id
= g_timeout_add(250, (GSourceFunc
)search_cb
, (gpointer
)t
);
5620 return (XT_CB_PASSTHROUGH
);
5624 match_uri(const gchar
*uri
, const gchar
*key
) {
5627 gboolean match
= FALSE
;
5631 if (!strncmp(key
, uri
, len
))
5634 voffset
= strstr(uri
, "/") + 2;
5635 if (!strncmp(key
, voffset
, len
))
5637 else if (g_str_has_prefix(voffset
, "www.")) {
5638 voffset
= voffset
+ strlen("www.");
5639 if (!strncmp(key
, voffset
, len
))
5648 match_session(const gchar
*name
, const gchar
*key
) {
5651 sub
= strcasestr(name
, key
);
5657 cmd_getlist(int id
, char *key
)
5664 if (cmds
[id
].type
& XT_URLARG
) {
5665 RB_FOREACH_REVERSE(h
, history_list
, &hl
)
5666 if (match_uri(h
->uri
, key
)) {
5667 cmd_status
.list
[c
] = (char *)h
->uri
;
5673 } else if (cmds
[id
].type
& XT_SESSARG
) {
5674 TAILQ_FOREACH(s
, &sessions
, entry
)
5675 if (match_session(s
->name
, key
)) {
5676 cmd_status
.list
[c
] = (char *)s
->name
;
5682 } else if (cmds
[id
].type
& XT_SETARG
) {
5683 for (i
= 0; i
< get_settings_size(); i
++)
5684 if (!strncmp(key
, get_setting_name(i
),
5686 cmd_status
.list
[c
++] =
5687 get_setting_name(i
);
5693 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
5695 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
5696 if (cmds
[i
].level
< dep
)
5698 if (cmds
[i
].level
== dep
&& !strncmp(key
, cmds
[i
].cmd
,
5699 strlen(key
)) && !isdigit(cmds
[i
].cmd
[0]))
5700 cmd_status
.list
[c
++] = cmds
[i
].cmd
;
5708 cmd_getnext(int dir
)
5710 cmd_status
.index
+= dir
;
5712 if (cmd_status
.index
< 0)
5713 cmd_status
.index
= cmd_status
.len
- 1;
5714 else if (cmd_status
.index
>= cmd_status
.len
)
5715 cmd_status
.index
= 0;
5717 return cmd_status
.list
[cmd_status
.index
];
5721 cmd_tokenize(char *s
, char *tokens
[])
5724 char *tok
, *last
= NULL
;
5725 size_t len
= strlen(s
);
5728 blank
= len
== 0 || (len
> 0 && s
[len
- 1] == ' ');
5729 for (tok
= strtok_r(s
, " ", &last
); tok
&& i
< 3;
5730 tok
= strtok_r(NULL
, " ", &last
), i
++)
5740 cmd_complete(struct tab
*t
, char *str
, int dir
)
5742 GtkEntry
*w
= GTK_ENTRY(t
->cmd
);
5743 int i
, j
, levels
, c
= 0, dep
= 0, parent
= -1;
5745 char *tok
, *match
, *s
= g_strdup(str
);
5747 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
5750 DNPRINTF(XT_D_CMD
, "%s: complete %s\n", __func__
, str
);
5753 for (i
= 0; isdigit(s
[i
]); i
++)
5756 for (; isspace(s
[i
]); i
++)
5761 levels
= cmd_tokenize(s
, tokens
);
5763 for (i
= 0; i
< levels
- 1; i
++) {
5766 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5767 if (cmds
[j
].level
< dep
)
5769 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
,
5773 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
5780 if (matchcount
== 1) {
5781 strlcat(res
, tok
, sizeof res
);
5782 strlcat(res
, " ", sizeof res
);
5792 if (cmd_status
.index
== -1)
5793 cmd_getlist(parent
, tokens
[i
]);
5795 if (cmd_status
.len
> 0) {
5796 match
= cmd_getnext(dir
);
5797 strlcat(res
, match
, sizeof res
);
5798 gtk_entry_set_text(w
, res
);
5799 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5806 cmd_execute(struct tab
*t
, char *str
)
5808 struct cmd
*cmd
= NULL
;
5809 char *tok
, *last
= NULL
, *s
= g_strdup(str
), *sc
;
5811 int j
, len
, c
= 0, dep
= 0, matchcount
= 0;
5812 int prefix
= -1, rv
= XT_CB_PASSTHROUGH
;
5813 struct karg arg
= {0, NULL
, -1};
5818 for (j
= 0; j
<3 && isdigit(s
[j
]); j
++)
5824 while (isspace(s
[0]))
5827 if (strlen(s
) > 0 && strlen(prefixstr
) > 0)
5828 prefix
= atoi(prefixstr
);
5832 for (tok
= strtok_r(s
, " ", &last
); tok
;
5833 tok
= strtok_r(NULL
, " ", &last
)) {
5835 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5836 if (cmds
[j
].level
< dep
)
5838 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1 :
5840 if (cmds
[j
].level
== dep
&&
5841 !strncmp(tok
, cmds
[j
].cmd
, len
)) {
5845 if (len
== strlen(cmds
[j
].cmd
)) {
5851 if (matchcount
== 1) {
5856 show_oops(t
, "Invalid command: %s", str
);
5862 show_oops(t
, "Empty command");
5868 arg
.precount
= prefix
;
5869 else if (cmd_prefix
> 0)
5870 arg
.precount
= cmd_prefix
;
5872 if (j
> 0 && !(cmd
->type
& XT_PREFIX
) && arg
.precount
> -1) {
5873 show_oops(t
, "No prefix allowed: %s", str
);
5877 arg
.s
= last
? g_strdup(last
) : g_strdup("");
5878 if (cmd
->type
& XT_INTARG
&& last
&& strlen(last
) > 0) {
5879 if (arg
.s
== NULL
) {
5880 show_oops(t
, "Invalid command");
5883 arg
.precount
= atoi(arg
.s
);
5884 if (arg
.precount
<= 0) {
5885 if (arg
.s
[0] == '0')
5886 show_oops(t
, "Zero count");
5888 show_oops(t
, "Trailing characters");
5893 DNPRINTF(XT_D_CMD
, "%s: prefix %d arg %s\n",
5894 __func__
, arg
.precount
, arg
.s
);
5910 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5913 show_oops(NULL
, "entry_key_cb invalid parameters");
5914 return (XT_CB_PASSTHROUGH
);
5917 DNPRINTF(XT_D_CMD
, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
5918 e
->keyval
, e
->state
, t
->tab_id
);
5922 if (e
->keyval
== GDK_Escape
) {
5923 /* don't use focus_webview(t) because we want to type :cmds */
5924 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5927 return (handle_keypress(t
, e
, 1));
5930 struct command_entry
*
5931 history_prev(struct command_list
*l
, struct command_entry
*at
)
5934 at
= TAILQ_LAST(l
, command_list
);
5936 at
= TAILQ_PREV(at
, command_list
, entry
);
5938 at
= TAILQ_LAST(l
, command_list
);
5944 struct command_entry
*
5945 history_next(struct command_list
*l
, struct command_entry
*at
)
5948 at
= TAILQ_FIRST(l
);
5950 at
= TAILQ_NEXT(at
, entry
);
5952 at
= TAILQ_FIRST(l
);
5959 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5961 int rv
= XT_CB_HANDLED
;
5962 const gchar
*c
= gtk_entry_get_text(w
);
5966 show_oops(NULL
, "cmd_keypress_cb parameters");
5967 return (XT_CB_PASSTHROUGH
);
5970 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
5971 e
->keyval
, e
->state
, t
->tab_id
);
5975 e
->keyval
= GDK_Escape
;
5976 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5977 c
[0] == '.' || c
[0] == ','))
5978 e
->keyval
= GDK_Escape
;
5980 if (e
->keyval
!= GDK_Tab
&& e
->keyval
!= GDK_Shift_L
&&
5981 e
->keyval
!= GDK_ISO_Left_Tab
)
5982 cmd_status
.index
= -1;
5984 switch (e
->keyval
) {
5987 cmd_complete(t
, (char *)&c
[1], 1);
5988 else if (c
[0] == '.' || c
[0] == ',')
5989 run_script(t
, "hints.focusNextHint();");
5991 case GDK_ISO_Left_Tab
:
5993 cmd_complete(t
, (char *)&c
[1], -1);
5994 else if (c
[0] == '.' || c
[0] == ',')
5995 run_script(t
, "hints.focusPreviousHint();");
5999 if ((search_at
= history_next(&shl
, search_at
))) {
6000 search_at
->line
[0] = c
[0];
6001 gtk_entry_set_text(w
, search_at
->line
);
6002 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6004 } else if (c
[0] == '/') {
6005 if ((search_at
= history_prev(&shl
, search_at
))) {
6006 search_at
->line
[0] = c
[0];
6007 gtk_entry_set_text(w
, search_at
->line
);
6008 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6010 } if (c
[0] == ':') {
6011 if ((history_at
= history_prev(&chl
, history_at
))) {
6012 history_at
->line
[0] = c
[0];
6013 gtk_entry_set_text(w
, history_at
->line
);
6014 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6020 if ((search_at
= history_next(&shl
, search_at
))) {
6021 search_at
->line
[0] = c
[0];
6022 gtk_entry_set_text(w
, search_at
->line
);
6023 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6025 } else if (c
[0] == '?') {
6026 if ((search_at
= history_prev(&shl
, search_at
))) {
6027 search_at
->line
[0] = c
[0];
6028 gtk_entry_set_text(w
, search_at
->line
);
6029 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6031 } if (c
[0] == ':') {
6032 if ((history_at
= history_next(&chl
, history_at
))) {
6033 history_at
->line
[0] = c
[0];
6034 gtk_entry_set_text(w
, history_at
->line
);
6035 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6040 if (!(!strcmp(c
, ":") || !strcmp(c
, "/") || !strcmp(c
, "?") ||
6041 !strcmp(c
, ".") || !strcmp(c
, ","))) {
6042 /* see if we are doing hinting and reset it */
6043 if (c
[0] == '.' || c
[0] == ',') {
6044 /* recreate hints */
6045 s
= g_strdup_printf("hints.createHints('', "
6046 "'%c');", c
[0] == '.' ? 'f' : 'F');
6059 if (c
!= NULL
&& (c
[0] == '/' || c
[0] == '?'))
6060 webkit_web_view_unmark_text_matches(t
->wv
);
6062 /* no need to cancel hints */
6066 rv
= XT_CB_PASSTHROUGH
;
6072 wv_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
6074 DNPRINTF(XT_D_CMD
, "wv_popup_cb: tab %d\n", t
->tab_id
);
6078 cmd_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
6080 /* popup menu enabled */
6085 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
6088 show_oops(NULL
, "cmd_focusout_cb invalid parameters");
6089 return (XT_CB_PASSTHROUGH
);
6092 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d popup %d\n",
6093 t
->tab_id
, t
->popup
);
6095 /* if popup is enabled don't lose focus */
6098 return (XT_CB_PASSTHROUGH
);
6105 if (show_url
== 0 || t
->focus_wv
)
6108 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6110 return (XT_CB_PASSTHROUGH
);
6114 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
6117 const gchar
*c
= gtk_entry_get_text(entry
);
6120 show_oops(NULL
, "cmd_activate_cb invalid parameters");
6124 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
6129 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
6130 c
[0] == '.' || c
[0] == ','))
6136 if (c
[0] == '/' || c
[0] == '?') {
6137 /* see if there is a timer pending */
6139 g_source_remove(t
->search_id
);
6144 if (t
->search_text
) {
6145 g_free(t
->search_text
);
6146 t
->search_text
= NULL
;
6149 t
->search_text
= g_strdup(s
);
6151 g_free(global_search
);
6152 global_search
= g_strdup(s
);
6153 t
->search_forward
= c
[0] == '/';
6155 history_add(&shl
, search_file
, s
, &search_history_count
);
6156 } else if (c
[0] == '.' || c
[0] == ',') {
6157 run_script(t
, "hints.fire();");
6158 /* XXX history for link following? */
6159 } else if (c
[0] == ':') {
6160 history_add(&chl
, command_file
, s
, &cmd_history_count
);
6161 /* can't call hide_cmd after cmd_execute */
6172 backward_cb(GtkWidget
*w
, struct tab
*t
)
6177 show_oops(NULL
, "backward_cb invalid parameters");
6181 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
6188 forward_cb(GtkWidget
*w
, struct tab
*t
)
6193 show_oops(NULL
, "forward_cb invalid parameters");
6197 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
6199 a
.i
= XT_NAV_FORWARD
;
6204 home_cb(GtkWidget
*w
, struct tab
*t
)
6207 show_oops(NULL
, "home_cb invalid parameters");
6211 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
6217 stop_cb(GtkWidget
*w
, struct tab
*t
)
6219 WebKitWebFrame
*frame
;
6222 show_oops(NULL
, "stop_cb invalid parameters");
6226 DNPRINTF(XT_D_NAV
, "stop_cb: tab %d\n", t
->tab_id
);
6228 frame
= webkit_web_view_get_main_frame(t
->wv
);
6229 if (frame
== NULL
) {
6230 show_oops(t
, "stop_cb: no frame");
6234 webkit_web_frame_stop_loading(frame
);
6235 abort_favicon_download(t
);
6239 setup_webkit(struct tab
*t
)
6241 if (is_g_object_setting(G_OBJECT(t
->settings
), "enable-dns-prefetching"))
6242 g_object_set(G_OBJECT(t
->settings
), "enable-dns-prefetching",
6243 FALSE
, (char *)NULL
);
6245 warnx("webkit does not have \"enable-dns-prefetching\" property");
6246 g_object_set(G_OBJECT(t
->settings
),
6247 "user-agent", t
->user_agent
, (char *)NULL
);
6248 g_object_set(G_OBJECT(t
->settings
),
6249 "enable-scripts", enable_scripts
, (char *)NULL
);
6250 g_object_set(G_OBJECT(t
->settings
),
6251 "enable-plugins", enable_plugins
, (char *)NULL
);
6252 g_object_set(G_OBJECT(t
->settings
),
6253 "javascript-can-open-windows-automatically", enable_scripts
,
6255 g_object_set(G_OBJECT(t
->settings
),
6256 "enable-html5-database", FALSE
, (char *)NULL
);
6257 g_object_set(G_OBJECT(t
->settings
),
6258 "enable-html5-local-storage", enable_localstorage
, (char *)NULL
);
6259 g_object_set(G_OBJECT(t
->settings
),
6260 "enable_spell_checking", enable_spell_checking
, (char *)NULL
);
6261 g_object_set(G_OBJECT(t
->settings
),
6262 "spell_checking_languages", spell_check_languages
, (char *)NULL
);
6263 g_object_set(G_OBJECT(t
->settings
),
6264 "enable-developer-extras", TRUE
, (char *)NULL
);
6265 g_object_set(G_OBJECT(t
->wv
),
6266 "full-content-zoom", TRUE
, (char *)NULL
);
6267 g_object_set(G_OBJECT(t
->settings
),
6268 "auto-load-images", auto_load_images
, (char *)NULL
);
6270 webkit_web_view_set_settings(t
->wv
, t
->settings
);
6274 update_statusbar_position(GtkAdjustment
* adjustment
, gpointer data
)
6276 struct tab
*ti
, *t
= NULL
;
6277 gdouble view_size
, value
, max
;
6280 TAILQ_FOREACH(ti
, &tabs
, entry
)
6281 if (ti
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
6289 if (adjustment
== NULL
)
6290 adjustment
= gtk_scrolled_window_get_vadjustment(
6291 GTK_SCROLLED_WINDOW(t
->browser_win
));
6293 view_size
= gtk_adjustment_get_page_size(adjustment
);
6294 value
= gtk_adjustment_get_value(adjustment
);
6295 max
= gtk_adjustment_get_upper(adjustment
) - view_size
;
6298 position
= g_strdup("All");
6299 else if (value
== max
)
6300 position
= g_strdup("Bot");
6301 else if (value
== 0)
6302 position
= g_strdup("Top");
6304 position
= g_strdup_printf("%d%%", (int) ((value
/ max
) * 100));
6306 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.position
), position
);
6313 create_window(const gchar
*name
)
6317 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
6318 if (window_maximize
)
6319 gtk_window_maximize(GTK_WINDOW(w
));
6321 gtk_window_set_default_size(GTK_WINDOW(w
), window_width
, window_height
);
6322 gtk_widget_set_name(w
, name
);
6323 gtk_window_set_wmclass(GTK_WINDOW(w
), name
, "XXXTerm");
6329 create_browser(struct tab
*t
)
6333 GtkAdjustment
*adjustment
;
6336 show_oops(NULL
, "create_browser invalid parameters");
6340 t
->sb_h
= GTK_SCROLLBAR(gtk_hscrollbar_new(NULL
));
6341 t
->sb_v
= GTK_SCROLLBAR(gtk_vscrollbar_new(NULL
));
6342 t
->adjust_h
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_h
));
6343 t
->adjust_v
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_v
));
6345 w
= gtk_scrolled_window_new(t
->adjust_h
, t
->adjust_v
);
6346 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
6347 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
6349 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
6350 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
6353 t
->settings
= webkit_web_settings_new();
6355 g_object_set(t
->settings
, "default-encoding", encoding
, (char *)NULL
);
6357 if (user_agent
== NULL
) {
6358 g_object_get(G_OBJECT(t
->settings
), "user-agent", &strval
,
6360 t
->user_agent
= g_strdup_printf("%s %s+", strval
, version
);
6363 t
->user_agent
= g_strdup(user_agent
->value
);
6365 t
->stylesheet
= g_strdup_printf("file://%s/style.css", resource_dir
);
6366 t
->load_images
= auto_load_images
;
6369 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w
));
6370 g_signal_connect(G_OBJECT(adjustment
), "value-changed",
6371 G_CALLBACK(update_statusbar_position
), NULL
);
6380 create_kiosk_toolbar(struct tab
*t
)
6382 GtkWidget
*toolbar
= NULL
, *b
;
6384 b
= gtk_hbox_new(FALSE
, 0);
6386 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
6388 /* backward button */
6389 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
6390 gtk_widget_set_sensitive(t
->backward
, FALSE
);
6391 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
6392 G_CALLBACK(backward_cb
), t
);
6393 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, TRUE
, TRUE
, 0);
6395 /* forward button */
6396 t
->forward
= create_button("Forward", GTK_STOCK_GO_FORWARD
, 0);
6397 gtk_widget_set_sensitive(t
->forward
, FALSE
);
6398 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
6399 G_CALLBACK(forward_cb
), t
);
6400 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, TRUE
, TRUE
, 0);
6403 t
->gohome
= create_button("Home", GTK_STOCK_HOME
, 0);
6404 gtk_widget_set_sensitive(t
->gohome
, true);
6405 g_signal_connect(G_OBJECT(t
->gohome
), "clicked",
6406 G_CALLBACK(home_cb
), t
);
6407 gtk_box_pack_start(GTK_BOX(b
), t
->gohome
, TRUE
, TRUE
, 0);
6409 /* create widgets but don't use them */
6410 t
->uri_entry
= gtk_entry_new();
6411 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
6412 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
6413 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
6414 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
6420 create_toolbar(struct tab
*t
)
6422 GtkWidget
*toolbar
= NULL
, *b
, *eb1
;
6424 b
= gtk_hbox_new(FALSE
, 0);
6426 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
6428 /* backward button */
6429 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
6430 gtk_widget_set_sensitive(t
->backward
, FALSE
);
6431 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
6432 G_CALLBACK(backward_cb
), t
);
6433 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, FALSE
, FALSE
, 0);
6435 /* forward button */
6436 t
->forward
= create_button("Forward",GTK_STOCK_GO_FORWARD
, 0);
6437 gtk_widget_set_sensitive(t
->forward
, FALSE
);
6438 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
6439 G_CALLBACK(forward_cb
), t
);
6440 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, FALSE
,
6444 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
6445 gtk_widget_set_sensitive(t
->stop
, FALSE
);
6446 g_signal_connect(G_OBJECT(t
->stop
), "clicked",
6447 G_CALLBACK(stop_cb
), t
);
6448 gtk_box_pack_start(GTK_BOX(b
), t
->stop
, FALSE
,
6452 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
6453 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
6454 gtk_widget_set_sensitive(t
->js_toggle
, TRUE
);
6455 g_signal_connect(G_OBJECT(t
->js_toggle
), "clicked",
6456 G_CALLBACK(js_toggle_cb
), t
);
6457 gtk_box_pack_start(GTK_BOX(b
), t
->js_toggle
, FALSE
, FALSE
, 0);
6459 t
->uri_entry
= gtk_entry_new();
6460 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
6461 G_CALLBACK(activate_uri_entry_cb
), t
);
6462 g_signal_connect(G_OBJECT(t
->uri_entry
), "key-press-event",
6463 G_CALLBACK(entry_key_cb
), t
);
6465 eb1
= gtk_hbox_new(FALSE
, 0);
6466 gtk_container_set_border_width(GTK_CONTAINER(eb1
), 1);
6467 gtk_box_pack_start(GTK_BOX(eb1
), t
->uri_entry
, TRUE
, TRUE
, 0);
6468 gtk_box_pack_start(GTK_BOX(b
), eb1
, TRUE
, TRUE
, 0);
6471 if (search_string
!= NULL
&& strlen(search_string
) != 0) {
6473 t
->search_entry
= gtk_entry_new();
6474 gtk_entry_set_width_chars(GTK_ENTRY(t
->search_entry
), 30);
6475 g_signal_connect(G_OBJECT(t
->search_entry
), "activate",
6476 G_CALLBACK(activate_search_entry_cb
), t
);
6477 g_signal_connect(G_OBJECT(t
->search_entry
), "key-press-event",
6478 G_CALLBACK(entry_key_cb
), t
);
6479 gtk_widget_set_size_request(t
->search_entry
, -1, -1);
6480 eb2
= gtk_hbox_new(FALSE
, 0);
6481 gtk_container_set_border_width(GTK_CONTAINER(eb2
), 1);
6482 gtk_box_pack_start(GTK_BOX(eb2
), t
->search_entry
, TRUE
, TRUE
,
6484 gtk_box_pack_start(GTK_BOX(b
), eb2
, FALSE
, FALSE
, 0);
6486 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
6492 create_buffers(struct tab
*t
)
6494 GtkCellRenderer
*renderer
;
6497 view
= gtk_tree_view_new();
6499 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view
), FALSE
);
6501 renderer
= gtk_cell_renderer_text_new();
6502 gtk_tree_view_insert_column_with_attributes
6503 (GTK_TREE_VIEW(view
), -1, "Id", renderer
, "text", COL_ID
, (char *)NULL
);
6505 renderer
= gtk_cell_renderer_pixbuf_new();
6506 gtk_tree_view_insert_column_with_attributes
6507 (GTK_TREE_VIEW(view
), -1, "Favicon", renderer
, "pixbuf", COL_FAVICON
,
6510 renderer
= gtk_cell_renderer_text_new();
6511 gtk_tree_view_insert_column_with_attributes
6512 (GTK_TREE_VIEW(view
), -1, "Title", renderer
, "text", COL_TITLE
,
6515 gtk_tree_view_set_model
6516 (GTK_TREE_VIEW(view
), GTK_TREE_MODEL(buffers_store
));
6522 row_activated_cb(GtkTreeView
*view
, GtkTreePath
*path
,
6523 GtkTreeViewColumn
*col
, struct tab
*t
)
6528 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6530 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
,
6533 (GTK_TREE_MODEL(buffers_store
), &iter
, COL_ID
, &id
, -1);
6534 set_current_tab(id
- 1);
6540 /* after tab reordering/creation/removal */
6547 TAILQ_FOREACH(t
, &tabs
, entry
) {
6548 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
6549 if (t
->tab_id
> maxid
)
6552 gtk_widget_show(t
->tab_elems
.sep
);
6555 TAILQ_FOREACH(t
, &tabs
, entry
) {
6556 if (t
->tab_id
== maxid
) {
6557 gtk_widget_hide(t
->tab_elems
.sep
);
6563 /* after active tab change */
6565 recolor_compact_tabs(void)
6571 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6572 TAILQ_FOREACH(t
, &tabs
, entry
)
6573 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
,
6576 curid
= gtk_notebook_get_current_page(notebook
);
6577 TAILQ_FOREACH(t
, &tabs
, entry
)
6578 if (t
->tab_id
== curid
) {
6579 gdk_color_parse(XT_COLOR_CT_ACTIVE
, &color
);
6580 gtk_widget_modify_fg(t
->tab_elems
.label
,
6581 GTK_STATE_NORMAL
, &color
);
6587 set_current_tab(int page_num
)
6589 buffercmd_abort(get_current_tab());
6590 gtk_notebook_set_current_page(notebook
, page_num
);
6591 recolor_compact_tabs();
6595 undo_close_tab_save(struct tab
*t
)
6599 struct undo
*u1
, *u2
;
6601 WebKitWebHistoryItem
*item
;
6603 if ((uri
= get_uri(t
)) == NULL
)
6606 u1
= g_malloc0(sizeof(struct undo
));
6607 u1
->uri
= g_strdup(uri
);
6609 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
6611 m
= webkit_web_back_forward_list_get_forward_length(t
->bfl
);
6612 n
= webkit_web_back_forward_list_get_back_length(t
->bfl
);
6615 /* forward history */
6616 items
= webkit_web_back_forward_list_get_forward_list_with_limit(t
->bfl
, m
);
6620 u1
->history
= g_list_prepend(u1
->history
,
6621 webkit_web_history_item_copy(item
));
6622 items
= g_list_next(items
);
6627 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
6628 u1
->history
= g_list_prepend(u1
->history
,
6629 webkit_web_history_item_copy(item
));
6633 items
= webkit_web_back_forward_list_get_back_list_with_limit(t
->bfl
, n
);
6637 u1
->history
= g_list_prepend(u1
->history
,
6638 webkit_web_history_item_copy(item
));
6639 items
= g_list_next(items
);
6642 TAILQ_INSERT_HEAD(&undos
, u1
, entry
);
6644 if (undo_count
> XT_MAX_UNDO_CLOSE_TAB
) {
6645 u2
= TAILQ_LAST(&undos
, undo_tailq
);
6646 TAILQ_REMOVE(&undos
, u2
, entry
);
6648 g_list_free(u2
->history
);
6657 delete_tab(struct tab
*t
)
6661 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
6667 * no need to join thread here because it won't access t on completion
6670 TAILQ_REMOVE(&tabs
, t
, entry
);
6673 /* Halt all webkit activity. */
6674 abort_favicon_download(t
);
6675 webkit_web_view_stop_loading(t
->wv
);
6677 /* Save the tab, so we can undo the close. */
6678 undo_close_tab_save(t
);
6682 g_source_remove(t
->search_id
);
6685 bzero(&a
, sizeof a
);
6687 inspector_cmd(t
, &a
);
6689 if (browser_mode
== XT_BM_KIOSK
) {
6690 gtk_widget_destroy(t
->uri_entry
);
6691 gtk_widget_destroy(t
->stop
);
6692 gtk_widget_destroy(t
->js_toggle
);
6695 gtk_widget_destroy(t
->tab_elems
.eventbox
);
6696 gtk_widget_destroy(t
->vbox
);
6698 g_free(t
->user_agent
);
6699 g_free(t
->stylesheet
);
6704 if (TAILQ_EMPTY(&tabs
)) {
6705 if (browser_mode
== XT_BM_KIOSK
)
6706 create_new_tab(home
, NULL
, 1, -1);
6708 create_new_tab(NULL
, NULL
, 1, -1);
6711 /* recreate session */
6712 if (session_autosave
) {
6713 bzero(&a
, sizeof a
);
6715 save_tabs(NULL
, &a
);
6719 recolor_compact_tabs();
6723 update_statusbar_zoom(struct tab
*t
)
6726 char s
[16] = { '\0' };
6728 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6729 if ((zoom
<= 0.99 || zoom
>= 1.01))
6730 snprintf(s
, sizeof s
, "%d%%", (int)(zoom
* 100));
6731 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.zoom
), s
);
6735 setzoom_webkit(struct tab
*t
, int adjust
)
6737 #define XT_ZOOMPERCENT 0.04
6742 show_oops(NULL
, "setzoom_webkit invalid parameters");
6746 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6747 if (adjust
== XT_ZOOM_IN
)
6748 zoom
+= XT_ZOOMPERCENT
;
6749 else if (adjust
== XT_ZOOM_OUT
)
6750 zoom
-= XT_ZOOMPERCENT
;
6751 else if (adjust
> 0)
6752 zoom
= default_zoom_level
+ adjust
/ 100.0 - 1.0;
6754 show_oops(t
, "setzoom_webkit invalid zoom value");
6758 if (zoom
< XT_ZOOMPERCENT
)
6759 zoom
= XT_ZOOMPERCENT
;
6760 g_object_set(G_OBJECT(t
->wv
), "zoom-level", zoom
, (char *)NULL
);
6761 update_statusbar_zoom(t
);
6765 tab_clicked_cb(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
6767 struct tab
*t
= (struct tab
*) data
;
6769 DNPRINTF(XT_D_TAB
, "tab_clicked_cb: tab: %d\n", t
->tab_id
);
6771 switch (event
->button
) {
6773 set_current_tab(t
->tab_id
);
6784 append_tab(struct tab
*t
)
6789 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
6790 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
6794 create_sbe(int width
)
6798 sbe
= gtk_entry_new();
6799 gtk_entry_set_inner_border(GTK_ENTRY(sbe
), NULL
);
6800 gtk_entry_set_has_frame(GTK_ENTRY(sbe
), FALSE
);
6801 gtk_widget_set_can_focus(GTK_WIDGET(sbe
), FALSE
);
6802 gtk_widget_modify_font(GTK_WIDGET(sbe
), statusbar_font
);
6803 gtk_entry_set_alignment(GTK_ENTRY(sbe
), 1.0);
6804 gtk_widget_set_size_request(sbe
, width
, -1);
6810 create_new_tab(char *title
, struct undo
*u
, int focus
, int position
)
6815 WebKitWebHistoryItem
*item
;
6819 int sbe_p
= 0, sbe_b
= 0,
6822 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
6824 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
6825 DNPRINTF(XT_D_TAB
, "create_new_tab: new tab rejected\n");
6829 t
= g_malloc0(sizeof *t
);
6831 if (title
== NULL
) {
6832 title
= "(untitled)";
6836 t
->vbox
= gtk_vbox_new(FALSE
, 0);
6838 /* label + button for tab */
6839 b
= gtk_hbox_new(FALSE
, 0);
6842 #if GTK_CHECK_VERSION(2, 20, 0)
6843 t
->spinner
= gtk_spinner_new();
6845 t
->label
= gtk_label_new(title
);
6846 bb
= create_button("Close", GTK_STOCK_CLOSE
, 1);
6847 gtk_widget_set_size_request(t
->label
, 100, 0);
6848 gtk_label_set_max_width_chars(GTK_LABEL(t
->label
), 20);
6849 gtk_label_set_ellipsize(GTK_LABEL(t
->label
), PANGO_ELLIPSIZE_END
);
6850 gtk_widget_set_size_request(b
, 130, 0);
6852 gtk_box_pack_start(GTK_BOX(b
), bb
, FALSE
, FALSE
, 0);
6853 gtk_box_pack_start(GTK_BOX(b
), t
->label
, FALSE
, FALSE
, 0);
6854 #if GTK_CHECK_VERSION(2, 20, 0)
6855 gtk_box_pack_start(GTK_BOX(b
), t
->spinner
, FALSE
, FALSE
, 0);
6859 if (browser_mode
== XT_BM_KIOSK
) {
6860 t
->toolbar
= create_kiosk_toolbar(t
);
6861 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
6864 t
->toolbar
= create_toolbar(t
);
6866 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
,
6874 t
->browser_win
= create_browser(t
);
6875 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
6877 /* oops message for user feedback */
6878 t
->oops
= gtk_entry_new();
6879 gtk_entry_set_inner_border(GTK_ENTRY(t
->oops
), NULL
);
6880 gtk_entry_set_has_frame(GTK_ENTRY(t
->oops
), FALSE
);
6881 gtk_widget_set_can_focus(GTK_WIDGET(t
->oops
), FALSE
);
6882 gdk_color_parse(XT_COLOR_RED
, &color
);
6883 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
6884 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
6885 gtk_widget_modify_font(GTK_WIDGET(t
->oops
), oops_font
);
6888 t
->cmd
= gtk_entry_new();
6889 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
6890 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
6891 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
6892 gtk_widget_modify_font(GTK_WIDGET(t
->cmd
), cmd_font
);
6895 t
->statusbar_box
= gtk_hbox_new(FALSE
, 0);
6897 t
->sbe
.statusbar
= gtk_entry_new();
6898 gtk_entry_set_inner_border(GTK_ENTRY(t
->sbe
.statusbar
), NULL
);
6899 gtk_entry_set_has_frame(GTK_ENTRY(t
->sbe
.statusbar
), FALSE
);
6900 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.statusbar
), FALSE
);
6901 gtk_widget_modify_font(GTK_WIDGET(t
->sbe
.statusbar
), statusbar_font
);
6903 /* create these widgets only if specified in statusbar_elems */
6905 t
->sbe
.position
= create_sbe(40);
6906 t
->sbe
.zoom
= create_sbe(40);
6907 t
->sbe
.buffercmd
= create_sbe(60);
6909 statusbar_modify_attr(t
, XT_COLOR_WHITE
, XT_COLOR_BLACK
);
6911 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), t
->sbe
.statusbar
, TRUE
,
6914 /* gtk widgets cannot be added to a box twice. sbe_* variables
6915 make sure of this */
6916 for (p
= statusbar_elems
; *p
!= '\0'; p
++) {
6920 GtkWidget
*sep
= gtk_vseparator_new();
6922 gdk_color_parse(XT_COLOR_SB_SEPARATOR
, &color
);
6923 gtk_widget_modify_bg(sep
, GTK_STATE_NORMAL
, &color
);
6924 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), sep
,
6925 FALSE
, FALSE
, FALSE
);
6930 warnx("flag \"%c\" specified more than "
6931 "once in statusbar_elems\n", *p
);
6935 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6936 t
->sbe
.position
, FALSE
, FALSE
, FALSE
);
6940 warnx("flag \"%c\" specified more than "
6941 "once in statusbar_elems\n", *p
);
6945 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6946 t
->sbe
.buffercmd
, FALSE
, FALSE
, FALSE
);
6950 warnx("flag \"%c\" specified more than "
6951 "once in statusbar_elems\n", *p
);
6955 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6956 t
->sbe
.zoom
, FALSE
, FALSE
, FALSE
);
6959 warnx("illegal flag \"%c\" in statusbar_elems\n", *p
);
6964 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar_box
, FALSE
, FALSE
, 0);
6967 t
->buffers
= create_buffers(t
);
6968 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->buffers
, FALSE
, FALSE
, 0);
6970 /* xtp meaning is normal by default */
6971 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
6973 /* set empty favicon */
6974 xt_icon_from_name(t
, "text-html");
6976 /* and show it all */
6977 gtk_widget_show_all(b
);
6978 gtk_widget_show_all(t
->vbox
);
6980 /* compact tab bar */
6981 t
->tab_elems
.label
= gtk_label_new(title
);
6982 t
->tab_elems
.favicon
= gtk_image_new();
6983 gtk_label_set_width_chars(GTK_LABEL(t
->tab_elems
.label
), 1.0);
6984 gtk_misc_set_alignment(GTK_MISC(t
->tab_elems
.label
), 0.0, 0.0);
6985 gtk_misc_set_padding(GTK_MISC(t
->tab_elems
.label
), 4.0, 4.0);
6986 gtk_widget_modify_font(GTK_WIDGET(t
->tab_elems
.label
), tabbar_font
);
6988 t
->tab_elems
.eventbox
= gtk_event_box_new();
6989 t
->tab_elems
.box
= gtk_hbox_new(FALSE
, 0);
6990 t
->tab_elems
.sep
= gtk_vseparator_new();
6992 gdk_color_parse(XT_COLOR_CT_BACKGROUND
, &color
);
6993 gtk_widget_modify_bg(t
->tab_elems
.eventbox
, GTK_STATE_NORMAL
, &color
);
6994 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6995 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
, &color
);
6996 gdk_color_parse(XT_COLOR_CT_SEPARATOR
, &color
);
6997 gtk_widget_modify_bg(t
->tab_elems
.sep
, GTK_STATE_NORMAL
, &color
);
6999 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.favicon
, FALSE
,
7001 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.label
, TRUE
,
7003 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.sep
, FALSE
,
7005 gtk_container_add(GTK_CONTAINER(t
->tab_elems
.eventbox
),
7008 gtk_box_pack_start(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
, TRUE
,
7010 gtk_widget_show_all(t
->tab_elems
.eventbox
);
7012 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
7015 id
= position
>= 0 ? position
:
7016 gtk_notebook_get_current_page(notebook
) + 1;
7017 if (id
> gtk_notebook_get_n_pages(notebook
))
7020 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
7021 gtk_notebook_insert_page(notebook
, t
->vbox
, b
, id
);
7022 gtk_box_reorder_child(GTK_BOX(tab_bar
),
7023 t
->tab_elems
.eventbox
, id
);
7028 #if GTK_CHECK_VERSION(2, 20, 0)
7029 /* turn spinner off if we are a new tab without uri */
7031 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
7032 gtk_widget_hide(t
->spinner
);
7035 /* make notebook tabs reorderable */
7036 gtk_notebook_set_tab_reorderable(notebook
, t
->vbox
, TRUE
);
7038 /* compact tabs clickable */
7039 g_signal_connect(G_OBJECT(t
->tab_elems
.eventbox
),
7040 "button_press_event", G_CALLBACK(tab_clicked_cb
), t
);
7042 g_object_connect(G_OBJECT(t
->cmd
),
7043 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb
), t
,
7044 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
7045 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb
), t
,
7046 "signal::activate", G_CALLBACK(cmd_activate_cb
), t
,
7047 "signal::populate-popup", G_CALLBACK(cmd_popup_cb
), t
,
7050 /* reuse wv_button_cb to hide oops */
7051 g_object_connect(G_OBJECT(t
->oops
),
7052 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7055 g_signal_connect(t
->buffers
,
7056 "row-activated", G_CALLBACK(row_activated_cb
), t
);
7057 g_object_connect(G_OBJECT(t
->buffers
),
7058 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
, (char *)NULL
);
7060 g_object_connect(G_OBJECT(t
->wv
),
7061 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
,
7062 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb
), t
,
7063 "signal::download-requested", G_CALLBACK(webview_download_cb
), t
,
7064 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), t
,
7065 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7066 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7067 "signal::resource-request-starting", G_CALLBACK(webview_rrs_cb
), t
,
7068 "signal::create-web-view", G_CALLBACK(webview_cwv_cb
), t
,
7069 "signal::close-web-view", G_CALLBACK(webview_closewv_cb
), t
,
7070 "signal::event", G_CALLBACK(webview_event_cb
), t
,
7071 "signal::load-finished", G_CALLBACK(webview_load_finished_cb
), t
,
7072 "signal::load-progress-changed", G_CALLBACK(webview_progress_changed_cb
), t
,
7073 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb
), t
,
7074 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7075 "signal::button_release_event", G_CALLBACK(wv_release_button_cb
), t
,
7076 "signal::populate-popup", G_CALLBACK(wv_popup_cb
), t
,
7078 g_signal_connect(t
->wv
,
7079 "notify::load-status", G_CALLBACK(notify_load_status_cb
), t
);
7080 g_signal_connect(t
->wv
,
7081 "notify::title", G_CALLBACK(notify_title_cb
), t
);
7083 /* hijack the unused keys as if we were the browser */
7084 //g_object_connect(G_OBJECT(t->toolbar),
7085 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
7088 g_signal_connect(G_OBJECT(bb
), "button_press_event",
7089 G_CALLBACK(tab_close_cb
), t
);
7092 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
7093 /* restore the tab's history */
7094 if (u
&& u
->history
) {
7098 webkit_web_back_forward_list_add_item(t
->bfl
, item
);
7099 items
= g_list_next(items
);
7102 item
= g_list_nth_data(u
->history
, u
->back
);
7104 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
7107 g_list_free(u
->history
);
7109 webkit_web_back_forward_list_clear(t
->bfl
);
7115 url_set_visibility();
7116 statusbar_set_visibility();
7119 set_current_tab(t
->tab_id
);
7120 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
7124 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
7128 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
7135 if (userstyle_global
)
7138 recolor_compact_tabs();
7139 setzoom_webkit(t
, XT_ZOOM_NORMAL
);
7144 notebook_switchpage_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7150 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
7152 if (gtk_notebook_get_current_page(notebook
) == -1)
7155 TAILQ_FOREACH(t
, &tabs
, entry
) {
7156 if (t
->tab_id
== pn
) {
7157 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
7160 uri
= get_title(t
, TRUE
);
7161 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
7167 /* can't use focus_webview here */
7168 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
7175 notebook_pagereordered_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7178 struct tab
*t
= NULL
, *tt
;
7182 TAILQ_FOREACH(tt
, &tabs
, entry
)
7183 if (tt
->tab_id
== pn
) {
7189 DNPRINTF(XT_D_TAB
, "page_reordered_cb: tab: %d\n", t
->tab_id
);
7191 gtk_box_reorder_child(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
,
7196 menuitem_response(struct tab
*t
)
7198 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
7202 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
7204 GtkWidget
*menu
, *menu_items
;
7205 GdkEventButton
*bevent
;
7209 if (event
->type
== GDK_BUTTON_PRESS
) {
7210 bevent
= (GdkEventButton
*) event
;
7211 menu
= gtk_menu_new();
7213 TAILQ_FOREACH(ti
, &tabs
, entry
) {
7214 if ((uri
= get_uri(ti
)) == NULL
)
7215 /* XXX make sure there is something to print */
7216 /* XXX add gui pages in here to look purdy */
7218 menu_items
= gtk_menu_item_new_with_label(uri
);
7219 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), menu_items
);
7220 gtk_widget_show(menu_items
);
7222 g_signal_connect_swapped((menu_items
),
7223 "activate", G_CALLBACK(menuitem_response
),
7227 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
,
7228 bevent
->button
, bevent
->time
);
7230 /* unref object so it'll free itself when popped down */
7231 #if !GTK_CHECK_VERSION(3, 0, 0)
7232 /* XXX does not need unref with gtk+3? */
7233 g_object_ref_sink(menu
);
7234 g_object_unref(menu
);
7237 return (TRUE
/* eat event */);
7240 return (FALSE
/* propagate */);
7244 icon_size_map(int iconsz
)
7246 if (iconsz
<= GTK_ICON_SIZE_INVALID
||
7247 iconsz
> GTK_ICON_SIZE_DIALOG
)
7248 return (GTK_ICON_SIZE_SMALL_TOOLBAR
);
7254 create_button(char *name
, char *stockid
, int size
)
7256 GtkWidget
*button
, *image
;
7260 rcstring
= g_strdup_printf(
7261 "style \"%s-style\"\n"
7263 " GtkWidget::focus-padding = 0\n"
7264 " GtkWidget::focus-line-width = 0\n"
7268 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
7269 gtk_rc_parse_string(rcstring
);
7271 button
= gtk_button_new();
7272 gtk_button_set_focus_on_click(GTK_BUTTON(button
), FALSE
);
7273 gtk_icon_size
= icon_size_map(size
? size
: icon_size
);
7275 image
= gtk_image_new_from_stock(stockid
, gtk_icon_size
);
7276 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
7277 gtk_container_set_border_width(GTK_CONTAINER(button
), 1);
7278 gtk_container_add(GTK_CONTAINER(button
), GTK_WIDGET(image
));
7279 gtk_widget_set_name(button
, name
);
7280 gtk_button_set_relief(GTK_BUTTON(button
), GTK_RELIEF_NONE
);
7286 button_set_stockid(GtkWidget
*button
, char *stockid
)
7290 image
= gtk_image_new_from_stock(stockid
, icon_size_map(icon_size
));
7291 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
7292 gtk_button_set_image(GTK_BUTTON(button
), image
);
7296 clipb_primary_cb(GtkClipboard
*primary
, GdkEvent
*event
, gpointer notused
)
7299 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
7302 if (xterm_workaround
== 0)
7306 * xterm doesn't play nice with clipboards because it clears the
7307 * primary when clicked. We rely on primary being set to properly
7308 * handle middle mouse button clicks (paste). So when someone clears
7309 * primary copy whatever is in CUT_BUFFER0 into primary to simualte
7310 * other application behavior (as in DON'T clear primary).
7313 p
= gtk_clipboard_wait_for_text(primary
);
7315 if (gdk_property_get(gdk_get_default_root_window(),
7317 gdk_atom_intern("STRING", FALSE
),
7319 1024 * 1024 /* picked out of my butt */,
7327 /* yes sir, we need to NUL the string */
7329 gtk_clipboard_set_text(primary
, p
, -1);
7343 char file
[PATH_MAX
];
7346 vbox
= gtk_vbox_new(FALSE
, 0);
7347 gtk_box_set_spacing(GTK_BOX(vbox
), 0);
7348 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
7349 #if !GTK_CHECK_VERSION(3, 0, 0)
7350 /* XXX seems to be needed with gtk+2 */
7351 gtk_notebook_set_tab_hborder(notebook
, 0);
7352 gtk_notebook_set_tab_vborder(notebook
, 0);
7354 gtk_notebook_set_scrollable(notebook
, TRUE
);
7355 gtk_notebook_set_show_border(notebook
, FALSE
);
7356 gtk_widget_set_can_focus(GTK_WIDGET(notebook
), FALSE
);
7358 abtn
= gtk_button_new();
7359 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
7360 gtk_widget_set_size_request(arrow
, -1, -1);
7361 gtk_container_add(GTK_CONTAINER(abtn
), arrow
);
7362 gtk_widget_set_size_request(abtn
, -1, 20);
7364 #if GTK_CHECK_VERSION(2, 20, 0)
7365 gtk_notebook_set_action_widget(notebook
, abtn
, GTK_PACK_END
);
7367 gtk_widget_set_size_request(GTK_WIDGET(notebook
), -1, -1);
7369 /* compact tab bar */
7370 tab_bar
= gtk_hbox_new(TRUE
, 0);
7372 gtk_box_pack_start(GTK_BOX(vbox
), tab_bar
, FALSE
, FALSE
, 0);
7373 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
7374 gtk_widget_set_size_request(vbox
, -1, -1);
7376 g_object_connect(G_OBJECT(notebook
),
7377 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb
), NULL
,
7379 g_object_connect(G_OBJECT(notebook
),
7380 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb
),
7381 NULL
, (char *)NULL
);
7382 g_signal_connect(G_OBJECT(abtn
), "button_press_event",
7383 G_CALLBACK(arrow_cb
), NULL
);
7385 main_window
= create_window("xxxterm");
7386 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
7387 g_signal_connect(G_OBJECT(main_window
), "delete_event",
7388 G_CALLBACK(gtk_main_quit
), NULL
);
7391 for (i
= 0; i
< LENGTH(icons
); i
++) {
7392 snprintf(file
, sizeof file
, "%s" PS
"%s", resource_dir
, icons
[i
]);
7393 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
7394 l
= g_list_append(l
, pb
);
7396 gtk_window_set_default_icon_list(l
);
7398 /* clipboard work around */
7399 if (xterm_workaround
)
7401 G_OBJECT(gtk_clipboard_get(GDK_SELECTION_PRIMARY
)),
7402 "owner-change", G_CALLBACK(clipb_primary_cb
), NULL
);
7404 gtk_widget_show_all(abtn
);
7405 gtk_widget_show_all(main_window
);
7406 notebook_tab_set_visibility();
7409 #ifndef XT_SOCKET_DISABLE
7411 send_cmd_to_socket(char *cmd
)
7414 struct sockaddr_un sa
;
7416 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7417 warnx("%s: socket", __func__
);
7421 sa
.sun_family
= AF_UNIX
;
7422 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
7423 work_dir
, XT_SOCKET_FILE
);
7426 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7427 warnx("%s: connect", __func__
);
7431 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
7432 warnx("%s: send", __func__
);
7443 socket_watcher(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
7446 char str
[XT_MAX_URL_LENGTH
];
7447 socklen_t t
= sizeof(struct sockaddr_un
);
7448 struct sockaddr_un sa
;
7453 gint fd
= g_io_channel_unix_get_fd(source
);
7455 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
7460 if (getpeereid(s
, &uid
, &gid
) == -1) {
7464 if (uid
!= getuid() || gid
!= getgid()) {
7465 warnx("unauthorized user");
7471 warnx("not a valid user");
7475 n
= recv(s
, str
, sizeof(str
), 0);
7479 tt
= TAILQ_LAST(&tabs
, tab_list
);
7480 cmd_execute(tt
, str
);
7488 struct sockaddr_un sa
;
7490 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7491 warn("is_running: socket");
7495 sa
.sun_family
= AF_UNIX
;
7496 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
7497 work_dir
, XT_SOCKET_FILE
);
7500 /* connect to see if there is a listener */
7501 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
7502 rv
= 0; /* not running */
7504 rv
= 1; /* already running */
7515 struct sockaddr_un sa
;
7517 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7518 warn("build_socket: socket");
7522 sa
.sun_family
= AF_UNIX
;
7523 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
7524 work_dir
, XT_SOCKET_FILE
);
7527 /* connect to see if there is a listener */
7528 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7529 /* no listener so we will */
7530 unlink(sa
.sun_path
);
7532 if (bind(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7533 warn("build_socket: bind");
7537 if (listen(s
, 1) == -1) {
7538 warn("build_socket: listen");
7556 if (stat(dir
, &sb
)) {
7557 #if defined __MINGW32__
7558 printf("making: %s\n", dir
);
7559 if (mkdir(dir
) == -1)
7561 if (mkdir(dir
, S_IRWXU
) == -1)
7563 err(1, "mkdir %s", dir
);
7565 err(1, "stat %s", dir
);
7567 if (S_ISDIR(sb
.st_mode
) == 0)
7568 errx(1, "%s not a dir", dir
);
7569 #if !defined __MINGW32__
7570 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
7571 warnx("fixing invalid permissions on %s", dir
);
7572 if (chmod(dir
, S_IRWXU
) == -1)
7573 err(1, "chmod %s", dir
);
7582 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
7586 GStaticRecMutex my_gdk_mtx
= G_STATIC_REC_MUTEX_INIT
;
7587 volatile int mtx_depth
;
7591 * The linux flash plugin violates the gdk locking mechanism.
7592 * Work around the issue by using a recursive mutex with some match applied
7593 * to see if we hit a buggy condition.
7595 * The following code is painful so just don't read it. It really doesn't
7596 * make much sense but seems to work.
7601 g_static_rec_mutex_lock(&my_gdk_mtx
);
7604 if (mtx_depth
<= 0) {
7605 /* should not happen */
7606 show_oops(NULL
, "negative mutex locking bug, trying to "
7608 fprintf(stderr
, "negative mutex locking bug, trying to "
7610 g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
7611 g_static_rec_mutex_lock(&my_gdk_mtx
);
7616 if (mtx_depth
!= 1) {
7617 /* decrease mutext depth to 1 */
7619 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7621 } while (mtx_depth
> 1);
7630 /* if mutex depth isn't 1 then something went bad */
7631 if (mtx_depth
!= 1) {
7632 x
= g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
7634 /* should not happen */
7635 show_oops(NULL
, "mutex unlocking bug, trying to "
7637 fprintf(stderr
, "mutex unlocking bug, trying to "
7641 if (mtx_complain
== 0) {
7642 show_oops(NULL
, "buggy mutex implementation detected, "
7643 "work around implemented");
7644 fprintf(stderr
, "buggy mutex implementation detected, "
7645 "work around implemented");
7652 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7658 startpage_add("<b>Welcome to xxxterm %s!</b><p>", version
);
7659 startpage_add("Beware that this is the final version that will use the"
7660 " xxxterm name.<br>Moving forward the browser will be called "
7661 "xombrero.<p>", version
);
7662 startpage_add("Details will soon appear on the "
7663 "<a href=https://opensource.conformal.com/wiki/xombrero>xombrero "
7664 "wiki page</a><p>");
7665 startpage_add("Unfortunately scroogle has shut it's doors and due to "
7666 "that one has to edit search_string in ~/.xxxterm.conf.<br>"
7667 "There are various examples in the configuration file.<br>"
7668 "The authors of xxxterm are not in a position to suggest a search "
7673 main(int argc
, char **argv
)
7676 int c
, optn
= 0, opte
= 0, focus
= 1;
7677 char conf
[PATH_MAX
] = { '\0' };
7678 char file
[PATH_MAX
];
7679 char sodversion
[32];
7680 char *env_proxy
= NULL
;
7684 start_argv
= (char * const *)argv
;
7688 g_thread_init(NULL
);
7689 gdk_threads_set_lock_functions(mtx_lock
, mtx_unlock
);
7691 gdk_threads_enter();
7693 gcry_control (GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
7695 gtk_init(&argc
, &argv
);
7697 gnutls_global_init();
7699 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
7704 RB_INIT(&downloads
);
7707 TAILQ_INIT(&sessions
);
7710 TAILQ_INIT(&aliases
);
7716 TAILQ_INIT(&ua_list
);
7718 #ifndef XT_RESOURCE_LIMITS_DISABLE
7722 GIOChannel
*channel
;
7724 /* fiddle with ulimits */
7725 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7728 /* just use them all */
7729 rlp
.rlim_cur
= rlp
.rlim_max
;
7730 if (setrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7732 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7734 else if (rlp
.rlim_cur
< 1024)
7735 startpage_add("%s requires at least 1024 "
7736 "(2048 recommended) file " "descriptors, "
7737 "currently it has up to %d available",
7738 __progname
, rlp
.rlim_cur
);
7742 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
7751 #ifdef XXXTERM_BUILDSTR
7752 errx(0 , "Version: %s Build: %s",
7753 version
, XXXTERM_BUILDSTR
);
7755 errx(0 , "Version: %s", version
);
7759 strlcpy(conf
, optarg
, sizeof(conf
));
7762 strlcpy(named_session
, optarg
, sizeof(named_session
));
7783 xtp_generate_keys();
7785 /* XXX this hammer is way too big but treat all signaling the same */
7786 #ifndef XT_SIGNALS_DISABLE
7788 struct sigaction sact
;
7790 bzero(&sact
, sizeof(sact
));
7791 sigemptyset(&sact
.sa_mask
);
7792 sact
.sa_handler
= sigchild
;
7793 sact
.sa_flags
= SA_NOCLDSTOP
;
7794 sigaction(SIGCHLD
, &sact
, NULL
);
7797 /* set download dir */
7798 pwd
= getpwuid(getuid());
7800 errx(1, "invalid user %d", getuid());
7801 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
7803 /* compile buffer command regexes */
7806 /* set default string settings */
7807 home
= g_strdup("https://www.cyphertite.com");
7808 search_string
= g_strdup("https://ssl.scroogle.org/cgi-bin/nbbwssl.cgi?Gw=%s");
7809 resource_dir
= g_strdup("/usr/local/share/xxxterm/");
7810 strlcpy(runtime_settings
, "runtime", sizeof runtime_settings
);
7811 cmd_font_name
= g_strdup("monospace normal 9");
7812 oops_font_name
= g_strdup("monospace normal 9");
7813 statusbar_font_name
= g_strdup("monospace normal 9");
7814 tabbar_font_name
= g_strdup("monospace normal 9");
7815 statusbar_elems
= g_strdup("BP");
7816 spell_check_languages
= g_strdup("en_US");
7817 encoding
= g_strdup("UTF-8");
7819 /* read config file */
7820 if (strlen(conf
) == 0)
7821 snprintf(conf
, sizeof conf
, "%s" PS
".%s",
7822 pwd
->pw_dir
, XT_CONF_FILE
);
7823 config_parse(conf
, 0);
7826 cmd_font
= pango_font_description_from_string(cmd_font_name
);
7827 oops_font
= pango_font_description_from_string(oops_font_name
);
7828 statusbar_font
= pango_font_description_from_string(statusbar_font_name
);
7829 tabbar_font
= pango_font_description_from_string(tabbar_font_name
);
7831 /* working directory */
7832 if (strlen(work_dir
) == 0)
7833 snprintf(work_dir
, sizeof work_dir
, "%s" PS
"%s",
7834 pwd
->pw_dir
, XT_DIR
);
7837 /* icon cache dir */
7838 snprintf(cache_dir
, sizeof cache_dir
, "%s" PS
"%s", work_dir
, XT_CACHE_DIR
);
7842 snprintf(certs_dir
, sizeof certs_dir
, "%s" PS
"%s", work_dir
, XT_CERT_DIR
);
7846 snprintf(sessions_dir
, sizeof sessions_dir
, "%s" PS
"%s",
7847 work_dir
, XT_SESSIONS_DIR
);
7848 xxx_dir(sessions_dir
);
7851 snprintf(js_dir
, sizeof js_dir
, "%s" PS
"%s", work_dir
, XT_JS_DIR
);
7855 snprintf(temp_dir
, sizeof temp_dir
, "%s" PS
"%s", work_dir
, XT_TEMP_DIR
);
7858 /* runtime settings that can override config file */
7859 if (runtime_settings
[0] != '\0')
7860 config_parse(runtime_settings
, 1);
7863 if (!strcmp(download_dir
, pwd
->pw_dir
))
7864 strlcat(download_dir
, PS
"downloads", sizeof download_dir
);
7865 xxx_dir(download_dir
);
7867 /* first start file */
7868 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_SOD_FILE
);
7869 if (stat(file
, &sb
)) {
7870 warnx("start of day file doesn't exist, creating it");
7871 if ((f
= fopen(file
, "w")) == NULL
)
7872 err(1, "startofday");
7873 if (fputs(version
, f
) == EOF
)
7880 if ((f
= fopen(file
, "r+")) == NULL
)
7881 err(1, "startofday");
7882 if (fgets(sodversion
, sizeof sodversion
, f
) == NULL
)
7884 sodversion
[strcspn(sodversion
, "\n")] = '\0';
7885 if (strcmp(version
, sodversion
)) {
7887 if (fputs(version
, f
) == EOF
)
7890 /* upgrade, say something smart */
7896 /* favorites file */
7897 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_FAVS_FILE
);
7898 if (stat(file
, &sb
)) {
7899 warnx("favorites file doesn't exist, creating it");
7900 if ((f
= fopen(file
, "w")) == NULL
)
7901 err(1, "favorites");
7905 /* quickmarks file */
7906 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
7907 if (stat(file
, &sb
)) {
7908 warnx("quickmarks file doesn't exist, creating it");
7909 if ((f
= fopen(file
, "w")) == NULL
)
7910 err(1, "quickmarks");
7914 /* search history */
7915 if (history_autosave
) {
7916 snprintf(search_file
, sizeof search_file
, "%s" PS
"%s",
7917 work_dir
, XT_SEARCH_FILE
);
7918 if (stat(search_file
, &sb
)) {
7919 warnx("search history file doesn't exist, creating it");
7920 if ((f
= fopen(search_file
, "w")) == NULL
)
7921 err(1, "search_history");
7924 history_read(&shl
, search_file
, &search_history_count
);
7927 /* command history */
7928 if (history_autosave
) {
7929 snprintf(command_file
, sizeof command_file
, "%s" PS
"%s",
7930 work_dir
, XT_COMMAND_FILE
);
7931 if (stat(command_file
, &sb
)) {
7932 warnx("command history file doesn't exist, creating it");
7933 if ((f
= fopen(command_file
, "w")) == NULL
)
7934 err(1, "command_history");
7937 history_read(&chl
, command_file
, &cmd_history_count
);
7941 session
= webkit_get_default_session();
7945 set_ssl_ca_file(ssl_ca_file
);
7947 /* guess_search regex */
7948 if (url_regex
== NULL
)
7949 url_regex
= g_strdup(XT_URL_REGEX
);
7951 if (regcomp(&url_re
, url_regex
, REG_EXTENDED
| REG_NOSUB
))
7952 startpage_add("invalid url regex %s", url_regex
);
7955 env_proxy
= getenv("http_proxy");
7957 setup_proxy(env_proxy
);
7959 env_proxy
= getenv("HTTP_PROXY");
7961 setup_proxy(env_proxy
);
7963 setup_proxy(http_proxy
);
7965 #ifndef XT_SOCKET_DISABLE
7967 send_cmd_to_socket(argv
[0]);
7971 opte
= opte
; /* shut mingw up */
7973 /* set some connection parameters */
7974 g_object_set(session
, "max-conns", max_connections
, (char *)NULL
);
7975 g_object_set(session
, "max-conns-per-host", max_host_connections
,
7978 g_signal_connect(session
, "request-queued", G_CALLBACK(session_rq_cb
),
7981 #ifndef XT_SOCKET_DISABLE
7982 /* see if there is already an xxxterm running */
7983 if (single_instance
&& is_running()) {
7985 warnx("already running");
7990 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
7991 send_cmd_to_socket(cmd
);
8001 optn
= optn
; /* shut mingw up */
8006 if (enable_strict_transport
)
8007 strict_transport_init();
8009 /* uri completion */
8010 completion_model
= gtk_list_store_new(1, G_TYPE_STRING
);
8013 buffers_store
= gtk_list_store_new
8014 (NUM_COLS
, G_TYPE_UINT
, GDK_TYPE_PIXBUF
, G_TYPE_STRING
);
8020 notebook_tab_set_visibility();
8022 if (save_global_history
)
8023 restore_global_history();
8025 /* restore session list */
8026 restore_sessions_list();
8028 if (!strcmp(named_session
, XT_SAVED_TABS_FILE
))
8029 restore_saved_tabs();
8031 a
.s
= named_session
;
8032 a
.i
= XT_SES_DONOTHING
;
8033 open_tabs(NULL
, &a
);
8036 /* see if we have an exception */
8037 if (!TAILQ_EMPTY(&spl
)) {
8038 create_new_tab("about:startpage", NULL
, focus
, -1);
8043 create_new_tab(argv
[0], NULL
, focus
, -1);
8050 if (TAILQ_EMPTY(&tabs
))
8051 create_new_tab(home
, NULL
, 1, -1);
8052 #ifndef XT_SOCKET_DISABLE
8054 if ((s
= build_socket()) != -1) {
8055 channel
= g_io_channel_unix_new(s
);
8056 g_io_add_watch(channel
, G_IO_IN
, socket_watcher
, NULL
);
8063 gdk_threads_leave();
8064 g_static_rec_mutex_unlock_full(&my_gdk_mtx
); /* just in case */
8067 gnutls_global_deinit();