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 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 u_int32_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_TAB_LAST (-4)
150 #define XT_TAB_FIRST (-3)
151 #define XT_TAB_PREV (-2)
152 #define XT_TAB_NEXT (-1)
153 #define XT_TAB_INVALID (0)
154 #define XT_TAB_NEW (1)
155 #define XT_TAB_DELETE (2)
156 #define XT_TAB_DELQUIT (3)
157 #define XT_TAB_OPEN (4)
158 #define XT_TAB_UNDO_CLOSE (5)
159 #define XT_TAB_SHOW (6)
160 #define XT_TAB_HIDE (7)
161 #define XT_TAB_NEXTSTYLE (8)
162 #define XT_TAB_LOAD_IMAGES (9)
164 #define XT_NAV_INVALID (0)
165 #define XT_NAV_BACK (1)
166 #define XT_NAV_FORWARD (2)
167 #define XT_NAV_RELOAD (3)
168 #define XT_NAV_STOP (4)
170 #define XT_FOCUS_INVALID (0)
171 #define XT_FOCUS_URI (1)
172 #define XT_FOCUS_SEARCH (2)
174 #define XT_SEARCH_INVALID (0)
175 #define XT_SEARCH_NEXT (1)
176 #define XT_SEARCH_PREV (2)
178 #define XT_STYLE_CURRENT_TAB (0)
179 #define XT_STYLE_GLOBAL (1)
181 #define XT_PASTE_CURRENT_TAB (0)
182 #define XT_PASTE_NEW_TAB (1)
184 #define XT_ZOOM_IN (-1)
185 #define XT_ZOOM_OUT (-2)
186 #define XT_ZOOM_NORMAL (100)
188 #define XT_URL_SHOW (1)
189 #define XT_URL_HIDE (2)
191 #define XT_CMD_OPEN (0)
192 #define XT_CMD_OPEN_CURRENT (1)
193 #define XT_CMD_TABNEW (2)
194 #define XT_CMD_TABNEW_CURRENT (3)
196 #define XT_STATUS_NOTHING (0)
197 #define XT_STATUS_LINK (1)
198 #define XT_STATUS_URI (2)
199 #define XT_STATUS_LOADING (3)
201 #define XT_SES_DONOTHING (0)
202 #define XT_SES_CLOSETABS (1)
204 #define XT_PREFIX (1<<0)
205 #define XT_USERARG (1<<1)
206 #define XT_URLARG (1<<2)
207 #define XT_INTARG (1<<3)
208 #define XT_SESSARG (1<<4)
209 #define XT_SETARG (1<<5)
211 #define XT_HINT_NEWTAB (1<<0)
213 #define XT_BUFCMD_SZ (8)
215 #define XT_EJS_SHOW (1<<0)
217 GtkWidget
* create_button(char *, char *, int);
219 void recalc_tabs(void);
220 void recolor_compact_tabs(void);
221 void set_current_tab(int page_num
);
222 gboolean
update_statusbar_position(GtkAdjustment
*, gpointer
);
223 void marks_clear(struct tab
*t
);
226 extern char *__progname
;
227 const char **start_argv
;
229 GtkWidget
*main_window
;
230 GtkNotebook
*notebook
;
232 GtkWidget
*arrow
, *abtn
;
233 struct tab_list tabs
;
234 struct history_list hl
;
235 int hl_purge_count
= 0;
236 struct session_list sessions
;
237 struct domain_list c_wl
;
238 struct domain_list js_wl
;
239 struct domain_list pl_wl
;
240 struct undo_tailq undos
;
241 struct keybinding_list kbl
;
243 struct user_agent_list ua_list
;
244 int user_agent_count
= 0;
245 struct command_list chl
;
246 struct command_list shl
;
247 struct command_entry
*history_at
;
248 struct command_entry
*search_at
;
250 int cmd_history_count
= 0;
251 int search_history_count
= 0;
253 long long unsigned int blocked_cookies
= 0;
254 char named_session
[PATH_MAX
];
255 GtkListStore
*completion_model
;
256 GtkListStore
*buffers_store
;
258 char *qmarks
[XT_NOMARKS
];
259 int btn_down
; /* M1 down in any wv */
260 regex_t url_re
; /* guess_search regex */
262 /* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
263 int next_download_id
= 1;
265 void xxx_dir(char *);
266 int icon_size_map(int);
269 history_delete(struct command_list
*l
, int *counter
)
271 struct command_entry
*c
;
273 if (l
== NULL
|| counter
== NULL
)
276 c
= TAILQ_LAST(l
, command_list
);
280 TAILQ_REMOVE(l
, c
, entry
);
287 history_add(struct command_list
*list
, char *file
, char *l
, int *counter
)
289 struct command_entry
*c
;
292 if (list
== NULL
|| l
== NULL
|| counter
== NULL
)
295 /* don't add the same line */
296 c
= TAILQ_FIRST(list
);
298 if (!strcmp(c
->line
+ 1 /* skip space */, l
))
301 c
= g_malloc0(sizeof *c
);
302 c
->line
= g_strdup_printf(" %s", l
);
305 TAILQ_INSERT_HEAD(list
, c
, entry
);
308 history_delete(list
, counter
);
310 if (history_autosave
&& file
) {
311 f
= fopen(file
, "w");
313 show_oops(NULL
, "couldn't write history %s", file
);
317 TAILQ_FOREACH_REVERSE(c
, list
, command_list
, entry
) {
319 fprintf(f
, "%s\n", c
->line
);
327 history_read(struct command_list
*list
, char *file
, int *counter
)
330 char *s
, line
[65536];
332 if (list
== NULL
|| file
== NULL
)
335 f
= fopen(file
, "r");
337 startpage_add("couldn't open history file %s", file
);
342 s
= fgets(line
, sizeof line
, f
);
343 if (s
== NULL
|| feof(f
) || ferror(f
))
345 if ((s
= strchr(line
, '\n')) == NULL
) {
346 startpage_add("invalid history file %s", file
);
352 history_add(list
, NULL
, line
+ 1, counter
);
360 /* marks and quickmarks array storage.
361 * first a-z, then A-Z, then 0-9 */
368 if (i
>= 0 && i
<= 'z' - 'a')
372 if (i
>= 0 && i
<= 'Z' - 'A')
387 if (m
>= 'a' && m
<= 'z')
388 return ret
+ m
- 'a';
390 ret
+= 'z' - 'a' + 1;
391 if (m
>= 'A' && m
<= 'Z')
392 return ret
+ m
- 'A';
394 ret
+= 'Z' - 'A' + 1;
395 if (m
>= '0' && m
<= '9')
396 return ret
+ m
- '0';
401 #ifndef XT_SIGNALS_DISABLE
405 int saved_errno
, status
;
410 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
414 if (errno
!= ECHILD
) {
416 clog_warn("sigchild: waitpid:");
422 if (WIFEXITED(status
)) {
423 if (WEXITSTATUS(status
) != 0) {
425 clog_warnx("sigchild: child exit status: %d",
426 WEXITSTATUS(status));
431 clog_warnx("sigchild: child is terminated abnormally");
441 is_g_object_setting(GObject
*o
, char *str
)
443 guint n_props
= 0, i
;
444 GParamSpec
**proplist
;
446 if (! G_IS_OBJECT(o
))
449 proplist
= g_object_class_list_properties(G_OBJECT_GET_CLASS(o
),
452 for (i
=0; i
< n_props
; i
++) {
453 if (! strcmp(proplist
[i
]->name
, str
))
460 get_current_tab(void)
464 TAILQ_FOREACH(t
, &tabs
, entry
) {
465 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
469 warnx("%s: no current tab", __func__
);
475 set_status(struct tab
*t
, gchar
*s
, int status
)
483 case XT_STATUS_LOADING
:
484 type
= g_strdup_printf("Loading: %s", s
);
488 type
= g_strdup_printf("Link: %s", s
);
490 t
->status
= g_strdup(gtk_entry_get_text(
491 GTK_ENTRY(t
->sbe
.statusbar
)));
495 type
= g_strdup_printf("%s", s
);
497 t
->status
= g_strdup(type
);
501 t
->status
= g_strdup(s
);
503 case XT_STATUS_NOTHING
:
508 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.statusbar
), s
);
514 hide_cmd(struct tab
*t
)
516 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
518 history_at
= NULL
; /* just in case */
519 search_at
= NULL
; /* just in case */
520 gtk_widget_hide(t
->cmd
);
524 show_cmd(struct tab
*t
)
526 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
530 gtk_widget_hide(t
->oops
);
531 gtk_widget_show(t
->cmd
);
535 hide_buffers(struct tab
*t
)
537 gtk_widget_hide(t
->buffers
);
538 gtk_list_store_clear(buffers_store
);
549 sort_tabs_by_page_num(struct tab
***stabs
)
554 num_tabs
= gtk_notebook_get_n_pages(notebook
);
556 *stabs
= g_malloc0(num_tabs
* sizeof(struct tab
*));
558 TAILQ_FOREACH(t
, &tabs
, entry
)
559 (*stabs
)[gtk_notebook_page_num(notebook
, t
->vbox
)] = t
;
565 buffers_make_list(void)
568 const gchar
*title
= NULL
;
570 struct tab
**stabs
= NULL
;
572 num_tabs
= sort_tabs_by_page_num(&stabs
);
574 for (i
= 0; i
< num_tabs
; i
++)
576 gtk_list_store_append(buffers_store
, &iter
);
577 title
= get_title(stabs
[i
], FALSE
);
578 gtk_list_store_set(buffers_store
, &iter
,
579 COL_ID
, i
+ 1, /* Enumerate the tabs starting from 1
581 COL_FAVICON
, gtk_image_get_pixbuf
582 (GTK_IMAGE(stabs
[i
]->tab_elems
.favicon
)),
591 show_buffers(struct tab
*t
)
593 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)))
597 gtk_widget_show(t
->buffers
);
598 gtk_widget_grab_focus(GTK_WIDGET(t
->buffers
));
602 toggle_buffers(struct tab
*t
)
604 if (gtk_widget_get_visible(t
->buffers
))
611 buffers(struct tab
*t
, struct karg
*args
)
619 hide_oops(struct tab
*t
)
621 gtk_widget_hide(t
->oops
);
625 show_oops(struct tab
*at
, const char *fmt
, ...)
629 struct tab
*t
= NULL
;
635 if ((t
= get_current_tab()) == NULL
)
641 if (vasprintf(&msg
, fmt
, ap
) == -1)
642 errx(1, "show_oops failed");
645 gtk_entry_set_text(GTK_ENTRY(t
->oops
), msg
);
646 gtk_widget_hide(t
->cmd
);
647 gtk_widget_show(t
->oops
);
653 char work_dir
[PATH_MAX
];
654 char certs_dir
[PATH_MAX
];
655 char js_dir
[PATH_MAX
];
656 char cache_dir
[PATH_MAX
];
657 char sessions_dir
[PATH_MAX
];
658 char temp_dir
[PATH_MAX
];
659 char cookie_file
[PATH_MAX
];
660 SoupSession
*session
;
661 SoupCookieJar
*s_cookiejar
;
662 SoupCookieJar
*p_cookiejar
;
663 char rc_fname
[PATH_MAX
];
665 struct mime_type_list mtl
;
666 struct alias_list aliases
;
669 struct tab
*create_new_tab(char *, struct undo
*, int, int);
670 void delete_tab(struct tab
*);
671 void setzoom_webkit(struct tab
*, int);
672 int download_rb_cmp(struct download
*, struct download
*);
673 gboolean
cmd_execute(struct tab
*t
, char *str
);
676 history_rb_cmp(struct history
*h1
, struct history
*h2
)
678 return (strcmp(h1
->uri
, h2
->uri
));
680 RB_GENERATE(history_list
, history
, entry
, history_rb_cmp
);
683 domain_rb_cmp(struct domain
*d1
, struct domain
*d2
)
685 return (strcmp(d1
->d
, d2
->d
));
687 RB_GENERATE(domain_list
, domain
, entry
, domain_rb_cmp
);
690 download_rb_cmp(struct download
*e1
, struct download
*e2
)
692 return (e1
->id
< e2
->id
? -1 : e1
->id
> e2
->id
);
694 RB_GENERATE(download_list
, download
, entry
, download_rb_cmp
);
696 struct valid_url_types
{
707 valid_url_type(char *url
)
711 for (i
= 0; i
< LENGTH(vut
); i
++)
712 if (!strncasecmp(vut
[i
].type
, url
, strlen(vut
[i
].type
)))
719 match_alias(char *url_in
)
723 char *url_out
= NULL
, *search
, *enc_arg
;
725 search
= g_strdup(url_in
);
727 if (strsep(&arg
, " \t") == NULL
) {
728 show_oops(NULL
, "match_alias: NULL URL");
732 TAILQ_FOREACH(a
, &aliases
, entry
) {
733 if (!strcmp(search
, a
->a_name
))
738 DNPRINTF(XT_D_URL
, "match_alias: matched alias %s\n",
741 enc_arg
= soup_uri_encode(arg
, XT_RESERVED_CHARS
);
742 url_out
= g_strdup_printf(a
->a_uri
, enc_arg
);
745 url_out
= g_strdup_printf(a
->a_uri
, "");
753 guess_url_type(char *url_in
)
756 char *url_out
= NULL
, *enc_search
= NULL
;
761 /* substitute aliases */
762 url_out
= match_alias(url_in
);
766 /* see if we are an about page */
767 if (!strncmp(url_in
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
))
768 for (i
= 0; i
< about_list_size(); i
++)
769 if (!strcmp(&url_in
[XT_URI_ABOUT_LEN
],
770 about_list
[i
].name
)) {
771 url_out
= g_strdup(url_in
);
775 if (guess_search
&& url_regex
&&
776 !(g_str_has_prefix(url_in
, "http://") ||
777 g_str_has_prefix(url_in
, "https://"))) {
778 if (regexec(&url_re
, url_in
, 0, NULL
, 0)) {
779 /* invalid URI so search instead */
780 enc_search
= soup_uri_encode(url_in
, XT_RESERVED_CHARS
);
781 url_out
= g_strdup_printf(search_string
, enc_search
);
787 /* XXX not sure about this heuristic */
788 if (stat(url_in
, &sb
) == 0) {
789 if (url_in
[0] == '/')
790 url_out
= g_strdup_printf("file://%s", url_in
);
792 cwd
= malloc(PATH_MAX
);
793 if (getcwd(cwd
, PATH_MAX
) != NULL
) {
794 url_out
= g_strdup_printf("file://%s/%s",cwd
, url_in
);
799 url_out
= g_strdup_printf("http://%s", url_in
); /* guess http */
801 DNPRINTF(XT_D_URL
, "guess_url_type: guessed %s\n", url_out
);
807 load_uri(struct tab
*t
, gchar
*uri
)
810 gchar
*newuri
= NULL
;
816 /* Strip leading spaces. */
817 while (*uri
&& isspace(*uri
))
820 if (strlen(uri
) == 0) {
825 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
827 if (valid_url_type(uri
)) {
828 newuri
= guess_url_type(uri
);
832 if (!strncmp(uri
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
)) {
833 for (i
= 0; i
< about_list_size(); i
++)
834 if (!strcmp(&uri
[XT_URI_ABOUT_LEN
], about_list
[i
].name
)) {
835 bzero(&args
, sizeof args
);
836 about_list
[i
].func(t
, &args
);
837 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
),
841 show_oops(t
, "invalid about page");
845 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
847 webkit_web_view_load_uri(t
->wv
, uri
);
854 get_uri(struct tab
*t
)
856 const gchar
*uri
= NULL
;
858 if (webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
)
860 if (t
->xtp_meaning
== XT_XTP_TAB_MEANING_NORMAL
) {
861 uri
= webkit_web_view_get_uri(t
->wv
);
863 /* use tmp_uri to make sure it is g_freed */
866 t
->tmp_uri
=g_strdup_printf("%s%s", XT_URI_ABOUT
,
867 about_list
[t
->xtp_meaning
].name
);
874 get_title(struct tab
*t
, bool window
)
876 const gchar
*set
= NULL
, *title
= NULL
;
877 WebKitLoadStatus status
= webkit_web_view_get_load_status(t
->wv
);
879 if (status
== WEBKIT_LOAD_PROVISIONAL
|| status
== WEBKIT_LOAD_FAILED
||
880 t
->xtp_meaning
== XT_XTP_TAB_MEANING_BL
)
883 title
= webkit_web_view_get_title(t
->wv
);
884 if ((set
= title
? title
: get_uri(t
)))
888 set
= window
? XT_NAME
: "(untitled)";
894 find_mime_type(char *mime_type
)
896 struct mime_type
*m
, *def
= NULL
, *rv
= NULL
;
898 TAILQ_FOREACH(m
, &mtl
, entry
) {
900 !strncmp(mime_type
, m
->mt_type
, strlen(m
->mt_type
)))
903 if (m
->mt_default
== 0 && !strcmp(mime_type
, m
->mt_type
)) {
916 wl_find_uri(const gchar
*s
, struct domain_list
*wl
)
922 if (s
== NULL
|| wl
== NULL
)
925 if (!strncmp(s
, "http://", strlen("http://")))
926 s
= &s
[strlen("http://")];
927 else if (!strncmp(s
, "https://", strlen("https://")))
928 s
= &s
[strlen("https://")];
933 for (i
= 0; i
< strlen(s
) + 1 /* yes er need this */; i
++)
934 /* chop string at first slash */
935 if (s
[i
] == '/' || s
[i
] == ':' || s
[i
] == '\0') {
947 js_ref_to_string(JSContextRef context
, JSValueRef ref
)
953 jsref
= JSValueToStringCopy(context
, ref
, NULL
);
957 l
= JSStringGetMaximumUTF8CStringSize(jsref
);
960 JSStringGetUTF8CString(jsref
, s
, l
);
961 JSStringRelease(jsref
);
966 #define XT_JS_DONE ("done;")
967 #define XT_JS_DONE_LEN (strlen(XT_JS_DONE))
968 #define XT_JS_INSERT ("insert;")
969 #define XT_JS_INSERT_LEN (strlen(XT_JS_INSERT))
972 run_script(struct tab
*t
, char *s
)
974 JSGlobalContextRef ctx
;
975 WebKitWebFrame
*frame
;
977 JSValueRef val
, exception
;
980 DNPRINTF(XT_D_JS
, "%s: tab %d %s\n", __func__
,
981 t
->tab_id
, s
== (char *)JS_HINTING
? "JS_HINTING" : s
);
983 frame
= webkit_web_view_get_main_frame(t
->wv
);
984 ctx
= webkit_web_frame_get_global_context(frame
);
986 str
= JSStringCreateWithUTF8CString(s
);
987 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
988 NULL
, 0, &exception
);
989 JSStringRelease(str
);
991 DNPRINTF(XT_D_JS
, "%s: val %p\n", __func__
, val
);
993 es
= js_ref_to_string(ctx
, exception
);
995 DNPRINTF(XT_D_JS
, "%s: exception %s\n", __func__
, es
);
1000 es
= js_ref_to_string(ctx
, val
);
1003 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
1005 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
1009 DNPRINTF(XT_D_JS
, "%s: val %s\n", __func__
, es
);
1018 enable_hints(struct tab
*t
)
1020 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
1023 run_script(t
, "hints.createHints('', 'F');");
1025 run_script(t
, "hints.createHints('', 'f');");
1026 t
->mode
= XT_MODE_HINT
;
1030 disable_hints(struct tab
*t
)
1032 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
1034 run_script(t
, "hints.clearHints();");
1035 t
->mode
= XT_MODE_COMMAND
;
1040 passthrough(struct tab
*t
, struct karg
*args
)
1042 t
->mode
= XT_MODE_PASSTHROUGH
;
1047 hint(struct tab
*t
, struct karg
*args
)
1050 DNPRINTF(XT_D_JS
, "hint: tab %d args %d\n", t
->tab_id
, args
->i
);
1052 if (t
->mode
== XT_MODE_HINT
) {
1053 if (args
->i
== XT_HINT_NEWTAB
)
1063 apply_style(struct tab
*t
)
1066 g_object_set(G_OBJECT(t
->settings
),
1067 "user-stylesheet-uri", t
->stylesheet
, (char *)NULL
);
1071 remove_style(struct tab
*t
)
1074 g_object_set(G_OBJECT(t
->settings
),
1075 "user-stylesheet-uri", NULL
, (char *)NULL
);
1079 userstyle(struct tab
*t
, struct karg
*args
)
1083 DNPRINTF(XT_D_JS
, "userstyle: tab %d\n", t
->tab_id
);
1086 case XT_STYLE_CURRENT_TAB
:
1092 case XT_STYLE_GLOBAL
:
1093 if (userstyle_global
) {
1094 userstyle_global
= 0;
1095 TAILQ_FOREACH(tt
, &tabs
, entry
)
1098 userstyle_global
= 1;
1099 TAILQ_FOREACH(tt
, &tabs
, entry
)
1109 quit(struct tab
*t
, struct karg
*args
)
1111 if (save_global_history
)
1112 save_global_history_to_disk(t
);
1120 restore_sessions_list(void)
1123 struct dirent
*dp
= NULL
;
1126 sdir
= opendir(sessions_dir
);
1128 while ((dp
= readdir(sdir
)) != NULL
)
1129 if (dp
->d_type
== DT_REG
) {
1130 s
= g_malloc(sizeof(struct session
));
1131 s
->name
= g_strdup(dp
->d_name
);
1132 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
1139 open_tabs(struct tab
*t
, struct karg
*a
)
1141 char file
[PATH_MAX
];
1145 struct tab
*ti
, *tt
;
1150 ti
= TAILQ_LAST(&tabs
, tab_list
);
1152 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, a
->s
);
1153 if ((f
= fopen(file
, "r")) == NULL
)
1157 if ((uri
= fparseln(f
, NULL
, NULL
, "\0\0\0", 0)) == NULL
)
1158 if (feof(f
) || ferror(f
))
1161 /* retrieve session name */
1162 if (uri
&& g_str_has_prefix(uri
, XT_SAVE_SESSION_ID
)) {
1163 strlcpy(named_session
,
1164 &uri
[strlen(XT_SAVE_SESSION_ID
)],
1165 sizeof named_session
);
1169 if (uri
&& strlen(uri
))
1170 create_new_tab(uri
, NULL
, 1, -1);
1176 /* close open tabs */
1177 if (a
->i
== XT_SES_CLOSETABS
&& ti
!= NULL
) {
1179 tt
= TAILQ_FIRST(&tabs
);
1200 restore_saved_tabs(void)
1202 char file
[PATH_MAX
];
1203 int unlink_file
= 0;
1208 snprintf(file
, sizeof file
, "%s/%s",
1209 sessions_dir
, XT_RESTART_TABS_FILE
);
1210 if (stat(file
, &sb
) == -1)
1211 a
.s
= XT_SAVED_TABS_FILE
;
1214 a
.s
= XT_RESTART_TABS_FILE
;
1217 a
.i
= XT_SES_DONOTHING
;
1218 rv
= open_tabs(NULL
, &a
);
1227 save_tabs(struct tab
*t
, struct karg
*a
)
1229 char file
[PATH_MAX
];
1231 int num_tabs
= 0, i
;
1232 struct tab
**stabs
= NULL
;
1234 /* tab may be null here */
1239 snprintf(file
, sizeof file
, "%s/%s",
1240 sessions_dir
, named_session
);
1242 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, a
->s
);
1244 if ((f
= fopen(file
, "w")) == NULL
) {
1245 show_oops(t
, "Can't open save_tabs file: %s", strerror(errno
));
1249 /* save session name */
1250 fprintf(f
, "%s%s\n", XT_SAVE_SESSION_ID
, named_session
);
1252 /* Save tabs, in the order they are arranged in the notebook. */
1253 num_tabs
= sort_tabs_by_page_num(&stabs
);
1255 for (i
= 0; i
< num_tabs
; i
++)
1257 if (get_uri(stabs
[i
]) != NULL
)
1258 fprintf(f
, "%s\n", get_uri(stabs
[i
]));
1259 else if (gtk_entry_get_text(GTK_ENTRY(
1260 stabs
[i
]->uri_entry
)))
1261 fprintf(f
, "%s\n", gtk_entry_get_text(GTK_ENTRY(
1262 stabs
[i
]->uri_entry
)));
1267 /* try and make sure this gets to disk NOW. XXX Backup first? */
1268 if (fflush(f
) != 0 || fsync(fileno(f
)) != 0) {
1269 show_oops(t
, "May not have managed to save session: %s",
1279 save_tabs_and_quit(struct tab
*t
, struct karg
*args
)
1291 run_page_script(struct tab
*t
, struct karg
*args
)
1294 char *tmp
, script
[PATH_MAX
];
1296 tmp
= args
->s
!= NULL
&& strlen(args
->s
) > 0 ? args
->s
: default_script
;
1297 if (tmp
[0] == '\0') {
1298 show_oops(t
, "no script specified");
1302 if ((uri
= get_uri(t
)) == NULL
) {
1303 show_oops(t
, "tab is empty, not running script");
1308 snprintf(script
, sizeof script
, "%s/%s",
1309 pwd
->pw_dir
, &tmp
[1]);
1311 strlcpy(script
, tmp
, sizeof script
);
1315 show_oops(t
, "can't fork to run script");
1325 execlp(script
, script
, uri
, (void *)NULL
);
1335 yank_uri(struct tab
*t
, struct karg
*args
)
1338 GtkClipboard
*clipboard
;
1340 if ((uri
= get_uri(t
)) == NULL
)
1343 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1344 gtk_clipboard_set_text(clipboard
, uri
, -1);
1350 paste_uri(struct tab
*t
, struct karg
*args
)
1352 GtkClipboard
*clipboard
;
1353 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
1355 gchar
*p
= NULL
, *uri
;
1357 /* try primary clipboard first */
1358 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1359 p
= gtk_clipboard_wait_for_text(clipboard
);
1361 /* if it failed get whatever text is in cut_buffer0 */
1362 if (p
== NULL
&& xterm_workaround
)
1363 if (gdk_property_get(gdk_get_default_root_window(),
1365 gdk_atom_intern("STRING", FALSE
),
1367 1024 * 1024 /* picked out of my butt */,
1375 /* yes sir, we need to NUL the string */
1381 while (*uri
&& isspace(*uri
))
1383 if (strlen(uri
) == 0) {
1384 show_oops(t
, "empty paste buffer");
1387 if (guess_search
== 0 && valid_url_type(uri
)) {
1388 /* we can be clever and paste this in search box */
1389 show_oops(t
, "not a valid URL");
1393 if (args
->i
== XT_PASTE_CURRENT_TAB
)
1395 else if (args
->i
== XT_PASTE_NEW_TAB
)
1396 create_new_tab(uri
, NULL
, 1, -1);
1407 js_toggle_cb(GtkWidget
*w
, struct tab
*t
)
1412 g_object_get(G_OBJECT(t
->settings
),
1413 "enable-scripts", &es
, (char *)NULL
);
1418 set
= XT_WL_DISABLE
;
1420 a
.i
= set
| XT_WL_TOPLEVEL
;
1423 a
.i
= set
| XT_WL_TOPLEVEL
;
1426 a
.i
= XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
;
1431 toggle_src(struct tab
*t
, struct karg
*args
)
1438 mode
= webkit_web_view_get_view_source_mode(t
->wv
);
1439 webkit_web_view_set_view_source_mode(t
->wv
, !mode
);
1440 webkit_web_view_reload(t
->wv
);
1446 focus_webview(struct tab
*t
)
1451 /* only grab focus if we are visible */
1452 if (gtk_notebook_get_current_page(notebook
) == t
->tab_id
)
1453 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
1457 focus(struct tab
*t
, struct karg
*args
)
1459 if (t
== NULL
|| args
== NULL
)
1465 if (args
->i
== XT_FOCUS_URI
)
1466 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
1467 else if (args
->i
== XT_FOCUS_SEARCH
)
1468 gtk_widget_grab_focus(GTK_WIDGET(t
->search_entry
));
1474 connect_socket_from_uri(const gchar
*uri
, const gchar
**error_str
, char *domain
,
1478 struct addrinfo hints
, *res
= NULL
, *ai
;
1479 int rv
= -1, s
= -1, on
, error
;
1481 static gchar myerror
[256]; /* this is not thread safe */
1484 *error_str
= myerror
;
1485 if (uri
&& !g_str_has_prefix(uri
, "https://")) {
1486 *error_str
= "invalid URI";
1490 su
= soup_uri_new(uri
);
1492 *error_str
= "invalid soup URI";
1495 if (!SOUP_URI_VALID_FOR_HTTP(su
)) {
1496 *error_str
= "invalid HTTPS URI";
1500 snprintf(port
, sizeof port
, "%d", su
->port
);
1501 bzero(&hints
, sizeof(struct addrinfo
));
1502 hints
.ai_flags
= AI_CANONNAME
;
1503 hints
.ai_family
= AF_UNSPEC
;
1504 hints
.ai_socktype
= SOCK_STREAM
;
1506 if ((error
= getaddrinfo(su
->host
, port
, &hints
, &res
))) {
1507 snprintf(myerror
, sizeof myerror
, "getaddrinfo failed: %s",
1508 gai_strerror(errno
));
1512 for (ai
= res
; ai
; ai
= ai
->ai_next
) {
1518 if (ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
)
1520 s
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
1523 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &on
,
1526 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) == 0)
1530 snprintf(myerror
, sizeof myerror
,
1531 "could not obtain certificates from: %s",
1537 strlcpy(domain
, su
->host
, domain_sz
);
1544 if (rv
== -1 && s
!= -1)
1551 stop_tls(gnutls_session_t gsession
, gnutls_certificate_credentials_t xcred
)
1554 gnutls_deinit(gsession
);
1556 gnutls_certificate_free_credentials(xcred
);
1562 start_tls(const gchar
**error_str
, int s
, gnutls_session_t
*gs
,
1563 gnutls_certificate_credentials_t
*xc
)
1565 gnutls_certificate_credentials_t xcred
;
1566 gnutls_session_t gsession
;
1568 static gchar myerror
[1024]; /* this is not thread safe */
1570 if (gs
== NULL
|| xc
== NULL
)
1577 gnutls_certificate_allocate_credentials(&xcred
);
1578 gnutls_certificate_set_x509_trust_file(xcred
, ssl_ca_file
,
1579 GNUTLS_X509_FMT_PEM
);
1581 gnutls_init(&gsession
, GNUTLS_CLIENT
);
1582 gnutls_priority_set_direct(gsession
, "PERFORMANCE", NULL
);
1583 gnutls_credentials_set(gsession
, GNUTLS_CRD_CERTIFICATE
, xcred
);
1584 gnutls_transport_set_ptr(gsession
, (gnutls_transport_ptr_t
)(long)s
);
1585 if ((rv
= gnutls_handshake(gsession
)) < 0) {
1586 snprintf(myerror
, sizeof myerror
,
1587 "gnutls_handshake failed %d fatal %d %s",
1589 gnutls_error_is_fatal(rv
),
1590 #if LIBGNUTLS_VERSION_MAJOR >= 2 && LIBGNUTLS_VERSION_MINOR >= 6
1591 gnutls_strerror_name(rv
));
1593 "GNUTLS version is too old to provide human readable error");
1595 stop_tls(gsession
, xcred
);
1599 gnutls_credentials_type_t cred
;
1600 cred
= gnutls_auth_get_type(gsession
);
1601 if (cred
!= GNUTLS_CRD_CERTIFICATE
) {
1602 snprintf(myerror
, sizeof myerror
,
1603 "gnutls_auth_get_type failed %d",
1605 stop_tls(gsession
, xcred
);
1613 *error_str
= myerror
;
1618 get_connection_certs(gnutls_session_t gsession
, gnutls_x509_crt_t
**certs
,
1622 const gnutls_datum_t
*cl
;
1623 gnutls_x509_crt_t
*all_certs
;
1626 if (certs
== NULL
|| cert_count
== NULL
)
1628 if (gnutls_certificate_type_get(gsession
) != GNUTLS_CRT_X509
)
1630 cl
= gnutls_certificate_get_peers(gsession
, &len
);
1634 all_certs
= g_malloc(sizeof(gnutls_x509_crt_t
) * len
);
1635 for (i
= 0; i
< len
; i
++) {
1636 gnutls_x509_crt_init(&all_certs
[i
]);
1637 if (gnutls_x509_crt_import(all_certs
[i
], &cl
[i
],
1638 GNUTLS_X509_FMT_PEM
< 0)) {
1652 free_connection_certs(gnutls_x509_crt_t
*certs
, size_t cert_count
)
1656 for (i
= 0; i
< cert_count
; i
++)
1657 gnutls_x509_crt_deinit(certs
[i
]);
1662 statusbar_modify_attr(struct tab
*t
, const char *text
, const char *base
)
1664 GdkColor c_text
, c_base
;
1666 gdk_color_parse(text
, &c_text
);
1667 gdk_color_parse(base
, &c_base
);
1669 gtk_widget_modify_text(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_text
);
1670 gtk_widget_modify_text(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_text
);
1671 gtk_widget_modify_text(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_text
);
1672 gtk_widget_modify_text(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_text
);
1674 gtk_widget_modify_base(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_base
);
1675 gtk_widget_modify_base(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_base
);
1676 gtk_widget_modify_base(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_base
);
1677 gtk_widget_modify_base(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_base
);
1681 save_certs(struct tab
*t
, gnutls_x509_crt_t
*certs
,
1682 size_t cert_count
, char *domain
)
1685 char cert_buf
[64 * 1024], file
[PATH_MAX
];
1690 if (t
== NULL
|| certs
== NULL
|| cert_count
<= 0 || domain
== NULL
)
1693 snprintf(file
, sizeof file
, "%s/%s", certs_dir
, domain
);
1694 if ((f
= fopen(file
, "w")) == NULL
) {
1695 show_oops(t
, "Can't create cert file %s %s",
1696 file
, strerror(errno
));
1700 for (i
= 0; i
< cert_count
; i
++) {
1701 cert_buf_sz
= sizeof cert_buf
;
1702 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1703 cert_buf
, &cert_buf_sz
)) {
1704 show_oops(t
, "gnutls_x509_crt_export failed");
1707 if (fwrite(cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1708 show_oops(t
, "Can't write certs: %s", strerror(errno
));
1713 /* not the best spot but oh well */
1714 gdk_color_parse("lightblue", &color
);
1715 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
1716 statusbar_modify_attr(t
, XT_COLOR_BLACK
, "lightblue");
1729 load_compare_cert(const gchar
*uri
, const gchar
**error_str
)
1731 char domain
[8182], file
[PATH_MAX
];
1732 char cert_buf
[64 * 1024], r_cert_buf
[64 * 1024];
1734 unsigned int error
= 0;
1736 size_t cert_buf_sz
, cert_count
;
1737 enum cert_trust rv
= CERT_UNTRUSTED
;
1738 static gchar serr
[80]; /* this isn't thread safe */
1739 gnutls_session_t gsession
;
1740 gnutls_x509_crt_t
*certs
;
1741 gnutls_certificate_credentials_t xcred
;
1743 DNPRINTF(XT_D_URL
, "%s: %s\n", __func__
, uri
);
1747 if ((s
= connect_socket_from_uri(uri
, error_str
, domain
,
1748 sizeof domain
)) == -1)
1751 DNPRINTF(XT_D_URL
, "%s: fd %d\n", __func__
, s
);
1754 if (start_tls(error_str
, s
, &gsession
, &xcred
))
1756 DNPRINTF(XT_D_URL
, "%s: got tls\n", __func__
);
1758 /* verify certs in case cert file doesn't exist */
1759 if (gnutls_certificate_verify_peers2(gsession
, &error
) !=
1761 *error_str
= "Invalid certificates";
1766 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1767 *error_str
= "Can't get connection certificates";
1771 snprintf(file
, sizeof file
, "%s/%s", certs_dir
, domain
);
1772 if ((f
= fopen(file
, "r")) == NULL
) {
1778 for (i
= 0; i
< cert_count
; i
++) {
1779 cert_buf_sz
= sizeof cert_buf
;
1780 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1781 cert_buf
, &cert_buf_sz
)) {
1784 if (fread(r_cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1785 rv
= CERT_BAD
; /* critical */
1788 if (bcmp(r_cert_buf
, cert_buf
, sizeof cert_buf_sz
)) {
1789 rv
= CERT_BAD
; /* critical */
1798 free_connection_certs(certs
, cert_count
);
1800 /* we close the socket first for speed */
1804 /* only complain if we didn't save it locally */
1805 if (error
&& rv
!= CERT_LOCAL
) {
1806 strlcpy(serr
, "Certificate exception(s): ", sizeof serr
);
1807 if (error
& GNUTLS_CERT_INVALID
)
1808 strlcat(serr
, "invalid, ", sizeof serr
);
1809 if (error
& GNUTLS_CERT_REVOKED
)
1810 strlcat(serr
, "revoked, ", sizeof serr
);
1811 if (error
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
1812 strlcat(serr
, "signer not found, ", sizeof serr
);
1813 if (error
& GNUTLS_CERT_SIGNER_NOT_CA
)
1814 strlcat(serr
, "not signed by CA, ", sizeof serr
);
1815 if (error
& GNUTLS_CERT_INSECURE_ALGORITHM
)
1816 strlcat(serr
, "insecure algorithm, ", sizeof serr
);
1817 #if LIBGNUTLS_VERSION_MAJOR >= 2 && LIBGNUTLS_VERSION_MINOR >= 6
1818 if (error
& GNUTLS_CERT_NOT_ACTIVATED
)
1819 strlcat(serr
, "not activated, ", sizeof serr
);
1820 if (error
& GNUTLS_CERT_EXPIRED
)
1821 strlcat(serr
, "expired, ", sizeof serr
);
1823 for (i
= strlen(serr
) - 1; i
> 0; i
--)
1824 if (serr
[i
] == ',') {
1831 stop_tls(gsession
, xcred
);
1837 cert_cmd(struct tab
*t
, struct karg
*args
)
1839 const gchar
*uri
, *error_str
= NULL
;
1843 gnutls_session_t gsession
;
1844 gnutls_x509_crt_t
*certs
;
1845 gnutls_certificate_credentials_t xcred
;
1850 if (ssl_ca_file
== NULL
) {
1851 show_oops(t
, "Can't open CA file: %s", ssl_ca_file
);
1855 if ((uri
= get_uri(t
)) == NULL
) {
1856 show_oops(t
, "Invalid URI");
1860 if ((s
= connect_socket_from_uri(uri
, &error_str
, domain
,
1861 sizeof domain
)) == -1) {
1862 show_oops(t
, "%s", error_str
);
1867 if (start_tls(&error_str
, s
, &gsession
, &xcred
))
1871 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1872 show_oops(t
, "get_connection_certs failed");
1876 if (args
->i
& XT_SHOW
)
1877 show_certs(t
, certs
, cert_count
, "Certificate Chain");
1878 else if (args
->i
& XT_SAVE
)
1879 save_certs(t
, certs
, cert_count
, domain
);
1881 free_connection_certs(certs
, cert_count
);
1883 /* we close the socket first for speed */
1886 stop_tls(gsession
, xcred
);
1887 if (error_str
&& strlen(error_str
))
1888 show_oops(t
, "%s", error_str
);
1893 remove_cookie(int index
)
1899 DNPRINTF(XT_D_COOKIE
, "remove_cookie: %d\n", index
);
1901 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
1903 for (i
= 1; cf
; cf
= cf
->next
, i
++) {
1907 print_cookie("remove cookie", c
);
1908 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
1913 soup_cookies_free(cf
);
1919 remove_cookie_domain(int domain_id
)
1921 int domain_count
, rv
= 1;
1926 DNPRINTF(XT_D_COOKIE
, "remove_cookie_domain: %d\n", domain_id
);
1929 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
1931 for (domain_count
= 0; cf
; cf
= cf
->next
) {
1934 if (strcmp(last_domain
, c
->domain
) != 0) {
1936 last_domain
= c
->domain
;
1939 if (domain_count
< domain_id
)
1941 else if (domain_count
> domain_id
)
1944 print_cookie("remove cookie", c
);
1945 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
1949 soup_cookies_free(cf
);
1955 toplevel_cmd(struct tab
*t
, struct karg
*args
)
1957 js_toggle_cb(t
->js_toggle
, t
);
1963 can_go_back_for_real(struct tab
*t
)
1966 WebKitWebHistoryItem
*item
;
1972 /* rely on webkit to make sure we can go backward when on an about page */
1974 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
1975 return (webkit_web_view_can_go_back(t
->wv
));
1977 /* the back/forwars list is stupid so help determine if we can go back */
1978 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
1980 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
1981 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
1989 can_go_forward_for_real(struct tab
*t
)
1992 WebKitWebHistoryItem
*item
;
1998 /* rely on webkit to make sure we can go forward when on an about page */
2000 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
2001 return (webkit_web_view_can_go_forward(t
->wv
));
2003 /* the back/forwars list is stupid so help selecting a different item */
2004 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2006 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2007 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
2015 go_back_for_real(struct tab
*t
)
2018 WebKitWebHistoryItem
*item
;
2026 webkit_web_view_go_back(t
->wv
);
2029 /* the back/forwars list is stupid so help selecting a different item */
2030 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2032 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2033 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2034 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2041 go_forward_for_real(struct tab
*t
)
2044 WebKitWebHistoryItem
*item
;
2052 webkit_web_view_go_forward(t
->wv
);
2055 /* the back/forwars list is stupid so help selecting a different item */
2056 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2058 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2059 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2060 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2067 navaction(struct tab
*t
, struct karg
*args
)
2069 WebKitWebHistoryItem
*item
;
2070 WebKitWebFrame
*frame
;
2072 DNPRINTF(XT_D_NAV
, "navaction: tab %d opcode %d\n",
2073 t
->tab_id
, args
->i
);
2075 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
2077 if (args
->i
== XT_NAV_BACK
)
2078 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2080 item
= webkit_web_back_forward_list_get_forward_item(t
->bfl
);
2082 return (XT_CB_PASSTHROUGH
);
2083 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2085 return (XT_CB_PASSTHROUGH
);
2091 go_back_for_real(t
);
2093 case XT_NAV_FORWARD
:
2095 go_forward_for_real(t
);
2098 frame
= webkit_web_view_get_main_frame(t
->wv
);
2099 webkit_web_frame_reload(frame
);
2102 frame
= webkit_web_view_get_main_frame(t
->wv
);
2103 webkit_web_frame_stop_loading(frame
);
2106 return (XT_CB_PASSTHROUGH
);
2110 move(struct tab
*t
, struct karg
*args
)
2112 GtkAdjustment
*adjust
;
2113 double pi
, si
, pos
, ps
, upper
, lower
, max
;
2119 case XT_MOVE_BOTTOM
:
2121 case XT_MOVE_PAGEDOWN
:
2122 case XT_MOVE_PAGEUP
:
2123 case XT_MOVE_HALFDOWN
:
2124 case XT_MOVE_HALFUP
:
2125 case XT_MOVE_PERCENT
:
2126 case XT_MOVE_CENTER
:
2127 adjust
= t
->adjust_v
;
2130 adjust
= t
->adjust_h
;
2134 pos
= gtk_adjustment_get_value(adjust
);
2135 ps
= gtk_adjustment_get_page_size(adjust
);
2136 upper
= gtk_adjustment_get_upper(adjust
);
2137 lower
= gtk_adjustment_get_lower(adjust
);
2138 si
= gtk_adjustment_get_step_increment(adjust
);
2139 pi
= gtk_adjustment_get_page_increment(adjust
);
2142 DNPRINTF(XT_D_MOVE
, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2143 "max %f si %f pi %f\n",
2144 args
->i
, adjust
== t
->adjust_h
? "horizontal" : "vertical",
2145 pos
, ps
, upper
, lower
, max
, si
, pi
);
2151 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2156 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2158 case XT_MOVE_BOTTOM
:
2159 case XT_MOVE_FARRIGHT
:
2160 gtk_adjustment_set_value(adjust
, max
);
2163 case XT_MOVE_FARLEFT
:
2164 gtk_adjustment_set_value(adjust
, lower
);
2166 case XT_MOVE_PAGEDOWN
:
2168 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2170 case XT_MOVE_PAGEUP
:
2172 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2174 case XT_MOVE_HALFDOWN
:
2176 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2178 case XT_MOVE_HALFUP
:
2180 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2182 case XT_MOVE_CENTER
:
2183 args
->s
= g_strdup("50.0");
2185 case XT_MOVE_PERCENT
:
2186 percent
= atoi(args
->s
) / 100.0;
2187 pos
= max
* percent
;
2188 if (pos
< 0.0 || pos
> max
)
2190 gtk_adjustment_set_value(adjust
, pos
);
2193 return (XT_CB_PASSTHROUGH
);
2196 DNPRINTF(XT_D_MOVE
, "move: new pos %f %f\n", pos
, MIN(pos
, max
));
2198 return (XT_CB_HANDLED
);
2202 url_set_visibility(void)
2206 TAILQ_FOREACH(t
, &tabs
, entry
)
2207 if (show_url
== 0) {
2208 gtk_widget_hide(t
->toolbar
);
2211 gtk_widget_show(t
->toolbar
);
2215 notebook_tab_set_visibility(void)
2217 if (show_tabs
== 0) {
2218 gtk_widget_hide(tab_bar
);
2219 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2221 if (tab_style
== XT_TABS_NORMAL
) {
2222 gtk_widget_hide(tab_bar
);
2223 gtk_notebook_set_show_tabs(notebook
, TRUE
);
2224 } else if (tab_style
== XT_TABS_COMPACT
) {
2225 gtk_widget_show(tab_bar
);
2226 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2232 statusbar_set_visibility(void)
2236 TAILQ_FOREACH(t
, &tabs
, entry
){
2237 if (show_statusbar
== 0)
2238 gtk_widget_hide(t
->statusbar_box
);
2240 gtk_widget_show(t
->statusbar_box
);
2247 url_set(struct tab
*t
, int enable_url_entry
)
2252 show_url
= enable_url_entry
;
2254 if (enable_url_entry
) {
2255 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
2256 GTK_ENTRY_ICON_PRIMARY
, NULL
);
2257 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
), 0);
2259 pixbuf
= gtk_entry_get_icon_pixbuf(GTK_ENTRY(t
->uri_entry
),
2260 GTK_ENTRY_ICON_PRIMARY
);
2262 gtk_entry_get_progress_fraction(GTK_ENTRY(t
->uri_entry
));
2263 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
2264 GTK_ENTRY_ICON_PRIMARY
, pixbuf
);
2265 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
2271 fullscreen(struct tab
*t
, struct karg
*args
)
2273 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2276 return (XT_CB_PASSTHROUGH
);
2278 if (show_url
== 0) {
2286 url_set_visibility();
2287 notebook_tab_set_visibility();
2289 return (XT_CB_HANDLED
);
2293 statustoggle(struct tab
*t
, struct karg
*args
)
2295 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2297 if (show_statusbar
== 1) {
2299 statusbar_set_visibility();
2300 } else if (show_statusbar
== 0) {
2302 statusbar_set_visibility();
2304 return (XT_CB_HANDLED
);
2308 urlaction(struct tab
*t
, struct karg
*args
)
2310 int rv
= XT_CB_HANDLED
;
2312 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2315 return (XT_CB_PASSTHROUGH
);
2319 if (show_url
== 0) {
2321 url_set_visibility();
2325 if (show_url
== 1) {
2327 url_set_visibility();
2335 tabaction(struct tab
*t
, struct karg
*args
)
2337 int rv
= XT_CB_HANDLED
;
2338 char *url
= args
->s
;
2342 DNPRINTF(XT_D_TAB
, "tabaction: %p %d\n", t
, args
->i
);
2345 return (XT_CB_PASSTHROUGH
);
2349 if (strlen(url
) > 0)
2350 create_new_tab(url
, NULL
, 1, args
->precount
);
2352 create_new_tab(NULL
, NULL
, 1, args
->precount
);
2355 if (args
->precount
< 0)
2358 TAILQ_FOREACH(tt
, &tabs
, entry
)
2359 if (tt
->tab_id
== args
->precount
- 1) {
2364 case XT_TAB_DELQUIT
:
2365 if (gtk_notebook_get_n_pages(notebook
) > 1)
2371 if (strlen(url
) > 0)
2374 rv
= XT_CB_PASSTHROUGH
;
2380 if (show_tabs
== 0) {
2382 notebook_tab_set_visibility();
2386 if (show_tabs
== 1) {
2388 notebook_tab_set_visibility();
2391 case XT_TAB_NEXTSTYLE
:
2392 if (tab_style
== XT_TABS_NORMAL
) {
2393 tab_style
= XT_TABS_COMPACT
;
2394 recolor_compact_tabs();
2397 tab_style
= XT_TABS_NORMAL
;
2398 notebook_tab_set_visibility();
2400 case XT_TAB_UNDO_CLOSE
:
2401 if (undo_count
== 0) {
2402 DNPRINTF(XT_D_TAB
, "%s: no tabs to undo close",
2407 u
= TAILQ_FIRST(&undos
);
2408 create_new_tab(u
->uri
, u
, 1, -1);
2410 TAILQ_REMOVE(&undos
, u
, entry
);
2412 /* u->history is freed in create_new_tab() */
2416 case XT_TAB_LOAD_IMAGES
:
2418 if (!auto_load_images
) {
2420 /* Enable auto-load images (this will load all
2421 * previously unloaded images). */
2422 g_object_set(G_OBJECT(t
->settings
),
2423 "auto-load-images", TRUE
, (char *)NULL
);
2424 webkit_web_view_set_settings(t
->wv
, t
->settings
);
2426 webkit_web_view_reload(t
->wv
);
2428 /* Webkit triggers an event when we change the setting,
2429 * so we can't disable the auto-loading at once.
2431 * Unfortunately, webkit does not tell us when it's done.
2432 * Instead, we wait until the next request, and then
2433 * disable autoloading again.
2435 t
->load_images
= TRUE
;
2439 rv
= XT_CB_PASSTHROUGH
;
2453 resizetab(struct tab
*t
, struct karg
*args
)
2455 if (t
== NULL
|| args
== NULL
) {
2456 show_oops(NULL
, "resizetab invalid parameters");
2457 return (XT_CB_PASSTHROUGH
);
2460 DNPRINTF(XT_D_TAB
, "resizetab: tab %d %d\n",
2461 t
->tab_id
, args
->i
);
2463 setzoom_webkit(t
, args
->i
);
2465 return (XT_CB_HANDLED
);
2469 movetab(struct tab
*t
, struct karg
*args
)
2473 if (t
== NULL
|| args
== NULL
) {
2474 show_oops(NULL
, "movetab invalid parameters");
2475 return (XT_CB_PASSTHROUGH
);
2478 DNPRINTF(XT_D_TAB
, "movetab: tab %d opcode %d\n",
2479 t
->tab_id
, args
->i
);
2481 if (args
->i
>= XT_TAB_INVALID
)
2482 return (XT_CB_PASSTHROUGH
);
2484 if (TAILQ_EMPTY(&tabs
))
2485 return (XT_CB_PASSTHROUGH
);
2487 n
= gtk_notebook_get_n_pages(notebook
);
2488 dest
= gtk_notebook_get_current_page(notebook
);
2492 if (args
->precount
< 0)
2493 dest
= dest
== n
- 1 ? 0 : dest
+ 1;
2495 dest
= args
->precount
- 1;
2499 if (args
->precount
< 0)
2502 dest
-= args
->precount
% n
;
2515 return (XT_CB_PASSTHROUGH
);
2518 if (dest
< 0 || dest
>= n
)
2519 return (XT_CB_PASSTHROUGH
);
2520 if (t
->tab_id
== dest
) {
2521 DNPRINTF(XT_D_TAB
, "movetab: do nothing\n");
2522 return (XT_CB_HANDLED
);
2525 set_current_tab(dest
);
2527 return (XT_CB_HANDLED
);
2534 command(struct tab
*t
, struct karg
*args
)
2536 char *s
= NULL
, *ss
= NULL
;
2541 if (t
== NULL
|| args
== NULL
) {
2542 show_oops(NULL
, "command invalid parameters");
2543 return (XT_CB_PASSTHROUGH
);
2554 if (cmd_prefix
== 0)
2557 ss
= g_strdup_printf(":%d", cmd_prefix
);
2563 t
->mode
= XT_MODE_HINT
;
2564 bzero(&a
, sizeof a
);
2570 t
->mode
= XT_MODE_HINT
;
2571 bzero(&a
, sizeof a
);
2572 a
.i
= XT_HINT_NEWTAB
;
2582 case XT_CMD_OPEN_CURRENT
:
2585 case XT_CMD_TABNEW_CURRENT
:
2586 if (!s
) /* FALL THROUGH? */
2588 if ((uri
= get_uri(t
)) != NULL
) {
2589 ss
= g_strdup_printf("%s%s", s
, uri
);
2594 show_oops(t
, "command: invalid opcode %d", args
->i
);
2595 return (XT_CB_PASSTHROUGH
);
2598 DNPRINTF(XT_D_CMD
, "%s: tab %d type %s\n", __func__
, t
->tab_id
, s
);
2600 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), s
);
2601 gdk_color_parse(XT_COLOR_WHITE
, &color
);
2602 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
2604 gtk_widget_grab_focus(GTK_WIDGET(t
->cmd
));
2605 gtk_editable_set_position(GTK_EDITABLE(t
->cmd
), -1);
2610 return (XT_CB_HANDLED
);
2614 search(struct tab
*t
, struct karg
*args
)
2618 if (t
== NULL
|| args
== NULL
) {
2619 show_oops(NULL
, "search invalid parameters");
2624 case XT_SEARCH_NEXT
:
2625 d
= t
->search_forward
;
2627 case XT_SEARCH_PREV
:
2628 d
= !t
->search_forward
;
2631 return (XT_CB_PASSTHROUGH
);
2634 if (t
->search_text
== NULL
) {
2635 if (global_search
== NULL
)
2636 return (XT_CB_PASSTHROUGH
);
2638 d
= t
->search_forward
= TRUE
;
2639 t
->search_text
= g_strdup(global_search
);
2640 webkit_web_view_mark_text_matches(t
->wv
, global_search
, FALSE
, 0);
2641 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
2645 DNPRINTF(XT_D_CMD
, "search: tab %d opc %d forw %d text %s\n",
2646 t
->tab_id
, args
->i
, t
->search_forward
, t
->search_text
);
2648 webkit_web_view_search_text(t
->wv
, t
->search_text
, FALSE
, d
, TRUE
);
2650 return (XT_CB_HANDLED
);
2654 session_save(struct tab
*t
, char *filename
)
2660 if (strlen(filename
) == 0)
2663 if (filename
[0] == '.' || filename
[0] == '/')
2667 if (save_tabs(t
, &a
))
2669 strlcpy(named_session
, filename
, sizeof named_session
);
2671 /* add the new session to the list of sessions */
2672 s
= g_malloc(sizeof(struct session
));
2673 s
->name
= g_strdup(filename
);
2674 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
2682 session_open(struct tab
*t
, char *filename
)
2687 if (strlen(filename
) == 0)
2690 if (filename
[0] == '.' || filename
[0] == '/')
2694 a
.i
= XT_SES_CLOSETABS
;
2695 if (open_tabs(t
, &a
))
2698 strlcpy(named_session
, filename
, sizeof named_session
);
2706 session_delete(struct tab
*t
, char *filename
)
2708 char file
[PATH_MAX
];
2712 if (strlen(filename
) == 0)
2715 if (filename
[0] == '.' || filename
[0] == '/')
2718 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, filename
);
2722 if (!strcmp(filename
, named_session
))
2723 strlcpy(named_session
, XT_SAVED_TABS_FILE
,
2724 sizeof named_session
);
2726 /* remove session from sessions list */
2727 TAILQ_FOREACH(s
, &sessions
, entry
) {
2728 if (!strcmp(s
->name
, filename
))
2733 TAILQ_REMOVE(&sessions
, s
, entry
);
2734 g_free((gpointer
) s
->name
);
2743 session_cmd(struct tab
*t
, struct karg
*args
)
2745 char *filename
= args
->s
;
2750 if (args
->i
& XT_SHOW
)
2751 show_oops(t
, "Current session: %s", named_session
[0] == '\0' ?
2752 XT_SAVED_TABS_FILE
: named_session
);
2753 else if (args
->i
& XT_SAVE
) {
2754 if (session_save(t
, filename
)) {
2755 show_oops(t
, "Can't save session: %s",
2756 filename
? filename
: "INVALID");
2759 } else if (args
->i
& XT_OPEN
) {
2760 if (session_open(t
, filename
)) {
2761 show_oops(t
, "Can't open session: %s",
2762 filename
? filename
: "INVALID");
2765 } else if (args
->i
& XT_DELETE
) {
2766 if (session_delete(t
, filename
)) {
2767 show_oops(t
, "Can't delete session: %s",
2768 filename
? filename
: "INVALID");
2773 return (XT_CB_PASSTHROUGH
);
2777 script_cmd(struct tab
*t
, struct karg
*args
)
2786 if ((f
= fopen(args
->s
, "r")) == NULL
) {
2787 show_oops(t
, "Can't open script file: %s", args
->s
);
2791 if (fstat(fileno(f
), &sb
) == -1) {
2792 show_oops(t
, "Can't stat script file: %s", args
->s
);
2796 buf
= g_malloc0(sb
.st_size
+ 1);
2797 if (fread(buf
, 1, sb
.st_size
, f
) != sb
.st_size
) {
2798 show_oops(t
, "Can't read script file: %s", args
->s
);
2802 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
2811 return (XT_CB_PASSTHROUGH
);
2815 * Make a hardcopy of the page
2818 print_page(struct tab
*t
, struct karg
*args
)
2820 WebKitWebFrame
*frame
;
2822 GtkPrintOperation
*op
;
2823 GtkPrintOperationAction action
;
2824 GtkPrintOperationResult print_res
;
2825 GError
*g_err
= NULL
;
2826 int marg_l
, marg_r
, marg_t
, marg_b
;
2828 DNPRINTF(XT_D_PRINTING
, "%s:", __func__
);
2830 ps
= gtk_page_setup_new();
2831 op
= gtk_print_operation_new();
2832 action
= GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
;
2833 frame
= webkit_web_view_get_main_frame(t
->wv
);
2835 /* the default margins are too small, so we will bump them */
2836 marg_l
= gtk_page_setup_get_left_margin(ps
, GTK_UNIT_MM
) +
2837 XT_PRINT_EXTRA_MARGIN
;
2838 marg_r
= gtk_page_setup_get_right_margin(ps
, GTK_UNIT_MM
) +
2839 XT_PRINT_EXTRA_MARGIN
;
2840 marg_t
= gtk_page_setup_get_top_margin(ps
, GTK_UNIT_MM
) +
2841 XT_PRINT_EXTRA_MARGIN
;
2842 marg_b
= gtk_page_setup_get_bottom_margin(ps
, GTK_UNIT_MM
) +
2843 XT_PRINT_EXTRA_MARGIN
;
2846 gtk_page_setup_set_left_margin(ps
, marg_l
, GTK_UNIT_MM
);
2847 gtk_page_setup_set_right_margin(ps
, marg_r
, GTK_UNIT_MM
);
2848 gtk_page_setup_set_top_margin(ps
, marg_t
, GTK_UNIT_MM
);
2849 gtk_page_setup_set_bottom_margin(ps
, marg_b
, GTK_UNIT_MM
);
2851 gtk_print_operation_set_default_page_setup(op
, ps
);
2853 /* this appears to free 'op' and 'ps' */
2854 print_res
= webkit_web_frame_print_full(frame
, op
, action
, &g_err
);
2856 /* check it worked */
2857 if (print_res
== GTK_PRINT_OPERATION_RESULT_ERROR
) {
2858 show_oops(NULL
, "can't print: %s", g_err
->message
);
2859 g_error_free (g_err
);
2867 go_home(struct tab
*t
, struct karg
*args
)
2874 set_encoding(struct tab
*t
, struct karg
*args
)
2878 if (args
->s
&& strlen(g_strstrip(args
->s
)) == 0) {
2879 e
= webkit_web_view_get_custom_encoding(t
->wv
);
2881 e
= webkit_web_view_get_encoding(t
->wv
);
2882 show_oops(t
, "encoding: %s", e
? e
: "N/A");
2884 webkit_web_view_set_custom_encoding(t
->wv
, args
->s
);
2890 restart(struct tab
*t
, struct karg
*args
)
2894 a
.s
= XT_RESTART_TABS_FILE
;
2896 execvp(start_argv
[0], start_argv
);
2902 char *http_proxy_save
; /* not a setting, used to toggle */
2905 proxy_cmd(struct tab
*t
, struct karg
*args
)
2907 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
2914 if (http_proxy_save
)
2915 g_free(http_proxy_save
);
2916 http_proxy_save
= g_strdup(http_proxy
);
2919 if (args
->i
& XT_PRXY_SHOW
) {
2921 show_oops(t
, "http_proxy = %s", http_proxy
);
2923 show_oops(t
, "proxy is currently disabled");
2924 } else if (args
->i
& XT_PRXY_TOGGLE
) {
2925 if (http_proxy_save
== NULL
&& http_proxy
== NULL
) {
2926 show_oops(t
, "can't toggle proxy");
2932 setup_proxy(http_proxy_save
);
2935 return (XT_CB_PASSTHROUGH
);
2940 int (*func
)(struct tab
*, struct karg
*);
2944 { "command_mode", 0, command_mode
, XT_MODE_COMMAND
, 0 },
2945 { "insert_mode", 0, command_mode
, XT_MODE_INSERT
, 0 },
2946 { "command", 0, command
, ':', 0 },
2947 { "search", 0, command
, '/', 0 },
2948 { "searchb", 0, command
, '?', 0 },
2949 { "hinting", 0, command
, '.', 0 },
2950 { "hinting_newtab", 0, command
, ',', 0 },
2951 { "togglesrc", 0, toggle_src
, 0, 0 },
2952 { "editsrc", 0, edit_src
, 0, 0 },
2953 { "editelement", 0, edit_element
, 0, 0 },
2954 { "passthrough", 0, passthrough
, 0, 0 },
2956 /* yanking and pasting */
2957 { "yankuri", 0, yank_uri
, 0, 0 },
2958 /* XXX: pasteuri{cur,new} do not work from the cmd_entry? */
2959 { "pasteuricur", 0, paste_uri
, XT_PASTE_CURRENT_TAB
, 0 },
2960 { "pasteurinew", 0, paste_uri
, XT_PASTE_NEW_TAB
, 0 },
2963 { "searchnext", 0, search
, XT_SEARCH_NEXT
, 0 },
2964 { "searchprevious", 0, search
, XT_SEARCH_PREV
, 0 },
2967 { "focusaddress", 0, focus
, XT_FOCUS_URI
, 0 },
2968 { "focussearch", 0, focus
, XT_FOCUS_SEARCH
, 0 },
2971 { "hinting", 0, hint
, 0, 0 },
2972 { "hinting_newtab", 0, hint
, XT_HINT_NEWTAB
, 0 },
2974 /* custom stylesheet */
2975 { "userstyle", 0, userstyle
, XT_STYLE_CURRENT_TAB
, 0 },
2976 { "userstyle_global", 0, userstyle
, XT_STYLE_GLOBAL
, 0 },
2979 { "goback", 0, navaction
, XT_NAV_BACK
, 0 },
2980 { "goforward", 0, navaction
, XT_NAV_FORWARD
, 0 },
2981 { "reload", 0, navaction
, XT_NAV_RELOAD
, 0 },
2982 { "stop", 0, navaction
, XT_NAV_STOP
, 0 },
2984 /* vertical movement */
2985 { "scrolldown", 0, move
, XT_MOVE_DOWN
, 0 },
2986 { "scrollup", 0, move
, XT_MOVE_UP
, 0 },
2987 { "scrollbottom", 0, move
, XT_MOVE_BOTTOM
, 0 },
2988 { "scrolltop", 0, move
, XT_MOVE_TOP
, 0 },
2989 { "1", 0, move
, XT_MOVE_TOP
, 0 },
2990 { "scrollhalfdown", 0, move
, XT_MOVE_HALFDOWN
, 0 },
2991 { "scrollhalfup", 0, move
, XT_MOVE_HALFUP
, 0 },
2992 { "scrollpagedown", 0, move
, XT_MOVE_PAGEDOWN
, 0 },
2993 { "scrollpageup", 0, move
, XT_MOVE_PAGEUP
, 0 },
2994 /* horizontal movement */
2995 { "scrollright", 0, move
, XT_MOVE_RIGHT
, 0 },
2996 { "scrollleft", 0, move
, XT_MOVE_LEFT
, 0 },
2997 { "scrollfarright", 0, move
, XT_MOVE_FARRIGHT
, 0 },
2998 { "scrollfarleft", 0, move
, XT_MOVE_FARLEFT
, 0 },
3000 { "favorites", 0, xtp_page_fl
, 0, 0 },
3001 { "fav", 0, xtp_page_fl
, 0, 0 },
3002 { "favadd", 0, add_favorite
, 0, 0 },
3004 { "qall", 0, quit
, 0, 0 },
3005 { "quitall", 0, quit
, 0, 0 },
3006 { "w", 0, save_tabs
, 0, 0 },
3007 { "wq", 0, save_tabs_and_quit
, 0, 0 },
3008 { "help", 0, help
, 0, 0 },
3009 { "about", 0, about
, 0, 0 },
3010 { "stats", 0, stats
, 0, 0 },
3011 { "version", 0, about
, 0, 0 },
3014 { "js", 0, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3015 { "save", 1, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3016 { "domain", 2, js_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3017 { "fqdn", 2, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3018 { "show", 1, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3019 { "all", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3020 { "persistent", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3021 { "session", 2, js_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3022 { "toggle", 1, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3023 { "domain", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3024 { "fqdn", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3026 /* cookie command */
3027 { "cookie", 0, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3028 { "save", 1, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3029 { "domain", 2, cookie_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3030 { "fqdn", 2, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3031 { "show", 1, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3032 { "all", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3033 { "persistent", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3034 { "session", 2, cookie_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3035 { "toggle", 1, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3036 { "domain", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3037 { "fqdn", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3039 /* plugin command */
3040 { "plugin", 0, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3041 { "save", 1, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3042 { "domain", 2, pl_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3043 { "fqdn", 2, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3044 { "show", 1, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3045 { "all", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3046 { "persistent", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3047 { "session", 2, pl_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3048 { "toggle", 1, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3049 { "domain", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3050 { "fqdn", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3052 /* toplevel (domain) command */
3053 { "toplevel", 0, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3054 { "toggle", 1, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3057 { "cookiejar", 0, xtp_page_cl
, 0, 0 },
3060 { "cert", 0, cert_cmd
, XT_SHOW
, 0 },
3061 { "save", 1, cert_cmd
, XT_SAVE
, 0 },
3062 { "show", 1, cert_cmd
, XT_SHOW
, 0 },
3064 { "ca", 0, ca_cmd
, 0, 0 },
3065 { "downloadmgr", 0, xtp_page_dl
, 0, 0 },
3066 { "dl", 0, xtp_page_dl
, 0, 0 },
3067 { "h", 0, xtp_page_hl
, 0, 0 },
3068 { "history", 0, xtp_page_hl
, 0, 0 },
3069 { "home", 0, go_home
, 0, 0 },
3070 { "restart", 0, restart
, 0, 0 },
3071 { "urlhide", 0, urlaction
, XT_URL_HIDE
, 0 },
3072 { "urlshow", 0, urlaction
, XT_URL_SHOW
, 0 },
3073 { "statustoggle", 0, statustoggle
, 0, 0 },
3074 { "run_script", 0, run_page_script
, 0, XT_USERARG
},
3076 { "print", 0, print_page
, 0, 0 },
3079 { "focusin", 0, resizetab
, XT_ZOOM_IN
, 0 },
3080 { "focusout", 0, resizetab
, XT_ZOOM_OUT
, 0 },
3081 { "focusreset", 0, resizetab
, XT_ZOOM_NORMAL
, 0 },
3082 { "q", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3083 { "quit", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3084 { "open", 0, tabaction
, XT_TAB_OPEN
, XT_URLARG
},
3085 { "tabclose", 0, tabaction
, XT_TAB_DELETE
, XT_PREFIX
| XT_INTARG
},
3086 { "tabedit", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3087 { "tabfirst", 0, movetab
, XT_TAB_FIRST
, 0 },
3088 { "tabhide", 0, tabaction
, XT_TAB_HIDE
, 0 },
3089 { "tablast", 0, movetab
, XT_TAB_LAST
, 0 },
3090 { "tabnew", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3091 { "tabnext", 0, movetab
, XT_TAB_NEXT
, XT_PREFIX
| XT_INTARG
},
3092 { "tabnextstyle", 0, tabaction
, XT_TAB_NEXTSTYLE
, 0 },
3093 { "tabprevious", 0, movetab
, XT_TAB_PREV
, XT_PREFIX
| XT_INTARG
},
3094 { "tabrewind", 0, movetab
, XT_TAB_FIRST
, 0 },
3095 { "tabshow", 0, tabaction
, XT_TAB_SHOW
, 0 },
3096 { "tabs", 0, buffers
, 0, 0 },
3097 { "tabundoclose", 0, tabaction
, XT_TAB_UNDO_CLOSE
, 0 },
3098 { "buffers", 0, buffers
, 0, 0 },
3099 { "ls", 0, buffers
, 0, 0 },
3100 { "encoding", 0, set_encoding
, 0, XT_USERARG
},
3101 { "loadimages", 0, tabaction
, XT_TAB_LOAD_IMAGES
, 0 },
3103 /* command aliases (handy when -S flag is used) */
3104 { "promptopen", 0, command
, XT_CMD_OPEN
, 0 },
3105 { "promptopencurrent", 0, command
, XT_CMD_OPEN_CURRENT
, 0 },
3106 { "prompttabnew", 0, command
, XT_CMD_TABNEW
, 0 },
3107 { "prompttabnewcurrent",0, command
, XT_CMD_TABNEW_CURRENT
, 0 },
3110 { "set", 0, set
, 0, XT_SETARG
},
3112 { "fullscreen", 0, fullscreen
, 0, 0 },
3113 { "f", 0, fullscreen
, 0, 0 },
3116 { "session", 0, session_cmd
, XT_SHOW
, 0 },
3117 { "delete", 1, session_cmd
, XT_DELETE
, XT_SESSARG
},
3118 { "open", 1, session_cmd
, XT_OPEN
, XT_SESSARG
},
3119 { "save", 1, session_cmd
, XT_SAVE
, XT_USERARG
},
3120 { "show", 1, session_cmd
, XT_SHOW
, 0 },
3122 /* external javascript */
3123 { "script", 0, script_cmd
, XT_EJS_SHOW
, XT_USERARG
},
3126 { "inspector", 0, inspector_cmd
, XT_INS_SHOW
, 0 },
3127 { "show", 1, inspector_cmd
, XT_INS_SHOW
, 0 },
3128 { "hide", 1, inspector_cmd
, XT_INS_HIDE
, 0 },
3131 { "proxy", 0, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3132 { "show", 1, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3133 { "toggle", 1, proxy_cmd
, XT_PRXY_TOGGLE
, 0 },
3140 } cmd_status
= {-1, 0};
3143 wv_release_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3146 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 1)
3153 wv_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3156 WebKitHitTestResult
*hit_test_result
;
3159 hit_test_result
= webkit_web_view_get_hit_test_result(t
->wv
, e
);
3160 g_object_get(hit_test_result
, "context", &context
, NULL
);
3165 if (context
& WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE
)
3166 t
->mode
= XT_MODE_INSERT
;
3168 t
->mode
= XT_MODE_COMMAND
;
3170 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3172 else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 8 /* btn 4 */) {
3178 } else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 9 /* btn 5 */) {
3180 a
.i
= XT_NAV_FORWARD
;
3190 tab_close_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3192 DNPRINTF(XT_D_TAB
, "tab_close_cb: tab %d\n", t
->tab_id
);
3194 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3203 activate_uri_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3205 const gchar
*uri
= gtk_entry_get_text(GTK_ENTRY(entry
));
3207 DNPRINTF(XT_D_URL
, "activate_uri_entry_cb: %s\n", uri
);
3210 show_oops(NULL
, "activate_uri_entry_cb invalid parameters");
3215 show_oops(t
, "activate_uri_entry_cb no uri");
3219 uri
+= strspn(uri
, "\t ");
3221 /* if xxxt:// treat specially */
3222 if (parse_xtp_url(t
, uri
))
3225 /* otherwise continue to load page normally */
3226 load_uri(t
, (gchar
*)uri
);
3231 activate_search_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3233 const gchar
*search
= gtk_entry_get_text(GTK_ENTRY(entry
));
3234 char *newuri
= NULL
;
3237 DNPRINTF(XT_D_URL
, "activate_search_entry_cb: %s\n", search
);
3240 show_oops(NULL
, "activate_search_entry_cb invalid parameters");
3244 if (search_string
== NULL
) {
3245 show_oops(t
, "no search_string");
3249 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
3251 enc_search
= soup_uri_encode(search
, XT_RESERVED_CHARS
);
3252 newuri
= g_strdup_printf(search_string
, enc_search
);
3256 webkit_web_view_load_uri(t
->wv
, newuri
);
3264 check_and_set_cookie(const gchar
*uri
, struct tab
*t
)
3266 struct domain
*d
= NULL
;
3269 if (uri
== NULL
|| t
== NULL
)
3272 if ((d
= wl_find_uri(uri
, &c_wl
)) == NULL
)
3277 DNPRINTF(XT_D_COOKIE
, "check_and_set_cookie: %s %s\n",
3278 es
? "enable" : "disable", uri
);
3280 g_object_set(G_OBJECT(t
->settings
),
3281 "enable-html5-local-storage", es
, (char *)NULL
);
3282 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3286 check_and_set_js(const gchar
*uri
, struct tab
*t
)
3288 struct domain
*d
= NULL
;
3291 if (uri
== NULL
|| t
== NULL
)
3294 if ((d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3299 DNPRINTF(XT_D_JS
, "check_and_set_js: %s %s\n",
3300 es
? "enable" : "disable", uri
);
3302 g_object_set(G_OBJECT(t
->settings
),
3303 "enable-scripts", es
, (char *)NULL
);
3304 g_object_set(G_OBJECT(t
->settings
),
3305 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
3306 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3308 button_set_stockid(t
->js_toggle
,
3309 es
? GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
);
3313 check_and_set_pl(const gchar
*uri
, struct tab
*t
)
3315 struct domain
*d
= NULL
;
3318 if (uri
== NULL
|| t
== NULL
)
3321 if ((d
= wl_find_uri(uri
, &pl_wl
)) == NULL
)
3326 DNPRINTF(XT_D_JS
, "check_and_set_pl: %s %s\n",
3327 es
? "enable" : "disable", uri
);
3329 g_object_set(G_OBJECT(t
->settings
),
3330 "enable-plugins", es
, (char *)NULL
);
3331 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3335 color_address_bar(gpointer p
)
3338 struct tab
*tt
, *t
= p
;
3339 gchar
*col_str
= XT_COLOR_WHITE
;
3340 const gchar
*uri
, *u
= NULL
, *error_str
= NULL
;
3343 gdk_threads_enter();
3345 DNPRINTF(XT_D_URL
, "%s:\n", __func__
);
3347 /* make sure t still exists */
3350 TAILQ_FOREACH(tt
, &tabs
, entry
)
3356 if ((uri
= get_uri(t
)) == NULL
)
3361 gdk_threads_leave();
3364 col_str
= XT_COLOR_YELLOW
;
3365 switch (load_compare_cert(u
, &error_str
)) {
3367 col_str
= XT_COLOR_BLUE
;
3370 col_str
= XT_COLOR_GREEN
;
3372 case CERT_UNTRUSTED
:
3373 col_str
= XT_COLOR_YELLOW
;
3376 col_str
= XT_COLOR_RED
;
3381 gdk_threads_enter();
3383 /* make sure t isn't deleted */
3384 TAILQ_FOREACH(tt
, &tabs
, entry
)
3391 /* test to see if the user navigated away and canceled the thread */
3392 if (t
->thread
!= g_thread_self())
3394 if ((uri
= get_uri(t
)) == NULL
) {
3398 if (strcmp(uri
, u
)) {
3399 /* make sure we are still the same url */
3405 gdk_color_parse(col_str
, &color
);
3406 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3408 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3409 statusbar_modify_attr(t
, col_str
, XT_COLOR_BLACK
);
3411 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3413 if (error_str
&& error_str
[0] != '\0')
3414 show_oops(t
, "%s", error_str
);
3419 /* t is invalid at this point */
3421 g_free((gpointer
)u
);
3423 gdk_threads_leave();
3428 show_ca_status(struct tab
*t
, const char *uri
)
3431 gchar
*col_str
= XT_COLOR_WHITE
;
3433 DNPRINTF(XT_D_URL
, "show_ca_status: %d %s %s\n",
3434 ssl_strict_certs
, ssl_ca_file
, uri
);
3441 if (ssl_ca_file
== NULL
) {
3442 if (g_str_has_prefix(uri
, "http://"))
3444 if (g_str_has_prefix(uri
, "https://")) {
3445 col_str
= XT_COLOR_RED
;
3450 if (g_str_has_prefix(uri
, "http://") ||
3451 !g_str_has_prefix(uri
, "https://"))
3455 * It is not necessary to see if the thread is already running.
3456 * If the thread is in progress setting it to something else aborts it
3460 /* thread the coloring of the address bar */
3461 t
->thread
= g_thread_create((GThreadFunc
)color_address_bar
, t
, TRUE
, NULL
);
3463 color_address_bar(t
);
3469 gdk_color_parse(col_str
, &color
);
3470 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3472 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3473 statusbar_modify_attr(t
, col_str
, XT_COLOR_BLACK
);
3475 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3480 free_favicon(struct tab
*t
)
3482 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p req %p\n",
3483 __func__
, t
->icon_download
, t
->icon_request
);
3485 if (t
->icon_request
)
3486 g_object_unref(t
->icon_request
);
3487 if (t
->icon_dest_uri
)
3488 g_free(t
->icon_dest_uri
);
3490 t
->icon_request
= NULL
;
3491 t
->icon_dest_uri
= NULL
;
3495 xt_icon_from_name(struct tab
*t
, gchar
*name
)
3497 if (!enable_favicon_entry
)
3500 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->uri_entry
),
3501 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3503 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3504 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3506 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3507 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3511 xt_icon_from_pixbuf(struct tab
*t
, GdkPixbuf
*pb
)
3513 GdkPixbuf
*pb_scaled
;
3515 if (gdk_pixbuf_get_width(pb
) > 16 || gdk_pixbuf_get_height(pb
) > 16)
3516 pb_scaled
= gdk_pixbuf_scale_simple(pb
, 16, 16,
3517 GDK_INTERP_BILINEAR
);
3521 if (enable_favicon_entry
) {
3524 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
3525 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3528 if (show_url
== 0) {
3529 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
3530 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3532 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3533 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3536 /* XXX: Only supports the minimal tabs atm. */
3537 if (enable_favicon_tabs
)
3538 gtk_image_set_from_pixbuf(GTK_IMAGE(t
->tab_elems
.favicon
),
3541 if (pb_scaled
!= pb
)
3542 g_object_unref(pb_scaled
);
3546 xt_icon_from_file(struct tab
*t
, char *file
)
3550 if (g_str_has_prefix(file
, "file://"))
3551 file
+= strlen("file://");
3553 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
3555 xt_icon_from_pixbuf(t
, pb
);
3558 xt_icon_from_name(t
, "text-html");
3562 is_valid_icon(char *file
)
3565 const char *mime_type
;
3569 gf
= g_file_new_for_path(file
);
3570 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
3572 mime_type
= g_file_info_get_content_type(fi
);
3573 valid
= g_strcmp0(mime_type
, "image/x-ico") == 0 ||
3574 g_strcmp0(mime_type
, "image/vnd.microsoft.icon") == 0 ||
3575 g_strcmp0(mime_type
, "image/png") == 0 ||
3576 g_strcmp0(mime_type
, "image/gif") == 0 ||
3577 g_strcmp0(mime_type
, "application/octet-stream") == 0;
3585 set_favicon_from_file(struct tab
*t
, char *file
)
3589 if (t
== NULL
|| file
== NULL
)
3592 if (g_str_has_prefix(file
, "file://"))
3593 file
+= strlen("file://");
3594 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading %s\n", __func__
, file
);
3596 if (!stat(file
, &sb
)) {
3597 if (sb
.st_size
== 0 || !is_valid_icon(file
)) {
3598 /* corrupt icon so trash it */
3599 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3602 /* no need to set icon to default here */
3606 xt_icon_from_file(t
, file
);
3610 favicon_download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
3613 WebKitDownloadStatus status
= webkit_download_get_status(download
);
3614 struct tab
*tt
= NULL
, *t
= NULL
;
3617 * find the webview instead of passing in the tab as it could have been
3618 * deleted from underneath us.
3620 TAILQ_FOREACH(tt
, &tabs
, entry
) {
3629 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d status %d\n",
3630 __func__
, t
->tab_id
, status
);
3633 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
3635 t
->icon_download
= NULL
;
3638 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
3641 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
3644 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
3646 DNPRINTF(XT_D_DOWNLOAD
, "%s: freeing favicon %d\n",
3647 __func__
, t
->tab_id
);
3648 t
->icon_download
= NULL
;
3651 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
3654 DNPRINTF(XT_D_DOWNLOAD
, "%s: setting icon to %s\n",
3655 __func__
, t
->icon_dest_uri
);
3656 set_favicon_from_file(t
, t
->icon_dest_uri
);
3657 /* these will be freed post callback */
3658 t
->icon_request
= NULL
;
3659 t
->icon_download
= NULL
;
3667 abort_favicon_download(struct tab
*t
)
3669 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p\n", __func__
, t
->icon_download
);
3671 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
3672 if (t
->icon_download
) {
3673 g_signal_handlers_disconnect_by_func(G_OBJECT(t
->icon_download
),
3674 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3675 webkit_download_cancel(t
->icon_download
);
3676 t
->icon_download
= NULL
;
3681 xt_icon_from_name(t
, "text-html");
3685 notify_icon_loaded_cb(WebKitWebView
*wv
, gchar
*uri
, struct tab
*t
)
3687 DNPRINTF(XT_D_DOWNLOAD
, "%s %s\n", __func__
, uri
);
3689 if (uri
== NULL
|| t
== NULL
)
3692 #if WEBKIT_CHECK_VERSION(1, 4, 0)
3693 /* take icon from WebKitIconDatabase */
3696 pb
= webkit_web_view_get_icon_pixbuf(wv
);
3698 xt_icon_from_pixbuf(t
, pb
);
3701 xt_icon_from_name(t
, "text-html");
3702 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
3703 /* download icon to cache dir */
3704 gchar
*name_hash
, file
[PATH_MAX
];
3707 if (t
->icon_request
) {
3708 DNPRINTF(XT_D_DOWNLOAD
, "%s: download in progress\n", __func__
);
3712 /* check to see if we got the icon in cache */
3713 name_hash
= g_compute_checksum_for_string(G_CHECKSUM_SHA256
, uri
, -1);
3714 snprintf(file
, sizeof file
, "%s/%s.ico", cache_dir
, name_hash
);
3717 if (!stat(file
, &sb
)) {
3718 if (sb
.st_size
> 0) {
3719 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading from cache %s\n",
3721 set_favicon_from_file(t
, file
);
3725 /* corrupt icon so trash it */
3726 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3731 /* create download for icon */
3732 t
->icon_request
= webkit_network_request_new(uri
);
3733 if (t
->icon_request
== NULL
) {
3734 DNPRINTF(XT_D_DOWNLOAD
, "%s: invalid uri %s\n",
3739 t
->icon_download
= webkit_download_new(t
->icon_request
);
3740 if (t
->icon_download
== NULL
)
3743 /* we have to free icon_dest_uri later */
3744 t
->icon_dest_uri
= g_strdup_printf("file://%s", file
);
3745 webkit_download_set_destination_uri(t
->icon_download
,
3748 if (webkit_download_get_status(t
->icon_download
) ==
3749 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
3750 g_object_unref(t
->icon_request
);
3751 g_free(t
->icon_dest_uri
);
3752 t
->icon_request
= NULL
;
3753 t
->icon_dest_uri
= NULL
;
3757 g_signal_connect(G_OBJECT(t
->icon_download
), "notify::status",
3758 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3760 webkit_download_start(t
->icon_download
);
3765 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3767 const gchar
*uri
= NULL
;
3768 struct history
*h
, find
;
3772 DNPRINTF(XT_D_URL
, "notify_load_status_cb: %d %s\n",
3773 webkit_web_view_get_load_status(wview
),
3774 get_uri(t
) ? get_uri(t
) : "NOTHING");
3777 show_oops(NULL
, "notify_load_status_cb invalid parameters");
3781 switch (webkit_web_view_get_load_status(wview
)) {
3782 case WEBKIT_LOAD_PROVISIONAL
:
3784 abort_favicon_download(t
);
3785 #if GTK_CHECK_VERSION(2, 20, 0)
3786 gtk_widget_show(t
->spinner
);
3787 gtk_spinner_start(GTK_SPINNER(t
->spinner
));
3789 gtk_label_set_text(GTK_LABEL(t
->label
), "Loading");
3791 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), TRUE
);
3793 /* assume we are a new address */
3794 gdk_color_parse("white", &color
);
3795 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3796 statusbar_modify_attr(t
, "white", XT_COLOR_BLACK
);
3798 /* take focus if we are visible */
3804 /* kill color thread */
3809 case WEBKIT_LOAD_COMMITTED
:
3814 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
3820 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
3822 /* check if js white listing is enabled */
3823 if (enable_plugin_whitelist
)
3824 check_and_set_pl(uri
, t
);
3825 if (enable_cookie_whitelist
)
3826 check_and_set_cookie(uri
, t
);
3827 if (enable_js_whitelist
)
3828 check_and_set_js(uri
, t
);
3834 /* we know enough to autosave the session */
3835 if (session_autosave
) {
3840 show_ca_status(t
, uri
);
3841 run_script(t
, JS_HINTING
);
3842 if (enable_autoscroll
)
3843 run_script(t
, JS_AUTOSCROLL
);
3846 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
3848 if (color_visited_uris
) {
3849 color_visited(t
, color_visited_helper());
3851 /* This colors the links you middle-click (open in new
3852 * tab) in the current tab. */
3853 if (t
->tab_id
!= gtk_notebook_get_current_page(notebook
) &&
3854 (uri
= get_uri(t
)) != NULL
)
3855 color_visited(get_current_tab(),
3856 g_strdup_printf("{'%s' : 'dummy'}", uri
));
3860 case WEBKIT_LOAD_FINISHED
:
3862 if ((uri
= get_uri(t
)) == NULL
)
3865 if (!strncmp(uri
, "http://", strlen("http://")) ||
3866 !strncmp(uri
, "https://", strlen("https://")) ||
3867 !strncmp(uri
, "file://", strlen("file://"))) {
3868 find
.uri
= (gchar
*)uri
;
3869 h
= RB_FIND(history_list
, &hl
, &find
);
3871 insert_history_item(uri
,
3872 get_title(t
, FALSE
), time(NULL
));
3874 h
->time
= time(NULL
);
3877 set_status(t
, (char *)uri
, XT_STATUS_URI
);
3878 #if WEBKIT_CHECK_VERSION(1, 1, 18)
3879 case WEBKIT_LOAD_FAILED
:
3882 #if GTK_CHECK_VERSION(2, 20, 0)
3883 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
3884 gtk_widget_hide(t
->spinner
);
3887 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
3892 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
), TRUE
);
3894 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
),
3895 can_go_back_for_real(t
));
3897 gtk_widget_set_sensitive(GTK_WIDGET(t
->forward
),
3898 can_go_forward_for_real(t
));
3902 notify_title_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3904 const gchar
*title
= NULL
, *win_title
= NULL
;
3906 title
= get_title(t
, FALSE
);
3907 win_title
= get_title(t
, TRUE
);
3909 gtk_label_set_text(GTK_LABEL(t
->label
), title
);
3910 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
), title
);
3913 if (win_title
&& t
->tab_id
== gtk_notebook_get_current_page(notebook
))
3914 gtk_window_set_title(GTK_WINDOW(main_window
), win_title
);
3918 get_domain(const gchar
*host
)
3923 /* handle silly domains like .co.uk */
3925 if ((x
= strlen(host
)) <= 6)
3926 return (g_strdup(host
));
3928 if (host
[x
- 3] == '.' && host
[x
- 6] == '.') {
3934 return (g_strdup(&host
[x
+ 1]));
3938 p
= g_strrstr(host
, ".");
3940 return (g_strdup(""));
3946 return (g_strdup(p
+ 1));
3948 return (g_strdup(host
));
3952 js_autorun(struct tab
*t
)
3956 size_t got_default
= 0, got_host
= 0;
3958 char deff
[PATH_MAX
], hostf
[PATH_MAX
];
3959 char *js
= NULL
, *jsat
, *domain
= NULL
;
3960 FILE *deffile
= NULL
, *hostfile
= NULL
;
3962 if (js_autorun_enabled
== 0)
3967 !(g_str_has_prefix(uri
, "http://") ||
3968 g_str_has_prefix(uri
, "https://")))
3971 su
= soup_uri_new(uri
);
3974 if (!SOUP_URI_VALID_FOR_HTTP(su
))
3977 DNPRINTF(XT_D_JS
, "%s: host: %s domain: %s\n", __func__
,
3979 domain
= get_domain(su
->host
);
3981 snprintf(deff
, sizeof deff
, "%s/default.js", js_dir
);
3982 if ((deffile
= fopen(deff
, "r")) != NULL
) {
3983 if (fstat(fileno(deffile
), &sb
) == -1) {
3984 show_oops(t
, "can't stat default JS file");
3987 got_default
= sb
.st_size
;
3990 /* try host first followed by domain */
3991 snprintf(hostf
, sizeof hostf
, "%s/%s.js", js_dir
, su
->host
);
3992 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
3993 if ((hostfile
= fopen(hostf
, "r")) == NULL
) {
3994 snprintf(hostf
, sizeof hostf
, "%s/%s.js", js_dir
, domain
);
3995 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
3996 if ((hostfile
= fopen(hostf
, "r")) == NULL
)
3999 DNPRINTF(XT_D_JS
, "file: %s\n", hostf
);
4000 if (fstat(fileno(hostfile
), &sb
) == -1) {
4001 show_oops(t
, "can't stat %s JS file", hostf
);
4004 got_host
= sb
.st_size
;
4007 if (got_default
+ got_host
== 0)
4010 js
= g_malloc0(got_default
+ got_host
+ 1);
4014 if (fread(js
, got_default
, 1, deffile
) != 1) {
4015 show_oops(t
, "default file read error");
4018 jsat
= js
+ got_default
;
4022 if (fread(jsat
, got_host
, 1, hostfile
) != 1) {
4023 show_oops(t
, "host file read error");
4028 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
4045 webview_load_finished_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4047 /* autorun some js if enabled */
4055 webview_progress_changed_cb(WebKitWebView
*wv
, int progress
, struct tab
*t
)
4057 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->uri_entry
),
4058 progress
== 100 ? 0 : (double)progress
/ 100);
4059 if (show_url
== 0) {
4060 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
4061 progress
== 100 ? 0 : (double)progress
/ 100);
4064 update_statusbar_position(NULL
, NULL
);
4068 session_rq_cb(SoupSession
*s
, SoupMessage
*msg
, SoupSocket
*socket
, gpointer data
)
4073 if (s
== NULL
|| msg
== NULL
)
4076 if (referer_mode
== XT_REFERER_ALWAYS
)
4079 /* Check if referer is set - and what the user requested for referers */
4080 ref
= soup_message_headers_get_one(msg
->request_headers
, "Referer");
4082 DNPRINTF(XT_D_NAV
, "session_rq_cb: Referer: %s\n", ref
);
4083 switch (referer_mode
) {
4084 case XT_REFERER_NEVER
:
4085 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing referer\n");
4086 soup_message_headers_remove(msg
->request_headers
,
4089 case XT_REFERER_SAME_DOMAIN
:
4090 dest
= soup_message_get_uri(msg
);
4092 if (dest
&& !strstr(ref
, dest
->host
)) {
4093 soup_message_headers_remove(msg
->request_headers
, "Referer");
4094 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing referer (not same domain)\n");
4097 case XT_REFERER_CUSTOM
:
4098 DNPRINTF(XT_D_NAV
, "session_rq_cb: setting referer to %s\n", referer_custom
);
4099 soup_message_headers_replace(msg
->request_headers
,
4100 "Referer", referer_custom
);
4107 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
4108 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
4109 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
4112 WebKitWebNavigationReason reason
;
4113 struct domain
*d
= NULL
;
4116 show_oops(NULL
, "webview_npd_cb invalid parameters");
4120 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
4122 webkit_network_request_get_uri(request
));
4124 uri
= (char *)webkit_network_request_get_uri(request
);
4126 if (!auto_load_images
&& t
->load_images
) {
4128 /* Disable autoloading of images, now that we're done loading
4130 g_object_set(G_OBJECT(t
->settings
),
4131 "auto-load-images", FALSE
, (char *)NULL
);
4132 webkit_web_view_set_settings(t
->wv
, t
->settings
);
4134 t
->load_images
= FALSE
;
4137 /* If this is an xtp url, we don't load anything else. */
4138 if (parse_xtp_url(t
, uri
))
4141 if ((t
->mode
== XT_MODE_HINT
&& t
->new_tab
) || t
->ctrl_click
) {
4143 create_new_tab(uri
, NULL
, ctrl_click_focus
, -1);
4144 webkit_web_policy_decision_ignore(pd
);
4145 return (TRUE
); /* we made the decission */
4148 /* Change user agent if more than one has been given. */
4149 if (user_agent_count
> 1) {
4150 struct user_agent
*ua
;
4152 if ((ua
= TAILQ_NEXT(user_agent
, entry
)) == NULL
)
4153 user_agent
= TAILQ_FIRST(&ua_list
);
4157 free(t
->user_agent
);
4158 t
->user_agent
= g_strdup(user_agent
->value
);
4160 DNPRINTF(XT_D_NAV
, "user-agent: %s\n", t
->user_agent
);
4162 g_object_set(G_OBJECT(t
->settings
),
4163 "user-agent", t
->user_agent
, (char *)NULL
);
4165 webkit_web_view_set_settings(wv
, t
->settings
);
4169 * This is a little hairy but it comes down to this:
4170 * when we run in whitelist mode we have to assist the browser in
4171 * opening the URL that it would have opened in a new tab.
4173 reason
= webkit_web_navigation_action_get_reason(na
);
4174 if (reason
== WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED
) {
4175 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
4176 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1)
4177 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4179 webkit_web_policy_decision_use(pd
);
4180 return (TRUE
); /* we made the decision */
4187 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4190 struct domain
*d
= NULL
;
4192 WebKitWebView
*webview
= NULL
;
4195 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
4196 webkit_web_view_get_uri(wv
));
4199 /* open in current tab */
4201 } else if (enable_scripts
== 0 && enable_js_whitelist
== 1) {
4202 uri
= webkit_web_view_get_uri(wv
);
4203 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4206 if (t
->ctrl_click
) {
4207 x
= ctrl_click_focus
;
4210 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4212 } else if (enable_scripts
== 1) {
4213 if (t
->ctrl_click
) {
4214 x
= ctrl_click_focus
;
4217 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4225 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
4228 struct domain
*d
= NULL
;
4230 DNPRINTF(XT_D_NAV
, "webview_close_cb: %d\n", t
->tab_id
);
4232 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
4233 uri
= webkit_web_view_get_uri(wv
);
4234 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4238 } else if (enable_scripts
== 1)
4245 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
4247 /* we can not eat the event without throwing gtk off so defer it */
4249 /* catch middle click */
4250 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 2) {
4255 /* catch ctrl click */
4256 if (e
->type
== GDK_BUTTON_RELEASE
&&
4257 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
4262 return (XT_CB_PASSTHROUGH
);
4266 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
4268 struct mime_type
*m
;
4270 m
= find_mime_type(mime_type
);
4278 show_oops(t
, "can't fork mime handler");
4288 execlp(m
->mt_action
, m
->mt_action
,
4289 webkit_network_request_get_uri(request
), (void *)NULL
);
4298 get_mime_type(const char *file
)
4301 char *mime_type
= NULL
;
4305 if (g_str_has_prefix(file
, "file://"))
4306 file
+= strlen("file://");
4308 gf
= g_file_new_for_path(file
);
4309 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
4311 if ((m
= g_file_info_get_content_type(fi
)) != NULL
)
4312 mime_type
= g_strdup(m
);
4320 run_download_mimehandler(char *mime_type
, char *file
)
4322 struct mime_type
*m
;
4324 m
= find_mime_type(mime_type
);
4330 show_oops(NULL
, "can't fork download mime handler");
4340 if (g_str_has_prefix(file
, "file://"))
4341 file
+= strlen("file://");
4342 execlp(m
->mt_action
, m
->mt_action
, file
, (void *)NULL
);
4351 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
4354 WebKitDownloadStatus status
;
4355 const char *file
= NULL
;
4358 if (download
== NULL
)
4360 status
= webkit_download_get_status(download
);
4361 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
4364 file
= webkit_download_get_destination_uri(download
);
4367 mime
= get_mime_type(file
);
4371 run_download_mimehandler((char *)mime
, (char *)file
);
4376 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
4377 WebKitNetworkRequest
*request
, char *mime_type
,
4378 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
4381 show_oops(NULL
, "webview_mimetype_cb invalid parameters");
4385 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
4386 t
->tab_id
, mime_type
);
4388 if (run_mimehandler(t
, mime_type
, request
) == 0) {
4389 webkit_web_policy_decision_ignore(decision
);
4394 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
4395 webkit_web_policy_decision_download(decision
);
4403 download_start(struct tab
*t
, struct download
*d
, int flag
)
4405 WebKitNetworkRequest
*req
;
4407 const gchar
*suggested_name
;
4408 gchar
*filename
= NULL
;
4413 if (d
== NULL
|| t
== NULL
) {
4414 show_oops(NULL
, "%s invalid parameters", __func__
);
4418 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
4419 if (suggested_name
== NULL
)
4420 return (FALSE
); /* abort download */
4431 filename
= g_strdup_printf("%d%s", i
, suggested_name
);
4433 uri
= g_strdup_printf("file://%s/%s", download_dir
, i
?
4434 filename
: suggested_name
);
4436 } while (!stat(uri
+ strlen("file://"), &sb
));
4438 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d filename %s "
4439 "local %s\n", __func__
, t
->tab_id
, filename
, uri
);
4441 /* if we're restarting the download, or starting
4442 * it after doing something else, we need to recreate
4443 * the download request.
4445 if (flag
== XT_DL_RESTART
) {
4446 req
= webkit_network_request_new(webkit_download_get_uri(d
->download
));
4447 webkit_download_cancel(d
->download
);
4448 g_object_unref(d
->download
);
4449 d
->download
= webkit_download_new(req
);
4452 webkit_download_set_destination_uri(d
->download
, uri
);
4454 if (webkit_download_get_status(d
->download
) ==
4455 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
4456 show_oops(t
, "%s: download failed to start", __func__
);
4458 gtk_label_set_text(GTK_LABEL(t
->label
), "Download Failed");
4460 /* connect "download first" mime handler */
4461 g_signal_connect(G_OBJECT(d
->download
), "notify::status",
4462 G_CALLBACK(download_status_changed_cb
), NULL
);
4464 /* get from history */
4465 g_object_ref(d
->download
);
4466 gtk_label_set_text(GTK_LABEL(t
->label
), "Downloading");
4467 show_oops(t
, "Download of '%s' started...",
4468 basename((char *)webkit_download_get_destination_uri(d
->download
)));
4471 if (flag
!= XT_DL_START
)
4472 webkit_download_start(d
->download
);
4474 DNPRINTF(XT_D_DOWNLOAD
, "download status : %d",
4475 webkit_download_get_status(d
->download
));
4477 /* sync other download manager tabs */
4478 update_download_tabs(NULL
);
4490 download_ask_cb(struct tab
*t
, GdkEventKey
*e
, gpointer data
)
4492 struct download
*d
= data
;
4496 t
->mode_cb_data
= NULL
;
4499 e
->keyval
= GDK_Escape
;
4500 return (XT_CB_PASSTHROUGH
);
4503 DPRINTF("download_ask_cb: User pressed %c\n", e
->keyval
);
4504 if (e
->keyval
== 'y' || e
->keyval
== 'Y' || e
->keyval
== GDK_Return
)
4505 /* We need to do a RESTART, because we're not calling from
4506 * webview_download_cb
4508 download_start(t
, d
, XT_DL_RESTART
);
4510 /* for all other keyvals, we just let the download be */
4511 e
->keyval
= GDK_Escape
;
4512 return (XT_CB_HANDLED
);
4516 download_ask(struct tab
*t
, struct download
*d
)
4518 const gchar
*suggested_name
;
4520 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
4521 if (suggested_name
== NULL
)
4522 return (FALSE
); /* abort download */
4524 show_oops(t
, "download file %s [y/n] ?", suggested_name
);
4525 t
->mode_cb
= download_ask_cb
;
4526 t
->mode_cb_data
= d
;
4532 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
4535 const gchar
*suggested_name
;
4536 struct download
*download_entry
;
4539 if (wk_download
== NULL
|| t
== NULL
) {
4540 show_oops(NULL
, "%s invalid parameters", __func__
);
4544 suggested_name
= webkit_download_get_suggested_filename(wk_download
);
4545 if (suggested_name
== NULL
)
4546 return (FALSE
); /* abort download */
4548 download_entry
= g_malloc(sizeof(struct download
));
4549 download_entry
->download
= wk_download
;
4550 download_entry
->tab
= t
;
4551 download_entry
->id
= next_download_id
++;
4552 RB_INSERT(download_list
, &downloads
, download_entry
);
4554 if (download_mode
== XT_DM_START
)
4555 ret
= download_start(t
, download_entry
, XT_DL_START
);
4556 else if (download_mode
== XT_DM_ASK
)
4557 ret
= download_ask(t
, download_entry
);
4558 else if (download_mode
== XT_DM_ADD
)
4559 show_oops(t
, "added %s to download manager",
4562 /* sync other download manager tabs */
4563 update_download_tabs(NULL
);
4566 * NOTE: never redirect/render the current tab before this
4567 * function returns. This will cause the download to never start.
4569 return (ret
); /* start download */
4573 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
4575 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
4578 show_oops(NULL
, "webview_hover_cb");
4583 set_status(t
, uri
, XT_STATUS_LINK
);
4586 set_status(t
, t
->status
, XT_STATUS_NOTHING
);
4591 mark(struct tab
*t
, struct karg
*arg
)
4597 if ((index
= marktoindex(mark
)) == -1)
4600 if (arg
->i
== XT_MARK_SET
)
4601 t
->mark
[index
] = gtk_adjustment_get_value(t
->adjust_v
);
4602 else if (arg
->i
== XT_MARK_GOTO
) {
4603 if (t
->mark
[index
] == XT_INVALID_MARK
) {
4604 show_oops(t
, "mark '%c' does not exist", mark
);
4607 /* XXX t->mark[index] can be bigger than the maximum if ajax or
4608 something changes the document size */
4609 gtk_adjustment_set_value(t
->adjust_v
, t
->mark
[index
]);
4616 marks_clear(struct tab
*t
)
4620 for (i
= 0; i
< LENGTH(t
->mark
); i
++)
4621 t
->mark
[i
] = XT_INVALID_MARK
;
4627 char file
[PATH_MAX
];
4628 char *line
= NULL
, *p
;
4633 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
4634 if ((f
= fopen(file
, "r+")) == NULL
) {
4635 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
4639 for (i
= 1; ; i
++) {
4640 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
4642 if (strlen(line
) == 0 || line
[0] == '#') {
4648 p
= strtok(line
, " \t");
4650 if (p
== NULL
|| strlen(p
) != 1 ||
4651 (index
= marktoindex(*p
)) == -1) {
4652 warnx("corrupt quickmarks file, line %d", i
);
4656 p
= strtok(NULL
, " \t");
4657 if (qmarks
[index
] != NULL
)
4658 g_free(qmarks
[index
]);
4659 qmarks
[index
] = g_strdup(p
);
4670 char file
[PATH_MAX
];
4674 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
4675 if ((f
= fopen(file
, "r+")) == NULL
) {
4676 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
4680 for (i
= 0; i
< XT_NOMARKS
; i
++)
4681 if (qmarks
[i
] != NULL
)
4682 fprintf(f
, "%c %s\n", indextomark(i
), qmarks
[i
]);
4690 qmark(struct tab
*t
, struct karg
*arg
)
4695 mark
= arg
->s
[strlen(arg
->s
)-1];
4696 index
= marktoindex(mark
);
4702 if (qmarks
[index
] != NULL
) {
4703 g_free(qmarks
[index
]);
4704 qmarks
[index
] = NULL
;
4707 qmarks_load(); /* sync if multiple instances */
4708 qmarks
[index
] = g_strdup(get_uri(t
));
4712 if (qmarks
[index
] != NULL
)
4713 load_uri(t
, qmarks
[index
]);
4715 show_oops(t
, "quickmark \"%c\" does not exist",
4721 if (qmarks
[index
] != NULL
)
4722 create_new_tab(qmarks
[index
], NULL
, 1, -1);
4724 show_oops(t
, "quickmark \"%c\" does not exist",
4735 go_up(struct tab
*t
, struct karg
*args
)
4742 if (args
->i
== XT_GO_UP_ROOT
)
4743 levels
= XT_GO_UP_ROOT
;
4744 else if ((levels
= atoi(args
->s
)) == 0)
4747 uri
= g_strdup(get_uri(t
));
4749 if ((tmp
= strstr(uri
, XT_PROTO_DELIM
)) == NULL
)
4752 tmp
+= strlen(XT_PROTO_DELIM
);
4754 /* if an uri starts with a slash, leave it alone (for file:///) */
4759 p
= strrchr(tmp
, '/');
4773 gototab(struct tab
*t
, struct karg
*args
)
4776 struct karg arg
= {0, NULL
, -1};
4778 tab
= atoi(args
->s
);
4781 arg
.i
= XT_TAB_NEXT
;
4793 gotonexttab(struct tab
*t
, struct karg
*args
)
4795 int count
, n_tabs
, dest
;
4796 struct karg arg
= {0, NULL
, -1};
4798 count
= atoi(args
->s
);
4802 arg
.i
= XT_TAB_NEXT
;
4804 n_tabs
= gtk_notebook_get_n_pages(notebook
);
4805 dest
= gtk_notebook_get_current_page(notebook
);
4807 dest
+= (count
+ 1) % n_tabs
;
4810 arg
.precount
= dest
;
4812 DNPRINTF(XT_D_BUFFERCMD
, "gotonexttab: count: %d - dest : %d \n", count
, dest
);
4820 gotoprevtab(struct tab
*t
, struct karg
*args
)
4823 struct karg arg
= {0, NULL
, -1};
4825 count
= atoi(args
->s
);
4829 arg
.i
= XT_TAB_PREV
;
4830 arg
.precount
= count
;
4832 DNPRINTF(XT_D_BUFFERCMD
, "gotoprevtab: count: %d\n", count
);
4839 zoom_amount(struct tab
*t
, struct karg
*arg
)
4841 struct karg narg
= {0, NULL
, -1};
4843 narg
.i
= atoi(arg
->s
);
4844 resizetab(t
, &narg
);
4850 flip_colon(struct tab
*t
, struct karg
*arg
)
4852 struct karg narg
= {0, NULL
, -1};
4855 if (t
== NULL
|| arg
== NULL
)
4858 p
= strstr(arg
->s
, ":");
4870 /* buffer commands receive the regex that triggered them in arg.s */
4871 char bcmd
[XT_BUFCMD_SZ
];
4875 #define XT_PRE_NO (0)
4876 #define XT_PRE_YES (1)
4877 #define XT_PRE_MAYBE (2)
4879 int (*func
)(struct tab
*, struct karg
*);
4883 { "^[0-9]*gu$", XT_PRE_MAYBE
, "gu", go_up
, 0 },
4884 { "^gU$", XT_PRE_NO
, "gU", go_up
, XT_GO_UP_ROOT
},
4885 { "^gg$", XT_PRE_NO
, "gg", move
, XT_MOVE_TOP
},
4886 { "^gG$", XT_PRE_NO
, "gG", move
, XT_MOVE_BOTTOM
},
4887 { "^[0-9]+%$", XT_PRE_YES
, "%", move
, XT_MOVE_PERCENT
},
4888 { "^zz$", XT_PRE_NO
, "zz", move
, XT_MOVE_CENTER
},
4889 { "^gh$", XT_PRE_NO
, "gh", go_home
, 0 },
4890 { "^m[a-zA-Z0-9]$", XT_PRE_NO
, "m", mark
, XT_MARK_SET
},
4891 { "^['][a-zA-Z0-9]$", XT_PRE_NO
, "'", mark
, XT_MARK_GOTO
},
4892 { "^[0-9]+t$", XT_PRE_YES
, "t", gototab
, 0 },
4893 { "^g0$", XT_PRE_YES
, "g0", gototab
, XT_TAB_FIRST
},
4894 { "^g[$]$", XT_PRE_YES
, "g$", gototab
, XT_TAB_LAST
},
4895 { "^[0-9]*gt$", XT_PRE_YES
, "t", gotonexttab
, 0 },
4896 { "^[0-9]*gT$", XT_PRE_YES
, "T", gotoprevtab
, 0 },
4897 { "^M[a-zA-Z0-9]$", XT_PRE_NO
, "M", qmark
, XT_QMARK_SET
},
4898 { "^go[a-zA-Z0-9]$", XT_PRE_NO
, "go", qmark
, XT_QMARK_OPEN
},
4899 { "^gn[a-zA-Z0-9]$", XT_PRE_NO
, "gn", qmark
, XT_QMARK_TAB
},
4900 { "^ZR$", XT_PRE_NO
, "ZR", restart
, 0 },
4901 { "^ZZ$", XT_PRE_NO
, "ZZ", quit
, 0 },
4902 { "^zi$", XT_PRE_NO
, "zi", resizetab
, XT_ZOOM_IN
},
4903 { "^zo$", XT_PRE_NO
, "zo", resizetab
, XT_ZOOM_OUT
},
4904 { "^z0$", XT_PRE_NO
, "z0", resizetab
, XT_ZOOM_NORMAL
},
4905 { "^[0-9]+Z$", XT_PRE_YES
, "Z", zoom_amount
, 0 },
4906 { "^[0-9]+:$", XT_PRE_YES
, ":", flip_colon
, 0 },
4910 buffercmd_init(void)
4914 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
4915 if (regcomp(&buffercmds
[i
].cregex
, buffercmds
[i
].regex
,
4916 REG_EXTENDED
| REG_NOSUB
))
4917 startpage_add("invalid buffercmd regex %s",
4918 buffercmds
[i
].regex
);
4922 buffercmd_abort(struct tab
*t
)
4929 DNPRINTF(XT_D_BUFFERCMD
, "%s: clearing buffer\n", __func__
);
4931 for (i
= 0; i
< LENGTH(bcmd
); i
++)
4934 cmd_prefix
= 0; /* clear prefix for non-buffer commands */
4935 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
4939 buffercmd_execute(struct tab
*t
, struct buffercmd
*cmd
)
4941 struct karg arg
= {0, NULL
, -1};
4944 arg
.s
= g_strdup(bcmd
);
4946 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_execute: buffer \"%s\" "
4947 "matches regex \"%s\", executing\n", bcmd
, cmd
->regex
);
4957 buffercmd_addkey(struct tab
*t
, guint keyval
)
4960 char s
[XT_BUFCMD_SZ
];
4962 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
))) {
4964 return (XT_CB_PASSTHROUGH
);
4967 if (keyval
== GDK_Escape
) {
4969 return (XT_CB_HANDLED
);
4972 /* key with modifier or non-ascii character */
4973 if (!isascii(keyval
)) {
4975 * XXX this looks wrong but fixes some sites like
4976 * http://www.seslisozluk.com/
4977 * that eat a shift or ctrl and end putting default focus in js
4978 * instead of ignoring the keystroke
4979 * so instead of return (XT_CB_PASSTHROUGH); eat the key
4981 return (XT_CB_HANDLED
);
4984 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: adding key \"%c\" "
4985 "to buffer \"%s\"\n", keyval
, bcmd
);
4987 for (i
= 0; i
< LENGTH(bcmd
); i
++)
4988 if (bcmd
[i
] == '\0') {
4993 /* buffer full, ignore input */
4994 if (i
>= LENGTH(bcmd
) -1) {
4995 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: buffer full\n");
4997 return (XT_CB_HANDLED
);
5000 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
5002 /* find exact match */
5003 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5004 if (regexec(&buffercmds
[i
].cregex
, bcmd
,
5005 (size_t) 0, NULL
, 0) == 0) {
5006 buffercmd_execute(t
, &buffercmds
[i
]);
5010 /* find non exact matches to see if we need to abort ot not */
5011 for (i
= 0, match
= 0; i
< LENGTH(buffercmds
); i
++) {
5012 DNPRINTF(XT_D_BUFFERCMD
, "trying: %s\n", bcmd
);
5015 if (buffercmds
[i
].precount
== XT_PRE_MAYBE
) {
5016 if (isdigit(bcmd
[0])) {
5017 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5021 if (sscanf(bcmd
, "%s", s
) == 0)
5024 } else if (buffercmds
[i
].precount
== XT_PRE_YES
) {
5025 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5028 if (sscanf(bcmd
, "%s", s
) == 0)
5031 if (c
== -1 && buffercmds
[i
].precount
)
5033 if (!strncmp(s
, buffercmds
[i
].cmd
, strlen(s
)))
5036 DNPRINTF(XT_D_BUFFERCMD
, "got[%d] %d <%s>: %d %s\n",
5037 i
, match
, buffercmds
[i
].cmd
, c
, s
);
5040 DNPRINTF(XT_D_BUFFERCMD
, "aborting: %s\n", bcmd
);
5045 return (XT_CB_HANDLED
);
5049 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
5051 struct key_binding
*k
;
5053 /* handle keybindings if buffercmd is empty.
5054 if not empty, allow commands like C-n */
5055 if (bcmd
[0] == '\0' || ((e
->state
& (CTRL
| MOD1
)) != 0))
5056 TAILQ_FOREACH(k
, &kbl
, entry
)
5057 if (e
->keyval
== k
->key
5058 && (entry
? k
->use_in_entry
: 1)) {
5059 /* when we are edditing eat ctrl/mod keys */
5060 if (edit_mode
== XT_EM_VI
&&
5061 t
->mode
== XT_MODE_INSERT
&&
5062 (e
->state
& CTRL
|| e
->state
& MOD1
))
5063 return (XT_CB_PASSTHROUGH
);
5066 if ((e
->state
& (CTRL
| MOD1
)) == 0)
5067 return (cmd_execute(t
, k
->cmd
));
5068 } else if ((e
->state
& k
->mask
) == k
->mask
) {
5069 return (cmd_execute(t
, k
->cmd
));
5073 if (!entry
&& ((e
->state
& (CTRL
| MOD1
)) == 0))
5074 return buffercmd_addkey(t
, e
->keyval
);
5076 return (XT_CB_PASSTHROUGH
);
5080 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5084 /* don't use w directly; use t->whatever instead */
5087 show_oops(NULL
, "wv_keypress_cb");
5088 return (XT_CB_PASSTHROUGH
);
5094 return t
->mode_cb(t
, e
, t
->mode_cb_data
);
5096 DNPRINTF(XT_D_KEY
, "wv_keypress_cb: mode %d keyval 0x%x mask "
5097 "0x%x tab %d\n", t
->mode
, e
->keyval
, e
->state
, t
->tab_id
);
5099 /* Hide buffers, if they are visible, with escape. */
5100 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)) &&
5101 CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
5102 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5104 return (XT_CB_HANDLED
);
5107 if (t
->mode
== XT_MODE_HINT
)
5108 return (XT_CB_HANDLED
);
5110 if ((CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Tab
) ||
5111 (CLEAN(e
->state
) == SHFT
&& e
->keyval
== GDK_Tab
))
5112 /* something focussy is about to happen */
5113 return (XT_CB_PASSTHROUGH
);
5115 /* check if we are some sort of text input thing in the dom */
5116 input_check_mode(t
);
5118 if (t
->mode
== XT_MODE_HINT
) {
5119 /* XXX make sure cmd entry is enabled */
5120 return (XT_CB_HANDLED
);
5121 } else if (t
->mode
== XT_MODE_PASSTHROUGH
) {
5122 if (CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
)
5123 t
->mode
= XT_MODE_COMMAND
;
5124 return (XT_CB_PASSTHROUGH
);
5125 } else if (t
->mode
== XT_MODE_COMMAND
) {
5127 snprintf(s
, sizeof s
, "%c", e
->keyval
);
5128 if (CLEAN(e
->state
) == 0 && isdigit(s
[0]))
5129 cmd_prefix
= 10 * cmd_prefix
+ atoi(s
);
5130 return (handle_keypress(t
, e
, 0));
5133 return (handle_keypress(t
, e
, 1));
5137 return (XT_CB_PASSTHROUGH
);
5141 hint_continue(struct tab
*t
)
5143 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5145 const gchar
*errstr
= NULL
;
5149 if (!(c
[0] == '.' || c
[0] == ','))
5151 if (strlen(c
) == 1) {
5152 /* XXX should not happen */
5157 if (isdigit(c
[1])) {
5159 i
= strtonum(&c
[1], 1, 4096, &errstr
);
5161 show_oops(t
, "invalid numerical hint %s", &c
[1]);
5164 s
= g_strdup_printf("hints.updateHints(%d", i
);
5168 /* alphanumeric input */
5169 s
= g_strdup_printf("hints.createHints('%s', '%c');",
5170 &c
[1], c
[0] == '.' ? 'f' : 'F');
5181 search_continue(struct tab
*t
)
5183 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5184 gboolean rv
= FALSE
;
5186 if (c
[0] == ':' || c
[0] == '.' || c
[0] == ',')
5188 if (strlen(c
) == 1) {
5189 webkit_web_view_unmark_text_matches(t
->wv
);
5194 t
->search_forward
= TRUE
;
5195 else if (c
[0] == '?')
5196 t
->search_forward
= FALSE
;
5206 search_cb(struct tab
*t
)
5208 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5211 if (search_continue(t
) == FALSE
)
5215 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, t
->search_forward
,
5217 /* not found, mark red */
5218 gdk_color_parse(XT_COLOR_RED
, &color
);
5219 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
5220 /* unmark and remove selection */
5221 webkit_web_view_unmark_text_matches(t
->wv
);
5222 /* my kingdom for a way to unselect text in webview */
5224 /* found, highlight all */
5225 webkit_web_view_unmark_text_matches(t
->wv
);
5226 webkit_web_view_mark_text_matches(t
->wv
, &c
[1], FALSE
, 0);
5227 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
5228 gdk_color_parse(XT_COLOR_WHITE
, &color
);
5229 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
5237 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5239 const gchar
*c
= gtk_entry_get_text(w
);
5242 show_oops(NULL
, "cmd_keyrelease_cb invalid parameters");
5243 return (XT_CB_PASSTHROUGH
);
5246 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
5247 e
->keyval
, e
->state
, t
->tab_id
);
5250 if (!(e
->keyval
== GDK_Tab
|| e
->keyval
== GDK_ISO_Left_Tab
)) {
5251 if (hint_continue(t
) == FALSE
)
5256 if (search_continue(t
) == FALSE
)
5259 /* if search length is > 4 then no longer play timeout games */
5260 if (strlen(c
) > 4) {
5262 g_source_remove(t
->search_id
);
5269 /* reestablish a new timer if the user types fast */
5271 g_source_remove(t
->search_id
);
5272 t
->search_id
= g_timeout_add(250, (GSourceFunc
)search_cb
, (gpointer
)t
);
5275 return (XT_CB_PASSTHROUGH
);
5279 match_uri(const gchar
*uri
, const gchar
*key
) {
5282 gboolean match
= FALSE
;
5286 if (!strncmp(key
, uri
, len
))
5289 voffset
= strstr(uri
, "/") + 2;
5290 if (!strncmp(key
, voffset
, len
))
5292 else if (g_str_has_prefix(voffset
, "www.")) {
5293 voffset
= voffset
+ strlen("www.");
5294 if (!strncmp(key
, voffset
, len
))
5303 match_session(const gchar
*name
, const gchar
*key
) {
5306 sub
= strcasestr(name
, key
);
5312 cmd_getlist(int id
, char *key
)
5319 if (cmds
[id
].type
& XT_URLARG
) {
5320 RB_FOREACH_REVERSE(h
, history_list
, &hl
)
5321 if (match_uri(h
->uri
, key
)) {
5322 cmd_status
.list
[c
] = (char *)h
->uri
;
5328 } else if (cmds
[id
].type
& XT_SESSARG
) {
5329 TAILQ_FOREACH(s
, &sessions
, entry
)
5330 if (match_session(s
->name
, key
)) {
5331 cmd_status
.list
[c
] = (char *)s
->name
;
5337 } else if (cmds
[id
].type
& XT_SETARG
) {
5338 for (i
= 0; i
< get_settings_size(); i
++)
5339 if (!strncmp(key
, get_setting_name(i
),
5341 cmd_status
.list
[c
++] =
5342 get_setting_name(i
);
5348 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
5350 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
5351 if (cmds
[i
].level
< dep
)
5353 if (cmds
[i
].level
== dep
&& !strncmp(key
, cmds
[i
].cmd
,
5354 strlen(key
)) && !isdigit(cmds
[i
].cmd
[0]))
5355 cmd_status
.list
[c
++] = cmds
[i
].cmd
;
5363 cmd_getnext(int dir
)
5365 cmd_status
.index
+= dir
;
5367 if (cmd_status
.index
< 0)
5368 cmd_status
.index
= cmd_status
.len
- 1;
5369 else if (cmd_status
.index
>= cmd_status
.len
)
5370 cmd_status
.index
= 0;
5372 return cmd_status
.list
[cmd_status
.index
];
5376 cmd_tokenize(char *s
, char *tokens
[])
5379 char *tok
, *last
= NULL
;
5380 size_t len
= strlen(s
);
5383 blank
= len
== 0 || (len
> 0 && s
[len
- 1] == ' ');
5384 for (tok
= strtok_r(s
, " ", &last
); tok
&& i
< 3;
5385 tok
= strtok_r(NULL
, " ", &last
), i
++)
5395 cmd_complete(struct tab
*t
, char *str
, int dir
)
5397 GtkEntry
*w
= GTK_ENTRY(t
->cmd
);
5398 int i
, j
, levels
, c
= 0, dep
= 0, parent
= -1;
5400 char *tok
, *match
, *s
= g_strdup(str
);
5402 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
5405 DNPRINTF(XT_D_CMD
, "%s: complete %s\n", __func__
, str
);
5408 for (i
= 0; isdigit(s
[i
]); i
++)
5411 for (; isspace(s
[i
]); i
++)
5416 levels
= cmd_tokenize(s
, tokens
);
5418 for (i
= 0; i
< levels
- 1; i
++) {
5421 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5422 if (cmds
[j
].level
< dep
)
5424 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
,
5428 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
5435 if (matchcount
== 1) {
5436 strlcat(res
, tok
, sizeof res
);
5437 strlcat(res
, " ", sizeof res
);
5447 if (cmd_status
.index
== -1)
5448 cmd_getlist(parent
, tokens
[i
]);
5450 if (cmd_status
.len
> 0) {
5451 match
= cmd_getnext(dir
);
5452 strlcat(res
, match
, sizeof res
);
5453 gtk_entry_set_text(w
, res
);
5454 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5461 cmd_execute(struct tab
*t
, char *str
)
5463 struct cmd
*cmd
= NULL
;
5464 char *tok
, *last
= NULL
, *s
= g_strdup(str
), *sc
;
5466 int j
, len
, c
= 0, dep
= 0, matchcount
= 0;
5467 int prefix
= -1, rv
= XT_CB_PASSTHROUGH
;
5468 struct karg arg
= {0, NULL
, -1};
5473 for (j
= 0; j
<3 && isdigit(s
[j
]); j
++)
5479 while (isspace(s
[0]))
5482 if (strlen(s
) > 0 && strlen(prefixstr
) > 0)
5483 prefix
= atoi(prefixstr
);
5487 for (tok
= strtok_r(s
, " ", &last
); tok
;
5488 tok
= strtok_r(NULL
, " ", &last
)) {
5490 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5491 if (cmds
[j
].level
< dep
)
5493 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1 :
5495 if (cmds
[j
].level
== dep
&&
5496 !strncmp(tok
, cmds
[j
].cmd
, len
)) {
5500 if (len
== strlen(cmds
[j
].cmd
)) {
5506 if (matchcount
== 1) {
5511 show_oops(t
, "Invalid command: %s", str
);
5517 show_oops(t
, "Empty command");
5523 arg
.precount
= prefix
;
5524 else if (cmd_prefix
> 0)
5525 arg
.precount
= cmd_prefix
;
5527 if (j
> 0 && !(cmd
->type
& XT_PREFIX
) && arg
.precount
> -1) {
5528 show_oops(t
, "No prefix allowed: %s", str
);
5532 arg
.s
= last
? g_strdup(last
) : g_strdup("");
5533 if (cmd
->type
& XT_INTARG
&& last
&& strlen(last
) > 0) {
5534 if (arg
.s
== NULL
) {
5535 show_oops(t
, "Invalid command");
5538 arg
.precount
= atoi(arg
.s
);
5539 if (arg
.precount
<= 0) {
5540 if (arg
.s
[0] == '0')
5541 show_oops(t
, "Zero count");
5543 show_oops(t
, "Trailing characters");
5548 DNPRINTF(XT_D_CMD
, "%s: prefix %d arg %s\n",
5549 __func__
, arg
.precount
, arg
.s
);
5565 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5568 show_oops(NULL
, "entry_key_cb invalid parameters");
5569 return (XT_CB_PASSTHROUGH
);
5572 DNPRINTF(XT_D_CMD
, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
5573 e
->keyval
, e
->state
, t
->tab_id
);
5577 if (e
->keyval
== GDK_Escape
) {
5578 /* don't use focus_webview(t) because we want to type :cmds */
5579 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5582 return (handle_keypress(t
, e
, 1));
5585 struct command_entry
*
5586 history_prev(struct command_list
*l
, struct command_entry
*at
)
5589 at
= TAILQ_LAST(l
, command_list
);
5591 at
= TAILQ_PREV(at
, command_list
, entry
);
5593 at
= TAILQ_LAST(l
, command_list
);
5599 struct command_entry
*
5600 history_next(struct command_list
*l
, struct command_entry
*at
)
5603 at
= TAILQ_FIRST(l
);
5605 at
= TAILQ_NEXT(at
, entry
);
5607 at
= TAILQ_FIRST(l
);
5614 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5616 int rv
= XT_CB_HANDLED
;
5617 const gchar
*c
= gtk_entry_get_text(w
);
5621 show_oops(NULL
, "cmd_keypress_cb parameters");
5622 return (XT_CB_PASSTHROUGH
);
5625 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
5626 e
->keyval
, e
->state
, t
->tab_id
);
5630 e
->keyval
= GDK_Escape
;
5631 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5632 c
[0] == '.' || c
[0] == ','))
5633 e
->keyval
= GDK_Escape
;
5635 if (e
->keyval
!= GDK_Tab
&& e
->keyval
!= GDK_Shift_L
&&
5636 e
->keyval
!= GDK_ISO_Left_Tab
)
5637 cmd_status
.index
= -1;
5639 switch (e
->keyval
) {
5642 cmd_complete(t
, (char *)&c
[1], 1);
5643 else if (c
[0] == '.' || c
[0] == ',')
5644 run_script(t
, "hints.focusNextHint();");
5646 case GDK_ISO_Left_Tab
:
5648 cmd_complete(t
, (char *)&c
[1], -1);
5649 else if (c
[0] == '.' || c
[0] == ',')
5650 run_script(t
, "hints.focusPreviousHint();");
5654 if ((search_at
= history_next(&shl
, search_at
))) {
5655 search_at
->line
[0] = c
[0];
5656 gtk_entry_set_text(w
, search_at
->line
);
5657 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5659 } else if (c
[0] == '/') {
5660 if ((search_at
= history_prev(&chl
, search_at
))) {
5661 search_at
->line
[0] = c
[0];
5662 gtk_entry_set_text(w
, search_at
->line
);
5663 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5665 } if (c
[0] == ':') {
5666 if ((history_at
= history_prev(&chl
, history_at
))) {
5667 history_at
->line
[0] = c
[0];
5668 gtk_entry_set_text(w
, history_at
->line
);
5669 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5675 if ((search_at
= history_next(&shl
, search_at
))) {
5676 search_at
->line
[0] = c
[0];
5677 gtk_entry_set_text(w
, search_at
->line
);
5678 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5680 } else if (c
[0] == '?') {
5681 if ((search_at
= history_prev(&chl
, search_at
))) {
5682 search_at
->line
[0] = c
[0];
5683 gtk_entry_set_text(w
, search_at
->line
);
5684 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5686 } if (c
[0] == ':') {
5687 if ((history_at
= history_next(&chl
, history_at
))) {
5688 history_at
->line
[0] = c
[0];
5689 gtk_entry_set_text(w
, history_at
->line
);
5690 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5695 if (!(!strcmp(c
, ":") || !strcmp(c
, "/") || !strcmp(c
, "?") ||
5696 !strcmp(c
, ".") || !strcmp(c
, ","))) {
5697 /* see if we are doing hinting and reset it */
5698 if (c
[0] == '.' || c
[0] == ',') {
5699 /* recreate hints */
5700 s
= g_strdup_printf("hints.createHints('', "
5701 "'%c');", c
[0] == '.' ? 'f' : 'F');
5714 if (c
!= NULL
&& (c
[0] == '/' || c
[0] == '?'))
5715 webkit_web_view_unmark_text_matches(t
->wv
);
5717 /* no need to cancel hints */
5721 rv
= XT_CB_PASSTHROUGH
;
5727 wv_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
5729 DNPRINTF(XT_D_CMD
, "wv_popup_cb: tab %d\n", t
->tab_id
);
5733 cmd_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
5735 /* popup menu enabled */
5740 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
5743 show_oops(NULL
, "cmd_focusout_cb invalid parameters");
5744 return (XT_CB_PASSTHROUGH
);
5747 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d popup %d\n",
5748 t
->tab_id
, t
->popup
);
5750 /* if popup is enabled don't lose focus */
5753 return (XT_CB_PASSTHROUGH
);
5760 if (show_url
== 0 || t
->focus_wv
)
5763 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
5765 return (XT_CB_PASSTHROUGH
);
5769 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
5772 const gchar
*c
= gtk_entry_get_text(entry
);
5775 show_oops(NULL
, "cmd_activate_cb invalid parameters");
5779 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
5784 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5785 c
[0] == '.' || c
[0] == ','))
5791 if (c
[0] == '/' || c
[0] == '?') {
5792 /* see if there is a timer pending */
5794 g_source_remove(t
->search_id
);
5799 if (t
->search_text
) {
5800 g_free(t
->search_text
);
5801 t
->search_text
= NULL
;
5804 t
->search_text
= g_strdup(s
);
5806 g_free(global_search
);
5807 global_search
= g_strdup(s
);
5808 t
->search_forward
= c
[0] == '/';
5810 history_add(&shl
, search_file
, s
, &search_history_count
);
5811 } else if (c
[0] == '.' || c
[0] == ',') {
5812 run_script(t
, "hints.fire();");
5813 /* XXX history for link following? */
5814 } else if (c
[0] == ':') {
5815 history_add(&chl
, command_file
, s
, &cmd_history_count
);
5816 /* can't call hide_cmd after cmd_execute */
5827 backward_cb(GtkWidget
*w
, struct tab
*t
)
5832 show_oops(NULL
, "backward_cb invalid parameters");
5836 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
5843 forward_cb(GtkWidget
*w
, struct tab
*t
)
5848 show_oops(NULL
, "forward_cb invalid parameters");
5852 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
5854 a
.i
= XT_NAV_FORWARD
;
5859 home_cb(GtkWidget
*w
, struct tab
*t
)
5862 show_oops(NULL
, "home_cb invalid parameters");
5866 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
5872 stop_cb(GtkWidget
*w
, struct tab
*t
)
5874 WebKitWebFrame
*frame
;
5877 show_oops(NULL
, "stop_cb invalid parameters");
5881 DNPRINTF(XT_D_NAV
, "stop_cb: tab %d\n", t
->tab_id
);
5883 frame
= webkit_web_view_get_main_frame(t
->wv
);
5884 if (frame
== NULL
) {
5885 show_oops(t
, "stop_cb: no frame");
5889 webkit_web_frame_stop_loading(frame
);
5890 abort_favicon_download(t
);
5894 setup_webkit(struct tab
*t
)
5896 if (is_g_object_setting(G_OBJECT(t
->settings
), "enable-dns-prefetching"))
5897 g_object_set(G_OBJECT(t
->settings
), "enable-dns-prefetching",
5898 FALSE
, (char *)NULL
);
5900 warnx("webkit does not have \"enable-dns-prefetching\" property");
5901 g_object_set(G_OBJECT(t
->settings
),
5902 "user-agent", t
->user_agent
, (char *)NULL
);
5903 g_object_set(G_OBJECT(t
->settings
),
5904 "enable-scripts", enable_scripts
, (char *)NULL
);
5905 g_object_set(G_OBJECT(t
->settings
),
5906 "enable-plugins", enable_plugins
, (char *)NULL
);
5907 g_object_set(G_OBJECT(t
->settings
),
5908 "javascript-can-open-windows-automatically", enable_scripts
,
5910 g_object_set(G_OBJECT(t
->settings
),
5911 "enable-html5-database", FALSE
, (char *)NULL
);
5912 g_object_set(G_OBJECT(t
->settings
),
5913 "enable-html5-local-storage", enable_localstorage
, (char *)NULL
);
5914 g_object_set(G_OBJECT(t
->settings
),
5915 "enable_spell_checking", enable_spell_checking
, (char *)NULL
);
5916 g_object_set(G_OBJECT(t
->settings
),
5917 "spell_checking_languages", spell_check_languages
, (char *)NULL
);
5918 g_object_set(G_OBJECT(t
->settings
),
5919 "enable-developer-extras", TRUE
, (char *)NULL
);
5920 g_object_set(G_OBJECT(t
->wv
),
5921 "full-content-zoom", TRUE
, (char *)NULL
);
5922 g_object_set(G_OBJECT(t
->settings
),
5923 "auto-load-images", auto_load_images
, (char *)NULL
);
5925 webkit_web_view_set_settings(t
->wv
, t
->settings
);
5929 update_statusbar_position(GtkAdjustment
* adjustment
, gpointer data
)
5931 struct tab
*ti
, *t
= NULL
;
5932 gdouble view_size
, value
, max
;
5935 TAILQ_FOREACH(ti
, &tabs
, entry
)
5936 if (ti
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
5944 if (adjustment
== NULL
)
5945 adjustment
= gtk_scrolled_window_get_vadjustment(
5946 GTK_SCROLLED_WINDOW(t
->browser_win
));
5948 view_size
= gtk_adjustment_get_page_size(adjustment
);
5949 value
= gtk_adjustment_get_value(adjustment
);
5950 max
= gtk_adjustment_get_upper(adjustment
) - view_size
;
5953 position
= g_strdup("All");
5954 else if (value
== max
)
5955 position
= g_strdup("Bot");
5956 else if (value
== 0)
5957 position
= g_strdup("Top");
5959 position
= g_strdup_printf("%d%%", (int) ((value
/ max
) * 100));
5961 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.position
), position
);
5968 create_window(const gchar
*name
)
5972 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
5973 if (window_maximize
)
5974 gtk_window_maximize(GTK_WINDOW(w
));
5976 gtk_window_set_default_size(GTK_WINDOW(w
), window_width
, window_height
);
5977 gtk_widget_set_name(w
, name
);
5978 gtk_window_set_wmclass(GTK_WINDOW(w
), name
, "XXXTerm");
5984 create_browser(struct tab
*t
)
5988 GtkAdjustment
*adjustment
;
5991 show_oops(NULL
, "create_browser invalid parameters");
5995 t
->sb_h
= GTK_SCROLLBAR(gtk_hscrollbar_new(NULL
));
5996 t
->sb_v
= GTK_SCROLLBAR(gtk_vscrollbar_new(NULL
));
5997 t
->adjust_h
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_h
));
5998 t
->adjust_v
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_v
));
6000 w
= gtk_scrolled_window_new(t
->adjust_h
, t
->adjust_v
);
6001 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
6002 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
6004 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
6005 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
6008 t
->settings
= webkit_web_settings_new();
6010 g_object_set(t
->settings
, "default-encoding", encoding
, (char *)NULL
);
6012 if (user_agent
== NULL
) {
6013 g_object_get(G_OBJECT(t
->settings
), "user-agent", &strval
,
6015 t
->user_agent
= g_strdup_printf("%s %s+", strval
, version
);
6018 t
->user_agent
= g_strdup(user_agent
->value
);
6020 t
->stylesheet
= g_strdup_printf("file://%s/style.css", resource_dir
);
6021 t
->load_images
= auto_load_images
;
6024 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w
));
6025 g_signal_connect(G_OBJECT(adjustment
), "value-changed",
6026 G_CALLBACK(update_statusbar_position
), NULL
);
6035 create_kiosk_toolbar(struct tab
*t
)
6037 GtkWidget
*toolbar
= NULL
, *b
;
6039 b
= gtk_hbox_new(FALSE
, 0);
6041 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
6043 /* backward button */
6044 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
6045 gtk_widget_set_sensitive(t
->backward
, FALSE
);
6046 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
6047 G_CALLBACK(backward_cb
), t
);
6048 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, TRUE
, TRUE
, 0);
6050 /* forward button */
6051 t
->forward
= create_button("Forward", GTK_STOCK_GO_FORWARD
, 0);
6052 gtk_widget_set_sensitive(t
->forward
, FALSE
);
6053 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
6054 G_CALLBACK(forward_cb
), t
);
6055 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, TRUE
, TRUE
, 0);
6058 t
->gohome
= create_button("Home", GTK_STOCK_HOME
, 0);
6059 gtk_widget_set_sensitive(t
->gohome
, true);
6060 g_signal_connect(G_OBJECT(t
->gohome
), "clicked",
6061 G_CALLBACK(home_cb
), t
);
6062 gtk_box_pack_start(GTK_BOX(b
), t
->gohome
, TRUE
, TRUE
, 0);
6064 /* create widgets but don't use them */
6065 t
->uri_entry
= gtk_entry_new();
6066 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
6067 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
6068 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
6074 create_toolbar(struct tab
*t
)
6076 GtkWidget
*toolbar
= NULL
, *b
, *eb1
;
6078 b
= gtk_hbox_new(FALSE
, 0);
6080 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
6082 /* backward button */
6083 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
6084 gtk_widget_set_sensitive(t
->backward
, FALSE
);
6085 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
6086 G_CALLBACK(backward_cb
), t
);
6087 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, FALSE
, FALSE
, 0);
6089 /* forward button */
6090 t
->forward
= create_button("Forward",GTK_STOCK_GO_FORWARD
, 0);
6091 gtk_widget_set_sensitive(t
->forward
, FALSE
);
6092 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
6093 G_CALLBACK(forward_cb
), t
);
6094 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, FALSE
,
6098 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
6099 gtk_widget_set_sensitive(t
->stop
, FALSE
);
6100 g_signal_connect(G_OBJECT(t
->stop
), "clicked",
6101 G_CALLBACK(stop_cb
), t
);
6102 gtk_box_pack_start(GTK_BOX(b
), t
->stop
, FALSE
,
6106 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
6107 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
6108 gtk_widget_set_sensitive(t
->js_toggle
, TRUE
);
6109 g_signal_connect(G_OBJECT(t
->js_toggle
), "clicked",
6110 G_CALLBACK(js_toggle_cb
), t
);
6111 gtk_box_pack_start(GTK_BOX(b
), t
->js_toggle
, FALSE
, FALSE
, 0);
6113 t
->uri_entry
= gtk_entry_new();
6114 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
6115 G_CALLBACK(activate_uri_entry_cb
), t
);
6116 g_signal_connect(G_OBJECT(t
->uri_entry
), "key-press-event",
6117 G_CALLBACK(entry_key_cb
), t
);
6119 eb1
= gtk_hbox_new(FALSE
, 0);
6120 gtk_container_set_border_width(GTK_CONTAINER(eb1
), 1);
6121 gtk_box_pack_start(GTK_BOX(eb1
), t
->uri_entry
, TRUE
, TRUE
, 0);
6122 gtk_box_pack_start(GTK_BOX(b
), eb1
, TRUE
, TRUE
, 0);
6125 if (search_string
) {
6127 t
->search_entry
= gtk_entry_new();
6128 gtk_entry_set_width_chars(GTK_ENTRY(t
->search_entry
), 30);
6129 g_signal_connect(G_OBJECT(t
->search_entry
), "activate",
6130 G_CALLBACK(activate_search_entry_cb
), t
);
6131 g_signal_connect(G_OBJECT(t
->search_entry
), "key-press-event",
6132 G_CALLBACK(entry_key_cb
), t
);
6133 gtk_widget_set_size_request(t
->search_entry
, -1, -1);
6134 eb2
= gtk_hbox_new(FALSE
, 0);
6135 gtk_container_set_border_width(GTK_CONTAINER(eb2
), 1);
6136 gtk_box_pack_start(GTK_BOX(eb2
), t
->search_entry
, TRUE
, TRUE
,
6138 gtk_box_pack_start(GTK_BOX(b
), eb2
, FALSE
, FALSE
, 0);
6145 create_buffers(struct tab
*t
)
6147 GtkCellRenderer
*renderer
;
6150 view
= gtk_tree_view_new();
6152 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view
), FALSE
);
6154 renderer
= gtk_cell_renderer_text_new();
6155 gtk_tree_view_insert_column_with_attributes
6156 (GTK_TREE_VIEW(view
), -1, "Id", renderer
, "text", COL_ID
, (char *)NULL
);
6158 renderer
= gtk_cell_renderer_pixbuf_new();
6159 gtk_tree_view_insert_column_with_attributes
6160 (GTK_TREE_VIEW(view
), -1, "Favicon", renderer
, "pixbuf", COL_FAVICON
,
6163 renderer
= gtk_cell_renderer_text_new();
6164 gtk_tree_view_insert_column_with_attributes
6165 (GTK_TREE_VIEW(view
), -1, "Title", renderer
, "text", COL_TITLE
,
6168 gtk_tree_view_set_model
6169 (GTK_TREE_VIEW(view
), GTK_TREE_MODEL(buffers_store
));
6175 row_activated_cb(GtkTreeView
*view
, GtkTreePath
*path
,
6176 GtkTreeViewColumn
*col
, struct tab
*t
)
6181 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6183 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
,
6186 (GTK_TREE_MODEL(buffers_store
), &iter
, COL_ID
, &id
, -1);
6187 set_current_tab(id
- 1);
6193 /* after tab reordering/creation/removal */
6200 TAILQ_FOREACH(t
, &tabs
, entry
) {
6201 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
6202 if (t
->tab_id
> maxid
)
6205 gtk_widget_show(t
->tab_elems
.sep
);
6208 TAILQ_FOREACH(t
, &tabs
, entry
) {
6209 if (t
->tab_id
== maxid
) {
6210 gtk_widget_hide(t
->tab_elems
.sep
);
6216 /* after active tab change */
6218 recolor_compact_tabs(void)
6224 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6225 TAILQ_FOREACH(t
, &tabs
, entry
)
6226 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
,
6229 curid
= gtk_notebook_get_current_page(notebook
);
6230 TAILQ_FOREACH(t
, &tabs
, entry
)
6231 if (t
->tab_id
== curid
) {
6232 gdk_color_parse(XT_COLOR_CT_ACTIVE
, &color
);
6233 gtk_widget_modify_fg(t
->tab_elems
.label
,
6234 GTK_STATE_NORMAL
, &color
);
6240 set_current_tab(int page_num
)
6242 buffercmd_abort(get_current_tab());
6243 gtk_notebook_set_current_page(notebook
, page_num
);
6244 recolor_compact_tabs();
6248 undo_close_tab_save(struct tab
*t
)
6252 struct undo
*u1
, *u2
;
6254 WebKitWebHistoryItem
*item
;
6256 if ((uri
= get_uri(t
)) == NULL
)
6259 u1
= g_malloc0(sizeof(struct undo
));
6260 u1
->uri
= g_strdup(uri
);
6262 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
6264 m
= webkit_web_back_forward_list_get_forward_length(t
->bfl
);
6265 n
= webkit_web_back_forward_list_get_back_length(t
->bfl
);
6268 /* forward history */
6269 items
= webkit_web_back_forward_list_get_forward_list_with_limit(t
->bfl
, m
);
6273 u1
->history
= g_list_prepend(u1
->history
,
6274 webkit_web_history_item_copy(item
));
6275 items
= g_list_next(items
);
6280 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
6281 u1
->history
= g_list_prepend(u1
->history
,
6282 webkit_web_history_item_copy(item
));
6286 items
= webkit_web_back_forward_list_get_back_list_with_limit(t
->bfl
, n
);
6290 u1
->history
= g_list_prepend(u1
->history
,
6291 webkit_web_history_item_copy(item
));
6292 items
= g_list_next(items
);
6295 TAILQ_INSERT_HEAD(&undos
, u1
, entry
);
6297 if (undo_count
> XT_MAX_UNDO_CLOSE_TAB
) {
6298 u2
= TAILQ_LAST(&undos
, undo_tailq
);
6299 TAILQ_REMOVE(&undos
, u2
, entry
);
6301 g_list_free(u2
->history
);
6310 delete_tab(struct tab
*t
)
6314 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
6320 * no need to join thread here because it won't access t on completion
6323 TAILQ_REMOVE(&tabs
, t
, entry
);
6326 /* Halt all webkit activity. */
6327 abort_favicon_download(t
);
6328 webkit_web_view_stop_loading(t
->wv
);
6330 /* Save the tab, so we can undo the close. */
6331 undo_close_tab_save(t
);
6335 g_source_remove(t
->search_id
);
6338 bzero(&a
, sizeof a
);
6340 inspector_cmd(t
, &a
);
6342 if (browser_mode
== XT_BM_KIOSK
) {
6343 gtk_widget_destroy(t
->uri_entry
);
6344 gtk_widget_destroy(t
->stop
);
6345 gtk_widget_destroy(t
->js_toggle
);
6348 gtk_widget_destroy(t
->tab_elems
.eventbox
);
6349 gtk_widget_destroy(t
->vbox
);
6351 g_free(t
->user_agent
);
6352 g_free(t
->stylesheet
);
6357 if (TAILQ_EMPTY(&tabs
)) {
6358 if (browser_mode
== XT_BM_KIOSK
)
6359 create_new_tab(home
, NULL
, 1, -1);
6361 create_new_tab(NULL
, NULL
, 1, -1);
6364 /* recreate session */
6365 if (session_autosave
) {
6366 bzero(&a
, sizeof a
);
6368 save_tabs(NULL
, &a
);
6372 recolor_compact_tabs();
6376 update_statusbar_zoom(struct tab
*t
)
6379 char s
[16] = { '\0' };
6381 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6382 if ((zoom
<= 0.99 || zoom
>= 1.01))
6383 snprintf(s
, sizeof s
, "%d%%", (int)(zoom
* 100));
6384 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.zoom
), s
);
6388 setzoom_webkit(struct tab
*t
, int adjust
)
6390 #define XT_ZOOMPERCENT 0.04
6395 show_oops(NULL
, "setzoom_webkit invalid parameters");
6399 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6400 if (adjust
== XT_ZOOM_IN
)
6401 zoom
+= XT_ZOOMPERCENT
;
6402 else if (adjust
== XT_ZOOM_OUT
)
6403 zoom
-= XT_ZOOMPERCENT
;
6404 else if (adjust
> 0)
6405 zoom
= default_zoom_level
+ adjust
/ 100.0 - 1.0;
6407 show_oops(t
, "setzoom_webkit invalid zoom value");
6411 if (zoom
< XT_ZOOMPERCENT
)
6412 zoom
= XT_ZOOMPERCENT
;
6413 g_object_set(G_OBJECT(t
->wv
), "zoom-level", zoom
, (char *)NULL
);
6414 update_statusbar_zoom(t
);
6418 tab_clicked_cb(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
6420 struct tab
*t
= (struct tab
*) data
;
6422 DNPRINTF(XT_D_TAB
, "tab_clicked_cb: tab: %d\n", t
->tab_id
);
6424 switch (event
->button
) {
6426 set_current_tab(t
->tab_id
);
6437 append_tab(struct tab
*t
)
6442 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
6443 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
6447 create_sbe(int width
)
6451 sbe
= gtk_entry_new();
6452 gtk_entry_set_inner_border(GTK_ENTRY(sbe
), NULL
);
6453 gtk_entry_set_has_frame(GTK_ENTRY(sbe
), FALSE
);
6454 gtk_widget_set_can_focus(GTK_WIDGET(sbe
), FALSE
);
6455 gtk_widget_modify_font(GTK_WIDGET(sbe
), statusbar_font
);
6456 gtk_entry_set_alignment(GTK_ENTRY(sbe
), 1.0);
6457 gtk_widget_set_size_request(sbe
, width
, -1);
6463 create_new_tab(char *title
, struct undo
*u
, int focus
, int position
)
6468 WebKitWebHistoryItem
*item
;
6472 int sbe_p
= 0, sbe_b
= 0,
6475 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
6477 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
6478 DNPRINTF(XT_D_TAB
, "create_new_tab: new tab rejected\n");
6482 t
= g_malloc0(sizeof *t
);
6484 if (title
== NULL
) {
6485 title
= "(untitled)";
6489 t
->vbox
= gtk_vbox_new(FALSE
, 0);
6491 /* label + button for tab */
6492 b
= gtk_hbox_new(FALSE
, 0);
6495 #if GTK_CHECK_VERSION(2, 20, 0)
6496 t
->spinner
= gtk_spinner_new();
6498 t
->label
= gtk_label_new(title
);
6499 bb
= create_button("Close", GTK_STOCK_CLOSE
, 1);
6500 gtk_widget_set_size_request(t
->label
, 100, 0);
6501 gtk_label_set_max_width_chars(GTK_LABEL(t
->label
), 20);
6502 gtk_label_set_ellipsize(GTK_LABEL(t
->label
), PANGO_ELLIPSIZE_END
);
6503 gtk_widget_set_size_request(b
, 130, 0);
6505 gtk_box_pack_start(GTK_BOX(b
), bb
, FALSE
, FALSE
, 0);
6506 gtk_box_pack_start(GTK_BOX(b
), t
->label
, FALSE
, FALSE
, 0);
6507 #if GTK_CHECK_VERSION(2, 20, 0)
6508 gtk_box_pack_start(GTK_BOX(b
), t
->spinner
, FALSE
, FALSE
, 0);
6512 if (browser_mode
== XT_BM_KIOSK
) {
6513 t
->toolbar
= create_kiosk_toolbar(t
);
6514 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
6517 t
->toolbar
= create_toolbar(t
);
6519 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
,
6527 t
->browser_win
= create_browser(t
);
6528 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
6530 /* oops message for user feedback */
6531 t
->oops
= gtk_entry_new();
6532 gtk_entry_set_inner_border(GTK_ENTRY(t
->oops
), NULL
);
6533 gtk_entry_set_has_frame(GTK_ENTRY(t
->oops
), FALSE
);
6534 gtk_widget_set_can_focus(GTK_WIDGET(t
->oops
), FALSE
);
6535 gdk_color_parse(XT_COLOR_RED
, &color
);
6536 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
6537 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
6538 gtk_widget_modify_font(GTK_WIDGET(t
->oops
), oops_font
);
6541 t
->cmd
= gtk_entry_new();
6542 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
6543 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
6544 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
6545 gtk_widget_modify_font(GTK_WIDGET(t
->cmd
), cmd_font
);
6548 t
->statusbar_box
= gtk_hbox_new(FALSE
, 0);
6550 t
->sbe
.statusbar
= gtk_entry_new();
6551 gtk_entry_set_inner_border(GTK_ENTRY(t
->sbe
.statusbar
), NULL
);
6552 gtk_entry_set_has_frame(GTK_ENTRY(t
->sbe
.statusbar
), FALSE
);
6553 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.statusbar
), FALSE
);
6554 gtk_widget_modify_font(GTK_WIDGET(t
->sbe
.statusbar
), statusbar_font
);
6556 /* create these widgets only if specified in statusbar_elems */
6558 t
->sbe
.position
= create_sbe(40);
6559 t
->sbe
.zoom
= create_sbe(40);
6560 t
->sbe
.buffercmd
= create_sbe(60);
6562 statusbar_modify_attr(t
, XT_COLOR_WHITE
, XT_COLOR_BLACK
);
6564 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), t
->sbe
.statusbar
, TRUE
,
6567 /* gtk widgets cannot be added to a box twice. sbe_* variables
6568 make sure of this */
6569 for (p
= statusbar_elems
; *p
!= '\0'; p
++) {
6573 GtkWidget
*sep
= gtk_vseparator_new();
6575 gdk_color_parse(XT_COLOR_SB_SEPARATOR
, &color
);
6576 gtk_widget_modify_bg(sep
, GTK_STATE_NORMAL
, &color
);
6577 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), sep
,
6578 FALSE
, FALSE
, FALSE
);
6583 warnx("flag \"%c\" specified more than "
6584 "once in statusbar_elems\n", *p
);
6588 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6589 t
->sbe
.position
, FALSE
, FALSE
, FALSE
);
6593 warnx("flag \"%c\" specified more than "
6594 "once in statusbar_elems\n", *p
);
6598 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6599 t
->sbe
.buffercmd
, FALSE
, FALSE
, FALSE
);
6603 warnx("flag \"%c\" specified more than "
6604 "once in statusbar_elems\n", *p
);
6608 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6609 t
->sbe
.zoom
, FALSE
, FALSE
, FALSE
);
6612 warnx("illegal flag \"%c\" in statusbar_elems\n", *p
);
6617 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar_box
, FALSE
, FALSE
, 0);
6620 t
->buffers
= create_buffers(t
);
6621 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->buffers
, FALSE
, FALSE
, 0);
6623 /* xtp meaning is normal by default */
6624 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
6626 /* set empty favicon */
6627 xt_icon_from_name(t
, "text-html");
6629 /* and show it all */
6630 gtk_widget_show_all(b
);
6631 gtk_widget_show_all(t
->vbox
);
6633 /* compact tab bar */
6634 t
->tab_elems
.label
= gtk_label_new(title
);
6635 t
->tab_elems
.favicon
= gtk_image_new();
6636 gtk_label_set_width_chars(GTK_LABEL(t
->tab_elems
.label
), 1.0);
6637 gtk_misc_set_alignment(GTK_MISC(t
->tab_elems
.label
), 0.0, 0.0);
6638 gtk_misc_set_padding(GTK_MISC(t
->tab_elems
.label
), 4.0, 4.0);
6639 gtk_widget_modify_font(GTK_WIDGET(t
->tab_elems
.label
), tabbar_font
);
6641 t
->tab_elems
.eventbox
= gtk_event_box_new();
6642 t
->tab_elems
.box
= gtk_hbox_new(FALSE
, 0);
6643 t
->tab_elems
.sep
= gtk_vseparator_new();
6645 gdk_color_parse(XT_COLOR_CT_BACKGROUND
, &color
);
6646 gtk_widget_modify_bg(t
->tab_elems
.eventbox
, GTK_STATE_NORMAL
, &color
);
6647 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6648 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
, &color
);
6649 gdk_color_parse(XT_COLOR_CT_SEPARATOR
, &color
);
6650 gtk_widget_modify_bg(t
->tab_elems
.sep
, GTK_STATE_NORMAL
, &color
);
6652 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.favicon
, FALSE
,
6654 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.label
, TRUE
,
6656 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.sep
, FALSE
,
6658 gtk_container_add(GTK_CONTAINER(t
->tab_elems
.eventbox
),
6661 gtk_box_pack_start(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
, TRUE
,
6663 gtk_widget_show_all(t
->tab_elems
.eventbox
);
6665 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
6668 id
= position
>= 0 ? position
:
6669 gtk_notebook_get_current_page(notebook
) + 1;
6670 if (id
> gtk_notebook_get_n_pages(notebook
))
6673 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
6674 gtk_notebook_insert_page(notebook
, t
->vbox
, b
, id
);
6675 gtk_box_reorder_child(GTK_BOX(tab_bar
),
6676 t
->tab_elems
.eventbox
, id
);
6681 #if GTK_CHECK_VERSION(2, 20, 0)
6682 /* turn spinner off if we are a new tab without uri */
6684 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
6685 gtk_widget_hide(t
->spinner
);
6688 /* make notebook tabs reorderable */
6689 gtk_notebook_set_tab_reorderable(notebook
, t
->vbox
, TRUE
);
6691 /* compact tabs clickable */
6692 g_signal_connect(G_OBJECT(t
->tab_elems
.eventbox
),
6693 "button_press_event", G_CALLBACK(tab_clicked_cb
), t
);
6695 g_object_connect(G_OBJECT(t
->cmd
),
6696 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb
), t
,
6697 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
6698 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb
), t
,
6699 "signal::activate", G_CALLBACK(cmd_activate_cb
), t
,
6700 "signal::populate-popup", G_CALLBACK(cmd_popup_cb
), t
,
6703 /* reuse wv_button_cb to hide oops */
6704 g_object_connect(G_OBJECT(t
->oops
),
6705 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
6708 g_signal_connect(t
->buffers
,
6709 "row-activated", G_CALLBACK(row_activated_cb
), t
);
6710 g_object_connect(G_OBJECT(t
->buffers
),
6711 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
, (char *)NULL
);
6713 g_object_connect(G_OBJECT(t
->wv
),
6714 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
,
6715 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb
), t
,
6716 "signal::download-requested", G_CALLBACK(webview_download_cb
), t
,
6717 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), t
,
6718 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
6719 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
6720 "signal::create-web-view", G_CALLBACK(webview_cwv_cb
), t
,
6721 "signal::close-web-view", G_CALLBACK(webview_closewv_cb
), t
,
6722 "signal::event", G_CALLBACK(webview_event_cb
), t
,
6723 "signal::load-finished", G_CALLBACK(webview_load_finished_cb
), t
,
6724 "signal::load-progress-changed", G_CALLBACK(webview_progress_changed_cb
), t
,
6725 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb
), t
,
6726 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
6727 "signal::button_release_event", G_CALLBACK(wv_release_button_cb
), t
,
6728 "signal::populate-popup", G_CALLBACK(wv_popup_cb
), t
,
6730 g_signal_connect(t
->wv
,
6731 "notify::load-status", G_CALLBACK(notify_load_status_cb
), t
);
6732 g_signal_connect(t
->wv
,
6733 "notify::title", G_CALLBACK(notify_title_cb
), t
);
6735 /* hijack the unused keys as if we were the browser */
6736 //g_object_connect(G_OBJECT(t->toolbar),
6737 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
6740 g_signal_connect(G_OBJECT(bb
), "button_press_event",
6741 G_CALLBACK(tab_close_cb
), t
);
6744 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
6745 /* restore the tab's history */
6746 if (u
&& u
->history
) {
6750 webkit_web_back_forward_list_add_item(t
->bfl
, item
);
6751 items
= g_list_next(items
);
6754 item
= g_list_nth_data(u
->history
, u
->back
);
6756 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
6759 g_list_free(u
->history
);
6761 webkit_web_back_forward_list_clear(t
->bfl
);
6767 url_set_visibility();
6768 statusbar_set_visibility();
6771 set_current_tab(t
->tab_id
);
6772 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
6776 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
6780 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6787 if (userstyle_global
)
6790 recolor_compact_tabs();
6791 setzoom_webkit(t
, XT_ZOOM_NORMAL
);
6796 notebook_switchpage_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
6802 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
6804 if (gtk_notebook_get_current_page(notebook
) == -1)
6807 TAILQ_FOREACH(t
, &tabs
, entry
) {
6808 if (t
->tab_id
== pn
) {
6809 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
6812 uri
= get_title(t
, TRUE
);
6813 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
6819 /* can't use focus_webview here */
6820 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6827 notebook_pagereordered_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
6830 struct tab
*t
= NULL
, *tt
;
6834 TAILQ_FOREACH(tt
, &tabs
, entry
)
6835 if (tt
->tab_id
== pn
) {
6841 DNPRINTF(XT_D_TAB
, "page_reordered_cb: tab: %d\n", t
->tab_id
);
6843 gtk_box_reorder_child(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
,
6848 menuitem_response(struct tab
*t
)
6850 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
6854 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
6856 GtkWidget
*menu
, *menu_items
;
6857 GdkEventButton
*bevent
;
6861 if (event
->type
== GDK_BUTTON_PRESS
) {
6862 bevent
= (GdkEventButton
*) event
;
6863 menu
= gtk_menu_new();
6865 TAILQ_FOREACH(ti
, &tabs
, entry
) {
6866 if ((uri
= get_uri(ti
)) == NULL
)
6867 /* XXX make sure there is something to print */
6868 /* XXX add gui pages in here to look purdy */
6870 menu_items
= gtk_menu_item_new_with_label(uri
);
6871 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), menu_items
);
6872 gtk_widget_show(menu_items
);
6874 g_signal_connect_swapped((menu_items
),
6875 "activate", G_CALLBACK(menuitem_response
),
6879 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
,
6880 bevent
->button
, bevent
->time
);
6882 /* unref object so it'll free itself when popped down */
6883 #if !GTK_CHECK_VERSION(3, 0, 0)
6884 /* XXX does not need unref with gtk+3? */
6885 g_object_ref_sink(menu
);
6886 g_object_unref(menu
);
6889 return (TRUE
/* eat event */);
6892 return (FALSE
/* propagate */);
6896 icon_size_map(int iconsz
)
6898 if (iconsz
<= GTK_ICON_SIZE_INVALID
||
6899 iconsz
> GTK_ICON_SIZE_DIALOG
)
6900 return (GTK_ICON_SIZE_SMALL_TOOLBAR
);
6906 create_button(char *name
, char *stockid
, int size
)
6908 GtkWidget
*button
, *image
;
6912 rcstring
= g_strdup_printf(
6913 "style \"%s-style\"\n"
6915 " GtkWidget::focus-padding = 0\n"
6916 " GtkWidget::focus-line-width = 0\n"
6920 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
6921 gtk_rc_parse_string(rcstring
);
6923 button
= gtk_button_new();
6924 gtk_button_set_focus_on_click(GTK_BUTTON(button
), FALSE
);
6925 gtk_icon_size
= icon_size_map(size
? size
: icon_size
);
6927 image
= gtk_image_new_from_stock(stockid
, gtk_icon_size
);
6928 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
6929 gtk_container_set_border_width(GTK_CONTAINER(button
), 1);
6930 gtk_container_add(GTK_CONTAINER(button
), GTK_WIDGET(image
));
6931 gtk_widget_set_name(button
, name
);
6932 gtk_button_set_relief(GTK_BUTTON(button
), GTK_RELIEF_NONE
);
6938 button_set_stockid(GtkWidget
*button
, char *stockid
)
6942 image
= gtk_image_new_from_stock(stockid
, icon_size_map(icon_size
));
6943 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
6944 gtk_button_set_image(GTK_BUTTON(button
), image
);
6948 clipb_primary_cb(GtkClipboard
*primary
, GdkEvent
*event
, gpointer notused
)
6951 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
6954 if (xterm_workaround
== 0)
6958 * xterm doesn't play nice with clipboards because it clears the
6959 * primary when clicked. We rely on primary being set to properly
6960 * handle middle mouse button clicks (paste). So when someone clears
6961 * primary copy whatever is in CUT_BUFFER0 into primary to simualte
6962 * other application behavior (as in DON'T clear primary).
6965 p
= gtk_clipboard_wait_for_text(primary
);
6967 if (gdk_property_get(gdk_get_default_root_window(),
6969 gdk_atom_intern("STRING", FALSE
),
6971 1024 * 1024 /* picked out of my butt */,
6979 /* yes sir, we need to NUL the string */
6981 gtk_clipboard_set_text(primary
, p
, -1);
6995 char file
[PATH_MAX
];
6998 vbox
= gtk_vbox_new(FALSE
, 0);
6999 gtk_box_set_spacing(GTK_BOX(vbox
), 0);
7000 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
7001 #if !GTK_CHECK_VERSION(3, 0, 0)
7002 /* XXX seems to be needed with gtk+2 */
7003 gtk_notebook_set_tab_hborder(notebook
, 0);
7004 gtk_notebook_set_tab_vborder(notebook
, 0);
7006 gtk_notebook_set_scrollable(notebook
, TRUE
);
7007 gtk_notebook_set_show_border(notebook
, FALSE
);
7008 gtk_widget_set_can_focus(GTK_WIDGET(notebook
), FALSE
);
7010 abtn
= gtk_button_new();
7011 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
7012 gtk_widget_set_size_request(arrow
, -1, -1);
7013 gtk_container_add(GTK_CONTAINER(abtn
), arrow
);
7014 gtk_widget_set_size_request(abtn
, -1, 20);
7016 #if GTK_CHECK_VERSION(2, 20, 0)
7017 gtk_notebook_set_action_widget(notebook
, abtn
, GTK_PACK_END
);
7019 gtk_widget_set_size_request(GTK_WIDGET(notebook
), -1, -1);
7021 /* compact tab bar */
7022 tab_bar
= gtk_hbox_new(TRUE
, 0);
7024 gtk_box_pack_start(GTK_BOX(vbox
), tab_bar
, FALSE
, FALSE
, 0);
7025 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
7026 gtk_widget_set_size_request(vbox
, -1, -1);
7028 g_object_connect(G_OBJECT(notebook
),
7029 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb
), NULL
,
7031 g_object_connect(G_OBJECT(notebook
),
7032 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb
),
7033 NULL
, (char *)NULL
);
7034 g_signal_connect(G_OBJECT(abtn
), "button_press_event",
7035 G_CALLBACK(arrow_cb
), NULL
);
7037 main_window
= create_window("xxxterm");
7038 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
7039 g_signal_connect(G_OBJECT(main_window
), "delete_event",
7040 G_CALLBACK(gtk_main_quit
), NULL
);
7043 for (i
= 0; i
< LENGTH(icons
); i
++) {
7044 snprintf(file
, sizeof file
, "%s/%s", resource_dir
, icons
[i
]);
7045 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
7046 l
= g_list_append(l
, pb
);
7048 gtk_window_set_default_icon_list(l
);
7050 /* clipboard work around */
7051 if (xterm_workaround
)
7053 G_OBJECT(gtk_clipboard_get(GDK_SELECTION_PRIMARY
)),
7054 "owner-change", G_CALLBACK(clipb_primary_cb
), NULL
);
7056 gtk_widget_show_all(abtn
);
7057 gtk_widget_show_all(main_window
);
7058 notebook_tab_set_visibility();
7061 #ifndef XT_SOCKET_DISABLE
7063 send_cmd_to_socket(char *cmd
)
7066 struct sockaddr_un sa
;
7068 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7069 warnx("%s: socket", __func__
);
7073 sa
.sun_family
= AF_UNIX
;
7074 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
7075 work_dir
, XT_SOCKET_FILE
);
7078 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7079 warnx("%s: connect", __func__
);
7083 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
7084 warnx("%s: send", __func__
);
7095 socket_watcher(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
7098 char str
[XT_MAX_URL_LENGTH
];
7099 socklen_t t
= sizeof(struct sockaddr_un
);
7100 struct sockaddr_un sa
;
7105 gint fd
= g_io_channel_unix_get_fd(source
);
7107 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
7112 if (getpeereid(s
, &uid
, &gid
) == -1) {
7116 if (uid
!= getuid() || gid
!= getgid()) {
7117 warnx("unauthorized user");
7123 warnx("not a valid user");
7127 n
= recv(s
, str
, sizeof(str
), 0);
7131 tt
= TAILQ_LAST(&tabs
, tab_list
);
7132 cmd_execute(tt
, str
);
7140 struct sockaddr_un sa
;
7142 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7143 warn("is_running: socket");
7147 sa
.sun_family
= AF_UNIX
;
7148 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
7149 work_dir
, XT_SOCKET_FILE
);
7152 /* connect to see if there is a listener */
7153 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
7154 rv
= 0; /* not running */
7156 rv
= 1; /* already running */
7167 struct sockaddr_un sa
;
7169 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7170 warn("build_socket: socket");
7174 sa
.sun_family
= AF_UNIX
;
7175 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
7176 work_dir
, XT_SOCKET_FILE
);
7179 /* connect to see if there is a listener */
7180 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7181 /* no listener so we will */
7182 unlink(sa
.sun_path
);
7184 if (bind(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7185 warn("build_socket: bind");
7189 if (listen(s
, 1) == -1) {
7190 warn("build_socket: listen");
7208 if (stat(dir
, &sb
)) {
7209 if (mkdir(dir
, S_IRWXU
) == -1)
7210 err(1, "mkdir %s", dir
);
7212 err(1, "stat %s", dir
);
7214 if (S_ISDIR(sb
.st_mode
) == 0)
7215 errx(1, "%s not a dir", dir
);
7216 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
7217 warnx("fixing invalid permissions on %s", dir
);
7218 if (chmod(dir
, S_IRWXU
) == -1)
7219 err(1, "chmod %s", dir
);
7227 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
7231 GStaticRecMutex my_gdk_mtx
= G_STATIC_REC_MUTEX_INIT
;
7232 volatile int mtx_depth
;
7236 * The linux flash plugin violates the gdk locking mechanism.
7237 * Work around the issue by using a recursive mutex with some match applied
7238 * to see if we hit a buggy condition.
7240 * The following code is painful so just don't read it. It really doesn't
7241 * make much sense but seems to work.
7246 g_static_rec_mutex_lock(&my_gdk_mtx
);
7249 if (mtx_depth
<= 0) {
7250 /* should not happen */
7251 show_oops(NULL
, "negative mutex locking bug, trying to "
7253 fprintf(stderr
, "negative mutex locking bug, trying to "
7255 g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
7256 g_static_rec_mutex_lock(&my_gdk_mtx
);
7261 if (mtx_depth
!= 1) {
7262 /* decrease mutext depth to 1 */
7264 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7266 } while (mtx_depth
> 1);
7275 /* if mutex depth isn't 1 then something went bad */
7276 if (mtx_depth
!= 1) {
7277 x
= g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
7279 /* should not happen */
7280 show_oops(NULL
, "mutex unlocking bug, trying to "
7282 fprintf(stderr
, "mutex unlocking bug, trying to "
7286 if (mtx_complain
== 0) {
7287 show_oops(NULL
, "buggy mutex implementation detected, "
7288 "work around implemented");
7289 fprintf(stderr
, "buggy mutex implementation detected, "
7290 "work around implemented");
7297 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7301 main(int argc
, char **argv
)
7304 int c
, optn
= 0, opte
= 0, focus
= 1;
7305 char conf
[PATH_MAX
] = { '\0' };
7306 char file
[PATH_MAX
];
7307 char *env_proxy
= NULL
;
7311 start_argv
= (const char **)argv
;
7315 g_thread_init(NULL
);
7316 gdk_threads_set_lock_functions(mtx_lock
, mtx_unlock
);
7318 gdk_threads_enter();
7320 gcry_control (GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
7322 gtk_init(&argc
, &argv
);
7324 gnutls_global_init();
7326 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
7331 RB_INIT(&downloads
);
7333 TAILQ_INIT(&sessions
);
7336 TAILQ_INIT(&aliases
);
7342 TAILQ_INIT(&ua_list
);
7344 #ifndef XT_RESOURCE_LIMITS_DISABLE
7348 GIOChannel
*channel
;
7350 /* fiddle with ulimits */
7351 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7354 /* just use them all */
7355 rlp
.rlim_cur
= rlp
.rlim_max
;
7356 if (setrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7358 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7360 else if (rlp
.rlim_cur
<= 256)
7361 startpage_add("%s requires at least 256 file "
7362 "descriptors, currently it has up to %d available",
7363 __progname
, rlp
.rlim_cur
);
7367 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
7376 #ifdef XXXTERM_BUILDSTR
7377 errx(0 , "Version: %s Build: %s",
7378 version
, XXXTERM_BUILDSTR
);
7380 errx(0 , "Version: %s", version
);
7384 strlcpy(conf
, optarg
, sizeof(conf
));
7387 strlcpy(named_session
, optarg
, sizeof(named_session
));
7408 xtp_generate_keys();
7410 /* XXX this hammer is way too big but treat all signaling the same */
7411 #ifndef XT_SIGNALS_DISABLE
7413 struct sigaction sact
;
7415 bzero(&sact
, sizeof(sact
));
7416 sigemptyset(&sact
.sa_mask
);
7417 sact
.sa_handler
= sigchild
;
7418 sact
.sa_flags
= SA_NOCLDSTOP
;
7419 sigaction(SIGCHLD
, &sact
, NULL
);
7422 /* set download dir */
7423 pwd
= getpwuid(getuid());
7425 errx(1, "invalid user %d", getuid());
7426 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
7428 /* compile buffer command regexes */
7431 /* set default string settings */
7432 home
= g_strdup("https://www.cyphertite.com");
7433 search_string
= g_strdup("https://ssl.scroogle.org/cgi-bin/nbbwssl.cgi?Gw=%s");
7434 resource_dir
= g_strdup("/usr/local/share/xxxterm/");
7435 strlcpy(runtime_settings
, "runtime", sizeof runtime_settings
);
7436 cmd_font_name
= g_strdup("monospace normal 9");
7437 oops_font_name
= g_strdup("monospace normal 9");
7438 statusbar_font_name
= g_strdup("monospace normal 9");
7439 tabbar_font_name
= g_strdup("monospace normal 9");
7440 statusbar_elems
= g_strdup("BP");
7441 spell_check_languages
= g_strdup("en_US");
7442 encoding
= g_strdup("UTF-8");
7444 /* read config file */
7445 if (strlen(conf
) == 0)
7446 snprintf(conf
, sizeof conf
, "%s/.%s",
7447 pwd
->pw_dir
, XT_CONF_FILE
);
7448 config_parse(conf
, 0);
7451 cmd_font
= pango_font_description_from_string(cmd_font_name
);
7452 oops_font
= pango_font_description_from_string(oops_font_name
);
7453 statusbar_font
= pango_font_description_from_string(statusbar_font_name
);
7454 tabbar_font
= pango_font_description_from_string(tabbar_font_name
);
7456 /* working directory */
7457 if (strlen(work_dir
) == 0)
7458 snprintf(work_dir
, sizeof work_dir
, "%s/%s",
7459 pwd
->pw_dir
, XT_DIR
);
7462 /* icon cache dir */
7463 snprintf(cache_dir
, sizeof cache_dir
, "%s/%s", work_dir
, XT_CACHE_DIR
);
7467 snprintf(certs_dir
, sizeof certs_dir
, "%s/%s", work_dir
, XT_CERT_DIR
);
7471 snprintf(sessions_dir
, sizeof sessions_dir
, "%s/%s",
7472 work_dir
, XT_SESSIONS_DIR
);
7473 xxx_dir(sessions_dir
);
7476 snprintf(js_dir
, sizeof js_dir
, "%s/%s", work_dir
, XT_JS_DIR
);
7480 snprintf(temp_dir
, sizeof temp_dir
, "%s/%s", work_dir
, XT_TEMP_DIR
);
7483 /* runtime settings that can override config file */
7484 if (runtime_settings
[0] != '\0')
7485 config_parse(runtime_settings
, 1);
7488 if (!strcmp(download_dir
, pwd
->pw_dir
))
7489 strlcat(download_dir
, "/downloads", sizeof download_dir
);
7490 xxx_dir(download_dir
);
7492 /* favorites file */
7493 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_FAVS_FILE
);
7494 if (stat(file
, &sb
)) {
7495 warnx("favorites file doesn't exist, creating it");
7496 if ((f
= fopen(file
, "w")) == NULL
)
7497 err(1, "favorites");
7501 /* quickmarks file */
7502 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
7503 if (stat(file
, &sb
)) {
7504 warnx("quickmarks file doesn't exist, creating it");
7505 if ((f
= fopen(file
, "w")) == NULL
)
7506 err(1, "quickmarks");
7510 /* search history */
7511 if (history_autosave
) {
7512 snprintf(search_file
, sizeof search_file
, "%s/%s",
7513 work_dir
, XT_SEARCH_FILE
);
7514 if (stat(search_file
, &sb
)) {
7515 warnx("search history file doesn't exist, creating it");
7516 if ((f
= fopen(search_file
, "w")) == NULL
)
7517 err(1, "search_history");
7520 history_read(&shl
, search_file
, &search_history_count
);
7523 /* command history */
7524 if (history_autosave
) {
7525 snprintf(command_file
, sizeof command_file
, "%s/%s",
7526 work_dir
, XT_COMMAND_FILE
);
7527 if (stat(command_file
, &sb
)) {
7528 warnx("command history file doesn't exist, creating it");
7529 if ((f
= fopen(command_file
, "w")) == NULL
)
7530 err(1, "command_history");
7533 history_read(&chl
, command_file
, &cmd_history_count
);
7537 session
= webkit_get_default_session();
7542 if (stat(ssl_ca_file
, &sb
)) {
7543 warnx("no CA file: %s", ssl_ca_file
);
7544 g_free(ssl_ca_file
);
7547 g_object_set(session
,
7548 SOUP_SESSION_SSL_CA_FILE
, ssl_ca_file
,
7549 SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
7553 /* guess_search regex */
7554 if (url_regex
== NULL
)
7555 url_regex
= g_strdup(XT_URL_REGEX
);
7557 if (regcomp(&url_re
, url_regex
, REG_EXTENDED
| REG_NOSUB
))
7558 startpage_add("invalid url regex %s", url_regex
);
7561 env_proxy
= getenv("http_proxy");
7563 setup_proxy(env_proxy
);
7565 env_proxy
= getenv("HTTP_PROXY");
7567 setup_proxy(env_proxy
);
7569 setup_proxy(http_proxy
);
7571 #ifndef XT_SOCKET_DISABLE
7573 send_cmd_to_socket(argv
[0]);
7577 opte
= opte
; /* shut mingw up */
7579 /* set some connection parameters */
7580 g_object_set(session
, "max-conns", max_connections
, (char *)NULL
);
7581 g_object_set(session
, "max-conns-per-host", max_host_connections
,
7584 g_signal_connect(session
, "request-queued", G_CALLBACK(session_rq_cb
),
7587 #ifndef XT_SOCKET_DISABLE
7588 /* see if there is already an xxxterm running */
7589 if (single_instance
&& is_running()) {
7591 warnx("already running");
7596 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
7597 send_cmd_to_socket(cmd
);
7607 optn
= optn
; /* shut mingw up */
7612 /* uri completion */
7613 completion_model
= gtk_list_store_new(1, G_TYPE_STRING
);
7616 buffers_store
= gtk_list_store_new
7617 (NUM_COLS
, G_TYPE_UINT
, GDK_TYPE_PIXBUF
, G_TYPE_STRING
);
7623 notebook_tab_set_visibility();
7625 if (save_global_history
)
7626 restore_global_history();
7628 /* restore session list */
7629 restore_sessions_list();
7631 if (!strcmp(named_session
, XT_SAVED_TABS_FILE
))
7632 restore_saved_tabs();
7634 a
.s
= named_session
;
7635 a
.i
= XT_SES_DONOTHING
;
7636 open_tabs(NULL
, &a
);
7639 /* see if we have an exception */
7640 if (!TAILQ_EMPTY(&spl
)) {
7641 create_new_tab("about:startpage", NULL
, focus
, -1);
7646 create_new_tab(argv
[0], NULL
, focus
, -1);
7653 if (TAILQ_EMPTY(&tabs
))
7654 create_new_tab(home
, NULL
, 1, -1);
7655 #ifndef XT_SOCKET_DISABLE
7657 if ((s
= build_socket()) != -1) {
7658 channel
= g_io_channel_unix_new(s
);
7659 g_io_add_watch(channel
, G_IO_IN
, socket_watcher
, NULL
);
7666 gdk_threads_leave();
7667 g_static_rec_mutex_unlock_full(&my_gdk_mtx
); /* just in case */
7670 gnutls_global_deinit();