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)
135 #define XT_QMARK_SET (0)
136 #define XT_QMARK_OPEN (1)
137 #define XT_QMARK_TAB (2)
139 #define XT_MARK_SET (0)
140 #define XT_MARK_GOTO (1)
142 #define XT_TAB_LAST (-4)
143 #define XT_TAB_FIRST (-3)
144 #define XT_TAB_PREV (-2)
145 #define XT_TAB_NEXT (-1)
146 #define XT_TAB_INVALID (0)
147 #define XT_TAB_NEW (1)
148 #define XT_TAB_DELETE (2)
149 #define XT_TAB_DELQUIT (3)
150 #define XT_TAB_OPEN (4)
151 #define XT_TAB_UNDO_CLOSE (5)
152 #define XT_TAB_SHOW (6)
153 #define XT_TAB_HIDE (7)
154 #define XT_TAB_NEXTSTYLE (8)
156 #define XT_NAV_INVALID (0)
157 #define XT_NAV_BACK (1)
158 #define XT_NAV_FORWARD (2)
159 #define XT_NAV_RELOAD (3)
161 #define XT_FOCUS_INVALID (0)
162 #define XT_FOCUS_URI (1)
163 #define XT_FOCUS_SEARCH (2)
165 #define XT_SEARCH_INVALID (0)
166 #define XT_SEARCH_NEXT (1)
167 #define XT_SEARCH_PREV (2)
169 #define XT_PASTE_CURRENT_TAB (0)
170 #define XT_PASTE_NEW_TAB (1)
172 #define XT_ZOOM_IN (-1)
173 #define XT_ZOOM_OUT (-2)
174 #define XT_ZOOM_NORMAL (100)
176 #define XT_URL_SHOW (1)
177 #define XT_URL_HIDE (2)
179 #define XT_CMD_OPEN (0)
180 #define XT_CMD_OPEN_CURRENT (1)
181 #define XT_CMD_TABNEW (2)
182 #define XT_CMD_TABNEW_CURRENT (3)
184 #define XT_STATUS_NOTHING (0)
185 #define XT_STATUS_LINK (1)
186 #define XT_STATUS_URI (2)
187 #define XT_STATUS_LOADING (3)
189 #define XT_SES_DONOTHING (0)
190 #define XT_SES_CLOSETABS (1)
192 #define XT_PREFIX (1<<0)
193 #define XT_USERARG (1<<1)
194 #define XT_URLARG (1<<2)
195 #define XT_INTARG (1<<3)
196 #define XT_SESSARG (1<<4)
197 #define XT_SETARG (1<<5)
199 #define XT_HINT_NEWTAB (1<<0)
201 #define XT_MODE_INSERT (0)
202 #define XT_MODE_COMMAND (1)
204 #define XT_BUFCMD_SZ (8)
206 #define XT_EJS_SHOW (1<<0)
208 GtkWidget
* create_button(char *, char *, int);
210 void recalc_tabs(void);
211 void recolor_compact_tabs(void);
212 void set_current_tab(int page_num
);
213 gboolean
update_statusbar_position(GtkAdjustment
*, gpointer
);
214 void marks_clear(struct tab
*t
);
217 extern char *__progname
;
220 GtkWidget
*main_window
;
221 GtkNotebook
*notebook
;
223 GtkWidget
*arrow
, *abtn
;
224 struct tab_list tabs
;
225 struct history_list hl
;
226 struct session_list sessions
;
227 struct domain_list c_wl
;
228 struct domain_list js_wl
;
229 struct domain_list pl_wl
;
230 struct undo_tailq undos
;
231 struct keybinding_list kbl
;
233 struct command_list chl
;
234 struct command_list shl
;
235 struct command_entry
*history_at
;
236 struct command_entry
*search_at
;
238 int cmd_history_count
= 0;
239 int search_history_count
= 0;
241 long long unsigned int blocked_cookies
= 0;
242 char named_session
[PATH_MAX
];
243 GtkListStore
*completion_model
;
244 GtkListStore
*buffers_store
;
246 char *qmarks
[XT_NOMARKS
];
247 int btn_down
; /* M1 down in any wv */
248 regex_t url_re
; /* guess_search regex */
250 /* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
251 int next_download_id
= 1;
253 void xxx_dir(char *);
254 int icon_size_map(int);
255 void completion_add(struct tab
*);
256 void completion_add_uri(const gchar
*);
259 history_delete(struct command_list
*l
, int *counter
)
261 struct command_entry
*c
;
263 if (l
== NULL
|| counter
== NULL
)
266 c
= TAILQ_LAST(l
, command_list
);
270 TAILQ_REMOVE(l
, c
, entry
);
277 history_add(struct command_list
*list
, char *file
, char *l
, int *counter
)
279 struct command_entry
*c
;
282 if (list
== NULL
|| l
== NULL
|| counter
== NULL
)
285 /* don't add the same line */
286 c
= TAILQ_FIRST(list
);
288 if (!strcmp(c
->line
+ 1 /* skip space */, l
))
291 c
= g_malloc0(sizeof *c
);
292 c
->line
= g_strdup_printf(" %s", l
);
295 TAILQ_INSERT_HEAD(list
, c
, entry
);
298 history_delete(list
, counter
);
300 if (history_autosave
&& file
) {
301 f
= fopen(file
, "w");
303 show_oops(NULL
, "couldn't write history %s", file
);
307 TAILQ_FOREACH_REVERSE(c
, list
, command_list
, entry
) {
309 fprintf(f
, "%s\n", c
->line
);
317 history_read(struct command_list
*list
, char *file
, int *counter
)
320 char *s
, line
[65536];
322 if (list
== NULL
|| file
== NULL
)
325 f
= fopen(file
, "r");
327 startpage_add("couldn't open history file %s", file
);
332 s
= fgets(line
, sizeof line
, f
);
333 if (s
== NULL
|| feof(f
) || ferror(f
))
335 if ((s
= strchr(line
, '\n')) == NULL
) {
336 startpage_add("invalid history file %s", file
);
342 history_add(list
, NULL
, line
+ 1, counter
);
350 /* marks and quickmarks array storage.
351 * first a-z, then A-Z, then 0-9 */
358 if (i
>= 0 && i
<= 'z' - 'a')
362 if (i
>= 0 && i
<= 'Z' - 'A')
377 if (m
>= 'a' && m
<= 'z')
378 return ret
+ m
- 'a';
380 ret
+= 'z' - 'a' + 1;
381 if (m
>= 'A' && m
<= 'Z')
382 return ret
+ m
- 'A';
384 ret
+= 'Z' - 'A' + 1;
385 if (m
>= '0' && m
<= '9')
386 return ret
+ m
- '0';
395 int saved_errno
, status
;
400 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
404 if (errno
!= ECHILD
) {
406 clog_warn("sigchild: waitpid:");
412 if (WIFEXITED(status
)) {
413 if (WEXITSTATUS(status
) != 0) {
415 clog_warnx("sigchild: child exit status: %d",
416 WEXITSTATUS(status));
421 clog_warnx("sigchild: child is terminated abnormally");
430 is_g_object_setting(GObject
*o
, char *str
)
432 guint n_props
= 0, i
;
433 GParamSpec
**proplist
;
435 if (! G_IS_OBJECT(o
))
438 proplist
= g_object_class_list_properties(G_OBJECT_GET_CLASS(o
),
441 for (i
=0; i
< n_props
; i
++) {
442 if (! strcmp(proplist
[i
]->name
, str
))
449 get_current_tab(void)
453 TAILQ_FOREACH(t
, &tabs
, entry
) {
454 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
458 warnx("%s: no current tab", __func__
);
464 set_status(struct tab
*t
, gchar
*s
, int status
)
472 case XT_STATUS_LOADING
:
473 type
= g_strdup_printf("Loading: %s", s
);
477 type
= g_strdup_printf("Link: %s", s
);
479 t
->status
= g_strdup(gtk_entry_get_text(
480 GTK_ENTRY(t
->sbe
.statusbar
)));
484 type
= g_strdup_printf("%s", s
);
486 t
->status
= g_strdup(type
);
490 t
->status
= g_strdup(s
);
492 case XT_STATUS_NOTHING
:
497 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.statusbar
), s
);
503 hide_cmd(struct tab
*t
)
505 history_at
= NULL
; /* just in case */
506 search_at
= NULL
; /* just in case */
507 gtk_widget_hide(t
->cmd
);
511 show_cmd(struct tab
*t
)
515 gtk_widget_hide(t
->oops
);
516 gtk_widget_show(t
->cmd
);
520 hide_buffers(struct tab
*t
)
522 gtk_widget_hide(t
->buffers
);
523 gtk_list_store_clear(buffers_store
);
533 sort_tabs_by_page_num(struct tab
***stabs
)
538 num_tabs
= gtk_notebook_get_n_pages(notebook
);
540 *stabs
= g_malloc0(num_tabs
* sizeof(struct tab
*));
542 TAILQ_FOREACH(t
, &tabs
, entry
)
543 (*stabs
)[gtk_notebook_page_num(notebook
, t
->vbox
)] = t
;
549 buffers_make_list(void)
552 const gchar
*title
= NULL
;
554 struct tab
**stabs
= NULL
;
556 num_tabs
= sort_tabs_by_page_num(&stabs
);
558 for (i
= 0; i
< num_tabs
; i
++)
560 gtk_list_store_append(buffers_store
, &iter
);
561 title
= get_title(stabs
[i
], FALSE
);
562 gtk_list_store_set(buffers_store
, &iter
,
563 COL_ID
, i
+ 1, /* Enumerate the tabs starting from 1
573 show_buffers(struct tab
*t
)
576 gtk_widget_show(t
->buffers
);
577 gtk_widget_grab_focus(GTK_WIDGET(t
->buffers
));
581 toggle_buffers(struct tab
*t
)
583 if (gtk_widget_get_visible(t
->buffers
))
590 buffers(struct tab
*t
, struct karg
*args
)
598 hide_oops(struct tab
*t
)
600 gtk_widget_hide(t
->oops
);
604 show_oops(struct tab
*at
, const char *fmt
, ...)
608 struct tab
*t
= NULL
;
614 if ((t
= get_current_tab()) == NULL
)
620 if (vasprintf(&msg
, fmt
, ap
) == -1)
621 errx(1, "show_oops failed");
624 gtk_entry_set_text(GTK_ENTRY(t
->oops
), msg
);
625 gtk_widget_hide(t
->cmd
);
626 gtk_widget_show(t
->oops
);
632 char work_dir
[PATH_MAX
];
633 char certs_dir
[PATH_MAX
];
634 char cache_dir
[PATH_MAX
];
635 char sessions_dir
[PATH_MAX
];
636 char cookie_file
[PATH_MAX
];
637 SoupSession
*session
;
638 SoupCookieJar
*s_cookiejar
;
639 SoupCookieJar
*p_cookiejar
;
640 char rc_fname
[PATH_MAX
];
642 struct mime_type_list mtl
;
643 struct alias_list aliases
;
646 struct tab
*create_new_tab(char *, struct undo
*, int, int);
647 void delete_tab(struct tab
*);
648 void setzoom_webkit(struct tab
*, int);
649 int run_script(struct tab
*, char *);
650 int download_rb_cmp(struct download
*, struct download
*);
651 gboolean
cmd_execute(struct tab
*t
, char *str
);
654 history_rb_cmp(struct history
*h1
, struct history
*h2
)
656 return (strcmp(h1
->uri
, h2
->uri
));
658 RB_GENERATE(history_list
, history
, entry
, history_rb_cmp
);
661 domain_rb_cmp(struct domain
*d1
, struct domain
*d2
)
663 return (strcmp(d1
->d
, d2
->d
));
665 RB_GENERATE(domain_list
, domain
, entry
, domain_rb_cmp
);
668 download_rb_cmp(struct download
*e1
, struct download
*e2
)
670 return (e1
->id
< e2
->id
? -1 : e1
->id
> e2
->id
);
672 RB_GENERATE(download_list
, download
, entry
, download_rb_cmp
);
674 struct valid_url_types
{
685 valid_url_type(char *url
)
689 for (i
= 0; i
< LENGTH(vut
); i
++)
690 if (!strncasecmp(vut
[i
].type
, url
, strlen(vut
[i
].type
)))
697 match_alias(char *url_in
)
701 char *url_out
= NULL
, *search
, *enc_arg
;
703 search
= g_strdup(url_in
);
705 if (strsep(&arg
, " \t") == NULL
) {
706 show_oops(NULL
, "match_alias: NULL URL");
710 TAILQ_FOREACH(a
, &aliases
, entry
) {
711 if (!strcmp(search
, a
->a_name
))
716 DNPRINTF(XT_D_URL
, "match_alias: matched alias %s\n",
719 enc_arg
= soup_uri_encode(arg
, XT_RESERVED_CHARS
);
720 url_out
= g_strdup_printf(a
->a_uri
, enc_arg
);
723 url_out
= g_strdup_printf(a
->a_uri
, "");
731 guess_url_type(char *url_in
)
734 char *url_out
= NULL
, *enc_search
= NULL
;
737 /* substitute aliases */
738 url_out
= match_alias(url_in
);
742 /* see if we are an about page */
743 if (!strncmp(url_in
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
))
744 for (i
= 0; i
< about_list_size(); i
++)
745 if (!strcmp(&url_in
[XT_URI_ABOUT_LEN
],
746 about_list
[i
].name
)) {
747 url_out
= g_strdup(url_in
);
751 if (guess_search
&& url_regex
&&
752 !(g_str_has_prefix(url_in
, "http://") ||
753 g_str_has_prefix(url_in
, "https://"))) {
754 if (regexec(&url_re
, url_in
, 0, NULL
, 0)) {
755 /* invalid URI so search instead */
756 enc_search
= soup_uri_encode(url_in
, XT_RESERVED_CHARS
);
757 url_out
= g_strdup_printf(search_string
, enc_search
);
763 /* XXX not sure about this heuristic */
764 if (stat(url_in
, &sb
) == 0)
765 url_out
= g_strdup_printf("file://%s", url_in
);
767 url_out
= g_strdup_printf("http://%s", url_in
); /* guess http */
769 DNPRINTF(XT_D_URL
, "guess_url_type: guessed %s\n", url_out
);
775 load_uri(struct tab
*t
, gchar
*uri
)
778 gchar
*newuri
= NULL
;
784 /* Strip leading spaces. */
785 while (*uri
&& isspace(*uri
))
788 if (strlen(uri
) == 0) {
793 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
795 if (valid_url_type(uri
)) {
796 newuri
= guess_url_type(uri
);
800 if (!strncmp(uri
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
)) {
801 for (i
= 0; i
< about_list_size(); i
++)
802 if (!strcmp(&uri
[XT_URI_ABOUT_LEN
], about_list
[i
].name
)) {
803 bzero(&args
, sizeof args
);
804 about_list
[i
].func(t
, &args
);
805 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
),
809 show_oops(t
, "invalid about page");
813 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
815 webkit_web_view_load_uri(t
->wv
, uri
);
822 get_uri(struct tab
*t
)
824 const gchar
*uri
= NULL
;
826 if (webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
)
828 if (t
->xtp_meaning
== XT_XTP_TAB_MEANING_NORMAL
) {
829 uri
= webkit_web_view_get_uri(t
->wv
);
831 /* use tmp_uri to make sure it is g_freed */
834 t
->tmp_uri
=g_strdup_printf("%s%s", XT_URI_ABOUT
,
835 about_list
[t
->xtp_meaning
].name
);
842 get_title(struct tab
*t
, bool window
)
844 const gchar
*set
= NULL
, *title
= NULL
;
845 WebKitLoadStatus status
= webkit_web_view_get_load_status(t
->wv
);
847 if (status
== WEBKIT_LOAD_PROVISIONAL
|| status
== WEBKIT_LOAD_FAILED
||
848 t
->xtp_meaning
== XT_XTP_TAB_MEANING_BL
)
851 title
= webkit_web_view_get_title(t
->wv
);
852 if ((set
= title
? title
: get_uri(t
)))
856 set
= window
? XT_NAME
: "(untitled)";
862 find_mime_type(char *mime_type
)
864 struct mime_type
*m
, *def
= NULL
, *rv
= NULL
;
866 TAILQ_FOREACH(m
, &mtl
, entry
) {
868 !strncmp(mime_type
, m
->mt_type
, strlen(m
->mt_type
)))
871 if (m
->mt_default
== 0 && !strcmp(mime_type
, m
->mt_type
)) {
884 wl_find_uri(const gchar
*s
, struct domain_list
*wl
)
890 if (s
== NULL
|| wl
== NULL
)
893 if (!strncmp(s
, "http://", strlen("http://")))
894 s
= &s
[strlen("http://")];
895 else if (!strncmp(s
, "https://", strlen("https://")))
896 s
= &s
[strlen("https://")];
901 for (i
= 0; i
< strlen(s
) + 1 /* yes er need this */; i
++)
902 /* chop string at first slash */
903 if (s
[i
] == '/' || s
[i
] == ':' || s
[i
] == '\0') {
915 js_ref_to_string(JSContextRef context
, JSValueRef ref
)
921 jsref
= JSValueToStringCopy(context
, ref
, NULL
);
925 l
= JSStringGetMaximumUTF8CStringSize(jsref
);
928 JSStringGetUTF8CString(jsref
, s
, l
);
929 JSStringRelease(jsref
);
935 disable_hints(struct tab
*t
)
937 DNPRINTF(XT_D_JS
, "%s\n", __func__
);
939 run_script(t
, "hints.clearHints();");
945 enable_hints(struct tab
*t
)
947 DNPRINTF(XT_D_JS
, "%s\n", __func__
);
949 run_script(t
, JS_HINTING
);
951 run_script(t
, "hints.createHints('', 'F');");
953 run_script(t
, "hints.createHints('', 'f');");
957 #define XT_JS_DONE ("done;")
958 #define XT_JS_DONE_LEN (strlen(XT_JS_DONE))
959 #define XT_JS_INSERT ("insert;")
960 #define XT_JS_INSERT_LEN (strlen(XT_JS_INSERT))
963 run_script(struct tab
*t
, char *s
)
965 JSGlobalContextRef ctx
;
966 WebKitWebFrame
*frame
;
968 JSValueRef val
, exception
;
971 DNPRINTF(XT_D_JS
, "run_script: tab %d %s\n",
972 t
->tab_id
, s
== (char *)JS_HINTING
? "JS_HINTING" : s
);
974 /* a bit silly but it prevents some heartburn */
975 if (t
->script_init
== 0) {
977 run_script(t
, JS_HINTING
);
980 frame
= webkit_web_view_get_main_frame(t
->wv
);
981 ctx
= webkit_web_frame_get_global_context(frame
);
983 str
= JSStringCreateWithUTF8CString(s
);
984 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
985 NULL
, 0, &exception
);
986 JSStringRelease(str
);
988 DNPRINTF(XT_D_JS
, "run_script: val %p\n", val
);
990 es
= js_ref_to_string(ctx
, exception
);
992 /* show_oops(t, "script exception: %s", es); */
993 DNPRINTF(XT_D_JS
, "run_script: exception %s\n", es
);
998 es
= js_ref_to_string(ctx
, val
);
1001 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
1003 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
1007 DNPRINTF(XT_D_JS
, "run_script: val %s\n", es
);
1016 hint(struct tab
*t
, struct karg
*args
)
1019 DNPRINTF(XT_D_JS
, "hint: tab %d args %d\n", t
->tab_id
, args
->i
);
1021 if (t
->hints_on
== 0) {
1022 if (args
->i
== XT_HINT_NEWTAB
)
1032 apply_style(struct tab
*t
)
1034 g_object_set(G_OBJECT(t
->settings
),
1035 "user-stylesheet-uri", t
->stylesheet
, (char *)NULL
);
1039 userstyle(struct tab
*t
, struct karg
*args
)
1041 DNPRINTF(XT_D_JS
, "userstyle: tab %d\n", t
->tab_id
);
1045 g_object_set(G_OBJECT(t
->settings
),
1046 "user-stylesheet-uri", NULL
, (char *)NULL
);
1055 * Doesn't work fully, due to the following bug:
1056 * https://bugs.webkit.org/show_bug.cgi?id=51747
1059 restore_global_history(void)
1061 char file
[PATH_MAX
];
1066 const char delim
[3] = {'\\', '\\', '\0'};
1068 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_HISTORY_FILE
);
1070 if ((f
= fopen(file
, "r")) == NULL
) {
1071 warnx("%s: fopen", __func__
);
1076 if ((uri
= fparseln(f
, NULL
, NULL
, delim
, 0)) == NULL
)
1077 if (feof(f
) || ferror(f
))
1080 if ((title
= fparseln(f
, NULL
, NULL
, delim
, 0)) == NULL
)
1081 if (feof(f
) || ferror(f
)) {
1083 warnx("%s: broken history file\n", __func__
);
1087 if (uri
&& strlen(uri
) && title
&& strlen(title
)) {
1088 webkit_web_history_item_new_with_data(uri
, title
);
1089 h
= g_malloc(sizeof(struct history
));
1090 h
->uri
= g_strdup(uri
);
1091 h
->title
= g_strdup(title
);
1092 RB_INSERT(history_list
, &hl
, h
);
1093 completion_add_uri(h
->uri
);
1095 warnx("%s: failed to restore history\n", __func__
);
1111 save_global_history_to_disk(struct tab
*t
)
1113 char file
[PATH_MAX
];
1117 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_HISTORY_FILE
);
1119 if ((f
= fopen(file
, "w")) == NULL
) {
1120 show_oops(t
, "%s: global history file: %s",
1121 __func__
, strerror(errno
));
1125 RB_FOREACH_REVERSE(h
, history_list
, &hl
) {
1126 if (h
->uri
&& h
->title
)
1127 fprintf(f
, "%s\n%s\n", h
->uri
, h
->title
);
1136 quit(struct tab
*t
, struct karg
*args
)
1138 if (save_global_history
)
1139 save_global_history_to_disk(t
);
1147 restore_sessions_list(void)
1150 struct dirent
*dp
= NULL
;
1153 sdir
= opendir(sessions_dir
);
1155 while ((dp
= readdir(sdir
)) != NULL
)
1156 if (dp
->d_type
== DT_REG
) {
1157 s
= g_malloc(sizeof(struct session
));
1158 s
->name
= g_strdup(dp
->d_name
);
1159 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
1166 open_tabs(struct tab
*t
, struct karg
*a
)
1168 char file
[PATH_MAX
];
1172 struct tab
*ti
, *tt
;
1177 ti
= TAILQ_LAST(&tabs
, tab_list
);
1179 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, a
->s
);
1180 if ((f
= fopen(file
, "r")) == NULL
)
1184 if ((uri
= fparseln(f
, NULL
, NULL
, "\0\0\0", 0)) == NULL
)
1185 if (feof(f
) || ferror(f
))
1188 /* retrieve session name */
1189 if (uri
&& g_str_has_prefix(uri
, XT_SAVE_SESSION_ID
)) {
1190 strlcpy(named_session
,
1191 &uri
[strlen(XT_SAVE_SESSION_ID
)],
1192 sizeof named_session
);
1196 if (uri
&& strlen(uri
))
1197 create_new_tab(uri
, NULL
, 1, -1);
1203 /* close open tabs */
1204 if (a
->i
== XT_SES_CLOSETABS
&& ti
!= NULL
) {
1206 tt
= TAILQ_FIRST(&tabs
);
1227 restore_saved_tabs(void)
1229 char file
[PATH_MAX
];
1230 int unlink_file
= 0;
1235 snprintf(file
, sizeof file
, "%s/%s",
1236 sessions_dir
, XT_RESTART_TABS_FILE
);
1237 if (stat(file
, &sb
) == -1)
1238 a
.s
= XT_SAVED_TABS_FILE
;
1241 a
.s
= XT_RESTART_TABS_FILE
;
1244 a
.i
= XT_SES_DONOTHING
;
1245 rv
= open_tabs(NULL
, &a
);
1254 save_tabs(struct tab
*t
, struct karg
*a
)
1256 char file
[PATH_MAX
];
1258 int num_tabs
= 0, i
;
1259 struct tab
**stabs
= NULL
;
1261 /* tab may be null here */
1266 snprintf(file
, sizeof file
, "%s/%s",
1267 sessions_dir
, named_session
);
1269 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, a
->s
);
1271 if ((f
= fopen(file
, "w")) == NULL
) {
1272 show_oops(t
, "Can't open save_tabs file: %s", strerror(errno
));
1276 /* save session name */
1277 fprintf(f
, "%s%s\n", XT_SAVE_SESSION_ID
, named_session
);
1279 /* Save tabs, in the order they are arranged in the notebook. */
1280 num_tabs
= sort_tabs_by_page_num(&stabs
);
1282 for (i
= 0; i
< num_tabs
; i
++)
1284 if (get_uri(stabs
[i
]) != NULL
)
1285 fprintf(f
, "%s\n", get_uri(stabs
[i
]));
1286 else if (gtk_entry_get_text(GTK_ENTRY(
1287 stabs
[i
]->uri_entry
)))
1288 fprintf(f
, "%s\n", gtk_entry_get_text(GTK_ENTRY(
1289 stabs
[i
]->uri_entry
)));
1294 /* try and make sure this gets to disk NOW. XXX Backup first? */
1295 if (fflush(f
) != 0 || fsync(fileno(f
)) != 0) {
1296 show_oops(t
, "May not have managed to save session: %s",
1306 save_tabs_and_quit(struct tab
*t
, struct karg
*args
)
1318 run_page_script(struct tab
*t
, struct karg
*args
)
1321 char *tmp
, script
[PATH_MAX
];
1323 tmp
= args
->s
!= NULL
&& strlen(args
->s
) > 0 ? args
->s
: default_script
;
1324 if (tmp
[0] == '\0') {
1325 show_oops(t
, "no script specified");
1329 if ((uri
= get_uri(t
)) == NULL
) {
1330 show_oops(t
, "tab is empty, not running script");
1335 snprintf(script
, sizeof script
, "%s/%s",
1336 pwd
->pw_dir
, &tmp
[1]);
1338 strlcpy(script
, tmp
, sizeof script
);
1342 show_oops(t
, "can't fork to run script");
1352 execlp(script
, script
, uri
, (void *)NULL
);
1362 yank_uri(struct tab
*t
, struct karg
*args
)
1365 GtkClipboard
*clipboard
;
1367 if ((uri
= get_uri(t
)) == NULL
)
1370 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1371 gtk_clipboard_set_text(clipboard
, uri
, -1);
1377 paste_uri(struct tab
*t
, struct karg
*args
)
1379 GtkClipboard
*clipboard
;
1380 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
1382 gchar
*p
= NULL
, *uri
;
1384 /* try primary clipboard first */
1385 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1386 p
= gtk_clipboard_wait_for_text(clipboard
);
1388 /* if it failed get whatever text is in cut_buffer0 */
1389 if (p
== NULL
&& xterm_workaround
)
1390 if (gdk_property_get(gdk_get_default_root_window(),
1392 gdk_atom_intern("STRING", FALSE
),
1394 1024 * 1024 /* picked out of my butt */,
1400 /* yes sir, we need to NUL the string */
1406 while (*uri
&& isspace(*uri
))
1408 if (strlen(uri
) == 0) {
1409 show_oops(t
, "empty paste buffer");
1412 if (guess_search
== 0 && valid_url_type(uri
)) {
1413 /* we can be clever and paste this in search box */
1414 show_oops(t
, "not a valid URL");
1418 if (args
->i
== XT_PASTE_CURRENT_TAB
)
1420 else if (args
->i
== XT_PASTE_NEW_TAB
)
1421 create_new_tab(uri
, NULL
, 1, -1);
1432 js_toggle_cb(GtkWidget
*w
, struct tab
*t
)
1437 g_object_get(G_OBJECT(t
->settings
),
1438 "enable-scripts", &es
, (char *)NULL
);
1443 set
= XT_WL_DISABLE
;
1445 a
.i
= set
| XT_WL_TOPLEVEL
;
1448 a
.i
= set
| XT_WL_TOPLEVEL
;
1451 a
.i
= XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
;
1456 toggle_src(struct tab
*t
, struct karg
*args
)
1463 mode
= webkit_web_view_get_view_source_mode(t
->wv
);
1464 webkit_web_view_set_view_source_mode(t
->wv
, !mode
);
1465 webkit_web_view_reload(t
->wv
);
1471 focus_webview(struct tab
*t
)
1476 /* only grab focus if we are visible */
1477 if (gtk_notebook_get_current_page(notebook
) == t
->tab_id
)
1478 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
1482 focus(struct tab
*t
, struct karg
*args
)
1484 if (t
== NULL
|| args
== NULL
)
1490 if (args
->i
== XT_FOCUS_URI
)
1491 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
1492 else if (args
->i
== XT_FOCUS_SEARCH
)
1493 gtk_widget_grab_focus(GTK_WIDGET(t
->search_entry
));
1499 connect_socket_from_uri(const gchar
*uri
, const gchar
**error_str
, char *domain
,
1503 struct addrinfo hints
, *res
= NULL
, *ai
;
1504 int rv
= -1, s
= -1, on
, error
;
1506 static gchar myerror
[256]; /* this is not thread safe */
1509 *error_str
= myerror
;
1510 if (uri
&& !g_str_has_prefix(uri
, "https://")) {
1511 *error_str
= "invalid URI";
1515 su
= soup_uri_new(uri
);
1517 *error_str
= "invalid soup URI";
1520 if (!SOUP_URI_VALID_FOR_HTTP(su
)) {
1521 *error_str
= "invalid HTTPS URI";
1525 snprintf(port
, sizeof port
, "%d", su
->port
);
1526 bzero(&hints
, sizeof(struct addrinfo
));
1527 hints
.ai_flags
= AI_CANONNAME
;
1528 hints
.ai_family
= AF_UNSPEC
;
1529 hints
.ai_socktype
= SOCK_STREAM
;
1531 if ((error
= getaddrinfo(su
->host
, port
, &hints
, &res
))) {
1532 snprintf(myerror
, sizeof myerror
, "getaddrinfo failed: %s",
1533 gai_strerror(errno
));
1537 for (ai
= res
; ai
; ai
= ai
->ai_next
) {
1543 if (ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
)
1545 s
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
1548 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &on
,
1551 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) == 0)
1555 snprintf(myerror
, sizeof myerror
,
1556 "could not obtain certificates from: %s",
1562 strlcpy(domain
, su
->host
, domain_sz
);
1569 if (rv
== -1 && s
!= -1)
1576 stop_tls(gnutls_session_t gsession
, gnutls_certificate_credentials_t xcred
)
1579 gnutls_deinit(gsession
);
1581 gnutls_certificate_free_credentials(xcred
);
1587 start_tls(const gchar
**error_str
, int s
, gnutls_session_t
*gs
,
1588 gnutls_certificate_credentials_t
*xc
)
1590 gnutls_certificate_credentials_t xcred
;
1591 gnutls_session_t gsession
;
1593 static gchar myerror
[1024]; /* this is not thread safe */
1595 if (gs
== NULL
|| xc
== NULL
)
1602 gnutls_certificate_allocate_credentials(&xcred
);
1603 gnutls_certificate_set_x509_trust_file(xcred
, ssl_ca_file
,
1604 GNUTLS_X509_FMT_PEM
);
1606 gnutls_init(&gsession
, GNUTLS_CLIENT
);
1607 gnutls_priority_set_direct(gsession
, "PERFORMANCE", NULL
);
1608 gnutls_credentials_set(gsession
, GNUTLS_CRD_CERTIFICATE
, xcred
);
1609 gnutls_transport_set_ptr(gsession
, (gnutls_transport_ptr_t
)(long)s
);
1610 if ((rv
= gnutls_handshake(gsession
)) < 0) {
1611 snprintf(myerror
, sizeof myerror
,
1612 "gnutls_handshake failed %d fatal %d %s",
1614 gnutls_error_is_fatal(rv
),
1615 gnutls_strerror_name(rv
));
1616 stop_tls(gsession
, xcred
);
1620 gnutls_credentials_type_t cred
;
1621 cred
= gnutls_auth_get_type(gsession
);
1622 if (cred
!= GNUTLS_CRD_CERTIFICATE
) {
1623 snprintf(myerror
, sizeof myerror
,
1624 "gnutls_auth_get_type failed %d",
1626 stop_tls(gsession
, xcred
);
1634 *error_str
= myerror
;
1639 get_connection_certs(gnutls_session_t gsession
, gnutls_x509_crt_t
**certs
,
1643 const gnutls_datum_t
*cl
;
1644 gnutls_x509_crt_t
*all_certs
;
1647 if (certs
== NULL
|| cert_count
== NULL
)
1649 if (gnutls_certificate_type_get(gsession
) != GNUTLS_CRT_X509
)
1651 cl
= gnutls_certificate_get_peers(gsession
, &len
);
1655 all_certs
= g_malloc(sizeof(gnutls_x509_crt_t
) * len
);
1656 for (i
= 0; i
< len
; i
++) {
1657 gnutls_x509_crt_init(&all_certs
[i
]);
1658 if (gnutls_x509_crt_import(all_certs
[i
], &cl
[i
],
1659 GNUTLS_X509_FMT_PEM
< 0)) {
1673 free_connection_certs(gnutls_x509_crt_t
*certs
, size_t cert_count
)
1677 for (i
= 0; i
< cert_count
; i
++)
1678 gnutls_x509_crt_deinit(certs
[i
]);
1683 statusbar_modify_attr(struct tab
*t
, const char *text
, const char *base
)
1685 GdkColor c_text
, c_base
;
1687 gdk_color_parse(text
, &c_text
);
1688 gdk_color_parse(base
, &c_base
);
1690 gtk_widget_modify_text(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_text
);
1691 gtk_widget_modify_text(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_text
);
1692 gtk_widget_modify_text(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_text
);
1693 gtk_widget_modify_text(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_text
);
1695 gtk_widget_modify_base(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_base
);
1696 gtk_widget_modify_base(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_base
);
1697 gtk_widget_modify_base(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_base
);
1698 gtk_widget_modify_base(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_base
);
1702 save_certs(struct tab
*t
, gnutls_x509_crt_t
*certs
,
1703 size_t cert_count
, char *domain
)
1706 char cert_buf
[64 * 1024], file
[PATH_MAX
];
1711 if (t
== NULL
|| certs
== NULL
|| cert_count
<= 0 || domain
== NULL
)
1714 snprintf(file
, sizeof file
, "%s/%s", certs_dir
, domain
);
1715 if ((f
= fopen(file
, "w")) == NULL
) {
1716 show_oops(t
, "Can't create cert file %s %s",
1717 file
, strerror(errno
));
1721 for (i
= 0; i
< cert_count
; i
++) {
1722 cert_buf_sz
= sizeof cert_buf
;
1723 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1724 cert_buf
, &cert_buf_sz
)) {
1725 show_oops(t
, "gnutls_x509_crt_export failed");
1728 if (fwrite(cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1729 show_oops(t
, "Can't write certs: %s", strerror(errno
));
1734 /* not the best spot but oh well */
1735 gdk_color_parse("lightblue", &color
);
1736 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
1737 statusbar_modify_attr(t
, XT_COLOR_BLACK
, "lightblue");
1750 load_compare_cert(const gchar
*uri
, const gchar
**error_str
)
1752 char domain
[8182], file
[PATH_MAX
];
1753 char cert_buf
[64 * 1024], r_cert_buf
[64 * 1024];
1755 unsigned int error
= 0;
1757 size_t cert_buf_sz
, cert_count
;
1758 enum cert_trust rv
= CERT_UNTRUSTED
;
1759 static gchar serr
[80]; /* this isn't thread safe */
1760 gnutls_session_t gsession
;
1761 gnutls_x509_crt_t
*certs
;
1762 gnutls_certificate_credentials_t xcred
;
1764 DNPRINTF(XT_D_URL
, "%s: %s\n", __func__
, uri
);
1768 if ((s
= connect_socket_from_uri(uri
, error_str
, domain
,
1769 sizeof domain
)) == -1)
1772 DNPRINTF(XT_D_URL
, "%s: fd %d\n", __func__
, s
);
1775 if (start_tls(error_str
, s
, &gsession
, &xcred
))
1777 DNPRINTF(XT_D_URL
, "%s: got tls\n", __func__
);
1779 /* verify certs in case cert file doesn't exist */
1780 if (gnutls_certificate_verify_peers2(gsession
, &error
) !=
1782 *error_str
= "Invalid certificates";
1787 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1788 *error_str
= "Can't get connection certificates";
1792 snprintf(file
, sizeof file
, "%s/%s", certs_dir
, domain
);
1793 if ((f
= fopen(file
, "r")) == NULL
) {
1799 for (i
= 0; i
< cert_count
; i
++) {
1800 cert_buf_sz
= sizeof cert_buf
;
1801 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1802 cert_buf
, &cert_buf_sz
)) {
1805 if (fread(r_cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1806 rv
= CERT_BAD
; /* critical */
1809 if (bcmp(r_cert_buf
, cert_buf
, sizeof cert_buf_sz
)) {
1810 rv
= CERT_BAD
; /* critical */
1819 free_connection_certs(certs
, cert_count
);
1821 /* we close the socket first for speed */
1825 /* only complain if we didn't save it locally */
1826 if (error
&& rv
!= CERT_LOCAL
) {
1827 strlcpy(serr
, "Certificate exception(s): ", sizeof serr
);
1828 if (error
& GNUTLS_CERT_INVALID
)
1829 strlcat(serr
, "invalid, ", sizeof serr
);
1830 if (error
& GNUTLS_CERT_REVOKED
)
1831 strlcat(serr
, "revoked, ", sizeof serr
);
1832 if (error
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
1833 strlcat(serr
, "signer not found, ", sizeof serr
);
1834 if (error
& GNUTLS_CERT_SIGNER_NOT_CA
)
1835 strlcat(serr
, "not signed by CA, ", sizeof serr
);
1836 if (error
& GNUTLS_CERT_INSECURE_ALGORITHM
)
1837 strlcat(serr
, "insecure algorithm, ", sizeof serr
);
1838 if (error
& GNUTLS_CERT_NOT_ACTIVATED
)
1839 strlcat(serr
, "not activated, ", sizeof serr
);
1840 if (error
& GNUTLS_CERT_EXPIRED
)
1841 strlcat(serr
, "expired, ", sizeof serr
);
1842 for (i
= strlen(serr
) - 1; i
> 0; i
--)
1843 if (serr
[i
] == ',') {
1850 stop_tls(gsession
, xcred
);
1856 cert_cmd(struct tab
*t
, struct karg
*args
)
1858 const gchar
*uri
, *error_str
= NULL
;
1862 gnutls_session_t gsession
;
1863 gnutls_x509_crt_t
*certs
;
1864 gnutls_certificate_credentials_t xcred
;
1869 if (ssl_ca_file
== NULL
) {
1870 show_oops(t
, "Can't open CA file: %s", ssl_ca_file
);
1874 if ((uri
= get_uri(t
)) == NULL
) {
1875 show_oops(t
, "Invalid URI");
1879 if ((s
= connect_socket_from_uri(uri
, &error_str
, domain
,
1880 sizeof domain
)) == -1) {
1881 show_oops(t
, "%s", error_str
);
1886 if (start_tls(&error_str
, s
, &gsession
, &xcred
))
1890 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1891 show_oops(t
, "get_connection_certs failed");
1895 if (args
->i
& XT_SHOW
)
1896 show_certs(t
, certs
, cert_count
, "Certificate Chain");
1897 else if (args
->i
& XT_SAVE
)
1898 save_certs(t
, certs
, cert_count
, domain
);
1900 free_connection_certs(certs
, cert_count
);
1902 /* we close the socket first for speed */
1905 stop_tls(gsession
, xcred
);
1906 if (error_str
&& strlen(error_str
))
1907 show_oops(t
, "%s", error_str
);
1912 remove_cookie(int index
)
1918 DNPRINTF(XT_D_COOKIE
, "remove_cookie: %d\n", index
);
1920 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
1922 for (i
= 1; cf
; cf
= cf
->next
, i
++) {
1926 print_cookie("remove cookie", c
);
1927 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
1932 soup_cookies_free(cf
);
1938 toplevel_cmd(struct tab
*t
, struct karg
*args
)
1940 js_toggle_cb(t
->js_toggle
, t
);
1946 can_go_back_for_real(struct tab
*t
)
1949 WebKitWebHistoryItem
*item
;
1955 /* rely on webkit to make sure we can go backward when on an about page */
1957 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
1958 return (webkit_web_view_can_go_back(t
->wv
));
1960 /* the back/forwars list is stupid so help determine if we can go back */
1961 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
1963 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
1964 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
1972 can_go_forward_for_real(struct tab
*t
)
1975 WebKitWebHistoryItem
*item
;
1981 /* rely on webkit to make sure we can go forward when on an about page */
1983 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
1984 return (webkit_web_view_can_go_forward(t
->wv
));
1986 /* the back/forwars list is stupid so help selecting a different item */
1987 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
1989 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
1990 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
1998 go_back_for_real(struct tab
*t
)
2001 WebKitWebHistoryItem
*item
;
2009 webkit_web_view_go_back(t
->wv
);
2012 /* the back/forwars list is stupid so help selecting a different item */
2013 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2015 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2016 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2017 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2024 go_forward_for_real(struct tab
*t
)
2027 WebKitWebHistoryItem
*item
;
2035 webkit_web_view_go_forward(t
->wv
);
2038 /* the back/forwars list is stupid so help selecting a different item */
2039 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2041 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2042 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2043 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2050 navaction(struct tab
*t
, struct karg
*args
)
2052 WebKitWebHistoryItem
*item
;
2053 WebKitWebFrame
*frame
;
2055 DNPRINTF(XT_D_NAV
, "navaction: tab %d opcode %d\n",
2056 t
->tab_id
, args
->i
);
2058 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
2060 if (args
->i
== XT_NAV_BACK
)
2061 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2063 item
= webkit_web_back_forward_list_get_forward_item(t
->bfl
);
2065 return (XT_CB_PASSTHROUGH
);
2066 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2068 return (XT_CB_PASSTHROUGH
);
2074 go_back_for_real(t
);
2076 case XT_NAV_FORWARD
:
2078 go_forward_for_real(t
);
2081 frame
= webkit_web_view_get_main_frame(t
->wv
);
2082 webkit_web_frame_reload(frame
);
2085 return (XT_CB_PASSTHROUGH
);
2089 move(struct tab
*t
, struct karg
*args
)
2091 GtkAdjustment
*adjust
;
2092 double pi
, si
, pos
, ps
, upper
, lower
, max
;
2098 case XT_MOVE_BOTTOM
:
2100 case XT_MOVE_PAGEDOWN
:
2101 case XT_MOVE_PAGEUP
:
2102 case XT_MOVE_HALFDOWN
:
2103 case XT_MOVE_HALFUP
:
2104 case XT_MOVE_PERCENT
:
2105 adjust
= t
->adjust_v
;
2108 adjust
= t
->adjust_h
;
2112 pos
= gtk_adjustment_get_value(adjust
);
2113 ps
= gtk_adjustment_get_page_size(adjust
);
2114 upper
= gtk_adjustment_get_upper(adjust
);
2115 lower
= gtk_adjustment_get_lower(adjust
);
2116 si
= gtk_adjustment_get_step_increment(adjust
);
2117 pi
= gtk_adjustment_get_page_increment(adjust
);
2120 DNPRINTF(XT_D_MOVE
, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2121 "max %f si %f pi %f\n",
2122 args
->i
, adjust
== t
->adjust_h
? "horizontal" : "vertical",
2123 pos
, ps
, upper
, lower
, max
, si
, pi
);
2129 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2134 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2136 case XT_MOVE_BOTTOM
:
2137 case XT_MOVE_FARRIGHT
:
2138 gtk_adjustment_set_value(adjust
, max
);
2141 case XT_MOVE_FARLEFT
:
2142 gtk_adjustment_set_value(adjust
, lower
);
2144 case XT_MOVE_PAGEDOWN
:
2146 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2148 case XT_MOVE_PAGEUP
:
2150 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2152 case XT_MOVE_HALFDOWN
:
2154 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2156 case XT_MOVE_HALFUP
:
2158 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2160 case XT_MOVE_PERCENT
:
2161 percent
= atoi(args
->s
) / 100.0;
2162 pos
= max
* percent
;
2163 if (pos
< 0.0 || pos
> max
)
2165 gtk_adjustment_set_value(adjust
, pos
);
2168 return (XT_CB_PASSTHROUGH
);
2171 DNPRINTF(XT_D_MOVE
, "move: new pos %f %f\n", pos
, MIN(pos
, max
));
2173 return (XT_CB_HANDLED
);
2177 url_set_visibility(void)
2181 TAILQ_FOREACH(t
, &tabs
, entry
)
2182 if (show_url
== 0) {
2183 gtk_widget_hide(t
->toolbar
);
2186 gtk_widget_show(t
->toolbar
);
2190 notebook_tab_set_visibility(void)
2192 if (show_tabs
== 0) {
2193 gtk_widget_hide(tab_bar
);
2194 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2196 if (tab_style
== XT_TABS_NORMAL
) {
2197 gtk_widget_hide(tab_bar
);
2198 gtk_notebook_set_show_tabs(notebook
, TRUE
);
2199 } else if (tab_style
== XT_TABS_COMPACT
) {
2200 gtk_widget_show(tab_bar
);
2201 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2207 statusbar_set_visibility(void)
2211 TAILQ_FOREACH(t
, &tabs
, entry
)
2212 if (show_statusbar
== 0) {
2213 gtk_widget_hide(t
->statusbar_box
);
2216 gtk_widget_show(t
->statusbar_box
);
2220 url_set(struct tab
*t
, int enable_url_entry
)
2225 show_url
= enable_url_entry
;
2227 if (enable_url_entry
) {
2228 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
2229 GTK_ENTRY_ICON_PRIMARY
, NULL
);
2230 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
), 0);
2232 pixbuf
= gtk_entry_get_icon_pixbuf(GTK_ENTRY(t
->uri_entry
),
2233 GTK_ENTRY_ICON_PRIMARY
);
2235 gtk_entry_get_progress_fraction(GTK_ENTRY(t
->uri_entry
));
2236 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
2237 GTK_ENTRY_ICON_PRIMARY
, pixbuf
);
2238 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
2244 fullscreen(struct tab
*t
, struct karg
*args
)
2246 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2249 return (XT_CB_PASSTHROUGH
);
2251 if (show_url
== 0) {
2259 url_set_visibility();
2260 notebook_tab_set_visibility();
2262 return (XT_CB_HANDLED
);
2266 statustoggle(struct tab
*t
, struct karg
*args
)
2268 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2270 if (show_statusbar
== 1) {
2272 statusbar_set_visibility();
2273 } else if (show_statusbar
== 0) {
2275 statusbar_set_visibility();
2277 return (XT_CB_HANDLED
);
2281 urlaction(struct tab
*t
, struct karg
*args
)
2283 int rv
= XT_CB_HANDLED
;
2285 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2288 return (XT_CB_PASSTHROUGH
);
2292 if (show_url
== 0) {
2294 url_set_visibility();
2298 if (show_url
== 1) {
2300 url_set_visibility();
2308 tabaction(struct tab
*t
, struct karg
*args
)
2310 int rv
= XT_CB_HANDLED
;
2311 char *url
= args
->s
;
2315 DNPRINTF(XT_D_TAB
, "tabaction: %p %d\n", t
, args
->i
);
2318 return (XT_CB_PASSTHROUGH
);
2322 if (strlen(url
) > 0)
2323 create_new_tab(url
, NULL
, 1, args
->precount
);
2325 create_new_tab(NULL
, NULL
, 1, args
->precount
);
2328 if (args
->precount
< 0)
2331 TAILQ_FOREACH(tt
, &tabs
, entry
)
2332 if (tt
->tab_id
== args
->precount
- 1) {
2337 case XT_TAB_DELQUIT
:
2338 if (gtk_notebook_get_n_pages(notebook
) > 1)
2344 if (strlen(url
) > 0)
2347 rv
= XT_CB_PASSTHROUGH
;
2353 if (show_tabs
== 0) {
2355 notebook_tab_set_visibility();
2359 if (show_tabs
== 1) {
2361 notebook_tab_set_visibility();
2364 case XT_TAB_NEXTSTYLE
:
2365 if (tab_style
== XT_TABS_NORMAL
) {
2366 tab_style
= XT_TABS_COMPACT
;
2367 recolor_compact_tabs();
2370 tab_style
= XT_TABS_NORMAL
;
2371 notebook_tab_set_visibility();
2373 case XT_TAB_UNDO_CLOSE
:
2374 if (undo_count
== 0) {
2375 DNPRINTF(XT_D_TAB
, "%s: no tabs to undo close",
2380 u
= TAILQ_FIRST(&undos
);
2381 create_new_tab(u
->uri
, u
, 1, -1);
2383 TAILQ_REMOVE(&undos
, u
, entry
);
2385 /* u->history is freed in create_new_tab() */
2390 rv
= XT_CB_PASSTHROUGH
;
2404 resizetab(struct tab
*t
, struct karg
*args
)
2406 if (t
== NULL
|| args
== NULL
) {
2407 show_oops(NULL
, "resizetab invalid parameters");
2408 return (XT_CB_PASSTHROUGH
);
2411 DNPRINTF(XT_D_TAB
, "resizetab: tab %d %d\n",
2412 t
->tab_id
, args
->i
);
2414 setzoom_webkit(t
, args
->i
);
2416 return (XT_CB_HANDLED
);
2420 movetab(struct tab
*t
, struct karg
*args
)
2424 if (t
== NULL
|| args
== NULL
) {
2425 show_oops(NULL
, "movetab invalid parameters");
2426 return (XT_CB_PASSTHROUGH
);
2429 DNPRINTF(XT_D_TAB
, "movetab: tab %d opcode %d\n",
2430 t
->tab_id
, args
->i
);
2432 if (args
->i
>= XT_TAB_INVALID
)
2433 return (XT_CB_PASSTHROUGH
);
2435 if (TAILQ_EMPTY(&tabs
))
2436 return (XT_CB_PASSTHROUGH
);
2438 n
= gtk_notebook_get_n_pages(notebook
);
2439 dest
= gtk_notebook_get_current_page(notebook
);
2443 if (args
->precount
< 0)
2444 dest
= dest
== n
- 1 ? 0 : dest
+ 1;
2446 dest
= args
->precount
- 1;
2450 if (args
->precount
< 0)
2453 dest
-= args
->precount
% n
;
2466 return (XT_CB_PASSTHROUGH
);
2469 if (dest
< 0 || dest
>= n
)
2470 return (XT_CB_PASSTHROUGH
);
2471 if (t
->tab_id
== dest
) {
2472 DNPRINTF(XT_D_TAB
, "movetab: do nothing\n");
2473 return (XT_CB_HANDLED
);
2476 set_current_tab(dest
);
2478 return (XT_CB_HANDLED
);
2484 command_mode(struct tab
*t
, struct karg
*args
)
2486 run_script(t
, JS_HINTING
);
2487 if (args
->i
== XT_MODE_COMMAND
)
2488 run_script(t
, "hints.clearFocus();");
2490 run_script(t
, "hints.focusInput();");
2492 return (XT_CB_HANDLED
);
2496 command(struct tab
*t
, struct karg
*args
)
2498 char *s
= NULL
, *ss
= NULL
;
2503 if (t
== NULL
|| args
== NULL
) {
2504 show_oops(NULL
, "command invalid parameters");
2505 return (XT_CB_PASSTHROUGH
);
2516 if (cmd_prefix
== 0)
2519 ss
= g_strdup_printf(":%d", cmd_prefix
);
2525 bzero(&a
, sizeof a
);
2531 bzero(&a
, sizeof a
);
2532 a
.i
= XT_HINT_NEWTAB
;
2542 case XT_CMD_OPEN_CURRENT
:
2545 case XT_CMD_TABNEW_CURRENT
:
2546 if (!s
) /* FALL THROUGH? */
2548 if ((uri
= get_uri(t
)) != NULL
) {
2549 ss
= g_strdup_printf("%s%s", s
, uri
);
2554 show_oops(t
, "command: invalid opcode %d", args
->i
);
2555 return (XT_CB_PASSTHROUGH
);
2558 DNPRINTF(XT_D_CMD
, "command: type %s\n", s
);
2560 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), s
);
2561 gdk_color_parse(XT_COLOR_WHITE
, &color
);
2562 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
2564 gtk_widget_grab_focus(GTK_WIDGET(t
->cmd
));
2565 gtk_editable_set_position(GTK_EDITABLE(t
->cmd
), -1);
2570 return (XT_CB_HANDLED
);
2574 search(struct tab
*t
, struct karg
*args
)
2578 if (t
== NULL
|| args
== NULL
) {
2579 show_oops(NULL
, "search invalid parameters");
2584 case XT_SEARCH_NEXT
:
2585 d
= t
->search_forward
;
2587 case XT_SEARCH_PREV
:
2588 d
= !t
->search_forward
;
2591 return (XT_CB_PASSTHROUGH
);
2594 if (t
->search_text
== NULL
) {
2595 if (global_search
== NULL
)
2596 return (XT_CB_PASSTHROUGH
);
2598 d
= t
->search_forward
= TRUE
;
2599 t
->search_text
= g_strdup(global_search
);
2600 webkit_web_view_mark_text_matches(t
->wv
, global_search
, FALSE
, 0);
2601 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
2605 DNPRINTF(XT_D_CMD
, "search: tab %d opc %d forw %d text %s\n",
2606 t
->tab_id
, args
->i
, t
->search_forward
, t
->search_text
);
2608 webkit_web_view_search_text(t
->wv
, t
->search_text
, FALSE
, d
, TRUE
);
2610 return (XT_CB_HANDLED
);
2614 session_save(struct tab
*t
, char *filename
)
2620 if (strlen(filename
) == 0)
2623 if (filename
[0] == '.' || filename
[0] == '/')
2627 if (save_tabs(t
, &a
))
2629 strlcpy(named_session
, filename
, sizeof named_session
);
2631 /* add the new session to the list of sessions */
2632 s
= g_malloc(sizeof(struct session
));
2633 s
->name
= g_strdup(filename
);
2634 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
2642 session_open(struct tab
*t
, char *filename
)
2647 if (strlen(filename
) == 0)
2650 if (filename
[0] == '.' || filename
[0] == '/')
2654 a
.i
= XT_SES_CLOSETABS
;
2655 if (open_tabs(t
, &a
))
2658 strlcpy(named_session
, filename
, sizeof named_session
);
2666 session_delete(struct tab
*t
, char *filename
)
2668 char file
[PATH_MAX
];
2672 if (strlen(filename
) == 0)
2675 if (filename
[0] == '.' || filename
[0] == '/')
2678 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, filename
);
2682 if (!strcmp(filename
, named_session
))
2683 strlcpy(named_session
, XT_SAVED_TABS_FILE
,
2684 sizeof named_session
);
2686 /* remove session from sessions list */
2687 TAILQ_FOREACH(s
, &sessions
, entry
) {
2688 if (!strcmp(s
->name
, filename
))
2693 TAILQ_REMOVE(&sessions
, s
, entry
);
2694 g_free((gpointer
) s
->name
);
2703 session_cmd(struct tab
*t
, struct karg
*args
)
2705 char *filename
= args
->s
;
2710 if (args
->i
& XT_SHOW
)
2711 show_oops(t
, "Current session: %s", named_session
[0] == '\0' ?
2712 XT_SAVED_TABS_FILE
: named_session
);
2713 else if (args
->i
& XT_SAVE
) {
2714 if (session_save(t
, filename
)) {
2715 show_oops(t
, "Can't save session: %s",
2716 filename
? filename
: "INVALID");
2719 } else if (args
->i
& XT_OPEN
) {
2720 if (session_open(t
, filename
)) {
2721 show_oops(t
, "Can't open session: %s",
2722 filename
? filename
: "INVALID");
2725 } else if (args
->i
& XT_DELETE
) {
2726 if (session_delete(t
, filename
)) {
2727 show_oops(t
, "Can't delete session: %s",
2728 filename
? filename
: "INVALID");
2733 return (XT_CB_PASSTHROUGH
);
2737 script_cmd(struct tab
*t
, struct karg
*args
)
2739 JSGlobalContextRef ctx
;
2740 WebKitWebFrame
*frame
;
2742 JSValueRef val
, exception
;
2751 if ((f
= fopen(args
->s
, "r")) == NULL
) {
2752 show_oops(t
, "Can't open script file: %s", args
->s
);
2756 if (fstat(fileno(f
), &sb
) == -1) {
2757 show_oops(t
, "Can't stat script file: %s", args
->s
);
2761 buf
= g_malloc0(sb
.st_size
+ 1);
2762 if (fread(buf
, 1, sb
.st_size
, f
) != sb
.st_size
) {
2763 show_oops(t
, "Can't read script file: %s", args
->s
);
2767 /* this code needs to be redone */
2768 frame
= webkit_web_view_get_main_frame(t
->wv
);
2769 ctx
= webkit_web_frame_get_global_context(frame
);
2771 str
= JSStringCreateWithUTF8CString(buf
);
2772 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
2773 NULL
, 0, &exception
);
2774 JSStringRelease(str
);
2776 DNPRINTF(XT_D_JS
, "run_script: val %p\n", val
);
2778 es
= js_ref_to_string(ctx
, exception
);
2780 show_oops(t
, "script exception: %s", es
);
2785 es
= js_ref_to_string(ctx
, val
);
2788 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
2790 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
2794 show_oops(t
, "script complete return value: '%s'", es
);
2797 show_oops(t
, "script complete: without a return value");
2806 return (XT_CB_PASSTHROUGH
);
2810 * Make a hardcopy of the page
2813 print_page(struct tab
*t
, struct karg
*args
)
2815 WebKitWebFrame
*frame
;
2817 GtkPrintOperation
*op
;
2818 GtkPrintOperationAction action
;
2819 GtkPrintOperationResult print_res
;
2820 GError
*g_err
= NULL
;
2821 int marg_l
, marg_r
, marg_t
, marg_b
;
2823 DNPRINTF(XT_D_PRINTING
, "%s:", __func__
);
2825 ps
= gtk_page_setup_new();
2826 op
= gtk_print_operation_new();
2827 action
= GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
;
2828 frame
= webkit_web_view_get_main_frame(t
->wv
);
2830 /* the default margins are too small, so we will bump them */
2831 marg_l
= gtk_page_setup_get_left_margin(ps
, GTK_UNIT_MM
) +
2832 XT_PRINT_EXTRA_MARGIN
;
2833 marg_r
= gtk_page_setup_get_right_margin(ps
, GTK_UNIT_MM
) +
2834 XT_PRINT_EXTRA_MARGIN
;
2835 marg_t
= gtk_page_setup_get_top_margin(ps
, GTK_UNIT_MM
) +
2836 XT_PRINT_EXTRA_MARGIN
;
2837 marg_b
= gtk_page_setup_get_bottom_margin(ps
, GTK_UNIT_MM
) +
2838 XT_PRINT_EXTRA_MARGIN
;
2841 gtk_page_setup_set_left_margin(ps
, marg_l
, GTK_UNIT_MM
);
2842 gtk_page_setup_set_right_margin(ps
, marg_r
, GTK_UNIT_MM
);
2843 gtk_page_setup_set_top_margin(ps
, marg_t
, GTK_UNIT_MM
);
2844 gtk_page_setup_set_bottom_margin(ps
, marg_b
, GTK_UNIT_MM
);
2846 gtk_print_operation_set_default_page_setup(op
, ps
);
2848 /* this appears to free 'op' and 'ps' */
2849 print_res
= webkit_web_frame_print_full(frame
, op
, action
, &g_err
);
2851 /* check it worked */
2852 if (print_res
== GTK_PRINT_OPERATION_RESULT_ERROR
) {
2853 show_oops(NULL
, "can't print: %s", g_err
->message
);
2854 g_error_free (g_err
);
2862 go_home(struct tab
*t
, struct karg
*args
)
2869 set_encoding(struct tab
*t
, struct karg
*args
)
2873 if (args
->s
&& strlen(g_strstrip(args
->s
)) == 0) {
2874 e
= webkit_web_view_get_custom_encoding(t
->wv
);
2876 e
= webkit_web_view_get_encoding(t
->wv
);
2877 show_oops(t
, "encoding: %s", e
? e
: "N/A");
2879 webkit_web_view_set_custom_encoding(t
->wv
, args
->s
);
2885 restart(struct tab
*t
, struct karg
*args
)
2889 a
.s
= XT_RESTART_TABS_FILE
;
2891 execvp(start_argv
[0], start_argv
);
2900 int (*func
)(struct tab
*, struct karg
*);
2904 { "command_mode", 0, command_mode
, XT_MODE_COMMAND
, 0 },
2905 { "insert_mode", 0, command_mode
, XT_MODE_INSERT
, 0 },
2906 { "command", 0, command
, ':', 0 },
2907 { "search", 0, command
, '/', 0 },
2908 { "searchb", 0, command
, '?', 0 },
2909 { "hinting", 0, command
, '.', 0 },
2910 { "hinting_newtab", 0, command
, ',', 0 },
2911 { "togglesrc", 0, toggle_src
, 0, 0 },
2913 /* yanking and pasting */
2914 { "yankuri", 0, yank_uri
, 0, 0 },
2915 /* XXX: pasteuri{cur,new} do not work from the cmd_entry? */
2916 { "pasteuricur", 0, paste_uri
, XT_PASTE_CURRENT_TAB
, 0 },
2917 { "pasteurinew", 0, paste_uri
, XT_PASTE_NEW_TAB
, 0 },
2920 { "searchnext", 0, search
, XT_SEARCH_NEXT
, 0 },
2921 { "searchprevious", 0, search
, XT_SEARCH_PREV
, 0 },
2924 { "focusaddress", 0, focus
, XT_FOCUS_URI
, 0 },
2925 { "focussearch", 0, focus
, XT_FOCUS_SEARCH
, 0 },
2928 { "hinting", 0, hint
, 0, 0 },
2929 { "hinting_newtab", 0, hint
, XT_HINT_NEWTAB
, 0 },
2931 /* custom stylesheet */
2932 { "userstyle", 0, userstyle
, 0, 0 },
2935 { "goback", 0, navaction
, XT_NAV_BACK
, 0 },
2936 { "goforward", 0, navaction
, XT_NAV_FORWARD
, 0 },
2937 { "reload", 0, navaction
, XT_NAV_RELOAD
, 0 },
2939 /* vertical movement */
2940 { "scrolldown", 0, move
, XT_MOVE_DOWN
, 0 },
2941 { "scrollup", 0, move
, XT_MOVE_UP
, 0 },
2942 { "scrollbottom", 0, move
, XT_MOVE_BOTTOM
, 0 },
2943 { "scrolltop", 0, move
, XT_MOVE_TOP
, 0 },
2944 { "1", 0, move
, XT_MOVE_TOP
, 0 },
2945 { "scrollhalfdown", 0, move
, XT_MOVE_HALFDOWN
, 0 },
2946 { "scrollhalfup", 0, move
, XT_MOVE_HALFUP
, 0 },
2947 { "scrollpagedown", 0, move
, XT_MOVE_PAGEDOWN
, 0 },
2948 { "scrollpageup", 0, move
, XT_MOVE_PAGEUP
, 0 },
2949 /* horizontal movement */
2950 { "scrollright", 0, move
, XT_MOVE_RIGHT
, 0 },
2951 { "scrollleft", 0, move
, XT_MOVE_LEFT
, 0 },
2952 { "scrollfarright", 0, move
, XT_MOVE_FARRIGHT
, 0 },
2953 { "scrollfarleft", 0, move
, XT_MOVE_FARLEFT
, 0 },
2955 { "favorites", 0, xtp_page_fl
, 0, 0 },
2956 { "fav", 0, xtp_page_fl
, 0, 0 },
2957 { "favadd", 0, add_favorite
, 0, 0 },
2959 { "qall", 0, quit
, 0, 0 },
2960 { "quitall", 0, quit
, 0, 0 },
2961 { "w", 0, save_tabs
, 0, 0 },
2962 { "wq", 0, save_tabs_and_quit
, 0, 0 },
2963 { "help", 0, help
, 0, 0 },
2964 { "about", 0, about
, 0, 0 },
2965 { "stats", 0, stats
, 0, 0 },
2966 { "version", 0, about
, 0, 0 },
2969 { "js", 0, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2970 { "save", 1, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
2971 { "domain", 2, js_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
2972 { "fqdn", 2, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
2973 { "show", 1, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2974 { "all", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2975 { "persistent", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
2976 { "session", 2, js_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
2977 { "toggle", 1, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
2978 { "domain", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
2979 { "fqdn", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
2981 /* cookie command */
2982 { "cookie", 0, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2983 { "save", 1, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
2984 { "domain", 2, cookie_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
2985 { "fqdn", 2, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
2986 { "show", 1, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2987 { "all", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2988 { "persistent", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
2989 { "session", 2, cookie_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
2990 { "toggle", 1, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
2991 { "domain", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
2992 { "fqdn", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
2994 /* plugin command */
2995 { "plugin", 0, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2996 { "save", 1, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
2997 { "domain", 2, pl_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
2998 { "fqdn", 2, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
2999 { "show", 1, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3000 { "all", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3001 { "persistent", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3002 { "session", 2, pl_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3003 { "toggle", 1, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3004 { "domain", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3005 { "fqdn", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3007 /* toplevel (domain) command */
3008 { "toplevel", 0, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3009 { "toggle", 1, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3012 { "cookiejar", 0, xtp_page_cl
, 0, 0 },
3015 { "cert", 0, cert_cmd
, XT_SHOW
, 0 },
3016 { "save", 1, cert_cmd
, XT_SAVE
, 0 },
3017 { "show", 1, cert_cmd
, XT_SHOW
, 0 },
3019 { "ca", 0, ca_cmd
, 0, 0 },
3020 { "downloadmgr", 0, xtp_page_dl
, 0, 0 },
3021 { "dl", 0, xtp_page_dl
, 0, 0 },
3022 { "h", 0, xtp_page_hl
, 0, 0 },
3023 { "history", 0, xtp_page_hl
, 0, 0 },
3024 { "home", 0, go_home
, 0, 0 },
3025 { "restart", 0, restart
, 0, 0 },
3026 { "urlhide", 0, urlaction
, XT_URL_HIDE
, 0 },
3027 { "urlshow", 0, urlaction
, XT_URL_SHOW
, 0 },
3028 { "statustoggle", 0, statustoggle
, 0, 0 },
3029 { "run_script", 0, run_page_script
, 0, XT_USERARG
},
3031 { "print", 0, print_page
, 0, 0 },
3034 { "focusin", 0, resizetab
, XT_ZOOM_IN
, 0 },
3035 { "focusout", 0, resizetab
, XT_ZOOM_OUT
, 0 },
3036 { "focusreset", 0, resizetab
, XT_ZOOM_NORMAL
, 0 },
3037 { "q", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3038 { "quit", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3039 { "open", 0, tabaction
, XT_TAB_OPEN
, XT_URLARG
},
3040 { "tabclose", 0, tabaction
, XT_TAB_DELETE
, XT_PREFIX
| XT_INTARG
},
3041 { "tabedit", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3042 { "tabfirst", 0, movetab
, XT_TAB_FIRST
, 0 },
3043 { "tabhide", 0, tabaction
, XT_TAB_HIDE
, 0 },
3044 { "tablast", 0, movetab
, XT_TAB_LAST
, 0 },
3045 { "tabnew", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3046 { "tabnext", 0, movetab
, XT_TAB_NEXT
, XT_PREFIX
| XT_INTARG
},
3047 { "tabnextstyle", 0, tabaction
, XT_TAB_NEXTSTYLE
, 0 },
3048 { "tabprevious", 0, movetab
, XT_TAB_PREV
, XT_PREFIX
| XT_INTARG
},
3049 { "tabrewind", 0, movetab
, XT_TAB_FIRST
, 0 },
3050 { "tabshow", 0, tabaction
, XT_TAB_SHOW
, 0 },
3051 { "tabs", 0, buffers
, 0, 0 },
3052 { "tabundoclose", 0, tabaction
, XT_TAB_UNDO_CLOSE
, 0 },
3053 { "buffers", 0, buffers
, 0, 0 },
3054 { "ls", 0, buffers
, 0, 0 },
3055 { "encoding", 0, set_encoding
, 0, XT_USERARG
},
3057 /* command aliases (handy when -S flag is used) */
3058 { "promptopen", 0, command
, XT_CMD_OPEN
, 0 },
3059 { "promptopencurrent", 0, command
, XT_CMD_OPEN_CURRENT
, 0 },
3060 { "prompttabnew", 0, command
, XT_CMD_TABNEW
, 0 },
3061 { "prompttabnewcurrent",0, command
, XT_CMD_TABNEW_CURRENT
, 0 },
3064 { "set", 0, set
, 0, XT_SETARG
},
3066 { "fullscreen", 0, fullscreen
, 0, 0 },
3067 { "f", 0, fullscreen
, 0, 0 },
3070 { "session", 0, session_cmd
, XT_SHOW
, 0 },
3071 { "delete", 1, session_cmd
, XT_DELETE
, XT_SESSARG
},
3072 { "open", 1, session_cmd
, XT_OPEN
, XT_SESSARG
},
3073 { "save", 1, session_cmd
, XT_SAVE
, XT_USERARG
},
3074 { "show", 1, session_cmd
, XT_SHOW
, 0 },
3076 /* external javascript */
3077 { "script", 0, script_cmd
, XT_EJS_SHOW
, XT_USERARG
},
3080 { "inspector", 0, inspector_cmd
, XT_INS_SHOW
, 0 },
3081 { "show", 1, inspector_cmd
, XT_INS_SHOW
, 0 },
3082 { "hide", 1, inspector_cmd
, XT_INS_HIDE
, 0 },
3089 } cmd_status
= {-1, 0};
3092 wv_release_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3095 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 1)
3102 wv_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3109 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3111 else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 8 /* btn 4 */) {
3117 } else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 9 /* btn 5 */) {
3119 a
.i
= XT_NAV_FORWARD
;
3129 tab_close_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3131 DNPRINTF(XT_D_TAB
, "tab_close_cb: tab %d\n", t
->tab_id
);
3133 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3142 activate_uri_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3144 const gchar
*uri
= gtk_entry_get_text(GTK_ENTRY(entry
));
3146 DNPRINTF(XT_D_URL
, "activate_uri_entry_cb: %s\n", uri
);
3149 show_oops(NULL
, "activate_uri_entry_cb invalid parameters");
3154 show_oops(t
, "activate_uri_entry_cb no uri");
3158 uri
+= strspn(uri
, "\t ");
3160 /* if xxxt:// treat specially */
3161 if (parse_xtp_url(t
, uri
))
3164 /* otherwise continue to load page normally */
3165 load_uri(t
, (gchar
*)uri
);
3170 activate_search_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3172 const gchar
*search
= gtk_entry_get_text(GTK_ENTRY(entry
));
3173 char *newuri
= NULL
;
3176 DNPRINTF(XT_D_URL
, "activate_search_entry_cb: %s\n", search
);
3179 show_oops(NULL
, "activate_search_entry_cb invalid parameters");
3183 if (search_string
== NULL
) {
3184 show_oops(t
, "no search_string");
3188 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
3190 enc_search
= soup_uri_encode(search
, XT_RESERVED_CHARS
);
3191 newuri
= g_strdup_printf(search_string
, enc_search
);
3195 webkit_web_view_load_uri(t
->wv
, newuri
);
3203 check_and_set_cookie(const gchar
*uri
, struct tab
*t
)
3205 struct domain
*d
= NULL
;
3208 if (uri
== NULL
|| t
== NULL
)
3211 if ((d
= wl_find_uri(uri
, &c_wl
)) == NULL
)
3216 DNPRINTF(XT_D_COOKIE
, "check_and_set_cookie: %s %s\n",
3217 es
? "enable" : "disable", uri
);
3219 g_object_set(G_OBJECT(t
->settings
),
3220 "enable-html5-local-storage", es
, (char *)NULL
);
3221 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3225 check_and_set_js(const gchar
*uri
, struct tab
*t
)
3227 struct domain
*d
= NULL
;
3230 if (uri
== NULL
|| t
== NULL
)
3233 if ((d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3238 DNPRINTF(XT_D_JS
, "check_and_set_js: %s %s\n",
3239 es
? "enable" : "disable", uri
);
3241 g_object_set(G_OBJECT(t
->settings
),
3242 "enable-scripts", es
, (char *)NULL
);
3243 g_object_set(G_OBJECT(t
->settings
),
3244 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
3245 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3247 button_set_stockid(t
->js_toggle
,
3248 es
? GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
);
3252 check_and_set_pl(const gchar
*uri
, struct tab
*t
)
3254 struct domain
*d
= NULL
;
3257 if (uri
== NULL
|| t
== NULL
)
3260 if ((d
= wl_find_uri(uri
, &pl_wl
)) == NULL
)
3265 DNPRINTF(XT_D_JS
, "check_and_set_pl: %s %s\n",
3266 es
? "enable" : "disable", uri
);
3268 g_object_set(G_OBJECT(t
->settings
),
3269 "enable-plugins", es
, (char *)NULL
);
3270 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3274 color_address_bar(gpointer p
)
3277 struct tab
*tt
, *t
= p
;
3278 gchar
*col_str
= XT_COLOR_WHITE
;
3279 const gchar
*uri
, *u
= NULL
, *error_str
= NULL
;
3282 gdk_threads_enter();
3284 DNPRINTF(XT_D_URL
, "%s:\n", __func__
);
3286 /* make sure t still exists */
3289 TAILQ_FOREACH(tt
, &tabs
, entry
)
3295 if ((uri
= get_uri(t
)) == NULL
)
3300 gdk_threads_leave();
3303 col_str
= XT_COLOR_YELLOW
;
3304 switch (load_compare_cert(u
, &error_str
)) {
3306 col_str
= XT_COLOR_BLUE
;
3309 col_str
= XT_COLOR_GREEN
;
3311 case CERT_UNTRUSTED
:
3312 col_str
= XT_COLOR_YELLOW
;
3315 col_str
= XT_COLOR_RED
;
3320 gdk_threads_enter();
3322 /* make sure t isn't deleted */
3323 TAILQ_FOREACH(tt
, &tabs
, entry
)
3330 /* test to see if the user navigated away and canceled the thread */
3331 if (t
->thread
!= g_thread_self())
3333 if ((uri
= get_uri(t
)) == NULL
) {
3337 if (strcmp(uri
, u
)) {
3338 /* make sure we are still the same url */
3344 gdk_color_parse(col_str
, &color
);
3345 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3347 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3348 statusbar_modify_attr(t
, col_str
, XT_COLOR_BLACK
);
3350 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3352 if (error_str
&& error_str
[0] != '\0')
3353 show_oops(t
, "%s", error_str
);
3358 /* t is invalid at this point */
3360 g_free((gpointer
)u
);
3362 gdk_threads_leave();
3367 show_ca_status(struct tab
*t
, const char *uri
)
3370 gchar
*col_str
= XT_COLOR_WHITE
;
3372 DNPRINTF(XT_D_URL
, "show_ca_status: %d %s %s\n",
3373 ssl_strict_certs
, ssl_ca_file
, uri
);
3380 if (ssl_ca_file
== NULL
) {
3381 if (g_str_has_prefix(uri
, "http://"))
3383 if (g_str_has_prefix(uri
, "https://")) {
3384 col_str
= XT_COLOR_RED
;
3389 if (g_str_has_prefix(uri
, "http://") ||
3390 !g_str_has_prefix(uri
, "https://"))
3394 * It is not necessary to see if the thread is already running.
3395 * If the thread is in progress setting it to something else aborts it
3399 /* thread the coloring of the address bar */
3400 t
->thread
= g_thread_create((GThreadFunc
)color_address_bar
, t
, TRUE
, NULL
);
3402 color_address_bar(t
);
3408 gdk_color_parse(col_str
, &color
);
3409 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3411 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3412 statusbar_modify_attr(t
, col_str
, XT_COLOR_BLACK
);
3414 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3419 free_favicon(struct tab
*t
)
3421 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p req %p\n",
3422 __func__
, t
->icon_download
, t
->icon_request
);
3424 if (t
->icon_request
)
3425 g_object_unref(t
->icon_request
);
3426 if (t
->icon_dest_uri
)
3427 g_free(t
->icon_dest_uri
);
3429 t
->icon_request
= NULL
;
3430 t
->icon_dest_uri
= NULL
;
3434 xt_icon_from_name(struct tab
*t
, gchar
*name
)
3436 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->uri_entry
),
3437 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3439 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3440 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3442 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3443 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3447 xt_icon_from_pixbuf(struct tab
*t
, GdkPixbuf
*pb
)
3449 GdkPixbuf
*pb_scaled
;
3451 if (gdk_pixbuf_get_width(pb
) > 16 || gdk_pixbuf_get_height(pb
) > 16)
3452 pb_scaled
= gdk_pixbuf_scale_simple(pb
, 16, 16,
3453 GDK_INTERP_BILINEAR
);
3457 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
3458 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3460 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
3461 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3463 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3464 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3466 if (pb_scaled
!= pb
)
3467 g_object_unref(pb_scaled
);
3471 xt_icon_from_file(struct tab
*t
, char *file
)
3475 if (g_str_has_prefix(file
, "file://"))
3476 file
+= strlen("file://");
3478 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
3480 xt_icon_from_pixbuf(t
, pb
);
3483 xt_icon_from_name(t
, "text-html");
3487 is_valid_icon(char *file
)
3490 const char *mime_type
;
3494 gf
= g_file_new_for_path(file
);
3495 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
3497 mime_type
= g_file_info_get_content_type(fi
);
3498 valid
= g_strcmp0(mime_type
, "image/x-ico") == 0 ||
3499 g_strcmp0(mime_type
, "image/vnd.microsoft.icon") == 0 ||
3500 g_strcmp0(mime_type
, "image/png") == 0 ||
3501 g_strcmp0(mime_type
, "image/gif") == 0 ||
3502 g_strcmp0(mime_type
, "application/octet-stream") == 0;
3510 set_favicon_from_file(struct tab
*t
, char *file
)
3514 if (t
== NULL
|| file
== NULL
)
3517 if (g_str_has_prefix(file
, "file://"))
3518 file
+= strlen("file://");
3519 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading %s\n", __func__
, file
);
3521 if (!stat(file
, &sb
)) {
3522 if (sb
.st_size
== 0 || !is_valid_icon(file
)) {
3523 /* corrupt icon so trash it */
3524 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3527 /* no need to set icon to default here */
3531 xt_icon_from_file(t
, file
);
3535 favicon_download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
3538 WebKitDownloadStatus status
= webkit_download_get_status(download
);
3539 struct tab
*tt
= NULL
, *t
= NULL
;
3542 * find the webview instead of passing in the tab as it could have been
3543 * deleted from underneath us.
3545 TAILQ_FOREACH(tt
, &tabs
, entry
) {
3554 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d status %d\n",
3555 __func__
, t
->tab_id
, status
);
3558 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
3560 t
->icon_download
= NULL
;
3563 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
3566 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
3569 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
3571 DNPRINTF(XT_D_DOWNLOAD
, "%s: freeing favicon %d\n",
3572 __func__
, t
->tab_id
);
3573 t
->icon_download
= NULL
;
3576 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
3579 DNPRINTF(XT_D_DOWNLOAD
, "%s: setting icon to %s\n",
3580 __func__
, t
->icon_dest_uri
);
3581 set_favicon_from_file(t
, t
->icon_dest_uri
);
3582 /* these will be freed post callback */
3583 t
->icon_request
= NULL
;
3584 t
->icon_download
= NULL
;
3592 abort_favicon_download(struct tab
*t
)
3594 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p\n", __func__
, t
->icon_download
);
3596 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
3597 if (t
->icon_download
) {
3598 g_signal_handlers_disconnect_by_func(G_OBJECT(t
->icon_download
),
3599 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3600 webkit_download_cancel(t
->icon_download
);
3601 t
->icon_download
= NULL
;
3606 xt_icon_from_name(t
, "text-html");
3610 notify_icon_loaded_cb(WebKitWebView
*wv
, gchar
*uri
, struct tab
*t
)
3612 DNPRINTF(XT_D_DOWNLOAD
, "%s %s\n", __func__
, uri
);
3614 if (uri
== NULL
|| t
== NULL
)
3617 #if WEBKIT_CHECK_VERSION(1, 4, 0)
3618 /* take icon from WebKitIconDatabase */
3621 pb
= webkit_web_view_get_icon_pixbuf(wv
);
3623 xt_icon_from_pixbuf(t
, pb
);
3626 xt_icon_from_name(t
, "text-html");
3627 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
3628 /* download icon to cache dir */
3629 gchar
*name_hash
, file
[PATH_MAX
];
3632 if (t
->icon_request
) {
3633 DNPRINTF(XT_D_DOWNLOAD
, "%s: download in progress\n", __func__
);
3637 /* check to see if we got the icon in cache */
3638 name_hash
= g_compute_checksum_for_string(G_CHECKSUM_SHA256
, uri
, -1);
3639 snprintf(file
, sizeof file
, "%s/%s.ico", cache_dir
, name_hash
);
3642 if (!stat(file
, &sb
)) {
3643 if (sb
.st_size
> 0) {
3644 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading from cache %s\n",
3646 set_favicon_from_file(t
, file
);
3650 /* corrupt icon so trash it */
3651 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3656 /* create download for icon */
3657 t
->icon_request
= webkit_network_request_new(uri
);
3658 if (t
->icon_request
== NULL
) {
3659 DNPRINTF(XT_D_DOWNLOAD
, "%s: invalid uri %s\n",
3664 t
->icon_download
= webkit_download_new(t
->icon_request
);
3665 if (t
->icon_download
== NULL
)
3668 /* we have to free icon_dest_uri later */
3669 t
->icon_dest_uri
= g_strdup_printf("file://%s", file
);
3670 webkit_download_set_destination_uri(t
->icon_download
,
3673 if (webkit_download_get_status(t
->icon_download
) ==
3674 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
3675 g_object_unref(t
->icon_request
);
3676 g_free(t
->icon_dest_uri
);
3677 t
->icon_request
= NULL
;
3678 t
->icon_dest_uri
= NULL
;
3682 g_signal_connect(G_OBJECT(t
->icon_download
), "notify::status",
3683 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3685 webkit_download_start(t
->icon_download
);
3690 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3692 const gchar
*uri
= NULL
, *title
= NULL
;
3693 struct history
*h
, find
;
3697 DNPRINTF(XT_D_URL
, "notify_load_status_cb: %d %s\n",
3698 webkit_web_view_get_load_status(wview
),
3699 get_uri(t
) ? get_uri(t
) : "NOTHING");
3702 show_oops(NULL
, "notify_load_status_cb invalid parameters");
3706 switch (webkit_web_view_get_load_status(wview
)) {
3707 case WEBKIT_LOAD_PROVISIONAL
:
3709 abort_favicon_download(t
);
3710 #if GTK_CHECK_VERSION(2, 20, 0)
3711 gtk_widget_show(t
->spinner
);
3712 gtk_spinner_start(GTK_SPINNER(t
->spinner
));
3714 gtk_label_set_text(GTK_LABEL(t
->label
), "Loading");
3716 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), TRUE
);
3718 /* assume we are a new address */
3719 gdk_color_parse("white", &color
);
3720 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3721 statusbar_modify_attr(t
, "white", XT_COLOR_BLACK
);
3723 /* take focus if we are visible */
3729 /* kill color thread */
3734 case WEBKIT_LOAD_COMMITTED
:
3739 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
3745 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
3747 /* check if js white listing is enabled */
3748 if (enable_plugin_whitelist
)
3749 check_and_set_pl(uri
, t
);
3750 if (enable_cookie_whitelist
)
3751 check_and_set_cookie(uri
, t
);
3752 if (enable_js_whitelist
)
3753 check_and_set_js(uri
, t
);
3759 /* we know enough to autosave the session */
3760 if (session_autosave
) {
3765 show_ca_status(t
, uri
);
3768 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
3772 case WEBKIT_LOAD_FINISHED
:
3778 if (!strncmp(uri
, "http://", strlen("http://")) ||
3779 !strncmp(uri
, "https://", strlen("https://")) ||
3780 !strncmp(uri
, "file://", strlen("file://"))) {
3782 h
= RB_FIND(history_list
, &hl
, &find
);
3784 title
= get_title(t
, FALSE
);
3785 h
= g_malloc(sizeof *h
);
3786 h
->uri
= g_strdup(uri
);
3787 h
->title
= g_strdup(title
);
3788 RB_INSERT(history_list
, &hl
, h
);
3789 completion_add_uri(h
->uri
);
3790 update_history_tabs(NULL
);
3794 set_status(t
, (char *)uri
, XT_STATUS_URI
);
3795 #if WEBKIT_CHECK_VERSION(1, 1, 18)
3796 case WEBKIT_LOAD_FAILED
:
3799 #if GTK_CHECK_VERSION(2, 20, 0)
3800 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
3801 gtk_widget_hide(t
->spinner
);
3804 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
3809 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
), TRUE
);
3811 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
),
3812 can_go_back_for_real(t
));
3814 gtk_widget_set_sensitive(GTK_WIDGET(t
->forward
),
3815 can_go_forward_for_real(t
));
3819 notify_title_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3821 const gchar
*title
= NULL
, *win_title
= NULL
;
3823 title
= get_title(t
, FALSE
);
3824 win_title
= get_title(t
, TRUE
);
3825 gtk_label_set_text(GTK_LABEL(t
->label
), title
);
3826 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
), title
);
3827 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
3828 gtk_window_set_title(GTK_WINDOW(main_window
), win_title
);
3832 webview_load_finished_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
3834 run_script(t
, JS_HINTING
);
3835 if (autofocus_onload
&&
3836 t
->tab_id
== gtk_notebook_get_current_page(notebook
))
3837 run_script(t
, "hints.focusInput();");
3839 run_script(t
, "hints.clearFocus();");
3843 webview_progress_changed_cb(WebKitWebView
*wv
, int progress
, struct tab
*t
)
3845 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->uri_entry
),
3846 progress
== 100 ? 0 : (double)progress
/ 100);
3847 if (show_url
== 0) {
3848 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
3849 progress
== 100 ? 0 : (double)progress
/ 100);
3852 update_statusbar_position(NULL
, NULL
);
3856 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
3857 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
3858 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
3861 WebKitWebNavigationReason reason
;
3862 struct domain
*d
= NULL
;
3865 show_oops(NULL
, "webview_npd_cb invalid parameters");
3869 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
3871 webkit_network_request_get_uri(request
));
3873 uri
= (char *)webkit_network_request_get_uri(request
);
3875 /* if this is an xtp url, we don't load anything else */
3876 if (parse_xtp_url(t
, uri
))
3879 if ((t
->hints_on
&& t
->new_tab
) || t
->ctrl_click
) {
3881 create_new_tab(uri
, NULL
, ctrl_click_focus
, -1);
3882 webkit_web_policy_decision_ignore(pd
);
3883 return (TRUE
); /* we made the decission */
3887 * This is a little hairy but it comes down to this:
3888 * when we run in whitelist mode we have to assist the browser in
3889 * opening the URL that it would have opened in a new tab.
3891 reason
= webkit_web_navigation_action_get_reason(na
);
3892 if (reason
== WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED
) {
3893 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
3894 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1)
3895 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3897 webkit_web_policy_decision_use(pd
);
3898 return (TRUE
); /* we made the decision */
3905 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
3908 struct domain
*d
= NULL
;
3910 WebKitWebView
*webview
= NULL
;
3912 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
3913 webkit_web_view_get_uri(wv
));
3916 /* open in current tab */
3918 } else if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
3919 uri
= webkit_web_view_get_uri(wv
);
3920 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3923 tt
= create_new_tab(NULL
, NULL
, 1, -1);
3925 } else if (enable_scripts
== 1) {
3926 tt
= create_new_tab(NULL
, NULL
, 1, -1);
3934 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
3937 struct domain
*d
= NULL
;
3939 DNPRINTF(XT_D_NAV
, "webview_close_cb: %d\n", t
->tab_id
);
3941 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
3942 uri
= webkit_web_view_get_uri(wv
);
3943 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3947 } else if (enable_scripts
== 1)
3954 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
3956 /* we can not eat the event without throwing gtk off so defer it */
3958 /* catch middle click */
3959 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 2) {
3964 /* catch ctrl click */
3965 if (e
->type
== GDK_BUTTON_RELEASE
&&
3966 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
3971 return (XT_CB_PASSTHROUGH
);
3975 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
3977 struct mime_type
*m
;
3979 m
= find_mime_type(mime_type
);
3987 show_oops(t
, "can't fork mime handler");
3997 execlp(m
->mt_action
, m
->mt_action
,
3998 webkit_network_request_get_uri(request
), (void *)NULL
);
4007 get_mime_type(const char *file
)
4010 char *mime_type
= NULL
;
4014 if (g_str_has_prefix(file
, "file://"))
4015 file
+= strlen("file://");
4017 gf
= g_file_new_for_path(file
);
4018 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
4020 if ((m
= g_file_info_get_content_type(fi
)) != NULL
)
4021 mime_type
= g_strdup(m
);
4029 run_download_mimehandler(char *mime_type
, char *file
)
4031 struct mime_type
*m
;
4033 m
= find_mime_type(mime_type
);
4039 show_oops(NULL
, "can't fork download mime handler");
4049 if (g_str_has_prefix(file
, "file://"))
4050 file
+= strlen("file://");
4051 execlp(m
->mt_action
, m
->mt_action
, file
, (void *)NULL
);
4060 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
4063 WebKitDownloadStatus status
;
4064 const char *file
= NULL
;
4067 if (download
== NULL
)
4069 status
= webkit_download_get_status(download
);
4070 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
4073 file
= webkit_download_get_destination_uri(download
);
4076 mime
= get_mime_type(file
);
4080 run_download_mimehandler((char *)mime
, (char *)file
);
4085 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
4086 WebKitNetworkRequest
*request
, char *mime_type
,
4087 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
4090 show_oops(NULL
, "webview_mimetype_cb invalid parameters");
4094 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
4095 t
->tab_id
, mime_type
);
4097 if (run_mimehandler(t
, mime_type
, request
) == 0) {
4098 webkit_web_policy_decision_ignore(decision
);
4103 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
4104 webkit_web_policy_decision_download(decision
);
4112 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
4116 const gchar
*suggested_name
;
4117 gchar
*filename
= NULL
;
4119 struct download
*download_entry
;
4122 if (wk_download
== NULL
|| t
== NULL
) {
4123 show_oops(NULL
, "%s invalid parameters", __func__
);
4127 suggested_name
= webkit_download_get_suggested_filename(wk_download
);
4128 if (suggested_name
== NULL
)
4129 return (FALSE
); /* abort download */
4140 filename
= g_strdup_printf("%d%s", i
, suggested_name
);
4142 uri
= g_strdup_printf("file://%s/%s", download_dir
, i
?
4143 filename
: suggested_name
);
4145 } while (!stat(uri
+ strlen("file://"), &sb
));
4147 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d filename %s "
4148 "local %s\n", __func__
, t
->tab_id
, filename
, uri
);
4150 webkit_download_set_destination_uri(wk_download
, uri
);
4152 if (webkit_download_get_status(wk_download
) ==
4153 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
4154 show_oops(t
, "%s: download failed to start", __func__
);
4156 gtk_label_set_text(GTK_LABEL(t
->label
), "Download Failed");
4158 /* connect "download first" mime handler */
4159 g_signal_connect(G_OBJECT(wk_download
), "notify::status",
4160 G_CALLBACK(download_status_changed_cb
), NULL
);
4162 download_entry
= g_malloc(sizeof(struct download
));
4163 download_entry
->download
= wk_download
;
4164 download_entry
->tab
= t
;
4165 download_entry
->id
= next_download_id
++;
4166 RB_INSERT(download_list
, &downloads
, download_entry
);
4167 /* get from history */
4168 g_object_ref(wk_download
);
4169 gtk_label_set_text(GTK_LABEL(t
->label
), "Downloading");
4170 show_oops(t
, "Download of '%s' started...",
4171 basename((char *)webkit_download_get_destination_uri(wk_download
)));
4180 /* sync other download manager tabs */
4181 update_download_tabs(NULL
);
4184 * NOTE: never redirect/render the current tab before this
4185 * function returns. This will cause the download to never start.
4187 return (ret
); /* start download */
4191 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
4193 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
4196 show_oops(NULL
, "webview_hover_cb");
4201 set_status(t
, uri
, XT_STATUS_LINK
);
4204 set_status(t
, t
->status
, XT_STATUS_NOTHING
);
4209 mark(struct tab
*t
, struct karg
*arg
)
4215 if ((index
= marktoindex(mark
)) == -1)
4218 if (arg
->i
== XT_MARK_SET
)
4219 t
->mark
[index
] = gtk_adjustment_get_value(t
->adjust_v
);
4220 else if (arg
->i
== XT_MARK_GOTO
) {
4221 if (t
->mark
[index
] == XT_INVALID_MARK
) {
4222 show_oops(t
, "mark '%c' does not exist", mark
);
4225 /* XXX t->mark[index] can be bigger than the maximum if ajax or
4226 something changes the document size */
4227 gtk_adjustment_set_value(t
->adjust_v
, t
->mark
[index
]);
4234 marks_clear(struct tab
*t
)
4238 for (i
= 0; i
< LENGTH(t
->mark
); i
++)
4239 t
->mark
[i
] = XT_INVALID_MARK
;
4245 char file
[PATH_MAX
];
4246 char *line
= NULL
, *p
;
4251 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
4252 if ((f
= fopen(file
, "r+")) == NULL
) {
4253 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
4257 for (i
= 1; ; i
++) {
4258 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
4260 if (strlen(line
) == 0 || line
[0] == '#') {
4266 p
= strtok(line
, " \t");
4268 if (p
== NULL
|| strlen(p
) != 1 ||
4269 (index
= marktoindex(*p
)) == -1) {
4270 warnx("corrupt quickmarks file, line %d", i
);
4274 p
= strtok(NULL
, " \t");
4275 if (qmarks
[index
] != NULL
)
4276 g_free(qmarks
[index
]);
4277 qmarks
[index
] = g_strdup(p
);
4288 char file
[PATH_MAX
];
4292 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
4293 if ((f
= fopen(file
, "r+")) == NULL
) {
4294 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
4298 for (i
= 0; i
< XT_NOMARKS
; i
++)
4299 if (qmarks
[i
] != NULL
)
4300 fprintf(f
, "%c %s\n", indextomark(i
), qmarks
[i
]);
4308 qmark(struct tab
*t
, struct karg
*arg
)
4313 mark
= arg
->s
[strlen(arg
->s
)-1];
4314 index
= marktoindex(mark
);
4320 if (qmarks
[index
] != NULL
)
4321 g_free(qmarks
[index
]);
4323 qmarks_load(); /* sync if multiple instances */
4324 qmarks
[index
] = g_strdup(get_uri(t
));
4328 if (qmarks
[index
] != NULL
)
4329 load_uri(t
, qmarks
[index
]);
4331 show_oops(t
, "quickmark \"%c\" does not exist",
4337 if (qmarks
[index
] != NULL
)
4338 create_new_tab(qmarks
[index
], NULL
, 1, -1);
4340 show_oops(t
, "quickmark \"%c\" does not exist",
4351 go_up(struct tab
*t
, struct karg
*args
)
4357 levels
= atoi(args
->s
);
4361 uri
= g_strdup(webkit_web_view_get_uri(t
->wv
));
4362 if ((tmp
= strstr(uri
, XT_PROTO_DELIM
)) == NULL
)
4364 tmp
+= strlen(XT_PROTO_DELIM
);
4366 /* if an uri starts with a slash, leave it alone (for file:///) */
4373 p
= strrchr(tmp
, '/');
4387 gototab(struct tab
*t
, struct karg
*args
)
4390 struct karg arg
= {0, NULL
, -1};
4392 tab
= atoi(args
->s
);
4394 arg
.i
= XT_TAB_NEXT
;
4403 zoom_amount(struct tab
*t
, struct karg
*arg
)
4405 struct karg narg
= {0, NULL
, -1};
4407 narg
.i
= atoi(arg
->s
);
4408 resizetab(t
, &narg
);
4414 flip_colon(struct tab
*t
, struct karg
*arg
)
4416 struct karg narg
= {0, NULL
, -1};
4419 if (t
== NULL
|| arg
== NULL
)
4422 p
= strstr(arg
->s
, ":");
4434 /* buffer commands receive the regex that triggered them in arg.s */
4435 char bcmd
[XT_BUFCMD_SZ
];
4439 #define XT_PRE_NO (0)
4440 #define XT_PRE_YES (1)
4441 #define XT_PRE_MAYBE (2)
4443 int (*func
)(struct tab
*, struct karg
*);
4447 { "^[0-9]*gu$", XT_PRE_MAYBE
, "gu", go_up
, 0 },
4448 { "^gg$", XT_PRE_NO
, "gg", move
, XT_MOVE_TOP
},
4449 { "^gG$", XT_PRE_NO
, "gG", move
, XT_MOVE_BOTTOM
},
4450 { "^[0-9]+%$", XT_PRE_YES
, "%", move
, XT_MOVE_PERCENT
},
4451 { "^gh$", XT_PRE_NO
, "gh", go_home
, 0 },
4452 { "^m[a-zA-Z0-9]$", XT_PRE_NO
, "m", mark
, XT_MARK_SET
},
4453 { "^['][a-zA-Z0-9]$", XT_PRE_NO
, "'", mark
, XT_MARK_GOTO
},
4454 { "^[0-9]+t$", XT_PRE_YES
, "t", gototab
, 0 },
4455 { "^M[a-zA-Z0-9]$", XT_PRE_NO
, "M", qmark
, XT_QMARK_SET
},
4456 { "^go[a-zA-Z0-9]$", XT_PRE_NO
, "go", qmark
, XT_QMARK_OPEN
},
4457 { "^gn[a-zA-Z0-9]$", XT_PRE_NO
, "gn", qmark
, XT_QMARK_TAB
},
4458 { "^ZR$", XT_PRE_NO
, "ZR", restart
, 0 },
4459 { "^ZZ$", XT_PRE_NO
, "ZZ", quit
, 0 },
4460 { "^zi$", XT_PRE_NO
, "zi", resizetab
, XT_ZOOM_IN
},
4461 { "^zo$", XT_PRE_NO
, "zo", resizetab
, XT_ZOOM_OUT
},
4462 { "^z0$", XT_PRE_NO
, "z0", resizetab
, XT_ZOOM_NORMAL
},
4463 { "^[0-9]+Z$", XT_PRE_YES
, "Z", zoom_amount
, 0 },
4464 { "^[0-9]+:$", XT_PRE_YES
, ":", flip_colon
, 0 },
4468 buffercmd_init(void)
4472 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
4473 if (regcomp(&buffercmds
[i
].cregex
, buffercmds
[i
].regex
,
4474 REG_EXTENDED
| REG_NOSUB
))
4475 startpage_add("invalid buffercmd regex %s",
4476 buffercmds
[i
].regex
);
4480 buffercmd_abort(struct tab
*t
)
4484 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_abort: clearing buffer\n");
4485 for (i
= 0; i
< LENGTH(bcmd
); i
++)
4488 cmd_prefix
= 0; /* clear prefix for non-buffer commands */
4489 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
4493 buffercmd_execute(struct tab
*t
, struct buffercmd
*cmd
)
4495 struct karg arg
= {0, NULL
, -1};
4498 arg
.s
= g_strdup(bcmd
);
4500 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_execute: buffer \"%s\" "
4501 "matches regex \"%s\", executing\n", bcmd
, cmd
->regex
);
4511 buffercmd_addkey(struct tab
*t
, guint keyval
)
4514 char s
[XT_BUFCMD_SZ
];
4516 if (keyval
== GDK_Escape
) {
4518 return (XT_CB_HANDLED
);
4521 /* key with modifier or non-ascii character */
4522 if (!isascii(keyval
))
4523 return (XT_CB_PASSTHROUGH
);
4525 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: adding key \"%c\" "
4526 "to buffer \"%s\"\n", keyval
, bcmd
);
4528 for (i
= 0; i
< LENGTH(bcmd
); i
++)
4529 if (bcmd
[i
] == '\0') {
4534 /* buffer full, ignore input */
4535 if (i
>= LENGTH(bcmd
) -1) {
4536 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: buffer full\n");
4538 return (XT_CB_HANDLED
);
4541 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
4543 /* find exact match */
4544 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
4545 if (regexec(&buffercmds
[i
].cregex
, bcmd
,
4546 (size_t) 0, NULL
, 0) == 0) {
4547 buffercmd_execute(t
, &buffercmds
[i
]);
4551 /* find non exact matches to see if we need to abort ot not */
4552 for (i
= 0, match
= 0; i
< LENGTH(buffercmds
); i
++) {
4553 DNPRINTF(XT_D_BUFFERCMD
, "trying: %s\n", bcmd
);
4556 if (buffercmds
[i
].precount
== XT_PRE_MAYBE
) {
4557 if (isdigit(bcmd
[0])) {
4558 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
4562 if (sscanf(bcmd
, "%s", s
) == 0)
4565 } else if (buffercmds
[i
].precount
== XT_PRE_YES
) {
4566 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
4569 if (sscanf(bcmd
, "%s", s
) == 0)
4572 if (c
== -1 && buffercmds
[i
].precount
)
4574 if (!strncmp(s
, buffercmds
[i
].cmd
, strlen(s
)))
4577 DNPRINTF(XT_D_BUFFERCMD
, "got[%d] %d <%s>: %d %s\n",
4578 i
, match
, buffercmds
[i
].cmd
, c
, s
);
4581 DNPRINTF(XT_D_BUFFERCMD
, "aborting: %s\n", bcmd
);
4586 return (XT_CB_HANDLED
);
4590 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
4592 struct key_binding
*k
;
4594 /* handle keybindings if buffercmd is empty.
4595 if not empty, allow commands like C-n */
4596 if (bcmd
[0] == '\0' || ((e
->state
& (CTRL
| MOD1
)) != 0))
4597 TAILQ_FOREACH(k
, &kbl
, entry
)
4598 if (e
->keyval
== k
->key
4599 && (entry
? k
->use_in_entry
: 1)) {
4601 if ((e
->state
& (CTRL
| MOD1
)) == 0)
4602 return (cmd_execute(t
, k
->cmd
));
4603 } else if ((e
->state
& k
->mask
) == k
->mask
) {
4604 return (cmd_execute(t
, k
->cmd
));
4608 if (!entry
&& ((e
->state
& (CTRL
| MOD1
)) == 0))
4609 return buffercmd_addkey(t
, e
->keyval
);
4611 return (XT_CB_PASSTHROUGH
);
4615 wv_keypress_after_cb(GtkWidget
*w
, GdkEventKey
*e
, struct tab
*t
)
4619 /* don't use w directly; use t->whatever instead */
4622 show_oops(NULL
, "wv_keypress_after_cb");
4623 return (XT_CB_PASSTHROUGH
);
4626 DNPRINTF(XT_D_KEY
, "wv_keypress_after_cb: keyval 0x%x mask 0x%x t %p\n",
4627 e
->keyval
, e
->state
, t
);
4630 /* XXX make sure cmd entry is enabled */
4631 return (XT_CB_HANDLED
);
4634 snprintf(s
, sizeof s
, "%c", e
->keyval
);
4635 if (CLEAN(e
->state
) == 0 && isdigit(s
[0]))
4636 cmd_prefix
= 10 * cmd_prefix
+ atoi(s
);
4639 return (handle_keypress(t
, e
, 0));
4643 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
4647 /* Hide buffers, if they are visible, with escape. */
4648 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)) &&
4649 CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
4650 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
4652 return (XT_CB_HANDLED
);
4655 return (XT_CB_PASSTHROUGH
);
4659 hint_continue(struct tab
*t
)
4661 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
4663 const gchar
*errstr
= NULL
;
4667 if (!(c
[0] == '.' || c
[0] == ','))
4669 if (strlen(c
) == 1) {
4670 /* XXX should not happen */
4675 if (isdigit(c
[1])) {
4677 i
= strtonum(&c
[1], 1, 4096, &errstr
);
4679 show_oops(t
, "invalid numerical hint %s", &c
[1]);
4682 s
= g_strdup_printf("hints.updateHints(%lld);", i
);
4686 /* alphanumeric input */
4687 s
= g_strdup_printf("hints.createHints('%s', '%c');",
4688 &c
[1], c
[0] == '.' ? 'f' : 'F');
4699 search_continue(struct tab
*t
)
4701 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
4702 gboolean rv
= FALSE
;
4704 if (c
[0] == ':' || c
[0] == '.' || c
[0] == ',')
4706 if (strlen(c
) == 1) {
4707 webkit_web_view_unmark_text_matches(t
->wv
);
4712 t
->search_forward
= TRUE
;
4713 else if (c
[0] == '?')
4714 t
->search_forward
= FALSE
;
4724 search_cb(struct tab
*t
)
4726 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
4729 if (search_continue(t
) == FALSE
)
4733 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, t
->search_forward
,
4735 /* not found, mark red */
4736 gdk_color_parse(XT_COLOR_RED
, &color
);
4737 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
4738 /* unmark and remove selection */
4739 webkit_web_view_unmark_text_matches(t
->wv
);
4740 /* my kingdom for a way to unselect text in webview */
4742 /* found, highlight all */
4743 webkit_web_view_unmark_text_matches(t
->wv
);
4744 webkit_web_view_mark_text_matches(t
->wv
, &c
[1], FALSE
, 0);
4745 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
4746 gdk_color_parse(XT_COLOR_WHITE
, &color
);
4747 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
4755 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
4757 const gchar
*c
= gtk_entry_get_text(w
);
4760 show_oops(NULL
, "cmd_keyrelease_cb invalid parameters");
4761 return (XT_CB_PASSTHROUGH
);
4764 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x t %p\n",
4765 e
->keyval
, e
->state
, t
);
4768 if (!(e
->keyval
== GDK_Tab
|| e
->keyval
== GDK_ISO_Left_Tab
)) {
4769 if (hint_continue(t
) == FALSE
)
4774 if (search_continue(t
) == FALSE
)
4777 /* if search length is > 4 then no longer play timeout games */
4778 if (strlen(c
) > 4) {
4780 g_source_remove(t
->search_id
);
4787 /* reestablish a new timer if the user types fast */
4789 g_source_remove(t
->search_id
);
4790 t
->search_id
= g_timeout_add(250, (GSourceFunc
)search_cb
, (gpointer
)t
);
4793 return (XT_CB_PASSTHROUGH
);
4797 match_uri(const gchar
*uri
, const gchar
*key
) {
4800 gboolean match
= FALSE
;
4804 if (!strncmp(key
, uri
, len
))
4807 voffset
= strstr(uri
, "/") + 2;
4808 if (!strncmp(key
, voffset
, len
))
4810 else if (g_str_has_prefix(voffset
, "www.")) {
4811 voffset
= voffset
+ strlen("www.");
4812 if (!strncmp(key
, voffset
, len
))
4821 match_session(const gchar
*name
, const gchar
*key
) {
4824 sub
= strcasestr(name
, key
);
4830 cmd_getlist(int id
, char *key
)
4837 if (cmds
[id
].type
& XT_URLARG
) {
4838 RB_FOREACH_REVERSE(h
, history_list
, &hl
)
4839 if (match_uri(h
->uri
, key
)) {
4840 cmd_status
.list
[c
] = (char *)h
->uri
;
4846 } else if (cmds
[id
].type
& XT_SESSARG
) {
4847 TAILQ_FOREACH(s
, &sessions
, entry
)
4848 if (match_session(s
->name
, key
)) {
4849 cmd_status
.list
[c
] = (char *)s
->name
;
4855 } else if (cmds
[id
].type
& XT_SETARG
) {
4856 for (i
= 0; i
< get_settings_size(); i
++)
4857 if(!strncmp(key
, get_setting_name(i
),
4859 cmd_status
.list
[c
++] = get_setting_name(i
);
4865 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
4867 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
4868 if (cmds
[i
].level
< dep
)
4870 if (cmds
[i
].level
== dep
&& !strncmp(key
, cmds
[i
].cmd
,
4871 strlen(key
)) && !isdigit(cmds
[i
].cmd
[0]))
4872 cmd_status
.list
[c
++] = cmds
[i
].cmd
;
4880 cmd_getnext(int dir
)
4882 cmd_status
.index
+= dir
;
4884 if (cmd_status
.index
< 0)
4885 cmd_status
.index
= cmd_status
.len
- 1;
4886 else if (cmd_status
.index
>= cmd_status
.len
)
4887 cmd_status
.index
= 0;
4889 return cmd_status
.list
[cmd_status
.index
];
4893 cmd_tokenize(char *s
, char *tokens
[])
4896 char *tok
, *last
= NULL
;
4897 size_t len
= strlen(s
);
4900 blank
= len
== 0 || (len
> 0 && s
[len
- 1] == ' ');
4901 for (tok
= strtok_r(s
, " ", &last
); tok
&& i
< 3;
4902 tok
= strtok_r(NULL
, " ", &last
), i
++)
4912 cmd_complete(struct tab
*t
, char *str
, int dir
)
4914 GtkEntry
*w
= GTK_ENTRY(t
->cmd
);
4915 int i
, j
, levels
, c
= 0, dep
= 0, parent
= -1;
4917 char *tok
, *match
, *s
= g_strdup(str
);
4919 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
4922 DNPRINTF(XT_D_CMD
, "%s: complete %s\n", __func__
, str
);
4925 for (i
= 0; isdigit(s
[i
]); i
++)
4928 for (; isspace(s
[i
]); i
++)
4933 levels
= cmd_tokenize(s
, tokens
);
4935 for (i
= 0; i
< levels
- 1; i
++) {
4938 for (j
= c
; j
< LENGTH(cmds
); j
++) {
4939 if (cmds
[j
].level
< dep
)
4941 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
,
4945 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
4952 if (matchcount
== 1) {
4953 strlcat(res
, tok
, sizeof res
);
4954 strlcat(res
, " ", sizeof res
);
4964 if (cmd_status
.index
== -1)
4965 cmd_getlist(parent
, tokens
[i
]);
4967 if (cmd_status
.len
> 0) {
4968 match
= cmd_getnext(dir
);
4969 strlcat(res
, match
, sizeof res
);
4970 gtk_entry_set_text(w
, res
);
4971 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
4978 cmd_execute(struct tab
*t
, char *str
)
4980 struct cmd
*cmd
= NULL
;
4981 char *tok
, *last
= NULL
, *s
= g_strdup(str
), *sc
;
4983 int j
, len
, c
= 0, dep
= 0, matchcount
= 0;
4984 int prefix
= -1, rv
= XT_CB_PASSTHROUGH
;
4985 struct karg arg
= {0, NULL
, -1};
4990 for (j
= 0; j
<3 && isdigit(s
[j
]); j
++)
4996 while (isspace(s
[0]))
4999 if (strlen(s
) > 0 && strlen(prefixstr
) > 0)
5000 prefix
= atoi(prefixstr
);
5004 for (tok
= strtok_r(s
, " ", &last
); tok
;
5005 tok
= strtok_r(NULL
, " ", &last
)) {
5007 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5008 if (cmds
[j
].level
< dep
)
5010 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1 :
5012 if (cmds
[j
].level
== dep
&&
5013 !strncmp(tok
, cmds
[j
].cmd
, len
)) {
5017 if (len
== strlen(cmds
[j
].cmd
)) {
5023 if (matchcount
== 1) {
5028 show_oops(t
, "Invalid command: %s", str
);
5034 show_oops(t
, "Empty command");
5040 arg
.precount
= prefix
;
5041 else if (cmd_prefix
> 0)
5042 arg
.precount
= cmd_prefix
;
5044 if (j
> 0 && !(cmd
->type
& XT_PREFIX
) && arg
.precount
> -1) {
5045 show_oops(t
, "No prefix allowed: %s", str
);
5049 arg
.s
= last
? g_strdup(last
) : g_strdup("");
5050 if (cmd
->type
& XT_INTARG
&& last
&& strlen(last
) > 0) {
5051 if (arg
.s
== NULL
) {
5052 show_oops(t
, "Invalid command");
5055 arg
.precount
= atoi(arg
.s
);
5056 if (arg
.precount
<= 0) {
5057 if (arg
.s
[0] == '0')
5058 show_oops(t
, "Zero count");
5060 show_oops(t
, "Trailing characters");
5065 DNPRINTF(XT_D_CMD
, "%s: prefix %d arg %s\n",
5066 __func__
, arg
.precount
, arg
.s
);
5082 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5085 show_oops(NULL
, "entry_key_cb invalid parameters");
5086 return (XT_CB_PASSTHROUGH
);
5089 DNPRINTF(XT_D_CMD
, "entry_key_cb: keyval 0x%x mask 0x%x t %p\n",
5090 e
->keyval
, e
->state
, t
);
5094 if (e
->keyval
== GDK_Escape
) {
5095 /* don't use focus_webview(t) because we want to type :cmds */
5096 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5099 return (handle_keypress(t
, e
, 1));
5102 struct command_entry
*
5103 history_prev(struct command_list
*l
, struct command_entry
*at
)
5106 at
= TAILQ_LAST(l
, command_list
);
5108 at
= TAILQ_PREV(at
, command_list
, entry
);
5110 at
= TAILQ_LAST(l
, command_list
);
5116 struct command_entry
*
5117 history_next(struct command_list
*l
, struct command_entry
*at
)
5120 at
= TAILQ_FIRST(l
);
5122 at
= TAILQ_NEXT(at
, entry
);
5124 at
= TAILQ_FIRST(l
);
5131 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5133 int rv
= XT_CB_HANDLED
;
5134 const gchar
*c
= gtk_entry_get_text(w
);
5138 show_oops(NULL
, "cmd_keypress_cb parameters");
5139 return (XT_CB_PASSTHROUGH
);
5142 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x t %p\n",
5143 e
->keyval
, e
->state
, t
);
5147 e
->keyval
= GDK_Escape
;
5148 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5149 c
[0] == '.' || c
[0] == ','))
5150 e
->keyval
= GDK_Escape
;
5152 if (e
->keyval
!= GDK_Tab
&& e
->keyval
!= GDK_Shift_L
&&
5153 e
->keyval
!= GDK_ISO_Left_Tab
)
5154 cmd_status
.index
= -1;
5156 switch (e
->keyval
) {
5159 cmd_complete(t
, (char *)&c
[1], 1);
5160 else if (c
[0] == '.' || c
[0] == ',')
5161 run_script(t
, "hints.focusNextHint();");
5163 case GDK_ISO_Left_Tab
:
5165 cmd_complete(t
, (char *)&c
[1], -1);
5166 else if (c
[0] == '.' || c
[0] == ',')
5167 run_script(t
, "hints.focusPreviousHint();");
5171 if ((search_at
= history_next(&shl
, search_at
))) {
5172 search_at
->line
[0] = c
[0];
5173 gtk_entry_set_text(w
, search_at
->line
);
5174 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5176 } else if (c
[0] == '/') {
5177 if ((search_at
= history_prev(&chl
, 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 } if (c
[0] == ':') {
5183 if ((history_at
= history_prev(&chl
, history_at
))) {
5184 history_at
->line
[0] = c
[0];
5185 gtk_entry_set_text(w
, history_at
->line
);
5186 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5192 if ((search_at
= history_next(&shl
, search_at
))) {
5193 search_at
->line
[0] = c
[0];
5194 gtk_entry_set_text(w
, search_at
->line
);
5195 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5197 } else if (c
[0] == '?') {
5198 if ((search_at
= history_prev(&chl
, 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 } if (c
[0] == ':') {
5204 if ((history_at
= history_next(&chl
, history_at
))) {
5205 history_at
->line
[0] = c
[0];
5206 gtk_entry_set_text(w
, history_at
->line
);
5207 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5212 if (!(!strcmp(c
, ":") || !strcmp(c
, "/") || !strcmp(c
, "?") ||
5213 !strcmp(c
, ".") || !strcmp(c
, ","))) {
5214 /* see if we are doing hinting and reset it */
5215 if (c
[0] == '.' || c
[0] == ',') {
5216 /* recreate hints */
5217 s
= g_strdup_printf("hints.createHints('', "
5218 "'%c');", c
[0] == '.' ? 'f' : 'F');
5231 if (c
!= NULL
&& (c
[0] == '/' || c
[0] == '?'))
5232 webkit_web_view_unmark_text_matches(t
->wv
);
5234 /* no need to cancel hints */
5238 rv
= XT_CB_PASSTHROUGH
;
5244 wv_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
5246 DNPRINTF(XT_D_CMD
, "wv_popup_cb: tab %d\n", t
->tab_id
);
5250 cmd_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
5252 /* popup menu enabled */
5257 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
5260 show_oops(NULL
, "cmd_focusout_cb invalid parameters");
5261 return (XT_CB_PASSTHROUGH
);
5264 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d popup %d\n",
5265 t
->tab_id
, t
->popup
);
5267 /* if popup is enabled don't lose focus */
5270 return (XT_CB_PASSTHROUGH
);
5277 if (show_url
== 0 || t
->focus_wv
)
5280 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
5282 return (XT_CB_PASSTHROUGH
);
5286 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
5289 const gchar
*c
= gtk_entry_get_text(entry
);
5292 show_oops(NULL
, "cmd_activate_cb invalid parameters");
5296 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
5301 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5302 c
[0] == '.' || c
[0] == ','))
5308 if (c
[0] == '/' || c
[0] == '?') {
5309 /* see if there is a timer pending */
5311 g_source_remove(t
->search_id
);
5316 if (t
->search_text
) {
5317 g_free(t
->search_text
);
5318 t
->search_text
= NULL
;
5321 t
->search_text
= g_strdup(s
);
5323 g_free(global_search
);
5324 global_search
= g_strdup(s
);
5325 t
->search_forward
= c
[0] == '/';
5327 history_add(&shl
, search_file
, s
, &search_history_count
);
5328 } else if (c
[0] == '.' || c
[0] == ',') {
5329 run_script(t
, "hints.fire();");
5330 /* XXX history for link following? */
5331 } else if (c
[0] == ':') {
5332 history_add(&chl
, command_file
, s
, &cmd_history_count
);
5333 /* can't call hide_cmd after cmd_execute */
5345 backward_cb(GtkWidget
*w
, struct tab
*t
)
5350 show_oops(NULL
, "backward_cb invalid parameters");
5354 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
5361 forward_cb(GtkWidget
*w
, struct tab
*t
)
5366 show_oops(NULL
, "forward_cb invalid parameters");
5370 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
5372 a
.i
= XT_NAV_FORWARD
;
5377 home_cb(GtkWidget
*w
, struct tab
*t
)
5380 show_oops(NULL
, "home_cb invalid parameters");
5384 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
5390 stop_cb(GtkWidget
*w
, struct tab
*t
)
5392 WebKitWebFrame
*frame
;
5395 show_oops(NULL
, "stop_cb invalid parameters");
5399 DNPRINTF(XT_D_NAV
, "stop_cb: tab %d\n", t
->tab_id
);
5401 frame
= webkit_web_view_get_main_frame(t
->wv
);
5402 if (frame
== NULL
) {
5403 show_oops(t
, "stop_cb: no frame");
5407 webkit_web_frame_stop_loading(frame
);
5408 abort_favicon_download(t
);
5412 setup_webkit(struct tab
*t
)
5414 if (is_g_object_setting(G_OBJECT(t
->settings
), "enable-dns-prefetching"))
5415 g_object_set(G_OBJECT(t
->settings
), "enable-dns-prefetching",
5416 FALSE
, (char *)NULL
);
5418 warnx("webkit does not have \"enable-dns-prefetching\" property");
5419 g_object_set(G_OBJECT(t
->settings
),
5420 "user-agent", t
->user_agent
, (char *)NULL
);
5421 g_object_set(G_OBJECT(t
->settings
),
5422 "enable-scripts", enable_scripts
, (char *)NULL
);
5423 g_object_set(G_OBJECT(t
->settings
),
5424 "enable-plugins", enable_plugins
, (char *)NULL
);
5425 g_object_set(G_OBJECT(t
->settings
),
5426 "javascript-can-open-windows-automatically", enable_scripts
,
5428 g_object_set(G_OBJECT(t
->settings
),
5429 "enable-html5-database", FALSE
, (char *)NULL
);
5430 g_object_set(G_OBJECT(t
->settings
),
5431 "enable-html5-local-storage", enable_localstorage
, (char *)NULL
);
5432 g_object_set(G_OBJECT(t
->settings
),
5433 "enable_spell_checking", enable_spell_checking
, (char *)NULL
);
5434 g_object_set(G_OBJECT(t
->settings
),
5435 "spell_checking_languages", spell_check_languages
, (char *)NULL
);
5436 g_object_set(G_OBJECT(t
->settings
),
5437 "enable-developer-extras", TRUE
, (char *)NULL
);
5438 g_object_set(G_OBJECT(t
->wv
),
5439 "full-content-zoom", TRUE
, (char *)NULL
);
5441 webkit_web_view_set_settings(t
->wv
, t
->settings
);
5445 update_statusbar_position(GtkAdjustment
* adjustment
, gpointer data
)
5447 struct tab
*ti
, *t
= NULL
;
5448 gdouble view_size
, value
, max
;
5451 TAILQ_FOREACH(ti
, &tabs
, entry
)
5452 if (ti
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
5460 if (adjustment
== NULL
)
5461 adjustment
= gtk_scrolled_window_get_vadjustment(
5462 GTK_SCROLLED_WINDOW(t
->browser_win
));
5464 view_size
= gtk_adjustment_get_page_size(adjustment
);
5465 value
= gtk_adjustment_get_value(adjustment
);
5466 max
= gtk_adjustment_get_upper(adjustment
) - view_size
;
5469 position
= g_strdup("All");
5470 else if (value
== max
)
5471 position
= g_strdup("Bot");
5472 else if (value
== 0)
5473 position
= g_strdup("Top");
5475 position
= g_strdup_printf("%d%%", (int) ((value
/ max
) * 100));
5477 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.position
), position
);
5484 create_window(const gchar
*name
)
5488 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
5489 gtk_window_set_default_size(GTK_WINDOW(w
), window_width
, window_height
);
5490 gtk_widget_set_name(w
, name
);
5491 gtk_window_set_wmclass(GTK_WINDOW(w
), name
, "XXXTerm");
5497 create_browser(struct tab
*t
)
5501 GtkAdjustment
*adjustment
;
5504 show_oops(NULL
, "create_browser invalid parameters");
5508 t
->sb_h
= GTK_SCROLLBAR(gtk_hscrollbar_new(NULL
));
5509 t
->sb_v
= GTK_SCROLLBAR(gtk_vscrollbar_new(NULL
));
5510 t
->adjust_h
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_h
));
5511 t
->adjust_v
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_v
));
5513 w
= gtk_scrolled_window_new(t
->adjust_h
, t
->adjust_v
);
5514 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
5515 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
5517 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
5518 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
5521 t
->settings
= webkit_web_settings_new();
5523 g_object_set(t
->settings
, "default-encoding", encoding
, (char *)NULL
);
5525 if (user_agent
== NULL
) {
5526 g_object_get(G_OBJECT(t
->settings
), "user-agent", &strval
,
5528 t
->user_agent
= g_strdup_printf("%s %s+", strval
, version
);
5531 t
->user_agent
= g_strdup(user_agent
);
5533 t
->stylesheet
= g_strdup_printf("file://%s/style.css", resource_dir
);
5536 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w
));
5537 g_signal_connect(G_OBJECT(adjustment
), "value-changed",
5538 G_CALLBACK(update_statusbar_position
), NULL
);
5547 create_kiosk_toolbar(struct tab
*t
)
5549 GtkWidget
*toolbar
= NULL
, *b
;
5551 b
= gtk_hbox_new(FALSE
, 0);
5553 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
5555 /* backward button */
5556 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
5557 gtk_widget_set_sensitive(t
->backward
, FALSE
);
5558 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
5559 G_CALLBACK(backward_cb
), t
);
5560 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, TRUE
, TRUE
, 0);
5562 /* forward button */
5563 t
->forward
= create_button("Forward", GTK_STOCK_GO_FORWARD
, 0);
5564 gtk_widget_set_sensitive(t
->forward
, FALSE
);
5565 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
5566 G_CALLBACK(forward_cb
), t
);
5567 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, TRUE
, TRUE
, 0);
5570 t
->gohome
= create_button("Home", GTK_STOCK_HOME
, 0);
5571 gtk_widget_set_sensitive(t
->gohome
, true);
5572 g_signal_connect(G_OBJECT(t
->gohome
), "clicked",
5573 G_CALLBACK(home_cb
), t
);
5574 gtk_box_pack_start(GTK_BOX(b
), t
->gohome
, TRUE
, TRUE
, 0);
5576 /* create widgets but don't use them */
5577 t
->uri_entry
= gtk_entry_new();
5578 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
5579 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
5580 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
5586 create_toolbar(struct tab
*t
)
5588 GtkWidget
*toolbar
= NULL
, *b
, *eb1
;
5590 b
= gtk_hbox_new(FALSE
, 0);
5592 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
5594 /* backward button */
5595 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
5596 gtk_widget_set_sensitive(t
->backward
, FALSE
);
5597 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
5598 G_CALLBACK(backward_cb
), t
);
5599 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, FALSE
, FALSE
, 0);
5601 /* forward button */
5602 t
->forward
= create_button("Forward",GTK_STOCK_GO_FORWARD
, 0);
5603 gtk_widget_set_sensitive(t
->forward
, FALSE
);
5604 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
5605 G_CALLBACK(forward_cb
), t
);
5606 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, FALSE
,
5610 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
5611 gtk_widget_set_sensitive(t
->stop
, FALSE
);
5612 g_signal_connect(G_OBJECT(t
->stop
), "clicked",
5613 G_CALLBACK(stop_cb
), t
);
5614 gtk_box_pack_start(GTK_BOX(b
), t
->stop
, FALSE
,
5618 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
5619 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
5620 gtk_widget_set_sensitive(t
->js_toggle
, TRUE
);
5621 g_signal_connect(G_OBJECT(t
->js_toggle
), "clicked",
5622 G_CALLBACK(js_toggle_cb
), t
);
5623 gtk_box_pack_start(GTK_BOX(b
), t
->js_toggle
, FALSE
, FALSE
, 0);
5625 t
->uri_entry
= gtk_entry_new();
5626 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
5627 G_CALLBACK(activate_uri_entry_cb
), t
);
5628 g_signal_connect(G_OBJECT(t
->uri_entry
), "key-press-event",
5629 G_CALLBACK(entry_key_cb
), t
);
5631 eb1
= gtk_hbox_new(FALSE
, 0);
5632 gtk_container_set_border_width(GTK_CONTAINER(eb1
), 1);
5633 gtk_box_pack_start(GTK_BOX(eb1
), t
->uri_entry
, TRUE
, TRUE
, 0);
5634 gtk_box_pack_start(GTK_BOX(b
), eb1
, TRUE
, TRUE
, 0);
5637 if (search_string
) {
5639 t
->search_entry
= gtk_entry_new();
5640 gtk_entry_set_width_chars(GTK_ENTRY(t
->search_entry
), 30);
5641 g_signal_connect(G_OBJECT(t
->search_entry
), "activate",
5642 G_CALLBACK(activate_search_entry_cb
), t
);
5643 g_signal_connect(G_OBJECT(t
->search_entry
), "key-press-event",
5644 G_CALLBACK(entry_key_cb
), t
);
5645 gtk_widget_set_size_request(t
->search_entry
, -1, -1);
5646 eb2
= gtk_hbox_new(FALSE
, 0);
5647 gtk_container_set_border_width(GTK_CONTAINER(eb2
), 1);
5648 gtk_box_pack_start(GTK_BOX(eb2
), t
->search_entry
, TRUE
, TRUE
,
5650 gtk_box_pack_start(GTK_BOX(b
), eb2
, FALSE
, FALSE
, 0);
5657 create_buffers(struct tab
*t
)
5659 GtkCellRenderer
*renderer
;
5662 view
= gtk_tree_view_new();
5664 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view
), FALSE
);
5666 renderer
= gtk_cell_renderer_text_new();
5667 gtk_tree_view_insert_column_with_attributes
5668 (GTK_TREE_VIEW(view
), -1, "Id", renderer
, "text", COL_ID
, (char *)NULL
);
5670 renderer
= gtk_cell_renderer_text_new();
5671 gtk_tree_view_insert_column_with_attributes
5672 (GTK_TREE_VIEW(view
), -1, "Title", renderer
, "text", COL_TITLE
,
5675 gtk_tree_view_set_model
5676 (GTK_TREE_VIEW(view
), GTK_TREE_MODEL(buffers_store
));
5682 row_activated_cb(GtkTreeView
*view
, GtkTreePath
*path
,
5683 GtkTreeViewColumn
*col
, struct tab
*t
)
5688 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5690 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
,
5693 (GTK_TREE_MODEL(buffers_store
), &iter
, COL_ID
, &id
, -1);
5694 set_current_tab(id
- 1);
5700 /* after tab reordering/creation/removal */
5707 TAILQ_FOREACH(t
, &tabs
, entry
) {
5708 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
5709 if (t
->tab_id
> maxid
)
5712 gtk_widget_show(t
->tab_elems
.sep
);
5715 TAILQ_FOREACH(t
, &tabs
, entry
) {
5716 if (t
->tab_id
== maxid
) {
5717 gtk_widget_hide(t
->tab_elems
.sep
);
5723 /* after active tab change */
5725 recolor_compact_tabs(void)
5731 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
5732 TAILQ_FOREACH(t
, &tabs
, entry
)
5733 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
,
5736 curid
= gtk_notebook_get_current_page(notebook
);
5737 TAILQ_FOREACH(t
, &tabs
, entry
)
5738 if (t
->tab_id
== curid
) {
5739 gdk_color_parse(XT_COLOR_CT_ACTIVE
, &color
);
5740 gtk_widget_modify_fg(t
->tab_elems
.label
,
5741 GTK_STATE_NORMAL
, &color
);
5747 set_current_tab(int page_num
)
5749 buffercmd_abort(get_current_tab());
5750 gtk_notebook_set_current_page(notebook
, page_num
);
5751 recolor_compact_tabs();
5755 undo_close_tab_save(struct tab
*t
)
5759 struct undo
*u1
, *u2
;
5761 WebKitWebHistoryItem
*item
;
5763 if ((uri
= get_uri(t
)) == NULL
)
5766 u1
= g_malloc0(sizeof(struct undo
));
5767 u1
->uri
= g_strdup(uri
);
5769 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
5771 m
= webkit_web_back_forward_list_get_forward_length(t
->bfl
);
5772 n
= webkit_web_back_forward_list_get_back_length(t
->bfl
);
5775 /* forward history */
5776 items
= webkit_web_back_forward_list_get_forward_list_with_limit(t
->bfl
, m
);
5780 u1
->history
= g_list_prepend(u1
->history
,
5781 webkit_web_history_item_copy(item
));
5782 items
= g_list_next(items
);
5787 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
5788 u1
->history
= g_list_prepend(u1
->history
,
5789 webkit_web_history_item_copy(item
));
5793 items
= webkit_web_back_forward_list_get_back_list_with_limit(t
->bfl
, n
);
5797 u1
->history
= g_list_prepend(u1
->history
,
5798 webkit_web_history_item_copy(item
));
5799 items
= g_list_next(items
);
5802 TAILQ_INSERT_HEAD(&undos
, u1
, entry
);
5804 if (undo_count
> XT_MAX_UNDO_CLOSE_TAB
) {
5805 u2
= TAILQ_LAST(&undos
, undo_tailq
);
5806 TAILQ_REMOVE(&undos
, u2
, entry
);
5808 g_list_free(u2
->history
);
5817 delete_tab(struct tab
*t
)
5821 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
5827 * no need to join thread here because it won't access t on completion
5830 TAILQ_REMOVE(&tabs
, t
, entry
);
5833 /* Halt all webkit activity. */
5834 abort_favicon_download(t
);
5835 webkit_web_view_stop_loading(t
->wv
);
5837 /* Save the tab, so we can undo the close. */
5838 undo_close_tab_save(t
);
5842 g_source_remove(t
->search_id
);
5845 bzero(&a
, sizeof a
);
5847 inspector_cmd(t
, &a
);
5849 if (browser_mode
== XT_BM_KIOSK
) {
5850 gtk_widget_destroy(t
->uri_entry
);
5851 gtk_widget_destroy(t
->stop
);
5852 gtk_widget_destroy(t
->js_toggle
);
5855 gtk_widget_destroy(t
->tab_elems
.eventbox
);
5856 gtk_widget_destroy(t
->vbox
);
5858 g_free(t
->user_agent
);
5859 g_free(t
->stylesheet
);
5864 if (TAILQ_EMPTY(&tabs
)) {
5865 if (browser_mode
== XT_BM_KIOSK
)
5866 create_new_tab(home
, NULL
, 1, -1);
5868 create_new_tab(NULL
, NULL
, 1, -1);
5871 /* recreate session */
5872 if (session_autosave
) {
5873 bzero(&a
, sizeof a
);
5875 save_tabs(NULL
, &a
);
5879 recolor_compact_tabs();
5883 update_statusbar_zoom(struct tab
*t
)
5886 char s
[16] = { '\0' };
5888 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
5889 if ((zoom
<= 0.99 || zoom
>= 1.01))
5890 snprintf(s
, sizeof s
, "%d%%", (int)(zoom
* 100));
5891 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.zoom
), s
);
5895 setzoom_webkit(struct tab
*t
, int adjust
)
5897 #define XT_ZOOMPERCENT 0.04
5902 show_oops(NULL
, "setzoom_webkit invalid parameters");
5906 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
5907 if (adjust
== XT_ZOOM_IN
)
5908 zoom
+= XT_ZOOMPERCENT
;
5909 else if (adjust
== XT_ZOOM_OUT
)
5910 zoom
-= XT_ZOOMPERCENT
;
5911 else if (adjust
> 0)
5912 zoom
= default_zoom_level
+ adjust
/ 100.0 - 1.0;
5914 show_oops(t
, "setzoom_webkit invalid zoom value");
5918 if (zoom
< XT_ZOOMPERCENT
)
5919 zoom
= XT_ZOOMPERCENT
;
5920 g_object_set(G_OBJECT(t
->wv
), "zoom-level", zoom
, (char *)NULL
);
5921 update_statusbar_zoom(t
);
5925 tab_clicked_cb(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
5927 struct tab
*t
= (struct tab
*) data
;
5929 DNPRINTF(XT_D_TAB
, "tab_clicked_cb: tab: %d\n", t
->tab_id
);
5931 switch (event
->button
) {
5933 set_current_tab(t
->tab_id
);
5944 append_tab(struct tab
*t
)
5949 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
5950 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
5954 create_sbe(int width
)
5958 sbe
= gtk_entry_new();
5959 gtk_entry_set_inner_border(GTK_ENTRY(sbe
), NULL
);
5960 gtk_entry_set_has_frame(GTK_ENTRY(sbe
), FALSE
);
5961 gtk_widget_set_can_focus(GTK_WIDGET(sbe
), FALSE
);
5962 gtk_widget_modify_font(GTK_WIDGET(sbe
), statusbar_font
);
5963 gtk_entry_set_alignment(GTK_ENTRY(sbe
), 1.0);
5964 gtk_widget_set_size_request(sbe
, width
, -1);
5970 create_new_tab(char *title
, struct undo
*u
, int focus
, int position
)
5975 WebKitWebHistoryItem
*item
;
5979 int sbe_p
= 0, sbe_b
= 0,
5982 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
5984 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
5985 DNPRINTF(XT_D_TAB
, "create_new_tab: new tab rejected\n");
5989 t
= g_malloc0(sizeof *t
);
5991 if (title
== NULL
) {
5992 title
= "(untitled)";
5996 t
->vbox
= gtk_vbox_new(FALSE
, 0);
5998 /* label + button for tab */
5999 b
= gtk_hbox_new(FALSE
, 0);
6002 #if GTK_CHECK_VERSION(2, 20, 0)
6003 t
->spinner
= gtk_spinner_new();
6005 t
->label
= gtk_label_new(title
);
6006 bb
= create_button("Close", GTK_STOCK_CLOSE
, 1);
6007 gtk_widget_set_size_request(t
->label
, 100, 0);
6008 gtk_label_set_max_width_chars(GTK_LABEL(t
->label
), 20);
6009 gtk_label_set_ellipsize(GTK_LABEL(t
->label
), PANGO_ELLIPSIZE_END
);
6010 gtk_widget_set_size_request(b
, 130, 0);
6012 gtk_box_pack_start(GTK_BOX(b
), bb
, FALSE
, FALSE
, 0);
6013 gtk_box_pack_start(GTK_BOX(b
), t
->label
, FALSE
, FALSE
, 0);
6014 #if GTK_CHECK_VERSION(2, 20, 0)
6015 gtk_box_pack_start(GTK_BOX(b
), t
->spinner
, FALSE
, FALSE
, 0);
6019 if (browser_mode
== XT_BM_KIOSK
) {
6020 t
->toolbar
= create_kiosk_toolbar(t
);
6021 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
6024 t
->toolbar
= create_toolbar(t
);
6026 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
,
6034 t
->browser_win
= create_browser(t
);
6035 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
6037 /* oops message for user feedback */
6038 t
->oops
= gtk_entry_new();
6039 gtk_entry_set_inner_border(GTK_ENTRY(t
->oops
), NULL
);
6040 gtk_entry_set_has_frame(GTK_ENTRY(t
->oops
), FALSE
);
6041 gtk_widget_set_can_focus(GTK_WIDGET(t
->oops
), FALSE
);
6042 gdk_color_parse(XT_COLOR_RED
, &color
);
6043 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
6044 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
6045 gtk_widget_modify_font(GTK_WIDGET(t
->oops
), oops_font
);
6048 t
->cmd
= gtk_entry_new();
6049 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
6050 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
6051 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
6052 gtk_widget_modify_font(GTK_WIDGET(t
->cmd
), cmd_font
);
6055 t
->statusbar_box
= gtk_hbox_new(FALSE
, 0);
6057 t
->sbe
.statusbar
= gtk_entry_new();
6058 gtk_entry_set_inner_border(GTK_ENTRY(t
->sbe
.statusbar
), NULL
);
6059 gtk_entry_set_has_frame(GTK_ENTRY(t
->sbe
.statusbar
), FALSE
);
6060 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.statusbar
), FALSE
);
6061 gtk_widget_modify_font(GTK_WIDGET(t
->sbe
.statusbar
), statusbar_font
);
6063 /* create these widgets only if specified in statusbar_elems */
6065 t
->sbe
.position
= create_sbe(40);
6066 t
->sbe
.zoom
= create_sbe(40);
6067 t
->sbe
.buffercmd
= create_sbe(60);
6069 statusbar_modify_attr(t
, XT_COLOR_WHITE
, XT_COLOR_BLACK
);
6071 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), t
->sbe
.statusbar
, TRUE
,
6074 /* gtk widgets cannot be added to a box twice. sbe_* variables
6075 make sure of this */
6076 for (p
= statusbar_elems
; *p
!= '\0'; p
++) {
6080 GtkWidget
*sep
= gtk_vseparator_new();
6082 gdk_color_parse(XT_COLOR_SB_SEPARATOR
, &color
);
6083 gtk_widget_modify_bg(sep
, GTK_STATE_NORMAL
, &color
);
6084 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), sep
,
6085 FALSE
, FALSE
, FALSE
);
6090 warnx("flag \"%c\" specified more than "
6091 "once in statusbar_elems\n", *p
);
6095 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6096 t
->sbe
.position
, FALSE
, FALSE
, FALSE
);
6100 warnx("flag \"%c\" specified more than "
6101 "once in statusbar_elems\n", *p
);
6105 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6106 t
->sbe
.buffercmd
, FALSE
, FALSE
, FALSE
);
6110 warnx("flag \"%c\" specified more than "
6111 "once in statusbar_elems\n", *p
);
6115 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6116 t
->sbe
.zoom
, FALSE
, FALSE
, FALSE
);
6119 warnx("illegal flag \"%c\" in statusbar_elems\n", *p
);
6124 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar_box
, FALSE
, FALSE
, 0);
6127 t
->buffers
= create_buffers(t
);
6128 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->buffers
, FALSE
, FALSE
, 0);
6130 /* xtp meaning is normal by default */
6131 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
6133 /* set empty favicon */
6134 xt_icon_from_name(t
, "text-html");
6136 /* and show it all */
6137 gtk_widget_show_all(b
);
6138 gtk_widget_show_all(t
->vbox
);
6140 /* compact tab bar */
6141 t
->tab_elems
.label
= gtk_label_new(title
);
6142 gtk_label_set_width_chars(GTK_LABEL(t
->tab_elems
.label
), 1.0);
6143 gtk_misc_set_alignment(GTK_MISC(t
->tab_elems
.label
), 0.0, 0.0);
6144 gtk_misc_set_padding(GTK_MISC(t
->tab_elems
.label
), 4.0, 4.0);
6145 gtk_widget_modify_font(GTK_WIDGET(t
->tab_elems
.label
), tabbar_font
);
6147 t
->tab_elems
.eventbox
= gtk_event_box_new();
6148 t
->tab_elems
.box
= gtk_hbox_new(FALSE
, 0);
6149 t
->tab_elems
.sep
= gtk_vseparator_new();
6151 gdk_color_parse(XT_COLOR_CT_BACKGROUND
, &color
);
6152 gtk_widget_modify_bg(t
->tab_elems
.eventbox
, GTK_STATE_NORMAL
, &color
);
6153 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6154 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
, &color
);
6155 gdk_color_parse(XT_COLOR_CT_SEPARATOR
, &color
);
6156 gtk_widget_modify_bg(t
->tab_elems
.sep
, GTK_STATE_NORMAL
, &color
);
6158 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.label
, TRUE
,
6160 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.sep
, FALSE
,
6162 gtk_container_add(GTK_CONTAINER(t
->tab_elems
.eventbox
),
6165 gtk_box_pack_start(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
, TRUE
,
6167 gtk_widget_show_all(t
->tab_elems
.eventbox
);
6169 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
6172 id
= position
>= 0 ? position
:
6173 gtk_notebook_get_current_page(notebook
) + 1;
6174 if (id
> gtk_notebook_get_n_pages(notebook
))
6177 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
6178 gtk_notebook_insert_page(notebook
, t
->vbox
, b
, id
);
6179 gtk_box_reorder_child(GTK_BOX(tab_bar
),
6180 t
->tab_elems
.eventbox
, id
);
6185 #if GTK_CHECK_VERSION(2, 20, 0)
6186 /* turn spinner off if we are a new tab without uri */
6188 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
6189 gtk_widget_hide(t
->spinner
);
6192 /* make notebook tabs reorderable */
6193 gtk_notebook_set_tab_reorderable(notebook
, t
->vbox
, TRUE
);
6195 /* compact tabs clickable */
6196 g_signal_connect(G_OBJECT(t
->tab_elems
.eventbox
),
6197 "button_press_event", G_CALLBACK(tab_clicked_cb
), t
);
6199 g_object_connect(G_OBJECT(t
->cmd
),
6200 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb
), t
,
6201 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
6202 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb
), t
,
6203 "signal::activate", G_CALLBACK(cmd_activate_cb
), t
,
6204 "signal::populate-popup", G_CALLBACK(cmd_popup_cb
), t
,
6207 /* reuse wv_button_cb to hide oops */
6208 g_object_connect(G_OBJECT(t
->oops
),
6209 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
6212 g_signal_connect(t
->buffers
,
6213 "row-activated", G_CALLBACK(row_activated_cb
), t
);
6214 g_object_connect(G_OBJECT(t
->buffers
),
6215 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
, (char *)NULL
);
6217 g_object_connect(G_OBJECT(t
->wv
),
6218 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
,
6219 "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb
), t
,
6220 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb
), t
,
6221 "signal::download-requested", G_CALLBACK(webview_download_cb
), t
,
6222 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), t
,
6223 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
6224 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
6225 "signal::create-web-view", G_CALLBACK(webview_cwv_cb
), t
,
6226 "signal::close-web-view", G_CALLBACK(webview_closewv_cb
), t
,
6227 "signal::event", G_CALLBACK(webview_event_cb
), t
,
6228 "signal::load-finished", G_CALLBACK(webview_load_finished_cb
), t
,
6229 "signal::load-progress-changed", G_CALLBACK(webview_progress_changed_cb
), t
,
6230 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb
), t
,
6231 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
6232 "signal::button_release_event", G_CALLBACK(wv_release_button_cb
), t
,
6233 "signal::populate-popup", G_CALLBACK(wv_popup_cb
), t
,
6235 g_signal_connect(t
->wv
,
6236 "notify::load-status", G_CALLBACK(notify_load_status_cb
), t
);
6237 g_signal_connect(t
->wv
,
6238 "notify::title", G_CALLBACK(notify_title_cb
), t
);
6240 /* hijack the unused keys as if we were the browser */
6241 g_object_connect(G_OBJECT(t
->toolbar
),
6242 "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb
), t
,
6245 g_signal_connect(G_OBJECT(bb
), "button_press_event",
6246 G_CALLBACK(tab_close_cb
), t
);
6249 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
6250 /* restore the tab's history */
6251 if (u
&& u
->history
) {
6255 webkit_web_back_forward_list_add_item(t
->bfl
, item
);
6256 items
= g_list_next(items
);
6259 item
= g_list_nth_data(u
->history
, u
->back
);
6261 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
6264 g_list_free(u
->history
);
6266 webkit_web_back_forward_list_clear(t
->bfl
);
6272 url_set_visibility();
6273 statusbar_set_visibility();
6276 set_current_tab(t
->tab_id
);
6277 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
6281 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
6285 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6292 recolor_compact_tabs();
6293 setzoom_webkit(t
, XT_ZOOM_NORMAL
);
6298 notebook_switchpage_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
6304 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
6306 if (gtk_notebook_get_current_page(notebook
) == -1)
6309 TAILQ_FOREACH(t
, &tabs
, entry
) {
6310 if (t
->tab_id
== pn
) {
6311 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
6314 uri
= get_title(t
, TRUE
);
6315 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
6321 /* can't use focus_webview here */
6322 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6329 notebook_pagereordered_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
6332 struct tab
*t
= NULL
, *tt
;
6336 TAILQ_FOREACH(tt
, &tabs
, entry
)
6337 if (tt
->tab_id
== pn
) {
6343 DNPRINTF(XT_D_TAB
, "page_reordered_cb: tab: %d\n", t
->tab_id
);
6345 gtk_box_reorder_child(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
,
6350 menuitem_response(struct tab
*t
)
6352 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
6356 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
6358 GtkWidget
*menu
, *menu_items
;
6359 GdkEventButton
*bevent
;
6363 if (event
->type
== GDK_BUTTON_PRESS
) {
6364 bevent
= (GdkEventButton
*) event
;
6365 menu
= gtk_menu_new();
6367 TAILQ_FOREACH(ti
, &tabs
, entry
) {
6368 if ((uri
= get_uri(ti
)) == NULL
)
6369 /* XXX make sure there is something to print */
6370 /* XXX add gui pages in here to look purdy */
6372 menu_items
= gtk_menu_item_new_with_label(uri
);
6373 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), menu_items
);
6374 gtk_widget_show(menu_items
);
6376 g_signal_connect_swapped((menu_items
),
6377 "activate", G_CALLBACK(menuitem_response
),
6381 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
,
6382 bevent
->button
, bevent
->time
);
6384 /* unref object so it'll free itself when popped down */
6385 #if !GTK_CHECK_VERSION(3, 0, 0)
6386 /* XXX does not need unref with gtk+3? */
6387 g_object_ref_sink(menu
);
6388 g_object_unref(menu
);
6391 return (TRUE
/* eat event */);
6394 return (FALSE
/* propagate */);
6398 icon_size_map(int icon_size
)
6400 if (icon_size
<= GTK_ICON_SIZE_INVALID
||
6401 icon_size
> GTK_ICON_SIZE_DIALOG
)
6402 return (GTK_ICON_SIZE_SMALL_TOOLBAR
);
6408 create_button(char *name
, char *stockid
, int size
)
6410 GtkWidget
*button
, *image
;
6414 rcstring
= g_strdup_printf(
6415 "style \"%s-style\"\n"
6417 " GtkWidget::focus-padding = 0\n"
6418 " GtkWidget::focus-line-width = 0\n"
6422 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
6423 gtk_rc_parse_string(rcstring
);
6425 button
= gtk_button_new();
6426 gtk_button_set_focus_on_click(GTK_BUTTON(button
), FALSE
);
6427 gtk_icon_size
= icon_size_map(size
? size
: icon_size
);
6429 image
= gtk_image_new_from_stock(stockid
, gtk_icon_size
);
6430 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
6431 gtk_container_set_border_width(GTK_CONTAINER(button
), 1);
6432 gtk_container_add(GTK_CONTAINER(button
), GTK_WIDGET(image
));
6433 gtk_widget_set_name(button
, name
);
6434 gtk_button_set_relief(GTK_BUTTON(button
), GTK_RELIEF_NONE
);
6440 button_set_stockid(GtkWidget
*button
, char *stockid
)
6444 image
= gtk_image_new_from_stock(stockid
, icon_size_map(icon_size
));
6445 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
6446 gtk_button_set_image(GTK_BUTTON(button
), image
);
6450 clipb_primary_cb(GtkClipboard
*primary
, GdkEvent
*event
, gpointer notused
)
6453 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
6456 if (xterm_workaround
== 0)
6460 * xterm doesn't play nice with clipboards because it clears the
6461 * primary when clicked. We rely on primary being set to properly
6462 * handle middle mouse button clicks (paste). So when someone clears
6463 * primary copy whatever is in CUT_BUFFER0 into primary to simualte
6464 * other application behavior (as in DON'T clear primary).
6467 p
= gtk_clipboard_wait_for_text(primary
);
6469 if (gdk_property_get(gdk_get_default_root_window(),
6471 gdk_atom_intern("STRING", FALSE
),
6473 1024 * 1024 /* picked out of my butt */,
6479 /* yes sir, we need to NUL the string */
6481 gtk_clipboard_set_text(primary
, p
, -1);
6495 char file
[PATH_MAX
];
6498 vbox
= gtk_vbox_new(FALSE
, 0);
6499 gtk_box_set_spacing(GTK_BOX(vbox
), 0);
6500 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
6501 #if !GTK_CHECK_VERSION(3, 0, 0)
6502 /* XXX seems to be needed with gtk+2 */
6503 gtk_notebook_set_tab_hborder(notebook
, 0);
6504 gtk_notebook_set_tab_vborder(notebook
, 0);
6506 gtk_notebook_set_scrollable(notebook
, TRUE
);
6507 gtk_notebook_set_show_border(notebook
, FALSE
);
6508 gtk_widget_set_can_focus(GTK_WIDGET(notebook
), FALSE
);
6510 abtn
= gtk_button_new();
6511 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
6512 gtk_widget_set_size_request(arrow
, -1, -1);
6513 gtk_container_add(GTK_CONTAINER(abtn
), arrow
);
6514 gtk_widget_set_size_request(abtn
, -1, 20);
6516 #if GTK_CHECK_VERSION(2, 20, 0)
6517 gtk_notebook_set_action_widget(notebook
, abtn
, GTK_PACK_END
);
6519 gtk_widget_set_size_request(GTK_WIDGET(notebook
), -1, -1);
6521 /* compact tab bar */
6522 tab_bar
= gtk_hbox_new(TRUE
, 0);
6524 gtk_box_pack_start(GTK_BOX(vbox
), tab_bar
, FALSE
, FALSE
, 0);
6525 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
6526 gtk_widget_set_size_request(vbox
, -1, -1);
6528 g_object_connect(G_OBJECT(notebook
),
6529 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb
), NULL
,
6531 g_object_connect(G_OBJECT(notebook
),
6532 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb
),
6533 NULL
, (char *)NULL
);
6534 g_signal_connect(G_OBJECT(abtn
), "button_press_event",
6535 G_CALLBACK(arrow_cb
), NULL
);
6537 main_window
= create_window("xxxterm");
6538 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
6539 g_signal_connect(G_OBJECT(main_window
), "delete_event",
6540 G_CALLBACK(gtk_main_quit
), NULL
);
6543 for (i
= 0; i
< LENGTH(icons
); i
++) {
6544 snprintf(file
, sizeof file
, "%s/%s", resource_dir
, icons
[i
]);
6545 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
6546 l
= g_list_append(l
, pb
);
6548 gtk_window_set_default_icon_list(l
);
6550 /* clipboard work around */
6551 if (xterm_workaround
)
6553 G_OBJECT(gtk_clipboard_get(GDK_SELECTION_PRIMARY
)),
6554 "owner-change", G_CALLBACK(clipb_primary_cb
), NULL
);
6556 gtk_widget_show_all(abtn
);
6557 gtk_widget_show_all(main_window
);
6558 notebook_tab_set_visibility();
6562 send_cmd_to_socket(char *cmd
)
6565 struct sockaddr_un sa
;
6567 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
6568 warnx("%s: socket", __func__
);
6572 sa
.sun_family
= AF_UNIX
;
6573 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
6574 work_dir
, XT_SOCKET_FILE
);
6577 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
6578 warnx("%s: connect", __func__
);
6582 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
6583 warnx("%s: send", __func__
);
6594 socket_watcher(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
6597 char str
[XT_MAX_URL_LENGTH
];
6598 socklen_t t
= sizeof(struct sockaddr_un
);
6599 struct sockaddr_un sa
;
6604 gint fd
= g_io_channel_unix_get_fd(source
);
6606 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
6611 if (getpeereid(s
, &uid
, &gid
) == -1) {
6615 if (uid
!= getuid() || gid
!= getgid()) {
6616 warnx("unauthorized user");
6622 warnx("not a valid user");
6626 n
= recv(s
, str
, sizeof(str
), 0);
6630 tt
= TAILQ_LAST(&tabs
, tab_list
);
6631 cmd_execute(tt
, str
);
6639 struct sockaddr_un sa
;
6641 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
6642 warn("is_running: socket");
6646 sa
.sun_family
= AF_UNIX
;
6647 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
6648 work_dir
, XT_SOCKET_FILE
);
6651 /* connect to see if there is a listener */
6652 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
6653 rv
= 0; /* not running */
6655 rv
= 1; /* already running */
6666 struct sockaddr_un sa
;
6668 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
6669 warn("build_socket: socket");
6673 sa
.sun_family
= AF_UNIX
;
6674 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
6675 work_dir
, XT_SOCKET_FILE
);
6678 /* connect to see if there is a listener */
6679 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
6680 /* no listener so we will */
6681 unlink(sa
.sun_path
);
6683 if (bind(s
, (struct sockaddr
*)&sa
, len
) == -1) {
6684 warn("build_socket: bind");
6688 if (listen(s
, 1) == -1) {
6689 warn("build_socket: listen");
6702 completion_select_cb(GtkEntryCompletion
*widget
, GtkTreeModel
*model
,
6703 GtkTreeIter
*iter
, struct tab
*t
)
6707 gtk_tree_model_get(model
, iter
, 0, &value
, -1);
6715 completion_hover_cb(GtkEntryCompletion
*widget
, GtkTreeModel
*model
,
6716 GtkTreeIter
*iter
, struct tab
*t
)
6720 gtk_tree_model_get(model
, iter
, 0, &value
, -1);
6721 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), value
);
6722 gtk_editable_set_position(GTK_EDITABLE(t
->uri_entry
), -1);
6729 completion_add_uri(const gchar
*uri
)
6733 /* add uri to list_store */
6734 gtk_list_store_append(completion_model
, &iter
);
6735 gtk_list_store_set(completion_model
, &iter
, 0, uri
, -1);
6739 completion_match(GtkEntryCompletion
*completion
, const gchar
*key
,
6740 GtkTreeIter
*iter
, gpointer user_data
)
6743 gboolean match
= FALSE
;
6745 gtk_tree_model_get(GTK_TREE_MODEL(completion_model
), iter
, 0, &value
,
6751 match
= match_uri(value
, key
);
6758 completion_add(struct tab
*t
)
6760 /* enable completion for tab */
6761 t
->completion
= gtk_entry_completion_new();
6762 gtk_entry_completion_set_text_column(t
->completion
, 0);
6763 gtk_entry_set_completion(GTK_ENTRY(t
->uri_entry
), t
->completion
);
6764 gtk_entry_completion_set_model(t
->completion
,
6765 GTK_TREE_MODEL(completion_model
));
6766 gtk_entry_completion_set_match_func(t
->completion
, completion_match
,
6768 gtk_entry_completion_set_minimum_key_length(t
->completion
, 1);
6769 gtk_entry_completion_set_inline_selection(t
->completion
, TRUE
);
6770 g_signal_connect(G_OBJECT (t
->completion
), "match-selected",
6771 G_CALLBACK(completion_select_cb
), t
);
6772 g_signal_connect(G_OBJECT (t
->completion
), "cursor-on-match",
6773 G_CALLBACK(completion_hover_cb
), t
);
6781 if (stat(dir
, &sb
)) {
6782 if (mkdir(dir
, S_IRWXU
) == -1)
6783 err(1, "mkdir %s", dir
);
6785 err(1, "stat %s", dir
);
6787 if (S_ISDIR(sb
.st_mode
) == 0)
6788 errx(1, "%s not a dir", dir
);
6789 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
6790 warnx("fixing invalid permissions on %s", dir
);
6791 if (chmod(dir
, S_IRWXU
) == -1)
6792 err(1, "chmod %s", dir
);
6800 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
6804 GStaticRecMutex my_gdk_mtx
= G_STATIC_REC_MUTEX_INIT
;
6805 volatile int mtx_depth
;
6809 * The linux flash plugin violates the gdk locking mechanism.
6810 * Work around the issue by using a recursive mutex with some match applied
6811 * to see if we hit a buggy condition.
6813 * The following code is painful so just don't read it. It really doesn't
6814 * make much sense but seems to work.
6819 g_static_rec_mutex_lock(&my_gdk_mtx
);
6822 if (mtx_depth
<= 0) {
6823 /* should not happen */
6824 show_oops(NULL
, "negative mutex locking bug, trying to "
6826 fprintf(stderr
, "negative mutex locking bug, trying to "
6828 g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
6829 g_static_rec_mutex_lock(&my_gdk_mtx
);
6834 if (mtx_depth
!= 1) {
6835 /* decrease mutext depth to 1 */
6837 g_static_rec_mutex_unlock(&my_gdk_mtx
);
6839 } while (mtx_depth
> 1);
6848 /* if mutex depth isn't 1 then something went bad */
6849 if (mtx_depth
!= 1) {
6850 x
= g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
6852 /* should not happen */
6853 show_oops(NULL
, "mutex unlocking bug, trying to "
6855 fprintf(stderr
, "mutex unlocking bug, trying to "
6859 if (mtx_complain
== 0) {
6860 show_oops(NULL
, "buggy mutex implementation detected, "
6861 "work around implemented");
6862 fprintf(stderr
, "buggy mutex implementation detected, "
6863 "work around implemented");
6870 g_static_rec_mutex_unlock(&my_gdk_mtx
);
6874 main(int argc
, char *argv
[])
6877 int c
, s
, optn
= 0, opte
= 0, focus
= 1;
6878 char conf
[PATH_MAX
] = { '\0' };
6879 char file
[PATH_MAX
];
6880 char *env_proxy
= NULL
;
6884 struct sigaction sact
;
6885 GIOChannel
*channel
;
6892 g_thread_init(NULL
);
6893 gdk_threads_set_lock_functions(mtx_lock
, mtx_unlock
);
6895 gdk_threads_enter();
6897 gcry_control (GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
6899 gtk_init(&argc
, &argv
);
6901 gnutls_global_init();
6903 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
6908 RB_INIT(&downloads
);
6910 TAILQ_INIT(&sessions
);
6913 TAILQ_INIT(&aliases
);
6920 /* fiddle with ulimits */
6921 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
6924 /* just use them all */
6925 rlp
.rlim_cur
= rlp
.rlim_max
;
6926 if (setrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
6928 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
6930 else if (rlp
.rlim_cur
<= 256)
6931 startpage_add("%s requires at least 256 file "
6932 "descriptors, currently it has up to %d available",
6933 __progname
, rlp
.rlim_cur
);
6936 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
6945 errx(0 , "Version: %s", version
);
6948 strlcpy(conf
, optarg
, sizeof(conf
));
6951 strlcpy(named_session
, optarg
, sizeof(named_session
));
6972 xtp_generate_keys();
6975 bzero(&sact
, sizeof(sact
));
6976 sigemptyset(&sact
.sa_mask
);
6977 sact
.sa_handler
= sigchild
;
6978 sact
.sa_flags
= SA_NOCLDSTOP
;
6979 sigaction(SIGCHLD
, &sact
, NULL
);
6981 /* set download dir */
6982 pwd
= getpwuid(getuid());
6984 errx(1, "invalid user %d", getuid());
6985 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
6987 /* compile buffer command regexes */
6990 /* set default string settings */
6991 home
= g_strdup("https://www.cyphertite.com");
6992 search_string
= g_strdup("https://ssl.scroogle.org/cgi-bin/nbbwssl.cgi?Gw=%s");
6993 resource_dir
= g_strdup("/usr/local/share/xxxterm/");
6994 strlcpy(runtime_settings
, "runtime", sizeof runtime_settings
);
6995 cmd_font_name
= g_strdup("monospace normal 9");
6996 oops_font_name
= g_strdup("monospace normal 9");
6997 statusbar_font_name
= g_strdup("monospace normal 9");
6998 tabbar_font_name
= g_strdup("monospace normal 9");
6999 statusbar_elems
= g_strdup("BP");
7000 encoding
= g_strdup("ISO-8859-1");
7002 /* read config file */
7003 if (strlen(conf
) == 0)
7004 snprintf(conf
, sizeof conf
, "%s/.%s",
7005 pwd
->pw_dir
, XT_CONF_FILE
);
7006 config_parse(conf
, 0);
7009 cmd_font
= pango_font_description_from_string(cmd_font_name
);
7010 oops_font
= pango_font_description_from_string(oops_font_name
);
7011 statusbar_font
= pango_font_description_from_string(statusbar_font_name
);
7012 tabbar_font
= pango_font_description_from_string(tabbar_font_name
);
7014 /* working directory */
7015 if (strlen(work_dir
) == 0)
7016 snprintf(work_dir
, sizeof work_dir
, "%s/%s",
7017 pwd
->pw_dir
, XT_DIR
);
7020 /* icon cache dir */
7021 snprintf(cache_dir
, sizeof cache_dir
, "%s/%s", work_dir
, XT_CACHE_DIR
);
7025 snprintf(certs_dir
, sizeof certs_dir
, "%s/%s", work_dir
, XT_CERT_DIR
);
7029 snprintf(sessions_dir
, sizeof sessions_dir
, "%s/%s",
7030 work_dir
, XT_SESSIONS_DIR
);
7031 xxx_dir(sessions_dir
);
7033 /* runtime settings that can override config file */
7034 if (runtime_settings
[0] != '\0')
7035 config_parse(runtime_settings
, 1);
7038 if (!strcmp(download_dir
, pwd
->pw_dir
))
7039 strlcat(download_dir
, "/downloads", sizeof download_dir
);
7040 xxx_dir(download_dir
);
7042 /* favorites file */
7043 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_FAVS_FILE
);
7044 if (stat(file
, &sb
)) {
7045 warnx("favorites file doesn't exist, creating it");
7046 if ((f
= fopen(file
, "w")) == NULL
)
7047 err(1, "favorites");
7051 /* quickmarks file */
7052 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
7053 if (stat(file
, &sb
)) {
7054 warnx("quickmarks file doesn't exist, creating it");
7055 if ((f
= fopen(file
, "w")) == NULL
)
7056 err(1, "quickmarks");
7060 /* search history */
7061 if (history_autosave
) {
7062 snprintf(search_file
, sizeof search_file
, "%s/%s",
7063 work_dir
, XT_SEARCH_FILE
);
7064 if (stat(search_file
, &sb
)) {
7065 warnx("search history file doesn't exist, creating it");
7066 if ((f
= fopen(search_file
, "w")) == NULL
)
7067 err(1, "search_history");
7070 history_read(&shl
, search_file
, &search_history_count
);
7073 /* command history */
7074 if (history_autosave
) {
7075 snprintf(command_file
, sizeof command_file
, "%s/%s",
7076 work_dir
, XT_COMMAND_FILE
);
7077 if (stat(command_file
, &sb
)) {
7078 warnx("command history file doesn't exist, creating it");
7079 if ((f
= fopen(command_file
, "w")) == NULL
)
7080 err(1, "command_history");
7083 history_read(&chl
, command_file
, &cmd_history_count
);
7087 session
= webkit_get_default_session();
7092 if (stat(ssl_ca_file
, &sb
)) {
7093 warnx("no CA file: %s", ssl_ca_file
);
7094 g_free(ssl_ca_file
);
7097 g_object_set(session
,
7098 SOUP_SESSION_SSL_CA_FILE
, ssl_ca_file
,
7099 SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
7103 /* guess_search regex */
7104 if (url_regex
== NULL
)
7105 url_regex
= g_strdup(XT_URL_REGEX
);
7107 if (regcomp(&url_re
, url_regex
, REG_EXTENDED
| REG_NOSUB
))
7108 startpage_add("invalid url regex %s", url_regex
);
7111 env_proxy
= getenv("http_proxy");
7113 setup_proxy(env_proxy
);
7115 setup_proxy(http_proxy
);
7118 send_cmd_to_socket(argv
[0]);
7122 /* set some connection parameters */
7123 g_object_set(session
, "max-conns", max_connections
, (char *)NULL
);
7124 g_object_set(session
, "max-conns-per-host", max_host_connections
,
7127 /* see if there is already an xxxterm running */
7128 if (single_instance
&& is_running()) {
7130 warnx("already running");
7135 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
7136 send_cmd_to_socket(cmd
);
7146 /* uri completion */
7147 completion_model
= gtk_list_store_new(1, G_TYPE_STRING
);
7150 buffers_store
= gtk_list_store_new
7151 (NUM_COLS
, G_TYPE_UINT
, G_TYPE_STRING
);
7157 notebook_tab_set_visibility();
7159 if (save_global_history
)
7160 restore_global_history();
7162 /* restore session list */
7163 restore_sessions_list();
7165 if (!strcmp(named_session
, XT_SAVED_TABS_FILE
))
7166 restore_saved_tabs();
7168 a
.s
= named_session
;
7169 a
.i
= XT_SES_DONOTHING
;
7170 open_tabs(NULL
, &a
);
7173 /* see if we have an exception */
7174 if (!TAILQ_EMPTY(&spl
)) {
7175 create_new_tab("about:startpage", NULL
, focus
, -1);
7180 create_new_tab(argv
[0], NULL
, focus
, -1);
7187 if (TAILQ_EMPTY(&tabs
))
7188 create_new_tab(home
, NULL
, 1, -1);
7191 if ((s
= build_socket()) != -1) {
7192 channel
= g_io_channel_unix_new(s
);
7193 g_io_add_watch(channel
, G_IO_IN
, socket_watcher
, NULL
);
7199 gdk_threads_leave();
7200 g_static_rec_mutex_unlock_full(&my_gdk_mtx
); /* just in case */
7203 gnutls_global_deinit();