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.
24 char *version
= XXXTERM_VERSION
;
27 u_int32_t swm_debug
= 0
50 GCRY_THREAD_OPTION_PTHREAD_IMPL
;
62 TAILQ_ENTRY(session
) entry
;
65 TAILQ_HEAD(session_list
, session
);
68 TAILQ_ENTRY(undo
) entry
;
71 int back
; /* Keeps track of how many back
72 * history items there are. */
74 TAILQ_HEAD(undo_tailq
, undo
);
76 struct command_entry
{
78 TAILQ_ENTRY(command_entry
) entry
;
80 TAILQ_HEAD(command_list
, command_entry
);
83 #define XT_DIR (".xxxterm")
84 #define XT_CACHE_DIR ("cache")
85 #define XT_CERT_DIR ("certs/")
86 #define XT_JS_DIR ("js/")
87 #define XT_SESSIONS_DIR ("sessions/")
88 #define XT_TEMP_DIR ("tmp")
89 #define XT_CONF_FILE ("xxxterm.conf")
90 #define XT_QMARKS_FILE ("quickmarks")
91 #define XT_SAVED_TABS_FILE ("main_session")
92 #define XT_RESTART_TABS_FILE ("restart_tabs")
93 #define XT_SOCKET_FILE ("socket")
94 #define XT_SAVE_SESSION_ID ("SESSION_NAME=")
95 #define XT_SEARCH_FILE ("search_history")
96 #define XT_COMMAND_FILE ("command_history")
97 #define XT_DLMAN_REFRESH "10"
98 #define XT_MAX_URL_LENGTH (4096) /* 1 page is atomic, don't make bigger */
99 #define XT_MAX_UNDO_CLOSE_TAB (32)
100 #define XT_RESERVED_CHARS "$&+,/:;=?@ \"<>#%%{}|^~[]`"
101 #define XT_PRINT_EXTRA_MARGIN 10
102 #define XT_URL_REGEX ("^[[:blank:]]*[^[:blank:]]*([[:alnum:]-]+\\.)+[[:alnum:]-][^[:blank:]]*[[:blank:]]*$")
103 #define XT_INVALID_MARK (-1) /* XXX this is a double, maybe use something else, like a nan */
106 #define XT_COLOR_RED "#cc0000"
107 #define XT_COLOR_YELLOW "#ffff66"
108 #define XT_COLOR_BLUE "lightblue"
109 #define XT_COLOR_GREEN "#99ff66"
110 #define XT_COLOR_WHITE "white"
111 #define XT_COLOR_BLACK "black"
113 #define XT_COLOR_CT_BACKGROUND "#000000"
114 #define XT_COLOR_CT_INACTIVE "#dddddd"
115 #define XT_COLOR_CT_ACTIVE "#bbbb00"
116 #define XT_COLOR_CT_SEPARATOR "#555555"
118 #define XT_COLOR_SB_SEPARATOR "#555555"
120 #define XT_PROTO_DELIM "://"
123 #define XT_MOVE_INVALID (0)
124 #define XT_MOVE_DOWN (1)
125 #define XT_MOVE_UP (2)
126 #define XT_MOVE_BOTTOM (3)
127 #define XT_MOVE_TOP (4)
128 #define XT_MOVE_PAGEDOWN (5)
129 #define XT_MOVE_PAGEUP (6)
130 #define XT_MOVE_HALFDOWN (7)
131 #define XT_MOVE_HALFUP (8)
132 #define XT_MOVE_LEFT (9)
133 #define XT_MOVE_FARLEFT (10)
134 #define XT_MOVE_RIGHT (11)
135 #define XT_MOVE_FARRIGHT (12)
136 #define XT_MOVE_PERCENT (13)
137 #define XT_MOVE_CENTER (14)
139 #define XT_QMARK_SET (0)
140 #define XT_QMARK_OPEN (1)
141 #define XT_QMARK_TAB (2)
143 #define XT_MARK_SET (0)
144 #define XT_MARK_GOTO (1)
146 #define XT_GO_UP_ROOT (999)
148 #define XT_TAB_LAST (-4)
149 #define XT_TAB_FIRST (-3)
150 #define XT_TAB_PREV (-2)
151 #define XT_TAB_NEXT (-1)
152 #define XT_TAB_INVALID (0)
153 #define XT_TAB_NEW (1)
154 #define XT_TAB_DELETE (2)
155 #define XT_TAB_DELQUIT (3)
156 #define XT_TAB_OPEN (4)
157 #define XT_TAB_UNDO_CLOSE (5)
158 #define XT_TAB_SHOW (6)
159 #define XT_TAB_HIDE (7)
160 #define XT_TAB_NEXTSTYLE (8)
161 #define XT_TAB_LOAD_IMAGES (9)
163 #define XT_NAV_INVALID (0)
164 #define XT_NAV_BACK (1)
165 #define XT_NAV_FORWARD (2)
166 #define XT_NAV_RELOAD (3)
167 #define XT_NAV_STOP (4)
169 #define XT_FOCUS_INVALID (0)
170 #define XT_FOCUS_URI (1)
171 #define XT_FOCUS_SEARCH (2)
173 #define XT_SEARCH_INVALID (0)
174 #define XT_SEARCH_NEXT (1)
175 #define XT_SEARCH_PREV (2)
177 #define XT_STYLE_CURRENT_TAB (0)
178 #define XT_STYLE_GLOBAL (1)
180 #define XT_PASTE_CURRENT_TAB (0)
181 #define XT_PASTE_NEW_TAB (1)
183 #define XT_ZOOM_IN (-1)
184 #define XT_ZOOM_OUT (-2)
185 #define XT_ZOOM_NORMAL (100)
187 #define XT_URL_SHOW (1)
188 #define XT_URL_HIDE (2)
190 #define XT_CMD_OPEN (0)
191 #define XT_CMD_OPEN_CURRENT (1)
192 #define XT_CMD_TABNEW (2)
193 #define XT_CMD_TABNEW_CURRENT (3)
195 #define XT_STATUS_NOTHING (0)
196 #define XT_STATUS_LINK (1)
197 #define XT_STATUS_URI (2)
198 #define XT_STATUS_LOADING (3)
200 #define XT_SES_DONOTHING (0)
201 #define XT_SES_CLOSETABS (1)
203 #define XT_PREFIX (1<<0)
204 #define XT_USERARG (1<<1)
205 #define XT_URLARG (1<<2)
206 #define XT_INTARG (1<<3)
207 #define XT_SESSARG (1<<4)
208 #define XT_SETARG (1<<5)
210 #define XT_HINT_NEWTAB (1<<0)
212 #define XT_BUFCMD_SZ (8)
214 #define XT_EJS_SHOW (1<<0)
216 GtkWidget
* create_button(char *, char *, int);
218 void recalc_tabs(void);
219 void recolor_compact_tabs(void);
220 void set_current_tab(int page_num
);
221 gboolean
update_statusbar_position(GtkAdjustment
*, gpointer
);
222 void marks_clear(struct tab
*t
);
225 extern char *__progname
;
228 GtkWidget
*main_window
;
229 GtkNotebook
*notebook
;
231 GtkWidget
*arrow
, *abtn
;
232 struct tab_list tabs
;
233 struct history_list hl
;
234 int hl_purge_count
= 0;
235 struct session_list sessions
;
236 struct domain_list c_wl
;
237 struct domain_list js_wl
;
238 struct domain_list pl_wl
;
239 struct undo_tailq undos
;
240 struct keybinding_list kbl
;
242 struct user_agent_list ua_list
;
243 int user_agent_count
= 0;
244 struct command_list chl
;
245 struct command_list shl
;
246 struct command_entry
*history_at
;
247 struct command_entry
*search_at
;
249 int cmd_history_count
= 0;
250 int search_history_count
= 0;
252 long long unsigned int blocked_cookies
= 0;
253 char named_session
[PATH_MAX
];
254 GtkListStore
*completion_model
;
255 GtkListStore
*buffers_store
;
257 char *qmarks
[XT_NOMARKS
];
258 int btn_down
; /* M1 down in any wv */
259 regex_t url_re
; /* guess_search regex */
261 /* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
262 int next_download_id
= 1;
264 void xxx_dir(char *);
265 int icon_size_map(int);
268 history_delete(struct command_list
*l
, int *counter
)
270 struct command_entry
*c
;
272 if (l
== NULL
|| counter
== NULL
)
275 c
= TAILQ_LAST(l
, command_list
);
279 TAILQ_REMOVE(l
, c
, entry
);
286 history_add(struct command_list
*list
, char *file
, char *l
, int *counter
)
288 struct command_entry
*c
;
291 if (list
== NULL
|| l
== NULL
|| counter
== NULL
)
294 /* don't add the same line */
295 c
= TAILQ_FIRST(list
);
297 if (!strcmp(c
->line
+ 1 /* skip space */, l
))
300 c
= g_malloc0(sizeof *c
);
301 c
->line
= g_strdup_printf(" %s", l
);
304 TAILQ_INSERT_HEAD(list
, c
, entry
);
307 history_delete(list
, counter
);
309 if (history_autosave
&& file
) {
310 f
= fopen(file
, "w");
312 show_oops(NULL
, "couldn't write history %s", file
);
316 TAILQ_FOREACH_REVERSE(c
, list
, command_list
, entry
) {
318 fprintf(f
, "%s\n", c
->line
);
326 history_read(struct command_list
*list
, char *file
, int *counter
)
329 char *s
, line
[65536];
331 if (list
== NULL
|| file
== NULL
)
334 f
= fopen(file
, "r");
336 startpage_add("couldn't open history file %s", file
);
341 s
= fgets(line
, sizeof line
, f
);
342 if (s
== NULL
|| feof(f
) || ferror(f
))
344 if ((s
= strchr(line
, '\n')) == NULL
) {
345 startpage_add("invalid history file %s", file
);
351 history_add(list
, NULL
, line
+ 1, counter
);
359 /* marks and quickmarks array storage.
360 * first a-z, then A-Z, then 0-9 */
367 if (i
>= 0 && i
<= 'z' - 'a')
371 if (i
>= 0 && i
<= 'Z' - 'A')
386 if (m
>= 'a' && m
<= 'z')
387 return ret
+ m
- 'a';
389 ret
+= 'z' - 'a' + 1;
390 if (m
>= 'A' && m
<= 'Z')
391 return ret
+ m
- 'A';
393 ret
+= 'Z' - 'A' + 1;
394 if (m
>= '0' && m
<= '9')
395 return ret
+ m
- '0';
404 int saved_errno
, status
;
409 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
413 if (errno
!= ECHILD
) {
415 clog_warn("sigchild: waitpid:");
421 if (WIFEXITED(status
)) {
422 if (WEXITSTATUS(status
) != 0) {
424 clog_warnx("sigchild: child exit status: %d",
425 WEXITSTATUS(status));
430 clog_warnx("sigchild: child is terminated abnormally");
439 is_g_object_setting(GObject
*o
, char *str
)
441 guint n_props
= 0, i
;
442 GParamSpec
**proplist
;
444 if (! G_IS_OBJECT(o
))
447 proplist
= g_object_class_list_properties(G_OBJECT_GET_CLASS(o
),
450 for (i
=0; i
< n_props
; i
++) {
451 if (! strcmp(proplist
[i
]->name
, str
))
458 get_current_tab(void)
462 TAILQ_FOREACH(t
, &tabs
, entry
) {
463 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
467 warnx("%s: no current tab", __func__
);
473 set_status(struct tab
*t
, gchar
*s
, int status
)
481 case XT_STATUS_LOADING
:
482 type
= g_strdup_printf("Loading: %s", s
);
486 type
= g_strdup_printf("Link: %s", s
);
488 t
->status
= g_strdup(gtk_entry_get_text(
489 GTK_ENTRY(t
->sbe
.statusbar
)));
493 type
= g_strdup_printf("%s", s
);
495 t
->status
= g_strdup(type
);
499 t
->status
= g_strdup(s
);
501 case XT_STATUS_NOTHING
:
506 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.statusbar
), s
);
512 hide_cmd(struct tab
*t
)
514 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
516 history_at
= NULL
; /* just in case */
517 search_at
= NULL
; /* just in case */
518 gtk_widget_hide(t
->cmd
);
522 show_cmd(struct tab
*t
)
524 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
528 gtk_widget_hide(t
->oops
);
529 gtk_widget_show(t
->cmd
);
533 hide_buffers(struct tab
*t
)
535 gtk_widget_hide(t
->buffers
);
536 gtk_list_store_clear(buffers_store
);
547 sort_tabs_by_page_num(struct tab
***stabs
)
552 num_tabs
= gtk_notebook_get_n_pages(notebook
);
554 *stabs
= g_malloc0(num_tabs
* sizeof(struct tab
*));
556 TAILQ_FOREACH(t
, &tabs
, entry
)
557 (*stabs
)[gtk_notebook_page_num(notebook
, t
->vbox
)] = t
;
563 buffers_make_list(void)
566 const gchar
*title
= NULL
;
568 struct tab
**stabs
= NULL
;
570 num_tabs
= sort_tabs_by_page_num(&stabs
);
572 for (i
= 0; i
< num_tabs
; i
++)
574 gtk_list_store_append(buffers_store
, &iter
);
575 title
= get_title(stabs
[i
], FALSE
);
576 gtk_list_store_set(buffers_store
, &iter
,
577 COL_ID
, i
+ 1, /* Enumerate the tabs starting from 1
579 COL_FAVICON
, gtk_image_get_pixbuf
580 (GTK_IMAGE(stabs
[i
]->tab_elems
.favicon
)),
589 show_buffers(struct tab
*t
)
591 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)))
595 gtk_widget_show(t
->buffers
);
596 gtk_widget_grab_focus(GTK_WIDGET(t
->buffers
));
600 toggle_buffers(struct tab
*t
)
602 if (gtk_widget_get_visible(t
->buffers
))
609 buffers(struct tab
*t
, struct karg
*args
)
617 hide_oops(struct tab
*t
)
619 gtk_widget_hide(t
->oops
);
623 show_oops(struct tab
*at
, const char *fmt
, ...)
627 struct tab
*t
= NULL
;
633 if ((t
= get_current_tab()) == NULL
)
639 if (vasprintf(&msg
, fmt
, ap
) == -1)
640 errx(1, "show_oops failed");
643 gtk_entry_set_text(GTK_ENTRY(t
->oops
), msg
);
644 gtk_widget_hide(t
->cmd
);
645 gtk_widget_show(t
->oops
);
651 char work_dir
[PATH_MAX
];
652 char certs_dir
[PATH_MAX
];
653 char js_dir
[PATH_MAX
];
654 char cache_dir
[PATH_MAX
];
655 char sessions_dir
[PATH_MAX
];
656 char temp_dir
[PATH_MAX
];
657 char cookie_file
[PATH_MAX
];
658 SoupSession
*session
;
659 SoupCookieJar
*s_cookiejar
;
660 SoupCookieJar
*p_cookiejar
;
661 char rc_fname
[PATH_MAX
];
663 struct mime_type_list mtl
;
664 struct alias_list aliases
;
667 struct tab
*create_new_tab(char *, struct undo
*, int, int);
668 void delete_tab(struct tab
*);
669 void setzoom_webkit(struct tab
*, int);
670 int download_rb_cmp(struct download
*, struct download
*);
671 gboolean
cmd_execute(struct tab
*t
, char *str
);
674 history_rb_cmp(struct history
*h1
, struct history
*h2
)
676 return (strcmp(h1
->uri
, h2
->uri
));
678 RB_GENERATE(history_list
, history
, entry
, history_rb_cmp
);
681 domain_rb_cmp(struct domain
*d1
, struct domain
*d2
)
683 return (strcmp(d1
->d
, d2
->d
));
685 RB_GENERATE(domain_list
, domain
, entry
, domain_rb_cmp
);
688 download_rb_cmp(struct download
*e1
, struct download
*e2
)
690 return (e1
->id
< e2
->id
? -1 : e1
->id
> e2
->id
);
692 RB_GENERATE(download_list
, download
, entry
, download_rb_cmp
);
694 struct valid_url_types
{
705 valid_url_type(char *url
)
709 for (i
= 0; i
< LENGTH(vut
); i
++)
710 if (!strncasecmp(vut
[i
].type
, url
, strlen(vut
[i
].type
)))
717 match_alias(char *url_in
)
721 char *url_out
= NULL
, *search
, *enc_arg
;
723 search
= g_strdup(url_in
);
725 if (strsep(&arg
, " \t") == NULL
) {
726 show_oops(NULL
, "match_alias: NULL URL");
730 TAILQ_FOREACH(a
, &aliases
, entry
) {
731 if (!strcmp(search
, a
->a_name
))
736 DNPRINTF(XT_D_URL
, "match_alias: matched alias %s\n",
739 enc_arg
= soup_uri_encode(arg
, XT_RESERVED_CHARS
);
740 url_out
= g_strdup_printf(a
->a_uri
, enc_arg
);
743 url_out
= g_strdup_printf(a
->a_uri
, "");
751 guess_url_type(char *url_in
)
754 char *url_out
= NULL
, *enc_search
= NULL
;
759 /* substitute aliases */
760 url_out
= match_alias(url_in
);
764 /* see if we are an about page */
765 if (!strncmp(url_in
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
))
766 for (i
= 0; i
< about_list_size(); i
++)
767 if (!strcmp(&url_in
[XT_URI_ABOUT_LEN
],
768 about_list
[i
].name
)) {
769 url_out
= g_strdup(url_in
);
773 if (guess_search
&& url_regex
&&
774 !(g_str_has_prefix(url_in
, "http://") ||
775 g_str_has_prefix(url_in
, "https://"))) {
776 if (regexec(&url_re
, url_in
, 0, NULL
, 0)) {
777 /* invalid URI so search instead */
778 enc_search
= soup_uri_encode(url_in
, XT_RESERVED_CHARS
);
779 url_out
= g_strdup_printf(search_string
, enc_search
);
785 /* XXX not sure about this heuristic */
786 if (stat(url_in
, &sb
) == 0) {
787 if (url_in
[0] == '/')
788 url_out
= g_strdup_printf("file://%s", url_in
);
790 cwd
= malloc(PATH_MAX
);
791 if (getcwd(cwd
, PATH_MAX
) != NULL
) {
792 url_out
= g_strdup_printf("file://%s/%s",cwd
, url_in
);
797 url_out
= g_strdup_printf("http://%s", url_in
); /* guess http */
799 DNPRINTF(XT_D_URL
, "guess_url_type: guessed %s\n", url_out
);
805 load_uri(struct tab
*t
, gchar
*uri
)
808 gchar
*newuri
= NULL
;
814 /* Strip leading spaces. */
815 while (*uri
&& isspace(*uri
))
818 if (strlen(uri
) == 0) {
823 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
825 if (valid_url_type(uri
)) {
826 newuri
= guess_url_type(uri
);
830 if (!strncmp(uri
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
)) {
831 for (i
= 0; i
< about_list_size(); i
++)
832 if (!strcmp(&uri
[XT_URI_ABOUT_LEN
], about_list
[i
].name
)) {
833 bzero(&args
, sizeof args
);
834 about_list
[i
].func(t
, &args
);
835 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
),
839 show_oops(t
, "invalid about page");
843 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
845 webkit_web_view_load_uri(t
->wv
, uri
);
852 get_uri(struct tab
*t
)
854 const gchar
*uri
= NULL
;
856 if (webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
)
858 if (t
->xtp_meaning
== XT_XTP_TAB_MEANING_NORMAL
) {
859 uri
= webkit_web_view_get_uri(t
->wv
);
861 /* use tmp_uri to make sure it is g_freed */
864 t
->tmp_uri
=g_strdup_printf("%s%s", XT_URI_ABOUT
,
865 about_list
[t
->xtp_meaning
].name
);
872 get_title(struct tab
*t
, bool window
)
874 const gchar
*set
= NULL
, *title
= NULL
;
875 WebKitLoadStatus status
= webkit_web_view_get_load_status(t
->wv
);
877 if (status
== WEBKIT_LOAD_PROVISIONAL
|| status
== WEBKIT_LOAD_FAILED
||
878 t
->xtp_meaning
== XT_XTP_TAB_MEANING_BL
)
881 title
= webkit_web_view_get_title(t
->wv
);
882 if ((set
= title
? title
: get_uri(t
)))
886 set
= window
? XT_NAME
: "(untitled)";
892 find_mime_type(char *mime_type
)
894 struct mime_type
*m
, *def
= NULL
, *rv
= NULL
;
896 TAILQ_FOREACH(m
, &mtl
, entry
) {
898 !strncmp(mime_type
, m
->mt_type
, strlen(m
->mt_type
)))
901 if (m
->mt_default
== 0 && !strcmp(mime_type
, m
->mt_type
)) {
914 wl_find_uri(const gchar
*s
, struct domain_list
*wl
)
920 if (s
== NULL
|| wl
== NULL
)
923 if (!strncmp(s
, "http://", strlen("http://")))
924 s
= &s
[strlen("http://")];
925 else if (!strncmp(s
, "https://", strlen("https://")))
926 s
= &s
[strlen("https://")];
931 for (i
= 0; i
< strlen(s
) + 1 /* yes er need this */; i
++)
932 /* chop string at first slash */
933 if (s
[i
] == '/' || s
[i
] == ':' || s
[i
] == '\0') {
945 js_ref_to_string(JSContextRef context
, JSValueRef ref
)
951 jsref
= JSValueToStringCopy(context
, ref
, NULL
);
955 l
= JSStringGetMaximumUTF8CStringSize(jsref
);
958 JSStringGetUTF8CString(jsref
, s
, l
);
959 JSStringRelease(jsref
);
964 #define XT_JS_DONE ("done;")
965 #define XT_JS_DONE_LEN (strlen(XT_JS_DONE))
966 #define XT_JS_INSERT ("insert;")
967 #define XT_JS_INSERT_LEN (strlen(XT_JS_INSERT))
970 run_script(struct tab
*t
, char *s
)
972 JSGlobalContextRef ctx
;
973 WebKitWebFrame
*frame
;
975 JSValueRef val
, exception
;
978 DNPRINTF(XT_D_JS
, "%s: tab %d %s\n", __func__
,
979 t
->tab_id
, s
== (char *)JS_HINTING
? "JS_HINTING" : s
);
981 frame
= webkit_web_view_get_main_frame(t
->wv
);
982 ctx
= webkit_web_frame_get_global_context(frame
);
984 str
= JSStringCreateWithUTF8CString(s
);
985 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
986 NULL
, 0, &exception
);
987 JSStringRelease(str
);
989 DNPRINTF(XT_D_JS
, "%s: val %p\n", __func__
, val
);
991 es
= js_ref_to_string(ctx
, exception
);
993 DNPRINTF(XT_D_JS
, "%s: exception %s\n", __func__
, es
);
998 es
= js_ref_to_string(ctx
, val
);
1001 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
1003 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
1007 DNPRINTF(XT_D_JS
, "%s: val %s\n", __func__
, es
);
1016 enable_hints(struct tab
*t
)
1018 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
1021 run_script(t
, "hints.createHints('', 'F');");
1023 run_script(t
, "hints.createHints('', 'f');");
1024 t
->mode
= XT_MODE_HINT
;
1028 disable_hints(struct tab
*t
)
1030 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
1032 run_script(t
, "hints.clearHints();");
1033 t
->mode
= XT_MODE_COMMAND
;
1038 hint(struct tab
*t
, struct karg
*args
)
1041 DNPRINTF(XT_D_JS
, "hint: tab %d args %d\n", t
->tab_id
, args
->i
);
1043 if (t
->mode
== XT_MODE_HINT
) {
1044 if (args
->i
== XT_HINT_NEWTAB
)
1054 apply_style(struct tab
*t
)
1057 g_object_set(G_OBJECT(t
->settings
),
1058 "user-stylesheet-uri", t
->stylesheet
, (char *)NULL
);
1062 remove_style(struct tab
*t
)
1065 g_object_set(G_OBJECT(t
->settings
),
1066 "user-stylesheet-uri", NULL
, (char *)NULL
);
1070 userstyle(struct tab
*t
, struct karg
*args
)
1074 DNPRINTF(XT_D_JS
, "userstyle: tab %d\n", t
->tab_id
);
1077 case XT_STYLE_CURRENT_TAB
:
1083 case XT_STYLE_GLOBAL
:
1084 if (userstyle_global
) {
1085 userstyle_global
= 0;
1086 TAILQ_FOREACH(tt
, &tabs
, entry
)
1089 userstyle_global
= 1;
1090 TAILQ_FOREACH(tt
, &tabs
, entry
)
1100 quit(struct tab
*t
, struct karg
*args
)
1102 if (save_global_history
)
1103 save_global_history_to_disk(t
);
1111 restore_sessions_list(void)
1114 struct dirent
*dp
= NULL
;
1117 sdir
= opendir(sessions_dir
);
1119 while ((dp
= readdir(sdir
)) != NULL
)
1120 if (dp
->d_type
== DT_REG
) {
1121 s
= g_malloc(sizeof(struct session
));
1122 s
->name
= g_strdup(dp
->d_name
);
1123 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
1130 open_tabs(struct tab
*t
, struct karg
*a
)
1132 char file
[PATH_MAX
];
1136 struct tab
*ti
, *tt
;
1141 ti
= TAILQ_LAST(&tabs
, tab_list
);
1143 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, a
->s
);
1144 if ((f
= fopen(file
, "r")) == NULL
)
1148 if ((uri
= fparseln(f
, NULL
, NULL
, "\0\0\0", 0)) == NULL
)
1149 if (feof(f
) || ferror(f
))
1152 /* retrieve session name */
1153 if (uri
&& g_str_has_prefix(uri
, XT_SAVE_SESSION_ID
)) {
1154 strlcpy(named_session
,
1155 &uri
[strlen(XT_SAVE_SESSION_ID
)],
1156 sizeof named_session
);
1160 if (uri
&& strlen(uri
))
1161 create_new_tab(uri
, NULL
, 1, -1);
1167 /* close open tabs */
1168 if (a
->i
== XT_SES_CLOSETABS
&& ti
!= NULL
) {
1170 tt
= TAILQ_FIRST(&tabs
);
1191 restore_saved_tabs(void)
1193 char file
[PATH_MAX
];
1194 int unlink_file
= 0;
1199 snprintf(file
, sizeof file
, "%s/%s",
1200 sessions_dir
, XT_RESTART_TABS_FILE
);
1201 if (stat(file
, &sb
) == -1)
1202 a
.s
= XT_SAVED_TABS_FILE
;
1205 a
.s
= XT_RESTART_TABS_FILE
;
1208 a
.i
= XT_SES_DONOTHING
;
1209 rv
= open_tabs(NULL
, &a
);
1218 save_tabs(struct tab
*t
, struct karg
*a
)
1220 char file
[PATH_MAX
];
1222 int num_tabs
= 0, i
;
1223 struct tab
**stabs
= NULL
;
1225 /* tab may be null here */
1230 snprintf(file
, sizeof file
, "%s/%s",
1231 sessions_dir
, named_session
);
1233 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, a
->s
);
1235 if ((f
= fopen(file
, "w")) == NULL
) {
1236 show_oops(t
, "Can't open save_tabs file: %s", strerror(errno
));
1240 /* save session name */
1241 fprintf(f
, "%s%s\n", XT_SAVE_SESSION_ID
, named_session
);
1243 /* Save tabs, in the order they are arranged in the notebook. */
1244 num_tabs
= sort_tabs_by_page_num(&stabs
);
1246 for (i
= 0; i
< num_tabs
; i
++)
1248 if (get_uri(stabs
[i
]) != NULL
)
1249 fprintf(f
, "%s\n", get_uri(stabs
[i
]));
1250 else if (gtk_entry_get_text(GTK_ENTRY(
1251 stabs
[i
]->uri_entry
)))
1252 fprintf(f
, "%s\n", gtk_entry_get_text(GTK_ENTRY(
1253 stabs
[i
]->uri_entry
)));
1258 /* try and make sure this gets to disk NOW. XXX Backup first? */
1259 if (fflush(f
) != 0 || fsync(fileno(f
)) != 0) {
1260 show_oops(t
, "May not have managed to save session: %s",
1270 save_tabs_and_quit(struct tab
*t
, struct karg
*args
)
1282 run_page_script(struct tab
*t
, struct karg
*args
)
1285 char *tmp
, script
[PATH_MAX
];
1287 tmp
= args
->s
!= NULL
&& strlen(args
->s
) > 0 ? args
->s
: default_script
;
1288 if (tmp
[0] == '\0') {
1289 show_oops(t
, "no script specified");
1293 if ((uri
= get_uri(t
)) == NULL
) {
1294 show_oops(t
, "tab is empty, not running script");
1299 snprintf(script
, sizeof script
, "%s/%s",
1300 pwd
->pw_dir
, &tmp
[1]);
1302 strlcpy(script
, tmp
, sizeof script
);
1306 show_oops(t
, "can't fork to run script");
1316 execlp(script
, script
, uri
, (void *)NULL
);
1326 yank_uri(struct tab
*t
, struct karg
*args
)
1329 GtkClipboard
*clipboard
;
1331 if ((uri
= get_uri(t
)) == NULL
)
1334 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1335 gtk_clipboard_set_text(clipboard
, uri
, -1);
1341 paste_uri(struct tab
*t
, struct karg
*args
)
1343 GtkClipboard
*clipboard
;
1344 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
1346 gchar
*p
= NULL
, *uri
;
1348 /* try primary clipboard first */
1349 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1350 p
= gtk_clipboard_wait_for_text(clipboard
);
1352 /* if it failed get whatever text is in cut_buffer0 */
1353 if (p
== NULL
&& xterm_workaround
)
1354 if (gdk_property_get(gdk_get_default_root_window(),
1356 gdk_atom_intern("STRING", FALSE
),
1358 1024 * 1024 /* picked out of my butt */,
1364 /* yes sir, we need to NUL the string */
1370 while (*uri
&& isspace(*uri
))
1372 if (strlen(uri
) == 0) {
1373 show_oops(t
, "empty paste buffer");
1376 if (guess_search
== 0 && valid_url_type(uri
)) {
1377 /* we can be clever and paste this in search box */
1378 show_oops(t
, "not a valid URL");
1382 if (args
->i
== XT_PASTE_CURRENT_TAB
)
1384 else if (args
->i
== XT_PASTE_NEW_TAB
)
1385 create_new_tab(uri
, NULL
, 1, -1);
1396 js_toggle_cb(GtkWidget
*w
, struct tab
*t
)
1401 g_object_get(G_OBJECT(t
->settings
),
1402 "enable-scripts", &es
, (char *)NULL
);
1407 set
= XT_WL_DISABLE
;
1409 a
.i
= set
| XT_WL_TOPLEVEL
;
1412 a
.i
= set
| XT_WL_TOPLEVEL
;
1415 a
.i
= XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
;
1420 toggle_src(struct tab
*t
, struct karg
*args
)
1427 mode
= webkit_web_view_get_view_source_mode(t
->wv
);
1428 webkit_web_view_set_view_source_mode(t
->wv
, !mode
);
1429 webkit_web_view_reload(t
->wv
);
1435 focus_webview(struct tab
*t
)
1440 /* only grab focus if we are visible */
1441 if (gtk_notebook_get_current_page(notebook
) == t
->tab_id
)
1442 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
1446 focus(struct tab
*t
, struct karg
*args
)
1448 if (t
== NULL
|| args
== NULL
)
1454 if (args
->i
== XT_FOCUS_URI
)
1455 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
1456 else if (args
->i
== XT_FOCUS_SEARCH
)
1457 gtk_widget_grab_focus(GTK_WIDGET(t
->search_entry
));
1463 connect_socket_from_uri(const gchar
*uri
, const gchar
**error_str
, char *domain
,
1467 struct addrinfo hints
, *res
= NULL
, *ai
;
1468 int rv
= -1, s
= -1, on
, error
;
1470 static gchar myerror
[256]; /* this is not thread safe */
1473 *error_str
= myerror
;
1474 if (uri
&& !g_str_has_prefix(uri
, "https://")) {
1475 *error_str
= "invalid URI";
1479 su
= soup_uri_new(uri
);
1481 *error_str
= "invalid soup URI";
1484 if (!SOUP_URI_VALID_FOR_HTTP(su
)) {
1485 *error_str
= "invalid HTTPS URI";
1489 snprintf(port
, sizeof port
, "%d", su
->port
);
1490 bzero(&hints
, sizeof(struct addrinfo
));
1491 hints
.ai_flags
= AI_CANONNAME
;
1492 hints
.ai_family
= AF_UNSPEC
;
1493 hints
.ai_socktype
= SOCK_STREAM
;
1495 if ((error
= getaddrinfo(su
->host
, port
, &hints
, &res
))) {
1496 snprintf(myerror
, sizeof myerror
, "getaddrinfo failed: %s",
1497 gai_strerror(errno
));
1501 for (ai
= res
; ai
; ai
= ai
->ai_next
) {
1507 if (ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
)
1509 s
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
1512 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &on
,
1515 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) == 0)
1519 snprintf(myerror
, sizeof myerror
,
1520 "could not obtain certificates from: %s",
1526 strlcpy(domain
, su
->host
, domain_sz
);
1533 if (rv
== -1 && s
!= -1)
1540 stop_tls(gnutls_session_t gsession
, gnutls_certificate_credentials_t xcred
)
1543 gnutls_deinit(gsession
);
1545 gnutls_certificate_free_credentials(xcred
);
1551 start_tls(const gchar
**error_str
, int s
, gnutls_session_t
*gs
,
1552 gnutls_certificate_credentials_t
*xc
)
1554 gnutls_certificate_credentials_t xcred
;
1555 gnutls_session_t gsession
;
1557 static gchar myerror
[1024]; /* this is not thread safe */
1559 if (gs
== NULL
|| xc
== NULL
)
1566 gnutls_certificate_allocate_credentials(&xcred
);
1567 gnutls_certificate_set_x509_trust_file(xcred
, ssl_ca_file
,
1568 GNUTLS_X509_FMT_PEM
);
1570 gnutls_init(&gsession
, GNUTLS_CLIENT
);
1571 gnutls_priority_set_direct(gsession
, "PERFORMANCE", NULL
);
1572 gnutls_credentials_set(gsession
, GNUTLS_CRD_CERTIFICATE
, xcred
);
1573 gnutls_transport_set_ptr(gsession
, (gnutls_transport_ptr_t
)(long)s
);
1574 if ((rv
= gnutls_handshake(gsession
)) < 0) {
1575 snprintf(myerror
, sizeof myerror
,
1576 "gnutls_handshake failed %d fatal %d %s",
1578 gnutls_error_is_fatal(rv
),
1579 gnutls_strerror_name(rv
));
1580 stop_tls(gsession
, xcred
);
1584 gnutls_credentials_type_t cred
;
1585 cred
= gnutls_auth_get_type(gsession
);
1586 if (cred
!= GNUTLS_CRD_CERTIFICATE
) {
1587 snprintf(myerror
, sizeof myerror
,
1588 "gnutls_auth_get_type failed %d",
1590 stop_tls(gsession
, xcred
);
1598 *error_str
= myerror
;
1603 get_connection_certs(gnutls_session_t gsession
, gnutls_x509_crt_t
**certs
,
1607 const gnutls_datum_t
*cl
;
1608 gnutls_x509_crt_t
*all_certs
;
1611 if (certs
== NULL
|| cert_count
== NULL
)
1613 if (gnutls_certificate_type_get(gsession
) != GNUTLS_CRT_X509
)
1615 cl
= gnutls_certificate_get_peers(gsession
, &len
);
1619 all_certs
= g_malloc(sizeof(gnutls_x509_crt_t
) * len
);
1620 for (i
= 0; i
< len
; i
++) {
1621 gnutls_x509_crt_init(&all_certs
[i
]);
1622 if (gnutls_x509_crt_import(all_certs
[i
], &cl
[i
],
1623 GNUTLS_X509_FMT_PEM
< 0)) {
1637 free_connection_certs(gnutls_x509_crt_t
*certs
, size_t cert_count
)
1641 for (i
= 0; i
< cert_count
; i
++)
1642 gnutls_x509_crt_deinit(certs
[i
]);
1647 statusbar_modify_attr(struct tab
*t
, const char *text
, const char *base
)
1649 GdkColor c_text
, c_base
;
1651 gdk_color_parse(text
, &c_text
);
1652 gdk_color_parse(base
, &c_base
);
1654 gtk_widget_modify_text(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_text
);
1655 gtk_widget_modify_text(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_text
);
1656 gtk_widget_modify_text(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_text
);
1657 gtk_widget_modify_text(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_text
);
1659 gtk_widget_modify_base(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_base
);
1660 gtk_widget_modify_base(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_base
);
1661 gtk_widget_modify_base(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_base
);
1662 gtk_widget_modify_base(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_base
);
1666 save_certs(struct tab
*t
, gnutls_x509_crt_t
*certs
,
1667 size_t cert_count
, char *domain
)
1670 char cert_buf
[64 * 1024], file
[PATH_MAX
];
1675 if (t
== NULL
|| certs
== NULL
|| cert_count
<= 0 || domain
== NULL
)
1678 snprintf(file
, sizeof file
, "%s/%s", certs_dir
, domain
);
1679 if ((f
= fopen(file
, "w")) == NULL
) {
1680 show_oops(t
, "Can't create cert file %s %s",
1681 file
, strerror(errno
));
1685 for (i
= 0; i
< cert_count
; i
++) {
1686 cert_buf_sz
= sizeof cert_buf
;
1687 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1688 cert_buf
, &cert_buf_sz
)) {
1689 show_oops(t
, "gnutls_x509_crt_export failed");
1692 if (fwrite(cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1693 show_oops(t
, "Can't write certs: %s", strerror(errno
));
1698 /* not the best spot but oh well */
1699 gdk_color_parse("lightblue", &color
);
1700 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
1701 statusbar_modify_attr(t
, XT_COLOR_BLACK
, "lightblue");
1714 load_compare_cert(const gchar
*uri
, const gchar
**error_str
)
1716 char domain
[8182], file
[PATH_MAX
];
1717 char cert_buf
[64 * 1024], r_cert_buf
[64 * 1024];
1719 unsigned int error
= 0;
1721 size_t cert_buf_sz
, cert_count
;
1722 enum cert_trust rv
= CERT_UNTRUSTED
;
1723 static gchar serr
[80]; /* this isn't thread safe */
1724 gnutls_session_t gsession
;
1725 gnutls_x509_crt_t
*certs
;
1726 gnutls_certificate_credentials_t xcred
;
1728 DNPRINTF(XT_D_URL
, "%s: %s\n", __func__
, uri
);
1732 if ((s
= connect_socket_from_uri(uri
, error_str
, domain
,
1733 sizeof domain
)) == -1)
1736 DNPRINTF(XT_D_URL
, "%s: fd %d\n", __func__
, s
);
1739 if (start_tls(error_str
, s
, &gsession
, &xcred
))
1741 DNPRINTF(XT_D_URL
, "%s: got tls\n", __func__
);
1743 /* verify certs in case cert file doesn't exist */
1744 if (gnutls_certificate_verify_peers2(gsession
, &error
) !=
1746 *error_str
= "Invalid certificates";
1751 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1752 *error_str
= "Can't get connection certificates";
1756 snprintf(file
, sizeof file
, "%s/%s", certs_dir
, domain
);
1757 if ((f
= fopen(file
, "r")) == NULL
) {
1763 for (i
= 0; i
< cert_count
; i
++) {
1764 cert_buf_sz
= sizeof cert_buf
;
1765 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1766 cert_buf
, &cert_buf_sz
)) {
1769 if (fread(r_cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1770 rv
= CERT_BAD
; /* critical */
1773 if (bcmp(r_cert_buf
, cert_buf
, sizeof cert_buf_sz
)) {
1774 rv
= CERT_BAD
; /* critical */
1783 free_connection_certs(certs
, cert_count
);
1785 /* we close the socket first for speed */
1789 /* only complain if we didn't save it locally */
1790 if (error
&& rv
!= CERT_LOCAL
) {
1791 strlcpy(serr
, "Certificate exception(s): ", sizeof serr
);
1792 if (error
& GNUTLS_CERT_INVALID
)
1793 strlcat(serr
, "invalid, ", sizeof serr
);
1794 if (error
& GNUTLS_CERT_REVOKED
)
1795 strlcat(serr
, "revoked, ", sizeof serr
);
1796 if (error
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
1797 strlcat(serr
, "signer not found, ", sizeof serr
);
1798 if (error
& GNUTLS_CERT_SIGNER_NOT_CA
)
1799 strlcat(serr
, "not signed by CA, ", sizeof serr
);
1800 if (error
& GNUTLS_CERT_INSECURE_ALGORITHM
)
1801 strlcat(serr
, "insecure algorithm, ", sizeof serr
);
1802 if (error
& GNUTLS_CERT_NOT_ACTIVATED
)
1803 strlcat(serr
, "not activated, ", sizeof serr
);
1804 if (error
& GNUTLS_CERT_EXPIRED
)
1805 strlcat(serr
, "expired, ", sizeof serr
);
1806 for (i
= strlen(serr
) - 1; i
> 0; i
--)
1807 if (serr
[i
] == ',') {
1814 stop_tls(gsession
, xcred
);
1820 cert_cmd(struct tab
*t
, struct karg
*args
)
1822 const gchar
*uri
, *error_str
= NULL
;
1826 gnutls_session_t gsession
;
1827 gnutls_x509_crt_t
*certs
;
1828 gnutls_certificate_credentials_t xcred
;
1833 if (ssl_ca_file
== NULL
) {
1834 show_oops(t
, "Can't open CA file: %s", ssl_ca_file
);
1838 if ((uri
= get_uri(t
)) == NULL
) {
1839 show_oops(t
, "Invalid URI");
1843 if ((s
= connect_socket_from_uri(uri
, &error_str
, domain
,
1844 sizeof domain
)) == -1) {
1845 show_oops(t
, "%s", error_str
);
1850 if (start_tls(&error_str
, s
, &gsession
, &xcred
))
1854 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1855 show_oops(t
, "get_connection_certs failed");
1859 if (args
->i
& XT_SHOW
)
1860 show_certs(t
, certs
, cert_count
, "Certificate Chain");
1861 else if (args
->i
& XT_SAVE
)
1862 save_certs(t
, certs
, cert_count
, domain
);
1864 free_connection_certs(certs
, cert_count
);
1866 /* we close the socket first for speed */
1869 stop_tls(gsession
, xcred
);
1870 if (error_str
&& strlen(error_str
))
1871 show_oops(t
, "%s", error_str
);
1876 remove_cookie(int index
)
1882 DNPRINTF(XT_D_COOKIE
, "remove_cookie: %d\n", index
);
1884 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
1886 for (i
= 1; cf
; cf
= cf
->next
, i
++) {
1890 print_cookie("remove cookie", c
);
1891 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
1896 soup_cookies_free(cf
);
1902 remove_cookie_domain(int domain_id
)
1904 int domain_count
, rv
= 1;
1909 DNPRINTF(XT_D_COOKIE
, "remove_cookie_domain: %d\n", domain_id
);
1912 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
1914 for (domain_count
= 0; cf
; cf
= cf
->next
) {
1917 if (strcmp(last_domain
, c
->domain
) != 0) {
1919 last_domain
= c
->domain
;
1922 if (domain_count
< domain_id
)
1924 else if (domain_count
> domain_id
)
1927 print_cookie("remove cookie", c
);
1928 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
1932 soup_cookies_free(cf
);
1938 toplevel_cmd(struct tab
*t
, struct karg
*args
)
1940 js_toggle_cb(t
->js_toggle
, t
);
1946 can_go_back_for_real(struct tab
*t
)
1949 WebKitWebHistoryItem
*item
;
1955 /* rely on webkit to make sure we can go backward when on an about page */
1957 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
1958 return (webkit_web_view_can_go_back(t
->wv
));
1960 /* the back/forwars list is stupid so help determine if we can go back */
1961 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
1963 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
1964 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
1972 can_go_forward_for_real(struct tab
*t
)
1975 WebKitWebHistoryItem
*item
;
1981 /* rely on webkit to make sure we can go forward when on an about page */
1983 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
1984 return (webkit_web_view_can_go_forward(t
->wv
));
1986 /* the back/forwars list is stupid so help selecting a different item */
1987 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
1989 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
1990 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
1998 go_back_for_real(struct tab
*t
)
2001 WebKitWebHistoryItem
*item
;
2009 webkit_web_view_go_back(t
->wv
);
2012 /* the back/forwars list is stupid so help selecting a different item */
2013 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2015 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2016 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2017 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2024 go_forward_for_real(struct tab
*t
)
2027 WebKitWebHistoryItem
*item
;
2035 webkit_web_view_go_forward(t
->wv
);
2038 /* the back/forwars list is stupid so help selecting a different item */
2039 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2041 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2042 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2043 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2050 navaction(struct tab
*t
, struct karg
*args
)
2052 WebKitWebHistoryItem
*item
;
2053 WebKitWebFrame
*frame
;
2055 DNPRINTF(XT_D_NAV
, "navaction: tab %d opcode %d\n",
2056 t
->tab_id
, args
->i
);
2058 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
2060 if (args
->i
== XT_NAV_BACK
)
2061 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2063 item
= webkit_web_back_forward_list_get_forward_item(t
->bfl
);
2065 return (XT_CB_PASSTHROUGH
);
2066 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2068 return (XT_CB_PASSTHROUGH
);
2074 go_back_for_real(t
);
2076 case XT_NAV_FORWARD
:
2078 go_forward_for_real(t
);
2081 frame
= webkit_web_view_get_main_frame(t
->wv
);
2082 webkit_web_frame_reload(frame
);
2085 frame
= webkit_web_view_get_main_frame(t
->wv
);
2086 webkit_web_frame_stop_loading(frame
);
2089 return (XT_CB_PASSTHROUGH
);
2093 move(struct tab
*t
, struct karg
*args
)
2095 GtkAdjustment
*adjust
;
2096 double pi
, si
, pos
, ps
, upper
, lower
, max
;
2102 case XT_MOVE_BOTTOM
:
2104 case XT_MOVE_PAGEDOWN
:
2105 case XT_MOVE_PAGEUP
:
2106 case XT_MOVE_HALFDOWN
:
2107 case XT_MOVE_HALFUP
:
2108 case XT_MOVE_PERCENT
:
2109 case XT_MOVE_CENTER
:
2110 adjust
= t
->adjust_v
;
2113 adjust
= t
->adjust_h
;
2117 pos
= gtk_adjustment_get_value(adjust
);
2118 ps
= gtk_adjustment_get_page_size(adjust
);
2119 upper
= gtk_adjustment_get_upper(adjust
);
2120 lower
= gtk_adjustment_get_lower(adjust
);
2121 si
= gtk_adjustment_get_step_increment(adjust
);
2122 pi
= gtk_adjustment_get_page_increment(adjust
);
2125 DNPRINTF(XT_D_MOVE
, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2126 "max %f si %f pi %f\n",
2127 args
->i
, adjust
== t
->adjust_h
? "horizontal" : "vertical",
2128 pos
, ps
, upper
, lower
, max
, si
, pi
);
2134 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2139 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2141 case XT_MOVE_BOTTOM
:
2142 case XT_MOVE_FARRIGHT
:
2143 gtk_adjustment_set_value(adjust
, max
);
2146 case XT_MOVE_FARLEFT
:
2147 gtk_adjustment_set_value(adjust
, lower
);
2149 case XT_MOVE_PAGEDOWN
:
2151 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2153 case XT_MOVE_PAGEUP
:
2155 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2157 case XT_MOVE_HALFDOWN
:
2159 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2161 case XT_MOVE_HALFUP
:
2163 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2165 case XT_MOVE_CENTER
:
2166 args
->s
= g_strdup("50.0");
2168 case XT_MOVE_PERCENT
:
2169 percent
= atoi(args
->s
) / 100.0;
2170 pos
= max
* percent
;
2171 if (pos
< 0.0 || pos
> max
)
2173 gtk_adjustment_set_value(adjust
, pos
);
2176 return (XT_CB_PASSTHROUGH
);
2179 DNPRINTF(XT_D_MOVE
, "move: new pos %f %f\n", pos
, MIN(pos
, max
));
2181 return (XT_CB_HANDLED
);
2185 url_set_visibility(void)
2189 TAILQ_FOREACH(t
, &tabs
, entry
)
2190 if (show_url
== 0) {
2191 gtk_widget_hide(t
->toolbar
);
2194 gtk_widget_show(t
->toolbar
);
2198 notebook_tab_set_visibility(void)
2200 if (show_tabs
== 0) {
2201 gtk_widget_hide(tab_bar
);
2202 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2204 if (tab_style
== XT_TABS_NORMAL
) {
2205 gtk_widget_hide(tab_bar
);
2206 gtk_notebook_set_show_tabs(notebook
, TRUE
);
2207 } else if (tab_style
== XT_TABS_COMPACT
) {
2208 gtk_widget_show(tab_bar
);
2209 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2215 statusbar_set_visibility(void)
2219 TAILQ_FOREACH(t
, &tabs
, entry
){
2220 if (show_statusbar
== 0)
2221 gtk_widget_hide(t
->statusbar_box
);
2223 gtk_widget_show(t
->statusbar_box
);
2230 url_set(struct tab
*t
, int enable_url_entry
)
2235 show_url
= enable_url_entry
;
2237 if (enable_url_entry
) {
2238 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
2239 GTK_ENTRY_ICON_PRIMARY
, NULL
);
2240 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
), 0);
2242 pixbuf
= gtk_entry_get_icon_pixbuf(GTK_ENTRY(t
->uri_entry
),
2243 GTK_ENTRY_ICON_PRIMARY
);
2245 gtk_entry_get_progress_fraction(GTK_ENTRY(t
->uri_entry
));
2246 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
2247 GTK_ENTRY_ICON_PRIMARY
, pixbuf
);
2248 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
2254 fullscreen(struct tab
*t
, struct karg
*args
)
2256 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2259 return (XT_CB_PASSTHROUGH
);
2261 if (show_url
== 0) {
2269 url_set_visibility();
2270 notebook_tab_set_visibility();
2272 return (XT_CB_HANDLED
);
2276 statustoggle(struct tab
*t
, struct karg
*args
)
2278 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2280 if (show_statusbar
== 1) {
2282 statusbar_set_visibility();
2283 } else if (show_statusbar
== 0) {
2285 statusbar_set_visibility();
2287 return (XT_CB_HANDLED
);
2291 urlaction(struct tab
*t
, struct karg
*args
)
2293 int rv
= XT_CB_HANDLED
;
2295 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2298 return (XT_CB_PASSTHROUGH
);
2302 if (show_url
== 0) {
2304 url_set_visibility();
2308 if (show_url
== 1) {
2310 url_set_visibility();
2318 tabaction(struct tab
*t
, struct karg
*args
)
2320 int rv
= XT_CB_HANDLED
;
2321 char *url
= args
->s
;
2325 DNPRINTF(XT_D_TAB
, "tabaction: %p %d\n", t
, args
->i
);
2328 return (XT_CB_PASSTHROUGH
);
2332 if (strlen(url
) > 0)
2333 create_new_tab(url
, NULL
, 1, args
->precount
);
2335 create_new_tab(NULL
, NULL
, 1, args
->precount
);
2338 if (args
->precount
< 0)
2341 TAILQ_FOREACH(tt
, &tabs
, entry
)
2342 if (tt
->tab_id
== args
->precount
- 1) {
2347 case XT_TAB_DELQUIT
:
2348 if (gtk_notebook_get_n_pages(notebook
) > 1)
2354 if (strlen(url
) > 0)
2357 rv
= XT_CB_PASSTHROUGH
;
2363 if (show_tabs
== 0) {
2365 notebook_tab_set_visibility();
2369 if (show_tabs
== 1) {
2371 notebook_tab_set_visibility();
2374 case XT_TAB_NEXTSTYLE
:
2375 if (tab_style
== XT_TABS_NORMAL
) {
2376 tab_style
= XT_TABS_COMPACT
;
2377 recolor_compact_tabs();
2380 tab_style
= XT_TABS_NORMAL
;
2381 notebook_tab_set_visibility();
2383 case XT_TAB_UNDO_CLOSE
:
2384 if (undo_count
== 0) {
2385 DNPRINTF(XT_D_TAB
, "%s: no tabs to undo close",
2390 u
= TAILQ_FIRST(&undos
);
2391 create_new_tab(u
->uri
, u
, 1, -1);
2393 TAILQ_REMOVE(&undos
, u
, entry
);
2395 /* u->history is freed in create_new_tab() */
2399 case XT_TAB_LOAD_IMAGES
:
2401 if (!auto_load_images
) {
2403 /* Enable auto-load images (this will load all
2404 * previously unloaded images). */
2405 g_object_set(G_OBJECT(t
->settings
),
2406 "auto-load-images", TRUE
, (char *)NULL
);
2407 webkit_web_view_set_settings(t
->wv
, t
->settings
);
2409 webkit_web_view_reload(t
->wv
);
2411 /* Webkit triggers an event when we change the setting,
2412 * so we can't disable the auto-loading at once.
2414 * Unfortunately, webkit does not tell us when it's done.
2415 * Instead, we wait until the next request, and then
2416 * disable autoloading again.
2418 t
->load_images
= TRUE
;
2422 rv
= XT_CB_PASSTHROUGH
;
2436 resizetab(struct tab
*t
, struct karg
*args
)
2438 if (t
== NULL
|| args
== NULL
) {
2439 show_oops(NULL
, "resizetab invalid parameters");
2440 return (XT_CB_PASSTHROUGH
);
2443 DNPRINTF(XT_D_TAB
, "resizetab: tab %d %d\n",
2444 t
->tab_id
, args
->i
);
2446 setzoom_webkit(t
, args
->i
);
2448 return (XT_CB_HANDLED
);
2452 movetab(struct tab
*t
, struct karg
*args
)
2456 if (t
== NULL
|| args
== NULL
) {
2457 show_oops(NULL
, "movetab invalid parameters");
2458 return (XT_CB_PASSTHROUGH
);
2461 DNPRINTF(XT_D_TAB
, "movetab: tab %d opcode %d\n",
2462 t
->tab_id
, args
->i
);
2464 if (args
->i
>= XT_TAB_INVALID
)
2465 return (XT_CB_PASSTHROUGH
);
2467 if (TAILQ_EMPTY(&tabs
))
2468 return (XT_CB_PASSTHROUGH
);
2470 n
= gtk_notebook_get_n_pages(notebook
);
2471 dest
= gtk_notebook_get_current_page(notebook
);
2475 if (args
->precount
< 0)
2476 dest
= dest
== n
- 1 ? 0 : dest
+ 1;
2478 dest
= args
->precount
- 1;
2482 if (args
->precount
< 0)
2485 dest
-= args
->precount
% n
;
2498 return (XT_CB_PASSTHROUGH
);
2501 if (dest
< 0 || dest
>= n
)
2502 return (XT_CB_PASSTHROUGH
);
2503 if (t
->tab_id
== dest
) {
2504 DNPRINTF(XT_D_TAB
, "movetab: do nothing\n");
2505 return (XT_CB_HANDLED
);
2508 set_current_tab(dest
);
2510 return (XT_CB_HANDLED
);
2517 command(struct tab
*t
, struct karg
*args
)
2519 char *s
= NULL
, *ss
= NULL
;
2524 if (t
== NULL
|| args
== NULL
) {
2525 show_oops(NULL
, "command invalid parameters");
2526 return (XT_CB_PASSTHROUGH
);
2537 if (cmd_prefix
== 0)
2540 ss
= g_strdup_printf(":%d", cmd_prefix
);
2546 t
->mode
= XT_MODE_HINT
;
2547 bzero(&a
, sizeof a
);
2553 t
->mode
= XT_MODE_HINT
;
2554 bzero(&a
, sizeof a
);
2555 a
.i
= XT_HINT_NEWTAB
;
2565 case XT_CMD_OPEN_CURRENT
:
2568 case XT_CMD_TABNEW_CURRENT
:
2569 if (!s
) /* FALL THROUGH? */
2571 if ((uri
= get_uri(t
)) != NULL
) {
2572 ss
= g_strdup_printf("%s%s", s
, uri
);
2577 show_oops(t
, "command: invalid opcode %d", args
->i
);
2578 return (XT_CB_PASSTHROUGH
);
2581 DNPRINTF(XT_D_CMD
, "%s: tab %d type %s\n", __func__
, t
->tab_id
, s
);
2583 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), s
);
2584 gdk_color_parse(XT_COLOR_WHITE
, &color
);
2585 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
2587 gtk_widget_grab_focus(GTK_WIDGET(t
->cmd
));
2588 gtk_editable_set_position(GTK_EDITABLE(t
->cmd
), -1);
2593 return (XT_CB_HANDLED
);
2597 search(struct tab
*t
, struct karg
*args
)
2601 if (t
== NULL
|| args
== NULL
) {
2602 show_oops(NULL
, "search invalid parameters");
2607 case XT_SEARCH_NEXT
:
2608 d
= t
->search_forward
;
2610 case XT_SEARCH_PREV
:
2611 d
= !t
->search_forward
;
2614 return (XT_CB_PASSTHROUGH
);
2617 if (t
->search_text
== NULL
) {
2618 if (global_search
== NULL
)
2619 return (XT_CB_PASSTHROUGH
);
2621 d
= t
->search_forward
= TRUE
;
2622 t
->search_text
= g_strdup(global_search
);
2623 webkit_web_view_mark_text_matches(t
->wv
, global_search
, FALSE
, 0);
2624 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
2628 DNPRINTF(XT_D_CMD
, "search: tab %d opc %d forw %d text %s\n",
2629 t
->tab_id
, args
->i
, t
->search_forward
, t
->search_text
);
2631 webkit_web_view_search_text(t
->wv
, t
->search_text
, FALSE
, d
, TRUE
);
2633 return (XT_CB_HANDLED
);
2637 session_save(struct tab
*t
, char *filename
)
2643 if (strlen(filename
) == 0)
2646 if (filename
[0] == '.' || filename
[0] == '/')
2650 if (save_tabs(t
, &a
))
2652 strlcpy(named_session
, filename
, sizeof named_session
);
2654 /* add the new session to the list of sessions */
2655 s
= g_malloc(sizeof(struct session
));
2656 s
->name
= g_strdup(filename
);
2657 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
2665 session_open(struct tab
*t
, char *filename
)
2670 if (strlen(filename
) == 0)
2673 if (filename
[0] == '.' || filename
[0] == '/')
2677 a
.i
= XT_SES_CLOSETABS
;
2678 if (open_tabs(t
, &a
))
2681 strlcpy(named_session
, filename
, sizeof named_session
);
2689 session_delete(struct tab
*t
, char *filename
)
2691 char file
[PATH_MAX
];
2695 if (strlen(filename
) == 0)
2698 if (filename
[0] == '.' || filename
[0] == '/')
2701 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, filename
);
2705 if (!strcmp(filename
, named_session
))
2706 strlcpy(named_session
, XT_SAVED_TABS_FILE
,
2707 sizeof named_session
);
2709 /* remove session from sessions list */
2710 TAILQ_FOREACH(s
, &sessions
, entry
) {
2711 if (!strcmp(s
->name
, filename
))
2716 TAILQ_REMOVE(&sessions
, s
, entry
);
2717 g_free((gpointer
) s
->name
);
2726 session_cmd(struct tab
*t
, struct karg
*args
)
2728 char *filename
= args
->s
;
2733 if (args
->i
& XT_SHOW
)
2734 show_oops(t
, "Current session: %s", named_session
[0] == '\0' ?
2735 XT_SAVED_TABS_FILE
: named_session
);
2736 else if (args
->i
& XT_SAVE
) {
2737 if (session_save(t
, filename
)) {
2738 show_oops(t
, "Can't save session: %s",
2739 filename
? filename
: "INVALID");
2742 } else if (args
->i
& XT_OPEN
) {
2743 if (session_open(t
, filename
)) {
2744 show_oops(t
, "Can't open session: %s",
2745 filename
? filename
: "INVALID");
2748 } else if (args
->i
& XT_DELETE
) {
2749 if (session_delete(t
, filename
)) {
2750 show_oops(t
, "Can't delete session: %s",
2751 filename
? filename
: "INVALID");
2756 return (XT_CB_PASSTHROUGH
);
2760 script_cmd(struct tab
*t
, struct karg
*args
)
2769 if ((f
= fopen(args
->s
, "r")) == NULL
) {
2770 show_oops(t
, "Can't open script file: %s", args
->s
);
2774 if (fstat(fileno(f
), &sb
) == -1) {
2775 show_oops(t
, "Can't stat script file: %s", args
->s
);
2779 buf
= g_malloc0(sb
.st_size
+ 1);
2780 if (fread(buf
, 1, sb
.st_size
, f
) != sb
.st_size
) {
2781 show_oops(t
, "Can't read script file: %s", args
->s
);
2785 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
2794 return (XT_CB_PASSTHROUGH
);
2798 * Make a hardcopy of the page
2801 print_page(struct tab
*t
, struct karg
*args
)
2803 WebKitWebFrame
*frame
;
2805 GtkPrintOperation
*op
;
2806 GtkPrintOperationAction action
;
2807 GtkPrintOperationResult print_res
;
2808 GError
*g_err
= NULL
;
2809 int marg_l
, marg_r
, marg_t
, marg_b
;
2811 DNPRINTF(XT_D_PRINTING
, "%s:", __func__
);
2813 ps
= gtk_page_setup_new();
2814 op
= gtk_print_operation_new();
2815 action
= GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
;
2816 frame
= webkit_web_view_get_main_frame(t
->wv
);
2818 /* the default margins are too small, so we will bump them */
2819 marg_l
= gtk_page_setup_get_left_margin(ps
, GTK_UNIT_MM
) +
2820 XT_PRINT_EXTRA_MARGIN
;
2821 marg_r
= gtk_page_setup_get_right_margin(ps
, GTK_UNIT_MM
) +
2822 XT_PRINT_EXTRA_MARGIN
;
2823 marg_t
= gtk_page_setup_get_top_margin(ps
, GTK_UNIT_MM
) +
2824 XT_PRINT_EXTRA_MARGIN
;
2825 marg_b
= gtk_page_setup_get_bottom_margin(ps
, GTK_UNIT_MM
) +
2826 XT_PRINT_EXTRA_MARGIN
;
2829 gtk_page_setup_set_left_margin(ps
, marg_l
, GTK_UNIT_MM
);
2830 gtk_page_setup_set_right_margin(ps
, marg_r
, GTK_UNIT_MM
);
2831 gtk_page_setup_set_top_margin(ps
, marg_t
, GTK_UNIT_MM
);
2832 gtk_page_setup_set_bottom_margin(ps
, marg_b
, GTK_UNIT_MM
);
2834 gtk_print_operation_set_default_page_setup(op
, ps
);
2836 /* this appears to free 'op' and 'ps' */
2837 print_res
= webkit_web_frame_print_full(frame
, op
, action
, &g_err
);
2839 /* check it worked */
2840 if (print_res
== GTK_PRINT_OPERATION_RESULT_ERROR
) {
2841 show_oops(NULL
, "can't print: %s", g_err
->message
);
2842 g_error_free (g_err
);
2850 go_home(struct tab
*t
, struct karg
*args
)
2857 set_encoding(struct tab
*t
, struct karg
*args
)
2861 if (args
->s
&& strlen(g_strstrip(args
->s
)) == 0) {
2862 e
= webkit_web_view_get_custom_encoding(t
->wv
);
2864 e
= webkit_web_view_get_encoding(t
->wv
);
2865 show_oops(t
, "encoding: %s", e
? e
: "N/A");
2867 webkit_web_view_set_custom_encoding(t
->wv
, args
->s
);
2873 restart(struct tab
*t
, struct karg
*args
)
2877 a
.s
= XT_RESTART_TABS_FILE
;
2879 execvp(start_argv
[0], start_argv
);
2885 char *http_proxy_save
; /* not a setting, used to toggle */
2888 proxy_cmd(struct tab
*t
, struct karg
*args
)
2890 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
2897 if (http_proxy_save
)
2898 g_free(http_proxy_save
);
2899 http_proxy_save
= g_strdup(http_proxy
);
2902 if (args
->i
& XT_PRXY_SHOW
) {
2904 show_oops(t
, "http_proxy = %s", http_proxy
);
2906 show_oops(t
, "proxy is currently disabled");
2907 } else if (args
->i
& XT_PRXY_TOGGLE
) {
2908 if (http_proxy_save
== NULL
&& http_proxy
== NULL
) {
2909 show_oops(t
, "can't toggle proxy");
2915 setup_proxy(http_proxy_save
);
2918 return (XT_CB_PASSTHROUGH
);
2923 int (*func
)(struct tab
*, struct karg
*);
2927 { "command_mode", 0, command_mode
, XT_MODE_COMMAND
, 0 },
2928 { "insert_mode", 0, command_mode
, XT_MODE_INSERT
, 0 },
2929 { "command", 0, command
, ':', 0 },
2930 { "search", 0, command
, '/', 0 },
2931 { "searchb", 0, command
, '?', 0 },
2932 { "hinting", 0, command
, '.', 0 },
2933 { "hinting_newtab", 0, command
, ',', 0 },
2934 { "togglesrc", 0, toggle_src
, 0, 0 },
2935 { "editsrc", 0, edit_src
, 0, 0 },
2936 { "editelement", 0, edit_element
, 0, 0 },
2938 /* yanking and pasting */
2939 { "yankuri", 0, yank_uri
, 0, 0 },
2940 /* XXX: pasteuri{cur,new} do not work from the cmd_entry? */
2941 { "pasteuricur", 0, paste_uri
, XT_PASTE_CURRENT_TAB
, 0 },
2942 { "pasteurinew", 0, paste_uri
, XT_PASTE_NEW_TAB
, 0 },
2945 { "searchnext", 0, search
, XT_SEARCH_NEXT
, 0 },
2946 { "searchprevious", 0, search
, XT_SEARCH_PREV
, 0 },
2949 { "focusaddress", 0, focus
, XT_FOCUS_URI
, 0 },
2950 { "focussearch", 0, focus
, XT_FOCUS_SEARCH
, 0 },
2953 { "hinting", 0, hint
, 0, 0 },
2954 { "hinting_newtab", 0, hint
, XT_HINT_NEWTAB
, 0 },
2956 /* custom stylesheet */
2957 { "userstyle", 0, userstyle
, XT_STYLE_CURRENT_TAB
, 0 },
2958 { "userstyle_global", 0, userstyle
, XT_STYLE_GLOBAL
, 0 },
2961 { "goback", 0, navaction
, XT_NAV_BACK
, 0 },
2962 { "goforward", 0, navaction
, XT_NAV_FORWARD
, 0 },
2963 { "reload", 0, navaction
, XT_NAV_RELOAD
, 0 },
2964 { "stop", 0, navaction
, XT_NAV_STOP
, 0 },
2966 /* vertical movement */
2967 { "scrolldown", 0, move
, XT_MOVE_DOWN
, 0 },
2968 { "scrollup", 0, move
, XT_MOVE_UP
, 0 },
2969 { "scrollbottom", 0, move
, XT_MOVE_BOTTOM
, 0 },
2970 { "scrolltop", 0, move
, XT_MOVE_TOP
, 0 },
2971 { "1", 0, move
, XT_MOVE_TOP
, 0 },
2972 { "scrollhalfdown", 0, move
, XT_MOVE_HALFDOWN
, 0 },
2973 { "scrollhalfup", 0, move
, XT_MOVE_HALFUP
, 0 },
2974 { "scrollpagedown", 0, move
, XT_MOVE_PAGEDOWN
, 0 },
2975 { "scrollpageup", 0, move
, XT_MOVE_PAGEUP
, 0 },
2976 /* horizontal movement */
2977 { "scrollright", 0, move
, XT_MOVE_RIGHT
, 0 },
2978 { "scrollleft", 0, move
, XT_MOVE_LEFT
, 0 },
2979 { "scrollfarright", 0, move
, XT_MOVE_FARRIGHT
, 0 },
2980 { "scrollfarleft", 0, move
, XT_MOVE_FARLEFT
, 0 },
2982 { "favorites", 0, xtp_page_fl
, 0, 0 },
2983 { "fav", 0, xtp_page_fl
, 0, 0 },
2984 { "favadd", 0, add_favorite
, 0, 0 },
2986 { "qall", 0, quit
, 0, 0 },
2987 { "quitall", 0, quit
, 0, 0 },
2988 { "w", 0, save_tabs
, 0, 0 },
2989 { "wq", 0, save_tabs_and_quit
, 0, 0 },
2990 { "help", 0, help
, 0, 0 },
2991 { "about", 0, about
, 0, 0 },
2992 { "stats", 0, stats
, 0, 0 },
2993 { "version", 0, about
, 0, 0 },
2996 { "js", 0, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2997 { "save", 1, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
2998 { "domain", 2, js_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
2999 { "fqdn", 2, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3000 { "show", 1, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3001 { "all", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3002 { "persistent", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3003 { "session", 2, js_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3004 { "toggle", 1, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3005 { "domain", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3006 { "fqdn", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3008 /* cookie command */
3009 { "cookie", 0, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3010 { "save", 1, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3011 { "domain", 2, cookie_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3012 { "fqdn", 2, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3013 { "show", 1, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3014 { "all", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3015 { "persistent", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3016 { "session", 2, cookie_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3017 { "toggle", 1, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3018 { "domain", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3019 { "fqdn", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3021 /* plugin command */
3022 { "plugin", 0, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3023 { "save", 1, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3024 { "domain", 2, pl_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3025 { "fqdn", 2, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3026 { "show", 1, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3027 { "all", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3028 { "persistent", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3029 { "session", 2, pl_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3030 { "toggle", 1, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3031 { "domain", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3032 { "fqdn", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3034 /* toplevel (domain) command */
3035 { "toplevel", 0, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3036 { "toggle", 1, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3039 { "cookiejar", 0, xtp_page_cl
, 0, 0 },
3042 { "cert", 0, cert_cmd
, XT_SHOW
, 0 },
3043 { "save", 1, cert_cmd
, XT_SAVE
, 0 },
3044 { "show", 1, cert_cmd
, XT_SHOW
, 0 },
3046 { "ca", 0, ca_cmd
, 0, 0 },
3047 { "downloadmgr", 0, xtp_page_dl
, 0, 0 },
3048 { "dl", 0, xtp_page_dl
, 0, 0 },
3049 { "h", 0, xtp_page_hl
, 0, 0 },
3050 { "history", 0, xtp_page_hl
, 0, 0 },
3051 { "home", 0, go_home
, 0, 0 },
3052 { "restart", 0, restart
, 0, 0 },
3053 { "urlhide", 0, urlaction
, XT_URL_HIDE
, 0 },
3054 { "urlshow", 0, urlaction
, XT_URL_SHOW
, 0 },
3055 { "statustoggle", 0, statustoggle
, 0, 0 },
3056 { "run_script", 0, run_page_script
, 0, XT_USERARG
},
3058 { "print", 0, print_page
, 0, 0 },
3061 { "focusin", 0, resizetab
, XT_ZOOM_IN
, 0 },
3062 { "focusout", 0, resizetab
, XT_ZOOM_OUT
, 0 },
3063 { "focusreset", 0, resizetab
, XT_ZOOM_NORMAL
, 0 },
3064 { "q", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3065 { "quit", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3066 { "open", 0, tabaction
, XT_TAB_OPEN
, XT_URLARG
},
3067 { "tabclose", 0, tabaction
, XT_TAB_DELETE
, XT_PREFIX
| XT_INTARG
},
3068 { "tabedit", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3069 { "tabfirst", 0, movetab
, XT_TAB_FIRST
, 0 },
3070 { "tabhide", 0, tabaction
, XT_TAB_HIDE
, 0 },
3071 { "tablast", 0, movetab
, XT_TAB_LAST
, 0 },
3072 { "tabnew", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3073 { "tabnext", 0, movetab
, XT_TAB_NEXT
, XT_PREFIX
| XT_INTARG
},
3074 { "tabnextstyle", 0, tabaction
, XT_TAB_NEXTSTYLE
, 0 },
3075 { "tabprevious", 0, movetab
, XT_TAB_PREV
, XT_PREFIX
| XT_INTARG
},
3076 { "tabrewind", 0, movetab
, XT_TAB_FIRST
, 0 },
3077 { "tabshow", 0, tabaction
, XT_TAB_SHOW
, 0 },
3078 { "tabs", 0, buffers
, 0, 0 },
3079 { "tabundoclose", 0, tabaction
, XT_TAB_UNDO_CLOSE
, 0 },
3080 { "buffers", 0, buffers
, 0, 0 },
3081 { "ls", 0, buffers
, 0, 0 },
3082 { "encoding", 0, set_encoding
, 0, XT_USERARG
},
3083 { "loadimages", 0, tabaction
, XT_TAB_LOAD_IMAGES
, 0 },
3085 /* command aliases (handy when -S flag is used) */
3086 { "promptopen", 0, command
, XT_CMD_OPEN
, 0 },
3087 { "promptopencurrent", 0, command
, XT_CMD_OPEN_CURRENT
, 0 },
3088 { "prompttabnew", 0, command
, XT_CMD_TABNEW
, 0 },
3089 { "prompttabnewcurrent",0, command
, XT_CMD_TABNEW_CURRENT
, 0 },
3092 { "set", 0, set
, 0, XT_SETARG
},
3094 { "fullscreen", 0, fullscreen
, 0, 0 },
3095 { "f", 0, fullscreen
, 0, 0 },
3098 { "session", 0, session_cmd
, XT_SHOW
, 0 },
3099 { "delete", 1, session_cmd
, XT_DELETE
, XT_SESSARG
},
3100 { "open", 1, session_cmd
, XT_OPEN
, XT_SESSARG
},
3101 { "save", 1, session_cmd
, XT_SAVE
, XT_USERARG
},
3102 { "show", 1, session_cmd
, XT_SHOW
, 0 },
3104 /* external javascript */
3105 { "script", 0, script_cmd
, XT_EJS_SHOW
, XT_USERARG
},
3108 { "inspector", 0, inspector_cmd
, XT_INS_SHOW
, 0 },
3109 { "show", 1, inspector_cmd
, XT_INS_SHOW
, 0 },
3110 { "hide", 1, inspector_cmd
, XT_INS_HIDE
, 0 },
3113 { "proxy", 0, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3114 { "show", 1, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3115 { "toggle", 1, proxy_cmd
, XT_PRXY_TOGGLE
, 0 },
3122 } cmd_status
= {-1, 0};
3125 wv_release_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3128 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 1)
3135 wv_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3138 WebKitHitTestResult
*hit_test_result
;
3141 hit_test_result
= webkit_web_view_get_hit_test_result(t
->wv
, e
);
3142 g_object_get(hit_test_result
, "context", &context
, NULL
);
3147 if (context
& WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE
)
3148 t
->mode
= XT_MODE_INSERT
;
3150 t
->mode
= XT_MODE_COMMAND
;
3152 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3154 else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 8 /* btn 4 */) {
3160 } else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 9 /* btn 5 */) {
3162 a
.i
= XT_NAV_FORWARD
;
3172 tab_close_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3174 DNPRINTF(XT_D_TAB
, "tab_close_cb: tab %d\n", t
->tab_id
);
3176 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3185 activate_uri_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3187 const gchar
*uri
= gtk_entry_get_text(GTK_ENTRY(entry
));
3189 DNPRINTF(XT_D_URL
, "activate_uri_entry_cb: %s\n", uri
);
3192 show_oops(NULL
, "activate_uri_entry_cb invalid parameters");
3197 show_oops(t
, "activate_uri_entry_cb no uri");
3201 uri
+= strspn(uri
, "\t ");
3203 /* if xxxt:// treat specially */
3204 if (parse_xtp_url(t
, uri
))
3207 /* otherwise continue to load page normally */
3208 load_uri(t
, (gchar
*)uri
);
3213 activate_search_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3215 const gchar
*search
= gtk_entry_get_text(GTK_ENTRY(entry
));
3216 char *newuri
= NULL
;
3219 DNPRINTF(XT_D_URL
, "activate_search_entry_cb: %s\n", search
);
3222 show_oops(NULL
, "activate_search_entry_cb invalid parameters");
3226 if (search_string
== NULL
) {
3227 show_oops(t
, "no search_string");
3231 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
3233 enc_search
= soup_uri_encode(search
, XT_RESERVED_CHARS
);
3234 newuri
= g_strdup_printf(search_string
, enc_search
);
3238 webkit_web_view_load_uri(t
->wv
, newuri
);
3246 check_and_set_cookie(const gchar
*uri
, struct tab
*t
)
3248 struct domain
*d
= NULL
;
3251 if (uri
== NULL
|| t
== NULL
)
3254 if ((d
= wl_find_uri(uri
, &c_wl
)) == NULL
)
3259 DNPRINTF(XT_D_COOKIE
, "check_and_set_cookie: %s %s\n",
3260 es
? "enable" : "disable", uri
);
3262 g_object_set(G_OBJECT(t
->settings
),
3263 "enable-html5-local-storage", es
, (char *)NULL
);
3264 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3268 check_and_set_js(const gchar
*uri
, struct tab
*t
)
3270 struct domain
*d
= NULL
;
3273 if (uri
== NULL
|| t
== NULL
)
3276 if ((d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3281 DNPRINTF(XT_D_JS
, "check_and_set_js: %s %s\n",
3282 es
? "enable" : "disable", uri
);
3284 g_object_set(G_OBJECT(t
->settings
),
3285 "enable-scripts", es
, (char *)NULL
);
3286 g_object_set(G_OBJECT(t
->settings
),
3287 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
3288 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3290 button_set_stockid(t
->js_toggle
,
3291 es
? GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
);
3295 check_and_set_pl(const gchar
*uri
, struct tab
*t
)
3297 struct domain
*d
= NULL
;
3300 if (uri
== NULL
|| t
== NULL
)
3303 if ((d
= wl_find_uri(uri
, &pl_wl
)) == NULL
)
3308 DNPRINTF(XT_D_JS
, "check_and_set_pl: %s %s\n",
3309 es
? "enable" : "disable", uri
);
3311 g_object_set(G_OBJECT(t
->settings
),
3312 "enable-plugins", es
, (char *)NULL
);
3313 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3317 color_address_bar(gpointer p
)
3320 struct tab
*tt
, *t
= p
;
3321 gchar
*col_str
= XT_COLOR_WHITE
;
3322 const gchar
*uri
, *u
= NULL
, *error_str
= NULL
;
3325 gdk_threads_enter();
3327 DNPRINTF(XT_D_URL
, "%s:\n", __func__
);
3329 /* make sure t still exists */
3332 TAILQ_FOREACH(tt
, &tabs
, entry
)
3338 if ((uri
= get_uri(t
)) == NULL
)
3343 gdk_threads_leave();
3346 col_str
= XT_COLOR_YELLOW
;
3347 switch (load_compare_cert(u
, &error_str
)) {
3349 col_str
= XT_COLOR_BLUE
;
3352 col_str
= XT_COLOR_GREEN
;
3354 case CERT_UNTRUSTED
:
3355 col_str
= XT_COLOR_YELLOW
;
3358 col_str
= XT_COLOR_RED
;
3363 gdk_threads_enter();
3365 /* make sure t isn't deleted */
3366 TAILQ_FOREACH(tt
, &tabs
, entry
)
3373 /* test to see if the user navigated away and canceled the thread */
3374 if (t
->thread
!= g_thread_self())
3376 if ((uri
= get_uri(t
)) == NULL
) {
3380 if (strcmp(uri
, u
)) {
3381 /* make sure we are still the same url */
3387 gdk_color_parse(col_str
, &color
);
3388 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3390 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3391 statusbar_modify_attr(t
, col_str
, XT_COLOR_BLACK
);
3393 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3395 if (error_str
&& error_str
[0] != '\0')
3396 show_oops(t
, "%s", error_str
);
3401 /* t is invalid at this point */
3403 g_free((gpointer
)u
);
3405 gdk_threads_leave();
3410 show_ca_status(struct tab
*t
, const char *uri
)
3413 gchar
*col_str
= XT_COLOR_WHITE
;
3415 DNPRINTF(XT_D_URL
, "show_ca_status: %d %s %s\n",
3416 ssl_strict_certs
, ssl_ca_file
, uri
);
3423 if (ssl_ca_file
== NULL
) {
3424 if (g_str_has_prefix(uri
, "http://"))
3426 if (g_str_has_prefix(uri
, "https://")) {
3427 col_str
= XT_COLOR_RED
;
3432 if (g_str_has_prefix(uri
, "http://") ||
3433 !g_str_has_prefix(uri
, "https://"))
3437 * It is not necessary to see if the thread is already running.
3438 * If the thread is in progress setting it to something else aborts it
3442 /* thread the coloring of the address bar */
3443 t
->thread
= g_thread_create((GThreadFunc
)color_address_bar
, t
, TRUE
, NULL
);
3445 color_address_bar(t
);
3451 gdk_color_parse(col_str
, &color
);
3452 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3454 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3455 statusbar_modify_attr(t
, col_str
, XT_COLOR_BLACK
);
3457 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3462 free_favicon(struct tab
*t
)
3464 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p req %p\n",
3465 __func__
, t
->icon_download
, t
->icon_request
);
3467 if (t
->icon_request
)
3468 g_object_unref(t
->icon_request
);
3469 if (t
->icon_dest_uri
)
3470 g_free(t
->icon_dest_uri
);
3472 t
->icon_request
= NULL
;
3473 t
->icon_dest_uri
= NULL
;
3477 xt_icon_from_name(struct tab
*t
, gchar
*name
)
3479 if (!enable_favicon_entry
)
3482 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->uri_entry
),
3483 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3485 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3486 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3488 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3489 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3493 xt_icon_from_pixbuf(struct tab
*t
, GdkPixbuf
*pb
)
3495 GdkPixbuf
*pb_scaled
;
3497 if (gdk_pixbuf_get_width(pb
) > 16 || gdk_pixbuf_get_height(pb
) > 16)
3498 pb_scaled
= gdk_pixbuf_scale_simple(pb
, 16, 16,
3499 GDK_INTERP_BILINEAR
);
3503 if (enable_favicon_entry
) {
3506 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
3507 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3510 if (show_url
== 0) {
3511 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
3512 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3514 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3515 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3518 /* XXX: Only supports the minimal tabs atm. */
3519 if (enable_favicon_tabs
)
3520 gtk_image_set_from_pixbuf(GTK_IMAGE(t
->tab_elems
.favicon
),
3523 if (pb_scaled
!= pb
)
3524 g_object_unref(pb_scaled
);
3528 xt_icon_from_file(struct tab
*t
, char *file
)
3532 if (g_str_has_prefix(file
, "file://"))
3533 file
+= strlen("file://");
3535 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
3537 xt_icon_from_pixbuf(t
, pb
);
3540 xt_icon_from_name(t
, "text-html");
3544 is_valid_icon(char *file
)
3547 const char *mime_type
;
3551 gf
= g_file_new_for_path(file
);
3552 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
3554 mime_type
= g_file_info_get_content_type(fi
);
3555 valid
= g_strcmp0(mime_type
, "image/x-ico") == 0 ||
3556 g_strcmp0(mime_type
, "image/vnd.microsoft.icon") == 0 ||
3557 g_strcmp0(mime_type
, "image/png") == 0 ||
3558 g_strcmp0(mime_type
, "image/gif") == 0 ||
3559 g_strcmp0(mime_type
, "application/octet-stream") == 0;
3567 set_favicon_from_file(struct tab
*t
, char *file
)
3571 if (t
== NULL
|| file
== NULL
)
3574 if (g_str_has_prefix(file
, "file://"))
3575 file
+= strlen("file://");
3576 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading %s\n", __func__
, file
);
3578 if (!stat(file
, &sb
)) {
3579 if (sb
.st_size
== 0 || !is_valid_icon(file
)) {
3580 /* corrupt icon so trash it */
3581 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3584 /* no need to set icon to default here */
3588 xt_icon_from_file(t
, file
);
3592 favicon_download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
3595 WebKitDownloadStatus status
= webkit_download_get_status(download
);
3596 struct tab
*tt
= NULL
, *t
= NULL
;
3599 * find the webview instead of passing in the tab as it could have been
3600 * deleted from underneath us.
3602 TAILQ_FOREACH(tt
, &tabs
, entry
) {
3611 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d status %d\n",
3612 __func__
, t
->tab_id
, status
);
3615 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
3617 t
->icon_download
= NULL
;
3620 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
3623 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
3626 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
3628 DNPRINTF(XT_D_DOWNLOAD
, "%s: freeing favicon %d\n",
3629 __func__
, t
->tab_id
);
3630 t
->icon_download
= NULL
;
3633 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
3636 DNPRINTF(XT_D_DOWNLOAD
, "%s: setting icon to %s\n",
3637 __func__
, t
->icon_dest_uri
);
3638 set_favicon_from_file(t
, t
->icon_dest_uri
);
3639 /* these will be freed post callback */
3640 t
->icon_request
= NULL
;
3641 t
->icon_download
= NULL
;
3649 abort_favicon_download(struct tab
*t
)
3651 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p\n", __func__
, t
->icon_download
);
3653 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
3654 if (t
->icon_download
) {
3655 g_signal_handlers_disconnect_by_func(G_OBJECT(t
->icon_download
),
3656 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3657 webkit_download_cancel(t
->icon_download
);
3658 t
->icon_download
= NULL
;
3663 xt_icon_from_name(t
, "text-html");
3667 notify_icon_loaded_cb(WebKitWebView
*wv
, gchar
*uri
, struct tab
*t
)
3669 DNPRINTF(XT_D_DOWNLOAD
, "%s %s\n", __func__
, uri
);
3671 if (uri
== NULL
|| t
== NULL
)
3674 #if WEBKIT_CHECK_VERSION(1, 4, 0)
3675 /* take icon from WebKitIconDatabase */
3678 pb
= webkit_web_view_get_icon_pixbuf(wv
);
3680 xt_icon_from_pixbuf(t
, pb
);
3683 xt_icon_from_name(t
, "text-html");
3684 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
3685 /* download icon to cache dir */
3686 gchar
*name_hash
, file
[PATH_MAX
];
3689 if (t
->icon_request
) {
3690 DNPRINTF(XT_D_DOWNLOAD
, "%s: download in progress\n", __func__
);
3694 /* check to see if we got the icon in cache */
3695 name_hash
= g_compute_checksum_for_string(G_CHECKSUM_SHA256
, uri
, -1);
3696 snprintf(file
, sizeof file
, "%s/%s.ico", cache_dir
, name_hash
);
3699 if (!stat(file
, &sb
)) {
3700 if (sb
.st_size
> 0) {
3701 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading from cache %s\n",
3703 set_favicon_from_file(t
, file
);
3707 /* corrupt icon so trash it */
3708 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3713 /* create download for icon */
3714 t
->icon_request
= webkit_network_request_new(uri
);
3715 if (t
->icon_request
== NULL
) {
3716 DNPRINTF(XT_D_DOWNLOAD
, "%s: invalid uri %s\n",
3721 t
->icon_download
= webkit_download_new(t
->icon_request
);
3722 if (t
->icon_download
== NULL
)
3725 /* we have to free icon_dest_uri later */
3726 t
->icon_dest_uri
= g_strdup_printf("file://%s", file
);
3727 webkit_download_set_destination_uri(t
->icon_download
,
3730 if (webkit_download_get_status(t
->icon_download
) ==
3731 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
3732 g_object_unref(t
->icon_request
);
3733 g_free(t
->icon_dest_uri
);
3734 t
->icon_request
= NULL
;
3735 t
->icon_dest_uri
= NULL
;
3739 g_signal_connect(G_OBJECT(t
->icon_download
), "notify::status",
3740 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3742 webkit_download_start(t
->icon_download
);
3747 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3749 const gchar
*uri
= NULL
;
3750 struct history
*h
, find
;
3754 DNPRINTF(XT_D_URL
, "notify_load_status_cb: %d %s\n",
3755 webkit_web_view_get_load_status(wview
),
3756 get_uri(t
) ? get_uri(t
) : "NOTHING");
3759 show_oops(NULL
, "notify_load_status_cb invalid parameters");
3763 switch (webkit_web_view_get_load_status(wview
)) {
3764 case WEBKIT_LOAD_PROVISIONAL
:
3766 abort_favicon_download(t
);
3767 #if GTK_CHECK_VERSION(2, 20, 0)
3768 gtk_widget_show(t
->spinner
);
3769 gtk_spinner_start(GTK_SPINNER(t
->spinner
));
3771 gtk_label_set_text(GTK_LABEL(t
->label
), "Loading");
3773 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), TRUE
);
3775 /* assume we are a new address */
3776 gdk_color_parse("white", &color
);
3777 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3778 statusbar_modify_attr(t
, "white", XT_COLOR_BLACK
);
3780 /* take focus if we are visible */
3786 /* kill color thread */
3791 case WEBKIT_LOAD_COMMITTED
:
3796 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
3802 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
3804 /* check if js white listing is enabled */
3805 if (enable_plugin_whitelist
)
3806 check_and_set_pl(uri
, t
);
3807 if (enable_cookie_whitelist
)
3808 check_and_set_cookie(uri
, t
);
3809 if (enable_js_whitelist
)
3810 check_and_set_js(uri
, t
);
3816 /* we know enough to autosave the session */
3817 if (session_autosave
) {
3822 show_ca_status(t
, uri
);
3823 run_script(t
, JS_HINTING
);
3824 if (enable_autoscroll
)
3825 run_script(t
, JS_AUTOSCROLL
);
3828 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
3830 if (color_visited_uris
) {
3831 color_visited(t
, color_visited_helper());
3833 /* This colors the links you middle-click (open in new
3834 * tab) in the current tab. */
3835 if (t
->tab_id
!= gtk_notebook_get_current_page(notebook
) &&
3836 (uri
= get_uri(t
)) != NULL
)
3837 color_visited(get_current_tab(),
3838 g_strdup_printf("{'%s' : 'dummy'}", uri
));
3842 case WEBKIT_LOAD_FINISHED
:
3844 if ((uri
= get_uri(t
)) == NULL
)
3847 if (!strncmp(uri
, "http://", strlen("http://")) ||
3848 !strncmp(uri
, "https://", strlen("https://")) ||
3849 !strncmp(uri
, "file://", strlen("file://"))) {
3850 find
.uri
= (gchar
*)uri
;
3851 h
= RB_FIND(history_list
, &hl
, &find
);
3853 insert_history_item(uri
,
3854 get_title(t
, FALSE
), time(NULL
));
3856 h
->time
= time(NULL
);
3859 set_status(t
, (char *)uri
, XT_STATUS_URI
);
3860 #if WEBKIT_CHECK_VERSION(1, 1, 18)
3861 case WEBKIT_LOAD_FAILED
:
3864 #if GTK_CHECK_VERSION(2, 20, 0)
3865 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
3866 gtk_widget_hide(t
->spinner
);
3869 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
3874 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
), TRUE
);
3876 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
),
3877 can_go_back_for_real(t
));
3879 gtk_widget_set_sensitive(GTK_WIDGET(t
->forward
),
3880 can_go_forward_for_real(t
));
3884 notify_title_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3886 const gchar
*title
= NULL
, *win_title
= NULL
;
3888 title
= get_title(t
, FALSE
);
3889 win_title
= get_title(t
, TRUE
);
3891 gtk_label_set_text(GTK_LABEL(t
->label
), title
);
3892 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
), title
);
3895 if (win_title
&& t
->tab_id
== gtk_notebook_get_current_page(notebook
))
3896 gtk_window_set_title(GTK_WINDOW(main_window
), win_title
);
3900 get_domain(const gchar
*host
)
3905 /* handle silly domains like .co.uk */
3907 if ((x
= strlen(host
)) <= 6)
3908 return (g_strdup(host
));
3910 if (host
[x
- 3] == '.' && host
[x
- 6] == '.') {
3916 return (g_strdup(&host
[x
+ 1]));
3920 p
= g_strrstr(host
, ".");
3922 return (g_strdup(""));
3928 return (g_strdup(p
+ 1));
3930 return (g_strdup(host
));
3934 js_autorun(struct tab
*t
)
3938 size_t got_default
= 0, got_host
= 0;
3940 char deff
[PATH_MAX
], hostf
[PATH_MAX
];
3941 char *js
= NULL
, *jsat
, *domain
= NULL
;
3942 FILE *deffile
= NULL
, *hostfile
= NULL
;
3944 if (js_autorun_enabled
== 0)
3949 !(g_str_has_prefix(uri
, "http://") ||
3950 g_str_has_prefix(uri
, "https://")))
3953 su
= soup_uri_new(uri
);
3956 if (!SOUP_URI_VALID_FOR_HTTP(su
))
3959 DNPRINTF(XT_D_JS
, "%s: host: %s domain: %s\n", __func__
,
3961 domain
= get_domain(su
->host
);
3963 snprintf(deff
, sizeof deff
, "%s/default.js", js_dir
);
3964 if ((deffile
= fopen(deff
, "r")) != NULL
) {
3965 if (fstat(fileno(deffile
), &sb
) == -1) {
3966 show_oops(t
, "can't stat default JS file");
3969 got_default
= sb
.st_size
;
3972 /* try host first followed by domain */
3973 snprintf(hostf
, sizeof hostf
, "%s/%s.js", js_dir
, su
->host
);
3974 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
3975 if ((hostfile
= fopen(hostf
, "r")) == NULL
) {
3976 snprintf(hostf
, sizeof hostf
, "%s/%s.js", js_dir
, domain
);
3977 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
3978 if ((hostfile
= fopen(hostf
, "r")) == NULL
)
3981 DNPRINTF(XT_D_JS
, "file: %s\n", hostf
);
3982 if (fstat(fileno(hostfile
), &sb
) == -1) {
3983 show_oops(t
, "can't stat %s JS file", hostf
);
3986 got_host
= sb
.st_size
;
3989 if (got_default
+ got_host
== 0)
3992 js
= g_malloc0(got_default
+ got_host
+ 1);
3996 if (fread(js
, got_default
, 1, deffile
) != 1) {
3997 show_oops(t
, "default file read error");
4000 jsat
= js
+ got_default
;
4004 if (fread(jsat
, got_host
, 1, hostfile
) != 1) {
4005 show_oops(t
, "host file read error");
4010 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
4027 webview_load_finished_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4029 /* autorun some js if enabled */
4037 webview_progress_changed_cb(WebKitWebView
*wv
, int progress
, struct tab
*t
)
4039 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->uri_entry
),
4040 progress
== 100 ? 0 : (double)progress
/ 100);
4041 if (show_url
== 0) {
4042 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
4043 progress
== 100 ? 0 : (double)progress
/ 100);
4046 update_statusbar_position(NULL
, NULL
);
4050 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
4051 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
4052 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
4055 WebKitWebNavigationReason reason
;
4056 struct domain
*d
= NULL
;
4059 show_oops(NULL
, "webview_npd_cb invalid parameters");
4063 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
4065 webkit_network_request_get_uri(request
));
4067 uri
= (char *)webkit_network_request_get_uri(request
);
4069 if (!auto_load_images
&& t
->load_images
) {
4071 /* Disable autoloading of images, now that we're done loading
4073 g_object_set(G_OBJECT(t
->settings
),
4074 "auto-load-images", FALSE
, (char *)NULL
);
4075 webkit_web_view_set_settings(t
->wv
, t
->settings
);
4077 t
->load_images
= FALSE
;
4080 /* If this is an xtp url, we don't load anything else. */
4081 if (parse_xtp_url(t
, uri
))
4084 if ((t
->mode
== XT_MODE_HINT
&& t
->new_tab
) || t
->ctrl_click
) {
4086 create_new_tab(uri
, NULL
, ctrl_click_focus
, -1);
4087 webkit_web_policy_decision_ignore(pd
);
4088 return (TRUE
); /* we made the decission */
4091 /* Change user agent if more than one has been given. */
4092 if (user_agent_count
> 1) {
4093 struct user_agent
*ua
;
4095 if ((ua
= TAILQ_NEXT(user_agent
, entry
)) == NULL
)
4096 user_agent
= TAILQ_FIRST(&ua_list
);
4100 free(t
->user_agent
);
4101 t
->user_agent
= g_strdup(user_agent
->value
);
4103 DNPRINTF(XT_D_NAV
, "user-agent: %s\n", t
->user_agent
);
4105 g_object_set(G_OBJECT(t
->settings
),
4106 "user-agent", t
->user_agent
, (char *)NULL
);
4108 webkit_web_view_set_settings(wv
, t
->settings
);
4112 * This is a little hairy but it comes down to this:
4113 * when we run in whitelist mode we have to assist the browser in
4114 * opening the URL that it would have opened in a new tab.
4116 reason
= webkit_web_navigation_action_get_reason(na
);
4117 if (reason
== WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED
) {
4118 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
4119 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1)
4120 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4122 webkit_web_policy_decision_use(pd
);
4123 return (TRUE
); /* we made the decision */
4130 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4133 struct domain
*d
= NULL
;
4135 WebKitWebView
*webview
= NULL
;
4138 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
4139 webkit_web_view_get_uri(wv
));
4142 /* open in current tab */
4144 } else if (enable_scripts
== 0 && enable_js_whitelist
== 1) {
4145 uri
= webkit_web_view_get_uri(wv
);
4146 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4149 if (t
->ctrl_click
) {
4150 x
= ctrl_click_focus
;
4153 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4155 } else if (enable_scripts
== 1) {
4156 if (t
->ctrl_click
) {
4157 x
= ctrl_click_focus
;
4160 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4168 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
4171 struct domain
*d
= NULL
;
4173 DNPRINTF(XT_D_NAV
, "webview_close_cb: %d\n", t
->tab_id
);
4175 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
4176 uri
= webkit_web_view_get_uri(wv
);
4177 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4181 } else if (enable_scripts
== 1)
4188 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
4190 /* we can not eat the event without throwing gtk off so defer it */
4192 /* catch middle click */
4193 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 2) {
4198 /* catch ctrl click */
4199 if (e
->type
== GDK_BUTTON_RELEASE
&&
4200 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
4205 return (XT_CB_PASSTHROUGH
);
4209 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
4211 struct mime_type
*m
;
4213 m
= find_mime_type(mime_type
);
4221 show_oops(t
, "can't fork mime handler");
4231 execlp(m
->mt_action
, m
->mt_action
,
4232 webkit_network_request_get_uri(request
), (void *)NULL
);
4241 get_mime_type(const char *file
)
4244 char *mime_type
= NULL
;
4248 if (g_str_has_prefix(file
, "file://"))
4249 file
+= strlen("file://");
4251 gf
= g_file_new_for_path(file
);
4252 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
4254 if ((m
= g_file_info_get_content_type(fi
)) != NULL
)
4255 mime_type
= g_strdup(m
);
4263 run_download_mimehandler(char *mime_type
, char *file
)
4265 struct mime_type
*m
;
4267 m
= find_mime_type(mime_type
);
4273 show_oops(NULL
, "can't fork download mime handler");
4283 if (g_str_has_prefix(file
, "file://"))
4284 file
+= strlen("file://");
4285 execlp(m
->mt_action
, m
->mt_action
, file
, (void *)NULL
);
4294 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
4297 WebKitDownloadStatus status
;
4298 const char *file
= NULL
;
4301 if (download
== NULL
)
4303 status
= webkit_download_get_status(download
);
4304 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
4307 file
= webkit_download_get_destination_uri(download
);
4310 mime
= get_mime_type(file
);
4314 run_download_mimehandler((char *)mime
, (char *)file
);
4319 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
4320 WebKitNetworkRequest
*request
, char *mime_type
,
4321 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
4324 show_oops(NULL
, "webview_mimetype_cb invalid parameters");
4328 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
4329 t
->tab_id
, mime_type
);
4331 if (run_mimehandler(t
, mime_type
, request
) == 0) {
4332 webkit_web_policy_decision_ignore(decision
);
4337 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
4338 webkit_web_policy_decision_download(decision
);
4346 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
4350 const gchar
*suggested_name
;
4351 gchar
*filename
= NULL
;
4353 struct download
*download_entry
;
4356 if (wk_download
== NULL
|| t
== NULL
) {
4357 show_oops(NULL
, "%s invalid parameters", __func__
);
4361 suggested_name
= webkit_download_get_suggested_filename(wk_download
);
4362 if (suggested_name
== NULL
)
4363 return (FALSE
); /* abort download */
4374 filename
= g_strdup_printf("%d%s", i
, suggested_name
);
4376 uri
= g_strdup_printf("file://%s/%s", download_dir
, i
?
4377 filename
: suggested_name
);
4379 } while (!stat(uri
+ strlen("file://"), &sb
));
4381 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d filename %s "
4382 "local %s\n", __func__
, t
->tab_id
, filename
, uri
);
4384 webkit_download_set_destination_uri(wk_download
, uri
);
4386 if (webkit_download_get_status(wk_download
) ==
4387 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
4388 show_oops(t
, "%s: download failed to start", __func__
);
4390 gtk_label_set_text(GTK_LABEL(t
->label
), "Download Failed");
4392 /* connect "download first" mime handler */
4393 g_signal_connect(G_OBJECT(wk_download
), "notify::status",
4394 G_CALLBACK(download_status_changed_cb
), NULL
);
4396 download_entry
= g_malloc(sizeof(struct download
));
4397 download_entry
->download
= wk_download
;
4398 download_entry
->tab
= t
;
4399 download_entry
->id
= next_download_id
++;
4400 RB_INSERT(download_list
, &downloads
, download_entry
);
4401 /* get from history */
4402 g_object_ref(wk_download
);
4403 gtk_label_set_text(GTK_LABEL(t
->label
), "Downloading");
4404 show_oops(t
, "Download of '%s' started...",
4405 basename((char *)webkit_download_get_destination_uri(wk_download
)));
4414 /* sync other download manager tabs */
4415 update_download_tabs(NULL
);
4418 * NOTE: never redirect/render the current tab before this
4419 * function returns. This will cause the download to never start.
4421 return (ret
); /* start download */
4425 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
4427 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
4430 show_oops(NULL
, "webview_hover_cb");
4435 set_status(t
, uri
, XT_STATUS_LINK
);
4438 set_status(t
, t
->status
, XT_STATUS_NOTHING
);
4443 mark(struct tab
*t
, struct karg
*arg
)
4449 if ((index
= marktoindex(mark
)) == -1)
4452 if (arg
->i
== XT_MARK_SET
)
4453 t
->mark
[index
] = gtk_adjustment_get_value(t
->adjust_v
);
4454 else if (arg
->i
== XT_MARK_GOTO
) {
4455 if (t
->mark
[index
] == XT_INVALID_MARK
) {
4456 show_oops(t
, "mark '%c' does not exist", mark
);
4459 /* XXX t->mark[index] can be bigger than the maximum if ajax or
4460 something changes the document size */
4461 gtk_adjustment_set_value(t
->adjust_v
, t
->mark
[index
]);
4468 marks_clear(struct tab
*t
)
4472 for (i
= 0; i
< LENGTH(t
->mark
); i
++)
4473 t
->mark
[i
] = XT_INVALID_MARK
;
4479 char file
[PATH_MAX
];
4480 char *line
= NULL
, *p
;
4485 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
4486 if ((f
= fopen(file
, "r+")) == NULL
) {
4487 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
4491 for (i
= 1; ; i
++) {
4492 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
4494 if (strlen(line
) == 0 || line
[0] == '#') {
4500 p
= strtok(line
, " \t");
4502 if (p
== NULL
|| strlen(p
) != 1 ||
4503 (index
= marktoindex(*p
)) == -1) {
4504 warnx("corrupt quickmarks file, line %d", i
);
4508 p
= strtok(NULL
, " \t");
4509 if (qmarks
[index
] != NULL
)
4510 g_free(qmarks
[index
]);
4511 qmarks
[index
] = g_strdup(p
);
4522 char file
[PATH_MAX
];
4526 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
4527 if ((f
= fopen(file
, "r+")) == NULL
) {
4528 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
4532 for (i
= 0; i
< XT_NOMARKS
; i
++)
4533 if (qmarks
[i
] != NULL
)
4534 fprintf(f
, "%c %s\n", indextomark(i
), qmarks
[i
]);
4542 qmark(struct tab
*t
, struct karg
*arg
)
4547 mark
= arg
->s
[strlen(arg
->s
)-1];
4548 index
= marktoindex(mark
);
4554 if (qmarks
[index
] != NULL
)
4555 g_free(qmarks
[index
]);
4557 qmarks_load(); /* sync if multiple instances */
4558 qmarks
[index
] = g_strdup(get_uri(t
));
4562 if (qmarks
[index
] != NULL
)
4563 load_uri(t
, qmarks
[index
]);
4565 show_oops(t
, "quickmark \"%c\" does not exist",
4571 if (qmarks
[index
] != NULL
)
4572 create_new_tab(qmarks
[index
], NULL
, 1, -1);
4574 show_oops(t
, "quickmark \"%c\" does not exist",
4585 go_up(struct tab
*t
, struct karg
*args
)
4592 if (args
->i
== XT_GO_UP_ROOT
)
4593 levels
= XT_GO_UP_ROOT
;
4594 else if ((levels
= atoi(args
->s
)) == 0)
4597 uri
= g_strdup(get_uri(t
));
4599 if ((tmp
= strstr(uri
, XT_PROTO_DELIM
)) == NULL
)
4602 tmp
+= strlen(XT_PROTO_DELIM
);
4604 /* if an uri starts with a slash, leave it alone (for file:///) */
4609 p
= strrchr(tmp
, '/');
4623 gototab(struct tab
*t
, struct karg
*args
)
4626 struct karg arg
= {0, NULL
, -1};
4628 tab
= atoi(args
->s
);
4631 arg
.i
= XT_TAB_NEXT
;
4643 gotonexttab(struct tab
*t
, struct karg
*args
)
4645 int count
, n_tabs
, dest
;
4646 struct karg arg
= {0, NULL
, -1};
4648 count
= atoi(args
->s
);
4652 arg
.i
= XT_TAB_NEXT
;
4654 n_tabs
= gtk_notebook_get_n_pages(notebook
);
4655 dest
= gtk_notebook_get_current_page(notebook
);
4657 dest
+= (count
+ 1) % n_tabs
;
4660 arg
.precount
= dest
;
4662 DNPRINTF(XT_D_BUFFERCMD
, "gotonexttab: count: %d - dest : %d \n", count
, dest
);
4670 gotoprevtab(struct tab
*t
, struct karg
*args
)
4673 struct karg arg
= {0, NULL
, -1};
4675 count
= atoi(args
->s
);
4679 arg
.i
= XT_TAB_PREV
;
4680 arg
.precount
= count
;
4682 DNPRINTF(XT_D_BUFFERCMD
, "gotoprevtab: count: %d\n", count
);
4689 zoom_amount(struct tab
*t
, struct karg
*arg
)
4691 struct karg narg
= {0, NULL
, -1};
4693 narg
.i
= atoi(arg
->s
);
4694 resizetab(t
, &narg
);
4700 flip_colon(struct tab
*t
, struct karg
*arg
)
4702 struct karg narg
= {0, NULL
, -1};
4705 if (t
== NULL
|| arg
== NULL
)
4708 p
= strstr(arg
->s
, ":");
4720 /* buffer commands receive the regex that triggered them in arg.s */
4721 char bcmd
[XT_BUFCMD_SZ
];
4725 #define XT_PRE_NO (0)
4726 #define XT_PRE_YES (1)
4727 #define XT_PRE_MAYBE (2)
4729 int (*func
)(struct tab
*, struct karg
*);
4733 { "^[0-9]*gu$", XT_PRE_MAYBE
, "gu", go_up
, 0 },
4734 { "^gU$", XT_PRE_NO
, "gU", go_up
, XT_GO_UP_ROOT
},
4735 { "^gg$", XT_PRE_NO
, "gg", move
, XT_MOVE_TOP
},
4736 { "^gG$", XT_PRE_NO
, "gG", move
, XT_MOVE_BOTTOM
},
4737 { "^[0-9]+%$", XT_PRE_YES
, "%", move
, XT_MOVE_PERCENT
},
4738 { "^zz$", XT_PRE_NO
, "zz", move
, XT_MOVE_CENTER
},
4739 { "^gh$", XT_PRE_NO
, "gh", go_home
, 0 },
4740 { "^m[a-zA-Z0-9]$", XT_PRE_NO
, "m", mark
, XT_MARK_SET
},
4741 { "^['][a-zA-Z0-9]$", XT_PRE_NO
, "'", mark
, XT_MARK_GOTO
},
4742 { "^[0-9]+t$", XT_PRE_YES
, "t", gototab
, 0 },
4743 { "^g0$", XT_PRE_YES
, "g0", gototab
, XT_TAB_FIRST
},
4744 { "^g[$]$", XT_PRE_YES
, "g$", gototab
, XT_TAB_LAST
},
4745 { "^[0-9]*gt$", XT_PRE_YES
, "t", gotonexttab
, 0 },
4746 { "^[0-9]*gT$", XT_PRE_YES
, "T", gotoprevtab
, 0 },
4747 { "^M[a-zA-Z0-9]$", XT_PRE_NO
, "M", qmark
, XT_QMARK_SET
},
4748 { "^go[a-zA-Z0-9]$", XT_PRE_NO
, "go", qmark
, XT_QMARK_OPEN
},
4749 { "^gn[a-zA-Z0-9]$", XT_PRE_NO
, "gn", qmark
, XT_QMARK_TAB
},
4750 { "^ZR$", XT_PRE_NO
, "ZR", restart
, 0 },
4751 { "^ZZ$", XT_PRE_NO
, "ZZ", quit
, 0 },
4752 { "^zi$", XT_PRE_NO
, "zi", resizetab
, XT_ZOOM_IN
},
4753 { "^zo$", XT_PRE_NO
, "zo", resizetab
, XT_ZOOM_OUT
},
4754 { "^z0$", XT_PRE_NO
, "z0", resizetab
, XT_ZOOM_NORMAL
},
4755 { "^[0-9]+Z$", XT_PRE_YES
, "Z", zoom_amount
, 0 },
4756 { "^[0-9]+:$", XT_PRE_YES
, ":", flip_colon
, 0 },
4760 buffercmd_init(void)
4764 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
4765 if (regcomp(&buffercmds
[i
].cregex
, buffercmds
[i
].regex
,
4766 REG_EXTENDED
| REG_NOSUB
))
4767 startpage_add("invalid buffercmd regex %s",
4768 buffercmds
[i
].regex
);
4772 buffercmd_abort(struct tab
*t
)
4779 DNPRINTF(XT_D_BUFFERCMD
, "%s: clearing buffer\n", __func__
);
4781 for (i
= 0; i
< LENGTH(bcmd
); i
++)
4784 cmd_prefix
= 0; /* clear prefix for non-buffer commands */
4785 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
4789 buffercmd_execute(struct tab
*t
, struct buffercmd
*cmd
)
4791 struct karg arg
= {0, NULL
, -1};
4794 arg
.s
= g_strdup(bcmd
);
4796 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_execute: buffer \"%s\" "
4797 "matches regex \"%s\", executing\n", bcmd
, cmd
->regex
);
4807 buffercmd_addkey(struct tab
*t
, guint keyval
)
4810 char s
[XT_BUFCMD_SZ
];
4812 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
))) {
4814 return (XT_CB_PASSTHROUGH
);
4817 if (keyval
== GDK_Escape
) {
4819 return (XT_CB_HANDLED
);
4822 /* key with modifier or non-ascii character */
4823 if (!isascii(keyval
)) {
4825 * XXX this looks wrong but fixes some sites like
4826 * http://www.seslisozluk.com/
4827 * that eat a shift or ctrl and end putting default focus in js
4828 * instead of ignoring the keystroke
4829 * so instead of return (XT_CB_PASSTHROUGH); eat the key
4831 return (XT_CB_HANDLED
);
4834 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: adding key \"%c\" "
4835 "to buffer \"%s\"\n", keyval
, bcmd
);
4837 for (i
= 0; i
< LENGTH(bcmd
); i
++)
4838 if (bcmd
[i
] == '\0') {
4843 /* buffer full, ignore input */
4844 if (i
>= LENGTH(bcmd
) -1) {
4845 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: buffer full\n");
4847 return (XT_CB_HANDLED
);
4850 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
4852 /* find exact match */
4853 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
4854 if (regexec(&buffercmds
[i
].cregex
, bcmd
,
4855 (size_t) 0, NULL
, 0) == 0) {
4856 buffercmd_execute(t
, &buffercmds
[i
]);
4860 /* find non exact matches to see if we need to abort ot not */
4861 for (i
= 0, match
= 0; i
< LENGTH(buffercmds
); i
++) {
4862 DNPRINTF(XT_D_BUFFERCMD
, "trying: %s\n", bcmd
);
4865 if (buffercmds
[i
].precount
== XT_PRE_MAYBE
) {
4866 if (isdigit(bcmd
[0])) {
4867 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
4871 if (sscanf(bcmd
, "%s", s
) == 0)
4874 } else if (buffercmds
[i
].precount
== XT_PRE_YES
) {
4875 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
4878 if (sscanf(bcmd
, "%s", s
) == 0)
4881 if (c
== -1 && buffercmds
[i
].precount
)
4883 if (!strncmp(s
, buffercmds
[i
].cmd
, strlen(s
)))
4886 DNPRINTF(XT_D_BUFFERCMD
, "got[%d] %d <%s>: %d %s\n",
4887 i
, match
, buffercmds
[i
].cmd
, c
, s
);
4890 DNPRINTF(XT_D_BUFFERCMD
, "aborting: %s\n", bcmd
);
4895 return (XT_CB_HANDLED
);
4899 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
4901 struct key_binding
*k
;
4903 /* handle keybindings if buffercmd is empty.
4904 if not empty, allow commands like C-n */
4905 if (bcmd
[0] == '\0' || ((e
->state
& (CTRL
| MOD1
)) != 0))
4906 TAILQ_FOREACH(k
, &kbl
, entry
)
4907 if (e
->keyval
== k
->key
4908 && (entry
? k
->use_in_entry
: 1)) {
4909 /* when we are edditing eat ctrl/mod keys */
4910 if (edit_mode
== XT_EM_VI
&&
4911 t
->mode
== XT_MODE_INSERT
&&
4912 (e
->state
& CTRL
|| e
->state
& MOD1
))
4913 return (XT_CB_PASSTHROUGH
);
4916 if ((e
->state
& (CTRL
| MOD1
)) == 0)
4917 return (cmd_execute(t
, k
->cmd
));
4918 } else if ((e
->state
& k
->mask
) == k
->mask
) {
4919 return (cmd_execute(t
, k
->cmd
));
4923 if (!entry
&& ((e
->state
& (CTRL
| MOD1
)) == 0))
4924 return buffercmd_addkey(t
, e
->keyval
);
4926 return (XT_CB_PASSTHROUGH
);
4930 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
4933 void *active
= NULL
;
4935 /* don't use w directly; use t->whatever instead */
4938 show_oops(NULL
, "wv_keypress_cb");
4939 return (XT_CB_PASSTHROUGH
);
4944 DNPRINTF(XT_D_KEY
, "wv_keypress_cb: mode %d keyval 0x%x mask "
4945 "0x%x tab %d\n", t
->mode
, e
->keyval
, e
->state
, t
->tab_id
);
4947 /* Hide buffers, if they are visible, with escape. */
4948 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)) &&
4949 CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
4950 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
4952 return (XT_CB_HANDLED
);
4955 if (t
->mode
== XT_MODE_HINT
)
4956 return (XT_CB_HANDLED
);
4958 if ((CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Tab
) ||
4959 (CLEAN(e
->state
) == SHFT
&& e
->keyval
== GDK_Tab
))
4960 /* something focussy is about to happen */
4961 return (XT_CB_PASSTHROUGH
);
4963 /* check if we are some sort of text input thing in the dom */
4964 active
= input_check_mode(t
);
4966 if (t
->mode
== XT_MODE_HINT
) {
4967 /* XXX make sure cmd entry is enabled */
4968 return (XT_CB_HANDLED
);
4969 } else if (t
->mode
== XT_MODE_COMMAND
) {
4971 snprintf(s
, sizeof s
, "%c", e
->keyval
);
4972 if (CLEAN(e
->state
) == 0 && isdigit(s
[0]))
4973 cmd_prefix
= 10 * cmd_prefix
+ atoi(s
);
4974 return (handle_keypress(t
, e
, 0));
4977 return (handle_keypress(t
, e
, 1));
4981 return (XT_CB_PASSTHROUGH
);
4985 hint_continue(struct tab
*t
)
4987 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
4989 const gchar
*errstr
= NULL
;
4993 if (!(c
[0] == '.' || c
[0] == ','))
4995 if (strlen(c
) == 1) {
4996 /* XXX should not happen */
5001 if (isdigit(c
[1])) {
5003 i
= strtonum(&c
[1], 1, 4096, &errstr
);
5005 show_oops(t
, "invalid numerical hint %s", &c
[1]);
5008 s
= g_strdup_printf("hints.updateHints(%lld);", i
);
5012 /* alphanumeric input */
5013 s
= g_strdup_printf("hints.createHints('%s', '%c');",
5014 &c
[1], c
[0] == '.' ? 'f' : 'F');
5025 search_continue(struct tab
*t
)
5027 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5028 gboolean rv
= FALSE
;
5030 if (c
[0] == ':' || c
[0] == '.' || c
[0] == ',')
5032 if (strlen(c
) == 1) {
5033 webkit_web_view_unmark_text_matches(t
->wv
);
5038 t
->search_forward
= TRUE
;
5039 else if (c
[0] == '?')
5040 t
->search_forward
= FALSE
;
5050 search_cb(struct tab
*t
)
5052 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5055 if (search_continue(t
) == FALSE
)
5059 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, t
->search_forward
,
5061 /* not found, mark red */
5062 gdk_color_parse(XT_COLOR_RED
, &color
);
5063 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
5064 /* unmark and remove selection */
5065 webkit_web_view_unmark_text_matches(t
->wv
);
5066 /* my kingdom for a way to unselect text in webview */
5068 /* found, highlight all */
5069 webkit_web_view_unmark_text_matches(t
->wv
);
5070 webkit_web_view_mark_text_matches(t
->wv
, &c
[1], FALSE
, 0);
5071 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
5072 gdk_color_parse(XT_COLOR_WHITE
, &color
);
5073 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
5081 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5083 const gchar
*c
= gtk_entry_get_text(w
);
5086 show_oops(NULL
, "cmd_keyrelease_cb invalid parameters");
5087 return (XT_CB_PASSTHROUGH
);
5090 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
5091 e
->keyval
, e
->state
, t
->tab_id
);
5094 if (!(e
->keyval
== GDK_Tab
|| e
->keyval
== GDK_ISO_Left_Tab
)) {
5095 if (hint_continue(t
) == FALSE
)
5100 if (search_continue(t
) == FALSE
)
5103 /* if search length is > 4 then no longer play timeout games */
5104 if (strlen(c
) > 4) {
5106 g_source_remove(t
->search_id
);
5113 /* reestablish a new timer if the user types fast */
5115 g_source_remove(t
->search_id
);
5116 t
->search_id
= g_timeout_add(250, (GSourceFunc
)search_cb
, (gpointer
)t
);
5119 return (XT_CB_PASSTHROUGH
);
5123 match_uri(const gchar
*uri
, const gchar
*key
) {
5126 gboolean match
= FALSE
;
5130 if (!strncmp(key
, uri
, len
))
5133 voffset
= strstr(uri
, "/") + 2;
5134 if (!strncmp(key
, voffset
, len
))
5136 else if (g_str_has_prefix(voffset
, "www.")) {
5137 voffset
= voffset
+ strlen("www.");
5138 if (!strncmp(key
, voffset
, len
))
5147 match_session(const gchar
*name
, const gchar
*key
) {
5150 sub
= strcasestr(name
, key
);
5156 cmd_getlist(int id
, char *key
)
5163 if (cmds
[id
].type
& XT_URLARG
) {
5164 RB_FOREACH_REVERSE(h
, history_list
, &hl
)
5165 if (match_uri(h
->uri
, key
)) {
5166 cmd_status
.list
[c
] = (char *)h
->uri
;
5172 } else if (cmds
[id
].type
& XT_SESSARG
) {
5173 TAILQ_FOREACH(s
, &sessions
, entry
)
5174 if (match_session(s
->name
, key
)) {
5175 cmd_status
.list
[c
] = (char *)s
->name
;
5181 } else if (cmds
[id
].type
& XT_SETARG
) {
5182 for (i
= 0; i
< get_settings_size(); i
++)
5183 if (!strncmp(key
, get_setting_name(i
),
5185 cmd_status
.list
[c
++] =
5186 get_setting_name(i
);
5192 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
5194 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
5195 if (cmds
[i
].level
< dep
)
5197 if (cmds
[i
].level
== dep
&& !strncmp(key
, cmds
[i
].cmd
,
5198 strlen(key
)) && !isdigit(cmds
[i
].cmd
[0]))
5199 cmd_status
.list
[c
++] = cmds
[i
].cmd
;
5207 cmd_getnext(int dir
)
5209 cmd_status
.index
+= dir
;
5211 if (cmd_status
.index
< 0)
5212 cmd_status
.index
= cmd_status
.len
- 1;
5213 else if (cmd_status
.index
>= cmd_status
.len
)
5214 cmd_status
.index
= 0;
5216 return cmd_status
.list
[cmd_status
.index
];
5220 cmd_tokenize(char *s
, char *tokens
[])
5223 char *tok
, *last
= NULL
;
5224 size_t len
= strlen(s
);
5227 blank
= len
== 0 || (len
> 0 && s
[len
- 1] == ' ');
5228 for (tok
= strtok_r(s
, " ", &last
); tok
&& i
< 3;
5229 tok
= strtok_r(NULL
, " ", &last
), i
++)
5239 cmd_complete(struct tab
*t
, char *str
, int dir
)
5241 GtkEntry
*w
= GTK_ENTRY(t
->cmd
);
5242 int i
, j
, levels
, c
= 0, dep
= 0, parent
= -1;
5244 char *tok
, *match
, *s
= g_strdup(str
);
5246 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
5249 DNPRINTF(XT_D_CMD
, "%s: complete %s\n", __func__
, str
);
5252 for (i
= 0; isdigit(s
[i
]); i
++)
5255 for (; isspace(s
[i
]); i
++)
5260 levels
= cmd_tokenize(s
, tokens
);
5262 for (i
= 0; i
< levels
- 1; i
++) {
5265 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5266 if (cmds
[j
].level
< dep
)
5268 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
,
5272 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
5279 if (matchcount
== 1) {
5280 strlcat(res
, tok
, sizeof res
);
5281 strlcat(res
, " ", sizeof res
);
5291 if (cmd_status
.index
== -1)
5292 cmd_getlist(parent
, tokens
[i
]);
5294 if (cmd_status
.len
> 0) {
5295 match
= cmd_getnext(dir
);
5296 strlcat(res
, match
, sizeof res
);
5297 gtk_entry_set_text(w
, res
);
5298 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5305 cmd_execute(struct tab
*t
, char *str
)
5307 struct cmd
*cmd
= NULL
;
5308 char *tok
, *last
= NULL
, *s
= g_strdup(str
), *sc
;
5310 int j
, len
, c
= 0, dep
= 0, matchcount
= 0;
5311 int prefix
= -1, rv
= XT_CB_PASSTHROUGH
;
5312 struct karg arg
= {0, NULL
, -1};
5317 for (j
= 0; j
<3 && isdigit(s
[j
]); j
++)
5323 while (isspace(s
[0]))
5326 if (strlen(s
) > 0 && strlen(prefixstr
) > 0)
5327 prefix
= atoi(prefixstr
);
5331 for (tok
= strtok_r(s
, " ", &last
); tok
;
5332 tok
= strtok_r(NULL
, " ", &last
)) {
5334 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5335 if (cmds
[j
].level
< dep
)
5337 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1 :
5339 if (cmds
[j
].level
== dep
&&
5340 !strncmp(tok
, cmds
[j
].cmd
, len
)) {
5344 if (len
== strlen(cmds
[j
].cmd
)) {
5350 if (matchcount
== 1) {
5355 show_oops(t
, "Invalid command: %s", str
);
5361 show_oops(t
, "Empty command");
5367 arg
.precount
= prefix
;
5368 else if (cmd_prefix
> 0)
5369 arg
.precount
= cmd_prefix
;
5371 if (j
> 0 && !(cmd
->type
& XT_PREFIX
) && arg
.precount
> -1) {
5372 show_oops(t
, "No prefix allowed: %s", str
);
5376 arg
.s
= last
? g_strdup(last
) : g_strdup("");
5377 if (cmd
->type
& XT_INTARG
&& last
&& strlen(last
) > 0) {
5378 if (arg
.s
== NULL
) {
5379 show_oops(t
, "Invalid command");
5382 arg
.precount
= atoi(arg
.s
);
5383 if (arg
.precount
<= 0) {
5384 if (arg
.s
[0] == '0')
5385 show_oops(t
, "Zero count");
5387 show_oops(t
, "Trailing characters");
5392 DNPRINTF(XT_D_CMD
, "%s: prefix %d arg %s\n",
5393 __func__
, arg
.precount
, arg
.s
);
5409 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5412 show_oops(NULL
, "entry_key_cb invalid parameters");
5413 return (XT_CB_PASSTHROUGH
);
5416 DNPRINTF(XT_D_CMD
, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
5417 e
->keyval
, e
->state
, t
->tab_id
);
5421 if (e
->keyval
== GDK_Escape
) {
5422 /* don't use focus_webview(t) because we want to type :cmds */
5423 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5426 return (handle_keypress(t
, e
, 1));
5429 struct command_entry
*
5430 history_prev(struct command_list
*l
, struct command_entry
*at
)
5433 at
= TAILQ_LAST(l
, command_list
);
5435 at
= TAILQ_PREV(at
, command_list
, entry
);
5437 at
= TAILQ_LAST(l
, command_list
);
5443 struct command_entry
*
5444 history_next(struct command_list
*l
, struct command_entry
*at
)
5447 at
= TAILQ_FIRST(l
);
5449 at
= TAILQ_NEXT(at
, entry
);
5451 at
= TAILQ_FIRST(l
);
5458 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5460 int rv
= XT_CB_HANDLED
;
5461 const gchar
*c
= gtk_entry_get_text(w
);
5465 show_oops(NULL
, "cmd_keypress_cb parameters");
5466 return (XT_CB_PASSTHROUGH
);
5469 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
5470 e
->keyval
, e
->state
, t
->tab_id
);
5474 e
->keyval
= GDK_Escape
;
5475 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5476 c
[0] == '.' || c
[0] == ','))
5477 e
->keyval
= GDK_Escape
;
5479 if (e
->keyval
!= GDK_Tab
&& e
->keyval
!= GDK_Shift_L
&&
5480 e
->keyval
!= GDK_ISO_Left_Tab
)
5481 cmd_status
.index
= -1;
5483 switch (e
->keyval
) {
5486 cmd_complete(t
, (char *)&c
[1], 1);
5487 else if (c
[0] == '.' || c
[0] == ',')
5488 run_script(t
, "hints.focusNextHint();");
5490 case GDK_ISO_Left_Tab
:
5492 cmd_complete(t
, (char *)&c
[1], -1);
5493 else if (c
[0] == '.' || c
[0] == ',')
5494 run_script(t
, "hints.focusPreviousHint();");
5498 if ((search_at
= history_next(&shl
, search_at
))) {
5499 search_at
->line
[0] = c
[0];
5500 gtk_entry_set_text(w
, search_at
->line
);
5501 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5503 } else if (c
[0] == '/') {
5504 if ((search_at
= history_prev(&chl
, search_at
))) {
5505 search_at
->line
[0] = c
[0];
5506 gtk_entry_set_text(w
, search_at
->line
);
5507 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5509 } if (c
[0] == ':') {
5510 if ((history_at
= history_prev(&chl
, history_at
))) {
5511 history_at
->line
[0] = c
[0];
5512 gtk_entry_set_text(w
, history_at
->line
);
5513 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5519 if ((search_at
= history_next(&shl
, search_at
))) {
5520 search_at
->line
[0] = c
[0];
5521 gtk_entry_set_text(w
, search_at
->line
);
5522 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5524 } else if (c
[0] == '?') {
5525 if ((search_at
= history_prev(&chl
, search_at
))) {
5526 search_at
->line
[0] = c
[0];
5527 gtk_entry_set_text(w
, search_at
->line
);
5528 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5530 } if (c
[0] == ':') {
5531 if ((history_at
= history_next(&chl
, history_at
))) {
5532 history_at
->line
[0] = c
[0];
5533 gtk_entry_set_text(w
, history_at
->line
);
5534 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5539 if (!(!strcmp(c
, ":") || !strcmp(c
, "/") || !strcmp(c
, "?") ||
5540 !strcmp(c
, ".") || !strcmp(c
, ","))) {
5541 /* see if we are doing hinting and reset it */
5542 if (c
[0] == '.' || c
[0] == ',') {
5543 /* recreate hints */
5544 s
= g_strdup_printf("hints.createHints('', "
5545 "'%c');", c
[0] == '.' ? 'f' : 'F');
5558 if (c
!= NULL
&& (c
[0] == '/' || c
[0] == '?'))
5559 webkit_web_view_unmark_text_matches(t
->wv
);
5561 /* no need to cancel hints */
5565 rv
= XT_CB_PASSTHROUGH
;
5571 wv_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
5573 DNPRINTF(XT_D_CMD
, "wv_popup_cb: tab %d\n", t
->tab_id
);
5577 cmd_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
5579 /* popup menu enabled */
5584 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
5587 show_oops(NULL
, "cmd_focusout_cb invalid parameters");
5588 return (XT_CB_PASSTHROUGH
);
5591 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d popup %d\n",
5592 t
->tab_id
, t
->popup
);
5594 /* if popup is enabled don't lose focus */
5597 return (XT_CB_PASSTHROUGH
);
5604 if (show_url
== 0 || t
->focus_wv
)
5607 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
5609 return (XT_CB_PASSTHROUGH
);
5613 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
5616 const gchar
*c
= gtk_entry_get_text(entry
);
5619 show_oops(NULL
, "cmd_activate_cb invalid parameters");
5623 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
5628 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5629 c
[0] == '.' || c
[0] == ','))
5635 if (c
[0] == '/' || c
[0] == '?') {
5636 /* see if there is a timer pending */
5638 g_source_remove(t
->search_id
);
5643 if (t
->search_text
) {
5644 g_free(t
->search_text
);
5645 t
->search_text
= NULL
;
5648 t
->search_text
= g_strdup(s
);
5650 g_free(global_search
);
5651 global_search
= g_strdup(s
);
5652 t
->search_forward
= c
[0] == '/';
5654 history_add(&shl
, search_file
, s
, &search_history_count
);
5655 } else if (c
[0] == '.' || c
[0] == ',') {
5656 run_script(t
, "hints.fire();");
5657 /* XXX history for link following? */
5658 } else if (c
[0] == ':') {
5659 history_add(&chl
, command_file
, s
, &cmd_history_count
);
5660 /* can't call hide_cmd after cmd_execute */
5671 backward_cb(GtkWidget
*w
, struct tab
*t
)
5676 show_oops(NULL
, "backward_cb invalid parameters");
5680 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
5687 forward_cb(GtkWidget
*w
, struct tab
*t
)
5692 show_oops(NULL
, "forward_cb invalid parameters");
5696 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
5698 a
.i
= XT_NAV_FORWARD
;
5703 home_cb(GtkWidget
*w
, struct tab
*t
)
5706 show_oops(NULL
, "home_cb invalid parameters");
5710 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
5716 stop_cb(GtkWidget
*w
, struct tab
*t
)
5718 WebKitWebFrame
*frame
;
5721 show_oops(NULL
, "stop_cb invalid parameters");
5725 DNPRINTF(XT_D_NAV
, "stop_cb: tab %d\n", t
->tab_id
);
5727 frame
= webkit_web_view_get_main_frame(t
->wv
);
5728 if (frame
== NULL
) {
5729 show_oops(t
, "stop_cb: no frame");
5733 webkit_web_frame_stop_loading(frame
);
5734 abort_favicon_download(t
);
5738 setup_webkit(struct tab
*t
)
5740 if (is_g_object_setting(G_OBJECT(t
->settings
), "enable-dns-prefetching"))
5741 g_object_set(G_OBJECT(t
->settings
), "enable-dns-prefetching",
5742 FALSE
, (char *)NULL
);
5744 warnx("webkit does not have \"enable-dns-prefetching\" property");
5745 g_object_set(G_OBJECT(t
->settings
),
5746 "user-agent", t
->user_agent
, (char *)NULL
);
5747 g_object_set(G_OBJECT(t
->settings
),
5748 "enable-scripts", enable_scripts
, (char *)NULL
);
5749 g_object_set(G_OBJECT(t
->settings
),
5750 "enable-plugins", enable_plugins
, (char *)NULL
);
5751 g_object_set(G_OBJECT(t
->settings
),
5752 "javascript-can-open-windows-automatically", enable_scripts
,
5754 g_object_set(G_OBJECT(t
->settings
),
5755 "enable-html5-database", FALSE
, (char *)NULL
);
5756 g_object_set(G_OBJECT(t
->settings
),
5757 "enable-html5-local-storage", enable_localstorage
, (char *)NULL
);
5758 g_object_set(G_OBJECT(t
->settings
),
5759 "enable_spell_checking", enable_spell_checking
, (char *)NULL
);
5760 g_object_set(G_OBJECT(t
->settings
),
5761 "spell_checking_languages", spell_check_languages
, (char *)NULL
);
5762 g_object_set(G_OBJECT(t
->settings
),
5763 "enable-developer-extras", TRUE
, (char *)NULL
);
5764 g_object_set(G_OBJECT(t
->wv
),
5765 "full-content-zoom", TRUE
, (char *)NULL
);
5766 g_object_set(G_OBJECT(t
->settings
),
5767 "auto-load-images", auto_load_images
, (char *)NULL
);
5769 webkit_web_view_set_settings(t
->wv
, t
->settings
);
5773 update_statusbar_position(GtkAdjustment
* adjustment
, gpointer data
)
5775 struct tab
*ti
, *t
= NULL
;
5776 gdouble view_size
, value
, max
;
5779 TAILQ_FOREACH(ti
, &tabs
, entry
)
5780 if (ti
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
5788 if (adjustment
== NULL
)
5789 adjustment
= gtk_scrolled_window_get_vadjustment(
5790 GTK_SCROLLED_WINDOW(t
->browser_win
));
5792 view_size
= gtk_adjustment_get_page_size(adjustment
);
5793 value
= gtk_adjustment_get_value(adjustment
);
5794 max
= gtk_adjustment_get_upper(adjustment
) - view_size
;
5797 position
= g_strdup("All");
5798 else if (value
== max
)
5799 position
= g_strdup("Bot");
5800 else if (value
== 0)
5801 position
= g_strdup("Top");
5803 position
= g_strdup_printf("%d%%", (int) ((value
/ max
) * 100));
5805 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.position
), position
);
5812 create_window(const gchar
*name
)
5816 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
5817 if (window_maximize
)
5818 gtk_window_maximize(GTK_WINDOW(w
));
5820 gtk_window_set_default_size(GTK_WINDOW(w
), window_width
, window_height
);
5821 gtk_widget_set_name(w
, name
);
5822 gtk_window_set_wmclass(GTK_WINDOW(w
), name
, "XXXTerm");
5828 create_browser(struct tab
*t
)
5832 GtkAdjustment
*adjustment
;
5835 show_oops(NULL
, "create_browser invalid parameters");
5839 t
->sb_h
= GTK_SCROLLBAR(gtk_hscrollbar_new(NULL
));
5840 t
->sb_v
= GTK_SCROLLBAR(gtk_vscrollbar_new(NULL
));
5841 t
->adjust_h
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_h
));
5842 t
->adjust_v
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_v
));
5844 w
= gtk_scrolled_window_new(t
->adjust_h
, t
->adjust_v
);
5845 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
5846 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
5848 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
5849 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
5852 t
->settings
= webkit_web_settings_new();
5854 g_object_set(t
->settings
, "default-encoding", encoding
, (char *)NULL
);
5856 if (user_agent
== NULL
) {
5857 g_object_get(G_OBJECT(t
->settings
), "user-agent", &strval
,
5859 t
->user_agent
= g_strdup_printf("%s %s+", strval
, version
);
5862 t
->user_agent
= g_strdup(user_agent
->value
);
5864 t
->stylesheet
= g_strdup_printf("file://%s/style.css", resource_dir
);
5865 t
->load_images
= auto_load_images
;
5868 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w
));
5869 g_signal_connect(G_OBJECT(adjustment
), "value-changed",
5870 G_CALLBACK(update_statusbar_position
), NULL
);
5879 create_kiosk_toolbar(struct tab
*t
)
5881 GtkWidget
*toolbar
= NULL
, *b
;
5883 b
= gtk_hbox_new(FALSE
, 0);
5885 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
5887 /* backward button */
5888 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
5889 gtk_widget_set_sensitive(t
->backward
, FALSE
);
5890 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
5891 G_CALLBACK(backward_cb
), t
);
5892 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, TRUE
, TRUE
, 0);
5894 /* forward button */
5895 t
->forward
= create_button("Forward", GTK_STOCK_GO_FORWARD
, 0);
5896 gtk_widget_set_sensitive(t
->forward
, FALSE
);
5897 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
5898 G_CALLBACK(forward_cb
), t
);
5899 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, TRUE
, TRUE
, 0);
5902 t
->gohome
= create_button("Home", GTK_STOCK_HOME
, 0);
5903 gtk_widget_set_sensitive(t
->gohome
, true);
5904 g_signal_connect(G_OBJECT(t
->gohome
), "clicked",
5905 G_CALLBACK(home_cb
), t
);
5906 gtk_box_pack_start(GTK_BOX(b
), t
->gohome
, TRUE
, TRUE
, 0);
5908 /* create widgets but don't use them */
5909 t
->uri_entry
= gtk_entry_new();
5910 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
5911 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
5912 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
5918 create_toolbar(struct tab
*t
)
5920 GtkWidget
*toolbar
= NULL
, *b
, *eb1
;
5922 b
= gtk_hbox_new(FALSE
, 0);
5924 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
5926 /* backward button */
5927 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
5928 gtk_widget_set_sensitive(t
->backward
, FALSE
);
5929 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
5930 G_CALLBACK(backward_cb
), t
);
5931 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, FALSE
, FALSE
, 0);
5933 /* forward button */
5934 t
->forward
= create_button("Forward",GTK_STOCK_GO_FORWARD
, 0);
5935 gtk_widget_set_sensitive(t
->forward
, FALSE
);
5936 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
5937 G_CALLBACK(forward_cb
), t
);
5938 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, FALSE
,
5942 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
5943 gtk_widget_set_sensitive(t
->stop
, FALSE
);
5944 g_signal_connect(G_OBJECT(t
->stop
), "clicked",
5945 G_CALLBACK(stop_cb
), t
);
5946 gtk_box_pack_start(GTK_BOX(b
), t
->stop
, FALSE
,
5950 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
5951 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
5952 gtk_widget_set_sensitive(t
->js_toggle
, TRUE
);
5953 g_signal_connect(G_OBJECT(t
->js_toggle
), "clicked",
5954 G_CALLBACK(js_toggle_cb
), t
);
5955 gtk_box_pack_start(GTK_BOX(b
), t
->js_toggle
, FALSE
, FALSE
, 0);
5957 t
->uri_entry
= gtk_entry_new();
5958 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
5959 G_CALLBACK(activate_uri_entry_cb
), t
);
5960 g_signal_connect(G_OBJECT(t
->uri_entry
), "key-press-event",
5961 G_CALLBACK(entry_key_cb
), t
);
5963 eb1
= gtk_hbox_new(FALSE
, 0);
5964 gtk_container_set_border_width(GTK_CONTAINER(eb1
), 1);
5965 gtk_box_pack_start(GTK_BOX(eb1
), t
->uri_entry
, TRUE
, TRUE
, 0);
5966 gtk_box_pack_start(GTK_BOX(b
), eb1
, TRUE
, TRUE
, 0);
5969 if (search_string
) {
5971 t
->search_entry
= gtk_entry_new();
5972 gtk_entry_set_width_chars(GTK_ENTRY(t
->search_entry
), 30);
5973 g_signal_connect(G_OBJECT(t
->search_entry
), "activate",
5974 G_CALLBACK(activate_search_entry_cb
), t
);
5975 g_signal_connect(G_OBJECT(t
->search_entry
), "key-press-event",
5976 G_CALLBACK(entry_key_cb
), t
);
5977 gtk_widget_set_size_request(t
->search_entry
, -1, -1);
5978 eb2
= gtk_hbox_new(FALSE
, 0);
5979 gtk_container_set_border_width(GTK_CONTAINER(eb2
), 1);
5980 gtk_box_pack_start(GTK_BOX(eb2
), t
->search_entry
, TRUE
, TRUE
,
5982 gtk_box_pack_start(GTK_BOX(b
), eb2
, FALSE
, FALSE
, 0);
5989 create_buffers(struct tab
*t
)
5991 GtkCellRenderer
*renderer
;
5994 view
= gtk_tree_view_new();
5996 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view
), FALSE
);
5998 renderer
= gtk_cell_renderer_text_new();
5999 gtk_tree_view_insert_column_with_attributes
6000 (GTK_TREE_VIEW(view
), -1, "Id", renderer
, "text", COL_ID
, (char *)NULL
);
6002 renderer
= gtk_cell_renderer_pixbuf_new();
6003 gtk_tree_view_insert_column_with_attributes
6004 (GTK_TREE_VIEW(view
), -1, "Favicon", renderer
, "pixbuf", COL_FAVICON
,
6007 renderer
= gtk_cell_renderer_text_new();
6008 gtk_tree_view_insert_column_with_attributes
6009 (GTK_TREE_VIEW(view
), -1, "Title", renderer
, "text", COL_TITLE
,
6012 gtk_tree_view_set_model
6013 (GTK_TREE_VIEW(view
), GTK_TREE_MODEL(buffers_store
));
6019 row_activated_cb(GtkTreeView
*view
, GtkTreePath
*path
,
6020 GtkTreeViewColumn
*col
, struct tab
*t
)
6025 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6027 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
,
6030 (GTK_TREE_MODEL(buffers_store
), &iter
, COL_ID
, &id
, -1);
6031 set_current_tab(id
- 1);
6037 /* after tab reordering/creation/removal */
6044 TAILQ_FOREACH(t
, &tabs
, entry
) {
6045 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
6046 if (t
->tab_id
> maxid
)
6049 gtk_widget_show(t
->tab_elems
.sep
);
6052 TAILQ_FOREACH(t
, &tabs
, entry
) {
6053 if (t
->tab_id
== maxid
) {
6054 gtk_widget_hide(t
->tab_elems
.sep
);
6060 /* after active tab change */
6062 recolor_compact_tabs(void)
6068 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6069 TAILQ_FOREACH(t
, &tabs
, entry
)
6070 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
,
6073 curid
= gtk_notebook_get_current_page(notebook
);
6074 TAILQ_FOREACH(t
, &tabs
, entry
)
6075 if (t
->tab_id
== curid
) {
6076 gdk_color_parse(XT_COLOR_CT_ACTIVE
, &color
);
6077 gtk_widget_modify_fg(t
->tab_elems
.label
,
6078 GTK_STATE_NORMAL
, &color
);
6084 set_current_tab(int page_num
)
6086 buffercmd_abort(get_current_tab());
6087 gtk_notebook_set_current_page(notebook
, page_num
);
6088 recolor_compact_tabs();
6092 undo_close_tab_save(struct tab
*t
)
6096 struct undo
*u1
, *u2
;
6098 WebKitWebHistoryItem
*item
;
6100 if ((uri
= get_uri(t
)) == NULL
)
6103 u1
= g_malloc0(sizeof(struct undo
));
6104 u1
->uri
= g_strdup(uri
);
6106 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
6108 m
= webkit_web_back_forward_list_get_forward_length(t
->bfl
);
6109 n
= webkit_web_back_forward_list_get_back_length(t
->bfl
);
6112 /* forward history */
6113 items
= webkit_web_back_forward_list_get_forward_list_with_limit(t
->bfl
, m
);
6117 u1
->history
= g_list_prepend(u1
->history
,
6118 webkit_web_history_item_copy(item
));
6119 items
= g_list_next(items
);
6124 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
6125 u1
->history
= g_list_prepend(u1
->history
,
6126 webkit_web_history_item_copy(item
));
6130 items
= webkit_web_back_forward_list_get_back_list_with_limit(t
->bfl
, n
);
6134 u1
->history
= g_list_prepend(u1
->history
,
6135 webkit_web_history_item_copy(item
));
6136 items
= g_list_next(items
);
6139 TAILQ_INSERT_HEAD(&undos
, u1
, entry
);
6141 if (undo_count
> XT_MAX_UNDO_CLOSE_TAB
) {
6142 u2
= TAILQ_LAST(&undos
, undo_tailq
);
6143 TAILQ_REMOVE(&undos
, u2
, entry
);
6145 g_list_free(u2
->history
);
6154 delete_tab(struct tab
*t
)
6158 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
6164 * no need to join thread here because it won't access t on completion
6167 TAILQ_REMOVE(&tabs
, t
, entry
);
6170 /* Halt all webkit activity. */
6171 abort_favicon_download(t
);
6172 webkit_web_view_stop_loading(t
->wv
);
6174 /* Save the tab, so we can undo the close. */
6175 undo_close_tab_save(t
);
6179 g_source_remove(t
->search_id
);
6182 bzero(&a
, sizeof a
);
6184 inspector_cmd(t
, &a
);
6186 if (browser_mode
== XT_BM_KIOSK
) {
6187 gtk_widget_destroy(t
->uri_entry
);
6188 gtk_widget_destroy(t
->stop
);
6189 gtk_widget_destroy(t
->js_toggle
);
6192 gtk_widget_destroy(t
->tab_elems
.eventbox
);
6193 gtk_widget_destroy(t
->vbox
);
6195 g_free(t
->user_agent
);
6196 g_free(t
->stylesheet
);
6201 if (TAILQ_EMPTY(&tabs
)) {
6202 if (browser_mode
== XT_BM_KIOSK
)
6203 create_new_tab(home
, NULL
, 1, -1);
6205 create_new_tab(NULL
, NULL
, 1, -1);
6208 /* recreate session */
6209 if (session_autosave
) {
6210 bzero(&a
, sizeof a
);
6212 save_tabs(NULL
, &a
);
6216 recolor_compact_tabs();
6220 update_statusbar_zoom(struct tab
*t
)
6223 char s
[16] = { '\0' };
6225 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6226 if ((zoom
<= 0.99 || zoom
>= 1.01))
6227 snprintf(s
, sizeof s
, "%d%%", (int)(zoom
* 100));
6228 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.zoom
), s
);
6232 setzoom_webkit(struct tab
*t
, int adjust
)
6234 #define XT_ZOOMPERCENT 0.04
6239 show_oops(NULL
, "setzoom_webkit invalid parameters");
6243 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6244 if (adjust
== XT_ZOOM_IN
)
6245 zoom
+= XT_ZOOMPERCENT
;
6246 else if (adjust
== XT_ZOOM_OUT
)
6247 zoom
-= XT_ZOOMPERCENT
;
6248 else if (adjust
> 0)
6249 zoom
= default_zoom_level
+ adjust
/ 100.0 - 1.0;
6251 show_oops(t
, "setzoom_webkit invalid zoom value");
6255 if (zoom
< XT_ZOOMPERCENT
)
6256 zoom
= XT_ZOOMPERCENT
;
6257 g_object_set(G_OBJECT(t
->wv
), "zoom-level", zoom
, (char *)NULL
);
6258 update_statusbar_zoom(t
);
6262 tab_clicked_cb(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
6264 struct tab
*t
= (struct tab
*) data
;
6266 DNPRINTF(XT_D_TAB
, "tab_clicked_cb: tab: %d\n", t
->tab_id
);
6268 switch (event
->button
) {
6270 set_current_tab(t
->tab_id
);
6281 append_tab(struct tab
*t
)
6286 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
6287 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
6291 create_sbe(int width
)
6295 sbe
= gtk_entry_new();
6296 gtk_entry_set_inner_border(GTK_ENTRY(sbe
), NULL
);
6297 gtk_entry_set_has_frame(GTK_ENTRY(sbe
), FALSE
);
6298 gtk_widget_set_can_focus(GTK_WIDGET(sbe
), FALSE
);
6299 gtk_widget_modify_font(GTK_WIDGET(sbe
), statusbar_font
);
6300 gtk_entry_set_alignment(GTK_ENTRY(sbe
), 1.0);
6301 gtk_widget_set_size_request(sbe
, width
, -1);
6307 create_new_tab(char *title
, struct undo
*u
, int focus
, int position
)
6312 WebKitWebHistoryItem
*item
;
6316 int sbe_p
= 0, sbe_b
= 0,
6319 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
6321 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
6322 DNPRINTF(XT_D_TAB
, "create_new_tab: new tab rejected\n");
6326 t
= g_malloc0(sizeof *t
);
6328 if (title
== NULL
) {
6329 title
= "(untitled)";
6333 t
->vbox
= gtk_vbox_new(FALSE
, 0);
6335 /* label + button for tab */
6336 b
= gtk_hbox_new(FALSE
, 0);
6339 #if GTK_CHECK_VERSION(2, 20, 0)
6340 t
->spinner
= gtk_spinner_new();
6342 t
->label
= gtk_label_new(title
);
6343 bb
= create_button("Close", GTK_STOCK_CLOSE
, 1);
6344 gtk_widget_set_size_request(t
->label
, 100, 0);
6345 gtk_label_set_max_width_chars(GTK_LABEL(t
->label
), 20);
6346 gtk_label_set_ellipsize(GTK_LABEL(t
->label
), PANGO_ELLIPSIZE_END
);
6347 gtk_widget_set_size_request(b
, 130, 0);
6349 gtk_box_pack_start(GTK_BOX(b
), bb
, FALSE
, FALSE
, 0);
6350 gtk_box_pack_start(GTK_BOX(b
), t
->label
, FALSE
, FALSE
, 0);
6351 #if GTK_CHECK_VERSION(2, 20, 0)
6352 gtk_box_pack_start(GTK_BOX(b
), t
->spinner
, FALSE
, FALSE
, 0);
6356 if (browser_mode
== XT_BM_KIOSK
) {
6357 t
->toolbar
= create_kiosk_toolbar(t
);
6358 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
6361 t
->toolbar
= create_toolbar(t
);
6363 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
,
6371 t
->browser_win
= create_browser(t
);
6372 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
6374 /* oops message for user feedback */
6375 t
->oops
= gtk_entry_new();
6376 gtk_entry_set_inner_border(GTK_ENTRY(t
->oops
), NULL
);
6377 gtk_entry_set_has_frame(GTK_ENTRY(t
->oops
), FALSE
);
6378 gtk_widget_set_can_focus(GTK_WIDGET(t
->oops
), FALSE
);
6379 gdk_color_parse(XT_COLOR_RED
, &color
);
6380 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
6381 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
6382 gtk_widget_modify_font(GTK_WIDGET(t
->oops
), oops_font
);
6385 t
->cmd
= gtk_entry_new();
6386 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
6387 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
6388 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
6389 gtk_widget_modify_font(GTK_WIDGET(t
->cmd
), cmd_font
);
6392 t
->statusbar_box
= gtk_hbox_new(FALSE
, 0);
6394 t
->sbe
.statusbar
= gtk_entry_new();
6395 gtk_entry_set_inner_border(GTK_ENTRY(t
->sbe
.statusbar
), NULL
);
6396 gtk_entry_set_has_frame(GTK_ENTRY(t
->sbe
.statusbar
), FALSE
);
6397 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.statusbar
), FALSE
);
6398 gtk_widget_modify_font(GTK_WIDGET(t
->sbe
.statusbar
), statusbar_font
);
6400 /* create these widgets only if specified in statusbar_elems */
6402 t
->sbe
.position
= create_sbe(40);
6403 t
->sbe
.zoom
= create_sbe(40);
6404 t
->sbe
.buffercmd
= create_sbe(60);
6406 statusbar_modify_attr(t
, XT_COLOR_WHITE
, XT_COLOR_BLACK
);
6408 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), t
->sbe
.statusbar
, TRUE
,
6411 /* gtk widgets cannot be added to a box twice. sbe_* variables
6412 make sure of this */
6413 for (p
= statusbar_elems
; *p
!= '\0'; p
++) {
6417 GtkWidget
*sep
= gtk_vseparator_new();
6419 gdk_color_parse(XT_COLOR_SB_SEPARATOR
, &color
);
6420 gtk_widget_modify_bg(sep
, GTK_STATE_NORMAL
, &color
);
6421 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), sep
,
6422 FALSE
, FALSE
, FALSE
);
6427 warnx("flag \"%c\" specified more than "
6428 "once in statusbar_elems\n", *p
);
6432 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6433 t
->sbe
.position
, FALSE
, FALSE
, FALSE
);
6437 warnx("flag \"%c\" specified more than "
6438 "once in statusbar_elems\n", *p
);
6442 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6443 t
->sbe
.buffercmd
, FALSE
, FALSE
, FALSE
);
6447 warnx("flag \"%c\" specified more than "
6448 "once in statusbar_elems\n", *p
);
6452 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6453 t
->sbe
.zoom
, FALSE
, FALSE
, FALSE
);
6456 warnx("illegal flag \"%c\" in statusbar_elems\n", *p
);
6461 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar_box
, FALSE
, FALSE
, 0);
6464 t
->buffers
= create_buffers(t
);
6465 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->buffers
, FALSE
, FALSE
, 0);
6467 /* xtp meaning is normal by default */
6468 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
6470 /* set empty favicon */
6471 xt_icon_from_name(t
, "text-html");
6473 /* and show it all */
6474 gtk_widget_show_all(b
);
6475 gtk_widget_show_all(t
->vbox
);
6477 /* compact tab bar */
6478 t
->tab_elems
.label
= gtk_label_new(title
);
6479 t
->tab_elems
.favicon
= gtk_image_new();
6480 gtk_label_set_width_chars(GTK_LABEL(t
->tab_elems
.label
), 1.0);
6481 gtk_misc_set_alignment(GTK_MISC(t
->tab_elems
.label
), 0.0, 0.0);
6482 gtk_misc_set_padding(GTK_MISC(t
->tab_elems
.label
), 4.0, 4.0);
6483 gtk_widget_modify_font(GTK_WIDGET(t
->tab_elems
.label
), tabbar_font
);
6485 t
->tab_elems
.eventbox
= gtk_event_box_new();
6486 t
->tab_elems
.box
= gtk_hbox_new(FALSE
, 0);
6487 t
->tab_elems
.sep
= gtk_vseparator_new();
6489 gdk_color_parse(XT_COLOR_CT_BACKGROUND
, &color
);
6490 gtk_widget_modify_bg(t
->tab_elems
.eventbox
, GTK_STATE_NORMAL
, &color
);
6491 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6492 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
, &color
);
6493 gdk_color_parse(XT_COLOR_CT_SEPARATOR
, &color
);
6494 gtk_widget_modify_bg(t
->tab_elems
.sep
, GTK_STATE_NORMAL
, &color
);
6496 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.favicon
, FALSE
,
6498 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.label
, TRUE
,
6500 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.sep
, FALSE
,
6502 gtk_container_add(GTK_CONTAINER(t
->tab_elems
.eventbox
),
6505 gtk_box_pack_start(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
, TRUE
,
6507 gtk_widget_show_all(t
->tab_elems
.eventbox
);
6509 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
6512 id
= position
>= 0 ? position
:
6513 gtk_notebook_get_current_page(notebook
) + 1;
6514 if (id
> gtk_notebook_get_n_pages(notebook
))
6517 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
6518 gtk_notebook_insert_page(notebook
, t
->vbox
, b
, id
);
6519 gtk_box_reorder_child(GTK_BOX(tab_bar
),
6520 t
->tab_elems
.eventbox
, id
);
6525 #if GTK_CHECK_VERSION(2, 20, 0)
6526 /* turn spinner off if we are a new tab without uri */
6528 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
6529 gtk_widget_hide(t
->spinner
);
6532 /* make notebook tabs reorderable */
6533 gtk_notebook_set_tab_reorderable(notebook
, t
->vbox
, TRUE
);
6535 /* compact tabs clickable */
6536 g_signal_connect(G_OBJECT(t
->tab_elems
.eventbox
),
6537 "button_press_event", G_CALLBACK(tab_clicked_cb
), t
);
6539 g_object_connect(G_OBJECT(t
->cmd
),
6540 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb
), t
,
6541 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
6542 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb
), t
,
6543 "signal::activate", G_CALLBACK(cmd_activate_cb
), t
,
6544 "signal::populate-popup", G_CALLBACK(cmd_popup_cb
), t
,
6547 /* reuse wv_button_cb to hide oops */
6548 g_object_connect(G_OBJECT(t
->oops
),
6549 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
6552 g_signal_connect(t
->buffers
,
6553 "row-activated", G_CALLBACK(row_activated_cb
), t
);
6554 g_object_connect(G_OBJECT(t
->buffers
),
6555 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
, (char *)NULL
);
6557 g_object_connect(G_OBJECT(t
->wv
),
6558 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
,
6559 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb
), t
,
6560 "signal::download-requested", G_CALLBACK(webview_download_cb
), t
,
6561 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), t
,
6562 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
6563 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
6564 "signal::create-web-view", G_CALLBACK(webview_cwv_cb
), t
,
6565 "signal::close-web-view", G_CALLBACK(webview_closewv_cb
), t
,
6566 "signal::event", G_CALLBACK(webview_event_cb
), t
,
6567 "signal::load-finished", G_CALLBACK(webview_load_finished_cb
), t
,
6568 "signal::load-progress-changed", G_CALLBACK(webview_progress_changed_cb
), t
,
6569 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb
), t
,
6570 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
6571 "signal::button_release_event", G_CALLBACK(wv_release_button_cb
), t
,
6572 "signal::populate-popup", G_CALLBACK(wv_popup_cb
), t
,
6574 g_signal_connect(t
->wv
,
6575 "notify::load-status", G_CALLBACK(notify_load_status_cb
), t
);
6576 g_signal_connect(t
->wv
,
6577 "notify::title", G_CALLBACK(notify_title_cb
), t
);
6579 /* hijack the unused keys as if we were the browser */
6580 //g_object_connect(G_OBJECT(t->toolbar),
6581 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
6584 g_signal_connect(G_OBJECT(bb
), "button_press_event",
6585 G_CALLBACK(tab_close_cb
), t
);
6588 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
6589 /* restore the tab's history */
6590 if (u
&& u
->history
) {
6594 webkit_web_back_forward_list_add_item(t
->bfl
, item
);
6595 items
= g_list_next(items
);
6598 item
= g_list_nth_data(u
->history
, u
->back
);
6600 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
6603 g_list_free(u
->history
);
6605 webkit_web_back_forward_list_clear(t
->bfl
);
6611 url_set_visibility();
6612 statusbar_set_visibility();
6615 set_current_tab(t
->tab_id
);
6616 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
6620 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
6624 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6631 if (userstyle_global
)
6634 recolor_compact_tabs();
6635 setzoom_webkit(t
, XT_ZOOM_NORMAL
);
6640 notebook_switchpage_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
6646 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
6648 if (gtk_notebook_get_current_page(notebook
) == -1)
6651 TAILQ_FOREACH(t
, &tabs
, entry
) {
6652 if (t
->tab_id
== pn
) {
6653 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
6656 uri
= get_title(t
, TRUE
);
6657 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
6663 /* can't use focus_webview here */
6664 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6671 notebook_pagereordered_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
6674 struct tab
*t
= NULL
, *tt
;
6678 TAILQ_FOREACH(tt
, &tabs
, entry
)
6679 if (tt
->tab_id
== pn
) {
6685 DNPRINTF(XT_D_TAB
, "page_reordered_cb: tab: %d\n", t
->tab_id
);
6687 gtk_box_reorder_child(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
,
6692 menuitem_response(struct tab
*t
)
6694 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
6698 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
6700 GtkWidget
*menu
, *menu_items
;
6701 GdkEventButton
*bevent
;
6705 if (event
->type
== GDK_BUTTON_PRESS
) {
6706 bevent
= (GdkEventButton
*) event
;
6707 menu
= gtk_menu_new();
6709 TAILQ_FOREACH(ti
, &tabs
, entry
) {
6710 if ((uri
= get_uri(ti
)) == NULL
)
6711 /* XXX make sure there is something to print */
6712 /* XXX add gui pages in here to look purdy */
6714 menu_items
= gtk_menu_item_new_with_label(uri
);
6715 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), menu_items
);
6716 gtk_widget_show(menu_items
);
6718 g_signal_connect_swapped((menu_items
),
6719 "activate", G_CALLBACK(menuitem_response
),
6723 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
,
6724 bevent
->button
, bevent
->time
);
6726 /* unref object so it'll free itself when popped down */
6727 #if !GTK_CHECK_VERSION(3, 0, 0)
6728 /* XXX does not need unref with gtk+3? */
6729 g_object_ref_sink(menu
);
6730 g_object_unref(menu
);
6733 return (TRUE
/* eat event */);
6736 return (FALSE
/* propagate */);
6740 icon_size_map(int iconsz
)
6742 if (iconsz
<= GTK_ICON_SIZE_INVALID
||
6743 iconsz
> GTK_ICON_SIZE_DIALOG
)
6744 return (GTK_ICON_SIZE_SMALL_TOOLBAR
);
6750 create_button(char *name
, char *stockid
, int size
)
6752 GtkWidget
*button
, *image
;
6756 rcstring
= g_strdup_printf(
6757 "style \"%s-style\"\n"
6759 " GtkWidget::focus-padding = 0\n"
6760 " GtkWidget::focus-line-width = 0\n"
6764 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
6765 gtk_rc_parse_string(rcstring
);
6767 button
= gtk_button_new();
6768 gtk_button_set_focus_on_click(GTK_BUTTON(button
), FALSE
);
6769 gtk_icon_size
= icon_size_map(size
? size
: icon_size
);
6771 image
= gtk_image_new_from_stock(stockid
, gtk_icon_size
);
6772 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
6773 gtk_container_set_border_width(GTK_CONTAINER(button
), 1);
6774 gtk_container_add(GTK_CONTAINER(button
), GTK_WIDGET(image
));
6775 gtk_widget_set_name(button
, name
);
6776 gtk_button_set_relief(GTK_BUTTON(button
), GTK_RELIEF_NONE
);
6782 button_set_stockid(GtkWidget
*button
, char *stockid
)
6786 image
= gtk_image_new_from_stock(stockid
, icon_size_map(icon_size
));
6787 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
6788 gtk_button_set_image(GTK_BUTTON(button
), image
);
6792 clipb_primary_cb(GtkClipboard
*primary
, GdkEvent
*event
, gpointer notused
)
6795 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
6798 if (xterm_workaround
== 0)
6802 * xterm doesn't play nice with clipboards because it clears the
6803 * primary when clicked. We rely on primary being set to properly
6804 * handle middle mouse button clicks (paste). So when someone clears
6805 * primary copy whatever is in CUT_BUFFER0 into primary to simualte
6806 * other application behavior (as in DON'T clear primary).
6809 p
= gtk_clipboard_wait_for_text(primary
);
6811 if (gdk_property_get(gdk_get_default_root_window(),
6813 gdk_atom_intern("STRING", FALSE
),
6815 1024 * 1024 /* picked out of my butt */,
6821 /* yes sir, we need to NUL the string */
6823 gtk_clipboard_set_text(primary
, p
, -1);
6837 char file
[PATH_MAX
];
6840 vbox
= gtk_vbox_new(FALSE
, 0);
6841 gtk_box_set_spacing(GTK_BOX(vbox
), 0);
6842 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
6843 #if !GTK_CHECK_VERSION(3, 0, 0)
6844 /* XXX seems to be needed with gtk+2 */
6845 gtk_notebook_set_tab_hborder(notebook
, 0);
6846 gtk_notebook_set_tab_vborder(notebook
, 0);
6848 gtk_notebook_set_scrollable(notebook
, TRUE
);
6849 gtk_notebook_set_show_border(notebook
, FALSE
);
6850 gtk_widget_set_can_focus(GTK_WIDGET(notebook
), FALSE
);
6852 abtn
= gtk_button_new();
6853 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
6854 gtk_widget_set_size_request(arrow
, -1, -1);
6855 gtk_container_add(GTK_CONTAINER(abtn
), arrow
);
6856 gtk_widget_set_size_request(abtn
, -1, 20);
6858 #if GTK_CHECK_VERSION(2, 20, 0)
6859 gtk_notebook_set_action_widget(notebook
, abtn
, GTK_PACK_END
);
6861 gtk_widget_set_size_request(GTK_WIDGET(notebook
), -1, -1);
6863 /* compact tab bar */
6864 tab_bar
= gtk_hbox_new(TRUE
, 0);
6866 gtk_box_pack_start(GTK_BOX(vbox
), tab_bar
, FALSE
, FALSE
, 0);
6867 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
6868 gtk_widget_set_size_request(vbox
, -1, -1);
6870 g_object_connect(G_OBJECT(notebook
),
6871 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb
), NULL
,
6873 g_object_connect(G_OBJECT(notebook
),
6874 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb
),
6875 NULL
, (char *)NULL
);
6876 g_signal_connect(G_OBJECT(abtn
), "button_press_event",
6877 G_CALLBACK(arrow_cb
), NULL
);
6879 main_window
= create_window("xxxterm");
6880 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
6881 g_signal_connect(G_OBJECT(main_window
), "delete_event",
6882 G_CALLBACK(gtk_main_quit
), NULL
);
6885 for (i
= 0; i
< LENGTH(icons
); i
++) {
6886 snprintf(file
, sizeof file
, "%s/%s", resource_dir
, icons
[i
]);
6887 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
6888 l
= g_list_append(l
, pb
);
6890 gtk_window_set_default_icon_list(l
);
6892 /* clipboard work around */
6893 if (xterm_workaround
)
6895 G_OBJECT(gtk_clipboard_get(GDK_SELECTION_PRIMARY
)),
6896 "owner-change", G_CALLBACK(clipb_primary_cb
), NULL
);
6898 gtk_widget_show_all(abtn
);
6899 gtk_widget_show_all(main_window
);
6900 notebook_tab_set_visibility();
6904 send_cmd_to_socket(char *cmd
)
6907 struct sockaddr_un sa
;
6909 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
6910 warnx("%s: socket", __func__
);
6914 sa
.sun_family
= AF_UNIX
;
6915 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
6916 work_dir
, XT_SOCKET_FILE
);
6919 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
6920 warnx("%s: connect", __func__
);
6924 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
6925 warnx("%s: send", __func__
);
6936 socket_watcher(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
6939 char str
[XT_MAX_URL_LENGTH
];
6940 socklen_t t
= sizeof(struct sockaddr_un
);
6941 struct sockaddr_un sa
;
6946 gint fd
= g_io_channel_unix_get_fd(source
);
6948 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
6953 if (getpeereid(s
, &uid
, &gid
) == -1) {
6957 if (uid
!= getuid() || gid
!= getgid()) {
6958 warnx("unauthorized user");
6964 warnx("not a valid user");
6968 n
= recv(s
, str
, sizeof(str
), 0);
6972 tt
= TAILQ_LAST(&tabs
, tab_list
);
6973 cmd_execute(tt
, str
);
6981 struct sockaddr_un sa
;
6983 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
6984 warn("is_running: socket");
6988 sa
.sun_family
= AF_UNIX
;
6989 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
6990 work_dir
, XT_SOCKET_FILE
);
6993 /* connect to see if there is a listener */
6994 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
6995 rv
= 0; /* not running */
6997 rv
= 1; /* already running */
7008 struct sockaddr_un sa
;
7010 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7011 warn("build_socket: socket");
7015 sa
.sun_family
= AF_UNIX
;
7016 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
7017 work_dir
, XT_SOCKET_FILE
);
7020 /* connect to see if there is a listener */
7021 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7022 /* no listener so we will */
7023 unlink(sa
.sun_path
);
7025 if (bind(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7026 warn("build_socket: bind");
7030 if (listen(s
, 1) == -1) {
7031 warn("build_socket: listen");
7048 if (stat(dir
, &sb
)) {
7049 if (mkdir(dir
, S_IRWXU
) == -1)
7050 err(1, "mkdir %s", dir
);
7052 err(1, "stat %s", dir
);
7054 if (S_ISDIR(sb
.st_mode
) == 0)
7055 errx(1, "%s not a dir", dir
);
7056 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
7057 warnx("fixing invalid permissions on %s", dir
);
7058 if (chmod(dir
, S_IRWXU
) == -1)
7059 err(1, "chmod %s", dir
);
7067 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
7071 GStaticRecMutex my_gdk_mtx
= G_STATIC_REC_MUTEX_INIT
;
7072 volatile int mtx_depth
;
7076 * The linux flash plugin violates the gdk locking mechanism.
7077 * Work around the issue by using a recursive mutex with some match applied
7078 * to see if we hit a buggy condition.
7080 * The following code is painful so just don't read it. It really doesn't
7081 * make much sense but seems to work.
7086 g_static_rec_mutex_lock(&my_gdk_mtx
);
7089 if (mtx_depth
<= 0) {
7090 /* should not happen */
7091 show_oops(NULL
, "negative mutex locking bug, trying to "
7093 fprintf(stderr
, "negative mutex locking bug, trying to "
7095 g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
7096 g_static_rec_mutex_lock(&my_gdk_mtx
);
7101 if (mtx_depth
!= 1) {
7102 /* decrease mutext depth to 1 */
7104 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7106 } while (mtx_depth
> 1);
7115 /* if mutex depth isn't 1 then something went bad */
7116 if (mtx_depth
!= 1) {
7117 x
= g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
7119 /* should not happen */
7120 show_oops(NULL
, "mutex unlocking bug, trying to "
7122 fprintf(stderr
, "mutex unlocking bug, trying to "
7126 if (mtx_complain
== 0) {
7127 show_oops(NULL
, "buggy mutex implementation detected, "
7128 "work around implemented");
7129 fprintf(stderr
, "buggy mutex implementation detected, "
7130 "work around implemented");
7137 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7141 main(int argc
, char *argv
[])
7144 int c
, s
, optn
= 0, opte
= 0, focus
= 1;
7145 char conf
[PATH_MAX
] = { '\0' };
7146 char file
[PATH_MAX
];
7147 char *env_proxy
= NULL
;
7151 struct sigaction sact
;
7152 GIOChannel
*channel
;
7159 g_thread_init(NULL
);
7160 gdk_threads_set_lock_functions(mtx_lock
, mtx_unlock
);
7162 gdk_threads_enter();
7164 gcry_control (GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
7166 gtk_init(&argc
, &argv
);
7168 gnutls_global_init();
7170 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
7175 RB_INIT(&downloads
);
7177 TAILQ_INIT(&sessions
);
7180 TAILQ_INIT(&aliases
);
7186 TAILQ_INIT(&ua_list
);
7188 /* fiddle with ulimits */
7189 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7192 /* just use them all */
7193 rlp
.rlim_cur
= rlp
.rlim_max
;
7194 if (setrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7196 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7198 else if (rlp
.rlim_cur
<= 256)
7199 startpage_add("%s requires at least 256 file "
7200 "descriptors, currently it has up to %d available",
7201 __progname
, rlp
.rlim_cur
);
7204 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
7213 #ifdef XXXTERM_BUILDSTR
7214 errx(0 , "Version: %s Build: %s",
7215 version
, XXXTERM_BUILDSTR
);
7217 errx(0 , "Version: %s", version
);
7221 strlcpy(conf
, optarg
, sizeof(conf
));
7224 strlcpy(named_session
, optarg
, sizeof(named_session
));
7245 xtp_generate_keys();
7248 bzero(&sact
, sizeof(sact
));
7249 sigemptyset(&sact
.sa_mask
);
7250 sact
.sa_handler
= sigchild
;
7251 sact
.sa_flags
= SA_NOCLDSTOP
;
7252 sigaction(SIGCHLD
, &sact
, NULL
);
7254 /* set download dir */
7255 pwd
= getpwuid(getuid());
7257 errx(1, "invalid user %d", getuid());
7258 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
7260 /* compile buffer command regexes */
7263 /* set default string settings */
7264 home
= g_strdup("https://www.cyphertite.com");
7265 search_string
= g_strdup("https://ssl.scroogle.org/cgi-bin/nbbwssl.cgi?Gw=%s");
7266 resource_dir
= g_strdup("/usr/local/share/xxxterm/");
7267 strlcpy(runtime_settings
, "runtime", sizeof runtime_settings
);
7268 cmd_font_name
= g_strdup("monospace normal 9");
7269 oops_font_name
= g_strdup("monospace normal 9");
7270 statusbar_font_name
= g_strdup("monospace normal 9");
7271 tabbar_font_name
= g_strdup("monospace normal 9");
7272 statusbar_elems
= g_strdup("BP");
7273 spell_check_languages
= g_strdup("en_US");
7274 encoding
= g_strdup("UTF-8");
7276 /* read config file */
7277 if (strlen(conf
) == 0)
7278 snprintf(conf
, sizeof conf
, "%s/.%s",
7279 pwd
->pw_dir
, XT_CONF_FILE
);
7280 config_parse(conf
, 0);
7283 cmd_font
= pango_font_description_from_string(cmd_font_name
);
7284 oops_font
= pango_font_description_from_string(oops_font_name
);
7285 statusbar_font
= pango_font_description_from_string(statusbar_font_name
);
7286 tabbar_font
= pango_font_description_from_string(tabbar_font_name
);
7288 /* working directory */
7289 if (strlen(work_dir
) == 0)
7290 snprintf(work_dir
, sizeof work_dir
, "%s/%s",
7291 pwd
->pw_dir
, XT_DIR
);
7294 /* icon cache dir */
7295 snprintf(cache_dir
, sizeof cache_dir
, "%s/%s", work_dir
, XT_CACHE_DIR
);
7299 snprintf(certs_dir
, sizeof certs_dir
, "%s/%s", work_dir
, XT_CERT_DIR
);
7303 snprintf(sessions_dir
, sizeof sessions_dir
, "%s/%s",
7304 work_dir
, XT_SESSIONS_DIR
);
7305 xxx_dir(sessions_dir
);
7308 snprintf(js_dir
, sizeof js_dir
, "%s/%s", work_dir
, XT_JS_DIR
);
7312 snprintf(temp_dir
, sizeof temp_dir
, "%s/%s", work_dir
, XT_TEMP_DIR
);
7315 /* runtime settings that can override config file */
7316 if (runtime_settings
[0] != '\0')
7317 config_parse(runtime_settings
, 1);
7320 if (!strcmp(download_dir
, pwd
->pw_dir
))
7321 strlcat(download_dir
, "/downloads", sizeof download_dir
);
7322 xxx_dir(download_dir
);
7324 /* favorites file */
7325 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_FAVS_FILE
);
7326 if (stat(file
, &sb
)) {
7327 warnx("favorites file doesn't exist, creating it");
7328 if ((f
= fopen(file
, "w")) == NULL
)
7329 err(1, "favorites");
7333 /* quickmarks file */
7334 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
7335 if (stat(file
, &sb
)) {
7336 warnx("quickmarks file doesn't exist, creating it");
7337 if ((f
= fopen(file
, "w")) == NULL
)
7338 err(1, "quickmarks");
7342 /* search history */
7343 if (history_autosave
) {
7344 snprintf(search_file
, sizeof search_file
, "%s/%s",
7345 work_dir
, XT_SEARCH_FILE
);
7346 if (stat(search_file
, &sb
)) {
7347 warnx("search history file doesn't exist, creating it");
7348 if ((f
= fopen(search_file
, "w")) == NULL
)
7349 err(1, "search_history");
7352 history_read(&shl
, search_file
, &search_history_count
);
7355 /* command history */
7356 if (history_autosave
) {
7357 snprintf(command_file
, sizeof command_file
, "%s/%s",
7358 work_dir
, XT_COMMAND_FILE
);
7359 if (stat(command_file
, &sb
)) {
7360 warnx("command history file doesn't exist, creating it");
7361 if ((f
= fopen(command_file
, "w")) == NULL
)
7362 err(1, "command_history");
7365 history_read(&chl
, command_file
, &cmd_history_count
);
7369 session
= webkit_get_default_session();
7374 if (stat(ssl_ca_file
, &sb
)) {
7375 warnx("no CA file: %s", ssl_ca_file
);
7376 g_free(ssl_ca_file
);
7379 g_object_set(session
,
7380 SOUP_SESSION_SSL_CA_FILE
, ssl_ca_file
,
7381 SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
7385 /* guess_search regex */
7386 if (url_regex
== NULL
)
7387 url_regex
= g_strdup(XT_URL_REGEX
);
7389 if (regcomp(&url_re
, url_regex
, REG_EXTENDED
| REG_NOSUB
))
7390 startpage_add("invalid url regex %s", url_regex
);
7393 env_proxy
= getenv("http_proxy");
7395 setup_proxy(env_proxy
);
7397 env_proxy
= getenv("HTTP_PROXY");
7399 setup_proxy(env_proxy
);
7401 setup_proxy(http_proxy
);
7405 send_cmd_to_socket(argv
[0]);
7409 /* set some connection parameters */
7410 g_object_set(session
, "max-conns", max_connections
, (char *)NULL
);
7411 g_object_set(session
, "max-conns-per-host", max_host_connections
,
7414 /* see if there is already an xxxterm running */
7415 if (single_instance
&& is_running()) {
7417 warnx("already running");
7422 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
7423 send_cmd_to_socket(cmd
);
7433 /* uri completion */
7434 completion_model
= gtk_list_store_new(1, G_TYPE_STRING
);
7437 buffers_store
= gtk_list_store_new
7438 (NUM_COLS
, G_TYPE_UINT
, GDK_TYPE_PIXBUF
, G_TYPE_STRING
);
7444 notebook_tab_set_visibility();
7446 if (save_global_history
)
7447 restore_global_history();
7449 /* restore session list */
7450 restore_sessions_list();
7452 if (!strcmp(named_session
, XT_SAVED_TABS_FILE
))
7453 restore_saved_tabs();
7455 a
.s
= named_session
;
7456 a
.i
= XT_SES_DONOTHING
;
7457 open_tabs(NULL
, &a
);
7460 /* see if we have an exception */
7461 if (!TAILQ_EMPTY(&spl
)) {
7462 create_new_tab("about:startpage", NULL
, focus
, -1);
7467 create_new_tab(argv
[0], NULL
, focus
, -1);
7474 if (TAILQ_EMPTY(&tabs
))
7475 create_new_tab(home
, NULL
, 1, -1);
7478 if ((s
= build_socket()) != -1) {
7479 channel
= g_io_channel_unix_new(s
);
7480 g_io_add_watch(channel
, G_IO_IN
, socket_watcher
, NULL
);
7486 gdk_threads_leave();
7487 g_static_rec_mutex_unlock_full(&my_gdk_mtx
); /* just in case */
7490 gnutls_global_deinit();