2 * Copyright (c) 2010, 2011 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2011 Stevan Andjelkovic <stevan@student.chalmers.se>
4 * Copyright (c) 2010, 2011 Edd Barrett <vext01@gmail.com>
5 * Copyright (c) 2011 Todd T. Fries <todd@fries.net>
6 * Copyright (c) 2011 Raphael Graf <r@undefined.ch>
7 * Copyright (c) 2011 Michal Mazurek <akfaew@jasminek.net>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 char *version
= XXXTERM_VERSION
;
27 u_int32_t swm_debug
= 0
48 GCRY_THREAD_OPTION_PTHREAD_IMPL
;
60 TAILQ_ENTRY(session
) entry
;
63 TAILQ_HEAD(session_list
, session
);
66 TAILQ_ENTRY(undo
) entry
;
69 int back
; /* Keeps track of how many back
70 * history items there are. */
72 TAILQ_HEAD(undo_tailq
, undo
);
74 struct command_entry
{
76 TAILQ_ENTRY(command_entry
) entry
;
78 TAILQ_HEAD(command_list
, command_entry
);
81 #define XT_DIR (".xxxterm")
82 #define XT_CACHE_DIR ("cache")
83 #define XT_CERT_DIR ("certs/")
84 #define XT_SESSIONS_DIR ("sessions/")
85 #define XT_CONF_FILE ("xxxterm.conf")
86 #define XT_QMARKS_FILE ("quickmarks")
87 #define XT_SAVED_TABS_FILE ("main_session")
88 #define XT_RESTART_TABS_FILE ("restart_tabs")
89 #define XT_SOCKET_FILE ("socket")
90 #define XT_HISTORY_FILE ("history")
91 #define XT_SAVE_SESSION_ID ("SESSION_NAME=")
92 #define XT_SEARCH_FILE ("search_history")
93 #define XT_COMMAND_FILE ("command_history")
94 #define XT_DLMAN_REFRESH "10"
95 #define XT_MAX_URL_LENGTH (4096) /* 1 page is atomic, don't make bigger */
96 #define XT_MAX_UNDO_CLOSE_TAB (32)
97 #define XT_RESERVED_CHARS "$&+,/:;=?@ \"<>#%%{}|^~[]`"
98 #define XT_PRINT_EXTRA_MARGIN 10
99 #define XT_URL_REGEX ("^[[:blank:]]*[^[:blank:]]*([[:alnum:]-]+\\.)+[[:alnum:]-][^[:blank:]]*[[:blank:]]*$")
100 #define XT_INVALID_MARK (-1) /* XXX this is a double, maybe use something else, like a nan */
103 #define XT_COLOR_RED "#cc0000"
104 #define XT_COLOR_YELLOW "#ffff66"
105 #define XT_COLOR_BLUE "lightblue"
106 #define XT_COLOR_GREEN "#99ff66"
107 #define XT_COLOR_WHITE "white"
108 #define XT_COLOR_BLACK "black"
110 #define XT_COLOR_CT_BACKGROUND "#000000"
111 #define XT_COLOR_CT_INACTIVE "#dddddd"
112 #define XT_COLOR_CT_ACTIVE "#bbbb00"
113 #define XT_COLOR_CT_SEPARATOR "#555555"
115 #define XT_COLOR_SB_SEPARATOR "#555555"
117 #define XT_PROTO_DELIM "://"
120 #define XT_MOVE_INVALID (0)
121 #define XT_MOVE_DOWN (1)
122 #define XT_MOVE_UP (2)
123 #define XT_MOVE_BOTTOM (3)
124 #define XT_MOVE_TOP (4)
125 #define XT_MOVE_PAGEDOWN (5)
126 #define XT_MOVE_PAGEUP (6)
127 #define XT_MOVE_HALFDOWN (7)
128 #define XT_MOVE_HALFUP (8)
129 #define XT_MOVE_LEFT (9)
130 #define XT_MOVE_FARLEFT (10)
131 #define XT_MOVE_RIGHT (11)
132 #define XT_MOVE_FARRIGHT (12)
133 #define XT_MOVE_PERCENT (13)
134 #define XT_MOVE_CENTER (14)
136 #define XT_QMARK_SET (0)
137 #define XT_QMARK_OPEN (1)
138 #define XT_QMARK_TAB (2)
140 #define XT_MARK_SET (0)
141 #define XT_MARK_GOTO (1)
143 #define XT_TAB_LAST (-4)
144 #define XT_TAB_FIRST (-3)
145 #define XT_TAB_PREV (-2)
146 #define XT_TAB_NEXT (-1)
147 #define XT_TAB_INVALID (0)
148 #define XT_TAB_NEW (1)
149 #define XT_TAB_DELETE (2)
150 #define XT_TAB_DELQUIT (3)
151 #define XT_TAB_OPEN (4)
152 #define XT_TAB_UNDO_CLOSE (5)
153 #define XT_TAB_SHOW (6)
154 #define XT_TAB_HIDE (7)
155 #define XT_TAB_NEXTSTYLE (8)
157 #define XT_NAV_INVALID (0)
158 #define XT_NAV_BACK (1)
159 #define XT_NAV_FORWARD (2)
160 #define XT_NAV_RELOAD (3)
162 #define XT_FOCUS_INVALID (0)
163 #define XT_FOCUS_URI (1)
164 #define XT_FOCUS_SEARCH (2)
166 #define XT_SEARCH_INVALID (0)
167 #define XT_SEARCH_NEXT (1)
168 #define XT_SEARCH_PREV (2)
170 #define XT_PASTE_CURRENT_TAB (0)
171 #define XT_PASTE_NEW_TAB (1)
173 #define XT_ZOOM_IN (-1)
174 #define XT_ZOOM_OUT (-2)
175 #define XT_ZOOM_NORMAL (100)
177 #define XT_URL_SHOW (1)
178 #define XT_URL_HIDE (2)
180 #define XT_CMD_OPEN (0)
181 #define XT_CMD_OPEN_CURRENT (1)
182 #define XT_CMD_TABNEW (2)
183 #define XT_CMD_TABNEW_CURRENT (3)
185 #define XT_STATUS_NOTHING (0)
186 #define XT_STATUS_LINK (1)
187 #define XT_STATUS_URI (2)
188 #define XT_STATUS_LOADING (3)
190 #define XT_SES_DONOTHING (0)
191 #define XT_SES_CLOSETABS (1)
193 #define XT_PREFIX (1<<0)
194 #define XT_USERARG (1<<1)
195 #define XT_URLARG (1<<2)
196 #define XT_INTARG (1<<3)
197 #define XT_SESSARG (1<<4)
198 #define XT_SETARG (1<<5)
200 #define XT_HINT_NEWTAB (1<<0)
202 #define XT_MODE_INSERT (0)
203 #define XT_MODE_COMMAND (1)
205 #define XT_BUFCMD_SZ (8)
207 #define XT_EJS_SHOW (1<<0)
209 GtkWidget
* create_button(char *, char *, int);
211 void recalc_tabs(void);
212 void recolor_compact_tabs(void);
213 void set_current_tab(int page_num
);
214 gboolean
update_statusbar_position(GtkAdjustment
*, gpointer
);
215 void marks_clear(struct tab
*t
);
218 extern char *__progname
;
221 GtkWidget
*main_window
;
222 GtkNotebook
*notebook
;
224 GtkWidget
*arrow
, *abtn
;
225 struct tab_list tabs
;
226 struct history_list hl
;
227 struct session_list sessions
;
228 struct domain_list c_wl
;
229 struct domain_list js_wl
;
230 struct domain_list pl_wl
;
231 struct undo_tailq undos
;
232 struct keybinding_list kbl
;
234 struct command_list chl
;
235 struct command_list shl
;
236 struct command_entry
*history_at
;
237 struct command_entry
*search_at
;
239 int cmd_history_count
= 0;
240 int search_history_count
= 0;
242 long long unsigned int blocked_cookies
= 0;
243 char named_session
[PATH_MAX
];
244 GtkListStore
*completion_model
;
245 GtkListStore
*buffers_store
;
247 char *qmarks
[XT_NOMARKS
];
248 int btn_down
; /* M1 down in any wv */
249 regex_t url_re
; /* guess_search regex */
251 /* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
252 int next_download_id
= 1;
254 void xxx_dir(char *);
255 int icon_size_map(int);
256 void completion_add(struct tab
*);
257 void completion_add_uri(const gchar
*);
260 history_delete(struct command_list
*l
, int *counter
)
262 struct command_entry
*c
;
264 if (l
== NULL
|| counter
== NULL
)
267 c
= TAILQ_LAST(l
, command_list
);
271 TAILQ_REMOVE(l
, c
, entry
);
278 history_add(struct command_list
*list
, char *file
, char *l
, int *counter
)
280 struct command_entry
*c
;
283 if (list
== NULL
|| l
== NULL
|| counter
== NULL
)
286 /* don't add the same line */
287 c
= TAILQ_FIRST(list
);
289 if (!strcmp(c
->line
+ 1 /* skip space */, l
))
292 c
= g_malloc0(sizeof *c
);
293 c
->line
= g_strdup_printf(" %s", l
);
296 TAILQ_INSERT_HEAD(list
, c
, entry
);
299 history_delete(list
, counter
);
301 if (history_autosave
&& file
) {
302 f
= fopen(file
, "w");
304 show_oops(NULL
, "couldn't write history %s", file
);
308 TAILQ_FOREACH_REVERSE(c
, list
, command_list
, entry
) {
310 fprintf(f
, "%s\n", c
->line
);
318 history_read(struct command_list
*list
, char *file
, int *counter
)
321 char *s
, line
[65536];
323 if (list
== NULL
|| file
== NULL
)
326 f
= fopen(file
, "r");
328 startpage_add("couldn't open history file %s", file
);
333 s
= fgets(line
, sizeof line
, f
);
334 if (s
== NULL
|| feof(f
) || ferror(f
))
336 if ((s
= strchr(line
, '\n')) == NULL
) {
337 startpage_add("invalid history file %s", file
);
343 history_add(list
, NULL
, line
+ 1, counter
);
351 /* marks and quickmarks array storage.
352 * first a-z, then A-Z, then 0-9 */
359 if (i
>= 0 && i
<= 'z' - 'a')
363 if (i
>= 0 && i
<= 'Z' - 'A')
378 if (m
>= 'a' && m
<= 'z')
379 return ret
+ m
- 'a';
381 ret
+= 'z' - 'a' + 1;
382 if (m
>= 'A' && m
<= 'Z')
383 return ret
+ m
- 'A';
385 ret
+= 'Z' - 'A' + 1;
386 if (m
>= '0' && m
<= '9')
387 return ret
+ m
- '0';
396 int saved_errno
, status
;
401 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
405 if (errno
!= ECHILD
) {
407 clog_warn("sigchild: waitpid:");
413 if (WIFEXITED(status
)) {
414 if (WEXITSTATUS(status
) != 0) {
416 clog_warnx("sigchild: child exit status: %d",
417 WEXITSTATUS(status));
422 clog_warnx("sigchild: child is terminated abnormally");
431 is_g_object_setting(GObject
*o
, char *str
)
433 guint n_props
= 0, i
;
434 GParamSpec
**proplist
;
436 if (! G_IS_OBJECT(o
))
439 proplist
= g_object_class_list_properties(G_OBJECT_GET_CLASS(o
),
442 for (i
=0; i
< n_props
; i
++) {
443 if (! strcmp(proplist
[i
]->name
, str
))
450 get_current_tab(void)
454 TAILQ_FOREACH(t
, &tabs
, entry
) {
455 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
459 warnx("%s: no current tab", __func__
);
465 set_status(struct tab
*t
, gchar
*s
, int status
)
473 case XT_STATUS_LOADING
:
474 type
= g_strdup_printf("Loading: %s", s
);
478 type
= g_strdup_printf("Link: %s", s
);
480 t
->status
= g_strdup(gtk_entry_get_text(
481 GTK_ENTRY(t
->sbe
.statusbar
)));
485 type
= g_strdup_printf("%s", s
);
487 t
->status
= g_strdup(type
);
491 t
->status
= g_strdup(s
);
493 case XT_STATUS_NOTHING
:
498 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.statusbar
), s
);
504 hide_cmd(struct tab
*t
)
506 history_at
= NULL
; /* just in case */
507 search_at
= NULL
; /* just in case */
508 gtk_widget_hide(t
->cmd
);
512 show_cmd(struct tab
*t
)
516 gtk_widget_hide(t
->oops
);
517 gtk_widget_show(t
->cmd
);
521 hide_buffers(struct tab
*t
)
523 gtk_widget_hide(t
->buffers
);
524 gtk_list_store_clear(buffers_store
);
534 sort_tabs_by_page_num(struct tab
***stabs
)
539 num_tabs
= gtk_notebook_get_n_pages(notebook
);
541 *stabs
= g_malloc0(num_tabs
* sizeof(struct tab
*));
543 TAILQ_FOREACH(t
, &tabs
, entry
)
544 (*stabs
)[gtk_notebook_page_num(notebook
, t
->vbox
)] = t
;
550 buffers_make_list(void)
553 const gchar
*title
= NULL
;
555 struct tab
**stabs
= NULL
;
557 num_tabs
= sort_tabs_by_page_num(&stabs
);
559 for (i
= 0; i
< num_tabs
; i
++)
561 gtk_list_store_append(buffers_store
, &iter
);
562 title
= get_title(stabs
[i
], FALSE
);
563 gtk_list_store_set(buffers_store
, &iter
,
564 COL_ID
, i
+ 1, /* Enumerate the tabs starting from 1
574 show_buffers(struct tab
*t
)
577 gtk_widget_show(t
->buffers
);
578 gtk_widget_grab_focus(GTK_WIDGET(t
->buffers
));
582 toggle_buffers(struct tab
*t
)
584 if (gtk_widget_get_visible(t
->buffers
))
591 buffers(struct tab
*t
, struct karg
*args
)
599 hide_oops(struct tab
*t
)
601 gtk_widget_hide(t
->oops
);
605 show_oops(struct tab
*at
, const char *fmt
, ...)
609 struct tab
*t
= NULL
;
615 if ((t
= get_current_tab()) == NULL
)
621 if (vasprintf(&msg
, fmt
, ap
) == -1)
622 errx(1, "show_oops failed");
625 gtk_entry_set_text(GTK_ENTRY(t
->oops
), msg
);
626 gtk_widget_hide(t
->cmd
);
627 gtk_widget_show(t
->oops
);
633 char work_dir
[PATH_MAX
];
634 char certs_dir
[PATH_MAX
];
635 char cache_dir
[PATH_MAX
];
636 char sessions_dir
[PATH_MAX
];
637 char cookie_file
[PATH_MAX
];
638 SoupSession
*session
;
639 SoupCookieJar
*s_cookiejar
;
640 SoupCookieJar
*p_cookiejar
;
641 char rc_fname
[PATH_MAX
];
643 struct mime_type_list mtl
;
644 struct alias_list aliases
;
647 struct tab
*create_new_tab(char *, struct undo
*, int, int);
648 void delete_tab(struct tab
*);
649 void setzoom_webkit(struct tab
*, int);
650 int run_script(struct tab
*, char *);
651 int download_rb_cmp(struct download
*, struct download
*);
652 gboolean
cmd_execute(struct tab
*t
, char *str
);
655 history_rb_cmp(struct history
*h1
, struct history
*h2
)
657 return (strcmp(h1
->uri
, h2
->uri
));
659 RB_GENERATE(history_list
, history
, entry
, history_rb_cmp
);
662 domain_rb_cmp(struct domain
*d1
, struct domain
*d2
)
664 return (strcmp(d1
->d
, d2
->d
));
666 RB_GENERATE(domain_list
, domain
, entry
, domain_rb_cmp
);
669 download_rb_cmp(struct download
*e1
, struct download
*e2
)
671 return (e1
->id
< e2
->id
? -1 : e1
->id
> e2
->id
);
673 RB_GENERATE(download_list
, download
, entry
, download_rb_cmp
);
675 struct valid_url_types
{
686 valid_url_type(char *url
)
690 for (i
= 0; i
< LENGTH(vut
); i
++)
691 if (!strncasecmp(vut
[i
].type
, url
, strlen(vut
[i
].type
)))
698 match_alias(char *url_in
)
702 char *url_out
= NULL
, *search
, *enc_arg
;
704 search
= g_strdup(url_in
);
706 if (strsep(&arg
, " \t") == NULL
) {
707 show_oops(NULL
, "match_alias: NULL URL");
711 TAILQ_FOREACH(a
, &aliases
, entry
) {
712 if (!strcmp(search
, a
->a_name
))
717 DNPRINTF(XT_D_URL
, "match_alias: matched alias %s\n",
720 enc_arg
= soup_uri_encode(arg
, XT_RESERVED_CHARS
);
721 url_out
= g_strdup_printf(a
->a_uri
, enc_arg
);
724 url_out
= g_strdup_printf(a
->a_uri
, "");
732 guess_url_type(char *url_in
)
735 char *url_out
= NULL
, *enc_search
= NULL
;
738 /* substitute aliases */
739 url_out
= match_alias(url_in
);
743 /* see if we are an about page */
744 if (!strncmp(url_in
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
))
745 for (i
= 0; i
< about_list_size(); i
++)
746 if (!strcmp(&url_in
[XT_URI_ABOUT_LEN
],
747 about_list
[i
].name
)) {
748 url_out
= g_strdup(url_in
);
752 if (guess_search
&& url_regex
&&
753 !(g_str_has_prefix(url_in
, "http://") ||
754 g_str_has_prefix(url_in
, "https://"))) {
755 if (regexec(&url_re
, url_in
, 0, NULL
, 0)) {
756 /* invalid URI so search instead */
757 enc_search
= soup_uri_encode(url_in
, XT_RESERVED_CHARS
);
758 url_out
= g_strdup_printf(search_string
, enc_search
);
764 /* XXX not sure about this heuristic */
765 if (stat(url_in
, &sb
) == 0)
766 url_out
= g_strdup_printf("file://%s", url_in
);
768 url_out
= g_strdup_printf("http://%s", url_in
); /* guess http */
770 DNPRINTF(XT_D_URL
, "guess_url_type: guessed %s\n", url_out
);
776 load_uri(struct tab
*t
, gchar
*uri
)
779 gchar
*newuri
= NULL
;
785 /* Strip leading spaces. */
786 while (*uri
&& isspace(*uri
))
789 if (strlen(uri
) == 0) {
794 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
796 if (valid_url_type(uri
)) {
797 newuri
= guess_url_type(uri
);
801 if (!strncmp(uri
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
)) {
802 for (i
= 0; i
< about_list_size(); i
++)
803 if (!strcmp(&uri
[XT_URI_ABOUT_LEN
], about_list
[i
].name
)) {
804 bzero(&args
, sizeof args
);
805 about_list
[i
].func(t
, &args
);
806 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
),
810 show_oops(t
, "invalid about page");
814 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
816 webkit_web_view_load_uri(t
->wv
, uri
);
823 get_uri(struct tab
*t
)
825 const gchar
*uri
= NULL
;
827 if (webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
)
829 if (t
->xtp_meaning
== XT_XTP_TAB_MEANING_NORMAL
) {
830 uri
= webkit_web_view_get_uri(t
->wv
);
832 /* use tmp_uri to make sure it is g_freed */
835 t
->tmp_uri
=g_strdup_printf("%s%s", XT_URI_ABOUT
,
836 about_list
[t
->xtp_meaning
].name
);
843 get_title(struct tab
*t
, bool window
)
845 const gchar
*set
= NULL
, *title
= NULL
;
846 WebKitLoadStatus status
= webkit_web_view_get_load_status(t
->wv
);
848 if (status
== WEBKIT_LOAD_PROVISIONAL
|| status
== WEBKIT_LOAD_FAILED
||
849 t
->xtp_meaning
== XT_XTP_TAB_MEANING_BL
)
852 title
= webkit_web_view_get_title(t
->wv
);
853 if ((set
= title
? title
: get_uri(t
)))
857 set
= window
? XT_NAME
: "(untitled)";
863 find_mime_type(char *mime_type
)
865 struct mime_type
*m
, *def
= NULL
, *rv
= NULL
;
867 TAILQ_FOREACH(m
, &mtl
, entry
) {
869 !strncmp(mime_type
, m
->mt_type
, strlen(m
->mt_type
)))
872 if (m
->mt_default
== 0 && !strcmp(mime_type
, m
->mt_type
)) {
885 wl_find_uri(const gchar
*s
, struct domain_list
*wl
)
891 if (s
== NULL
|| wl
== NULL
)
894 if (!strncmp(s
, "http://", strlen("http://")))
895 s
= &s
[strlen("http://")];
896 else if (!strncmp(s
, "https://", strlen("https://")))
897 s
= &s
[strlen("https://")];
902 for (i
= 0; i
< strlen(s
) + 1 /* yes er need this */; i
++)
903 /* chop string at first slash */
904 if (s
[i
] == '/' || s
[i
] == ':' || s
[i
] == '\0') {
916 js_ref_to_string(JSContextRef context
, JSValueRef ref
)
922 jsref
= JSValueToStringCopy(context
, ref
, NULL
);
926 l
= JSStringGetMaximumUTF8CStringSize(jsref
);
929 JSStringGetUTF8CString(jsref
, s
, l
);
930 JSStringRelease(jsref
);
936 disable_hints(struct tab
*t
)
938 DNPRINTF(XT_D_JS
, "%s\n", __func__
);
940 run_script(t
, "hints.clearHints();");
946 enable_hints(struct tab
*t
)
948 DNPRINTF(XT_D_JS
, "%s\n", __func__
);
950 run_script(t
, JS_HINTING
);
952 run_script(t
, "hints.createHints('', 'F');");
954 run_script(t
, "hints.createHints('', 'f');");
958 #define XT_JS_DONE ("done;")
959 #define XT_JS_DONE_LEN (strlen(XT_JS_DONE))
960 #define XT_JS_INSERT ("insert;")
961 #define XT_JS_INSERT_LEN (strlen(XT_JS_INSERT))
964 run_script(struct tab
*t
, char *s
)
966 JSGlobalContextRef ctx
;
967 WebKitWebFrame
*frame
;
969 JSValueRef val
, exception
;
972 DNPRINTF(XT_D_JS
, "run_script: tab %d %s\n",
973 t
->tab_id
, s
== (char *)JS_HINTING
? "JS_HINTING" : s
);
975 /* a bit silly but it prevents some heartburn */
976 if (t
->script_init
== 0) {
978 run_script(t
, JS_HINTING
);
981 frame
= webkit_web_view_get_main_frame(t
->wv
);
982 ctx
= webkit_web_frame_get_global_context(frame
);
984 str
= JSStringCreateWithUTF8CString(s
);
985 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
986 NULL
, 0, &exception
);
987 JSStringRelease(str
);
989 DNPRINTF(XT_D_JS
, "run_script: val %p\n", val
);
991 es
= js_ref_to_string(ctx
, exception
);
993 /* show_oops(t, "script exception: %s", es); */
994 DNPRINTF(XT_D_JS
, "run_script: exception %s\n", es
);
999 es
= js_ref_to_string(ctx
, val
);
1002 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
1004 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
1008 DNPRINTF(XT_D_JS
, "run_script: val %s\n", es
);
1017 hint(struct tab
*t
, struct karg
*args
)
1020 DNPRINTF(XT_D_JS
, "hint: tab %d args %d\n", t
->tab_id
, args
->i
);
1022 if (t
->hints_on
== 0) {
1023 if (args
->i
== XT_HINT_NEWTAB
)
1033 apply_style(struct tab
*t
)
1035 g_object_set(G_OBJECT(t
->settings
),
1036 "user-stylesheet-uri", t
->stylesheet
, (char *)NULL
);
1040 userstyle(struct tab
*t
, struct karg
*args
)
1042 DNPRINTF(XT_D_JS
, "userstyle: tab %d\n", t
->tab_id
);
1046 g_object_set(G_OBJECT(t
->settings
),
1047 "user-stylesheet-uri", NULL
, (char *)NULL
);
1056 * Doesn't work fully, due to the following bug:
1057 * https://bugs.webkit.org/show_bug.cgi?id=51747
1060 restore_global_history(void)
1062 char file
[PATH_MAX
];
1067 const char delim
[3] = {'\\', '\\', '\0'};
1069 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_HISTORY_FILE
);
1071 if ((f
= fopen(file
, "r")) == NULL
) {
1072 warnx("%s: fopen", __func__
);
1077 if ((uri
= fparseln(f
, NULL
, NULL
, delim
, 0)) == NULL
)
1078 if (feof(f
) || ferror(f
))
1081 if ((title
= fparseln(f
, NULL
, NULL
, delim
, 0)) == NULL
)
1082 if (feof(f
) || ferror(f
)) {
1084 warnx("%s: broken history file\n", __func__
);
1088 if (uri
&& strlen(uri
) && title
&& strlen(title
)) {
1089 webkit_web_history_item_new_with_data(uri
, title
);
1090 h
= g_malloc(sizeof(struct history
));
1091 h
->uri
= g_strdup(uri
);
1092 h
->title
= g_strdup(title
);
1093 RB_INSERT(history_list
, &hl
, h
);
1094 completion_add_uri(h
->uri
);
1096 warnx("%s: failed to restore history\n", __func__
);
1112 save_global_history_to_disk(struct tab
*t
)
1114 char file
[PATH_MAX
];
1118 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_HISTORY_FILE
);
1120 if ((f
= fopen(file
, "w")) == NULL
) {
1121 show_oops(t
, "%s: global history file: %s",
1122 __func__
, strerror(errno
));
1126 RB_FOREACH_REVERSE(h
, history_list
, &hl
) {
1127 if (h
->uri
&& h
->title
)
1128 fprintf(f
, "%s\n%s\n", h
->uri
, h
->title
);
1137 quit(struct tab
*t
, struct karg
*args
)
1139 if (save_global_history
)
1140 save_global_history_to_disk(t
);
1148 restore_sessions_list(void)
1151 struct dirent
*dp
= NULL
;
1154 sdir
= opendir(sessions_dir
);
1156 while ((dp
= readdir(sdir
)) != NULL
)
1157 if (dp
->d_type
== DT_REG
) {
1158 s
= g_malloc(sizeof(struct session
));
1159 s
->name
= g_strdup(dp
->d_name
);
1160 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
1167 open_tabs(struct tab
*t
, struct karg
*a
)
1169 char file
[PATH_MAX
];
1173 struct tab
*ti
, *tt
;
1178 ti
= TAILQ_LAST(&tabs
, tab_list
);
1180 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, a
->s
);
1181 if ((f
= fopen(file
, "r")) == NULL
)
1185 if ((uri
= fparseln(f
, NULL
, NULL
, "\0\0\0", 0)) == NULL
)
1186 if (feof(f
) || ferror(f
))
1189 /* retrieve session name */
1190 if (uri
&& g_str_has_prefix(uri
, XT_SAVE_SESSION_ID
)) {
1191 strlcpy(named_session
,
1192 &uri
[strlen(XT_SAVE_SESSION_ID
)],
1193 sizeof named_session
);
1197 if (uri
&& strlen(uri
))
1198 create_new_tab(uri
, NULL
, 1, -1);
1204 /* close open tabs */
1205 if (a
->i
== XT_SES_CLOSETABS
&& ti
!= NULL
) {
1207 tt
= TAILQ_FIRST(&tabs
);
1228 restore_saved_tabs(void)
1230 char file
[PATH_MAX
];
1231 int unlink_file
= 0;
1236 snprintf(file
, sizeof file
, "%s/%s",
1237 sessions_dir
, XT_RESTART_TABS_FILE
);
1238 if (stat(file
, &sb
) == -1)
1239 a
.s
= XT_SAVED_TABS_FILE
;
1242 a
.s
= XT_RESTART_TABS_FILE
;
1245 a
.i
= XT_SES_DONOTHING
;
1246 rv
= open_tabs(NULL
, &a
);
1255 save_tabs(struct tab
*t
, struct karg
*a
)
1257 char file
[PATH_MAX
];
1259 int num_tabs
= 0, i
;
1260 struct tab
**stabs
= NULL
;
1262 /* tab may be null here */
1267 snprintf(file
, sizeof file
, "%s/%s",
1268 sessions_dir
, named_session
);
1270 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, a
->s
);
1272 if ((f
= fopen(file
, "w")) == NULL
) {
1273 show_oops(t
, "Can't open save_tabs file: %s", strerror(errno
));
1277 /* save session name */
1278 fprintf(f
, "%s%s\n", XT_SAVE_SESSION_ID
, named_session
);
1280 /* Save tabs, in the order they are arranged in the notebook. */
1281 num_tabs
= sort_tabs_by_page_num(&stabs
);
1283 for (i
= 0; i
< num_tabs
; i
++)
1285 if (get_uri(stabs
[i
]) != NULL
)
1286 fprintf(f
, "%s\n", get_uri(stabs
[i
]));
1287 else if (gtk_entry_get_text(GTK_ENTRY(
1288 stabs
[i
]->uri_entry
)))
1289 fprintf(f
, "%s\n", gtk_entry_get_text(GTK_ENTRY(
1290 stabs
[i
]->uri_entry
)));
1295 /* try and make sure this gets to disk NOW. XXX Backup first? */
1296 if (fflush(f
) != 0 || fsync(fileno(f
)) != 0) {
1297 show_oops(t
, "May not have managed to save session: %s",
1307 save_tabs_and_quit(struct tab
*t
, struct karg
*args
)
1319 run_page_script(struct tab
*t
, struct karg
*args
)
1322 char *tmp
, script
[PATH_MAX
];
1324 tmp
= args
->s
!= NULL
&& strlen(args
->s
) > 0 ? args
->s
: default_script
;
1325 if (tmp
[0] == '\0') {
1326 show_oops(t
, "no script specified");
1330 if ((uri
= get_uri(t
)) == NULL
) {
1331 show_oops(t
, "tab is empty, not running script");
1336 snprintf(script
, sizeof script
, "%s/%s",
1337 pwd
->pw_dir
, &tmp
[1]);
1339 strlcpy(script
, tmp
, sizeof script
);
1343 show_oops(t
, "can't fork to run script");
1353 execlp(script
, script
, uri
, (void *)NULL
);
1363 yank_uri(struct tab
*t
, struct karg
*args
)
1366 GtkClipboard
*clipboard
;
1368 if ((uri
= get_uri(t
)) == NULL
)
1371 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1372 gtk_clipboard_set_text(clipboard
, uri
, -1);
1378 paste_uri(struct tab
*t
, struct karg
*args
)
1380 GtkClipboard
*clipboard
;
1381 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
1383 gchar
*p
= NULL
, *uri
;
1385 /* try primary clipboard first */
1386 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1387 p
= gtk_clipboard_wait_for_text(clipboard
);
1389 /* if it failed get whatever text is in cut_buffer0 */
1390 if (p
== NULL
&& xterm_workaround
)
1391 if (gdk_property_get(gdk_get_default_root_window(),
1393 gdk_atom_intern("STRING", FALSE
),
1395 1024 * 1024 /* picked out of my butt */,
1401 /* yes sir, we need to NUL the string */
1407 while (*uri
&& isspace(*uri
))
1409 if (strlen(uri
) == 0) {
1410 show_oops(t
, "empty paste buffer");
1413 if (guess_search
== 0 && valid_url_type(uri
)) {
1414 /* we can be clever and paste this in search box */
1415 show_oops(t
, "not a valid URL");
1419 if (args
->i
== XT_PASTE_CURRENT_TAB
)
1421 else if (args
->i
== XT_PASTE_NEW_TAB
)
1422 create_new_tab(uri
, NULL
, 1, -1);
1433 js_toggle_cb(GtkWidget
*w
, struct tab
*t
)
1438 g_object_get(G_OBJECT(t
->settings
),
1439 "enable-scripts", &es
, (char *)NULL
);
1444 set
= XT_WL_DISABLE
;
1446 a
.i
= set
| XT_WL_TOPLEVEL
;
1449 a
.i
= set
| XT_WL_TOPLEVEL
;
1452 a
.i
= XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
;
1457 toggle_src(struct tab
*t
, struct karg
*args
)
1464 mode
= webkit_web_view_get_view_source_mode(t
->wv
);
1465 webkit_web_view_set_view_source_mode(t
->wv
, !mode
);
1466 webkit_web_view_reload(t
->wv
);
1472 focus_webview(struct tab
*t
)
1477 /* only grab focus if we are visible */
1478 if (gtk_notebook_get_current_page(notebook
) == t
->tab_id
)
1479 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
1483 focus(struct tab
*t
, struct karg
*args
)
1485 if (t
== NULL
|| args
== NULL
)
1491 if (args
->i
== XT_FOCUS_URI
)
1492 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
1493 else if (args
->i
== XT_FOCUS_SEARCH
)
1494 gtk_widget_grab_focus(GTK_WIDGET(t
->search_entry
));
1500 connect_socket_from_uri(const gchar
*uri
, const gchar
**error_str
, char *domain
,
1504 struct addrinfo hints
, *res
= NULL
, *ai
;
1505 int rv
= -1, s
= -1, on
, error
;
1507 static gchar myerror
[256]; /* this is not thread safe */
1510 *error_str
= myerror
;
1511 if (uri
&& !g_str_has_prefix(uri
, "https://")) {
1512 *error_str
= "invalid URI";
1516 su
= soup_uri_new(uri
);
1518 *error_str
= "invalid soup URI";
1521 if (!SOUP_URI_VALID_FOR_HTTP(su
)) {
1522 *error_str
= "invalid HTTPS URI";
1526 snprintf(port
, sizeof port
, "%d", su
->port
);
1527 bzero(&hints
, sizeof(struct addrinfo
));
1528 hints
.ai_flags
= AI_CANONNAME
;
1529 hints
.ai_family
= AF_UNSPEC
;
1530 hints
.ai_socktype
= SOCK_STREAM
;
1532 if ((error
= getaddrinfo(su
->host
, port
, &hints
, &res
))) {
1533 snprintf(myerror
, sizeof myerror
, "getaddrinfo failed: %s",
1534 gai_strerror(errno
));
1538 for (ai
= res
; ai
; ai
= ai
->ai_next
) {
1544 if (ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
)
1546 s
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
1549 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &on
,
1552 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) == 0)
1556 snprintf(myerror
, sizeof myerror
,
1557 "could not obtain certificates from: %s",
1563 strlcpy(domain
, su
->host
, domain_sz
);
1570 if (rv
== -1 && s
!= -1)
1577 stop_tls(gnutls_session_t gsession
, gnutls_certificate_credentials_t xcred
)
1580 gnutls_deinit(gsession
);
1582 gnutls_certificate_free_credentials(xcred
);
1588 start_tls(const gchar
**error_str
, int s
, gnutls_session_t
*gs
,
1589 gnutls_certificate_credentials_t
*xc
)
1591 gnutls_certificate_credentials_t xcred
;
1592 gnutls_session_t gsession
;
1594 static gchar myerror
[1024]; /* this is not thread safe */
1596 if (gs
== NULL
|| xc
== NULL
)
1603 gnutls_certificate_allocate_credentials(&xcred
);
1604 gnutls_certificate_set_x509_trust_file(xcred
, ssl_ca_file
,
1605 GNUTLS_X509_FMT_PEM
);
1607 gnutls_init(&gsession
, GNUTLS_CLIENT
);
1608 gnutls_priority_set_direct(gsession
, "PERFORMANCE", NULL
);
1609 gnutls_credentials_set(gsession
, GNUTLS_CRD_CERTIFICATE
, xcred
);
1610 gnutls_transport_set_ptr(gsession
, (gnutls_transport_ptr_t
)(long)s
);
1611 if ((rv
= gnutls_handshake(gsession
)) < 0) {
1612 snprintf(myerror
, sizeof myerror
,
1613 "gnutls_handshake failed %d fatal %d %s",
1615 gnutls_error_is_fatal(rv
),
1616 gnutls_strerror_name(rv
));
1617 stop_tls(gsession
, xcred
);
1621 gnutls_credentials_type_t cred
;
1622 cred
= gnutls_auth_get_type(gsession
);
1623 if (cred
!= GNUTLS_CRD_CERTIFICATE
) {
1624 snprintf(myerror
, sizeof myerror
,
1625 "gnutls_auth_get_type failed %d",
1627 stop_tls(gsession
, xcred
);
1635 *error_str
= myerror
;
1640 get_connection_certs(gnutls_session_t gsession
, gnutls_x509_crt_t
**certs
,
1644 const gnutls_datum_t
*cl
;
1645 gnutls_x509_crt_t
*all_certs
;
1648 if (certs
== NULL
|| cert_count
== NULL
)
1650 if (gnutls_certificate_type_get(gsession
) != GNUTLS_CRT_X509
)
1652 cl
= gnutls_certificate_get_peers(gsession
, &len
);
1656 all_certs
= g_malloc(sizeof(gnutls_x509_crt_t
) * len
);
1657 for (i
= 0; i
< len
; i
++) {
1658 gnutls_x509_crt_init(&all_certs
[i
]);
1659 if (gnutls_x509_crt_import(all_certs
[i
], &cl
[i
],
1660 GNUTLS_X509_FMT_PEM
< 0)) {
1674 free_connection_certs(gnutls_x509_crt_t
*certs
, size_t cert_count
)
1678 for (i
= 0; i
< cert_count
; i
++)
1679 gnutls_x509_crt_deinit(certs
[i
]);
1684 statusbar_modify_attr(struct tab
*t
, const char *text
, const char *base
)
1686 GdkColor c_text
, c_base
;
1688 gdk_color_parse(text
, &c_text
);
1689 gdk_color_parse(base
, &c_base
);
1691 gtk_widget_modify_text(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_text
);
1692 gtk_widget_modify_text(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_text
);
1693 gtk_widget_modify_text(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_text
);
1694 gtk_widget_modify_text(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_text
);
1696 gtk_widget_modify_base(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_base
);
1697 gtk_widget_modify_base(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_base
);
1698 gtk_widget_modify_base(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_base
);
1699 gtk_widget_modify_base(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_base
);
1703 save_certs(struct tab
*t
, gnutls_x509_crt_t
*certs
,
1704 size_t cert_count
, char *domain
)
1707 char cert_buf
[64 * 1024], file
[PATH_MAX
];
1712 if (t
== NULL
|| certs
== NULL
|| cert_count
<= 0 || domain
== NULL
)
1715 snprintf(file
, sizeof file
, "%s/%s", certs_dir
, domain
);
1716 if ((f
= fopen(file
, "w")) == NULL
) {
1717 show_oops(t
, "Can't create cert file %s %s",
1718 file
, strerror(errno
));
1722 for (i
= 0; i
< cert_count
; i
++) {
1723 cert_buf_sz
= sizeof cert_buf
;
1724 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1725 cert_buf
, &cert_buf_sz
)) {
1726 show_oops(t
, "gnutls_x509_crt_export failed");
1729 if (fwrite(cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1730 show_oops(t
, "Can't write certs: %s", strerror(errno
));
1735 /* not the best spot but oh well */
1736 gdk_color_parse("lightblue", &color
);
1737 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
1738 statusbar_modify_attr(t
, XT_COLOR_BLACK
, "lightblue");
1751 load_compare_cert(const gchar
*uri
, const gchar
**error_str
)
1753 char domain
[8182], file
[PATH_MAX
];
1754 char cert_buf
[64 * 1024], r_cert_buf
[64 * 1024];
1756 unsigned int error
= 0;
1758 size_t cert_buf_sz
, cert_count
;
1759 enum cert_trust rv
= CERT_UNTRUSTED
;
1760 static gchar serr
[80]; /* this isn't thread safe */
1761 gnutls_session_t gsession
;
1762 gnutls_x509_crt_t
*certs
;
1763 gnutls_certificate_credentials_t xcred
;
1765 DNPRINTF(XT_D_URL
, "%s: %s\n", __func__
, uri
);
1769 if ((s
= connect_socket_from_uri(uri
, error_str
, domain
,
1770 sizeof domain
)) == -1)
1773 DNPRINTF(XT_D_URL
, "%s: fd %d\n", __func__
, s
);
1776 if (start_tls(error_str
, s
, &gsession
, &xcred
))
1778 DNPRINTF(XT_D_URL
, "%s: got tls\n", __func__
);
1780 /* verify certs in case cert file doesn't exist */
1781 if (gnutls_certificate_verify_peers2(gsession
, &error
) !=
1783 *error_str
= "Invalid certificates";
1788 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1789 *error_str
= "Can't get connection certificates";
1793 snprintf(file
, sizeof file
, "%s/%s", certs_dir
, domain
);
1794 if ((f
= fopen(file
, "r")) == NULL
) {
1800 for (i
= 0; i
< cert_count
; i
++) {
1801 cert_buf_sz
= sizeof cert_buf
;
1802 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1803 cert_buf
, &cert_buf_sz
)) {
1806 if (fread(r_cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1807 rv
= CERT_BAD
; /* critical */
1810 if (bcmp(r_cert_buf
, cert_buf
, sizeof cert_buf_sz
)) {
1811 rv
= CERT_BAD
; /* critical */
1820 free_connection_certs(certs
, cert_count
);
1822 /* we close the socket first for speed */
1826 /* only complain if we didn't save it locally */
1827 if (error
&& rv
!= CERT_LOCAL
) {
1828 strlcpy(serr
, "Certificate exception(s): ", sizeof serr
);
1829 if (error
& GNUTLS_CERT_INVALID
)
1830 strlcat(serr
, "invalid, ", sizeof serr
);
1831 if (error
& GNUTLS_CERT_REVOKED
)
1832 strlcat(serr
, "revoked, ", sizeof serr
);
1833 if (error
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
1834 strlcat(serr
, "signer not found, ", sizeof serr
);
1835 if (error
& GNUTLS_CERT_SIGNER_NOT_CA
)
1836 strlcat(serr
, "not signed by CA, ", sizeof serr
);
1837 if (error
& GNUTLS_CERT_INSECURE_ALGORITHM
)
1838 strlcat(serr
, "insecure algorithm, ", sizeof serr
);
1839 if (error
& GNUTLS_CERT_NOT_ACTIVATED
)
1840 strlcat(serr
, "not activated, ", sizeof serr
);
1841 if (error
& GNUTLS_CERT_EXPIRED
)
1842 strlcat(serr
, "expired, ", sizeof serr
);
1843 for (i
= strlen(serr
) - 1; i
> 0; i
--)
1844 if (serr
[i
] == ',') {
1851 stop_tls(gsession
, xcred
);
1857 cert_cmd(struct tab
*t
, struct karg
*args
)
1859 const gchar
*uri
, *error_str
= NULL
;
1863 gnutls_session_t gsession
;
1864 gnutls_x509_crt_t
*certs
;
1865 gnutls_certificate_credentials_t xcred
;
1870 if (ssl_ca_file
== NULL
) {
1871 show_oops(t
, "Can't open CA file: %s", ssl_ca_file
);
1875 if ((uri
= get_uri(t
)) == NULL
) {
1876 show_oops(t
, "Invalid URI");
1880 if ((s
= connect_socket_from_uri(uri
, &error_str
, domain
,
1881 sizeof domain
)) == -1) {
1882 show_oops(t
, "%s", error_str
);
1887 if (start_tls(&error_str
, s
, &gsession
, &xcred
))
1891 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1892 show_oops(t
, "get_connection_certs failed");
1896 if (args
->i
& XT_SHOW
)
1897 show_certs(t
, certs
, cert_count
, "Certificate Chain");
1898 else if (args
->i
& XT_SAVE
)
1899 save_certs(t
, certs
, cert_count
, domain
);
1901 free_connection_certs(certs
, cert_count
);
1903 /* we close the socket first for speed */
1906 stop_tls(gsession
, xcred
);
1907 if (error_str
&& strlen(error_str
))
1908 show_oops(t
, "%s", error_str
);
1913 remove_cookie(int index
)
1919 DNPRINTF(XT_D_COOKIE
, "remove_cookie: %d\n", index
);
1921 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
1923 for (i
= 1; cf
; cf
= cf
->next
, i
++) {
1927 print_cookie("remove cookie", c
);
1928 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
1933 soup_cookies_free(cf
);
1939 toplevel_cmd(struct tab
*t
, struct karg
*args
)
1941 js_toggle_cb(t
->js_toggle
, t
);
1947 can_go_back_for_real(struct tab
*t
)
1950 WebKitWebHistoryItem
*item
;
1956 /* rely on webkit to make sure we can go backward when on an about page */
1958 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
1959 return (webkit_web_view_can_go_back(t
->wv
));
1961 /* the back/forwars list is stupid so help determine if we can go back */
1962 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
1964 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
1965 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
1973 can_go_forward_for_real(struct tab
*t
)
1976 WebKitWebHistoryItem
*item
;
1982 /* rely on webkit to make sure we can go forward when on an about page */
1984 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
1985 return (webkit_web_view_can_go_forward(t
->wv
));
1987 /* the back/forwars list is stupid so help selecting a different item */
1988 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
1990 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
1991 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
1999 go_back_for_real(struct tab
*t
)
2002 WebKitWebHistoryItem
*item
;
2010 webkit_web_view_go_back(t
->wv
);
2013 /* the back/forwars list is stupid so help selecting a different item */
2014 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2016 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2017 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2018 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2025 go_forward_for_real(struct tab
*t
)
2028 WebKitWebHistoryItem
*item
;
2036 webkit_web_view_go_forward(t
->wv
);
2039 /* the back/forwars list is stupid so help selecting a different item */
2040 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2042 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2043 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2044 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2051 navaction(struct tab
*t
, struct karg
*args
)
2053 WebKitWebHistoryItem
*item
;
2054 WebKitWebFrame
*frame
;
2056 DNPRINTF(XT_D_NAV
, "navaction: tab %d opcode %d\n",
2057 t
->tab_id
, args
->i
);
2059 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
2061 if (args
->i
== XT_NAV_BACK
)
2062 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2064 item
= webkit_web_back_forward_list_get_forward_item(t
->bfl
);
2066 return (XT_CB_PASSTHROUGH
);
2067 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2069 return (XT_CB_PASSTHROUGH
);
2075 go_back_for_real(t
);
2077 case XT_NAV_FORWARD
:
2079 go_forward_for_real(t
);
2082 frame
= webkit_web_view_get_main_frame(t
->wv
);
2083 webkit_web_frame_reload(frame
);
2086 return (XT_CB_PASSTHROUGH
);
2090 move(struct tab
*t
, struct karg
*args
)
2092 GtkAdjustment
*adjust
;
2093 double pi
, si
, pos
, ps
, upper
, lower
, max
;
2099 case XT_MOVE_BOTTOM
:
2101 case XT_MOVE_PAGEDOWN
:
2102 case XT_MOVE_PAGEUP
:
2103 case XT_MOVE_HALFDOWN
:
2104 case XT_MOVE_HALFUP
:
2105 case XT_MOVE_PERCENT
:
2106 case XT_MOVE_CENTER
:
2107 adjust
= t
->adjust_v
;
2110 adjust
= t
->adjust_h
;
2114 pos
= gtk_adjustment_get_value(adjust
);
2115 ps
= gtk_adjustment_get_page_size(adjust
);
2116 upper
= gtk_adjustment_get_upper(adjust
);
2117 lower
= gtk_adjustment_get_lower(adjust
);
2118 si
= gtk_adjustment_get_step_increment(adjust
);
2119 pi
= gtk_adjustment_get_page_increment(adjust
);
2122 DNPRINTF(XT_D_MOVE
, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2123 "max %f si %f pi %f\n",
2124 args
->i
, adjust
== t
->adjust_h
? "horizontal" : "vertical",
2125 pos
, ps
, upper
, lower
, max
, si
, pi
);
2131 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2136 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2138 case XT_MOVE_BOTTOM
:
2139 case XT_MOVE_FARRIGHT
:
2140 gtk_adjustment_set_value(adjust
, max
);
2143 case XT_MOVE_FARLEFT
:
2144 gtk_adjustment_set_value(adjust
, lower
);
2146 case XT_MOVE_PAGEDOWN
:
2148 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2150 case XT_MOVE_PAGEUP
:
2152 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2154 case XT_MOVE_HALFDOWN
:
2156 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2158 case XT_MOVE_HALFUP
:
2160 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2162 case XT_MOVE_CENTER
:
2163 args
->s
= g_strdup("50.0");
2165 case XT_MOVE_PERCENT
:
2166 percent
= atoi(args
->s
) / 100.0;
2167 pos
= max
* percent
;
2168 if (pos
< 0.0 || pos
> max
)
2170 gtk_adjustment_set_value(adjust
, pos
);
2173 return (XT_CB_PASSTHROUGH
);
2176 DNPRINTF(XT_D_MOVE
, "move: new pos %f %f\n", pos
, MIN(pos
, max
));
2178 return (XT_CB_HANDLED
);
2182 url_set_visibility(void)
2186 TAILQ_FOREACH(t
, &tabs
, entry
)
2187 if (show_url
== 0) {
2188 gtk_widget_hide(t
->toolbar
);
2191 gtk_widget_show(t
->toolbar
);
2195 notebook_tab_set_visibility(void)
2197 if (show_tabs
== 0) {
2198 gtk_widget_hide(tab_bar
);
2199 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2201 if (tab_style
== XT_TABS_NORMAL
) {
2202 gtk_widget_hide(tab_bar
);
2203 gtk_notebook_set_show_tabs(notebook
, TRUE
);
2204 } else if (tab_style
== XT_TABS_COMPACT
) {
2205 gtk_widget_show(tab_bar
);
2206 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2212 statusbar_set_visibility(void)
2216 TAILQ_FOREACH(t
, &tabs
, entry
)
2217 if (show_statusbar
== 0) {
2218 gtk_widget_hide(t
->statusbar_box
);
2221 gtk_widget_show(t
->statusbar_box
);
2225 url_set(struct tab
*t
, int enable_url_entry
)
2230 show_url
= enable_url_entry
;
2232 if (enable_url_entry
) {
2233 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
2234 GTK_ENTRY_ICON_PRIMARY
, NULL
);
2235 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
), 0);
2237 pixbuf
= gtk_entry_get_icon_pixbuf(GTK_ENTRY(t
->uri_entry
),
2238 GTK_ENTRY_ICON_PRIMARY
);
2240 gtk_entry_get_progress_fraction(GTK_ENTRY(t
->uri_entry
));
2241 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
2242 GTK_ENTRY_ICON_PRIMARY
, pixbuf
);
2243 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
2249 fullscreen(struct tab
*t
, struct karg
*args
)
2251 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2254 return (XT_CB_PASSTHROUGH
);
2256 if (show_url
== 0) {
2264 url_set_visibility();
2265 notebook_tab_set_visibility();
2267 return (XT_CB_HANDLED
);
2271 statustoggle(struct tab
*t
, struct karg
*args
)
2273 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2275 if (show_statusbar
== 1) {
2277 statusbar_set_visibility();
2278 } else if (show_statusbar
== 0) {
2280 statusbar_set_visibility();
2282 return (XT_CB_HANDLED
);
2286 urlaction(struct tab
*t
, struct karg
*args
)
2288 int rv
= XT_CB_HANDLED
;
2290 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2293 return (XT_CB_PASSTHROUGH
);
2297 if (show_url
== 0) {
2299 url_set_visibility();
2303 if (show_url
== 1) {
2305 url_set_visibility();
2313 tabaction(struct tab
*t
, struct karg
*args
)
2315 int rv
= XT_CB_HANDLED
;
2316 char *url
= args
->s
;
2320 DNPRINTF(XT_D_TAB
, "tabaction: %p %d\n", t
, args
->i
);
2323 return (XT_CB_PASSTHROUGH
);
2327 if (strlen(url
) > 0)
2328 create_new_tab(url
, NULL
, 1, args
->precount
);
2330 create_new_tab(NULL
, NULL
, 1, args
->precount
);
2333 if (args
->precount
< 0)
2336 TAILQ_FOREACH(tt
, &tabs
, entry
)
2337 if (tt
->tab_id
== args
->precount
- 1) {
2342 case XT_TAB_DELQUIT
:
2343 if (gtk_notebook_get_n_pages(notebook
) > 1)
2349 if (strlen(url
) > 0)
2352 rv
= XT_CB_PASSTHROUGH
;
2358 if (show_tabs
== 0) {
2360 notebook_tab_set_visibility();
2364 if (show_tabs
== 1) {
2366 notebook_tab_set_visibility();
2369 case XT_TAB_NEXTSTYLE
:
2370 if (tab_style
== XT_TABS_NORMAL
) {
2371 tab_style
= XT_TABS_COMPACT
;
2372 recolor_compact_tabs();
2375 tab_style
= XT_TABS_NORMAL
;
2376 notebook_tab_set_visibility();
2378 case XT_TAB_UNDO_CLOSE
:
2379 if (undo_count
== 0) {
2380 DNPRINTF(XT_D_TAB
, "%s: no tabs to undo close",
2385 u
= TAILQ_FIRST(&undos
);
2386 create_new_tab(u
->uri
, u
, 1, -1);
2388 TAILQ_REMOVE(&undos
, u
, entry
);
2390 /* u->history is freed in create_new_tab() */
2395 rv
= XT_CB_PASSTHROUGH
;
2409 resizetab(struct tab
*t
, struct karg
*args
)
2411 if (t
== NULL
|| args
== NULL
) {
2412 show_oops(NULL
, "resizetab invalid parameters");
2413 return (XT_CB_PASSTHROUGH
);
2416 DNPRINTF(XT_D_TAB
, "resizetab: tab %d %d\n",
2417 t
->tab_id
, args
->i
);
2419 setzoom_webkit(t
, args
->i
);
2421 return (XT_CB_HANDLED
);
2425 movetab(struct tab
*t
, struct karg
*args
)
2429 if (t
== NULL
|| args
== NULL
) {
2430 show_oops(NULL
, "movetab invalid parameters");
2431 return (XT_CB_PASSTHROUGH
);
2434 DNPRINTF(XT_D_TAB
, "movetab: tab %d opcode %d\n",
2435 t
->tab_id
, args
->i
);
2437 if (args
->i
>= XT_TAB_INVALID
)
2438 return (XT_CB_PASSTHROUGH
);
2440 if (TAILQ_EMPTY(&tabs
))
2441 return (XT_CB_PASSTHROUGH
);
2443 n
= gtk_notebook_get_n_pages(notebook
);
2444 dest
= gtk_notebook_get_current_page(notebook
);
2448 if (args
->precount
< 0)
2449 dest
= dest
== n
- 1 ? 0 : dest
+ 1;
2451 dest
= args
->precount
- 1;
2455 if (args
->precount
< 0)
2458 dest
-= args
->precount
% n
;
2471 return (XT_CB_PASSTHROUGH
);
2474 if (dest
< 0 || dest
>= n
)
2475 return (XT_CB_PASSTHROUGH
);
2476 if (t
->tab_id
== dest
) {
2477 DNPRINTF(XT_D_TAB
, "movetab: do nothing\n");
2478 return (XT_CB_HANDLED
);
2481 set_current_tab(dest
);
2483 return (XT_CB_HANDLED
);
2489 command_mode(struct tab
*t
, struct karg
*args
)
2491 run_script(t
, JS_HINTING
);
2492 if (args
->i
== XT_MODE_COMMAND
)
2493 run_script(t
, "hints.clearFocus();");
2495 run_script(t
, "hints.focusInput();");
2497 return (XT_CB_HANDLED
);
2501 command(struct tab
*t
, struct karg
*args
)
2503 char *s
= NULL
, *ss
= NULL
;
2508 if (t
== NULL
|| args
== NULL
) {
2509 show_oops(NULL
, "command invalid parameters");
2510 return (XT_CB_PASSTHROUGH
);
2521 if (cmd_prefix
== 0)
2524 ss
= g_strdup_printf(":%d", cmd_prefix
);
2530 bzero(&a
, sizeof a
);
2536 bzero(&a
, sizeof a
);
2537 a
.i
= XT_HINT_NEWTAB
;
2547 case XT_CMD_OPEN_CURRENT
:
2550 case XT_CMD_TABNEW_CURRENT
:
2551 if (!s
) /* FALL THROUGH? */
2553 if ((uri
= get_uri(t
)) != NULL
) {
2554 ss
= g_strdup_printf("%s%s", s
, uri
);
2559 show_oops(t
, "command: invalid opcode %d", args
->i
);
2560 return (XT_CB_PASSTHROUGH
);
2563 DNPRINTF(XT_D_CMD
, "command: type %s\n", s
);
2565 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), s
);
2566 gdk_color_parse(XT_COLOR_WHITE
, &color
);
2567 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
2569 gtk_widget_grab_focus(GTK_WIDGET(t
->cmd
));
2570 gtk_editable_set_position(GTK_EDITABLE(t
->cmd
), -1);
2575 return (XT_CB_HANDLED
);
2579 search(struct tab
*t
, struct karg
*args
)
2583 if (t
== NULL
|| args
== NULL
) {
2584 show_oops(NULL
, "search invalid parameters");
2589 case XT_SEARCH_NEXT
:
2590 d
= t
->search_forward
;
2592 case XT_SEARCH_PREV
:
2593 d
= !t
->search_forward
;
2596 return (XT_CB_PASSTHROUGH
);
2599 if (t
->search_text
== NULL
) {
2600 if (global_search
== NULL
)
2601 return (XT_CB_PASSTHROUGH
);
2603 d
= t
->search_forward
= TRUE
;
2604 t
->search_text
= g_strdup(global_search
);
2605 webkit_web_view_mark_text_matches(t
->wv
, global_search
, FALSE
, 0);
2606 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
2610 DNPRINTF(XT_D_CMD
, "search: tab %d opc %d forw %d text %s\n",
2611 t
->tab_id
, args
->i
, t
->search_forward
, t
->search_text
);
2613 webkit_web_view_search_text(t
->wv
, t
->search_text
, FALSE
, d
, TRUE
);
2615 return (XT_CB_HANDLED
);
2619 session_save(struct tab
*t
, char *filename
)
2625 if (strlen(filename
) == 0)
2628 if (filename
[0] == '.' || filename
[0] == '/')
2632 if (save_tabs(t
, &a
))
2634 strlcpy(named_session
, filename
, sizeof named_session
);
2636 /* add the new session to the list of sessions */
2637 s
= g_malloc(sizeof(struct session
));
2638 s
->name
= g_strdup(filename
);
2639 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
2647 session_open(struct tab
*t
, char *filename
)
2652 if (strlen(filename
) == 0)
2655 if (filename
[0] == '.' || filename
[0] == '/')
2659 a
.i
= XT_SES_CLOSETABS
;
2660 if (open_tabs(t
, &a
))
2663 strlcpy(named_session
, filename
, sizeof named_session
);
2671 session_delete(struct tab
*t
, char *filename
)
2673 char file
[PATH_MAX
];
2677 if (strlen(filename
) == 0)
2680 if (filename
[0] == '.' || filename
[0] == '/')
2683 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, filename
);
2687 if (!strcmp(filename
, named_session
))
2688 strlcpy(named_session
, XT_SAVED_TABS_FILE
,
2689 sizeof named_session
);
2691 /* remove session from sessions list */
2692 TAILQ_FOREACH(s
, &sessions
, entry
) {
2693 if (!strcmp(s
->name
, filename
))
2698 TAILQ_REMOVE(&sessions
, s
, entry
);
2699 g_free((gpointer
) s
->name
);
2708 session_cmd(struct tab
*t
, struct karg
*args
)
2710 char *filename
= args
->s
;
2715 if (args
->i
& XT_SHOW
)
2716 show_oops(t
, "Current session: %s", named_session
[0] == '\0' ?
2717 XT_SAVED_TABS_FILE
: named_session
);
2718 else if (args
->i
& XT_SAVE
) {
2719 if (session_save(t
, filename
)) {
2720 show_oops(t
, "Can't save session: %s",
2721 filename
? filename
: "INVALID");
2724 } else if (args
->i
& XT_OPEN
) {
2725 if (session_open(t
, filename
)) {
2726 show_oops(t
, "Can't open session: %s",
2727 filename
? filename
: "INVALID");
2730 } else if (args
->i
& XT_DELETE
) {
2731 if (session_delete(t
, filename
)) {
2732 show_oops(t
, "Can't delete session: %s",
2733 filename
? filename
: "INVALID");
2738 return (XT_CB_PASSTHROUGH
);
2742 script_cmd(struct tab
*t
, struct karg
*args
)
2744 JSGlobalContextRef ctx
;
2745 WebKitWebFrame
*frame
;
2747 JSValueRef val
, exception
;
2756 if ((f
= fopen(args
->s
, "r")) == NULL
) {
2757 show_oops(t
, "Can't open script file: %s", args
->s
);
2761 if (fstat(fileno(f
), &sb
) == -1) {
2762 show_oops(t
, "Can't stat script file: %s", args
->s
);
2766 buf
= g_malloc0(sb
.st_size
+ 1);
2767 if (fread(buf
, 1, sb
.st_size
, f
) != sb
.st_size
) {
2768 show_oops(t
, "Can't read script file: %s", args
->s
);
2772 /* this code needs to be redone */
2773 frame
= webkit_web_view_get_main_frame(t
->wv
);
2774 ctx
= webkit_web_frame_get_global_context(frame
);
2776 str
= JSStringCreateWithUTF8CString(buf
);
2777 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
2778 NULL
, 0, &exception
);
2779 JSStringRelease(str
);
2781 DNPRINTF(XT_D_JS
, "run_script: val %p\n", val
);
2783 es
= js_ref_to_string(ctx
, exception
);
2785 show_oops(t
, "script exception: %s", es
);
2790 es
= js_ref_to_string(ctx
, val
);
2793 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
2795 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
2799 show_oops(t
, "script complete return value: '%s'", es
);
2802 show_oops(t
, "script complete: without a return value");
2811 return (XT_CB_PASSTHROUGH
);
2815 * Make a hardcopy of the page
2818 print_page(struct tab
*t
, struct karg
*args
)
2820 WebKitWebFrame
*frame
;
2822 GtkPrintOperation
*op
;
2823 GtkPrintOperationAction action
;
2824 GtkPrintOperationResult print_res
;
2825 GError
*g_err
= NULL
;
2826 int marg_l
, marg_r
, marg_t
, marg_b
;
2828 DNPRINTF(XT_D_PRINTING
, "%s:", __func__
);
2830 ps
= gtk_page_setup_new();
2831 op
= gtk_print_operation_new();
2832 action
= GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
;
2833 frame
= webkit_web_view_get_main_frame(t
->wv
);
2835 /* the default margins are too small, so we will bump them */
2836 marg_l
= gtk_page_setup_get_left_margin(ps
, GTK_UNIT_MM
) +
2837 XT_PRINT_EXTRA_MARGIN
;
2838 marg_r
= gtk_page_setup_get_right_margin(ps
, GTK_UNIT_MM
) +
2839 XT_PRINT_EXTRA_MARGIN
;
2840 marg_t
= gtk_page_setup_get_top_margin(ps
, GTK_UNIT_MM
) +
2841 XT_PRINT_EXTRA_MARGIN
;
2842 marg_b
= gtk_page_setup_get_bottom_margin(ps
, GTK_UNIT_MM
) +
2843 XT_PRINT_EXTRA_MARGIN
;
2846 gtk_page_setup_set_left_margin(ps
, marg_l
, GTK_UNIT_MM
);
2847 gtk_page_setup_set_right_margin(ps
, marg_r
, GTK_UNIT_MM
);
2848 gtk_page_setup_set_top_margin(ps
, marg_t
, GTK_UNIT_MM
);
2849 gtk_page_setup_set_bottom_margin(ps
, marg_b
, GTK_UNIT_MM
);
2851 gtk_print_operation_set_default_page_setup(op
, ps
);
2853 /* this appears to free 'op' and 'ps' */
2854 print_res
= webkit_web_frame_print_full(frame
, op
, action
, &g_err
);
2856 /* check it worked */
2857 if (print_res
== GTK_PRINT_OPERATION_RESULT_ERROR
) {
2858 show_oops(NULL
, "can't print: %s", g_err
->message
);
2859 g_error_free (g_err
);
2867 go_home(struct tab
*t
, struct karg
*args
)
2874 set_encoding(struct tab
*t
, struct karg
*args
)
2878 if (args
->s
&& strlen(g_strstrip(args
->s
)) == 0) {
2879 e
= webkit_web_view_get_custom_encoding(t
->wv
);
2881 e
= webkit_web_view_get_encoding(t
->wv
);
2882 show_oops(t
, "encoding: %s", e
? e
: "N/A");
2884 webkit_web_view_set_custom_encoding(t
->wv
, args
->s
);
2890 restart(struct tab
*t
, struct karg
*args
)
2894 a
.s
= XT_RESTART_TABS_FILE
;
2896 execvp(start_argv
[0], start_argv
);
2905 int (*func
)(struct tab
*, struct karg
*);
2909 { "command_mode", 0, command_mode
, XT_MODE_COMMAND
, 0 },
2910 { "insert_mode", 0, command_mode
, XT_MODE_INSERT
, 0 },
2911 { "command", 0, command
, ':', 0 },
2912 { "search", 0, command
, '/', 0 },
2913 { "searchb", 0, command
, '?', 0 },
2914 { "hinting", 0, command
, '.', 0 },
2915 { "hinting_newtab", 0, command
, ',', 0 },
2916 { "togglesrc", 0, toggle_src
, 0, 0 },
2918 /* yanking and pasting */
2919 { "yankuri", 0, yank_uri
, 0, 0 },
2920 /* XXX: pasteuri{cur,new} do not work from the cmd_entry? */
2921 { "pasteuricur", 0, paste_uri
, XT_PASTE_CURRENT_TAB
, 0 },
2922 { "pasteurinew", 0, paste_uri
, XT_PASTE_NEW_TAB
, 0 },
2925 { "searchnext", 0, search
, XT_SEARCH_NEXT
, 0 },
2926 { "searchprevious", 0, search
, XT_SEARCH_PREV
, 0 },
2929 { "focusaddress", 0, focus
, XT_FOCUS_URI
, 0 },
2930 { "focussearch", 0, focus
, XT_FOCUS_SEARCH
, 0 },
2933 { "hinting", 0, hint
, 0, 0 },
2934 { "hinting_newtab", 0, hint
, XT_HINT_NEWTAB
, 0 },
2936 /* custom stylesheet */
2937 { "userstyle", 0, userstyle
, 0, 0 },
2940 { "goback", 0, navaction
, XT_NAV_BACK
, 0 },
2941 { "goforward", 0, navaction
, XT_NAV_FORWARD
, 0 },
2942 { "reload", 0, navaction
, XT_NAV_RELOAD
, 0 },
2944 /* vertical movement */
2945 { "scrolldown", 0, move
, XT_MOVE_DOWN
, 0 },
2946 { "scrollup", 0, move
, XT_MOVE_UP
, 0 },
2947 { "scrollbottom", 0, move
, XT_MOVE_BOTTOM
, 0 },
2948 { "scrolltop", 0, move
, XT_MOVE_TOP
, 0 },
2949 { "1", 0, move
, XT_MOVE_TOP
, 0 },
2950 { "scrollhalfdown", 0, move
, XT_MOVE_HALFDOWN
, 0 },
2951 { "scrollhalfup", 0, move
, XT_MOVE_HALFUP
, 0 },
2952 { "scrollpagedown", 0, move
, XT_MOVE_PAGEDOWN
, 0 },
2953 { "scrollpageup", 0, move
, XT_MOVE_PAGEUP
, 0 },
2954 /* horizontal movement */
2955 { "scrollright", 0, move
, XT_MOVE_RIGHT
, 0 },
2956 { "scrollleft", 0, move
, XT_MOVE_LEFT
, 0 },
2957 { "scrollfarright", 0, move
, XT_MOVE_FARRIGHT
, 0 },
2958 { "scrollfarleft", 0, move
, XT_MOVE_FARLEFT
, 0 },
2960 { "favorites", 0, xtp_page_fl
, 0, 0 },
2961 { "fav", 0, xtp_page_fl
, 0, 0 },
2962 { "favadd", 0, add_favorite
, 0, 0 },
2964 { "qall", 0, quit
, 0, 0 },
2965 { "quitall", 0, quit
, 0, 0 },
2966 { "w", 0, save_tabs
, 0, 0 },
2967 { "wq", 0, save_tabs_and_quit
, 0, 0 },
2968 { "help", 0, help
, 0, 0 },
2969 { "about", 0, about
, 0, 0 },
2970 { "stats", 0, stats
, 0, 0 },
2971 { "version", 0, about
, 0, 0 },
2974 { "js", 0, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2975 { "save", 1, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
2976 { "domain", 2, js_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
2977 { "fqdn", 2, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
2978 { "show", 1, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2979 { "all", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2980 { "persistent", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
2981 { "session", 2, js_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
2982 { "toggle", 1, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
2983 { "domain", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
2984 { "fqdn", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
2986 /* cookie command */
2987 { "cookie", 0, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2988 { "save", 1, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
2989 { "domain", 2, cookie_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
2990 { "fqdn", 2, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
2991 { "show", 1, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2992 { "all", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2993 { "persistent", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
2994 { "session", 2, cookie_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
2995 { "toggle", 1, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
2996 { "domain", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
2997 { "fqdn", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
2999 /* plugin command */
3000 { "plugin", 0, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3001 { "save", 1, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3002 { "domain", 2, pl_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3003 { "fqdn", 2, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3004 { "show", 1, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3005 { "all", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3006 { "persistent", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3007 { "session", 2, pl_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3008 { "toggle", 1, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3009 { "domain", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3010 { "fqdn", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3012 /* toplevel (domain) command */
3013 { "toplevel", 0, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3014 { "toggle", 1, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3017 { "cookiejar", 0, xtp_page_cl
, 0, 0 },
3020 { "cert", 0, cert_cmd
, XT_SHOW
, 0 },
3021 { "save", 1, cert_cmd
, XT_SAVE
, 0 },
3022 { "show", 1, cert_cmd
, XT_SHOW
, 0 },
3024 { "ca", 0, ca_cmd
, 0, 0 },
3025 { "downloadmgr", 0, xtp_page_dl
, 0, 0 },
3026 { "dl", 0, xtp_page_dl
, 0, 0 },
3027 { "h", 0, xtp_page_hl
, 0, 0 },
3028 { "history", 0, xtp_page_hl
, 0, 0 },
3029 { "home", 0, go_home
, 0, 0 },
3030 { "restart", 0, restart
, 0, 0 },
3031 { "urlhide", 0, urlaction
, XT_URL_HIDE
, 0 },
3032 { "urlshow", 0, urlaction
, XT_URL_SHOW
, 0 },
3033 { "statustoggle", 0, statustoggle
, 0, 0 },
3034 { "run_script", 0, run_page_script
, 0, XT_USERARG
},
3036 { "print", 0, print_page
, 0, 0 },
3039 { "focusin", 0, resizetab
, XT_ZOOM_IN
, 0 },
3040 { "focusout", 0, resizetab
, XT_ZOOM_OUT
, 0 },
3041 { "focusreset", 0, resizetab
, XT_ZOOM_NORMAL
, 0 },
3042 { "q", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3043 { "quit", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3044 { "open", 0, tabaction
, XT_TAB_OPEN
, XT_URLARG
},
3045 { "tabclose", 0, tabaction
, XT_TAB_DELETE
, XT_PREFIX
| XT_INTARG
},
3046 { "tabedit", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3047 { "tabfirst", 0, movetab
, XT_TAB_FIRST
, 0 },
3048 { "tabhide", 0, tabaction
, XT_TAB_HIDE
, 0 },
3049 { "tablast", 0, movetab
, XT_TAB_LAST
, 0 },
3050 { "tabnew", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3051 { "tabnext", 0, movetab
, XT_TAB_NEXT
, XT_PREFIX
| XT_INTARG
},
3052 { "tabnextstyle", 0, tabaction
, XT_TAB_NEXTSTYLE
, 0 },
3053 { "tabprevious", 0, movetab
, XT_TAB_PREV
, XT_PREFIX
| XT_INTARG
},
3054 { "tabrewind", 0, movetab
, XT_TAB_FIRST
, 0 },
3055 { "tabshow", 0, tabaction
, XT_TAB_SHOW
, 0 },
3056 { "tabs", 0, buffers
, 0, 0 },
3057 { "tabundoclose", 0, tabaction
, XT_TAB_UNDO_CLOSE
, 0 },
3058 { "buffers", 0, buffers
, 0, 0 },
3059 { "ls", 0, buffers
, 0, 0 },
3060 { "encoding", 0, set_encoding
, 0, XT_USERARG
},
3062 /* command aliases (handy when -S flag is used) */
3063 { "promptopen", 0, command
, XT_CMD_OPEN
, 0 },
3064 { "promptopencurrent", 0, command
, XT_CMD_OPEN_CURRENT
, 0 },
3065 { "prompttabnew", 0, command
, XT_CMD_TABNEW
, 0 },
3066 { "prompttabnewcurrent",0, command
, XT_CMD_TABNEW_CURRENT
, 0 },
3069 { "set", 0, set
, 0, XT_SETARG
},
3071 { "fullscreen", 0, fullscreen
, 0, 0 },
3072 { "f", 0, fullscreen
, 0, 0 },
3075 { "session", 0, session_cmd
, XT_SHOW
, 0 },
3076 { "delete", 1, session_cmd
, XT_DELETE
, XT_SESSARG
},
3077 { "open", 1, session_cmd
, XT_OPEN
, XT_SESSARG
},
3078 { "save", 1, session_cmd
, XT_SAVE
, XT_USERARG
},
3079 { "show", 1, session_cmd
, XT_SHOW
, 0 },
3081 /* external javascript */
3082 { "script", 0, script_cmd
, XT_EJS_SHOW
, XT_USERARG
},
3085 { "inspector", 0, inspector_cmd
, XT_INS_SHOW
, 0 },
3086 { "show", 1, inspector_cmd
, XT_INS_SHOW
, 0 },
3087 { "hide", 1, inspector_cmd
, XT_INS_HIDE
, 0 },
3094 } cmd_status
= {-1, 0};
3097 wv_release_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3100 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 1)
3107 wv_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3114 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3116 else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 8 /* btn 4 */) {
3122 } else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 9 /* btn 5 */) {
3124 a
.i
= XT_NAV_FORWARD
;
3134 tab_close_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3136 DNPRINTF(XT_D_TAB
, "tab_close_cb: tab %d\n", t
->tab_id
);
3138 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3147 activate_uri_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3149 const gchar
*uri
= gtk_entry_get_text(GTK_ENTRY(entry
));
3151 DNPRINTF(XT_D_URL
, "activate_uri_entry_cb: %s\n", uri
);
3154 show_oops(NULL
, "activate_uri_entry_cb invalid parameters");
3159 show_oops(t
, "activate_uri_entry_cb no uri");
3163 uri
+= strspn(uri
, "\t ");
3165 /* if xxxt:// treat specially */
3166 if (parse_xtp_url(t
, uri
))
3169 /* otherwise continue to load page normally */
3170 load_uri(t
, (gchar
*)uri
);
3175 activate_search_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3177 const gchar
*search
= gtk_entry_get_text(GTK_ENTRY(entry
));
3178 char *newuri
= NULL
;
3181 DNPRINTF(XT_D_URL
, "activate_search_entry_cb: %s\n", search
);
3184 show_oops(NULL
, "activate_search_entry_cb invalid parameters");
3188 if (search_string
== NULL
) {
3189 show_oops(t
, "no search_string");
3193 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
3195 enc_search
= soup_uri_encode(search
, XT_RESERVED_CHARS
);
3196 newuri
= g_strdup_printf(search_string
, enc_search
);
3200 webkit_web_view_load_uri(t
->wv
, newuri
);
3208 check_and_set_cookie(const gchar
*uri
, struct tab
*t
)
3210 struct domain
*d
= NULL
;
3213 if (uri
== NULL
|| t
== NULL
)
3216 if ((d
= wl_find_uri(uri
, &c_wl
)) == NULL
)
3221 DNPRINTF(XT_D_COOKIE
, "check_and_set_cookie: %s %s\n",
3222 es
? "enable" : "disable", uri
);
3224 g_object_set(G_OBJECT(t
->settings
),
3225 "enable-html5-local-storage", es
, (char *)NULL
);
3226 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3230 check_and_set_js(const gchar
*uri
, struct tab
*t
)
3232 struct domain
*d
= NULL
;
3235 if (uri
== NULL
|| t
== NULL
)
3238 if ((d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3243 DNPRINTF(XT_D_JS
, "check_and_set_js: %s %s\n",
3244 es
? "enable" : "disable", uri
);
3246 g_object_set(G_OBJECT(t
->settings
),
3247 "enable-scripts", es
, (char *)NULL
);
3248 g_object_set(G_OBJECT(t
->settings
),
3249 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
3250 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3252 button_set_stockid(t
->js_toggle
,
3253 es
? GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
);
3257 check_and_set_pl(const gchar
*uri
, struct tab
*t
)
3259 struct domain
*d
= NULL
;
3262 if (uri
== NULL
|| t
== NULL
)
3265 if ((d
= wl_find_uri(uri
, &pl_wl
)) == NULL
)
3270 DNPRINTF(XT_D_JS
, "check_and_set_pl: %s %s\n",
3271 es
? "enable" : "disable", uri
);
3273 g_object_set(G_OBJECT(t
->settings
),
3274 "enable-plugins", es
, (char *)NULL
);
3275 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3279 color_address_bar(gpointer p
)
3282 struct tab
*tt
, *t
= p
;
3283 gchar
*col_str
= XT_COLOR_WHITE
;
3284 const gchar
*uri
, *u
= NULL
, *error_str
= NULL
;
3287 gdk_threads_enter();
3289 DNPRINTF(XT_D_URL
, "%s:\n", __func__
);
3291 /* make sure t still exists */
3294 TAILQ_FOREACH(tt
, &tabs
, entry
)
3300 if ((uri
= get_uri(t
)) == NULL
)
3305 gdk_threads_leave();
3308 col_str
= XT_COLOR_YELLOW
;
3309 switch (load_compare_cert(u
, &error_str
)) {
3311 col_str
= XT_COLOR_BLUE
;
3314 col_str
= XT_COLOR_GREEN
;
3316 case CERT_UNTRUSTED
:
3317 col_str
= XT_COLOR_YELLOW
;
3320 col_str
= XT_COLOR_RED
;
3325 gdk_threads_enter();
3327 /* make sure t isn't deleted */
3328 TAILQ_FOREACH(tt
, &tabs
, entry
)
3335 /* test to see if the user navigated away and canceled the thread */
3336 if (t
->thread
!= g_thread_self())
3338 if ((uri
= get_uri(t
)) == NULL
) {
3342 if (strcmp(uri
, u
)) {
3343 /* make sure we are still the same url */
3349 gdk_color_parse(col_str
, &color
);
3350 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3352 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3353 statusbar_modify_attr(t
, col_str
, XT_COLOR_BLACK
);
3355 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3357 if (error_str
&& error_str
[0] != '\0')
3358 show_oops(t
, "%s", error_str
);
3363 /* t is invalid at this point */
3365 g_free((gpointer
)u
);
3367 gdk_threads_leave();
3372 show_ca_status(struct tab
*t
, const char *uri
)
3375 gchar
*col_str
= XT_COLOR_WHITE
;
3377 DNPRINTF(XT_D_URL
, "show_ca_status: %d %s %s\n",
3378 ssl_strict_certs
, ssl_ca_file
, uri
);
3385 if (ssl_ca_file
== NULL
) {
3386 if (g_str_has_prefix(uri
, "http://"))
3388 if (g_str_has_prefix(uri
, "https://")) {
3389 col_str
= XT_COLOR_RED
;
3394 if (g_str_has_prefix(uri
, "http://") ||
3395 !g_str_has_prefix(uri
, "https://"))
3399 * It is not necessary to see if the thread is already running.
3400 * If the thread is in progress setting it to something else aborts it
3404 /* thread the coloring of the address bar */
3405 t
->thread
= g_thread_create((GThreadFunc
)color_address_bar
, t
, TRUE
, NULL
);
3407 color_address_bar(t
);
3413 gdk_color_parse(col_str
, &color
);
3414 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3416 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3417 statusbar_modify_attr(t
, col_str
, XT_COLOR_BLACK
);
3419 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3424 free_favicon(struct tab
*t
)
3426 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p req %p\n",
3427 __func__
, t
->icon_download
, t
->icon_request
);
3429 if (t
->icon_request
)
3430 g_object_unref(t
->icon_request
);
3431 if (t
->icon_dest_uri
)
3432 g_free(t
->icon_dest_uri
);
3434 t
->icon_request
= NULL
;
3435 t
->icon_dest_uri
= NULL
;
3439 xt_icon_from_name(struct tab
*t
, gchar
*name
)
3441 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->uri_entry
),
3442 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3444 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3445 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3447 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3448 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3452 xt_icon_from_pixbuf(struct tab
*t
, GdkPixbuf
*pb
)
3454 GdkPixbuf
*pb_scaled
;
3456 if (gdk_pixbuf_get_width(pb
) > 16 || gdk_pixbuf_get_height(pb
) > 16)
3457 pb_scaled
= gdk_pixbuf_scale_simple(pb
, 16, 16,
3458 GDK_INTERP_BILINEAR
);
3462 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
3463 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3465 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
3466 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3468 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3469 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3471 if (pb_scaled
!= pb
)
3472 g_object_unref(pb_scaled
);
3476 xt_icon_from_file(struct tab
*t
, char *file
)
3480 if (g_str_has_prefix(file
, "file://"))
3481 file
+= strlen("file://");
3483 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
3485 xt_icon_from_pixbuf(t
, pb
);
3488 xt_icon_from_name(t
, "text-html");
3492 is_valid_icon(char *file
)
3495 const char *mime_type
;
3499 gf
= g_file_new_for_path(file
);
3500 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
3502 mime_type
= g_file_info_get_content_type(fi
);
3503 valid
= g_strcmp0(mime_type
, "image/x-ico") == 0 ||
3504 g_strcmp0(mime_type
, "image/vnd.microsoft.icon") == 0 ||
3505 g_strcmp0(mime_type
, "image/png") == 0 ||
3506 g_strcmp0(mime_type
, "image/gif") == 0 ||
3507 g_strcmp0(mime_type
, "application/octet-stream") == 0;
3515 set_favicon_from_file(struct tab
*t
, char *file
)
3519 if (t
== NULL
|| file
== NULL
)
3522 if (g_str_has_prefix(file
, "file://"))
3523 file
+= strlen("file://");
3524 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading %s\n", __func__
, file
);
3526 if (!stat(file
, &sb
)) {
3527 if (sb
.st_size
== 0 || !is_valid_icon(file
)) {
3528 /* corrupt icon so trash it */
3529 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3532 /* no need to set icon to default here */
3536 xt_icon_from_file(t
, file
);
3540 favicon_download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
3543 WebKitDownloadStatus status
= webkit_download_get_status(download
);
3544 struct tab
*tt
= NULL
, *t
= NULL
;
3547 * find the webview instead of passing in the tab as it could have been
3548 * deleted from underneath us.
3550 TAILQ_FOREACH(tt
, &tabs
, entry
) {
3559 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d status %d\n",
3560 __func__
, t
->tab_id
, status
);
3563 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
3565 t
->icon_download
= NULL
;
3568 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
3571 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
3574 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
3576 DNPRINTF(XT_D_DOWNLOAD
, "%s: freeing favicon %d\n",
3577 __func__
, t
->tab_id
);
3578 t
->icon_download
= NULL
;
3581 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
3584 DNPRINTF(XT_D_DOWNLOAD
, "%s: setting icon to %s\n",
3585 __func__
, t
->icon_dest_uri
);
3586 set_favicon_from_file(t
, t
->icon_dest_uri
);
3587 /* these will be freed post callback */
3588 t
->icon_request
= NULL
;
3589 t
->icon_download
= NULL
;
3597 abort_favicon_download(struct tab
*t
)
3599 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p\n", __func__
, t
->icon_download
);
3601 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
3602 if (t
->icon_download
) {
3603 g_signal_handlers_disconnect_by_func(G_OBJECT(t
->icon_download
),
3604 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3605 webkit_download_cancel(t
->icon_download
);
3606 t
->icon_download
= NULL
;
3611 xt_icon_from_name(t
, "text-html");
3615 notify_icon_loaded_cb(WebKitWebView
*wv
, gchar
*uri
, struct tab
*t
)
3617 DNPRINTF(XT_D_DOWNLOAD
, "%s %s\n", __func__
, uri
);
3619 if (uri
== NULL
|| t
== NULL
)
3622 #if WEBKIT_CHECK_VERSION(1, 4, 0)
3623 /* take icon from WebKitIconDatabase */
3626 pb
= webkit_web_view_get_icon_pixbuf(wv
);
3628 xt_icon_from_pixbuf(t
, pb
);
3631 xt_icon_from_name(t
, "text-html");
3632 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
3633 /* download icon to cache dir */
3634 gchar
*name_hash
, file
[PATH_MAX
];
3637 if (t
->icon_request
) {
3638 DNPRINTF(XT_D_DOWNLOAD
, "%s: download in progress\n", __func__
);
3642 /* check to see if we got the icon in cache */
3643 name_hash
= g_compute_checksum_for_string(G_CHECKSUM_SHA256
, uri
, -1);
3644 snprintf(file
, sizeof file
, "%s/%s.ico", cache_dir
, name_hash
);
3647 if (!stat(file
, &sb
)) {
3648 if (sb
.st_size
> 0) {
3649 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading from cache %s\n",
3651 set_favicon_from_file(t
, file
);
3655 /* corrupt icon so trash it */
3656 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3661 /* create download for icon */
3662 t
->icon_request
= webkit_network_request_new(uri
);
3663 if (t
->icon_request
== NULL
) {
3664 DNPRINTF(XT_D_DOWNLOAD
, "%s: invalid uri %s\n",
3669 t
->icon_download
= webkit_download_new(t
->icon_request
);
3670 if (t
->icon_download
== NULL
)
3673 /* we have to free icon_dest_uri later */
3674 t
->icon_dest_uri
= g_strdup_printf("file://%s", file
);
3675 webkit_download_set_destination_uri(t
->icon_download
,
3678 if (webkit_download_get_status(t
->icon_download
) ==
3679 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
3680 g_object_unref(t
->icon_request
);
3681 g_free(t
->icon_dest_uri
);
3682 t
->icon_request
= NULL
;
3683 t
->icon_dest_uri
= NULL
;
3687 g_signal_connect(G_OBJECT(t
->icon_download
), "notify::status",
3688 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3690 webkit_download_start(t
->icon_download
);
3695 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3697 const gchar
*uri
= NULL
, *title
= NULL
;
3698 struct history
*h
, find
;
3702 DNPRINTF(XT_D_URL
, "notify_load_status_cb: %d %s\n",
3703 webkit_web_view_get_load_status(wview
),
3704 get_uri(t
) ? get_uri(t
) : "NOTHING");
3707 show_oops(NULL
, "notify_load_status_cb invalid parameters");
3711 switch (webkit_web_view_get_load_status(wview
)) {
3712 case WEBKIT_LOAD_PROVISIONAL
:
3714 abort_favicon_download(t
);
3715 #if GTK_CHECK_VERSION(2, 20, 0)
3716 gtk_widget_show(t
->spinner
);
3717 gtk_spinner_start(GTK_SPINNER(t
->spinner
));
3719 gtk_label_set_text(GTK_LABEL(t
->label
), "Loading");
3721 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), TRUE
);
3723 /* assume we are a new address */
3724 gdk_color_parse("white", &color
);
3725 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3726 statusbar_modify_attr(t
, "white", XT_COLOR_BLACK
);
3728 /* take focus if we are visible */
3734 /* kill color thread */
3739 case WEBKIT_LOAD_COMMITTED
:
3744 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
3750 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
3752 /* check if js white listing is enabled */
3753 if (enable_plugin_whitelist
)
3754 check_and_set_pl(uri
, t
);
3755 if (enable_cookie_whitelist
)
3756 check_and_set_cookie(uri
, t
);
3757 if (enable_js_whitelist
)
3758 check_and_set_js(uri
, t
);
3764 /* we know enough to autosave the session */
3765 if (session_autosave
) {
3770 show_ca_status(t
, uri
);
3773 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
3777 case WEBKIT_LOAD_FINISHED
:
3783 if (!strncmp(uri
, "http://", strlen("http://")) ||
3784 !strncmp(uri
, "https://", strlen("https://")) ||
3785 !strncmp(uri
, "file://", strlen("file://"))) {
3787 h
= RB_FIND(history_list
, &hl
, &find
);
3789 title
= get_title(t
, FALSE
);
3790 h
= g_malloc(sizeof *h
);
3791 h
->uri
= g_strdup(uri
);
3792 h
->title
= g_strdup(title
);
3793 RB_INSERT(history_list
, &hl
, h
);
3794 completion_add_uri(h
->uri
);
3795 update_history_tabs(NULL
);
3799 set_status(t
, (char *)uri
, XT_STATUS_URI
);
3800 #if WEBKIT_CHECK_VERSION(1, 1, 18)
3801 case WEBKIT_LOAD_FAILED
:
3804 #if GTK_CHECK_VERSION(2, 20, 0)
3805 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
3806 gtk_widget_hide(t
->spinner
);
3809 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
3814 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
), TRUE
);
3816 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
),
3817 can_go_back_for_real(t
));
3819 gtk_widget_set_sensitive(GTK_WIDGET(t
->forward
),
3820 can_go_forward_for_real(t
));
3824 notify_title_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3826 const gchar
*title
= NULL
, *win_title
= NULL
;
3828 title
= get_title(t
, FALSE
);
3829 win_title
= get_title(t
, TRUE
);
3830 gtk_label_set_text(GTK_LABEL(t
->label
), title
);
3831 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
), title
);
3832 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
3833 gtk_window_set_title(GTK_WINDOW(main_window
), win_title
);
3837 webview_load_finished_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
3839 run_script(t
, JS_HINTING
);
3840 if (autofocus_onload
&&
3841 t
->tab_id
== gtk_notebook_get_current_page(notebook
))
3842 run_script(t
, "hints.focusInput();");
3844 run_script(t
, "hints.clearFocus();");
3848 webview_progress_changed_cb(WebKitWebView
*wv
, int progress
, struct tab
*t
)
3850 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->uri_entry
),
3851 progress
== 100 ? 0 : (double)progress
/ 100);
3852 if (show_url
== 0) {
3853 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
3854 progress
== 100 ? 0 : (double)progress
/ 100);
3857 update_statusbar_position(NULL
, NULL
);
3861 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
3862 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
3863 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
3866 WebKitWebNavigationReason reason
;
3867 struct domain
*d
= NULL
;
3870 show_oops(NULL
, "webview_npd_cb invalid parameters");
3874 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
3876 webkit_network_request_get_uri(request
));
3878 uri
= (char *)webkit_network_request_get_uri(request
);
3880 /* if this is an xtp url, we don't load anything else */
3881 if (parse_xtp_url(t
, uri
))
3884 if ((t
->hints_on
&& t
->new_tab
) || t
->ctrl_click
) {
3886 create_new_tab(uri
, NULL
, ctrl_click_focus
, -1);
3887 webkit_web_policy_decision_ignore(pd
);
3888 return (TRUE
); /* we made the decission */
3892 * This is a little hairy but it comes down to this:
3893 * when we run in whitelist mode we have to assist the browser in
3894 * opening the URL that it would have opened in a new tab.
3896 reason
= webkit_web_navigation_action_get_reason(na
);
3897 if (reason
== WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED
) {
3898 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
3899 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1)
3900 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3902 webkit_web_policy_decision_use(pd
);
3903 return (TRUE
); /* we made the decision */
3910 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
3913 struct domain
*d
= NULL
;
3915 WebKitWebView
*webview
= NULL
;
3917 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
3918 webkit_web_view_get_uri(wv
));
3921 /* open in current tab */
3923 } else if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
3924 uri
= webkit_web_view_get_uri(wv
);
3925 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3928 tt
= create_new_tab(NULL
, NULL
, 1, -1);
3930 } else if (enable_scripts
== 1) {
3931 tt
= create_new_tab(NULL
, NULL
, 1, -1);
3939 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
3942 struct domain
*d
= NULL
;
3944 DNPRINTF(XT_D_NAV
, "webview_close_cb: %d\n", t
->tab_id
);
3946 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
3947 uri
= webkit_web_view_get_uri(wv
);
3948 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3952 } else if (enable_scripts
== 1)
3959 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
3961 /* we can not eat the event without throwing gtk off so defer it */
3963 /* catch middle click */
3964 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 2) {
3969 /* catch ctrl click */
3970 if (e
->type
== GDK_BUTTON_RELEASE
&&
3971 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
3976 return (XT_CB_PASSTHROUGH
);
3980 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
3982 struct mime_type
*m
;
3984 m
= find_mime_type(mime_type
);
3992 show_oops(t
, "can't fork mime handler");
4002 execlp(m
->mt_action
, m
->mt_action
,
4003 webkit_network_request_get_uri(request
), (void *)NULL
);
4012 get_mime_type(const char *file
)
4015 char *mime_type
= NULL
;
4019 if (g_str_has_prefix(file
, "file://"))
4020 file
+= strlen("file://");
4022 gf
= g_file_new_for_path(file
);
4023 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
4025 if ((m
= g_file_info_get_content_type(fi
)) != NULL
)
4026 mime_type
= g_strdup(m
);
4034 run_download_mimehandler(char *mime_type
, char *file
)
4036 struct mime_type
*m
;
4038 m
= find_mime_type(mime_type
);
4044 show_oops(NULL
, "can't fork download mime handler");
4054 if (g_str_has_prefix(file
, "file://"))
4055 file
+= strlen("file://");
4056 execlp(m
->mt_action
, m
->mt_action
, file
, (void *)NULL
);
4065 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
4068 WebKitDownloadStatus status
;
4069 const char *file
= NULL
;
4072 if (download
== NULL
)
4074 status
= webkit_download_get_status(download
);
4075 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
4078 file
= webkit_download_get_destination_uri(download
);
4081 mime
= get_mime_type(file
);
4085 run_download_mimehandler((char *)mime
, (char *)file
);
4090 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
4091 WebKitNetworkRequest
*request
, char *mime_type
,
4092 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
4095 show_oops(NULL
, "webview_mimetype_cb invalid parameters");
4099 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
4100 t
->tab_id
, mime_type
);
4102 if (run_mimehandler(t
, mime_type
, request
) == 0) {
4103 webkit_web_policy_decision_ignore(decision
);
4108 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
4109 webkit_web_policy_decision_download(decision
);
4117 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
4121 const gchar
*suggested_name
;
4122 gchar
*filename
= NULL
;
4124 struct download
*download_entry
;
4127 if (wk_download
== NULL
|| t
== NULL
) {
4128 show_oops(NULL
, "%s invalid parameters", __func__
);
4132 suggested_name
= webkit_download_get_suggested_filename(wk_download
);
4133 if (suggested_name
== NULL
)
4134 return (FALSE
); /* abort download */
4145 filename
= g_strdup_printf("%d%s", i
, suggested_name
);
4147 uri
= g_strdup_printf("file://%s/%s", download_dir
, i
?
4148 filename
: suggested_name
);
4150 } while (!stat(uri
+ strlen("file://"), &sb
));
4152 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d filename %s "
4153 "local %s\n", __func__
, t
->tab_id
, filename
, uri
);
4155 webkit_download_set_destination_uri(wk_download
, uri
);
4157 if (webkit_download_get_status(wk_download
) ==
4158 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
4159 show_oops(t
, "%s: download failed to start", __func__
);
4161 gtk_label_set_text(GTK_LABEL(t
->label
), "Download Failed");
4163 /* connect "download first" mime handler */
4164 g_signal_connect(G_OBJECT(wk_download
), "notify::status",
4165 G_CALLBACK(download_status_changed_cb
), NULL
);
4167 download_entry
= g_malloc(sizeof(struct download
));
4168 download_entry
->download
= wk_download
;
4169 download_entry
->tab
= t
;
4170 download_entry
->id
= next_download_id
++;
4171 RB_INSERT(download_list
, &downloads
, download_entry
);
4172 /* get from history */
4173 g_object_ref(wk_download
);
4174 gtk_label_set_text(GTK_LABEL(t
->label
), "Downloading");
4175 show_oops(t
, "Download of '%s' started...",
4176 basename((char *)webkit_download_get_destination_uri(wk_download
)));
4185 /* sync other download manager tabs */
4186 update_download_tabs(NULL
);
4189 * NOTE: never redirect/render the current tab before this
4190 * function returns. This will cause the download to never start.
4192 return (ret
); /* start download */
4196 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
4198 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
4201 show_oops(NULL
, "webview_hover_cb");
4206 set_status(t
, uri
, XT_STATUS_LINK
);
4209 set_status(t
, t
->status
, XT_STATUS_NOTHING
);
4214 mark(struct tab
*t
, struct karg
*arg
)
4220 if ((index
= marktoindex(mark
)) == -1)
4223 if (arg
->i
== XT_MARK_SET
)
4224 t
->mark
[index
] = gtk_adjustment_get_value(t
->adjust_v
);
4225 else if (arg
->i
== XT_MARK_GOTO
) {
4226 if (t
->mark
[index
] == XT_INVALID_MARK
) {
4227 show_oops(t
, "mark '%c' does not exist", mark
);
4230 /* XXX t->mark[index] can be bigger than the maximum if ajax or
4231 something changes the document size */
4232 gtk_adjustment_set_value(t
->adjust_v
, t
->mark
[index
]);
4239 marks_clear(struct tab
*t
)
4243 for (i
= 0; i
< LENGTH(t
->mark
); i
++)
4244 t
->mark
[i
] = XT_INVALID_MARK
;
4250 char file
[PATH_MAX
];
4251 char *line
= NULL
, *p
;
4256 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
4257 if ((f
= fopen(file
, "r+")) == NULL
) {
4258 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
4262 for (i
= 1; ; i
++) {
4263 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
4265 if (strlen(line
) == 0 || line
[0] == '#') {
4271 p
= strtok(line
, " \t");
4273 if (p
== NULL
|| strlen(p
) != 1 ||
4274 (index
= marktoindex(*p
)) == -1) {
4275 warnx("corrupt quickmarks file, line %d", i
);
4279 p
= strtok(NULL
, " \t");
4280 if (qmarks
[index
] != NULL
)
4281 g_free(qmarks
[index
]);
4282 qmarks
[index
] = g_strdup(p
);
4293 char file
[PATH_MAX
];
4297 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
4298 if ((f
= fopen(file
, "r+")) == NULL
) {
4299 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
4303 for (i
= 0; i
< XT_NOMARKS
; i
++)
4304 if (qmarks
[i
] != NULL
)
4305 fprintf(f
, "%c %s\n", indextomark(i
), qmarks
[i
]);
4313 qmark(struct tab
*t
, struct karg
*arg
)
4318 mark
= arg
->s
[strlen(arg
->s
)-1];
4319 index
= marktoindex(mark
);
4325 if (qmarks
[index
] != NULL
)
4326 g_free(qmarks
[index
]);
4328 qmarks_load(); /* sync if multiple instances */
4329 qmarks
[index
] = g_strdup(get_uri(t
));
4333 if (qmarks
[index
] != NULL
)
4334 load_uri(t
, qmarks
[index
]);
4336 show_oops(t
, "quickmark \"%c\" does not exist",
4342 if (qmarks
[index
] != NULL
)
4343 create_new_tab(qmarks
[index
], NULL
, 1, -1);
4345 show_oops(t
, "quickmark \"%c\" does not exist",
4356 go_up(struct tab
*t
, struct karg
*args
)
4362 levels
= atoi(args
->s
);
4366 uri
= g_strdup(webkit_web_view_get_uri(t
->wv
));
4367 if ((tmp
= strstr(uri
, XT_PROTO_DELIM
)) == NULL
)
4369 tmp
+= strlen(XT_PROTO_DELIM
);
4371 /* if an uri starts with a slash, leave it alone (for file:///) */
4378 p
= strrchr(tmp
, '/');
4392 gototab(struct tab
*t
, struct karg
*args
)
4395 struct karg arg
= {0, NULL
, -1};
4397 tab
= atoi(args
->s
);
4399 arg
.i
= XT_TAB_NEXT
;
4408 zoom_amount(struct tab
*t
, struct karg
*arg
)
4410 struct karg narg
= {0, NULL
, -1};
4412 narg
.i
= atoi(arg
->s
);
4413 resizetab(t
, &narg
);
4419 flip_colon(struct tab
*t
, struct karg
*arg
)
4421 struct karg narg
= {0, NULL
, -1};
4424 if (t
== NULL
|| arg
== NULL
)
4427 p
= strstr(arg
->s
, ":");
4439 /* buffer commands receive the regex that triggered them in arg.s */
4440 char bcmd
[XT_BUFCMD_SZ
];
4444 #define XT_PRE_NO (0)
4445 #define XT_PRE_YES (1)
4446 #define XT_PRE_MAYBE (2)
4448 int (*func
)(struct tab
*, struct karg
*);
4452 { "^[0-9]*gu$", XT_PRE_MAYBE
, "gu", go_up
, 0 },
4453 { "^gg$", XT_PRE_NO
, "gg", move
, XT_MOVE_TOP
},
4454 { "^gG$", XT_PRE_NO
, "gG", move
, XT_MOVE_BOTTOM
},
4455 { "^[0-9]+%$", XT_PRE_YES
, "%", move
, XT_MOVE_PERCENT
},
4456 { "^zz$", XT_PRE_NO
, "zz", move
, XT_MOVE_CENTER
},
4457 { "^gh$", XT_PRE_NO
, "gh", go_home
, 0 },
4458 { "^m[a-zA-Z0-9]$", XT_PRE_NO
, "m", mark
, XT_MARK_SET
},
4459 { "^['][a-zA-Z0-9]$", XT_PRE_NO
, "'", mark
, XT_MARK_GOTO
},
4460 { "^[0-9]+t$", XT_PRE_YES
, "t", gototab
, 0 },
4461 { "^M[a-zA-Z0-9]$", XT_PRE_NO
, "M", qmark
, XT_QMARK_SET
},
4462 { "^go[a-zA-Z0-9]$", XT_PRE_NO
, "go", qmark
, XT_QMARK_OPEN
},
4463 { "^gn[a-zA-Z0-9]$", XT_PRE_NO
, "gn", qmark
, XT_QMARK_TAB
},
4464 { "^ZR$", XT_PRE_NO
, "ZR", restart
, 0 },
4465 { "^ZZ$", XT_PRE_NO
, "ZZ", quit
, 0 },
4466 { "^zi$", XT_PRE_NO
, "zi", resizetab
, XT_ZOOM_IN
},
4467 { "^zo$", XT_PRE_NO
, "zo", resizetab
, XT_ZOOM_OUT
},
4468 { "^z0$", XT_PRE_NO
, "z0", resizetab
, XT_ZOOM_NORMAL
},
4469 { "^[0-9]+Z$", XT_PRE_YES
, "Z", zoom_amount
, 0 },
4470 { "^[0-9]+:$", XT_PRE_YES
, ":", flip_colon
, 0 },
4474 buffercmd_init(void)
4478 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
4479 if (regcomp(&buffercmds
[i
].cregex
, buffercmds
[i
].regex
,
4480 REG_EXTENDED
| REG_NOSUB
))
4481 startpage_add("invalid buffercmd regex %s",
4482 buffercmds
[i
].regex
);
4486 buffercmd_abort(struct tab
*t
)
4490 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_abort: clearing buffer\n");
4491 for (i
= 0; i
< LENGTH(bcmd
); i
++)
4494 cmd_prefix
= 0; /* clear prefix for non-buffer commands */
4495 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
4499 buffercmd_execute(struct tab
*t
, struct buffercmd
*cmd
)
4501 struct karg arg
= {0, NULL
, -1};
4504 arg
.s
= g_strdup(bcmd
);
4506 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_execute: buffer \"%s\" "
4507 "matches regex \"%s\", executing\n", bcmd
, cmd
->regex
);
4517 buffercmd_addkey(struct tab
*t
, guint keyval
)
4520 char s
[XT_BUFCMD_SZ
];
4522 if (keyval
== GDK_Escape
) {
4524 return (XT_CB_HANDLED
);
4527 /* key with modifier or non-ascii character */
4528 if (!isascii(keyval
))
4529 return (XT_CB_PASSTHROUGH
);
4531 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: adding key \"%c\" "
4532 "to buffer \"%s\"\n", keyval
, bcmd
);
4534 for (i
= 0; i
< LENGTH(bcmd
); i
++)
4535 if (bcmd
[i
] == '\0') {
4540 /* buffer full, ignore input */
4541 if (i
>= LENGTH(bcmd
) -1) {
4542 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: buffer full\n");
4544 return (XT_CB_HANDLED
);
4547 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
4549 /* find exact match */
4550 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
4551 if (regexec(&buffercmds
[i
].cregex
, bcmd
,
4552 (size_t) 0, NULL
, 0) == 0) {
4553 buffercmd_execute(t
, &buffercmds
[i
]);
4557 /* find non exact matches to see if we need to abort ot not */
4558 for (i
= 0, match
= 0; i
< LENGTH(buffercmds
); i
++) {
4559 DNPRINTF(XT_D_BUFFERCMD
, "trying: %s\n", bcmd
);
4562 if (buffercmds
[i
].precount
== XT_PRE_MAYBE
) {
4563 if (isdigit(bcmd
[0])) {
4564 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
4568 if (sscanf(bcmd
, "%s", s
) == 0)
4571 } else if (buffercmds
[i
].precount
== XT_PRE_YES
) {
4572 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
4575 if (sscanf(bcmd
, "%s", s
) == 0)
4578 if (c
== -1 && buffercmds
[i
].precount
)
4580 if (!strncmp(s
, buffercmds
[i
].cmd
, strlen(s
)))
4583 DNPRINTF(XT_D_BUFFERCMD
, "got[%d] %d <%s>: %d %s\n",
4584 i
, match
, buffercmds
[i
].cmd
, c
, s
);
4587 DNPRINTF(XT_D_BUFFERCMD
, "aborting: %s\n", bcmd
);
4592 return (XT_CB_HANDLED
);
4596 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
4598 struct key_binding
*k
;
4600 /* handle keybindings if buffercmd is empty.
4601 if not empty, allow commands like C-n */
4602 if (bcmd
[0] == '\0' || ((e
->state
& (CTRL
| MOD1
)) != 0))
4603 TAILQ_FOREACH(k
, &kbl
, entry
)
4604 if (e
->keyval
== k
->key
4605 && (entry
? k
->use_in_entry
: 1)) {
4607 if ((e
->state
& (CTRL
| MOD1
)) == 0)
4608 return (cmd_execute(t
, k
->cmd
));
4609 } else if ((e
->state
& k
->mask
) == k
->mask
) {
4610 return (cmd_execute(t
, k
->cmd
));
4614 if (!entry
&& ((e
->state
& (CTRL
| MOD1
)) == 0))
4615 return buffercmd_addkey(t
, e
->keyval
);
4617 return (XT_CB_PASSTHROUGH
);
4621 wv_keypress_after_cb(GtkWidget
*w
, GdkEventKey
*e
, struct tab
*t
)
4625 /* don't use w directly; use t->whatever instead */
4628 show_oops(NULL
, "wv_keypress_after_cb");
4629 return (XT_CB_PASSTHROUGH
);
4632 DNPRINTF(XT_D_KEY
, "wv_keypress_after_cb: keyval 0x%x mask 0x%x t %p\n",
4633 e
->keyval
, e
->state
, t
);
4636 /* XXX make sure cmd entry is enabled */
4637 return (XT_CB_HANDLED
);
4640 snprintf(s
, sizeof s
, "%c", e
->keyval
);
4641 if (CLEAN(e
->state
) == 0 && isdigit(s
[0]))
4642 cmd_prefix
= 10 * cmd_prefix
+ atoi(s
);
4645 return (handle_keypress(t
, e
, 0));
4649 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
4653 /* Hide buffers, if they are visible, with escape. */
4654 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)) &&
4655 CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
4656 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
4658 return (XT_CB_HANDLED
);
4661 return (XT_CB_PASSTHROUGH
);
4665 hint_continue(struct tab
*t
)
4667 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
4669 const gchar
*errstr
= NULL
;
4673 if (!(c
[0] == '.' || c
[0] == ','))
4675 if (strlen(c
) == 1) {
4676 /* XXX should not happen */
4681 if (isdigit(c
[1])) {
4683 i
= strtonum(&c
[1], 1, 4096, &errstr
);
4685 show_oops(t
, "invalid numerical hint %s", &c
[1]);
4688 s
= g_strdup_printf("hints.updateHints(%lld);", i
);
4692 /* alphanumeric input */
4693 s
= g_strdup_printf("hints.createHints('%s', '%c');",
4694 &c
[1], c
[0] == '.' ? 'f' : 'F');
4705 search_continue(struct tab
*t
)
4707 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
4708 gboolean rv
= FALSE
;
4710 if (c
[0] == ':' || c
[0] == '.' || c
[0] == ',')
4712 if (strlen(c
) == 1) {
4713 webkit_web_view_unmark_text_matches(t
->wv
);
4718 t
->search_forward
= TRUE
;
4719 else if (c
[0] == '?')
4720 t
->search_forward
= FALSE
;
4730 search_cb(struct tab
*t
)
4732 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
4735 if (search_continue(t
) == FALSE
)
4739 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, t
->search_forward
,
4741 /* not found, mark red */
4742 gdk_color_parse(XT_COLOR_RED
, &color
);
4743 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
4744 /* unmark and remove selection */
4745 webkit_web_view_unmark_text_matches(t
->wv
);
4746 /* my kingdom for a way to unselect text in webview */
4748 /* found, highlight all */
4749 webkit_web_view_unmark_text_matches(t
->wv
);
4750 webkit_web_view_mark_text_matches(t
->wv
, &c
[1], FALSE
, 0);
4751 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
4752 gdk_color_parse(XT_COLOR_WHITE
, &color
);
4753 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
4761 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
4763 const gchar
*c
= gtk_entry_get_text(w
);
4766 show_oops(NULL
, "cmd_keyrelease_cb invalid parameters");
4767 return (XT_CB_PASSTHROUGH
);
4770 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x t %p\n",
4771 e
->keyval
, e
->state
, t
);
4774 if (!(e
->keyval
== GDK_Tab
|| e
->keyval
== GDK_ISO_Left_Tab
)) {
4775 if (hint_continue(t
) == FALSE
)
4780 if (search_continue(t
) == FALSE
)
4783 /* if search length is > 4 then no longer play timeout games */
4784 if (strlen(c
) > 4) {
4786 g_source_remove(t
->search_id
);
4793 /* reestablish a new timer if the user types fast */
4795 g_source_remove(t
->search_id
);
4796 t
->search_id
= g_timeout_add(250, (GSourceFunc
)search_cb
, (gpointer
)t
);
4799 return (XT_CB_PASSTHROUGH
);
4803 match_uri(const gchar
*uri
, const gchar
*key
) {
4806 gboolean match
= FALSE
;
4810 if (!strncmp(key
, uri
, len
))
4813 voffset
= strstr(uri
, "/") + 2;
4814 if (!strncmp(key
, voffset
, len
))
4816 else if (g_str_has_prefix(voffset
, "www.")) {
4817 voffset
= voffset
+ strlen("www.");
4818 if (!strncmp(key
, voffset
, len
))
4827 match_session(const gchar
*name
, const gchar
*key
) {
4830 sub
= strcasestr(name
, key
);
4836 cmd_getlist(int id
, char *key
)
4843 if (cmds
[id
].type
& XT_URLARG
) {
4844 RB_FOREACH_REVERSE(h
, history_list
, &hl
)
4845 if (match_uri(h
->uri
, key
)) {
4846 cmd_status
.list
[c
] = (char *)h
->uri
;
4852 } else if (cmds
[id
].type
& XT_SESSARG
) {
4853 TAILQ_FOREACH(s
, &sessions
, entry
)
4854 if (match_session(s
->name
, key
)) {
4855 cmd_status
.list
[c
] = (char *)s
->name
;
4861 } else if (cmds
[id
].type
& XT_SETARG
) {
4862 for (i
= 0; i
< get_settings_size(); i
++)
4863 if(!strncmp(key
, get_setting_name(i
),
4865 cmd_status
.list
[c
++] = get_setting_name(i
);
4871 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
4873 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
4874 if (cmds
[i
].level
< dep
)
4876 if (cmds
[i
].level
== dep
&& !strncmp(key
, cmds
[i
].cmd
,
4877 strlen(key
)) && !isdigit(cmds
[i
].cmd
[0]))
4878 cmd_status
.list
[c
++] = cmds
[i
].cmd
;
4886 cmd_getnext(int dir
)
4888 cmd_status
.index
+= dir
;
4890 if (cmd_status
.index
< 0)
4891 cmd_status
.index
= cmd_status
.len
- 1;
4892 else if (cmd_status
.index
>= cmd_status
.len
)
4893 cmd_status
.index
= 0;
4895 return cmd_status
.list
[cmd_status
.index
];
4899 cmd_tokenize(char *s
, char *tokens
[])
4902 char *tok
, *last
= NULL
;
4903 size_t len
= strlen(s
);
4906 blank
= len
== 0 || (len
> 0 && s
[len
- 1] == ' ');
4907 for (tok
= strtok_r(s
, " ", &last
); tok
&& i
< 3;
4908 tok
= strtok_r(NULL
, " ", &last
), i
++)
4918 cmd_complete(struct tab
*t
, char *str
, int dir
)
4920 GtkEntry
*w
= GTK_ENTRY(t
->cmd
);
4921 int i
, j
, levels
, c
= 0, dep
= 0, parent
= -1;
4923 char *tok
, *match
, *s
= g_strdup(str
);
4925 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
4928 DNPRINTF(XT_D_CMD
, "%s: complete %s\n", __func__
, str
);
4931 for (i
= 0; isdigit(s
[i
]); i
++)
4934 for (; isspace(s
[i
]); i
++)
4939 levels
= cmd_tokenize(s
, tokens
);
4941 for (i
= 0; i
< levels
- 1; i
++) {
4944 for (j
= c
; j
< LENGTH(cmds
); j
++) {
4945 if (cmds
[j
].level
< dep
)
4947 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
,
4951 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
4958 if (matchcount
== 1) {
4959 strlcat(res
, tok
, sizeof res
);
4960 strlcat(res
, " ", sizeof res
);
4970 if (cmd_status
.index
== -1)
4971 cmd_getlist(parent
, tokens
[i
]);
4973 if (cmd_status
.len
> 0) {
4974 match
= cmd_getnext(dir
);
4975 strlcat(res
, match
, sizeof res
);
4976 gtk_entry_set_text(w
, res
);
4977 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
4984 cmd_execute(struct tab
*t
, char *str
)
4986 struct cmd
*cmd
= NULL
;
4987 char *tok
, *last
= NULL
, *s
= g_strdup(str
), *sc
;
4989 int j
, len
, c
= 0, dep
= 0, matchcount
= 0;
4990 int prefix
= -1, rv
= XT_CB_PASSTHROUGH
;
4991 struct karg arg
= {0, NULL
, -1};
4996 for (j
= 0; j
<3 && isdigit(s
[j
]); j
++)
5002 while (isspace(s
[0]))
5005 if (strlen(s
) > 0 && strlen(prefixstr
) > 0)
5006 prefix
= atoi(prefixstr
);
5010 for (tok
= strtok_r(s
, " ", &last
); tok
;
5011 tok
= strtok_r(NULL
, " ", &last
)) {
5013 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5014 if (cmds
[j
].level
< dep
)
5016 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1 :
5018 if (cmds
[j
].level
== dep
&&
5019 !strncmp(tok
, cmds
[j
].cmd
, len
)) {
5023 if (len
== strlen(cmds
[j
].cmd
)) {
5029 if (matchcount
== 1) {
5034 show_oops(t
, "Invalid command: %s", str
);
5040 show_oops(t
, "Empty command");
5046 arg
.precount
= prefix
;
5047 else if (cmd_prefix
> 0)
5048 arg
.precount
= cmd_prefix
;
5050 if (j
> 0 && !(cmd
->type
& XT_PREFIX
) && arg
.precount
> -1) {
5051 show_oops(t
, "No prefix allowed: %s", str
);
5055 arg
.s
= last
? g_strdup(last
) : g_strdup("");
5056 if (cmd
->type
& XT_INTARG
&& last
&& strlen(last
) > 0) {
5057 if (arg
.s
== NULL
) {
5058 show_oops(t
, "Invalid command");
5061 arg
.precount
= atoi(arg
.s
);
5062 if (arg
.precount
<= 0) {
5063 if (arg
.s
[0] == '0')
5064 show_oops(t
, "Zero count");
5066 show_oops(t
, "Trailing characters");
5071 DNPRINTF(XT_D_CMD
, "%s: prefix %d arg %s\n",
5072 __func__
, arg
.precount
, arg
.s
);
5088 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5091 show_oops(NULL
, "entry_key_cb invalid parameters");
5092 return (XT_CB_PASSTHROUGH
);
5095 DNPRINTF(XT_D_CMD
, "entry_key_cb: keyval 0x%x mask 0x%x t %p\n",
5096 e
->keyval
, e
->state
, t
);
5100 if (e
->keyval
== GDK_Escape
) {
5101 /* don't use focus_webview(t) because we want to type :cmds */
5102 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5105 return (handle_keypress(t
, e
, 1));
5108 struct command_entry
*
5109 history_prev(struct command_list
*l
, struct command_entry
*at
)
5112 at
= TAILQ_LAST(l
, command_list
);
5114 at
= TAILQ_PREV(at
, command_list
, entry
);
5116 at
= TAILQ_LAST(l
, command_list
);
5122 struct command_entry
*
5123 history_next(struct command_list
*l
, struct command_entry
*at
)
5126 at
= TAILQ_FIRST(l
);
5128 at
= TAILQ_NEXT(at
, entry
);
5130 at
= TAILQ_FIRST(l
);
5137 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5139 int rv
= XT_CB_HANDLED
;
5140 const gchar
*c
= gtk_entry_get_text(w
);
5144 show_oops(NULL
, "cmd_keypress_cb parameters");
5145 return (XT_CB_PASSTHROUGH
);
5148 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x t %p\n",
5149 e
->keyval
, e
->state
, t
);
5153 e
->keyval
= GDK_Escape
;
5154 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5155 c
[0] == '.' || c
[0] == ','))
5156 e
->keyval
= GDK_Escape
;
5158 if (e
->keyval
!= GDK_Tab
&& e
->keyval
!= GDK_Shift_L
&&
5159 e
->keyval
!= GDK_ISO_Left_Tab
)
5160 cmd_status
.index
= -1;
5162 switch (e
->keyval
) {
5165 cmd_complete(t
, (char *)&c
[1], 1);
5166 else if (c
[0] == '.' || c
[0] == ',')
5167 run_script(t
, "hints.focusNextHint();");
5169 case GDK_ISO_Left_Tab
:
5171 cmd_complete(t
, (char *)&c
[1], -1);
5172 else if (c
[0] == '.' || c
[0] == ',')
5173 run_script(t
, "hints.focusPreviousHint();");
5177 if ((search_at
= history_next(&shl
, search_at
))) {
5178 search_at
->line
[0] = c
[0];
5179 gtk_entry_set_text(w
, search_at
->line
);
5180 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5182 } else if (c
[0] == '/') {
5183 if ((search_at
= history_prev(&chl
, search_at
))) {
5184 search_at
->line
[0] = c
[0];
5185 gtk_entry_set_text(w
, search_at
->line
);
5186 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5188 } if (c
[0] == ':') {
5189 if ((history_at
= history_prev(&chl
, history_at
))) {
5190 history_at
->line
[0] = c
[0];
5191 gtk_entry_set_text(w
, history_at
->line
);
5192 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5198 if ((search_at
= history_next(&shl
, search_at
))) {
5199 search_at
->line
[0] = c
[0];
5200 gtk_entry_set_text(w
, search_at
->line
);
5201 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5203 } else if (c
[0] == '?') {
5204 if ((search_at
= history_prev(&chl
, search_at
))) {
5205 search_at
->line
[0] = c
[0];
5206 gtk_entry_set_text(w
, search_at
->line
);
5207 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5209 } if (c
[0] == ':') {
5210 if ((history_at
= history_next(&chl
, history_at
))) {
5211 history_at
->line
[0] = c
[0];
5212 gtk_entry_set_text(w
, history_at
->line
);
5213 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5218 if (!(!strcmp(c
, ":") || !strcmp(c
, "/") || !strcmp(c
, "?") ||
5219 !strcmp(c
, ".") || !strcmp(c
, ","))) {
5220 /* see if we are doing hinting and reset it */
5221 if (c
[0] == '.' || c
[0] == ',') {
5222 /* recreate hints */
5223 s
= g_strdup_printf("hints.createHints('', "
5224 "'%c');", c
[0] == '.' ? 'f' : 'F');
5237 if (c
!= NULL
&& (c
[0] == '/' || c
[0] == '?'))
5238 webkit_web_view_unmark_text_matches(t
->wv
);
5240 /* no need to cancel hints */
5244 rv
= XT_CB_PASSTHROUGH
;
5250 wv_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
5252 DNPRINTF(XT_D_CMD
, "wv_popup_cb: tab %d\n", t
->tab_id
);
5256 cmd_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
5258 /* popup menu enabled */
5263 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
5266 show_oops(NULL
, "cmd_focusout_cb invalid parameters");
5267 return (XT_CB_PASSTHROUGH
);
5270 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d popup %d\n",
5271 t
->tab_id
, t
->popup
);
5273 /* if popup is enabled don't lose focus */
5276 return (XT_CB_PASSTHROUGH
);
5283 if (show_url
== 0 || t
->focus_wv
)
5286 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
5288 return (XT_CB_PASSTHROUGH
);
5292 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
5295 const gchar
*c
= gtk_entry_get_text(entry
);
5298 show_oops(NULL
, "cmd_activate_cb invalid parameters");
5302 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
5307 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5308 c
[0] == '.' || c
[0] == ','))
5314 if (c
[0] == '/' || c
[0] == '?') {
5315 /* see if there is a timer pending */
5317 g_source_remove(t
->search_id
);
5322 if (t
->search_text
) {
5323 g_free(t
->search_text
);
5324 t
->search_text
= NULL
;
5327 t
->search_text
= g_strdup(s
);
5329 g_free(global_search
);
5330 global_search
= g_strdup(s
);
5331 t
->search_forward
= c
[0] == '/';
5333 history_add(&shl
, search_file
, s
, &search_history_count
);
5334 } else if (c
[0] == '.' || c
[0] == ',') {
5335 run_script(t
, "hints.fire();");
5336 /* XXX history for link following? */
5337 } else if (c
[0] == ':') {
5338 history_add(&chl
, command_file
, s
, &cmd_history_count
);
5339 /* can't call hide_cmd after cmd_execute */
5351 backward_cb(GtkWidget
*w
, struct tab
*t
)
5356 show_oops(NULL
, "backward_cb invalid parameters");
5360 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
5367 forward_cb(GtkWidget
*w
, struct tab
*t
)
5372 show_oops(NULL
, "forward_cb invalid parameters");
5376 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
5378 a
.i
= XT_NAV_FORWARD
;
5383 home_cb(GtkWidget
*w
, struct tab
*t
)
5386 show_oops(NULL
, "home_cb invalid parameters");
5390 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
5396 stop_cb(GtkWidget
*w
, struct tab
*t
)
5398 WebKitWebFrame
*frame
;
5401 show_oops(NULL
, "stop_cb invalid parameters");
5405 DNPRINTF(XT_D_NAV
, "stop_cb: tab %d\n", t
->tab_id
);
5407 frame
= webkit_web_view_get_main_frame(t
->wv
);
5408 if (frame
== NULL
) {
5409 show_oops(t
, "stop_cb: no frame");
5413 webkit_web_frame_stop_loading(frame
);
5414 abort_favicon_download(t
);
5418 setup_webkit(struct tab
*t
)
5420 if (is_g_object_setting(G_OBJECT(t
->settings
), "enable-dns-prefetching"))
5421 g_object_set(G_OBJECT(t
->settings
), "enable-dns-prefetching",
5422 FALSE
, (char *)NULL
);
5424 warnx("webkit does not have \"enable-dns-prefetching\" property");
5425 g_object_set(G_OBJECT(t
->settings
),
5426 "user-agent", t
->user_agent
, (char *)NULL
);
5427 g_object_set(G_OBJECT(t
->settings
),
5428 "enable-scripts", enable_scripts
, (char *)NULL
);
5429 g_object_set(G_OBJECT(t
->settings
),
5430 "enable-plugins", enable_plugins
, (char *)NULL
);
5431 g_object_set(G_OBJECT(t
->settings
),
5432 "javascript-can-open-windows-automatically", enable_scripts
,
5434 g_object_set(G_OBJECT(t
->settings
),
5435 "enable-html5-database", FALSE
, (char *)NULL
);
5436 g_object_set(G_OBJECT(t
->settings
),
5437 "enable-html5-local-storage", enable_localstorage
, (char *)NULL
);
5438 g_object_set(G_OBJECT(t
->settings
),
5439 "enable_spell_checking", enable_spell_checking
, (char *)NULL
);
5440 g_object_set(G_OBJECT(t
->settings
),
5441 "spell_checking_languages", spell_check_languages
, (char *)NULL
);
5442 g_object_set(G_OBJECT(t
->settings
),
5443 "enable-developer-extras", TRUE
, (char *)NULL
);
5444 g_object_set(G_OBJECT(t
->wv
),
5445 "full-content-zoom", TRUE
, (char *)NULL
);
5447 webkit_web_view_set_settings(t
->wv
, t
->settings
);
5451 update_statusbar_position(GtkAdjustment
* adjustment
, gpointer data
)
5453 struct tab
*ti
, *t
= NULL
;
5454 gdouble view_size
, value
, max
;
5457 TAILQ_FOREACH(ti
, &tabs
, entry
)
5458 if (ti
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
5466 if (adjustment
== NULL
)
5467 adjustment
= gtk_scrolled_window_get_vadjustment(
5468 GTK_SCROLLED_WINDOW(t
->browser_win
));
5470 view_size
= gtk_adjustment_get_page_size(adjustment
);
5471 value
= gtk_adjustment_get_value(adjustment
);
5472 max
= gtk_adjustment_get_upper(adjustment
) - view_size
;
5475 position
= g_strdup("All");
5476 else if (value
== max
)
5477 position
= g_strdup("Bot");
5478 else if (value
== 0)
5479 position
= g_strdup("Top");
5481 position
= g_strdup_printf("%d%%", (int) ((value
/ max
) * 100));
5483 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.position
), position
);
5490 create_window(const gchar
*name
)
5494 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
5495 gtk_window_set_default_size(GTK_WINDOW(w
), window_width
, window_height
);
5496 gtk_widget_set_name(w
, name
);
5497 gtk_window_set_wmclass(GTK_WINDOW(w
), name
, "XXXTerm");
5503 create_browser(struct tab
*t
)
5507 GtkAdjustment
*adjustment
;
5510 show_oops(NULL
, "create_browser invalid parameters");
5514 t
->sb_h
= GTK_SCROLLBAR(gtk_hscrollbar_new(NULL
));
5515 t
->sb_v
= GTK_SCROLLBAR(gtk_vscrollbar_new(NULL
));
5516 t
->adjust_h
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_h
));
5517 t
->adjust_v
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_v
));
5519 w
= gtk_scrolled_window_new(t
->adjust_h
, t
->adjust_v
);
5520 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
5521 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
5523 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
5524 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
5527 t
->settings
= webkit_web_settings_new();
5529 g_object_set(t
->settings
, "default-encoding", encoding
, (char *)NULL
);
5531 if (user_agent
== NULL
) {
5532 g_object_get(G_OBJECT(t
->settings
), "user-agent", &strval
,
5534 t
->user_agent
= g_strdup_printf("%s %s+", strval
, version
);
5537 t
->user_agent
= g_strdup(user_agent
);
5539 t
->stylesheet
= g_strdup_printf("file://%s/style.css", resource_dir
);
5542 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w
));
5543 g_signal_connect(G_OBJECT(adjustment
), "value-changed",
5544 G_CALLBACK(update_statusbar_position
), NULL
);
5553 create_kiosk_toolbar(struct tab
*t
)
5555 GtkWidget
*toolbar
= NULL
, *b
;
5557 b
= gtk_hbox_new(FALSE
, 0);
5559 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
5561 /* backward button */
5562 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
5563 gtk_widget_set_sensitive(t
->backward
, FALSE
);
5564 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
5565 G_CALLBACK(backward_cb
), t
);
5566 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, TRUE
, TRUE
, 0);
5568 /* forward button */
5569 t
->forward
= create_button("Forward", GTK_STOCK_GO_FORWARD
, 0);
5570 gtk_widget_set_sensitive(t
->forward
, FALSE
);
5571 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
5572 G_CALLBACK(forward_cb
), t
);
5573 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, TRUE
, TRUE
, 0);
5576 t
->gohome
= create_button("Home", GTK_STOCK_HOME
, 0);
5577 gtk_widget_set_sensitive(t
->gohome
, true);
5578 g_signal_connect(G_OBJECT(t
->gohome
), "clicked",
5579 G_CALLBACK(home_cb
), t
);
5580 gtk_box_pack_start(GTK_BOX(b
), t
->gohome
, TRUE
, TRUE
, 0);
5582 /* create widgets but don't use them */
5583 t
->uri_entry
= gtk_entry_new();
5584 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
5585 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
5586 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
5592 create_toolbar(struct tab
*t
)
5594 GtkWidget
*toolbar
= NULL
, *b
, *eb1
;
5596 b
= gtk_hbox_new(FALSE
, 0);
5598 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
5600 /* backward button */
5601 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
5602 gtk_widget_set_sensitive(t
->backward
, FALSE
);
5603 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
5604 G_CALLBACK(backward_cb
), t
);
5605 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, FALSE
, FALSE
, 0);
5607 /* forward button */
5608 t
->forward
= create_button("Forward",GTK_STOCK_GO_FORWARD
, 0);
5609 gtk_widget_set_sensitive(t
->forward
, FALSE
);
5610 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
5611 G_CALLBACK(forward_cb
), t
);
5612 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, FALSE
,
5616 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
5617 gtk_widget_set_sensitive(t
->stop
, FALSE
);
5618 g_signal_connect(G_OBJECT(t
->stop
), "clicked",
5619 G_CALLBACK(stop_cb
), t
);
5620 gtk_box_pack_start(GTK_BOX(b
), t
->stop
, FALSE
,
5624 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
5625 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
5626 gtk_widget_set_sensitive(t
->js_toggle
, TRUE
);
5627 g_signal_connect(G_OBJECT(t
->js_toggle
), "clicked",
5628 G_CALLBACK(js_toggle_cb
), t
);
5629 gtk_box_pack_start(GTK_BOX(b
), t
->js_toggle
, FALSE
, FALSE
, 0);
5631 t
->uri_entry
= gtk_entry_new();
5632 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
5633 G_CALLBACK(activate_uri_entry_cb
), t
);
5634 g_signal_connect(G_OBJECT(t
->uri_entry
), "key-press-event",
5635 G_CALLBACK(entry_key_cb
), t
);
5637 eb1
= gtk_hbox_new(FALSE
, 0);
5638 gtk_container_set_border_width(GTK_CONTAINER(eb1
), 1);
5639 gtk_box_pack_start(GTK_BOX(eb1
), t
->uri_entry
, TRUE
, TRUE
, 0);
5640 gtk_box_pack_start(GTK_BOX(b
), eb1
, TRUE
, TRUE
, 0);
5643 if (search_string
) {
5645 t
->search_entry
= gtk_entry_new();
5646 gtk_entry_set_width_chars(GTK_ENTRY(t
->search_entry
), 30);
5647 g_signal_connect(G_OBJECT(t
->search_entry
), "activate",
5648 G_CALLBACK(activate_search_entry_cb
), t
);
5649 g_signal_connect(G_OBJECT(t
->search_entry
), "key-press-event",
5650 G_CALLBACK(entry_key_cb
), t
);
5651 gtk_widget_set_size_request(t
->search_entry
, -1, -1);
5652 eb2
= gtk_hbox_new(FALSE
, 0);
5653 gtk_container_set_border_width(GTK_CONTAINER(eb2
), 1);
5654 gtk_box_pack_start(GTK_BOX(eb2
), t
->search_entry
, TRUE
, TRUE
,
5656 gtk_box_pack_start(GTK_BOX(b
), eb2
, FALSE
, FALSE
, 0);
5663 create_buffers(struct tab
*t
)
5665 GtkCellRenderer
*renderer
;
5668 view
= gtk_tree_view_new();
5670 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view
), FALSE
);
5672 renderer
= gtk_cell_renderer_text_new();
5673 gtk_tree_view_insert_column_with_attributes
5674 (GTK_TREE_VIEW(view
), -1, "Id", renderer
, "text", COL_ID
, (char *)NULL
);
5676 renderer
= gtk_cell_renderer_text_new();
5677 gtk_tree_view_insert_column_with_attributes
5678 (GTK_TREE_VIEW(view
), -1, "Title", renderer
, "text", COL_TITLE
,
5681 gtk_tree_view_set_model
5682 (GTK_TREE_VIEW(view
), GTK_TREE_MODEL(buffers_store
));
5688 row_activated_cb(GtkTreeView
*view
, GtkTreePath
*path
,
5689 GtkTreeViewColumn
*col
, struct tab
*t
)
5694 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5696 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
,
5699 (GTK_TREE_MODEL(buffers_store
), &iter
, COL_ID
, &id
, -1);
5700 set_current_tab(id
- 1);
5706 /* after tab reordering/creation/removal */
5713 TAILQ_FOREACH(t
, &tabs
, entry
) {
5714 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
5715 if (t
->tab_id
> maxid
)
5718 gtk_widget_show(t
->tab_elems
.sep
);
5721 TAILQ_FOREACH(t
, &tabs
, entry
) {
5722 if (t
->tab_id
== maxid
) {
5723 gtk_widget_hide(t
->tab_elems
.sep
);
5729 /* after active tab change */
5731 recolor_compact_tabs(void)
5737 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
5738 TAILQ_FOREACH(t
, &tabs
, entry
)
5739 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
,
5742 curid
= gtk_notebook_get_current_page(notebook
);
5743 TAILQ_FOREACH(t
, &tabs
, entry
)
5744 if (t
->tab_id
== curid
) {
5745 gdk_color_parse(XT_COLOR_CT_ACTIVE
, &color
);
5746 gtk_widget_modify_fg(t
->tab_elems
.label
,
5747 GTK_STATE_NORMAL
, &color
);
5753 set_current_tab(int page_num
)
5755 buffercmd_abort(get_current_tab());
5756 gtk_notebook_set_current_page(notebook
, page_num
);
5757 recolor_compact_tabs();
5761 undo_close_tab_save(struct tab
*t
)
5765 struct undo
*u1
, *u2
;
5767 WebKitWebHistoryItem
*item
;
5769 if ((uri
= get_uri(t
)) == NULL
)
5772 u1
= g_malloc0(sizeof(struct undo
));
5773 u1
->uri
= g_strdup(uri
);
5775 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
5777 m
= webkit_web_back_forward_list_get_forward_length(t
->bfl
);
5778 n
= webkit_web_back_forward_list_get_back_length(t
->bfl
);
5781 /* forward history */
5782 items
= webkit_web_back_forward_list_get_forward_list_with_limit(t
->bfl
, m
);
5786 u1
->history
= g_list_prepend(u1
->history
,
5787 webkit_web_history_item_copy(item
));
5788 items
= g_list_next(items
);
5793 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
5794 u1
->history
= g_list_prepend(u1
->history
,
5795 webkit_web_history_item_copy(item
));
5799 items
= webkit_web_back_forward_list_get_back_list_with_limit(t
->bfl
, n
);
5803 u1
->history
= g_list_prepend(u1
->history
,
5804 webkit_web_history_item_copy(item
));
5805 items
= g_list_next(items
);
5808 TAILQ_INSERT_HEAD(&undos
, u1
, entry
);
5810 if (undo_count
> XT_MAX_UNDO_CLOSE_TAB
) {
5811 u2
= TAILQ_LAST(&undos
, undo_tailq
);
5812 TAILQ_REMOVE(&undos
, u2
, entry
);
5814 g_list_free(u2
->history
);
5823 delete_tab(struct tab
*t
)
5827 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
5833 * no need to join thread here because it won't access t on completion
5836 TAILQ_REMOVE(&tabs
, t
, entry
);
5839 /* Halt all webkit activity. */
5840 abort_favicon_download(t
);
5841 webkit_web_view_stop_loading(t
->wv
);
5843 /* Save the tab, so we can undo the close. */
5844 undo_close_tab_save(t
);
5848 g_source_remove(t
->search_id
);
5851 bzero(&a
, sizeof a
);
5853 inspector_cmd(t
, &a
);
5855 if (browser_mode
== XT_BM_KIOSK
) {
5856 gtk_widget_destroy(t
->uri_entry
);
5857 gtk_widget_destroy(t
->stop
);
5858 gtk_widget_destroy(t
->js_toggle
);
5861 gtk_widget_destroy(t
->tab_elems
.eventbox
);
5862 gtk_widget_destroy(t
->vbox
);
5864 g_free(t
->user_agent
);
5865 g_free(t
->stylesheet
);
5870 if (TAILQ_EMPTY(&tabs
)) {
5871 if (browser_mode
== XT_BM_KIOSK
)
5872 create_new_tab(home
, NULL
, 1, -1);
5874 create_new_tab(NULL
, NULL
, 1, -1);
5877 /* recreate session */
5878 if (session_autosave
) {
5879 bzero(&a
, sizeof a
);
5881 save_tabs(NULL
, &a
);
5885 recolor_compact_tabs();
5889 update_statusbar_zoom(struct tab
*t
)
5892 char s
[16] = { '\0' };
5894 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
5895 if ((zoom
<= 0.99 || zoom
>= 1.01))
5896 snprintf(s
, sizeof s
, "%d%%", (int)(zoom
* 100));
5897 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.zoom
), s
);
5901 setzoom_webkit(struct tab
*t
, int adjust
)
5903 #define XT_ZOOMPERCENT 0.04
5908 show_oops(NULL
, "setzoom_webkit invalid parameters");
5912 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
5913 if (adjust
== XT_ZOOM_IN
)
5914 zoom
+= XT_ZOOMPERCENT
;
5915 else if (adjust
== XT_ZOOM_OUT
)
5916 zoom
-= XT_ZOOMPERCENT
;
5917 else if (adjust
> 0)
5918 zoom
= default_zoom_level
+ adjust
/ 100.0 - 1.0;
5920 show_oops(t
, "setzoom_webkit invalid zoom value");
5924 if (zoom
< XT_ZOOMPERCENT
)
5925 zoom
= XT_ZOOMPERCENT
;
5926 g_object_set(G_OBJECT(t
->wv
), "zoom-level", zoom
, (char *)NULL
);
5927 update_statusbar_zoom(t
);
5931 tab_clicked_cb(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
5933 struct tab
*t
= (struct tab
*) data
;
5935 DNPRINTF(XT_D_TAB
, "tab_clicked_cb: tab: %d\n", t
->tab_id
);
5937 switch (event
->button
) {
5939 set_current_tab(t
->tab_id
);
5950 append_tab(struct tab
*t
)
5955 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
5956 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
5960 create_sbe(int width
)
5964 sbe
= gtk_entry_new();
5965 gtk_entry_set_inner_border(GTK_ENTRY(sbe
), NULL
);
5966 gtk_entry_set_has_frame(GTK_ENTRY(sbe
), FALSE
);
5967 gtk_widget_set_can_focus(GTK_WIDGET(sbe
), FALSE
);
5968 gtk_widget_modify_font(GTK_WIDGET(sbe
), statusbar_font
);
5969 gtk_entry_set_alignment(GTK_ENTRY(sbe
), 1.0);
5970 gtk_widget_set_size_request(sbe
, width
, -1);
5976 create_new_tab(char *title
, struct undo
*u
, int focus
, int position
)
5981 WebKitWebHistoryItem
*item
;
5985 int sbe_p
= 0, sbe_b
= 0,
5988 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
5990 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
5991 DNPRINTF(XT_D_TAB
, "create_new_tab: new tab rejected\n");
5995 t
= g_malloc0(sizeof *t
);
5997 if (title
== NULL
) {
5998 title
= "(untitled)";
6002 t
->vbox
= gtk_vbox_new(FALSE
, 0);
6004 /* label + button for tab */
6005 b
= gtk_hbox_new(FALSE
, 0);
6008 #if GTK_CHECK_VERSION(2, 20, 0)
6009 t
->spinner
= gtk_spinner_new();
6011 t
->label
= gtk_label_new(title
);
6012 bb
= create_button("Close", GTK_STOCK_CLOSE
, 1);
6013 gtk_widget_set_size_request(t
->label
, 100, 0);
6014 gtk_label_set_max_width_chars(GTK_LABEL(t
->label
), 20);
6015 gtk_label_set_ellipsize(GTK_LABEL(t
->label
), PANGO_ELLIPSIZE_END
);
6016 gtk_widget_set_size_request(b
, 130, 0);
6018 gtk_box_pack_start(GTK_BOX(b
), bb
, FALSE
, FALSE
, 0);
6019 gtk_box_pack_start(GTK_BOX(b
), t
->label
, FALSE
, FALSE
, 0);
6020 #if GTK_CHECK_VERSION(2, 20, 0)
6021 gtk_box_pack_start(GTK_BOX(b
), t
->spinner
, FALSE
, FALSE
, 0);
6025 if (browser_mode
== XT_BM_KIOSK
) {
6026 t
->toolbar
= create_kiosk_toolbar(t
);
6027 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
6030 t
->toolbar
= create_toolbar(t
);
6032 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
,
6040 t
->browser_win
= create_browser(t
);
6041 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
6043 /* oops message for user feedback */
6044 t
->oops
= gtk_entry_new();
6045 gtk_entry_set_inner_border(GTK_ENTRY(t
->oops
), NULL
);
6046 gtk_entry_set_has_frame(GTK_ENTRY(t
->oops
), FALSE
);
6047 gtk_widget_set_can_focus(GTK_WIDGET(t
->oops
), FALSE
);
6048 gdk_color_parse(XT_COLOR_RED
, &color
);
6049 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
6050 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
6051 gtk_widget_modify_font(GTK_WIDGET(t
->oops
), oops_font
);
6054 t
->cmd
= gtk_entry_new();
6055 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
6056 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
6057 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
6058 gtk_widget_modify_font(GTK_WIDGET(t
->cmd
), cmd_font
);
6061 t
->statusbar_box
= gtk_hbox_new(FALSE
, 0);
6063 t
->sbe
.statusbar
= gtk_entry_new();
6064 gtk_entry_set_inner_border(GTK_ENTRY(t
->sbe
.statusbar
), NULL
);
6065 gtk_entry_set_has_frame(GTK_ENTRY(t
->sbe
.statusbar
), FALSE
);
6066 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.statusbar
), FALSE
);
6067 gtk_widget_modify_font(GTK_WIDGET(t
->sbe
.statusbar
), statusbar_font
);
6069 /* create these widgets only if specified in statusbar_elems */
6071 t
->sbe
.position
= create_sbe(40);
6072 t
->sbe
.zoom
= create_sbe(40);
6073 t
->sbe
.buffercmd
= create_sbe(60);
6075 statusbar_modify_attr(t
, XT_COLOR_WHITE
, XT_COLOR_BLACK
);
6077 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), t
->sbe
.statusbar
, TRUE
,
6080 /* gtk widgets cannot be added to a box twice. sbe_* variables
6081 make sure of this */
6082 for (p
= statusbar_elems
; *p
!= '\0'; p
++) {
6086 GtkWidget
*sep
= gtk_vseparator_new();
6088 gdk_color_parse(XT_COLOR_SB_SEPARATOR
, &color
);
6089 gtk_widget_modify_bg(sep
, GTK_STATE_NORMAL
, &color
);
6090 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), sep
,
6091 FALSE
, FALSE
, FALSE
);
6096 warnx("flag \"%c\" specified more than "
6097 "once in statusbar_elems\n", *p
);
6101 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6102 t
->sbe
.position
, FALSE
, FALSE
, FALSE
);
6106 warnx("flag \"%c\" specified more than "
6107 "once in statusbar_elems\n", *p
);
6111 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6112 t
->sbe
.buffercmd
, FALSE
, FALSE
, FALSE
);
6116 warnx("flag \"%c\" specified more than "
6117 "once in statusbar_elems\n", *p
);
6121 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6122 t
->sbe
.zoom
, FALSE
, FALSE
, FALSE
);
6125 warnx("illegal flag \"%c\" in statusbar_elems\n", *p
);
6130 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar_box
, FALSE
, FALSE
, 0);
6133 t
->buffers
= create_buffers(t
);
6134 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->buffers
, FALSE
, FALSE
, 0);
6136 /* xtp meaning is normal by default */
6137 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
6139 /* set empty favicon */
6140 xt_icon_from_name(t
, "text-html");
6142 /* and show it all */
6143 gtk_widget_show_all(b
);
6144 gtk_widget_show_all(t
->vbox
);
6146 /* compact tab bar */
6147 t
->tab_elems
.label
= gtk_label_new(title
);
6148 gtk_label_set_width_chars(GTK_LABEL(t
->tab_elems
.label
), 1.0);
6149 gtk_misc_set_alignment(GTK_MISC(t
->tab_elems
.label
), 0.0, 0.0);
6150 gtk_misc_set_padding(GTK_MISC(t
->tab_elems
.label
), 4.0, 4.0);
6151 gtk_widget_modify_font(GTK_WIDGET(t
->tab_elems
.label
), tabbar_font
);
6153 t
->tab_elems
.eventbox
= gtk_event_box_new();
6154 t
->tab_elems
.box
= gtk_hbox_new(FALSE
, 0);
6155 t
->tab_elems
.sep
= gtk_vseparator_new();
6157 gdk_color_parse(XT_COLOR_CT_BACKGROUND
, &color
);
6158 gtk_widget_modify_bg(t
->tab_elems
.eventbox
, GTK_STATE_NORMAL
, &color
);
6159 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6160 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
, &color
);
6161 gdk_color_parse(XT_COLOR_CT_SEPARATOR
, &color
);
6162 gtk_widget_modify_bg(t
->tab_elems
.sep
, GTK_STATE_NORMAL
, &color
);
6164 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.label
, TRUE
,
6166 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.sep
, FALSE
,
6168 gtk_container_add(GTK_CONTAINER(t
->tab_elems
.eventbox
),
6171 gtk_box_pack_start(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
, TRUE
,
6173 gtk_widget_show_all(t
->tab_elems
.eventbox
);
6175 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
6178 id
= position
>= 0 ? position
:
6179 gtk_notebook_get_current_page(notebook
) + 1;
6180 if (id
> gtk_notebook_get_n_pages(notebook
))
6183 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
6184 gtk_notebook_insert_page(notebook
, t
->vbox
, b
, id
);
6185 gtk_box_reorder_child(GTK_BOX(tab_bar
),
6186 t
->tab_elems
.eventbox
, id
);
6191 #if GTK_CHECK_VERSION(2, 20, 0)
6192 /* turn spinner off if we are a new tab without uri */
6194 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
6195 gtk_widget_hide(t
->spinner
);
6198 /* make notebook tabs reorderable */
6199 gtk_notebook_set_tab_reorderable(notebook
, t
->vbox
, TRUE
);
6201 /* compact tabs clickable */
6202 g_signal_connect(G_OBJECT(t
->tab_elems
.eventbox
),
6203 "button_press_event", G_CALLBACK(tab_clicked_cb
), t
);
6205 g_object_connect(G_OBJECT(t
->cmd
),
6206 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb
), t
,
6207 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
6208 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb
), t
,
6209 "signal::activate", G_CALLBACK(cmd_activate_cb
), t
,
6210 "signal::populate-popup", G_CALLBACK(cmd_popup_cb
), t
,
6213 /* reuse wv_button_cb to hide oops */
6214 g_object_connect(G_OBJECT(t
->oops
),
6215 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
6218 g_signal_connect(t
->buffers
,
6219 "row-activated", G_CALLBACK(row_activated_cb
), t
);
6220 g_object_connect(G_OBJECT(t
->buffers
),
6221 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
, (char *)NULL
);
6223 g_object_connect(G_OBJECT(t
->wv
),
6224 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
,
6225 "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb
), t
,
6226 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb
), t
,
6227 "signal::download-requested", G_CALLBACK(webview_download_cb
), t
,
6228 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), t
,
6229 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
6230 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
6231 "signal::create-web-view", G_CALLBACK(webview_cwv_cb
), t
,
6232 "signal::close-web-view", G_CALLBACK(webview_closewv_cb
), t
,
6233 "signal::event", G_CALLBACK(webview_event_cb
), t
,
6234 "signal::load-finished", G_CALLBACK(webview_load_finished_cb
), t
,
6235 "signal::load-progress-changed", G_CALLBACK(webview_progress_changed_cb
), t
,
6236 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb
), t
,
6237 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
6238 "signal::button_release_event", G_CALLBACK(wv_release_button_cb
), t
,
6239 "signal::populate-popup", G_CALLBACK(wv_popup_cb
), t
,
6241 g_signal_connect(t
->wv
,
6242 "notify::load-status", G_CALLBACK(notify_load_status_cb
), t
);
6243 g_signal_connect(t
->wv
,
6244 "notify::title", G_CALLBACK(notify_title_cb
), t
);
6246 /* hijack the unused keys as if we were the browser */
6247 g_object_connect(G_OBJECT(t
->toolbar
),
6248 "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb
), t
,
6251 g_signal_connect(G_OBJECT(bb
), "button_press_event",
6252 G_CALLBACK(tab_close_cb
), t
);
6255 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
6256 /* restore the tab's history */
6257 if (u
&& u
->history
) {
6261 webkit_web_back_forward_list_add_item(t
->bfl
, item
);
6262 items
= g_list_next(items
);
6265 item
= g_list_nth_data(u
->history
, u
->back
);
6267 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
6270 g_list_free(u
->history
);
6272 webkit_web_back_forward_list_clear(t
->bfl
);
6278 url_set_visibility();
6279 statusbar_set_visibility();
6282 set_current_tab(t
->tab_id
);
6283 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
6287 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
6291 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6298 recolor_compact_tabs();
6299 setzoom_webkit(t
, XT_ZOOM_NORMAL
);
6304 notebook_switchpage_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
6310 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
6312 if (gtk_notebook_get_current_page(notebook
) == -1)
6315 TAILQ_FOREACH(t
, &tabs
, entry
) {
6316 if (t
->tab_id
== pn
) {
6317 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
6320 uri
= get_title(t
, TRUE
);
6321 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
6327 /* can't use focus_webview here */
6328 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6335 notebook_pagereordered_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
6338 struct tab
*t
= NULL
, *tt
;
6342 TAILQ_FOREACH(tt
, &tabs
, entry
)
6343 if (tt
->tab_id
== pn
) {
6349 DNPRINTF(XT_D_TAB
, "page_reordered_cb: tab: %d\n", t
->tab_id
);
6351 gtk_box_reorder_child(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
,
6356 menuitem_response(struct tab
*t
)
6358 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
6362 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
6364 GtkWidget
*menu
, *menu_items
;
6365 GdkEventButton
*bevent
;
6369 if (event
->type
== GDK_BUTTON_PRESS
) {
6370 bevent
= (GdkEventButton
*) event
;
6371 menu
= gtk_menu_new();
6373 TAILQ_FOREACH(ti
, &tabs
, entry
) {
6374 if ((uri
= get_uri(ti
)) == NULL
)
6375 /* XXX make sure there is something to print */
6376 /* XXX add gui pages in here to look purdy */
6378 menu_items
= gtk_menu_item_new_with_label(uri
);
6379 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), menu_items
);
6380 gtk_widget_show(menu_items
);
6382 g_signal_connect_swapped((menu_items
),
6383 "activate", G_CALLBACK(menuitem_response
),
6387 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
,
6388 bevent
->button
, bevent
->time
);
6390 /* unref object so it'll free itself when popped down */
6391 #if !GTK_CHECK_VERSION(3, 0, 0)
6392 /* XXX does not need unref with gtk+3? */
6393 g_object_ref_sink(menu
);
6394 g_object_unref(menu
);
6397 return (TRUE
/* eat event */);
6400 return (FALSE
/* propagate */);
6404 icon_size_map(int icon_size
)
6406 if (icon_size
<= GTK_ICON_SIZE_INVALID
||
6407 icon_size
> GTK_ICON_SIZE_DIALOG
)
6408 return (GTK_ICON_SIZE_SMALL_TOOLBAR
);
6414 create_button(char *name
, char *stockid
, int size
)
6416 GtkWidget
*button
, *image
;
6420 rcstring
= g_strdup_printf(
6421 "style \"%s-style\"\n"
6423 " GtkWidget::focus-padding = 0\n"
6424 " GtkWidget::focus-line-width = 0\n"
6428 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
6429 gtk_rc_parse_string(rcstring
);
6431 button
= gtk_button_new();
6432 gtk_button_set_focus_on_click(GTK_BUTTON(button
), FALSE
);
6433 gtk_icon_size
= icon_size_map(size
? size
: icon_size
);
6435 image
= gtk_image_new_from_stock(stockid
, gtk_icon_size
);
6436 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
6437 gtk_container_set_border_width(GTK_CONTAINER(button
), 1);
6438 gtk_container_add(GTK_CONTAINER(button
), GTK_WIDGET(image
));
6439 gtk_widget_set_name(button
, name
);
6440 gtk_button_set_relief(GTK_BUTTON(button
), GTK_RELIEF_NONE
);
6446 button_set_stockid(GtkWidget
*button
, char *stockid
)
6450 image
= gtk_image_new_from_stock(stockid
, icon_size_map(icon_size
));
6451 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
6452 gtk_button_set_image(GTK_BUTTON(button
), image
);
6456 clipb_primary_cb(GtkClipboard
*primary
, GdkEvent
*event
, gpointer notused
)
6459 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
6462 if (xterm_workaround
== 0)
6466 * xterm doesn't play nice with clipboards because it clears the
6467 * primary when clicked. We rely on primary being set to properly
6468 * handle middle mouse button clicks (paste). So when someone clears
6469 * primary copy whatever is in CUT_BUFFER0 into primary to simualte
6470 * other application behavior (as in DON'T clear primary).
6473 p
= gtk_clipboard_wait_for_text(primary
);
6475 if (gdk_property_get(gdk_get_default_root_window(),
6477 gdk_atom_intern("STRING", FALSE
),
6479 1024 * 1024 /* picked out of my butt */,
6485 /* yes sir, we need to NUL the string */
6487 gtk_clipboard_set_text(primary
, p
, -1);
6501 char file
[PATH_MAX
];
6504 vbox
= gtk_vbox_new(FALSE
, 0);
6505 gtk_box_set_spacing(GTK_BOX(vbox
), 0);
6506 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
6507 #if !GTK_CHECK_VERSION(3, 0, 0)
6508 /* XXX seems to be needed with gtk+2 */
6509 gtk_notebook_set_tab_hborder(notebook
, 0);
6510 gtk_notebook_set_tab_vborder(notebook
, 0);
6512 gtk_notebook_set_scrollable(notebook
, TRUE
);
6513 gtk_notebook_set_show_border(notebook
, FALSE
);
6514 gtk_widget_set_can_focus(GTK_WIDGET(notebook
), FALSE
);
6516 abtn
= gtk_button_new();
6517 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
6518 gtk_widget_set_size_request(arrow
, -1, -1);
6519 gtk_container_add(GTK_CONTAINER(abtn
), arrow
);
6520 gtk_widget_set_size_request(abtn
, -1, 20);
6522 #if GTK_CHECK_VERSION(2, 20, 0)
6523 gtk_notebook_set_action_widget(notebook
, abtn
, GTK_PACK_END
);
6525 gtk_widget_set_size_request(GTK_WIDGET(notebook
), -1, -1);
6527 /* compact tab bar */
6528 tab_bar
= gtk_hbox_new(TRUE
, 0);
6530 gtk_box_pack_start(GTK_BOX(vbox
), tab_bar
, FALSE
, FALSE
, 0);
6531 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
6532 gtk_widget_set_size_request(vbox
, -1, -1);
6534 g_object_connect(G_OBJECT(notebook
),
6535 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb
), NULL
,
6537 g_object_connect(G_OBJECT(notebook
),
6538 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb
),
6539 NULL
, (char *)NULL
);
6540 g_signal_connect(G_OBJECT(abtn
), "button_press_event",
6541 G_CALLBACK(arrow_cb
), NULL
);
6543 main_window
= create_window("xxxterm");
6544 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
6545 g_signal_connect(G_OBJECT(main_window
), "delete_event",
6546 G_CALLBACK(gtk_main_quit
), NULL
);
6549 for (i
= 0; i
< LENGTH(icons
); i
++) {
6550 snprintf(file
, sizeof file
, "%s/%s", resource_dir
, icons
[i
]);
6551 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
6552 l
= g_list_append(l
, pb
);
6554 gtk_window_set_default_icon_list(l
);
6556 /* clipboard work around */
6557 if (xterm_workaround
)
6559 G_OBJECT(gtk_clipboard_get(GDK_SELECTION_PRIMARY
)),
6560 "owner-change", G_CALLBACK(clipb_primary_cb
), NULL
);
6562 gtk_widget_show_all(abtn
);
6563 gtk_widget_show_all(main_window
);
6564 notebook_tab_set_visibility();
6568 send_cmd_to_socket(char *cmd
)
6571 struct sockaddr_un sa
;
6573 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
6574 warnx("%s: socket", __func__
);
6578 sa
.sun_family
= AF_UNIX
;
6579 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
6580 work_dir
, XT_SOCKET_FILE
);
6583 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
6584 warnx("%s: connect", __func__
);
6588 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
6589 warnx("%s: send", __func__
);
6600 socket_watcher(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
6603 char str
[XT_MAX_URL_LENGTH
];
6604 socklen_t t
= sizeof(struct sockaddr_un
);
6605 struct sockaddr_un sa
;
6610 gint fd
= g_io_channel_unix_get_fd(source
);
6612 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
6617 if (getpeereid(s
, &uid
, &gid
) == -1) {
6621 if (uid
!= getuid() || gid
!= getgid()) {
6622 warnx("unauthorized user");
6628 warnx("not a valid user");
6632 n
= recv(s
, str
, sizeof(str
), 0);
6636 tt
= TAILQ_LAST(&tabs
, tab_list
);
6637 cmd_execute(tt
, str
);
6645 struct sockaddr_un sa
;
6647 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
6648 warn("is_running: socket");
6652 sa
.sun_family
= AF_UNIX
;
6653 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
6654 work_dir
, XT_SOCKET_FILE
);
6657 /* connect to see if there is a listener */
6658 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
6659 rv
= 0; /* not running */
6661 rv
= 1; /* already running */
6672 struct sockaddr_un sa
;
6674 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
6675 warn("build_socket: socket");
6679 sa
.sun_family
= AF_UNIX
;
6680 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
6681 work_dir
, XT_SOCKET_FILE
);
6684 /* connect to see if there is a listener */
6685 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
6686 /* no listener so we will */
6687 unlink(sa
.sun_path
);
6689 if (bind(s
, (struct sockaddr
*)&sa
, len
) == -1) {
6690 warn("build_socket: bind");
6694 if (listen(s
, 1) == -1) {
6695 warn("build_socket: listen");
6708 completion_select_cb(GtkEntryCompletion
*widget
, GtkTreeModel
*model
,
6709 GtkTreeIter
*iter
, struct tab
*t
)
6713 gtk_tree_model_get(model
, iter
, 0, &value
, -1);
6721 completion_hover_cb(GtkEntryCompletion
*widget
, GtkTreeModel
*model
,
6722 GtkTreeIter
*iter
, struct tab
*t
)
6726 gtk_tree_model_get(model
, iter
, 0, &value
, -1);
6727 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), value
);
6728 gtk_editable_set_position(GTK_EDITABLE(t
->uri_entry
), -1);
6735 completion_add_uri(const gchar
*uri
)
6739 /* add uri to list_store */
6740 gtk_list_store_append(completion_model
, &iter
);
6741 gtk_list_store_set(completion_model
, &iter
, 0, uri
, -1);
6745 completion_match(GtkEntryCompletion
*completion
, const gchar
*key
,
6746 GtkTreeIter
*iter
, gpointer user_data
)
6749 gboolean match
= FALSE
;
6751 gtk_tree_model_get(GTK_TREE_MODEL(completion_model
), iter
, 0, &value
,
6757 match
= match_uri(value
, key
);
6764 completion_add(struct tab
*t
)
6766 /* enable completion for tab */
6767 t
->completion
= gtk_entry_completion_new();
6768 gtk_entry_completion_set_text_column(t
->completion
, 0);
6769 gtk_entry_set_completion(GTK_ENTRY(t
->uri_entry
), t
->completion
);
6770 gtk_entry_completion_set_model(t
->completion
,
6771 GTK_TREE_MODEL(completion_model
));
6772 gtk_entry_completion_set_match_func(t
->completion
, completion_match
,
6774 gtk_entry_completion_set_minimum_key_length(t
->completion
, 1);
6775 gtk_entry_completion_set_inline_selection(t
->completion
, TRUE
);
6776 g_signal_connect(G_OBJECT (t
->completion
), "match-selected",
6777 G_CALLBACK(completion_select_cb
), t
);
6778 g_signal_connect(G_OBJECT (t
->completion
), "cursor-on-match",
6779 G_CALLBACK(completion_hover_cb
), t
);
6787 if (stat(dir
, &sb
)) {
6788 if (mkdir(dir
, S_IRWXU
) == -1)
6789 err(1, "mkdir %s", dir
);
6791 err(1, "stat %s", dir
);
6793 if (S_ISDIR(sb
.st_mode
) == 0)
6794 errx(1, "%s not a dir", dir
);
6795 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
6796 warnx("fixing invalid permissions on %s", dir
);
6797 if (chmod(dir
, S_IRWXU
) == -1)
6798 err(1, "chmod %s", dir
);
6806 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
6810 GStaticRecMutex my_gdk_mtx
= G_STATIC_REC_MUTEX_INIT
;
6811 volatile int mtx_depth
;
6815 * The linux flash plugin violates the gdk locking mechanism.
6816 * Work around the issue by using a recursive mutex with some match applied
6817 * to see if we hit a buggy condition.
6819 * The following code is painful so just don't read it. It really doesn't
6820 * make much sense but seems to work.
6825 g_static_rec_mutex_lock(&my_gdk_mtx
);
6828 if (mtx_depth
<= 0) {
6829 /* should not happen */
6830 show_oops(NULL
, "negative mutex locking bug, trying to "
6832 fprintf(stderr
, "negative mutex locking bug, trying to "
6834 g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
6835 g_static_rec_mutex_lock(&my_gdk_mtx
);
6840 if (mtx_depth
!= 1) {
6841 /* decrease mutext depth to 1 */
6843 g_static_rec_mutex_unlock(&my_gdk_mtx
);
6845 } while (mtx_depth
> 1);
6854 /* if mutex depth isn't 1 then something went bad */
6855 if (mtx_depth
!= 1) {
6856 x
= g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
6858 /* should not happen */
6859 show_oops(NULL
, "mutex unlocking bug, trying to "
6861 fprintf(stderr
, "mutex unlocking bug, trying to "
6865 if (mtx_complain
== 0) {
6866 show_oops(NULL
, "buggy mutex implementation detected, "
6867 "work around implemented");
6868 fprintf(stderr
, "buggy mutex implementation detected, "
6869 "work around implemented");
6876 g_static_rec_mutex_unlock(&my_gdk_mtx
);
6880 main(int argc
, char *argv
[])
6883 int c
, s
, optn
= 0, opte
= 0, focus
= 1;
6884 char conf
[PATH_MAX
] = { '\0' };
6885 char file
[PATH_MAX
];
6886 char *env_proxy
= NULL
;
6890 struct sigaction sact
;
6891 GIOChannel
*channel
;
6898 g_thread_init(NULL
);
6899 gdk_threads_set_lock_functions(mtx_lock
, mtx_unlock
);
6901 gdk_threads_enter();
6903 gcry_control (GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
6905 gtk_init(&argc
, &argv
);
6907 gnutls_global_init();
6909 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
6914 RB_INIT(&downloads
);
6916 TAILQ_INIT(&sessions
);
6919 TAILQ_INIT(&aliases
);
6926 /* fiddle with ulimits */
6927 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
6930 /* just use them all */
6931 rlp
.rlim_cur
= rlp
.rlim_max
;
6932 if (setrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
6934 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
6936 else if (rlp
.rlim_cur
<= 256)
6937 startpage_add("%s requires at least 256 file "
6938 "descriptors, currently it has up to %d available",
6939 __progname
, rlp
.rlim_cur
);
6942 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
6951 errx(0 , "Version: %s", version
);
6954 strlcpy(conf
, optarg
, sizeof(conf
));
6957 strlcpy(named_session
, optarg
, sizeof(named_session
));
6978 xtp_generate_keys();
6981 bzero(&sact
, sizeof(sact
));
6982 sigemptyset(&sact
.sa_mask
);
6983 sact
.sa_handler
= sigchild
;
6984 sact
.sa_flags
= SA_NOCLDSTOP
;
6985 sigaction(SIGCHLD
, &sact
, NULL
);
6987 /* set download dir */
6988 pwd
= getpwuid(getuid());
6990 errx(1, "invalid user %d", getuid());
6991 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
6993 /* compile buffer command regexes */
6996 /* set default string settings */
6997 home
= g_strdup("https://www.cyphertite.com");
6998 search_string
= g_strdup("https://ssl.scroogle.org/cgi-bin/nbbwssl.cgi?Gw=%s");
6999 resource_dir
= g_strdup("/usr/local/share/xxxterm/");
7000 strlcpy(runtime_settings
, "runtime", sizeof runtime_settings
);
7001 cmd_font_name
= g_strdup("monospace normal 9");
7002 oops_font_name
= g_strdup("monospace normal 9");
7003 statusbar_font_name
= g_strdup("monospace normal 9");
7004 tabbar_font_name
= g_strdup("monospace normal 9");
7005 statusbar_elems
= g_strdup("BP");
7006 encoding
= g_strdup("UTF-8");
7008 /* read config file */
7009 if (strlen(conf
) == 0)
7010 snprintf(conf
, sizeof conf
, "%s/.%s",
7011 pwd
->pw_dir
, XT_CONF_FILE
);
7012 config_parse(conf
, 0);
7015 cmd_font
= pango_font_description_from_string(cmd_font_name
);
7016 oops_font
= pango_font_description_from_string(oops_font_name
);
7017 statusbar_font
= pango_font_description_from_string(statusbar_font_name
);
7018 tabbar_font
= pango_font_description_from_string(tabbar_font_name
);
7020 /* working directory */
7021 if (strlen(work_dir
) == 0)
7022 snprintf(work_dir
, sizeof work_dir
, "%s/%s",
7023 pwd
->pw_dir
, XT_DIR
);
7026 /* icon cache dir */
7027 snprintf(cache_dir
, sizeof cache_dir
, "%s/%s", work_dir
, XT_CACHE_DIR
);
7031 snprintf(certs_dir
, sizeof certs_dir
, "%s/%s", work_dir
, XT_CERT_DIR
);
7035 snprintf(sessions_dir
, sizeof sessions_dir
, "%s/%s",
7036 work_dir
, XT_SESSIONS_DIR
);
7037 xxx_dir(sessions_dir
);
7039 /* runtime settings that can override config file */
7040 if (runtime_settings
[0] != '\0')
7041 config_parse(runtime_settings
, 1);
7044 if (!strcmp(download_dir
, pwd
->pw_dir
))
7045 strlcat(download_dir
, "/downloads", sizeof download_dir
);
7046 xxx_dir(download_dir
);
7048 /* favorites file */
7049 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_FAVS_FILE
);
7050 if (stat(file
, &sb
)) {
7051 warnx("favorites file doesn't exist, creating it");
7052 if ((f
= fopen(file
, "w")) == NULL
)
7053 err(1, "favorites");
7057 /* quickmarks file */
7058 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
7059 if (stat(file
, &sb
)) {
7060 warnx("quickmarks file doesn't exist, creating it");
7061 if ((f
= fopen(file
, "w")) == NULL
)
7062 err(1, "quickmarks");
7066 /* search history */
7067 if (history_autosave
) {
7068 snprintf(search_file
, sizeof search_file
, "%s/%s",
7069 work_dir
, XT_SEARCH_FILE
);
7070 if (stat(search_file
, &sb
)) {
7071 warnx("search history file doesn't exist, creating it");
7072 if ((f
= fopen(search_file
, "w")) == NULL
)
7073 err(1, "search_history");
7076 history_read(&shl
, search_file
, &search_history_count
);
7079 /* command history */
7080 if (history_autosave
) {
7081 snprintf(command_file
, sizeof command_file
, "%s/%s",
7082 work_dir
, XT_COMMAND_FILE
);
7083 if (stat(command_file
, &sb
)) {
7084 warnx("command history file doesn't exist, creating it");
7085 if ((f
= fopen(command_file
, "w")) == NULL
)
7086 err(1, "command_history");
7089 history_read(&chl
, command_file
, &cmd_history_count
);
7093 session
= webkit_get_default_session();
7098 if (stat(ssl_ca_file
, &sb
)) {
7099 warnx("no CA file: %s", ssl_ca_file
);
7100 g_free(ssl_ca_file
);
7103 g_object_set(session
,
7104 SOUP_SESSION_SSL_CA_FILE
, ssl_ca_file
,
7105 SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
7109 /* guess_search regex */
7110 if (url_regex
== NULL
)
7111 url_regex
= g_strdup(XT_URL_REGEX
);
7113 if (regcomp(&url_re
, url_regex
, REG_EXTENDED
| REG_NOSUB
))
7114 startpage_add("invalid url regex %s", url_regex
);
7117 env_proxy
= getenv("http_proxy");
7119 setup_proxy(env_proxy
);
7121 setup_proxy(http_proxy
);
7124 send_cmd_to_socket(argv
[0]);
7128 /* set some connection parameters */
7129 g_object_set(session
, "max-conns", max_connections
, (char *)NULL
);
7130 g_object_set(session
, "max-conns-per-host", max_host_connections
,
7133 /* see if there is already an xxxterm running */
7134 if (single_instance
&& is_running()) {
7136 warnx("already running");
7141 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
7142 send_cmd_to_socket(cmd
);
7152 /* uri completion */
7153 completion_model
= gtk_list_store_new(1, G_TYPE_STRING
);
7156 buffers_store
= gtk_list_store_new
7157 (NUM_COLS
, G_TYPE_UINT
, G_TYPE_STRING
);
7163 notebook_tab_set_visibility();
7165 if (save_global_history
)
7166 restore_global_history();
7168 /* restore session list */
7169 restore_sessions_list();
7171 if (!strcmp(named_session
, XT_SAVED_TABS_FILE
))
7172 restore_saved_tabs();
7174 a
.s
= named_session
;
7175 a
.i
= XT_SES_DONOTHING
;
7176 open_tabs(NULL
, &a
);
7179 /* see if we have an exception */
7180 if (!TAILQ_EMPTY(&spl
)) {
7181 create_new_tab("about:startpage", NULL
, focus
, -1);
7186 create_new_tab(argv
[0], NULL
, focus
, -1);
7193 if (TAILQ_EMPTY(&tabs
))
7194 create_new_tab(home
, NULL
, 1, -1);
7197 if ((s
= build_socket()) != -1) {
7198 channel
= g_io_channel_unix_new(s
);
7199 g_io_add_watch(channel
, G_IO_IN
, socket_watcher
, NULL
);
7205 gdk_threads_leave();
7206 g_static_rec_mutex_unlock_full(&my_gdk_mtx
); /* just in case */
7209 gnutls_global_deinit();