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
52 GCRY_THREAD_OPTION_PTHREAD_IMPL
;
64 TAILQ_ENTRY(session
) entry
;
67 TAILQ_HEAD(session_list
, session
);
70 TAILQ_ENTRY(undo
) entry
;
73 int back
; /* Keeps track of how many back
74 * history items there are. */
76 TAILQ_HEAD(undo_tailq
, undo
);
78 struct command_entry
{
80 TAILQ_ENTRY(command_entry
) entry
;
82 TAILQ_HEAD(command_list
, command_entry
);
85 #define XT_DIR (".xxxterm")
86 #define XT_CACHE_DIR ("cache")
87 #define XT_CERT_DIR ("certs/")
88 #define XT_JS_DIR ("js/")
89 #define XT_SESSIONS_DIR ("sessions/")
90 #define XT_CONF_FILE ("xxxterm.conf")
91 #define XT_QMARKS_FILE ("quickmarks")
92 #define XT_SAVED_TABS_FILE ("main_session")
93 #define XT_RESTART_TABS_FILE ("restart_tabs")
94 #define XT_SOCKET_FILE ("socket")
95 #define XT_HISTORY_FILE ("history")
96 #define XT_SAVE_SESSION_ID ("SESSION_NAME=")
97 #define XT_SEARCH_FILE ("search_history")
98 #define XT_COMMAND_FILE ("command_history")
99 #define XT_DLMAN_REFRESH "10"
100 #define XT_MAX_URL_LENGTH (4096) /* 1 page is atomic, don't make bigger */
101 #define XT_MAX_UNDO_CLOSE_TAB (32)
102 #define XT_MAX_HL_PURGE_COUNT (1000) /* Purge the history for every
103 * MAX_HL_PURGE_COUNT items inserted into
104 * history and delete all items older
105 * than MAX_HISTORY_AGE. */
106 #define XT_MAX_HISTORY_AGE (60.0 * 60.0 * 24 * 14) /* 14 days */
107 #define XT_RESERVED_CHARS "$&+,/:;=?@ \"<>#%%{}|^~[]`"
108 #define XT_PRINT_EXTRA_MARGIN 10
109 #define XT_URL_REGEX ("^[[:blank:]]*[^[:blank:]]*([[:alnum:]-]+\\.)+[[:alnum:]-][^[:blank:]]*[[:blank:]]*$")
110 #define XT_INVALID_MARK (-1) /* XXX this is a double, maybe use something else, like a nan */
113 #define XT_COLOR_RED "#cc0000"
114 #define XT_COLOR_YELLOW "#ffff66"
115 #define XT_COLOR_BLUE "lightblue"
116 #define XT_COLOR_GREEN "#99ff66"
117 #define XT_COLOR_WHITE "white"
118 #define XT_COLOR_BLACK "black"
120 #define XT_COLOR_CT_BACKGROUND "#000000"
121 #define XT_COLOR_CT_INACTIVE "#dddddd"
122 #define XT_COLOR_CT_ACTIVE "#bbbb00"
123 #define XT_COLOR_CT_SEPARATOR "#555555"
125 #define XT_COLOR_SB_SEPARATOR "#555555"
127 #define XT_PROTO_DELIM "://"
130 #define XT_MOVE_INVALID (0)
131 #define XT_MOVE_DOWN (1)
132 #define XT_MOVE_UP (2)
133 #define XT_MOVE_BOTTOM (3)
134 #define XT_MOVE_TOP (4)
135 #define XT_MOVE_PAGEDOWN (5)
136 #define XT_MOVE_PAGEUP (6)
137 #define XT_MOVE_HALFDOWN (7)
138 #define XT_MOVE_HALFUP (8)
139 #define XT_MOVE_LEFT (9)
140 #define XT_MOVE_FARLEFT (10)
141 #define XT_MOVE_RIGHT (11)
142 #define XT_MOVE_FARRIGHT (12)
143 #define XT_MOVE_PERCENT (13)
144 #define XT_MOVE_CENTER (14)
146 #define XT_QMARK_SET (0)
147 #define XT_QMARK_OPEN (1)
148 #define XT_QMARK_TAB (2)
150 #define XT_MARK_SET (0)
151 #define XT_MARK_GOTO (1)
153 #define XT_TAB_LAST (-4)
154 #define XT_TAB_FIRST (-3)
155 #define XT_TAB_PREV (-2)
156 #define XT_TAB_NEXT (-1)
157 #define XT_TAB_INVALID (0)
158 #define XT_TAB_NEW (1)
159 #define XT_TAB_DELETE (2)
160 #define XT_TAB_DELQUIT (3)
161 #define XT_TAB_OPEN (4)
162 #define XT_TAB_UNDO_CLOSE (5)
163 #define XT_TAB_SHOW (6)
164 #define XT_TAB_HIDE (7)
165 #define XT_TAB_NEXTSTYLE (8)
167 #define XT_NAV_INVALID (0)
168 #define XT_NAV_BACK (1)
169 #define XT_NAV_FORWARD (2)
170 #define XT_NAV_RELOAD (3)
171 #define XT_NAV_STOP (4)
173 #define XT_FOCUS_INVALID (0)
174 #define XT_FOCUS_URI (1)
175 #define XT_FOCUS_SEARCH (2)
177 #define XT_SEARCH_INVALID (0)
178 #define XT_SEARCH_NEXT (1)
179 #define XT_SEARCH_PREV (2)
181 #define XT_PASTE_CURRENT_TAB (0)
182 #define XT_PASTE_NEW_TAB (1)
184 #define XT_ZOOM_IN (-1)
185 #define XT_ZOOM_OUT (-2)
186 #define XT_ZOOM_NORMAL (100)
188 #define XT_URL_SHOW (1)
189 #define XT_URL_HIDE (2)
191 #define XT_CMD_OPEN (0)
192 #define XT_CMD_OPEN_CURRENT (1)
193 #define XT_CMD_TABNEW (2)
194 #define XT_CMD_TABNEW_CURRENT (3)
196 #define XT_STATUS_NOTHING (0)
197 #define XT_STATUS_LINK (1)
198 #define XT_STATUS_URI (2)
199 #define XT_STATUS_LOADING (3)
201 #define XT_SES_DONOTHING (0)
202 #define XT_SES_CLOSETABS (1)
204 #define XT_PREFIX (1<<0)
205 #define XT_USERARG (1<<1)
206 #define XT_URLARG (1<<2)
207 #define XT_INTARG (1<<3)
208 #define XT_SESSARG (1<<4)
209 #define XT_SETARG (1<<5)
211 #define XT_HINT_NEWTAB (1<<0)
213 #define XT_BUFCMD_SZ (8)
215 #define XT_EJS_SHOW (1<<0)
217 GtkWidget
* create_button(char *, char *, int);
219 void recalc_tabs(void);
220 void recolor_compact_tabs(void);
221 void set_current_tab(int page_num
);
222 gboolean
update_statusbar_position(GtkAdjustment
*, gpointer
);
223 void marks_clear(struct tab
*t
);
226 extern char *__progname
;
229 GtkWidget
*main_window
;
230 GtkNotebook
*notebook
;
232 GtkWidget
*arrow
, *abtn
;
233 struct tab_list tabs
;
234 struct history_list hl
;
235 int hl_purge_count
= 0;
236 struct session_list sessions
;
237 struct domain_list c_wl
;
238 struct domain_list js_wl
;
239 struct domain_list pl_wl
;
240 struct undo_tailq undos
;
241 struct keybinding_list kbl
;
243 struct command_list chl
;
244 struct command_list shl
;
245 struct command_entry
*history_at
;
246 struct command_entry
*search_at
;
248 int cmd_history_count
= 0;
249 int search_history_count
= 0;
251 long long unsigned int blocked_cookies
= 0;
252 char named_session
[PATH_MAX
];
253 GtkListStore
*completion_model
;
254 GtkListStore
*buffers_store
;
256 char *qmarks
[XT_NOMARKS
];
257 int btn_down
; /* M1 down in any wv */
258 regex_t url_re
; /* guess_search regex */
260 /* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
261 int next_download_id
= 1;
263 void xxx_dir(char *);
264 int icon_size_map(int);
265 void completion_add(struct tab
*);
266 void completion_add_uri(const gchar
*);
269 history_delete(struct command_list
*l
, int *counter
)
271 struct command_entry
*c
;
273 if (l
== NULL
|| counter
== NULL
)
276 c
= TAILQ_LAST(l
, command_list
);
280 TAILQ_REMOVE(l
, c
, entry
);
287 history_add(struct command_list
*list
, char *file
, char *l
, int *counter
)
289 struct command_entry
*c
;
292 if (list
== NULL
|| l
== NULL
|| counter
== NULL
)
295 /* don't add the same line */
296 c
= TAILQ_FIRST(list
);
298 if (!strcmp(c
->line
+ 1 /* skip space */, l
))
301 c
= g_malloc0(sizeof *c
);
302 c
->line
= g_strdup_printf(" %s", l
);
305 TAILQ_INSERT_HEAD(list
, c
, entry
);
308 history_delete(list
, counter
);
310 if (history_autosave
&& file
) {
311 f
= fopen(file
, "w");
313 show_oops(NULL
, "couldn't write history %s", file
);
317 TAILQ_FOREACH_REVERSE(c
, list
, command_list
, entry
) {
319 fprintf(f
, "%s\n", c
->line
);
327 history_read(struct command_list
*list
, char *file
, int *counter
)
330 char *s
, line
[65536];
332 if (list
== NULL
|| file
== NULL
)
335 f
= fopen(file
, "r");
337 startpage_add("couldn't open history file %s", file
);
342 s
= fgets(line
, sizeof line
, f
);
343 if (s
== NULL
|| feof(f
) || ferror(f
))
345 if ((s
= strchr(line
, '\n')) == NULL
) {
346 startpage_add("invalid history file %s", file
);
352 history_add(list
, NULL
, line
+ 1, counter
);
360 /* marks and quickmarks array storage.
361 * first a-z, then A-Z, then 0-9 */
368 if (i
>= 0 && i
<= 'z' - 'a')
372 if (i
>= 0 && i
<= 'Z' - 'A')
387 if (m
>= 'a' && m
<= 'z')
388 return ret
+ m
- 'a';
390 ret
+= 'z' - 'a' + 1;
391 if (m
>= 'A' && m
<= 'Z')
392 return ret
+ m
- 'A';
394 ret
+= 'Z' - 'A' + 1;
395 if (m
>= '0' && m
<= '9')
396 return ret
+ m
- '0';
405 int saved_errno
, status
;
410 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
414 if (errno
!= ECHILD
) {
416 clog_warn("sigchild: waitpid:");
422 if (WIFEXITED(status
)) {
423 if (WEXITSTATUS(status
) != 0) {
425 clog_warnx("sigchild: child exit status: %d",
426 WEXITSTATUS(status));
431 clog_warnx("sigchild: child is terminated abnormally");
440 is_g_object_setting(GObject
*o
, char *str
)
442 guint n_props
= 0, i
;
443 GParamSpec
**proplist
;
445 if (! G_IS_OBJECT(o
))
448 proplist
= g_object_class_list_properties(G_OBJECT_GET_CLASS(o
),
451 for (i
=0; i
< n_props
; i
++) {
452 if (! strcmp(proplist
[i
]->name
, str
))
459 get_current_tab(void)
463 TAILQ_FOREACH(t
, &tabs
, entry
) {
464 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
468 warnx("%s: no current tab", __func__
);
474 set_status(struct tab
*t
, gchar
*s
, int status
)
482 case XT_STATUS_LOADING
:
483 type
= g_strdup_printf("Loading: %s", s
);
487 type
= g_strdup_printf("Link: %s", s
);
489 t
->status
= g_strdup(gtk_entry_get_text(
490 GTK_ENTRY(t
->sbe
.statusbar
)));
494 type
= g_strdup_printf("%s", s
);
496 t
->status
= g_strdup(type
);
500 t
->status
= g_strdup(s
);
502 case XT_STATUS_NOTHING
:
507 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.statusbar
), s
);
513 hide_cmd(struct tab
*t
)
515 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
517 history_at
= NULL
; /* just in case */
518 search_at
= NULL
; /* just in case */
519 gtk_widget_hide(t
->cmd
);
523 show_cmd(struct tab
*t
)
525 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
529 gtk_widget_hide(t
->oops
);
530 gtk_widget_show(t
->cmd
);
534 hide_buffers(struct tab
*t
)
536 gtk_widget_hide(t
->buffers
);
537 gtk_list_store_clear(buffers_store
);
547 sort_tabs_by_page_num(struct tab
***stabs
)
552 num_tabs
= gtk_notebook_get_n_pages(notebook
);
554 *stabs
= g_malloc0(num_tabs
* sizeof(struct tab
*));
556 TAILQ_FOREACH(t
, &tabs
, entry
)
557 (*stabs
)[gtk_notebook_page_num(notebook
, t
->vbox
)] = t
;
563 buffers_make_list(void)
566 const gchar
*title
= NULL
;
568 struct tab
**stabs
= NULL
;
570 num_tabs
= sort_tabs_by_page_num(&stabs
);
572 for (i
= 0; i
< num_tabs
; i
++)
574 gtk_list_store_append(buffers_store
, &iter
);
575 title
= get_title(stabs
[i
], FALSE
);
576 gtk_list_store_set(buffers_store
, &iter
,
577 COL_ID
, i
+ 1, /* Enumerate the tabs starting from 1
587 show_buffers(struct tab
*t
)
590 gtk_widget_show(t
->buffers
);
591 gtk_widget_grab_focus(GTK_WIDGET(t
->buffers
));
595 toggle_buffers(struct tab
*t
)
597 if (gtk_widget_get_visible(t
->buffers
))
604 buffers(struct tab
*t
, struct karg
*args
)
612 hide_oops(struct tab
*t
)
614 gtk_widget_hide(t
->oops
);
618 show_oops(struct tab
*at
, const char *fmt
, ...)
622 struct tab
*t
= NULL
;
628 if ((t
= get_current_tab()) == NULL
)
634 if (vasprintf(&msg
, fmt
, ap
) == -1)
635 errx(1, "show_oops failed");
638 gtk_entry_set_text(GTK_ENTRY(t
->oops
), msg
);
639 gtk_widget_hide(t
->cmd
);
640 gtk_widget_show(t
->oops
);
646 char work_dir
[PATH_MAX
];
647 char certs_dir
[PATH_MAX
];
648 char js_dir
[PATH_MAX
];
649 char cache_dir
[PATH_MAX
];
650 char sessions_dir
[PATH_MAX
];
651 char cookie_file
[PATH_MAX
];
652 SoupSession
*session
;
653 SoupCookieJar
*s_cookiejar
;
654 SoupCookieJar
*p_cookiejar
;
655 char rc_fname
[PATH_MAX
];
657 struct mime_type_list mtl
;
658 struct alias_list aliases
;
661 struct tab
*create_new_tab(char *, struct undo
*, int, int);
662 void delete_tab(struct tab
*);
663 void setzoom_webkit(struct tab
*, int);
664 int download_rb_cmp(struct download
*, struct download
*);
665 gboolean
cmd_execute(struct tab
*t
, char *str
);
668 history_rb_cmp(struct history
*h1
, struct history
*h2
)
670 return (strcmp(h1
->uri
, h2
->uri
));
672 RB_GENERATE(history_list
, history
, entry
, history_rb_cmp
);
675 domain_rb_cmp(struct domain
*d1
, struct domain
*d2
)
677 return (strcmp(d1
->d
, d2
->d
));
679 RB_GENERATE(domain_list
, domain
, entry
, domain_rb_cmp
);
682 download_rb_cmp(struct download
*e1
, struct download
*e2
)
684 return (e1
->id
< e2
->id
? -1 : e1
->id
> e2
->id
);
686 RB_GENERATE(download_list
, download
, entry
, download_rb_cmp
);
688 struct valid_url_types
{
699 valid_url_type(char *url
)
703 for (i
= 0; i
< LENGTH(vut
); i
++)
704 if (!strncasecmp(vut
[i
].type
, url
, strlen(vut
[i
].type
)))
711 match_alias(char *url_in
)
715 char *url_out
= NULL
, *search
, *enc_arg
;
717 search
= g_strdup(url_in
);
719 if (strsep(&arg
, " \t") == NULL
) {
720 show_oops(NULL
, "match_alias: NULL URL");
724 TAILQ_FOREACH(a
, &aliases
, entry
) {
725 if (!strcmp(search
, a
->a_name
))
730 DNPRINTF(XT_D_URL
, "match_alias: matched alias %s\n",
733 enc_arg
= soup_uri_encode(arg
, XT_RESERVED_CHARS
);
734 url_out
= g_strdup_printf(a
->a_uri
, enc_arg
);
737 url_out
= g_strdup_printf(a
->a_uri
, "");
745 guess_url_type(char *url_in
)
748 char *url_out
= NULL
, *enc_search
= NULL
;
751 /* substitute aliases */
752 url_out
= match_alias(url_in
);
756 /* see if we are an about page */
757 if (!strncmp(url_in
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
))
758 for (i
= 0; i
< about_list_size(); i
++)
759 if (!strcmp(&url_in
[XT_URI_ABOUT_LEN
],
760 about_list
[i
].name
)) {
761 url_out
= g_strdup(url_in
);
765 if (guess_search
&& url_regex
&&
766 !(g_str_has_prefix(url_in
, "http://") ||
767 g_str_has_prefix(url_in
, "https://"))) {
768 if (regexec(&url_re
, url_in
, 0, NULL
, 0)) {
769 /* invalid URI so search instead */
770 enc_search
= soup_uri_encode(url_in
, XT_RESERVED_CHARS
);
771 url_out
= g_strdup_printf(search_string
, enc_search
);
777 /* XXX not sure about this heuristic */
778 if (stat(url_in
, &sb
) == 0)
779 url_out
= g_strdup_printf("file://%s", url_in
);
781 url_out
= g_strdup_printf("http://%s", url_in
); /* guess http */
783 DNPRINTF(XT_D_URL
, "guess_url_type: guessed %s\n", url_out
);
789 load_uri(struct tab
*t
, gchar
*uri
)
792 gchar
*newuri
= NULL
;
798 /* Strip leading spaces. */
799 while (*uri
&& isspace(*uri
))
802 if (strlen(uri
) == 0) {
807 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
809 if (valid_url_type(uri
)) {
810 newuri
= guess_url_type(uri
);
814 if (!strncmp(uri
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
)) {
815 for (i
= 0; i
< about_list_size(); i
++)
816 if (!strcmp(&uri
[XT_URI_ABOUT_LEN
], about_list
[i
].name
)) {
817 bzero(&args
, sizeof args
);
818 about_list
[i
].func(t
, &args
);
819 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
),
823 show_oops(t
, "invalid about page");
827 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
829 webkit_web_view_load_uri(t
->wv
, uri
);
836 get_uri(struct tab
*t
)
838 const gchar
*uri
= NULL
;
840 if (webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
)
842 if (t
->xtp_meaning
== XT_XTP_TAB_MEANING_NORMAL
) {
843 uri
= webkit_web_view_get_uri(t
->wv
);
845 /* use tmp_uri to make sure it is g_freed */
848 t
->tmp_uri
=g_strdup_printf("%s%s", XT_URI_ABOUT
,
849 about_list
[t
->xtp_meaning
].name
);
856 get_title(struct tab
*t
, bool window
)
858 const gchar
*set
= NULL
, *title
= NULL
;
859 WebKitLoadStatus status
= webkit_web_view_get_load_status(t
->wv
);
861 if (status
== WEBKIT_LOAD_PROVISIONAL
|| status
== WEBKIT_LOAD_FAILED
||
862 t
->xtp_meaning
== XT_XTP_TAB_MEANING_BL
)
865 title
= webkit_web_view_get_title(t
->wv
);
866 if ((set
= title
? title
: get_uri(t
)))
870 set
= window
? XT_NAME
: "(untitled)";
876 find_mime_type(char *mime_type
)
878 struct mime_type
*m
, *def
= NULL
, *rv
= NULL
;
880 TAILQ_FOREACH(m
, &mtl
, entry
) {
882 !strncmp(mime_type
, m
->mt_type
, strlen(m
->mt_type
)))
885 if (m
->mt_default
== 0 && !strcmp(mime_type
, m
->mt_type
)) {
898 wl_find_uri(const gchar
*s
, struct domain_list
*wl
)
904 if (s
== NULL
|| wl
== NULL
)
907 if (!strncmp(s
, "http://", strlen("http://")))
908 s
= &s
[strlen("http://")];
909 else if (!strncmp(s
, "https://", strlen("https://")))
910 s
= &s
[strlen("https://")];
915 for (i
= 0; i
< strlen(s
) + 1 /* yes er need this */; i
++)
916 /* chop string at first slash */
917 if (s
[i
] == '/' || s
[i
] == ':' || s
[i
] == '\0') {
929 js_ref_to_string(JSContextRef context
, JSValueRef ref
)
935 jsref
= JSValueToStringCopy(context
, ref
, NULL
);
939 l
= JSStringGetMaximumUTF8CStringSize(jsref
);
942 JSStringGetUTF8CString(jsref
, s
, l
);
943 JSStringRelease(jsref
);
949 disable_hints(struct tab
*t
)
951 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
953 run_script(t
, "hints.clearHints();");
954 t
->mode
= XT_MODE_COMMAND
;
959 enable_hints(struct tab
*t
)
961 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
964 run_script(t
, "hints.createHints('', 'F');");
966 run_script(t
, "hints.createHints('', 'f');");
967 t
->mode
= XT_MODE_HINT
;
970 #define XT_JS_DONE ("done;")
971 #define XT_JS_DONE_LEN (strlen(XT_JS_DONE))
972 #define XT_JS_INSERT ("insert;")
973 #define XT_JS_INSERT_LEN (strlen(XT_JS_INSERT))
976 run_script(struct tab
*t
, char *s
)
978 JSGlobalContextRef ctx
;
979 WebKitWebFrame
*frame
;
981 JSValueRef val
, exception
;
984 DNPRINTF(XT_D_JS
, "run_script: tab %d %s\n",
985 t
->tab_id
, s
== (char *)JS_HINTING
? "JS_HINTING" : s
);
987 frame
= webkit_web_view_get_main_frame(t
->wv
);
988 ctx
= webkit_web_frame_get_global_context(frame
);
990 str
= JSStringCreateWithUTF8CString(s
);
991 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
992 NULL
, 0, &exception
);
993 JSStringRelease(str
);
995 DNPRINTF(XT_D_JS
, "run_script: val %p\n", val
);
997 es
= js_ref_to_string(ctx
, exception
);
999 /* show_oops(t, "script exception: %s", es); */
1000 DNPRINTF(XT_D_JS
, "run_script: exception %s\n", es
);
1005 es
= js_ref_to_string(ctx
, val
);
1008 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
1010 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
1014 DNPRINTF(XT_D_JS
, "run_script: val %s\n", es
);
1023 hint(struct tab
*t
, struct karg
*args
)
1026 DNPRINTF(XT_D_JS
, "hint: tab %d args %d\n", t
->tab_id
, args
->i
);
1028 if (t
->mode
== XT_MODE_HINT
) {
1029 if (args
->i
== XT_HINT_NEWTAB
)
1039 apply_style(struct tab
*t
)
1041 g_object_set(G_OBJECT(t
->settings
),
1042 "user-stylesheet-uri", t
->stylesheet
, (char *)NULL
);
1046 userstyle(struct tab
*t
, struct karg
*args
)
1048 DNPRINTF(XT_D_JS
, "userstyle: tab %d\n", t
->tab_id
);
1052 g_object_set(G_OBJECT(t
->settings
),
1053 "user-stylesheet-uri", NULL
, (char *)NULL
);
1064 struct history
*h
, *next
;
1067 DNPRINTF(XT_D_HISTORY
, "%s: hl_purge_count = %d (%d is max)\n",
1068 __func__
, hl_purge_count
, XT_MAX_HL_PURGE_COUNT
);
1070 if (hl_purge_count
== XT_MAX_HL_PURGE_COUNT
) {
1073 for (h
= RB_MIN(history_list
, &hl
); h
!= NULL
; h
= next
) {
1075 next
= RB_NEXT(history_list
, &hl
, h
);
1077 age
= difftime(time(NULL
), h
->time
);
1079 if (age
> XT_MAX_HISTORY_AGE
) {
1080 DNPRINTF(XT_D_HISTORY
, "%s: removing %s (age %.1f)\n",
1081 __func__
, h
->uri
, age
);
1083 RB_REMOVE(history_list
, &hl
, h
);
1088 DNPRINTF(XT_D_HISTORY
, "%s: keeping %s (age %.1f)\n",
1089 __func__
, h
->uri
, age
);
1098 insert_history_item(const gchar
*uri
, const gchar
*title
, time_t time
)
1102 if (!(uri
&& strlen(uri
) && title
&& strlen(title
)))
1105 h
= g_malloc(sizeof(struct history
));
1106 h
->uri
= g_strdup(uri
);
1107 h
->title
= g_strdup(title
);
1110 DNPRINTF(XT_D_HISTORY
, "%s: adding %s\n", __func__
, h
->uri
);
1112 RB_INSERT(history_list
, &hl
, h
);
1113 completion_add_uri(h
->uri
);
1117 update_history_tabs(NULL
);
1123 restore_global_history(void)
1125 char file
[PATH_MAX
];
1127 gchar
*uri
, *title
, *stime
, *err
= NULL
;
1130 const char delim
[3] = {'\\', '\\', '\0'};
1132 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_HISTORY_FILE
);
1134 if ((f
= fopen(file
, "r")) == NULL
) {
1135 warnx("%s: fopen", __func__
);
1140 if ((uri
= fparseln(f
, NULL
, NULL
, delim
, 0)) == NULL
)
1141 if (feof(f
) || ferror(f
))
1144 if ((title
= fparseln(f
, NULL
, NULL
, delim
, 0)) == NULL
)
1145 if (feof(f
) || ferror(f
)) {
1146 err
= "broken history file (title)";
1150 if ((stime
= fparseln(f
, NULL
, NULL
, delim
, 0)) == NULL
)
1151 if (feof(f
) || ferror(f
)) {
1152 err
= "broken history file (time)";
1156 if (strptime(stime
, "%a %b %d %H:%M:%S %Y", &tm
) == NULL
) {
1157 err
= "strptime failed to parse time";
1163 if (insert_history_item(uri
, title
, time
)) {
1164 err
= "failed to insert item";
1177 if (err
&& strlen(err
)) {
1178 warnx("%s: %s\n", __func__
, err
);
1190 save_global_history_to_disk(struct tab
*t
)
1192 char file
[PATH_MAX
];
1196 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_HISTORY_FILE
);
1198 if ((f
= fopen(file
, "w")) == NULL
) {
1199 show_oops(t
, "%s: global history file: %s",
1200 __func__
, strerror(errno
));
1204 RB_FOREACH_REVERSE(h
, history_list
, &hl
) {
1205 if (h
->uri
&& h
->title
&& h
->time
)
1206 fprintf(f
, "%s\n%s\n%s", h
->uri
, h
->title
,
1216 quit(struct tab
*t
, struct karg
*args
)
1218 if (save_global_history
)
1219 save_global_history_to_disk(t
);
1227 restore_sessions_list(void)
1230 struct dirent
*dp
= NULL
;
1233 sdir
= opendir(sessions_dir
);
1235 while ((dp
= readdir(sdir
)) != NULL
)
1236 if (dp
->d_type
== DT_REG
) {
1237 s
= g_malloc(sizeof(struct session
));
1238 s
->name
= g_strdup(dp
->d_name
);
1239 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
1246 open_tabs(struct tab
*t
, struct karg
*a
)
1248 char file
[PATH_MAX
];
1252 struct tab
*ti
, *tt
;
1257 ti
= TAILQ_LAST(&tabs
, tab_list
);
1259 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, a
->s
);
1260 if ((f
= fopen(file
, "r")) == NULL
)
1264 if ((uri
= fparseln(f
, NULL
, NULL
, "\0\0\0", 0)) == NULL
)
1265 if (feof(f
) || ferror(f
))
1268 /* retrieve session name */
1269 if (uri
&& g_str_has_prefix(uri
, XT_SAVE_SESSION_ID
)) {
1270 strlcpy(named_session
,
1271 &uri
[strlen(XT_SAVE_SESSION_ID
)],
1272 sizeof named_session
);
1276 if (uri
&& strlen(uri
))
1277 create_new_tab(uri
, NULL
, 1, -1);
1283 /* close open tabs */
1284 if (a
->i
== XT_SES_CLOSETABS
&& ti
!= NULL
) {
1286 tt
= TAILQ_FIRST(&tabs
);
1307 restore_saved_tabs(void)
1309 char file
[PATH_MAX
];
1310 int unlink_file
= 0;
1315 snprintf(file
, sizeof file
, "%s/%s",
1316 sessions_dir
, XT_RESTART_TABS_FILE
);
1317 if (stat(file
, &sb
) == -1)
1318 a
.s
= XT_SAVED_TABS_FILE
;
1321 a
.s
= XT_RESTART_TABS_FILE
;
1324 a
.i
= XT_SES_DONOTHING
;
1325 rv
= open_tabs(NULL
, &a
);
1334 save_tabs(struct tab
*t
, struct karg
*a
)
1336 char file
[PATH_MAX
];
1338 int num_tabs
= 0, i
;
1339 struct tab
**stabs
= NULL
;
1341 /* tab may be null here */
1346 snprintf(file
, sizeof file
, "%s/%s",
1347 sessions_dir
, named_session
);
1349 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, a
->s
);
1351 if ((f
= fopen(file
, "w")) == NULL
) {
1352 show_oops(t
, "Can't open save_tabs file: %s", strerror(errno
));
1356 /* save session name */
1357 fprintf(f
, "%s%s\n", XT_SAVE_SESSION_ID
, named_session
);
1359 /* Save tabs, in the order they are arranged in the notebook. */
1360 num_tabs
= sort_tabs_by_page_num(&stabs
);
1362 for (i
= 0; i
< num_tabs
; i
++)
1364 if (get_uri(stabs
[i
]) != NULL
)
1365 fprintf(f
, "%s\n", get_uri(stabs
[i
]));
1366 else if (gtk_entry_get_text(GTK_ENTRY(
1367 stabs
[i
]->uri_entry
)))
1368 fprintf(f
, "%s\n", gtk_entry_get_text(GTK_ENTRY(
1369 stabs
[i
]->uri_entry
)));
1374 /* try and make sure this gets to disk NOW. XXX Backup first? */
1375 if (fflush(f
) != 0 || fsync(fileno(f
)) != 0) {
1376 show_oops(t
, "May not have managed to save session: %s",
1386 save_tabs_and_quit(struct tab
*t
, struct karg
*args
)
1397 /* Marshall the internal record of visited URIs into a Javascript hash table in
1400 color_visited_helper(void)
1405 RB_FOREACH_REVERSE(h
, history_list
, &hl
) {
1407 s
= g_strdup_printf("'%s':'dummy'", h
->uri
);
1409 s
= g_strjoin(",", s
,
1410 g_strdup_printf("'%s':'dummy'", h
->uri
), NULL
);
1413 s
= g_strdup_printf("{%s}", s
);
1415 DNPRINTF(XT_D_VISITED
, "%s: s = %s\n", __func__
, s
);
1421 color_visited(struct tab
*t
, char *visited
)
1425 if (t
== NULL
|| visited
== NULL
) {
1426 show_oops(NULL
, "%s: invalid parameters", __func__
);
1430 /* Create a string representing an annonymous Javascript function, which
1431 * takes a hash table of visited URIs as an argument, goes through the
1432 * links at the current web page and colors them if they indeed been
1436 "(function(visitedUris) {",
1437 " for (var i = 0; i < document.links.length; i++)",
1438 " if (visitedUris[document.links[i].href])",
1439 " document.links[i].style.color = 'purple';",
1441 /* Apply the annonymous function to the hash table containing
1443 g_strdup_printf("(%s);", visited
),
1454 run_page_script(struct tab
*t
, struct karg
*args
)
1457 char *tmp
, script
[PATH_MAX
];
1459 tmp
= args
->s
!= NULL
&& strlen(args
->s
) > 0 ? args
->s
: default_script
;
1460 if (tmp
[0] == '\0') {
1461 show_oops(t
, "no script specified");
1465 if ((uri
= get_uri(t
)) == NULL
) {
1466 show_oops(t
, "tab is empty, not running script");
1471 snprintf(script
, sizeof script
, "%s/%s",
1472 pwd
->pw_dir
, &tmp
[1]);
1474 strlcpy(script
, tmp
, sizeof script
);
1478 show_oops(t
, "can't fork to run script");
1488 execlp(script
, script
, uri
, (void *)NULL
);
1498 yank_uri(struct tab
*t
, struct karg
*args
)
1501 GtkClipboard
*clipboard
;
1503 if ((uri
= get_uri(t
)) == NULL
)
1506 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1507 gtk_clipboard_set_text(clipboard
, uri
, -1);
1513 paste_uri(struct tab
*t
, struct karg
*args
)
1515 GtkClipboard
*clipboard
;
1516 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
1518 gchar
*p
= NULL
, *uri
;
1520 /* try primary clipboard first */
1521 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1522 p
= gtk_clipboard_wait_for_text(clipboard
);
1524 /* if it failed get whatever text is in cut_buffer0 */
1525 if (p
== NULL
&& xterm_workaround
)
1526 if (gdk_property_get(gdk_get_default_root_window(),
1528 gdk_atom_intern("STRING", FALSE
),
1530 1024 * 1024 /* picked out of my butt */,
1536 /* yes sir, we need to NUL the string */
1542 while (*uri
&& isspace(*uri
))
1544 if (strlen(uri
) == 0) {
1545 show_oops(t
, "empty paste buffer");
1548 if (guess_search
== 0 && valid_url_type(uri
)) {
1549 /* we can be clever and paste this in search box */
1550 show_oops(t
, "not a valid URL");
1554 if (args
->i
== XT_PASTE_CURRENT_TAB
)
1556 else if (args
->i
== XT_PASTE_NEW_TAB
)
1557 create_new_tab(uri
, NULL
, 1, -1);
1568 js_toggle_cb(GtkWidget
*w
, struct tab
*t
)
1573 g_object_get(G_OBJECT(t
->settings
),
1574 "enable-scripts", &es
, (char *)NULL
);
1579 set
= XT_WL_DISABLE
;
1581 a
.i
= set
| XT_WL_TOPLEVEL
;
1584 a
.i
= set
| XT_WL_TOPLEVEL
;
1587 a
.i
= XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
;
1592 toggle_src(struct tab
*t
, struct karg
*args
)
1599 mode
= webkit_web_view_get_view_source_mode(t
->wv
);
1600 webkit_web_view_set_view_source_mode(t
->wv
, !mode
);
1601 webkit_web_view_reload(t
->wv
);
1607 focus_webview(struct tab
*t
)
1612 /* only grab focus if we are visible */
1613 if (gtk_notebook_get_current_page(notebook
) == t
->tab_id
)
1614 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
1618 focus(struct tab
*t
, struct karg
*args
)
1620 if (t
== NULL
|| args
== NULL
)
1626 if (args
->i
== XT_FOCUS_URI
)
1627 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
1628 else if (args
->i
== XT_FOCUS_SEARCH
)
1629 gtk_widget_grab_focus(GTK_WIDGET(t
->search_entry
));
1635 connect_socket_from_uri(const gchar
*uri
, const gchar
**error_str
, char *domain
,
1639 struct addrinfo hints
, *res
= NULL
, *ai
;
1640 int rv
= -1, s
= -1, on
, error
;
1642 static gchar myerror
[256]; /* this is not thread safe */
1645 *error_str
= myerror
;
1646 if (uri
&& !g_str_has_prefix(uri
, "https://")) {
1647 *error_str
= "invalid URI";
1651 su
= soup_uri_new(uri
);
1653 *error_str
= "invalid soup URI";
1656 if (!SOUP_URI_VALID_FOR_HTTP(su
)) {
1657 *error_str
= "invalid HTTPS URI";
1661 snprintf(port
, sizeof port
, "%d", su
->port
);
1662 bzero(&hints
, sizeof(struct addrinfo
));
1663 hints
.ai_flags
= AI_CANONNAME
;
1664 hints
.ai_family
= AF_UNSPEC
;
1665 hints
.ai_socktype
= SOCK_STREAM
;
1667 if ((error
= getaddrinfo(su
->host
, port
, &hints
, &res
))) {
1668 snprintf(myerror
, sizeof myerror
, "getaddrinfo failed: %s",
1669 gai_strerror(errno
));
1673 for (ai
= res
; ai
; ai
= ai
->ai_next
) {
1679 if (ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
)
1681 s
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
1684 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &on
,
1687 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) == 0)
1691 snprintf(myerror
, sizeof myerror
,
1692 "could not obtain certificates from: %s",
1698 strlcpy(domain
, su
->host
, domain_sz
);
1705 if (rv
== -1 && s
!= -1)
1712 stop_tls(gnutls_session_t gsession
, gnutls_certificate_credentials_t xcred
)
1715 gnutls_deinit(gsession
);
1717 gnutls_certificate_free_credentials(xcred
);
1723 start_tls(const gchar
**error_str
, int s
, gnutls_session_t
*gs
,
1724 gnutls_certificate_credentials_t
*xc
)
1726 gnutls_certificate_credentials_t xcred
;
1727 gnutls_session_t gsession
;
1729 static gchar myerror
[1024]; /* this is not thread safe */
1731 if (gs
== NULL
|| xc
== NULL
)
1738 gnutls_certificate_allocate_credentials(&xcred
);
1739 gnutls_certificate_set_x509_trust_file(xcred
, ssl_ca_file
,
1740 GNUTLS_X509_FMT_PEM
);
1742 gnutls_init(&gsession
, GNUTLS_CLIENT
);
1743 gnutls_priority_set_direct(gsession
, "PERFORMANCE", NULL
);
1744 gnutls_credentials_set(gsession
, GNUTLS_CRD_CERTIFICATE
, xcred
);
1745 gnutls_transport_set_ptr(gsession
, (gnutls_transport_ptr_t
)(long)s
);
1746 if ((rv
= gnutls_handshake(gsession
)) < 0) {
1747 snprintf(myerror
, sizeof myerror
,
1748 "gnutls_handshake failed %d fatal %d %s",
1750 gnutls_error_is_fatal(rv
),
1751 gnutls_strerror_name(rv
));
1752 stop_tls(gsession
, xcred
);
1756 gnutls_credentials_type_t cred
;
1757 cred
= gnutls_auth_get_type(gsession
);
1758 if (cred
!= GNUTLS_CRD_CERTIFICATE
) {
1759 snprintf(myerror
, sizeof myerror
,
1760 "gnutls_auth_get_type failed %d",
1762 stop_tls(gsession
, xcred
);
1770 *error_str
= myerror
;
1775 get_connection_certs(gnutls_session_t gsession
, gnutls_x509_crt_t
**certs
,
1779 const gnutls_datum_t
*cl
;
1780 gnutls_x509_crt_t
*all_certs
;
1783 if (certs
== NULL
|| cert_count
== NULL
)
1785 if (gnutls_certificate_type_get(gsession
) != GNUTLS_CRT_X509
)
1787 cl
= gnutls_certificate_get_peers(gsession
, &len
);
1791 all_certs
= g_malloc(sizeof(gnutls_x509_crt_t
) * len
);
1792 for (i
= 0; i
< len
; i
++) {
1793 gnutls_x509_crt_init(&all_certs
[i
]);
1794 if (gnutls_x509_crt_import(all_certs
[i
], &cl
[i
],
1795 GNUTLS_X509_FMT_PEM
< 0)) {
1809 free_connection_certs(gnutls_x509_crt_t
*certs
, size_t cert_count
)
1813 for (i
= 0; i
< cert_count
; i
++)
1814 gnutls_x509_crt_deinit(certs
[i
]);
1819 statusbar_modify_attr(struct tab
*t
, const char *text
, const char *base
)
1821 GdkColor c_text
, c_base
;
1823 gdk_color_parse(text
, &c_text
);
1824 gdk_color_parse(base
, &c_base
);
1826 gtk_widget_modify_text(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_text
);
1827 gtk_widget_modify_text(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_text
);
1828 gtk_widget_modify_text(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_text
);
1829 gtk_widget_modify_text(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_text
);
1831 gtk_widget_modify_base(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_base
);
1832 gtk_widget_modify_base(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_base
);
1833 gtk_widget_modify_base(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_base
);
1834 gtk_widget_modify_base(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_base
);
1838 save_certs(struct tab
*t
, gnutls_x509_crt_t
*certs
,
1839 size_t cert_count
, char *domain
)
1842 char cert_buf
[64 * 1024], file
[PATH_MAX
];
1847 if (t
== NULL
|| certs
== NULL
|| cert_count
<= 0 || domain
== NULL
)
1850 snprintf(file
, sizeof file
, "%s/%s", certs_dir
, domain
);
1851 if ((f
= fopen(file
, "w")) == NULL
) {
1852 show_oops(t
, "Can't create cert file %s %s",
1853 file
, strerror(errno
));
1857 for (i
= 0; i
< cert_count
; i
++) {
1858 cert_buf_sz
= sizeof cert_buf
;
1859 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1860 cert_buf
, &cert_buf_sz
)) {
1861 show_oops(t
, "gnutls_x509_crt_export failed");
1864 if (fwrite(cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1865 show_oops(t
, "Can't write certs: %s", strerror(errno
));
1870 /* not the best spot but oh well */
1871 gdk_color_parse("lightblue", &color
);
1872 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
1873 statusbar_modify_attr(t
, XT_COLOR_BLACK
, "lightblue");
1886 load_compare_cert(const gchar
*uri
, const gchar
**error_str
)
1888 char domain
[8182], file
[PATH_MAX
];
1889 char cert_buf
[64 * 1024], r_cert_buf
[64 * 1024];
1891 unsigned int error
= 0;
1893 size_t cert_buf_sz
, cert_count
;
1894 enum cert_trust rv
= CERT_UNTRUSTED
;
1895 static gchar serr
[80]; /* this isn't thread safe */
1896 gnutls_session_t gsession
;
1897 gnutls_x509_crt_t
*certs
;
1898 gnutls_certificate_credentials_t xcred
;
1900 DNPRINTF(XT_D_URL
, "%s: %s\n", __func__
, uri
);
1904 if ((s
= connect_socket_from_uri(uri
, error_str
, domain
,
1905 sizeof domain
)) == -1)
1908 DNPRINTF(XT_D_URL
, "%s: fd %d\n", __func__
, s
);
1911 if (start_tls(error_str
, s
, &gsession
, &xcred
))
1913 DNPRINTF(XT_D_URL
, "%s: got tls\n", __func__
);
1915 /* verify certs in case cert file doesn't exist */
1916 if (gnutls_certificate_verify_peers2(gsession
, &error
) !=
1918 *error_str
= "Invalid certificates";
1923 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1924 *error_str
= "Can't get connection certificates";
1928 snprintf(file
, sizeof file
, "%s/%s", certs_dir
, domain
);
1929 if ((f
= fopen(file
, "r")) == NULL
) {
1935 for (i
= 0; i
< cert_count
; i
++) {
1936 cert_buf_sz
= sizeof cert_buf
;
1937 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1938 cert_buf
, &cert_buf_sz
)) {
1941 if (fread(r_cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1942 rv
= CERT_BAD
; /* critical */
1945 if (bcmp(r_cert_buf
, cert_buf
, sizeof cert_buf_sz
)) {
1946 rv
= CERT_BAD
; /* critical */
1955 free_connection_certs(certs
, cert_count
);
1957 /* we close the socket first for speed */
1961 /* only complain if we didn't save it locally */
1962 if (error
&& rv
!= CERT_LOCAL
) {
1963 strlcpy(serr
, "Certificate exception(s): ", sizeof serr
);
1964 if (error
& GNUTLS_CERT_INVALID
)
1965 strlcat(serr
, "invalid, ", sizeof serr
);
1966 if (error
& GNUTLS_CERT_REVOKED
)
1967 strlcat(serr
, "revoked, ", sizeof serr
);
1968 if (error
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
1969 strlcat(serr
, "signer not found, ", sizeof serr
);
1970 if (error
& GNUTLS_CERT_SIGNER_NOT_CA
)
1971 strlcat(serr
, "not signed by CA, ", sizeof serr
);
1972 if (error
& GNUTLS_CERT_INSECURE_ALGORITHM
)
1973 strlcat(serr
, "insecure algorithm, ", sizeof serr
);
1974 if (error
& GNUTLS_CERT_NOT_ACTIVATED
)
1975 strlcat(serr
, "not activated, ", sizeof serr
);
1976 if (error
& GNUTLS_CERT_EXPIRED
)
1977 strlcat(serr
, "expired, ", sizeof serr
);
1978 for (i
= strlen(serr
) - 1; i
> 0; i
--)
1979 if (serr
[i
] == ',') {
1986 stop_tls(gsession
, xcred
);
1992 cert_cmd(struct tab
*t
, struct karg
*args
)
1994 const gchar
*uri
, *error_str
= NULL
;
1998 gnutls_session_t gsession
;
1999 gnutls_x509_crt_t
*certs
;
2000 gnutls_certificate_credentials_t xcred
;
2005 if (ssl_ca_file
== NULL
) {
2006 show_oops(t
, "Can't open CA file: %s", ssl_ca_file
);
2010 if ((uri
= get_uri(t
)) == NULL
) {
2011 show_oops(t
, "Invalid URI");
2015 if ((s
= connect_socket_from_uri(uri
, &error_str
, domain
,
2016 sizeof domain
)) == -1) {
2017 show_oops(t
, "%s", error_str
);
2022 if (start_tls(&error_str
, s
, &gsession
, &xcred
))
2026 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
2027 show_oops(t
, "get_connection_certs failed");
2031 if (args
->i
& XT_SHOW
)
2032 show_certs(t
, certs
, cert_count
, "Certificate Chain");
2033 else if (args
->i
& XT_SAVE
)
2034 save_certs(t
, certs
, cert_count
, domain
);
2036 free_connection_certs(certs
, cert_count
);
2038 /* we close the socket first for speed */
2041 stop_tls(gsession
, xcred
);
2042 if (error_str
&& strlen(error_str
))
2043 show_oops(t
, "%s", error_str
);
2048 remove_cookie(int index
)
2054 DNPRINTF(XT_D_COOKIE
, "remove_cookie: %d\n", index
);
2056 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
2058 for (i
= 1; cf
; cf
= cf
->next
, i
++) {
2062 print_cookie("remove cookie", c
);
2063 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2068 soup_cookies_free(cf
);
2074 toplevel_cmd(struct tab
*t
, struct karg
*args
)
2076 js_toggle_cb(t
->js_toggle
, t
);
2082 can_go_back_for_real(struct tab
*t
)
2085 WebKitWebHistoryItem
*item
;
2091 /* rely on webkit to make sure we can go backward when on an about page */
2093 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
2094 return (webkit_web_view_can_go_back(t
->wv
));
2096 /* the back/forwars list is stupid so help determine if we can go back */
2097 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2099 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2100 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
2108 can_go_forward_for_real(struct tab
*t
)
2111 WebKitWebHistoryItem
*item
;
2117 /* rely on webkit to make sure we can go forward when on an about page */
2119 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
2120 return (webkit_web_view_can_go_forward(t
->wv
));
2122 /* the back/forwars list is stupid so help selecting a different item */
2123 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2125 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2126 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
2134 go_back_for_real(struct tab
*t
)
2137 WebKitWebHistoryItem
*item
;
2145 webkit_web_view_go_back(t
->wv
);
2148 /* the back/forwars list is stupid so help selecting a different item */
2149 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2151 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2152 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2153 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2160 go_forward_for_real(struct tab
*t
)
2163 WebKitWebHistoryItem
*item
;
2171 webkit_web_view_go_forward(t
->wv
);
2174 /* the back/forwars list is stupid so help selecting a different item */
2175 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2177 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2178 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2179 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2186 navaction(struct tab
*t
, struct karg
*args
)
2188 WebKitWebHistoryItem
*item
;
2189 WebKitWebFrame
*frame
;
2191 DNPRINTF(XT_D_NAV
, "navaction: tab %d opcode %d\n",
2192 t
->tab_id
, args
->i
);
2194 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
2196 if (args
->i
== XT_NAV_BACK
)
2197 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2199 item
= webkit_web_back_forward_list_get_forward_item(t
->bfl
);
2201 return (XT_CB_PASSTHROUGH
);
2202 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2204 return (XT_CB_PASSTHROUGH
);
2210 go_back_for_real(t
);
2212 case XT_NAV_FORWARD
:
2214 go_forward_for_real(t
);
2217 frame
= webkit_web_view_get_main_frame(t
->wv
);
2218 webkit_web_frame_reload(frame
);
2221 frame
= webkit_web_view_get_main_frame(t
->wv
);
2222 webkit_web_frame_stop_loading(frame
);
2225 return (XT_CB_PASSTHROUGH
);
2229 move(struct tab
*t
, struct karg
*args
)
2231 GtkAdjustment
*adjust
;
2232 double pi
, si
, pos
, ps
, upper
, lower
, max
;
2238 case XT_MOVE_BOTTOM
:
2240 case XT_MOVE_PAGEDOWN
:
2241 case XT_MOVE_PAGEUP
:
2242 case XT_MOVE_HALFDOWN
:
2243 case XT_MOVE_HALFUP
:
2244 case XT_MOVE_PERCENT
:
2245 case XT_MOVE_CENTER
:
2246 adjust
= t
->adjust_v
;
2249 adjust
= t
->adjust_h
;
2253 pos
= gtk_adjustment_get_value(adjust
);
2254 ps
= gtk_adjustment_get_page_size(adjust
);
2255 upper
= gtk_adjustment_get_upper(adjust
);
2256 lower
= gtk_adjustment_get_lower(adjust
);
2257 si
= gtk_adjustment_get_step_increment(adjust
);
2258 pi
= gtk_adjustment_get_page_increment(adjust
);
2261 DNPRINTF(XT_D_MOVE
, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2262 "max %f si %f pi %f\n",
2263 args
->i
, adjust
== t
->adjust_h
? "horizontal" : "vertical",
2264 pos
, ps
, upper
, lower
, max
, si
, pi
);
2270 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2275 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2277 case XT_MOVE_BOTTOM
:
2278 case XT_MOVE_FARRIGHT
:
2279 gtk_adjustment_set_value(adjust
, max
);
2282 case XT_MOVE_FARLEFT
:
2283 gtk_adjustment_set_value(adjust
, lower
);
2285 case XT_MOVE_PAGEDOWN
:
2287 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2289 case XT_MOVE_PAGEUP
:
2291 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2293 case XT_MOVE_HALFDOWN
:
2295 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2297 case XT_MOVE_HALFUP
:
2299 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2301 case XT_MOVE_CENTER
:
2302 args
->s
= g_strdup("50.0");
2304 case XT_MOVE_PERCENT
:
2305 percent
= atoi(args
->s
) / 100.0;
2306 pos
= max
* percent
;
2307 if (pos
< 0.0 || pos
> max
)
2309 gtk_adjustment_set_value(adjust
, pos
);
2312 return (XT_CB_PASSTHROUGH
);
2315 DNPRINTF(XT_D_MOVE
, "move: new pos %f %f\n", pos
, MIN(pos
, max
));
2317 return (XT_CB_HANDLED
);
2321 url_set_visibility(void)
2325 TAILQ_FOREACH(t
, &tabs
, entry
)
2326 if (show_url
== 0) {
2327 gtk_widget_hide(t
->toolbar
);
2330 gtk_widget_show(t
->toolbar
);
2334 notebook_tab_set_visibility(void)
2336 if (show_tabs
== 0) {
2337 gtk_widget_hide(tab_bar
);
2338 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2340 if (tab_style
== XT_TABS_NORMAL
) {
2341 gtk_widget_hide(tab_bar
);
2342 gtk_notebook_set_show_tabs(notebook
, TRUE
);
2343 } else if (tab_style
== XT_TABS_COMPACT
) {
2344 gtk_widget_show(tab_bar
);
2345 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2351 statusbar_set_visibility(void)
2355 TAILQ_FOREACH(t
, &tabs
, entry
){
2356 if (show_statusbar
== 0)
2357 gtk_widget_hide(t
->statusbar_box
);
2359 gtk_widget_show(t
->statusbar_box
);
2366 url_set(struct tab
*t
, int enable_url_entry
)
2371 show_url
= enable_url_entry
;
2373 if (enable_url_entry
) {
2374 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
2375 GTK_ENTRY_ICON_PRIMARY
, NULL
);
2376 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
), 0);
2378 pixbuf
= gtk_entry_get_icon_pixbuf(GTK_ENTRY(t
->uri_entry
),
2379 GTK_ENTRY_ICON_PRIMARY
);
2381 gtk_entry_get_progress_fraction(GTK_ENTRY(t
->uri_entry
));
2382 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
2383 GTK_ENTRY_ICON_PRIMARY
, pixbuf
);
2384 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
2390 fullscreen(struct tab
*t
, struct karg
*args
)
2392 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2395 return (XT_CB_PASSTHROUGH
);
2397 if (show_url
== 0) {
2405 url_set_visibility();
2406 notebook_tab_set_visibility();
2408 return (XT_CB_HANDLED
);
2412 statustoggle(struct tab
*t
, struct karg
*args
)
2414 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2416 if (show_statusbar
== 1) {
2418 statusbar_set_visibility();
2419 } else if (show_statusbar
== 0) {
2421 statusbar_set_visibility();
2423 return (XT_CB_HANDLED
);
2427 urlaction(struct tab
*t
, struct karg
*args
)
2429 int rv
= XT_CB_HANDLED
;
2431 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2434 return (XT_CB_PASSTHROUGH
);
2438 if (show_url
== 0) {
2440 url_set_visibility();
2444 if (show_url
== 1) {
2446 url_set_visibility();
2454 tabaction(struct tab
*t
, struct karg
*args
)
2456 int rv
= XT_CB_HANDLED
;
2457 char *url
= args
->s
;
2461 DNPRINTF(XT_D_TAB
, "tabaction: %p %d\n", t
, args
->i
);
2464 return (XT_CB_PASSTHROUGH
);
2468 if (strlen(url
) > 0)
2469 create_new_tab(url
, NULL
, 1, args
->precount
);
2471 create_new_tab(NULL
, NULL
, 1, args
->precount
);
2474 if (args
->precount
< 0)
2477 TAILQ_FOREACH(tt
, &tabs
, entry
)
2478 if (tt
->tab_id
== args
->precount
- 1) {
2483 case XT_TAB_DELQUIT
:
2484 if (gtk_notebook_get_n_pages(notebook
) > 1)
2490 if (strlen(url
) > 0)
2493 rv
= XT_CB_PASSTHROUGH
;
2499 if (show_tabs
== 0) {
2501 notebook_tab_set_visibility();
2505 if (show_tabs
== 1) {
2507 notebook_tab_set_visibility();
2510 case XT_TAB_NEXTSTYLE
:
2511 if (tab_style
== XT_TABS_NORMAL
) {
2512 tab_style
= XT_TABS_COMPACT
;
2513 recolor_compact_tabs();
2516 tab_style
= XT_TABS_NORMAL
;
2517 notebook_tab_set_visibility();
2519 case XT_TAB_UNDO_CLOSE
:
2520 if (undo_count
== 0) {
2521 DNPRINTF(XT_D_TAB
, "%s: no tabs to undo close",
2526 u
= TAILQ_FIRST(&undos
);
2527 create_new_tab(u
->uri
, u
, 1, -1);
2529 TAILQ_REMOVE(&undos
, u
, entry
);
2531 /* u->history is freed in create_new_tab() */
2536 rv
= XT_CB_PASSTHROUGH
;
2550 resizetab(struct tab
*t
, struct karg
*args
)
2552 if (t
== NULL
|| args
== NULL
) {
2553 show_oops(NULL
, "resizetab invalid parameters");
2554 return (XT_CB_PASSTHROUGH
);
2557 DNPRINTF(XT_D_TAB
, "resizetab: tab %d %d\n",
2558 t
->tab_id
, args
->i
);
2560 setzoom_webkit(t
, args
->i
);
2562 return (XT_CB_HANDLED
);
2566 movetab(struct tab
*t
, struct karg
*args
)
2570 if (t
== NULL
|| args
== NULL
) {
2571 show_oops(NULL
, "movetab invalid parameters");
2572 return (XT_CB_PASSTHROUGH
);
2575 DNPRINTF(XT_D_TAB
, "movetab: tab %d opcode %d\n",
2576 t
->tab_id
, args
->i
);
2578 if (args
->i
>= XT_TAB_INVALID
)
2579 return (XT_CB_PASSTHROUGH
);
2581 if (TAILQ_EMPTY(&tabs
))
2582 return (XT_CB_PASSTHROUGH
);
2584 n
= gtk_notebook_get_n_pages(notebook
);
2585 dest
= gtk_notebook_get_current_page(notebook
);
2589 if (args
->precount
< 0)
2590 dest
= dest
== n
- 1 ? 0 : dest
+ 1;
2592 dest
= args
->precount
- 1;
2596 if (args
->precount
< 0)
2599 dest
-= args
->precount
% n
;
2612 return (XT_CB_PASSTHROUGH
);
2615 if (dest
< 0 || dest
>= n
)
2616 return (XT_CB_PASSTHROUGH
);
2617 if (t
->tab_id
== dest
) {
2618 DNPRINTF(XT_D_TAB
, "movetab: do nothing\n");
2619 return (XT_CB_HANDLED
);
2622 set_current_tab(dest
);
2624 return (XT_CB_HANDLED
);
2631 command(struct tab
*t
, struct karg
*args
)
2633 char *s
= NULL
, *ss
= NULL
;
2638 if (t
== NULL
|| args
== NULL
) {
2639 show_oops(NULL
, "command invalid parameters");
2640 return (XT_CB_PASSTHROUGH
);
2651 if (cmd_prefix
== 0)
2654 ss
= g_strdup_printf(":%d", cmd_prefix
);
2660 t
->mode
= XT_MODE_HINT
;
2661 bzero(&a
, sizeof a
);
2667 t
->mode
= XT_MODE_HINT
;
2668 bzero(&a
, sizeof a
);
2669 a
.i
= XT_HINT_NEWTAB
;
2679 case XT_CMD_OPEN_CURRENT
:
2682 case XT_CMD_TABNEW_CURRENT
:
2683 if (!s
) /* FALL THROUGH? */
2685 if ((uri
= get_uri(t
)) != NULL
) {
2686 ss
= g_strdup_printf("%s%s", s
, uri
);
2691 show_oops(t
, "command: invalid opcode %d", args
->i
);
2692 return (XT_CB_PASSTHROUGH
);
2695 DNPRINTF(XT_D_CMD
, "%s: tab %d type %s\n", __func__
, t
->tab_id
, s
);
2697 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), s
);
2698 gdk_color_parse(XT_COLOR_WHITE
, &color
);
2699 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
2701 gtk_widget_grab_focus(GTK_WIDGET(t
->cmd
));
2702 gtk_editable_set_position(GTK_EDITABLE(t
->cmd
), -1);
2707 return (XT_CB_HANDLED
);
2711 search(struct tab
*t
, struct karg
*args
)
2715 if (t
== NULL
|| args
== NULL
) {
2716 show_oops(NULL
, "search invalid parameters");
2721 case XT_SEARCH_NEXT
:
2722 d
= t
->search_forward
;
2724 case XT_SEARCH_PREV
:
2725 d
= !t
->search_forward
;
2728 return (XT_CB_PASSTHROUGH
);
2731 if (t
->search_text
== NULL
) {
2732 if (global_search
== NULL
)
2733 return (XT_CB_PASSTHROUGH
);
2735 d
= t
->search_forward
= TRUE
;
2736 t
->search_text
= g_strdup(global_search
);
2737 webkit_web_view_mark_text_matches(t
->wv
, global_search
, FALSE
, 0);
2738 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
2742 DNPRINTF(XT_D_CMD
, "search: tab %d opc %d forw %d text %s\n",
2743 t
->tab_id
, args
->i
, t
->search_forward
, t
->search_text
);
2745 webkit_web_view_search_text(t
->wv
, t
->search_text
, FALSE
, d
, TRUE
);
2747 return (XT_CB_HANDLED
);
2751 session_save(struct tab
*t
, char *filename
)
2757 if (strlen(filename
) == 0)
2760 if (filename
[0] == '.' || filename
[0] == '/')
2764 if (save_tabs(t
, &a
))
2766 strlcpy(named_session
, filename
, sizeof named_session
);
2768 /* add the new session to the list of sessions */
2769 s
= g_malloc(sizeof(struct session
));
2770 s
->name
= g_strdup(filename
);
2771 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
2779 session_open(struct tab
*t
, char *filename
)
2784 if (strlen(filename
) == 0)
2787 if (filename
[0] == '.' || filename
[0] == '/')
2791 a
.i
= XT_SES_CLOSETABS
;
2792 if (open_tabs(t
, &a
))
2795 strlcpy(named_session
, filename
, sizeof named_session
);
2803 session_delete(struct tab
*t
, char *filename
)
2805 char file
[PATH_MAX
];
2809 if (strlen(filename
) == 0)
2812 if (filename
[0] == '.' || filename
[0] == '/')
2815 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, filename
);
2819 if (!strcmp(filename
, named_session
))
2820 strlcpy(named_session
, XT_SAVED_TABS_FILE
,
2821 sizeof named_session
);
2823 /* remove session from sessions list */
2824 TAILQ_FOREACH(s
, &sessions
, entry
) {
2825 if (!strcmp(s
->name
, filename
))
2830 TAILQ_REMOVE(&sessions
, s
, entry
);
2831 g_free((gpointer
) s
->name
);
2840 session_cmd(struct tab
*t
, struct karg
*args
)
2842 char *filename
= args
->s
;
2847 if (args
->i
& XT_SHOW
)
2848 show_oops(t
, "Current session: %s", named_session
[0] == '\0' ?
2849 XT_SAVED_TABS_FILE
: named_session
);
2850 else if (args
->i
& XT_SAVE
) {
2851 if (session_save(t
, filename
)) {
2852 show_oops(t
, "Can't save session: %s",
2853 filename
? filename
: "INVALID");
2856 } else if (args
->i
& XT_OPEN
) {
2857 if (session_open(t
, filename
)) {
2858 show_oops(t
, "Can't open session: %s",
2859 filename
? filename
: "INVALID");
2862 } else if (args
->i
& XT_DELETE
) {
2863 if (session_delete(t
, filename
)) {
2864 show_oops(t
, "Can't delete session: %s",
2865 filename
? filename
: "INVALID");
2870 return (XT_CB_PASSTHROUGH
);
2874 script_cmd(struct tab
*t
, struct karg
*args
)
2876 JSGlobalContextRef ctx
;
2877 WebKitWebFrame
*frame
;
2879 JSValueRef val
, exception
;
2888 if ((f
= fopen(args
->s
, "r")) == NULL
) {
2889 show_oops(t
, "Can't open script file: %s", args
->s
);
2893 if (fstat(fileno(f
), &sb
) == -1) {
2894 show_oops(t
, "Can't stat script file: %s", args
->s
);
2898 buf
= g_malloc0(sb
.st_size
+ 1);
2899 if (fread(buf
, 1, sb
.st_size
, f
) != sb
.st_size
) {
2900 show_oops(t
, "Can't read script file: %s", args
->s
);
2904 /* this code needs to be redone */
2905 frame
= webkit_web_view_get_main_frame(t
->wv
);
2906 ctx
= webkit_web_frame_get_global_context(frame
);
2908 str
= JSStringCreateWithUTF8CString(buf
);
2909 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
2910 NULL
, 0, &exception
);
2911 JSStringRelease(str
);
2913 DNPRINTF(XT_D_JS
, "%s: val %p\n", __func__
, val
);
2915 es
= js_ref_to_string(ctx
, exception
);
2917 show_oops(t
, "script exception: %s", es
);
2922 es
= js_ref_to_string(ctx
, val
);
2925 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
2927 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
2931 show_oops(t
, "script complete return value: '%s'", es
);
2934 show_oops(t
, "script complete: without a return value");
2943 return (XT_CB_PASSTHROUGH
);
2947 * Make a hardcopy of the page
2950 print_page(struct tab
*t
, struct karg
*args
)
2952 WebKitWebFrame
*frame
;
2954 GtkPrintOperation
*op
;
2955 GtkPrintOperationAction action
;
2956 GtkPrintOperationResult print_res
;
2957 GError
*g_err
= NULL
;
2958 int marg_l
, marg_r
, marg_t
, marg_b
;
2960 DNPRINTF(XT_D_PRINTING
, "%s:", __func__
);
2962 ps
= gtk_page_setup_new();
2963 op
= gtk_print_operation_new();
2964 action
= GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
;
2965 frame
= webkit_web_view_get_main_frame(t
->wv
);
2967 /* the default margins are too small, so we will bump them */
2968 marg_l
= gtk_page_setup_get_left_margin(ps
, GTK_UNIT_MM
) +
2969 XT_PRINT_EXTRA_MARGIN
;
2970 marg_r
= gtk_page_setup_get_right_margin(ps
, GTK_UNIT_MM
) +
2971 XT_PRINT_EXTRA_MARGIN
;
2972 marg_t
= gtk_page_setup_get_top_margin(ps
, GTK_UNIT_MM
) +
2973 XT_PRINT_EXTRA_MARGIN
;
2974 marg_b
= gtk_page_setup_get_bottom_margin(ps
, GTK_UNIT_MM
) +
2975 XT_PRINT_EXTRA_MARGIN
;
2978 gtk_page_setup_set_left_margin(ps
, marg_l
, GTK_UNIT_MM
);
2979 gtk_page_setup_set_right_margin(ps
, marg_r
, GTK_UNIT_MM
);
2980 gtk_page_setup_set_top_margin(ps
, marg_t
, GTK_UNIT_MM
);
2981 gtk_page_setup_set_bottom_margin(ps
, marg_b
, GTK_UNIT_MM
);
2983 gtk_print_operation_set_default_page_setup(op
, ps
);
2985 /* this appears to free 'op' and 'ps' */
2986 print_res
= webkit_web_frame_print_full(frame
, op
, action
, &g_err
);
2988 /* check it worked */
2989 if (print_res
== GTK_PRINT_OPERATION_RESULT_ERROR
) {
2990 show_oops(NULL
, "can't print: %s", g_err
->message
);
2991 g_error_free (g_err
);
2999 go_home(struct tab
*t
, struct karg
*args
)
3006 set_encoding(struct tab
*t
, struct karg
*args
)
3010 if (args
->s
&& strlen(g_strstrip(args
->s
)) == 0) {
3011 e
= webkit_web_view_get_custom_encoding(t
->wv
);
3013 e
= webkit_web_view_get_encoding(t
->wv
);
3014 show_oops(t
, "encoding: %s", e
? e
: "N/A");
3016 webkit_web_view_set_custom_encoding(t
->wv
, args
->s
);
3022 restart(struct tab
*t
, struct karg
*args
)
3026 a
.s
= XT_RESTART_TABS_FILE
;
3028 execvp(start_argv
[0], start_argv
);
3034 char *http_proxy_save
; /* not a setting, used to toggle */
3037 proxy_cmd(struct tab
*t
, struct karg
*args
)
3039 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
3046 if (http_proxy_save
)
3047 g_free(http_proxy_save
);
3048 http_proxy_save
= g_strdup(http_proxy
);
3051 if (args
->i
& XT_PRXY_SHOW
) {
3053 show_oops(t
, "http_proxy = %s", http_proxy
);
3055 show_oops(t
, "proxy is currently disabled");
3056 } else if (args
->i
& XT_PRXY_TOGGLE
) {
3057 if (http_proxy_save
== NULL
&& http_proxy
== NULL
) {
3058 show_oops(t
, "can't toggle proxy");
3064 setup_proxy(http_proxy_save
);
3067 return (XT_CB_PASSTHROUGH
);
3072 int (*func
)(struct tab
*, struct karg
*);
3076 { "command_mode", 0, command_mode
, XT_MODE_COMMAND
, 0 },
3077 { "insert_mode", 0, command_mode
, XT_MODE_INSERT
, 0 },
3078 { "command", 0, command
, ':', 0 },
3079 { "search", 0, command
, '/', 0 },
3080 { "searchb", 0, command
, '?', 0 },
3081 { "hinting", 0, command
, '.', 0 },
3082 { "hinting_newtab", 0, command
, ',', 0 },
3083 { "togglesrc", 0, toggle_src
, 0, 0 },
3085 /* yanking and pasting */
3086 { "yankuri", 0, yank_uri
, 0, 0 },
3087 /* XXX: pasteuri{cur,new} do not work from the cmd_entry? */
3088 { "pasteuricur", 0, paste_uri
, XT_PASTE_CURRENT_TAB
, 0 },
3089 { "pasteurinew", 0, paste_uri
, XT_PASTE_NEW_TAB
, 0 },
3092 { "searchnext", 0, search
, XT_SEARCH_NEXT
, 0 },
3093 { "searchprevious", 0, search
, XT_SEARCH_PREV
, 0 },
3096 { "focusaddress", 0, focus
, XT_FOCUS_URI
, 0 },
3097 { "focussearch", 0, focus
, XT_FOCUS_SEARCH
, 0 },
3100 { "hinting", 0, hint
, 0, 0 },
3101 { "hinting_newtab", 0, hint
, XT_HINT_NEWTAB
, 0 },
3103 /* custom stylesheet */
3104 { "userstyle", 0, userstyle
, 0, 0 },
3107 { "goback", 0, navaction
, XT_NAV_BACK
, 0 },
3108 { "goforward", 0, navaction
, XT_NAV_FORWARD
, 0 },
3109 { "reload", 0, navaction
, XT_NAV_RELOAD
, 0 },
3110 { "stop", 0, navaction
, XT_NAV_STOP
, 0 },
3112 /* vertical movement */
3113 { "scrolldown", 0, move
, XT_MOVE_DOWN
, 0 },
3114 { "scrollup", 0, move
, XT_MOVE_UP
, 0 },
3115 { "scrollbottom", 0, move
, XT_MOVE_BOTTOM
, 0 },
3116 { "scrolltop", 0, move
, XT_MOVE_TOP
, 0 },
3117 { "1", 0, move
, XT_MOVE_TOP
, 0 },
3118 { "scrollhalfdown", 0, move
, XT_MOVE_HALFDOWN
, 0 },
3119 { "scrollhalfup", 0, move
, XT_MOVE_HALFUP
, 0 },
3120 { "scrollpagedown", 0, move
, XT_MOVE_PAGEDOWN
, 0 },
3121 { "scrollpageup", 0, move
, XT_MOVE_PAGEUP
, 0 },
3122 /* horizontal movement */
3123 { "scrollright", 0, move
, XT_MOVE_RIGHT
, 0 },
3124 { "scrollleft", 0, move
, XT_MOVE_LEFT
, 0 },
3125 { "scrollfarright", 0, move
, XT_MOVE_FARRIGHT
, 0 },
3126 { "scrollfarleft", 0, move
, XT_MOVE_FARLEFT
, 0 },
3128 { "favorites", 0, xtp_page_fl
, 0, 0 },
3129 { "fav", 0, xtp_page_fl
, 0, 0 },
3130 { "favadd", 0, add_favorite
, 0, 0 },
3132 { "qall", 0, quit
, 0, 0 },
3133 { "quitall", 0, quit
, 0, 0 },
3134 { "w", 0, save_tabs
, 0, 0 },
3135 { "wq", 0, save_tabs_and_quit
, 0, 0 },
3136 { "help", 0, help
, 0, 0 },
3137 { "about", 0, about
, 0, 0 },
3138 { "stats", 0, stats
, 0, 0 },
3139 { "version", 0, about
, 0, 0 },
3142 { "js", 0, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3143 { "save", 1, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3144 { "domain", 2, js_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3145 { "fqdn", 2, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3146 { "show", 1, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3147 { "all", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3148 { "persistent", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3149 { "session", 2, js_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3150 { "toggle", 1, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3151 { "domain", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3152 { "fqdn", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3154 /* cookie command */
3155 { "cookie", 0, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3156 { "save", 1, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3157 { "domain", 2, cookie_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3158 { "fqdn", 2, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3159 { "show", 1, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3160 { "all", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3161 { "persistent", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3162 { "session", 2, cookie_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3163 { "toggle", 1, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3164 { "domain", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3165 { "fqdn", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3167 /* plugin command */
3168 { "plugin", 0, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3169 { "save", 1, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3170 { "domain", 2, pl_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3171 { "fqdn", 2, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3172 { "show", 1, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3173 { "all", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3174 { "persistent", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3175 { "session", 2, pl_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3176 { "toggle", 1, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3177 { "domain", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3178 { "fqdn", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3180 /* toplevel (domain) command */
3181 { "toplevel", 0, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3182 { "toggle", 1, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3185 { "cookiejar", 0, xtp_page_cl
, 0, 0 },
3188 { "cert", 0, cert_cmd
, XT_SHOW
, 0 },
3189 { "save", 1, cert_cmd
, XT_SAVE
, 0 },
3190 { "show", 1, cert_cmd
, XT_SHOW
, 0 },
3192 { "ca", 0, ca_cmd
, 0, 0 },
3193 { "downloadmgr", 0, xtp_page_dl
, 0, 0 },
3194 { "dl", 0, xtp_page_dl
, 0, 0 },
3195 { "h", 0, xtp_page_hl
, 0, 0 },
3196 { "history", 0, xtp_page_hl
, 0, 0 },
3197 { "home", 0, go_home
, 0, 0 },
3198 { "restart", 0, restart
, 0, 0 },
3199 { "urlhide", 0, urlaction
, XT_URL_HIDE
, 0 },
3200 { "urlshow", 0, urlaction
, XT_URL_SHOW
, 0 },
3201 { "statustoggle", 0, statustoggle
, 0, 0 },
3202 { "run_script", 0, run_page_script
, 0, XT_USERARG
},
3204 { "print", 0, print_page
, 0, 0 },
3207 { "focusin", 0, resizetab
, XT_ZOOM_IN
, 0 },
3208 { "focusout", 0, resizetab
, XT_ZOOM_OUT
, 0 },
3209 { "focusreset", 0, resizetab
, XT_ZOOM_NORMAL
, 0 },
3210 { "q", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3211 { "quit", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3212 { "open", 0, tabaction
, XT_TAB_OPEN
, XT_URLARG
},
3213 { "tabclose", 0, tabaction
, XT_TAB_DELETE
, XT_PREFIX
| XT_INTARG
},
3214 { "tabedit", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3215 { "tabfirst", 0, movetab
, XT_TAB_FIRST
, 0 },
3216 { "tabhide", 0, tabaction
, XT_TAB_HIDE
, 0 },
3217 { "tablast", 0, movetab
, XT_TAB_LAST
, 0 },
3218 { "tabnew", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3219 { "tabnext", 0, movetab
, XT_TAB_NEXT
, XT_PREFIX
| XT_INTARG
},
3220 { "tabnextstyle", 0, tabaction
, XT_TAB_NEXTSTYLE
, 0 },
3221 { "tabprevious", 0, movetab
, XT_TAB_PREV
, XT_PREFIX
| XT_INTARG
},
3222 { "tabrewind", 0, movetab
, XT_TAB_FIRST
, 0 },
3223 { "tabshow", 0, tabaction
, XT_TAB_SHOW
, 0 },
3224 { "tabs", 0, buffers
, 0, 0 },
3225 { "tabundoclose", 0, tabaction
, XT_TAB_UNDO_CLOSE
, 0 },
3226 { "buffers", 0, buffers
, 0, 0 },
3227 { "ls", 0, buffers
, 0, 0 },
3228 { "encoding", 0, set_encoding
, 0, XT_USERARG
},
3230 /* command aliases (handy when -S flag is used) */
3231 { "promptopen", 0, command
, XT_CMD_OPEN
, 0 },
3232 { "promptopencurrent", 0, command
, XT_CMD_OPEN_CURRENT
, 0 },
3233 { "prompttabnew", 0, command
, XT_CMD_TABNEW
, 0 },
3234 { "prompttabnewcurrent",0, command
, XT_CMD_TABNEW_CURRENT
, 0 },
3237 { "set", 0, set
, 0, XT_SETARG
},
3239 { "fullscreen", 0, fullscreen
, 0, 0 },
3240 { "f", 0, fullscreen
, 0, 0 },
3243 { "session", 0, session_cmd
, XT_SHOW
, 0 },
3244 { "delete", 1, session_cmd
, XT_DELETE
, XT_SESSARG
},
3245 { "open", 1, session_cmd
, XT_OPEN
, XT_SESSARG
},
3246 { "save", 1, session_cmd
, XT_SAVE
, XT_USERARG
},
3247 { "show", 1, session_cmd
, XT_SHOW
, 0 },
3249 /* external javascript */
3250 { "script", 0, script_cmd
, XT_EJS_SHOW
, XT_USERARG
},
3253 { "inspector", 0, inspector_cmd
, XT_INS_SHOW
, 0 },
3254 { "show", 1, inspector_cmd
, XT_INS_SHOW
, 0 },
3255 { "hide", 1, inspector_cmd
, XT_INS_HIDE
, 0 },
3258 { "proxy", 0, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3259 { "show", 1, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3260 { "toggle", 1, proxy_cmd
, XT_PRXY_TOGGLE
, 0 },
3267 } cmd_status
= {-1, 0};
3270 wv_release_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3273 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 1)
3280 wv_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3283 WebKitHitTestResult
*hit_test_result
;
3286 hit_test_result
= webkit_web_view_get_hit_test_result(t
->wv
, e
);
3287 g_object_get(hit_test_result
, "context", &context
, NULL
);
3292 if (context
& WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE
)
3293 t
->mode
= XT_MODE_INSERT
;
3295 t
->mode
= XT_MODE_COMMAND
;
3297 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3299 else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 8 /* btn 4 */) {
3305 } else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 9 /* btn 5 */) {
3307 a
.i
= XT_NAV_FORWARD
;
3317 tab_close_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3319 DNPRINTF(XT_D_TAB
, "tab_close_cb: tab %d\n", t
->tab_id
);
3321 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3330 activate_uri_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3332 const gchar
*uri
= gtk_entry_get_text(GTK_ENTRY(entry
));
3334 DNPRINTF(XT_D_URL
, "activate_uri_entry_cb: %s\n", uri
);
3337 show_oops(NULL
, "activate_uri_entry_cb invalid parameters");
3342 show_oops(t
, "activate_uri_entry_cb no uri");
3346 uri
+= strspn(uri
, "\t ");
3348 /* if xxxt:// treat specially */
3349 if (parse_xtp_url(t
, uri
))
3352 /* otherwise continue to load page normally */
3353 load_uri(t
, (gchar
*)uri
);
3358 activate_search_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3360 const gchar
*search
= gtk_entry_get_text(GTK_ENTRY(entry
));
3361 char *newuri
= NULL
;
3364 DNPRINTF(XT_D_URL
, "activate_search_entry_cb: %s\n", search
);
3367 show_oops(NULL
, "activate_search_entry_cb invalid parameters");
3371 if (search_string
== NULL
) {
3372 show_oops(t
, "no search_string");
3376 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
3378 enc_search
= soup_uri_encode(search
, XT_RESERVED_CHARS
);
3379 newuri
= g_strdup_printf(search_string
, enc_search
);
3383 webkit_web_view_load_uri(t
->wv
, newuri
);
3391 check_and_set_cookie(const gchar
*uri
, struct tab
*t
)
3393 struct domain
*d
= NULL
;
3396 if (uri
== NULL
|| t
== NULL
)
3399 if ((d
= wl_find_uri(uri
, &c_wl
)) == NULL
)
3404 DNPRINTF(XT_D_COOKIE
, "check_and_set_cookie: %s %s\n",
3405 es
? "enable" : "disable", uri
);
3407 g_object_set(G_OBJECT(t
->settings
),
3408 "enable-html5-local-storage", es
, (char *)NULL
);
3409 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3413 check_and_set_js(const gchar
*uri
, struct tab
*t
)
3415 struct domain
*d
= NULL
;
3418 if (uri
== NULL
|| t
== NULL
)
3421 if ((d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3426 DNPRINTF(XT_D_JS
, "check_and_set_js: %s %s\n",
3427 es
? "enable" : "disable", uri
);
3429 g_object_set(G_OBJECT(t
->settings
),
3430 "enable-scripts", es
, (char *)NULL
);
3431 g_object_set(G_OBJECT(t
->settings
),
3432 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
3433 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3435 button_set_stockid(t
->js_toggle
,
3436 es
? GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
);
3440 check_and_set_pl(const gchar
*uri
, struct tab
*t
)
3442 struct domain
*d
= NULL
;
3445 if (uri
== NULL
|| t
== NULL
)
3448 if ((d
= wl_find_uri(uri
, &pl_wl
)) == NULL
)
3453 DNPRINTF(XT_D_JS
, "check_and_set_pl: %s %s\n",
3454 es
? "enable" : "disable", uri
);
3456 g_object_set(G_OBJECT(t
->settings
),
3457 "enable-plugins", es
, (char *)NULL
);
3458 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3462 color_address_bar(gpointer p
)
3465 struct tab
*tt
, *t
= p
;
3466 gchar
*col_str
= XT_COLOR_WHITE
;
3467 const gchar
*uri
, *u
= NULL
, *error_str
= NULL
;
3470 gdk_threads_enter();
3472 DNPRINTF(XT_D_URL
, "%s:\n", __func__
);
3474 /* make sure t still exists */
3477 TAILQ_FOREACH(tt
, &tabs
, entry
)
3483 if ((uri
= get_uri(t
)) == NULL
)
3488 gdk_threads_leave();
3491 col_str
= XT_COLOR_YELLOW
;
3492 switch (load_compare_cert(u
, &error_str
)) {
3494 col_str
= XT_COLOR_BLUE
;
3497 col_str
= XT_COLOR_GREEN
;
3499 case CERT_UNTRUSTED
:
3500 col_str
= XT_COLOR_YELLOW
;
3503 col_str
= XT_COLOR_RED
;
3508 gdk_threads_enter();
3510 /* make sure t isn't deleted */
3511 TAILQ_FOREACH(tt
, &tabs
, entry
)
3518 /* test to see if the user navigated away and canceled the thread */
3519 if (t
->thread
!= g_thread_self())
3521 if ((uri
= get_uri(t
)) == NULL
) {
3525 if (strcmp(uri
, u
)) {
3526 /* make sure we are still the same url */
3532 gdk_color_parse(col_str
, &color
);
3533 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3535 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3536 statusbar_modify_attr(t
, col_str
, XT_COLOR_BLACK
);
3538 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3540 if (error_str
&& error_str
[0] != '\0')
3541 show_oops(t
, "%s", error_str
);
3546 /* t is invalid at this point */
3548 g_free((gpointer
)u
);
3550 gdk_threads_leave();
3555 show_ca_status(struct tab
*t
, const char *uri
)
3558 gchar
*col_str
= XT_COLOR_WHITE
;
3560 DNPRINTF(XT_D_URL
, "show_ca_status: %d %s %s\n",
3561 ssl_strict_certs
, ssl_ca_file
, uri
);
3568 if (ssl_ca_file
== NULL
) {
3569 if (g_str_has_prefix(uri
, "http://"))
3571 if (g_str_has_prefix(uri
, "https://")) {
3572 col_str
= XT_COLOR_RED
;
3577 if (g_str_has_prefix(uri
, "http://") ||
3578 !g_str_has_prefix(uri
, "https://"))
3582 * It is not necessary to see if the thread is already running.
3583 * If the thread is in progress setting it to something else aborts it
3587 /* thread the coloring of the address bar */
3588 t
->thread
= g_thread_create((GThreadFunc
)color_address_bar
, t
, TRUE
, NULL
);
3590 color_address_bar(t
);
3596 gdk_color_parse(col_str
, &color
);
3597 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3599 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3600 statusbar_modify_attr(t
, col_str
, XT_COLOR_BLACK
);
3602 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3607 free_favicon(struct tab
*t
)
3609 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p req %p\n",
3610 __func__
, t
->icon_download
, t
->icon_request
);
3612 if (t
->icon_request
)
3613 g_object_unref(t
->icon_request
);
3614 if (t
->icon_dest_uri
)
3615 g_free(t
->icon_dest_uri
);
3617 t
->icon_request
= NULL
;
3618 t
->icon_dest_uri
= NULL
;
3622 xt_icon_from_name(struct tab
*t
, gchar
*name
)
3624 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->uri_entry
),
3625 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3627 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3628 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3630 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3631 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3635 xt_icon_from_pixbuf(struct tab
*t
, GdkPixbuf
*pb
)
3637 GdkPixbuf
*pb_scaled
;
3639 if (gdk_pixbuf_get_width(pb
) > 16 || gdk_pixbuf_get_height(pb
) > 16)
3640 pb_scaled
= gdk_pixbuf_scale_simple(pb
, 16, 16,
3641 GDK_INTERP_BILINEAR
);
3645 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
3646 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3648 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
3649 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3651 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3652 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3654 if (pb_scaled
!= pb
)
3655 g_object_unref(pb_scaled
);
3659 xt_icon_from_file(struct tab
*t
, char *file
)
3663 if (g_str_has_prefix(file
, "file://"))
3664 file
+= strlen("file://");
3666 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
3668 xt_icon_from_pixbuf(t
, pb
);
3671 xt_icon_from_name(t
, "text-html");
3675 is_valid_icon(char *file
)
3678 const char *mime_type
;
3682 gf
= g_file_new_for_path(file
);
3683 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
3685 mime_type
= g_file_info_get_content_type(fi
);
3686 valid
= g_strcmp0(mime_type
, "image/x-ico") == 0 ||
3687 g_strcmp0(mime_type
, "image/vnd.microsoft.icon") == 0 ||
3688 g_strcmp0(mime_type
, "image/png") == 0 ||
3689 g_strcmp0(mime_type
, "image/gif") == 0 ||
3690 g_strcmp0(mime_type
, "application/octet-stream") == 0;
3698 set_favicon_from_file(struct tab
*t
, char *file
)
3702 if (t
== NULL
|| file
== NULL
)
3705 if (g_str_has_prefix(file
, "file://"))
3706 file
+= strlen("file://");
3707 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading %s\n", __func__
, file
);
3709 if (!stat(file
, &sb
)) {
3710 if (sb
.st_size
== 0 || !is_valid_icon(file
)) {
3711 /* corrupt icon so trash it */
3712 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3715 /* no need to set icon to default here */
3719 xt_icon_from_file(t
, file
);
3723 favicon_download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
3726 WebKitDownloadStatus status
= webkit_download_get_status(download
);
3727 struct tab
*tt
= NULL
, *t
= NULL
;
3730 * find the webview instead of passing in the tab as it could have been
3731 * deleted from underneath us.
3733 TAILQ_FOREACH(tt
, &tabs
, entry
) {
3742 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d status %d\n",
3743 __func__
, t
->tab_id
, status
);
3746 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
3748 t
->icon_download
= NULL
;
3751 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
3754 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
3757 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
3759 DNPRINTF(XT_D_DOWNLOAD
, "%s: freeing favicon %d\n",
3760 __func__
, t
->tab_id
);
3761 t
->icon_download
= NULL
;
3764 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
3767 DNPRINTF(XT_D_DOWNLOAD
, "%s: setting icon to %s\n",
3768 __func__
, t
->icon_dest_uri
);
3769 set_favicon_from_file(t
, t
->icon_dest_uri
);
3770 /* these will be freed post callback */
3771 t
->icon_request
= NULL
;
3772 t
->icon_download
= NULL
;
3780 abort_favicon_download(struct tab
*t
)
3782 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p\n", __func__
, t
->icon_download
);
3784 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
3785 if (t
->icon_download
) {
3786 g_signal_handlers_disconnect_by_func(G_OBJECT(t
->icon_download
),
3787 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3788 webkit_download_cancel(t
->icon_download
);
3789 t
->icon_download
= NULL
;
3794 xt_icon_from_name(t
, "text-html");
3798 notify_icon_loaded_cb(WebKitWebView
*wv
, gchar
*uri
, struct tab
*t
)
3800 DNPRINTF(XT_D_DOWNLOAD
, "%s %s\n", __func__
, uri
);
3802 if (uri
== NULL
|| t
== NULL
)
3805 #if WEBKIT_CHECK_VERSION(1, 4, 0)
3806 /* take icon from WebKitIconDatabase */
3809 pb
= webkit_web_view_get_icon_pixbuf(wv
);
3811 xt_icon_from_pixbuf(t
, pb
);
3814 xt_icon_from_name(t
, "text-html");
3815 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
3816 /* download icon to cache dir */
3817 gchar
*name_hash
, file
[PATH_MAX
];
3820 if (t
->icon_request
) {
3821 DNPRINTF(XT_D_DOWNLOAD
, "%s: download in progress\n", __func__
);
3825 /* check to see if we got the icon in cache */
3826 name_hash
= g_compute_checksum_for_string(G_CHECKSUM_SHA256
, uri
, -1);
3827 snprintf(file
, sizeof file
, "%s/%s.ico", cache_dir
, name_hash
);
3830 if (!stat(file
, &sb
)) {
3831 if (sb
.st_size
> 0) {
3832 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading from cache %s\n",
3834 set_favicon_from_file(t
, file
);
3838 /* corrupt icon so trash it */
3839 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3844 /* create download for icon */
3845 t
->icon_request
= webkit_network_request_new(uri
);
3846 if (t
->icon_request
== NULL
) {
3847 DNPRINTF(XT_D_DOWNLOAD
, "%s: invalid uri %s\n",
3852 t
->icon_download
= webkit_download_new(t
->icon_request
);
3853 if (t
->icon_download
== NULL
)
3856 /* we have to free icon_dest_uri later */
3857 t
->icon_dest_uri
= g_strdup_printf("file://%s", file
);
3858 webkit_download_set_destination_uri(t
->icon_download
,
3861 if (webkit_download_get_status(t
->icon_download
) ==
3862 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
3863 g_object_unref(t
->icon_request
);
3864 g_free(t
->icon_dest_uri
);
3865 t
->icon_request
= NULL
;
3866 t
->icon_dest_uri
= NULL
;
3870 g_signal_connect(G_OBJECT(t
->icon_download
), "notify::status",
3871 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3873 webkit_download_start(t
->icon_download
);
3878 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3880 const gchar
*uri
= NULL
;
3881 struct history
*h
, find
;
3885 DNPRINTF(XT_D_URL
, "notify_load_status_cb: %d %s\n",
3886 webkit_web_view_get_load_status(wview
),
3887 get_uri(t
) ? get_uri(t
) : "NOTHING");
3890 show_oops(NULL
, "notify_load_status_cb invalid parameters");
3894 switch (webkit_web_view_get_load_status(wview
)) {
3895 case WEBKIT_LOAD_PROVISIONAL
:
3897 abort_favicon_download(t
);
3898 #if GTK_CHECK_VERSION(2, 20, 0)
3899 gtk_widget_show(t
->spinner
);
3900 gtk_spinner_start(GTK_SPINNER(t
->spinner
));
3902 gtk_label_set_text(GTK_LABEL(t
->label
), "Loading");
3904 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), TRUE
);
3906 /* assume we are a new address */
3907 gdk_color_parse("white", &color
);
3908 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3909 statusbar_modify_attr(t
, "white", XT_COLOR_BLACK
);
3911 /* take focus if we are visible */
3917 /* kill color thread */
3922 case WEBKIT_LOAD_COMMITTED
:
3927 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
3933 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
3935 /* check if js white listing is enabled */
3936 if (enable_plugin_whitelist
)
3937 check_and_set_pl(uri
, t
);
3938 if (enable_cookie_whitelist
)
3939 check_and_set_cookie(uri
, t
);
3940 if (enable_js_whitelist
)
3941 check_and_set_js(uri
, t
);
3947 /* we know enough to autosave the session */
3948 if (session_autosave
) {
3953 show_ca_status(t
, uri
);
3954 run_script(t
, JS_HINTING
);
3957 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
3959 if (color_visited_uris
) {
3960 color_visited(t
, color_visited_helper());
3962 /* This colors the links you middle-click (open in new
3963 * tab) in the current tab. */
3964 if (t
->tab_id
!= gtk_notebook_get_current_page(notebook
) &&
3965 (uri
= get_uri(t
)) != NULL
)
3966 color_visited(get_current_tab(),
3967 g_strdup_printf("{'%s' : 'dummy'}", uri
));
3971 case WEBKIT_LOAD_FINISHED
:
3973 if ((uri
= get_uri(t
)) == NULL
)
3976 if (!strncmp(uri
, "http://", strlen("http://")) ||
3977 !strncmp(uri
, "https://", strlen("https://")) ||
3978 !strncmp(uri
, "file://", strlen("file://"))) {
3979 find
.uri
= (gchar
*)uri
;
3980 h
= RB_FIND(history_list
, &hl
, &find
);
3982 insert_history_item(uri
,
3983 get_title(t
, FALSE
), time(NULL
));
3985 h
->time
= time(NULL
);
3988 set_status(t
, (char *)uri
, XT_STATUS_URI
);
3989 #if WEBKIT_CHECK_VERSION(1, 1, 18)
3990 case WEBKIT_LOAD_FAILED
:
3993 #if GTK_CHECK_VERSION(2, 20, 0)
3994 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
3995 gtk_widget_hide(t
->spinner
);
3998 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
4003 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
), TRUE
);
4005 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
),
4006 can_go_back_for_real(t
));
4008 gtk_widget_set_sensitive(GTK_WIDGET(t
->forward
),
4009 can_go_forward_for_real(t
));
4013 notify_title_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
4015 const gchar
*title
= NULL
, *win_title
= NULL
;
4017 title
= get_title(t
, FALSE
);
4018 win_title
= get_title(t
, TRUE
);
4020 gtk_label_set_text(GTK_LABEL(t
->label
), title
);
4021 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
), title
);
4024 if (win_title
&& t
->tab_id
== gtk_notebook_get_current_page(notebook
))
4025 gtk_window_set_title(GTK_WINDOW(main_window
), win_title
);
4029 get_domain(const gchar
*host
)
4034 /* handle silly domains like .co.uk */
4036 if ((x
= strlen(host
)) <= 6)
4037 return (g_strdup(host
));
4039 if (host
[x
- 3] == '.' && host
[x
- 6] == '.') {
4045 return (g_strdup(&host
[x
+ 1]));
4049 p
= g_strrstr(host
, ".");
4051 return (g_strdup(""));
4057 return (g_strdup(p
+ 1));
4059 return (g_strdup(host
));
4063 js_autorun(struct tab
*t
)
4067 size_t got_default
= 0, got_host
= 0;
4069 char deff
[PATH_MAX
], hostf
[PATH_MAX
];
4070 char *js
= NULL
, *jsat
, *domain
= NULL
;
4071 FILE *deffile
= NULL
, *hostfile
= NULL
;
4072 JSGlobalContextRef ctx
;
4073 WebKitWebFrame
*frame
;
4075 JSValueRef val
, exception
;
4078 if (js_autorun_enabled
== 0)
4083 !(g_str_has_prefix(uri
, "http://") ||
4084 g_str_has_prefix(uri
, "https://")))
4087 su
= soup_uri_new(uri
);
4090 if (!SOUP_URI_VALID_FOR_HTTP(su
))
4093 DNPRINTF(XT_D_JS
, "%s: host: %s domain: %s\n", __func__
,
4095 domain
= get_domain(su
->host
);
4097 snprintf(deff
, sizeof deff
, "%s/default.js", js_dir
);
4098 if ((deffile
= fopen(deff
, "r")) != NULL
) {
4099 if (fstat(fileno(deffile
), &sb
) == -1) {
4100 show_oops(t
, "can't stat default JS file");
4103 got_default
= sb
.st_size
;
4106 /* try host first followed by domain */
4107 snprintf(hostf
, sizeof hostf
, "%s/%s.js", js_dir
, su
->host
);
4108 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4109 if ((hostfile
= fopen(hostf
, "r")) == NULL
) {
4110 snprintf(hostf
, sizeof hostf
, "%s/%s.js", js_dir
, domain
);
4111 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4112 if ((hostfile
= fopen(hostf
, "r")) == NULL
)
4115 DNPRINTF(XT_D_JS
, "file: %s\n", hostf
);
4116 if (fstat(fileno(hostfile
), &sb
) == -1) {
4117 show_oops(t
, "can't stat %s JS file", hostf
);
4120 got_host
= sb
.st_size
;
4123 if (got_default
+ got_host
== 0)
4126 js
= g_malloc0(got_default
+ got_host
+ 1);
4130 if (fread(js
, got_default
, 1, deffile
) != 1) {
4131 show_oops(t
, "default file read error");
4134 jsat
= js
+ got_default
;
4138 if (fread(jsat
, got_host
, 1, hostfile
) != 1) {
4139 show_oops(t
, "host file read error");
4144 /* this code needs to be redone */
4145 frame
= webkit_web_view_get_main_frame(t
->wv
);
4146 ctx
= webkit_web_frame_get_global_context(frame
);
4148 str
= JSStringCreateWithUTF8CString(js
);
4149 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
4150 NULL
, 0, &exception
);
4151 JSStringRelease(str
);
4153 DNPRINTF(XT_D_JS
, "%s: val %p\n", __func__
, val
);
4155 es
= js_ref_to_string(ctx
, exception
);
4157 show_oops(t
, "script exception: %s", es
);
4162 es
= js_ref_to_string(ctx
, val
);
4166 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
4168 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
4171 show_oops(t
, "script complete return value: '%s'", es
);
4174 show_oops(t
, "script complete: without a return value");
4191 webview_load_finished_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4193 /* autorun some js if enabled */
4201 webview_progress_changed_cb(WebKitWebView
*wv
, int progress
, struct tab
*t
)
4203 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->uri_entry
),
4204 progress
== 100 ? 0 : (double)progress
/ 100);
4205 if (show_url
== 0) {
4206 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
4207 progress
== 100 ? 0 : (double)progress
/ 100);
4210 update_statusbar_position(NULL
, NULL
);
4214 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
4215 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
4216 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
4219 WebKitWebNavigationReason reason
;
4220 struct domain
*d
= NULL
;
4223 show_oops(NULL
, "webview_npd_cb invalid parameters");
4227 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
4229 webkit_network_request_get_uri(request
));
4231 uri
= (char *)webkit_network_request_get_uri(request
);
4233 /* if this is an xtp url, we don't load anything else */
4234 if (parse_xtp_url(t
, uri
))
4237 if ((t
->mode
== XT_MODE_HINT
&& t
->new_tab
) || t
->ctrl_click
) {
4239 create_new_tab(uri
, NULL
, ctrl_click_focus
, -1);
4240 webkit_web_policy_decision_ignore(pd
);
4241 return (TRUE
); /* we made the decission */
4245 * This is a little hairy but it comes down to this:
4246 * when we run in whitelist mode we have to assist the browser in
4247 * opening the URL that it would have opened in a new tab.
4249 reason
= webkit_web_navigation_action_get_reason(na
);
4250 if (reason
== WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED
) {
4251 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
4252 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1)
4253 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4255 webkit_web_policy_decision_use(pd
);
4256 return (TRUE
); /* we made the decision */
4263 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4266 struct domain
*d
= NULL
;
4268 WebKitWebView
*webview
= NULL
;
4271 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
4272 webkit_web_view_get_uri(wv
));
4275 /* open in current tab */
4277 } else if (enable_scripts
== 0 && enable_js_whitelist
== 1) {
4278 uri
= webkit_web_view_get_uri(wv
);
4279 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4282 if (t
->ctrl_click
) {
4283 x
= ctrl_click_focus
;
4286 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4288 } else if (enable_scripts
== 1) {
4289 if (t
->ctrl_click
) {
4290 x
= ctrl_click_focus
;
4293 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4301 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
4304 struct domain
*d
= NULL
;
4306 DNPRINTF(XT_D_NAV
, "webview_close_cb: %d\n", t
->tab_id
);
4308 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
4309 uri
= webkit_web_view_get_uri(wv
);
4310 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4314 } else if (enable_scripts
== 1)
4321 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
4323 /* we can not eat the event without throwing gtk off so defer it */
4325 /* catch middle click */
4326 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 2) {
4331 /* catch ctrl click */
4332 if (e
->type
== GDK_BUTTON_RELEASE
&&
4333 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
4338 return (XT_CB_PASSTHROUGH
);
4342 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
4344 struct mime_type
*m
;
4346 m
= find_mime_type(mime_type
);
4354 show_oops(t
, "can't fork mime handler");
4364 execlp(m
->mt_action
, m
->mt_action
,
4365 webkit_network_request_get_uri(request
), (void *)NULL
);
4374 get_mime_type(const char *file
)
4377 char *mime_type
= NULL
;
4381 if (g_str_has_prefix(file
, "file://"))
4382 file
+= strlen("file://");
4384 gf
= g_file_new_for_path(file
);
4385 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
4387 if ((m
= g_file_info_get_content_type(fi
)) != NULL
)
4388 mime_type
= g_strdup(m
);
4396 run_download_mimehandler(char *mime_type
, char *file
)
4398 struct mime_type
*m
;
4400 m
= find_mime_type(mime_type
);
4406 show_oops(NULL
, "can't fork download mime handler");
4416 if (g_str_has_prefix(file
, "file://"))
4417 file
+= strlen("file://");
4418 execlp(m
->mt_action
, m
->mt_action
, file
, (void *)NULL
);
4427 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
4430 WebKitDownloadStatus status
;
4431 const char *file
= NULL
;
4434 if (download
== NULL
)
4436 status
= webkit_download_get_status(download
);
4437 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
4440 file
= webkit_download_get_destination_uri(download
);
4443 mime
= get_mime_type(file
);
4447 run_download_mimehandler((char *)mime
, (char *)file
);
4452 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
4453 WebKitNetworkRequest
*request
, char *mime_type
,
4454 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
4457 show_oops(NULL
, "webview_mimetype_cb invalid parameters");
4461 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
4462 t
->tab_id
, mime_type
);
4464 if (run_mimehandler(t
, mime_type
, request
) == 0) {
4465 webkit_web_policy_decision_ignore(decision
);
4470 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
4471 webkit_web_policy_decision_download(decision
);
4479 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
4483 const gchar
*suggested_name
;
4484 gchar
*filename
= NULL
;
4486 struct download
*download_entry
;
4489 if (wk_download
== NULL
|| t
== NULL
) {
4490 show_oops(NULL
, "%s invalid parameters", __func__
);
4494 suggested_name
= webkit_download_get_suggested_filename(wk_download
);
4495 if (suggested_name
== NULL
)
4496 return (FALSE
); /* abort download */
4507 filename
= g_strdup_printf("%d%s", i
, suggested_name
);
4509 uri
= g_strdup_printf("file://%s/%s", download_dir
, i
?
4510 filename
: suggested_name
);
4512 } while (!stat(uri
+ strlen("file://"), &sb
));
4514 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d filename %s "
4515 "local %s\n", __func__
, t
->tab_id
, filename
, uri
);
4517 webkit_download_set_destination_uri(wk_download
, uri
);
4519 if (webkit_download_get_status(wk_download
) ==
4520 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
4521 show_oops(t
, "%s: download failed to start", __func__
);
4523 gtk_label_set_text(GTK_LABEL(t
->label
), "Download Failed");
4525 /* connect "download first" mime handler */
4526 g_signal_connect(G_OBJECT(wk_download
), "notify::status",
4527 G_CALLBACK(download_status_changed_cb
), NULL
);
4529 download_entry
= g_malloc(sizeof(struct download
));
4530 download_entry
->download
= wk_download
;
4531 download_entry
->tab
= t
;
4532 download_entry
->id
= next_download_id
++;
4533 RB_INSERT(download_list
, &downloads
, download_entry
);
4534 /* get from history */
4535 g_object_ref(wk_download
);
4536 gtk_label_set_text(GTK_LABEL(t
->label
), "Downloading");
4537 show_oops(t
, "Download of '%s' started...",
4538 basename((char *)webkit_download_get_destination_uri(wk_download
)));
4547 /* sync other download manager tabs */
4548 update_download_tabs(NULL
);
4551 * NOTE: never redirect/render the current tab before this
4552 * function returns. This will cause the download to never start.
4554 return (ret
); /* start download */
4558 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
4560 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
4563 show_oops(NULL
, "webview_hover_cb");
4568 set_status(t
, uri
, XT_STATUS_LINK
);
4571 set_status(t
, t
->status
, XT_STATUS_NOTHING
);
4576 mark(struct tab
*t
, struct karg
*arg
)
4582 if ((index
= marktoindex(mark
)) == -1)
4585 if (arg
->i
== XT_MARK_SET
)
4586 t
->mark
[index
] = gtk_adjustment_get_value(t
->adjust_v
);
4587 else if (arg
->i
== XT_MARK_GOTO
) {
4588 if (t
->mark
[index
] == XT_INVALID_MARK
) {
4589 show_oops(t
, "mark '%c' does not exist", mark
);
4592 /* XXX t->mark[index] can be bigger than the maximum if ajax or
4593 something changes the document size */
4594 gtk_adjustment_set_value(t
->adjust_v
, t
->mark
[index
]);
4601 marks_clear(struct tab
*t
)
4605 for (i
= 0; i
< LENGTH(t
->mark
); i
++)
4606 t
->mark
[i
] = XT_INVALID_MARK
;
4612 char file
[PATH_MAX
];
4613 char *line
= NULL
, *p
;
4618 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
4619 if ((f
= fopen(file
, "r+")) == NULL
) {
4620 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
4624 for (i
= 1; ; i
++) {
4625 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
4627 if (strlen(line
) == 0 || line
[0] == '#') {
4633 p
= strtok(line
, " \t");
4635 if (p
== NULL
|| strlen(p
) != 1 ||
4636 (index
= marktoindex(*p
)) == -1) {
4637 warnx("corrupt quickmarks file, line %d", i
);
4641 p
= strtok(NULL
, " \t");
4642 if (qmarks
[index
] != NULL
)
4643 g_free(qmarks
[index
]);
4644 qmarks
[index
] = g_strdup(p
);
4655 char file
[PATH_MAX
];
4659 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
4660 if ((f
= fopen(file
, "r+")) == NULL
) {
4661 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
4665 for (i
= 0; i
< XT_NOMARKS
; i
++)
4666 if (qmarks
[i
] != NULL
)
4667 fprintf(f
, "%c %s\n", indextomark(i
), qmarks
[i
]);
4675 qmark(struct tab
*t
, struct karg
*arg
)
4680 mark
= arg
->s
[strlen(arg
->s
)-1];
4681 index
= marktoindex(mark
);
4687 if (qmarks
[index
] != NULL
)
4688 g_free(qmarks
[index
]);
4690 qmarks_load(); /* sync if multiple instances */
4691 qmarks
[index
] = g_strdup(get_uri(t
));
4695 if (qmarks
[index
] != NULL
)
4696 load_uri(t
, qmarks
[index
]);
4698 show_oops(t
, "quickmark \"%c\" does not exist",
4704 if (qmarks
[index
] != NULL
)
4705 create_new_tab(qmarks
[index
], NULL
, 1, -1);
4707 show_oops(t
, "quickmark \"%c\" does not exist",
4718 go_up(struct tab
*t
, struct karg
*args
)
4724 levels
= atoi(args
->s
);
4728 uri
= g_strdup(webkit_web_view_get_uri(t
->wv
));
4729 if ((tmp
= strstr(uri
, XT_PROTO_DELIM
)) == NULL
)
4731 tmp
+= strlen(XT_PROTO_DELIM
);
4733 /* if an uri starts with a slash, leave it alone (for file:///) */
4740 p
= strrchr(tmp
, '/');
4754 gototab(struct tab
*t
, struct karg
*args
)
4757 struct karg arg
= {0, NULL
, -1};
4759 tab
= atoi(args
->s
);
4761 arg
.i
= XT_TAB_NEXT
;
4770 zoom_amount(struct tab
*t
, struct karg
*arg
)
4772 struct karg narg
= {0, NULL
, -1};
4774 narg
.i
= atoi(arg
->s
);
4775 resizetab(t
, &narg
);
4781 flip_colon(struct tab
*t
, struct karg
*arg
)
4783 struct karg narg
= {0, NULL
, -1};
4786 if (t
== NULL
|| arg
== NULL
)
4789 p
= strstr(arg
->s
, ":");
4801 /* buffer commands receive the regex that triggered them in arg.s */
4802 char bcmd
[XT_BUFCMD_SZ
];
4806 #define XT_PRE_NO (0)
4807 #define XT_PRE_YES (1)
4808 #define XT_PRE_MAYBE (2)
4810 int (*func
)(struct tab
*, struct karg
*);
4814 { "^[0-9]*gu$", XT_PRE_MAYBE
, "gu", go_up
, 0 },
4815 { "^gg$", XT_PRE_NO
, "gg", move
, XT_MOVE_TOP
},
4816 { "^gG$", XT_PRE_NO
, "gG", move
, XT_MOVE_BOTTOM
},
4817 { "^[0-9]+%$", XT_PRE_YES
, "%", move
, XT_MOVE_PERCENT
},
4818 { "^zz$", XT_PRE_NO
, "zz", move
, XT_MOVE_CENTER
},
4819 { "^gh$", XT_PRE_NO
, "gh", go_home
, 0 },
4820 { "^m[a-zA-Z0-9]$", XT_PRE_NO
, "m", mark
, XT_MARK_SET
},
4821 { "^['][a-zA-Z0-9]$", XT_PRE_NO
, "'", mark
, XT_MARK_GOTO
},
4822 { "^[0-9]+t$", XT_PRE_YES
, "t", gototab
, 0 },
4823 { "^M[a-zA-Z0-9]$", XT_PRE_NO
, "M", qmark
, XT_QMARK_SET
},
4824 { "^go[a-zA-Z0-9]$", XT_PRE_NO
, "go", qmark
, XT_QMARK_OPEN
},
4825 { "^gn[a-zA-Z0-9]$", XT_PRE_NO
, "gn", qmark
, XT_QMARK_TAB
},
4826 { "^ZR$", XT_PRE_NO
, "ZR", restart
, 0 },
4827 { "^ZZ$", XT_PRE_NO
, "ZZ", quit
, 0 },
4828 { "^zi$", XT_PRE_NO
, "zi", resizetab
, XT_ZOOM_IN
},
4829 { "^zo$", XT_PRE_NO
, "zo", resizetab
, XT_ZOOM_OUT
},
4830 { "^z0$", XT_PRE_NO
, "z0", resizetab
, XT_ZOOM_NORMAL
},
4831 { "^[0-9]+Z$", XT_PRE_YES
, "Z", zoom_amount
, 0 },
4832 { "^[0-9]+:$", XT_PRE_YES
, ":", flip_colon
, 0 },
4836 buffercmd_init(void)
4840 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
4841 if (regcomp(&buffercmds
[i
].cregex
, buffercmds
[i
].regex
,
4842 REG_EXTENDED
| REG_NOSUB
))
4843 startpage_add("invalid buffercmd regex %s",
4844 buffercmds
[i
].regex
);
4848 buffercmd_abort(struct tab
*t
)
4852 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_abort: clearing buffer\n");
4853 for (i
= 0; i
< LENGTH(bcmd
); i
++)
4856 cmd_prefix
= 0; /* clear prefix for non-buffer commands */
4857 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
4861 buffercmd_execute(struct tab
*t
, struct buffercmd
*cmd
)
4863 struct karg arg
= {0, NULL
, -1};
4866 arg
.s
= g_strdup(bcmd
);
4868 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_execute: buffer \"%s\" "
4869 "matches regex \"%s\", executing\n", bcmd
, cmd
->regex
);
4879 buffercmd_addkey(struct tab
*t
, guint keyval
)
4882 char s
[XT_BUFCMD_SZ
];
4884 if (keyval
== GDK_Escape
) {
4886 return (XT_CB_HANDLED
);
4889 /* key with modifier or non-ascii character */
4890 if (!isascii(keyval
)) {
4892 * XXX this looks wrong but fixes some sites like
4893 * http://www.seslisozluk.com/
4894 * that eat a shift or ctrl and end putting default focus in js
4895 * instead of ignoring the keystroke
4896 * so instead of return (XT_CB_PASSTHROUGH); eat the key
4898 return (XT_CB_HANDLED
);
4901 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: adding key \"%c\" "
4902 "to buffer \"%s\"\n", keyval
, bcmd
);
4904 for (i
= 0; i
< LENGTH(bcmd
); i
++)
4905 if (bcmd
[i
] == '\0') {
4910 /* buffer full, ignore input */
4911 if (i
>= LENGTH(bcmd
) -1) {
4912 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: buffer full\n");
4914 return (XT_CB_HANDLED
);
4917 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
4919 /* find exact match */
4920 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
4921 if (regexec(&buffercmds
[i
].cregex
, bcmd
,
4922 (size_t) 0, NULL
, 0) == 0) {
4923 buffercmd_execute(t
, &buffercmds
[i
]);
4927 /* find non exact matches to see if we need to abort ot not */
4928 for (i
= 0, match
= 0; i
< LENGTH(buffercmds
); i
++) {
4929 DNPRINTF(XT_D_BUFFERCMD
, "trying: %s\n", bcmd
);
4932 if (buffercmds
[i
].precount
== XT_PRE_MAYBE
) {
4933 if (isdigit(bcmd
[0])) {
4934 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
4938 if (sscanf(bcmd
, "%s", s
) == 0)
4941 } else if (buffercmds
[i
].precount
== XT_PRE_YES
) {
4942 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
4945 if (sscanf(bcmd
, "%s", s
) == 0)
4948 if (c
== -1 && buffercmds
[i
].precount
)
4950 if (!strncmp(s
, buffercmds
[i
].cmd
, strlen(s
)))
4953 DNPRINTF(XT_D_BUFFERCMD
, "got[%d] %d <%s>: %d %s\n",
4954 i
, match
, buffercmds
[i
].cmd
, c
, s
);
4957 DNPRINTF(XT_D_BUFFERCMD
, "aborting: %s\n", bcmd
);
4962 return (XT_CB_HANDLED
);
4966 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
4968 struct key_binding
*k
;
4970 /* handle keybindings if buffercmd is empty.
4971 if not empty, allow commands like C-n */
4972 if (bcmd
[0] == '\0' || ((e
->state
& (CTRL
| MOD1
)) != 0))
4973 TAILQ_FOREACH(k
, &kbl
, entry
)
4974 if (e
->keyval
== k
->key
4975 && (entry
? k
->use_in_entry
: 1)) {
4976 /* when we are edditing eat ctrl/mod keys */
4977 if (edit_mode
== XT_EM_VI
&&
4978 t
->mode
== XT_MODE_INSERT
&&
4979 (e
->state
& CTRL
|| e
->state
& MOD1
))
4980 return (XT_CB_PASSTHROUGH
);
4983 if ((e
->state
& (CTRL
| MOD1
)) == 0)
4984 return (cmd_execute(t
, k
->cmd
));
4985 } else if ((e
->state
& k
->mask
) == k
->mask
) {
4986 return (cmd_execute(t
, k
->cmd
));
4990 if (!entry
&& ((e
->state
& (CTRL
| MOD1
)) == 0))
4991 return buffercmd_addkey(t
, e
->keyval
);
4993 return (XT_CB_PASSTHROUGH
);
4997 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5000 void *active
= NULL
;
5002 /* don't use w directly; use t->whatever instead */
5005 show_oops(NULL
, "wv_keypress_cb");
5006 return (XT_CB_PASSTHROUGH
);
5011 DNPRINTF(XT_D_KEY
, "wv_keypress_cb: mode %d keyval 0x%x mask "
5012 "0x%x tab %d\n", t
->mode
, e
->keyval
, e
->state
, t
->tab_id
);
5014 /* Hide buffers, if they are visible, with escape. */
5015 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)) &&
5016 CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
5017 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5019 return (XT_CB_HANDLED
);
5022 if (t
->mode
== XT_MODE_HINT
)
5023 return (XT_CB_HANDLED
);
5025 if ((CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Tab
) ||
5026 (CLEAN(e
->state
) == SHFT
&& e
->keyval
== GDK_Tab
))
5027 /* something focussy is about to happen */
5028 return (XT_CB_PASSTHROUGH
);
5030 /* check if we are some sort of text input thing in the dom */
5031 active
= input_check_mode(t
);
5033 if (t
->mode
== XT_MODE_HINT
) {
5034 /* XXX make sure cmd entry is enabled */
5035 return (XT_CB_HANDLED
);
5036 } else if (t
->mode
== XT_MODE_COMMAND
) {
5038 snprintf(s
, sizeof s
, "%c", e
->keyval
);
5039 if (CLEAN(e
->state
) == 0 && isdigit(s
[0]))
5040 cmd_prefix
= 10 * cmd_prefix
+ atoi(s
);
5041 return (handle_keypress(t
, e
, 0));
5044 return (handle_keypress(t
, e
, 1));
5048 return (XT_CB_PASSTHROUGH
);
5052 hint_continue(struct tab
*t
)
5054 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5056 const gchar
*errstr
= NULL
;
5060 if (!(c
[0] == '.' || c
[0] == ','))
5062 if (strlen(c
) == 1) {
5063 /* XXX should not happen */
5068 if (isdigit(c
[1])) {
5070 i
= strtonum(&c
[1], 1, 4096, &errstr
);
5072 show_oops(t
, "invalid numerical hint %s", &c
[1]);
5075 s
= g_strdup_printf("hints.updateHints(%lld);", i
);
5079 /* alphanumeric input */
5080 s
= g_strdup_printf("hints.createHints('%s', '%c');",
5081 &c
[1], c
[0] == '.' ? 'f' : 'F');
5092 search_continue(struct tab
*t
)
5094 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5095 gboolean rv
= FALSE
;
5097 if (c
[0] == ':' || c
[0] == '.' || c
[0] == ',')
5099 if (strlen(c
) == 1) {
5100 webkit_web_view_unmark_text_matches(t
->wv
);
5105 t
->search_forward
= TRUE
;
5106 else if (c
[0] == '?')
5107 t
->search_forward
= FALSE
;
5117 search_cb(struct tab
*t
)
5119 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5122 if (search_continue(t
) == FALSE
)
5126 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, t
->search_forward
,
5128 /* not found, mark red */
5129 gdk_color_parse(XT_COLOR_RED
, &color
);
5130 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
5131 /* unmark and remove selection */
5132 webkit_web_view_unmark_text_matches(t
->wv
);
5133 /* my kingdom for a way to unselect text in webview */
5135 /* found, highlight all */
5136 webkit_web_view_unmark_text_matches(t
->wv
);
5137 webkit_web_view_mark_text_matches(t
->wv
, &c
[1], FALSE
, 0);
5138 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
5139 gdk_color_parse(XT_COLOR_WHITE
, &color
);
5140 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
5148 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5150 const gchar
*c
= gtk_entry_get_text(w
);
5153 show_oops(NULL
, "cmd_keyrelease_cb invalid parameters");
5154 return (XT_CB_PASSTHROUGH
);
5157 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
5158 e
->keyval
, e
->state
, t
->tab_id
);
5161 if (!(e
->keyval
== GDK_Tab
|| e
->keyval
== GDK_ISO_Left_Tab
)) {
5162 if (hint_continue(t
) == FALSE
)
5167 if (search_continue(t
) == FALSE
)
5170 /* if search length is > 4 then no longer play timeout games */
5171 if (strlen(c
) > 4) {
5173 g_source_remove(t
->search_id
);
5180 /* reestablish a new timer if the user types fast */
5182 g_source_remove(t
->search_id
);
5183 t
->search_id
= g_timeout_add(250, (GSourceFunc
)search_cb
, (gpointer
)t
);
5186 return (XT_CB_PASSTHROUGH
);
5190 match_uri(const gchar
*uri
, const gchar
*key
) {
5193 gboolean match
= FALSE
;
5197 if (!strncmp(key
, uri
, len
))
5200 voffset
= strstr(uri
, "/") + 2;
5201 if (!strncmp(key
, voffset
, len
))
5203 else if (g_str_has_prefix(voffset
, "www.")) {
5204 voffset
= voffset
+ strlen("www.");
5205 if (!strncmp(key
, voffset
, len
))
5214 match_session(const gchar
*name
, const gchar
*key
) {
5217 sub
= strcasestr(name
, key
);
5223 cmd_getlist(int id
, char *key
)
5230 if (cmds
[id
].type
& XT_URLARG
) {
5231 RB_FOREACH_REVERSE(h
, history_list
, &hl
)
5232 if (match_uri(h
->uri
, key
)) {
5233 cmd_status
.list
[c
] = (char *)h
->uri
;
5239 } else if (cmds
[id
].type
& XT_SESSARG
) {
5240 TAILQ_FOREACH(s
, &sessions
, entry
)
5241 if (match_session(s
->name
, key
)) {
5242 cmd_status
.list
[c
] = (char *)s
->name
;
5248 } else if (cmds
[id
].type
& XT_SETARG
) {
5249 for (i
= 0; i
< get_settings_size(); i
++)
5250 if(!strncmp(key
, get_setting_name(i
),
5252 cmd_status
.list
[c
++] = get_setting_name(i
);
5258 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
5260 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
5261 if (cmds
[i
].level
< dep
)
5263 if (cmds
[i
].level
== dep
&& !strncmp(key
, cmds
[i
].cmd
,
5264 strlen(key
)) && !isdigit(cmds
[i
].cmd
[0]))
5265 cmd_status
.list
[c
++] = cmds
[i
].cmd
;
5273 cmd_getnext(int dir
)
5275 cmd_status
.index
+= dir
;
5277 if (cmd_status
.index
< 0)
5278 cmd_status
.index
= cmd_status
.len
- 1;
5279 else if (cmd_status
.index
>= cmd_status
.len
)
5280 cmd_status
.index
= 0;
5282 return cmd_status
.list
[cmd_status
.index
];
5286 cmd_tokenize(char *s
, char *tokens
[])
5289 char *tok
, *last
= NULL
;
5290 size_t len
= strlen(s
);
5293 blank
= len
== 0 || (len
> 0 && s
[len
- 1] == ' ');
5294 for (tok
= strtok_r(s
, " ", &last
); tok
&& i
< 3;
5295 tok
= strtok_r(NULL
, " ", &last
), i
++)
5305 cmd_complete(struct tab
*t
, char *str
, int dir
)
5307 GtkEntry
*w
= GTK_ENTRY(t
->cmd
);
5308 int i
, j
, levels
, c
= 0, dep
= 0, parent
= -1;
5310 char *tok
, *match
, *s
= g_strdup(str
);
5312 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
5315 DNPRINTF(XT_D_CMD
, "%s: complete %s\n", __func__
, str
);
5318 for (i
= 0; isdigit(s
[i
]); i
++)
5321 for (; isspace(s
[i
]); i
++)
5326 levels
= cmd_tokenize(s
, tokens
);
5328 for (i
= 0; i
< levels
- 1; i
++) {
5331 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5332 if (cmds
[j
].level
< dep
)
5334 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
,
5338 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
5345 if (matchcount
== 1) {
5346 strlcat(res
, tok
, sizeof res
);
5347 strlcat(res
, " ", sizeof res
);
5357 if (cmd_status
.index
== -1)
5358 cmd_getlist(parent
, tokens
[i
]);
5360 if (cmd_status
.len
> 0) {
5361 match
= cmd_getnext(dir
);
5362 strlcat(res
, match
, sizeof res
);
5363 gtk_entry_set_text(w
, res
);
5364 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5371 cmd_execute(struct tab
*t
, char *str
)
5373 struct cmd
*cmd
= NULL
;
5374 char *tok
, *last
= NULL
, *s
= g_strdup(str
), *sc
;
5376 int j
, len
, c
= 0, dep
= 0, matchcount
= 0;
5377 int prefix
= -1, rv
= XT_CB_PASSTHROUGH
;
5378 struct karg arg
= {0, NULL
, -1};
5383 for (j
= 0; j
<3 && isdigit(s
[j
]); j
++)
5389 while (isspace(s
[0]))
5392 if (strlen(s
) > 0 && strlen(prefixstr
) > 0)
5393 prefix
= atoi(prefixstr
);
5397 for (tok
= strtok_r(s
, " ", &last
); tok
;
5398 tok
= strtok_r(NULL
, " ", &last
)) {
5400 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5401 if (cmds
[j
].level
< dep
)
5403 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1 :
5405 if (cmds
[j
].level
== dep
&&
5406 !strncmp(tok
, cmds
[j
].cmd
, len
)) {
5410 if (len
== strlen(cmds
[j
].cmd
)) {
5416 if (matchcount
== 1) {
5421 show_oops(t
, "Invalid command: %s", str
);
5427 show_oops(t
, "Empty command");
5433 arg
.precount
= prefix
;
5434 else if (cmd_prefix
> 0)
5435 arg
.precount
= cmd_prefix
;
5437 if (j
> 0 && !(cmd
->type
& XT_PREFIX
) && arg
.precount
> -1) {
5438 show_oops(t
, "No prefix allowed: %s", str
);
5442 arg
.s
= last
? g_strdup(last
) : g_strdup("");
5443 if (cmd
->type
& XT_INTARG
&& last
&& strlen(last
) > 0) {
5444 if (arg
.s
== NULL
) {
5445 show_oops(t
, "Invalid command");
5448 arg
.precount
= atoi(arg
.s
);
5449 if (arg
.precount
<= 0) {
5450 if (arg
.s
[0] == '0')
5451 show_oops(t
, "Zero count");
5453 show_oops(t
, "Trailing characters");
5458 DNPRINTF(XT_D_CMD
, "%s: prefix %d arg %s\n",
5459 __func__
, arg
.precount
, arg
.s
);
5475 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5478 show_oops(NULL
, "entry_key_cb invalid parameters");
5479 return (XT_CB_PASSTHROUGH
);
5482 DNPRINTF(XT_D_CMD
, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
5483 e
->keyval
, e
->state
, t
->tab_id
);
5487 if (e
->keyval
== GDK_Escape
) {
5488 /* don't use focus_webview(t) because we want to type :cmds */
5489 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5492 return (handle_keypress(t
, e
, 1));
5495 struct command_entry
*
5496 history_prev(struct command_list
*l
, struct command_entry
*at
)
5499 at
= TAILQ_LAST(l
, command_list
);
5501 at
= TAILQ_PREV(at
, command_list
, entry
);
5503 at
= TAILQ_LAST(l
, command_list
);
5509 struct command_entry
*
5510 history_next(struct command_list
*l
, struct command_entry
*at
)
5513 at
= TAILQ_FIRST(l
);
5515 at
= TAILQ_NEXT(at
, entry
);
5517 at
= TAILQ_FIRST(l
);
5524 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5526 int rv
= XT_CB_HANDLED
;
5527 const gchar
*c
= gtk_entry_get_text(w
);
5531 show_oops(NULL
, "cmd_keypress_cb parameters");
5532 return (XT_CB_PASSTHROUGH
);
5535 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
5536 e
->keyval
, e
->state
, t
->tab_id
);
5540 e
->keyval
= GDK_Escape
;
5541 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5542 c
[0] == '.' || c
[0] == ','))
5543 e
->keyval
= GDK_Escape
;
5545 if (e
->keyval
!= GDK_Tab
&& e
->keyval
!= GDK_Shift_L
&&
5546 e
->keyval
!= GDK_ISO_Left_Tab
)
5547 cmd_status
.index
= -1;
5549 switch (e
->keyval
) {
5552 cmd_complete(t
, (char *)&c
[1], 1);
5553 else if (c
[0] == '.' || c
[0] == ',')
5554 run_script(t
, "hints.focusNextHint();");
5556 case GDK_ISO_Left_Tab
:
5558 cmd_complete(t
, (char *)&c
[1], -1);
5559 else if (c
[0] == '.' || c
[0] == ',')
5560 run_script(t
, "hints.focusPreviousHint();");
5564 if ((search_at
= history_next(&shl
, search_at
))) {
5565 search_at
->line
[0] = c
[0];
5566 gtk_entry_set_text(w
, search_at
->line
);
5567 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5569 } else if (c
[0] == '/') {
5570 if ((search_at
= history_prev(&chl
, search_at
))) {
5571 search_at
->line
[0] = c
[0];
5572 gtk_entry_set_text(w
, search_at
->line
);
5573 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5575 } if (c
[0] == ':') {
5576 if ((history_at
= history_prev(&chl
, history_at
))) {
5577 history_at
->line
[0] = c
[0];
5578 gtk_entry_set_text(w
, history_at
->line
);
5579 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5585 if ((search_at
= history_next(&shl
, 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 } else if (c
[0] == '?') {
5591 if ((search_at
= history_prev(&chl
, search_at
))) {
5592 search_at
->line
[0] = c
[0];
5593 gtk_entry_set_text(w
, search_at
->line
);
5594 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5596 } if (c
[0] == ':') {
5597 if ((history_at
= history_next(&chl
, history_at
))) {
5598 history_at
->line
[0] = c
[0];
5599 gtk_entry_set_text(w
, history_at
->line
);
5600 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5605 if (!(!strcmp(c
, ":") || !strcmp(c
, "/") || !strcmp(c
, "?") ||
5606 !strcmp(c
, ".") || !strcmp(c
, ","))) {
5607 /* see if we are doing hinting and reset it */
5608 if (c
[0] == '.' || c
[0] == ',') {
5609 /* recreate hints */
5610 s
= g_strdup_printf("hints.createHints('', "
5611 "'%c');", c
[0] == '.' ? 'f' : 'F');
5624 if (c
!= NULL
&& (c
[0] == '/' || c
[0] == '?'))
5625 webkit_web_view_unmark_text_matches(t
->wv
);
5627 /* no need to cancel hints */
5631 rv
= XT_CB_PASSTHROUGH
;
5637 wv_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
5639 DNPRINTF(XT_D_CMD
, "wv_popup_cb: tab %d\n", t
->tab_id
);
5643 cmd_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
5645 /* popup menu enabled */
5650 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
5653 show_oops(NULL
, "cmd_focusout_cb invalid parameters");
5654 return (XT_CB_PASSTHROUGH
);
5657 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d popup %d\n",
5658 t
->tab_id
, t
->popup
);
5660 /* if popup is enabled don't lose focus */
5663 return (XT_CB_PASSTHROUGH
);
5670 if (show_url
== 0 || t
->focus_wv
)
5673 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
5675 return (XT_CB_PASSTHROUGH
);
5679 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
5682 const gchar
*c
= gtk_entry_get_text(entry
);
5685 show_oops(NULL
, "cmd_activate_cb invalid parameters");
5689 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
5694 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5695 c
[0] == '.' || c
[0] == ','))
5701 if (c
[0] == '/' || c
[0] == '?') {
5702 /* see if there is a timer pending */
5704 g_source_remove(t
->search_id
);
5709 if (t
->search_text
) {
5710 g_free(t
->search_text
);
5711 t
->search_text
= NULL
;
5714 t
->search_text
= g_strdup(s
);
5716 g_free(global_search
);
5717 global_search
= g_strdup(s
);
5718 t
->search_forward
= c
[0] == '/';
5720 history_add(&shl
, search_file
, s
, &search_history_count
);
5721 } else if (c
[0] == '.' || c
[0] == ',') {
5722 run_script(t
, "hints.fire();");
5723 /* XXX history for link following? */
5724 } else if (c
[0] == ':') {
5725 history_add(&chl
, command_file
, s
, &cmd_history_count
);
5726 /* can't call hide_cmd after cmd_execute */
5737 backward_cb(GtkWidget
*w
, struct tab
*t
)
5742 show_oops(NULL
, "backward_cb invalid parameters");
5746 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
5753 forward_cb(GtkWidget
*w
, struct tab
*t
)
5758 show_oops(NULL
, "forward_cb invalid parameters");
5762 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
5764 a
.i
= XT_NAV_FORWARD
;
5769 home_cb(GtkWidget
*w
, struct tab
*t
)
5772 show_oops(NULL
, "home_cb invalid parameters");
5776 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
5782 stop_cb(GtkWidget
*w
, struct tab
*t
)
5784 WebKitWebFrame
*frame
;
5787 show_oops(NULL
, "stop_cb invalid parameters");
5791 DNPRINTF(XT_D_NAV
, "stop_cb: tab %d\n", t
->tab_id
);
5793 frame
= webkit_web_view_get_main_frame(t
->wv
);
5794 if (frame
== NULL
) {
5795 show_oops(t
, "stop_cb: no frame");
5799 webkit_web_frame_stop_loading(frame
);
5800 abort_favicon_download(t
);
5804 setup_webkit(struct tab
*t
)
5806 if (is_g_object_setting(G_OBJECT(t
->settings
), "enable-dns-prefetching"))
5807 g_object_set(G_OBJECT(t
->settings
), "enable-dns-prefetching",
5808 FALSE
, (char *)NULL
);
5810 warnx("webkit does not have \"enable-dns-prefetching\" property");
5811 g_object_set(G_OBJECT(t
->settings
),
5812 "user-agent", t
->user_agent
, (char *)NULL
);
5813 g_object_set(G_OBJECT(t
->settings
),
5814 "enable-scripts", enable_scripts
, (char *)NULL
);
5815 g_object_set(G_OBJECT(t
->settings
),
5816 "enable-plugins", enable_plugins
, (char *)NULL
);
5817 g_object_set(G_OBJECT(t
->settings
),
5818 "javascript-can-open-windows-automatically", enable_scripts
,
5820 g_object_set(G_OBJECT(t
->settings
),
5821 "enable-html5-database", FALSE
, (char *)NULL
);
5822 g_object_set(G_OBJECT(t
->settings
),
5823 "enable-html5-local-storage", enable_localstorage
, (char *)NULL
);
5824 g_object_set(G_OBJECT(t
->settings
),
5825 "enable_spell_checking", enable_spell_checking
, (char *)NULL
);
5826 g_object_set(G_OBJECT(t
->settings
),
5827 "spell_checking_languages", spell_check_languages
, (char *)NULL
);
5828 g_object_set(G_OBJECT(t
->settings
),
5829 "enable-developer-extras", TRUE
, (char *)NULL
);
5830 g_object_set(G_OBJECT(t
->wv
),
5831 "full-content-zoom", TRUE
, (char *)NULL
);
5833 webkit_web_view_set_settings(t
->wv
, t
->settings
);
5837 update_statusbar_position(GtkAdjustment
* adjustment
, gpointer data
)
5839 struct tab
*ti
, *t
= NULL
;
5840 gdouble view_size
, value
, max
;
5843 TAILQ_FOREACH(ti
, &tabs
, entry
)
5844 if (ti
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
5852 if (adjustment
== NULL
)
5853 adjustment
= gtk_scrolled_window_get_vadjustment(
5854 GTK_SCROLLED_WINDOW(t
->browser_win
));
5856 view_size
= gtk_adjustment_get_page_size(adjustment
);
5857 value
= gtk_adjustment_get_value(adjustment
);
5858 max
= gtk_adjustment_get_upper(adjustment
) - view_size
;
5861 position
= g_strdup("All");
5862 else if (value
== max
)
5863 position
= g_strdup("Bot");
5864 else if (value
== 0)
5865 position
= g_strdup("Top");
5867 position
= g_strdup_printf("%d%%", (int) ((value
/ max
) * 100));
5869 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.position
), position
);
5876 create_window(const gchar
*name
)
5880 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
5881 gtk_window_set_default_size(GTK_WINDOW(w
), window_width
, window_height
);
5882 gtk_widget_set_name(w
, name
);
5883 gtk_window_set_wmclass(GTK_WINDOW(w
), name
, "XXXTerm");
5889 create_browser(struct tab
*t
)
5893 GtkAdjustment
*adjustment
;
5896 show_oops(NULL
, "create_browser invalid parameters");
5900 t
->sb_h
= GTK_SCROLLBAR(gtk_hscrollbar_new(NULL
));
5901 t
->sb_v
= GTK_SCROLLBAR(gtk_vscrollbar_new(NULL
));
5902 t
->adjust_h
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_h
));
5903 t
->adjust_v
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_v
));
5905 w
= gtk_scrolled_window_new(t
->adjust_h
, t
->adjust_v
);
5906 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
5907 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
5909 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
5910 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
5913 t
->settings
= webkit_web_settings_new();
5915 g_object_set(t
->settings
, "default-encoding", encoding
, (char *)NULL
);
5917 if (user_agent
== NULL
) {
5918 g_object_get(G_OBJECT(t
->settings
), "user-agent", &strval
,
5920 t
->user_agent
= g_strdup_printf("%s %s+", strval
, version
);
5923 t
->user_agent
= g_strdup(user_agent
);
5925 t
->stylesheet
= g_strdup_printf("file://%s/style.css", resource_dir
);
5928 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w
));
5929 g_signal_connect(G_OBJECT(adjustment
), "value-changed",
5930 G_CALLBACK(update_statusbar_position
), NULL
);
5939 create_kiosk_toolbar(struct tab
*t
)
5941 GtkWidget
*toolbar
= NULL
, *b
;
5943 b
= gtk_hbox_new(FALSE
, 0);
5945 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
5947 /* backward button */
5948 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
5949 gtk_widget_set_sensitive(t
->backward
, FALSE
);
5950 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
5951 G_CALLBACK(backward_cb
), t
);
5952 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, TRUE
, TRUE
, 0);
5954 /* forward button */
5955 t
->forward
= create_button("Forward", GTK_STOCK_GO_FORWARD
, 0);
5956 gtk_widget_set_sensitive(t
->forward
, FALSE
);
5957 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
5958 G_CALLBACK(forward_cb
), t
);
5959 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, TRUE
, TRUE
, 0);
5962 t
->gohome
= create_button("Home", GTK_STOCK_HOME
, 0);
5963 gtk_widget_set_sensitive(t
->gohome
, true);
5964 g_signal_connect(G_OBJECT(t
->gohome
), "clicked",
5965 G_CALLBACK(home_cb
), t
);
5966 gtk_box_pack_start(GTK_BOX(b
), t
->gohome
, TRUE
, TRUE
, 0);
5968 /* create widgets but don't use them */
5969 t
->uri_entry
= gtk_entry_new();
5970 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
5971 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
5972 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
5978 create_toolbar(struct tab
*t
)
5980 GtkWidget
*toolbar
= NULL
, *b
, *eb1
;
5982 b
= gtk_hbox_new(FALSE
, 0);
5984 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
5986 /* backward button */
5987 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
5988 gtk_widget_set_sensitive(t
->backward
, FALSE
);
5989 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
5990 G_CALLBACK(backward_cb
), t
);
5991 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, FALSE
, FALSE
, 0);
5993 /* forward button */
5994 t
->forward
= create_button("Forward",GTK_STOCK_GO_FORWARD
, 0);
5995 gtk_widget_set_sensitive(t
->forward
, FALSE
);
5996 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
5997 G_CALLBACK(forward_cb
), t
);
5998 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, FALSE
,
6002 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
6003 gtk_widget_set_sensitive(t
->stop
, FALSE
);
6004 g_signal_connect(G_OBJECT(t
->stop
), "clicked",
6005 G_CALLBACK(stop_cb
), t
);
6006 gtk_box_pack_start(GTK_BOX(b
), t
->stop
, FALSE
,
6010 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
6011 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
6012 gtk_widget_set_sensitive(t
->js_toggle
, TRUE
);
6013 g_signal_connect(G_OBJECT(t
->js_toggle
), "clicked",
6014 G_CALLBACK(js_toggle_cb
), t
);
6015 gtk_box_pack_start(GTK_BOX(b
), t
->js_toggle
, FALSE
, FALSE
, 0);
6017 t
->uri_entry
= gtk_entry_new();
6018 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
6019 G_CALLBACK(activate_uri_entry_cb
), t
);
6020 g_signal_connect(G_OBJECT(t
->uri_entry
), "key-press-event",
6021 G_CALLBACK(entry_key_cb
), t
);
6023 eb1
= gtk_hbox_new(FALSE
, 0);
6024 gtk_container_set_border_width(GTK_CONTAINER(eb1
), 1);
6025 gtk_box_pack_start(GTK_BOX(eb1
), t
->uri_entry
, TRUE
, TRUE
, 0);
6026 gtk_box_pack_start(GTK_BOX(b
), eb1
, TRUE
, TRUE
, 0);
6029 if (search_string
) {
6031 t
->search_entry
= gtk_entry_new();
6032 gtk_entry_set_width_chars(GTK_ENTRY(t
->search_entry
), 30);
6033 g_signal_connect(G_OBJECT(t
->search_entry
), "activate",
6034 G_CALLBACK(activate_search_entry_cb
), t
);
6035 g_signal_connect(G_OBJECT(t
->search_entry
), "key-press-event",
6036 G_CALLBACK(entry_key_cb
), t
);
6037 gtk_widget_set_size_request(t
->search_entry
, -1, -1);
6038 eb2
= gtk_hbox_new(FALSE
, 0);
6039 gtk_container_set_border_width(GTK_CONTAINER(eb2
), 1);
6040 gtk_box_pack_start(GTK_BOX(eb2
), t
->search_entry
, TRUE
, TRUE
,
6042 gtk_box_pack_start(GTK_BOX(b
), eb2
, FALSE
, FALSE
, 0);
6049 create_buffers(struct tab
*t
)
6051 GtkCellRenderer
*renderer
;
6054 view
= gtk_tree_view_new();
6056 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view
), FALSE
);
6058 renderer
= gtk_cell_renderer_text_new();
6059 gtk_tree_view_insert_column_with_attributes
6060 (GTK_TREE_VIEW(view
), -1, "Id", renderer
, "text", COL_ID
, (char *)NULL
);
6062 renderer
= gtk_cell_renderer_text_new();
6063 gtk_tree_view_insert_column_with_attributes
6064 (GTK_TREE_VIEW(view
), -1, "Title", renderer
, "text", COL_TITLE
,
6067 gtk_tree_view_set_model
6068 (GTK_TREE_VIEW(view
), GTK_TREE_MODEL(buffers_store
));
6074 row_activated_cb(GtkTreeView
*view
, GtkTreePath
*path
,
6075 GtkTreeViewColumn
*col
, struct tab
*t
)
6080 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6082 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
,
6085 (GTK_TREE_MODEL(buffers_store
), &iter
, COL_ID
, &id
, -1);
6086 set_current_tab(id
- 1);
6092 /* after tab reordering/creation/removal */
6099 TAILQ_FOREACH(t
, &tabs
, entry
) {
6100 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
6101 if (t
->tab_id
> maxid
)
6104 gtk_widget_show(t
->tab_elems
.sep
);
6107 TAILQ_FOREACH(t
, &tabs
, entry
) {
6108 if (t
->tab_id
== maxid
) {
6109 gtk_widget_hide(t
->tab_elems
.sep
);
6115 /* after active tab change */
6117 recolor_compact_tabs(void)
6123 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6124 TAILQ_FOREACH(t
, &tabs
, entry
)
6125 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
,
6128 curid
= gtk_notebook_get_current_page(notebook
);
6129 TAILQ_FOREACH(t
, &tabs
, entry
)
6130 if (t
->tab_id
== curid
) {
6131 gdk_color_parse(XT_COLOR_CT_ACTIVE
, &color
);
6132 gtk_widget_modify_fg(t
->tab_elems
.label
,
6133 GTK_STATE_NORMAL
, &color
);
6139 set_current_tab(int page_num
)
6141 buffercmd_abort(get_current_tab());
6142 gtk_notebook_set_current_page(notebook
, page_num
);
6143 recolor_compact_tabs();
6147 undo_close_tab_save(struct tab
*t
)
6151 struct undo
*u1
, *u2
;
6153 WebKitWebHistoryItem
*item
;
6155 if ((uri
= get_uri(t
)) == NULL
)
6158 u1
= g_malloc0(sizeof(struct undo
));
6159 u1
->uri
= g_strdup(uri
);
6161 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
6163 m
= webkit_web_back_forward_list_get_forward_length(t
->bfl
);
6164 n
= webkit_web_back_forward_list_get_back_length(t
->bfl
);
6167 /* forward history */
6168 items
= webkit_web_back_forward_list_get_forward_list_with_limit(t
->bfl
, m
);
6172 u1
->history
= g_list_prepend(u1
->history
,
6173 webkit_web_history_item_copy(item
));
6174 items
= g_list_next(items
);
6179 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
6180 u1
->history
= g_list_prepend(u1
->history
,
6181 webkit_web_history_item_copy(item
));
6185 items
= webkit_web_back_forward_list_get_back_list_with_limit(t
->bfl
, n
);
6189 u1
->history
= g_list_prepend(u1
->history
,
6190 webkit_web_history_item_copy(item
));
6191 items
= g_list_next(items
);
6194 TAILQ_INSERT_HEAD(&undos
, u1
, entry
);
6196 if (undo_count
> XT_MAX_UNDO_CLOSE_TAB
) {
6197 u2
= TAILQ_LAST(&undos
, undo_tailq
);
6198 TAILQ_REMOVE(&undos
, u2
, entry
);
6200 g_list_free(u2
->history
);
6209 delete_tab(struct tab
*t
)
6213 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
6219 * no need to join thread here because it won't access t on completion
6222 TAILQ_REMOVE(&tabs
, t
, entry
);
6225 /* Halt all webkit activity. */
6226 abort_favicon_download(t
);
6227 webkit_web_view_stop_loading(t
->wv
);
6229 /* Save the tab, so we can undo the close. */
6230 undo_close_tab_save(t
);
6234 g_source_remove(t
->search_id
);
6237 bzero(&a
, sizeof a
);
6239 inspector_cmd(t
, &a
);
6241 if (browser_mode
== XT_BM_KIOSK
) {
6242 gtk_widget_destroy(t
->uri_entry
);
6243 gtk_widget_destroy(t
->stop
);
6244 gtk_widget_destroy(t
->js_toggle
);
6247 gtk_widget_destroy(t
->tab_elems
.eventbox
);
6248 gtk_widget_destroy(t
->vbox
);
6250 g_free(t
->user_agent
);
6251 g_free(t
->stylesheet
);
6256 if (TAILQ_EMPTY(&tabs
)) {
6257 if (browser_mode
== XT_BM_KIOSK
)
6258 create_new_tab(home
, NULL
, 1, -1);
6260 create_new_tab(NULL
, NULL
, 1, -1);
6263 /* recreate session */
6264 if (session_autosave
) {
6265 bzero(&a
, sizeof a
);
6267 save_tabs(NULL
, &a
);
6271 recolor_compact_tabs();
6275 update_statusbar_zoom(struct tab
*t
)
6278 char s
[16] = { '\0' };
6280 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6281 if ((zoom
<= 0.99 || zoom
>= 1.01))
6282 snprintf(s
, sizeof s
, "%d%%", (int)(zoom
* 100));
6283 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.zoom
), s
);
6287 setzoom_webkit(struct tab
*t
, int adjust
)
6289 #define XT_ZOOMPERCENT 0.04
6294 show_oops(NULL
, "setzoom_webkit invalid parameters");
6298 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6299 if (adjust
== XT_ZOOM_IN
)
6300 zoom
+= XT_ZOOMPERCENT
;
6301 else if (adjust
== XT_ZOOM_OUT
)
6302 zoom
-= XT_ZOOMPERCENT
;
6303 else if (adjust
> 0)
6304 zoom
= default_zoom_level
+ adjust
/ 100.0 - 1.0;
6306 show_oops(t
, "setzoom_webkit invalid zoom value");
6310 if (zoom
< XT_ZOOMPERCENT
)
6311 zoom
= XT_ZOOMPERCENT
;
6312 g_object_set(G_OBJECT(t
->wv
), "zoom-level", zoom
, (char *)NULL
);
6313 update_statusbar_zoom(t
);
6317 tab_clicked_cb(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
6319 struct tab
*t
= (struct tab
*) data
;
6321 DNPRINTF(XT_D_TAB
, "tab_clicked_cb: tab: %d\n", t
->tab_id
);
6323 switch (event
->button
) {
6325 set_current_tab(t
->tab_id
);
6336 append_tab(struct tab
*t
)
6341 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
6342 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
6346 create_sbe(int width
)
6350 sbe
= gtk_entry_new();
6351 gtk_entry_set_inner_border(GTK_ENTRY(sbe
), NULL
);
6352 gtk_entry_set_has_frame(GTK_ENTRY(sbe
), FALSE
);
6353 gtk_widget_set_can_focus(GTK_WIDGET(sbe
), FALSE
);
6354 gtk_widget_modify_font(GTK_WIDGET(sbe
), statusbar_font
);
6355 gtk_entry_set_alignment(GTK_ENTRY(sbe
), 1.0);
6356 gtk_widget_set_size_request(sbe
, width
, -1);
6362 create_new_tab(char *title
, struct undo
*u
, int focus
, int position
)
6367 WebKitWebHistoryItem
*item
;
6371 int sbe_p
= 0, sbe_b
= 0,
6374 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
6376 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
6377 DNPRINTF(XT_D_TAB
, "create_new_tab: new tab rejected\n");
6381 t
= g_malloc0(sizeof *t
);
6383 if (title
== NULL
) {
6384 title
= "(untitled)";
6388 t
->vbox
= gtk_vbox_new(FALSE
, 0);
6390 /* label + button for tab */
6391 b
= gtk_hbox_new(FALSE
, 0);
6394 #if GTK_CHECK_VERSION(2, 20, 0)
6395 t
->spinner
= gtk_spinner_new();
6397 t
->label
= gtk_label_new(title
);
6398 bb
= create_button("Close", GTK_STOCK_CLOSE
, 1);
6399 gtk_widget_set_size_request(t
->label
, 100, 0);
6400 gtk_label_set_max_width_chars(GTK_LABEL(t
->label
), 20);
6401 gtk_label_set_ellipsize(GTK_LABEL(t
->label
), PANGO_ELLIPSIZE_END
);
6402 gtk_widget_set_size_request(b
, 130, 0);
6404 gtk_box_pack_start(GTK_BOX(b
), bb
, FALSE
, FALSE
, 0);
6405 gtk_box_pack_start(GTK_BOX(b
), t
->label
, FALSE
, FALSE
, 0);
6406 #if GTK_CHECK_VERSION(2, 20, 0)
6407 gtk_box_pack_start(GTK_BOX(b
), t
->spinner
, FALSE
, FALSE
, 0);
6411 if (browser_mode
== XT_BM_KIOSK
) {
6412 t
->toolbar
= create_kiosk_toolbar(t
);
6413 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
6416 t
->toolbar
= create_toolbar(t
);
6418 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
,
6426 t
->browser_win
= create_browser(t
);
6427 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
6429 /* oops message for user feedback */
6430 t
->oops
= gtk_entry_new();
6431 gtk_entry_set_inner_border(GTK_ENTRY(t
->oops
), NULL
);
6432 gtk_entry_set_has_frame(GTK_ENTRY(t
->oops
), FALSE
);
6433 gtk_widget_set_can_focus(GTK_WIDGET(t
->oops
), FALSE
);
6434 gdk_color_parse(XT_COLOR_RED
, &color
);
6435 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
6436 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
6437 gtk_widget_modify_font(GTK_WIDGET(t
->oops
), oops_font
);
6440 t
->cmd
= gtk_entry_new();
6441 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
6442 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
6443 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
6444 gtk_widget_modify_font(GTK_WIDGET(t
->cmd
), cmd_font
);
6447 t
->statusbar_box
= gtk_hbox_new(FALSE
, 0);
6449 t
->sbe
.statusbar
= gtk_entry_new();
6450 gtk_entry_set_inner_border(GTK_ENTRY(t
->sbe
.statusbar
), NULL
);
6451 gtk_entry_set_has_frame(GTK_ENTRY(t
->sbe
.statusbar
), FALSE
);
6452 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.statusbar
), FALSE
);
6453 gtk_widget_modify_font(GTK_WIDGET(t
->sbe
.statusbar
), statusbar_font
);
6455 /* create these widgets only if specified in statusbar_elems */
6457 t
->sbe
.position
= create_sbe(40);
6458 t
->sbe
.zoom
= create_sbe(40);
6459 t
->sbe
.buffercmd
= create_sbe(60);
6461 statusbar_modify_attr(t
, XT_COLOR_WHITE
, XT_COLOR_BLACK
);
6463 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), t
->sbe
.statusbar
, TRUE
,
6466 /* gtk widgets cannot be added to a box twice. sbe_* variables
6467 make sure of this */
6468 for (p
= statusbar_elems
; *p
!= '\0'; p
++) {
6472 GtkWidget
*sep
= gtk_vseparator_new();
6474 gdk_color_parse(XT_COLOR_SB_SEPARATOR
, &color
);
6475 gtk_widget_modify_bg(sep
, GTK_STATE_NORMAL
, &color
);
6476 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), sep
,
6477 FALSE
, FALSE
, FALSE
);
6482 warnx("flag \"%c\" specified more than "
6483 "once in statusbar_elems\n", *p
);
6487 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6488 t
->sbe
.position
, FALSE
, FALSE
, FALSE
);
6492 warnx("flag \"%c\" specified more than "
6493 "once in statusbar_elems\n", *p
);
6497 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6498 t
->sbe
.buffercmd
, FALSE
, FALSE
, FALSE
);
6502 warnx("flag \"%c\" specified more than "
6503 "once in statusbar_elems\n", *p
);
6507 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6508 t
->sbe
.zoom
, FALSE
, FALSE
, FALSE
);
6511 warnx("illegal flag \"%c\" in statusbar_elems\n", *p
);
6516 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar_box
, FALSE
, FALSE
, 0);
6519 t
->buffers
= create_buffers(t
);
6520 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->buffers
, FALSE
, FALSE
, 0);
6522 /* xtp meaning is normal by default */
6523 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
6525 /* set empty favicon */
6526 xt_icon_from_name(t
, "text-html");
6528 /* and show it all */
6529 gtk_widget_show_all(b
);
6530 gtk_widget_show_all(t
->vbox
);
6532 /* compact tab bar */
6533 t
->tab_elems
.label
= gtk_label_new(title
);
6534 gtk_label_set_width_chars(GTK_LABEL(t
->tab_elems
.label
), 1.0);
6535 gtk_misc_set_alignment(GTK_MISC(t
->tab_elems
.label
), 0.0, 0.0);
6536 gtk_misc_set_padding(GTK_MISC(t
->tab_elems
.label
), 4.0, 4.0);
6537 gtk_widget_modify_font(GTK_WIDGET(t
->tab_elems
.label
), tabbar_font
);
6539 t
->tab_elems
.eventbox
= gtk_event_box_new();
6540 t
->tab_elems
.box
= gtk_hbox_new(FALSE
, 0);
6541 t
->tab_elems
.sep
= gtk_vseparator_new();
6543 gdk_color_parse(XT_COLOR_CT_BACKGROUND
, &color
);
6544 gtk_widget_modify_bg(t
->tab_elems
.eventbox
, GTK_STATE_NORMAL
, &color
);
6545 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6546 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
, &color
);
6547 gdk_color_parse(XT_COLOR_CT_SEPARATOR
, &color
);
6548 gtk_widget_modify_bg(t
->tab_elems
.sep
, GTK_STATE_NORMAL
, &color
);
6550 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.label
, TRUE
,
6552 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.sep
, FALSE
,
6554 gtk_container_add(GTK_CONTAINER(t
->tab_elems
.eventbox
),
6557 gtk_box_pack_start(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
, TRUE
,
6559 gtk_widget_show_all(t
->tab_elems
.eventbox
);
6561 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
6564 id
= position
>= 0 ? position
:
6565 gtk_notebook_get_current_page(notebook
) + 1;
6566 if (id
> gtk_notebook_get_n_pages(notebook
))
6569 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
6570 gtk_notebook_insert_page(notebook
, t
->vbox
, b
, id
);
6571 gtk_box_reorder_child(GTK_BOX(tab_bar
),
6572 t
->tab_elems
.eventbox
, id
);
6577 #if GTK_CHECK_VERSION(2, 20, 0)
6578 /* turn spinner off if we are a new tab without uri */
6580 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
6581 gtk_widget_hide(t
->spinner
);
6584 /* make notebook tabs reorderable */
6585 gtk_notebook_set_tab_reorderable(notebook
, t
->vbox
, TRUE
);
6587 /* compact tabs clickable */
6588 g_signal_connect(G_OBJECT(t
->tab_elems
.eventbox
),
6589 "button_press_event", G_CALLBACK(tab_clicked_cb
), t
);
6591 g_object_connect(G_OBJECT(t
->cmd
),
6592 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb
), t
,
6593 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
6594 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb
), t
,
6595 "signal::activate", G_CALLBACK(cmd_activate_cb
), t
,
6596 "signal::populate-popup", G_CALLBACK(cmd_popup_cb
), t
,
6599 /* reuse wv_button_cb to hide oops */
6600 g_object_connect(G_OBJECT(t
->oops
),
6601 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
6604 g_signal_connect(t
->buffers
,
6605 "row-activated", G_CALLBACK(row_activated_cb
), t
);
6606 g_object_connect(G_OBJECT(t
->buffers
),
6607 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
, (char *)NULL
);
6609 g_object_connect(G_OBJECT(t
->wv
),
6610 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
,
6611 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb
), t
,
6612 "signal::download-requested", G_CALLBACK(webview_download_cb
), t
,
6613 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), t
,
6614 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
6615 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
6616 "signal::create-web-view", G_CALLBACK(webview_cwv_cb
), t
,
6617 "signal::close-web-view", G_CALLBACK(webview_closewv_cb
), t
,
6618 "signal::event", G_CALLBACK(webview_event_cb
), t
,
6619 "signal::load-finished", G_CALLBACK(webview_load_finished_cb
), t
,
6620 "signal::load-progress-changed", G_CALLBACK(webview_progress_changed_cb
), t
,
6621 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb
), t
,
6622 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
6623 "signal::button_release_event", G_CALLBACK(wv_release_button_cb
), t
,
6624 "signal::populate-popup", G_CALLBACK(wv_popup_cb
), t
,
6626 g_signal_connect(t
->wv
,
6627 "notify::load-status", G_CALLBACK(notify_load_status_cb
), t
);
6628 g_signal_connect(t
->wv
,
6629 "notify::title", G_CALLBACK(notify_title_cb
), t
);
6631 /* hijack the unused keys as if we were the browser */
6632 //g_object_connect(G_OBJECT(t->toolbar),
6633 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
6636 g_signal_connect(G_OBJECT(bb
), "button_press_event",
6637 G_CALLBACK(tab_close_cb
), t
);
6640 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
6641 /* restore the tab's history */
6642 if (u
&& u
->history
) {
6646 webkit_web_back_forward_list_add_item(t
->bfl
, item
);
6647 items
= g_list_next(items
);
6650 item
= g_list_nth_data(u
->history
, u
->back
);
6652 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
6655 g_list_free(u
->history
);
6657 webkit_web_back_forward_list_clear(t
->bfl
);
6663 url_set_visibility();
6664 statusbar_set_visibility();
6667 set_current_tab(t
->tab_id
);
6668 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
6672 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
6676 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6683 recolor_compact_tabs();
6684 setzoom_webkit(t
, XT_ZOOM_NORMAL
);
6689 notebook_switchpage_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
6695 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
6697 if (gtk_notebook_get_current_page(notebook
) == -1)
6700 TAILQ_FOREACH(t
, &tabs
, entry
) {
6701 if (t
->tab_id
== pn
) {
6702 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
6705 uri
= get_title(t
, TRUE
);
6706 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
6712 /* can't use focus_webview here */
6713 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6720 notebook_pagereordered_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
6723 struct tab
*t
= NULL
, *tt
;
6727 TAILQ_FOREACH(tt
, &tabs
, entry
)
6728 if (tt
->tab_id
== pn
) {
6734 DNPRINTF(XT_D_TAB
, "page_reordered_cb: tab: %d\n", t
->tab_id
);
6736 gtk_box_reorder_child(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
,
6741 menuitem_response(struct tab
*t
)
6743 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
6747 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
6749 GtkWidget
*menu
, *menu_items
;
6750 GdkEventButton
*bevent
;
6754 if (event
->type
== GDK_BUTTON_PRESS
) {
6755 bevent
= (GdkEventButton
*) event
;
6756 menu
= gtk_menu_new();
6758 TAILQ_FOREACH(ti
, &tabs
, entry
) {
6759 if ((uri
= get_uri(ti
)) == NULL
)
6760 /* XXX make sure there is something to print */
6761 /* XXX add gui pages in here to look purdy */
6763 menu_items
= gtk_menu_item_new_with_label(uri
);
6764 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), menu_items
);
6765 gtk_widget_show(menu_items
);
6767 g_signal_connect_swapped((menu_items
),
6768 "activate", G_CALLBACK(menuitem_response
),
6772 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
,
6773 bevent
->button
, bevent
->time
);
6775 /* unref object so it'll free itself when popped down */
6776 #if !GTK_CHECK_VERSION(3, 0, 0)
6777 /* XXX does not need unref with gtk+3? */
6778 g_object_ref_sink(menu
);
6779 g_object_unref(menu
);
6782 return (TRUE
/* eat event */);
6785 return (FALSE
/* propagate */);
6789 icon_size_map(int iconsz
)
6791 if (iconsz
<= GTK_ICON_SIZE_INVALID
||
6792 iconsz
> GTK_ICON_SIZE_DIALOG
)
6793 return (GTK_ICON_SIZE_SMALL_TOOLBAR
);
6799 create_button(char *name
, char *stockid
, int size
)
6801 GtkWidget
*button
, *image
;
6805 rcstring
= g_strdup_printf(
6806 "style \"%s-style\"\n"
6808 " GtkWidget::focus-padding = 0\n"
6809 " GtkWidget::focus-line-width = 0\n"
6813 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
6814 gtk_rc_parse_string(rcstring
);
6816 button
= gtk_button_new();
6817 gtk_button_set_focus_on_click(GTK_BUTTON(button
), FALSE
);
6818 gtk_icon_size
= icon_size_map(size
? size
: icon_size
);
6820 image
= gtk_image_new_from_stock(stockid
, gtk_icon_size
);
6821 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
6822 gtk_container_set_border_width(GTK_CONTAINER(button
), 1);
6823 gtk_container_add(GTK_CONTAINER(button
), GTK_WIDGET(image
));
6824 gtk_widget_set_name(button
, name
);
6825 gtk_button_set_relief(GTK_BUTTON(button
), GTK_RELIEF_NONE
);
6831 button_set_stockid(GtkWidget
*button
, char *stockid
)
6835 image
= gtk_image_new_from_stock(stockid
, icon_size_map(icon_size
));
6836 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
6837 gtk_button_set_image(GTK_BUTTON(button
), image
);
6841 clipb_primary_cb(GtkClipboard
*primary
, GdkEvent
*event
, gpointer notused
)
6844 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
6847 if (xterm_workaround
== 0)
6851 * xterm doesn't play nice with clipboards because it clears the
6852 * primary when clicked. We rely on primary being set to properly
6853 * handle middle mouse button clicks (paste). So when someone clears
6854 * primary copy whatever is in CUT_BUFFER0 into primary to simualte
6855 * other application behavior (as in DON'T clear primary).
6858 p
= gtk_clipboard_wait_for_text(primary
);
6860 if (gdk_property_get(gdk_get_default_root_window(),
6862 gdk_atom_intern("STRING", FALSE
),
6864 1024 * 1024 /* picked out of my butt */,
6870 /* yes sir, we need to NUL the string */
6872 gtk_clipboard_set_text(primary
, p
, -1);
6886 char file
[PATH_MAX
];
6889 vbox
= gtk_vbox_new(FALSE
, 0);
6890 gtk_box_set_spacing(GTK_BOX(vbox
), 0);
6891 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
6892 #if !GTK_CHECK_VERSION(3, 0, 0)
6893 /* XXX seems to be needed with gtk+2 */
6894 gtk_notebook_set_tab_hborder(notebook
, 0);
6895 gtk_notebook_set_tab_vborder(notebook
, 0);
6897 gtk_notebook_set_scrollable(notebook
, TRUE
);
6898 gtk_notebook_set_show_border(notebook
, FALSE
);
6899 gtk_widget_set_can_focus(GTK_WIDGET(notebook
), FALSE
);
6901 abtn
= gtk_button_new();
6902 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
6903 gtk_widget_set_size_request(arrow
, -1, -1);
6904 gtk_container_add(GTK_CONTAINER(abtn
), arrow
);
6905 gtk_widget_set_size_request(abtn
, -1, 20);
6907 #if GTK_CHECK_VERSION(2, 20, 0)
6908 gtk_notebook_set_action_widget(notebook
, abtn
, GTK_PACK_END
);
6910 gtk_widget_set_size_request(GTK_WIDGET(notebook
), -1, -1);
6912 /* compact tab bar */
6913 tab_bar
= gtk_hbox_new(TRUE
, 0);
6915 gtk_box_pack_start(GTK_BOX(vbox
), tab_bar
, FALSE
, FALSE
, 0);
6916 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
6917 gtk_widget_set_size_request(vbox
, -1, -1);
6919 g_object_connect(G_OBJECT(notebook
),
6920 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb
), NULL
,
6922 g_object_connect(G_OBJECT(notebook
),
6923 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb
),
6924 NULL
, (char *)NULL
);
6925 g_signal_connect(G_OBJECT(abtn
), "button_press_event",
6926 G_CALLBACK(arrow_cb
), NULL
);
6928 main_window
= create_window("xxxterm");
6929 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
6930 g_signal_connect(G_OBJECT(main_window
), "delete_event",
6931 G_CALLBACK(gtk_main_quit
), NULL
);
6934 for (i
= 0; i
< LENGTH(icons
); i
++) {
6935 snprintf(file
, sizeof file
, "%s/%s", resource_dir
, icons
[i
]);
6936 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
6937 l
= g_list_append(l
, pb
);
6939 gtk_window_set_default_icon_list(l
);
6941 /* clipboard work around */
6942 if (xterm_workaround
)
6944 G_OBJECT(gtk_clipboard_get(GDK_SELECTION_PRIMARY
)),
6945 "owner-change", G_CALLBACK(clipb_primary_cb
), NULL
);
6947 gtk_widget_show_all(abtn
);
6948 gtk_widget_show_all(main_window
);
6949 notebook_tab_set_visibility();
6953 send_cmd_to_socket(char *cmd
)
6956 struct sockaddr_un sa
;
6958 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
6959 warnx("%s: socket", __func__
);
6963 sa
.sun_family
= AF_UNIX
;
6964 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
6965 work_dir
, XT_SOCKET_FILE
);
6968 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
6969 warnx("%s: connect", __func__
);
6973 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
6974 warnx("%s: send", __func__
);
6985 socket_watcher(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
6988 char str
[XT_MAX_URL_LENGTH
];
6989 socklen_t t
= sizeof(struct sockaddr_un
);
6990 struct sockaddr_un sa
;
6995 gint fd
= g_io_channel_unix_get_fd(source
);
6997 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
7002 if (getpeereid(s
, &uid
, &gid
) == -1) {
7006 if (uid
!= getuid() || gid
!= getgid()) {
7007 warnx("unauthorized user");
7013 warnx("not a valid user");
7017 n
= recv(s
, str
, sizeof(str
), 0);
7021 tt
= TAILQ_LAST(&tabs
, tab_list
);
7022 cmd_execute(tt
, str
);
7030 struct sockaddr_un sa
;
7032 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7033 warn("is_running: socket");
7037 sa
.sun_family
= AF_UNIX
;
7038 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
7039 work_dir
, XT_SOCKET_FILE
);
7042 /* connect to see if there is a listener */
7043 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
7044 rv
= 0; /* not running */
7046 rv
= 1; /* already running */
7057 struct sockaddr_un sa
;
7059 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7060 warn("build_socket: socket");
7064 sa
.sun_family
= AF_UNIX
;
7065 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
7066 work_dir
, XT_SOCKET_FILE
);
7069 /* connect to see if there is a listener */
7070 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7071 /* no listener so we will */
7072 unlink(sa
.sun_path
);
7074 if (bind(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7075 warn("build_socket: bind");
7079 if (listen(s
, 1) == -1) {
7080 warn("build_socket: listen");
7093 completion_select_cb(GtkEntryCompletion
*widget
, GtkTreeModel
*model
,
7094 GtkTreeIter
*iter
, struct tab
*t
)
7098 gtk_tree_model_get(model
, iter
, 0, &value
, -1);
7106 completion_hover_cb(GtkEntryCompletion
*widget
, GtkTreeModel
*model
,
7107 GtkTreeIter
*iter
, struct tab
*t
)
7111 gtk_tree_model_get(model
, iter
, 0, &value
, -1);
7112 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), value
);
7113 gtk_editable_set_position(GTK_EDITABLE(t
->uri_entry
), -1);
7120 completion_add_uri(const gchar
*uri
)
7124 /* add uri to list_store */
7125 gtk_list_store_append(completion_model
, &iter
);
7126 gtk_list_store_set(completion_model
, &iter
, 0, uri
, -1);
7130 completion_match(GtkEntryCompletion
*completion
, const gchar
*key
,
7131 GtkTreeIter
*iter
, gpointer user_data
)
7134 gboolean match
= FALSE
;
7136 gtk_tree_model_get(GTK_TREE_MODEL(completion_model
), iter
, 0, &value
,
7142 match
= match_uri(value
, key
);
7149 completion_add(struct tab
*t
)
7151 /* enable completion for tab */
7152 t
->completion
= gtk_entry_completion_new();
7153 gtk_entry_completion_set_text_column(t
->completion
, 0);
7154 gtk_entry_set_completion(GTK_ENTRY(t
->uri_entry
), t
->completion
);
7155 gtk_entry_completion_set_model(t
->completion
,
7156 GTK_TREE_MODEL(completion_model
));
7157 gtk_entry_completion_set_match_func(t
->completion
, completion_match
,
7159 gtk_entry_completion_set_minimum_key_length(t
->completion
, 1);
7160 gtk_entry_completion_set_inline_selection(t
->completion
, TRUE
);
7161 g_signal_connect(G_OBJECT (t
->completion
), "match-selected",
7162 G_CALLBACK(completion_select_cb
), t
);
7163 g_signal_connect(G_OBJECT (t
->completion
), "cursor-on-match",
7164 G_CALLBACK(completion_hover_cb
), t
);
7172 if (stat(dir
, &sb
)) {
7173 if (mkdir(dir
, S_IRWXU
) == -1)
7174 err(1, "mkdir %s", dir
);
7176 err(1, "stat %s", dir
);
7178 if (S_ISDIR(sb
.st_mode
) == 0)
7179 errx(1, "%s not a dir", dir
);
7180 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
7181 warnx("fixing invalid permissions on %s", dir
);
7182 if (chmod(dir
, S_IRWXU
) == -1)
7183 err(1, "chmod %s", dir
);
7191 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
7195 GStaticRecMutex my_gdk_mtx
= G_STATIC_REC_MUTEX_INIT
;
7196 volatile int mtx_depth
;
7200 * The linux flash plugin violates the gdk locking mechanism.
7201 * Work around the issue by using a recursive mutex with some match applied
7202 * to see if we hit a buggy condition.
7204 * The following code is painful so just don't read it. It really doesn't
7205 * make much sense but seems to work.
7210 g_static_rec_mutex_lock(&my_gdk_mtx
);
7213 if (mtx_depth
<= 0) {
7214 /* should not happen */
7215 show_oops(NULL
, "negative mutex locking bug, trying to "
7217 fprintf(stderr
, "negative mutex locking bug, trying to "
7219 g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
7220 g_static_rec_mutex_lock(&my_gdk_mtx
);
7225 if (mtx_depth
!= 1) {
7226 /* decrease mutext depth to 1 */
7228 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7230 } while (mtx_depth
> 1);
7239 /* if mutex depth isn't 1 then something went bad */
7240 if (mtx_depth
!= 1) {
7241 x
= g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
7243 /* should not happen */
7244 show_oops(NULL
, "mutex unlocking bug, trying to "
7246 fprintf(stderr
, "mutex unlocking bug, trying to "
7250 if (mtx_complain
== 0) {
7251 show_oops(NULL
, "buggy mutex implementation detected, "
7252 "work around implemented");
7253 fprintf(stderr
, "buggy mutex implementation detected, "
7254 "work around implemented");
7261 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7265 main(int argc
, char *argv
[])
7268 int c
, s
, optn
= 0, opte
= 0, focus
= 1;
7269 char conf
[PATH_MAX
] = { '\0' };
7270 char file
[PATH_MAX
];
7271 char *env_proxy
= NULL
;
7275 struct sigaction sact
;
7276 GIOChannel
*channel
;
7283 g_thread_init(NULL
);
7284 gdk_threads_set_lock_functions(mtx_lock
, mtx_unlock
);
7286 gdk_threads_enter();
7288 gcry_control (GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
7290 gtk_init(&argc
, &argv
);
7292 gnutls_global_init();
7294 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
7299 RB_INIT(&downloads
);
7301 TAILQ_INIT(&sessions
);
7304 TAILQ_INIT(&aliases
);
7311 /* fiddle with ulimits */
7312 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7315 /* just use them all */
7316 rlp
.rlim_cur
= rlp
.rlim_max
;
7317 if (setrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7319 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7321 else if (rlp
.rlim_cur
<= 256)
7322 startpage_add("%s requires at least 256 file "
7323 "descriptors, currently it has up to %d available",
7324 __progname
, rlp
.rlim_cur
);
7327 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
7336 #ifdef XXXTERM_BUILDSTR
7337 errx(0 , "Version: %s Build: %s",
7338 version
, XXXTERM_BUILDSTR
);
7340 errx(0 , "Version: %s", version
);
7344 strlcpy(conf
, optarg
, sizeof(conf
));
7347 strlcpy(named_session
, optarg
, sizeof(named_session
));
7368 xtp_generate_keys();
7371 bzero(&sact
, sizeof(sact
));
7372 sigemptyset(&sact
.sa_mask
);
7373 sact
.sa_handler
= sigchild
;
7374 sact
.sa_flags
= SA_NOCLDSTOP
;
7375 sigaction(SIGCHLD
, &sact
, NULL
);
7377 /* set download dir */
7378 pwd
= getpwuid(getuid());
7380 errx(1, "invalid user %d", getuid());
7381 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
7383 /* compile buffer command regexes */
7386 /* set default string settings */
7387 home
= g_strdup("https://www.cyphertite.com");
7388 search_string
= g_strdup("https://ssl.scroogle.org/cgi-bin/nbbwssl.cgi?Gw=%s");
7389 resource_dir
= g_strdup("/usr/local/share/xxxterm/");
7390 strlcpy(runtime_settings
, "runtime", sizeof runtime_settings
);
7391 cmd_font_name
= g_strdup("monospace normal 9");
7392 oops_font_name
= g_strdup("monospace normal 9");
7393 statusbar_font_name
= g_strdup("monospace normal 9");
7394 tabbar_font_name
= g_strdup("monospace normal 9");
7395 statusbar_elems
= g_strdup("BP");
7396 encoding
= g_strdup("UTF-8");
7398 /* read config file */
7399 if (strlen(conf
) == 0)
7400 snprintf(conf
, sizeof conf
, "%s/.%s",
7401 pwd
->pw_dir
, XT_CONF_FILE
);
7402 config_parse(conf
, 0);
7405 cmd_font
= pango_font_description_from_string(cmd_font_name
);
7406 oops_font
= pango_font_description_from_string(oops_font_name
);
7407 statusbar_font
= pango_font_description_from_string(statusbar_font_name
);
7408 tabbar_font
= pango_font_description_from_string(tabbar_font_name
);
7410 /* working directory */
7411 if (strlen(work_dir
) == 0)
7412 snprintf(work_dir
, sizeof work_dir
, "%s/%s",
7413 pwd
->pw_dir
, XT_DIR
);
7416 /* icon cache dir */
7417 snprintf(cache_dir
, sizeof cache_dir
, "%s/%s", work_dir
, XT_CACHE_DIR
);
7421 snprintf(certs_dir
, sizeof certs_dir
, "%s/%s", work_dir
, XT_CERT_DIR
);
7425 snprintf(sessions_dir
, sizeof sessions_dir
, "%s/%s",
7426 work_dir
, XT_SESSIONS_DIR
);
7427 xxx_dir(sessions_dir
);
7430 snprintf(js_dir
, sizeof js_dir
, "%s/%s", work_dir
, XT_JS_DIR
);
7433 /* runtime settings that can override config file */
7434 if (runtime_settings
[0] != '\0')
7435 config_parse(runtime_settings
, 1);
7438 if (!strcmp(download_dir
, pwd
->pw_dir
))
7439 strlcat(download_dir
, "/downloads", sizeof download_dir
);
7440 xxx_dir(download_dir
);
7442 /* favorites file */
7443 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_FAVS_FILE
);
7444 if (stat(file
, &sb
)) {
7445 warnx("favorites file doesn't exist, creating it");
7446 if ((f
= fopen(file
, "w")) == NULL
)
7447 err(1, "favorites");
7451 /* quickmarks file */
7452 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
7453 if (stat(file
, &sb
)) {
7454 warnx("quickmarks file doesn't exist, creating it");
7455 if ((f
= fopen(file
, "w")) == NULL
)
7456 err(1, "quickmarks");
7460 /* search history */
7461 if (history_autosave
) {
7462 snprintf(search_file
, sizeof search_file
, "%s/%s",
7463 work_dir
, XT_SEARCH_FILE
);
7464 if (stat(search_file
, &sb
)) {
7465 warnx("search history file doesn't exist, creating it");
7466 if ((f
= fopen(search_file
, "w")) == NULL
)
7467 err(1, "search_history");
7470 history_read(&shl
, search_file
, &search_history_count
);
7473 /* command history */
7474 if (history_autosave
) {
7475 snprintf(command_file
, sizeof command_file
, "%s/%s",
7476 work_dir
, XT_COMMAND_FILE
);
7477 if (stat(command_file
, &sb
)) {
7478 warnx("command history file doesn't exist, creating it");
7479 if ((f
= fopen(command_file
, "w")) == NULL
)
7480 err(1, "command_history");
7483 history_read(&chl
, command_file
, &cmd_history_count
);
7487 session
= webkit_get_default_session();
7492 if (stat(ssl_ca_file
, &sb
)) {
7493 warnx("no CA file: %s", ssl_ca_file
);
7494 g_free(ssl_ca_file
);
7497 g_object_set(session
,
7498 SOUP_SESSION_SSL_CA_FILE
, ssl_ca_file
,
7499 SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
7503 /* guess_search regex */
7504 if (url_regex
== NULL
)
7505 url_regex
= g_strdup(XT_URL_REGEX
);
7507 if (regcomp(&url_re
, url_regex
, REG_EXTENDED
| REG_NOSUB
))
7508 startpage_add("invalid url regex %s", url_regex
);
7511 env_proxy
= getenv("http_proxy");
7513 setup_proxy(env_proxy
);
7515 env_proxy
= getenv("HTTP_PROXY");
7517 setup_proxy(env_proxy
);
7519 setup_proxy(http_proxy
);
7523 send_cmd_to_socket(argv
[0]);
7527 /* set some connection parameters */
7528 g_object_set(session
, "max-conns", max_connections
, (char *)NULL
);
7529 g_object_set(session
, "max-conns-per-host", max_host_connections
,
7532 /* see if there is already an xxxterm running */
7533 if (single_instance
&& is_running()) {
7535 warnx("already running");
7540 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
7541 send_cmd_to_socket(cmd
);
7551 /* uri completion */
7552 completion_model
= gtk_list_store_new(1, G_TYPE_STRING
);
7555 buffers_store
= gtk_list_store_new
7556 (NUM_COLS
, G_TYPE_UINT
, G_TYPE_STRING
);
7562 notebook_tab_set_visibility();
7564 if (save_global_history
)
7565 restore_global_history();
7567 /* restore session list */
7568 restore_sessions_list();
7570 if (!strcmp(named_session
, XT_SAVED_TABS_FILE
))
7571 restore_saved_tabs();
7573 a
.s
= named_session
;
7574 a
.i
= XT_SES_DONOTHING
;
7575 open_tabs(NULL
, &a
);
7578 /* see if we have an exception */
7579 if (!TAILQ_EMPTY(&spl
)) {
7580 create_new_tab("about:startpage", NULL
, focus
, -1);
7585 create_new_tab(argv
[0], NULL
, focus
, -1);
7592 if (TAILQ_EMPTY(&tabs
))
7593 create_new_tab(home
, NULL
, 1, -1);
7596 if ((s
= build_socket()) != -1) {
7597 channel
= g_io_channel_unix_new(s
);
7598 g_io_add_watch(channel
, G_IO_IN
, socket_watcher
, NULL
);
7604 gdk_threads_leave();
7605 g_static_rec_mutex_unlock_full(&my_gdk_mtx
); /* just in case */
7608 gnutls_global_deinit();