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
48 GCRY_THREAD_OPTION_PTHREAD_IMPL
;
60 TAILQ_ENTRY(session
) entry
;
63 TAILQ_HEAD(session_list
, session
);
66 TAILQ_ENTRY(undo
) entry
;
69 int back
; /* Keeps track of how many back
70 * history items there are. */
72 TAILQ_HEAD(undo_tailq
, undo
);
74 struct command_entry
{
76 TAILQ_ENTRY(command_entry
) entry
;
78 TAILQ_HEAD(command_list
, command_entry
);
81 #define XT_DIR (".xxxterm")
82 #define XT_CACHE_DIR ("cache")
83 #define XT_CERT_DIR ("certs/")
84 #define XT_JS_DIR ("js/")
85 #define XT_SESSIONS_DIR ("sessions/")
86 #define XT_CONF_FILE ("xxxterm.conf")
87 #define XT_QMARKS_FILE ("quickmarks")
88 #define XT_SAVED_TABS_FILE ("main_session")
89 #define XT_RESTART_TABS_FILE ("restart_tabs")
90 #define XT_SOCKET_FILE ("socket")
91 #define XT_HISTORY_FILE ("history")
92 #define XT_SAVE_SESSION_ID ("SESSION_NAME=")
93 #define XT_SEARCH_FILE ("search_history")
94 #define XT_COMMAND_FILE ("command_history")
95 #define XT_DLMAN_REFRESH "10"
96 #define XT_MAX_URL_LENGTH (4096) /* 1 page is atomic, don't make bigger */
97 #define XT_MAX_UNDO_CLOSE_TAB (32)
98 #define XT_RESERVED_CHARS "$&+,/:;=?@ \"<>#%%{}|^~[]`"
99 #define XT_PRINT_EXTRA_MARGIN 10
100 #define XT_URL_REGEX ("^[[:blank:]]*[^[:blank:]]*([[:alnum:]-]+\\.)+[[:alnum:]-][^[:blank:]]*[[:blank:]]*$")
101 #define XT_INVALID_MARK (-1) /* XXX this is a double, maybe use something else, like a nan */
104 #define XT_COLOR_RED "#cc0000"
105 #define XT_COLOR_YELLOW "#ffff66"
106 #define XT_COLOR_BLUE "lightblue"
107 #define XT_COLOR_GREEN "#99ff66"
108 #define XT_COLOR_WHITE "white"
109 #define XT_COLOR_BLACK "black"
111 #define XT_COLOR_CT_BACKGROUND "#000000"
112 #define XT_COLOR_CT_INACTIVE "#dddddd"
113 #define XT_COLOR_CT_ACTIVE "#bbbb00"
114 #define XT_COLOR_CT_SEPARATOR "#555555"
116 #define XT_COLOR_SB_SEPARATOR "#555555"
118 #define XT_PROTO_DELIM "://"
121 #define XT_MOVE_INVALID (0)
122 #define XT_MOVE_DOWN (1)
123 #define XT_MOVE_UP (2)
124 #define XT_MOVE_BOTTOM (3)
125 #define XT_MOVE_TOP (4)
126 #define XT_MOVE_PAGEDOWN (5)
127 #define XT_MOVE_PAGEUP (6)
128 #define XT_MOVE_HALFDOWN (7)
129 #define XT_MOVE_HALFUP (8)
130 #define XT_MOVE_LEFT (9)
131 #define XT_MOVE_FARLEFT (10)
132 #define XT_MOVE_RIGHT (11)
133 #define XT_MOVE_FARRIGHT (12)
134 #define XT_MOVE_PERCENT (13)
135 #define XT_MOVE_CENTER (14)
137 #define XT_QMARK_SET (0)
138 #define XT_QMARK_OPEN (1)
139 #define XT_QMARK_TAB (2)
141 #define XT_MARK_SET (0)
142 #define XT_MARK_GOTO (1)
144 #define XT_TAB_LAST (-4)
145 #define XT_TAB_FIRST (-3)
146 #define XT_TAB_PREV (-2)
147 #define XT_TAB_NEXT (-1)
148 #define XT_TAB_INVALID (0)
149 #define XT_TAB_NEW (1)
150 #define XT_TAB_DELETE (2)
151 #define XT_TAB_DELQUIT (3)
152 #define XT_TAB_OPEN (4)
153 #define XT_TAB_UNDO_CLOSE (5)
154 #define XT_TAB_SHOW (6)
155 #define XT_TAB_HIDE (7)
156 #define XT_TAB_NEXTSTYLE (8)
158 #define XT_NAV_INVALID (0)
159 #define XT_NAV_BACK (1)
160 #define XT_NAV_FORWARD (2)
161 #define XT_NAV_RELOAD (3)
162 #define XT_NAV_STOP (4)
164 #define XT_FOCUS_INVALID (0)
165 #define XT_FOCUS_URI (1)
166 #define XT_FOCUS_SEARCH (2)
168 #define XT_SEARCH_INVALID (0)
169 #define XT_SEARCH_NEXT (1)
170 #define XT_SEARCH_PREV (2)
172 #define XT_PASTE_CURRENT_TAB (0)
173 #define XT_PASTE_NEW_TAB (1)
175 #define XT_ZOOM_IN (-1)
176 #define XT_ZOOM_OUT (-2)
177 #define XT_ZOOM_NORMAL (100)
179 #define XT_URL_SHOW (1)
180 #define XT_URL_HIDE (2)
182 #define XT_CMD_OPEN (0)
183 #define XT_CMD_OPEN_CURRENT (1)
184 #define XT_CMD_TABNEW (2)
185 #define XT_CMD_TABNEW_CURRENT (3)
187 #define XT_STATUS_NOTHING (0)
188 #define XT_STATUS_LINK (1)
189 #define XT_STATUS_URI (2)
190 #define XT_STATUS_LOADING (3)
192 #define XT_SES_DONOTHING (0)
193 #define XT_SES_CLOSETABS (1)
195 #define XT_PREFIX (1<<0)
196 #define XT_USERARG (1<<1)
197 #define XT_URLARG (1<<2)
198 #define XT_INTARG (1<<3)
199 #define XT_SESSARG (1<<4)
200 #define XT_SETARG (1<<5)
202 #define XT_HINT_NEWTAB (1<<0)
204 #define XT_BUFCMD_SZ (8)
206 #define XT_EJS_SHOW (1<<0)
208 GtkWidget
* create_button(char *, char *, int);
210 void recalc_tabs(void);
211 void recolor_compact_tabs(void);
212 void set_current_tab(int page_num
);
213 gboolean
update_statusbar_position(GtkAdjustment
*, gpointer
);
214 void marks_clear(struct tab
*t
);
217 extern char *__progname
;
220 GtkWidget
*main_window
;
221 GtkNotebook
*notebook
;
223 GtkWidget
*arrow
, *abtn
;
224 struct tab_list tabs
;
225 struct history_list hl
;
226 struct session_list sessions
;
227 struct domain_list c_wl
;
228 struct domain_list js_wl
;
229 struct domain_list pl_wl
;
230 struct undo_tailq undos
;
231 struct keybinding_list kbl
;
233 struct command_list chl
;
234 struct command_list shl
;
235 struct command_entry
*history_at
;
236 struct command_entry
*search_at
;
238 int cmd_history_count
= 0;
239 int search_history_count
= 0;
241 long long unsigned int blocked_cookies
= 0;
242 char named_session
[PATH_MAX
];
243 GtkListStore
*completion_model
;
244 GtkListStore
*buffers_store
;
246 char *qmarks
[XT_NOMARKS
];
247 int btn_down
; /* M1 down in any wv */
248 regex_t url_re
; /* guess_search regex */
250 /* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
251 int next_download_id
= 1;
253 void xxx_dir(char *);
254 int icon_size_map(int);
255 void completion_add(struct tab
*);
256 void completion_add_uri(const gchar
*);
259 history_delete(struct command_list
*l
, int *counter
)
261 struct command_entry
*c
;
263 if (l
== NULL
|| counter
== NULL
)
266 c
= TAILQ_LAST(l
, command_list
);
270 TAILQ_REMOVE(l
, c
, entry
);
277 history_add(struct command_list
*list
, char *file
, char *l
, int *counter
)
279 struct command_entry
*c
;
282 if (list
== NULL
|| l
== NULL
|| counter
== NULL
)
285 /* don't add the same line */
286 c
= TAILQ_FIRST(list
);
288 if (!strcmp(c
->line
+ 1 /* skip space */, l
))
291 c
= g_malloc0(sizeof *c
);
292 c
->line
= g_strdup_printf(" %s", l
);
295 TAILQ_INSERT_HEAD(list
, c
, entry
);
298 history_delete(list
, counter
);
300 if (history_autosave
&& file
) {
301 f
= fopen(file
, "w");
303 show_oops(NULL
, "couldn't write history %s", file
);
307 TAILQ_FOREACH_REVERSE(c
, list
, command_list
, entry
) {
309 fprintf(f
, "%s\n", c
->line
);
317 history_read(struct command_list
*list
, char *file
, int *counter
)
320 char *s
, line
[65536];
322 if (list
== NULL
|| file
== NULL
)
325 f
= fopen(file
, "r");
327 startpage_add("couldn't open history file %s", file
);
332 s
= fgets(line
, sizeof line
, f
);
333 if (s
== NULL
|| feof(f
) || ferror(f
))
335 if ((s
= strchr(line
, '\n')) == NULL
) {
336 startpage_add("invalid history file %s", file
);
342 history_add(list
, NULL
, line
+ 1, counter
);
350 /* marks and quickmarks array storage.
351 * first a-z, then A-Z, then 0-9 */
358 if (i
>= 0 && i
<= 'z' - 'a')
362 if (i
>= 0 && i
<= 'Z' - 'A')
377 if (m
>= 'a' && m
<= 'z')
378 return ret
+ m
- 'a';
380 ret
+= 'z' - 'a' + 1;
381 if (m
>= 'A' && m
<= 'Z')
382 return ret
+ m
- 'A';
384 ret
+= 'Z' - 'A' + 1;
385 if (m
>= '0' && m
<= '9')
386 return ret
+ m
- '0';
395 int saved_errno
, status
;
400 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
404 if (errno
!= ECHILD
) {
406 clog_warn("sigchild: waitpid:");
412 if (WIFEXITED(status
)) {
413 if (WEXITSTATUS(status
) != 0) {
415 clog_warnx("sigchild: child exit status: %d",
416 WEXITSTATUS(status));
421 clog_warnx("sigchild: child is terminated abnormally");
430 is_g_object_setting(GObject
*o
, char *str
)
432 guint n_props
= 0, i
;
433 GParamSpec
**proplist
;
435 if (! G_IS_OBJECT(o
))
438 proplist
= g_object_class_list_properties(G_OBJECT_GET_CLASS(o
),
441 for (i
=0; i
< n_props
; i
++) {
442 if (! strcmp(proplist
[i
]->name
, str
))
449 get_current_tab(void)
453 TAILQ_FOREACH(t
, &tabs
, entry
) {
454 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
458 warnx("%s: no current tab", __func__
);
464 set_status(struct tab
*t
, gchar
*s
, int status
)
472 case XT_STATUS_LOADING
:
473 type
= g_strdup_printf("Loading: %s", s
);
477 type
= g_strdup_printf("Link: %s", s
);
479 t
->status
= g_strdup(gtk_entry_get_text(
480 GTK_ENTRY(t
->sbe
.statusbar
)));
484 type
= g_strdup_printf("%s", s
);
486 t
->status
= g_strdup(type
);
490 t
->status
= g_strdup(s
);
492 case XT_STATUS_NOTHING
:
497 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.statusbar
), s
);
503 hide_cmd(struct tab
*t
)
505 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
507 history_at
= NULL
; /* just in case */
508 search_at
= NULL
; /* just in case */
509 gtk_widget_hide(t
->cmd
);
513 show_cmd(struct tab
*t
)
515 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
519 gtk_widget_hide(t
->oops
);
520 gtk_widget_show(t
->cmd
);
524 hide_buffers(struct tab
*t
)
526 gtk_widget_hide(t
->buffers
);
527 gtk_list_store_clear(buffers_store
);
537 sort_tabs_by_page_num(struct tab
***stabs
)
542 num_tabs
= gtk_notebook_get_n_pages(notebook
);
544 *stabs
= g_malloc0(num_tabs
* sizeof(struct tab
*));
546 TAILQ_FOREACH(t
, &tabs
, entry
)
547 (*stabs
)[gtk_notebook_page_num(notebook
, t
->vbox
)] = t
;
553 buffers_make_list(void)
556 const gchar
*title
= NULL
;
558 struct tab
**stabs
= NULL
;
560 num_tabs
= sort_tabs_by_page_num(&stabs
);
562 for (i
= 0; i
< num_tabs
; i
++)
564 gtk_list_store_append(buffers_store
, &iter
);
565 title
= get_title(stabs
[i
], FALSE
);
566 gtk_list_store_set(buffers_store
, &iter
,
567 COL_ID
, i
+ 1, /* Enumerate the tabs starting from 1
577 show_buffers(struct tab
*t
)
580 gtk_widget_show(t
->buffers
);
581 gtk_widget_grab_focus(GTK_WIDGET(t
->buffers
));
585 toggle_buffers(struct tab
*t
)
587 if (gtk_widget_get_visible(t
->buffers
))
594 buffers(struct tab
*t
, struct karg
*args
)
602 hide_oops(struct tab
*t
)
604 gtk_widget_hide(t
->oops
);
608 show_oops(struct tab
*at
, const char *fmt
, ...)
612 struct tab
*t
= NULL
;
618 if ((t
= get_current_tab()) == NULL
)
624 if (vasprintf(&msg
, fmt
, ap
) == -1)
625 errx(1, "show_oops failed");
628 gtk_entry_set_text(GTK_ENTRY(t
->oops
), msg
);
629 gtk_widget_hide(t
->cmd
);
630 gtk_widget_show(t
->oops
);
636 char work_dir
[PATH_MAX
];
637 char certs_dir
[PATH_MAX
];
638 char js_dir
[PATH_MAX
];
639 char cache_dir
[PATH_MAX
];
640 char sessions_dir
[PATH_MAX
];
641 char cookie_file
[PATH_MAX
];
642 SoupSession
*session
;
643 SoupCookieJar
*s_cookiejar
;
644 SoupCookieJar
*p_cookiejar
;
645 char rc_fname
[PATH_MAX
];
647 struct mime_type_list mtl
;
648 struct alias_list aliases
;
651 struct tab
*create_new_tab(char *, struct undo
*, int, int);
652 void delete_tab(struct tab
*);
653 void setzoom_webkit(struct tab
*, int);
654 int run_script(struct tab
*, char *);
655 int download_rb_cmp(struct download
*, struct download
*);
656 gboolean
cmd_execute(struct tab
*t
, char *str
);
659 history_rb_cmp(struct history
*h1
, struct history
*h2
)
661 return (strcmp(h1
->uri
, h2
->uri
));
663 RB_GENERATE(history_list
, history
, entry
, history_rb_cmp
);
666 domain_rb_cmp(struct domain
*d1
, struct domain
*d2
)
668 return (strcmp(d1
->d
, d2
->d
));
670 RB_GENERATE(domain_list
, domain
, entry
, domain_rb_cmp
);
673 download_rb_cmp(struct download
*e1
, struct download
*e2
)
675 return (e1
->id
< e2
->id
? -1 : e1
->id
> e2
->id
);
677 RB_GENERATE(download_list
, download
, entry
, download_rb_cmp
);
679 struct valid_url_types
{
690 valid_url_type(char *url
)
694 for (i
= 0; i
< LENGTH(vut
); i
++)
695 if (!strncasecmp(vut
[i
].type
, url
, strlen(vut
[i
].type
)))
702 match_alias(char *url_in
)
706 char *url_out
= NULL
, *search
, *enc_arg
;
708 search
= g_strdup(url_in
);
710 if (strsep(&arg
, " \t") == NULL
) {
711 show_oops(NULL
, "match_alias: NULL URL");
715 TAILQ_FOREACH(a
, &aliases
, entry
) {
716 if (!strcmp(search
, a
->a_name
))
721 DNPRINTF(XT_D_URL
, "match_alias: matched alias %s\n",
724 enc_arg
= soup_uri_encode(arg
, XT_RESERVED_CHARS
);
725 url_out
= g_strdup_printf(a
->a_uri
, enc_arg
);
728 url_out
= g_strdup_printf(a
->a_uri
, "");
736 guess_url_type(char *url_in
)
739 char *url_out
= NULL
, *enc_search
= NULL
;
742 /* substitute aliases */
743 url_out
= match_alias(url_in
);
747 /* see if we are an about page */
748 if (!strncmp(url_in
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
))
749 for (i
= 0; i
< about_list_size(); i
++)
750 if (!strcmp(&url_in
[XT_URI_ABOUT_LEN
],
751 about_list
[i
].name
)) {
752 url_out
= g_strdup(url_in
);
756 if (guess_search
&& url_regex
&&
757 !(g_str_has_prefix(url_in
, "http://") ||
758 g_str_has_prefix(url_in
, "https://"))) {
759 if (regexec(&url_re
, url_in
, 0, NULL
, 0)) {
760 /* invalid URI so search instead */
761 enc_search
= soup_uri_encode(url_in
, XT_RESERVED_CHARS
);
762 url_out
= g_strdup_printf(search_string
, enc_search
);
768 /* XXX not sure about this heuristic */
769 if (stat(url_in
, &sb
) == 0)
770 url_out
= g_strdup_printf("file://%s", url_in
);
772 url_out
= g_strdup_printf("http://%s", url_in
); /* guess http */
774 DNPRINTF(XT_D_URL
, "guess_url_type: guessed %s\n", url_out
);
780 load_uri(struct tab
*t
, gchar
*uri
)
783 gchar
*newuri
= NULL
;
789 /* Strip leading spaces. */
790 while (*uri
&& isspace(*uri
))
793 if (strlen(uri
) == 0) {
798 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
800 if (valid_url_type(uri
)) {
801 newuri
= guess_url_type(uri
);
805 if (!strncmp(uri
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
)) {
806 for (i
= 0; i
< about_list_size(); i
++)
807 if (!strcmp(&uri
[XT_URI_ABOUT_LEN
], about_list
[i
].name
)) {
808 bzero(&args
, sizeof args
);
809 about_list
[i
].func(t
, &args
);
810 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
),
814 show_oops(t
, "invalid about page");
818 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
820 webkit_web_view_load_uri(t
->wv
, uri
);
827 get_uri(struct tab
*t
)
829 const gchar
*uri
= NULL
;
831 if (webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
)
833 if (t
->xtp_meaning
== XT_XTP_TAB_MEANING_NORMAL
) {
834 uri
= webkit_web_view_get_uri(t
->wv
);
836 /* use tmp_uri to make sure it is g_freed */
839 t
->tmp_uri
=g_strdup_printf("%s%s", XT_URI_ABOUT
,
840 about_list
[t
->xtp_meaning
].name
);
847 get_title(struct tab
*t
, bool window
)
849 const gchar
*set
= NULL
, *title
= NULL
;
850 WebKitLoadStatus status
= webkit_web_view_get_load_status(t
->wv
);
852 if (status
== WEBKIT_LOAD_PROVISIONAL
|| status
== WEBKIT_LOAD_FAILED
||
853 t
->xtp_meaning
== XT_XTP_TAB_MEANING_BL
)
856 title
= webkit_web_view_get_title(t
->wv
);
857 if ((set
= title
? title
: get_uri(t
)))
861 set
= window
? XT_NAME
: "(untitled)";
867 find_mime_type(char *mime_type
)
869 struct mime_type
*m
, *def
= NULL
, *rv
= NULL
;
871 TAILQ_FOREACH(m
, &mtl
, entry
) {
873 !strncmp(mime_type
, m
->mt_type
, strlen(m
->mt_type
)))
876 if (m
->mt_default
== 0 && !strcmp(mime_type
, m
->mt_type
)) {
889 wl_find_uri(const gchar
*s
, struct domain_list
*wl
)
895 if (s
== NULL
|| wl
== NULL
)
898 if (!strncmp(s
, "http://", strlen("http://")))
899 s
= &s
[strlen("http://")];
900 else if (!strncmp(s
, "https://", strlen("https://")))
901 s
= &s
[strlen("https://")];
906 for (i
= 0; i
< strlen(s
) + 1 /* yes er need this */; i
++)
907 /* chop string at first slash */
908 if (s
[i
] == '/' || s
[i
] == ':' || s
[i
] == '\0') {
920 js_ref_to_string(JSContextRef context
, JSValueRef ref
)
926 jsref
= JSValueToStringCopy(context
, ref
, NULL
);
930 l
= JSStringGetMaximumUTF8CStringSize(jsref
);
933 JSStringGetUTF8CString(jsref
, s
, l
);
934 JSStringRelease(jsref
);
940 disable_hints(struct tab
*t
)
942 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
944 run_script(t
, "hints.clearHints();");
945 t
->mode
= XT_MODE_COMMAND
;
950 enable_hints(struct tab
*t
)
952 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
955 run_script(t
, "hints.createHints('', 'F');");
957 run_script(t
, "hints.createHints('', 'f');");
958 t
->mode
= XT_MODE_HINT
;
961 #define XT_JS_DONE ("done;")
962 #define XT_JS_DONE_LEN (strlen(XT_JS_DONE))
963 #define XT_JS_INSERT ("insert;")
964 #define XT_JS_INSERT_LEN (strlen(XT_JS_INSERT))
967 run_script(struct tab
*t
, char *s
)
969 JSGlobalContextRef ctx
;
970 WebKitWebFrame
*frame
;
972 JSValueRef val
, exception
;
975 DNPRINTF(XT_D_JS
, "run_script: tab %d %s\n",
976 t
->tab_id
, s
== (char *)JS_HINTING
? "JS_HINTING" : s
);
978 frame
= webkit_web_view_get_main_frame(t
->wv
);
979 ctx
= webkit_web_frame_get_global_context(frame
);
981 str
= JSStringCreateWithUTF8CString(s
);
982 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
983 NULL
, 0, &exception
);
984 JSStringRelease(str
);
986 DNPRINTF(XT_D_JS
, "run_script: val %p\n", val
);
988 es
= js_ref_to_string(ctx
, exception
);
990 /* show_oops(t, "script exception: %s", es); */
991 DNPRINTF(XT_D_JS
, "run_script: exception %s\n", es
);
996 es
= js_ref_to_string(ctx
, val
);
999 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
1001 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
1005 DNPRINTF(XT_D_JS
, "run_script: val %s\n", es
);
1014 hint(struct tab
*t
, struct karg
*args
)
1017 DNPRINTF(XT_D_JS
, "hint: tab %d args %d\n", t
->tab_id
, args
->i
);
1019 if (t
->mode
== XT_MODE_HINT
) {
1020 if (args
->i
== XT_HINT_NEWTAB
)
1030 apply_style(struct tab
*t
)
1032 g_object_set(G_OBJECT(t
->settings
),
1033 "user-stylesheet-uri", t
->stylesheet
, (char *)NULL
);
1037 userstyle(struct tab
*t
, struct karg
*args
)
1039 DNPRINTF(XT_D_JS
, "userstyle: tab %d\n", t
->tab_id
);
1043 g_object_set(G_OBJECT(t
->settings
),
1044 "user-stylesheet-uri", NULL
, (char *)NULL
);
1053 * Doesn't work fully, due to the following bug:
1054 * https://bugs.webkit.org/show_bug.cgi?id=51747
1057 restore_global_history(void)
1059 char file
[PATH_MAX
];
1064 const char delim
[3] = {'\\', '\\', '\0'};
1066 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_HISTORY_FILE
);
1068 if ((f
= fopen(file
, "r")) == NULL
) {
1069 warnx("%s: fopen", __func__
);
1074 if ((uri
= fparseln(f
, NULL
, NULL
, delim
, 0)) == NULL
)
1075 if (feof(f
) || ferror(f
))
1078 if ((title
= fparseln(f
, NULL
, NULL
, delim
, 0)) == NULL
)
1079 if (feof(f
) || ferror(f
)) {
1081 warnx("%s: broken history file\n", __func__
);
1085 if (uri
&& strlen(uri
) && title
&& strlen(title
)) {
1086 webkit_web_history_item_new_with_data(uri
, title
);
1087 h
= g_malloc(sizeof(struct history
));
1088 h
->uri
= g_strdup(uri
);
1089 h
->title
= g_strdup(title
);
1090 RB_INSERT(history_list
, &hl
, h
);
1091 completion_add_uri(h
->uri
);
1093 warnx("%s: failed to restore history\n", __func__
);
1109 save_global_history_to_disk(struct tab
*t
)
1111 char file
[PATH_MAX
];
1115 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_HISTORY_FILE
);
1117 if ((f
= fopen(file
, "w")) == NULL
) {
1118 show_oops(t
, "%s: global history file: %s",
1119 __func__
, strerror(errno
));
1123 RB_FOREACH_REVERSE(h
, history_list
, &hl
) {
1124 if (h
->uri
&& h
->title
)
1125 fprintf(f
, "%s\n%s\n", h
->uri
, h
->title
);
1134 quit(struct tab
*t
, struct karg
*args
)
1136 if (save_global_history
)
1137 save_global_history_to_disk(t
);
1145 restore_sessions_list(void)
1148 struct dirent
*dp
= NULL
;
1151 sdir
= opendir(sessions_dir
);
1153 while ((dp
= readdir(sdir
)) != NULL
)
1154 if (dp
->d_type
== DT_REG
) {
1155 s
= g_malloc(sizeof(struct session
));
1156 s
->name
= g_strdup(dp
->d_name
);
1157 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
1164 open_tabs(struct tab
*t
, struct karg
*a
)
1166 char file
[PATH_MAX
];
1170 struct tab
*ti
, *tt
;
1175 ti
= TAILQ_LAST(&tabs
, tab_list
);
1177 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, a
->s
);
1178 if ((f
= fopen(file
, "r")) == NULL
)
1182 if ((uri
= fparseln(f
, NULL
, NULL
, "\0\0\0", 0)) == NULL
)
1183 if (feof(f
) || ferror(f
))
1186 /* retrieve session name */
1187 if (uri
&& g_str_has_prefix(uri
, XT_SAVE_SESSION_ID
)) {
1188 strlcpy(named_session
,
1189 &uri
[strlen(XT_SAVE_SESSION_ID
)],
1190 sizeof named_session
);
1194 if (uri
&& strlen(uri
))
1195 create_new_tab(uri
, NULL
, 1, -1);
1201 /* close open tabs */
1202 if (a
->i
== XT_SES_CLOSETABS
&& ti
!= NULL
) {
1204 tt
= TAILQ_FIRST(&tabs
);
1225 restore_saved_tabs(void)
1227 char file
[PATH_MAX
];
1228 int unlink_file
= 0;
1233 snprintf(file
, sizeof file
, "%s/%s",
1234 sessions_dir
, XT_RESTART_TABS_FILE
);
1235 if (stat(file
, &sb
) == -1)
1236 a
.s
= XT_SAVED_TABS_FILE
;
1239 a
.s
= XT_RESTART_TABS_FILE
;
1242 a
.i
= XT_SES_DONOTHING
;
1243 rv
= open_tabs(NULL
, &a
);
1252 save_tabs(struct tab
*t
, struct karg
*a
)
1254 char file
[PATH_MAX
];
1256 int num_tabs
= 0, i
;
1257 struct tab
**stabs
= NULL
;
1259 /* tab may be null here */
1264 snprintf(file
, sizeof file
, "%s/%s",
1265 sessions_dir
, named_session
);
1267 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, a
->s
);
1269 if ((f
= fopen(file
, "w")) == NULL
) {
1270 show_oops(t
, "Can't open save_tabs file: %s", strerror(errno
));
1274 /* save session name */
1275 fprintf(f
, "%s%s\n", XT_SAVE_SESSION_ID
, named_session
);
1277 /* Save tabs, in the order they are arranged in the notebook. */
1278 num_tabs
= sort_tabs_by_page_num(&stabs
);
1280 for (i
= 0; i
< num_tabs
; i
++)
1282 if (get_uri(stabs
[i
]) != NULL
)
1283 fprintf(f
, "%s\n", get_uri(stabs
[i
]));
1284 else if (gtk_entry_get_text(GTK_ENTRY(
1285 stabs
[i
]->uri_entry
)))
1286 fprintf(f
, "%s\n", gtk_entry_get_text(GTK_ENTRY(
1287 stabs
[i
]->uri_entry
)));
1292 /* try and make sure this gets to disk NOW. XXX Backup first? */
1293 if (fflush(f
) != 0 || fsync(fileno(f
)) != 0) {
1294 show_oops(t
, "May not have managed to save session: %s",
1304 save_tabs_and_quit(struct tab
*t
, struct karg
*args
)
1316 run_page_script(struct tab
*t
, struct karg
*args
)
1319 char *tmp
, script
[PATH_MAX
];
1321 tmp
= args
->s
!= NULL
&& strlen(args
->s
) > 0 ? args
->s
: default_script
;
1322 if (tmp
[0] == '\0') {
1323 show_oops(t
, "no script specified");
1327 if ((uri
= get_uri(t
)) == NULL
) {
1328 show_oops(t
, "tab is empty, not running script");
1333 snprintf(script
, sizeof script
, "%s/%s",
1334 pwd
->pw_dir
, &tmp
[1]);
1336 strlcpy(script
, tmp
, sizeof script
);
1340 show_oops(t
, "can't fork to run script");
1350 execlp(script
, script
, uri
, (void *)NULL
);
1360 yank_uri(struct tab
*t
, struct karg
*args
)
1363 GtkClipboard
*clipboard
;
1365 if ((uri
= get_uri(t
)) == NULL
)
1368 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1369 gtk_clipboard_set_text(clipboard
, uri
, -1);
1375 paste_uri(struct tab
*t
, struct karg
*args
)
1377 GtkClipboard
*clipboard
;
1378 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
1380 gchar
*p
= NULL
, *uri
;
1382 /* try primary clipboard first */
1383 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1384 p
= gtk_clipboard_wait_for_text(clipboard
);
1386 /* if it failed get whatever text is in cut_buffer0 */
1387 if (p
== NULL
&& xterm_workaround
)
1388 if (gdk_property_get(gdk_get_default_root_window(),
1390 gdk_atom_intern("STRING", FALSE
),
1392 1024 * 1024 /* picked out of my butt */,
1398 /* yes sir, we need to NUL the string */
1404 while (*uri
&& isspace(*uri
))
1406 if (strlen(uri
) == 0) {
1407 show_oops(t
, "empty paste buffer");
1410 if (guess_search
== 0 && valid_url_type(uri
)) {
1411 /* we can be clever and paste this in search box */
1412 show_oops(t
, "not a valid URL");
1416 if (args
->i
== XT_PASTE_CURRENT_TAB
)
1418 else if (args
->i
== XT_PASTE_NEW_TAB
)
1419 create_new_tab(uri
, NULL
, 1, -1);
1430 js_toggle_cb(GtkWidget
*w
, struct tab
*t
)
1435 g_object_get(G_OBJECT(t
->settings
),
1436 "enable-scripts", &es
, (char *)NULL
);
1441 set
= XT_WL_DISABLE
;
1443 a
.i
= set
| XT_WL_TOPLEVEL
;
1446 a
.i
= set
| XT_WL_TOPLEVEL
;
1449 a
.i
= XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
;
1454 toggle_src(struct tab
*t
, struct karg
*args
)
1461 mode
= webkit_web_view_get_view_source_mode(t
->wv
);
1462 webkit_web_view_set_view_source_mode(t
->wv
, !mode
);
1463 webkit_web_view_reload(t
->wv
);
1469 focus_webview(struct tab
*t
)
1474 /* only grab focus if we are visible */
1475 if (gtk_notebook_get_current_page(notebook
) == t
->tab_id
)
1476 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
1480 focus(struct tab
*t
, struct karg
*args
)
1482 if (t
== NULL
|| args
== NULL
)
1488 if (args
->i
== XT_FOCUS_URI
)
1489 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
1490 else if (args
->i
== XT_FOCUS_SEARCH
)
1491 gtk_widget_grab_focus(GTK_WIDGET(t
->search_entry
));
1497 connect_socket_from_uri(const gchar
*uri
, const gchar
**error_str
, char *domain
,
1501 struct addrinfo hints
, *res
= NULL
, *ai
;
1502 int rv
= -1, s
= -1, on
, error
;
1504 static gchar myerror
[256]; /* this is not thread safe */
1507 *error_str
= myerror
;
1508 if (uri
&& !g_str_has_prefix(uri
, "https://")) {
1509 *error_str
= "invalid URI";
1513 su
= soup_uri_new(uri
);
1515 *error_str
= "invalid soup URI";
1518 if (!SOUP_URI_VALID_FOR_HTTP(su
)) {
1519 *error_str
= "invalid HTTPS URI";
1523 snprintf(port
, sizeof port
, "%d", su
->port
);
1524 bzero(&hints
, sizeof(struct addrinfo
));
1525 hints
.ai_flags
= AI_CANONNAME
;
1526 hints
.ai_family
= AF_UNSPEC
;
1527 hints
.ai_socktype
= SOCK_STREAM
;
1529 if ((error
= getaddrinfo(su
->host
, port
, &hints
, &res
))) {
1530 snprintf(myerror
, sizeof myerror
, "getaddrinfo failed: %s",
1531 gai_strerror(errno
));
1535 for (ai
= res
; ai
; ai
= ai
->ai_next
) {
1541 if (ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
)
1543 s
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
1546 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &on
,
1549 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) == 0)
1553 snprintf(myerror
, sizeof myerror
,
1554 "could not obtain certificates from: %s",
1560 strlcpy(domain
, su
->host
, domain_sz
);
1567 if (rv
== -1 && s
!= -1)
1574 stop_tls(gnutls_session_t gsession
, gnutls_certificate_credentials_t xcred
)
1577 gnutls_deinit(gsession
);
1579 gnutls_certificate_free_credentials(xcred
);
1585 start_tls(const gchar
**error_str
, int s
, gnutls_session_t
*gs
,
1586 gnutls_certificate_credentials_t
*xc
)
1588 gnutls_certificate_credentials_t xcred
;
1589 gnutls_session_t gsession
;
1591 static gchar myerror
[1024]; /* this is not thread safe */
1593 if (gs
== NULL
|| xc
== NULL
)
1600 gnutls_certificate_allocate_credentials(&xcred
);
1601 gnutls_certificate_set_x509_trust_file(xcred
, ssl_ca_file
,
1602 GNUTLS_X509_FMT_PEM
);
1604 gnutls_init(&gsession
, GNUTLS_CLIENT
);
1605 gnutls_priority_set_direct(gsession
, "PERFORMANCE", NULL
);
1606 gnutls_credentials_set(gsession
, GNUTLS_CRD_CERTIFICATE
, xcred
);
1607 gnutls_transport_set_ptr(gsession
, (gnutls_transport_ptr_t
)(long)s
);
1608 if ((rv
= gnutls_handshake(gsession
)) < 0) {
1609 snprintf(myerror
, sizeof myerror
,
1610 "gnutls_handshake failed %d fatal %d %s",
1612 gnutls_error_is_fatal(rv
),
1613 gnutls_strerror_name(rv
));
1614 stop_tls(gsession
, xcred
);
1618 gnutls_credentials_type_t cred
;
1619 cred
= gnutls_auth_get_type(gsession
);
1620 if (cred
!= GNUTLS_CRD_CERTIFICATE
) {
1621 snprintf(myerror
, sizeof myerror
,
1622 "gnutls_auth_get_type failed %d",
1624 stop_tls(gsession
, xcred
);
1632 *error_str
= myerror
;
1637 get_connection_certs(gnutls_session_t gsession
, gnutls_x509_crt_t
**certs
,
1641 const gnutls_datum_t
*cl
;
1642 gnutls_x509_crt_t
*all_certs
;
1645 if (certs
== NULL
|| cert_count
== NULL
)
1647 if (gnutls_certificate_type_get(gsession
) != GNUTLS_CRT_X509
)
1649 cl
= gnutls_certificate_get_peers(gsession
, &len
);
1653 all_certs
= g_malloc(sizeof(gnutls_x509_crt_t
) * len
);
1654 for (i
= 0; i
< len
; i
++) {
1655 gnutls_x509_crt_init(&all_certs
[i
]);
1656 if (gnutls_x509_crt_import(all_certs
[i
], &cl
[i
],
1657 GNUTLS_X509_FMT_PEM
< 0)) {
1671 free_connection_certs(gnutls_x509_crt_t
*certs
, size_t cert_count
)
1675 for (i
= 0; i
< cert_count
; i
++)
1676 gnutls_x509_crt_deinit(certs
[i
]);
1681 statusbar_modify_attr(struct tab
*t
, const char *text
, const char *base
)
1683 GdkColor c_text
, c_base
;
1685 gdk_color_parse(text
, &c_text
);
1686 gdk_color_parse(base
, &c_base
);
1688 gtk_widget_modify_text(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_text
);
1689 gtk_widget_modify_text(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_text
);
1690 gtk_widget_modify_text(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_text
);
1691 gtk_widget_modify_text(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_text
);
1693 gtk_widget_modify_base(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_base
);
1694 gtk_widget_modify_base(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_base
);
1695 gtk_widget_modify_base(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_base
);
1696 gtk_widget_modify_base(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_base
);
1700 save_certs(struct tab
*t
, gnutls_x509_crt_t
*certs
,
1701 size_t cert_count
, char *domain
)
1704 char cert_buf
[64 * 1024], file
[PATH_MAX
];
1709 if (t
== NULL
|| certs
== NULL
|| cert_count
<= 0 || domain
== NULL
)
1712 snprintf(file
, sizeof file
, "%s/%s", certs_dir
, domain
);
1713 if ((f
= fopen(file
, "w")) == NULL
) {
1714 show_oops(t
, "Can't create cert file %s %s",
1715 file
, strerror(errno
));
1719 for (i
= 0; i
< cert_count
; i
++) {
1720 cert_buf_sz
= sizeof cert_buf
;
1721 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1722 cert_buf
, &cert_buf_sz
)) {
1723 show_oops(t
, "gnutls_x509_crt_export failed");
1726 if (fwrite(cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1727 show_oops(t
, "Can't write certs: %s", strerror(errno
));
1732 /* not the best spot but oh well */
1733 gdk_color_parse("lightblue", &color
);
1734 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
1735 statusbar_modify_attr(t
, XT_COLOR_BLACK
, "lightblue");
1748 load_compare_cert(const gchar
*uri
, const gchar
**error_str
)
1750 char domain
[8182], file
[PATH_MAX
];
1751 char cert_buf
[64 * 1024], r_cert_buf
[64 * 1024];
1753 unsigned int error
= 0;
1755 size_t cert_buf_sz
, cert_count
;
1756 enum cert_trust rv
= CERT_UNTRUSTED
;
1757 static gchar serr
[80]; /* this isn't thread safe */
1758 gnutls_session_t gsession
;
1759 gnutls_x509_crt_t
*certs
;
1760 gnutls_certificate_credentials_t xcred
;
1762 DNPRINTF(XT_D_URL
, "%s: %s\n", __func__
, uri
);
1766 if ((s
= connect_socket_from_uri(uri
, error_str
, domain
,
1767 sizeof domain
)) == -1)
1770 DNPRINTF(XT_D_URL
, "%s: fd %d\n", __func__
, s
);
1773 if (start_tls(error_str
, s
, &gsession
, &xcred
))
1775 DNPRINTF(XT_D_URL
, "%s: got tls\n", __func__
);
1777 /* verify certs in case cert file doesn't exist */
1778 if (gnutls_certificate_verify_peers2(gsession
, &error
) !=
1780 *error_str
= "Invalid certificates";
1785 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1786 *error_str
= "Can't get connection certificates";
1790 snprintf(file
, sizeof file
, "%s/%s", certs_dir
, domain
);
1791 if ((f
= fopen(file
, "r")) == NULL
) {
1797 for (i
= 0; i
< cert_count
; i
++) {
1798 cert_buf_sz
= sizeof cert_buf
;
1799 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1800 cert_buf
, &cert_buf_sz
)) {
1803 if (fread(r_cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1804 rv
= CERT_BAD
; /* critical */
1807 if (bcmp(r_cert_buf
, cert_buf
, sizeof cert_buf_sz
)) {
1808 rv
= CERT_BAD
; /* critical */
1817 free_connection_certs(certs
, cert_count
);
1819 /* we close the socket first for speed */
1823 /* only complain if we didn't save it locally */
1824 if (error
&& rv
!= CERT_LOCAL
) {
1825 strlcpy(serr
, "Certificate exception(s): ", sizeof serr
);
1826 if (error
& GNUTLS_CERT_INVALID
)
1827 strlcat(serr
, "invalid, ", sizeof serr
);
1828 if (error
& GNUTLS_CERT_REVOKED
)
1829 strlcat(serr
, "revoked, ", sizeof serr
);
1830 if (error
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
1831 strlcat(serr
, "signer not found, ", sizeof serr
);
1832 if (error
& GNUTLS_CERT_SIGNER_NOT_CA
)
1833 strlcat(serr
, "not signed by CA, ", sizeof serr
);
1834 if (error
& GNUTLS_CERT_INSECURE_ALGORITHM
)
1835 strlcat(serr
, "insecure algorithm, ", sizeof serr
);
1836 if (error
& GNUTLS_CERT_NOT_ACTIVATED
)
1837 strlcat(serr
, "not activated, ", sizeof serr
);
1838 if (error
& GNUTLS_CERT_EXPIRED
)
1839 strlcat(serr
, "expired, ", sizeof serr
);
1840 for (i
= strlen(serr
) - 1; i
> 0; i
--)
1841 if (serr
[i
] == ',') {
1848 stop_tls(gsession
, xcred
);
1854 cert_cmd(struct tab
*t
, struct karg
*args
)
1856 const gchar
*uri
, *error_str
= NULL
;
1860 gnutls_session_t gsession
;
1861 gnutls_x509_crt_t
*certs
;
1862 gnutls_certificate_credentials_t xcred
;
1867 if (ssl_ca_file
== NULL
) {
1868 show_oops(t
, "Can't open CA file: %s", ssl_ca_file
);
1872 if ((uri
= get_uri(t
)) == NULL
) {
1873 show_oops(t
, "Invalid URI");
1877 if ((s
= connect_socket_from_uri(uri
, &error_str
, domain
,
1878 sizeof domain
)) == -1) {
1879 show_oops(t
, "%s", error_str
);
1884 if (start_tls(&error_str
, s
, &gsession
, &xcred
))
1888 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1889 show_oops(t
, "get_connection_certs failed");
1893 if (args
->i
& XT_SHOW
)
1894 show_certs(t
, certs
, cert_count
, "Certificate Chain");
1895 else if (args
->i
& XT_SAVE
)
1896 save_certs(t
, certs
, cert_count
, domain
);
1898 free_connection_certs(certs
, cert_count
);
1900 /* we close the socket first for speed */
1903 stop_tls(gsession
, xcred
);
1904 if (error_str
&& strlen(error_str
))
1905 show_oops(t
, "%s", error_str
);
1910 remove_cookie(int index
)
1916 DNPRINTF(XT_D_COOKIE
, "remove_cookie: %d\n", index
);
1918 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
1920 for (i
= 1; cf
; cf
= cf
->next
, i
++) {
1924 print_cookie("remove cookie", c
);
1925 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
1930 soup_cookies_free(cf
);
1936 toplevel_cmd(struct tab
*t
, struct karg
*args
)
1938 js_toggle_cb(t
->js_toggle
, t
);
1944 can_go_back_for_real(struct tab
*t
)
1947 WebKitWebHistoryItem
*item
;
1953 /* rely on webkit to make sure we can go backward when on an about page */
1955 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
1956 return (webkit_web_view_can_go_back(t
->wv
));
1958 /* the back/forwars list is stupid so help determine if we can go back */
1959 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
1961 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
1962 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
1970 can_go_forward_for_real(struct tab
*t
)
1973 WebKitWebHistoryItem
*item
;
1979 /* rely on webkit to make sure we can go forward when on an about page */
1981 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
1982 return (webkit_web_view_can_go_forward(t
->wv
));
1984 /* the back/forwars list is stupid so help selecting a different item */
1985 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
1987 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
1988 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
1996 go_back_for_real(struct tab
*t
)
1999 WebKitWebHistoryItem
*item
;
2007 webkit_web_view_go_back(t
->wv
);
2010 /* the back/forwars list is stupid so help selecting a different item */
2011 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2013 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2014 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2015 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2022 go_forward_for_real(struct tab
*t
)
2025 WebKitWebHistoryItem
*item
;
2033 webkit_web_view_go_forward(t
->wv
);
2036 /* the back/forwars list is stupid so help selecting a different item */
2037 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2039 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2040 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2041 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2048 navaction(struct tab
*t
, struct karg
*args
)
2050 WebKitWebHistoryItem
*item
;
2051 WebKitWebFrame
*frame
;
2053 DNPRINTF(XT_D_NAV
, "navaction: tab %d opcode %d\n",
2054 t
->tab_id
, args
->i
);
2056 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
2058 if (args
->i
== XT_NAV_BACK
)
2059 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2061 item
= webkit_web_back_forward_list_get_forward_item(t
->bfl
);
2063 return (XT_CB_PASSTHROUGH
);
2064 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2066 return (XT_CB_PASSTHROUGH
);
2072 go_back_for_real(t
);
2074 case XT_NAV_FORWARD
:
2076 go_forward_for_real(t
);
2079 frame
= webkit_web_view_get_main_frame(t
->wv
);
2080 webkit_web_frame_reload(frame
);
2083 frame
= webkit_web_view_get_main_frame(t
->wv
);
2084 webkit_web_frame_stop_loading(frame
);
2087 return (XT_CB_PASSTHROUGH
);
2091 move(struct tab
*t
, struct karg
*args
)
2093 GtkAdjustment
*adjust
;
2094 double pi
, si
, pos
, ps
, upper
, lower
, max
;
2100 case XT_MOVE_BOTTOM
:
2102 case XT_MOVE_PAGEDOWN
:
2103 case XT_MOVE_PAGEUP
:
2104 case XT_MOVE_HALFDOWN
:
2105 case XT_MOVE_HALFUP
:
2106 case XT_MOVE_PERCENT
:
2107 case XT_MOVE_CENTER
:
2108 adjust
= t
->adjust_v
;
2111 adjust
= t
->adjust_h
;
2115 pos
= gtk_adjustment_get_value(adjust
);
2116 ps
= gtk_adjustment_get_page_size(adjust
);
2117 upper
= gtk_adjustment_get_upper(adjust
);
2118 lower
= gtk_adjustment_get_lower(adjust
);
2119 si
= gtk_adjustment_get_step_increment(adjust
);
2120 pi
= gtk_adjustment_get_page_increment(adjust
);
2123 DNPRINTF(XT_D_MOVE
, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2124 "max %f si %f pi %f\n",
2125 args
->i
, adjust
== t
->adjust_h
? "horizontal" : "vertical",
2126 pos
, ps
, upper
, lower
, max
, si
, pi
);
2132 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2137 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2139 case XT_MOVE_BOTTOM
:
2140 case XT_MOVE_FARRIGHT
:
2141 gtk_adjustment_set_value(adjust
, max
);
2144 case XT_MOVE_FARLEFT
:
2145 gtk_adjustment_set_value(adjust
, lower
);
2147 case XT_MOVE_PAGEDOWN
:
2149 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2151 case XT_MOVE_PAGEUP
:
2153 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2155 case XT_MOVE_HALFDOWN
:
2157 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2159 case XT_MOVE_HALFUP
:
2161 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2163 case XT_MOVE_CENTER
:
2164 args
->s
= g_strdup("50.0");
2166 case XT_MOVE_PERCENT
:
2167 percent
= atoi(args
->s
) / 100.0;
2168 pos
= max
* percent
;
2169 if (pos
< 0.0 || pos
> max
)
2171 gtk_adjustment_set_value(adjust
, pos
);
2174 return (XT_CB_PASSTHROUGH
);
2177 DNPRINTF(XT_D_MOVE
, "move: new pos %f %f\n", pos
, MIN(pos
, max
));
2179 return (XT_CB_HANDLED
);
2183 url_set_visibility(void)
2187 TAILQ_FOREACH(t
, &tabs
, entry
)
2188 if (show_url
== 0) {
2189 gtk_widget_hide(t
->toolbar
);
2192 gtk_widget_show(t
->toolbar
);
2196 notebook_tab_set_visibility(void)
2198 if (show_tabs
== 0) {
2199 gtk_widget_hide(tab_bar
);
2200 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2202 if (tab_style
== XT_TABS_NORMAL
) {
2203 gtk_widget_hide(tab_bar
);
2204 gtk_notebook_set_show_tabs(notebook
, TRUE
);
2205 } else if (tab_style
== XT_TABS_COMPACT
) {
2206 gtk_widget_show(tab_bar
);
2207 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2213 statusbar_set_visibility(void)
2217 TAILQ_FOREACH(t
, &tabs
, entry
){
2218 if (show_statusbar
== 0)
2219 gtk_widget_hide(t
->statusbar_box
);
2221 gtk_widget_show(t
->statusbar_box
);
2228 url_set(struct tab
*t
, int enable_url_entry
)
2233 show_url
= enable_url_entry
;
2235 if (enable_url_entry
) {
2236 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
2237 GTK_ENTRY_ICON_PRIMARY
, NULL
);
2238 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
), 0);
2240 pixbuf
= gtk_entry_get_icon_pixbuf(GTK_ENTRY(t
->uri_entry
),
2241 GTK_ENTRY_ICON_PRIMARY
);
2243 gtk_entry_get_progress_fraction(GTK_ENTRY(t
->uri_entry
));
2244 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
2245 GTK_ENTRY_ICON_PRIMARY
, pixbuf
);
2246 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
2252 fullscreen(struct tab
*t
, struct karg
*args
)
2254 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2257 return (XT_CB_PASSTHROUGH
);
2259 if (show_url
== 0) {
2267 url_set_visibility();
2268 notebook_tab_set_visibility();
2270 return (XT_CB_HANDLED
);
2274 statustoggle(struct tab
*t
, struct karg
*args
)
2276 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2278 if (show_statusbar
== 1) {
2280 statusbar_set_visibility();
2281 } else if (show_statusbar
== 0) {
2283 statusbar_set_visibility();
2285 return (XT_CB_HANDLED
);
2289 urlaction(struct tab
*t
, struct karg
*args
)
2291 int rv
= XT_CB_HANDLED
;
2293 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2296 return (XT_CB_PASSTHROUGH
);
2300 if (show_url
== 0) {
2302 url_set_visibility();
2306 if (show_url
== 1) {
2308 url_set_visibility();
2316 tabaction(struct tab
*t
, struct karg
*args
)
2318 int rv
= XT_CB_HANDLED
;
2319 char *url
= args
->s
;
2323 DNPRINTF(XT_D_TAB
, "tabaction: %p %d\n", t
, args
->i
);
2326 return (XT_CB_PASSTHROUGH
);
2330 if (strlen(url
) > 0)
2331 create_new_tab(url
, NULL
, 1, args
->precount
);
2333 create_new_tab(NULL
, NULL
, 1, args
->precount
);
2336 if (args
->precount
< 0)
2339 TAILQ_FOREACH(tt
, &tabs
, entry
)
2340 if (tt
->tab_id
== args
->precount
- 1) {
2345 case XT_TAB_DELQUIT
:
2346 if (gtk_notebook_get_n_pages(notebook
) > 1)
2352 if (strlen(url
) > 0)
2355 rv
= XT_CB_PASSTHROUGH
;
2361 if (show_tabs
== 0) {
2363 notebook_tab_set_visibility();
2367 if (show_tabs
== 1) {
2369 notebook_tab_set_visibility();
2372 case XT_TAB_NEXTSTYLE
:
2373 if (tab_style
== XT_TABS_NORMAL
) {
2374 tab_style
= XT_TABS_COMPACT
;
2375 recolor_compact_tabs();
2378 tab_style
= XT_TABS_NORMAL
;
2379 notebook_tab_set_visibility();
2381 case XT_TAB_UNDO_CLOSE
:
2382 if (undo_count
== 0) {
2383 DNPRINTF(XT_D_TAB
, "%s: no tabs to undo close",
2388 u
= TAILQ_FIRST(&undos
);
2389 create_new_tab(u
->uri
, u
, 1, -1);
2391 TAILQ_REMOVE(&undos
, u
, entry
);
2393 /* u->history is freed in create_new_tab() */
2398 rv
= XT_CB_PASSTHROUGH
;
2412 resizetab(struct tab
*t
, struct karg
*args
)
2414 if (t
== NULL
|| args
== NULL
) {
2415 show_oops(NULL
, "resizetab invalid parameters");
2416 return (XT_CB_PASSTHROUGH
);
2419 DNPRINTF(XT_D_TAB
, "resizetab: tab %d %d\n",
2420 t
->tab_id
, args
->i
);
2422 setzoom_webkit(t
, args
->i
);
2424 return (XT_CB_HANDLED
);
2428 movetab(struct tab
*t
, struct karg
*args
)
2432 if (t
== NULL
|| args
== NULL
) {
2433 show_oops(NULL
, "movetab invalid parameters");
2434 return (XT_CB_PASSTHROUGH
);
2437 DNPRINTF(XT_D_TAB
, "movetab: tab %d opcode %d\n",
2438 t
->tab_id
, args
->i
);
2440 if (args
->i
>= XT_TAB_INVALID
)
2441 return (XT_CB_PASSTHROUGH
);
2443 if (TAILQ_EMPTY(&tabs
))
2444 return (XT_CB_PASSTHROUGH
);
2446 n
= gtk_notebook_get_n_pages(notebook
);
2447 dest
= gtk_notebook_get_current_page(notebook
);
2451 if (args
->precount
< 0)
2452 dest
= dest
== n
- 1 ? 0 : dest
+ 1;
2454 dest
= args
->precount
- 1;
2458 if (args
->precount
< 0)
2461 dest
-= args
->precount
% n
;
2474 return (XT_CB_PASSTHROUGH
);
2477 if (dest
< 0 || dest
>= n
)
2478 return (XT_CB_PASSTHROUGH
);
2479 if (t
->tab_id
== dest
) {
2480 DNPRINTF(XT_D_TAB
, "movetab: do nothing\n");
2481 return (XT_CB_HANDLED
);
2484 set_current_tab(dest
);
2486 return (XT_CB_HANDLED
);
2492 focus_input_document(struct tab
*t
, WebKitDOMDocument
*doc
)
2494 WebKitDOMNodeList
*input
= NULL
, *textarea
= NULL
;
2497 int i
, rv
= 0 /* not found */;
2499 WebKitDOMHTMLTextAreaElement
*ta
;
2500 WebKitDOMHTMLInputElement
*in
;
2502 /* we are deliberately ignoring tab index! */
2504 /* try input first */
2505 input
= webkit_dom_document_get_elements_by_tag_name(doc
, "input");
2506 for (i
= 0; i
< webkit_dom_node_list_get_length(input
); i
++) {
2507 n
= webkit_dom_node_list_item(input
, i
);
2508 in
= (WebKitDOMHTMLInputElement
*)n
;
2509 g_object_get(G_OBJECT(in
), "type", &es
, (char *)NULL
);
2510 if ((!g_str_equal("text", es
) && !g_str_equal("password",es
)) ||
2511 webkit_dom_html_input_element_get_disabled(in
)) {
2516 webkit_dom_element_focus((WebKitDOMElement
*)in
);
2522 /* now try textarea */
2523 textarea
= webkit_dom_document_get_elements_by_tag_name(doc
, "textarea");
2524 for (i
= 0; i
< webkit_dom_node_list_get_length(textarea
); i
++) {
2525 n
= webkit_dom_node_list_item(textarea
, i
);
2526 ta
= (WebKitDOMHTMLTextAreaElement
*)n
;
2527 if (webkit_dom_html_text_area_element_get_disabled(ta
)) {
2528 /* it is hidden so skip */
2531 webkit_dom_element_focus((WebKitDOMElement
*)ta
);
2537 g_object_unref(input
);
2539 g_object_unref(textarea
);
2544 focus_input(struct tab
*t
)
2546 WebKitDOMDocument
*doc
;
2548 WebKitDOMNodeList
*fl
= NULL
, *ifl
= NULL
;
2549 int i
, fl_count
, ifl_count
, rv
= 0;
2551 WebKitDOMHTMLFrameElement
*frame
;
2552 WebKitDOMHTMLIFrameElement
*iframe
;
2555 * Here is what we are doing:
2556 * See if we got frames or iframes
2558 * if we do focus on input or textarea in frame or in iframe
2560 * if we find nothing or there are no frames focus on first input or
2564 doc
= webkit_web_view_get_dom_document(t
->wv
);
2567 fl
= webkit_dom_document_get_elements_by_tag_name(doc
, "frame");
2568 fl_count
= webkit_dom_node_list_get_length(fl
);
2571 ifl
= webkit_dom_document_get_elements_by_tag_name(doc
, "iframe");
2572 ifl_count
= webkit_dom_node_list_get_length(ifl
);
2574 /* walk frames and look for a text input */
2575 for (i
= 0; i
< fl_count
; i
++) {
2576 n
= webkit_dom_node_list_item(fl
, i
);
2577 frame
= (WebKitDOMHTMLFrameElement
*)n
;
2578 doc
= webkit_dom_html_frame_element_get_content_document(frame
);
2580 if (focus_input_document(t
, doc
)) {
2586 /* walk iframes and look for a text input */
2587 for (i
= 0; i
< ifl_count
; i
++) {
2588 n
= webkit_dom_node_list_item(ifl
, i
);
2589 iframe
= (WebKitDOMHTMLIFrameElement
*)n
;
2590 doc
= webkit_dom_html_iframe_element_get_content_document(iframe
);
2592 if (focus_input_document(t
, doc
)) {
2598 /* if we made it here nothing got focuses so use normal heuristic */
2599 if (focus_input_document(t
, webkit_web_view_get_dom_document(t
->wv
))) {
2607 g_object_unref(ifl
);
2613 dom_is_input(struct tab
*t
, WebKitDOMElement
**active
)
2615 WebKitDOMDocument
*doc
;
2616 WebKitDOMElement
*a
;
2618 WebKitDOMHTMLFrameElement
*frame
;
2619 WebKitDOMHTMLIFrameElement
*iframe
;
2621 /* proof positive that OO is stupid */
2623 doc
= webkit_web_view_get_dom_document(t
->wv
);
2625 /* unwind frames and iframes until the cows come home */
2627 a
= webkit_dom_html_document_get_active_element(
2628 (WebKitDOMHTMLDocument
*)doc
);
2633 * I think this is a total hack because this property isn't
2634 * set for textareas or input however, it is set for jquery
2635 * textareas that do rich text. Since this works around issues
2636 * in RT we'll simply keep it!
2638 * This might break some other stuff but for now it helps.
2640 if (webkit_dom_html_element_get_is_content_editable(
2641 (WebKitDOMHTMLElement
*)a
)) {
2646 frame
= (WebKitDOMHTMLFrameElement
*)a
;
2647 if (WEBKIT_DOM_IS_HTML_FRAME_ELEMENT(frame
)) {
2648 doc
= webkit_dom_html_frame_element_get_content_document(
2653 iframe
= (WebKitDOMHTMLIFrameElement
*)a
;
2654 if (WEBKIT_DOM_IS_HTML_IFRAME_ELEMENT(iframe
)) {
2655 doc
= webkit_dom_html_iframe_element_get_content_document(
2666 if (WEBKIT_DOM_IS_HTML_INPUT_ELEMENT((WebKitDOMNode
*)a
) ||
2667 WEBKIT_DOM_IS_HTML_TEXT_AREA_ELEMENT((WebKitDOMNode
*)a
)) {
2676 command_mode(struct tab
*t
, struct karg
*args
)
2678 WebKitDOMElement
*active
= NULL
;
2680 if (args
->i
== XT_MODE_COMMAND
) {
2681 if (dom_is_input(t
, &active
))
2683 webkit_dom_element_blur(active
);
2684 t
->mode
= XT_MODE_COMMAND
;
2687 t
->mode
= XT_MODE_INSERT
;
2690 return (XT_CB_HANDLED
);
2694 command(struct tab
*t
, struct karg
*args
)
2696 char *s
= NULL
, *ss
= NULL
;
2701 if (t
== NULL
|| args
== NULL
) {
2702 show_oops(NULL
, "command invalid parameters");
2703 return (XT_CB_PASSTHROUGH
);
2714 if (cmd_prefix
== 0)
2717 ss
= g_strdup_printf(":%d", cmd_prefix
);
2723 t
->mode
= XT_MODE_HINT
;
2724 bzero(&a
, sizeof a
);
2730 t
->mode
= XT_MODE_HINT
;
2731 bzero(&a
, sizeof a
);
2732 a
.i
= XT_HINT_NEWTAB
;
2742 case XT_CMD_OPEN_CURRENT
:
2745 case XT_CMD_TABNEW_CURRENT
:
2746 if (!s
) /* FALL THROUGH? */
2748 if ((uri
= get_uri(t
)) != NULL
) {
2749 ss
= g_strdup_printf("%s%s", s
, uri
);
2754 show_oops(t
, "command: invalid opcode %d", args
->i
);
2755 return (XT_CB_PASSTHROUGH
);
2758 DNPRINTF(XT_D_CMD
, "%s: tab %d type %s\n", __func__
, t
->tab_id
, s
);
2760 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), s
);
2761 gdk_color_parse(XT_COLOR_WHITE
, &color
);
2762 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
2764 gtk_widget_grab_focus(GTK_WIDGET(t
->cmd
));
2765 gtk_editable_set_position(GTK_EDITABLE(t
->cmd
), -1);
2770 return (XT_CB_HANDLED
);
2774 search(struct tab
*t
, struct karg
*args
)
2778 if (t
== NULL
|| args
== NULL
) {
2779 show_oops(NULL
, "search invalid parameters");
2784 case XT_SEARCH_NEXT
:
2785 d
= t
->search_forward
;
2787 case XT_SEARCH_PREV
:
2788 d
= !t
->search_forward
;
2791 return (XT_CB_PASSTHROUGH
);
2794 if (t
->search_text
== NULL
) {
2795 if (global_search
== NULL
)
2796 return (XT_CB_PASSTHROUGH
);
2798 d
= t
->search_forward
= TRUE
;
2799 t
->search_text
= g_strdup(global_search
);
2800 webkit_web_view_mark_text_matches(t
->wv
, global_search
, FALSE
, 0);
2801 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
2805 DNPRINTF(XT_D_CMD
, "search: tab %d opc %d forw %d text %s\n",
2806 t
->tab_id
, args
->i
, t
->search_forward
, t
->search_text
);
2808 webkit_web_view_search_text(t
->wv
, t
->search_text
, FALSE
, d
, TRUE
);
2810 return (XT_CB_HANDLED
);
2814 session_save(struct tab
*t
, char *filename
)
2820 if (strlen(filename
) == 0)
2823 if (filename
[0] == '.' || filename
[0] == '/')
2827 if (save_tabs(t
, &a
))
2829 strlcpy(named_session
, filename
, sizeof named_session
);
2831 /* add the new session to the list of sessions */
2832 s
= g_malloc(sizeof(struct session
));
2833 s
->name
= g_strdup(filename
);
2834 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
2842 session_open(struct tab
*t
, char *filename
)
2847 if (strlen(filename
) == 0)
2850 if (filename
[0] == '.' || filename
[0] == '/')
2854 a
.i
= XT_SES_CLOSETABS
;
2855 if (open_tabs(t
, &a
))
2858 strlcpy(named_session
, filename
, sizeof named_session
);
2866 session_delete(struct tab
*t
, char *filename
)
2868 char file
[PATH_MAX
];
2872 if (strlen(filename
) == 0)
2875 if (filename
[0] == '.' || filename
[0] == '/')
2878 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, filename
);
2882 if (!strcmp(filename
, named_session
))
2883 strlcpy(named_session
, XT_SAVED_TABS_FILE
,
2884 sizeof named_session
);
2886 /* remove session from sessions list */
2887 TAILQ_FOREACH(s
, &sessions
, entry
) {
2888 if (!strcmp(s
->name
, filename
))
2893 TAILQ_REMOVE(&sessions
, s
, entry
);
2894 g_free((gpointer
) s
->name
);
2903 session_cmd(struct tab
*t
, struct karg
*args
)
2905 char *filename
= args
->s
;
2910 if (args
->i
& XT_SHOW
)
2911 show_oops(t
, "Current session: %s", named_session
[0] == '\0' ?
2912 XT_SAVED_TABS_FILE
: named_session
);
2913 else if (args
->i
& XT_SAVE
) {
2914 if (session_save(t
, filename
)) {
2915 show_oops(t
, "Can't save session: %s",
2916 filename
? filename
: "INVALID");
2919 } else if (args
->i
& XT_OPEN
) {
2920 if (session_open(t
, filename
)) {
2921 show_oops(t
, "Can't open session: %s",
2922 filename
? filename
: "INVALID");
2925 } else if (args
->i
& XT_DELETE
) {
2926 if (session_delete(t
, filename
)) {
2927 show_oops(t
, "Can't delete session: %s",
2928 filename
? filename
: "INVALID");
2933 return (XT_CB_PASSTHROUGH
);
2937 script_cmd(struct tab
*t
, struct karg
*args
)
2939 JSGlobalContextRef ctx
;
2940 WebKitWebFrame
*frame
;
2942 JSValueRef val
, exception
;
2951 if ((f
= fopen(args
->s
, "r")) == NULL
) {
2952 show_oops(t
, "Can't open script file: %s", args
->s
);
2956 if (fstat(fileno(f
), &sb
) == -1) {
2957 show_oops(t
, "Can't stat script file: %s", args
->s
);
2961 buf
= g_malloc0(sb
.st_size
+ 1);
2962 if (fread(buf
, 1, sb
.st_size
, f
) != sb
.st_size
) {
2963 show_oops(t
, "Can't read script file: %s", args
->s
);
2967 /* this code needs to be redone */
2968 frame
= webkit_web_view_get_main_frame(t
->wv
);
2969 ctx
= webkit_web_frame_get_global_context(frame
);
2971 str
= JSStringCreateWithUTF8CString(buf
);
2972 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
2973 NULL
, 0, &exception
);
2974 JSStringRelease(str
);
2976 DNPRINTF(XT_D_JS
, "%s: val %p\n", __func__
, val
);
2978 es
= js_ref_to_string(ctx
, exception
);
2980 show_oops(t
, "script exception: %s", es
);
2985 es
= js_ref_to_string(ctx
, val
);
2988 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
2990 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
2994 show_oops(t
, "script complete return value: '%s'", es
);
2997 show_oops(t
, "script complete: without a return value");
3006 return (XT_CB_PASSTHROUGH
);
3010 * Make a hardcopy of the page
3013 print_page(struct tab
*t
, struct karg
*args
)
3015 WebKitWebFrame
*frame
;
3017 GtkPrintOperation
*op
;
3018 GtkPrintOperationAction action
;
3019 GtkPrintOperationResult print_res
;
3020 GError
*g_err
= NULL
;
3021 int marg_l
, marg_r
, marg_t
, marg_b
;
3023 DNPRINTF(XT_D_PRINTING
, "%s:", __func__
);
3025 ps
= gtk_page_setup_new();
3026 op
= gtk_print_operation_new();
3027 action
= GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
;
3028 frame
= webkit_web_view_get_main_frame(t
->wv
);
3030 /* the default margins are too small, so we will bump them */
3031 marg_l
= gtk_page_setup_get_left_margin(ps
, GTK_UNIT_MM
) +
3032 XT_PRINT_EXTRA_MARGIN
;
3033 marg_r
= gtk_page_setup_get_right_margin(ps
, GTK_UNIT_MM
) +
3034 XT_PRINT_EXTRA_MARGIN
;
3035 marg_t
= gtk_page_setup_get_top_margin(ps
, GTK_UNIT_MM
) +
3036 XT_PRINT_EXTRA_MARGIN
;
3037 marg_b
= gtk_page_setup_get_bottom_margin(ps
, GTK_UNIT_MM
) +
3038 XT_PRINT_EXTRA_MARGIN
;
3041 gtk_page_setup_set_left_margin(ps
, marg_l
, GTK_UNIT_MM
);
3042 gtk_page_setup_set_right_margin(ps
, marg_r
, GTK_UNIT_MM
);
3043 gtk_page_setup_set_top_margin(ps
, marg_t
, GTK_UNIT_MM
);
3044 gtk_page_setup_set_bottom_margin(ps
, marg_b
, GTK_UNIT_MM
);
3046 gtk_print_operation_set_default_page_setup(op
, ps
);
3048 /* this appears to free 'op' and 'ps' */
3049 print_res
= webkit_web_frame_print_full(frame
, op
, action
, &g_err
);
3051 /* check it worked */
3052 if (print_res
== GTK_PRINT_OPERATION_RESULT_ERROR
) {
3053 show_oops(NULL
, "can't print: %s", g_err
->message
);
3054 g_error_free (g_err
);
3062 go_home(struct tab
*t
, struct karg
*args
)
3069 set_encoding(struct tab
*t
, struct karg
*args
)
3073 if (args
->s
&& strlen(g_strstrip(args
->s
)) == 0) {
3074 e
= webkit_web_view_get_custom_encoding(t
->wv
);
3076 e
= webkit_web_view_get_encoding(t
->wv
);
3077 show_oops(t
, "encoding: %s", e
? e
: "N/A");
3079 webkit_web_view_set_custom_encoding(t
->wv
, args
->s
);
3085 restart(struct tab
*t
, struct karg
*args
)
3089 a
.s
= XT_RESTART_TABS_FILE
;
3091 execvp(start_argv
[0], start_argv
);
3100 int (*func
)(struct tab
*, struct karg
*);
3104 { "command_mode", 0, command_mode
, XT_MODE_COMMAND
, 0 },
3105 { "insert_mode", 0, command_mode
, XT_MODE_INSERT
, 0 },
3106 { "command", 0, command
, ':', 0 },
3107 { "search", 0, command
, '/', 0 },
3108 { "searchb", 0, command
, '?', 0 },
3109 { "hinting", 0, command
, '.', 0 },
3110 { "hinting_newtab", 0, command
, ',', 0 },
3111 { "togglesrc", 0, toggle_src
, 0, 0 },
3113 /* yanking and pasting */
3114 { "yankuri", 0, yank_uri
, 0, 0 },
3115 /* XXX: pasteuri{cur,new} do not work from the cmd_entry? */
3116 { "pasteuricur", 0, paste_uri
, XT_PASTE_CURRENT_TAB
, 0 },
3117 { "pasteurinew", 0, paste_uri
, XT_PASTE_NEW_TAB
, 0 },
3120 { "searchnext", 0, search
, XT_SEARCH_NEXT
, 0 },
3121 { "searchprevious", 0, search
, XT_SEARCH_PREV
, 0 },
3124 { "focusaddress", 0, focus
, XT_FOCUS_URI
, 0 },
3125 { "focussearch", 0, focus
, XT_FOCUS_SEARCH
, 0 },
3128 { "hinting", 0, hint
, 0, 0 },
3129 { "hinting_newtab", 0, hint
, XT_HINT_NEWTAB
, 0 },
3131 /* custom stylesheet */
3132 { "userstyle", 0, userstyle
, 0, 0 },
3135 { "goback", 0, navaction
, XT_NAV_BACK
, 0 },
3136 { "goforward", 0, navaction
, XT_NAV_FORWARD
, 0 },
3137 { "reload", 0, navaction
, XT_NAV_RELOAD
, 0 },
3138 { "stop", 0, navaction
, XT_NAV_STOP
, 0 },
3140 /* vertical movement */
3141 { "scrolldown", 0, move
, XT_MOVE_DOWN
, 0 },
3142 { "scrollup", 0, move
, XT_MOVE_UP
, 0 },
3143 { "scrollbottom", 0, move
, XT_MOVE_BOTTOM
, 0 },
3144 { "scrolltop", 0, move
, XT_MOVE_TOP
, 0 },
3145 { "1", 0, move
, XT_MOVE_TOP
, 0 },
3146 { "scrollhalfdown", 0, move
, XT_MOVE_HALFDOWN
, 0 },
3147 { "scrollhalfup", 0, move
, XT_MOVE_HALFUP
, 0 },
3148 { "scrollpagedown", 0, move
, XT_MOVE_PAGEDOWN
, 0 },
3149 { "scrollpageup", 0, move
, XT_MOVE_PAGEUP
, 0 },
3150 /* horizontal movement */
3151 { "scrollright", 0, move
, XT_MOVE_RIGHT
, 0 },
3152 { "scrollleft", 0, move
, XT_MOVE_LEFT
, 0 },
3153 { "scrollfarright", 0, move
, XT_MOVE_FARRIGHT
, 0 },
3154 { "scrollfarleft", 0, move
, XT_MOVE_FARLEFT
, 0 },
3156 { "favorites", 0, xtp_page_fl
, 0, 0 },
3157 { "fav", 0, xtp_page_fl
, 0, 0 },
3158 { "favadd", 0, add_favorite
, 0, 0 },
3160 { "qall", 0, quit
, 0, 0 },
3161 { "quitall", 0, quit
, 0, 0 },
3162 { "w", 0, save_tabs
, 0, 0 },
3163 { "wq", 0, save_tabs_and_quit
, 0, 0 },
3164 { "help", 0, help
, 0, 0 },
3165 { "about", 0, about
, 0, 0 },
3166 { "stats", 0, stats
, 0, 0 },
3167 { "version", 0, about
, 0, 0 },
3170 { "js", 0, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3171 { "save", 1, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3172 { "domain", 2, js_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3173 { "fqdn", 2, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3174 { "show", 1, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3175 { "all", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3176 { "persistent", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3177 { "session", 2, js_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3178 { "toggle", 1, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3179 { "domain", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3180 { "fqdn", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3182 /* cookie command */
3183 { "cookie", 0, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3184 { "save", 1, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3185 { "domain", 2, cookie_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3186 { "fqdn", 2, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3187 { "show", 1, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3188 { "all", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3189 { "persistent", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3190 { "session", 2, cookie_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3191 { "toggle", 1, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3192 { "domain", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3193 { "fqdn", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3195 /* plugin command */
3196 { "plugin", 0, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3197 { "save", 1, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3198 { "domain", 2, pl_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3199 { "fqdn", 2, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3200 { "show", 1, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3201 { "all", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3202 { "persistent", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3203 { "session", 2, pl_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3204 { "toggle", 1, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3205 { "domain", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3206 { "fqdn", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3208 /* toplevel (domain) command */
3209 { "toplevel", 0, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3210 { "toggle", 1, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3213 { "cookiejar", 0, xtp_page_cl
, 0, 0 },
3216 { "cert", 0, cert_cmd
, XT_SHOW
, 0 },
3217 { "save", 1, cert_cmd
, XT_SAVE
, 0 },
3218 { "show", 1, cert_cmd
, XT_SHOW
, 0 },
3220 { "ca", 0, ca_cmd
, 0, 0 },
3221 { "downloadmgr", 0, xtp_page_dl
, 0, 0 },
3222 { "dl", 0, xtp_page_dl
, 0, 0 },
3223 { "h", 0, xtp_page_hl
, 0, 0 },
3224 { "history", 0, xtp_page_hl
, 0, 0 },
3225 { "home", 0, go_home
, 0, 0 },
3226 { "restart", 0, restart
, 0, 0 },
3227 { "urlhide", 0, urlaction
, XT_URL_HIDE
, 0 },
3228 { "urlshow", 0, urlaction
, XT_URL_SHOW
, 0 },
3229 { "statustoggle", 0, statustoggle
, 0, 0 },
3230 { "run_script", 0, run_page_script
, 0, XT_USERARG
},
3232 { "print", 0, print_page
, 0, 0 },
3235 { "focusin", 0, resizetab
, XT_ZOOM_IN
, 0 },
3236 { "focusout", 0, resizetab
, XT_ZOOM_OUT
, 0 },
3237 { "focusreset", 0, resizetab
, XT_ZOOM_NORMAL
, 0 },
3238 { "q", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3239 { "quit", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3240 { "open", 0, tabaction
, XT_TAB_OPEN
, XT_URLARG
},
3241 { "tabclose", 0, tabaction
, XT_TAB_DELETE
, XT_PREFIX
| XT_INTARG
},
3242 { "tabedit", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3243 { "tabfirst", 0, movetab
, XT_TAB_FIRST
, 0 },
3244 { "tabhide", 0, tabaction
, XT_TAB_HIDE
, 0 },
3245 { "tablast", 0, movetab
, XT_TAB_LAST
, 0 },
3246 { "tabnew", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3247 { "tabnext", 0, movetab
, XT_TAB_NEXT
, XT_PREFIX
| XT_INTARG
},
3248 { "tabnextstyle", 0, tabaction
, XT_TAB_NEXTSTYLE
, 0 },
3249 { "tabprevious", 0, movetab
, XT_TAB_PREV
, XT_PREFIX
| XT_INTARG
},
3250 { "tabrewind", 0, movetab
, XT_TAB_FIRST
, 0 },
3251 { "tabshow", 0, tabaction
, XT_TAB_SHOW
, 0 },
3252 { "tabs", 0, buffers
, 0, 0 },
3253 { "tabundoclose", 0, tabaction
, XT_TAB_UNDO_CLOSE
, 0 },
3254 { "buffers", 0, buffers
, 0, 0 },
3255 { "ls", 0, buffers
, 0, 0 },
3256 { "encoding", 0, set_encoding
, 0, XT_USERARG
},
3258 /* command aliases (handy when -S flag is used) */
3259 { "promptopen", 0, command
, XT_CMD_OPEN
, 0 },
3260 { "promptopencurrent", 0, command
, XT_CMD_OPEN_CURRENT
, 0 },
3261 { "prompttabnew", 0, command
, XT_CMD_TABNEW
, 0 },
3262 { "prompttabnewcurrent",0, command
, XT_CMD_TABNEW_CURRENT
, 0 },
3265 { "set", 0, set
, 0, XT_SETARG
},
3267 { "fullscreen", 0, fullscreen
, 0, 0 },
3268 { "f", 0, fullscreen
, 0, 0 },
3271 { "session", 0, session_cmd
, XT_SHOW
, 0 },
3272 { "delete", 1, session_cmd
, XT_DELETE
, XT_SESSARG
},
3273 { "open", 1, session_cmd
, XT_OPEN
, XT_SESSARG
},
3274 { "save", 1, session_cmd
, XT_SAVE
, XT_USERARG
},
3275 { "show", 1, session_cmd
, XT_SHOW
, 0 },
3277 /* external javascript */
3278 { "script", 0, script_cmd
, XT_EJS_SHOW
, XT_USERARG
},
3281 { "inspector", 0, inspector_cmd
, XT_INS_SHOW
, 0 },
3282 { "show", 1, inspector_cmd
, XT_INS_SHOW
, 0 },
3283 { "hide", 1, inspector_cmd
, XT_INS_HIDE
, 0 },
3290 } cmd_status
= {-1, 0};
3293 wv_release_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3296 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 1)
3303 wv_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3306 WebKitHitTestResult
*hit_test_result
;
3309 hit_test_result
= webkit_web_view_get_hit_test_result(t
->wv
, e
);
3310 g_object_get(hit_test_result
, "context", &context
, NULL
);
3315 if (context
& WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE
)
3316 t
->mode
= XT_MODE_INSERT
;
3318 t
->mode
= XT_MODE_COMMAND
;
3320 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3322 else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 8 /* btn 4 */) {
3328 } else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 9 /* btn 5 */) {
3330 a
.i
= XT_NAV_FORWARD
;
3340 tab_close_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3342 DNPRINTF(XT_D_TAB
, "tab_close_cb: tab %d\n", t
->tab_id
);
3344 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3353 activate_uri_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3355 const gchar
*uri
= gtk_entry_get_text(GTK_ENTRY(entry
));
3357 DNPRINTF(XT_D_URL
, "activate_uri_entry_cb: %s\n", uri
);
3360 show_oops(NULL
, "activate_uri_entry_cb invalid parameters");
3365 show_oops(t
, "activate_uri_entry_cb no uri");
3369 uri
+= strspn(uri
, "\t ");
3371 /* if xxxt:// treat specially */
3372 if (parse_xtp_url(t
, uri
))
3375 /* otherwise continue to load page normally */
3376 load_uri(t
, (gchar
*)uri
);
3381 activate_search_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3383 const gchar
*search
= gtk_entry_get_text(GTK_ENTRY(entry
));
3384 char *newuri
= NULL
;
3387 DNPRINTF(XT_D_URL
, "activate_search_entry_cb: %s\n", search
);
3390 show_oops(NULL
, "activate_search_entry_cb invalid parameters");
3394 if (search_string
== NULL
) {
3395 show_oops(t
, "no search_string");
3399 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
3401 enc_search
= soup_uri_encode(search
, XT_RESERVED_CHARS
);
3402 newuri
= g_strdup_printf(search_string
, enc_search
);
3406 webkit_web_view_load_uri(t
->wv
, newuri
);
3414 check_and_set_cookie(const gchar
*uri
, struct tab
*t
)
3416 struct domain
*d
= NULL
;
3419 if (uri
== NULL
|| t
== NULL
)
3422 if ((d
= wl_find_uri(uri
, &c_wl
)) == NULL
)
3427 DNPRINTF(XT_D_COOKIE
, "check_and_set_cookie: %s %s\n",
3428 es
? "enable" : "disable", uri
);
3430 g_object_set(G_OBJECT(t
->settings
),
3431 "enable-html5-local-storage", es
, (char *)NULL
);
3432 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3436 check_and_set_js(const gchar
*uri
, struct tab
*t
)
3438 struct domain
*d
= NULL
;
3441 if (uri
== NULL
|| t
== NULL
)
3444 if ((d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3449 DNPRINTF(XT_D_JS
, "check_and_set_js: %s %s\n",
3450 es
? "enable" : "disable", uri
);
3452 g_object_set(G_OBJECT(t
->settings
),
3453 "enable-scripts", es
, (char *)NULL
);
3454 g_object_set(G_OBJECT(t
->settings
),
3455 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
3456 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3458 button_set_stockid(t
->js_toggle
,
3459 es
? GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
);
3463 check_and_set_pl(const gchar
*uri
, struct tab
*t
)
3465 struct domain
*d
= NULL
;
3468 if (uri
== NULL
|| t
== NULL
)
3471 if ((d
= wl_find_uri(uri
, &pl_wl
)) == NULL
)
3476 DNPRINTF(XT_D_JS
, "check_and_set_pl: %s %s\n",
3477 es
? "enable" : "disable", uri
);
3479 g_object_set(G_OBJECT(t
->settings
),
3480 "enable-plugins", es
, (char *)NULL
);
3481 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3485 color_address_bar(gpointer p
)
3488 struct tab
*tt
, *t
= p
;
3489 gchar
*col_str
= XT_COLOR_WHITE
;
3490 const gchar
*uri
, *u
= NULL
, *error_str
= NULL
;
3493 gdk_threads_enter();
3495 DNPRINTF(XT_D_URL
, "%s:\n", __func__
);
3497 /* make sure t still exists */
3500 TAILQ_FOREACH(tt
, &tabs
, entry
)
3506 if ((uri
= get_uri(t
)) == NULL
)
3511 gdk_threads_leave();
3514 col_str
= XT_COLOR_YELLOW
;
3515 switch (load_compare_cert(u
, &error_str
)) {
3517 col_str
= XT_COLOR_BLUE
;
3520 col_str
= XT_COLOR_GREEN
;
3522 case CERT_UNTRUSTED
:
3523 col_str
= XT_COLOR_YELLOW
;
3526 col_str
= XT_COLOR_RED
;
3531 gdk_threads_enter();
3533 /* make sure t isn't deleted */
3534 TAILQ_FOREACH(tt
, &tabs
, entry
)
3541 /* test to see if the user navigated away and canceled the thread */
3542 if (t
->thread
!= g_thread_self())
3544 if ((uri
= get_uri(t
)) == NULL
) {
3548 if (strcmp(uri
, u
)) {
3549 /* make sure we are still the same url */
3555 gdk_color_parse(col_str
, &color
);
3556 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3558 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3559 statusbar_modify_attr(t
, col_str
, XT_COLOR_BLACK
);
3561 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3563 if (error_str
&& error_str
[0] != '\0')
3564 show_oops(t
, "%s", error_str
);
3569 /* t is invalid at this point */
3571 g_free((gpointer
)u
);
3573 gdk_threads_leave();
3578 show_ca_status(struct tab
*t
, const char *uri
)
3581 gchar
*col_str
= XT_COLOR_WHITE
;
3583 DNPRINTF(XT_D_URL
, "show_ca_status: %d %s %s\n",
3584 ssl_strict_certs
, ssl_ca_file
, uri
);
3591 if (ssl_ca_file
== NULL
) {
3592 if (g_str_has_prefix(uri
, "http://"))
3594 if (g_str_has_prefix(uri
, "https://")) {
3595 col_str
= XT_COLOR_RED
;
3600 if (g_str_has_prefix(uri
, "http://") ||
3601 !g_str_has_prefix(uri
, "https://"))
3605 * It is not necessary to see if the thread is already running.
3606 * If the thread is in progress setting it to something else aborts it
3610 /* thread the coloring of the address bar */
3611 t
->thread
= g_thread_create((GThreadFunc
)color_address_bar
, t
, TRUE
, NULL
);
3613 color_address_bar(t
);
3619 gdk_color_parse(col_str
, &color
);
3620 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3622 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3623 statusbar_modify_attr(t
, col_str
, XT_COLOR_BLACK
);
3625 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3630 free_favicon(struct tab
*t
)
3632 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p req %p\n",
3633 __func__
, t
->icon_download
, t
->icon_request
);
3635 if (t
->icon_request
)
3636 g_object_unref(t
->icon_request
);
3637 if (t
->icon_dest_uri
)
3638 g_free(t
->icon_dest_uri
);
3640 t
->icon_request
= NULL
;
3641 t
->icon_dest_uri
= NULL
;
3645 xt_icon_from_name(struct tab
*t
, gchar
*name
)
3647 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->uri_entry
),
3648 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3650 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3651 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3653 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3654 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3658 xt_icon_from_pixbuf(struct tab
*t
, GdkPixbuf
*pb
)
3660 GdkPixbuf
*pb_scaled
;
3662 if (gdk_pixbuf_get_width(pb
) > 16 || gdk_pixbuf_get_height(pb
) > 16)
3663 pb_scaled
= gdk_pixbuf_scale_simple(pb
, 16, 16,
3664 GDK_INTERP_BILINEAR
);
3668 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
3669 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3671 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
3672 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3674 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3675 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3677 if (pb_scaled
!= pb
)
3678 g_object_unref(pb_scaled
);
3682 xt_icon_from_file(struct tab
*t
, char *file
)
3686 if (g_str_has_prefix(file
, "file://"))
3687 file
+= strlen("file://");
3689 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
3691 xt_icon_from_pixbuf(t
, pb
);
3694 xt_icon_from_name(t
, "text-html");
3698 is_valid_icon(char *file
)
3701 const char *mime_type
;
3705 gf
= g_file_new_for_path(file
);
3706 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
3708 mime_type
= g_file_info_get_content_type(fi
);
3709 valid
= g_strcmp0(mime_type
, "image/x-ico") == 0 ||
3710 g_strcmp0(mime_type
, "image/vnd.microsoft.icon") == 0 ||
3711 g_strcmp0(mime_type
, "image/png") == 0 ||
3712 g_strcmp0(mime_type
, "image/gif") == 0 ||
3713 g_strcmp0(mime_type
, "application/octet-stream") == 0;
3721 set_favicon_from_file(struct tab
*t
, char *file
)
3725 if (t
== NULL
|| file
== NULL
)
3728 if (g_str_has_prefix(file
, "file://"))
3729 file
+= strlen("file://");
3730 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading %s\n", __func__
, file
);
3732 if (!stat(file
, &sb
)) {
3733 if (sb
.st_size
== 0 || !is_valid_icon(file
)) {
3734 /* corrupt icon so trash it */
3735 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3738 /* no need to set icon to default here */
3742 xt_icon_from_file(t
, file
);
3746 favicon_download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
3749 WebKitDownloadStatus status
= webkit_download_get_status(download
);
3750 struct tab
*tt
= NULL
, *t
= NULL
;
3753 * find the webview instead of passing in the tab as it could have been
3754 * deleted from underneath us.
3756 TAILQ_FOREACH(tt
, &tabs
, entry
) {
3765 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d status %d\n",
3766 __func__
, t
->tab_id
, status
);
3769 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
3771 t
->icon_download
= NULL
;
3774 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
3777 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
3780 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
3782 DNPRINTF(XT_D_DOWNLOAD
, "%s: freeing favicon %d\n",
3783 __func__
, t
->tab_id
);
3784 t
->icon_download
= NULL
;
3787 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
3790 DNPRINTF(XT_D_DOWNLOAD
, "%s: setting icon to %s\n",
3791 __func__
, t
->icon_dest_uri
);
3792 set_favicon_from_file(t
, t
->icon_dest_uri
);
3793 /* these will be freed post callback */
3794 t
->icon_request
= NULL
;
3795 t
->icon_download
= NULL
;
3803 abort_favicon_download(struct tab
*t
)
3805 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p\n", __func__
, t
->icon_download
);
3807 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
3808 if (t
->icon_download
) {
3809 g_signal_handlers_disconnect_by_func(G_OBJECT(t
->icon_download
),
3810 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3811 webkit_download_cancel(t
->icon_download
);
3812 t
->icon_download
= NULL
;
3817 xt_icon_from_name(t
, "text-html");
3821 notify_icon_loaded_cb(WebKitWebView
*wv
, gchar
*uri
, struct tab
*t
)
3823 DNPRINTF(XT_D_DOWNLOAD
, "%s %s\n", __func__
, uri
);
3825 if (uri
== NULL
|| t
== NULL
)
3828 #if WEBKIT_CHECK_VERSION(1, 4, 0)
3829 /* take icon from WebKitIconDatabase */
3832 pb
= webkit_web_view_get_icon_pixbuf(wv
);
3834 xt_icon_from_pixbuf(t
, pb
);
3837 xt_icon_from_name(t
, "text-html");
3838 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
3839 /* download icon to cache dir */
3840 gchar
*name_hash
, file
[PATH_MAX
];
3843 if (t
->icon_request
) {
3844 DNPRINTF(XT_D_DOWNLOAD
, "%s: download in progress\n", __func__
);
3848 /* check to see if we got the icon in cache */
3849 name_hash
= g_compute_checksum_for_string(G_CHECKSUM_SHA256
, uri
, -1);
3850 snprintf(file
, sizeof file
, "%s/%s.ico", cache_dir
, name_hash
);
3853 if (!stat(file
, &sb
)) {
3854 if (sb
.st_size
> 0) {
3855 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading from cache %s\n",
3857 set_favicon_from_file(t
, file
);
3861 /* corrupt icon so trash it */
3862 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3867 /* create download for icon */
3868 t
->icon_request
= webkit_network_request_new(uri
);
3869 if (t
->icon_request
== NULL
) {
3870 DNPRINTF(XT_D_DOWNLOAD
, "%s: invalid uri %s\n",
3875 t
->icon_download
= webkit_download_new(t
->icon_request
);
3876 if (t
->icon_download
== NULL
)
3879 /* we have to free icon_dest_uri later */
3880 t
->icon_dest_uri
= g_strdup_printf("file://%s", file
);
3881 webkit_download_set_destination_uri(t
->icon_download
,
3884 if (webkit_download_get_status(t
->icon_download
) ==
3885 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
3886 g_object_unref(t
->icon_request
);
3887 g_free(t
->icon_dest_uri
);
3888 t
->icon_request
= NULL
;
3889 t
->icon_dest_uri
= NULL
;
3893 g_signal_connect(G_OBJECT(t
->icon_download
), "notify::status",
3894 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3896 webkit_download_start(t
->icon_download
);
3901 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3903 const gchar
*uri
= NULL
, *title
= NULL
;
3904 struct history
*h
, find
;
3908 DNPRINTF(XT_D_URL
, "notify_load_status_cb: %d %s\n",
3909 webkit_web_view_get_load_status(wview
),
3910 get_uri(t
) ? get_uri(t
) : "NOTHING");
3913 show_oops(NULL
, "notify_load_status_cb invalid parameters");
3917 switch (webkit_web_view_get_load_status(wview
)) {
3918 case WEBKIT_LOAD_PROVISIONAL
:
3920 abort_favicon_download(t
);
3921 #if GTK_CHECK_VERSION(2, 20, 0)
3922 gtk_widget_show(t
->spinner
);
3923 gtk_spinner_start(GTK_SPINNER(t
->spinner
));
3925 gtk_label_set_text(GTK_LABEL(t
->label
), "Loading");
3927 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), TRUE
);
3929 /* assume we are a new address */
3930 gdk_color_parse("white", &color
);
3931 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3932 statusbar_modify_attr(t
, "white", XT_COLOR_BLACK
);
3934 /* take focus if we are visible */
3940 /* kill color thread */
3945 case WEBKIT_LOAD_COMMITTED
:
3950 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
3956 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
3958 /* check if js white listing is enabled */
3959 if (enable_plugin_whitelist
)
3960 check_and_set_pl(uri
, t
);
3961 if (enable_cookie_whitelist
)
3962 check_and_set_cookie(uri
, t
);
3963 if (enable_js_whitelist
)
3964 check_and_set_js(uri
, t
);
3970 /* we know enough to autosave the session */
3971 if (session_autosave
) {
3976 show_ca_status(t
, uri
);
3977 run_script(t
, JS_HINTING
);
3980 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
3984 case WEBKIT_LOAD_FINISHED
:
3990 if (!strncmp(uri
, "http://", strlen("http://")) ||
3991 !strncmp(uri
, "https://", strlen("https://")) ||
3992 !strncmp(uri
, "file://", strlen("file://"))) {
3994 h
= RB_FIND(history_list
, &hl
, &find
);
3996 title
= get_title(t
, FALSE
);
3997 h
= g_malloc(sizeof *h
);
3998 h
->uri
= g_strdup(uri
);
3999 h
->title
= g_strdup(title
);
4000 RB_INSERT(history_list
, &hl
, h
);
4001 completion_add_uri(h
->uri
);
4002 update_history_tabs(NULL
);
4006 set_status(t
, (char *)uri
, XT_STATUS_URI
);
4007 #if WEBKIT_CHECK_VERSION(1, 1, 18)
4008 case WEBKIT_LOAD_FAILED
:
4011 #if GTK_CHECK_VERSION(2, 20, 0)
4012 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
4013 gtk_widget_hide(t
->spinner
);
4016 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
4021 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
), TRUE
);
4023 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
),
4024 can_go_back_for_real(t
));
4026 gtk_widget_set_sensitive(GTK_WIDGET(t
->forward
),
4027 can_go_forward_for_real(t
));
4031 notify_title_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
4033 const gchar
*title
= NULL
, *win_title
= NULL
;
4035 title
= get_title(t
, FALSE
);
4036 win_title
= get_title(t
, TRUE
);
4037 gtk_label_set_text(GTK_LABEL(t
->label
), title
);
4038 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
), title
);
4039 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
4040 gtk_window_set_title(GTK_WINDOW(main_window
), win_title
);
4044 get_domain(const gchar
*host
)
4049 /* handle silly domains like .co.uk */
4051 if ((x
= strlen(host
)) <= 6)
4052 return (g_strdup(host
));
4054 if (host
[x
- 3] == '.' && host
[x
- 6] == '.') {
4060 return (g_strdup(&host
[x
+ 1]));
4064 p
= g_strrstr(host
, ".");
4066 return (g_strdup(""));
4072 return (g_strdup(p
+ 1));
4074 return (g_strdup(host
));
4078 js_autorun(struct tab
*t
)
4082 size_t got_default
= 0, got_host
= 0;
4084 char deff
[PATH_MAX
], hostf
[PATH_MAX
];
4085 char *js
= NULL
, *jsat
, *domain
= NULL
;
4086 FILE *deffile
= NULL
, *hostfile
= NULL
;
4087 JSGlobalContextRef ctx
;
4088 WebKitWebFrame
*frame
;
4090 JSValueRef val
, exception
;
4093 if (js_autorun_enabled
== 0)
4098 !(g_str_has_prefix(uri
, "http://") ||
4099 g_str_has_prefix(uri
, "https://")))
4102 su
= soup_uri_new(uri
);
4105 if (!SOUP_URI_VALID_FOR_HTTP(su
))
4108 DNPRINTF(XT_D_JS
, "%s: host: %s domain: %s\n", __func__
,
4110 domain
= get_domain(su
->host
);
4112 snprintf(deff
, sizeof deff
, "%s/default.js", js_dir
);
4113 if ((deffile
= fopen(deff
, "r")) != NULL
) {
4114 if (fstat(fileno(deffile
), &sb
) == -1) {
4115 show_oops(t
, "can't stat default JS file");
4118 got_default
= sb
.st_size
;
4121 /* try host first followed by domain */
4122 snprintf(hostf
, sizeof hostf
, "%s/%s.js", js_dir
, su
->host
);
4123 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4124 if ((hostfile
= fopen(hostf
, "r")) == NULL
) {
4125 snprintf(hostf
, sizeof hostf
, "%s/%s.js", js_dir
, domain
);
4126 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4127 if ((hostfile
= fopen(hostf
, "r")) == NULL
)
4130 DNPRINTF(XT_D_JS
, "file: %s\n", hostf
);
4131 if (fstat(fileno(hostfile
), &sb
) == -1) {
4132 show_oops(t
, "can't stat %s JS file", hostf
);
4135 got_host
= sb
.st_size
;
4138 if (got_default
+ got_host
== 0)
4141 js
= g_malloc0(got_default
+ got_host
+ 1);
4145 if (fread(js
, got_default
, 1, deffile
) != 1) {
4146 show_oops(t
, "default file read error");
4149 jsat
= js
+ got_default
;
4153 if (fread(jsat
, got_host
, 1, hostfile
) != 1) {
4154 show_oops(t
, "host file read error");
4159 /* this code needs to be redone */
4160 frame
= webkit_web_view_get_main_frame(t
->wv
);
4161 ctx
= webkit_web_frame_get_global_context(frame
);
4163 str
= JSStringCreateWithUTF8CString(js
);
4164 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
4165 NULL
, 0, &exception
);
4166 JSStringRelease(str
);
4168 DNPRINTF(XT_D_JS
, "%s: val %p\n", __func__
, val
);
4170 es
= js_ref_to_string(ctx
, exception
);
4172 show_oops(t
, "script exception: %s", es
);
4177 es
= js_ref_to_string(ctx
, val
);
4180 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
4182 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
4186 show_oops(t
, "script complete return value: '%s'", es
);
4189 show_oops(t
, "script complete: without a return value");
4205 webview_load_finished_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4207 WebKitDOMElement
*active
= NULL
;
4209 /* autorun some js if enabled */
4212 if (autofocus_onload
&&
4213 t
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
4215 t
->mode
= XT_MODE_INSERT
;
4217 if (dom_is_input(t
, &active
))
4219 webkit_dom_element_blur(active
);
4220 t
->mode
= XT_MODE_COMMAND
;
4225 webview_progress_changed_cb(WebKitWebView
*wv
, int progress
, struct tab
*t
)
4227 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->uri_entry
),
4228 progress
== 100 ? 0 : (double)progress
/ 100);
4229 if (show_url
== 0) {
4230 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
4231 progress
== 100 ? 0 : (double)progress
/ 100);
4234 update_statusbar_position(NULL
, NULL
);
4238 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
4239 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
4240 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
4243 WebKitWebNavigationReason reason
;
4244 struct domain
*d
= NULL
;
4247 show_oops(NULL
, "webview_npd_cb invalid parameters");
4251 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
4253 webkit_network_request_get_uri(request
));
4255 uri
= (char *)webkit_network_request_get_uri(request
);
4257 /* if this is an xtp url, we don't load anything else */
4258 if (parse_xtp_url(t
, uri
))
4261 if ((t
->mode
== XT_MODE_HINT
&& t
->new_tab
) || t
->ctrl_click
) {
4263 create_new_tab(uri
, NULL
, ctrl_click_focus
, -1);
4264 webkit_web_policy_decision_ignore(pd
);
4265 return (TRUE
); /* we made the decission */
4269 * This is a little hairy but it comes down to this:
4270 * when we run in whitelist mode we have to assist the browser in
4271 * opening the URL that it would have opened in a new tab.
4273 reason
= webkit_web_navigation_action_get_reason(na
);
4274 if (reason
== WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED
) {
4275 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
4276 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1)
4277 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4279 webkit_web_policy_decision_use(pd
);
4280 return (TRUE
); /* we made the decision */
4287 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4290 struct domain
*d
= NULL
;
4292 WebKitWebView
*webview
= NULL
;
4294 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
4295 webkit_web_view_get_uri(wv
));
4298 /* open in current tab */
4300 } else if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
4301 uri
= webkit_web_view_get_uri(wv
);
4302 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4305 tt
= create_new_tab(NULL
, NULL
, 1, -1);
4307 } else if (enable_scripts
== 1) {
4308 tt
= create_new_tab(NULL
, NULL
, 1, -1);
4316 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
4319 struct domain
*d
= NULL
;
4321 DNPRINTF(XT_D_NAV
, "webview_close_cb: %d\n", t
->tab_id
);
4323 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
4324 uri
= webkit_web_view_get_uri(wv
);
4325 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4329 } else if (enable_scripts
== 1)
4336 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
4338 /* we can not eat the event without throwing gtk off so defer it */
4340 /* catch middle click */
4341 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 2) {
4346 /* catch ctrl click */
4347 if (e
->type
== GDK_BUTTON_RELEASE
&&
4348 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
4353 return (XT_CB_PASSTHROUGH
);
4357 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
4359 struct mime_type
*m
;
4361 m
= find_mime_type(mime_type
);
4369 show_oops(t
, "can't fork mime handler");
4379 execlp(m
->mt_action
, m
->mt_action
,
4380 webkit_network_request_get_uri(request
), (void *)NULL
);
4389 get_mime_type(const char *file
)
4392 char *mime_type
= NULL
;
4396 if (g_str_has_prefix(file
, "file://"))
4397 file
+= strlen("file://");
4399 gf
= g_file_new_for_path(file
);
4400 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
4402 if ((m
= g_file_info_get_content_type(fi
)) != NULL
)
4403 mime_type
= g_strdup(m
);
4411 run_download_mimehandler(char *mime_type
, char *file
)
4413 struct mime_type
*m
;
4415 m
= find_mime_type(mime_type
);
4421 show_oops(NULL
, "can't fork download mime handler");
4431 if (g_str_has_prefix(file
, "file://"))
4432 file
+= strlen("file://");
4433 execlp(m
->mt_action
, m
->mt_action
, file
, (void *)NULL
);
4442 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
4445 WebKitDownloadStatus status
;
4446 const char *file
= NULL
;
4449 if (download
== NULL
)
4451 status
= webkit_download_get_status(download
);
4452 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
4455 file
= webkit_download_get_destination_uri(download
);
4458 mime
= get_mime_type(file
);
4462 run_download_mimehandler((char *)mime
, (char *)file
);
4467 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
4468 WebKitNetworkRequest
*request
, char *mime_type
,
4469 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
4472 show_oops(NULL
, "webview_mimetype_cb invalid parameters");
4476 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
4477 t
->tab_id
, mime_type
);
4479 if (run_mimehandler(t
, mime_type
, request
) == 0) {
4480 webkit_web_policy_decision_ignore(decision
);
4485 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
4486 webkit_web_policy_decision_download(decision
);
4494 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
4498 const gchar
*suggested_name
;
4499 gchar
*filename
= NULL
;
4501 struct download
*download_entry
;
4504 if (wk_download
== NULL
|| t
== NULL
) {
4505 show_oops(NULL
, "%s invalid parameters", __func__
);
4509 suggested_name
= webkit_download_get_suggested_filename(wk_download
);
4510 if (suggested_name
== NULL
)
4511 return (FALSE
); /* abort download */
4522 filename
= g_strdup_printf("%d%s", i
, suggested_name
);
4524 uri
= g_strdup_printf("file://%s/%s", download_dir
, i
?
4525 filename
: suggested_name
);
4527 } while (!stat(uri
+ strlen("file://"), &sb
));
4529 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d filename %s "
4530 "local %s\n", __func__
, t
->tab_id
, filename
, uri
);
4532 webkit_download_set_destination_uri(wk_download
, uri
);
4534 if (webkit_download_get_status(wk_download
) ==
4535 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
4536 show_oops(t
, "%s: download failed to start", __func__
);
4538 gtk_label_set_text(GTK_LABEL(t
->label
), "Download Failed");
4540 /* connect "download first" mime handler */
4541 g_signal_connect(G_OBJECT(wk_download
), "notify::status",
4542 G_CALLBACK(download_status_changed_cb
), NULL
);
4544 download_entry
= g_malloc(sizeof(struct download
));
4545 download_entry
->download
= wk_download
;
4546 download_entry
->tab
= t
;
4547 download_entry
->id
= next_download_id
++;
4548 RB_INSERT(download_list
, &downloads
, download_entry
);
4549 /* get from history */
4550 g_object_ref(wk_download
);
4551 gtk_label_set_text(GTK_LABEL(t
->label
), "Downloading");
4552 show_oops(t
, "Download of '%s' started...",
4553 basename((char *)webkit_download_get_destination_uri(wk_download
)));
4562 /* sync other download manager tabs */
4563 update_download_tabs(NULL
);
4566 * NOTE: never redirect/render the current tab before this
4567 * function returns. This will cause the download to never start.
4569 return (ret
); /* start download */
4573 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
4575 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
4578 show_oops(NULL
, "webview_hover_cb");
4583 set_status(t
, uri
, XT_STATUS_LINK
);
4586 set_status(t
, t
->status
, XT_STATUS_NOTHING
);
4591 mark(struct tab
*t
, struct karg
*arg
)
4597 if ((index
= marktoindex(mark
)) == -1)
4600 if (arg
->i
== XT_MARK_SET
)
4601 t
->mark
[index
] = gtk_adjustment_get_value(t
->adjust_v
);
4602 else if (arg
->i
== XT_MARK_GOTO
) {
4603 if (t
->mark
[index
] == XT_INVALID_MARK
) {
4604 show_oops(t
, "mark '%c' does not exist", mark
);
4607 /* XXX t->mark[index] can be bigger than the maximum if ajax or
4608 something changes the document size */
4609 gtk_adjustment_set_value(t
->adjust_v
, t
->mark
[index
]);
4616 marks_clear(struct tab
*t
)
4620 for (i
= 0; i
< LENGTH(t
->mark
); i
++)
4621 t
->mark
[i
] = XT_INVALID_MARK
;
4627 char file
[PATH_MAX
];
4628 char *line
= NULL
, *p
;
4633 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
4634 if ((f
= fopen(file
, "r+")) == NULL
) {
4635 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
4639 for (i
= 1; ; i
++) {
4640 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
4642 if (strlen(line
) == 0 || line
[0] == '#') {
4648 p
= strtok(line
, " \t");
4650 if (p
== NULL
|| strlen(p
) != 1 ||
4651 (index
= marktoindex(*p
)) == -1) {
4652 warnx("corrupt quickmarks file, line %d", i
);
4656 p
= strtok(NULL
, " \t");
4657 if (qmarks
[index
] != NULL
)
4658 g_free(qmarks
[index
]);
4659 qmarks
[index
] = g_strdup(p
);
4670 char file
[PATH_MAX
];
4674 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
4675 if ((f
= fopen(file
, "r+")) == NULL
) {
4676 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
4680 for (i
= 0; i
< XT_NOMARKS
; i
++)
4681 if (qmarks
[i
] != NULL
)
4682 fprintf(f
, "%c %s\n", indextomark(i
), qmarks
[i
]);
4690 qmark(struct tab
*t
, struct karg
*arg
)
4695 mark
= arg
->s
[strlen(arg
->s
)-1];
4696 index
= marktoindex(mark
);
4702 if (qmarks
[index
] != NULL
)
4703 g_free(qmarks
[index
]);
4705 qmarks_load(); /* sync if multiple instances */
4706 qmarks
[index
] = g_strdup(get_uri(t
));
4710 if (qmarks
[index
] != NULL
)
4711 load_uri(t
, qmarks
[index
]);
4713 show_oops(t
, "quickmark \"%c\" does not exist",
4719 if (qmarks
[index
] != NULL
)
4720 create_new_tab(qmarks
[index
], NULL
, 1, -1);
4722 show_oops(t
, "quickmark \"%c\" does not exist",
4733 go_up(struct tab
*t
, struct karg
*args
)
4739 levels
= atoi(args
->s
);
4743 uri
= g_strdup(webkit_web_view_get_uri(t
->wv
));
4744 if ((tmp
= strstr(uri
, XT_PROTO_DELIM
)) == NULL
)
4746 tmp
+= strlen(XT_PROTO_DELIM
);
4748 /* if an uri starts with a slash, leave it alone (for file:///) */
4755 p
= strrchr(tmp
, '/');
4769 gototab(struct tab
*t
, struct karg
*args
)
4772 struct karg arg
= {0, NULL
, -1};
4774 tab
= atoi(args
->s
);
4776 arg
.i
= XT_TAB_NEXT
;
4785 zoom_amount(struct tab
*t
, struct karg
*arg
)
4787 struct karg narg
= {0, NULL
, -1};
4789 narg
.i
= atoi(arg
->s
);
4790 resizetab(t
, &narg
);
4796 flip_colon(struct tab
*t
, struct karg
*arg
)
4798 struct karg narg
= {0, NULL
, -1};
4801 if (t
== NULL
|| arg
== NULL
)
4804 p
= strstr(arg
->s
, ":");
4816 /* buffer commands receive the regex that triggered them in arg.s */
4817 char bcmd
[XT_BUFCMD_SZ
];
4821 #define XT_PRE_NO (0)
4822 #define XT_PRE_YES (1)
4823 #define XT_PRE_MAYBE (2)
4825 int (*func
)(struct tab
*, struct karg
*);
4829 { "^[0-9]*gu$", XT_PRE_MAYBE
, "gu", go_up
, 0 },
4830 { "^gg$", XT_PRE_NO
, "gg", move
, XT_MOVE_TOP
},
4831 { "^gG$", XT_PRE_NO
, "gG", move
, XT_MOVE_BOTTOM
},
4832 { "^[0-9]+%$", XT_PRE_YES
, "%", move
, XT_MOVE_PERCENT
},
4833 { "^zz$", XT_PRE_NO
, "zz", move
, XT_MOVE_CENTER
},
4834 { "^gh$", XT_PRE_NO
, "gh", go_home
, 0 },
4835 { "^m[a-zA-Z0-9]$", XT_PRE_NO
, "m", mark
, XT_MARK_SET
},
4836 { "^['][a-zA-Z0-9]$", XT_PRE_NO
, "'", mark
, XT_MARK_GOTO
},
4837 { "^[0-9]+t$", XT_PRE_YES
, "t", gototab
, 0 },
4838 { "^M[a-zA-Z0-9]$", XT_PRE_NO
, "M", qmark
, XT_QMARK_SET
},
4839 { "^go[a-zA-Z0-9]$", XT_PRE_NO
, "go", qmark
, XT_QMARK_OPEN
},
4840 { "^gn[a-zA-Z0-9]$", XT_PRE_NO
, "gn", qmark
, XT_QMARK_TAB
},
4841 { "^ZR$", XT_PRE_NO
, "ZR", restart
, 0 },
4842 { "^ZZ$", XT_PRE_NO
, "ZZ", quit
, 0 },
4843 { "^zi$", XT_PRE_NO
, "zi", resizetab
, XT_ZOOM_IN
},
4844 { "^zo$", XT_PRE_NO
, "zo", resizetab
, XT_ZOOM_OUT
},
4845 { "^z0$", XT_PRE_NO
, "z0", resizetab
, XT_ZOOM_NORMAL
},
4846 { "^[0-9]+Z$", XT_PRE_YES
, "Z", zoom_amount
, 0 },
4847 { "^[0-9]+:$", XT_PRE_YES
, ":", flip_colon
, 0 },
4851 buffercmd_init(void)
4855 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
4856 if (regcomp(&buffercmds
[i
].cregex
, buffercmds
[i
].regex
,
4857 REG_EXTENDED
| REG_NOSUB
))
4858 startpage_add("invalid buffercmd regex %s",
4859 buffercmds
[i
].regex
);
4863 buffercmd_abort(struct tab
*t
)
4867 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_abort: clearing buffer\n");
4868 for (i
= 0; i
< LENGTH(bcmd
); i
++)
4871 cmd_prefix
= 0; /* clear prefix for non-buffer commands */
4872 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
4876 buffercmd_execute(struct tab
*t
, struct buffercmd
*cmd
)
4878 struct karg arg
= {0, NULL
, -1};
4881 arg
.s
= g_strdup(bcmd
);
4883 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_execute: buffer \"%s\" "
4884 "matches regex \"%s\", executing\n", bcmd
, cmd
->regex
);
4894 buffercmd_addkey(struct tab
*t
, guint keyval
)
4897 char s
[XT_BUFCMD_SZ
];
4899 if (keyval
== GDK_Escape
) {
4901 return (XT_CB_HANDLED
);
4904 /* key with modifier or non-ascii character */
4905 if (!isascii(keyval
)) {
4907 * XXX this looks wrong but fixes some sites like
4908 * http://www.seslisozluk.com/
4909 * that eat a shift or ctrl and end putting default focus in js
4910 * instead of ignoring the keystroke
4911 * so instead of return (XT_CB_PASSTHROUGH); eat the key
4913 return (XT_CB_HANDLED
);
4916 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: adding key \"%c\" "
4917 "to buffer \"%s\"\n", keyval
, bcmd
);
4919 for (i
= 0; i
< LENGTH(bcmd
); i
++)
4920 if (bcmd
[i
] == '\0') {
4925 /* buffer full, ignore input */
4926 if (i
>= LENGTH(bcmd
) -1) {
4927 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: buffer full\n");
4929 return (XT_CB_HANDLED
);
4932 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
4934 /* find exact match */
4935 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
4936 if (regexec(&buffercmds
[i
].cregex
, bcmd
,
4937 (size_t) 0, NULL
, 0) == 0) {
4938 buffercmd_execute(t
, &buffercmds
[i
]);
4942 /* find non exact matches to see if we need to abort ot not */
4943 for (i
= 0, match
= 0; i
< LENGTH(buffercmds
); i
++) {
4944 DNPRINTF(XT_D_BUFFERCMD
, "trying: %s\n", bcmd
);
4947 if (buffercmds
[i
].precount
== XT_PRE_MAYBE
) {
4948 if (isdigit(bcmd
[0])) {
4949 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
4953 if (sscanf(bcmd
, "%s", s
) == 0)
4956 } else if (buffercmds
[i
].precount
== XT_PRE_YES
) {
4957 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
4960 if (sscanf(bcmd
, "%s", s
) == 0)
4963 if (c
== -1 && buffercmds
[i
].precount
)
4965 if (!strncmp(s
, buffercmds
[i
].cmd
, strlen(s
)))
4968 DNPRINTF(XT_D_BUFFERCMD
, "got[%d] %d <%s>: %d %s\n",
4969 i
, match
, buffercmds
[i
].cmd
, c
, s
);
4972 DNPRINTF(XT_D_BUFFERCMD
, "aborting: %s\n", bcmd
);
4977 return (XT_CB_HANDLED
);
4981 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
4983 struct key_binding
*k
;
4985 /* handle keybindings if buffercmd is empty.
4986 if not empty, allow commands like C-n */
4987 if (bcmd
[0] == '\0' || ((e
->state
& (CTRL
| MOD1
)) != 0))
4988 TAILQ_FOREACH(k
, &kbl
, entry
)
4989 if (e
->keyval
== k
->key
4990 && (entry
? k
->use_in_entry
: 1)) {
4992 if ((e
->state
& (CTRL
| MOD1
)) == 0)
4993 return (cmd_execute(t
, k
->cmd
));
4994 } else if ((e
->state
& k
->mask
) == k
->mask
) {
4995 return (cmd_execute(t
, k
->cmd
));
4999 if (!entry
&& ((e
->state
& (CTRL
| MOD1
)) == 0))
5000 return buffercmd_addkey(t
, e
->keyval
);
5002 return (XT_CB_PASSTHROUGH
);
5006 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5009 WebKitDOMElement
*active
= NULL
;
5011 /* don't use w directly; use t->whatever instead */
5014 show_oops(NULL
, "wv_keypress_after_cb");
5015 return (XT_CB_PASSTHROUGH
);
5020 DNPRINTF(XT_D_KEY
, "wv_keypress_after_cb: mode %d keyval 0x%x mask "
5021 "0x%x tab %d\n", t
->mode
, e
->keyval
, e
->state
, t
->tab_id
);
5023 /* Hide buffers, if they are visible, with escape. */
5024 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)) &&
5025 CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
5026 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5028 return (XT_CB_HANDLED
);
5031 if (t
->mode
== XT_MODE_HINT
)
5032 return (XT_CB_HANDLED
);
5034 if ((CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Tab
) ||
5035 (CLEAN(e
->state
) == SHFT
&& e
->keyval
== GDK_Tab
))
5036 /* something focussy is about to happen */
5037 return (XT_CB_PASSTHROUGH
);
5039 /* check if we are some sort of text input thing in the dom */
5040 if (dom_is_input(t
, &active
))
5041 t
->mode
= XT_MODE_INSERT
;
5043 t
->mode
= XT_MODE_COMMAND
;
5045 if (t
->mode
== XT_MODE_HINT
) {
5046 /* XXX make sure cmd entry is enabled */
5047 return (XT_CB_HANDLED
);
5048 } else if (t
->mode
== XT_MODE_COMMAND
) {
5050 snprintf(s
, sizeof s
, "%c", e
->keyval
);
5051 if (CLEAN(e
->state
) == 0 && isdigit(s
[0]))
5052 cmd_prefix
= 10 * cmd_prefix
+ atoi(s
);
5053 return (handle_keypress(t
, e
, 0));
5056 if (CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
5057 t
->mode
= XT_MODE_COMMAND
;
5059 webkit_dom_element_blur(active
);
5060 return (XT_CB_HANDLED
);
5063 return (XT_CB_PASSTHROUGH
);
5067 hint_continue(struct tab
*t
)
5069 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5071 const gchar
*errstr
= NULL
;
5075 if (!(c
[0] == '.' || c
[0] == ','))
5077 if (strlen(c
) == 1) {
5078 /* XXX should not happen */
5083 if (isdigit(c
[1])) {
5085 i
= strtonum(&c
[1], 1, 4096, &errstr
);
5087 show_oops(t
, "invalid numerical hint %s", &c
[1]);
5090 s
= g_strdup_printf("hints.updateHints(%lld);", i
);
5094 /* alphanumeric input */
5095 s
= g_strdup_printf("hints.createHints('%s', '%c');",
5096 &c
[1], c
[0] == '.' ? 'f' : 'F');
5107 search_continue(struct tab
*t
)
5109 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5110 gboolean rv
= FALSE
;
5112 if (c
[0] == ':' || c
[0] == '.' || c
[0] == ',')
5114 if (strlen(c
) == 1) {
5115 webkit_web_view_unmark_text_matches(t
->wv
);
5120 t
->search_forward
= TRUE
;
5121 else if (c
[0] == '?')
5122 t
->search_forward
= FALSE
;
5132 search_cb(struct tab
*t
)
5134 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5137 if (search_continue(t
) == FALSE
)
5141 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, t
->search_forward
,
5143 /* not found, mark red */
5144 gdk_color_parse(XT_COLOR_RED
, &color
);
5145 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
5146 /* unmark and remove selection */
5147 webkit_web_view_unmark_text_matches(t
->wv
);
5148 /* my kingdom for a way to unselect text in webview */
5150 /* found, highlight all */
5151 webkit_web_view_unmark_text_matches(t
->wv
);
5152 webkit_web_view_mark_text_matches(t
->wv
, &c
[1], FALSE
, 0);
5153 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
5154 gdk_color_parse(XT_COLOR_WHITE
, &color
);
5155 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
5163 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5165 const gchar
*c
= gtk_entry_get_text(w
);
5168 show_oops(NULL
, "cmd_keyrelease_cb invalid parameters");
5169 return (XT_CB_PASSTHROUGH
);
5172 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
5173 e
->keyval
, e
->state
, t
->tab_id
);
5176 if (!(e
->keyval
== GDK_Tab
|| e
->keyval
== GDK_ISO_Left_Tab
)) {
5177 if (hint_continue(t
) == FALSE
)
5182 if (search_continue(t
) == FALSE
)
5185 /* if search length is > 4 then no longer play timeout games */
5186 if (strlen(c
) > 4) {
5188 g_source_remove(t
->search_id
);
5195 /* reestablish a new timer if the user types fast */
5197 g_source_remove(t
->search_id
);
5198 t
->search_id
= g_timeout_add(250, (GSourceFunc
)search_cb
, (gpointer
)t
);
5201 return (XT_CB_PASSTHROUGH
);
5205 match_uri(const gchar
*uri
, const gchar
*key
) {
5208 gboolean match
= FALSE
;
5212 if (!strncmp(key
, uri
, len
))
5215 voffset
= strstr(uri
, "/") + 2;
5216 if (!strncmp(key
, voffset
, len
))
5218 else if (g_str_has_prefix(voffset
, "www.")) {
5219 voffset
= voffset
+ strlen("www.");
5220 if (!strncmp(key
, voffset
, len
))
5229 match_session(const gchar
*name
, const gchar
*key
) {
5232 sub
= strcasestr(name
, key
);
5238 cmd_getlist(int id
, char *key
)
5245 if (cmds
[id
].type
& XT_URLARG
) {
5246 RB_FOREACH_REVERSE(h
, history_list
, &hl
)
5247 if (match_uri(h
->uri
, key
)) {
5248 cmd_status
.list
[c
] = (char *)h
->uri
;
5254 } else if (cmds
[id
].type
& XT_SESSARG
) {
5255 TAILQ_FOREACH(s
, &sessions
, entry
)
5256 if (match_session(s
->name
, key
)) {
5257 cmd_status
.list
[c
] = (char *)s
->name
;
5263 } else if (cmds
[id
].type
& XT_SETARG
) {
5264 for (i
= 0; i
< get_settings_size(); i
++)
5265 if(!strncmp(key
, get_setting_name(i
),
5267 cmd_status
.list
[c
++] = get_setting_name(i
);
5273 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
5275 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
5276 if (cmds
[i
].level
< dep
)
5278 if (cmds
[i
].level
== dep
&& !strncmp(key
, cmds
[i
].cmd
,
5279 strlen(key
)) && !isdigit(cmds
[i
].cmd
[0]))
5280 cmd_status
.list
[c
++] = cmds
[i
].cmd
;
5288 cmd_getnext(int dir
)
5290 cmd_status
.index
+= dir
;
5292 if (cmd_status
.index
< 0)
5293 cmd_status
.index
= cmd_status
.len
- 1;
5294 else if (cmd_status
.index
>= cmd_status
.len
)
5295 cmd_status
.index
= 0;
5297 return cmd_status
.list
[cmd_status
.index
];
5301 cmd_tokenize(char *s
, char *tokens
[])
5304 char *tok
, *last
= NULL
;
5305 size_t len
= strlen(s
);
5308 blank
= len
== 0 || (len
> 0 && s
[len
- 1] == ' ');
5309 for (tok
= strtok_r(s
, " ", &last
); tok
&& i
< 3;
5310 tok
= strtok_r(NULL
, " ", &last
), i
++)
5320 cmd_complete(struct tab
*t
, char *str
, int dir
)
5322 GtkEntry
*w
= GTK_ENTRY(t
->cmd
);
5323 int i
, j
, levels
, c
= 0, dep
= 0, parent
= -1;
5325 char *tok
, *match
, *s
= g_strdup(str
);
5327 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
5330 DNPRINTF(XT_D_CMD
, "%s: complete %s\n", __func__
, str
);
5333 for (i
= 0; isdigit(s
[i
]); i
++)
5336 for (; isspace(s
[i
]); i
++)
5341 levels
= cmd_tokenize(s
, tokens
);
5343 for (i
= 0; i
< levels
- 1; i
++) {
5346 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5347 if (cmds
[j
].level
< dep
)
5349 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
,
5353 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
5360 if (matchcount
== 1) {
5361 strlcat(res
, tok
, sizeof res
);
5362 strlcat(res
, " ", sizeof res
);
5372 if (cmd_status
.index
== -1)
5373 cmd_getlist(parent
, tokens
[i
]);
5375 if (cmd_status
.len
> 0) {
5376 match
= cmd_getnext(dir
);
5377 strlcat(res
, match
, sizeof res
);
5378 gtk_entry_set_text(w
, res
);
5379 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5386 cmd_execute(struct tab
*t
, char *str
)
5388 struct cmd
*cmd
= NULL
;
5389 char *tok
, *last
= NULL
, *s
= g_strdup(str
), *sc
;
5391 int j
, len
, c
= 0, dep
= 0, matchcount
= 0;
5392 int prefix
= -1, rv
= XT_CB_PASSTHROUGH
;
5393 struct karg arg
= {0, NULL
, -1};
5398 for (j
= 0; j
<3 && isdigit(s
[j
]); j
++)
5404 while (isspace(s
[0]))
5407 if (strlen(s
) > 0 && strlen(prefixstr
) > 0)
5408 prefix
= atoi(prefixstr
);
5412 for (tok
= strtok_r(s
, " ", &last
); tok
;
5413 tok
= strtok_r(NULL
, " ", &last
)) {
5415 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5416 if (cmds
[j
].level
< dep
)
5418 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1 :
5420 if (cmds
[j
].level
== dep
&&
5421 !strncmp(tok
, cmds
[j
].cmd
, len
)) {
5425 if (len
== strlen(cmds
[j
].cmd
)) {
5431 if (matchcount
== 1) {
5436 show_oops(t
, "Invalid command: %s", str
);
5442 show_oops(t
, "Empty command");
5448 arg
.precount
= prefix
;
5449 else if (cmd_prefix
> 0)
5450 arg
.precount
= cmd_prefix
;
5452 if (j
> 0 && !(cmd
->type
& XT_PREFIX
) && arg
.precount
> -1) {
5453 show_oops(t
, "No prefix allowed: %s", str
);
5457 arg
.s
= last
? g_strdup(last
) : g_strdup("");
5458 if (cmd
->type
& XT_INTARG
&& last
&& strlen(last
) > 0) {
5459 if (arg
.s
== NULL
) {
5460 show_oops(t
, "Invalid command");
5463 arg
.precount
= atoi(arg
.s
);
5464 if (arg
.precount
<= 0) {
5465 if (arg
.s
[0] == '0')
5466 show_oops(t
, "Zero count");
5468 show_oops(t
, "Trailing characters");
5473 DNPRINTF(XT_D_CMD
, "%s: prefix %d arg %s\n",
5474 __func__
, arg
.precount
, arg
.s
);
5490 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5493 show_oops(NULL
, "entry_key_cb invalid parameters");
5494 return (XT_CB_PASSTHROUGH
);
5497 DNPRINTF(XT_D_CMD
, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
5498 e
->keyval
, e
->state
, t
->tab_id
);
5502 if (e
->keyval
== GDK_Escape
) {
5503 /* don't use focus_webview(t) because we want to type :cmds */
5504 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5507 return (handle_keypress(t
, e
, 1));
5510 struct command_entry
*
5511 history_prev(struct command_list
*l
, struct command_entry
*at
)
5514 at
= TAILQ_LAST(l
, command_list
);
5516 at
= TAILQ_PREV(at
, command_list
, entry
);
5518 at
= TAILQ_LAST(l
, command_list
);
5524 struct command_entry
*
5525 history_next(struct command_list
*l
, struct command_entry
*at
)
5528 at
= TAILQ_FIRST(l
);
5530 at
= TAILQ_NEXT(at
, entry
);
5532 at
= TAILQ_FIRST(l
);
5539 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5541 int rv
= XT_CB_HANDLED
;
5542 const gchar
*c
= gtk_entry_get_text(w
);
5546 show_oops(NULL
, "cmd_keypress_cb parameters");
5547 return (XT_CB_PASSTHROUGH
);
5550 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
5551 e
->keyval
, e
->state
, t
->tab_id
);
5555 e
->keyval
= GDK_Escape
;
5556 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5557 c
[0] == '.' || c
[0] == ','))
5558 e
->keyval
= GDK_Escape
;
5560 if (e
->keyval
!= GDK_Tab
&& e
->keyval
!= GDK_Shift_L
&&
5561 e
->keyval
!= GDK_ISO_Left_Tab
)
5562 cmd_status
.index
= -1;
5564 switch (e
->keyval
) {
5567 cmd_complete(t
, (char *)&c
[1], 1);
5568 else if (c
[0] == '.' || c
[0] == ',')
5569 run_script(t
, "hints.focusNextHint();");
5571 case GDK_ISO_Left_Tab
:
5573 cmd_complete(t
, (char *)&c
[1], -1);
5574 else if (c
[0] == '.' || c
[0] == ',')
5575 run_script(t
, "hints.focusPreviousHint();");
5579 if ((search_at
= history_next(&shl
, search_at
))) {
5580 search_at
->line
[0] = c
[0];
5581 gtk_entry_set_text(w
, search_at
->line
);
5582 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5584 } else if (c
[0] == '/') {
5585 if ((search_at
= history_prev(&chl
, search_at
))) {
5586 search_at
->line
[0] = c
[0];
5587 gtk_entry_set_text(w
, search_at
->line
);
5588 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5590 } if (c
[0] == ':') {
5591 if ((history_at
= history_prev(&chl
, history_at
))) {
5592 history_at
->line
[0] = c
[0];
5593 gtk_entry_set_text(w
, history_at
->line
);
5594 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5600 if ((search_at
= history_next(&shl
, search_at
))) {
5601 search_at
->line
[0] = c
[0];
5602 gtk_entry_set_text(w
, search_at
->line
);
5603 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5605 } else if (c
[0] == '?') {
5606 if ((search_at
= history_prev(&chl
, search_at
))) {
5607 search_at
->line
[0] = c
[0];
5608 gtk_entry_set_text(w
, search_at
->line
);
5609 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5611 } if (c
[0] == ':') {
5612 if ((history_at
= history_next(&chl
, history_at
))) {
5613 history_at
->line
[0] = c
[0];
5614 gtk_entry_set_text(w
, history_at
->line
);
5615 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5620 if (!(!strcmp(c
, ":") || !strcmp(c
, "/") || !strcmp(c
, "?") ||
5621 !strcmp(c
, ".") || !strcmp(c
, ","))) {
5622 /* see if we are doing hinting and reset it */
5623 if (c
[0] == '.' || c
[0] == ',') {
5624 /* recreate hints */
5625 s
= g_strdup_printf("hints.createHints('', "
5626 "'%c');", c
[0] == '.' ? 'f' : 'F');
5639 if (c
!= NULL
&& (c
[0] == '/' || c
[0] == '?'))
5640 webkit_web_view_unmark_text_matches(t
->wv
);
5642 /* no need to cancel hints */
5646 rv
= XT_CB_PASSTHROUGH
;
5652 wv_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
5654 DNPRINTF(XT_D_CMD
, "wv_popup_cb: tab %d\n", t
->tab_id
);
5658 cmd_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
5660 /* popup menu enabled */
5665 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
5668 show_oops(NULL
, "cmd_focusout_cb invalid parameters");
5669 return (XT_CB_PASSTHROUGH
);
5672 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d popup %d\n",
5673 t
->tab_id
, t
->popup
);
5675 /* if popup is enabled don't lose focus */
5678 return (XT_CB_PASSTHROUGH
);
5685 if (show_url
== 0 || t
->focus_wv
)
5688 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
5690 return (XT_CB_PASSTHROUGH
);
5694 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
5697 const gchar
*c
= gtk_entry_get_text(entry
);
5700 show_oops(NULL
, "cmd_activate_cb invalid parameters");
5704 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
5709 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5710 c
[0] == '.' || c
[0] == ','))
5716 if (c
[0] == '/' || c
[0] == '?') {
5717 /* see if there is a timer pending */
5719 g_source_remove(t
->search_id
);
5724 if (t
->search_text
) {
5725 g_free(t
->search_text
);
5726 t
->search_text
= NULL
;
5729 t
->search_text
= g_strdup(s
);
5731 g_free(global_search
);
5732 global_search
= g_strdup(s
);
5733 t
->search_forward
= c
[0] == '/';
5735 history_add(&shl
, search_file
, s
, &search_history_count
);
5736 } else if (c
[0] == '.' || c
[0] == ',') {
5737 run_script(t
, "hints.fire();");
5738 /* XXX history for link following? */
5739 } else if (c
[0] == ':') {
5740 history_add(&chl
, command_file
, s
, &cmd_history_count
);
5741 /* can't call hide_cmd after cmd_execute */
5752 backward_cb(GtkWidget
*w
, struct tab
*t
)
5757 show_oops(NULL
, "backward_cb invalid parameters");
5761 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
5768 forward_cb(GtkWidget
*w
, struct tab
*t
)
5773 show_oops(NULL
, "forward_cb invalid parameters");
5777 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
5779 a
.i
= XT_NAV_FORWARD
;
5784 home_cb(GtkWidget
*w
, struct tab
*t
)
5787 show_oops(NULL
, "home_cb invalid parameters");
5791 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
5797 stop_cb(GtkWidget
*w
, struct tab
*t
)
5799 WebKitWebFrame
*frame
;
5802 show_oops(NULL
, "stop_cb invalid parameters");
5806 DNPRINTF(XT_D_NAV
, "stop_cb: tab %d\n", t
->tab_id
);
5808 frame
= webkit_web_view_get_main_frame(t
->wv
);
5809 if (frame
== NULL
) {
5810 show_oops(t
, "stop_cb: no frame");
5814 webkit_web_frame_stop_loading(frame
);
5815 abort_favicon_download(t
);
5819 setup_webkit(struct tab
*t
)
5821 if (is_g_object_setting(G_OBJECT(t
->settings
), "enable-dns-prefetching"))
5822 g_object_set(G_OBJECT(t
->settings
), "enable-dns-prefetching",
5823 FALSE
, (char *)NULL
);
5825 warnx("webkit does not have \"enable-dns-prefetching\" property");
5826 g_object_set(G_OBJECT(t
->settings
),
5827 "user-agent", t
->user_agent
, (char *)NULL
);
5828 g_object_set(G_OBJECT(t
->settings
),
5829 "enable-scripts", enable_scripts
, (char *)NULL
);
5830 g_object_set(G_OBJECT(t
->settings
),
5831 "enable-plugins", enable_plugins
, (char *)NULL
);
5832 g_object_set(G_OBJECT(t
->settings
),
5833 "javascript-can-open-windows-automatically", enable_scripts
,
5835 g_object_set(G_OBJECT(t
->settings
),
5836 "enable-html5-database", FALSE
, (char *)NULL
);
5837 g_object_set(G_OBJECT(t
->settings
),
5838 "enable-html5-local-storage", enable_localstorage
, (char *)NULL
);
5839 g_object_set(G_OBJECT(t
->settings
),
5840 "enable_spell_checking", enable_spell_checking
, (char *)NULL
);
5841 g_object_set(G_OBJECT(t
->settings
),
5842 "spell_checking_languages", spell_check_languages
, (char *)NULL
);
5843 g_object_set(G_OBJECT(t
->settings
),
5844 "enable-developer-extras", TRUE
, (char *)NULL
);
5845 g_object_set(G_OBJECT(t
->wv
),
5846 "full-content-zoom", TRUE
, (char *)NULL
);
5848 webkit_web_view_set_settings(t
->wv
, t
->settings
);
5852 update_statusbar_position(GtkAdjustment
* adjustment
, gpointer data
)
5854 struct tab
*ti
, *t
= NULL
;
5855 gdouble view_size
, value
, max
;
5858 TAILQ_FOREACH(ti
, &tabs
, entry
)
5859 if (ti
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
5867 if (adjustment
== NULL
)
5868 adjustment
= gtk_scrolled_window_get_vadjustment(
5869 GTK_SCROLLED_WINDOW(t
->browser_win
));
5871 view_size
= gtk_adjustment_get_page_size(adjustment
);
5872 value
= gtk_adjustment_get_value(adjustment
);
5873 max
= gtk_adjustment_get_upper(adjustment
) - view_size
;
5876 position
= g_strdup("All");
5877 else if (value
== max
)
5878 position
= g_strdup("Bot");
5879 else if (value
== 0)
5880 position
= g_strdup("Top");
5882 position
= g_strdup_printf("%d%%", (int) ((value
/ max
) * 100));
5884 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.position
), position
);
5891 create_window(const gchar
*name
)
5895 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
5896 gtk_window_set_default_size(GTK_WINDOW(w
), window_width
, window_height
);
5897 gtk_widget_set_name(w
, name
);
5898 gtk_window_set_wmclass(GTK_WINDOW(w
), name
, "XXXTerm");
5904 create_browser(struct tab
*t
)
5908 GtkAdjustment
*adjustment
;
5911 show_oops(NULL
, "create_browser invalid parameters");
5915 t
->sb_h
= GTK_SCROLLBAR(gtk_hscrollbar_new(NULL
));
5916 t
->sb_v
= GTK_SCROLLBAR(gtk_vscrollbar_new(NULL
));
5917 t
->adjust_h
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_h
));
5918 t
->adjust_v
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_v
));
5920 w
= gtk_scrolled_window_new(t
->adjust_h
, t
->adjust_v
);
5921 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
5922 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
5924 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
5925 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
5928 t
->settings
= webkit_web_settings_new();
5930 g_object_set(t
->settings
, "default-encoding", encoding
, (char *)NULL
);
5932 if (user_agent
== NULL
) {
5933 g_object_get(G_OBJECT(t
->settings
), "user-agent", &strval
,
5935 t
->user_agent
= g_strdup_printf("%s %s+", strval
, version
);
5938 t
->user_agent
= g_strdup(user_agent
);
5940 t
->stylesheet
= g_strdup_printf("file://%s/style.css", resource_dir
);
5943 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w
));
5944 g_signal_connect(G_OBJECT(adjustment
), "value-changed",
5945 G_CALLBACK(update_statusbar_position
), NULL
);
5954 create_kiosk_toolbar(struct tab
*t
)
5956 GtkWidget
*toolbar
= NULL
, *b
;
5958 b
= gtk_hbox_new(FALSE
, 0);
5960 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
5962 /* backward button */
5963 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
5964 gtk_widget_set_sensitive(t
->backward
, FALSE
);
5965 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
5966 G_CALLBACK(backward_cb
), t
);
5967 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, TRUE
, TRUE
, 0);
5969 /* forward button */
5970 t
->forward
= create_button("Forward", GTK_STOCK_GO_FORWARD
, 0);
5971 gtk_widget_set_sensitive(t
->forward
, FALSE
);
5972 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
5973 G_CALLBACK(forward_cb
), t
);
5974 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, TRUE
, TRUE
, 0);
5977 t
->gohome
= create_button("Home", GTK_STOCK_HOME
, 0);
5978 gtk_widget_set_sensitive(t
->gohome
, true);
5979 g_signal_connect(G_OBJECT(t
->gohome
), "clicked",
5980 G_CALLBACK(home_cb
), t
);
5981 gtk_box_pack_start(GTK_BOX(b
), t
->gohome
, TRUE
, TRUE
, 0);
5983 /* create widgets but don't use them */
5984 t
->uri_entry
= gtk_entry_new();
5985 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
5986 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
5987 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
5993 create_toolbar(struct tab
*t
)
5995 GtkWidget
*toolbar
= NULL
, *b
, *eb1
;
5997 b
= gtk_hbox_new(FALSE
, 0);
5999 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
6001 /* backward button */
6002 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
6003 gtk_widget_set_sensitive(t
->backward
, FALSE
);
6004 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
6005 G_CALLBACK(backward_cb
), t
);
6006 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, FALSE
, FALSE
, 0);
6008 /* forward button */
6009 t
->forward
= create_button("Forward",GTK_STOCK_GO_FORWARD
, 0);
6010 gtk_widget_set_sensitive(t
->forward
, FALSE
);
6011 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
6012 G_CALLBACK(forward_cb
), t
);
6013 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, FALSE
,
6017 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
6018 gtk_widget_set_sensitive(t
->stop
, FALSE
);
6019 g_signal_connect(G_OBJECT(t
->stop
), "clicked",
6020 G_CALLBACK(stop_cb
), t
);
6021 gtk_box_pack_start(GTK_BOX(b
), t
->stop
, FALSE
,
6025 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
6026 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
6027 gtk_widget_set_sensitive(t
->js_toggle
, TRUE
);
6028 g_signal_connect(G_OBJECT(t
->js_toggle
), "clicked",
6029 G_CALLBACK(js_toggle_cb
), t
);
6030 gtk_box_pack_start(GTK_BOX(b
), t
->js_toggle
, FALSE
, FALSE
, 0);
6032 t
->uri_entry
= gtk_entry_new();
6033 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
6034 G_CALLBACK(activate_uri_entry_cb
), t
);
6035 g_signal_connect(G_OBJECT(t
->uri_entry
), "key-press-event",
6036 G_CALLBACK(entry_key_cb
), t
);
6038 eb1
= gtk_hbox_new(FALSE
, 0);
6039 gtk_container_set_border_width(GTK_CONTAINER(eb1
), 1);
6040 gtk_box_pack_start(GTK_BOX(eb1
), t
->uri_entry
, TRUE
, TRUE
, 0);
6041 gtk_box_pack_start(GTK_BOX(b
), eb1
, TRUE
, TRUE
, 0);
6044 if (search_string
) {
6046 t
->search_entry
= gtk_entry_new();
6047 gtk_entry_set_width_chars(GTK_ENTRY(t
->search_entry
), 30);
6048 g_signal_connect(G_OBJECT(t
->search_entry
), "activate",
6049 G_CALLBACK(activate_search_entry_cb
), t
);
6050 g_signal_connect(G_OBJECT(t
->search_entry
), "key-press-event",
6051 G_CALLBACK(entry_key_cb
), t
);
6052 gtk_widget_set_size_request(t
->search_entry
, -1, -1);
6053 eb2
= gtk_hbox_new(FALSE
, 0);
6054 gtk_container_set_border_width(GTK_CONTAINER(eb2
), 1);
6055 gtk_box_pack_start(GTK_BOX(eb2
), t
->search_entry
, TRUE
, TRUE
,
6057 gtk_box_pack_start(GTK_BOX(b
), eb2
, FALSE
, FALSE
, 0);
6064 create_buffers(struct tab
*t
)
6066 GtkCellRenderer
*renderer
;
6069 view
= gtk_tree_view_new();
6071 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view
), FALSE
);
6073 renderer
= gtk_cell_renderer_text_new();
6074 gtk_tree_view_insert_column_with_attributes
6075 (GTK_TREE_VIEW(view
), -1, "Id", renderer
, "text", COL_ID
, (char *)NULL
);
6077 renderer
= gtk_cell_renderer_text_new();
6078 gtk_tree_view_insert_column_with_attributes
6079 (GTK_TREE_VIEW(view
), -1, "Title", renderer
, "text", COL_TITLE
,
6082 gtk_tree_view_set_model
6083 (GTK_TREE_VIEW(view
), GTK_TREE_MODEL(buffers_store
));
6089 row_activated_cb(GtkTreeView
*view
, GtkTreePath
*path
,
6090 GtkTreeViewColumn
*col
, struct tab
*t
)
6095 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6097 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
,
6100 (GTK_TREE_MODEL(buffers_store
), &iter
, COL_ID
, &id
, -1);
6101 set_current_tab(id
- 1);
6107 /* after tab reordering/creation/removal */
6114 TAILQ_FOREACH(t
, &tabs
, entry
) {
6115 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
6116 if (t
->tab_id
> maxid
)
6119 gtk_widget_show(t
->tab_elems
.sep
);
6122 TAILQ_FOREACH(t
, &tabs
, entry
) {
6123 if (t
->tab_id
== maxid
) {
6124 gtk_widget_hide(t
->tab_elems
.sep
);
6130 /* after active tab change */
6132 recolor_compact_tabs(void)
6138 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6139 TAILQ_FOREACH(t
, &tabs
, entry
)
6140 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
,
6143 curid
= gtk_notebook_get_current_page(notebook
);
6144 TAILQ_FOREACH(t
, &tabs
, entry
)
6145 if (t
->tab_id
== curid
) {
6146 gdk_color_parse(XT_COLOR_CT_ACTIVE
, &color
);
6147 gtk_widget_modify_fg(t
->tab_elems
.label
,
6148 GTK_STATE_NORMAL
, &color
);
6154 set_current_tab(int page_num
)
6156 buffercmd_abort(get_current_tab());
6157 gtk_notebook_set_current_page(notebook
, page_num
);
6158 recolor_compact_tabs();
6162 undo_close_tab_save(struct tab
*t
)
6166 struct undo
*u1
, *u2
;
6168 WebKitWebHistoryItem
*item
;
6170 if ((uri
= get_uri(t
)) == NULL
)
6173 u1
= g_malloc0(sizeof(struct undo
));
6174 u1
->uri
= g_strdup(uri
);
6176 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
6178 m
= webkit_web_back_forward_list_get_forward_length(t
->bfl
);
6179 n
= webkit_web_back_forward_list_get_back_length(t
->bfl
);
6182 /* forward history */
6183 items
= webkit_web_back_forward_list_get_forward_list_with_limit(t
->bfl
, m
);
6187 u1
->history
= g_list_prepend(u1
->history
,
6188 webkit_web_history_item_copy(item
));
6189 items
= g_list_next(items
);
6194 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
6195 u1
->history
= g_list_prepend(u1
->history
,
6196 webkit_web_history_item_copy(item
));
6200 items
= webkit_web_back_forward_list_get_back_list_with_limit(t
->bfl
, n
);
6204 u1
->history
= g_list_prepend(u1
->history
,
6205 webkit_web_history_item_copy(item
));
6206 items
= g_list_next(items
);
6209 TAILQ_INSERT_HEAD(&undos
, u1
, entry
);
6211 if (undo_count
> XT_MAX_UNDO_CLOSE_TAB
) {
6212 u2
= TAILQ_LAST(&undos
, undo_tailq
);
6213 TAILQ_REMOVE(&undos
, u2
, entry
);
6215 g_list_free(u2
->history
);
6224 delete_tab(struct tab
*t
)
6228 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
6234 * no need to join thread here because it won't access t on completion
6237 TAILQ_REMOVE(&tabs
, t
, entry
);
6240 /* Halt all webkit activity. */
6241 abort_favicon_download(t
);
6242 webkit_web_view_stop_loading(t
->wv
);
6244 /* Save the tab, so we can undo the close. */
6245 undo_close_tab_save(t
);
6249 g_source_remove(t
->search_id
);
6252 bzero(&a
, sizeof a
);
6254 inspector_cmd(t
, &a
);
6256 if (browser_mode
== XT_BM_KIOSK
) {
6257 gtk_widget_destroy(t
->uri_entry
);
6258 gtk_widget_destroy(t
->stop
);
6259 gtk_widget_destroy(t
->js_toggle
);
6262 gtk_widget_destroy(t
->tab_elems
.eventbox
);
6263 gtk_widget_destroy(t
->vbox
);
6265 g_free(t
->user_agent
);
6266 g_free(t
->stylesheet
);
6271 if (TAILQ_EMPTY(&tabs
)) {
6272 if (browser_mode
== XT_BM_KIOSK
)
6273 create_new_tab(home
, NULL
, 1, -1);
6275 create_new_tab(NULL
, NULL
, 1, -1);
6278 /* recreate session */
6279 if (session_autosave
) {
6280 bzero(&a
, sizeof a
);
6282 save_tabs(NULL
, &a
);
6286 recolor_compact_tabs();
6290 update_statusbar_zoom(struct tab
*t
)
6293 char s
[16] = { '\0' };
6295 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6296 if ((zoom
<= 0.99 || zoom
>= 1.01))
6297 snprintf(s
, sizeof s
, "%d%%", (int)(zoom
* 100));
6298 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.zoom
), s
);
6302 setzoom_webkit(struct tab
*t
, int adjust
)
6304 #define XT_ZOOMPERCENT 0.04
6309 show_oops(NULL
, "setzoom_webkit invalid parameters");
6313 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6314 if (adjust
== XT_ZOOM_IN
)
6315 zoom
+= XT_ZOOMPERCENT
;
6316 else if (adjust
== XT_ZOOM_OUT
)
6317 zoom
-= XT_ZOOMPERCENT
;
6318 else if (adjust
> 0)
6319 zoom
= default_zoom_level
+ adjust
/ 100.0 - 1.0;
6321 show_oops(t
, "setzoom_webkit invalid zoom value");
6325 if (zoom
< XT_ZOOMPERCENT
)
6326 zoom
= XT_ZOOMPERCENT
;
6327 g_object_set(G_OBJECT(t
->wv
), "zoom-level", zoom
, (char *)NULL
);
6328 update_statusbar_zoom(t
);
6332 tab_clicked_cb(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
6334 struct tab
*t
= (struct tab
*) data
;
6336 DNPRINTF(XT_D_TAB
, "tab_clicked_cb: tab: %d\n", t
->tab_id
);
6338 switch (event
->button
) {
6340 set_current_tab(t
->tab_id
);
6351 append_tab(struct tab
*t
)
6356 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
6357 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
6361 create_sbe(int width
)
6365 sbe
= gtk_entry_new();
6366 gtk_entry_set_inner_border(GTK_ENTRY(sbe
), NULL
);
6367 gtk_entry_set_has_frame(GTK_ENTRY(sbe
), FALSE
);
6368 gtk_widget_set_can_focus(GTK_WIDGET(sbe
), FALSE
);
6369 gtk_widget_modify_font(GTK_WIDGET(sbe
), statusbar_font
);
6370 gtk_entry_set_alignment(GTK_ENTRY(sbe
), 1.0);
6371 gtk_widget_set_size_request(sbe
, width
, -1);
6377 create_new_tab(char *title
, struct undo
*u
, int focus
, int position
)
6382 WebKitWebHistoryItem
*item
;
6386 int sbe_p
= 0, sbe_b
= 0,
6389 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
6391 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
6392 DNPRINTF(XT_D_TAB
, "create_new_tab: new tab rejected\n");
6396 t
= g_malloc0(sizeof *t
);
6398 if (title
== NULL
) {
6399 title
= "(untitled)";
6403 t
->vbox
= gtk_vbox_new(FALSE
, 0);
6405 /* label + button for tab */
6406 b
= gtk_hbox_new(FALSE
, 0);
6409 #if GTK_CHECK_VERSION(2, 20, 0)
6410 t
->spinner
= gtk_spinner_new();
6412 t
->label
= gtk_label_new(title
);
6413 bb
= create_button("Close", GTK_STOCK_CLOSE
, 1);
6414 gtk_widget_set_size_request(t
->label
, 100, 0);
6415 gtk_label_set_max_width_chars(GTK_LABEL(t
->label
), 20);
6416 gtk_label_set_ellipsize(GTK_LABEL(t
->label
), PANGO_ELLIPSIZE_END
);
6417 gtk_widget_set_size_request(b
, 130, 0);
6419 gtk_box_pack_start(GTK_BOX(b
), bb
, FALSE
, FALSE
, 0);
6420 gtk_box_pack_start(GTK_BOX(b
), t
->label
, FALSE
, FALSE
, 0);
6421 #if GTK_CHECK_VERSION(2, 20, 0)
6422 gtk_box_pack_start(GTK_BOX(b
), t
->spinner
, FALSE
, FALSE
, 0);
6426 if (browser_mode
== XT_BM_KIOSK
) {
6427 t
->toolbar
= create_kiosk_toolbar(t
);
6428 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
6431 t
->toolbar
= create_toolbar(t
);
6433 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
,
6441 t
->browser_win
= create_browser(t
);
6442 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
6444 /* oops message for user feedback */
6445 t
->oops
= gtk_entry_new();
6446 gtk_entry_set_inner_border(GTK_ENTRY(t
->oops
), NULL
);
6447 gtk_entry_set_has_frame(GTK_ENTRY(t
->oops
), FALSE
);
6448 gtk_widget_set_can_focus(GTK_WIDGET(t
->oops
), FALSE
);
6449 gdk_color_parse(XT_COLOR_RED
, &color
);
6450 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
6451 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
6452 gtk_widget_modify_font(GTK_WIDGET(t
->oops
), oops_font
);
6455 t
->cmd
= gtk_entry_new();
6456 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
6457 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
6458 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
6459 gtk_widget_modify_font(GTK_WIDGET(t
->cmd
), cmd_font
);
6462 t
->statusbar_box
= gtk_hbox_new(FALSE
, 0);
6464 t
->sbe
.statusbar
= gtk_entry_new();
6465 gtk_entry_set_inner_border(GTK_ENTRY(t
->sbe
.statusbar
), NULL
);
6466 gtk_entry_set_has_frame(GTK_ENTRY(t
->sbe
.statusbar
), FALSE
);
6467 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.statusbar
), FALSE
);
6468 gtk_widget_modify_font(GTK_WIDGET(t
->sbe
.statusbar
), statusbar_font
);
6470 /* create these widgets only if specified in statusbar_elems */
6472 t
->sbe
.position
= create_sbe(40);
6473 t
->sbe
.zoom
= create_sbe(40);
6474 t
->sbe
.buffercmd
= create_sbe(60);
6476 statusbar_modify_attr(t
, XT_COLOR_WHITE
, XT_COLOR_BLACK
);
6478 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), t
->sbe
.statusbar
, TRUE
,
6481 /* gtk widgets cannot be added to a box twice. sbe_* variables
6482 make sure of this */
6483 for (p
= statusbar_elems
; *p
!= '\0'; p
++) {
6487 GtkWidget
*sep
= gtk_vseparator_new();
6489 gdk_color_parse(XT_COLOR_SB_SEPARATOR
, &color
);
6490 gtk_widget_modify_bg(sep
, GTK_STATE_NORMAL
, &color
);
6491 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), sep
,
6492 FALSE
, FALSE
, FALSE
);
6497 warnx("flag \"%c\" specified more than "
6498 "once in statusbar_elems\n", *p
);
6502 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6503 t
->sbe
.position
, FALSE
, FALSE
, FALSE
);
6507 warnx("flag \"%c\" specified more than "
6508 "once in statusbar_elems\n", *p
);
6512 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6513 t
->sbe
.buffercmd
, FALSE
, FALSE
, FALSE
);
6517 warnx("flag \"%c\" specified more than "
6518 "once in statusbar_elems\n", *p
);
6522 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6523 t
->sbe
.zoom
, FALSE
, FALSE
, FALSE
);
6526 warnx("illegal flag \"%c\" in statusbar_elems\n", *p
);
6531 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar_box
, FALSE
, FALSE
, 0);
6534 t
->buffers
= create_buffers(t
);
6535 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->buffers
, FALSE
, FALSE
, 0);
6537 /* xtp meaning is normal by default */
6538 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
6540 /* set empty favicon */
6541 xt_icon_from_name(t
, "text-html");
6543 /* and show it all */
6544 gtk_widget_show_all(b
);
6545 gtk_widget_show_all(t
->vbox
);
6547 /* compact tab bar */
6548 t
->tab_elems
.label
= gtk_label_new(title
);
6549 gtk_label_set_width_chars(GTK_LABEL(t
->tab_elems
.label
), 1.0);
6550 gtk_misc_set_alignment(GTK_MISC(t
->tab_elems
.label
), 0.0, 0.0);
6551 gtk_misc_set_padding(GTK_MISC(t
->tab_elems
.label
), 4.0, 4.0);
6552 gtk_widget_modify_font(GTK_WIDGET(t
->tab_elems
.label
), tabbar_font
);
6554 t
->tab_elems
.eventbox
= gtk_event_box_new();
6555 t
->tab_elems
.box
= gtk_hbox_new(FALSE
, 0);
6556 t
->tab_elems
.sep
= gtk_vseparator_new();
6558 gdk_color_parse(XT_COLOR_CT_BACKGROUND
, &color
);
6559 gtk_widget_modify_bg(t
->tab_elems
.eventbox
, GTK_STATE_NORMAL
, &color
);
6560 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6561 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
, &color
);
6562 gdk_color_parse(XT_COLOR_CT_SEPARATOR
, &color
);
6563 gtk_widget_modify_bg(t
->tab_elems
.sep
, GTK_STATE_NORMAL
, &color
);
6565 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.label
, TRUE
,
6567 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.sep
, FALSE
,
6569 gtk_container_add(GTK_CONTAINER(t
->tab_elems
.eventbox
),
6572 gtk_box_pack_start(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
, TRUE
,
6574 gtk_widget_show_all(t
->tab_elems
.eventbox
);
6576 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
6579 id
= position
>= 0 ? position
:
6580 gtk_notebook_get_current_page(notebook
) + 1;
6581 if (id
> gtk_notebook_get_n_pages(notebook
))
6584 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
6585 gtk_notebook_insert_page(notebook
, t
->vbox
, b
, id
);
6586 gtk_box_reorder_child(GTK_BOX(tab_bar
),
6587 t
->tab_elems
.eventbox
, id
);
6592 #if GTK_CHECK_VERSION(2, 20, 0)
6593 /* turn spinner off if we are a new tab without uri */
6595 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
6596 gtk_widget_hide(t
->spinner
);
6599 /* make notebook tabs reorderable */
6600 gtk_notebook_set_tab_reorderable(notebook
, t
->vbox
, TRUE
);
6602 /* compact tabs clickable */
6603 g_signal_connect(G_OBJECT(t
->tab_elems
.eventbox
),
6604 "button_press_event", G_CALLBACK(tab_clicked_cb
), t
);
6606 g_object_connect(G_OBJECT(t
->cmd
),
6607 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb
), t
,
6608 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
6609 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb
), t
,
6610 "signal::activate", G_CALLBACK(cmd_activate_cb
), t
,
6611 "signal::populate-popup", G_CALLBACK(cmd_popup_cb
), t
,
6614 /* reuse wv_button_cb to hide oops */
6615 g_object_connect(G_OBJECT(t
->oops
),
6616 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
6619 g_signal_connect(t
->buffers
,
6620 "row-activated", G_CALLBACK(row_activated_cb
), t
);
6621 g_object_connect(G_OBJECT(t
->buffers
),
6622 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
, (char *)NULL
);
6624 g_object_connect(G_OBJECT(t
->wv
),
6625 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
,
6626 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb
), t
,
6627 "signal::download-requested", G_CALLBACK(webview_download_cb
), t
,
6628 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), t
,
6629 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
6630 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
6631 "signal::create-web-view", G_CALLBACK(webview_cwv_cb
), t
,
6632 "signal::close-web-view", G_CALLBACK(webview_closewv_cb
), t
,
6633 "signal::event", G_CALLBACK(webview_event_cb
), t
,
6634 "signal::load-finished", G_CALLBACK(webview_load_finished_cb
), t
,
6635 "signal::load-progress-changed", G_CALLBACK(webview_progress_changed_cb
), t
,
6636 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb
), t
,
6637 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
6638 "signal::button_release_event", G_CALLBACK(wv_release_button_cb
), t
,
6639 "signal::populate-popup", G_CALLBACK(wv_popup_cb
), t
,
6641 g_signal_connect(t
->wv
,
6642 "notify::load-status", G_CALLBACK(notify_load_status_cb
), t
);
6643 g_signal_connect(t
->wv
,
6644 "notify::title", G_CALLBACK(notify_title_cb
), t
);
6646 /* hijack the unused keys as if we were the browser */
6647 //g_object_connect(G_OBJECT(t->toolbar),
6648 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
6651 g_signal_connect(G_OBJECT(bb
), "button_press_event",
6652 G_CALLBACK(tab_close_cb
), t
);
6655 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
6656 /* restore the tab's history */
6657 if (u
&& u
->history
) {
6661 webkit_web_back_forward_list_add_item(t
->bfl
, item
);
6662 items
= g_list_next(items
);
6665 item
= g_list_nth_data(u
->history
, u
->back
);
6667 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
6670 g_list_free(u
->history
);
6672 webkit_web_back_forward_list_clear(t
->bfl
);
6678 url_set_visibility();
6679 statusbar_set_visibility();
6682 set_current_tab(t
->tab_id
);
6683 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
6687 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
6691 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6698 recolor_compact_tabs();
6699 setzoom_webkit(t
, XT_ZOOM_NORMAL
);
6704 notebook_switchpage_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
6710 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
6712 if (gtk_notebook_get_current_page(notebook
) == -1)
6715 TAILQ_FOREACH(t
, &tabs
, entry
) {
6716 if (t
->tab_id
== pn
) {
6717 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
6720 uri
= get_title(t
, TRUE
);
6721 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
6727 /* can't use focus_webview here */
6728 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6735 notebook_pagereordered_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
6738 struct tab
*t
= NULL
, *tt
;
6742 TAILQ_FOREACH(tt
, &tabs
, entry
)
6743 if (tt
->tab_id
== pn
) {
6749 DNPRINTF(XT_D_TAB
, "page_reordered_cb: tab: %d\n", t
->tab_id
);
6751 gtk_box_reorder_child(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
,
6756 menuitem_response(struct tab
*t
)
6758 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
6762 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
6764 GtkWidget
*menu
, *menu_items
;
6765 GdkEventButton
*bevent
;
6769 if (event
->type
== GDK_BUTTON_PRESS
) {
6770 bevent
= (GdkEventButton
*) event
;
6771 menu
= gtk_menu_new();
6773 TAILQ_FOREACH(ti
, &tabs
, entry
) {
6774 if ((uri
= get_uri(ti
)) == NULL
)
6775 /* XXX make sure there is something to print */
6776 /* XXX add gui pages in here to look purdy */
6778 menu_items
= gtk_menu_item_new_with_label(uri
);
6779 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), menu_items
);
6780 gtk_widget_show(menu_items
);
6782 g_signal_connect_swapped((menu_items
),
6783 "activate", G_CALLBACK(menuitem_response
),
6787 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
,
6788 bevent
->button
, bevent
->time
);
6790 /* unref object so it'll free itself when popped down */
6791 #if !GTK_CHECK_VERSION(3, 0, 0)
6792 /* XXX does not need unref with gtk+3? */
6793 g_object_ref_sink(menu
);
6794 g_object_unref(menu
);
6797 return (TRUE
/* eat event */);
6800 return (FALSE
/* propagate */);
6804 icon_size_map(int iconsz
)
6806 if (iconsz
<= GTK_ICON_SIZE_INVALID
||
6807 iconsz
> GTK_ICON_SIZE_DIALOG
)
6808 return (GTK_ICON_SIZE_SMALL_TOOLBAR
);
6814 create_button(char *name
, char *stockid
, int size
)
6816 GtkWidget
*button
, *image
;
6820 rcstring
= g_strdup_printf(
6821 "style \"%s-style\"\n"
6823 " GtkWidget::focus-padding = 0\n"
6824 " GtkWidget::focus-line-width = 0\n"
6828 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
6829 gtk_rc_parse_string(rcstring
);
6831 button
= gtk_button_new();
6832 gtk_button_set_focus_on_click(GTK_BUTTON(button
), FALSE
);
6833 gtk_icon_size
= icon_size_map(size
? size
: icon_size
);
6835 image
= gtk_image_new_from_stock(stockid
, gtk_icon_size
);
6836 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
6837 gtk_container_set_border_width(GTK_CONTAINER(button
), 1);
6838 gtk_container_add(GTK_CONTAINER(button
), GTK_WIDGET(image
));
6839 gtk_widget_set_name(button
, name
);
6840 gtk_button_set_relief(GTK_BUTTON(button
), GTK_RELIEF_NONE
);
6846 button_set_stockid(GtkWidget
*button
, char *stockid
)
6850 image
= gtk_image_new_from_stock(stockid
, icon_size_map(icon_size
));
6851 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
6852 gtk_button_set_image(GTK_BUTTON(button
), image
);
6856 clipb_primary_cb(GtkClipboard
*primary
, GdkEvent
*event
, gpointer notused
)
6859 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
6862 if (xterm_workaround
== 0)
6866 * xterm doesn't play nice with clipboards because it clears the
6867 * primary when clicked. We rely on primary being set to properly
6868 * handle middle mouse button clicks (paste). So when someone clears
6869 * primary copy whatever is in CUT_BUFFER0 into primary to simualte
6870 * other application behavior (as in DON'T clear primary).
6873 p
= gtk_clipboard_wait_for_text(primary
);
6875 if (gdk_property_get(gdk_get_default_root_window(),
6877 gdk_atom_intern("STRING", FALSE
),
6879 1024 * 1024 /* picked out of my butt */,
6885 /* yes sir, we need to NUL the string */
6887 gtk_clipboard_set_text(primary
, p
, -1);
6901 char file
[PATH_MAX
];
6904 vbox
= gtk_vbox_new(FALSE
, 0);
6905 gtk_box_set_spacing(GTK_BOX(vbox
), 0);
6906 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
6907 #if !GTK_CHECK_VERSION(3, 0, 0)
6908 /* XXX seems to be needed with gtk+2 */
6909 gtk_notebook_set_tab_hborder(notebook
, 0);
6910 gtk_notebook_set_tab_vborder(notebook
, 0);
6912 gtk_notebook_set_scrollable(notebook
, TRUE
);
6913 gtk_notebook_set_show_border(notebook
, FALSE
);
6914 gtk_widget_set_can_focus(GTK_WIDGET(notebook
), FALSE
);
6916 abtn
= gtk_button_new();
6917 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
6918 gtk_widget_set_size_request(arrow
, -1, -1);
6919 gtk_container_add(GTK_CONTAINER(abtn
), arrow
);
6920 gtk_widget_set_size_request(abtn
, -1, 20);
6922 #if GTK_CHECK_VERSION(2, 20, 0)
6923 gtk_notebook_set_action_widget(notebook
, abtn
, GTK_PACK_END
);
6925 gtk_widget_set_size_request(GTK_WIDGET(notebook
), -1, -1);
6927 /* compact tab bar */
6928 tab_bar
= gtk_hbox_new(TRUE
, 0);
6930 gtk_box_pack_start(GTK_BOX(vbox
), tab_bar
, FALSE
, FALSE
, 0);
6931 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
6932 gtk_widget_set_size_request(vbox
, -1, -1);
6934 g_object_connect(G_OBJECT(notebook
),
6935 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb
), NULL
,
6937 g_object_connect(G_OBJECT(notebook
),
6938 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb
),
6939 NULL
, (char *)NULL
);
6940 g_signal_connect(G_OBJECT(abtn
), "button_press_event",
6941 G_CALLBACK(arrow_cb
), NULL
);
6943 main_window
= create_window("xxxterm");
6944 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
6945 g_signal_connect(G_OBJECT(main_window
), "delete_event",
6946 G_CALLBACK(gtk_main_quit
), NULL
);
6949 for (i
= 0; i
< LENGTH(icons
); i
++) {
6950 snprintf(file
, sizeof file
, "%s/%s", resource_dir
, icons
[i
]);
6951 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
6952 l
= g_list_append(l
, pb
);
6954 gtk_window_set_default_icon_list(l
);
6956 /* clipboard work around */
6957 if (xterm_workaround
)
6959 G_OBJECT(gtk_clipboard_get(GDK_SELECTION_PRIMARY
)),
6960 "owner-change", G_CALLBACK(clipb_primary_cb
), NULL
);
6962 gtk_widget_show_all(abtn
);
6963 gtk_widget_show_all(main_window
);
6964 notebook_tab_set_visibility();
6968 send_cmd_to_socket(char *cmd
)
6971 struct sockaddr_un sa
;
6973 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
6974 warnx("%s: socket", __func__
);
6978 sa
.sun_family
= AF_UNIX
;
6979 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
6980 work_dir
, XT_SOCKET_FILE
);
6983 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
6984 warnx("%s: connect", __func__
);
6988 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
6989 warnx("%s: send", __func__
);
7000 socket_watcher(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
7003 char str
[XT_MAX_URL_LENGTH
];
7004 socklen_t t
= sizeof(struct sockaddr_un
);
7005 struct sockaddr_un sa
;
7010 gint fd
= g_io_channel_unix_get_fd(source
);
7012 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
7017 if (getpeereid(s
, &uid
, &gid
) == -1) {
7021 if (uid
!= getuid() || gid
!= getgid()) {
7022 warnx("unauthorized user");
7028 warnx("not a valid user");
7032 n
= recv(s
, str
, sizeof(str
), 0);
7036 tt
= TAILQ_LAST(&tabs
, tab_list
);
7037 cmd_execute(tt
, str
);
7045 struct sockaddr_un sa
;
7047 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7048 warn("is_running: socket");
7052 sa
.sun_family
= AF_UNIX
;
7053 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
7054 work_dir
, XT_SOCKET_FILE
);
7057 /* connect to see if there is a listener */
7058 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
7059 rv
= 0; /* not running */
7061 rv
= 1; /* already running */
7072 struct sockaddr_un sa
;
7074 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7075 warn("build_socket: socket");
7079 sa
.sun_family
= AF_UNIX
;
7080 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
7081 work_dir
, XT_SOCKET_FILE
);
7084 /* connect to see if there is a listener */
7085 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7086 /* no listener so we will */
7087 unlink(sa
.sun_path
);
7089 if (bind(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7090 warn("build_socket: bind");
7094 if (listen(s
, 1) == -1) {
7095 warn("build_socket: listen");
7108 completion_select_cb(GtkEntryCompletion
*widget
, GtkTreeModel
*model
,
7109 GtkTreeIter
*iter
, struct tab
*t
)
7113 gtk_tree_model_get(model
, iter
, 0, &value
, -1);
7121 completion_hover_cb(GtkEntryCompletion
*widget
, GtkTreeModel
*model
,
7122 GtkTreeIter
*iter
, struct tab
*t
)
7126 gtk_tree_model_get(model
, iter
, 0, &value
, -1);
7127 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), value
);
7128 gtk_editable_set_position(GTK_EDITABLE(t
->uri_entry
), -1);
7135 completion_add_uri(const gchar
*uri
)
7139 /* add uri to list_store */
7140 gtk_list_store_append(completion_model
, &iter
);
7141 gtk_list_store_set(completion_model
, &iter
, 0, uri
, -1);
7145 completion_match(GtkEntryCompletion
*completion
, const gchar
*key
,
7146 GtkTreeIter
*iter
, gpointer user_data
)
7149 gboolean match
= FALSE
;
7151 gtk_tree_model_get(GTK_TREE_MODEL(completion_model
), iter
, 0, &value
,
7157 match
= match_uri(value
, key
);
7164 completion_add(struct tab
*t
)
7166 /* enable completion for tab */
7167 t
->completion
= gtk_entry_completion_new();
7168 gtk_entry_completion_set_text_column(t
->completion
, 0);
7169 gtk_entry_set_completion(GTK_ENTRY(t
->uri_entry
), t
->completion
);
7170 gtk_entry_completion_set_model(t
->completion
,
7171 GTK_TREE_MODEL(completion_model
));
7172 gtk_entry_completion_set_match_func(t
->completion
, completion_match
,
7174 gtk_entry_completion_set_minimum_key_length(t
->completion
, 1);
7175 gtk_entry_completion_set_inline_selection(t
->completion
, TRUE
);
7176 g_signal_connect(G_OBJECT (t
->completion
), "match-selected",
7177 G_CALLBACK(completion_select_cb
), t
);
7178 g_signal_connect(G_OBJECT (t
->completion
), "cursor-on-match",
7179 G_CALLBACK(completion_hover_cb
), t
);
7187 if (stat(dir
, &sb
)) {
7188 if (mkdir(dir
, S_IRWXU
) == -1)
7189 err(1, "mkdir %s", dir
);
7191 err(1, "stat %s", dir
);
7193 if (S_ISDIR(sb
.st_mode
) == 0)
7194 errx(1, "%s not a dir", dir
);
7195 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
7196 warnx("fixing invalid permissions on %s", dir
);
7197 if (chmod(dir
, S_IRWXU
) == -1)
7198 err(1, "chmod %s", dir
);
7206 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
7210 GStaticRecMutex my_gdk_mtx
= G_STATIC_REC_MUTEX_INIT
;
7211 volatile int mtx_depth
;
7215 * The linux flash plugin violates the gdk locking mechanism.
7216 * Work around the issue by using a recursive mutex with some match applied
7217 * to see if we hit a buggy condition.
7219 * The following code is painful so just don't read it. It really doesn't
7220 * make much sense but seems to work.
7225 g_static_rec_mutex_lock(&my_gdk_mtx
);
7228 if (mtx_depth
<= 0) {
7229 /* should not happen */
7230 show_oops(NULL
, "negative mutex locking bug, trying to "
7232 fprintf(stderr
, "negative mutex locking bug, trying to "
7234 g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
7235 g_static_rec_mutex_lock(&my_gdk_mtx
);
7240 if (mtx_depth
!= 1) {
7241 /* decrease mutext depth to 1 */
7243 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7245 } while (mtx_depth
> 1);
7254 /* if mutex depth isn't 1 then something went bad */
7255 if (mtx_depth
!= 1) {
7256 x
= g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
7258 /* should not happen */
7259 show_oops(NULL
, "mutex unlocking bug, trying to "
7261 fprintf(stderr
, "mutex unlocking bug, trying to "
7265 if (mtx_complain
== 0) {
7266 show_oops(NULL
, "buggy mutex implementation detected, "
7267 "work around implemented");
7268 fprintf(stderr
, "buggy mutex implementation detected, "
7269 "work around implemented");
7276 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7280 main(int argc
, char *argv
[])
7283 int c
, s
, optn
= 0, opte
= 0, focus
= 1;
7284 char conf
[PATH_MAX
] = { '\0' };
7285 char file
[PATH_MAX
];
7286 char *env_proxy
= NULL
;
7290 struct sigaction sact
;
7291 GIOChannel
*channel
;
7298 g_thread_init(NULL
);
7299 gdk_threads_set_lock_functions(mtx_lock
, mtx_unlock
);
7301 gdk_threads_enter();
7303 gcry_control (GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
7305 gtk_init(&argc
, &argv
);
7307 gnutls_global_init();
7309 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
7314 RB_INIT(&downloads
);
7316 TAILQ_INIT(&sessions
);
7319 TAILQ_INIT(&aliases
);
7326 /* fiddle with ulimits */
7327 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7330 /* just use them all */
7331 rlp
.rlim_cur
= rlp
.rlim_max
;
7332 if (setrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7334 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7336 else if (rlp
.rlim_cur
<= 256)
7337 startpage_add("%s requires at least 256 file "
7338 "descriptors, currently it has up to %d available",
7339 __progname
, rlp
.rlim_cur
);
7342 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
7351 errx(0 , "Version: %s", version
);
7354 strlcpy(conf
, optarg
, sizeof(conf
));
7357 strlcpy(named_session
, optarg
, sizeof(named_session
));
7378 xtp_generate_keys();
7381 bzero(&sact
, sizeof(sact
));
7382 sigemptyset(&sact
.sa_mask
);
7383 sact
.sa_handler
= sigchild
;
7384 sact
.sa_flags
= SA_NOCLDSTOP
;
7385 sigaction(SIGCHLD
, &sact
, NULL
);
7387 /* set download dir */
7388 pwd
= getpwuid(getuid());
7390 errx(1, "invalid user %d", getuid());
7391 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
7393 /* compile buffer command regexes */
7396 /* set default string settings */
7397 home
= g_strdup("https://www.cyphertite.com");
7398 search_string
= g_strdup("https://ssl.scroogle.org/cgi-bin/nbbwssl.cgi?Gw=%s");
7399 resource_dir
= g_strdup("/usr/local/share/xxxterm/");
7400 strlcpy(runtime_settings
, "runtime", sizeof runtime_settings
);
7401 cmd_font_name
= g_strdup("monospace normal 9");
7402 oops_font_name
= g_strdup("monospace normal 9");
7403 statusbar_font_name
= g_strdup("monospace normal 9");
7404 tabbar_font_name
= g_strdup("monospace normal 9");
7405 statusbar_elems
= g_strdup("BP");
7406 encoding
= g_strdup("UTF-8");
7408 /* read config file */
7409 if (strlen(conf
) == 0)
7410 snprintf(conf
, sizeof conf
, "%s/.%s",
7411 pwd
->pw_dir
, XT_CONF_FILE
);
7412 config_parse(conf
, 0);
7415 cmd_font
= pango_font_description_from_string(cmd_font_name
);
7416 oops_font
= pango_font_description_from_string(oops_font_name
);
7417 statusbar_font
= pango_font_description_from_string(statusbar_font_name
);
7418 tabbar_font
= pango_font_description_from_string(tabbar_font_name
);
7420 /* working directory */
7421 if (strlen(work_dir
) == 0)
7422 snprintf(work_dir
, sizeof work_dir
, "%s/%s",
7423 pwd
->pw_dir
, XT_DIR
);
7426 /* icon cache dir */
7427 snprintf(cache_dir
, sizeof cache_dir
, "%s/%s", work_dir
, XT_CACHE_DIR
);
7431 snprintf(certs_dir
, sizeof certs_dir
, "%s/%s", work_dir
, XT_CERT_DIR
);
7435 snprintf(sessions_dir
, sizeof sessions_dir
, "%s/%s",
7436 work_dir
, XT_SESSIONS_DIR
);
7437 xxx_dir(sessions_dir
);
7440 snprintf(js_dir
, sizeof js_dir
, "%s/%s", work_dir
, XT_JS_DIR
);
7443 /* runtime settings that can override config file */
7444 if (runtime_settings
[0] != '\0')
7445 config_parse(runtime_settings
, 1);
7448 if (!strcmp(download_dir
, pwd
->pw_dir
))
7449 strlcat(download_dir
, "/downloads", sizeof download_dir
);
7450 xxx_dir(download_dir
);
7452 /* favorites file */
7453 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_FAVS_FILE
);
7454 if (stat(file
, &sb
)) {
7455 warnx("favorites file doesn't exist, creating it");
7456 if ((f
= fopen(file
, "w")) == NULL
)
7457 err(1, "favorites");
7461 /* quickmarks file */
7462 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
7463 if (stat(file
, &sb
)) {
7464 warnx("quickmarks file doesn't exist, creating it");
7465 if ((f
= fopen(file
, "w")) == NULL
)
7466 err(1, "quickmarks");
7470 /* search history */
7471 if (history_autosave
) {
7472 snprintf(search_file
, sizeof search_file
, "%s/%s",
7473 work_dir
, XT_SEARCH_FILE
);
7474 if (stat(search_file
, &sb
)) {
7475 warnx("search history file doesn't exist, creating it");
7476 if ((f
= fopen(search_file
, "w")) == NULL
)
7477 err(1, "search_history");
7480 history_read(&shl
, search_file
, &search_history_count
);
7483 /* command history */
7484 if (history_autosave
) {
7485 snprintf(command_file
, sizeof command_file
, "%s/%s",
7486 work_dir
, XT_COMMAND_FILE
);
7487 if (stat(command_file
, &sb
)) {
7488 warnx("command history file doesn't exist, creating it");
7489 if ((f
= fopen(command_file
, "w")) == NULL
)
7490 err(1, "command_history");
7493 history_read(&chl
, command_file
, &cmd_history_count
);
7497 session
= webkit_get_default_session();
7502 if (stat(ssl_ca_file
, &sb
)) {
7503 warnx("no CA file: %s", ssl_ca_file
);
7504 g_free(ssl_ca_file
);
7507 g_object_set(session
,
7508 SOUP_SESSION_SSL_CA_FILE
, ssl_ca_file
,
7509 SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
7513 /* guess_search regex */
7514 if (url_regex
== NULL
)
7515 url_regex
= g_strdup(XT_URL_REGEX
);
7517 if (regcomp(&url_re
, url_regex
, REG_EXTENDED
| REG_NOSUB
))
7518 startpage_add("invalid url regex %s", url_regex
);
7521 env_proxy
= getenv("http_proxy");
7523 setup_proxy(env_proxy
);
7525 setup_proxy(http_proxy
);
7528 send_cmd_to_socket(argv
[0]);
7532 /* set some connection parameters */
7533 g_object_set(session
, "max-conns", max_connections
, (char *)NULL
);
7534 g_object_set(session
, "max-conns-per-host", max_host_connections
,
7537 /* see if there is already an xxxterm running */
7538 if (single_instance
&& is_running()) {
7540 warnx("already running");
7545 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
7546 send_cmd_to_socket(cmd
);
7556 /* uri completion */
7557 completion_model
= gtk_list_store_new(1, G_TYPE_STRING
);
7560 buffers_store
= gtk_list_store_new
7561 (NUM_COLS
, G_TYPE_UINT
, G_TYPE_STRING
);
7567 notebook_tab_set_visibility();
7569 if (save_global_history
)
7570 restore_global_history();
7572 /* restore session list */
7573 restore_sessions_list();
7575 if (!strcmp(named_session
, XT_SAVED_TABS_FILE
))
7576 restore_saved_tabs();
7578 a
.s
= named_session
;
7579 a
.i
= XT_SES_DONOTHING
;
7580 open_tabs(NULL
, &a
);
7583 /* see if we have an exception */
7584 if (!TAILQ_EMPTY(&spl
)) {
7585 create_new_tab("about:startpage", NULL
, focus
, -1);
7590 create_new_tab(argv
[0], NULL
, focus
, -1);
7597 if (TAILQ_EMPTY(&tabs
))
7598 create_new_tab(home
, NULL
, 1, -1);
7601 if ((s
= build_socket()) != -1) {
7602 channel
= g_io_channel_unix_new(s
);
7603 g_io_add_watch(channel
, G_IO_IN
, socket_watcher
, NULL
);
7609 gdk_threads_leave();
7610 g_static_rec_mutex_unlock_full(&my_gdk_mtx
); /* just in case */
7613 gnutls_global_deinit();