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 run_script_locked(struct tab
*t
, char *s
)
1037 gdk_threads_enter();
1039 rv
= run_script(t
, s
);
1042 gdk_threads_leave();
1048 enable_hints(struct tab
*t
)
1050 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
1053 run_script(t
, "hints.createHints('', 'F');");
1055 run_script(t
, "hints.createHints('', 'f');");
1056 t
->mode
= XT_MODE_HINT
;
1060 disable_hints(struct tab
*t
)
1062 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
1064 run_script(t
, "hints.clearHints();");
1065 t
->mode
= XT_MODE_COMMAND
;
1070 passthrough(struct tab
*t
, struct karg
*args
)
1072 t
->mode
= XT_MODE_PASSTHROUGH
;
1077 modurl(struct tab
*t
, struct karg
*args
)
1079 const gchar
*uri
= NULL
;
1082 /* XXX kind of a bad hack, but oh well */
1083 if (GTK_WIDGET_HAS_FOCUS(t
->uri_entry
)) {
1084 if ((uri
= gtk_entry_get_text(GTK_ENTRY(t
->uri_entry
))) &&
1086 u
= g_strdup_printf("www.%s.com", uri
);
1087 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), u
);
1089 activate_uri_entry_cb(t
->uri_entry
, t
);
1096 hint(struct tab
*t
, struct karg
*args
)
1099 DNPRINTF(XT_D_JS
, "hint: tab %d args %d\n", t
->tab_id
, args
->i
);
1101 if (t
->mode
== XT_MODE_HINT
) {
1102 if (args
->i
== XT_HINT_NEWTAB
)
1112 apply_style(struct tab
*t
)
1115 g_object_set(G_OBJECT(t
->settings
),
1116 "user-stylesheet-uri", t
->stylesheet
, (char *)NULL
);
1120 remove_style(struct tab
*t
)
1123 g_object_set(G_OBJECT(t
->settings
),
1124 "user-stylesheet-uri", NULL
, (char *)NULL
);
1128 userstyle(struct tab
*t
, struct karg
*args
)
1132 DNPRINTF(XT_D_JS
, "userstyle: tab %d\n", t
->tab_id
);
1135 case XT_STYLE_CURRENT_TAB
:
1141 case XT_STYLE_GLOBAL
:
1142 if (userstyle_global
) {
1143 userstyle_global
= 0;
1144 TAILQ_FOREACH(tt
, &tabs
, entry
)
1147 userstyle_global
= 1;
1148 TAILQ_FOREACH(tt
, &tabs
, entry
)
1158 quit(struct tab
*t
, struct karg
*args
)
1160 if (save_global_history
)
1161 save_global_history_to_disk(t
);
1169 restore_sessions_list(void)
1172 struct dirent
*dp
= NULL
;
1176 sdir
= opendir(sessions_dir
);
1178 while ((dp
= readdir(sdir
)) != NULL
) {
1179 #if defined __MINGW32__
1180 reg
= 1; /* windows only has regular files */
1182 reg
= dp
->d_type
== DT_REG
;
1185 s
= g_malloc(sizeof(struct session
));
1186 s
->name
= g_strdup(dp
->d_name
);
1187 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
1195 open_tabs(struct tab
*t
, struct karg
*a
)
1197 char file
[PATH_MAX
];
1201 struct tab
*ti
, *tt
;
1206 ti
= TAILQ_LAST(&tabs
, tab_list
);
1208 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, a
->s
);
1209 if ((f
= fopen(file
, "r")) == NULL
)
1213 if ((uri
= fparseln(f
, NULL
, NULL
, "\0\0\0", 0)) == NULL
)
1214 if (feof(f
) || ferror(f
))
1217 /* retrieve session name */
1218 if (uri
&& g_str_has_prefix(uri
, XT_SAVE_SESSION_ID
)) {
1219 strlcpy(named_session
,
1220 &uri
[strlen(XT_SAVE_SESSION_ID
)],
1221 sizeof named_session
);
1225 if (uri
&& strlen(uri
))
1226 create_new_tab(uri
, NULL
, 1, -1);
1232 /* close open tabs */
1233 if (a
->i
== XT_SES_CLOSETABS
&& ti
!= NULL
) {
1235 tt
= TAILQ_FIRST(&tabs
);
1256 restore_saved_tabs(void)
1258 char file
[PATH_MAX
];
1259 int unlink_file
= 0;
1264 snprintf(file
, sizeof file
, "%s" PS
"%s",
1265 sessions_dir
, XT_RESTART_TABS_FILE
);
1266 if (stat(file
, &sb
) == -1)
1267 a
.s
= XT_SAVED_TABS_FILE
;
1270 a
.s
= XT_RESTART_TABS_FILE
;
1273 a
.i
= XT_SES_DONOTHING
;
1274 rv
= open_tabs(NULL
, &a
);
1283 save_tabs(struct tab
*t
, struct karg
*a
)
1285 char file
[PATH_MAX
];
1287 int num_tabs
= 0, i
;
1288 struct tab
**stabs
= NULL
;
1290 /* tab may be null here */
1295 snprintf(file
, sizeof file
, "%s" PS
"%s",
1296 sessions_dir
, named_session
);
1298 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, a
->s
);
1300 if ((f
= fopen(file
, "w")) == NULL
) {
1301 show_oops(t
, "Can't open save_tabs file: %s", strerror(errno
));
1305 /* save session name */
1306 fprintf(f
, "%s%s\n", XT_SAVE_SESSION_ID
, named_session
);
1308 /* Save tabs, in the order they are arranged in the notebook. */
1309 num_tabs
= sort_tabs_by_page_num(&stabs
);
1311 for (i
= 0; i
< num_tabs
; i
++)
1313 if (get_uri(stabs
[i
]) != NULL
)
1314 fprintf(f
, "%s\n", get_uri(stabs
[i
]));
1315 else if (gtk_entry_get_text(GTK_ENTRY(
1316 stabs
[i
]->uri_entry
)))
1317 fprintf(f
, "%s\n", gtk_entry_get_text(GTK_ENTRY(
1318 stabs
[i
]->uri_entry
)));
1323 /* try and make sure this gets to disk NOW. XXX Backup first? */
1324 if (fflush(f
) != 0 || fsync(fileno(f
)) != 0) {
1325 show_oops(t
, "May not have managed to save session: %s",
1335 save_tabs_and_quit(struct tab
*t
, struct karg
*args
)
1347 run_page_script(struct tab
*t
, struct karg
*args
)
1350 char *tmp
, script
[PATH_MAX
];
1352 tmp
= args
->s
!= NULL
&& strlen(args
->s
) > 0 ? args
->s
: default_script
;
1353 if (tmp
[0] == '\0') {
1354 show_oops(t
, "no script specified");
1358 if ((uri
= get_uri(t
)) == NULL
) {
1359 show_oops(t
, "tab is empty, not running script");
1364 snprintf(script
, sizeof script
, "%s" PS
"%s",
1365 pwd
->pw_dir
, &tmp
[1]);
1367 strlcpy(script
, tmp
, sizeof script
);
1369 return (fork_exec(t
, script
, uri
, "can't launch external script", 1));
1373 yank_uri(struct tab
*t
, struct karg
*args
)
1376 GtkClipboard
*clipboard
;
1378 if ((uri
= get_uri(t
)) == NULL
)
1381 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1382 gtk_clipboard_set_text(clipboard
, uri
, -1);
1388 paste_uri(struct tab
*t
, struct karg
*args
)
1390 GtkClipboard
*clipboard
;
1391 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
1393 gchar
*p
= NULL
, *uri
;
1395 /* try primary clipboard first */
1396 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1397 p
= gtk_clipboard_wait_for_text(clipboard
);
1399 /* if it failed get whatever text is in cut_buffer0 */
1400 if (p
== NULL
&& xterm_workaround
)
1401 if (gdk_property_get(gdk_get_default_root_window(),
1403 gdk_atom_intern("STRING", FALSE
),
1405 1024 * 1024 /* picked out of my butt */,
1413 /* yes sir, we need to NUL the string */
1419 while (*uri
&& isspace(*uri
))
1421 if (strlen(uri
) == 0) {
1422 show_oops(t
, "empty paste buffer");
1425 if (guess_search
== 0 && valid_url_type(uri
)) {
1426 /* we can be clever and paste this in search box */
1427 show_oops(t
, "not a valid URL");
1431 if (args
->i
== XT_PASTE_CURRENT_TAB
)
1433 else if (args
->i
== XT_PASTE_NEW_TAB
)
1434 create_new_tab(uri
, NULL
, 1, -1);
1445 js_toggle_cb(GtkWidget
*w
, struct tab
*t
)
1450 g_object_get(G_OBJECT(t
->settings
),
1451 "enable-scripts", &es
, (char *)NULL
);
1456 set
= XT_WL_DISABLE
;
1458 a
.i
= set
| XT_WL_TOPLEVEL
;
1461 a
.i
= set
| XT_WL_TOPLEVEL
;
1464 a
.i
= XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
;
1469 toggle_src(struct tab
*t
, struct karg
*args
)
1476 mode
= webkit_web_view_get_view_source_mode(t
->wv
);
1477 webkit_web_view_set_view_source_mode(t
->wv
, !mode
);
1478 webkit_web_view_reload(t
->wv
);
1484 focus_webview(struct tab
*t
)
1489 /* only grab focus if we are visible */
1490 if (gtk_notebook_get_current_page(notebook
) == t
->tab_id
)
1491 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
1495 focus(struct tab
*t
, struct karg
*args
)
1497 if (t
== NULL
|| args
== NULL
)
1503 if (args
->i
== XT_FOCUS_URI
)
1504 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
1505 else if (args
->i
== XT_FOCUS_SEARCH
)
1506 gtk_widget_grab_focus(GTK_WIDGET(t
->search_entry
));
1512 connect_socket_from_uri(const gchar
*uri
, const gchar
**error_str
, char *domain
,
1516 struct addrinfo hints
, *res
= NULL
, *ai
;
1517 int rv
= -1, s
= -1, on
, error
;
1519 static gchar myerror
[256]; /* this is not thread safe */
1522 *error_str
= myerror
;
1523 if (uri
&& !g_str_has_prefix(uri
, "https://")) {
1524 *error_str
= "invalid URI";
1528 su
= soup_uri_new(uri
);
1530 *error_str
= "invalid soup URI";
1533 if (!SOUP_URI_VALID_FOR_HTTP(su
)) {
1534 *error_str
= "invalid HTTPS URI";
1538 snprintf(port
, sizeof port
, "%d", su
->port
);
1539 bzero(&hints
, sizeof(struct addrinfo
));
1540 hints
.ai_flags
= AI_CANONNAME
;
1541 hints
.ai_family
= AF_UNSPEC
;
1542 hints
.ai_socktype
= SOCK_STREAM
;
1544 if ((error
= getaddrinfo(su
->host
, port
, &hints
, &res
))) {
1545 snprintf(myerror
, sizeof myerror
, "getaddrinfo failed: %s",
1546 gai_strerror(errno
));
1550 for (ai
= res
; ai
; ai
= ai
->ai_next
) {
1556 if (ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
)
1558 s
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
1561 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &on
,
1564 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) == 0)
1568 snprintf(myerror
, sizeof myerror
,
1569 "could not obtain certificates from: %s",
1575 strlcpy(domain
, su
->host
, domain_sz
);
1582 if (rv
== -1 && s
!= -1)
1590 custom_gnutls_push(void *s
, const void *buf
, size_t len
)
1592 return send((size_t)s
, buf
, len
, 0);
1596 custom_gnutls_pull(void *s
, void *buf
, size_t len
)
1598 return recv((size_t)s
, buf
, len
, 0);
1603 stop_tls(gnutls_session_t gsession
, gnutls_certificate_credentials_t xcred
)
1606 gnutls_deinit(gsession
);
1608 gnutls_certificate_free_credentials(xcred
);
1614 start_tls(const gchar
**error_str
, int s
, gnutls_session_t
*gs
,
1615 gnutls_certificate_credentials_t
*xc
)
1617 gnutls_certificate_credentials_t xcred
;
1618 gnutls_session_t gsession
;
1620 static gchar myerror
[1024]; /* this is not thread safe */
1622 if (gs
== NULL
|| xc
== NULL
)
1629 gnutls_certificate_allocate_credentials(&xcred
);
1630 gnutls_certificate_set_x509_trust_file(xcred
, ssl_ca_file
,
1631 GNUTLS_X509_FMT_PEM
);
1633 gnutls_init(&gsession
, GNUTLS_CLIENT
);
1634 gnutls_priority_set_direct(gsession
, "PERFORMANCE", NULL
);
1635 gnutls_credentials_set(gsession
, GNUTLS_CRD_CERTIFICATE
, xcred
);
1636 gnutls_transport_set_ptr(gsession
, (gnutls_transport_ptr_t
)(long)s
);
1638 /* sockets on windows don't use file descriptors */
1639 gnutls_transport_set_push_function(gsession
, custom_gnutls_push
);
1640 gnutls_transport_set_pull_function(gsession
, custom_gnutls_pull
);
1642 if ((rv
= gnutls_handshake(gsession
)) < 0) {
1643 snprintf(myerror
, sizeof myerror
,
1644 "gnutls_handshake failed %d fatal %d %s",
1646 gnutls_error_is_fatal(rv
),
1647 #if LIBGNUTLS_VERSION_MAJOR >= 2 && LIBGNUTLS_VERSION_MINOR >= 6
1648 gnutls_strerror_name(rv
));
1650 "GNUTLS version is too old to provide human readable error");
1652 stop_tls(gsession
, xcred
);
1656 gnutls_credentials_type_t cred
;
1657 cred
= gnutls_auth_get_type(gsession
);
1658 if (cred
!= GNUTLS_CRD_CERTIFICATE
) {
1659 snprintf(myerror
, sizeof myerror
,
1660 "gnutls_auth_get_type failed %d",
1662 stop_tls(gsession
, xcred
);
1670 *error_str
= myerror
;
1675 get_connection_certs(gnutls_session_t gsession
, gnutls_x509_crt_t
**certs
,
1679 const gnutls_datum_t
*cl
;
1680 gnutls_x509_crt_t
*all_certs
;
1683 if (certs
== NULL
|| cert_count
== NULL
)
1685 if (gnutls_certificate_type_get(gsession
) != GNUTLS_CRT_X509
)
1687 cl
= gnutls_certificate_get_peers(gsession
, &len
);
1691 all_certs
= g_malloc(sizeof(gnutls_x509_crt_t
) * len
);
1692 for (i
= 0; i
< len
; i
++) {
1693 gnutls_x509_crt_init(&all_certs
[i
]);
1694 if (gnutls_x509_crt_import(all_certs
[i
], &cl
[i
],
1695 GNUTLS_X509_FMT_PEM
< 0)) {
1709 free_connection_certs(gnutls_x509_crt_t
*certs
, size_t cert_count
)
1713 for (i
= 0; i
< cert_count
; i
++)
1714 gnutls_x509_crt_deinit(certs
[i
]);
1719 statusbar_modify_attr(struct tab
*t
, const char *text
, const char *base
)
1721 GdkColor c_text
, c_base
;
1723 gdk_color_parse(text
, &c_text
);
1724 gdk_color_parse(base
, &c_base
);
1726 gtk_widget_modify_text(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_text
);
1727 gtk_widget_modify_text(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_text
);
1728 gtk_widget_modify_text(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_text
);
1729 gtk_widget_modify_text(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_text
);
1731 gtk_widget_modify_base(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_base
);
1732 gtk_widget_modify_base(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_base
);
1733 gtk_widget_modify_base(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_base
);
1734 gtk_widget_modify_base(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_base
);
1738 save_certs(struct tab
*t
, gnutls_x509_crt_t
*certs
,
1739 size_t cert_count
, char *domain
)
1742 char cert_buf
[64 * 1024], file
[PATH_MAX
];
1747 if (t
== NULL
|| certs
== NULL
|| cert_count
<= 0 || domain
== NULL
)
1750 snprintf(file
, sizeof file
, "%s" PS
"%s", certs_dir
, domain
);
1751 if ((f
= fopen(file
, "w")) == NULL
) {
1752 show_oops(t
, "Can't create cert file %s %s",
1753 file
, strerror(errno
));
1757 for (i
= 0; i
< cert_count
; i
++) {
1758 cert_buf_sz
= sizeof cert_buf
;
1759 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1760 cert_buf
, &cert_buf_sz
)) {
1761 show_oops(t
, "gnutls_x509_crt_export failed");
1764 if (fwrite(cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1765 show_oops(t
, "Can't write certs: %s", strerror(errno
));
1770 /* not the best spot but oh well */
1771 gdk_color_parse(XT_COLOR_BLUE
, &color
);
1772 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
1773 statusbar_modify_attr(t
, XT_COLOR_BLACK
, XT_COLOR_BLUE
);
1786 load_compare_cert(const gchar
*uri
, const gchar
**error_str
)
1788 char domain
[8182], file
[PATH_MAX
];
1789 char cert_buf
[64 * 1024], r_cert_buf
[64 * 1024];
1791 unsigned int error
= 0;
1793 size_t cert_buf_sz
, cert_count
;
1794 enum cert_trust rv
= CERT_UNTRUSTED
;
1795 static gchar serr
[80]; /* this isn't thread safe */
1796 gnutls_session_t gsession
;
1797 gnutls_x509_crt_t
*certs
;
1798 gnutls_certificate_credentials_t xcred
;
1800 DNPRINTF(XT_D_URL
, "%s: %s\n", __func__
, uri
);
1804 if ((s
= connect_socket_from_uri(uri
, error_str
, domain
,
1805 sizeof domain
)) == -1)
1808 DNPRINTF(XT_D_URL
, "%s: fd %d\n", __func__
, s
);
1811 if (start_tls(error_str
, s
, &gsession
, &xcred
))
1813 DNPRINTF(XT_D_URL
, "%s: got tls\n", __func__
);
1815 /* verify certs in case cert file doesn't exist */
1816 if (gnutls_certificate_verify_peers2(gsession
, &error
) !=
1818 *error_str
= "Invalid certificates";
1823 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1824 *error_str
= "Can't get connection certificates";
1828 snprintf(file
, sizeof file
, "%s" PS
"%s", certs_dir
, domain
);
1829 if ((f
= fopen(file
, "r")) == NULL
) {
1835 for (i
= 0; i
< cert_count
; i
++) {
1836 cert_buf_sz
= sizeof cert_buf
;
1837 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1838 cert_buf
, &cert_buf_sz
)) {
1841 if (fread(r_cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1842 rv
= CERT_BAD
; /* critical */
1845 if (bcmp(r_cert_buf
, cert_buf
, sizeof cert_buf_sz
)) {
1846 rv
= CERT_BAD
; /* critical */
1855 free_connection_certs(certs
, cert_count
);
1857 /* we close the socket first for speed */
1861 /* only complain if we didn't save it locally */
1862 if (error
&& rv
!= CERT_LOCAL
) {
1863 strlcpy(serr
, "Certificate exception(s): ", sizeof serr
);
1864 if (error
& GNUTLS_CERT_INVALID
)
1865 strlcat(serr
, "invalid, ", sizeof serr
);
1866 if (error
& GNUTLS_CERT_REVOKED
)
1867 strlcat(serr
, "revoked, ", sizeof serr
);
1868 if (error
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
1869 strlcat(serr
, "signer not found, ", sizeof serr
);
1870 if (error
& GNUTLS_CERT_SIGNER_NOT_CA
)
1871 strlcat(serr
, "not signed by CA, ", sizeof serr
);
1872 if (error
& GNUTLS_CERT_INSECURE_ALGORITHM
)
1873 strlcat(serr
, "insecure algorithm, ", sizeof serr
);
1874 #if LIBGNUTLS_VERSION_MAJOR >= 2 && LIBGNUTLS_VERSION_MINOR >= 6
1875 if (error
& GNUTLS_CERT_NOT_ACTIVATED
)
1876 strlcat(serr
, "not activated, ", sizeof serr
);
1877 if (error
& GNUTLS_CERT_EXPIRED
)
1878 strlcat(serr
, "expired, ", sizeof serr
);
1880 for (i
= strlen(serr
) - 1; i
> 0; i
--)
1881 if (serr
[i
] == ',') {
1888 stop_tls(gsession
, xcred
);
1894 cert_cmd(struct tab
*t
, struct karg
*args
)
1896 const gchar
*uri
, *error_str
= NULL
;
1900 gnutls_session_t gsession
;
1901 gnutls_x509_crt_t
*certs
;
1902 gnutls_certificate_credentials_t xcred
;
1907 if (ssl_ca_file
== NULL
) {
1908 show_oops(t
, "Can't open CA file: %s", ssl_ca_file
);
1912 if ((uri
= get_uri(t
)) == NULL
) {
1913 show_oops(t
, "Invalid URI");
1917 if ((s
= connect_socket_from_uri(uri
, &error_str
, domain
,
1918 sizeof domain
)) == -1) {
1919 show_oops(t
, "%s", error_str
);
1924 if (start_tls(&error_str
, s
, &gsession
, &xcred
))
1928 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1929 show_oops(t
, "get_connection_certs failed");
1933 if (args
->i
& XT_SHOW
)
1934 show_certs(t
, certs
, cert_count
, "Certificate Chain");
1935 else if (args
->i
& XT_SAVE
)
1936 save_certs(t
, certs
, cert_count
, domain
);
1938 free_connection_certs(certs
, cert_count
);
1940 /* we close the socket first for speed */
1943 stop_tls(gsession
, xcred
);
1944 if (error_str
&& strlen(error_str
))
1945 show_oops(t
, "%s", error_str
);
1950 remove_cookie(int index
)
1956 DNPRINTF(XT_D_COOKIE
, "remove_cookie: %d\n", index
);
1958 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
1960 for (i
= 1; cf
; cf
= cf
->next
, i
++) {
1964 print_cookie("remove cookie", c
);
1965 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
1970 soup_cookies_free(cf
);
1976 remove_cookie_domain(int domain_id
)
1978 int domain_count
, rv
= 1;
1983 DNPRINTF(XT_D_COOKIE
, "remove_cookie_domain: %d\n", domain_id
);
1986 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
1988 for (domain_count
= 0; cf
; cf
= cf
->next
) {
1991 if (strcmp(last_domain
, c
->domain
) != 0) {
1993 last_domain
= c
->domain
;
1996 if (domain_count
< domain_id
)
1998 else if (domain_count
> domain_id
)
2001 print_cookie("remove cookie", c
);
2002 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2006 soup_cookies_free(cf
);
2018 DNPRINTF(XT_D_COOKIE
, "remove_cookie_all\n");
2020 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
2022 for (; cf
; cf
= cf
->next
) {
2025 print_cookie("remove cookie", c
);
2026 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2030 soup_cookies_free(cf
);
2036 toplevel_cmd(struct tab
*t
, struct karg
*args
)
2038 js_toggle_cb(t
->js_toggle
, t
);
2044 can_go_back_for_real(struct tab
*t
)
2047 WebKitWebHistoryItem
*item
;
2053 /* rely on webkit to make sure we can go backward when on an about page */
2055 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
2056 return (webkit_web_view_can_go_back(t
->wv
));
2058 /* the back/forwars list is stupid so help determine if we can go back */
2059 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2061 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2062 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
2070 can_go_forward_for_real(struct tab
*t
)
2073 WebKitWebHistoryItem
*item
;
2079 /* rely on webkit to make sure we can go forward when on an about page */
2081 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
2082 return (webkit_web_view_can_go_forward(t
->wv
));
2084 /* the back/forwars list is stupid so help selecting a different item */
2085 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2087 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2088 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
2096 go_back_for_real(struct tab
*t
)
2099 WebKitWebHistoryItem
*item
;
2107 webkit_web_view_go_back(t
->wv
);
2110 /* the back/forwars list is stupid so help selecting a different item */
2111 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2113 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2114 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2115 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2122 go_forward_for_real(struct tab
*t
)
2125 WebKitWebHistoryItem
*item
;
2133 webkit_web_view_go_forward(t
->wv
);
2136 /* the back/forwars list is stupid so help selecting a different item */
2137 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2139 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2140 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2141 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2148 navaction(struct tab
*t
, struct karg
*args
)
2150 WebKitWebHistoryItem
*item
;
2151 WebKitWebFrame
*frame
;
2153 DNPRINTF(XT_D_NAV
, "navaction: tab %d opcode %d\n",
2154 t
->tab_id
, args
->i
);
2156 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
2158 if (args
->i
== XT_NAV_BACK
)
2159 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2161 item
= webkit_web_back_forward_list_get_forward_item(t
->bfl
);
2163 return (XT_CB_PASSTHROUGH
);
2164 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2166 return (XT_CB_PASSTHROUGH
);
2172 go_back_for_real(t
);
2174 case XT_NAV_FORWARD
:
2176 go_forward_for_real(t
);
2179 frame
= webkit_web_view_get_main_frame(t
->wv
);
2180 webkit_web_frame_reload(frame
);
2183 frame
= webkit_web_view_get_main_frame(t
->wv
);
2184 webkit_web_frame_stop_loading(frame
);
2187 return (XT_CB_PASSTHROUGH
);
2191 move(struct tab
*t
, struct karg
*args
)
2193 GtkAdjustment
*adjust
;
2194 double pi
, si
, pos
, ps
, upper
, lower
, max
;
2200 case XT_MOVE_BOTTOM
:
2202 case XT_MOVE_PAGEDOWN
:
2203 case XT_MOVE_PAGEUP
:
2204 case XT_MOVE_HALFDOWN
:
2205 case XT_MOVE_HALFUP
:
2206 case XT_MOVE_PERCENT
:
2207 case XT_MOVE_CENTER
:
2208 adjust
= t
->adjust_v
;
2211 adjust
= t
->adjust_h
;
2215 pos
= gtk_adjustment_get_value(adjust
);
2216 ps
= gtk_adjustment_get_page_size(adjust
);
2217 upper
= gtk_adjustment_get_upper(adjust
);
2218 lower
= gtk_adjustment_get_lower(adjust
);
2219 si
= gtk_adjustment_get_step_increment(adjust
);
2220 pi
= gtk_adjustment_get_page_increment(adjust
);
2223 DNPRINTF(XT_D_MOVE
, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2224 "max %f si %f pi %f\n",
2225 args
->i
, adjust
== t
->adjust_h
? "horizontal" : "vertical",
2226 pos
, ps
, upper
, lower
, max
, si
, pi
);
2232 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2237 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2239 case XT_MOVE_BOTTOM
:
2240 case XT_MOVE_FARRIGHT
:
2241 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2242 gtk_adjustment_set_value(adjust
, max
);
2245 case XT_MOVE_FARLEFT
:
2246 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2247 gtk_adjustment_set_value(adjust
, lower
);
2249 case XT_MOVE_PAGEDOWN
:
2251 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2253 case XT_MOVE_PAGEUP
:
2255 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2257 case XT_MOVE_HALFDOWN
:
2259 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2261 case XT_MOVE_HALFUP
:
2263 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2265 case XT_MOVE_CENTER
:
2266 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2267 args
->s
= g_strdup("50.0");
2269 case XT_MOVE_PERCENT
:
2270 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2271 percent
= atoi(args
->s
) / 100.0;
2272 pos
= max
* percent
;
2273 if (pos
< 0.0 || pos
> max
)
2275 gtk_adjustment_set_value(adjust
, pos
);
2278 return (XT_CB_PASSTHROUGH
);
2281 DNPRINTF(XT_D_MOVE
, "move: new pos %f %f\n", pos
, MIN(pos
, max
));
2283 return (XT_CB_HANDLED
);
2287 url_set_visibility(void)
2291 TAILQ_FOREACH(t
, &tabs
, entry
)
2292 if (show_url
== 0) {
2293 gtk_widget_hide(t
->toolbar
);
2296 gtk_widget_show(t
->toolbar
);
2300 notebook_tab_set_visibility(void)
2302 if (show_tabs
== 0) {
2303 gtk_widget_hide(tab_bar
);
2304 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2306 if (tab_style
== XT_TABS_NORMAL
) {
2307 gtk_widget_hide(tab_bar
);
2308 gtk_notebook_set_show_tabs(notebook
, TRUE
);
2309 } else if (tab_style
== XT_TABS_COMPACT
) {
2310 gtk_widget_show(tab_bar
);
2311 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2317 statusbar_set_visibility(void)
2321 TAILQ_FOREACH(t
, &tabs
, entry
){
2322 if (show_statusbar
== 0)
2323 gtk_widget_hide(t
->statusbar_box
);
2325 gtk_widget_show(t
->statusbar_box
);
2332 url_set(struct tab
*t
, int enable_url_entry
)
2337 show_url
= enable_url_entry
;
2339 if (enable_url_entry
) {
2340 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
2341 GTK_ENTRY_ICON_PRIMARY
, NULL
);
2342 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
), 0);
2344 pixbuf
= gtk_entry_get_icon_pixbuf(GTK_ENTRY(t
->uri_entry
),
2345 GTK_ENTRY_ICON_PRIMARY
);
2347 gtk_entry_get_progress_fraction(GTK_ENTRY(t
->uri_entry
));
2348 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
2349 GTK_ENTRY_ICON_PRIMARY
, pixbuf
);
2350 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
2356 fullscreen(struct tab
*t
, struct karg
*args
)
2358 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2361 return (XT_CB_PASSTHROUGH
);
2363 if (show_url
== 0) {
2371 url_set_visibility();
2372 notebook_tab_set_visibility();
2374 return (XT_CB_HANDLED
);
2378 statustoggle(struct tab
*t
, struct karg
*args
)
2380 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2382 if (show_statusbar
== 1) {
2384 statusbar_set_visibility();
2385 } else if (show_statusbar
== 0) {
2387 statusbar_set_visibility();
2389 return (XT_CB_HANDLED
);
2393 urlaction(struct tab
*t
, struct karg
*args
)
2395 int rv
= XT_CB_HANDLED
;
2397 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2400 return (XT_CB_PASSTHROUGH
);
2404 if (show_url
== 0) {
2406 url_set_visibility();
2410 if (show_url
== 1) {
2412 url_set_visibility();
2420 tabaction(struct tab
*t
, struct karg
*args
)
2422 int rv
= XT_CB_HANDLED
;
2423 char *url
= args
->s
;
2427 DNPRINTF(XT_D_TAB
, "tabaction: %p %d\n", t
, args
->i
);
2430 return (XT_CB_PASSTHROUGH
);
2434 if (strlen(url
) > 0)
2435 create_new_tab(url
, NULL
, 1, args
->precount
);
2437 create_new_tab(NULL
, NULL
, 1, args
->precount
);
2440 if (args
->precount
< 0)
2443 TAILQ_FOREACH(tt
, &tabs
, entry
)
2444 if (tt
->tab_id
== args
->precount
- 1) {
2449 case XT_TAB_DELQUIT
:
2450 if (gtk_notebook_get_n_pages(notebook
) > 1)
2456 if (strlen(url
) > 0)
2459 rv
= XT_CB_PASSTHROUGH
;
2465 if (show_tabs
== 0) {
2467 notebook_tab_set_visibility();
2471 if (show_tabs
== 1) {
2473 notebook_tab_set_visibility();
2476 case XT_TAB_NEXTSTYLE
:
2477 if (tab_style
== XT_TABS_NORMAL
) {
2478 tab_style
= XT_TABS_COMPACT
;
2479 recolor_compact_tabs();
2482 tab_style
= XT_TABS_NORMAL
;
2483 notebook_tab_set_visibility();
2485 case XT_TAB_UNDO_CLOSE
:
2486 if (undo_count
== 0) {
2487 DNPRINTF(XT_D_TAB
, "%s: no tabs to undo close",
2492 u
= TAILQ_FIRST(&undos
);
2493 create_new_tab(u
->uri
, u
, 1, -1);
2495 TAILQ_REMOVE(&undos
, u
, entry
);
2497 /* u->history is freed in create_new_tab() */
2501 case XT_TAB_LOAD_IMAGES
:
2503 if (!auto_load_images
) {
2505 /* Enable auto-load images (this will load all
2506 * previously unloaded images). */
2507 g_object_set(G_OBJECT(t
->settings
),
2508 "auto-load-images", TRUE
, (char *)NULL
);
2509 webkit_web_view_set_settings(t
->wv
, t
->settings
);
2511 webkit_web_view_reload(t
->wv
);
2513 /* Webkit triggers an event when we change the setting,
2514 * so we can't disable the auto-loading at once.
2516 * Unfortunately, webkit does not tell us when it's done.
2517 * Instead, we wait until the next request, and then
2518 * disable autoloading again.
2520 t
->load_images
= TRUE
;
2524 rv
= XT_CB_PASSTHROUGH
;
2538 resizetab(struct tab
*t
, struct karg
*args
)
2540 if (t
== NULL
|| args
== NULL
) {
2541 show_oops(NULL
, "resizetab invalid parameters");
2542 return (XT_CB_PASSTHROUGH
);
2545 DNPRINTF(XT_D_TAB
, "resizetab: tab %d %d\n",
2546 t
->tab_id
, args
->i
);
2548 setzoom_webkit(t
, args
->i
);
2550 return (XT_CB_HANDLED
);
2554 movetab(struct tab
*t
, struct karg
*args
)
2558 if (t
== NULL
|| args
== NULL
) {
2559 show_oops(NULL
, "movetab invalid parameters");
2560 return (XT_CB_PASSTHROUGH
);
2563 DNPRINTF(XT_D_TAB
, "movetab: tab %d opcode %d\n",
2564 t
->tab_id
, args
->i
);
2566 if (args
->i
>= XT_TAB_INVALID
)
2567 return (XT_CB_PASSTHROUGH
);
2569 if (TAILQ_EMPTY(&tabs
))
2570 return (XT_CB_PASSTHROUGH
);
2572 n
= gtk_notebook_get_n_pages(notebook
);
2573 dest
= gtk_notebook_get_current_page(notebook
);
2577 if (args
->precount
< 0)
2578 dest
= dest
== n
- 1 ? 0 : dest
+ 1;
2580 dest
= args
->precount
- 1;
2584 if (args
->precount
< 0)
2587 dest
-= args
->precount
% n
;
2600 return (XT_CB_PASSTHROUGH
);
2603 if (dest
< 0 || dest
>= n
)
2604 return (XT_CB_PASSTHROUGH
);
2605 if (t
->tab_id
== dest
) {
2606 DNPRINTF(XT_D_TAB
, "movetab: do nothing\n");
2607 return (XT_CB_HANDLED
);
2610 set_current_tab(dest
);
2612 return (XT_CB_HANDLED
);
2619 command(struct tab
*t
, struct karg
*args
)
2621 char *s
= NULL
, *ss
= NULL
;
2626 if (t
== NULL
|| args
== NULL
) {
2627 show_oops(NULL
, "command invalid parameters");
2628 return (XT_CB_PASSTHROUGH
);
2639 if (cmd_prefix
== 0)
2642 ss
= g_strdup_printf(":%d", cmd_prefix
);
2648 t
->mode
= XT_MODE_HINT
;
2649 bzero(&a
, sizeof a
);
2655 t
->mode
= XT_MODE_HINT
;
2656 bzero(&a
, sizeof a
);
2657 a
.i
= XT_HINT_NEWTAB
;
2667 case XT_CMD_OPEN_CURRENT
:
2670 case XT_CMD_TABNEW_CURRENT
:
2671 if (!s
) /* FALL THROUGH? */
2673 if ((uri
= get_uri(t
)) != NULL
) {
2674 ss
= g_strdup_printf("%s%s", s
, uri
);
2679 show_oops(t
, "command: invalid opcode %d", args
->i
);
2680 return (XT_CB_PASSTHROUGH
);
2683 DNPRINTF(XT_D_CMD
, "%s: tab %d type %s\n", __func__
, t
->tab_id
, s
);
2685 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), s
);
2686 text
= gdk_color_to_string(&t
->default_style
->text
[GTK_STATE_NORMAL
]);
2687 base
= gdk_color_to_string(&t
->default_style
->base
[GTK_STATE_NORMAL
]);
2688 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
2689 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
2690 statusbar_modify_attr(t
, text
, base
);
2694 gtk_widget_grab_focus(GTK_WIDGET(t
->cmd
));
2695 gtk_editable_set_position(GTK_EDITABLE(t
->cmd
), -1);
2700 return (XT_CB_HANDLED
);
2704 search(struct tab
*t
, struct karg
*args
)
2708 if (t
== NULL
|| args
== NULL
) {
2709 show_oops(NULL
, "search invalid parameters");
2714 case XT_SEARCH_NEXT
:
2715 d
= t
->search_forward
;
2717 case XT_SEARCH_PREV
:
2718 d
= !t
->search_forward
;
2721 return (XT_CB_PASSTHROUGH
);
2724 if (t
->search_text
== NULL
) {
2725 if (global_search
== NULL
)
2726 return (XT_CB_PASSTHROUGH
);
2728 d
= t
->search_forward
= TRUE
;
2729 t
->search_text
= g_strdup(global_search
);
2730 webkit_web_view_mark_text_matches(t
->wv
, global_search
, FALSE
, 0);
2731 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
2735 DNPRINTF(XT_D_CMD
, "search: tab %d opc %d forw %d text %s\n",
2736 t
->tab_id
, args
->i
, t
->search_forward
, t
->search_text
);
2738 webkit_web_view_search_text(t
->wv
, t
->search_text
, FALSE
, d
, TRUE
);
2740 return (XT_CB_HANDLED
);
2744 session_save(struct tab
*t
, char *filename
)
2750 if (strlen(filename
) == 0)
2753 if (filename
[0] == '.' || filename
[0] == '/')
2757 if (save_tabs(t
, &a
))
2759 strlcpy(named_session
, filename
, sizeof named_session
);
2761 /* add the new session to the list of sessions */
2762 s
= g_malloc(sizeof(struct session
));
2763 s
->name
= g_strdup(filename
);
2764 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
2772 session_open(struct tab
*t
, char *filename
)
2777 if (strlen(filename
) == 0)
2780 if (filename
[0] == '.' || filename
[0] == '/')
2784 a
.i
= XT_SES_CLOSETABS
;
2785 if (open_tabs(t
, &a
))
2788 strlcpy(named_session
, filename
, sizeof named_session
);
2796 session_delete(struct tab
*t
, char *filename
)
2798 char file
[PATH_MAX
];
2802 if (strlen(filename
) == 0)
2805 if (filename
[0] == '.' || filename
[0] == '/')
2808 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, filename
);
2812 if (!strcmp(filename
, named_session
))
2813 strlcpy(named_session
, XT_SAVED_TABS_FILE
,
2814 sizeof named_session
);
2816 /* remove session from sessions list */
2817 TAILQ_FOREACH(s
, &sessions
, entry
) {
2818 if (!strcmp(s
->name
, filename
))
2823 TAILQ_REMOVE(&sessions
, s
, entry
);
2824 g_free((gpointer
) s
->name
);
2833 session_cmd(struct tab
*t
, struct karg
*args
)
2835 char *filename
= args
->s
;
2840 if (args
->i
& XT_SHOW
)
2841 show_oops(t
, "Current session: %s", named_session
[0] == '\0' ?
2842 XT_SAVED_TABS_FILE
: named_session
);
2843 else if (args
->i
& XT_SAVE
) {
2844 if (session_save(t
, filename
)) {
2845 show_oops(t
, "Can't save session: %s",
2846 filename
? filename
: "INVALID");
2849 } else if (args
->i
& XT_OPEN
) {
2850 if (session_open(t
, filename
)) {
2851 show_oops(t
, "Can't open session: %s",
2852 filename
? filename
: "INVALID");
2855 } else if (args
->i
& XT_DELETE
) {
2856 if (session_delete(t
, filename
)) {
2857 show_oops(t
, "Can't delete session: %s",
2858 filename
? filename
: "INVALID");
2863 return (XT_CB_PASSTHROUGH
);
2867 script_cmd(struct tab
*t
, struct karg
*args
)
2876 if ((f
= fopen(args
->s
, "r")) == NULL
) {
2877 show_oops(t
, "Can't open script file: %s", args
->s
);
2881 if (fstat(fileno(f
), &sb
) == -1) {
2882 show_oops(t
, "Can't stat script file: %s", args
->s
);
2886 buf
= g_malloc0(sb
.st_size
+ 1);
2887 if (fread(buf
, 1, sb
.st_size
, f
) != sb
.st_size
) {
2888 show_oops(t
, "Can't read script file: %s", args
->s
);
2892 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
2901 return (XT_CB_PASSTHROUGH
);
2905 * Make a hardcopy of the page
2908 print_page(struct tab
*t
, struct karg
*args
)
2910 WebKitWebFrame
*frame
;
2912 GtkPrintOperation
*op
;
2913 GtkPrintOperationAction action
;
2914 GtkPrintOperationResult print_res
;
2915 GError
*g_err
= NULL
;
2916 int marg_l
, marg_r
, marg_t
, marg_b
;
2918 DNPRINTF(XT_D_PRINTING
, "%s:", __func__
);
2920 ps
= gtk_page_setup_new();
2921 op
= gtk_print_operation_new();
2922 action
= GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
;
2923 frame
= webkit_web_view_get_main_frame(t
->wv
);
2925 /* the default margins are too small, so we will bump them */
2926 marg_l
= gtk_page_setup_get_left_margin(ps
, GTK_UNIT_MM
) +
2927 XT_PRINT_EXTRA_MARGIN
;
2928 marg_r
= gtk_page_setup_get_right_margin(ps
, GTK_UNIT_MM
) +
2929 XT_PRINT_EXTRA_MARGIN
;
2930 marg_t
= gtk_page_setup_get_top_margin(ps
, GTK_UNIT_MM
) +
2931 XT_PRINT_EXTRA_MARGIN
;
2932 marg_b
= gtk_page_setup_get_bottom_margin(ps
, GTK_UNIT_MM
) +
2933 XT_PRINT_EXTRA_MARGIN
;
2936 gtk_page_setup_set_left_margin(ps
, marg_l
, GTK_UNIT_MM
);
2937 gtk_page_setup_set_right_margin(ps
, marg_r
, GTK_UNIT_MM
);
2938 gtk_page_setup_set_top_margin(ps
, marg_t
, GTK_UNIT_MM
);
2939 gtk_page_setup_set_bottom_margin(ps
, marg_b
, GTK_UNIT_MM
);
2941 gtk_print_operation_set_default_page_setup(op
, ps
);
2943 /* this appears to free 'op' and 'ps' */
2944 print_res
= webkit_web_frame_print_full(frame
, op
, action
, &g_err
);
2946 /* check it worked */
2947 if (print_res
== GTK_PRINT_OPERATION_RESULT_ERROR
) {
2948 show_oops(NULL
, "can't print: %s", g_err
->message
);
2949 g_error_free (g_err
);
2957 go_home(struct tab
*t
, struct karg
*args
)
2964 set_encoding(struct tab
*t
, struct karg
*args
)
2968 if (args
->s
&& strlen(g_strstrip(args
->s
)) == 0) {
2969 e
= webkit_web_view_get_custom_encoding(t
->wv
);
2971 e
= webkit_web_view_get_encoding(t
->wv
);
2972 show_oops(t
, "encoding: %s", e
? e
: "N/A");
2974 webkit_web_view_set_custom_encoding(t
->wv
, args
->s
);
2980 restart(struct tab
*t
, struct karg
*args
)
2984 a
.s
= XT_RESTART_TABS_FILE
;
2986 execvp(start_argv
[0], start_argv
);
2992 char *http_proxy_save
; /* not a setting, used to toggle */
2995 proxy_cmd(struct tab
*t
, struct karg
*args
)
2997 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
3004 if (http_proxy_save
)
3005 g_free(http_proxy_save
);
3006 http_proxy_save
= g_strdup(http_proxy
);
3009 if (args
->i
& XT_PRXY_SHOW
) {
3011 show_oops(t
, "http_proxy = %s", http_proxy
);
3013 show_oops(t
, "proxy is currently disabled");
3014 } else if (args
->i
& XT_PRXY_TOGGLE
) {
3015 if (http_proxy_save
== NULL
&& http_proxy
== NULL
) {
3016 show_oops(t
, "can't toggle proxy");
3022 setup_proxy(http_proxy_save
);
3025 return (XT_CB_PASSTHROUGH
);
3030 int (*func
)(struct tab
*, struct karg
*);
3034 { "command_mode", 0, command_mode
, XT_MODE_COMMAND
, 0 },
3035 { "insert_mode", 0, command_mode
, XT_MODE_INSERT
, 0 },
3036 { "command", 0, command
, ':', 0 },
3037 { "search", 0, command
, '/', 0 },
3038 { "searchb", 0, command
, '?', 0 },
3039 { "hinting", 0, command
, '.', 0 },
3040 { "hinting_newtab", 0, command
, ',', 0 },
3041 { "togglesrc", 0, toggle_src
, 0, 0 },
3042 { "editsrc", 0, edit_src
, 0, 0 },
3043 { "editelement", 0, edit_element
, 0, 0 },
3044 { "passthrough", 0, passthrough
, 0, 0 },
3045 { "modurl", 0, modurl
, 0, 0 },
3047 /* yanking and pasting */
3048 { "yankuri", 0, yank_uri
, 0, 0 },
3049 /* XXX: pasteuri{cur,new} do not work from the cmd_entry? */
3050 { "pasteuricur", 0, paste_uri
, XT_PASTE_CURRENT_TAB
, 0 },
3051 { "pasteurinew", 0, paste_uri
, XT_PASTE_NEW_TAB
, 0 },
3054 { "searchnext", 0, search
, XT_SEARCH_NEXT
, 0 },
3055 { "searchprevious", 0, search
, XT_SEARCH_PREV
, 0 },
3058 { "focusaddress", 0, focus
, XT_FOCUS_URI
, 0 },
3059 { "focussearch", 0, focus
, XT_FOCUS_SEARCH
, 0 },
3062 { "hinting", 0, hint
, 0, 0 },
3063 { "hinting_newtab", 0, hint
, XT_HINT_NEWTAB
, 0 },
3065 /* custom stylesheet */
3066 { "userstyle", 0, userstyle
, XT_STYLE_CURRENT_TAB
, 0 },
3067 { "userstyle_global", 0, userstyle
, XT_STYLE_GLOBAL
, 0 },
3070 { "goback", 0, navaction
, XT_NAV_BACK
, 0 },
3071 { "goforward", 0, navaction
, XT_NAV_FORWARD
, 0 },
3072 { "reload", 0, navaction
, XT_NAV_RELOAD
, 0 },
3073 { "stop", 0, navaction
, XT_NAV_STOP
, 0 },
3075 /* vertical movement */
3076 { "scrolldown", 0, move
, XT_MOVE_DOWN
, 0 },
3077 { "scrollup", 0, move
, XT_MOVE_UP
, 0 },
3078 { "scrollbottom", 0, move
, XT_MOVE_BOTTOM
, 0 },
3079 { "scrolltop", 0, move
, XT_MOVE_TOP
, 0 },
3080 { "1", 0, move
, XT_MOVE_TOP
, 0 },
3081 { "scrollhalfdown", 0, move
, XT_MOVE_HALFDOWN
, 0 },
3082 { "scrollhalfup", 0, move
, XT_MOVE_HALFUP
, 0 },
3083 { "scrollpagedown", 0, move
, XT_MOVE_PAGEDOWN
, 0 },
3084 { "scrollpageup", 0, move
, XT_MOVE_PAGEUP
, 0 },
3085 /* horizontal movement */
3086 { "scrollright", 0, move
, XT_MOVE_RIGHT
, 0 },
3087 { "scrollleft", 0, move
, XT_MOVE_LEFT
, 0 },
3088 { "scrollfarright", 0, move
, XT_MOVE_FARRIGHT
, 0 },
3089 { "scrollfarleft", 0, move
, XT_MOVE_FARLEFT
, 0 },
3091 { "favorites", 0, xtp_page_fl
, 0, 0 },
3092 { "fav", 0, xtp_page_fl
, 0, 0 },
3093 { "favadd", 0, add_favorite
, 0, 0 },
3095 { "qall", 0, quit
, 0, 0 },
3096 { "quitall", 0, quit
, 0, 0 },
3097 { "w", 0, save_tabs
, 0, 0 },
3098 { "wq", 0, save_tabs_and_quit
, 0, 0 },
3099 { "help", 0, help
, 0, 0 },
3100 { "about", 0, about
, 0, 0 },
3101 { "stats", 0, stats
, 0, 0 },
3102 { "version", 0, about
, 0, 0 },
3105 { "js", 0, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3106 { "save", 1, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3107 { "domain", 2, js_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3108 { "fqdn", 2, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3109 { "show", 1, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3110 { "all", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3111 { "persistent", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3112 { "session", 2, js_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3113 { "toggle", 1, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3114 { "domain", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3115 { "fqdn", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3117 /* cookie command */
3118 { "cookie", 0, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3119 { "save", 1, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3120 { "domain", 2, cookie_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3121 { "fqdn", 2, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3122 { "show", 1, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3123 { "all", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3124 { "persistent", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3125 { "session", 2, cookie_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3126 { "toggle", 1, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3127 { "domain", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3128 { "fqdn", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3129 { "purge", 1, cookie_cmd
, XT_DELETE
, 0 },
3131 /* plugin command */
3132 { "plugin", 0, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3133 { "save", 1, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3134 { "domain", 2, pl_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3135 { "fqdn", 2, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3136 { "show", 1, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3137 { "all", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3138 { "persistent", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3139 { "session", 2, pl_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3140 { "toggle", 1, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3141 { "domain", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3142 { "fqdn", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3144 /* toplevel (domain) command */
3145 { "toplevel", 0, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3146 { "toggle", 1, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3149 { "cookiejar", 0, xtp_page_cl
, 0, 0 },
3152 { "cert", 0, cert_cmd
, XT_SHOW
, 0 },
3153 { "save", 1, cert_cmd
, XT_SAVE
, 0 },
3154 { "show", 1, cert_cmd
, XT_SHOW
, 0 },
3156 { "ca", 0, ca_cmd
, 0, 0 },
3157 { "downloadmgr", 0, xtp_page_dl
, 0, 0 },
3158 { "dl", 0, xtp_page_dl
, 0, 0 },
3159 { "h", 0, xtp_page_hl
, 0, 0 },
3160 { "history", 0, xtp_page_hl
, 0, 0 },
3161 { "home", 0, go_home
, 0, 0 },
3162 { "restart", 0, restart
, 0, 0 },
3163 { "urlhide", 0, urlaction
, XT_URL_HIDE
, 0 },
3164 { "urlshow", 0, urlaction
, XT_URL_SHOW
, 0 },
3165 { "statustoggle", 0, statustoggle
, 0, 0 },
3166 { "run_script", 0, run_page_script
, 0, XT_USERARG
},
3168 { "print", 0, print_page
, 0, 0 },
3171 { "focusin", 0, resizetab
, XT_ZOOM_IN
, 0 },
3172 { "focusout", 0, resizetab
, XT_ZOOM_OUT
, 0 },
3173 { "focusreset", 0, resizetab
, XT_ZOOM_NORMAL
, 0 },
3174 { "q", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3175 { "quit", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3176 { "open", 0, tabaction
, XT_TAB_OPEN
, XT_URLARG
},
3177 { "tabclose", 0, tabaction
, XT_TAB_DELETE
, XT_PREFIX
| XT_INTARG
},
3178 { "tabedit", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3179 { "tabfirst", 0, movetab
, XT_TAB_FIRST
, 0 },
3180 { "tabhide", 0, tabaction
, XT_TAB_HIDE
, 0 },
3181 { "tablast", 0, movetab
, XT_TAB_LAST
, 0 },
3182 { "tabnew", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3183 { "tabnext", 0, movetab
, XT_TAB_NEXT
, XT_PREFIX
| XT_INTARG
},
3184 { "tabnextstyle", 0, tabaction
, XT_TAB_NEXTSTYLE
, 0 },
3185 { "tabprevious", 0, movetab
, XT_TAB_PREV
, XT_PREFIX
| XT_INTARG
},
3186 { "tabrewind", 0, movetab
, XT_TAB_FIRST
, 0 },
3187 { "tabshow", 0, tabaction
, XT_TAB_SHOW
, 0 },
3188 { "tabs", 0, buffers
, 0, 0 },
3189 { "tabundoclose", 0, tabaction
, XT_TAB_UNDO_CLOSE
, 0 },
3190 { "buffers", 0, buffers
, 0, 0 },
3191 { "ls", 0, buffers
, 0, 0 },
3192 { "encoding", 0, set_encoding
, 0, XT_USERARG
},
3193 { "loadimages", 0, tabaction
, XT_TAB_LOAD_IMAGES
, 0 },
3195 /* command aliases (handy when -S flag is used) */
3196 { "promptopen", 0, command
, XT_CMD_OPEN
, 0 },
3197 { "promptopencurrent", 0, command
, XT_CMD_OPEN_CURRENT
, 0 },
3198 { "prompttabnew", 0, command
, XT_CMD_TABNEW
, 0 },
3199 { "prompttabnewcurrent",0, command
, XT_CMD_TABNEW_CURRENT
, 0 },
3202 { "set", 0, set
, 0, XT_SETARG
},
3204 { "fullscreen", 0, fullscreen
, 0, 0 },
3205 { "f", 0, fullscreen
, 0, 0 },
3208 { "session", 0, session_cmd
, XT_SHOW
, 0 },
3209 { "delete", 1, session_cmd
, XT_DELETE
, XT_SESSARG
},
3210 { "open", 1, session_cmd
, XT_OPEN
, XT_SESSARG
},
3211 { "save", 1, session_cmd
, XT_SAVE
, XT_USERARG
},
3212 { "show", 1, session_cmd
, XT_SHOW
, 0 },
3214 /* external javascript */
3215 { "script", 0, script_cmd
, XT_EJS_SHOW
, XT_USERARG
},
3218 { "inspector", 0, inspector_cmd
, XT_INS_SHOW
, 0 },
3219 { "show", 1, inspector_cmd
, XT_INS_SHOW
, 0 },
3220 { "hide", 1, inspector_cmd
, XT_INS_HIDE
, 0 },
3223 { "proxy", 0, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3224 { "show", 1, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3225 { "toggle", 1, proxy_cmd
, XT_PRXY_TOGGLE
, 0 },
3232 } cmd_status
= {-1, 0};
3235 wv_release_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3238 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 1)
3245 wv_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3248 WebKitHitTestResult
*hit_test_result
;
3251 hit_test_result
= webkit_web_view_get_hit_test_result(t
->wv
, e
);
3252 g_object_get(hit_test_result
, "context", &context
, NULL
);
3257 if (context
& WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE
)
3258 t
->mode
= XT_MODE_INSERT
;
3260 t
->mode
= XT_MODE_COMMAND
;
3262 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3264 else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 8 /* btn 4 */) {
3270 } else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 9 /* btn 5 */) {
3272 a
.i
= XT_NAV_FORWARD
;
3282 tab_close_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3284 DNPRINTF(XT_D_TAB
, "tab_close_cb: tab %d\n", t
->tab_id
);
3286 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3295 activate_uri_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3297 const gchar
*uri
= gtk_entry_get_text(GTK_ENTRY(entry
));
3299 DNPRINTF(XT_D_URL
, "activate_uri_entry_cb: %s\n", uri
);
3302 show_oops(NULL
, "activate_uri_entry_cb invalid parameters");
3307 show_oops(t
, "activate_uri_entry_cb no uri");
3311 uri
+= strspn(uri
, "\t ");
3313 /* if xxxt:// treat specially */
3314 if (parse_xtp_url(t
, uri
))
3317 /* otherwise continue to load page normally */
3318 load_uri(t
, (gchar
*)uri
);
3323 activate_search_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3325 const gchar
*search
= gtk_entry_get_text(GTK_ENTRY(entry
));
3326 char *newuri
= NULL
;
3329 DNPRINTF(XT_D_URL
, "activate_search_entry_cb: %s\n", search
);
3332 show_oops(NULL
, "activate_search_entry_cb invalid parameters");
3336 if (search_string
== NULL
|| strlen(search_string
) == 0) {
3337 show_oops(t
, "no search_string");
3341 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
3343 enc_search
= soup_uri_encode(search
, XT_RESERVED_CHARS
);
3344 newuri
= g_strdup_printf(search_string
, enc_search
);
3348 webkit_web_view_load_uri(t
->wv
, newuri
);
3356 check_and_set_cookie(const gchar
*uri
, struct tab
*t
)
3358 struct domain
*d
= NULL
;
3361 if (uri
== NULL
|| t
== NULL
)
3364 if ((d
= wl_find_uri(uri
, &c_wl
)) == NULL
)
3369 DNPRINTF(XT_D_COOKIE
, "check_and_set_cookie: %s %s\n",
3370 es
? "enable" : "disable", uri
);
3372 g_object_set(G_OBJECT(t
->settings
),
3373 "enable-html5-local-storage", es
, (char *)NULL
);
3374 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3378 check_and_set_js(const gchar
*uri
, struct tab
*t
)
3380 struct domain
*d
= NULL
;
3383 if (uri
== NULL
|| t
== NULL
)
3386 if ((d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3391 DNPRINTF(XT_D_JS
, "check_and_set_js: %s %s\n",
3392 es
? "enable" : "disable", uri
);
3394 g_object_set(G_OBJECT(t
->settings
),
3395 "enable-scripts", es
, (char *)NULL
);
3396 g_object_set(G_OBJECT(t
->settings
),
3397 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
3398 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3400 button_set_stockid(t
->js_toggle
,
3401 es
? GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
);
3405 check_and_set_pl(const gchar
*uri
, struct tab
*t
)
3407 struct domain
*d
= NULL
;
3410 if (uri
== NULL
|| t
== NULL
)
3413 if ((d
= wl_find_uri(uri
, &pl_wl
)) == NULL
)
3418 DNPRINTF(XT_D_JS
, "check_and_set_pl: %s %s\n",
3419 es
? "enable" : "disable", uri
);
3421 g_object_set(G_OBJECT(t
->settings
),
3422 "enable-plugins", es
, (char *)NULL
);
3423 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3427 color_address_bar(gpointer p
)
3430 struct tab
*tt
, *t
= p
;
3431 gchar
*col_str
= XT_COLOR_WHITE
, *text
, *base
;
3432 const gchar
*uri
, *u
= NULL
, *error_str
= NULL
;
3435 gdk_threads_enter();
3437 DNPRINTF(XT_D_URL
, "%s:\n", __func__
);
3439 /* make sure t still exists */
3442 TAILQ_FOREACH(tt
, &tabs
, entry
)
3448 if ((uri
= get_uri(t
)) == NULL
)
3454 gdk_threads_leave();
3457 col_str
= XT_COLOR_YELLOW
;
3458 switch (load_compare_cert(u
, &error_str
)) {
3460 col_str
= XT_COLOR_BLUE
;
3463 col_str
= XT_COLOR_GREEN
;
3465 case CERT_UNTRUSTED
:
3466 col_str
= XT_COLOR_YELLOW
;
3469 col_str
= XT_COLOR_RED
;
3474 gdk_threads_enter();
3476 /* make sure t isn't deleted */
3477 TAILQ_FOREACH(tt
, &tabs
, entry
)
3484 /* test to see if the user navigated away and canceled the thread */
3485 if (t
->thread
!= g_thread_self())
3487 if ((uri
= get_uri(t
)) == NULL
) {
3491 if (strcmp(uri
, u
)) {
3492 /* make sure we are still the same url */
3499 if (!strcmp(col_str
, XT_COLOR_WHITE
)) {
3500 text
= gdk_color_to_string(
3501 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3502 base
= gdk_color_to_string(
3503 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3504 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3505 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3506 statusbar_modify_attr(t
, text
, base
);
3510 gdk_color_parse(col_str
, &color
);
3511 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3512 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3515 if (error_str
&& error_str
[0] != '\0')
3516 show_oops(t
, "%s", error_str
);
3521 /* t is invalid at this point */
3523 g_free((gpointer
)u
);
3526 gdk_threads_leave();
3531 show_ca_status(struct tab
*t
, const char *uri
)
3534 gchar
*col_str
= XT_COLOR_WHITE
, *text
, *base
;
3536 DNPRINTF(XT_D_URL
, "show_ca_status: %d %s %s\n",
3537 ssl_strict_certs
, ssl_ca_file
, uri
);
3544 if (ssl_ca_file
== NULL
) {
3545 if (g_str_has_prefix(uri
, "http://"))
3547 if (g_str_has_prefix(uri
, "https://")) {
3548 col_str
= XT_COLOR_RED
;
3553 if (g_str_has_prefix(uri
, "http://") ||
3554 !g_str_has_prefix(uri
, "https://"))
3558 * It is not necessary to see if the thread is already running.
3559 * If the thread is in progress setting it to something else aborts it
3563 /* thread the coloring of the address bar */
3564 t
->thread
= g_thread_create((GThreadFunc
)color_address_bar
, t
, TRUE
, NULL
);
3566 color_address_bar(t
);
3573 if (!strcmp(col_str
, XT_COLOR_WHITE
)) {
3574 text
= gdk_color_to_string(
3575 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3576 base
= gdk_color_to_string(
3577 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3578 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3579 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3580 statusbar_modify_attr(t
, text
, base
);
3584 gdk_color_parse(col_str
, &color
);
3585 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3586 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3592 free_favicon(struct tab
*t
)
3594 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p req %p\n",
3595 __func__
, t
->icon_download
, t
->icon_request
);
3597 if (t
->icon_request
)
3598 g_object_unref(t
->icon_request
);
3599 if (t
->icon_dest_uri
)
3600 g_free(t
->icon_dest_uri
);
3602 t
->icon_request
= NULL
;
3603 t
->icon_dest_uri
= NULL
;
3607 xt_icon_from_name(struct tab
*t
, gchar
*name
)
3609 if (!enable_favicon_entry
)
3612 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->uri_entry
),
3613 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3615 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3616 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3618 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3619 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3623 xt_icon_from_pixbuf(struct tab
*t
, GdkPixbuf
*pb
)
3625 GdkPixbuf
*pb_scaled
;
3627 if (gdk_pixbuf_get_width(pb
) > 16 || gdk_pixbuf_get_height(pb
) > 16)
3628 pb_scaled
= gdk_pixbuf_scale_simple(pb
, 16, 16,
3629 GDK_INTERP_BILINEAR
);
3633 if (enable_favicon_entry
) {
3636 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
3637 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3640 if (show_url
== 0) {
3641 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
3642 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3644 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3645 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3648 /* XXX: Only supports the minimal tabs atm. */
3649 if (enable_favicon_tabs
)
3650 gtk_image_set_from_pixbuf(GTK_IMAGE(t
->tab_elems
.favicon
),
3653 if (pb_scaled
!= pb
)
3654 g_object_unref(pb_scaled
);
3658 xt_icon_from_file(struct tab
*t
, char *file
)
3662 if (g_str_has_prefix(file
, "file://"))
3663 file
+= strlen("file://");
3665 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
3667 xt_icon_from_pixbuf(t
, pb
);
3670 xt_icon_from_name(t
, "text-html");
3674 is_valid_icon(char *file
)
3677 const char *mime_type
;
3681 gf
= g_file_new_for_path(file
);
3682 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
3684 mime_type
= g_file_info_get_content_type(fi
);
3685 valid
= g_strcmp0(mime_type
, "image/x-ico") == 0 ||
3686 g_strcmp0(mime_type
, "image/vnd.microsoft.icon") == 0 ||
3687 g_strcmp0(mime_type
, "image/png") == 0 ||
3688 g_strcmp0(mime_type
, "image/gif") == 0 ||
3689 g_strcmp0(mime_type
, "application/octet-stream") == 0;
3697 set_favicon_from_file(struct tab
*t
, char *file
)
3701 if (t
== NULL
|| file
== NULL
)
3704 if (g_str_has_prefix(file
, "file://"))
3705 file
+= strlen("file://");
3706 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading %s\n", __func__
, file
);
3708 if (!stat(file
, &sb
)) {
3709 if (sb
.st_size
== 0 || !is_valid_icon(file
)) {
3710 /* corrupt icon so trash it */
3711 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3714 /* no need to set icon to default here */
3718 xt_icon_from_file(t
, file
);
3722 favicon_download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
3725 WebKitDownloadStatus status
= webkit_download_get_status(download
);
3726 struct tab
*tt
= NULL
, *t
= NULL
;
3729 * find the webview instead of passing in the tab as it could have been
3730 * deleted from underneath us.
3732 TAILQ_FOREACH(tt
, &tabs
, entry
) {
3741 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d status %d\n",
3742 __func__
, t
->tab_id
, status
);
3745 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
3747 t
->icon_download
= NULL
;
3750 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
3753 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
3756 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
3758 DNPRINTF(XT_D_DOWNLOAD
, "%s: freeing favicon %d\n",
3759 __func__
, t
->tab_id
);
3760 t
->icon_download
= NULL
;
3763 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
3766 DNPRINTF(XT_D_DOWNLOAD
, "%s: setting icon to %s\n",
3767 __func__
, t
->icon_dest_uri
);
3768 set_favicon_from_file(t
, t
->icon_dest_uri
);
3769 /* these will be freed post callback */
3770 t
->icon_request
= NULL
;
3771 t
->icon_download
= NULL
;
3779 abort_favicon_download(struct tab
*t
)
3781 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p\n", __func__
, t
->icon_download
);
3783 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
3784 if (t
->icon_download
) {
3785 g_signal_handlers_disconnect_by_func(G_OBJECT(t
->icon_download
),
3786 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3787 webkit_download_cancel(t
->icon_download
);
3788 t
->icon_download
= NULL
;
3793 xt_icon_from_name(t
, "text-html");
3797 notify_icon_loaded_cb(WebKitWebView
*wv
, gchar
*uri
, struct tab
*t
)
3799 DNPRINTF(XT_D_DOWNLOAD
, "%s %s\n", __func__
, uri
);
3801 if (uri
== NULL
|| t
== NULL
)
3804 #if WEBKIT_CHECK_VERSION(1, 4, 0)
3805 /* take icon from WebKitIconDatabase */
3808 pb
= webkit_web_view_get_icon_pixbuf(wv
);
3810 xt_icon_from_pixbuf(t
, pb
);
3813 xt_icon_from_name(t
, "text-html");
3814 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
3815 /* download icon to cache dir */
3816 gchar
*name_hash
, file
[PATH_MAX
];
3819 if (t
->icon_request
) {
3820 DNPRINTF(XT_D_DOWNLOAD
, "%s: download in progress\n", __func__
);
3824 /* check to see if we got the icon in cache */
3825 name_hash
= g_compute_checksum_for_string(G_CHECKSUM_SHA256
, uri
, -1);
3826 snprintf(file
, sizeof file
, "%s" PS
"%s.ico", cache_dir
, name_hash
);
3829 if (!stat(file
, &sb
)) {
3830 if (sb
.st_size
> 0) {
3831 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading from cache %s\n",
3833 set_favicon_from_file(t
, file
);
3837 /* corrupt icon so trash it */
3838 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3843 /* create download for icon */
3844 t
->icon_request
= webkit_network_request_new(uri
);
3845 if (t
->icon_request
== NULL
) {
3846 DNPRINTF(XT_D_DOWNLOAD
, "%s: invalid uri %s\n",
3851 t
->icon_download
= webkit_download_new(t
->icon_request
);
3852 if (t
->icon_download
== NULL
)
3855 /* we have to free icon_dest_uri later */
3856 t
->icon_dest_uri
= g_strdup_printf("file://%s", file
);
3857 webkit_download_set_destination_uri(t
->icon_download
,
3860 if (webkit_download_get_status(t
->icon_download
) ==
3861 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
3862 g_object_unref(t
->icon_request
);
3863 g_free(t
->icon_dest_uri
);
3864 t
->icon_request
= NULL
;
3865 t
->icon_dest_uri
= NULL
;
3869 g_signal_connect(G_OBJECT(t
->icon_download
), "notify::status",
3870 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3872 webkit_download_start(t
->icon_download
);
3877 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3879 const gchar
*uri
= NULL
;
3880 struct history
*h
, find
;
3884 DNPRINTF(XT_D_URL
, "notify_load_status_cb: %d %s\n",
3885 webkit_web_view_get_load_status(wview
),
3886 get_uri(t
) ? get_uri(t
) : "NOTHING");
3889 show_oops(NULL
, "notify_load_status_cb invalid parameters");
3893 switch (webkit_web_view_get_load_status(wview
)) {
3894 case WEBKIT_LOAD_PROVISIONAL
:
3896 abort_favicon_download(t
);
3897 #if GTK_CHECK_VERSION(2, 20, 0)
3898 gtk_widget_show(t
->spinner
);
3899 gtk_spinner_start(GTK_SPINNER(t
->spinner
));
3901 gtk_label_set_text(GTK_LABEL(t
->label
), "Loading");
3903 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), TRUE
);
3905 /* assume we are a new address */
3906 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3907 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3908 text
= gdk_color_to_string(
3909 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3910 base
= gdk_color_to_string(
3911 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3912 statusbar_modify_attr(t
, text
, base
);
3916 /* take focus if we are visible */
3922 /* kill color thread */
3927 case WEBKIT_LOAD_COMMITTED
:
3932 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
3938 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
3940 /* check if js white listing is enabled */
3941 if (enable_plugin_whitelist
)
3942 check_and_set_pl(uri
, t
);
3943 if (enable_cookie_whitelist
)
3944 check_and_set_cookie(uri
, t
);
3945 if (enable_js_whitelist
)
3946 check_and_set_js(uri
, t
);
3952 /* we know enough to autosave the session */
3953 if (session_autosave
) {
3958 show_ca_status(t
, uri
);
3959 run_script(t
, JS_HINTING
);
3960 if (enable_autoscroll
)
3961 run_script(t
, JS_AUTOSCROLL
);
3964 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
3966 if (color_visited_uris
) {
3967 color_visited(t
, color_visited_helper());
3969 /* This colors the links you middle-click (open in new
3970 * tab) in the current tab. */
3971 if (t
->tab_id
!= gtk_notebook_get_current_page(notebook
) &&
3972 (uri
= get_uri(t
)) != NULL
)
3973 color_visited(get_current_tab(),
3974 g_strdup_printf("{'%s' : 'dummy'}", uri
));
3978 case WEBKIT_LOAD_FINISHED
:
3980 if ((uri
= get_uri(t
)) == NULL
)
3983 if (!strncmp(uri
, "http://", strlen("http://")) ||
3984 !strncmp(uri
, "https://", strlen("https://")) ||
3985 !strncmp(uri
, "file://", strlen("file://"))) {
3986 find
.uri
= (gchar
*)uri
;
3987 h
= RB_FIND(history_list
, &hl
, &find
);
3989 insert_history_item(uri
,
3990 get_title(t
, FALSE
), time(NULL
));
3992 h
->time
= time(NULL
);
3995 set_status(t
, (char *)uri
, XT_STATUS_URI
);
3996 #if WEBKIT_CHECK_VERSION(1, 1, 18)
3997 case WEBKIT_LOAD_FAILED
:
4000 #if GTK_CHECK_VERSION(2, 20, 0)
4001 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
4002 gtk_widget_hide(t
->spinner
);
4005 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
4010 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
), TRUE
);
4012 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
),
4013 can_go_back_for_real(t
));
4015 gtk_widget_set_sensitive(GTK_WIDGET(t
->forward
),
4016 can_go_forward_for_real(t
));
4020 notify_title_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
4022 const gchar
*title
= NULL
, *win_title
= NULL
;
4024 title
= get_title(t
, FALSE
);
4025 win_title
= get_title(t
, TRUE
);
4027 gtk_label_set_text(GTK_LABEL(t
->label
), title
);
4028 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
), title
);
4031 if (win_title
&& t
->tab_id
== gtk_notebook_get_current_page(notebook
))
4032 gtk_window_set_title(GTK_WINDOW(main_window
), win_title
);
4036 get_domain(const gchar
*host
)
4041 /* handle silly domains like .co.uk */
4043 if ((x
= strlen(host
)) <= 6)
4044 return (g_strdup(host
));
4046 if (host
[x
- 3] == '.' && host
[x
- 6] == '.') {
4052 return (g_strdup(&host
[x
+ 1]));
4056 p
= g_strrstr(host
, ".");
4058 return (g_strdup(""));
4064 return (g_strdup(p
+ 1));
4066 return (g_strdup(host
));
4070 js_autorun(struct tab
*t
)
4074 size_t got_default
= 0, got_host
= 0;
4076 char deff
[PATH_MAX
], hostf
[PATH_MAX
];
4077 char *js
= NULL
, *jsat
, *domain
= NULL
;
4078 FILE *deffile
= NULL
, *hostfile
= NULL
;
4080 if (enable_js_autorun
== 0)
4085 !(g_str_has_prefix(uri
, "http://") ||
4086 g_str_has_prefix(uri
, "https://")))
4089 su
= soup_uri_new(uri
);
4092 if (!SOUP_URI_VALID_FOR_HTTP(su
))
4095 DNPRINTF(XT_D_JS
, "%s: host: %s domain: %s\n", __func__
,
4097 domain
= get_domain(su
->host
);
4099 snprintf(deff
, sizeof deff
, "%s" PS
"default.js", js_dir
);
4100 if ((deffile
= fopen(deff
, "r")) != NULL
) {
4101 if (fstat(fileno(deffile
), &sb
) == -1) {
4102 show_oops(t
, "can't stat default JS file");
4105 got_default
= sb
.st_size
;
4108 /* try host first followed by domain */
4109 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, su
->host
);
4110 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4111 if ((hostfile
= fopen(hostf
, "r")) == NULL
) {
4112 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, domain
);
4113 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4114 if ((hostfile
= fopen(hostf
, "r")) == NULL
)
4117 DNPRINTF(XT_D_JS
, "file: %s\n", hostf
);
4118 if (fstat(fileno(hostfile
), &sb
) == -1) {
4119 show_oops(t
, "can't stat %s JS file", hostf
);
4122 got_host
= sb
.st_size
;
4125 if (got_default
+ got_host
== 0)
4128 js
= g_malloc0(got_default
+ got_host
+ 1);
4132 if (fread(js
, got_default
, 1, deffile
) != 1) {
4133 show_oops(t
, "default file read error");
4136 jsat
= js
+ got_default
;
4140 if (fread(jsat
, got_host
, 1, hostfile
) != 1) {
4141 show_oops(t
, "host file read error");
4146 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
4147 run_script_locked(t
, js
);
4163 webview_load_finished_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4165 /* autorun some js if enabled */
4173 webview_progress_changed_cb(WebKitWebView
*wv
, int progress
, struct tab
*t
)
4175 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->uri_entry
),
4176 progress
== 100 ? 0 : (double)progress
/ 100);
4177 if (show_url
== 0) {
4178 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
4179 progress
== 100 ? 0 : (double)progress
/ 100);
4182 update_statusbar_position(NULL
, NULL
);
4186 strict_transport_rb_cmp(struct strict_transport
*a
, struct strict_transport
*b
)
4191 /* compare strings from the end */
4192 l1
= strlen(a
->host
);
4193 l2
= strlen(b
->host
);
4197 for (; *p1
== *p2
&& p1
> a
->host
&& p2
> b
->host
;
4202 * Check if we need to do pattern expansion,
4203 * or if we're just keeping the tree in order
4205 if (a
->flags
& XT_STS_FLAGS_EXPAND
&&
4206 b
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) {
4207 /* Check if we're matching the
4208 * 'host.xyz' part in '*.host.xyz'
4210 if (p2
== b
->host
&& (p1
== a
->host
|| *(p1
-1) == '.')) {
4215 if (p1
== a
->host
&& p2
== b
->host
)
4229 RB_GENERATE(strict_transport_tree
, strict_transport
, entry
,
4230 strict_transport_rb_cmp
);
4233 strict_transport_add(const char *domain
, time_t timeout
, int subdomains
)
4235 struct strict_transport
*d
, find
;
4239 if (enable_strict_transport
== FALSE
)
4242 DPRINTF("strict_transport_add(%s,%lld,%d)\n", domain
,
4243 (long long)timeout
, subdomains
);
4249 find
.host
= (char *)domain
;
4251 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4255 /* check if update is needed */
4256 if (d
->timeout
== timeout
&&
4257 (d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) == subdomains
)
4260 d
->timeout
= timeout
;
4262 d
->flags
|= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4264 /* We're still initializing */
4265 if (strict_transport_file
== NULL
)
4268 if ((f
= fopen(strict_transport_file
, "w")) == NULL
) {
4270 "can't open strict-transport rules file");
4274 fprintf(f
, "# Generated file - do not update unless you know "
4275 "what you're doing\n");
4276 RB_FOREACH(d
, strict_transport_tree
, &st_tree
) {
4277 if (d
->timeout
< now
)
4279 fprintf(f
, "%s\t%lld\t%d\n", d
->host
, (long long)d
->timeout
,
4280 d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
);
4284 d
= g_malloc(sizeof *d
);
4285 d
->host
= g_strdup(domain
);
4286 d
->timeout
= timeout
;
4288 d
->flags
= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4291 RB_INSERT(strict_transport_tree
, &st_tree
, d
);
4293 /* We're still initializing */
4294 if (strict_transport_file
== NULL
)
4297 if ((f
= fopen(strict_transport_file
, "a+")) == NULL
) {
4299 "can't open strict-transport rules file");
4303 fseek(f
, 0, SEEK_END
);
4304 fprintf(f
,"%s\t%lld\t%d\n", d
->host
, (long long)timeout
, subdomains
);
4311 strict_transport_check(const char *host
)
4313 static struct strict_transport
*d
= NULL
;
4314 struct strict_transport find
;
4316 if (enable_strict_transport
== FALSE
)
4319 find
.host
= (char *)host
;
4321 /* match for domains that include subdomains */
4322 find
.flags
= XT_STS_FLAGS_EXPAND
;
4324 /* First, check if we're already at the right node */
4325 if (d
!= NULL
&& strict_transport_rb_cmp(&find
, d
) == 0) {
4329 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4337 strict_transport_init()
4339 char file
[PATH_MAX
];
4345 time_t timeout
, now
;
4348 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_STS_FILE
);
4349 if ((f
= fopen(file
, "r")) == NULL
) {
4350 strict_transport_file
= g_strdup(file
);
4361 if ((rule
= fparseln(f
, &len
, NULL
, delim
, 0)) == NULL
) {
4362 if (!feof(f
) || ferror(f
))
4368 /* get second entry */
4369 if ((ptr
= strpbrk(rule
, " \t")) == NULL
)
4373 timeout
= atoi(ptr
);
4375 /* get third entry */
4376 if ((ptr
= strpbrk(ptr
, " \t")) == NULL
)
4380 subdomains
= atoi(ptr
);
4383 strict_transport_add(rule
, timeout
, subdomains
);
4388 strict_transport_file
= g_strdup(file
);
4392 startpage_add("strict-transport rules file ('%s') is corrupt", file
);
4400 strict_transport_security_cb(SoupMessage
*msg
, gpointer data
)
4406 int subdomains
= FALSE
;
4411 sts
= soup_message_headers_get_one(msg
->response_headers
,
4412 "Strict-Transport-Security");
4413 uri
= soup_message_get_uri(msg
);
4415 if (sts
== NULL
|| uri
== NULL
)
4418 if ((ptr
= strcasestr(sts
, "max-age="))) {
4419 ptr
+= strlen("max-age=");
4420 timeout
= atoll(ptr
);
4422 return; /* malformed header - max-age must be included */
4424 if ((ptr
= strcasestr(sts
, "includeSubDomains")))
4427 strict_transport_add(uri
->host
, timeout
+ time(NULL
), subdomains
);
4431 session_rq_cb(SoupSession
*s
, SoupMessage
*msg
, SoupSocket
*socket
,
4441 if (s
== NULL
|| msg
== NULL
)
4444 if (referer_mode
== XT_REFERER_ALWAYS
)
4447 /* Check if referer is set - and what the user requested for referers */
4448 ref
= soup_message_headers_get_one(msg
->request_headers
, "Referer");
4450 DNPRINTF(XT_D_NAV
, "session_rq_cb: Referer: %s\n", ref
);
4451 switch (referer_mode
) {
4452 case XT_REFERER_NEVER
:
4453 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing referer\n");
4454 soup_message_headers_remove(msg
->request_headers
,
4457 case XT_REFERER_SAME_DOMAIN
:
4458 ref_uri
= soup_uri_new(ref
);
4459 dest
= soup_message_get_uri(msg
);
4461 ref_suffix
= tld_get_suffix(ref_uri
->host
);
4462 dest_suffix
= tld_get_suffix(dest
->host
);
4464 if (dest
&& ref_suffix
&& dest_suffix
&&
4465 strcmp(ref_suffix
, dest_suffix
) != 0) {
4466 soup_message_headers_remove(msg
->request_headers
,
4468 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4469 "referer (not same domain) (suffixes: %s - %s)\n",
4470 ref_suffix
, dest_suffix
);
4472 soup_uri_free(ref_uri
);
4474 case XT_REFERER_SAME_FQDN
:
4475 ref_uri
= soup_uri_new(ref
);
4476 dest
= soup_message_get_uri(msg
);
4477 if (dest
&& strcmp(ref_uri
->host
, dest
->host
) != 0) {
4478 soup_message_headers_remove(msg
->request_headers
,
4480 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4481 "referer (not same fqdn) (should be %s)\n",
4484 soup_uri_free(ref_uri
);
4486 case XT_REFERER_CUSTOM
:
4487 DNPRINTF(XT_D_NAV
, "session_rq_cb: setting referer "
4488 "to %s\n", referer_custom
);
4489 soup_message_headers_replace(msg
->request_headers
,
4490 "Referer", referer_custom
);
4495 if (enable_strict_transport
) {
4496 soup_message_add_header_handler(msg
, "finished",
4497 "Strict-Transport-Security",
4498 G_CALLBACK(strict_transport_security_cb
), NULL
);
4503 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
4504 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
4505 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
4508 WebKitWebNavigationReason reason
;
4509 struct domain
*d
= NULL
;
4512 show_oops(NULL
, "webview_npd_cb invalid parameters");
4516 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
4518 webkit_network_request_get_uri(request
));
4520 uri
= (char *)webkit_network_request_get_uri(request
);
4522 if (!auto_load_images
&& t
->load_images
) {
4524 /* Disable autoloading of images, now that we're done loading
4526 g_object_set(G_OBJECT(t
->settings
),
4527 "auto-load-images", FALSE
, (char *)NULL
);
4528 webkit_web_view_set_settings(t
->wv
, t
->settings
);
4530 t
->load_images
= FALSE
;
4533 /* If this is an xtp url, we don't load anything else. */
4534 if (parse_xtp_url(t
, uri
))
4537 if ((t
->mode
== XT_MODE_HINT
&& t
->new_tab
) || t
->ctrl_click
) {
4539 create_new_tab(uri
, NULL
, ctrl_click_focus
, -1);
4540 webkit_web_policy_decision_ignore(pd
);
4541 return (TRUE
); /* we made the decission */
4544 /* Change user agent if more than one has been given. */
4545 if (user_agent_count
> 1) {
4546 struct user_agent
*ua
;
4548 if ((ua
= TAILQ_NEXT(user_agent
, entry
)) == NULL
)
4549 user_agent
= TAILQ_FIRST(&ua_list
);
4553 free(t
->user_agent
);
4554 t
->user_agent
= g_strdup(user_agent
->value
);
4556 DNPRINTF(XT_D_NAV
, "user-agent: %s\n", t
->user_agent
);
4558 g_object_set(G_OBJECT(t
->settings
),
4559 "user-agent", t
->user_agent
, (char *)NULL
);
4561 webkit_web_view_set_settings(wv
, t
->settings
);
4565 * This is a little hairy but it comes down to this:
4566 * when we run in whitelist mode we have to assist the browser in
4567 * opening the URL that it would have opened in a new tab.
4569 reason
= webkit_web_navigation_action_get_reason(na
);
4570 if (reason
== WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED
) {
4571 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
4572 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1)
4573 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4575 webkit_web_policy_decision_use(pd
);
4576 return (TRUE
); /* we made the decision */
4583 webview_rrs_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, WebKitWebResource
*res
,
4584 WebKitNetworkRequest
*request
, WebKitNetworkResponse
*response
,
4590 msg
= webkit_network_request_get_message(request
);
4593 uri
= soup_message_get_uri(msg
);
4596 if (strcmp(soup_uri_get_scheme(uri
), SOUP_URI_SCHEME_HTTP
) == 0) {
4597 if (strict_transport_check(uri
->host
)) {
4598 DNPRINTF(XT_D_NAV
, "webview_rrs_cb: force https for %s\n",
4600 soup_uri_set_scheme(uri
, SOUP_URI_SCHEME_HTTPS
);
4606 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4609 struct domain
*d
= NULL
;
4611 WebKitWebView
*webview
= NULL
;
4614 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
4615 webkit_web_view_get_uri(wv
));
4618 /* open in current tab */
4620 } else if (enable_scripts
== 0 && enable_js_whitelist
== 1) {
4621 uri
= webkit_web_view_get_uri(wv
);
4622 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4625 if (t
->ctrl_click
) {
4626 x
= ctrl_click_focus
;
4629 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4631 } else if (enable_scripts
== 1) {
4632 if (t
->ctrl_click
) {
4633 x
= ctrl_click_focus
;
4636 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4644 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
4647 struct domain
*d
= NULL
;
4649 DNPRINTF(XT_D_NAV
, "webview_close_cb: %d\n", t
->tab_id
);
4651 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
4652 uri
= webkit_web_view_get_uri(wv
);
4653 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4657 } else if (enable_scripts
== 1)
4664 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
4666 /* we can not eat the event without throwing gtk off so defer it */
4668 /* catch middle click */
4669 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 2) {
4674 /* catch ctrl click */
4675 if (e
->type
== GDK_BUTTON_RELEASE
&&
4676 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
4681 return (XT_CB_PASSTHROUGH
);
4685 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
4687 struct mime_type
*m
;
4689 m
= find_mime_type(mime_type
);
4695 return (fork_exec(t
, m
->mt_action
,
4696 webkit_network_request_get_uri(request
),
4697 "can't launch MIME handler", 0));
4701 get_mime_type(const char *file
)
4704 char *mime_type
= NULL
;
4708 if (g_str_has_prefix(file
, "file://"))
4709 file
+= strlen("file://");
4711 gf
= g_file_new_for_path(file
);
4712 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
4714 if ((m
= g_file_info_get_content_type(fi
)) != NULL
)
4715 mime_type
= g_strdup(m
);
4723 run_download_mimehandler(char *mime_type
, char *file
)
4725 struct mime_type
*m
;
4727 m
= find_mime_type(mime_type
);
4731 return (fork_exec(NULL
, m
->mt_action
, file
,
4732 "can't launch download MIME handler", 0));
4736 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
4739 WebKitDownloadStatus status
;
4740 const char *file
= NULL
;
4743 if (download
== NULL
)
4745 status
= webkit_download_get_status(download
);
4746 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
4749 if (download_notifications
)
4750 show_oops(NULL
, "Download of '%s' finished",
4751 basename((char *)webkit_download_get_destination_uri(download
)));
4752 file
= webkit_download_get_destination_uri(download
);
4755 mime
= get_mime_type(file
);
4759 if (g_str_has_prefix(file
, "file://"))
4760 file
+= strlen("file://");
4761 run_download_mimehandler((char *)mime
, (char *)file
);
4766 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
4767 WebKitNetworkRequest
*request
, char *mime_type
,
4768 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
4771 show_oops(NULL
, "webview_mimetype_cb invalid parameters");
4775 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
4776 t
->tab_id
, mime_type
);
4778 if (run_mimehandler(t
, mime_type
, request
) == 0) {
4779 webkit_web_policy_decision_ignore(decision
);
4784 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
4785 webkit_web_policy_decision_download(decision
);
4793 download_start(struct tab
*t
, struct download
*d
, int flag
)
4795 WebKitNetworkRequest
*req
;
4797 const gchar
*suggested_name
;
4798 gchar
*filename
= NULL
;
4803 if (d
== NULL
|| t
== NULL
) {
4804 show_oops(NULL
, "%s invalid parameters", __func__
);
4808 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
4809 if (suggested_name
== NULL
)
4810 return (FALSE
); /* abort download */
4821 filename
= g_strdup_printf("%d%s", i
, suggested_name
);
4824 uri
= g_strdup_printf("%s\\%s", download_dir
, i
?
4825 filename
: suggested_name
);
4827 uri
= g_strdup_printf("file://%s/%s", download_dir
, i
?
4828 filename
: suggested_name
);
4832 } while (!stat(uri
, &sb
));
4834 } while (!stat(uri
+ strlen("file://"), &sb
)); /* XXX is the + strlen right? */
4837 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d filename %s "
4838 "local %s\n", __func__
, t
->tab_id
, filename
, uri
);
4840 /* if we're restarting the download, or starting
4841 * it after doing something else, we need to recreate
4842 * the download request.
4844 if (flag
== XT_DL_RESTART
) {
4845 req
= webkit_network_request_new(webkit_download_get_uri(d
->download
));
4846 webkit_download_cancel(d
->download
);
4847 g_object_unref(d
->download
);
4848 d
->download
= webkit_download_new(req
);
4851 webkit_download_set_destination_uri(d
->download
, uri
);
4853 if (webkit_download_get_status(d
->download
) ==
4854 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
4855 show_oops(t
, "%s: download failed to start", __func__
);
4857 gtk_label_set_text(GTK_LABEL(t
->label
), "Download Failed");
4859 /* connect "download first" mime handler */
4860 g_signal_connect(G_OBJECT(d
->download
), "notify::status",
4861 G_CALLBACK(download_status_changed_cb
), NULL
);
4863 /* get from history */
4864 g_object_ref(d
->download
);
4865 gtk_label_set_text(GTK_LABEL(t
->label
), "Downloading");
4866 if (download_notifications
)
4867 show_oops(t
, "Download of '%s' started...",
4868 basename((char *)webkit_download_get_destination_uri(d
->download
)));
4871 if (flag
!= XT_DL_START
)
4872 webkit_download_start(d
->download
);
4874 DNPRINTF(XT_D_DOWNLOAD
, "download status : %d",
4875 webkit_download_get_status(d
->download
));
4877 /* sync other download manager tabs */
4878 update_download_tabs(NULL
);
4890 download_ask_cb(struct tab
*t
, GdkEventKey
*e
, gpointer data
)
4892 struct download
*d
= data
;
4896 t
->mode_cb_data
= NULL
;
4899 e
->keyval
= GDK_Escape
;
4900 return (XT_CB_PASSTHROUGH
);
4903 DPRINTF("download_ask_cb: User pressed %c\n", e
->keyval
);
4904 if (e
->keyval
== 'y' || e
->keyval
== 'Y' || e
->keyval
== GDK_Return
)
4905 /* We need to do a RESTART, because we're not calling from
4906 * webview_download_cb
4908 download_start(t
, d
, XT_DL_RESTART
);
4910 /* for all other keyvals, we just let the download be */
4911 e
->keyval
= GDK_Escape
;
4912 return (XT_CB_HANDLED
);
4916 download_ask(struct tab
*t
, struct download
*d
)
4918 const gchar
*suggested_name
;
4920 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
4921 if (suggested_name
== NULL
)
4922 return (FALSE
); /* abort download */
4924 show_oops(t
, "download file %s [y/n] ?", suggested_name
);
4925 t
->mode_cb
= download_ask_cb
;
4926 t
->mode_cb_data
= d
;
4932 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
4935 const gchar
*suggested_name
;
4936 struct download
*download_entry
;
4939 if (wk_download
== NULL
|| t
== NULL
) {
4940 show_oops(NULL
, "%s invalid parameters", __func__
);
4944 suggested_name
= webkit_download_get_suggested_filename(wk_download
);
4945 if (suggested_name
== NULL
)
4946 return (FALSE
); /* abort download */
4948 download_entry
= g_malloc(sizeof(struct download
));
4949 download_entry
->download
= wk_download
;
4950 download_entry
->tab
= t
;
4951 download_entry
->id
= next_download_id
++;
4952 RB_INSERT(download_list
, &downloads
, download_entry
);
4954 if (download_mode
== XT_DM_START
)
4955 ret
= download_start(t
, download_entry
, XT_DL_START
);
4956 else if (download_mode
== XT_DM_ASK
)
4957 ret
= download_ask(t
, download_entry
);
4958 else if (download_mode
== XT_DM_ADD
)
4959 show_oops(t
, "added %s to download manager",
4962 /* sync other download manager tabs */
4963 update_download_tabs(NULL
);
4966 * NOTE: never redirect/render the current tab before this
4967 * function returns. This will cause the download to never start.
4969 return (ret
); /* start download */
4973 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
4975 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
4978 show_oops(NULL
, "webview_hover_cb");
4983 set_status(t
, uri
, XT_STATUS_LINK
);
4986 set_status(t
, t
->status
, XT_STATUS_NOTHING
);
4991 mark(struct tab
*t
, struct karg
*arg
)
4998 if ((index
= marktoindex(mark
)) == -1)
5001 if (arg
->i
== XT_MARK_SET
)
5002 t
->mark
[index
] = gtk_adjustment_get_value(t
->adjust_v
);
5003 else if (arg
->i
== XT_MARK_GOTO
) {
5004 if (t
->mark
[index
] == XT_INVALID_MARK
) {
5005 show_oops(t
, "mark '%c' does not exist", mark
);
5008 /* XXX t->mark[index] can be bigger than the maximum if ajax or
5009 something changes the document size */
5010 pos
= gtk_adjustment_get_value(t
->adjust_v
);
5011 gtk_adjustment_set_value(t
->adjust_v
, t
->mark
[index
]);
5012 t
->mark
[marktoindex('\'')] = pos
;
5019 marks_clear(struct tab
*t
)
5023 for (i
= 0; i
< LENGTH(t
->mark
); i
++)
5024 t
->mark
[i
] = XT_INVALID_MARK
;
5030 char file
[PATH_MAX
];
5031 char *line
= NULL
, *p
;
5036 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
5037 if ((f
= fopen(file
, "r+")) == NULL
) {
5038 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
5042 for (i
= 1; ; i
++) {
5043 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
5045 if (strlen(line
) == 0 || line
[0] == '#') {
5051 p
= strtok(line
, " \t");
5053 if (p
== NULL
|| strlen(p
) != 1 ||
5054 (index
= qmarktoindex(*p
)) == -1) {
5055 warnx("corrupt quickmarks file, line %d", i
);
5059 p
= strtok(NULL
, " \t");
5060 if (qmarks
[index
] != NULL
)
5061 g_free(qmarks
[index
]);
5062 qmarks
[index
] = g_strdup(p
);
5073 char file
[PATH_MAX
];
5077 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
5078 if ((f
= fopen(file
, "r+")) == NULL
) {
5079 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
5083 for (i
= 0; i
< XT_NOQMARKS
; i
++)
5084 if (qmarks
[i
] != NULL
)
5085 fprintf(f
, "%c %s\n", indextoqmark(i
), qmarks
[i
]);
5093 qmark(struct tab
*t
, struct karg
*arg
)
5098 mark
= arg
->s
[strlen(arg
->s
)-1];
5099 index
= qmarktoindex(mark
);
5105 if (qmarks
[index
] != NULL
) {
5106 g_free(qmarks
[index
]);
5107 qmarks
[index
] = NULL
;
5110 qmarks_load(); /* sync if multiple instances */
5111 qmarks
[index
] = g_strdup(get_uri(t
));
5115 if (qmarks
[index
] != NULL
)
5116 load_uri(t
, qmarks
[index
]);
5118 show_oops(t
, "quickmark \"%c\" does not exist",
5124 if (qmarks
[index
] != NULL
)
5125 create_new_tab(qmarks
[index
], NULL
, 1, -1);
5127 show_oops(t
, "quickmark \"%c\" does not exist",
5138 go_up(struct tab
*t
, struct karg
*args
)
5146 if (args
->i
== XT_GO_UP_ROOT
)
5147 levels
= XT_GO_UP_ROOT
;
5148 else if ((levels
= atoi(args
->s
)) == 0)
5151 uri
= g_strdup(get_uri(t
));
5155 if ((tmp
= strstr(uri
, XT_PROTO_DELIM
)) == NULL
)
5158 tmp
+= strlen(XT_PROTO_DELIM
);
5160 /* it makes no sense to strip the last slash from ".../dir/", skip it */
5161 lastidx
= strlen(tmp
) - 1;
5163 if (tmp
[lastidx
] == '/')
5164 tmp
[lastidx
] = '\0';
5168 p
= strrchr(tmp
, '/');
5169 if (p
== tmp
) { /* Are we at the root of a file://-path? */
5172 } else if (p
!= NULL
)
5185 gototab(struct tab
*t
, struct karg
*args
)
5188 struct karg arg
= {0, NULL
, -1};
5190 tab
= atoi(args
->s
);
5193 arg
.i
= XT_TAB_NEXT
;
5205 zoom_amount(struct tab
*t
, struct karg
*arg
)
5207 struct karg narg
= {0, NULL
, -1};
5209 narg
.i
= atoi(arg
->s
);
5210 resizetab(t
, &narg
);
5216 flip_colon(struct tab
*t
, struct karg
*arg
)
5218 struct karg narg
= {0, NULL
, -1};
5221 if (t
== NULL
|| arg
== NULL
)
5224 p
= strstr(arg
->s
, ":");
5236 /* buffer commands receive the regex that triggered them in arg.s */
5237 char bcmd
[XT_BUFCMD_SZ
];
5241 #define XT_PRE_NO (0)
5242 #define XT_PRE_YES (1)
5243 #define XT_PRE_MAYBE (2)
5245 int (*func
)(struct tab
*, struct karg
*);
5249 { "^[0-9]*gu$", XT_PRE_MAYBE
, "gu", go_up
, 0 },
5250 { "^gU$", XT_PRE_NO
, "gU", go_up
, XT_GO_UP_ROOT
},
5251 { "^gg$", XT_PRE_NO
, "gg", move
, XT_MOVE_TOP
},
5252 { "^gG$", XT_PRE_NO
, "gG", move
, XT_MOVE_BOTTOM
},
5253 { "^[0-9]+%$", XT_PRE_YES
, "%", move
, XT_MOVE_PERCENT
},
5254 { "^zz$", XT_PRE_NO
, "zz", move
, XT_MOVE_CENTER
},
5255 { "^gh$", XT_PRE_NO
, "gh", go_home
, 0 },
5256 { "^m[a-zA-Z0-9]$", XT_PRE_NO
, "m", mark
, XT_MARK_SET
},
5257 { "^['][a-zA-Z0-9']$", XT_PRE_NO
, "'", mark
, XT_MARK_GOTO
},
5258 { "^[0-9]+t$", XT_PRE_YES
, "t", gototab
, 0 },
5259 { "^g0$", XT_PRE_YES
, "g0", movetab
, XT_TAB_FIRST
},
5260 { "^g[$]$", XT_PRE_YES
, "g$", movetab
, XT_TAB_LAST
},
5261 { "^[0-9]*gt$", XT_PRE_YES
, "t", movetab
, XT_TAB_NEXT
},
5262 { "^[0-9]*gT$", XT_PRE_YES
, "T", movetab
, XT_TAB_PREV
},
5263 { "^M[a-zA-Z0-9]$", XT_PRE_NO
, "M", qmark
, XT_QMARK_SET
},
5264 { "^go[a-zA-Z0-9]$", XT_PRE_NO
, "go", qmark
, XT_QMARK_OPEN
},
5265 { "^gn[a-zA-Z0-9]$", XT_PRE_NO
, "gn", qmark
, XT_QMARK_TAB
},
5266 { "^ZR$", XT_PRE_NO
, "ZR", restart
, 0 },
5267 { "^ZZ$", XT_PRE_NO
, "ZZ", quit
, 0 },
5268 { "^zi$", XT_PRE_NO
, "zi", resizetab
, XT_ZOOM_IN
},
5269 { "^zo$", XT_PRE_NO
, "zo", resizetab
, XT_ZOOM_OUT
},
5270 { "^z0$", XT_PRE_NO
, "z0", resizetab
, XT_ZOOM_NORMAL
},
5271 { "^[0-9]+Z$", XT_PRE_YES
, "Z", zoom_amount
, 0 },
5272 { "^[0-9]+:$", XT_PRE_YES
, ":", flip_colon
, 0 },
5276 buffercmd_init(void)
5280 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5281 if (regcomp(&buffercmds
[i
].cregex
, buffercmds
[i
].regex
,
5282 REG_EXTENDED
| REG_NOSUB
))
5283 startpage_add("invalid buffercmd regex %s",
5284 buffercmds
[i
].regex
);
5288 buffercmd_abort(struct tab
*t
)
5295 DNPRINTF(XT_D_BUFFERCMD
, "%s: clearing buffer\n", __func__
);
5297 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5300 cmd_prefix
= 0; /* clear prefix for non-buffer commands */
5301 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
5305 buffercmd_execute(struct tab
*t
, struct buffercmd
*cmd
)
5307 struct karg arg
= {0, NULL
, -1};
5310 arg
.s
= g_strdup(bcmd
);
5312 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_execute: buffer \"%s\" "
5313 "matches regex \"%s\", executing\n", bcmd
, cmd
->regex
);
5323 buffercmd_addkey(struct tab
*t
, guint keyval
)
5326 char s
[XT_BUFCMD_SZ
];
5328 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
))) {
5330 return (XT_CB_PASSTHROUGH
);
5333 if (keyval
== GDK_Escape
) {
5335 return (XT_CB_HANDLED
);
5338 /* key with modifier or non-ascii character */
5339 if (!isascii(keyval
)) {
5341 * XXX this looks wrong but fixes some sites like
5342 * http://www.seslisozluk.com/
5343 * that eat a shift or ctrl and end putting default focus in js
5344 * instead of ignoring the keystroke
5345 * so instead of return (XT_CB_PASSTHROUGH); eat the key
5347 return (XT_CB_HANDLED
);
5350 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: adding key \"%c\" "
5351 "to buffer \"%s\"\n", keyval
, bcmd
);
5353 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5354 if (bcmd
[i
] == '\0') {
5359 /* buffer full, ignore input */
5360 if (i
>= LENGTH(bcmd
) -1) {
5361 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: buffer full\n");
5363 return (XT_CB_HANDLED
);
5366 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
5368 /* find exact match */
5369 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5370 if (regexec(&buffercmds
[i
].cregex
, bcmd
,
5371 (size_t) 0, NULL
, 0) == 0) {
5372 buffercmd_execute(t
, &buffercmds
[i
]);
5376 /* find non exact matches to see if we need to abort ot not */
5377 for (i
= 0, match
= 0; i
< LENGTH(buffercmds
); i
++) {
5378 DNPRINTF(XT_D_BUFFERCMD
, "trying: %s\n", bcmd
);
5381 if (buffercmds
[i
].precount
== XT_PRE_MAYBE
) {
5382 if (isdigit(bcmd
[0])) {
5383 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5387 if (sscanf(bcmd
, "%s", s
) == 0)
5390 } else if (buffercmds
[i
].precount
== XT_PRE_YES
) {
5391 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5394 if (sscanf(bcmd
, "%s", s
) == 0)
5397 if (c
== -1 && buffercmds
[i
].precount
)
5399 if (!strncmp(s
, buffercmds
[i
].cmd
, strlen(s
)))
5402 DNPRINTF(XT_D_BUFFERCMD
, "got[%d] %d <%s>: %d %s\n",
5403 i
, match
, buffercmds
[i
].cmd
, c
, s
);
5406 DNPRINTF(XT_D_BUFFERCMD
, "aborting: %s\n", bcmd
);
5411 return (XT_CB_HANDLED
);
5415 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
5417 struct key_binding
*k
;
5419 /* handle keybindings if buffercmd is empty.
5420 if not empty, allow commands like C-n */
5421 if (bcmd
[0] == '\0' || ((e
->state
& (CTRL
| MOD1
)) != 0))
5422 TAILQ_FOREACH(k
, &kbl
, entry
)
5423 if (e
->keyval
== k
->key
5424 && (entry
? k
->use_in_entry
: 1)) {
5425 /* when we are edditing eat ctrl/mod keys */
5426 if (edit_mode
== XT_EM_VI
&&
5427 t
->mode
== XT_MODE_INSERT
&&
5428 (e
->state
& CTRL
|| e
->state
& MOD1
))
5429 return (XT_CB_PASSTHROUGH
);
5432 if ((e
->state
& (CTRL
| MOD1
)) == 0)
5433 return (cmd_execute(t
, k
->cmd
));
5434 } else if ((e
->state
& k
->mask
) == k
->mask
) {
5435 return (cmd_execute(t
, k
->cmd
));
5439 if (!entry
&& ((e
->state
& (CTRL
| MOD1
)) == 0))
5440 return buffercmd_addkey(t
, e
->keyval
);
5442 return (XT_CB_PASSTHROUGH
);
5446 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5450 /* don't use w directly; use t->whatever instead */
5453 show_oops(NULL
, "wv_keypress_cb");
5454 return (XT_CB_PASSTHROUGH
);
5460 return t
->mode_cb(t
, e
, t
->mode_cb_data
);
5462 DNPRINTF(XT_D_KEY
, "wv_keypress_cb: mode %d keyval 0x%x mask "
5463 "0x%x tab %d\n", t
->mode
, e
->keyval
, e
->state
, t
->tab_id
);
5465 /* Hide buffers, if they are visible, with escape. */
5466 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)) &&
5467 CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
5468 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5470 return (XT_CB_HANDLED
);
5473 if (t
->mode
== XT_MODE_HINT
)
5474 return (XT_CB_HANDLED
);
5476 if ((CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Tab
) ||
5477 (CLEAN(e
->state
) == SHFT
&& e
->keyval
== GDK_Tab
))
5478 /* something focussy is about to happen */
5479 return (XT_CB_PASSTHROUGH
);
5481 /* check if we are some sort of text input thing in the dom */
5482 input_check_mode(t
);
5484 if (t
->mode
== XT_MODE_HINT
) {
5485 /* XXX make sure cmd entry is enabled */
5486 return (XT_CB_HANDLED
);
5487 } else if (t
->mode
== XT_MODE_PASSTHROUGH
) {
5488 if (CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
)
5489 t
->mode
= XT_MODE_COMMAND
;
5490 return (XT_CB_PASSTHROUGH
);
5491 } else if (t
->mode
== XT_MODE_COMMAND
) {
5493 snprintf(s
, sizeof s
, "%c", e
->keyval
);
5494 if (CLEAN(e
->state
) == 0 && isdigit(s
[0]))
5495 cmd_prefix
= 10 * cmd_prefix
+ atoi(s
);
5496 return (handle_keypress(t
, e
, 0));
5499 return (handle_keypress(t
, e
, 1));
5503 return (XT_CB_PASSTHROUGH
);
5507 hint_continue(struct tab
*t
)
5509 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5511 const gchar
*errstr
= NULL
;
5515 if (!(c
[0] == '.' || c
[0] == ','))
5517 if (strlen(c
) == 1) {
5518 /* XXX should not happen */
5523 if (isdigit(c
[1])) {
5525 i
= strtonum(&c
[1], 1, 4096, &errstr
);
5527 show_oops(t
, "invalid numerical hint %s", &c
[1]);
5530 s
= g_strdup_printf("hints.updateHints(%d);", i
);
5534 /* alphanumeric input */
5535 s
= g_strdup_printf("hints.createHints('%s', '%c');",
5536 &c
[1], c
[0] == '.' ? 'f' : 'F');
5547 search_continue(struct tab
*t
)
5549 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5550 gboolean rv
= FALSE
;
5552 if (c
[0] == ':' || c
[0] == '.' || c
[0] == ',')
5554 if (strlen(c
) == 1) {
5555 webkit_web_view_unmark_text_matches(t
->wv
);
5560 t
->search_forward
= TRUE
;
5561 else if (c
[0] == '?')
5562 t
->search_forward
= FALSE
;
5572 search_cb(struct tab
*t
)
5574 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5577 if (search_continue(t
) == FALSE
)
5581 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, t
->search_forward
,
5583 /* not found, mark red */
5584 gdk_color_parse(XT_COLOR_RED
, &color
);
5585 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
5586 /* unmark and remove selection */
5587 webkit_web_view_unmark_text_matches(t
->wv
);
5588 /* my kingdom for a way to unselect text in webview */
5590 /* found, highlight all */
5591 webkit_web_view_unmark_text_matches(t
->wv
);
5592 webkit_web_view_mark_text_matches(t
->wv
, &c
[1], FALSE
, 0);
5593 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
5594 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
5595 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
5603 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5605 const gchar
*c
= gtk_entry_get_text(w
);
5608 show_oops(NULL
, "cmd_keyrelease_cb invalid parameters");
5609 return (XT_CB_PASSTHROUGH
);
5612 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
5613 e
->keyval
, e
->state
, t
->tab_id
);
5616 if (!(e
->keyval
== GDK_Tab
|| e
->keyval
== GDK_ISO_Left_Tab
)) {
5617 if (hint_continue(t
) == FALSE
)
5622 if (search_continue(t
) == FALSE
)
5625 /* if search length is > 4 then no longer play timeout games */
5626 if (strlen(c
) > 4) {
5628 g_source_remove(t
->search_id
);
5635 /* reestablish a new timer if the user types fast */
5637 g_source_remove(t
->search_id
);
5638 t
->search_id
= g_timeout_add(250, (GSourceFunc
)search_cb
, (gpointer
)t
);
5641 return (XT_CB_PASSTHROUGH
);
5645 match_uri(const gchar
*uri
, const gchar
*key
) {
5648 gboolean match
= FALSE
;
5652 if (!strncmp(key
, uri
, len
))
5655 voffset
= strstr(uri
, "/") + 2;
5656 if (!strncmp(key
, voffset
, len
))
5658 else if (g_str_has_prefix(voffset
, "www.")) {
5659 voffset
= voffset
+ strlen("www.");
5660 if (!strncmp(key
, voffset
, len
))
5669 match_session(const gchar
*name
, const gchar
*key
) {
5672 sub
= strcasestr(name
, key
);
5678 cmd_getlist(int id
, char *key
)
5685 if (cmds
[id
].type
& XT_URLARG
) {
5686 RB_FOREACH_REVERSE(h
, history_list
, &hl
)
5687 if (match_uri(h
->uri
, key
)) {
5688 cmd_status
.list
[c
] = (char *)h
->uri
;
5694 } else if (cmds
[id
].type
& XT_SESSARG
) {
5695 TAILQ_FOREACH(s
, &sessions
, entry
)
5696 if (match_session(s
->name
, key
)) {
5697 cmd_status
.list
[c
] = (char *)s
->name
;
5703 } else if (cmds
[id
].type
& XT_SETARG
) {
5704 for (i
= 0; i
< get_settings_size(); i
++)
5705 if (!strncmp(key
, get_setting_name(i
),
5707 cmd_status
.list
[c
++] =
5708 get_setting_name(i
);
5714 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
5716 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
5717 if (cmds
[i
].level
< dep
)
5719 if (cmds
[i
].level
== dep
&& !strncmp(key
, cmds
[i
].cmd
,
5720 strlen(key
)) && !isdigit(cmds
[i
].cmd
[0]))
5721 cmd_status
.list
[c
++] = cmds
[i
].cmd
;
5729 cmd_getnext(int dir
)
5731 cmd_status
.index
+= dir
;
5733 if (cmd_status
.index
< 0)
5734 cmd_status
.index
= cmd_status
.len
- 1;
5735 else if (cmd_status
.index
>= cmd_status
.len
)
5736 cmd_status
.index
= 0;
5738 return cmd_status
.list
[cmd_status
.index
];
5742 cmd_tokenize(char *s
, char *tokens
[])
5745 char *tok
, *last
= NULL
;
5746 size_t len
= strlen(s
);
5749 blank
= len
== 0 || (len
> 0 && s
[len
- 1] == ' ');
5750 for (tok
= strtok_r(s
, " ", &last
); tok
&& i
< 3;
5751 tok
= strtok_r(NULL
, " ", &last
), i
++)
5761 cmd_complete(struct tab
*t
, char *str
, int dir
)
5763 GtkEntry
*w
= GTK_ENTRY(t
->cmd
);
5764 int i
, j
, levels
, c
= 0, dep
= 0, parent
= -1;
5766 char *tok
, *match
, *s
= g_strdup(str
);
5768 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
5771 DNPRINTF(XT_D_CMD
, "%s: complete %s\n", __func__
, str
);
5774 for (i
= 0; isdigit(s
[i
]); i
++)
5777 for (; isspace(s
[i
]); i
++)
5782 levels
= cmd_tokenize(s
, tokens
);
5784 for (i
= 0; i
< levels
- 1; i
++) {
5787 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5788 if (cmds
[j
].level
< dep
)
5790 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
,
5794 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
5801 if (matchcount
== 1) {
5802 strlcat(res
, tok
, sizeof res
);
5803 strlcat(res
, " ", sizeof res
);
5813 if (cmd_status
.index
== -1)
5814 cmd_getlist(parent
, tokens
[i
]);
5816 if (cmd_status
.len
> 0) {
5817 match
= cmd_getnext(dir
);
5818 strlcat(res
, match
, sizeof res
);
5819 gtk_entry_set_text(w
, res
);
5820 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5827 cmd_execute(struct tab
*t
, char *str
)
5829 struct cmd
*cmd
= NULL
;
5830 char *tok
, *last
= NULL
, *s
= g_strdup(str
), *sc
;
5832 int j
, len
, c
= 0, dep
= 0, matchcount
= 0;
5833 int prefix
= -1, rv
= XT_CB_PASSTHROUGH
;
5834 struct karg arg
= {0, NULL
, -1};
5839 for (j
= 0; j
<3 && isdigit(s
[j
]); j
++)
5845 while (isspace(s
[0]))
5848 if (strlen(s
) > 0 && strlen(prefixstr
) > 0)
5849 prefix
= atoi(prefixstr
);
5853 for (tok
= strtok_r(s
, " ", &last
); tok
;
5854 tok
= strtok_r(NULL
, " ", &last
)) {
5856 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5857 if (cmds
[j
].level
< dep
)
5859 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1 :
5861 if (cmds
[j
].level
== dep
&&
5862 !strncmp(tok
, cmds
[j
].cmd
, len
)) {
5866 if (len
== strlen(cmds
[j
].cmd
)) {
5872 if (matchcount
== 1) {
5877 show_oops(t
, "Invalid command: %s", str
);
5883 show_oops(t
, "Empty command");
5889 arg
.precount
= prefix
;
5890 else if (cmd_prefix
> 0)
5891 arg
.precount
= cmd_prefix
;
5893 if (j
> 0 && !(cmd
->type
& XT_PREFIX
) && arg
.precount
> -1) {
5894 show_oops(t
, "No prefix allowed: %s", str
);
5898 arg
.s
= last
? g_strdup(last
) : g_strdup("");
5899 if (cmd
->type
& XT_INTARG
&& last
&& strlen(last
) > 0) {
5900 if (arg
.s
== NULL
) {
5901 show_oops(t
, "Invalid command");
5904 arg
.precount
= atoi(arg
.s
);
5905 if (arg
.precount
<= 0) {
5906 if (arg
.s
[0] == '0')
5907 show_oops(t
, "Zero count");
5909 show_oops(t
, "Trailing characters");
5914 DNPRINTF(XT_D_CMD
, "%s: prefix %d arg %s\n",
5915 __func__
, arg
.precount
, arg
.s
);
5931 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5934 show_oops(NULL
, "entry_key_cb invalid parameters");
5935 return (XT_CB_PASSTHROUGH
);
5938 DNPRINTF(XT_D_CMD
, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
5939 e
->keyval
, e
->state
, t
->tab_id
);
5943 if (e
->keyval
== GDK_Escape
) {
5944 /* don't use focus_webview(t) because we want to type :cmds */
5945 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5948 return (handle_keypress(t
, e
, 1));
5951 struct command_entry
*
5952 history_prev(struct command_list
*l
, struct command_entry
*at
)
5955 at
= TAILQ_LAST(l
, command_list
);
5957 at
= TAILQ_PREV(at
, command_list
, entry
);
5959 at
= TAILQ_LAST(l
, command_list
);
5965 struct command_entry
*
5966 history_next(struct command_list
*l
, struct command_entry
*at
)
5969 at
= TAILQ_FIRST(l
);
5971 at
= TAILQ_NEXT(at
, entry
);
5973 at
= TAILQ_FIRST(l
);
5980 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5982 int rv
= XT_CB_HANDLED
;
5983 const gchar
*c
= gtk_entry_get_text(w
);
5987 show_oops(NULL
, "cmd_keypress_cb parameters");
5988 return (XT_CB_PASSTHROUGH
);
5991 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
5992 e
->keyval
, e
->state
, t
->tab_id
);
5996 e
->keyval
= GDK_Escape
;
5997 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5998 c
[0] == '.' || c
[0] == ','))
5999 e
->keyval
= GDK_Escape
;
6001 if (e
->keyval
!= GDK_Tab
&& e
->keyval
!= GDK_Shift_L
&&
6002 e
->keyval
!= GDK_ISO_Left_Tab
)
6003 cmd_status
.index
= -1;
6005 switch (e
->keyval
) {
6008 cmd_complete(t
, (char *)&c
[1], 1);
6009 else if (c
[0] == '.' || c
[0] == ',')
6010 run_script(t
, "hints.focusNextHint();");
6012 case GDK_ISO_Left_Tab
:
6014 cmd_complete(t
, (char *)&c
[1], -1);
6015 else if (c
[0] == '.' || c
[0] == ',')
6016 run_script(t
, "hints.focusPreviousHint();");
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_prev(&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);
6041 if ((search_at
= history_next(&shl
, search_at
))) {
6042 search_at
->line
[0] = c
[0];
6043 gtk_entry_set_text(w
, search_at
->line
);
6044 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6046 } else if (c
[0] == '?') {
6047 if ((search_at
= history_prev(&shl
, search_at
))) {
6048 search_at
->line
[0] = c
[0];
6049 gtk_entry_set_text(w
, search_at
->line
);
6050 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6052 } if (c
[0] == ':') {
6053 if ((history_at
= history_next(&chl
, history_at
))) {
6054 history_at
->line
[0] = c
[0];
6055 gtk_entry_set_text(w
, history_at
->line
);
6056 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6061 if (!(!strcmp(c
, ":") || !strcmp(c
, "/") || !strcmp(c
, "?") ||
6062 !strcmp(c
, ".") || !strcmp(c
, ","))) {
6063 /* see if we are doing hinting and reset it */
6064 if (c
[0] == '.' || c
[0] == ',') {
6065 /* recreate hints */
6066 s
= g_strdup_printf("hints.createHints('', "
6067 "'%c');", c
[0] == '.' ? 'f' : 'F');
6080 if (c
!= NULL
&& (c
[0] == '/' || c
[0] == '?'))
6081 webkit_web_view_unmark_text_matches(t
->wv
);
6083 /* no need to cancel hints */
6087 rv
= XT_CB_PASSTHROUGH
;
6093 wv_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
6095 DNPRINTF(XT_D_CMD
, "wv_popup_cb: tab %d\n", t
->tab_id
);
6099 cmd_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
6101 /* popup menu enabled */
6106 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
6109 show_oops(NULL
, "cmd_focusout_cb invalid parameters");
6110 return (XT_CB_PASSTHROUGH
);
6113 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d popup %d\n",
6114 t
->tab_id
, t
->popup
);
6116 /* if popup is enabled don't lose focus */
6119 return (XT_CB_PASSTHROUGH
);
6126 if (show_url
== 0 || t
->focus_wv
)
6129 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6131 return (XT_CB_PASSTHROUGH
);
6135 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
6138 const gchar
*c
= gtk_entry_get_text(entry
);
6141 show_oops(NULL
, "cmd_activate_cb invalid parameters");
6145 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
6150 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
6151 c
[0] == '.' || c
[0] == ','))
6157 if (c
[0] == '/' || c
[0] == '?') {
6158 /* see if there is a timer pending */
6160 g_source_remove(t
->search_id
);
6165 if (t
->search_text
) {
6166 g_free(t
->search_text
);
6167 t
->search_text
= NULL
;
6170 t
->search_text
= g_strdup(s
);
6172 g_free(global_search
);
6173 global_search
= g_strdup(s
);
6174 t
->search_forward
= c
[0] == '/';
6176 history_add(&shl
, search_file
, s
, &search_history_count
);
6177 } else if (c
[0] == '.' || c
[0] == ',') {
6178 run_script(t
, "hints.fire();");
6179 /* XXX history for link following? */
6180 } else if (c
[0] == ':') {
6181 history_add(&chl
, command_file
, s
, &cmd_history_count
);
6182 /* can't call hide_cmd after cmd_execute */
6193 backward_cb(GtkWidget
*w
, struct tab
*t
)
6198 show_oops(NULL
, "backward_cb invalid parameters");
6202 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
6209 forward_cb(GtkWidget
*w
, struct tab
*t
)
6214 show_oops(NULL
, "forward_cb invalid parameters");
6218 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
6220 a
.i
= XT_NAV_FORWARD
;
6225 home_cb(GtkWidget
*w
, struct tab
*t
)
6228 show_oops(NULL
, "home_cb invalid parameters");
6232 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
6238 stop_cb(GtkWidget
*w
, struct tab
*t
)
6240 WebKitWebFrame
*frame
;
6243 show_oops(NULL
, "stop_cb invalid parameters");
6247 DNPRINTF(XT_D_NAV
, "stop_cb: tab %d\n", t
->tab_id
);
6249 frame
= webkit_web_view_get_main_frame(t
->wv
);
6250 if (frame
== NULL
) {
6251 show_oops(t
, "stop_cb: no frame");
6255 webkit_web_frame_stop_loading(frame
);
6256 abort_favicon_download(t
);
6260 setup_webkit(struct tab
*t
)
6262 if (is_g_object_setting(G_OBJECT(t
->settings
), "enable-dns-prefetching"))
6263 g_object_set(G_OBJECT(t
->settings
), "enable-dns-prefetching",
6264 FALSE
, (char *)NULL
);
6266 warnx("webkit does not have \"enable-dns-prefetching\" property");
6267 g_object_set(G_OBJECT(t
->settings
),
6268 "user-agent", t
->user_agent
, (char *)NULL
);
6269 g_object_set(G_OBJECT(t
->settings
),
6270 "enable-scripts", enable_scripts
, (char *)NULL
);
6271 g_object_set(G_OBJECT(t
->settings
),
6272 "enable-plugins", enable_plugins
, (char *)NULL
);
6273 g_object_set(G_OBJECT(t
->settings
),
6274 "javascript-can-open-windows-automatically", enable_scripts
,
6276 g_object_set(G_OBJECT(t
->settings
),
6277 "enable-html5-database", FALSE
, (char *)NULL
);
6278 g_object_set(G_OBJECT(t
->settings
),
6279 "enable-html5-local-storage", enable_localstorage
, (char *)NULL
);
6280 g_object_set(G_OBJECT(t
->settings
),
6281 "enable_spell_checking", enable_spell_checking
, (char *)NULL
);
6282 g_object_set(G_OBJECT(t
->settings
),
6283 "spell_checking_languages", spell_check_languages
, (char *)NULL
);
6284 g_object_set(G_OBJECT(t
->settings
),
6285 "enable-developer-extras", TRUE
, (char *)NULL
);
6286 g_object_set(G_OBJECT(t
->wv
),
6287 "full-content-zoom", TRUE
, (char *)NULL
);
6288 g_object_set(G_OBJECT(t
->settings
),
6289 "auto-load-images", auto_load_images
, (char *)NULL
);
6291 webkit_web_view_set_settings(t
->wv
, t
->settings
);
6295 update_statusbar_position(GtkAdjustment
* adjustment
, gpointer data
)
6297 struct tab
*ti
, *t
= NULL
;
6298 gdouble view_size
, value
, max
;
6301 TAILQ_FOREACH(ti
, &tabs
, entry
)
6302 if (ti
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
6310 if (adjustment
== NULL
)
6311 adjustment
= gtk_scrolled_window_get_vadjustment(
6312 GTK_SCROLLED_WINDOW(t
->browser_win
));
6314 view_size
= gtk_adjustment_get_page_size(adjustment
);
6315 value
= gtk_adjustment_get_value(adjustment
);
6316 max
= gtk_adjustment_get_upper(adjustment
) - view_size
;
6319 position
= g_strdup("All");
6320 else if (value
== max
)
6321 position
= g_strdup("Bot");
6322 else if (value
== 0)
6323 position
= g_strdup("Top");
6325 position
= g_strdup_printf("%d%%", (int) ((value
/ max
) * 100));
6327 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.position
), position
);
6334 create_window(const gchar
*name
)
6338 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
6339 if (window_maximize
)
6340 gtk_window_maximize(GTK_WINDOW(w
));
6342 gtk_window_set_default_size(GTK_WINDOW(w
), window_width
, window_height
);
6343 gtk_widget_set_name(w
, name
);
6344 gtk_window_set_wmclass(GTK_WINDOW(w
), name
, "XXXTerm");
6350 create_browser(struct tab
*t
)
6354 GtkAdjustment
*adjustment
;
6357 show_oops(NULL
, "create_browser invalid parameters");
6361 t
->sb_h
= GTK_SCROLLBAR(gtk_hscrollbar_new(NULL
));
6362 t
->sb_v
= GTK_SCROLLBAR(gtk_vscrollbar_new(NULL
));
6363 t
->adjust_h
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_h
));
6364 t
->adjust_v
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_v
));
6366 w
= gtk_scrolled_window_new(t
->adjust_h
, t
->adjust_v
);
6367 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
6368 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
6370 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
6371 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
6374 t
->settings
= webkit_web_settings_new();
6376 g_object_set(t
->settings
, "default-encoding", encoding
, (char *)NULL
);
6378 if (user_agent
== NULL
) {
6379 g_object_get(G_OBJECT(t
->settings
), "user-agent", &strval
,
6381 t
->user_agent
= g_strdup_printf("%s %s+", strval
, version
);
6384 t
->user_agent
= g_strdup(user_agent
->value
);
6386 t
->stylesheet
= g_strdup_printf("file://%s/style.css", resource_dir
);
6387 t
->load_images
= auto_load_images
;
6390 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w
));
6391 g_signal_connect(G_OBJECT(adjustment
), "value-changed",
6392 G_CALLBACK(update_statusbar_position
), NULL
);
6401 create_kiosk_toolbar(struct tab
*t
)
6403 GtkWidget
*toolbar
= NULL
, *b
;
6405 b
= gtk_hbox_new(FALSE
, 0);
6407 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
6409 /* backward button */
6410 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
6411 gtk_widget_set_sensitive(t
->backward
, FALSE
);
6412 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
6413 G_CALLBACK(backward_cb
), t
);
6414 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, TRUE
, TRUE
, 0);
6416 /* forward button */
6417 t
->forward
= create_button("Forward", GTK_STOCK_GO_FORWARD
, 0);
6418 gtk_widget_set_sensitive(t
->forward
, FALSE
);
6419 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
6420 G_CALLBACK(forward_cb
), t
);
6421 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, TRUE
, TRUE
, 0);
6424 t
->gohome
= create_button("Home", GTK_STOCK_HOME
, 0);
6425 gtk_widget_set_sensitive(t
->gohome
, true);
6426 g_signal_connect(G_OBJECT(t
->gohome
), "clicked",
6427 G_CALLBACK(home_cb
), t
);
6428 gtk_box_pack_start(GTK_BOX(b
), t
->gohome
, TRUE
, TRUE
, 0);
6430 /* create widgets but don't use them */
6431 t
->uri_entry
= gtk_entry_new();
6432 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
6433 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
6434 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
6435 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
6441 create_toolbar(struct tab
*t
)
6443 GtkWidget
*toolbar
= NULL
, *b
, *eb1
;
6445 b
= gtk_hbox_new(FALSE
, 0);
6447 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
6449 /* backward button */
6450 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
6451 gtk_widget_set_sensitive(t
->backward
, FALSE
);
6452 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
6453 G_CALLBACK(backward_cb
), t
);
6454 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, FALSE
, FALSE
, 0);
6456 /* forward button */
6457 t
->forward
= create_button("Forward",GTK_STOCK_GO_FORWARD
, 0);
6458 gtk_widget_set_sensitive(t
->forward
, FALSE
);
6459 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
6460 G_CALLBACK(forward_cb
), t
);
6461 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, FALSE
,
6465 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
6466 gtk_widget_set_sensitive(t
->stop
, FALSE
);
6467 g_signal_connect(G_OBJECT(t
->stop
), "clicked",
6468 G_CALLBACK(stop_cb
), t
);
6469 gtk_box_pack_start(GTK_BOX(b
), t
->stop
, FALSE
,
6473 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
6474 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
6475 gtk_widget_set_sensitive(t
->js_toggle
, TRUE
);
6476 g_signal_connect(G_OBJECT(t
->js_toggle
), "clicked",
6477 G_CALLBACK(js_toggle_cb
), t
);
6478 gtk_box_pack_start(GTK_BOX(b
), t
->js_toggle
, FALSE
, FALSE
, 0);
6480 t
->uri_entry
= gtk_entry_new();
6481 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
6482 G_CALLBACK(activate_uri_entry_cb
), t
);
6483 g_signal_connect(G_OBJECT(t
->uri_entry
), "key-press-event",
6484 G_CALLBACK(entry_key_cb
), t
);
6486 eb1
= gtk_hbox_new(FALSE
, 0);
6487 gtk_container_set_border_width(GTK_CONTAINER(eb1
), 1);
6488 gtk_box_pack_start(GTK_BOX(eb1
), t
->uri_entry
, TRUE
, TRUE
, 0);
6489 gtk_box_pack_start(GTK_BOX(b
), eb1
, TRUE
, TRUE
, 0);
6492 if (search_string
!= NULL
&& strlen(search_string
) != 0) {
6494 t
->search_entry
= gtk_entry_new();
6495 gtk_entry_set_width_chars(GTK_ENTRY(t
->search_entry
), 30);
6496 g_signal_connect(G_OBJECT(t
->search_entry
), "activate",
6497 G_CALLBACK(activate_search_entry_cb
), t
);
6498 g_signal_connect(G_OBJECT(t
->search_entry
), "key-press-event",
6499 G_CALLBACK(entry_key_cb
), t
);
6500 gtk_widget_set_size_request(t
->search_entry
, -1, -1);
6501 eb2
= gtk_hbox_new(FALSE
, 0);
6502 gtk_container_set_border_width(GTK_CONTAINER(eb2
), 1);
6503 gtk_box_pack_start(GTK_BOX(eb2
), t
->search_entry
, TRUE
, TRUE
,
6505 gtk_box_pack_start(GTK_BOX(b
), eb2
, FALSE
, FALSE
, 0);
6507 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
6513 create_buffers(struct tab
*t
)
6515 GtkCellRenderer
*renderer
;
6518 view
= gtk_tree_view_new();
6520 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view
), FALSE
);
6522 renderer
= gtk_cell_renderer_text_new();
6523 gtk_tree_view_insert_column_with_attributes
6524 (GTK_TREE_VIEW(view
), -1, "Id", renderer
, "text", COL_ID
, (char *)NULL
);
6526 renderer
= gtk_cell_renderer_pixbuf_new();
6527 gtk_tree_view_insert_column_with_attributes
6528 (GTK_TREE_VIEW(view
), -1, "Favicon", renderer
, "pixbuf", COL_FAVICON
,
6531 renderer
= gtk_cell_renderer_text_new();
6532 gtk_tree_view_insert_column_with_attributes
6533 (GTK_TREE_VIEW(view
), -1, "Title", renderer
, "text", COL_TITLE
,
6536 gtk_tree_view_set_model
6537 (GTK_TREE_VIEW(view
), GTK_TREE_MODEL(buffers_store
));
6543 row_activated_cb(GtkTreeView
*view
, GtkTreePath
*path
,
6544 GtkTreeViewColumn
*col
, struct tab
*t
)
6549 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6551 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
,
6554 (GTK_TREE_MODEL(buffers_store
), &iter
, COL_ID
, &id
, -1);
6555 set_current_tab(id
- 1);
6561 /* after tab reordering/creation/removal */
6568 TAILQ_FOREACH(t
, &tabs
, entry
) {
6569 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
6570 if (t
->tab_id
> maxid
)
6573 gtk_widget_show(t
->tab_elems
.sep
);
6576 TAILQ_FOREACH(t
, &tabs
, entry
) {
6577 if (t
->tab_id
== maxid
) {
6578 gtk_widget_hide(t
->tab_elems
.sep
);
6584 /* after active tab change */
6586 recolor_compact_tabs(void)
6592 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6593 TAILQ_FOREACH(t
, &tabs
, entry
)
6594 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
,
6597 curid
= gtk_notebook_get_current_page(notebook
);
6598 TAILQ_FOREACH(t
, &tabs
, entry
)
6599 if (t
->tab_id
== curid
) {
6600 gdk_color_parse(XT_COLOR_CT_ACTIVE
, &color
);
6601 gtk_widget_modify_fg(t
->tab_elems
.label
,
6602 GTK_STATE_NORMAL
, &color
);
6608 set_current_tab(int page_num
)
6610 buffercmd_abort(get_current_tab());
6611 gtk_notebook_set_current_page(notebook
, page_num
);
6612 recolor_compact_tabs();
6616 undo_close_tab_save(struct tab
*t
)
6620 struct undo
*u1
, *u2
;
6622 WebKitWebHistoryItem
*item
;
6624 if ((uri
= get_uri(t
)) == NULL
)
6627 u1
= g_malloc0(sizeof(struct undo
));
6628 u1
->uri
= g_strdup(uri
);
6630 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
6632 m
= webkit_web_back_forward_list_get_forward_length(t
->bfl
);
6633 n
= webkit_web_back_forward_list_get_back_length(t
->bfl
);
6636 /* forward history */
6637 items
= webkit_web_back_forward_list_get_forward_list_with_limit(t
->bfl
, m
);
6641 u1
->history
= g_list_prepend(u1
->history
,
6642 webkit_web_history_item_copy(item
));
6643 items
= g_list_next(items
);
6648 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
6649 u1
->history
= g_list_prepend(u1
->history
,
6650 webkit_web_history_item_copy(item
));
6654 items
= webkit_web_back_forward_list_get_back_list_with_limit(t
->bfl
, n
);
6658 u1
->history
= g_list_prepend(u1
->history
,
6659 webkit_web_history_item_copy(item
));
6660 items
= g_list_next(items
);
6663 TAILQ_INSERT_HEAD(&undos
, u1
, entry
);
6665 if (undo_count
> XT_MAX_UNDO_CLOSE_TAB
) {
6666 u2
= TAILQ_LAST(&undos
, undo_tailq
);
6667 TAILQ_REMOVE(&undos
, u2
, entry
);
6669 g_list_free(u2
->history
);
6678 delete_tab(struct tab
*t
)
6682 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
6688 * no need to join thread here because it won't access t on completion
6691 TAILQ_REMOVE(&tabs
, t
, entry
);
6694 /* Halt all webkit activity. */
6695 abort_favicon_download(t
);
6696 webkit_web_view_stop_loading(t
->wv
);
6698 /* Save the tab, so we can undo the close. */
6699 undo_close_tab_save(t
);
6703 g_source_remove(t
->search_id
);
6706 bzero(&a
, sizeof a
);
6708 inspector_cmd(t
, &a
);
6710 if (browser_mode
== XT_BM_KIOSK
) {
6711 gtk_widget_destroy(t
->uri_entry
);
6712 gtk_widget_destroy(t
->stop
);
6713 gtk_widget_destroy(t
->js_toggle
);
6716 gtk_widget_destroy(t
->tab_elems
.eventbox
);
6717 gtk_widget_destroy(t
->vbox
);
6719 g_free(t
->user_agent
);
6720 g_free(t
->stylesheet
);
6725 if (TAILQ_EMPTY(&tabs
)) {
6726 if (browser_mode
== XT_BM_KIOSK
)
6727 create_new_tab(home
, NULL
, 1, -1);
6729 create_new_tab(NULL
, NULL
, 1, -1);
6732 /* recreate session */
6733 if (session_autosave
) {
6734 bzero(&a
, sizeof a
);
6736 save_tabs(NULL
, &a
);
6740 recolor_compact_tabs();
6744 update_statusbar_zoom(struct tab
*t
)
6747 char s
[16] = { '\0' };
6749 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6750 if ((zoom
<= 0.99 || zoom
>= 1.01))
6751 snprintf(s
, sizeof s
, "%d%%", (int)(zoom
* 100));
6752 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.zoom
), s
);
6756 setzoom_webkit(struct tab
*t
, int adjust
)
6758 #define XT_ZOOMPERCENT 0.04
6763 show_oops(NULL
, "setzoom_webkit invalid parameters");
6767 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6768 if (adjust
== XT_ZOOM_IN
)
6769 zoom
+= XT_ZOOMPERCENT
;
6770 else if (adjust
== XT_ZOOM_OUT
)
6771 zoom
-= XT_ZOOMPERCENT
;
6772 else if (adjust
> 0)
6773 zoom
= default_zoom_level
+ adjust
/ 100.0 - 1.0;
6775 show_oops(t
, "setzoom_webkit invalid zoom value");
6779 if (zoom
< XT_ZOOMPERCENT
)
6780 zoom
= XT_ZOOMPERCENT
;
6781 g_object_set(G_OBJECT(t
->wv
), "zoom-level", zoom
, (char *)NULL
);
6782 update_statusbar_zoom(t
);
6786 tab_clicked_cb(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
6788 struct tab
*t
= (struct tab
*) data
;
6790 DNPRINTF(XT_D_TAB
, "tab_clicked_cb: tab: %d\n", t
->tab_id
);
6792 switch (event
->button
) {
6794 set_current_tab(t
->tab_id
);
6805 append_tab(struct tab
*t
)
6810 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
6811 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
6815 create_sbe(int width
)
6819 sbe
= gtk_entry_new();
6820 gtk_entry_set_inner_border(GTK_ENTRY(sbe
), NULL
);
6821 gtk_entry_set_has_frame(GTK_ENTRY(sbe
), FALSE
);
6822 gtk_widget_set_can_focus(GTK_WIDGET(sbe
), FALSE
);
6823 gtk_widget_modify_font(GTK_WIDGET(sbe
), statusbar_font
);
6824 gtk_entry_set_alignment(GTK_ENTRY(sbe
), 1.0);
6825 gtk_widget_set_size_request(sbe
, width
, -1);
6831 create_new_tab(char *title
, struct undo
*u
, int focus
, int position
)
6836 WebKitWebHistoryItem
*item
;
6840 int sbe_p
= 0, sbe_b
= 0,
6843 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
6845 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
6846 DNPRINTF(XT_D_TAB
, "create_new_tab: new tab rejected\n");
6850 t
= g_malloc0(sizeof *t
);
6852 if (title
== NULL
) {
6853 title
= "(untitled)";
6857 t
->vbox
= gtk_vbox_new(FALSE
, 0);
6859 /* label + button for tab */
6860 b
= gtk_hbox_new(FALSE
, 0);
6863 #if GTK_CHECK_VERSION(2, 20, 0)
6864 t
->spinner
= gtk_spinner_new();
6866 t
->label
= gtk_label_new(title
);
6867 bb
= create_button("Close", GTK_STOCK_CLOSE
, 1);
6868 gtk_widget_set_size_request(t
->label
, 100, 0);
6869 gtk_label_set_max_width_chars(GTK_LABEL(t
->label
), 20);
6870 gtk_label_set_ellipsize(GTK_LABEL(t
->label
), PANGO_ELLIPSIZE_END
);
6871 gtk_widget_set_size_request(b
, 130, 0);
6873 gtk_box_pack_start(GTK_BOX(b
), bb
, FALSE
, FALSE
, 0);
6874 gtk_box_pack_start(GTK_BOX(b
), t
->label
, FALSE
, FALSE
, 0);
6875 #if GTK_CHECK_VERSION(2, 20, 0)
6876 gtk_box_pack_start(GTK_BOX(b
), t
->spinner
, FALSE
, FALSE
, 0);
6880 if (browser_mode
== XT_BM_KIOSK
) {
6881 t
->toolbar
= create_kiosk_toolbar(t
);
6882 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
6885 t
->toolbar
= create_toolbar(t
);
6887 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
,
6895 t
->browser_win
= create_browser(t
);
6896 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
6898 /* oops message for user feedback */
6899 t
->oops
= gtk_entry_new();
6900 gtk_entry_set_inner_border(GTK_ENTRY(t
->oops
), NULL
);
6901 gtk_entry_set_has_frame(GTK_ENTRY(t
->oops
), FALSE
);
6902 gtk_widget_set_can_focus(GTK_WIDGET(t
->oops
), FALSE
);
6903 gdk_color_parse(XT_COLOR_RED
, &color
);
6904 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
6905 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
6906 gtk_widget_modify_font(GTK_WIDGET(t
->oops
), oops_font
);
6909 t
->cmd
= gtk_entry_new();
6910 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
6911 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
6912 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
6913 gtk_widget_modify_font(GTK_WIDGET(t
->cmd
), cmd_font
);
6916 t
->statusbar_box
= gtk_hbox_new(FALSE
, 0);
6918 t
->sbe
.statusbar
= gtk_entry_new();
6919 gtk_entry_set_inner_border(GTK_ENTRY(t
->sbe
.statusbar
), NULL
);
6920 gtk_entry_set_has_frame(GTK_ENTRY(t
->sbe
.statusbar
), FALSE
);
6921 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.statusbar
), FALSE
);
6922 gtk_widget_modify_font(GTK_WIDGET(t
->sbe
.statusbar
), statusbar_font
);
6924 /* create these widgets only if specified in statusbar_elems */
6926 t
->sbe
.position
= create_sbe(40);
6927 t
->sbe
.zoom
= create_sbe(40);
6928 t
->sbe
.buffercmd
= create_sbe(60);
6930 statusbar_modify_attr(t
, XT_COLOR_WHITE
, XT_COLOR_BLACK
);
6932 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), t
->sbe
.statusbar
, TRUE
,
6935 /* gtk widgets cannot be added to a box twice. sbe_* variables
6936 make sure of this */
6937 for (p
= statusbar_elems
; *p
!= '\0'; p
++) {
6941 GtkWidget
*sep
= gtk_vseparator_new();
6943 gdk_color_parse(XT_COLOR_SB_SEPARATOR
, &color
);
6944 gtk_widget_modify_bg(sep
, GTK_STATE_NORMAL
, &color
);
6945 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), sep
,
6946 FALSE
, FALSE
, FALSE
);
6951 warnx("flag \"%c\" specified more than "
6952 "once in statusbar_elems\n", *p
);
6956 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6957 t
->sbe
.position
, FALSE
, FALSE
, FALSE
);
6961 warnx("flag \"%c\" specified more than "
6962 "once in statusbar_elems\n", *p
);
6966 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6967 t
->sbe
.buffercmd
, FALSE
, FALSE
, FALSE
);
6971 warnx("flag \"%c\" specified more than "
6972 "once in statusbar_elems\n", *p
);
6976 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6977 t
->sbe
.zoom
, FALSE
, FALSE
, FALSE
);
6980 warnx("illegal flag \"%c\" in statusbar_elems\n", *p
);
6985 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar_box
, FALSE
, FALSE
, 0);
6988 t
->buffers
= create_buffers(t
);
6989 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->buffers
, FALSE
, FALSE
, 0);
6991 /* xtp meaning is normal by default */
6992 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
6994 /* set empty favicon */
6995 xt_icon_from_name(t
, "text-html");
6997 /* and show it all */
6998 gtk_widget_show_all(b
);
6999 gtk_widget_show_all(t
->vbox
);
7001 /* compact tab bar */
7002 t
->tab_elems
.label
= gtk_label_new(title
);
7003 t
->tab_elems
.favicon
= gtk_image_new();
7004 gtk_label_set_width_chars(GTK_LABEL(t
->tab_elems
.label
), 1.0);
7005 gtk_misc_set_alignment(GTK_MISC(t
->tab_elems
.label
), 0.0, 0.0);
7006 gtk_misc_set_padding(GTK_MISC(t
->tab_elems
.label
), 4.0, 4.0);
7007 gtk_widget_modify_font(GTK_WIDGET(t
->tab_elems
.label
), tabbar_font
);
7009 t
->tab_elems
.eventbox
= gtk_event_box_new();
7010 t
->tab_elems
.box
= gtk_hbox_new(FALSE
, 0);
7011 t
->tab_elems
.sep
= gtk_vseparator_new();
7013 gdk_color_parse(XT_COLOR_CT_BACKGROUND
, &color
);
7014 gtk_widget_modify_bg(t
->tab_elems
.eventbox
, GTK_STATE_NORMAL
, &color
);
7015 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
7016 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
, &color
);
7017 gdk_color_parse(XT_COLOR_CT_SEPARATOR
, &color
);
7018 gtk_widget_modify_bg(t
->tab_elems
.sep
, GTK_STATE_NORMAL
, &color
);
7020 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.favicon
, FALSE
,
7022 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.label
, TRUE
,
7024 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.sep
, FALSE
,
7026 gtk_container_add(GTK_CONTAINER(t
->tab_elems
.eventbox
),
7029 gtk_box_pack_start(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
, TRUE
,
7031 gtk_widget_show_all(t
->tab_elems
.eventbox
);
7033 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
7036 id
= position
>= 0 ? position
:
7037 gtk_notebook_get_current_page(notebook
) + 1;
7038 if (id
> gtk_notebook_get_n_pages(notebook
))
7041 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
7042 gtk_notebook_insert_page(notebook
, t
->vbox
, b
, id
);
7043 gtk_box_reorder_child(GTK_BOX(tab_bar
),
7044 t
->tab_elems
.eventbox
, id
);
7049 #if GTK_CHECK_VERSION(2, 20, 0)
7050 /* turn spinner off if we are a new tab without uri */
7052 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
7053 gtk_widget_hide(t
->spinner
);
7056 /* make notebook tabs reorderable */
7057 gtk_notebook_set_tab_reorderable(notebook
, t
->vbox
, TRUE
);
7059 /* compact tabs clickable */
7060 g_signal_connect(G_OBJECT(t
->tab_elems
.eventbox
),
7061 "button_press_event", G_CALLBACK(tab_clicked_cb
), t
);
7063 g_object_connect(G_OBJECT(t
->cmd
),
7064 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb
), t
,
7065 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
7066 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb
), t
,
7067 "signal::activate", G_CALLBACK(cmd_activate_cb
), t
,
7068 "signal::populate-popup", G_CALLBACK(cmd_popup_cb
), t
,
7071 /* reuse wv_button_cb to hide oops */
7072 g_object_connect(G_OBJECT(t
->oops
),
7073 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7076 g_signal_connect(t
->buffers
,
7077 "row-activated", G_CALLBACK(row_activated_cb
), t
);
7078 g_object_connect(G_OBJECT(t
->buffers
),
7079 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
, (char *)NULL
);
7081 g_object_connect(G_OBJECT(t
->wv
),
7082 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
,
7083 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb
), t
,
7084 "signal::download-requested", G_CALLBACK(webview_download_cb
), t
,
7085 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), t
,
7086 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7087 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7088 "signal::resource-request-starting", G_CALLBACK(webview_rrs_cb
), t
,
7089 "signal::create-web-view", G_CALLBACK(webview_cwv_cb
), t
,
7090 "signal::close-web-view", G_CALLBACK(webview_closewv_cb
), t
,
7091 "signal::event", G_CALLBACK(webview_event_cb
), t
,
7092 "signal::load-finished", G_CALLBACK(webview_load_finished_cb
), t
,
7093 "signal::load-progress-changed", G_CALLBACK(webview_progress_changed_cb
), t
,
7094 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb
), t
,
7095 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7096 "signal::button_release_event", G_CALLBACK(wv_release_button_cb
), t
,
7097 "signal::populate-popup", G_CALLBACK(wv_popup_cb
), t
,
7099 g_signal_connect(t
->wv
,
7100 "notify::load-status", G_CALLBACK(notify_load_status_cb
), t
);
7101 g_signal_connect(t
->wv
,
7102 "notify::title", G_CALLBACK(notify_title_cb
), t
);
7104 /* hijack the unused keys as if we were the browser */
7105 //g_object_connect(G_OBJECT(t->toolbar),
7106 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
7109 g_signal_connect(G_OBJECT(bb
), "button_press_event",
7110 G_CALLBACK(tab_close_cb
), t
);
7113 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
7114 /* restore the tab's history */
7115 if (u
&& u
->history
) {
7119 webkit_web_back_forward_list_add_item(t
->bfl
, item
);
7120 items
= g_list_next(items
);
7123 item
= g_list_nth_data(u
->history
, u
->back
);
7125 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
7128 g_list_free(u
->history
);
7130 webkit_web_back_forward_list_clear(t
->bfl
);
7136 url_set_visibility();
7137 statusbar_set_visibility();
7140 set_current_tab(t
->tab_id
);
7141 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
7145 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
7149 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
7156 if (userstyle_global
)
7159 recolor_compact_tabs();
7160 setzoom_webkit(t
, XT_ZOOM_NORMAL
);
7165 notebook_switchpage_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7171 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
7173 if (gtk_notebook_get_current_page(notebook
) == -1)
7176 TAILQ_FOREACH(t
, &tabs
, entry
) {
7177 if (t
->tab_id
== pn
) {
7178 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
7181 uri
= get_title(t
, TRUE
);
7182 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
7188 /* can't use focus_webview here */
7189 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
7196 notebook_pagereordered_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7199 struct tab
*t
= NULL
, *tt
;
7203 TAILQ_FOREACH(tt
, &tabs
, entry
)
7204 if (tt
->tab_id
== pn
) {
7210 DNPRINTF(XT_D_TAB
, "page_reordered_cb: tab: %d\n", t
->tab_id
);
7212 gtk_box_reorder_child(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
,
7217 menuitem_response(struct tab
*t
)
7219 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
7223 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
7225 GtkWidget
*menu
, *menu_items
;
7226 GdkEventButton
*bevent
;
7230 if (event
->type
== GDK_BUTTON_PRESS
) {
7231 bevent
= (GdkEventButton
*) event
;
7232 menu
= gtk_menu_new();
7234 TAILQ_FOREACH(ti
, &tabs
, entry
) {
7235 if ((uri
= get_uri(ti
)) == NULL
)
7236 /* XXX make sure there is something to print */
7237 /* XXX add gui pages in here to look purdy */
7239 menu_items
= gtk_menu_item_new_with_label(uri
);
7240 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), menu_items
);
7241 gtk_widget_show(menu_items
);
7243 g_signal_connect_swapped((menu_items
),
7244 "activate", G_CALLBACK(menuitem_response
),
7248 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
,
7249 bevent
->button
, bevent
->time
);
7251 /* unref object so it'll free itself when popped down */
7252 #if !GTK_CHECK_VERSION(3, 0, 0)
7253 /* XXX does not need unref with gtk+3? */
7254 g_object_ref_sink(menu
);
7255 g_object_unref(menu
);
7258 return (TRUE
/* eat event */);
7261 return (FALSE
/* propagate */);
7265 icon_size_map(int iconsz
)
7267 if (iconsz
<= GTK_ICON_SIZE_INVALID
||
7268 iconsz
> GTK_ICON_SIZE_DIALOG
)
7269 return (GTK_ICON_SIZE_SMALL_TOOLBAR
);
7275 create_button(char *name
, char *stockid
, int size
)
7277 GtkWidget
*button
, *image
;
7281 rcstring
= g_strdup_printf(
7282 "style \"%s-style\"\n"
7284 " GtkWidget::focus-padding = 0\n"
7285 " GtkWidget::focus-line-width = 0\n"
7289 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
7290 gtk_rc_parse_string(rcstring
);
7292 button
= gtk_button_new();
7293 gtk_button_set_focus_on_click(GTK_BUTTON(button
), FALSE
);
7294 gtk_icon_size
= icon_size_map(size
? size
: icon_size
);
7296 image
= gtk_image_new_from_stock(stockid
, gtk_icon_size
);
7297 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
7298 gtk_container_set_border_width(GTK_CONTAINER(button
), 1);
7299 gtk_container_add(GTK_CONTAINER(button
), GTK_WIDGET(image
));
7300 gtk_widget_set_name(button
, name
);
7301 gtk_button_set_relief(GTK_BUTTON(button
), GTK_RELIEF_NONE
);
7307 button_set_stockid(GtkWidget
*button
, char *stockid
)
7311 image
= gtk_image_new_from_stock(stockid
, icon_size_map(icon_size
));
7312 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
7313 gtk_button_set_image(GTK_BUTTON(button
), image
);
7317 clipb_primary_cb(GtkClipboard
*primary
, GdkEvent
*event
, gpointer notused
)
7320 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
7323 if (xterm_workaround
== 0)
7327 * xterm doesn't play nice with clipboards because it clears the
7328 * primary when clicked. We rely on primary being set to properly
7329 * handle middle mouse button clicks (paste). So when someone clears
7330 * primary copy whatever is in CUT_BUFFER0 into primary to simualte
7331 * other application behavior (as in DON'T clear primary).
7334 p
= gtk_clipboard_wait_for_text(primary
);
7336 if (gdk_property_get(gdk_get_default_root_window(),
7338 gdk_atom_intern("STRING", FALSE
),
7340 1024 * 1024 /* picked out of my butt */,
7348 /* yes sir, we need to NUL the string */
7350 gtk_clipboard_set_text(primary
, p
, -1);
7364 char file
[PATH_MAX
];
7367 vbox
= gtk_vbox_new(FALSE
, 0);
7368 gtk_box_set_spacing(GTK_BOX(vbox
), 0);
7369 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
7370 #if !GTK_CHECK_VERSION(3, 0, 0)
7371 /* XXX seems to be needed with gtk+2 */
7372 gtk_notebook_set_tab_hborder(notebook
, 0);
7373 gtk_notebook_set_tab_vborder(notebook
, 0);
7375 gtk_notebook_set_scrollable(notebook
, TRUE
);
7376 gtk_notebook_set_show_border(notebook
, FALSE
);
7377 gtk_widget_set_can_focus(GTK_WIDGET(notebook
), FALSE
);
7379 abtn
= gtk_button_new();
7380 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
7381 gtk_widget_set_size_request(arrow
, -1, -1);
7382 gtk_container_add(GTK_CONTAINER(abtn
), arrow
);
7383 gtk_widget_set_size_request(abtn
, -1, 20);
7385 #if GTK_CHECK_VERSION(2, 20, 0)
7386 gtk_notebook_set_action_widget(notebook
, abtn
, GTK_PACK_END
);
7388 gtk_widget_set_size_request(GTK_WIDGET(notebook
), -1, -1);
7390 /* compact tab bar */
7391 tab_bar
= gtk_hbox_new(TRUE
, 0);
7393 gtk_box_pack_start(GTK_BOX(vbox
), tab_bar
, FALSE
, FALSE
, 0);
7394 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
7395 gtk_widget_set_size_request(vbox
, -1, -1);
7397 g_object_connect(G_OBJECT(notebook
),
7398 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb
), NULL
,
7400 g_object_connect(G_OBJECT(notebook
),
7401 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb
),
7402 NULL
, (char *)NULL
);
7403 g_signal_connect(G_OBJECT(abtn
), "button_press_event",
7404 G_CALLBACK(arrow_cb
), NULL
);
7406 main_window
= create_window("xxxterm");
7407 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
7408 g_signal_connect(G_OBJECT(main_window
), "delete_event",
7409 G_CALLBACK(gtk_main_quit
), NULL
);
7412 for (i
= 0; i
< LENGTH(icons
); i
++) {
7413 snprintf(file
, sizeof file
, "%s" PS
"%s", resource_dir
, icons
[i
]);
7414 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
7415 l
= g_list_append(l
, pb
);
7417 gtk_window_set_default_icon_list(l
);
7419 /* clipboard work around */
7420 if (xterm_workaround
)
7422 G_OBJECT(gtk_clipboard_get(GDK_SELECTION_PRIMARY
)),
7423 "owner-change", G_CALLBACK(clipb_primary_cb
), NULL
);
7425 gtk_widget_show_all(abtn
);
7426 gtk_widget_show_all(main_window
);
7427 notebook_tab_set_visibility();
7430 #ifndef XT_SOCKET_DISABLE
7432 send_cmd_to_socket(char *cmd
)
7435 struct sockaddr_un sa
;
7437 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7438 warnx("%s: socket", __func__
);
7442 sa
.sun_family
= AF_UNIX
;
7443 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
7444 work_dir
, XT_SOCKET_FILE
);
7447 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7448 warnx("%s: connect", __func__
);
7452 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
7453 warnx("%s: send", __func__
);
7464 socket_watcher(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
7467 char str
[XT_MAX_URL_LENGTH
];
7468 socklen_t t
= sizeof(struct sockaddr_un
);
7469 struct sockaddr_un sa
;
7474 gint fd
= g_io_channel_unix_get_fd(source
);
7476 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
7481 if (getpeereid(s
, &uid
, &gid
) == -1) {
7485 if (uid
!= getuid() || gid
!= getgid()) {
7486 warnx("unauthorized user");
7492 warnx("not a valid user");
7496 n
= recv(s
, str
, sizeof(str
), 0);
7500 tt
= TAILQ_LAST(&tabs
, tab_list
);
7501 cmd_execute(tt
, str
);
7509 struct sockaddr_un sa
;
7511 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7512 warn("is_running: socket");
7516 sa
.sun_family
= AF_UNIX
;
7517 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
7518 work_dir
, XT_SOCKET_FILE
);
7521 /* connect to see if there is a listener */
7522 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
7523 rv
= 0; /* not running */
7525 rv
= 1; /* already running */
7536 struct sockaddr_un sa
;
7538 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7539 warn("build_socket: socket");
7543 sa
.sun_family
= AF_UNIX
;
7544 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
7545 work_dir
, XT_SOCKET_FILE
);
7548 /* connect to see if there is a listener */
7549 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7550 /* no listener so we will */
7551 unlink(sa
.sun_path
);
7553 if (bind(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7554 warn("build_socket: bind");
7558 if (listen(s
, 1) == -1) {
7559 warn("build_socket: listen");
7577 if (stat(dir
, &sb
)) {
7578 #if defined __MINGW32__
7579 printf("making: %s\n", dir
);
7580 if (mkdir(dir
) == -1)
7582 if (mkdir(dir
, S_IRWXU
) == -1)
7584 err(1, "mkdir %s", dir
);
7586 err(1, "stat %s", dir
);
7588 if (S_ISDIR(sb
.st_mode
) == 0)
7589 errx(1, "%s not a dir", dir
);
7590 #if !defined __MINGW32__
7591 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
7592 warnx("fixing invalid permissions on %s", dir
);
7593 if (chmod(dir
, S_IRWXU
) == -1)
7594 err(1, "chmod %s", dir
);
7603 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
7607 GStaticRecMutex my_gdk_mtx
= G_STATIC_REC_MUTEX_INIT
;
7608 volatile int mtx_depth
;
7612 * The linux flash plugin violates the gdk locking mechanism.
7613 * Work around the issue by using a recursive mutex with some match applied
7614 * to see if we hit a buggy condition.
7616 * The following code is painful so just don't read it. It really doesn't
7617 * make much sense but seems to work.
7623 g_static_rec_mutex_lock(&my_gdk_mtx
);
7624 if (my_gdk_mtx
.depth
<= 0) {
7626 g_static_rec_mutex_lock(&my_gdk_mtx
);
7628 } else if (my_gdk_mtx
.depth
!= 1) {
7631 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7632 } while (my_gdk_mtx
.depth
> 1);
7638 if (mtx_complain
== 0) {
7639 show_oops(NULL
, "buggy mutex implementation detected(%s), "
7640 "work around implemented", s
);
7650 if (my_gdk_mtx
.depth
<= 0) {
7653 } else if (my_gdk_mtx
.depth
!= 1) {
7655 g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
7658 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7662 if (mtx_complain
== 0) {
7663 show_oops(NULL
, "buggy mutex implementation detected(%s), "
7664 "work around implemented", s
);
7672 startpage_add("<b>Welcome to xxxterm %s!</b><p>", version
);
7673 startpage_add("Beware that this is the final version that will use the"
7674 " xxxterm name.<br>Moving forward the browser will be called "
7675 "xombrero.<p>", version
);
7676 startpage_add("Details will soon appear on the "
7677 "<a href=https://opensource.conformal.com/wiki/xombrero>xombrero "
7678 "wiki page</a><p>");
7679 startpage_add("Unfortunately scroogle has shut it's doors and due to "
7680 "that one has to edit search_string in ~/.xxxterm.conf.<br>"
7681 "There are various examples in the configuration file.<br>"
7682 "The authors of xxxterm are not in a position to suggest a search "
7687 main(int argc
, char **argv
)
7690 int c
, optn
= 0, opte
= 0, focus
= 1;
7691 char conf
[PATH_MAX
] = { '\0' };
7692 char file
[PATH_MAX
];
7693 char sodversion
[32];
7694 char *env_proxy
= NULL
;
7698 start_argv
= (char * const *)argv
;
7702 /* http://web.archiveorange.com/archive/v/UsPjxkX5PsaXBIoOjqxf */
7705 /* http://developer.gnome.org/gdk/stable/gdk-Threads.html */
7706 g_thread_init(NULL
);
7707 gdk_threads_set_lock_functions(mtx_lock
, mtx_unlock
);
7709 gdk_threads_enter();
7711 /* http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html */
7712 gcry_control (GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
7714 gtk_init(&argc
, &argv
);
7716 gnutls_global_init();
7718 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
7723 RB_INIT(&downloads
);
7726 TAILQ_INIT(&sessions
);
7729 TAILQ_INIT(&aliases
);
7735 TAILQ_INIT(&ua_list
);
7737 #ifndef XT_RESOURCE_LIMITS_DISABLE
7741 GIOChannel
*channel
;
7743 /* fiddle with ulimits */
7744 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7747 /* just use them all */
7748 rlp
.rlim_cur
= rlp
.rlim_max
;
7749 if (setrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7751 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7753 else if (rlp
.rlim_cur
< 1024)
7754 startpage_add("%s requires at least 1024 "
7755 "(2048 recommended) file " "descriptors, "
7756 "currently it has up to %d available",
7757 __progname
, rlp
.rlim_cur
);
7761 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
7770 #ifdef XXXTERM_BUILDSTR
7771 errx(0 , "Version: %s Build: %s",
7772 version
, XXXTERM_BUILDSTR
);
7774 errx(0 , "Version: %s", version
);
7778 strlcpy(conf
, optarg
, sizeof(conf
));
7781 strlcpy(named_session
, optarg
, sizeof(named_session
));
7802 xtp_generate_keys();
7804 /* XXX this hammer is way too big but treat all signaling the same */
7805 #ifndef XT_SIGNALS_DISABLE
7807 struct sigaction sact
;
7809 bzero(&sact
, sizeof(sact
));
7810 sigemptyset(&sact
.sa_mask
);
7811 sact
.sa_handler
= sigchild
;
7812 sact
.sa_flags
= SA_NOCLDSTOP
;
7813 sigaction(SIGCHLD
, &sact
, NULL
);
7816 /* set download dir */
7817 pwd
= getpwuid(getuid());
7819 errx(1, "invalid user %d", getuid());
7820 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
7822 /* compile buffer command regexes */
7825 /* set default string settings */
7826 home
= g_strdup("https://www.cyphertite.com");
7827 search_string
= g_strdup("https://ssl.scroogle.org/cgi-bin/nbbwssl.cgi?Gw=%s");
7828 resource_dir
= g_strdup("/usr/local/share/xxxterm/");
7829 strlcpy(runtime_settings
, "runtime", sizeof runtime_settings
);
7830 cmd_font_name
= g_strdup("monospace normal 9");
7831 oops_font_name
= g_strdup("monospace normal 9");
7832 statusbar_font_name
= g_strdup("monospace normal 9");
7833 tabbar_font_name
= g_strdup("monospace normal 9");
7834 statusbar_elems
= g_strdup("BP");
7835 spell_check_languages
= g_strdup("en_US");
7836 encoding
= g_strdup("UTF-8");
7838 /* read config file */
7839 if (strlen(conf
) == 0)
7840 snprintf(conf
, sizeof conf
, "%s" PS
".%s",
7841 pwd
->pw_dir
, XT_CONF_FILE
);
7842 config_parse(conf
, 0);
7845 cmd_font
= pango_font_description_from_string(cmd_font_name
);
7846 oops_font
= pango_font_description_from_string(oops_font_name
);
7847 statusbar_font
= pango_font_description_from_string(statusbar_font_name
);
7848 tabbar_font
= pango_font_description_from_string(tabbar_font_name
);
7850 /* working directory */
7851 if (strlen(work_dir
) == 0)
7852 snprintf(work_dir
, sizeof work_dir
, "%s" PS
"%s",
7853 pwd
->pw_dir
, XT_DIR
);
7856 /* icon cache dir */
7857 snprintf(cache_dir
, sizeof cache_dir
, "%s" PS
"%s", work_dir
, XT_CACHE_DIR
);
7861 snprintf(certs_dir
, sizeof certs_dir
, "%s" PS
"%s", work_dir
, XT_CERT_DIR
);
7865 snprintf(sessions_dir
, sizeof sessions_dir
, "%s" PS
"%s",
7866 work_dir
, XT_SESSIONS_DIR
);
7867 xxx_dir(sessions_dir
);
7870 snprintf(js_dir
, sizeof js_dir
, "%s" PS
"%s", work_dir
, XT_JS_DIR
);
7874 snprintf(temp_dir
, sizeof temp_dir
, "%s" PS
"%s", work_dir
, XT_TEMP_DIR
);
7877 /* runtime settings that can override config file */
7878 if (runtime_settings
[0] != '\0')
7879 config_parse(runtime_settings
, 1);
7882 if (!strcmp(download_dir
, pwd
->pw_dir
))
7883 strlcat(download_dir
, PS
"downloads", sizeof download_dir
);
7884 xxx_dir(download_dir
);
7886 /* first start file */
7887 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_SOD_FILE
);
7888 if (stat(file
, &sb
)) {
7889 warnx("start of day file doesn't exist, creating it");
7890 if ((f
= fopen(file
, "w")) == NULL
)
7891 err(1, "startofday");
7892 if (fputs(version
, f
) == EOF
)
7899 if ((f
= fopen(file
, "r+")) == NULL
)
7900 err(1, "startofday");
7901 if (fgets(sodversion
, sizeof sodversion
, f
) == NULL
)
7903 sodversion
[strcspn(sodversion
, "\n")] = '\0';
7904 if (strcmp(version
, sodversion
)) {
7906 if (fputs(version
, f
) == EOF
)
7909 /* upgrade, say something smart */
7915 /* favorites file */
7916 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_FAVS_FILE
);
7917 if (stat(file
, &sb
)) {
7918 warnx("favorites file doesn't exist, creating it");
7919 if ((f
= fopen(file
, "w")) == NULL
)
7920 err(1, "favorites");
7924 /* quickmarks file */
7925 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
7926 if (stat(file
, &sb
)) {
7927 warnx("quickmarks file doesn't exist, creating it");
7928 if ((f
= fopen(file
, "w")) == NULL
)
7929 err(1, "quickmarks");
7933 /* search history */
7934 if (history_autosave
) {
7935 snprintf(search_file
, sizeof search_file
, "%s" PS
"%s",
7936 work_dir
, XT_SEARCH_FILE
);
7937 if (stat(search_file
, &sb
)) {
7938 warnx("search history file doesn't exist, creating it");
7939 if ((f
= fopen(search_file
, "w")) == NULL
)
7940 err(1, "search_history");
7943 history_read(&shl
, search_file
, &search_history_count
);
7946 /* command history */
7947 if (history_autosave
) {
7948 snprintf(command_file
, sizeof command_file
, "%s" PS
"%s",
7949 work_dir
, XT_COMMAND_FILE
);
7950 if (stat(command_file
, &sb
)) {
7951 warnx("command history file doesn't exist, creating it");
7952 if ((f
= fopen(command_file
, "w")) == NULL
)
7953 err(1, "command_history");
7956 history_read(&chl
, command_file
, &cmd_history_count
);
7960 session
= webkit_get_default_session();
7964 set_ssl_ca_file(ssl_ca_file
);
7966 /* guess_search regex */
7967 if (url_regex
== NULL
)
7968 url_regex
= g_strdup(XT_URL_REGEX
);
7970 if (regcomp(&url_re
, url_regex
, REG_EXTENDED
| REG_NOSUB
))
7971 startpage_add("invalid url regex %s", url_regex
);
7974 env_proxy
= getenv("http_proxy");
7976 setup_proxy(env_proxy
);
7978 env_proxy
= getenv("HTTP_PROXY");
7980 setup_proxy(env_proxy
);
7982 setup_proxy(http_proxy
);
7984 #ifndef XT_SOCKET_DISABLE
7986 send_cmd_to_socket(argv
[0]);
7990 opte
= opte
; /* shut mingw up */
7992 /* set some connection parameters */
7993 g_object_set(session
, "max-conns", max_connections
, (char *)NULL
);
7994 g_object_set(session
, "max-conns-per-host", max_host_connections
,
7997 g_signal_connect(session
, "request-queued", G_CALLBACK(session_rq_cb
),
8000 #ifndef XT_SOCKET_DISABLE
8001 /* see if there is already an xxxterm running */
8002 if (single_instance
&& is_running()) {
8004 warnx("already running");
8009 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
8010 send_cmd_to_socket(cmd
);
8020 optn
= optn
; /* shut mingw up */
8025 if (enable_strict_transport
)
8026 strict_transport_init();
8028 /* uri completion */
8029 completion_model
= gtk_list_store_new(1, G_TYPE_STRING
);
8032 buffers_store
= gtk_list_store_new
8033 (NUM_COLS
, G_TYPE_UINT
, GDK_TYPE_PIXBUF
, G_TYPE_STRING
);
8039 notebook_tab_set_visibility();
8041 if (save_global_history
)
8042 restore_global_history();
8044 /* restore session list */
8045 restore_sessions_list();
8047 if (!strcmp(named_session
, XT_SAVED_TABS_FILE
))
8048 restore_saved_tabs();
8050 a
.s
= named_session
;
8051 a
.i
= XT_SES_DONOTHING
;
8052 open_tabs(NULL
, &a
);
8055 /* see if we have an exception */
8056 if (!TAILQ_EMPTY(&spl
)) {
8057 create_new_tab("about:startpage", NULL
, focus
, -1);
8062 create_new_tab(argv
[0], NULL
, focus
, -1);
8069 if (TAILQ_EMPTY(&tabs
))
8070 create_new_tab(home
, NULL
, 1, -1);
8071 #ifndef XT_SOCKET_DISABLE
8073 if ((s
= build_socket()) != -1) {
8074 channel
= g_io_channel_unix_new(s
);
8075 g_io_add_watch(channel
, G_IO_IN
, socket_watcher
, NULL
);
8083 gdk_threads_leave();
8084 g_static_rec_mutex_unlock_full(&my_gdk_mtx
); /* just in case */
8087 gnutls_global_deinit();