2 * Copyright (c) 2010, 2011 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2011 Stevan Andjelkovic <stevan@student.chalmers.se>
4 * Copyright (c) 2010, 2011 Edd Barrett <vext01@gmail.com>
5 * Copyright (c) 2011 Todd T. Fries <todd@fries.net>
6 * Copyright (c) 2011 Raphael Graf <r@undefined.ch>
7 * Copyright (c) 2011 Michal Mazurek <akfaew@jasminek.net>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 char *version
= XXXTERM_VERSION
;
27 u_int32_t swm_debug
= 0
48 GCRY_THREAD_OPTION_PTHREAD_IMPL
;
60 TAILQ_ENTRY(session
) entry
;
63 TAILQ_HEAD(session_list
, session
);
66 TAILQ_ENTRY(undo
) entry
;
69 int back
; /* Keeps track of how many back
70 * history items there are. */
72 TAILQ_HEAD(undo_tailq
, undo
);
74 struct command_entry
{
76 TAILQ_ENTRY(command_entry
) entry
;
78 TAILQ_HEAD(command_list
, command_entry
);
81 #define XT_DIR (".xxxterm")
82 #define XT_CACHE_DIR ("cache")
83 #define XT_CERT_DIR ("certs/")
84 #define XT_SESSIONS_DIR ("sessions/")
85 #define XT_CONF_FILE ("xxxterm.conf")
86 #define XT_QMARKS_FILE ("quickmarks")
87 #define XT_SAVED_TABS_FILE ("main_session")
88 #define XT_RESTART_TABS_FILE ("restart_tabs")
89 #define XT_SOCKET_FILE ("socket")
90 #define XT_HISTORY_FILE ("history")
91 #define XT_SAVE_SESSION_ID ("SESSION_NAME=")
92 #define XT_SEARCH_FILE ("search_history")
93 #define XT_COMMAND_FILE ("command_history")
94 #define XT_DLMAN_REFRESH "10"
95 #define XT_MAX_URL_LENGTH (4096) /* 1 page is atomic, don't make bigger */
96 #define XT_MAX_UNDO_CLOSE_TAB (32)
97 #define XT_RESERVED_CHARS "$&+,/:;=?@ \"<>#%%{}|^~[]`"
98 #define XT_PRINT_EXTRA_MARGIN 10
99 #define XT_URL_REGEX ("^[[:blank:]]*[^[:blank:]]*([[:alnum:]-]+\\.)+[[:alnum:]-][^[:blank:]]*[[:blank:]]*$")
100 #define XT_INVALID_MARK (-1) /* XXX this is a double, maybe use something else, like a nan */
103 #define XT_COLOR_RED "#cc0000"
104 #define XT_COLOR_YELLOW "#ffff66"
105 #define XT_COLOR_BLUE "lightblue"
106 #define XT_COLOR_GREEN "#99ff66"
107 #define XT_COLOR_WHITE "white"
108 #define XT_COLOR_BLACK "black"
110 #define XT_COLOR_CT_BACKGROUND "#000000"
111 #define XT_COLOR_CT_INACTIVE "#dddddd"
112 #define XT_COLOR_CT_ACTIVE "#bbbb00"
113 #define XT_COLOR_CT_SEPARATOR "#555555"
115 #define XT_COLOR_SB_SEPARATOR "#555555"
117 #define XT_PROTO_DELIM "://"
120 #define XT_MOVE_INVALID (0)
121 #define XT_MOVE_DOWN (1)
122 #define XT_MOVE_UP (2)
123 #define XT_MOVE_BOTTOM (3)
124 #define XT_MOVE_TOP (4)
125 #define XT_MOVE_PAGEDOWN (5)
126 #define XT_MOVE_PAGEUP (6)
127 #define XT_MOVE_HALFDOWN (7)
128 #define XT_MOVE_HALFUP (8)
129 #define XT_MOVE_LEFT (9)
130 #define XT_MOVE_FARLEFT (10)
131 #define XT_MOVE_RIGHT (11)
132 #define XT_MOVE_FARRIGHT (12)
133 #define XT_MOVE_PERCENT (13)
134 #define XT_MOVE_CENTER (14)
136 #define XT_QMARK_SET (0)
137 #define XT_QMARK_OPEN (1)
138 #define XT_QMARK_TAB (2)
140 #define XT_MARK_SET (0)
141 #define XT_MARK_GOTO (1)
143 #define XT_TAB_LAST (-4)
144 #define XT_TAB_FIRST (-3)
145 #define XT_TAB_PREV (-2)
146 #define XT_TAB_NEXT (-1)
147 #define XT_TAB_INVALID (0)
148 #define XT_TAB_NEW (1)
149 #define XT_TAB_DELETE (2)
150 #define XT_TAB_DELQUIT (3)
151 #define XT_TAB_OPEN (4)
152 #define XT_TAB_UNDO_CLOSE (5)
153 #define XT_TAB_SHOW (6)
154 #define XT_TAB_HIDE (7)
155 #define XT_TAB_NEXTSTYLE (8)
157 #define XT_NAV_INVALID (0)
158 #define XT_NAV_BACK (1)
159 #define XT_NAV_FORWARD (2)
160 #define XT_NAV_RELOAD (3)
162 #define XT_FOCUS_INVALID (0)
163 #define XT_FOCUS_URI (1)
164 #define XT_FOCUS_SEARCH (2)
166 #define XT_SEARCH_INVALID (0)
167 #define XT_SEARCH_NEXT (1)
168 #define XT_SEARCH_PREV (2)
170 #define XT_PASTE_CURRENT_TAB (0)
171 #define XT_PASTE_NEW_TAB (1)
173 #define XT_ZOOM_IN (-1)
174 #define XT_ZOOM_OUT (-2)
175 #define XT_ZOOM_NORMAL (100)
177 #define XT_URL_SHOW (1)
178 #define XT_URL_HIDE (2)
180 #define XT_CMD_OPEN (0)
181 #define XT_CMD_OPEN_CURRENT (1)
182 #define XT_CMD_TABNEW (2)
183 #define XT_CMD_TABNEW_CURRENT (3)
185 #define XT_STATUS_NOTHING (0)
186 #define XT_STATUS_LINK (1)
187 #define XT_STATUS_URI (2)
188 #define XT_STATUS_LOADING (3)
190 #define XT_SES_DONOTHING (0)
191 #define XT_SES_CLOSETABS (1)
193 #define XT_PREFIX (1<<0)
194 #define XT_USERARG (1<<1)
195 #define XT_URLARG (1<<2)
196 #define XT_INTARG (1<<3)
197 #define XT_SESSARG (1<<4)
198 #define XT_SETARG (1<<5)
200 #define XT_HINT_NEWTAB (1<<0)
202 #define XT_MODE_INSERT (0)
203 #define XT_MODE_COMMAND (1)
205 #define XT_BUFCMD_SZ (8)
207 #define XT_EJS_SHOW (1<<0)
209 GtkWidget
* create_button(char *, char *, int);
211 void recalc_tabs(void);
212 void recolor_compact_tabs(void);
213 void set_current_tab(int page_num
);
214 gboolean
update_statusbar_position(GtkAdjustment
*, gpointer
);
215 void marks_clear(struct tab
*t
);
218 extern char *__progname
;
221 GtkWidget
*main_window
;
222 GtkNotebook
*notebook
;
224 GtkWidget
*arrow
, *abtn
;
225 struct tab_list tabs
;
226 struct history_list hl
;
227 struct session_list sessions
;
228 struct domain_list c_wl
;
229 struct domain_list js_wl
;
230 struct domain_list pl_wl
;
231 struct undo_tailq undos
;
232 struct keybinding_list kbl
;
234 struct command_list chl
;
235 struct command_list shl
;
236 struct command_entry
*history_at
;
237 struct command_entry
*search_at
;
239 int cmd_history_count
= 0;
240 int search_history_count
= 0;
242 long long unsigned int blocked_cookies
= 0;
243 char named_session
[PATH_MAX
];
244 GtkListStore
*completion_model
;
245 GtkListStore
*buffers_store
;
247 char *qmarks
[XT_NOMARKS
];
248 int btn_down
; /* M1 down in any wv */
249 regex_t url_re
; /* guess_search regex */
251 /* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
252 int next_download_id
= 1;
254 void xxx_dir(char *);
255 int icon_size_map(int);
256 void completion_add(struct tab
*);
257 void completion_add_uri(const gchar
*);
260 history_delete(struct command_list
*l
, int *counter
)
262 struct command_entry
*c
;
264 if (l
== NULL
|| counter
== NULL
)
267 c
= TAILQ_LAST(l
, command_list
);
271 TAILQ_REMOVE(l
, c
, entry
);
278 history_add(struct command_list
*list
, char *file
, char *l
, int *counter
)
280 struct command_entry
*c
;
283 if (list
== NULL
|| l
== NULL
|| counter
== NULL
)
286 /* don't add the same line */
287 c
= TAILQ_FIRST(list
);
289 if (!strcmp(c
->line
+ 1 /* skip space */, l
))
292 c
= g_malloc0(sizeof *c
);
293 c
->line
= g_strdup_printf(" %s", l
);
296 TAILQ_INSERT_HEAD(list
, c
, entry
);
299 history_delete(list
, counter
);
301 if (history_autosave
&& file
) {
302 f
= fopen(file
, "w");
304 show_oops(NULL
, "couldn't write history %s", file
);
308 TAILQ_FOREACH_REVERSE(c
, list
, command_list
, entry
) {
310 fprintf(f
, "%s\n", c
->line
);
318 history_read(struct command_list
*list
, char *file
, int *counter
)
321 char *s
, line
[65536];
323 if (list
== NULL
|| file
== NULL
)
326 f
= fopen(file
, "r");
328 startpage_add("couldn't open history file %s", file
);
333 s
= fgets(line
, sizeof line
, f
);
334 if (s
== NULL
|| feof(f
) || ferror(f
))
336 if ((s
= strchr(line
, '\n')) == NULL
) {
337 startpage_add("invalid history file %s", file
);
343 history_add(list
, NULL
, line
+ 1, counter
);
351 /* marks and quickmarks array storage.
352 * first a-z, then A-Z, then 0-9 */
359 if (i
>= 0 && i
<= 'z' - 'a')
363 if (i
>= 0 && i
<= 'Z' - 'A')
378 if (m
>= 'a' && m
<= 'z')
379 return ret
+ m
- 'a';
381 ret
+= 'z' - 'a' + 1;
382 if (m
>= 'A' && m
<= 'Z')
383 return ret
+ m
- 'A';
385 ret
+= 'Z' - 'A' + 1;
386 if (m
>= '0' && m
<= '9')
387 return ret
+ m
- '0';
396 int saved_errno
, status
;
401 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
405 if (errno
!= ECHILD
) {
407 clog_warn("sigchild: waitpid:");
413 if (WIFEXITED(status
)) {
414 if (WEXITSTATUS(status
) != 0) {
416 clog_warnx("sigchild: child exit status: %d",
417 WEXITSTATUS(status));
422 clog_warnx("sigchild: child is terminated abnormally");
431 is_g_object_setting(GObject
*o
, char *str
)
433 guint n_props
= 0, i
;
434 GParamSpec
**proplist
;
436 if (! G_IS_OBJECT(o
))
439 proplist
= g_object_class_list_properties(G_OBJECT_GET_CLASS(o
),
442 for (i
=0; i
< n_props
; i
++) {
443 if (! strcmp(proplist
[i
]->name
, str
))
450 get_current_tab(void)
454 TAILQ_FOREACH(t
, &tabs
, entry
) {
455 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
459 warnx("%s: no current tab", __func__
);
465 set_status(struct tab
*t
, gchar
*s
, int status
)
473 case XT_STATUS_LOADING
:
474 type
= g_strdup_printf("Loading: %s", s
);
478 type
= g_strdup_printf("Link: %s", s
);
480 t
->status
= g_strdup(gtk_entry_get_text(
481 GTK_ENTRY(t
->sbe
.statusbar
)));
485 type
= g_strdup_printf("%s", s
);
487 t
->status
= g_strdup(type
);
491 t
->status
= g_strdup(s
);
493 case XT_STATUS_NOTHING
:
498 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.statusbar
), s
);
504 hide_cmd(struct tab
*t
)
506 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
508 history_at
= NULL
; /* just in case */
509 search_at
= NULL
; /* just in case */
510 gtk_widget_hide(t
->cmd
);
514 show_cmd(struct tab
*t
)
516 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
520 gtk_widget_hide(t
->oops
);
521 gtk_widget_show(t
->cmd
);
525 hide_buffers(struct tab
*t
)
527 gtk_widget_hide(t
->buffers
);
528 gtk_list_store_clear(buffers_store
);
538 sort_tabs_by_page_num(struct tab
***stabs
)
543 num_tabs
= gtk_notebook_get_n_pages(notebook
);
545 *stabs
= g_malloc0(num_tabs
* sizeof(struct tab
*));
547 TAILQ_FOREACH(t
, &tabs
, entry
)
548 (*stabs
)[gtk_notebook_page_num(notebook
, t
->vbox
)] = t
;
554 buffers_make_list(void)
557 const gchar
*title
= NULL
;
559 struct tab
**stabs
= NULL
;
561 num_tabs
= sort_tabs_by_page_num(&stabs
);
563 for (i
= 0; i
< num_tabs
; i
++)
565 gtk_list_store_append(buffers_store
, &iter
);
566 title
= get_title(stabs
[i
], FALSE
);
567 gtk_list_store_set(buffers_store
, &iter
,
568 COL_ID
, i
+ 1, /* Enumerate the tabs starting from 1
578 show_buffers(struct tab
*t
)
581 gtk_widget_show(t
->buffers
);
582 gtk_widget_grab_focus(GTK_WIDGET(t
->buffers
));
586 toggle_buffers(struct tab
*t
)
588 if (gtk_widget_get_visible(t
->buffers
))
595 buffers(struct tab
*t
, struct karg
*args
)
603 hide_oops(struct tab
*t
)
605 gtk_widget_hide(t
->oops
);
609 show_oops(struct tab
*at
, const char *fmt
, ...)
613 struct tab
*t
= NULL
;
619 if ((t
= get_current_tab()) == NULL
)
625 if (vasprintf(&msg
, fmt
, ap
) == -1)
626 errx(1, "show_oops failed");
629 gtk_entry_set_text(GTK_ENTRY(t
->oops
), msg
);
630 gtk_widget_hide(t
->cmd
);
631 gtk_widget_show(t
->oops
);
637 char work_dir
[PATH_MAX
];
638 char certs_dir
[PATH_MAX
];
639 char cache_dir
[PATH_MAX
];
640 char sessions_dir
[PATH_MAX
];
641 char cookie_file
[PATH_MAX
];
642 SoupSession
*session
;
643 SoupCookieJar
*s_cookiejar
;
644 SoupCookieJar
*p_cookiejar
;
645 char rc_fname
[PATH_MAX
];
647 struct mime_type_list mtl
;
648 struct alias_list aliases
;
651 struct tab
*create_new_tab(char *, struct undo
*, int, int);
652 void delete_tab(struct tab
*);
653 void setzoom_webkit(struct tab
*, int);
654 int run_script(struct tab
*, char *);
655 int download_rb_cmp(struct download
*, struct download
*);
656 gboolean
cmd_execute(struct tab
*t
, char *str
);
659 history_rb_cmp(struct history
*h1
, struct history
*h2
)
661 return (strcmp(h1
->uri
, h2
->uri
));
663 RB_GENERATE(history_list
, history
, entry
, history_rb_cmp
);
666 domain_rb_cmp(struct domain
*d1
, struct domain
*d2
)
668 return (strcmp(d1
->d
, d2
->d
));
670 RB_GENERATE(domain_list
, domain
, entry
, domain_rb_cmp
);
673 download_rb_cmp(struct download
*e1
, struct download
*e2
)
675 return (e1
->id
< e2
->id
? -1 : e1
->id
> e2
->id
);
677 RB_GENERATE(download_list
, download
, entry
, download_rb_cmp
);
679 struct valid_url_types
{
690 valid_url_type(char *url
)
694 for (i
= 0; i
< LENGTH(vut
); i
++)
695 if (!strncasecmp(vut
[i
].type
, url
, strlen(vut
[i
].type
)))
702 match_alias(char *url_in
)
706 char *url_out
= NULL
, *search
, *enc_arg
;
708 search
= g_strdup(url_in
);
710 if (strsep(&arg
, " \t") == NULL
) {
711 show_oops(NULL
, "match_alias: NULL URL");
715 TAILQ_FOREACH(a
, &aliases
, entry
) {
716 if (!strcmp(search
, a
->a_name
))
721 DNPRINTF(XT_D_URL
, "match_alias: matched alias %s\n",
724 enc_arg
= soup_uri_encode(arg
, XT_RESERVED_CHARS
);
725 url_out
= g_strdup_printf(a
->a_uri
, enc_arg
);
728 url_out
= g_strdup_printf(a
->a_uri
, "");
736 guess_url_type(char *url_in
)
739 char *url_out
= NULL
, *enc_search
= NULL
;
742 /* substitute aliases */
743 url_out
= match_alias(url_in
);
747 /* see if we are an about page */
748 if (!strncmp(url_in
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
))
749 for (i
= 0; i
< about_list_size(); i
++)
750 if (!strcmp(&url_in
[XT_URI_ABOUT_LEN
],
751 about_list
[i
].name
)) {
752 url_out
= g_strdup(url_in
);
756 if (guess_search
&& url_regex
&&
757 !(g_str_has_prefix(url_in
, "http://") ||
758 g_str_has_prefix(url_in
, "https://"))) {
759 if (regexec(&url_re
, url_in
, 0, NULL
, 0)) {
760 /* invalid URI so search instead */
761 enc_search
= soup_uri_encode(url_in
, XT_RESERVED_CHARS
);
762 url_out
= g_strdup_printf(search_string
, enc_search
);
768 /* XXX not sure about this heuristic */
769 if (stat(url_in
, &sb
) == 0)
770 url_out
= g_strdup_printf("file://%s", url_in
);
772 url_out
= g_strdup_printf("http://%s", url_in
); /* guess http */
774 DNPRINTF(XT_D_URL
, "guess_url_type: guessed %s\n", url_out
);
780 load_uri(struct tab
*t
, gchar
*uri
)
783 gchar
*newuri
= NULL
;
789 /* Strip leading spaces. */
790 while (*uri
&& isspace(*uri
))
793 if (strlen(uri
) == 0) {
798 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
800 if (valid_url_type(uri
)) {
801 newuri
= guess_url_type(uri
);
805 if (!strncmp(uri
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
)) {
806 for (i
= 0; i
< about_list_size(); i
++)
807 if (!strcmp(&uri
[XT_URI_ABOUT_LEN
], about_list
[i
].name
)) {
808 bzero(&args
, sizeof args
);
809 about_list
[i
].func(t
, &args
);
810 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
),
814 show_oops(t
, "invalid about page");
818 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
820 webkit_web_view_load_uri(t
->wv
, uri
);
827 get_uri(struct tab
*t
)
829 const gchar
*uri
= NULL
;
831 if (webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
)
833 if (t
->xtp_meaning
== XT_XTP_TAB_MEANING_NORMAL
) {
834 uri
= webkit_web_view_get_uri(t
->wv
);
836 /* use tmp_uri to make sure it is g_freed */
839 t
->tmp_uri
=g_strdup_printf("%s%s", XT_URI_ABOUT
,
840 about_list
[t
->xtp_meaning
].name
);
847 get_title(struct tab
*t
, bool window
)
849 const gchar
*set
= NULL
, *title
= NULL
;
850 WebKitLoadStatus status
= webkit_web_view_get_load_status(t
->wv
);
852 if (status
== WEBKIT_LOAD_PROVISIONAL
|| status
== WEBKIT_LOAD_FAILED
||
853 t
->xtp_meaning
== XT_XTP_TAB_MEANING_BL
)
856 title
= webkit_web_view_get_title(t
->wv
);
857 if ((set
= title
? title
: get_uri(t
)))
861 set
= window
? XT_NAME
: "(untitled)";
867 find_mime_type(char *mime_type
)
869 struct mime_type
*m
, *def
= NULL
, *rv
= NULL
;
871 TAILQ_FOREACH(m
, &mtl
, entry
) {
873 !strncmp(mime_type
, m
->mt_type
, strlen(m
->mt_type
)))
876 if (m
->mt_default
== 0 && !strcmp(mime_type
, m
->mt_type
)) {
889 wl_find_uri(const gchar
*s
, struct domain_list
*wl
)
895 if (s
== NULL
|| wl
== NULL
)
898 if (!strncmp(s
, "http://", strlen("http://")))
899 s
= &s
[strlen("http://")];
900 else if (!strncmp(s
, "https://", strlen("https://")))
901 s
= &s
[strlen("https://")];
906 for (i
= 0; i
< strlen(s
) + 1 /* yes er need this */; i
++)
907 /* chop string at first slash */
908 if (s
[i
] == '/' || s
[i
] == ':' || s
[i
] == '\0') {
920 js_ref_to_string(JSContextRef context
, JSValueRef ref
)
926 jsref
= JSValueToStringCopy(context
, ref
, NULL
);
930 l
= JSStringGetMaximumUTF8CStringSize(jsref
);
933 JSStringGetUTF8CString(jsref
, s
, l
);
934 JSStringRelease(jsref
);
940 disable_hints(struct tab
*t
)
942 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
944 run_script(t
, "hints.clearHints();");
950 enable_hints(struct tab
*t
)
952 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
954 run_script(t
, JS_HINTING
);
956 run_script(t
, "hints.createHints('', 'F');");
958 run_script(t
, "hints.createHints('', 'f');");
962 #define XT_JS_DONE ("done;")
963 #define XT_JS_DONE_LEN (strlen(XT_JS_DONE))
964 #define XT_JS_INSERT ("insert;")
965 #define XT_JS_INSERT_LEN (strlen(XT_JS_INSERT))
968 run_script(struct tab
*t
, char *s
)
970 JSGlobalContextRef ctx
;
971 WebKitWebFrame
*frame
;
973 JSValueRef val
, exception
;
976 DNPRINTF(XT_D_JS
, "run_script: tab %d %s\n",
977 t
->tab_id
, s
== (char *)JS_HINTING
? "JS_HINTING" : s
);
979 /* a bit silly but it prevents some heartburn */
980 if (t
->script_init
== 0) {
982 run_script(t
, JS_HINTING
);
985 frame
= webkit_web_view_get_main_frame(t
->wv
);
986 ctx
= webkit_web_frame_get_global_context(frame
);
988 str
= JSStringCreateWithUTF8CString(s
);
989 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
990 NULL
, 0, &exception
);
991 JSStringRelease(str
);
993 DNPRINTF(XT_D_JS
, "run_script: val %p\n", val
);
995 es
= js_ref_to_string(ctx
, exception
);
997 /* show_oops(t, "script exception: %s", es); */
998 DNPRINTF(XT_D_JS
, "run_script: exception %s\n", es
);
1003 es
= js_ref_to_string(ctx
, val
);
1006 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
1008 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
1012 DNPRINTF(XT_D_JS
, "run_script: val %s\n", es
);
1021 hint(struct tab
*t
, struct karg
*args
)
1024 DNPRINTF(XT_D_JS
, "hint: tab %d args %d\n", t
->tab_id
, args
->i
);
1026 if (t
->hints_on
== 0) {
1027 if (args
->i
== XT_HINT_NEWTAB
)
1037 apply_style(struct tab
*t
)
1039 g_object_set(G_OBJECT(t
->settings
),
1040 "user-stylesheet-uri", t
->stylesheet
, (char *)NULL
);
1044 userstyle(struct tab
*t
, struct karg
*args
)
1046 DNPRINTF(XT_D_JS
, "userstyle: tab %d\n", t
->tab_id
);
1050 g_object_set(G_OBJECT(t
->settings
),
1051 "user-stylesheet-uri", NULL
, (char *)NULL
);
1060 * Doesn't work fully, due to the following bug:
1061 * https://bugs.webkit.org/show_bug.cgi?id=51747
1064 restore_global_history(void)
1066 char file
[PATH_MAX
];
1071 const char delim
[3] = {'\\', '\\', '\0'};
1073 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_HISTORY_FILE
);
1075 if ((f
= fopen(file
, "r")) == NULL
) {
1076 warnx("%s: fopen", __func__
);
1081 if ((uri
= fparseln(f
, NULL
, NULL
, delim
, 0)) == NULL
)
1082 if (feof(f
) || ferror(f
))
1085 if ((title
= fparseln(f
, NULL
, NULL
, delim
, 0)) == NULL
)
1086 if (feof(f
) || ferror(f
)) {
1088 warnx("%s: broken history file\n", __func__
);
1092 if (uri
&& strlen(uri
) && title
&& strlen(title
)) {
1093 webkit_web_history_item_new_with_data(uri
, title
);
1094 h
= g_malloc(sizeof(struct history
));
1095 h
->uri
= g_strdup(uri
);
1096 h
->title
= g_strdup(title
);
1097 RB_INSERT(history_list
, &hl
, h
);
1098 completion_add_uri(h
->uri
);
1100 warnx("%s: failed to restore history\n", __func__
);
1116 save_global_history_to_disk(struct tab
*t
)
1118 char file
[PATH_MAX
];
1122 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_HISTORY_FILE
);
1124 if ((f
= fopen(file
, "w")) == NULL
) {
1125 show_oops(t
, "%s: global history file: %s",
1126 __func__
, strerror(errno
));
1130 RB_FOREACH_REVERSE(h
, history_list
, &hl
) {
1131 if (h
->uri
&& h
->title
)
1132 fprintf(f
, "%s\n%s\n", h
->uri
, h
->title
);
1141 quit(struct tab
*t
, struct karg
*args
)
1143 if (save_global_history
)
1144 save_global_history_to_disk(t
);
1152 restore_sessions_list(void)
1155 struct dirent
*dp
= NULL
;
1158 sdir
= opendir(sessions_dir
);
1160 while ((dp
= readdir(sdir
)) != NULL
)
1161 if (dp
->d_type
== DT_REG
) {
1162 s
= g_malloc(sizeof(struct session
));
1163 s
->name
= g_strdup(dp
->d_name
);
1164 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
1171 open_tabs(struct tab
*t
, struct karg
*a
)
1173 char file
[PATH_MAX
];
1177 struct tab
*ti
, *tt
;
1182 ti
= TAILQ_LAST(&tabs
, tab_list
);
1184 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, a
->s
);
1185 if ((f
= fopen(file
, "r")) == NULL
)
1189 if ((uri
= fparseln(f
, NULL
, NULL
, "\0\0\0", 0)) == NULL
)
1190 if (feof(f
) || ferror(f
))
1193 /* retrieve session name */
1194 if (uri
&& g_str_has_prefix(uri
, XT_SAVE_SESSION_ID
)) {
1195 strlcpy(named_session
,
1196 &uri
[strlen(XT_SAVE_SESSION_ID
)],
1197 sizeof named_session
);
1201 if (uri
&& strlen(uri
))
1202 create_new_tab(uri
, NULL
, 1, -1);
1208 /* close open tabs */
1209 if (a
->i
== XT_SES_CLOSETABS
&& ti
!= NULL
) {
1211 tt
= TAILQ_FIRST(&tabs
);
1232 restore_saved_tabs(void)
1234 char file
[PATH_MAX
];
1235 int unlink_file
= 0;
1240 snprintf(file
, sizeof file
, "%s/%s",
1241 sessions_dir
, XT_RESTART_TABS_FILE
);
1242 if (stat(file
, &sb
) == -1)
1243 a
.s
= XT_SAVED_TABS_FILE
;
1246 a
.s
= XT_RESTART_TABS_FILE
;
1249 a
.i
= XT_SES_DONOTHING
;
1250 rv
= open_tabs(NULL
, &a
);
1259 save_tabs(struct tab
*t
, struct karg
*a
)
1261 char file
[PATH_MAX
];
1263 int num_tabs
= 0, i
;
1264 struct tab
**stabs
= NULL
;
1266 /* tab may be null here */
1271 snprintf(file
, sizeof file
, "%s/%s",
1272 sessions_dir
, named_session
);
1274 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, a
->s
);
1276 if ((f
= fopen(file
, "w")) == NULL
) {
1277 show_oops(t
, "Can't open save_tabs file: %s", strerror(errno
));
1281 /* save session name */
1282 fprintf(f
, "%s%s\n", XT_SAVE_SESSION_ID
, named_session
);
1284 /* Save tabs, in the order they are arranged in the notebook. */
1285 num_tabs
= sort_tabs_by_page_num(&stabs
);
1287 for (i
= 0; i
< num_tabs
; i
++)
1289 if (get_uri(stabs
[i
]) != NULL
)
1290 fprintf(f
, "%s\n", get_uri(stabs
[i
]));
1291 else if (gtk_entry_get_text(GTK_ENTRY(
1292 stabs
[i
]->uri_entry
)))
1293 fprintf(f
, "%s\n", gtk_entry_get_text(GTK_ENTRY(
1294 stabs
[i
]->uri_entry
)));
1299 /* try and make sure this gets to disk NOW. XXX Backup first? */
1300 if (fflush(f
) != 0 || fsync(fileno(f
)) != 0) {
1301 show_oops(t
, "May not have managed to save session: %s",
1311 save_tabs_and_quit(struct tab
*t
, struct karg
*args
)
1323 run_page_script(struct tab
*t
, struct karg
*args
)
1326 char *tmp
, script
[PATH_MAX
];
1328 tmp
= args
->s
!= NULL
&& strlen(args
->s
) > 0 ? args
->s
: default_script
;
1329 if (tmp
[0] == '\0') {
1330 show_oops(t
, "no script specified");
1334 if ((uri
= get_uri(t
)) == NULL
) {
1335 show_oops(t
, "tab is empty, not running script");
1340 snprintf(script
, sizeof script
, "%s/%s",
1341 pwd
->pw_dir
, &tmp
[1]);
1343 strlcpy(script
, tmp
, sizeof script
);
1347 show_oops(t
, "can't fork to run script");
1357 execlp(script
, script
, uri
, (void *)NULL
);
1367 yank_uri(struct tab
*t
, struct karg
*args
)
1370 GtkClipboard
*clipboard
;
1372 if ((uri
= get_uri(t
)) == NULL
)
1375 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1376 gtk_clipboard_set_text(clipboard
, uri
, -1);
1382 paste_uri(struct tab
*t
, struct karg
*args
)
1384 GtkClipboard
*clipboard
;
1385 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
1387 gchar
*p
= NULL
, *uri
;
1389 /* try primary clipboard first */
1390 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1391 p
= gtk_clipboard_wait_for_text(clipboard
);
1393 /* if it failed get whatever text is in cut_buffer0 */
1394 if (p
== NULL
&& xterm_workaround
)
1395 if (gdk_property_get(gdk_get_default_root_window(),
1397 gdk_atom_intern("STRING", FALSE
),
1399 1024 * 1024 /* picked out of my butt */,
1405 /* yes sir, we need to NUL the string */
1411 while (*uri
&& isspace(*uri
))
1413 if (strlen(uri
) == 0) {
1414 show_oops(t
, "empty paste buffer");
1417 if (guess_search
== 0 && valid_url_type(uri
)) {
1418 /* we can be clever and paste this in search box */
1419 show_oops(t
, "not a valid URL");
1423 if (args
->i
== XT_PASTE_CURRENT_TAB
)
1425 else if (args
->i
== XT_PASTE_NEW_TAB
)
1426 create_new_tab(uri
, NULL
, 1, -1);
1437 js_toggle_cb(GtkWidget
*w
, struct tab
*t
)
1442 g_object_get(G_OBJECT(t
->settings
),
1443 "enable-scripts", &es
, (char *)NULL
);
1448 set
= XT_WL_DISABLE
;
1450 a
.i
= set
| XT_WL_TOPLEVEL
;
1453 a
.i
= set
| XT_WL_TOPLEVEL
;
1456 a
.i
= XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
;
1461 toggle_src(struct tab
*t
, struct karg
*args
)
1468 mode
= webkit_web_view_get_view_source_mode(t
->wv
);
1469 webkit_web_view_set_view_source_mode(t
->wv
, !mode
);
1470 webkit_web_view_reload(t
->wv
);
1476 focus_webview(struct tab
*t
)
1481 /* only grab focus if we are visible */
1482 if (gtk_notebook_get_current_page(notebook
) == t
->tab_id
)
1483 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
1487 focus(struct tab
*t
, struct karg
*args
)
1489 if (t
== NULL
|| args
== NULL
)
1495 if (args
->i
== XT_FOCUS_URI
)
1496 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
1497 else if (args
->i
== XT_FOCUS_SEARCH
)
1498 gtk_widget_grab_focus(GTK_WIDGET(t
->search_entry
));
1504 connect_socket_from_uri(const gchar
*uri
, const gchar
**error_str
, char *domain
,
1508 struct addrinfo hints
, *res
= NULL
, *ai
;
1509 int rv
= -1, s
= -1, on
, error
;
1511 static gchar myerror
[256]; /* this is not thread safe */
1514 *error_str
= myerror
;
1515 if (uri
&& !g_str_has_prefix(uri
, "https://")) {
1516 *error_str
= "invalid URI";
1520 su
= soup_uri_new(uri
);
1522 *error_str
= "invalid soup URI";
1525 if (!SOUP_URI_VALID_FOR_HTTP(su
)) {
1526 *error_str
= "invalid HTTPS URI";
1530 snprintf(port
, sizeof port
, "%d", su
->port
);
1531 bzero(&hints
, sizeof(struct addrinfo
));
1532 hints
.ai_flags
= AI_CANONNAME
;
1533 hints
.ai_family
= AF_UNSPEC
;
1534 hints
.ai_socktype
= SOCK_STREAM
;
1536 if ((error
= getaddrinfo(su
->host
, port
, &hints
, &res
))) {
1537 snprintf(myerror
, sizeof myerror
, "getaddrinfo failed: %s",
1538 gai_strerror(errno
));
1542 for (ai
= res
; ai
; ai
= ai
->ai_next
) {
1548 if (ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
)
1550 s
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
1553 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &on
,
1556 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) == 0)
1560 snprintf(myerror
, sizeof myerror
,
1561 "could not obtain certificates from: %s",
1567 strlcpy(domain
, su
->host
, domain_sz
);
1574 if (rv
== -1 && s
!= -1)
1581 stop_tls(gnutls_session_t gsession
, gnutls_certificate_credentials_t xcred
)
1584 gnutls_deinit(gsession
);
1586 gnutls_certificate_free_credentials(xcred
);
1592 start_tls(const gchar
**error_str
, int s
, gnutls_session_t
*gs
,
1593 gnutls_certificate_credentials_t
*xc
)
1595 gnutls_certificate_credentials_t xcred
;
1596 gnutls_session_t gsession
;
1598 static gchar myerror
[1024]; /* this is not thread safe */
1600 if (gs
== NULL
|| xc
== NULL
)
1607 gnutls_certificate_allocate_credentials(&xcred
);
1608 gnutls_certificate_set_x509_trust_file(xcred
, ssl_ca_file
,
1609 GNUTLS_X509_FMT_PEM
);
1611 gnutls_init(&gsession
, GNUTLS_CLIENT
);
1612 gnutls_priority_set_direct(gsession
, "PERFORMANCE", NULL
);
1613 gnutls_credentials_set(gsession
, GNUTLS_CRD_CERTIFICATE
, xcred
);
1614 gnutls_transport_set_ptr(gsession
, (gnutls_transport_ptr_t
)(long)s
);
1615 if ((rv
= gnutls_handshake(gsession
)) < 0) {
1616 snprintf(myerror
, sizeof myerror
,
1617 "gnutls_handshake failed %d fatal %d %s",
1619 gnutls_error_is_fatal(rv
),
1620 gnutls_strerror_name(rv
));
1621 stop_tls(gsession
, xcred
);
1625 gnutls_credentials_type_t cred
;
1626 cred
= gnutls_auth_get_type(gsession
);
1627 if (cred
!= GNUTLS_CRD_CERTIFICATE
) {
1628 snprintf(myerror
, sizeof myerror
,
1629 "gnutls_auth_get_type failed %d",
1631 stop_tls(gsession
, xcred
);
1639 *error_str
= myerror
;
1644 get_connection_certs(gnutls_session_t gsession
, gnutls_x509_crt_t
**certs
,
1648 const gnutls_datum_t
*cl
;
1649 gnutls_x509_crt_t
*all_certs
;
1652 if (certs
== NULL
|| cert_count
== NULL
)
1654 if (gnutls_certificate_type_get(gsession
) != GNUTLS_CRT_X509
)
1656 cl
= gnutls_certificate_get_peers(gsession
, &len
);
1660 all_certs
= g_malloc(sizeof(gnutls_x509_crt_t
) * len
);
1661 for (i
= 0; i
< len
; i
++) {
1662 gnutls_x509_crt_init(&all_certs
[i
]);
1663 if (gnutls_x509_crt_import(all_certs
[i
], &cl
[i
],
1664 GNUTLS_X509_FMT_PEM
< 0)) {
1678 free_connection_certs(gnutls_x509_crt_t
*certs
, size_t cert_count
)
1682 for (i
= 0; i
< cert_count
; i
++)
1683 gnutls_x509_crt_deinit(certs
[i
]);
1688 statusbar_modify_attr(struct tab
*t
, const char *text
, const char *base
)
1690 GdkColor c_text
, c_base
;
1692 gdk_color_parse(text
, &c_text
);
1693 gdk_color_parse(base
, &c_base
);
1695 gtk_widget_modify_text(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_text
);
1696 gtk_widget_modify_text(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_text
);
1697 gtk_widget_modify_text(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_text
);
1698 gtk_widget_modify_text(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_text
);
1700 gtk_widget_modify_base(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_base
);
1701 gtk_widget_modify_base(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_base
);
1702 gtk_widget_modify_base(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_base
);
1703 gtk_widget_modify_base(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_base
);
1707 save_certs(struct tab
*t
, gnutls_x509_crt_t
*certs
,
1708 size_t cert_count
, char *domain
)
1711 char cert_buf
[64 * 1024], file
[PATH_MAX
];
1716 if (t
== NULL
|| certs
== NULL
|| cert_count
<= 0 || domain
== NULL
)
1719 snprintf(file
, sizeof file
, "%s/%s", certs_dir
, domain
);
1720 if ((f
= fopen(file
, "w")) == NULL
) {
1721 show_oops(t
, "Can't create cert file %s %s",
1722 file
, strerror(errno
));
1726 for (i
= 0; i
< cert_count
; i
++) {
1727 cert_buf_sz
= sizeof cert_buf
;
1728 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1729 cert_buf
, &cert_buf_sz
)) {
1730 show_oops(t
, "gnutls_x509_crt_export failed");
1733 if (fwrite(cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1734 show_oops(t
, "Can't write certs: %s", strerror(errno
));
1739 /* not the best spot but oh well */
1740 gdk_color_parse("lightblue", &color
);
1741 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
1742 statusbar_modify_attr(t
, XT_COLOR_BLACK
, "lightblue");
1755 load_compare_cert(const gchar
*uri
, const gchar
**error_str
)
1757 char domain
[8182], file
[PATH_MAX
];
1758 char cert_buf
[64 * 1024], r_cert_buf
[64 * 1024];
1760 unsigned int error
= 0;
1762 size_t cert_buf_sz
, cert_count
;
1763 enum cert_trust rv
= CERT_UNTRUSTED
;
1764 static gchar serr
[80]; /* this isn't thread safe */
1765 gnutls_session_t gsession
;
1766 gnutls_x509_crt_t
*certs
;
1767 gnutls_certificate_credentials_t xcred
;
1769 DNPRINTF(XT_D_URL
, "%s: %s\n", __func__
, uri
);
1773 if ((s
= connect_socket_from_uri(uri
, error_str
, domain
,
1774 sizeof domain
)) == -1)
1777 DNPRINTF(XT_D_URL
, "%s: fd %d\n", __func__
, s
);
1780 if (start_tls(error_str
, s
, &gsession
, &xcred
))
1782 DNPRINTF(XT_D_URL
, "%s: got tls\n", __func__
);
1784 /* verify certs in case cert file doesn't exist */
1785 if (gnutls_certificate_verify_peers2(gsession
, &error
) !=
1787 *error_str
= "Invalid certificates";
1792 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1793 *error_str
= "Can't get connection certificates";
1797 snprintf(file
, sizeof file
, "%s/%s", certs_dir
, domain
);
1798 if ((f
= fopen(file
, "r")) == NULL
) {
1804 for (i
= 0; i
< cert_count
; i
++) {
1805 cert_buf_sz
= sizeof cert_buf
;
1806 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1807 cert_buf
, &cert_buf_sz
)) {
1810 if (fread(r_cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1811 rv
= CERT_BAD
; /* critical */
1814 if (bcmp(r_cert_buf
, cert_buf
, sizeof cert_buf_sz
)) {
1815 rv
= CERT_BAD
; /* critical */
1824 free_connection_certs(certs
, cert_count
);
1826 /* we close the socket first for speed */
1830 /* only complain if we didn't save it locally */
1831 if (error
&& rv
!= CERT_LOCAL
) {
1832 strlcpy(serr
, "Certificate exception(s): ", sizeof serr
);
1833 if (error
& GNUTLS_CERT_INVALID
)
1834 strlcat(serr
, "invalid, ", sizeof serr
);
1835 if (error
& GNUTLS_CERT_REVOKED
)
1836 strlcat(serr
, "revoked, ", sizeof serr
);
1837 if (error
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
1838 strlcat(serr
, "signer not found, ", sizeof serr
);
1839 if (error
& GNUTLS_CERT_SIGNER_NOT_CA
)
1840 strlcat(serr
, "not signed by CA, ", sizeof serr
);
1841 if (error
& GNUTLS_CERT_INSECURE_ALGORITHM
)
1842 strlcat(serr
, "insecure algorithm, ", sizeof serr
);
1843 if (error
& GNUTLS_CERT_NOT_ACTIVATED
)
1844 strlcat(serr
, "not activated, ", sizeof serr
);
1845 if (error
& GNUTLS_CERT_EXPIRED
)
1846 strlcat(serr
, "expired, ", sizeof serr
);
1847 for (i
= strlen(serr
) - 1; i
> 0; i
--)
1848 if (serr
[i
] == ',') {
1855 stop_tls(gsession
, xcred
);
1861 cert_cmd(struct tab
*t
, struct karg
*args
)
1863 const gchar
*uri
, *error_str
= NULL
;
1867 gnutls_session_t gsession
;
1868 gnutls_x509_crt_t
*certs
;
1869 gnutls_certificate_credentials_t xcred
;
1874 if (ssl_ca_file
== NULL
) {
1875 show_oops(t
, "Can't open CA file: %s", ssl_ca_file
);
1879 if ((uri
= get_uri(t
)) == NULL
) {
1880 show_oops(t
, "Invalid URI");
1884 if ((s
= connect_socket_from_uri(uri
, &error_str
, domain
,
1885 sizeof domain
)) == -1) {
1886 show_oops(t
, "%s", error_str
);
1891 if (start_tls(&error_str
, s
, &gsession
, &xcred
))
1895 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1896 show_oops(t
, "get_connection_certs failed");
1900 if (args
->i
& XT_SHOW
)
1901 show_certs(t
, certs
, cert_count
, "Certificate Chain");
1902 else if (args
->i
& XT_SAVE
)
1903 save_certs(t
, certs
, cert_count
, domain
);
1905 free_connection_certs(certs
, cert_count
);
1907 /* we close the socket first for speed */
1910 stop_tls(gsession
, xcred
);
1911 if (error_str
&& strlen(error_str
))
1912 show_oops(t
, "%s", error_str
);
1917 remove_cookie(int index
)
1923 DNPRINTF(XT_D_COOKIE
, "remove_cookie: %d\n", index
);
1925 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
1927 for (i
= 1; cf
; cf
= cf
->next
, i
++) {
1931 print_cookie("remove cookie", c
);
1932 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
1937 soup_cookies_free(cf
);
1943 toplevel_cmd(struct tab
*t
, struct karg
*args
)
1945 js_toggle_cb(t
->js_toggle
, t
);
1951 can_go_back_for_real(struct tab
*t
)
1954 WebKitWebHistoryItem
*item
;
1960 /* rely on webkit to make sure we can go backward when on an about page */
1962 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
1963 return (webkit_web_view_can_go_back(t
->wv
));
1965 /* the back/forwars list is stupid so help determine if we can go back */
1966 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
1968 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
1969 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
1977 can_go_forward_for_real(struct tab
*t
)
1980 WebKitWebHistoryItem
*item
;
1986 /* rely on webkit to make sure we can go forward when on an about page */
1988 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
1989 return (webkit_web_view_can_go_forward(t
->wv
));
1991 /* the back/forwars list is stupid so help selecting a different item */
1992 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
1994 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
1995 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
2003 go_back_for_real(struct tab
*t
)
2006 WebKitWebHistoryItem
*item
;
2014 webkit_web_view_go_back(t
->wv
);
2017 /* the back/forwars list is stupid so help selecting a different item */
2018 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2020 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2021 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2022 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2029 go_forward_for_real(struct tab
*t
)
2032 WebKitWebHistoryItem
*item
;
2040 webkit_web_view_go_forward(t
->wv
);
2043 /* the back/forwars list is stupid so help selecting a different item */
2044 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2046 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2047 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2048 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2055 navaction(struct tab
*t
, struct karg
*args
)
2057 WebKitWebHistoryItem
*item
;
2058 WebKitWebFrame
*frame
;
2060 DNPRINTF(XT_D_NAV
, "navaction: tab %d opcode %d\n",
2061 t
->tab_id
, args
->i
);
2063 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
2065 if (args
->i
== XT_NAV_BACK
)
2066 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2068 item
= webkit_web_back_forward_list_get_forward_item(t
->bfl
);
2070 return (XT_CB_PASSTHROUGH
);
2071 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2073 return (XT_CB_PASSTHROUGH
);
2079 go_back_for_real(t
);
2081 case XT_NAV_FORWARD
:
2083 go_forward_for_real(t
);
2086 frame
= webkit_web_view_get_main_frame(t
->wv
);
2087 webkit_web_frame_reload(frame
);
2090 return (XT_CB_PASSTHROUGH
);
2094 move(struct tab
*t
, struct karg
*args
)
2096 GtkAdjustment
*adjust
;
2097 double pi
, si
, pos
, ps
, upper
, lower
, max
;
2103 case XT_MOVE_BOTTOM
:
2105 case XT_MOVE_PAGEDOWN
:
2106 case XT_MOVE_PAGEUP
:
2107 case XT_MOVE_HALFDOWN
:
2108 case XT_MOVE_HALFUP
:
2109 case XT_MOVE_PERCENT
:
2110 case XT_MOVE_CENTER
:
2111 adjust
= t
->adjust_v
;
2114 adjust
= t
->adjust_h
;
2118 pos
= gtk_adjustment_get_value(adjust
);
2119 ps
= gtk_adjustment_get_page_size(adjust
);
2120 upper
= gtk_adjustment_get_upper(adjust
);
2121 lower
= gtk_adjustment_get_lower(adjust
);
2122 si
= gtk_adjustment_get_step_increment(adjust
);
2123 pi
= gtk_adjustment_get_page_increment(adjust
);
2126 DNPRINTF(XT_D_MOVE
, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2127 "max %f si %f pi %f\n",
2128 args
->i
, adjust
== t
->adjust_h
? "horizontal" : "vertical",
2129 pos
, ps
, upper
, lower
, max
, si
, pi
);
2135 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2140 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2142 case XT_MOVE_BOTTOM
:
2143 case XT_MOVE_FARRIGHT
:
2144 gtk_adjustment_set_value(adjust
, max
);
2147 case XT_MOVE_FARLEFT
:
2148 gtk_adjustment_set_value(adjust
, lower
);
2150 case XT_MOVE_PAGEDOWN
:
2152 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2154 case XT_MOVE_PAGEUP
:
2156 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2158 case XT_MOVE_HALFDOWN
:
2160 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2162 case XT_MOVE_HALFUP
:
2164 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2166 case XT_MOVE_CENTER
:
2167 args
->s
= g_strdup("50.0");
2169 case XT_MOVE_PERCENT
:
2170 percent
= atoi(args
->s
) / 100.0;
2171 pos
= max
* percent
;
2172 if (pos
< 0.0 || pos
> max
)
2174 gtk_adjustment_set_value(adjust
, pos
);
2177 return (XT_CB_PASSTHROUGH
);
2180 DNPRINTF(XT_D_MOVE
, "move: new pos %f %f\n", pos
, MIN(pos
, max
));
2182 return (XT_CB_HANDLED
);
2186 url_set_visibility(void)
2190 TAILQ_FOREACH(t
, &tabs
, entry
)
2191 if (show_url
== 0) {
2192 gtk_widget_hide(t
->toolbar
);
2195 gtk_widget_show(t
->toolbar
);
2199 notebook_tab_set_visibility(void)
2201 if (show_tabs
== 0) {
2202 gtk_widget_hide(tab_bar
);
2203 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2205 if (tab_style
== XT_TABS_NORMAL
) {
2206 gtk_widget_hide(tab_bar
);
2207 gtk_notebook_set_show_tabs(notebook
, TRUE
);
2208 } else if (tab_style
== XT_TABS_COMPACT
) {
2209 gtk_widget_show(tab_bar
);
2210 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2216 statusbar_set_visibility(void)
2220 TAILQ_FOREACH(t
, &tabs
, entry
){
2221 if (show_statusbar
== 0)
2222 gtk_widget_hide(t
->statusbar_box
);
2224 gtk_widget_show(t
->statusbar_box
);
2231 url_set(struct tab
*t
, int enable_url_entry
)
2236 show_url
= enable_url_entry
;
2238 if (enable_url_entry
) {
2239 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
2240 GTK_ENTRY_ICON_PRIMARY
, NULL
);
2241 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
), 0);
2243 pixbuf
= gtk_entry_get_icon_pixbuf(GTK_ENTRY(t
->uri_entry
),
2244 GTK_ENTRY_ICON_PRIMARY
);
2246 gtk_entry_get_progress_fraction(GTK_ENTRY(t
->uri_entry
));
2247 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
2248 GTK_ENTRY_ICON_PRIMARY
, pixbuf
);
2249 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
2255 fullscreen(struct tab
*t
, struct karg
*args
)
2257 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2260 return (XT_CB_PASSTHROUGH
);
2262 if (show_url
== 0) {
2270 url_set_visibility();
2271 notebook_tab_set_visibility();
2273 return (XT_CB_HANDLED
);
2277 statustoggle(struct tab
*t
, struct karg
*args
)
2279 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2281 if (show_statusbar
== 1) {
2283 statusbar_set_visibility();
2284 } else if (show_statusbar
== 0) {
2286 statusbar_set_visibility();
2288 return (XT_CB_HANDLED
);
2292 urlaction(struct tab
*t
, struct karg
*args
)
2294 int rv
= XT_CB_HANDLED
;
2296 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2299 return (XT_CB_PASSTHROUGH
);
2303 if (show_url
== 0) {
2305 url_set_visibility();
2309 if (show_url
== 1) {
2311 url_set_visibility();
2319 tabaction(struct tab
*t
, struct karg
*args
)
2321 int rv
= XT_CB_HANDLED
;
2322 char *url
= args
->s
;
2326 DNPRINTF(XT_D_TAB
, "tabaction: %p %d\n", t
, args
->i
);
2329 return (XT_CB_PASSTHROUGH
);
2333 if (strlen(url
) > 0)
2334 create_new_tab(url
, NULL
, 1, args
->precount
);
2336 create_new_tab(NULL
, NULL
, 1, args
->precount
);
2339 if (args
->precount
< 0)
2342 TAILQ_FOREACH(tt
, &tabs
, entry
)
2343 if (tt
->tab_id
== args
->precount
- 1) {
2348 case XT_TAB_DELQUIT
:
2349 if (gtk_notebook_get_n_pages(notebook
) > 1)
2355 if (strlen(url
) > 0)
2358 rv
= XT_CB_PASSTHROUGH
;
2364 if (show_tabs
== 0) {
2366 notebook_tab_set_visibility();
2370 if (show_tabs
== 1) {
2372 notebook_tab_set_visibility();
2375 case XT_TAB_NEXTSTYLE
:
2376 if (tab_style
== XT_TABS_NORMAL
) {
2377 tab_style
= XT_TABS_COMPACT
;
2378 recolor_compact_tabs();
2381 tab_style
= XT_TABS_NORMAL
;
2382 notebook_tab_set_visibility();
2384 case XT_TAB_UNDO_CLOSE
:
2385 if (undo_count
== 0) {
2386 DNPRINTF(XT_D_TAB
, "%s: no tabs to undo close",
2391 u
= TAILQ_FIRST(&undos
);
2392 create_new_tab(u
->uri
, u
, 1, -1);
2394 TAILQ_REMOVE(&undos
, u
, entry
);
2396 /* u->history is freed in create_new_tab() */
2401 rv
= XT_CB_PASSTHROUGH
;
2415 resizetab(struct tab
*t
, struct karg
*args
)
2417 if (t
== NULL
|| args
== NULL
) {
2418 show_oops(NULL
, "resizetab invalid parameters");
2419 return (XT_CB_PASSTHROUGH
);
2422 DNPRINTF(XT_D_TAB
, "resizetab: tab %d %d\n",
2423 t
->tab_id
, args
->i
);
2425 setzoom_webkit(t
, args
->i
);
2427 return (XT_CB_HANDLED
);
2431 movetab(struct tab
*t
, struct karg
*args
)
2435 if (t
== NULL
|| args
== NULL
) {
2436 show_oops(NULL
, "movetab invalid parameters");
2437 return (XT_CB_PASSTHROUGH
);
2440 DNPRINTF(XT_D_TAB
, "movetab: tab %d opcode %d\n",
2441 t
->tab_id
, args
->i
);
2443 if (args
->i
>= XT_TAB_INVALID
)
2444 return (XT_CB_PASSTHROUGH
);
2446 if (TAILQ_EMPTY(&tabs
))
2447 return (XT_CB_PASSTHROUGH
);
2449 n
= gtk_notebook_get_n_pages(notebook
);
2450 dest
= gtk_notebook_get_current_page(notebook
);
2454 if (args
->precount
< 0)
2455 dest
= dest
== n
- 1 ? 0 : dest
+ 1;
2457 dest
= args
->precount
- 1;
2461 if (args
->precount
< 0)
2464 dest
-= args
->precount
% n
;
2477 return (XT_CB_PASSTHROUGH
);
2480 if (dest
< 0 || dest
>= n
)
2481 return (XT_CB_PASSTHROUGH
);
2482 if (t
->tab_id
== dest
) {
2483 DNPRINTF(XT_D_TAB
, "movetab: do nothing\n");
2484 return (XT_CB_HANDLED
);
2487 set_current_tab(dest
);
2489 return (XT_CB_HANDLED
);
2495 command_mode(struct tab
*t
, struct karg
*args
)
2497 run_script(t
, JS_HINTING
);
2498 if (args
->i
== XT_MODE_COMMAND
)
2499 run_script(t
, "hints.clearFocus();");
2501 run_script(t
, "hints.focusInput();");
2503 return (XT_CB_HANDLED
);
2507 command(struct tab
*t
, struct karg
*args
)
2509 char *s
= NULL
, *ss
= NULL
;
2514 if (t
== NULL
|| args
== NULL
) {
2515 show_oops(NULL
, "command invalid parameters");
2516 return (XT_CB_PASSTHROUGH
);
2527 if (cmd_prefix
== 0)
2530 ss
= g_strdup_printf(":%d", cmd_prefix
);
2536 bzero(&a
, sizeof a
);
2542 bzero(&a
, sizeof a
);
2543 a
.i
= XT_HINT_NEWTAB
;
2553 case XT_CMD_OPEN_CURRENT
:
2556 case XT_CMD_TABNEW_CURRENT
:
2557 if (!s
) /* FALL THROUGH? */
2559 if ((uri
= get_uri(t
)) != NULL
) {
2560 ss
= g_strdup_printf("%s%s", s
, uri
);
2565 show_oops(t
, "command: invalid opcode %d", args
->i
);
2566 return (XT_CB_PASSTHROUGH
);
2569 DNPRINTF(XT_D_CMD
, "%s: tab %d type %s\n", __func__
, t
->tab_id
, s
);
2571 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), s
);
2572 gdk_color_parse(XT_COLOR_WHITE
, &color
);
2573 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
2575 gtk_widget_grab_focus(GTK_WIDGET(t
->cmd
));
2576 gtk_editable_set_position(GTK_EDITABLE(t
->cmd
), -1);
2581 return (XT_CB_HANDLED
);
2585 search(struct tab
*t
, struct karg
*args
)
2589 if (t
== NULL
|| args
== NULL
) {
2590 show_oops(NULL
, "search invalid parameters");
2595 case XT_SEARCH_NEXT
:
2596 d
= t
->search_forward
;
2598 case XT_SEARCH_PREV
:
2599 d
= !t
->search_forward
;
2602 return (XT_CB_PASSTHROUGH
);
2605 if (t
->search_text
== NULL
) {
2606 if (global_search
== NULL
)
2607 return (XT_CB_PASSTHROUGH
);
2609 d
= t
->search_forward
= TRUE
;
2610 t
->search_text
= g_strdup(global_search
);
2611 webkit_web_view_mark_text_matches(t
->wv
, global_search
, FALSE
, 0);
2612 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
2616 DNPRINTF(XT_D_CMD
, "search: tab %d opc %d forw %d text %s\n",
2617 t
->tab_id
, args
->i
, t
->search_forward
, t
->search_text
);
2619 webkit_web_view_search_text(t
->wv
, t
->search_text
, FALSE
, d
, TRUE
);
2621 return (XT_CB_HANDLED
);
2625 session_save(struct tab
*t
, char *filename
)
2631 if (strlen(filename
) == 0)
2634 if (filename
[0] == '.' || filename
[0] == '/')
2638 if (save_tabs(t
, &a
))
2640 strlcpy(named_session
, filename
, sizeof named_session
);
2642 /* add the new session to the list of sessions */
2643 s
= g_malloc(sizeof(struct session
));
2644 s
->name
= g_strdup(filename
);
2645 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
2653 session_open(struct tab
*t
, char *filename
)
2658 if (strlen(filename
) == 0)
2661 if (filename
[0] == '.' || filename
[0] == '/')
2665 a
.i
= XT_SES_CLOSETABS
;
2666 if (open_tabs(t
, &a
))
2669 strlcpy(named_session
, filename
, sizeof named_session
);
2677 session_delete(struct tab
*t
, char *filename
)
2679 char file
[PATH_MAX
];
2683 if (strlen(filename
) == 0)
2686 if (filename
[0] == '.' || filename
[0] == '/')
2689 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, filename
);
2693 if (!strcmp(filename
, named_session
))
2694 strlcpy(named_session
, XT_SAVED_TABS_FILE
,
2695 sizeof named_session
);
2697 /* remove session from sessions list */
2698 TAILQ_FOREACH(s
, &sessions
, entry
) {
2699 if (!strcmp(s
->name
, filename
))
2704 TAILQ_REMOVE(&sessions
, s
, entry
);
2705 g_free((gpointer
) s
->name
);
2714 session_cmd(struct tab
*t
, struct karg
*args
)
2716 char *filename
= args
->s
;
2721 if (args
->i
& XT_SHOW
)
2722 show_oops(t
, "Current session: %s", named_session
[0] == '\0' ?
2723 XT_SAVED_TABS_FILE
: named_session
);
2724 else if (args
->i
& XT_SAVE
) {
2725 if (session_save(t
, filename
)) {
2726 show_oops(t
, "Can't save session: %s",
2727 filename
? filename
: "INVALID");
2730 } else if (args
->i
& XT_OPEN
) {
2731 if (session_open(t
, filename
)) {
2732 show_oops(t
, "Can't open session: %s",
2733 filename
? filename
: "INVALID");
2736 } else if (args
->i
& XT_DELETE
) {
2737 if (session_delete(t
, filename
)) {
2738 show_oops(t
, "Can't delete session: %s",
2739 filename
? filename
: "INVALID");
2744 return (XT_CB_PASSTHROUGH
);
2748 script_cmd(struct tab
*t
, struct karg
*args
)
2750 JSGlobalContextRef ctx
;
2751 WebKitWebFrame
*frame
;
2753 JSValueRef val
, exception
;
2762 if ((f
= fopen(args
->s
, "r")) == NULL
) {
2763 show_oops(t
, "Can't open script file: %s", args
->s
);
2767 if (fstat(fileno(f
), &sb
) == -1) {
2768 show_oops(t
, "Can't stat script file: %s", args
->s
);
2772 buf
= g_malloc0(sb
.st_size
+ 1);
2773 if (fread(buf
, 1, sb
.st_size
, f
) != sb
.st_size
) {
2774 show_oops(t
, "Can't read script file: %s", args
->s
);
2778 /* this code needs to be redone */
2779 frame
= webkit_web_view_get_main_frame(t
->wv
);
2780 ctx
= webkit_web_frame_get_global_context(frame
);
2782 str
= JSStringCreateWithUTF8CString(buf
);
2783 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
2784 NULL
, 0, &exception
);
2785 JSStringRelease(str
);
2787 DNPRINTF(XT_D_JS
, "run_script: val %p\n", val
);
2789 es
= js_ref_to_string(ctx
, exception
);
2791 show_oops(t
, "script exception: %s", es
);
2796 es
= js_ref_to_string(ctx
, val
);
2799 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
2801 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
2805 show_oops(t
, "script complete return value: '%s'", es
);
2808 show_oops(t
, "script complete: without a return value");
2817 return (XT_CB_PASSTHROUGH
);
2821 * Make a hardcopy of the page
2824 print_page(struct tab
*t
, struct karg
*args
)
2826 WebKitWebFrame
*frame
;
2828 GtkPrintOperation
*op
;
2829 GtkPrintOperationAction action
;
2830 GtkPrintOperationResult print_res
;
2831 GError
*g_err
= NULL
;
2832 int marg_l
, marg_r
, marg_t
, marg_b
;
2834 DNPRINTF(XT_D_PRINTING
, "%s:", __func__
);
2836 ps
= gtk_page_setup_new();
2837 op
= gtk_print_operation_new();
2838 action
= GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
;
2839 frame
= webkit_web_view_get_main_frame(t
->wv
);
2841 /* the default margins are too small, so we will bump them */
2842 marg_l
= gtk_page_setup_get_left_margin(ps
, GTK_UNIT_MM
) +
2843 XT_PRINT_EXTRA_MARGIN
;
2844 marg_r
= gtk_page_setup_get_right_margin(ps
, GTK_UNIT_MM
) +
2845 XT_PRINT_EXTRA_MARGIN
;
2846 marg_t
= gtk_page_setup_get_top_margin(ps
, GTK_UNIT_MM
) +
2847 XT_PRINT_EXTRA_MARGIN
;
2848 marg_b
= gtk_page_setup_get_bottom_margin(ps
, GTK_UNIT_MM
) +
2849 XT_PRINT_EXTRA_MARGIN
;
2852 gtk_page_setup_set_left_margin(ps
, marg_l
, GTK_UNIT_MM
);
2853 gtk_page_setup_set_right_margin(ps
, marg_r
, GTK_UNIT_MM
);
2854 gtk_page_setup_set_top_margin(ps
, marg_t
, GTK_UNIT_MM
);
2855 gtk_page_setup_set_bottom_margin(ps
, marg_b
, GTK_UNIT_MM
);
2857 gtk_print_operation_set_default_page_setup(op
, ps
);
2859 /* this appears to free 'op' and 'ps' */
2860 print_res
= webkit_web_frame_print_full(frame
, op
, action
, &g_err
);
2862 /* check it worked */
2863 if (print_res
== GTK_PRINT_OPERATION_RESULT_ERROR
) {
2864 show_oops(NULL
, "can't print: %s", g_err
->message
);
2865 g_error_free (g_err
);
2873 go_home(struct tab
*t
, struct karg
*args
)
2880 set_encoding(struct tab
*t
, struct karg
*args
)
2884 if (args
->s
&& strlen(g_strstrip(args
->s
)) == 0) {
2885 e
= webkit_web_view_get_custom_encoding(t
->wv
);
2887 e
= webkit_web_view_get_encoding(t
->wv
);
2888 show_oops(t
, "encoding: %s", e
? e
: "N/A");
2890 webkit_web_view_set_custom_encoding(t
->wv
, args
->s
);
2896 restart(struct tab
*t
, struct karg
*args
)
2900 a
.s
= XT_RESTART_TABS_FILE
;
2902 execvp(start_argv
[0], start_argv
);
2911 int (*func
)(struct tab
*, struct karg
*);
2915 { "command_mode", 0, command_mode
, XT_MODE_COMMAND
, 0 },
2916 { "insert_mode", 0, command_mode
, XT_MODE_INSERT
, 0 },
2917 { "command", 0, command
, ':', 0 },
2918 { "search", 0, command
, '/', 0 },
2919 { "searchb", 0, command
, '?', 0 },
2920 { "hinting", 0, command
, '.', 0 },
2921 { "hinting_newtab", 0, command
, ',', 0 },
2922 { "togglesrc", 0, toggle_src
, 0, 0 },
2924 /* yanking and pasting */
2925 { "yankuri", 0, yank_uri
, 0, 0 },
2926 /* XXX: pasteuri{cur,new} do not work from the cmd_entry? */
2927 { "pasteuricur", 0, paste_uri
, XT_PASTE_CURRENT_TAB
, 0 },
2928 { "pasteurinew", 0, paste_uri
, XT_PASTE_NEW_TAB
, 0 },
2931 { "searchnext", 0, search
, XT_SEARCH_NEXT
, 0 },
2932 { "searchprevious", 0, search
, XT_SEARCH_PREV
, 0 },
2935 { "focusaddress", 0, focus
, XT_FOCUS_URI
, 0 },
2936 { "focussearch", 0, focus
, XT_FOCUS_SEARCH
, 0 },
2939 { "hinting", 0, hint
, 0, 0 },
2940 { "hinting_newtab", 0, hint
, XT_HINT_NEWTAB
, 0 },
2942 /* custom stylesheet */
2943 { "userstyle", 0, userstyle
, 0, 0 },
2946 { "goback", 0, navaction
, XT_NAV_BACK
, 0 },
2947 { "goforward", 0, navaction
, XT_NAV_FORWARD
, 0 },
2948 { "reload", 0, navaction
, XT_NAV_RELOAD
, 0 },
2950 /* vertical movement */
2951 { "scrolldown", 0, move
, XT_MOVE_DOWN
, 0 },
2952 { "scrollup", 0, move
, XT_MOVE_UP
, 0 },
2953 { "scrollbottom", 0, move
, XT_MOVE_BOTTOM
, 0 },
2954 { "scrolltop", 0, move
, XT_MOVE_TOP
, 0 },
2955 { "1", 0, move
, XT_MOVE_TOP
, 0 },
2956 { "scrollhalfdown", 0, move
, XT_MOVE_HALFDOWN
, 0 },
2957 { "scrollhalfup", 0, move
, XT_MOVE_HALFUP
, 0 },
2958 { "scrollpagedown", 0, move
, XT_MOVE_PAGEDOWN
, 0 },
2959 { "scrollpageup", 0, move
, XT_MOVE_PAGEUP
, 0 },
2960 /* horizontal movement */
2961 { "scrollright", 0, move
, XT_MOVE_RIGHT
, 0 },
2962 { "scrollleft", 0, move
, XT_MOVE_LEFT
, 0 },
2963 { "scrollfarright", 0, move
, XT_MOVE_FARRIGHT
, 0 },
2964 { "scrollfarleft", 0, move
, XT_MOVE_FARLEFT
, 0 },
2966 { "favorites", 0, xtp_page_fl
, 0, 0 },
2967 { "fav", 0, xtp_page_fl
, 0, 0 },
2968 { "favadd", 0, add_favorite
, 0, 0 },
2970 { "qall", 0, quit
, 0, 0 },
2971 { "quitall", 0, quit
, 0, 0 },
2972 { "w", 0, save_tabs
, 0, 0 },
2973 { "wq", 0, save_tabs_and_quit
, 0, 0 },
2974 { "help", 0, help
, 0, 0 },
2975 { "about", 0, about
, 0, 0 },
2976 { "stats", 0, stats
, 0, 0 },
2977 { "version", 0, about
, 0, 0 },
2980 { "js", 0, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2981 { "save", 1, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
2982 { "domain", 2, js_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
2983 { "fqdn", 2, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
2984 { "show", 1, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2985 { "all", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2986 { "persistent", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
2987 { "session", 2, js_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
2988 { "toggle", 1, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
2989 { "domain", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
2990 { "fqdn", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
2992 /* cookie command */
2993 { "cookie", 0, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2994 { "save", 1, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
2995 { "domain", 2, cookie_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
2996 { "fqdn", 2, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
2997 { "show", 1, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2998 { "all", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
2999 { "persistent", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3000 { "session", 2, cookie_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3001 { "toggle", 1, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3002 { "domain", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3003 { "fqdn", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3005 /* plugin command */
3006 { "plugin", 0, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3007 { "save", 1, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3008 { "domain", 2, pl_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3009 { "fqdn", 2, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3010 { "show", 1, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3011 { "all", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3012 { "persistent", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3013 { "session", 2, pl_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3014 { "toggle", 1, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3015 { "domain", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3016 { "fqdn", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3018 /* toplevel (domain) command */
3019 { "toplevel", 0, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3020 { "toggle", 1, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3023 { "cookiejar", 0, xtp_page_cl
, 0, 0 },
3026 { "cert", 0, cert_cmd
, XT_SHOW
, 0 },
3027 { "save", 1, cert_cmd
, XT_SAVE
, 0 },
3028 { "show", 1, cert_cmd
, XT_SHOW
, 0 },
3030 { "ca", 0, ca_cmd
, 0, 0 },
3031 { "downloadmgr", 0, xtp_page_dl
, 0, 0 },
3032 { "dl", 0, xtp_page_dl
, 0, 0 },
3033 { "h", 0, xtp_page_hl
, 0, 0 },
3034 { "history", 0, xtp_page_hl
, 0, 0 },
3035 { "home", 0, go_home
, 0, 0 },
3036 { "restart", 0, restart
, 0, 0 },
3037 { "urlhide", 0, urlaction
, XT_URL_HIDE
, 0 },
3038 { "urlshow", 0, urlaction
, XT_URL_SHOW
, 0 },
3039 { "statustoggle", 0, statustoggle
, 0, 0 },
3040 { "run_script", 0, run_page_script
, 0, XT_USERARG
},
3042 { "print", 0, print_page
, 0, 0 },
3045 { "focusin", 0, resizetab
, XT_ZOOM_IN
, 0 },
3046 { "focusout", 0, resizetab
, XT_ZOOM_OUT
, 0 },
3047 { "focusreset", 0, resizetab
, XT_ZOOM_NORMAL
, 0 },
3048 { "q", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3049 { "quit", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3050 { "open", 0, tabaction
, XT_TAB_OPEN
, XT_URLARG
},
3051 { "tabclose", 0, tabaction
, XT_TAB_DELETE
, XT_PREFIX
| XT_INTARG
},
3052 { "tabedit", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3053 { "tabfirst", 0, movetab
, XT_TAB_FIRST
, 0 },
3054 { "tabhide", 0, tabaction
, XT_TAB_HIDE
, 0 },
3055 { "tablast", 0, movetab
, XT_TAB_LAST
, 0 },
3056 { "tabnew", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3057 { "tabnext", 0, movetab
, XT_TAB_NEXT
, XT_PREFIX
| XT_INTARG
},
3058 { "tabnextstyle", 0, tabaction
, XT_TAB_NEXTSTYLE
, 0 },
3059 { "tabprevious", 0, movetab
, XT_TAB_PREV
, XT_PREFIX
| XT_INTARG
},
3060 { "tabrewind", 0, movetab
, XT_TAB_FIRST
, 0 },
3061 { "tabshow", 0, tabaction
, XT_TAB_SHOW
, 0 },
3062 { "tabs", 0, buffers
, 0, 0 },
3063 { "tabundoclose", 0, tabaction
, XT_TAB_UNDO_CLOSE
, 0 },
3064 { "buffers", 0, buffers
, 0, 0 },
3065 { "ls", 0, buffers
, 0, 0 },
3066 { "encoding", 0, set_encoding
, 0, XT_USERARG
},
3068 /* command aliases (handy when -S flag is used) */
3069 { "promptopen", 0, command
, XT_CMD_OPEN
, 0 },
3070 { "promptopencurrent", 0, command
, XT_CMD_OPEN_CURRENT
, 0 },
3071 { "prompttabnew", 0, command
, XT_CMD_TABNEW
, 0 },
3072 { "prompttabnewcurrent",0, command
, XT_CMD_TABNEW_CURRENT
, 0 },
3075 { "set", 0, set
, 0, XT_SETARG
},
3077 { "fullscreen", 0, fullscreen
, 0, 0 },
3078 { "f", 0, fullscreen
, 0, 0 },
3081 { "session", 0, session_cmd
, XT_SHOW
, 0 },
3082 { "delete", 1, session_cmd
, XT_DELETE
, XT_SESSARG
},
3083 { "open", 1, session_cmd
, XT_OPEN
, XT_SESSARG
},
3084 { "save", 1, session_cmd
, XT_SAVE
, XT_USERARG
},
3085 { "show", 1, session_cmd
, XT_SHOW
, 0 },
3087 /* external javascript */
3088 { "script", 0, script_cmd
, XT_EJS_SHOW
, XT_USERARG
},
3091 { "inspector", 0, inspector_cmd
, XT_INS_SHOW
, 0 },
3092 { "show", 1, inspector_cmd
, XT_INS_SHOW
, 0 },
3093 { "hide", 1, inspector_cmd
, XT_INS_HIDE
, 0 },
3100 } cmd_status
= {-1, 0};
3103 wv_release_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3106 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 1)
3113 wv_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3120 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3122 else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 8 /* btn 4 */) {
3128 } else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 9 /* btn 5 */) {
3130 a
.i
= XT_NAV_FORWARD
;
3140 tab_close_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3142 DNPRINTF(XT_D_TAB
, "tab_close_cb: tab %d\n", t
->tab_id
);
3144 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3153 activate_uri_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3155 const gchar
*uri
= gtk_entry_get_text(GTK_ENTRY(entry
));
3157 DNPRINTF(XT_D_URL
, "activate_uri_entry_cb: %s\n", uri
);
3160 show_oops(NULL
, "activate_uri_entry_cb invalid parameters");
3165 show_oops(t
, "activate_uri_entry_cb no uri");
3169 uri
+= strspn(uri
, "\t ");
3171 /* if xxxt:// treat specially */
3172 if (parse_xtp_url(t
, uri
))
3175 /* otherwise continue to load page normally */
3176 load_uri(t
, (gchar
*)uri
);
3181 activate_search_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3183 const gchar
*search
= gtk_entry_get_text(GTK_ENTRY(entry
));
3184 char *newuri
= NULL
;
3187 DNPRINTF(XT_D_URL
, "activate_search_entry_cb: %s\n", search
);
3190 show_oops(NULL
, "activate_search_entry_cb invalid parameters");
3194 if (search_string
== NULL
) {
3195 show_oops(t
, "no search_string");
3199 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
3201 enc_search
= soup_uri_encode(search
, XT_RESERVED_CHARS
);
3202 newuri
= g_strdup_printf(search_string
, enc_search
);
3206 webkit_web_view_load_uri(t
->wv
, newuri
);
3214 check_and_set_cookie(const gchar
*uri
, struct tab
*t
)
3216 struct domain
*d
= NULL
;
3219 if (uri
== NULL
|| t
== NULL
)
3222 if ((d
= wl_find_uri(uri
, &c_wl
)) == NULL
)
3227 DNPRINTF(XT_D_COOKIE
, "check_and_set_cookie: %s %s\n",
3228 es
? "enable" : "disable", uri
);
3230 g_object_set(G_OBJECT(t
->settings
),
3231 "enable-html5-local-storage", es
, (char *)NULL
);
3232 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3236 check_and_set_js(const gchar
*uri
, struct tab
*t
)
3238 struct domain
*d
= NULL
;
3241 if (uri
== NULL
|| t
== NULL
)
3244 if ((d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3249 DNPRINTF(XT_D_JS
, "check_and_set_js: %s %s\n",
3250 es
? "enable" : "disable", uri
);
3252 g_object_set(G_OBJECT(t
->settings
),
3253 "enable-scripts", es
, (char *)NULL
);
3254 g_object_set(G_OBJECT(t
->settings
),
3255 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
3256 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3258 button_set_stockid(t
->js_toggle
,
3259 es
? GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
);
3263 check_and_set_pl(const gchar
*uri
, struct tab
*t
)
3265 struct domain
*d
= NULL
;
3268 if (uri
== NULL
|| t
== NULL
)
3271 if ((d
= wl_find_uri(uri
, &pl_wl
)) == NULL
)
3276 DNPRINTF(XT_D_JS
, "check_and_set_pl: %s %s\n",
3277 es
? "enable" : "disable", uri
);
3279 g_object_set(G_OBJECT(t
->settings
),
3280 "enable-plugins", es
, (char *)NULL
);
3281 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3285 color_address_bar(gpointer p
)
3288 struct tab
*tt
, *t
= p
;
3289 gchar
*col_str
= XT_COLOR_WHITE
;
3290 const gchar
*uri
, *u
= NULL
, *error_str
= NULL
;
3293 gdk_threads_enter();
3295 DNPRINTF(XT_D_URL
, "%s:\n", __func__
);
3297 /* make sure t still exists */
3300 TAILQ_FOREACH(tt
, &tabs
, entry
)
3306 if ((uri
= get_uri(t
)) == NULL
)
3311 gdk_threads_leave();
3314 col_str
= XT_COLOR_YELLOW
;
3315 switch (load_compare_cert(u
, &error_str
)) {
3317 col_str
= XT_COLOR_BLUE
;
3320 col_str
= XT_COLOR_GREEN
;
3322 case CERT_UNTRUSTED
:
3323 col_str
= XT_COLOR_YELLOW
;
3326 col_str
= XT_COLOR_RED
;
3331 gdk_threads_enter();
3333 /* make sure t isn't deleted */
3334 TAILQ_FOREACH(tt
, &tabs
, entry
)
3341 /* test to see if the user navigated away and canceled the thread */
3342 if (t
->thread
!= g_thread_self())
3344 if ((uri
= get_uri(t
)) == NULL
) {
3348 if (strcmp(uri
, u
)) {
3349 /* make sure we are still the same url */
3355 gdk_color_parse(col_str
, &color
);
3356 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3358 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3359 statusbar_modify_attr(t
, col_str
, XT_COLOR_BLACK
);
3361 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3363 if (error_str
&& error_str
[0] != '\0')
3364 show_oops(t
, "%s", error_str
);
3369 /* t is invalid at this point */
3371 g_free((gpointer
)u
);
3373 gdk_threads_leave();
3378 show_ca_status(struct tab
*t
, const char *uri
)
3381 gchar
*col_str
= XT_COLOR_WHITE
;
3383 DNPRINTF(XT_D_URL
, "show_ca_status: %d %s %s\n",
3384 ssl_strict_certs
, ssl_ca_file
, uri
);
3391 if (ssl_ca_file
== NULL
) {
3392 if (g_str_has_prefix(uri
, "http://"))
3394 if (g_str_has_prefix(uri
, "https://")) {
3395 col_str
= XT_COLOR_RED
;
3400 if (g_str_has_prefix(uri
, "http://") ||
3401 !g_str_has_prefix(uri
, "https://"))
3405 * It is not necessary to see if the thread is already running.
3406 * If the thread is in progress setting it to something else aborts it
3410 /* thread the coloring of the address bar */
3411 t
->thread
= g_thread_create((GThreadFunc
)color_address_bar
, t
, TRUE
, NULL
);
3413 color_address_bar(t
);
3419 gdk_color_parse(col_str
, &color
);
3420 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3422 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3423 statusbar_modify_attr(t
, col_str
, XT_COLOR_BLACK
);
3425 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3430 free_favicon(struct tab
*t
)
3432 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p req %p\n",
3433 __func__
, t
->icon_download
, t
->icon_request
);
3435 if (t
->icon_request
)
3436 g_object_unref(t
->icon_request
);
3437 if (t
->icon_dest_uri
)
3438 g_free(t
->icon_dest_uri
);
3440 t
->icon_request
= NULL
;
3441 t
->icon_dest_uri
= NULL
;
3445 xt_icon_from_name(struct tab
*t
, gchar
*name
)
3447 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->uri_entry
),
3448 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3450 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3451 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3453 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3454 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3458 xt_icon_from_pixbuf(struct tab
*t
, GdkPixbuf
*pb
)
3460 GdkPixbuf
*pb_scaled
;
3462 if (gdk_pixbuf_get_width(pb
) > 16 || gdk_pixbuf_get_height(pb
) > 16)
3463 pb_scaled
= gdk_pixbuf_scale_simple(pb
, 16, 16,
3464 GDK_INTERP_BILINEAR
);
3468 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
3469 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3471 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
3472 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3474 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3475 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3477 if (pb_scaled
!= pb
)
3478 g_object_unref(pb_scaled
);
3482 xt_icon_from_file(struct tab
*t
, char *file
)
3486 if (g_str_has_prefix(file
, "file://"))
3487 file
+= strlen("file://");
3489 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
3491 xt_icon_from_pixbuf(t
, pb
);
3494 xt_icon_from_name(t
, "text-html");
3498 is_valid_icon(char *file
)
3501 const char *mime_type
;
3505 gf
= g_file_new_for_path(file
);
3506 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
3508 mime_type
= g_file_info_get_content_type(fi
);
3509 valid
= g_strcmp0(mime_type
, "image/x-ico") == 0 ||
3510 g_strcmp0(mime_type
, "image/vnd.microsoft.icon") == 0 ||
3511 g_strcmp0(mime_type
, "image/png") == 0 ||
3512 g_strcmp0(mime_type
, "image/gif") == 0 ||
3513 g_strcmp0(mime_type
, "application/octet-stream") == 0;
3521 set_favicon_from_file(struct tab
*t
, char *file
)
3525 if (t
== NULL
|| file
== NULL
)
3528 if (g_str_has_prefix(file
, "file://"))
3529 file
+= strlen("file://");
3530 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading %s\n", __func__
, file
);
3532 if (!stat(file
, &sb
)) {
3533 if (sb
.st_size
== 0 || !is_valid_icon(file
)) {
3534 /* corrupt icon so trash it */
3535 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3538 /* no need to set icon to default here */
3542 xt_icon_from_file(t
, file
);
3546 favicon_download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
3549 WebKitDownloadStatus status
= webkit_download_get_status(download
);
3550 struct tab
*tt
= NULL
, *t
= NULL
;
3553 * find the webview instead of passing in the tab as it could have been
3554 * deleted from underneath us.
3556 TAILQ_FOREACH(tt
, &tabs
, entry
) {
3565 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d status %d\n",
3566 __func__
, t
->tab_id
, status
);
3569 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
3571 t
->icon_download
= NULL
;
3574 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
3577 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
3580 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
3582 DNPRINTF(XT_D_DOWNLOAD
, "%s: freeing favicon %d\n",
3583 __func__
, t
->tab_id
);
3584 t
->icon_download
= NULL
;
3587 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
3590 DNPRINTF(XT_D_DOWNLOAD
, "%s: setting icon to %s\n",
3591 __func__
, t
->icon_dest_uri
);
3592 set_favicon_from_file(t
, t
->icon_dest_uri
);
3593 /* these will be freed post callback */
3594 t
->icon_request
= NULL
;
3595 t
->icon_download
= NULL
;
3603 abort_favicon_download(struct tab
*t
)
3605 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p\n", __func__
, t
->icon_download
);
3607 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
3608 if (t
->icon_download
) {
3609 g_signal_handlers_disconnect_by_func(G_OBJECT(t
->icon_download
),
3610 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3611 webkit_download_cancel(t
->icon_download
);
3612 t
->icon_download
= NULL
;
3617 xt_icon_from_name(t
, "text-html");
3621 notify_icon_loaded_cb(WebKitWebView
*wv
, gchar
*uri
, struct tab
*t
)
3623 DNPRINTF(XT_D_DOWNLOAD
, "%s %s\n", __func__
, uri
);
3625 if (uri
== NULL
|| t
== NULL
)
3628 #if WEBKIT_CHECK_VERSION(1, 4, 0)
3629 /* take icon from WebKitIconDatabase */
3632 pb
= webkit_web_view_get_icon_pixbuf(wv
);
3634 xt_icon_from_pixbuf(t
, pb
);
3637 xt_icon_from_name(t
, "text-html");
3638 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
3639 /* download icon to cache dir */
3640 gchar
*name_hash
, file
[PATH_MAX
];
3643 if (t
->icon_request
) {
3644 DNPRINTF(XT_D_DOWNLOAD
, "%s: download in progress\n", __func__
);
3648 /* check to see if we got the icon in cache */
3649 name_hash
= g_compute_checksum_for_string(G_CHECKSUM_SHA256
, uri
, -1);
3650 snprintf(file
, sizeof file
, "%s/%s.ico", cache_dir
, name_hash
);
3653 if (!stat(file
, &sb
)) {
3654 if (sb
.st_size
> 0) {
3655 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading from cache %s\n",
3657 set_favicon_from_file(t
, file
);
3661 /* corrupt icon so trash it */
3662 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3667 /* create download for icon */
3668 t
->icon_request
= webkit_network_request_new(uri
);
3669 if (t
->icon_request
== NULL
) {
3670 DNPRINTF(XT_D_DOWNLOAD
, "%s: invalid uri %s\n",
3675 t
->icon_download
= webkit_download_new(t
->icon_request
);
3676 if (t
->icon_download
== NULL
)
3679 /* we have to free icon_dest_uri later */
3680 t
->icon_dest_uri
= g_strdup_printf("file://%s", file
);
3681 webkit_download_set_destination_uri(t
->icon_download
,
3684 if (webkit_download_get_status(t
->icon_download
) ==
3685 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
3686 g_object_unref(t
->icon_request
);
3687 g_free(t
->icon_dest_uri
);
3688 t
->icon_request
= NULL
;
3689 t
->icon_dest_uri
= NULL
;
3693 g_signal_connect(G_OBJECT(t
->icon_download
), "notify::status",
3694 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3696 webkit_download_start(t
->icon_download
);
3701 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3703 const gchar
*uri
= NULL
, *title
= NULL
;
3704 struct history
*h
, find
;
3708 DNPRINTF(XT_D_URL
, "notify_load_status_cb: %d %s\n",
3709 webkit_web_view_get_load_status(wview
),
3710 get_uri(t
) ? get_uri(t
) : "NOTHING");
3713 show_oops(NULL
, "notify_load_status_cb invalid parameters");
3717 switch (webkit_web_view_get_load_status(wview
)) {
3718 case WEBKIT_LOAD_PROVISIONAL
:
3720 abort_favicon_download(t
);
3721 #if GTK_CHECK_VERSION(2, 20, 0)
3722 gtk_widget_show(t
->spinner
);
3723 gtk_spinner_start(GTK_SPINNER(t
->spinner
));
3725 gtk_label_set_text(GTK_LABEL(t
->label
), "Loading");
3727 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), TRUE
);
3729 /* assume we are a new address */
3730 gdk_color_parse("white", &color
);
3731 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3732 statusbar_modify_attr(t
, "white", XT_COLOR_BLACK
);
3734 /* take focus if we are visible */
3740 /* kill color thread */
3745 case WEBKIT_LOAD_COMMITTED
:
3750 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
3756 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
3758 /* check if js white listing is enabled */
3759 if (enable_plugin_whitelist
)
3760 check_and_set_pl(uri
, t
);
3761 if (enable_cookie_whitelist
)
3762 check_and_set_cookie(uri
, t
);
3763 if (enable_js_whitelist
)
3764 check_and_set_js(uri
, t
);
3770 /* we know enough to autosave the session */
3771 if (session_autosave
) {
3776 show_ca_status(t
, uri
);
3779 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
3783 case WEBKIT_LOAD_FINISHED
:
3789 if (!strncmp(uri
, "http://", strlen("http://")) ||
3790 !strncmp(uri
, "https://", strlen("https://")) ||
3791 !strncmp(uri
, "file://", strlen("file://"))) {
3793 h
= RB_FIND(history_list
, &hl
, &find
);
3795 title
= get_title(t
, FALSE
);
3796 h
= g_malloc(sizeof *h
);
3797 h
->uri
= g_strdup(uri
);
3798 h
->title
= g_strdup(title
);
3799 RB_INSERT(history_list
, &hl
, h
);
3800 completion_add_uri(h
->uri
);
3801 update_history_tabs(NULL
);
3805 set_status(t
, (char *)uri
, XT_STATUS_URI
);
3806 #if WEBKIT_CHECK_VERSION(1, 1, 18)
3807 case WEBKIT_LOAD_FAILED
:
3810 #if GTK_CHECK_VERSION(2, 20, 0)
3811 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
3812 gtk_widget_hide(t
->spinner
);
3815 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
3820 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
), TRUE
);
3822 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
),
3823 can_go_back_for_real(t
));
3825 gtk_widget_set_sensitive(GTK_WIDGET(t
->forward
),
3826 can_go_forward_for_real(t
));
3830 notify_title_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3832 const gchar
*title
= NULL
, *win_title
= NULL
;
3834 title
= get_title(t
, FALSE
);
3835 win_title
= get_title(t
, TRUE
);
3836 gtk_label_set_text(GTK_LABEL(t
->label
), title
);
3837 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
), title
);
3838 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
3839 gtk_window_set_title(GTK_WINDOW(main_window
), win_title
);
3843 webview_load_finished_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
3845 run_script(t
, JS_HINTING
);
3846 if (autofocus_onload
&&
3847 t
->tab_id
== gtk_notebook_get_current_page(notebook
))
3848 run_script(t
, "hints.focusInput();");
3850 run_script(t
, "hints.clearFocus();");
3854 webview_progress_changed_cb(WebKitWebView
*wv
, int progress
, struct tab
*t
)
3856 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->uri_entry
),
3857 progress
== 100 ? 0 : (double)progress
/ 100);
3858 if (show_url
== 0) {
3859 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
3860 progress
== 100 ? 0 : (double)progress
/ 100);
3863 update_statusbar_position(NULL
, NULL
);
3867 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
3868 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
3869 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
3872 WebKitWebNavigationReason reason
;
3873 struct domain
*d
= NULL
;
3876 show_oops(NULL
, "webview_npd_cb invalid parameters");
3880 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
3882 webkit_network_request_get_uri(request
));
3884 uri
= (char *)webkit_network_request_get_uri(request
);
3886 /* if this is an xtp url, we don't load anything else */
3887 if (parse_xtp_url(t
, uri
))
3890 if ((t
->hints_on
&& t
->new_tab
) || t
->ctrl_click
) {
3892 create_new_tab(uri
, NULL
, ctrl_click_focus
, -1);
3893 webkit_web_policy_decision_ignore(pd
);
3894 return (TRUE
); /* we made the decission */
3898 * This is a little hairy but it comes down to this:
3899 * when we run in whitelist mode we have to assist the browser in
3900 * opening the URL that it would have opened in a new tab.
3902 reason
= webkit_web_navigation_action_get_reason(na
);
3903 if (reason
== WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED
) {
3904 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
3905 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1)
3906 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3908 webkit_web_policy_decision_use(pd
);
3909 return (TRUE
); /* we made the decision */
3916 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
3919 struct domain
*d
= NULL
;
3921 WebKitWebView
*webview
= NULL
;
3923 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
3924 webkit_web_view_get_uri(wv
));
3927 /* open in current tab */
3929 } else if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
3930 uri
= webkit_web_view_get_uri(wv
);
3931 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3934 tt
= create_new_tab(NULL
, NULL
, 1, -1);
3936 } else if (enable_scripts
== 1) {
3937 tt
= create_new_tab(NULL
, NULL
, 1, -1);
3945 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
3948 struct domain
*d
= NULL
;
3950 DNPRINTF(XT_D_NAV
, "webview_close_cb: %d\n", t
->tab_id
);
3952 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
3953 uri
= webkit_web_view_get_uri(wv
);
3954 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3958 } else if (enable_scripts
== 1)
3965 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
3967 /* we can not eat the event without throwing gtk off so defer it */
3969 /* catch middle click */
3970 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 2) {
3975 /* catch ctrl click */
3976 if (e
->type
== GDK_BUTTON_RELEASE
&&
3977 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
3982 return (XT_CB_PASSTHROUGH
);
3986 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
3988 struct mime_type
*m
;
3990 m
= find_mime_type(mime_type
);
3998 show_oops(t
, "can't fork mime handler");
4008 execlp(m
->mt_action
, m
->mt_action
,
4009 webkit_network_request_get_uri(request
), (void *)NULL
);
4018 get_mime_type(const char *file
)
4021 char *mime_type
= NULL
;
4025 if (g_str_has_prefix(file
, "file://"))
4026 file
+= strlen("file://");
4028 gf
= g_file_new_for_path(file
);
4029 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
4031 if ((m
= g_file_info_get_content_type(fi
)) != NULL
)
4032 mime_type
= g_strdup(m
);
4040 run_download_mimehandler(char *mime_type
, char *file
)
4042 struct mime_type
*m
;
4044 m
= find_mime_type(mime_type
);
4050 show_oops(NULL
, "can't fork download mime handler");
4060 if (g_str_has_prefix(file
, "file://"))
4061 file
+= strlen("file://");
4062 execlp(m
->mt_action
, m
->mt_action
, file
, (void *)NULL
);
4071 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
4074 WebKitDownloadStatus status
;
4075 const char *file
= NULL
;
4078 if (download
== NULL
)
4080 status
= webkit_download_get_status(download
);
4081 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
4084 file
= webkit_download_get_destination_uri(download
);
4087 mime
= get_mime_type(file
);
4091 run_download_mimehandler((char *)mime
, (char *)file
);
4096 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
4097 WebKitNetworkRequest
*request
, char *mime_type
,
4098 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
4101 show_oops(NULL
, "webview_mimetype_cb invalid parameters");
4105 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
4106 t
->tab_id
, mime_type
);
4108 if (run_mimehandler(t
, mime_type
, request
) == 0) {
4109 webkit_web_policy_decision_ignore(decision
);
4114 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
4115 webkit_web_policy_decision_download(decision
);
4123 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
4127 const gchar
*suggested_name
;
4128 gchar
*filename
= NULL
;
4130 struct download
*download_entry
;
4133 if (wk_download
== NULL
|| t
== NULL
) {
4134 show_oops(NULL
, "%s invalid parameters", __func__
);
4138 suggested_name
= webkit_download_get_suggested_filename(wk_download
);
4139 if (suggested_name
== NULL
)
4140 return (FALSE
); /* abort download */
4151 filename
= g_strdup_printf("%d%s", i
, suggested_name
);
4153 uri
= g_strdup_printf("file://%s/%s", download_dir
, i
?
4154 filename
: suggested_name
);
4156 } while (!stat(uri
+ strlen("file://"), &sb
));
4158 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d filename %s "
4159 "local %s\n", __func__
, t
->tab_id
, filename
, uri
);
4161 webkit_download_set_destination_uri(wk_download
, uri
);
4163 if (webkit_download_get_status(wk_download
) ==
4164 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
4165 show_oops(t
, "%s: download failed to start", __func__
);
4167 gtk_label_set_text(GTK_LABEL(t
->label
), "Download Failed");
4169 /* connect "download first" mime handler */
4170 g_signal_connect(G_OBJECT(wk_download
), "notify::status",
4171 G_CALLBACK(download_status_changed_cb
), NULL
);
4173 download_entry
= g_malloc(sizeof(struct download
));
4174 download_entry
->download
= wk_download
;
4175 download_entry
->tab
= t
;
4176 download_entry
->id
= next_download_id
++;
4177 RB_INSERT(download_list
, &downloads
, download_entry
);
4178 /* get from history */
4179 g_object_ref(wk_download
);
4180 gtk_label_set_text(GTK_LABEL(t
->label
), "Downloading");
4181 show_oops(t
, "Download of '%s' started...",
4182 basename((char *)webkit_download_get_destination_uri(wk_download
)));
4191 /* sync other download manager tabs */
4192 update_download_tabs(NULL
);
4195 * NOTE: never redirect/render the current tab before this
4196 * function returns. This will cause the download to never start.
4198 return (ret
); /* start download */
4202 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
4204 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
4207 show_oops(NULL
, "webview_hover_cb");
4212 set_status(t
, uri
, XT_STATUS_LINK
);
4215 set_status(t
, t
->status
, XT_STATUS_NOTHING
);
4220 mark(struct tab
*t
, struct karg
*arg
)
4226 if ((index
= marktoindex(mark
)) == -1)
4229 if (arg
->i
== XT_MARK_SET
)
4230 t
->mark
[index
] = gtk_adjustment_get_value(t
->adjust_v
);
4231 else if (arg
->i
== XT_MARK_GOTO
) {
4232 if (t
->mark
[index
] == XT_INVALID_MARK
) {
4233 show_oops(t
, "mark '%c' does not exist", mark
);
4236 /* XXX t->mark[index] can be bigger than the maximum if ajax or
4237 something changes the document size */
4238 gtk_adjustment_set_value(t
->adjust_v
, t
->mark
[index
]);
4245 marks_clear(struct tab
*t
)
4249 for (i
= 0; i
< LENGTH(t
->mark
); i
++)
4250 t
->mark
[i
] = XT_INVALID_MARK
;
4256 char file
[PATH_MAX
];
4257 char *line
= NULL
, *p
;
4262 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
4263 if ((f
= fopen(file
, "r+")) == NULL
) {
4264 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
4268 for (i
= 1; ; i
++) {
4269 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
4271 if (strlen(line
) == 0 || line
[0] == '#') {
4277 p
= strtok(line
, " \t");
4279 if (p
== NULL
|| strlen(p
) != 1 ||
4280 (index
= marktoindex(*p
)) == -1) {
4281 warnx("corrupt quickmarks file, line %d", i
);
4285 p
= strtok(NULL
, " \t");
4286 if (qmarks
[index
] != NULL
)
4287 g_free(qmarks
[index
]);
4288 qmarks
[index
] = g_strdup(p
);
4299 char file
[PATH_MAX
];
4303 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
4304 if ((f
= fopen(file
, "r+")) == NULL
) {
4305 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
4309 for (i
= 0; i
< XT_NOMARKS
; i
++)
4310 if (qmarks
[i
] != NULL
)
4311 fprintf(f
, "%c %s\n", indextomark(i
), qmarks
[i
]);
4319 qmark(struct tab
*t
, struct karg
*arg
)
4324 mark
= arg
->s
[strlen(arg
->s
)-1];
4325 index
= marktoindex(mark
);
4331 if (qmarks
[index
] != NULL
)
4332 g_free(qmarks
[index
]);
4334 qmarks_load(); /* sync if multiple instances */
4335 qmarks
[index
] = g_strdup(get_uri(t
));
4339 if (qmarks
[index
] != NULL
)
4340 load_uri(t
, qmarks
[index
]);
4342 show_oops(t
, "quickmark \"%c\" does not exist",
4348 if (qmarks
[index
] != NULL
)
4349 create_new_tab(qmarks
[index
], NULL
, 1, -1);
4351 show_oops(t
, "quickmark \"%c\" does not exist",
4362 go_up(struct tab
*t
, struct karg
*args
)
4368 levels
= atoi(args
->s
);
4372 uri
= g_strdup(webkit_web_view_get_uri(t
->wv
));
4373 if ((tmp
= strstr(uri
, XT_PROTO_DELIM
)) == NULL
)
4375 tmp
+= strlen(XT_PROTO_DELIM
);
4377 /* if an uri starts with a slash, leave it alone (for file:///) */
4384 p
= strrchr(tmp
, '/');
4398 gototab(struct tab
*t
, struct karg
*args
)
4401 struct karg arg
= {0, NULL
, -1};
4403 tab
= atoi(args
->s
);
4405 arg
.i
= XT_TAB_NEXT
;
4414 zoom_amount(struct tab
*t
, struct karg
*arg
)
4416 struct karg narg
= {0, NULL
, -1};
4418 narg
.i
= atoi(arg
->s
);
4419 resizetab(t
, &narg
);
4425 flip_colon(struct tab
*t
, struct karg
*arg
)
4427 struct karg narg
= {0, NULL
, -1};
4430 if (t
== NULL
|| arg
== NULL
)
4433 p
= strstr(arg
->s
, ":");
4445 /* buffer commands receive the regex that triggered them in arg.s */
4446 char bcmd
[XT_BUFCMD_SZ
];
4450 #define XT_PRE_NO (0)
4451 #define XT_PRE_YES (1)
4452 #define XT_PRE_MAYBE (2)
4454 int (*func
)(struct tab
*, struct karg
*);
4458 { "^[0-9]*gu$", XT_PRE_MAYBE
, "gu", go_up
, 0 },
4459 { "^gg$", XT_PRE_NO
, "gg", move
, XT_MOVE_TOP
},
4460 { "^gG$", XT_PRE_NO
, "gG", move
, XT_MOVE_BOTTOM
},
4461 { "^[0-9]+%$", XT_PRE_YES
, "%", move
, XT_MOVE_PERCENT
},
4462 { "^zz$", XT_PRE_NO
, "zz", move
, XT_MOVE_CENTER
},
4463 { "^gh$", XT_PRE_NO
, "gh", go_home
, 0 },
4464 { "^m[a-zA-Z0-9]$", XT_PRE_NO
, "m", mark
, XT_MARK_SET
},
4465 { "^['][a-zA-Z0-9]$", XT_PRE_NO
, "'", mark
, XT_MARK_GOTO
},
4466 { "^[0-9]+t$", XT_PRE_YES
, "t", gototab
, 0 },
4467 { "^M[a-zA-Z0-9]$", XT_PRE_NO
, "M", qmark
, XT_QMARK_SET
},
4468 { "^go[a-zA-Z0-9]$", XT_PRE_NO
, "go", qmark
, XT_QMARK_OPEN
},
4469 { "^gn[a-zA-Z0-9]$", XT_PRE_NO
, "gn", qmark
, XT_QMARK_TAB
},
4470 { "^ZR$", XT_PRE_NO
, "ZR", restart
, 0 },
4471 { "^ZZ$", XT_PRE_NO
, "ZZ", quit
, 0 },
4472 { "^zi$", XT_PRE_NO
, "zi", resizetab
, XT_ZOOM_IN
},
4473 { "^zo$", XT_PRE_NO
, "zo", resizetab
, XT_ZOOM_OUT
},
4474 { "^z0$", XT_PRE_NO
, "z0", resizetab
, XT_ZOOM_NORMAL
},
4475 { "^[0-9]+Z$", XT_PRE_YES
, "Z", zoom_amount
, 0 },
4476 { "^[0-9]+:$", XT_PRE_YES
, ":", flip_colon
, 0 },
4480 buffercmd_init(void)
4484 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
4485 if (regcomp(&buffercmds
[i
].cregex
, buffercmds
[i
].regex
,
4486 REG_EXTENDED
| REG_NOSUB
))
4487 startpage_add("invalid buffercmd regex %s",
4488 buffercmds
[i
].regex
);
4492 buffercmd_abort(struct tab
*t
)
4496 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_abort: clearing buffer\n");
4497 for (i
= 0; i
< LENGTH(bcmd
); i
++)
4500 cmd_prefix
= 0; /* clear prefix for non-buffer commands */
4501 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
4505 buffercmd_execute(struct tab
*t
, struct buffercmd
*cmd
)
4507 struct karg arg
= {0, NULL
, -1};
4510 arg
.s
= g_strdup(bcmd
);
4512 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_execute: buffer \"%s\" "
4513 "matches regex \"%s\", executing\n", bcmd
, cmd
->regex
);
4523 buffercmd_addkey(struct tab
*t
, guint keyval
)
4526 char s
[XT_BUFCMD_SZ
];
4528 if (keyval
== GDK_Escape
) {
4530 return (XT_CB_HANDLED
);
4533 /* key with modifier or non-ascii character */
4534 if (!isascii(keyval
))
4535 return (XT_CB_PASSTHROUGH
);
4537 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: adding key \"%c\" "
4538 "to buffer \"%s\"\n", keyval
, bcmd
);
4540 for (i
= 0; i
< LENGTH(bcmd
); i
++)
4541 if (bcmd
[i
] == '\0') {
4546 /* buffer full, ignore input */
4547 if (i
>= LENGTH(bcmd
) -1) {
4548 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: buffer full\n");
4550 return (XT_CB_HANDLED
);
4553 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
4555 /* find exact match */
4556 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
4557 if (regexec(&buffercmds
[i
].cregex
, bcmd
,
4558 (size_t) 0, NULL
, 0) == 0) {
4559 buffercmd_execute(t
, &buffercmds
[i
]);
4563 /* find non exact matches to see if we need to abort ot not */
4564 for (i
= 0, match
= 0; i
< LENGTH(buffercmds
); i
++) {
4565 DNPRINTF(XT_D_BUFFERCMD
, "trying: %s\n", bcmd
);
4568 if (buffercmds
[i
].precount
== XT_PRE_MAYBE
) {
4569 if (isdigit(bcmd
[0])) {
4570 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
4574 if (sscanf(bcmd
, "%s", s
) == 0)
4577 } else if (buffercmds
[i
].precount
== XT_PRE_YES
) {
4578 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
4581 if (sscanf(bcmd
, "%s", s
) == 0)
4584 if (c
== -1 && buffercmds
[i
].precount
)
4586 if (!strncmp(s
, buffercmds
[i
].cmd
, strlen(s
)))
4589 DNPRINTF(XT_D_BUFFERCMD
, "got[%d] %d <%s>: %d %s\n",
4590 i
, match
, buffercmds
[i
].cmd
, c
, s
);
4593 DNPRINTF(XT_D_BUFFERCMD
, "aborting: %s\n", bcmd
);
4598 return (XT_CB_HANDLED
);
4602 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
4604 struct key_binding
*k
;
4606 /* handle keybindings if buffercmd is empty.
4607 if not empty, allow commands like C-n */
4608 if (bcmd
[0] == '\0' || ((e
->state
& (CTRL
| MOD1
)) != 0))
4609 TAILQ_FOREACH(k
, &kbl
, entry
)
4610 if (e
->keyval
== k
->key
4611 && (entry
? k
->use_in_entry
: 1)) {
4613 if ((e
->state
& (CTRL
| MOD1
)) == 0)
4614 return (cmd_execute(t
, k
->cmd
));
4615 } else if ((e
->state
& k
->mask
) == k
->mask
) {
4616 return (cmd_execute(t
, k
->cmd
));
4620 if (!entry
&& ((e
->state
& (CTRL
| MOD1
)) == 0))
4621 return buffercmd_addkey(t
, e
->keyval
);
4623 return (XT_CB_PASSTHROUGH
);
4627 wv_keypress_after_cb(GtkWidget
*w
, GdkEventKey
*e
, struct tab
*t
)
4631 /* don't use w directly; use t->whatever instead */
4634 show_oops(NULL
, "wv_keypress_after_cb");
4635 return (XT_CB_PASSTHROUGH
);
4638 DNPRINTF(XT_D_KEY
, "wv_keypress_after_cb: keyval 0x%x mask 0x%x tab %d\n",
4639 e
->keyval
, e
->state
, t
->tab_id
);
4642 /* XXX make sure cmd entry is enabled */
4643 return (XT_CB_HANDLED
);
4646 snprintf(s
, sizeof s
, "%c", e
->keyval
);
4647 if (CLEAN(e
->state
) == 0 && isdigit(s
[0]))
4648 cmd_prefix
= 10 * cmd_prefix
+ atoi(s
);
4651 return (handle_keypress(t
, e
, 0));
4655 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
4659 /* Hide buffers, if they are visible, with escape. */
4660 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)) &&
4661 CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
4662 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
4664 return (XT_CB_HANDLED
);
4667 return (XT_CB_PASSTHROUGH
);
4671 hint_continue(struct tab
*t
)
4673 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
4675 const gchar
*errstr
= NULL
;
4679 if (!(c
[0] == '.' || c
[0] == ','))
4681 if (strlen(c
) == 1) {
4682 /* XXX should not happen */
4687 if (isdigit(c
[1])) {
4689 i
= strtonum(&c
[1], 1, 4096, &errstr
);
4691 show_oops(t
, "invalid numerical hint %s", &c
[1]);
4694 s
= g_strdup_printf("hints.updateHints(%lld);", i
);
4698 /* alphanumeric input */
4699 s
= g_strdup_printf("hints.createHints('%s', '%c');",
4700 &c
[1], c
[0] == '.' ? 'f' : 'F');
4711 search_continue(struct tab
*t
)
4713 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
4714 gboolean rv
= FALSE
;
4716 if (c
[0] == ':' || c
[0] == '.' || c
[0] == ',')
4718 if (strlen(c
) == 1) {
4719 webkit_web_view_unmark_text_matches(t
->wv
);
4724 t
->search_forward
= TRUE
;
4725 else if (c
[0] == '?')
4726 t
->search_forward
= FALSE
;
4736 search_cb(struct tab
*t
)
4738 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
4741 if (search_continue(t
) == FALSE
)
4745 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, t
->search_forward
,
4747 /* not found, mark red */
4748 gdk_color_parse(XT_COLOR_RED
, &color
);
4749 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
4750 /* unmark and remove selection */
4751 webkit_web_view_unmark_text_matches(t
->wv
);
4752 /* my kingdom for a way to unselect text in webview */
4754 /* found, highlight all */
4755 webkit_web_view_unmark_text_matches(t
->wv
);
4756 webkit_web_view_mark_text_matches(t
->wv
, &c
[1], FALSE
, 0);
4757 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
4758 gdk_color_parse(XT_COLOR_WHITE
, &color
);
4759 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
4767 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
4769 const gchar
*c
= gtk_entry_get_text(w
);
4772 show_oops(NULL
, "cmd_keyrelease_cb invalid parameters");
4773 return (XT_CB_PASSTHROUGH
);
4776 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
4777 e
->keyval
, e
->state
, t
->tab_id
);
4780 if (!(e
->keyval
== GDK_Tab
|| e
->keyval
== GDK_ISO_Left_Tab
)) {
4781 if (hint_continue(t
) == FALSE
)
4786 if (search_continue(t
) == FALSE
)
4789 /* if search length is > 4 then no longer play timeout games */
4790 if (strlen(c
) > 4) {
4792 g_source_remove(t
->search_id
);
4799 /* reestablish a new timer if the user types fast */
4801 g_source_remove(t
->search_id
);
4802 t
->search_id
= g_timeout_add(250, (GSourceFunc
)search_cb
, (gpointer
)t
);
4805 return (XT_CB_PASSTHROUGH
);
4809 match_uri(const gchar
*uri
, const gchar
*key
) {
4812 gboolean match
= FALSE
;
4816 if (!strncmp(key
, uri
, len
))
4819 voffset
= strstr(uri
, "/") + 2;
4820 if (!strncmp(key
, voffset
, len
))
4822 else if (g_str_has_prefix(voffset
, "www.")) {
4823 voffset
= voffset
+ strlen("www.");
4824 if (!strncmp(key
, voffset
, len
))
4833 match_session(const gchar
*name
, const gchar
*key
) {
4836 sub
= strcasestr(name
, key
);
4842 cmd_getlist(int id
, char *key
)
4849 if (cmds
[id
].type
& XT_URLARG
) {
4850 RB_FOREACH_REVERSE(h
, history_list
, &hl
)
4851 if (match_uri(h
->uri
, key
)) {
4852 cmd_status
.list
[c
] = (char *)h
->uri
;
4858 } else if (cmds
[id
].type
& XT_SESSARG
) {
4859 TAILQ_FOREACH(s
, &sessions
, entry
)
4860 if (match_session(s
->name
, key
)) {
4861 cmd_status
.list
[c
] = (char *)s
->name
;
4867 } else if (cmds
[id
].type
& XT_SETARG
) {
4868 for (i
= 0; i
< get_settings_size(); i
++)
4869 if(!strncmp(key
, get_setting_name(i
),
4871 cmd_status
.list
[c
++] = get_setting_name(i
);
4877 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
4879 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
4880 if (cmds
[i
].level
< dep
)
4882 if (cmds
[i
].level
== dep
&& !strncmp(key
, cmds
[i
].cmd
,
4883 strlen(key
)) && !isdigit(cmds
[i
].cmd
[0]))
4884 cmd_status
.list
[c
++] = cmds
[i
].cmd
;
4892 cmd_getnext(int dir
)
4894 cmd_status
.index
+= dir
;
4896 if (cmd_status
.index
< 0)
4897 cmd_status
.index
= cmd_status
.len
- 1;
4898 else if (cmd_status
.index
>= cmd_status
.len
)
4899 cmd_status
.index
= 0;
4901 return cmd_status
.list
[cmd_status
.index
];
4905 cmd_tokenize(char *s
, char *tokens
[])
4908 char *tok
, *last
= NULL
;
4909 size_t len
= strlen(s
);
4912 blank
= len
== 0 || (len
> 0 && s
[len
- 1] == ' ');
4913 for (tok
= strtok_r(s
, " ", &last
); tok
&& i
< 3;
4914 tok
= strtok_r(NULL
, " ", &last
), i
++)
4924 cmd_complete(struct tab
*t
, char *str
, int dir
)
4926 GtkEntry
*w
= GTK_ENTRY(t
->cmd
);
4927 int i
, j
, levels
, c
= 0, dep
= 0, parent
= -1;
4929 char *tok
, *match
, *s
= g_strdup(str
);
4931 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
4934 DNPRINTF(XT_D_CMD
, "%s: complete %s\n", __func__
, str
);
4937 for (i
= 0; isdigit(s
[i
]); i
++)
4940 for (; isspace(s
[i
]); i
++)
4945 levels
= cmd_tokenize(s
, tokens
);
4947 for (i
= 0; i
< levels
- 1; i
++) {
4950 for (j
= c
; j
< LENGTH(cmds
); j
++) {
4951 if (cmds
[j
].level
< dep
)
4953 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
,
4957 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
4964 if (matchcount
== 1) {
4965 strlcat(res
, tok
, sizeof res
);
4966 strlcat(res
, " ", sizeof res
);
4976 if (cmd_status
.index
== -1)
4977 cmd_getlist(parent
, tokens
[i
]);
4979 if (cmd_status
.len
> 0) {
4980 match
= cmd_getnext(dir
);
4981 strlcat(res
, match
, sizeof res
);
4982 gtk_entry_set_text(w
, res
);
4983 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
4990 cmd_execute(struct tab
*t
, char *str
)
4992 struct cmd
*cmd
= NULL
;
4993 char *tok
, *last
= NULL
, *s
= g_strdup(str
), *sc
;
4995 int j
, len
, c
= 0, dep
= 0, matchcount
= 0;
4996 int prefix
= -1, rv
= XT_CB_PASSTHROUGH
;
4997 struct karg arg
= {0, NULL
, -1};
5002 for (j
= 0; j
<3 && isdigit(s
[j
]); j
++)
5008 while (isspace(s
[0]))
5011 if (strlen(s
) > 0 && strlen(prefixstr
) > 0)
5012 prefix
= atoi(prefixstr
);
5016 for (tok
= strtok_r(s
, " ", &last
); tok
;
5017 tok
= strtok_r(NULL
, " ", &last
)) {
5019 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5020 if (cmds
[j
].level
< dep
)
5022 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1 :
5024 if (cmds
[j
].level
== dep
&&
5025 !strncmp(tok
, cmds
[j
].cmd
, len
)) {
5029 if (len
== strlen(cmds
[j
].cmd
)) {
5035 if (matchcount
== 1) {
5040 show_oops(t
, "Invalid command: %s", str
);
5046 show_oops(t
, "Empty command");
5052 arg
.precount
= prefix
;
5053 else if (cmd_prefix
> 0)
5054 arg
.precount
= cmd_prefix
;
5056 if (j
> 0 && !(cmd
->type
& XT_PREFIX
) && arg
.precount
> -1) {
5057 show_oops(t
, "No prefix allowed: %s", str
);
5061 arg
.s
= last
? g_strdup(last
) : g_strdup("");
5062 if (cmd
->type
& XT_INTARG
&& last
&& strlen(last
) > 0) {
5063 if (arg
.s
== NULL
) {
5064 show_oops(t
, "Invalid command");
5067 arg
.precount
= atoi(arg
.s
);
5068 if (arg
.precount
<= 0) {
5069 if (arg
.s
[0] == '0')
5070 show_oops(t
, "Zero count");
5072 show_oops(t
, "Trailing characters");
5077 DNPRINTF(XT_D_CMD
, "%s: prefix %d arg %s\n",
5078 __func__
, arg
.precount
, arg
.s
);
5094 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5097 show_oops(NULL
, "entry_key_cb invalid parameters");
5098 return (XT_CB_PASSTHROUGH
);
5101 DNPRINTF(XT_D_CMD
, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
5102 e
->keyval
, e
->state
, t
->tab_id
);
5106 if (e
->keyval
== GDK_Escape
) {
5107 /* don't use focus_webview(t) because we want to type :cmds */
5108 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5111 return (handle_keypress(t
, e
, 1));
5114 struct command_entry
*
5115 history_prev(struct command_list
*l
, struct command_entry
*at
)
5118 at
= TAILQ_LAST(l
, command_list
);
5120 at
= TAILQ_PREV(at
, command_list
, entry
);
5122 at
= TAILQ_LAST(l
, command_list
);
5128 struct command_entry
*
5129 history_next(struct command_list
*l
, struct command_entry
*at
)
5132 at
= TAILQ_FIRST(l
);
5134 at
= TAILQ_NEXT(at
, entry
);
5136 at
= TAILQ_FIRST(l
);
5143 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5145 int rv
= XT_CB_HANDLED
;
5146 const gchar
*c
= gtk_entry_get_text(w
);
5150 show_oops(NULL
, "cmd_keypress_cb parameters");
5151 return (XT_CB_PASSTHROUGH
);
5154 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
5155 e
->keyval
, e
->state
, t
->tab_id
);
5159 e
->keyval
= GDK_Escape
;
5160 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5161 c
[0] == '.' || c
[0] == ','))
5162 e
->keyval
= GDK_Escape
;
5164 if (e
->keyval
!= GDK_Tab
&& e
->keyval
!= GDK_Shift_L
&&
5165 e
->keyval
!= GDK_ISO_Left_Tab
)
5166 cmd_status
.index
= -1;
5168 switch (e
->keyval
) {
5171 cmd_complete(t
, (char *)&c
[1], 1);
5172 else if (c
[0] == '.' || c
[0] == ',')
5173 run_script(t
, "hints.focusNextHint();");
5175 case GDK_ISO_Left_Tab
:
5177 cmd_complete(t
, (char *)&c
[1], -1);
5178 else if (c
[0] == '.' || c
[0] == ',')
5179 run_script(t
, "hints.focusPreviousHint();");
5183 if ((search_at
= history_next(&shl
, search_at
))) {
5184 search_at
->line
[0] = c
[0];
5185 gtk_entry_set_text(w
, search_at
->line
);
5186 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5188 } else if (c
[0] == '/') {
5189 if ((search_at
= history_prev(&chl
, search_at
))) {
5190 search_at
->line
[0] = c
[0];
5191 gtk_entry_set_text(w
, search_at
->line
);
5192 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5194 } if (c
[0] == ':') {
5195 if ((history_at
= history_prev(&chl
, history_at
))) {
5196 history_at
->line
[0] = c
[0];
5197 gtk_entry_set_text(w
, history_at
->line
);
5198 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5204 if ((search_at
= history_next(&shl
, search_at
))) {
5205 search_at
->line
[0] = c
[0];
5206 gtk_entry_set_text(w
, search_at
->line
);
5207 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5209 } else if (c
[0] == '?') {
5210 if ((search_at
= history_prev(&chl
, search_at
))) {
5211 search_at
->line
[0] = c
[0];
5212 gtk_entry_set_text(w
, search_at
->line
);
5213 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5215 } if (c
[0] == ':') {
5216 if ((history_at
= history_next(&chl
, history_at
))) {
5217 history_at
->line
[0] = c
[0];
5218 gtk_entry_set_text(w
, history_at
->line
);
5219 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5224 if (!(!strcmp(c
, ":") || !strcmp(c
, "/") || !strcmp(c
, "?") ||
5225 !strcmp(c
, ".") || !strcmp(c
, ","))) {
5226 /* see if we are doing hinting and reset it */
5227 if (c
[0] == '.' || c
[0] == ',') {
5228 /* recreate hints */
5229 s
= g_strdup_printf("hints.createHints('', "
5230 "'%c');", c
[0] == '.' ? 'f' : 'F');
5243 if (c
!= NULL
&& (c
[0] == '/' || c
[0] == '?'))
5244 webkit_web_view_unmark_text_matches(t
->wv
);
5246 /* no need to cancel hints */
5250 rv
= XT_CB_PASSTHROUGH
;
5256 wv_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
5258 DNPRINTF(XT_D_CMD
, "wv_popup_cb: tab %d\n", t
->tab_id
);
5262 cmd_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
5264 /* popup menu enabled */
5269 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
5272 show_oops(NULL
, "cmd_focusout_cb invalid parameters");
5273 return (XT_CB_PASSTHROUGH
);
5276 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d popup %d\n",
5277 t
->tab_id
, t
->popup
);
5279 /* if popup is enabled don't lose focus */
5282 return (XT_CB_PASSTHROUGH
);
5289 if (show_url
== 0 || t
->focus_wv
)
5292 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
5294 return (XT_CB_PASSTHROUGH
);
5298 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
5301 const gchar
*c
= gtk_entry_get_text(entry
);
5304 show_oops(NULL
, "cmd_activate_cb invalid parameters");
5308 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
5313 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5314 c
[0] == '.' || c
[0] == ','))
5320 if (c
[0] == '/' || c
[0] == '?') {
5321 /* see if there is a timer pending */
5323 g_source_remove(t
->search_id
);
5328 if (t
->search_text
) {
5329 g_free(t
->search_text
);
5330 t
->search_text
= NULL
;
5333 t
->search_text
= g_strdup(s
);
5335 g_free(global_search
);
5336 global_search
= g_strdup(s
);
5337 t
->search_forward
= c
[0] == '/';
5339 history_add(&shl
, search_file
, s
, &search_history_count
);
5340 } else if (c
[0] == '.' || c
[0] == ',') {
5341 run_script(t
, "hints.fire();");
5342 /* XXX history for link following? */
5343 } else if (c
[0] == ':') {
5344 history_add(&chl
, command_file
, s
, &cmd_history_count
);
5345 /* can't call hide_cmd after cmd_execute */
5356 backward_cb(GtkWidget
*w
, struct tab
*t
)
5361 show_oops(NULL
, "backward_cb invalid parameters");
5365 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
5372 forward_cb(GtkWidget
*w
, struct tab
*t
)
5377 show_oops(NULL
, "forward_cb invalid parameters");
5381 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
5383 a
.i
= XT_NAV_FORWARD
;
5388 home_cb(GtkWidget
*w
, struct tab
*t
)
5391 show_oops(NULL
, "home_cb invalid parameters");
5395 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
5401 stop_cb(GtkWidget
*w
, struct tab
*t
)
5403 WebKitWebFrame
*frame
;
5406 show_oops(NULL
, "stop_cb invalid parameters");
5410 DNPRINTF(XT_D_NAV
, "stop_cb: tab %d\n", t
->tab_id
);
5412 frame
= webkit_web_view_get_main_frame(t
->wv
);
5413 if (frame
== NULL
) {
5414 show_oops(t
, "stop_cb: no frame");
5418 webkit_web_frame_stop_loading(frame
);
5419 abort_favicon_download(t
);
5423 setup_webkit(struct tab
*t
)
5425 if (is_g_object_setting(G_OBJECT(t
->settings
), "enable-dns-prefetching"))
5426 g_object_set(G_OBJECT(t
->settings
), "enable-dns-prefetching",
5427 FALSE
, (char *)NULL
);
5429 warnx("webkit does not have \"enable-dns-prefetching\" property");
5430 g_object_set(G_OBJECT(t
->settings
),
5431 "user-agent", t
->user_agent
, (char *)NULL
);
5432 g_object_set(G_OBJECT(t
->settings
),
5433 "enable-scripts", enable_scripts
, (char *)NULL
);
5434 g_object_set(G_OBJECT(t
->settings
),
5435 "enable-plugins", enable_plugins
, (char *)NULL
);
5436 g_object_set(G_OBJECT(t
->settings
),
5437 "javascript-can-open-windows-automatically", enable_scripts
,
5439 g_object_set(G_OBJECT(t
->settings
),
5440 "enable-html5-database", FALSE
, (char *)NULL
);
5441 g_object_set(G_OBJECT(t
->settings
),
5442 "enable-html5-local-storage", enable_localstorage
, (char *)NULL
);
5443 g_object_set(G_OBJECT(t
->settings
),
5444 "enable_spell_checking", enable_spell_checking
, (char *)NULL
);
5445 g_object_set(G_OBJECT(t
->settings
),
5446 "spell_checking_languages", spell_check_languages
, (char *)NULL
);
5447 g_object_set(G_OBJECT(t
->settings
),
5448 "enable-developer-extras", TRUE
, (char *)NULL
);
5449 g_object_set(G_OBJECT(t
->wv
),
5450 "full-content-zoom", TRUE
, (char *)NULL
);
5452 webkit_web_view_set_settings(t
->wv
, t
->settings
);
5456 update_statusbar_position(GtkAdjustment
* adjustment
, gpointer data
)
5458 struct tab
*ti
, *t
= NULL
;
5459 gdouble view_size
, value
, max
;
5462 TAILQ_FOREACH(ti
, &tabs
, entry
)
5463 if (ti
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
5471 if (adjustment
== NULL
)
5472 adjustment
= gtk_scrolled_window_get_vadjustment(
5473 GTK_SCROLLED_WINDOW(t
->browser_win
));
5475 view_size
= gtk_adjustment_get_page_size(adjustment
);
5476 value
= gtk_adjustment_get_value(adjustment
);
5477 max
= gtk_adjustment_get_upper(adjustment
) - view_size
;
5480 position
= g_strdup("All");
5481 else if (value
== max
)
5482 position
= g_strdup("Bot");
5483 else if (value
== 0)
5484 position
= g_strdup("Top");
5486 position
= g_strdup_printf("%d%%", (int) ((value
/ max
) * 100));
5488 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.position
), position
);
5495 create_window(const gchar
*name
)
5499 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
5500 gtk_window_set_default_size(GTK_WINDOW(w
), window_width
, window_height
);
5501 gtk_widget_set_name(w
, name
);
5502 gtk_window_set_wmclass(GTK_WINDOW(w
), name
, "XXXTerm");
5508 create_browser(struct tab
*t
)
5512 GtkAdjustment
*adjustment
;
5515 show_oops(NULL
, "create_browser invalid parameters");
5519 t
->sb_h
= GTK_SCROLLBAR(gtk_hscrollbar_new(NULL
));
5520 t
->sb_v
= GTK_SCROLLBAR(gtk_vscrollbar_new(NULL
));
5521 t
->adjust_h
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_h
));
5522 t
->adjust_v
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_v
));
5524 w
= gtk_scrolled_window_new(t
->adjust_h
, t
->adjust_v
);
5525 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
5526 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
5528 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
5529 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
5532 t
->settings
= webkit_web_settings_new();
5534 g_object_set(t
->settings
, "default-encoding", encoding
, (char *)NULL
);
5536 if (user_agent
== NULL
) {
5537 g_object_get(G_OBJECT(t
->settings
), "user-agent", &strval
,
5539 t
->user_agent
= g_strdup_printf("%s %s+", strval
, version
);
5542 t
->user_agent
= g_strdup(user_agent
);
5544 t
->stylesheet
= g_strdup_printf("file://%s/style.css", resource_dir
);
5547 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w
));
5548 g_signal_connect(G_OBJECT(adjustment
), "value-changed",
5549 G_CALLBACK(update_statusbar_position
), NULL
);
5558 create_kiosk_toolbar(struct tab
*t
)
5560 GtkWidget
*toolbar
= NULL
, *b
;
5562 b
= gtk_hbox_new(FALSE
, 0);
5564 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
5566 /* backward button */
5567 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
5568 gtk_widget_set_sensitive(t
->backward
, FALSE
);
5569 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
5570 G_CALLBACK(backward_cb
), t
);
5571 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, TRUE
, TRUE
, 0);
5573 /* forward button */
5574 t
->forward
= create_button("Forward", GTK_STOCK_GO_FORWARD
, 0);
5575 gtk_widget_set_sensitive(t
->forward
, FALSE
);
5576 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
5577 G_CALLBACK(forward_cb
), t
);
5578 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, TRUE
, TRUE
, 0);
5581 t
->gohome
= create_button("Home", GTK_STOCK_HOME
, 0);
5582 gtk_widget_set_sensitive(t
->gohome
, true);
5583 g_signal_connect(G_OBJECT(t
->gohome
), "clicked",
5584 G_CALLBACK(home_cb
), t
);
5585 gtk_box_pack_start(GTK_BOX(b
), t
->gohome
, TRUE
, TRUE
, 0);
5587 /* create widgets but don't use them */
5588 t
->uri_entry
= gtk_entry_new();
5589 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
5590 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
5591 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
5597 create_toolbar(struct tab
*t
)
5599 GtkWidget
*toolbar
= NULL
, *b
, *eb1
;
5601 b
= gtk_hbox_new(FALSE
, 0);
5603 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
5605 /* backward button */
5606 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
5607 gtk_widget_set_sensitive(t
->backward
, FALSE
);
5608 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
5609 G_CALLBACK(backward_cb
), t
);
5610 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, FALSE
, FALSE
, 0);
5612 /* forward button */
5613 t
->forward
= create_button("Forward",GTK_STOCK_GO_FORWARD
, 0);
5614 gtk_widget_set_sensitive(t
->forward
, FALSE
);
5615 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
5616 G_CALLBACK(forward_cb
), t
);
5617 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, FALSE
,
5621 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
5622 gtk_widget_set_sensitive(t
->stop
, FALSE
);
5623 g_signal_connect(G_OBJECT(t
->stop
), "clicked",
5624 G_CALLBACK(stop_cb
), t
);
5625 gtk_box_pack_start(GTK_BOX(b
), t
->stop
, FALSE
,
5629 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
5630 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
5631 gtk_widget_set_sensitive(t
->js_toggle
, TRUE
);
5632 g_signal_connect(G_OBJECT(t
->js_toggle
), "clicked",
5633 G_CALLBACK(js_toggle_cb
), t
);
5634 gtk_box_pack_start(GTK_BOX(b
), t
->js_toggle
, FALSE
, FALSE
, 0);
5636 t
->uri_entry
= gtk_entry_new();
5637 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
5638 G_CALLBACK(activate_uri_entry_cb
), t
);
5639 g_signal_connect(G_OBJECT(t
->uri_entry
), "key-press-event",
5640 G_CALLBACK(entry_key_cb
), t
);
5642 eb1
= gtk_hbox_new(FALSE
, 0);
5643 gtk_container_set_border_width(GTK_CONTAINER(eb1
), 1);
5644 gtk_box_pack_start(GTK_BOX(eb1
), t
->uri_entry
, TRUE
, TRUE
, 0);
5645 gtk_box_pack_start(GTK_BOX(b
), eb1
, TRUE
, TRUE
, 0);
5648 if (search_string
) {
5650 t
->search_entry
= gtk_entry_new();
5651 gtk_entry_set_width_chars(GTK_ENTRY(t
->search_entry
), 30);
5652 g_signal_connect(G_OBJECT(t
->search_entry
), "activate",
5653 G_CALLBACK(activate_search_entry_cb
), t
);
5654 g_signal_connect(G_OBJECT(t
->search_entry
), "key-press-event",
5655 G_CALLBACK(entry_key_cb
), t
);
5656 gtk_widget_set_size_request(t
->search_entry
, -1, -1);
5657 eb2
= gtk_hbox_new(FALSE
, 0);
5658 gtk_container_set_border_width(GTK_CONTAINER(eb2
), 1);
5659 gtk_box_pack_start(GTK_BOX(eb2
), t
->search_entry
, TRUE
, TRUE
,
5661 gtk_box_pack_start(GTK_BOX(b
), eb2
, FALSE
, FALSE
, 0);
5668 create_buffers(struct tab
*t
)
5670 GtkCellRenderer
*renderer
;
5673 view
= gtk_tree_view_new();
5675 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view
), FALSE
);
5677 renderer
= gtk_cell_renderer_text_new();
5678 gtk_tree_view_insert_column_with_attributes
5679 (GTK_TREE_VIEW(view
), -1, "Id", renderer
, "text", COL_ID
, (char *)NULL
);
5681 renderer
= gtk_cell_renderer_text_new();
5682 gtk_tree_view_insert_column_with_attributes
5683 (GTK_TREE_VIEW(view
), -1, "Title", renderer
, "text", COL_TITLE
,
5686 gtk_tree_view_set_model
5687 (GTK_TREE_VIEW(view
), GTK_TREE_MODEL(buffers_store
));
5693 row_activated_cb(GtkTreeView
*view
, GtkTreePath
*path
,
5694 GtkTreeViewColumn
*col
, struct tab
*t
)
5699 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5701 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
,
5704 (GTK_TREE_MODEL(buffers_store
), &iter
, COL_ID
, &id
, -1);
5705 set_current_tab(id
- 1);
5711 /* after tab reordering/creation/removal */
5718 TAILQ_FOREACH(t
, &tabs
, entry
) {
5719 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
5720 if (t
->tab_id
> maxid
)
5723 gtk_widget_show(t
->tab_elems
.sep
);
5726 TAILQ_FOREACH(t
, &tabs
, entry
) {
5727 if (t
->tab_id
== maxid
) {
5728 gtk_widget_hide(t
->tab_elems
.sep
);
5734 /* after active tab change */
5736 recolor_compact_tabs(void)
5742 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
5743 TAILQ_FOREACH(t
, &tabs
, entry
)
5744 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
,
5747 curid
= gtk_notebook_get_current_page(notebook
);
5748 TAILQ_FOREACH(t
, &tabs
, entry
)
5749 if (t
->tab_id
== curid
) {
5750 gdk_color_parse(XT_COLOR_CT_ACTIVE
, &color
);
5751 gtk_widget_modify_fg(t
->tab_elems
.label
,
5752 GTK_STATE_NORMAL
, &color
);
5758 set_current_tab(int page_num
)
5760 buffercmd_abort(get_current_tab());
5761 gtk_notebook_set_current_page(notebook
, page_num
);
5762 recolor_compact_tabs();
5766 undo_close_tab_save(struct tab
*t
)
5770 struct undo
*u1
, *u2
;
5772 WebKitWebHistoryItem
*item
;
5774 if ((uri
= get_uri(t
)) == NULL
)
5777 u1
= g_malloc0(sizeof(struct undo
));
5778 u1
->uri
= g_strdup(uri
);
5780 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
5782 m
= webkit_web_back_forward_list_get_forward_length(t
->bfl
);
5783 n
= webkit_web_back_forward_list_get_back_length(t
->bfl
);
5786 /* forward history */
5787 items
= webkit_web_back_forward_list_get_forward_list_with_limit(t
->bfl
, m
);
5791 u1
->history
= g_list_prepend(u1
->history
,
5792 webkit_web_history_item_copy(item
));
5793 items
= g_list_next(items
);
5798 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
5799 u1
->history
= g_list_prepend(u1
->history
,
5800 webkit_web_history_item_copy(item
));
5804 items
= webkit_web_back_forward_list_get_back_list_with_limit(t
->bfl
, n
);
5808 u1
->history
= g_list_prepend(u1
->history
,
5809 webkit_web_history_item_copy(item
));
5810 items
= g_list_next(items
);
5813 TAILQ_INSERT_HEAD(&undos
, u1
, entry
);
5815 if (undo_count
> XT_MAX_UNDO_CLOSE_TAB
) {
5816 u2
= TAILQ_LAST(&undos
, undo_tailq
);
5817 TAILQ_REMOVE(&undos
, u2
, entry
);
5819 g_list_free(u2
->history
);
5828 delete_tab(struct tab
*t
)
5832 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
5838 * no need to join thread here because it won't access t on completion
5841 TAILQ_REMOVE(&tabs
, t
, entry
);
5844 /* Halt all webkit activity. */
5845 abort_favicon_download(t
);
5846 webkit_web_view_stop_loading(t
->wv
);
5848 /* Save the tab, so we can undo the close. */
5849 undo_close_tab_save(t
);
5853 g_source_remove(t
->search_id
);
5856 bzero(&a
, sizeof a
);
5858 inspector_cmd(t
, &a
);
5860 if (browser_mode
== XT_BM_KIOSK
) {
5861 gtk_widget_destroy(t
->uri_entry
);
5862 gtk_widget_destroy(t
->stop
);
5863 gtk_widget_destroy(t
->js_toggle
);
5866 gtk_widget_destroy(t
->tab_elems
.eventbox
);
5867 gtk_widget_destroy(t
->vbox
);
5869 g_free(t
->user_agent
);
5870 g_free(t
->stylesheet
);
5875 if (TAILQ_EMPTY(&tabs
)) {
5876 if (browser_mode
== XT_BM_KIOSK
)
5877 create_new_tab(home
, NULL
, 1, -1);
5879 create_new_tab(NULL
, NULL
, 1, -1);
5882 /* recreate session */
5883 if (session_autosave
) {
5884 bzero(&a
, sizeof a
);
5886 save_tabs(NULL
, &a
);
5890 recolor_compact_tabs();
5894 update_statusbar_zoom(struct tab
*t
)
5897 char s
[16] = { '\0' };
5899 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
5900 if ((zoom
<= 0.99 || zoom
>= 1.01))
5901 snprintf(s
, sizeof s
, "%d%%", (int)(zoom
* 100));
5902 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.zoom
), s
);
5906 setzoom_webkit(struct tab
*t
, int adjust
)
5908 #define XT_ZOOMPERCENT 0.04
5913 show_oops(NULL
, "setzoom_webkit invalid parameters");
5917 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
5918 if (adjust
== XT_ZOOM_IN
)
5919 zoom
+= XT_ZOOMPERCENT
;
5920 else if (adjust
== XT_ZOOM_OUT
)
5921 zoom
-= XT_ZOOMPERCENT
;
5922 else if (adjust
> 0)
5923 zoom
= default_zoom_level
+ adjust
/ 100.0 - 1.0;
5925 show_oops(t
, "setzoom_webkit invalid zoom value");
5929 if (zoom
< XT_ZOOMPERCENT
)
5930 zoom
= XT_ZOOMPERCENT
;
5931 g_object_set(G_OBJECT(t
->wv
), "zoom-level", zoom
, (char *)NULL
);
5932 update_statusbar_zoom(t
);
5936 tab_clicked_cb(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
5938 struct tab
*t
= (struct tab
*) data
;
5940 DNPRINTF(XT_D_TAB
, "tab_clicked_cb: tab: %d\n", t
->tab_id
);
5942 switch (event
->button
) {
5944 set_current_tab(t
->tab_id
);
5955 append_tab(struct tab
*t
)
5960 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
5961 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
5965 create_sbe(int width
)
5969 sbe
= gtk_entry_new();
5970 gtk_entry_set_inner_border(GTK_ENTRY(sbe
), NULL
);
5971 gtk_entry_set_has_frame(GTK_ENTRY(sbe
), FALSE
);
5972 gtk_widget_set_can_focus(GTK_WIDGET(sbe
), FALSE
);
5973 gtk_widget_modify_font(GTK_WIDGET(sbe
), statusbar_font
);
5974 gtk_entry_set_alignment(GTK_ENTRY(sbe
), 1.0);
5975 gtk_widget_set_size_request(sbe
, width
, -1);
5981 create_new_tab(char *title
, struct undo
*u
, int focus
, int position
)
5986 WebKitWebHistoryItem
*item
;
5990 int sbe_p
= 0, sbe_b
= 0,
5993 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
5995 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
5996 DNPRINTF(XT_D_TAB
, "create_new_tab: new tab rejected\n");
6000 t
= g_malloc0(sizeof *t
);
6002 if (title
== NULL
) {
6003 title
= "(untitled)";
6007 t
->vbox
= gtk_vbox_new(FALSE
, 0);
6009 /* label + button for tab */
6010 b
= gtk_hbox_new(FALSE
, 0);
6013 #if GTK_CHECK_VERSION(2, 20, 0)
6014 t
->spinner
= gtk_spinner_new();
6016 t
->label
= gtk_label_new(title
);
6017 bb
= create_button("Close", GTK_STOCK_CLOSE
, 1);
6018 gtk_widget_set_size_request(t
->label
, 100, 0);
6019 gtk_label_set_max_width_chars(GTK_LABEL(t
->label
), 20);
6020 gtk_label_set_ellipsize(GTK_LABEL(t
->label
), PANGO_ELLIPSIZE_END
);
6021 gtk_widget_set_size_request(b
, 130, 0);
6023 gtk_box_pack_start(GTK_BOX(b
), bb
, FALSE
, FALSE
, 0);
6024 gtk_box_pack_start(GTK_BOX(b
), t
->label
, FALSE
, FALSE
, 0);
6025 #if GTK_CHECK_VERSION(2, 20, 0)
6026 gtk_box_pack_start(GTK_BOX(b
), t
->spinner
, FALSE
, FALSE
, 0);
6030 if (browser_mode
== XT_BM_KIOSK
) {
6031 t
->toolbar
= create_kiosk_toolbar(t
);
6032 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
6035 t
->toolbar
= create_toolbar(t
);
6037 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
,
6045 t
->browser_win
= create_browser(t
);
6046 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
6048 /* oops message for user feedback */
6049 t
->oops
= gtk_entry_new();
6050 gtk_entry_set_inner_border(GTK_ENTRY(t
->oops
), NULL
);
6051 gtk_entry_set_has_frame(GTK_ENTRY(t
->oops
), FALSE
);
6052 gtk_widget_set_can_focus(GTK_WIDGET(t
->oops
), FALSE
);
6053 gdk_color_parse(XT_COLOR_RED
, &color
);
6054 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
6055 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
6056 gtk_widget_modify_font(GTK_WIDGET(t
->oops
), oops_font
);
6059 t
->cmd
= gtk_entry_new();
6060 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
6061 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
6062 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
6063 gtk_widget_modify_font(GTK_WIDGET(t
->cmd
), cmd_font
);
6066 t
->statusbar_box
= gtk_hbox_new(FALSE
, 0);
6068 t
->sbe
.statusbar
= gtk_entry_new();
6069 gtk_entry_set_inner_border(GTK_ENTRY(t
->sbe
.statusbar
), NULL
);
6070 gtk_entry_set_has_frame(GTK_ENTRY(t
->sbe
.statusbar
), FALSE
);
6071 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.statusbar
), FALSE
);
6072 gtk_widget_modify_font(GTK_WIDGET(t
->sbe
.statusbar
), statusbar_font
);
6074 /* create these widgets only if specified in statusbar_elems */
6076 t
->sbe
.position
= create_sbe(40);
6077 t
->sbe
.zoom
= create_sbe(40);
6078 t
->sbe
.buffercmd
= create_sbe(60);
6080 statusbar_modify_attr(t
, XT_COLOR_WHITE
, XT_COLOR_BLACK
);
6082 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), t
->sbe
.statusbar
, TRUE
,
6085 /* gtk widgets cannot be added to a box twice. sbe_* variables
6086 make sure of this */
6087 for (p
= statusbar_elems
; *p
!= '\0'; p
++) {
6091 GtkWidget
*sep
= gtk_vseparator_new();
6093 gdk_color_parse(XT_COLOR_SB_SEPARATOR
, &color
);
6094 gtk_widget_modify_bg(sep
, GTK_STATE_NORMAL
, &color
);
6095 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), sep
,
6096 FALSE
, FALSE
, FALSE
);
6101 warnx("flag \"%c\" specified more than "
6102 "once in statusbar_elems\n", *p
);
6106 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6107 t
->sbe
.position
, FALSE
, FALSE
, FALSE
);
6111 warnx("flag \"%c\" specified more than "
6112 "once in statusbar_elems\n", *p
);
6116 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6117 t
->sbe
.buffercmd
, FALSE
, FALSE
, FALSE
);
6121 warnx("flag \"%c\" specified more than "
6122 "once in statusbar_elems\n", *p
);
6126 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6127 t
->sbe
.zoom
, FALSE
, FALSE
, FALSE
);
6130 warnx("illegal flag \"%c\" in statusbar_elems\n", *p
);
6135 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar_box
, FALSE
, FALSE
, 0);
6138 t
->buffers
= create_buffers(t
);
6139 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->buffers
, FALSE
, FALSE
, 0);
6141 /* xtp meaning is normal by default */
6142 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
6144 /* set empty favicon */
6145 xt_icon_from_name(t
, "text-html");
6147 /* and show it all */
6148 gtk_widget_show_all(b
);
6149 gtk_widget_show_all(t
->vbox
);
6151 /* compact tab bar */
6152 t
->tab_elems
.label
= gtk_label_new(title
);
6153 gtk_label_set_width_chars(GTK_LABEL(t
->tab_elems
.label
), 1.0);
6154 gtk_misc_set_alignment(GTK_MISC(t
->tab_elems
.label
), 0.0, 0.0);
6155 gtk_misc_set_padding(GTK_MISC(t
->tab_elems
.label
), 4.0, 4.0);
6156 gtk_widget_modify_font(GTK_WIDGET(t
->tab_elems
.label
), tabbar_font
);
6158 t
->tab_elems
.eventbox
= gtk_event_box_new();
6159 t
->tab_elems
.box
= gtk_hbox_new(FALSE
, 0);
6160 t
->tab_elems
.sep
= gtk_vseparator_new();
6162 gdk_color_parse(XT_COLOR_CT_BACKGROUND
, &color
);
6163 gtk_widget_modify_bg(t
->tab_elems
.eventbox
, GTK_STATE_NORMAL
, &color
);
6164 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6165 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
, &color
);
6166 gdk_color_parse(XT_COLOR_CT_SEPARATOR
, &color
);
6167 gtk_widget_modify_bg(t
->tab_elems
.sep
, GTK_STATE_NORMAL
, &color
);
6169 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.label
, TRUE
,
6171 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.sep
, FALSE
,
6173 gtk_container_add(GTK_CONTAINER(t
->tab_elems
.eventbox
),
6176 gtk_box_pack_start(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
, TRUE
,
6178 gtk_widget_show_all(t
->tab_elems
.eventbox
);
6180 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
6183 id
= position
>= 0 ? position
:
6184 gtk_notebook_get_current_page(notebook
) + 1;
6185 if (id
> gtk_notebook_get_n_pages(notebook
))
6188 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
6189 gtk_notebook_insert_page(notebook
, t
->vbox
, b
, id
);
6190 gtk_box_reorder_child(GTK_BOX(tab_bar
),
6191 t
->tab_elems
.eventbox
, id
);
6196 #if GTK_CHECK_VERSION(2, 20, 0)
6197 /* turn spinner off if we are a new tab without uri */
6199 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
6200 gtk_widget_hide(t
->spinner
);
6203 /* make notebook tabs reorderable */
6204 gtk_notebook_set_tab_reorderable(notebook
, t
->vbox
, TRUE
);
6206 /* compact tabs clickable */
6207 g_signal_connect(G_OBJECT(t
->tab_elems
.eventbox
),
6208 "button_press_event", G_CALLBACK(tab_clicked_cb
), t
);
6210 g_object_connect(G_OBJECT(t
->cmd
),
6211 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb
), t
,
6212 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
6213 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb
), t
,
6214 "signal::activate", G_CALLBACK(cmd_activate_cb
), t
,
6215 "signal::populate-popup", G_CALLBACK(cmd_popup_cb
), t
,
6218 /* reuse wv_button_cb to hide oops */
6219 g_object_connect(G_OBJECT(t
->oops
),
6220 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
6223 g_signal_connect(t
->buffers
,
6224 "row-activated", G_CALLBACK(row_activated_cb
), t
);
6225 g_object_connect(G_OBJECT(t
->buffers
),
6226 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
, (char *)NULL
);
6228 g_object_connect(G_OBJECT(t
->wv
),
6229 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
,
6230 "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb
), t
,
6231 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb
), t
,
6232 "signal::download-requested", G_CALLBACK(webview_download_cb
), t
,
6233 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), t
,
6234 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
6235 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
6236 "signal::create-web-view", G_CALLBACK(webview_cwv_cb
), t
,
6237 "signal::close-web-view", G_CALLBACK(webview_closewv_cb
), t
,
6238 "signal::event", G_CALLBACK(webview_event_cb
), t
,
6239 "signal::load-finished", G_CALLBACK(webview_load_finished_cb
), t
,
6240 "signal::load-progress-changed", G_CALLBACK(webview_progress_changed_cb
), t
,
6241 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb
), t
,
6242 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
6243 "signal::button_release_event", G_CALLBACK(wv_release_button_cb
), t
,
6244 "signal::populate-popup", G_CALLBACK(wv_popup_cb
), t
,
6246 g_signal_connect(t
->wv
,
6247 "notify::load-status", G_CALLBACK(notify_load_status_cb
), t
);
6248 g_signal_connect(t
->wv
,
6249 "notify::title", G_CALLBACK(notify_title_cb
), t
);
6251 /* hijack the unused keys as if we were the browser */
6252 g_object_connect(G_OBJECT(t
->toolbar
),
6253 "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb
), t
,
6256 g_signal_connect(G_OBJECT(bb
), "button_press_event",
6257 G_CALLBACK(tab_close_cb
), t
);
6260 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
6261 /* restore the tab's history */
6262 if (u
&& u
->history
) {
6266 webkit_web_back_forward_list_add_item(t
->bfl
, item
);
6267 items
= g_list_next(items
);
6270 item
= g_list_nth_data(u
->history
, u
->back
);
6272 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
6275 g_list_free(u
->history
);
6277 webkit_web_back_forward_list_clear(t
->bfl
);
6283 url_set_visibility();
6284 statusbar_set_visibility();
6287 set_current_tab(t
->tab_id
);
6288 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
6292 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
6296 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6303 recolor_compact_tabs();
6304 setzoom_webkit(t
, XT_ZOOM_NORMAL
);
6309 notebook_switchpage_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
6315 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
6317 if (gtk_notebook_get_current_page(notebook
) == -1)
6320 TAILQ_FOREACH(t
, &tabs
, entry
) {
6321 if (t
->tab_id
== pn
) {
6322 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
6325 uri
= get_title(t
, TRUE
);
6326 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
6332 /* can't use focus_webview here */
6333 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6340 notebook_pagereordered_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
6343 struct tab
*t
= NULL
, *tt
;
6347 TAILQ_FOREACH(tt
, &tabs
, entry
)
6348 if (tt
->tab_id
== pn
) {
6354 DNPRINTF(XT_D_TAB
, "page_reordered_cb: tab: %d\n", t
->tab_id
);
6356 gtk_box_reorder_child(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
,
6361 menuitem_response(struct tab
*t
)
6363 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
6367 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
6369 GtkWidget
*menu
, *menu_items
;
6370 GdkEventButton
*bevent
;
6374 if (event
->type
== GDK_BUTTON_PRESS
) {
6375 bevent
= (GdkEventButton
*) event
;
6376 menu
= gtk_menu_new();
6378 TAILQ_FOREACH(ti
, &tabs
, entry
) {
6379 if ((uri
= get_uri(ti
)) == NULL
)
6380 /* XXX make sure there is something to print */
6381 /* XXX add gui pages in here to look purdy */
6383 menu_items
= gtk_menu_item_new_with_label(uri
);
6384 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), menu_items
);
6385 gtk_widget_show(menu_items
);
6387 g_signal_connect_swapped((menu_items
),
6388 "activate", G_CALLBACK(menuitem_response
),
6392 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
,
6393 bevent
->button
, bevent
->time
);
6395 /* unref object so it'll free itself when popped down */
6396 #if !GTK_CHECK_VERSION(3, 0, 0)
6397 /* XXX does not need unref with gtk+3? */
6398 g_object_ref_sink(menu
);
6399 g_object_unref(menu
);
6402 return (TRUE
/* eat event */);
6405 return (FALSE
/* propagate */);
6409 icon_size_map(int iconsz
)
6411 if (iconsz
<= GTK_ICON_SIZE_INVALID
||
6412 iconsz
> GTK_ICON_SIZE_DIALOG
)
6413 return (GTK_ICON_SIZE_SMALL_TOOLBAR
);
6419 create_button(char *name
, char *stockid
, int size
)
6421 GtkWidget
*button
, *image
;
6425 rcstring
= g_strdup_printf(
6426 "style \"%s-style\"\n"
6428 " GtkWidget::focus-padding = 0\n"
6429 " GtkWidget::focus-line-width = 0\n"
6433 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
6434 gtk_rc_parse_string(rcstring
);
6436 button
= gtk_button_new();
6437 gtk_button_set_focus_on_click(GTK_BUTTON(button
), FALSE
);
6438 gtk_icon_size
= icon_size_map(size
? size
: icon_size
);
6440 image
= gtk_image_new_from_stock(stockid
, gtk_icon_size
);
6441 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
6442 gtk_container_set_border_width(GTK_CONTAINER(button
), 1);
6443 gtk_container_add(GTK_CONTAINER(button
), GTK_WIDGET(image
));
6444 gtk_widget_set_name(button
, name
);
6445 gtk_button_set_relief(GTK_BUTTON(button
), GTK_RELIEF_NONE
);
6451 button_set_stockid(GtkWidget
*button
, char *stockid
)
6455 image
= gtk_image_new_from_stock(stockid
, icon_size_map(icon_size
));
6456 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
6457 gtk_button_set_image(GTK_BUTTON(button
), image
);
6461 clipb_primary_cb(GtkClipboard
*primary
, GdkEvent
*event
, gpointer notused
)
6464 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
6467 if (xterm_workaround
== 0)
6471 * xterm doesn't play nice with clipboards because it clears the
6472 * primary when clicked. We rely on primary being set to properly
6473 * handle middle mouse button clicks (paste). So when someone clears
6474 * primary copy whatever is in CUT_BUFFER0 into primary to simualte
6475 * other application behavior (as in DON'T clear primary).
6478 p
= gtk_clipboard_wait_for_text(primary
);
6480 if (gdk_property_get(gdk_get_default_root_window(),
6482 gdk_atom_intern("STRING", FALSE
),
6484 1024 * 1024 /* picked out of my butt */,
6490 /* yes sir, we need to NUL the string */
6492 gtk_clipboard_set_text(primary
, p
, -1);
6506 char file
[PATH_MAX
];
6509 vbox
= gtk_vbox_new(FALSE
, 0);
6510 gtk_box_set_spacing(GTK_BOX(vbox
), 0);
6511 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
6512 #if !GTK_CHECK_VERSION(3, 0, 0)
6513 /* XXX seems to be needed with gtk+2 */
6514 gtk_notebook_set_tab_hborder(notebook
, 0);
6515 gtk_notebook_set_tab_vborder(notebook
, 0);
6517 gtk_notebook_set_scrollable(notebook
, TRUE
);
6518 gtk_notebook_set_show_border(notebook
, FALSE
);
6519 gtk_widget_set_can_focus(GTK_WIDGET(notebook
), FALSE
);
6521 abtn
= gtk_button_new();
6522 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
6523 gtk_widget_set_size_request(arrow
, -1, -1);
6524 gtk_container_add(GTK_CONTAINER(abtn
), arrow
);
6525 gtk_widget_set_size_request(abtn
, -1, 20);
6527 #if GTK_CHECK_VERSION(2, 20, 0)
6528 gtk_notebook_set_action_widget(notebook
, abtn
, GTK_PACK_END
);
6530 gtk_widget_set_size_request(GTK_WIDGET(notebook
), -1, -1);
6532 /* compact tab bar */
6533 tab_bar
= gtk_hbox_new(TRUE
, 0);
6535 gtk_box_pack_start(GTK_BOX(vbox
), tab_bar
, FALSE
, FALSE
, 0);
6536 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
6537 gtk_widget_set_size_request(vbox
, -1, -1);
6539 g_object_connect(G_OBJECT(notebook
),
6540 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb
), NULL
,
6542 g_object_connect(G_OBJECT(notebook
),
6543 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb
),
6544 NULL
, (char *)NULL
);
6545 g_signal_connect(G_OBJECT(abtn
), "button_press_event",
6546 G_CALLBACK(arrow_cb
), NULL
);
6548 main_window
= create_window("xxxterm");
6549 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
6550 g_signal_connect(G_OBJECT(main_window
), "delete_event",
6551 G_CALLBACK(gtk_main_quit
), NULL
);
6554 for (i
= 0; i
< LENGTH(icons
); i
++) {
6555 snprintf(file
, sizeof file
, "%s/%s", resource_dir
, icons
[i
]);
6556 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
6557 l
= g_list_append(l
, pb
);
6559 gtk_window_set_default_icon_list(l
);
6561 /* clipboard work around */
6562 if (xterm_workaround
)
6564 G_OBJECT(gtk_clipboard_get(GDK_SELECTION_PRIMARY
)),
6565 "owner-change", G_CALLBACK(clipb_primary_cb
), NULL
);
6567 gtk_widget_show_all(abtn
);
6568 gtk_widget_show_all(main_window
);
6569 notebook_tab_set_visibility();
6573 send_cmd_to_socket(char *cmd
)
6576 struct sockaddr_un sa
;
6578 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
6579 warnx("%s: socket", __func__
);
6583 sa
.sun_family
= AF_UNIX
;
6584 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
6585 work_dir
, XT_SOCKET_FILE
);
6588 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
6589 warnx("%s: connect", __func__
);
6593 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
6594 warnx("%s: send", __func__
);
6605 socket_watcher(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
6608 char str
[XT_MAX_URL_LENGTH
];
6609 socklen_t t
= sizeof(struct sockaddr_un
);
6610 struct sockaddr_un sa
;
6615 gint fd
= g_io_channel_unix_get_fd(source
);
6617 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
6622 if (getpeereid(s
, &uid
, &gid
) == -1) {
6626 if (uid
!= getuid() || gid
!= getgid()) {
6627 warnx("unauthorized user");
6633 warnx("not a valid user");
6637 n
= recv(s
, str
, sizeof(str
), 0);
6641 tt
= TAILQ_LAST(&tabs
, tab_list
);
6642 cmd_execute(tt
, str
);
6650 struct sockaddr_un sa
;
6652 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
6653 warn("is_running: socket");
6657 sa
.sun_family
= AF_UNIX
;
6658 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
6659 work_dir
, XT_SOCKET_FILE
);
6662 /* connect to see if there is a listener */
6663 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
6664 rv
= 0; /* not running */
6666 rv
= 1; /* already running */
6677 struct sockaddr_un sa
;
6679 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
6680 warn("build_socket: socket");
6684 sa
.sun_family
= AF_UNIX
;
6685 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
6686 work_dir
, XT_SOCKET_FILE
);
6689 /* connect to see if there is a listener */
6690 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
6691 /* no listener so we will */
6692 unlink(sa
.sun_path
);
6694 if (bind(s
, (struct sockaddr
*)&sa
, len
) == -1) {
6695 warn("build_socket: bind");
6699 if (listen(s
, 1) == -1) {
6700 warn("build_socket: listen");
6713 completion_select_cb(GtkEntryCompletion
*widget
, GtkTreeModel
*model
,
6714 GtkTreeIter
*iter
, struct tab
*t
)
6718 gtk_tree_model_get(model
, iter
, 0, &value
, -1);
6726 completion_hover_cb(GtkEntryCompletion
*widget
, GtkTreeModel
*model
,
6727 GtkTreeIter
*iter
, struct tab
*t
)
6731 gtk_tree_model_get(model
, iter
, 0, &value
, -1);
6732 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), value
);
6733 gtk_editable_set_position(GTK_EDITABLE(t
->uri_entry
), -1);
6740 completion_add_uri(const gchar
*uri
)
6744 /* add uri to list_store */
6745 gtk_list_store_append(completion_model
, &iter
);
6746 gtk_list_store_set(completion_model
, &iter
, 0, uri
, -1);
6750 completion_match(GtkEntryCompletion
*completion
, const gchar
*key
,
6751 GtkTreeIter
*iter
, gpointer user_data
)
6754 gboolean match
= FALSE
;
6756 gtk_tree_model_get(GTK_TREE_MODEL(completion_model
), iter
, 0, &value
,
6762 match
= match_uri(value
, key
);
6769 completion_add(struct tab
*t
)
6771 /* enable completion for tab */
6772 t
->completion
= gtk_entry_completion_new();
6773 gtk_entry_completion_set_text_column(t
->completion
, 0);
6774 gtk_entry_set_completion(GTK_ENTRY(t
->uri_entry
), t
->completion
);
6775 gtk_entry_completion_set_model(t
->completion
,
6776 GTK_TREE_MODEL(completion_model
));
6777 gtk_entry_completion_set_match_func(t
->completion
, completion_match
,
6779 gtk_entry_completion_set_minimum_key_length(t
->completion
, 1);
6780 gtk_entry_completion_set_inline_selection(t
->completion
, TRUE
);
6781 g_signal_connect(G_OBJECT (t
->completion
), "match-selected",
6782 G_CALLBACK(completion_select_cb
), t
);
6783 g_signal_connect(G_OBJECT (t
->completion
), "cursor-on-match",
6784 G_CALLBACK(completion_hover_cb
), t
);
6792 if (stat(dir
, &sb
)) {
6793 if (mkdir(dir
, S_IRWXU
) == -1)
6794 err(1, "mkdir %s", dir
);
6796 err(1, "stat %s", dir
);
6798 if (S_ISDIR(sb
.st_mode
) == 0)
6799 errx(1, "%s not a dir", dir
);
6800 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
6801 warnx("fixing invalid permissions on %s", dir
);
6802 if (chmod(dir
, S_IRWXU
) == -1)
6803 err(1, "chmod %s", dir
);
6811 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
6815 GStaticRecMutex my_gdk_mtx
= G_STATIC_REC_MUTEX_INIT
;
6816 volatile int mtx_depth
;
6820 * The linux flash plugin violates the gdk locking mechanism.
6821 * Work around the issue by using a recursive mutex with some match applied
6822 * to see if we hit a buggy condition.
6824 * The following code is painful so just don't read it. It really doesn't
6825 * make much sense but seems to work.
6830 g_static_rec_mutex_lock(&my_gdk_mtx
);
6833 if (mtx_depth
<= 0) {
6834 /* should not happen */
6835 show_oops(NULL
, "negative mutex locking bug, trying to "
6837 fprintf(stderr
, "negative mutex locking bug, trying to "
6839 g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
6840 g_static_rec_mutex_lock(&my_gdk_mtx
);
6845 if (mtx_depth
!= 1) {
6846 /* decrease mutext depth to 1 */
6848 g_static_rec_mutex_unlock(&my_gdk_mtx
);
6850 } while (mtx_depth
> 1);
6859 /* if mutex depth isn't 1 then something went bad */
6860 if (mtx_depth
!= 1) {
6861 x
= g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
6863 /* should not happen */
6864 show_oops(NULL
, "mutex unlocking bug, trying to "
6866 fprintf(stderr
, "mutex unlocking bug, trying to "
6870 if (mtx_complain
== 0) {
6871 show_oops(NULL
, "buggy mutex implementation detected, "
6872 "work around implemented");
6873 fprintf(stderr
, "buggy mutex implementation detected, "
6874 "work around implemented");
6881 g_static_rec_mutex_unlock(&my_gdk_mtx
);
6885 main(int argc
, char *argv
[])
6888 int c
, s
, optn
= 0, opte
= 0, focus
= 1;
6889 char conf
[PATH_MAX
] = { '\0' };
6890 char file
[PATH_MAX
];
6891 char *env_proxy
= NULL
;
6895 struct sigaction sact
;
6896 GIOChannel
*channel
;
6903 g_thread_init(NULL
);
6904 gdk_threads_set_lock_functions(mtx_lock
, mtx_unlock
);
6906 gdk_threads_enter();
6908 gcry_control (GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
6910 gtk_init(&argc
, &argv
);
6912 gnutls_global_init();
6914 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
6919 RB_INIT(&downloads
);
6921 TAILQ_INIT(&sessions
);
6924 TAILQ_INIT(&aliases
);
6931 /* fiddle with ulimits */
6932 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
6935 /* just use them all */
6936 rlp
.rlim_cur
= rlp
.rlim_max
;
6937 if (setrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
6939 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
6941 else if (rlp
.rlim_cur
<= 256)
6942 startpage_add("%s requires at least 256 file "
6943 "descriptors, currently it has up to %d available",
6944 __progname
, rlp
.rlim_cur
);
6947 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
6956 errx(0 , "Version: %s", version
);
6959 strlcpy(conf
, optarg
, sizeof(conf
));
6962 strlcpy(named_session
, optarg
, sizeof(named_session
));
6983 xtp_generate_keys();
6986 bzero(&sact
, sizeof(sact
));
6987 sigemptyset(&sact
.sa_mask
);
6988 sact
.sa_handler
= sigchild
;
6989 sact
.sa_flags
= SA_NOCLDSTOP
;
6990 sigaction(SIGCHLD
, &sact
, NULL
);
6992 /* set download dir */
6993 pwd
= getpwuid(getuid());
6995 errx(1, "invalid user %d", getuid());
6996 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
6998 /* compile buffer command regexes */
7001 /* set default string settings */
7002 home
= g_strdup("https://www.cyphertite.com");
7003 search_string
= g_strdup("https://ssl.scroogle.org/cgi-bin/nbbwssl.cgi?Gw=%s");
7004 resource_dir
= g_strdup("/usr/local/share/xxxterm/");
7005 strlcpy(runtime_settings
, "runtime", sizeof runtime_settings
);
7006 cmd_font_name
= g_strdup("monospace normal 9");
7007 oops_font_name
= g_strdup("monospace normal 9");
7008 statusbar_font_name
= g_strdup("monospace normal 9");
7009 tabbar_font_name
= g_strdup("monospace normal 9");
7010 statusbar_elems
= g_strdup("BP");
7011 encoding
= g_strdup("UTF-8");
7013 /* read config file */
7014 if (strlen(conf
) == 0)
7015 snprintf(conf
, sizeof conf
, "%s/.%s",
7016 pwd
->pw_dir
, XT_CONF_FILE
);
7017 config_parse(conf
, 0);
7020 cmd_font
= pango_font_description_from_string(cmd_font_name
);
7021 oops_font
= pango_font_description_from_string(oops_font_name
);
7022 statusbar_font
= pango_font_description_from_string(statusbar_font_name
);
7023 tabbar_font
= pango_font_description_from_string(tabbar_font_name
);
7025 /* working directory */
7026 if (strlen(work_dir
) == 0)
7027 snprintf(work_dir
, sizeof work_dir
, "%s/%s",
7028 pwd
->pw_dir
, XT_DIR
);
7031 /* icon cache dir */
7032 snprintf(cache_dir
, sizeof cache_dir
, "%s/%s", work_dir
, XT_CACHE_DIR
);
7036 snprintf(certs_dir
, sizeof certs_dir
, "%s/%s", work_dir
, XT_CERT_DIR
);
7040 snprintf(sessions_dir
, sizeof sessions_dir
, "%s/%s",
7041 work_dir
, XT_SESSIONS_DIR
);
7042 xxx_dir(sessions_dir
);
7044 /* runtime settings that can override config file */
7045 if (runtime_settings
[0] != '\0')
7046 config_parse(runtime_settings
, 1);
7049 if (!strcmp(download_dir
, pwd
->pw_dir
))
7050 strlcat(download_dir
, "/downloads", sizeof download_dir
);
7051 xxx_dir(download_dir
);
7053 /* favorites file */
7054 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_FAVS_FILE
);
7055 if (stat(file
, &sb
)) {
7056 warnx("favorites file doesn't exist, creating it");
7057 if ((f
= fopen(file
, "w")) == NULL
)
7058 err(1, "favorites");
7062 /* quickmarks file */
7063 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
7064 if (stat(file
, &sb
)) {
7065 warnx("quickmarks file doesn't exist, creating it");
7066 if ((f
= fopen(file
, "w")) == NULL
)
7067 err(1, "quickmarks");
7071 /* search history */
7072 if (history_autosave
) {
7073 snprintf(search_file
, sizeof search_file
, "%s/%s",
7074 work_dir
, XT_SEARCH_FILE
);
7075 if (stat(search_file
, &sb
)) {
7076 warnx("search history file doesn't exist, creating it");
7077 if ((f
= fopen(search_file
, "w")) == NULL
)
7078 err(1, "search_history");
7081 history_read(&shl
, search_file
, &search_history_count
);
7084 /* command history */
7085 if (history_autosave
) {
7086 snprintf(command_file
, sizeof command_file
, "%s/%s",
7087 work_dir
, XT_COMMAND_FILE
);
7088 if (stat(command_file
, &sb
)) {
7089 warnx("command history file doesn't exist, creating it");
7090 if ((f
= fopen(command_file
, "w")) == NULL
)
7091 err(1, "command_history");
7094 history_read(&chl
, command_file
, &cmd_history_count
);
7098 session
= webkit_get_default_session();
7103 if (stat(ssl_ca_file
, &sb
)) {
7104 warnx("no CA file: %s", ssl_ca_file
);
7105 g_free(ssl_ca_file
);
7108 g_object_set(session
,
7109 SOUP_SESSION_SSL_CA_FILE
, ssl_ca_file
,
7110 SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
7114 /* guess_search regex */
7115 if (url_regex
== NULL
)
7116 url_regex
= g_strdup(XT_URL_REGEX
);
7118 if (regcomp(&url_re
, url_regex
, REG_EXTENDED
| REG_NOSUB
))
7119 startpage_add("invalid url regex %s", url_regex
);
7122 env_proxy
= getenv("http_proxy");
7124 setup_proxy(env_proxy
);
7126 setup_proxy(http_proxy
);
7129 send_cmd_to_socket(argv
[0]);
7133 /* set some connection parameters */
7134 g_object_set(session
, "max-conns", max_connections
, (char *)NULL
);
7135 g_object_set(session
, "max-conns-per-host", max_host_connections
,
7138 /* see if there is already an xxxterm running */
7139 if (single_instance
&& is_running()) {
7141 warnx("already running");
7146 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
7147 send_cmd_to_socket(cmd
);
7157 /* uri completion */
7158 completion_model
= gtk_list_store_new(1, G_TYPE_STRING
);
7161 buffers_store
= gtk_list_store_new
7162 (NUM_COLS
, G_TYPE_UINT
, G_TYPE_STRING
);
7168 notebook_tab_set_visibility();
7170 if (save_global_history
)
7171 restore_global_history();
7173 /* restore session list */
7174 restore_sessions_list();
7176 if (!strcmp(named_session
, XT_SAVED_TABS_FILE
))
7177 restore_saved_tabs();
7179 a
.s
= named_session
;
7180 a
.i
= XT_SES_DONOTHING
;
7181 open_tabs(NULL
, &a
);
7184 /* see if we have an exception */
7185 if (!TAILQ_EMPTY(&spl
)) {
7186 create_new_tab("about:startpage", NULL
, focus
, -1);
7191 create_new_tab(argv
[0], NULL
, focus
, -1);
7198 if (TAILQ_EMPTY(&tabs
))
7199 create_new_tab(home
, NULL
, 1, -1);
7202 if ((s
= build_socket()) != -1) {
7203 channel
= g_io_channel_unix_new(s
);
7204 g_io_add_watch(channel
, G_IO_IN
, socket_watcher
, NULL
);
7210 gdk_threads_leave();
7211 g_static_rec_mutex_unlock_full(&my_gdk_mtx
); /* just in case */
7214 gnutls_global_deinit();