2 * Copyright (c) 2010, 2011 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2011 Stevan Andjelkovic <stevan@student.chalmers.se>
4 * Copyright (c) 2010, 2011 Edd Barrett <vext01@gmail.com>
5 * Copyright (c) 2011 Todd T. Fries <todd@fries.net>
6 * Copyright (c) 2011 Raphael Graf <r@undefined.ch>
7 * Copyright (c) 2011 Michal Mazurek <akfaew@jasminek.net>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 char *version
= XXXTERM_VERSION
;
27 u_int32_t swm_debug
= 0
48 GCRY_THREAD_OPTION_PTHREAD_IMPL
;
60 TAILQ_ENTRY(session
) entry
;
63 TAILQ_HEAD(session_list
, session
);
66 TAILQ_ENTRY(undo
) entry
;
69 int back
; /* Keeps track of how many back
70 * history items there are. */
72 TAILQ_HEAD(undo_tailq
, undo
);
74 struct command_entry
{
76 TAILQ_ENTRY(command_entry
) entry
;
78 TAILQ_HEAD(command_list
, command_entry
);
81 #define XT_DIR (".xxxterm")
82 #define XT_CACHE_DIR ("cache")
83 #define XT_CERT_DIR ("certs/")
84 #define XT_JS_DIR ("js/")
85 #define XT_SESSIONS_DIR ("sessions/")
86 #define XT_CONF_FILE ("xxxterm.conf")
87 #define XT_QMARKS_FILE ("quickmarks")
88 #define XT_SAVED_TABS_FILE ("main_session")
89 #define XT_RESTART_TABS_FILE ("restart_tabs")
90 #define XT_SOCKET_FILE ("socket")
91 #define XT_HISTORY_FILE ("history")
92 #define XT_SAVE_SESSION_ID ("SESSION_NAME=")
93 #define XT_SEARCH_FILE ("search_history")
94 #define XT_COMMAND_FILE ("command_history")
95 #define XT_DLMAN_REFRESH "10"
96 #define XT_MAX_URL_LENGTH (4096) /* 1 page is atomic, don't make bigger */
97 #define XT_MAX_UNDO_CLOSE_TAB (32)
98 #define XT_RESERVED_CHARS "$&+,/:;=?@ \"<>#%%{}|^~[]`"
99 #define XT_PRINT_EXTRA_MARGIN 10
100 #define XT_URL_REGEX ("^[[:blank:]]*[^[:blank:]]*([[:alnum:]-]+\\.)+[[:alnum:]-][^[:blank:]]*[[:blank:]]*$")
101 #define XT_INVALID_MARK (-1) /* XXX this is a double, maybe use something else, like a nan */
104 #define XT_COLOR_RED "#cc0000"
105 #define XT_COLOR_YELLOW "#ffff66"
106 #define XT_COLOR_BLUE "lightblue"
107 #define XT_COLOR_GREEN "#99ff66"
108 #define XT_COLOR_WHITE "white"
109 #define XT_COLOR_BLACK "black"
111 #define XT_COLOR_CT_BACKGROUND "#000000"
112 #define XT_COLOR_CT_INACTIVE "#dddddd"
113 #define XT_COLOR_CT_ACTIVE "#bbbb00"
114 #define XT_COLOR_CT_SEPARATOR "#555555"
116 #define XT_COLOR_SB_SEPARATOR "#555555"
118 #define XT_PROTO_DELIM "://"
121 #define XT_MOVE_INVALID (0)
122 #define XT_MOVE_DOWN (1)
123 #define XT_MOVE_UP (2)
124 #define XT_MOVE_BOTTOM (3)
125 #define XT_MOVE_TOP (4)
126 #define XT_MOVE_PAGEDOWN (5)
127 #define XT_MOVE_PAGEUP (6)
128 #define XT_MOVE_HALFDOWN (7)
129 #define XT_MOVE_HALFUP (8)
130 #define XT_MOVE_LEFT (9)
131 #define XT_MOVE_FARLEFT (10)
132 #define XT_MOVE_RIGHT (11)
133 #define XT_MOVE_FARRIGHT (12)
134 #define XT_MOVE_PERCENT (13)
135 #define XT_MOVE_CENTER (14)
137 #define XT_QMARK_SET (0)
138 #define XT_QMARK_OPEN (1)
139 #define XT_QMARK_TAB (2)
141 #define XT_MARK_SET (0)
142 #define XT_MARK_GOTO (1)
144 #define XT_TAB_LAST (-4)
145 #define XT_TAB_FIRST (-3)
146 #define XT_TAB_PREV (-2)
147 #define XT_TAB_NEXT (-1)
148 #define XT_TAB_INVALID (0)
149 #define XT_TAB_NEW (1)
150 #define XT_TAB_DELETE (2)
151 #define XT_TAB_DELQUIT (3)
152 #define XT_TAB_OPEN (4)
153 #define XT_TAB_UNDO_CLOSE (5)
154 #define XT_TAB_SHOW (6)
155 #define XT_TAB_HIDE (7)
156 #define XT_TAB_NEXTSTYLE (8)
158 #define XT_NAV_INVALID (0)
159 #define XT_NAV_BACK (1)
160 #define XT_NAV_FORWARD (2)
161 #define XT_NAV_RELOAD (3)
162 #define XT_NAV_STOP (4)
164 #define XT_FOCUS_INVALID (0)
165 #define XT_FOCUS_URI (1)
166 #define XT_FOCUS_SEARCH (2)
168 #define XT_SEARCH_INVALID (0)
169 #define XT_SEARCH_NEXT (1)
170 #define XT_SEARCH_PREV (2)
172 #define XT_PASTE_CURRENT_TAB (0)
173 #define XT_PASTE_NEW_TAB (1)
175 #define XT_ZOOM_IN (-1)
176 #define XT_ZOOM_OUT (-2)
177 #define XT_ZOOM_NORMAL (100)
179 #define XT_URL_SHOW (1)
180 #define XT_URL_HIDE (2)
182 #define XT_CMD_OPEN (0)
183 #define XT_CMD_OPEN_CURRENT (1)
184 #define XT_CMD_TABNEW (2)
185 #define XT_CMD_TABNEW_CURRENT (3)
187 #define XT_STATUS_NOTHING (0)
188 #define XT_STATUS_LINK (1)
189 #define XT_STATUS_URI (2)
190 #define XT_STATUS_LOADING (3)
192 #define XT_SES_DONOTHING (0)
193 #define XT_SES_CLOSETABS (1)
195 #define XT_PREFIX (1<<0)
196 #define XT_USERARG (1<<1)
197 #define XT_URLARG (1<<2)
198 #define XT_INTARG (1<<3)
199 #define XT_SESSARG (1<<4)
200 #define XT_SETARG (1<<5)
202 #define XT_HINT_NEWTAB (1<<0)
204 #define XT_BUFCMD_SZ (8)
206 #define XT_EJS_SHOW (1<<0)
208 GtkWidget
* create_button(char *, char *, int);
210 void recalc_tabs(void);
211 void recolor_compact_tabs(void);
212 void set_current_tab(int page_num
);
213 gboolean
update_statusbar_position(GtkAdjustment
*, gpointer
);
214 void marks_clear(struct tab
*t
);
217 extern char *__progname
;
220 GtkWidget
*main_window
;
221 GtkNotebook
*notebook
;
223 GtkWidget
*arrow
, *abtn
;
224 struct tab_list tabs
;
225 struct history_list hl
;
226 struct session_list sessions
;
227 struct domain_list c_wl
;
228 struct domain_list js_wl
;
229 struct domain_list pl_wl
;
230 struct undo_tailq undos
;
231 struct keybinding_list kbl
;
233 struct command_list chl
;
234 struct command_list shl
;
235 struct command_entry
*history_at
;
236 struct command_entry
*search_at
;
238 int cmd_history_count
= 0;
239 int search_history_count
= 0;
241 long long unsigned int blocked_cookies
= 0;
242 char named_session
[PATH_MAX
];
243 GtkListStore
*completion_model
;
244 GtkListStore
*buffers_store
;
246 char *qmarks
[XT_NOMARKS
];
247 int btn_down
; /* M1 down in any wv */
248 regex_t url_re
; /* guess_search regex */
250 /* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
251 int next_download_id
= 1;
253 void xxx_dir(char *);
254 int icon_size_map(int);
255 void completion_add(struct tab
*);
256 void completion_add_uri(const gchar
*);
259 history_delete(struct command_list
*l
, int *counter
)
261 struct command_entry
*c
;
263 if (l
== NULL
|| counter
== NULL
)
266 c
= TAILQ_LAST(l
, command_list
);
270 TAILQ_REMOVE(l
, c
, entry
);
277 history_add(struct command_list
*list
, char *file
, char *l
, int *counter
)
279 struct command_entry
*c
;
282 if (list
== NULL
|| l
== NULL
|| counter
== NULL
)
285 /* don't add the same line */
286 c
= TAILQ_FIRST(list
);
288 if (!strcmp(c
->line
+ 1 /* skip space */, l
))
291 c
= g_malloc0(sizeof *c
);
292 c
->line
= g_strdup_printf(" %s", l
);
295 TAILQ_INSERT_HEAD(list
, c
, entry
);
298 history_delete(list
, counter
);
300 if (history_autosave
&& file
) {
301 f
= fopen(file
, "w");
303 show_oops(NULL
, "couldn't write history %s", file
);
307 TAILQ_FOREACH_REVERSE(c
, list
, command_list
, entry
) {
309 fprintf(f
, "%s\n", c
->line
);
317 history_read(struct command_list
*list
, char *file
, int *counter
)
320 char *s
, line
[65536];
322 if (list
== NULL
|| file
== NULL
)
325 f
= fopen(file
, "r");
327 startpage_add("couldn't open history file %s", file
);
332 s
= fgets(line
, sizeof line
, f
);
333 if (s
== NULL
|| feof(f
) || ferror(f
))
335 if ((s
= strchr(line
, '\n')) == NULL
) {
336 startpage_add("invalid history file %s", file
);
342 history_add(list
, NULL
, line
+ 1, counter
);
350 /* marks and quickmarks array storage.
351 * first a-z, then A-Z, then 0-9 */
358 if (i
>= 0 && i
<= 'z' - 'a')
362 if (i
>= 0 && i
<= 'Z' - 'A')
377 if (m
>= 'a' && m
<= 'z')
378 return ret
+ m
- 'a';
380 ret
+= 'z' - 'a' + 1;
381 if (m
>= 'A' && m
<= 'Z')
382 return ret
+ m
- 'A';
384 ret
+= 'Z' - 'A' + 1;
385 if (m
>= '0' && m
<= '9')
386 return ret
+ m
- '0';
395 int saved_errno
, status
;
400 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
404 if (errno
!= ECHILD
) {
406 clog_warn("sigchild: waitpid:");
412 if (WIFEXITED(status
)) {
413 if (WEXITSTATUS(status
) != 0) {
415 clog_warnx("sigchild: child exit status: %d",
416 WEXITSTATUS(status));
421 clog_warnx("sigchild: child is terminated abnormally");
430 is_g_object_setting(GObject
*o
, char *str
)
432 guint n_props
= 0, i
;
433 GParamSpec
**proplist
;
435 if (! G_IS_OBJECT(o
))
438 proplist
= g_object_class_list_properties(G_OBJECT_GET_CLASS(o
),
441 for (i
=0; i
< n_props
; i
++) {
442 if (! strcmp(proplist
[i
]->name
, str
))
449 get_current_tab(void)
453 TAILQ_FOREACH(t
, &tabs
, entry
) {
454 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
458 warnx("%s: no current tab", __func__
);
464 set_status(struct tab
*t
, gchar
*s
, int status
)
472 case XT_STATUS_LOADING
:
473 type
= g_strdup_printf("Loading: %s", s
);
477 type
= g_strdup_printf("Link: %s", s
);
479 t
->status
= g_strdup(gtk_entry_get_text(
480 GTK_ENTRY(t
->sbe
.statusbar
)));
484 type
= g_strdup_printf("%s", s
);
486 t
->status
= g_strdup(type
);
490 t
->status
= g_strdup(s
);
492 case XT_STATUS_NOTHING
:
497 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.statusbar
), s
);
503 hide_cmd(struct tab
*t
)
505 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
507 history_at
= NULL
; /* just in case */
508 search_at
= NULL
; /* just in case */
509 gtk_widget_hide(t
->cmd
);
513 show_cmd(struct tab
*t
)
515 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
519 gtk_widget_hide(t
->oops
);
520 gtk_widget_show(t
->cmd
);
524 hide_buffers(struct tab
*t
)
526 gtk_widget_hide(t
->buffers
);
527 gtk_list_store_clear(buffers_store
);
537 sort_tabs_by_page_num(struct tab
***stabs
)
542 num_tabs
= gtk_notebook_get_n_pages(notebook
);
544 *stabs
= g_malloc0(num_tabs
* sizeof(struct tab
*));
546 TAILQ_FOREACH(t
, &tabs
, entry
)
547 (*stabs
)[gtk_notebook_page_num(notebook
, t
->vbox
)] = t
;
553 buffers_make_list(void)
556 const gchar
*title
= NULL
;
558 struct tab
**stabs
= NULL
;
560 num_tabs
= sort_tabs_by_page_num(&stabs
);
562 for (i
= 0; i
< num_tabs
; i
++)
564 gtk_list_store_append(buffers_store
, &iter
);
565 title
= get_title(stabs
[i
], FALSE
);
566 gtk_list_store_set(buffers_store
, &iter
,
567 COL_ID
, i
+ 1, /* Enumerate the tabs starting from 1
577 show_buffers(struct tab
*t
)
580 gtk_widget_show(t
->buffers
);
581 gtk_widget_grab_focus(GTK_WIDGET(t
->buffers
));
585 toggle_buffers(struct tab
*t
)
587 if (gtk_widget_get_visible(t
->buffers
))
594 buffers(struct tab
*t
, struct karg
*args
)
602 hide_oops(struct tab
*t
)
604 gtk_widget_hide(t
->oops
);
608 show_oops(struct tab
*at
, const char *fmt
, ...)
612 struct tab
*t
= NULL
;
618 if ((t
= get_current_tab()) == NULL
)
624 if (vasprintf(&msg
, fmt
, ap
) == -1)
625 errx(1, "show_oops failed");
628 gtk_entry_set_text(GTK_ENTRY(t
->oops
), msg
);
629 gtk_widget_hide(t
->cmd
);
630 gtk_widget_show(t
->oops
);
636 char work_dir
[PATH_MAX
];
637 char certs_dir
[PATH_MAX
];
638 char js_dir
[PATH_MAX
];
639 char cache_dir
[PATH_MAX
];
640 char sessions_dir
[PATH_MAX
];
641 char cookie_file
[PATH_MAX
];
642 SoupSession
*session
;
643 SoupCookieJar
*s_cookiejar
;
644 SoupCookieJar
*p_cookiejar
;
645 char rc_fname
[PATH_MAX
];
647 struct mime_type_list mtl
;
648 struct alias_list aliases
;
651 struct tab
*create_new_tab(char *, struct undo
*, int, int);
652 void delete_tab(struct tab
*);
653 void setzoom_webkit(struct tab
*, int);
654 int download_rb_cmp(struct download
*, struct download
*);
655 gboolean
cmd_execute(struct tab
*t
, char *str
);
658 history_rb_cmp(struct history
*h1
, struct history
*h2
)
660 return (strcmp(h1
->uri
, h2
->uri
));
662 RB_GENERATE(history_list
, history
, entry
, history_rb_cmp
);
665 domain_rb_cmp(struct domain
*d1
, struct domain
*d2
)
667 return (strcmp(d1
->d
, d2
->d
));
669 RB_GENERATE(domain_list
, domain
, entry
, domain_rb_cmp
);
672 download_rb_cmp(struct download
*e1
, struct download
*e2
)
674 return (e1
->id
< e2
->id
? -1 : e1
->id
> e2
->id
);
676 RB_GENERATE(download_list
, download
, entry
, download_rb_cmp
);
678 struct valid_url_types
{
689 valid_url_type(char *url
)
693 for (i
= 0; i
< LENGTH(vut
); i
++)
694 if (!strncasecmp(vut
[i
].type
, url
, strlen(vut
[i
].type
)))
701 match_alias(char *url_in
)
705 char *url_out
= NULL
, *search
, *enc_arg
;
707 search
= g_strdup(url_in
);
709 if (strsep(&arg
, " \t") == NULL
) {
710 show_oops(NULL
, "match_alias: NULL URL");
714 TAILQ_FOREACH(a
, &aliases
, entry
) {
715 if (!strcmp(search
, a
->a_name
))
720 DNPRINTF(XT_D_URL
, "match_alias: matched alias %s\n",
723 enc_arg
= soup_uri_encode(arg
, XT_RESERVED_CHARS
);
724 url_out
= g_strdup_printf(a
->a_uri
, enc_arg
);
727 url_out
= g_strdup_printf(a
->a_uri
, "");
735 guess_url_type(char *url_in
)
738 char *url_out
= NULL
, *enc_search
= NULL
;
741 /* substitute aliases */
742 url_out
= match_alias(url_in
);
746 /* see if we are an about page */
747 if (!strncmp(url_in
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
))
748 for (i
= 0; i
< about_list_size(); i
++)
749 if (!strcmp(&url_in
[XT_URI_ABOUT_LEN
],
750 about_list
[i
].name
)) {
751 url_out
= g_strdup(url_in
);
755 if (guess_search
&& url_regex
&&
756 !(g_str_has_prefix(url_in
, "http://") ||
757 g_str_has_prefix(url_in
, "https://"))) {
758 if (regexec(&url_re
, url_in
, 0, NULL
, 0)) {
759 /* invalid URI so search instead */
760 enc_search
= soup_uri_encode(url_in
, XT_RESERVED_CHARS
);
761 url_out
= g_strdup_printf(search_string
, enc_search
);
767 /* XXX not sure about this heuristic */
768 if (stat(url_in
, &sb
) == 0)
769 url_out
= g_strdup_printf("file://%s", url_in
);
771 url_out
= g_strdup_printf("http://%s", url_in
); /* guess http */
773 DNPRINTF(XT_D_URL
, "guess_url_type: guessed %s\n", url_out
);
779 load_uri(struct tab
*t
, gchar
*uri
)
782 gchar
*newuri
= NULL
;
788 /* Strip leading spaces. */
789 while (*uri
&& isspace(*uri
))
792 if (strlen(uri
) == 0) {
797 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
799 if (valid_url_type(uri
)) {
800 newuri
= guess_url_type(uri
);
804 if (!strncmp(uri
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
)) {
805 for (i
= 0; i
< about_list_size(); i
++)
806 if (!strcmp(&uri
[XT_URI_ABOUT_LEN
], about_list
[i
].name
)) {
807 bzero(&args
, sizeof args
);
808 about_list
[i
].func(t
, &args
);
809 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
),
813 show_oops(t
, "invalid about page");
817 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
819 webkit_web_view_load_uri(t
->wv
, uri
);
826 get_uri(struct tab
*t
)
828 const gchar
*uri
= NULL
;
830 if (webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
)
832 if (t
->xtp_meaning
== XT_XTP_TAB_MEANING_NORMAL
) {
833 uri
= webkit_web_view_get_uri(t
->wv
);
835 /* use tmp_uri to make sure it is g_freed */
838 t
->tmp_uri
=g_strdup_printf("%s%s", XT_URI_ABOUT
,
839 about_list
[t
->xtp_meaning
].name
);
846 get_title(struct tab
*t
, bool window
)
848 const gchar
*set
= NULL
, *title
= NULL
;
849 WebKitLoadStatus status
= webkit_web_view_get_load_status(t
->wv
);
851 if (status
== WEBKIT_LOAD_PROVISIONAL
|| status
== WEBKIT_LOAD_FAILED
||
852 t
->xtp_meaning
== XT_XTP_TAB_MEANING_BL
)
855 title
= webkit_web_view_get_title(t
->wv
);
856 if ((set
= title
? title
: get_uri(t
)))
860 set
= window
? XT_NAME
: "(untitled)";
866 find_mime_type(char *mime_type
)
868 struct mime_type
*m
, *def
= NULL
, *rv
= NULL
;
870 TAILQ_FOREACH(m
, &mtl
, entry
) {
872 !strncmp(mime_type
, m
->mt_type
, strlen(m
->mt_type
)))
875 if (m
->mt_default
== 0 && !strcmp(mime_type
, m
->mt_type
)) {
888 wl_find_uri(const gchar
*s
, struct domain_list
*wl
)
894 if (s
== NULL
|| wl
== NULL
)
897 if (!strncmp(s
, "http://", strlen("http://")))
898 s
= &s
[strlen("http://")];
899 else if (!strncmp(s
, "https://", strlen("https://")))
900 s
= &s
[strlen("https://")];
905 for (i
= 0; i
< strlen(s
) + 1 /* yes er need this */; i
++)
906 /* chop string at first slash */
907 if (s
[i
] == '/' || s
[i
] == ':' || s
[i
] == '\0') {
919 js_ref_to_string(JSContextRef context
, JSValueRef ref
)
925 jsref
= JSValueToStringCopy(context
, ref
, NULL
);
929 l
= JSStringGetMaximumUTF8CStringSize(jsref
);
932 JSStringGetUTF8CString(jsref
, s
, l
);
933 JSStringRelease(jsref
);
939 disable_hints(struct tab
*t
)
941 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
943 run_script(t
, "hints.clearHints();");
944 t
->mode
= XT_MODE_COMMAND
;
949 enable_hints(struct tab
*t
)
951 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
954 run_script(t
, "hints.createHints('', 'F');");
956 run_script(t
, "hints.createHints('', 'f');");
957 t
->mode
= XT_MODE_HINT
;
960 #define XT_JS_DONE ("done;")
961 #define XT_JS_DONE_LEN (strlen(XT_JS_DONE))
962 #define XT_JS_INSERT ("insert;")
963 #define XT_JS_INSERT_LEN (strlen(XT_JS_INSERT))
966 run_script(struct tab
*t
, char *s
)
968 JSGlobalContextRef ctx
;
969 WebKitWebFrame
*frame
;
971 JSValueRef val
, exception
;
974 DNPRINTF(XT_D_JS
, "run_script: tab %d %s\n",
975 t
->tab_id
, s
== (char *)JS_HINTING
? "JS_HINTING" : s
);
977 frame
= webkit_web_view_get_main_frame(t
->wv
);
978 ctx
= webkit_web_frame_get_global_context(frame
);
980 str
= JSStringCreateWithUTF8CString(s
);
981 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
982 NULL
, 0, &exception
);
983 JSStringRelease(str
);
985 DNPRINTF(XT_D_JS
, "run_script: val %p\n", val
);
987 es
= js_ref_to_string(ctx
, exception
);
989 /* show_oops(t, "script exception: %s", es); */
990 DNPRINTF(XT_D_JS
, "run_script: exception %s\n", es
);
995 es
= js_ref_to_string(ctx
, val
);
998 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
1000 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
1004 DNPRINTF(XT_D_JS
, "run_script: val %s\n", es
);
1013 hint(struct tab
*t
, struct karg
*args
)
1016 DNPRINTF(XT_D_JS
, "hint: tab %d args %d\n", t
->tab_id
, args
->i
);
1018 if (t
->mode
== XT_MODE_HINT
) {
1019 if (args
->i
== XT_HINT_NEWTAB
)
1029 apply_style(struct tab
*t
)
1031 g_object_set(G_OBJECT(t
->settings
),
1032 "user-stylesheet-uri", t
->stylesheet
, (char *)NULL
);
1036 userstyle(struct tab
*t
, struct karg
*args
)
1038 DNPRINTF(XT_D_JS
, "userstyle: tab %d\n", t
->tab_id
);
1042 g_object_set(G_OBJECT(t
->settings
),
1043 "user-stylesheet-uri", NULL
, (char *)NULL
);
1052 * Doesn't work fully, due to the following bug:
1053 * https://bugs.webkit.org/show_bug.cgi?id=51747
1056 restore_global_history(void)
1058 char file
[PATH_MAX
];
1063 const char delim
[3] = {'\\', '\\', '\0'};
1065 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_HISTORY_FILE
);
1067 if ((f
= fopen(file
, "r")) == NULL
) {
1068 warnx("%s: fopen", __func__
);
1073 if ((uri
= fparseln(f
, NULL
, NULL
, delim
, 0)) == NULL
)
1074 if (feof(f
) || ferror(f
))
1077 if ((title
= fparseln(f
, NULL
, NULL
, delim
, 0)) == NULL
)
1078 if (feof(f
) || ferror(f
)) {
1080 warnx("%s: broken history file\n", __func__
);
1084 if (uri
&& strlen(uri
) && title
&& strlen(title
)) {
1085 webkit_web_history_item_new_with_data(uri
, title
);
1086 h
= g_malloc(sizeof(struct history
));
1087 h
->uri
= g_strdup(uri
);
1088 h
->title
= g_strdup(title
);
1089 RB_INSERT(history_list
, &hl
, h
);
1090 completion_add_uri(h
->uri
);
1092 warnx("%s: failed to restore history\n", __func__
);
1108 save_global_history_to_disk(struct tab
*t
)
1110 char file
[PATH_MAX
];
1114 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_HISTORY_FILE
);
1116 if ((f
= fopen(file
, "w")) == NULL
) {
1117 show_oops(t
, "%s: global history file: %s",
1118 __func__
, strerror(errno
));
1122 RB_FOREACH_REVERSE(h
, history_list
, &hl
) {
1123 if (h
->uri
&& h
->title
)
1124 fprintf(f
, "%s\n%s\n", h
->uri
, h
->title
);
1133 quit(struct tab
*t
, struct karg
*args
)
1135 if (save_global_history
)
1136 save_global_history_to_disk(t
);
1144 restore_sessions_list(void)
1147 struct dirent
*dp
= NULL
;
1150 sdir
= opendir(sessions_dir
);
1152 while ((dp
= readdir(sdir
)) != NULL
)
1153 if (dp
->d_type
== DT_REG
) {
1154 s
= g_malloc(sizeof(struct session
));
1155 s
->name
= g_strdup(dp
->d_name
);
1156 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
1163 open_tabs(struct tab
*t
, struct karg
*a
)
1165 char file
[PATH_MAX
];
1169 struct tab
*ti
, *tt
;
1174 ti
= TAILQ_LAST(&tabs
, tab_list
);
1176 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, a
->s
);
1177 if ((f
= fopen(file
, "r")) == NULL
)
1181 if ((uri
= fparseln(f
, NULL
, NULL
, "\0\0\0", 0)) == NULL
)
1182 if (feof(f
) || ferror(f
))
1185 /* retrieve session name */
1186 if (uri
&& g_str_has_prefix(uri
, XT_SAVE_SESSION_ID
)) {
1187 strlcpy(named_session
,
1188 &uri
[strlen(XT_SAVE_SESSION_ID
)],
1189 sizeof named_session
);
1193 if (uri
&& strlen(uri
))
1194 create_new_tab(uri
, NULL
, 1, -1);
1200 /* close open tabs */
1201 if (a
->i
== XT_SES_CLOSETABS
&& ti
!= NULL
) {
1203 tt
= TAILQ_FIRST(&tabs
);
1224 restore_saved_tabs(void)
1226 char file
[PATH_MAX
];
1227 int unlink_file
= 0;
1232 snprintf(file
, sizeof file
, "%s/%s",
1233 sessions_dir
, XT_RESTART_TABS_FILE
);
1234 if (stat(file
, &sb
) == -1)
1235 a
.s
= XT_SAVED_TABS_FILE
;
1238 a
.s
= XT_RESTART_TABS_FILE
;
1241 a
.i
= XT_SES_DONOTHING
;
1242 rv
= open_tabs(NULL
, &a
);
1251 save_tabs(struct tab
*t
, struct karg
*a
)
1253 char file
[PATH_MAX
];
1255 int num_tabs
= 0, i
;
1256 struct tab
**stabs
= NULL
;
1258 /* tab may be null here */
1263 snprintf(file
, sizeof file
, "%s/%s",
1264 sessions_dir
, named_session
);
1266 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, a
->s
);
1268 if ((f
= fopen(file
, "w")) == NULL
) {
1269 show_oops(t
, "Can't open save_tabs file: %s", strerror(errno
));
1273 /* save session name */
1274 fprintf(f
, "%s%s\n", XT_SAVE_SESSION_ID
, named_session
);
1276 /* Save tabs, in the order they are arranged in the notebook. */
1277 num_tabs
= sort_tabs_by_page_num(&stabs
);
1279 for (i
= 0; i
< num_tabs
; i
++)
1281 if (get_uri(stabs
[i
]) != NULL
)
1282 fprintf(f
, "%s\n", get_uri(stabs
[i
]));
1283 else if (gtk_entry_get_text(GTK_ENTRY(
1284 stabs
[i
]->uri_entry
)))
1285 fprintf(f
, "%s\n", gtk_entry_get_text(GTK_ENTRY(
1286 stabs
[i
]->uri_entry
)));
1291 /* try and make sure this gets to disk NOW. XXX Backup first? */
1292 if (fflush(f
) != 0 || fsync(fileno(f
)) != 0) {
1293 show_oops(t
, "May not have managed to save session: %s",
1303 save_tabs_and_quit(struct tab
*t
, struct karg
*args
)
1315 run_page_script(struct tab
*t
, struct karg
*args
)
1318 char *tmp
, script
[PATH_MAX
];
1320 tmp
= args
->s
!= NULL
&& strlen(args
->s
) > 0 ? args
->s
: default_script
;
1321 if (tmp
[0] == '\0') {
1322 show_oops(t
, "no script specified");
1326 if ((uri
= get_uri(t
)) == NULL
) {
1327 show_oops(t
, "tab is empty, not running script");
1332 snprintf(script
, sizeof script
, "%s/%s",
1333 pwd
->pw_dir
, &tmp
[1]);
1335 strlcpy(script
, tmp
, sizeof script
);
1339 show_oops(t
, "can't fork to run script");
1349 execlp(script
, script
, uri
, (void *)NULL
);
1359 yank_uri(struct tab
*t
, struct karg
*args
)
1362 GtkClipboard
*clipboard
;
1364 if ((uri
= get_uri(t
)) == NULL
)
1367 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1368 gtk_clipboard_set_text(clipboard
, uri
, -1);
1374 paste_uri(struct tab
*t
, struct karg
*args
)
1376 GtkClipboard
*clipboard
;
1377 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
1379 gchar
*p
= NULL
, *uri
;
1381 /* try primary clipboard first */
1382 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1383 p
= gtk_clipboard_wait_for_text(clipboard
);
1385 /* if it failed get whatever text is in cut_buffer0 */
1386 if (p
== NULL
&& xterm_workaround
)
1387 if (gdk_property_get(gdk_get_default_root_window(),
1389 gdk_atom_intern("STRING", FALSE
),
1391 1024 * 1024 /* picked out of my butt */,
1397 /* yes sir, we need to NUL the string */
1403 while (*uri
&& isspace(*uri
))
1405 if (strlen(uri
) == 0) {
1406 show_oops(t
, "empty paste buffer");
1409 if (guess_search
== 0 && valid_url_type(uri
)) {
1410 /* we can be clever and paste this in search box */
1411 show_oops(t
, "not a valid URL");
1415 if (args
->i
== XT_PASTE_CURRENT_TAB
)
1417 else if (args
->i
== XT_PASTE_NEW_TAB
)
1418 create_new_tab(uri
, NULL
, 1, -1);
1429 js_toggle_cb(GtkWidget
*w
, struct tab
*t
)
1434 g_object_get(G_OBJECT(t
->settings
),
1435 "enable-scripts", &es
, (char *)NULL
);
1440 set
= XT_WL_DISABLE
;
1442 a
.i
= set
| XT_WL_TOPLEVEL
;
1445 a
.i
= set
| XT_WL_TOPLEVEL
;
1448 a
.i
= XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
;
1453 toggle_src(struct tab
*t
, struct karg
*args
)
1460 mode
= webkit_web_view_get_view_source_mode(t
->wv
);
1461 webkit_web_view_set_view_source_mode(t
->wv
, !mode
);
1462 webkit_web_view_reload(t
->wv
);
1468 focus_webview(struct tab
*t
)
1473 /* only grab focus if we are visible */
1474 if (gtk_notebook_get_current_page(notebook
) == t
->tab_id
)
1475 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
1479 focus(struct tab
*t
, struct karg
*args
)
1481 if (t
== NULL
|| args
== NULL
)
1487 if (args
->i
== XT_FOCUS_URI
)
1488 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
1489 else if (args
->i
== XT_FOCUS_SEARCH
)
1490 gtk_widget_grab_focus(GTK_WIDGET(t
->search_entry
));
1496 connect_socket_from_uri(const gchar
*uri
, const gchar
**error_str
, char *domain
,
1500 struct addrinfo hints
, *res
= NULL
, *ai
;
1501 int rv
= -1, s
= -1, on
, error
;
1503 static gchar myerror
[256]; /* this is not thread safe */
1506 *error_str
= myerror
;
1507 if (uri
&& !g_str_has_prefix(uri
, "https://")) {
1508 *error_str
= "invalid URI";
1512 su
= soup_uri_new(uri
);
1514 *error_str
= "invalid soup URI";
1517 if (!SOUP_URI_VALID_FOR_HTTP(su
)) {
1518 *error_str
= "invalid HTTPS URI";
1522 snprintf(port
, sizeof port
, "%d", su
->port
);
1523 bzero(&hints
, sizeof(struct addrinfo
));
1524 hints
.ai_flags
= AI_CANONNAME
;
1525 hints
.ai_family
= AF_UNSPEC
;
1526 hints
.ai_socktype
= SOCK_STREAM
;
1528 if ((error
= getaddrinfo(su
->host
, port
, &hints
, &res
))) {
1529 snprintf(myerror
, sizeof myerror
, "getaddrinfo failed: %s",
1530 gai_strerror(errno
));
1534 for (ai
= res
; ai
; ai
= ai
->ai_next
) {
1540 if (ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
)
1542 s
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
1545 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &on
,
1548 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) == 0)
1552 snprintf(myerror
, sizeof myerror
,
1553 "could not obtain certificates from: %s",
1559 strlcpy(domain
, su
->host
, domain_sz
);
1566 if (rv
== -1 && s
!= -1)
1573 stop_tls(gnutls_session_t gsession
, gnutls_certificate_credentials_t xcred
)
1576 gnutls_deinit(gsession
);
1578 gnutls_certificate_free_credentials(xcred
);
1584 start_tls(const gchar
**error_str
, int s
, gnutls_session_t
*gs
,
1585 gnutls_certificate_credentials_t
*xc
)
1587 gnutls_certificate_credentials_t xcred
;
1588 gnutls_session_t gsession
;
1590 static gchar myerror
[1024]; /* this is not thread safe */
1592 if (gs
== NULL
|| xc
== NULL
)
1599 gnutls_certificate_allocate_credentials(&xcred
);
1600 gnutls_certificate_set_x509_trust_file(xcred
, ssl_ca_file
,
1601 GNUTLS_X509_FMT_PEM
);
1603 gnutls_init(&gsession
, GNUTLS_CLIENT
);
1604 gnutls_priority_set_direct(gsession
, "PERFORMANCE", NULL
);
1605 gnutls_credentials_set(gsession
, GNUTLS_CRD_CERTIFICATE
, xcred
);
1606 gnutls_transport_set_ptr(gsession
, (gnutls_transport_ptr_t
)(long)s
);
1607 if ((rv
= gnutls_handshake(gsession
)) < 0) {
1608 snprintf(myerror
, sizeof myerror
,
1609 "gnutls_handshake failed %d fatal %d %s",
1611 gnutls_error_is_fatal(rv
),
1612 gnutls_strerror_name(rv
));
1613 stop_tls(gsession
, xcred
);
1617 gnutls_credentials_type_t cred
;
1618 cred
= gnutls_auth_get_type(gsession
);
1619 if (cred
!= GNUTLS_CRD_CERTIFICATE
) {
1620 snprintf(myerror
, sizeof myerror
,
1621 "gnutls_auth_get_type failed %d",
1623 stop_tls(gsession
, xcred
);
1631 *error_str
= myerror
;
1636 get_connection_certs(gnutls_session_t gsession
, gnutls_x509_crt_t
**certs
,
1640 const gnutls_datum_t
*cl
;
1641 gnutls_x509_crt_t
*all_certs
;
1644 if (certs
== NULL
|| cert_count
== NULL
)
1646 if (gnutls_certificate_type_get(gsession
) != GNUTLS_CRT_X509
)
1648 cl
= gnutls_certificate_get_peers(gsession
, &len
);
1652 all_certs
= g_malloc(sizeof(gnutls_x509_crt_t
) * len
);
1653 for (i
= 0; i
< len
; i
++) {
1654 gnutls_x509_crt_init(&all_certs
[i
]);
1655 if (gnutls_x509_crt_import(all_certs
[i
], &cl
[i
],
1656 GNUTLS_X509_FMT_PEM
< 0)) {
1670 free_connection_certs(gnutls_x509_crt_t
*certs
, size_t cert_count
)
1674 for (i
= 0; i
< cert_count
; i
++)
1675 gnutls_x509_crt_deinit(certs
[i
]);
1680 statusbar_modify_attr(struct tab
*t
, const char *text
, const char *base
)
1682 GdkColor c_text
, c_base
;
1684 gdk_color_parse(text
, &c_text
);
1685 gdk_color_parse(base
, &c_base
);
1687 gtk_widget_modify_text(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_text
);
1688 gtk_widget_modify_text(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_text
);
1689 gtk_widget_modify_text(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_text
);
1690 gtk_widget_modify_text(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_text
);
1692 gtk_widget_modify_base(t
->sbe
.statusbar
, GTK_STATE_NORMAL
, &c_base
);
1693 gtk_widget_modify_base(t
->sbe
.buffercmd
, GTK_STATE_NORMAL
, &c_base
);
1694 gtk_widget_modify_base(t
->sbe
.zoom
, GTK_STATE_NORMAL
, &c_base
);
1695 gtk_widget_modify_base(t
->sbe
.position
, GTK_STATE_NORMAL
, &c_base
);
1699 save_certs(struct tab
*t
, gnutls_x509_crt_t
*certs
,
1700 size_t cert_count
, char *domain
)
1703 char cert_buf
[64 * 1024], file
[PATH_MAX
];
1708 if (t
== NULL
|| certs
== NULL
|| cert_count
<= 0 || domain
== NULL
)
1711 snprintf(file
, sizeof file
, "%s/%s", certs_dir
, domain
);
1712 if ((f
= fopen(file
, "w")) == NULL
) {
1713 show_oops(t
, "Can't create cert file %s %s",
1714 file
, strerror(errno
));
1718 for (i
= 0; i
< cert_count
; i
++) {
1719 cert_buf_sz
= sizeof cert_buf
;
1720 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1721 cert_buf
, &cert_buf_sz
)) {
1722 show_oops(t
, "gnutls_x509_crt_export failed");
1725 if (fwrite(cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1726 show_oops(t
, "Can't write certs: %s", strerror(errno
));
1731 /* not the best spot but oh well */
1732 gdk_color_parse("lightblue", &color
);
1733 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
1734 statusbar_modify_attr(t
, XT_COLOR_BLACK
, "lightblue");
1747 load_compare_cert(const gchar
*uri
, const gchar
**error_str
)
1749 char domain
[8182], file
[PATH_MAX
];
1750 char cert_buf
[64 * 1024], r_cert_buf
[64 * 1024];
1752 unsigned int error
= 0;
1754 size_t cert_buf_sz
, cert_count
;
1755 enum cert_trust rv
= CERT_UNTRUSTED
;
1756 static gchar serr
[80]; /* this isn't thread safe */
1757 gnutls_session_t gsession
;
1758 gnutls_x509_crt_t
*certs
;
1759 gnutls_certificate_credentials_t xcred
;
1761 DNPRINTF(XT_D_URL
, "%s: %s\n", __func__
, uri
);
1765 if ((s
= connect_socket_from_uri(uri
, error_str
, domain
,
1766 sizeof domain
)) == -1)
1769 DNPRINTF(XT_D_URL
, "%s: fd %d\n", __func__
, s
);
1772 if (start_tls(error_str
, s
, &gsession
, &xcred
))
1774 DNPRINTF(XT_D_URL
, "%s: got tls\n", __func__
);
1776 /* verify certs in case cert file doesn't exist */
1777 if (gnutls_certificate_verify_peers2(gsession
, &error
) !=
1779 *error_str
= "Invalid certificates";
1784 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1785 *error_str
= "Can't get connection certificates";
1789 snprintf(file
, sizeof file
, "%s/%s", certs_dir
, domain
);
1790 if ((f
= fopen(file
, "r")) == NULL
) {
1796 for (i
= 0; i
< cert_count
; i
++) {
1797 cert_buf_sz
= sizeof cert_buf
;
1798 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
1799 cert_buf
, &cert_buf_sz
)) {
1802 if (fread(r_cert_buf
, cert_buf_sz
, 1, f
) != 1) {
1803 rv
= CERT_BAD
; /* critical */
1806 if (bcmp(r_cert_buf
, cert_buf
, sizeof cert_buf_sz
)) {
1807 rv
= CERT_BAD
; /* critical */
1816 free_connection_certs(certs
, cert_count
);
1818 /* we close the socket first for speed */
1822 /* only complain if we didn't save it locally */
1823 if (error
&& rv
!= CERT_LOCAL
) {
1824 strlcpy(serr
, "Certificate exception(s): ", sizeof serr
);
1825 if (error
& GNUTLS_CERT_INVALID
)
1826 strlcat(serr
, "invalid, ", sizeof serr
);
1827 if (error
& GNUTLS_CERT_REVOKED
)
1828 strlcat(serr
, "revoked, ", sizeof serr
);
1829 if (error
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
1830 strlcat(serr
, "signer not found, ", sizeof serr
);
1831 if (error
& GNUTLS_CERT_SIGNER_NOT_CA
)
1832 strlcat(serr
, "not signed by CA, ", sizeof serr
);
1833 if (error
& GNUTLS_CERT_INSECURE_ALGORITHM
)
1834 strlcat(serr
, "insecure algorithm, ", sizeof serr
);
1835 if (error
& GNUTLS_CERT_NOT_ACTIVATED
)
1836 strlcat(serr
, "not activated, ", sizeof serr
);
1837 if (error
& GNUTLS_CERT_EXPIRED
)
1838 strlcat(serr
, "expired, ", sizeof serr
);
1839 for (i
= strlen(serr
) - 1; i
> 0; i
--)
1840 if (serr
[i
] == ',') {
1847 stop_tls(gsession
, xcred
);
1853 cert_cmd(struct tab
*t
, struct karg
*args
)
1855 const gchar
*uri
, *error_str
= NULL
;
1859 gnutls_session_t gsession
;
1860 gnutls_x509_crt_t
*certs
;
1861 gnutls_certificate_credentials_t xcred
;
1866 if (ssl_ca_file
== NULL
) {
1867 show_oops(t
, "Can't open CA file: %s", ssl_ca_file
);
1871 if ((uri
= get_uri(t
)) == NULL
) {
1872 show_oops(t
, "Invalid URI");
1876 if ((s
= connect_socket_from_uri(uri
, &error_str
, domain
,
1877 sizeof domain
)) == -1) {
1878 show_oops(t
, "%s", error_str
);
1883 if (start_tls(&error_str
, s
, &gsession
, &xcred
))
1887 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1888 show_oops(t
, "get_connection_certs failed");
1892 if (args
->i
& XT_SHOW
)
1893 show_certs(t
, certs
, cert_count
, "Certificate Chain");
1894 else if (args
->i
& XT_SAVE
)
1895 save_certs(t
, certs
, cert_count
, domain
);
1897 free_connection_certs(certs
, cert_count
);
1899 /* we close the socket first for speed */
1902 stop_tls(gsession
, xcred
);
1903 if (error_str
&& strlen(error_str
))
1904 show_oops(t
, "%s", error_str
);
1909 remove_cookie(int index
)
1915 DNPRINTF(XT_D_COOKIE
, "remove_cookie: %d\n", index
);
1917 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
1919 for (i
= 1; cf
; cf
= cf
->next
, i
++) {
1923 print_cookie("remove cookie", c
);
1924 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
1929 soup_cookies_free(cf
);
1935 toplevel_cmd(struct tab
*t
, struct karg
*args
)
1937 js_toggle_cb(t
->js_toggle
, t
);
1943 can_go_back_for_real(struct tab
*t
)
1946 WebKitWebHistoryItem
*item
;
1952 /* rely on webkit to make sure we can go backward when on an about page */
1954 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
1955 return (webkit_web_view_can_go_back(t
->wv
));
1957 /* the back/forwars list is stupid so help determine if we can go back */
1958 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
1960 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
1961 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
1969 can_go_forward_for_real(struct tab
*t
)
1972 WebKitWebHistoryItem
*item
;
1978 /* rely on webkit to make sure we can go forward when on an about page */
1980 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
1981 return (webkit_web_view_can_go_forward(t
->wv
));
1983 /* the back/forwars list is stupid so help selecting a different item */
1984 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
1986 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
1987 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
1995 go_back_for_real(struct tab
*t
)
1998 WebKitWebHistoryItem
*item
;
2006 webkit_web_view_go_back(t
->wv
);
2009 /* the back/forwars list is stupid so help selecting a different item */
2010 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2012 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2013 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2014 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2021 go_forward_for_real(struct tab
*t
)
2024 WebKitWebHistoryItem
*item
;
2032 webkit_web_view_go_forward(t
->wv
);
2035 /* the back/forwars list is stupid so help selecting a different item */
2036 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2038 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2039 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2040 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2047 navaction(struct tab
*t
, struct karg
*args
)
2049 WebKitWebHistoryItem
*item
;
2050 WebKitWebFrame
*frame
;
2052 DNPRINTF(XT_D_NAV
, "navaction: tab %d opcode %d\n",
2053 t
->tab_id
, args
->i
);
2055 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
2057 if (args
->i
== XT_NAV_BACK
)
2058 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2060 item
= webkit_web_back_forward_list_get_forward_item(t
->bfl
);
2062 return (XT_CB_PASSTHROUGH
);
2063 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2065 return (XT_CB_PASSTHROUGH
);
2071 go_back_for_real(t
);
2073 case XT_NAV_FORWARD
:
2075 go_forward_for_real(t
);
2078 frame
= webkit_web_view_get_main_frame(t
->wv
);
2079 webkit_web_frame_reload(frame
);
2082 frame
= webkit_web_view_get_main_frame(t
->wv
);
2083 webkit_web_frame_stop_loading(frame
);
2086 return (XT_CB_PASSTHROUGH
);
2090 move(struct tab
*t
, struct karg
*args
)
2092 GtkAdjustment
*adjust
;
2093 double pi
, si
, pos
, ps
, upper
, lower
, max
;
2099 case XT_MOVE_BOTTOM
:
2101 case XT_MOVE_PAGEDOWN
:
2102 case XT_MOVE_PAGEUP
:
2103 case XT_MOVE_HALFDOWN
:
2104 case XT_MOVE_HALFUP
:
2105 case XT_MOVE_PERCENT
:
2106 case XT_MOVE_CENTER
:
2107 adjust
= t
->adjust_v
;
2110 adjust
= t
->adjust_h
;
2114 pos
= gtk_adjustment_get_value(adjust
);
2115 ps
= gtk_adjustment_get_page_size(adjust
);
2116 upper
= gtk_adjustment_get_upper(adjust
);
2117 lower
= gtk_adjustment_get_lower(adjust
);
2118 si
= gtk_adjustment_get_step_increment(adjust
);
2119 pi
= gtk_adjustment_get_page_increment(adjust
);
2122 DNPRINTF(XT_D_MOVE
, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2123 "max %f si %f pi %f\n",
2124 args
->i
, adjust
== t
->adjust_h
? "horizontal" : "vertical",
2125 pos
, ps
, upper
, lower
, max
, si
, pi
);
2131 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2136 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2138 case XT_MOVE_BOTTOM
:
2139 case XT_MOVE_FARRIGHT
:
2140 gtk_adjustment_set_value(adjust
, max
);
2143 case XT_MOVE_FARLEFT
:
2144 gtk_adjustment_set_value(adjust
, lower
);
2146 case XT_MOVE_PAGEDOWN
:
2148 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2150 case XT_MOVE_PAGEUP
:
2152 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2154 case XT_MOVE_HALFDOWN
:
2156 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2158 case XT_MOVE_HALFUP
:
2160 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2162 case XT_MOVE_CENTER
:
2163 args
->s
= g_strdup("50.0");
2165 case XT_MOVE_PERCENT
:
2166 percent
= atoi(args
->s
) / 100.0;
2167 pos
= max
* percent
;
2168 if (pos
< 0.0 || pos
> max
)
2170 gtk_adjustment_set_value(adjust
, pos
);
2173 return (XT_CB_PASSTHROUGH
);
2176 DNPRINTF(XT_D_MOVE
, "move: new pos %f %f\n", pos
, MIN(pos
, max
));
2178 return (XT_CB_HANDLED
);
2182 url_set_visibility(void)
2186 TAILQ_FOREACH(t
, &tabs
, entry
)
2187 if (show_url
== 0) {
2188 gtk_widget_hide(t
->toolbar
);
2191 gtk_widget_show(t
->toolbar
);
2195 notebook_tab_set_visibility(void)
2197 if (show_tabs
== 0) {
2198 gtk_widget_hide(tab_bar
);
2199 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2201 if (tab_style
== XT_TABS_NORMAL
) {
2202 gtk_widget_hide(tab_bar
);
2203 gtk_notebook_set_show_tabs(notebook
, TRUE
);
2204 } else if (tab_style
== XT_TABS_COMPACT
) {
2205 gtk_widget_show(tab_bar
);
2206 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2212 statusbar_set_visibility(void)
2216 TAILQ_FOREACH(t
, &tabs
, entry
){
2217 if (show_statusbar
== 0)
2218 gtk_widget_hide(t
->statusbar_box
);
2220 gtk_widget_show(t
->statusbar_box
);
2227 url_set(struct tab
*t
, int enable_url_entry
)
2232 show_url
= enable_url_entry
;
2234 if (enable_url_entry
) {
2235 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
2236 GTK_ENTRY_ICON_PRIMARY
, NULL
);
2237 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
), 0);
2239 pixbuf
= gtk_entry_get_icon_pixbuf(GTK_ENTRY(t
->uri_entry
),
2240 GTK_ENTRY_ICON_PRIMARY
);
2242 gtk_entry_get_progress_fraction(GTK_ENTRY(t
->uri_entry
));
2243 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
2244 GTK_ENTRY_ICON_PRIMARY
, pixbuf
);
2245 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
2251 fullscreen(struct tab
*t
, struct karg
*args
)
2253 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2256 return (XT_CB_PASSTHROUGH
);
2258 if (show_url
== 0) {
2266 url_set_visibility();
2267 notebook_tab_set_visibility();
2269 return (XT_CB_HANDLED
);
2273 statustoggle(struct tab
*t
, struct karg
*args
)
2275 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2277 if (show_statusbar
== 1) {
2279 statusbar_set_visibility();
2280 } else if (show_statusbar
== 0) {
2282 statusbar_set_visibility();
2284 return (XT_CB_HANDLED
);
2288 urlaction(struct tab
*t
, struct karg
*args
)
2290 int rv
= XT_CB_HANDLED
;
2292 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2295 return (XT_CB_PASSTHROUGH
);
2299 if (show_url
== 0) {
2301 url_set_visibility();
2305 if (show_url
== 1) {
2307 url_set_visibility();
2315 tabaction(struct tab
*t
, struct karg
*args
)
2317 int rv
= XT_CB_HANDLED
;
2318 char *url
= args
->s
;
2322 DNPRINTF(XT_D_TAB
, "tabaction: %p %d\n", t
, args
->i
);
2325 return (XT_CB_PASSTHROUGH
);
2329 if (strlen(url
) > 0)
2330 create_new_tab(url
, NULL
, 1, args
->precount
);
2332 create_new_tab(NULL
, NULL
, 1, args
->precount
);
2335 if (args
->precount
< 0)
2338 TAILQ_FOREACH(tt
, &tabs
, entry
)
2339 if (tt
->tab_id
== args
->precount
- 1) {
2344 case XT_TAB_DELQUIT
:
2345 if (gtk_notebook_get_n_pages(notebook
) > 1)
2351 if (strlen(url
) > 0)
2354 rv
= XT_CB_PASSTHROUGH
;
2360 if (show_tabs
== 0) {
2362 notebook_tab_set_visibility();
2366 if (show_tabs
== 1) {
2368 notebook_tab_set_visibility();
2371 case XT_TAB_NEXTSTYLE
:
2372 if (tab_style
== XT_TABS_NORMAL
) {
2373 tab_style
= XT_TABS_COMPACT
;
2374 recolor_compact_tabs();
2377 tab_style
= XT_TABS_NORMAL
;
2378 notebook_tab_set_visibility();
2380 case XT_TAB_UNDO_CLOSE
:
2381 if (undo_count
== 0) {
2382 DNPRINTF(XT_D_TAB
, "%s: no tabs to undo close",
2387 u
= TAILQ_FIRST(&undos
);
2388 create_new_tab(u
->uri
, u
, 1, -1);
2390 TAILQ_REMOVE(&undos
, u
, entry
);
2392 /* u->history is freed in create_new_tab() */
2397 rv
= XT_CB_PASSTHROUGH
;
2411 resizetab(struct tab
*t
, struct karg
*args
)
2413 if (t
== NULL
|| args
== NULL
) {
2414 show_oops(NULL
, "resizetab invalid parameters");
2415 return (XT_CB_PASSTHROUGH
);
2418 DNPRINTF(XT_D_TAB
, "resizetab: tab %d %d\n",
2419 t
->tab_id
, args
->i
);
2421 setzoom_webkit(t
, args
->i
);
2423 return (XT_CB_HANDLED
);
2427 movetab(struct tab
*t
, struct karg
*args
)
2431 if (t
== NULL
|| args
== NULL
) {
2432 show_oops(NULL
, "movetab invalid parameters");
2433 return (XT_CB_PASSTHROUGH
);
2436 DNPRINTF(XT_D_TAB
, "movetab: tab %d opcode %d\n",
2437 t
->tab_id
, args
->i
);
2439 if (args
->i
>= XT_TAB_INVALID
)
2440 return (XT_CB_PASSTHROUGH
);
2442 if (TAILQ_EMPTY(&tabs
))
2443 return (XT_CB_PASSTHROUGH
);
2445 n
= gtk_notebook_get_n_pages(notebook
);
2446 dest
= gtk_notebook_get_current_page(notebook
);
2450 if (args
->precount
< 0)
2451 dest
= dest
== n
- 1 ? 0 : dest
+ 1;
2453 dest
= args
->precount
- 1;
2457 if (args
->precount
< 0)
2460 dest
-= args
->precount
% n
;
2473 return (XT_CB_PASSTHROUGH
);
2476 if (dest
< 0 || dest
>= n
)
2477 return (XT_CB_PASSTHROUGH
);
2478 if (t
->tab_id
== dest
) {
2479 DNPRINTF(XT_D_TAB
, "movetab: do nothing\n");
2480 return (XT_CB_HANDLED
);
2483 set_current_tab(dest
);
2485 return (XT_CB_HANDLED
);
2492 command(struct tab
*t
, struct karg
*args
)
2494 char *s
= NULL
, *ss
= NULL
;
2499 if (t
== NULL
|| args
== NULL
) {
2500 show_oops(NULL
, "command invalid parameters");
2501 return (XT_CB_PASSTHROUGH
);
2512 if (cmd_prefix
== 0)
2515 ss
= g_strdup_printf(":%d", cmd_prefix
);
2521 t
->mode
= XT_MODE_HINT
;
2522 bzero(&a
, sizeof a
);
2528 t
->mode
= XT_MODE_HINT
;
2529 bzero(&a
, sizeof a
);
2530 a
.i
= XT_HINT_NEWTAB
;
2540 case XT_CMD_OPEN_CURRENT
:
2543 case XT_CMD_TABNEW_CURRENT
:
2544 if (!s
) /* FALL THROUGH? */
2546 if ((uri
= get_uri(t
)) != NULL
) {
2547 ss
= g_strdup_printf("%s%s", s
, uri
);
2552 show_oops(t
, "command: invalid opcode %d", args
->i
);
2553 return (XT_CB_PASSTHROUGH
);
2556 DNPRINTF(XT_D_CMD
, "%s: tab %d type %s\n", __func__
, t
->tab_id
, s
);
2558 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), s
);
2559 gdk_color_parse(XT_COLOR_WHITE
, &color
);
2560 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
2562 gtk_widget_grab_focus(GTK_WIDGET(t
->cmd
));
2563 gtk_editable_set_position(GTK_EDITABLE(t
->cmd
), -1);
2568 return (XT_CB_HANDLED
);
2572 search(struct tab
*t
, struct karg
*args
)
2576 if (t
== NULL
|| args
== NULL
) {
2577 show_oops(NULL
, "search invalid parameters");
2582 case XT_SEARCH_NEXT
:
2583 d
= t
->search_forward
;
2585 case XT_SEARCH_PREV
:
2586 d
= !t
->search_forward
;
2589 return (XT_CB_PASSTHROUGH
);
2592 if (t
->search_text
== NULL
) {
2593 if (global_search
== NULL
)
2594 return (XT_CB_PASSTHROUGH
);
2596 d
= t
->search_forward
= TRUE
;
2597 t
->search_text
= g_strdup(global_search
);
2598 webkit_web_view_mark_text_matches(t
->wv
, global_search
, FALSE
, 0);
2599 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
2603 DNPRINTF(XT_D_CMD
, "search: tab %d opc %d forw %d text %s\n",
2604 t
->tab_id
, args
->i
, t
->search_forward
, t
->search_text
);
2606 webkit_web_view_search_text(t
->wv
, t
->search_text
, FALSE
, d
, TRUE
);
2608 return (XT_CB_HANDLED
);
2612 session_save(struct tab
*t
, char *filename
)
2618 if (strlen(filename
) == 0)
2621 if (filename
[0] == '.' || filename
[0] == '/')
2625 if (save_tabs(t
, &a
))
2627 strlcpy(named_session
, filename
, sizeof named_session
);
2629 /* add the new session to the list of sessions */
2630 s
= g_malloc(sizeof(struct session
));
2631 s
->name
= g_strdup(filename
);
2632 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
2640 session_open(struct tab
*t
, char *filename
)
2645 if (strlen(filename
) == 0)
2648 if (filename
[0] == '.' || filename
[0] == '/')
2652 a
.i
= XT_SES_CLOSETABS
;
2653 if (open_tabs(t
, &a
))
2656 strlcpy(named_session
, filename
, sizeof named_session
);
2664 session_delete(struct tab
*t
, char *filename
)
2666 char file
[PATH_MAX
];
2670 if (strlen(filename
) == 0)
2673 if (filename
[0] == '.' || filename
[0] == '/')
2676 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, filename
);
2680 if (!strcmp(filename
, named_session
))
2681 strlcpy(named_session
, XT_SAVED_TABS_FILE
,
2682 sizeof named_session
);
2684 /* remove session from sessions list */
2685 TAILQ_FOREACH(s
, &sessions
, entry
) {
2686 if (!strcmp(s
->name
, filename
))
2691 TAILQ_REMOVE(&sessions
, s
, entry
);
2692 g_free((gpointer
) s
->name
);
2701 session_cmd(struct tab
*t
, struct karg
*args
)
2703 char *filename
= args
->s
;
2708 if (args
->i
& XT_SHOW
)
2709 show_oops(t
, "Current session: %s", named_session
[0] == '\0' ?
2710 XT_SAVED_TABS_FILE
: named_session
);
2711 else if (args
->i
& XT_SAVE
) {
2712 if (session_save(t
, filename
)) {
2713 show_oops(t
, "Can't save session: %s",
2714 filename
? filename
: "INVALID");
2717 } else if (args
->i
& XT_OPEN
) {
2718 if (session_open(t
, filename
)) {
2719 show_oops(t
, "Can't open session: %s",
2720 filename
? filename
: "INVALID");
2723 } else if (args
->i
& XT_DELETE
) {
2724 if (session_delete(t
, filename
)) {
2725 show_oops(t
, "Can't delete session: %s",
2726 filename
? filename
: "INVALID");
2731 return (XT_CB_PASSTHROUGH
);
2735 script_cmd(struct tab
*t
, struct karg
*args
)
2737 JSGlobalContextRef ctx
;
2738 WebKitWebFrame
*frame
;
2740 JSValueRef val
, exception
;
2749 if ((f
= fopen(args
->s
, "r")) == NULL
) {
2750 show_oops(t
, "Can't open script file: %s", args
->s
);
2754 if (fstat(fileno(f
), &sb
) == -1) {
2755 show_oops(t
, "Can't stat script file: %s", args
->s
);
2759 buf
= g_malloc0(sb
.st_size
+ 1);
2760 if (fread(buf
, 1, sb
.st_size
, f
) != sb
.st_size
) {
2761 show_oops(t
, "Can't read script file: %s", args
->s
);
2765 /* this code needs to be redone */
2766 frame
= webkit_web_view_get_main_frame(t
->wv
);
2767 ctx
= webkit_web_frame_get_global_context(frame
);
2769 str
= JSStringCreateWithUTF8CString(buf
);
2770 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
2771 NULL
, 0, &exception
);
2772 JSStringRelease(str
);
2774 DNPRINTF(XT_D_JS
, "%s: val %p\n", __func__
, val
);
2776 es
= js_ref_to_string(ctx
, exception
);
2778 show_oops(t
, "script exception: %s", es
);
2783 es
= js_ref_to_string(ctx
, val
);
2786 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
2788 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
2792 show_oops(t
, "script complete return value: '%s'", es
);
2795 show_oops(t
, "script complete: without a return value");
2804 return (XT_CB_PASSTHROUGH
);
2808 * Make a hardcopy of the page
2811 print_page(struct tab
*t
, struct karg
*args
)
2813 WebKitWebFrame
*frame
;
2815 GtkPrintOperation
*op
;
2816 GtkPrintOperationAction action
;
2817 GtkPrintOperationResult print_res
;
2818 GError
*g_err
= NULL
;
2819 int marg_l
, marg_r
, marg_t
, marg_b
;
2821 DNPRINTF(XT_D_PRINTING
, "%s:", __func__
);
2823 ps
= gtk_page_setup_new();
2824 op
= gtk_print_operation_new();
2825 action
= GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
;
2826 frame
= webkit_web_view_get_main_frame(t
->wv
);
2828 /* the default margins are too small, so we will bump them */
2829 marg_l
= gtk_page_setup_get_left_margin(ps
, GTK_UNIT_MM
) +
2830 XT_PRINT_EXTRA_MARGIN
;
2831 marg_r
= gtk_page_setup_get_right_margin(ps
, GTK_UNIT_MM
) +
2832 XT_PRINT_EXTRA_MARGIN
;
2833 marg_t
= gtk_page_setup_get_top_margin(ps
, GTK_UNIT_MM
) +
2834 XT_PRINT_EXTRA_MARGIN
;
2835 marg_b
= gtk_page_setup_get_bottom_margin(ps
, GTK_UNIT_MM
) +
2836 XT_PRINT_EXTRA_MARGIN
;
2839 gtk_page_setup_set_left_margin(ps
, marg_l
, GTK_UNIT_MM
);
2840 gtk_page_setup_set_right_margin(ps
, marg_r
, GTK_UNIT_MM
);
2841 gtk_page_setup_set_top_margin(ps
, marg_t
, GTK_UNIT_MM
);
2842 gtk_page_setup_set_bottom_margin(ps
, marg_b
, GTK_UNIT_MM
);
2844 gtk_print_operation_set_default_page_setup(op
, ps
);
2846 /* this appears to free 'op' and 'ps' */
2847 print_res
= webkit_web_frame_print_full(frame
, op
, action
, &g_err
);
2849 /* check it worked */
2850 if (print_res
== GTK_PRINT_OPERATION_RESULT_ERROR
) {
2851 show_oops(NULL
, "can't print: %s", g_err
->message
);
2852 g_error_free (g_err
);
2860 go_home(struct tab
*t
, struct karg
*args
)
2867 set_encoding(struct tab
*t
, struct karg
*args
)
2871 if (args
->s
&& strlen(g_strstrip(args
->s
)) == 0) {
2872 e
= webkit_web_view_get_custom_encoding(t
->wv
);
2874 e
= webkit_web_view_get_encoding(t
->wv
);
2875 show_oops(t
, "encoding: %s", e
? e
: "N/A");
2877 webkit_web_view_set_custom_encoding(t
->wv
, args
->s
);
2883 restart(struct tab
*t
, struct karg
*args
)
2887 a
.s
= XT_RESTART_TABS_FILE
;
2889 execvp(start_argv
[0], start_argv
);
2895 char *http_proxy_save
; /* not a setting, used to toggle */
2898 proxy_cmd(struct tab
*t
, struct karg
*args
)
2900 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
2907 if (http_proxy_save
)
2908 g_free(http_proxy_save
);
2909 http_proxy_save
= g_strdup(http_proxy
);
2912 if (args
->i
& XT_PRXY_SHOW
) {
2914 show_oops(t
, "http_proxy = %s", http_proxy
);
2916 show_oops(t
, "proxy is currently disabled");
2917 } else if (args
->i
& XT_PRXY_TOGGLE
) {
2918 if (http_proxy_save
== NULL
&& http_proxy
== NULL
) {
2919 show_oops(t
, "can't toggle proxy");
2925 setup_proxy(http_proxy_save
);
2928 return (XT_CB_PASSTHROUGH
);
2933 int (*func
)(struct tab
*, struct karg
*);
2937 { "command_mode", 0, command_mode
, XT_MODE_COMMAND
, 0 },
2938 { "insert_mode", 0, command_mode
, XT_MODE_INSERT
, 0 },
2939 { "command", 0, command
, ':', 0 },
2940 { "search", 0, command
, '/', 0 },
2941 { "searchb", 0, command
, '?', 0 },
2942 { "hinting", 0, command
, '.', 0 },
2943 { "hinting_newtab", 0, command
, ',', 0 },
2944 { "togglesrc", 0, toggle_src
, 0, 0 },
2946 /* yanking and pasting */
2947 { "yankuri", 0, yank_uri
, 0, 0 },
2948 /* XXX: pasteuri{cur,new} do not work from the cmd_entry? */
2949 { "pasteuricur", 0, paste_uri
, XT_PASTE_CURRENT_TAB
, 0 },
2950 { "pasteurinew", 0, paste_uri
, XT_PASTE_NEW_TAB
, 0 },
2953 { "searchnext", 0, search
, XT_SEARCH_NEXT
, 0 },
2954 { "searchprevious", 0, search
, XT_SEARCH_PREV
, 0 },
2957 { "focusaddress", 0, focus
, XT_FOCUS_URI
, 0 },
2958 { "focussearch", 0, focus
, XT_FOCUS_SEARCH
, 0 },
2961 { "hinting", 0, hint
, 0, 0 },
2962 { "hinting_newtab", 0, hint
, XT_HINT_NEWTAB
, 0 },
2964 /* custom stylesheet */
2965 { "userstyle", 0, userstyle
, 0, 0 },
2968 { "goback", 0, navaction
, XT_NAV_BACK
, 0 },
2969 { "goforward", 0, navaction
, XT_NAV_FORWARD
, 0 },
2970 { "reload", 0, navaction
, XT_NAV_RELOAD
, 0 },
2971 { "stop", 0, navaction
, XT_NAV_STOP
, 0 },
2973 /* vertical movement */
2974 { "scrolldown", 0, move
, XT_MOVE_DOWN
, 0 },
2975 { "scrollup", 0, move
, XT_MOVE_UP
, 0 },
2976 { "scrollbottom", 0, move
, XT_MOVE_BOTTOM
, 0 },
2977 { "scrolltop", 0, move
, XT_MOVE_TOP
, 0 },
2978 { "1", 0, move
, XT_MOVE_TOP
, 0 },
2979 { "scrollhalfdown", 0, move
, XT_MOVE_HALFDOWN
, 0 },
2980 { "scrollhalfup", 0, move
, XT_MOVE_HALFUP
, 0 },
2981 { "scrollpagedown", 0, move
, XT_MOVE_PAGEDOWN
, 0 },
2982 { "scrollpageup", 0, move
, XT_MOVE_PAGEUP
, 0 },
2983 /* horizontal movement */
2984 { "scrollright", 0, move
, XT_MOVE_RIGHT
, 0 },
2985 { "scrollleft", 0, move
, XT_MOVE_LEFT
, 0 },
2986 { "scrollfarright", 0, move
, XT_MOVE_FARRIGHT
, 0 },
2987 { "scrollfarleft", 0, move
, XT_MOVE_FARLEFT
, 0 },
2989 { "favorites", 0, xtp_page_fl
, 0, 0 },
2990 { "fav", 0, xtp_page_fl
, 0, 0 },
2991 { "favadd", 0, add_favorite
, 0, 0 },
2993 { "qall", 0, quit
, 0, 0 },
2994 { "quitall", 0, quit
, 0, 0 },
2995 { "w", 0, save_tabs
, 0, 0 },
2996 { "wq", 0, save_tabs_and_quit
, 0, 0 },
2997 { "help", 0, help
, 0, 0 },
2998 { "about", 0, about
, 0, 0 },
2999 { "stats", 0, stats
, 0, 0 },
3000 { "version", 0, about
, 0, 0 },
3003 { "js", 0, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3004 { "save", 1, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3005 { "domain", 2, js_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3006 { "fqdn", 2, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3007 { "show", 1, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3008 { "all", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3009 { "persistent", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3010 { "session", 2, js_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3011 { "toggle", 1, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3012 { "domain", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3013 { "fqdn", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3015 /* cookie command */
3016 { "cookie", 0, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3017 { "save", 1, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3018 { "domain", 2, cookie_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3019 { "fqdn", 2, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3020 { "show", 1, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3021 { "all", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3022 { "persistent", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3023 { "session", 2, cookie_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3024 { "toggle", 1, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3025 { "domain", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3026 { "fqdn", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3028 /* plugin command */
3029 { "plugin", 0, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3030 { "save", 1, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3031 { "domain", 2, pl_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3032 { "fqdn", 2, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3033 { "show", 1, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3034 { "all", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3035 { "persistent", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3036 { "session", 2, pl_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3037 { "toggle", 1, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3038 { "domain", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3039 { "fqdn", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3041 /* toplevel (domain) command */
3042 { "toplevel", 0, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3043 { "toggle", 1, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3046 { "cookiejar", 0, xtp_page_cl
, 0, 0 },
3049 { "cert", 0, cert_cmd
, XT_SHOW
, 0 },
3050 { "save", 1, cert_cmd
, XT_SAVE
, 0 },
3051 { "show", 1, cert_cmd
, XT_SHOW
, 0 },
3053 { "ca", 0, ca_cmd
, 0, 0 },
3054 { "downloadmgr", 0, xtp_page_dl
, 0, 0 },
3055 { "dl", 0, xtp_page_dl
, 0, 0 },
3056 { "h", 0, xtp_page_hl
, 0, 0 },
3057 { "history", 0, xtp_page_hl
, 0, 0 },
3058 { "home", 0, go_home
, 0, 0 },
3059 { "restart", 0, restart
, 0, 0 },
3060 { "urlhide", 0, urlaction
, XT_URL_HIDE
, 0 },
3061 { "urlshow", 0, urlaction
, XT_URL_SHOW
, 0 },
3062 { "statustoggle", 0, statustoggle
, 0, 0 },
3063 { "run_script", 0, run_page_script
, 0, XT_USERARG
},
3065 { "print", 0, print_page
, 0, 0 },
3068 { "focusin", 0, resizetab
, XT_ZOOM_IN
, 0 },
3069 { "focusout", 0, resizetab
, XT_ZOOM_OUT
, 0 },
3070 { "focusreset", 0, resizetab
, XT_ZOOM_NORMAL
, 0 },
3071 { "q", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3072 { "quit", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3073 { "open", 0, tabaction
, XT_TAB_OPEN
, XT_URLARG
},
3074 { "tabclose", 0, tabaction
, XT_TAB_DELETE
, XT_PREFIX
| XT_INTARG
},
3075 { "tabedit", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3076 { "tabfirst", 0, movetab
, XT_TAB_FIRST
, 0 },
3077 { "tabhide", 0, tabaction
, XT_TAB_HIDE
, 0 },
3078 { "tablast", 0, movetab
, XT_TAB_LAST
, 0 },
3079 { "tabnew", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3080 { "tabnext", 0, movetab
, XT_TAB_NEXT
, XT_PREFIX
| XT_INTARG
},
3081 { "tabnextstyle", 0, tabaction
, XT_TAB_NEXTSTYLE
, 0 },
3082 { "tabprevious", 0, movetab
, XT_TAB_PREV
, XT_PREFIX
| XT_INTARG
},
3083 { "tabrewind", 0, movetab
, XT_TAB_FIRST
, 0 },
3084 { "tabshow", 0, tabaction
, XT_TAB_SHOW
, 0 },
3085 { "tabs", 0, buffers
, 0, 0 },
3086 { "tabundoclose", 0, tabaction
, XT_TAB_UNDO_CLOSE
, 0 },
3087 { "buffers", 0, buffers
, 0, 0 },
3088 { "ls", 0, buffers
, 0, 0 },
3089 { "encoding", 0, set_encoding
, 0, XT_USERARG
},
3091 /* command aliases (handy when -S flag is used) */
3092 { "promptopen", 0, command
, XT_CMD_OPEN
, 0 },
3093 { "promptopencurrent", 0, command
, XT_CMD_OPEN_CURRENT
, 0 },
3094 { "prompttabnew", 0, command
, XT_CMD_TABNEW
, 0 },
3095 { "prompttabnewcurrent",0, command
, XT_CMD_TABNEW_CURRENT
, 0 },
3098 { "set", 0, set
, 0, XT_SETARG
},
3100 { "fullscreen", 0, fullscreen
, 0, 0 },
3101 { "f", 0, fullscreen
, 0, 0 },
3104 { "session", 0, session_cmd
, XT_SHOW
, 0 },
3105 { "delete", 1, session_cmd
, XT_DELETE
, XT_SESSARG
},
3106 { "open", 1, session_cmd
, XT_OPEN
, XT_SESSARG
},
3107 { "save", 1, session_cmd
, XT_SAVE
, XT_USERARG
},
3108 { "show", 1, session_cmd
, XT_SHOW
, 0 },
3110 /* external javascript */
3111 { "script", 0, script_cmd
, XT_EJS_SHOW
, XT_USERARG
},
3114 { "inspector", 0, inspector_cmd
, XT_INS_SHOW
, 0 },
3115 { "show", 1, inspector_cmd
, XT_INS_SHOW
, 0 },
3116 { "hide", 1, inspector_cmd
, XT_INS_HIDE
, 0 },
3119 { "proxy", 0, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3120 { "show", 1, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3121 { "toggle", 1, proxy_cmd
, XT_PRXY_TOGGLE
, 0 },
3128 } cmd_status
= {-1, 0};
3131 wv_release_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3134 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 1)
3141 wv_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3144 WebKitHitTestResult
*hit_test_result
;
3147 hit_test_result
= webkit_web_view_get_hit_test_result(t
->wv
, e
);
3148 g_object_get(hit_test_result
, "context", &context
, NULL
);
3153 if (context
& WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE
)
3154 t
->mode
= XT_MODE_INSERT
;
3156 t
->mode
= XT_MODE_COMMAND
;
3158 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3160 else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 8 /* btn 4 */) {
3166 } else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 9 /* btn 5 */) {
3168 a
.i
= XT_NAV_FORWARD
;
3178 tab_close_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3180 DNPRINTF(XT_D_TAB
, "tab_close_cb: tab %d\n", t
->tab_id
);
3182 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3191 activate_uri_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3193 const gchar
*uri
= gtk_entry_get_text(GTK_ENTRY(entry
));
3195 DNPRINTF(XT_D_URL
, "activate_uri_entry_cb: %s\n", uri
);
3198 show_oops(NULL
, "activate_uri_entry_cb invalid parameters");
3203 show_oops(t
, "activate_uri_entry_cb no uri");
3207 uri
+= strspn(uri
, "\t ");
3209 /* if xxxt:// treat specially */
3210 if (parse_xtp_url(t
, uri
))
3213 /* otherwise continue to load page normally */
3214 load_uri(t
, (gchar
*)uri
);
3219 activate_search_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3221 const gchar
*search
= gtk_entry_get_text(GTK_ENTRY(entry
));
3222 char *newuri
= NULL
;
3225 DNPRINTF(XT_D_URL
, "activate_search_entry_cb: %s\n", search
);
3228 show_oops(NULL
, "activate_search_entry_cb invalid parameters");
3232 if (search_string
== NULL
) {
3233 show_oops(t
, "no search_string");
3237 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
3239 enc_search
= soup_uri_encode(search
, XT_RESERVED_CHARS
);
3240 newuri
= g_strdup_printf(search_string
, enc_search
);
3244 webkit_web_view_load_uri(t
->wv
, newuri
);
3252 check_and_set_cookie(const gchar
*uri
, struct tab
*t
)
3254 struct domain
*d
= NULL
;
3257 if (uri
== NULL
|| t
== NULL
)
3260 if ((d
= wl_find_uri(uri
, &c_wl
)) == NULL
)
3265 DNPRINTF(XT_D_COOKIE
, "check_and_set_cookie: %s %s\n",
3266 es
? "enable" : "disable", uri
);
3268 g_object_set(G_OBJECT(t
->settings
),
3269 "enable-html5-local-storage", es
, (char *)NULL
);
3270 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3274 check_and_set_js(const gchar
*uri
, struct tab
*t
)
3276 struct domain
*d
= NULL
;
3279 if (uri
== NULL
|| t
== NULL
)
3282 if ((d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3287 DNPRINTF(XT_D_JS
, "check_and_set_js: %s %s\n",
3288 es
? "enable" : "disable", uri
);
3290 g_object_set(G_OBJECT(t
->settings
),
3291 "enable-scripts", es
, (char *)NULL
);
3292 g_object_set(G_OBJECT(t
->settings
),
3293 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
3294 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3296 button_set_stockid(t
->js_toggle
,
3297 es
? GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
);
3301 check_and_set_pl(const gchar
*uri
, struct tab
*t
)
3303 struct domain
*d
= NULL
;
3306 if (uri
== NULL
|| t
== NULL
)
3309 if ((d
= wl_find_uri(uri
, &pl_wl
)) == NULL
)
3314 DNPRINTF(XT_D_JS
, "check_and_set_pl: %s %s\n",
3315 es
? "enable" : "disable", uri
);
3317 g_object_set(G_OBJECT(t
->settings
),
3318 "enable-plugins", es
, (char *)NULL
);
3319 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3323 color_address_bar(gpointer p
)
3326 struct tab
*tt
, *t
= p
;
3327 gchar
*col_str
= XT_COLOR_WHITE
;
3328 const gchar
*uri
, *u
= NULL
, *error_str
= NULL
;
3331 gdk_threads_enter();
3333 DNPRINTF(XT_D_URL
, "%s:\n", __func__
);
3335 /* make sure t still exists */
3338 TAILQ_FOREACH(tt
, &tabs
, entry
)
3344 if ((uri
= get_uri(t
)) == NULL
)
3349 gdk_threads_leave();
3352 col_str
= XT_COLOR_YELLOW
;
3353 switch (load_compare_cert(u
, &error_str
)) {
3355 col_str
= XT_COLOR_BLUE
;
3358 col_str
= XT_COLOR_GREEN
;
3360 case CERT_UNTRUSTED
:
3361 col_str
= XT_COLOR_YELLOW
;
3364 col_str
= XT_COLOR_RED
;
3369 gdk_threads_enter();
3371 /* make sure t isn't deleted */
3372 TAILQ_FOREACH(tt
, &tabs
, entry
)
3379 /* test to see if the user navigated away and canceled the thread */
3380 if (t
->thread
!= g_thread_self())
3382 if ((uri
= get_uri(t
)) == NULL
) {
3386 if (strcmp(uri
, u
)) {
3387 /* make sure we are still the same url */
3393 gdk_color_parse(col_str
, &color
);
3394 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3396 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3397 statusbar_modify_attr(t
, col_str
, XT_COLOR_BLACK
);
3399 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3401 if (error_str
&& error_str
[0] != '\0')
3402 show_oops(t
, "%s", error_str
);
3407 /* t is invalid at this point */
3409 g_free((gpointer
)u
);
3411 gdk_threads_leave();
3416 show_ca_status(struct tab
*t
, const char *uri
)
3419 gchar
*col_str
= XT_COLOR_WHITE
;
3421 DNPRINTF(XT_D_URL
, "show_ca_status: %d %s %s\n",
3422 ssl_strict_certs
, ssl_ca_file
, uri
);
3429 if (ssl_ca_file
== NULL
) {
3430 if (g_str_has_prefix(uri
, "http://"))
3432 if (g_str_has_prefix(uri
, "https://")) {
3433 col_str
= XT_COLOR_RED
;
3438 if (g_str_has_prefix(uri
, "http://") ||
3439 !g_str_has_prefix(uri
, "https://"))
3443 * It is not necessary to see if the thread is already running.
3444 * If the thread is in progress setting it to something else aborts it
3448 /* thread the coloring of the address bar */
3449 t
->thread
= g_thread_create((GThreadFunc
)color_address_bar
, t
, TRUE
, NULL
);
3451 color_address_bar(t
);
3457 gdk_color_parse(col_str
, &color
);
3458 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3460 if (!strcmp(col_str
, XT_COLOR_WHITE
))
3461 statusbar_modify_attr(t
, col_str
, XT_COLOR_BLACK
);
3463 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3468 free_favicon(struct tab
*t
)
3470 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p req %p\n",
3471 __func__
, t
->icon_download
, t
->icon_request
);
3473 if (t
->icon_request
)
3474 g_object_unref(t
->icon_request
);
3475 if (t
->icon_dest_uri
)
3476 g_free(t
->icon_dest_uri
);
3478 t
->icon_request
= NULL
;
3479 t
->icon_dest_uri
= NULL
;
3483 xt_icon_from_name(struct tab
*t
, gchar
*name
)
3485 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->uri_entry
),
3486 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3488 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3489 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3491 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3492 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3496 xt_icon_from_pixbuf(struct tab
*t
, GdkPixbuf
*pb
)
3498 GdkPixbuf
*pb_scaled
;
3500 if (gdk_pixbuf_get_width(pb
) > 16 || gdk_pixbuf_get_height(pb
) > 16)
3501 pb_scaled
= gdk_pixbuf_scale_simple(pb
, 16, 16,
3502 GDK_INTERP_BILINEAR
);
3506 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
3507 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3509 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
3510 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3512 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3513 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3515 if (pb_scaled
!= pb
)
3516 g_object_unref(pb_scaled
);
3520 xt_icon_from_file(struct tab
*t
, char *file
)
3524 if (g_str_has_prefix(file
, "file://"))
3525 file
+= strlen("file://");
3527 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
3529 xt_icon_from_pixbuf(t
, pb
);
3532 xt_icon_from_name(t
, "text-html");
3536 is_valid_icon(char *file
)
3539 const char *mime_type
;
3543 gf
= g_file_new_for_path(file
);
3544 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
3546 mime_type
= g_file_info_get_content_type(fi
);
3547 valid
= g_strcmp0(mime_type
, "image/x-ico") == 0 ||
3548 g_strcmp0(mime_type
, "image/vnd.microsoft.icon") == 0 ||
3549 g_strcmp0(mime_type
, "image/png") == 0 ||
3550 g_strcmp0(mime_type
, "image/gif") == 0 ||
3551 g_strcmp0(mime_type
, "application/octet-stream") == 0;
3559 set_favicon_from_file(struct tab
*t
, char *file
)
3563 if (t
== NULL
|| file
== NULL
)
3566 if (g_str_has_prefix(file
, "file://"))
3567 file
+= strlen("file://");
3568 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading %s\n", __func__
, file
);
3570 if (!stat(file
, &sb
)) {
3571 if (sb
.st_size
== 0 || !is_valid_icon(file
)) {
3572 /* corrupt icon so trash it */
3573 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3576 /* no need to set icon to default here */
3580 xt_icon_from_file(t
, file
);
3584 favicon_download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
3587 WebKitDownloadStatus status
= webkit_download_get_status(download
);
3588 struct tab
*tt
= NULL
, *t
= NULL
;
3591 * find the webview instead of passing in the tab as it could have been
3592 * deleted from underneath us.
3594 TAILQ_FOREACH(tt
, &tabs
, entry
) {
3603 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d status %d\n",
3604 __func__
, t
->tab_id
, status
);
3607 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
3609 t
->icon_download
= NULL
;
3612 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
3615 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
3618 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
3620 DNPRINTF(XT_D_DOWNLOAD
, "%s: freeing favicon %d\n",
3621 __func__
, t
->tab_id
);
3622 t
->icon_download
= NULL
;
3625 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
3628 DNPRINTF(XT_D_DOWNLOAD
, "%s: setting icon to %s\n",
3629 __func__
, t
->icon_dest_uri
);
3630 set_favicon_from_file(t
, t
->icon_dest_uri
);
3631 /* these will be freed post callback */
3632 t
->icon_request
= NULL
;
3633 t
->icon_download
= NULL
;
3641 abort_favicon_download(struct tab
*t
)
3643 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p\n", __func__
, t
->icon_download
);
3645 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
3646 if (t
->icon_download
) {
3647 g_signal_handlers_disconnect_by_func(G_OBJECT(t
->icon_download
),
3648 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3649 webkit_download_cancel(t
->icon_download
);
3650 t
->icon_download
= NULL
;
3655 xt_icon_from_name(t
, "text-html");
3659 notify_icon_loaded_cb(WebKitWebView
*wv
, gchar
*uri
, struct tab
*t
)
3661 DNPRINTF(XT_D_DOWNLOAD
, "%s %s\n", __func__
, uri
);
3663 if (uri
== NULL
|| t
== NULL
)
3666 #if WEBKIT_CHECK_VERSION(1, 4, 0)
3667 /* take icon from WebKitIconDatabase */
3670 pb
= webkit_web_view_get_icon_pixbuf(wv
);
3672 xt_icon_from_pixbuf(t
, pb
);
3675 xt_icon_from_name(t
, "text-html");
3676 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
3677 /* download icon to cache dir */
3678 gchar
*name_hash
, file
[PATH_MAX
];
3681 if (t
->icon_request
) {
3682 DNPRINTF(XT_D_DOWNLOAD
, "%s: download in progress\n", __func__
);
3686 /* check to see if we got the icon in cache */
3687 name_hash
= g_compute_checksum_for_string(G_CHECKSUM_SHA256
, uri
, -1);
3688 snprintf(file
, sizeof file
, "%s/%s.ico", cache_dir
, name_hash
);
3691 if (!stat(file
, &sb
)) {
3692 if (sb
.st_size
> 0) {
3693 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading from cache %s\n",
3695 set_favicon_from_file(t
, file
);
3699 /* corrupt icon so trash it */
3700 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3705 /* create download for icon */
3706 t
->icon_request
= webkit_network_request_new(uri
);
3707 if (t
->icon_request
== NULL
) {
3708 DNPRINTF(XT_D_DOWNLOAD
, "%s: invalid uri %s\n",
3713 t
->icon_download
= webkit_download_new(t
->icon_request
);
3714 if (t
->icon_download
== NULL
)
3717 /* we have to free icon_dest_uri later */
3718 t
->icon_dest_uri
= g_strdup_printf("file://%s", file
);
3719 webkit_download_set_destination_uri(t
->icon_download
,
3722 if (webkit_download_get_status(t
->icon_download
) ==
3723 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
3724 g_object_unref(t
->icon_request
);
3725 g_free(t
->icon_dest_uri
);
3726 t
->icon_request
= NULL
;
3727 t
->icon_dest_uri
= NULL
;
3731 g_signal_connect(G_OBJECT(t
->icon_download
), "notify::status",
3732 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3734 webkit_download_start(t
->icon_download
);
3739 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3741 const gchar
*uri
= NULL
, *title
= NULL
;
3742 struct history
*h
, find
;
3746 DNPRINTF(XT_D_URL
, "notify_load_status_cb: %d %s\n",
3747 webkit_web_view_get_load_status(wview
),
3748 get_uri(t
) ? get_uri(t
) : "NOTHING");
3751 show_oops(NULL
, "notify_load_status_cb invalid parameters");
3755 switch (webkit_web_view_get_load_status(wview
)) {
3756 case WEBKIT_LOAD_PROVISIONAL
:
3758 abort_favicon_download(t
);
3759 #if GTK_CHECK_VERSION(2, 20, 0)
3760 gtk_widget_show(t
->spinner
);
3761 gtk_spinner_start(GTK_SPINNER(t
->spinner
));
3763 gtk_label_set_text(GTK_LABEL(t
->label
), "Loading");
3765 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), TRUE
);
3767 /* assume we are a new address */
3768 gdk_color_parse("white", &color
);
3769 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3770 statusbar_modify_attr(t
, "white", XT_COLOR_BLACK
);
3772 /* take focus if we are visible */
3778 /* kill color thread */
3783 case WEBKIT_LOAD_COMMITTED
:
3788 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
3794 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
3796 /* check if js white listing is enabled */
3797 if (enable_plugin_whitelist
)
3798 check_and_set_pl(uri
, t
);
3799 if (enable_cookie_whitelist
)
3800 check_and_set_cookie(uri
, t
);
3801 if (enable_js_whitelist
)
3802 check_and_set_js(uri
, t
);
3808 /* we know enough to autosave the session */
3809 if (session_autosave
) {
3814 show_ca_status(t
, uri
);
3815 run_script(t
, JS_HINTING
);
3818 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
3822 case WEBKIT_LOAD_FINISHED
:
3828 if (!strncmp(uri
, "http://", strlen("http://")) ||
3829 !strncmp(uri
, "https://", strlen("https://")) ||
3830 !strncmp(uri
, "file://", strlen("file://"))) {
3832 h
= RB_FIND(history_list
, &hl
, &find
);
3834 title
= get_title(t
, FALSE
);
3835 h
= g_malloc(sizeof *h
);
3836 h
->uri
= g_strdup(uri
);
3837 h
->title
= g_strdup(title
);
3838 RB_INSERT(history_list
, &hl
, h
);
3839 completion_add_uri(h
->uri
);
3840 update_history_tabs(NULL
);
3844 set_status(t
, (char *)uri
, XT_STATUS_URI
);
3845 #if WEBKIT_CHECK_VERSION(1, 1, 18)
3846 case WEBKIT_LOAD_FAILED
:
3849 #if GTK_CHECK_VERSION(2, 20, 0)
3850 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
3851 gtk_widget_hide(t
->spinner
);
3854 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
3859 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
), TRUE
);
3861 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
),
3862 can_go_back_for_real(t
));
3864 gtk_widget_set_sensitive(GTK_WIDGET(t
->forward
),
3865 can_go_forward_for_real(t
));
3869 notify_title_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3871 const gchar
*title
= NULL
, *win_title
= NULL
;
3873 title
= get_title(t
, FALSE
);
3874 win_title
= get_title(t
, TRUE
);
3875 gtk_label_set_text(GTK_LABEL(t
->label
), title
);
3876 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
), title
);
3877 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
3878 gtk_window_set_title(GTK_WINDOW(main_window
), win_title
);
3882 get_domain(const gchar
*host
)
3887 /* handle silly domains like .co.uk */
3889 if ((x
= strlen(host
)) <= 6)
3890 return (g_strdup(host
));
3892 if (host
[x
- 3] == '.' && host
[x
- 6] == '.') {
3898 return (g_strdup(&host
[x
+ 1]));
3902 p
= g_strrstr(host
, ".");
3904 return (g_strdup(""));
3910 return (g_strdup(p
+ 1));
3912 return (g_strdup(host
));
3916 js_autorun(struct tab
*t
)
3920 size_t got_default
= 0, got_host
= 0;
3922 char deff
[PATH_MAX
], hostf
[PATH_MAX
];
3923 char *js
= NULL
, *jsat
, *domain
= NULL
;
3924 FILE *deffile
= NULL
, *hostfile
= NULL
;
3925 JSGlobalContextRef ctx
;
3926 WebKitWebFrame
*frame
;
3928 JSValueRef val
, exception
;
3931 if (js_autorun_enabled
== 0)
3936 !(g_str_has_prefix(uri
, "http://") ||
3937 g_str_has_prefix(uri
, "https://")))
3940 su
= soup_uri_new(uri
);
3943 if (!SOUP_URI_VALID_FOR_HTTP(su
))
3946 DNPRINTF(XT_D_JS
, "%s: host: %s domain: %s\n", __func__
,
3948 domain
= get_domain(su
->host
);
3950 snprintf(deff
, sizeof deff
, "%s/default.js", js_dir
);
3951 if ((deffile
= fopen(deff
, "r")) != NULL
) {
3952 if (fstat(fileno(deffile
), &sb
) == -1) {
3953 show_oops(t
, "can't stat default JS file");
3956 got_default
= sb
.st_size
;
3959 /* try host first followed by domain */
3960 snprintf(hostf
, sizeof hostf
, "%s/%s.js", js_dir
, su
->host
);
3961 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
3962 if ((hostfile
= fopen(hostf
, "r")) == NULL
) {
3963 snprintf(hostf
, sizeof hostf
, "%s/%s.js", js_dir
, domain
);
3964 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
3965 if ((hostfile
= fopen(hostf
, "r")) == NULL
)
3968 DNPRINTF(XT_D_JS
, "file: %s\n", hostf
);
3969 if (fstat(fileno(hostfile
), &sb
) == -1) {
3970 show_oops(t
, "can't stat %s JS file", hostf
);
3973 got_host
= sb
.st_size
;
3976 if (got_default
+ got_host
== 0)
3979 js
= g_malloc0(got_default
+ got_host
+ 1);
3983 if (fread(js
, got_default
, 1, deffile
) != 1) {
3984 show_oops(t
, "default file read error");
3987 jsat
= js
+ got_default
;
3991 if (fread(jsat
, got_host
, 1, hostfile
) != 1) {
3992 show_oops(t
, "host file read error");
3997 /* this code needs to be redone */
3998 frame
= webkit_web_view_get_main_frame(t
->wv
);
3999 ctx
= webkit_web_frame_get_global_context(frame
);
4001 str
= JSStringCreateWithUTF8CString(js
);
4002 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
4003 NULL
, 0, &exception
);
4004 JSStringRelease(str
);
4006 DNPRINTF(XT_D_JS
, "%s: val %p\n", __func__
, val
);
4008 es
= js_ref_to_string(ctx
, exception
);
4010 show_oops(t
, "script exception: %s", es
);
4015 es
= js_ref_to_string(ctx
, val
);
4019 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
4021 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
4024 show_oops(t
, "script complete return value: '%s'", es
);
4027 show_oops(t
, "script complete: without a return value");
4044 webview_load_finished_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4046 /* autorun some js if enabled */
4054 webview_progress_changed_cb(WebKitWebView
*wv
, int progress
, struct tab
*t
)
4056 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->uri_entry
),
4057 progress
== 100 ? 0 : (double)progress
/ 100);
4058 if (show_url
== 0) {
4059 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
4060 progress
== 100 ? 0 : (double)progress
/ 100);
4063 update_statusbar_position(NULL
, NULL
);
4067 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
4068 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
4069 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
4072 WebKitWebNavigationReason reason
;
4073 struct domain
*d
= NULL
;
4076 show_oops(NULL
, "webview_npd_cb invalid parameters");
4080 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
4082 webkit_network_request_get_uri(request
));
4084 uri
= (char *)webkit_network_request_get_uri(request
);
4086 /* if this is an xtp url, we don't load anything else */
4087 if (parse_xtp_url(t
, uri
))
4090 if ((t
->mode
== XT_MODE_HINT
&& t
->new_tab
) || t
->ctrl_click
) {
4092 create_new_tab(uri
, NULL
, ctrl_click_focus
, -1);
4093 webkit_web_policy_decision_ignore(pd
);
4094 return (TRUE
); /* we made the decission */
4098 * This is a little hairy but it comes down to this:
4099 * when we run in whitelist mode we have to assist the browser in
4100 * opening the URL that it would have opened in a new tab.
4102 reason
= webkit_web_navigation_action_get_reason(na
);
4103 if (reason
== WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED
) {
4104 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
4105 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1)
4106 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4108 webkit_web_policy_decision_use(pd
);
4109 return (TRUE
); /* we made the decision */
4116 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4119 struct domain
*d
= NULL
;
4121 WebKitWebView
*webview
= NULL
;
4124 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
4125 webkit_web_view_get_uri(wv
));
4128 /* open in current tab */
4130 } else if (enable_scripts
== 0 && enable_js_whitelist
== 1) {
4131 uri
= webkit_web_view_get_uri(wv
);
4132 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4135 if (t
->ctrl_click
) {
4136 x
= ctrl_click_focus
;
4139 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4141 } else if (enable_scripts
== 1) {
4142 if (t
->ctrl_click
) {
4143 x
= ctrl_click_focus
;
4146 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4154 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
4157 struct domain
*d
= NULL
;
4159 DNPRINTF(XT_D_NAV
, "webview_close_cb: %d\n", t
->tab_id
);
4161 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
4162 uri
= webkit_web_view_get_uri(wv
);
4163 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4167 } else if (enable_scripts
== 1)
4174 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
4176 /* we can not eat the event without throwing gtk off so defer it */
4178 /* catch middle click */
4179 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 2) {
4184 /* catch ctrl click */
4185 if (e
->type
== GDK_BUTTON_RELEASE
&&
4186 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
4191 return (XT_CB_PASSTHROUGH
);
4195 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
4197 struct mime_type
*m
;
4199 m
= find_mime_type(mime_type
);
4207 show_oops(t
, "can't fork mime handler");
4217 execlp(m
->mt_action
, m
->mt_action
,
4218 webkit_network_request_get_uri(request
), (void *)NULL
);
4227 get_mime_type(const char *file
)
4230 char *mime_type
= NULL
;
4234 if (g_str_has_prefix(file
, "file://"))
4235 file
+= strlen("file://");
4237 gf
= g_file_new_for_path(file
);
4238 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
4240 if ((m
= g_file_info_get_content_type(fi
)) != NULL
)
4241 mime_type
= g_strdup(m
);
4249 run_download_mimehandler(char *mime_type
, char *file
)
4251 struct mime_type
*m
;
4253 m
= find_mime_type(mime_type
);
4259 show_oops(NULL
, "can't fork download mime handler");
4269 if (g_str_has_prefix(file
, "file://"))
4270 file
+= strlen("file://");
4271 execlp(m
->mt_action
, m
->mt_action
, file
, (void *)NULL
);
4280 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
4283 WebKitDownloadStatus status
;
4284 const char *file
= NULL
;
4287 if (download
== NULL
)
4289 status
= webkit_download_get_status(download
);
4290 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
4293 file
= webkit_download_get_destination_uri(download
);
4296 mime
= get_mime_type(file
);
4300 run_download_mimehandler((char *)mime
, (char *)file
);
4305 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
4306 WebKitNetworkRequest
*request
, char *mime_type
,
4307 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
4310 show_oops(NULL
, "webview_mimetype_cb invalid parameters");
4314 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
4315 t
->tab_id
, mime_type
);
4317 if (run_mimehandler(t
, mime_type
, request
) == 0) {
4318 webkit_web_policy_decision_ignore(decision
);
4323 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
4324 webkit_web_policy_decision_download(decision
);
4332 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
4336 const gchar
*suggested_name
;
4337 gchar
*filename
= NULL
;
4339 struct download
*download_entry
;
4342 if (wk_download
== NULL
|| t
== NULL
) {
4343 show_oops(NULL
, "%s invalid parameters", __func__
);
4347 suggested_name
= webkit_download_get_suggested_filename(wk_download
);
4348 if (suggested_name
== NULL
)
4349 return (FALSE
); /* abort download */
4360 filename
= g_strdup_printf("%d%s", i
, suggested_name
);
4362 uri
= g_strdup_printf("file://%s/%s", download_dir
, i
?
4363 filename
: suggested_name
);
4365 } while (!stat(uri
+ strlen("file://"), &sb
));
4367 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d filename %s "
4368 "local %s\n", __func__
, t
->tab_id
, filename
, uri
);
4370 webkit_download_set_destination_uri(wk_download
, uri
);
4372 if (webkit_download_get_status(wk_download
) ==
4373 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
4374 show_oops(t
, "%s: download failed to start", __func__
);
4376 gtk_label_set_text(GTK_LABEL(t
->label
), "Download Failed");
4378 /* connect "download first" mime handler */
4379 g_signal_connect(G_OBJECT(wk_download
), "notify::status",
4380 G_CALLBACK(download_status_changed_cb
), NULL
);
4382 download_entry
= g_malloc(sizeof(struct download
));
4383 download_entry
->download
= wk_download
;
4384 download_entry
->tab
= t
;
4385 download_entry
->id
= next_download_id
++;
4386 RB_INSERT(download_list
, &downloads
, download_entry
);
4387 /* get from history */
4388 g_object_ref(wk_download
);
4389 gtk_label_set_text(GTK_LABEL(t
->label
), "Downloading");
4390 show_oops(t
, "Download of '%s' started...",
4391 basename((char *)webkit_download_get_destination_uri(wk_download
)));
4400 /* sync other download manager tabs */
4401 update_download_tabs(NULL
);
4404 * NOTE: never redirect/render the current tab before this
4405 * function returns. This will cause the download to never start.
4407 return (ret
); /* start download */
4411 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
4413 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
4416 show_oops(NULL
, "webview_hover_cb");
4421 set_status(t
, uri
, XT_STATUS_LINK
);
4424 set_status(t
, t
->status
, XT_STATUS_NOTHING
);
4429 mark(struct tab
*t
, struct karg
*arg
)
4435 if ((index
= marktoindex(mark
)) == -1)
4438 if (arg
->i
== XT_MARK_SET
)
4439 t
->mark
[index
] = gtk_adjustment_get_value(t
->adjust_v
);
4440 else if (arg
->i
== XT_MARK_GOTO
) {
4441 if (t
->mark
[index
] == XT_INVALID_MARK
) {
4442 show_oops(t
, "mark '%c' does not exist", mark
);
4445 /* XXX t->mark[index] can be bigger than the maximum if ajax or
4446 something changes the document size */
4447 gtk_adjustment_set_value(t
->adjust_v
, t
->mark
[index
]);
4454 marks_clear(struct tab
*t
)
4458 for (i
= 0; i
< LENGTH(t
->mark
); i
++)
4459 t
->mark
[i
] = XT_INVALID_MARK
;
4465 char file
[PATH_MAX
];
4466 char *line
= NULL
, *p
;
4471 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
4472 if ((f
= fopen(file
, "r+")) == NULL
) {
4473 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
4477 for (i
= 1; ; i
++) {
4478 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
4480 if (strlen(line
) == 0 || line
[0] == '#') {
4486 p
= strtok(line
, " \t");
4488 if (p
== NULL
|| strlen(p
) != 1 ||
4489 (index
= marktoindex(*p
)) == -1) {
4490 warnx("corrupt quickmarks file, line %d", i
);
4494 p
= strtok(NULL
, " \t");
4495 if (qmarks
[index
] != NULL
)
4496 g_free(qmarks
[index
]);
4497 qmarks
[index
] = g_strdup(p
);
4508 char file
[PATH_MAX
];
4512 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
4513 if ((f
= fopen(file
, "r+")) == NULL
) {
4514 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
4518 for (i
= 0; i
< XT_NOMARKS
; i
++)
4519 if (qmarks
[i
] != NULL
)
4520 fprintf(f
, "%c %s\n", indextomark(i
), qmarks
[i
]);
4528 qmark(struct tab
*t
, struct karg
*arg
)
4533 mark
= arg
->s
[strlen(arg
->s
)-1];
4534 index
= marktoindex(mark
);
4540 if (qmarks
[index
] != NULL
)
4541 g_free(qmarks
[index
]);
4543 qmarks_load(); /* sync if multiple instances */
4544 qmarks
[index
] = g_strdup(get_uri(t
));
4548 if (qmarks
[index
] != NULL
)
4549 load_uri(t
, qmarks
[index
]);
4551 show_oops(t
, "quickmark \"%c\" does not exist",
4557 if (qmarks
[index
] != NULL
)
4558 create_new_tab(qmarks
[index
], NULL
, 1, -1);
4560 show_oops(t
, "quickmark \"%c\" does not exist",
4571 go_up(struct tab
*t
, struct karg
*args
)
4577 levels
= atoi(args
->s
);
4581 uri
= g_strdup(webkit_web_view_get_uri(t
->wv
));
4582 if ((tmp
= strstr(uri
, XT_PROTO_DELIM
)) == NULL
)
4584 tmp
+= strlen(XT_PROTO_DELIM
);
4586 /* if an uri starts with a slash, leave it alone (for file:///) */
4593 p
= strrchr(tmp
, '/');
4607 gototab(struct tab
*t
, struct karg
*args
)
4610 struct karg arg
= {0, NULL
, -1};
4612 tab
= atoi(args
->s
);
4614 arg
.i
= XT_TAB_NEXT
;
4623 zoom_amount(struct tab
*t
, struct karg
*arg
)
4625 struct karg narg
= {0, NULL
, -1};
4627 narg
.i
= atoi(arg
->s
);
4628 resizetab(t
, &narg
);
4634 flip_colon(struct tab
*t
, struct karg
*arg
)
4636 struct karg narg
= {0, NULL
, -1};
4639 if (t
== NULL
|| arg
== NULL
)
4642 p
= strstr(arg
->s
, ":");
4654 /* buffer commands receive the regex that triggered them in arg.s */
4655 char bcmd
[XT_BUFCMD_SZ
];
4659 #define XT_PRE_NO (0)
4660 #define XT_PRE_YES (1)
4661 #define XT_PRE_MAYBE (2)
4663 int (*func
)(struct tab
*, struct karg
*);
4667 { "^[0-9]*gu$", XT_PRE_MAYBE
, "gu", go_up
, 0 },
4668 { "^gg$", XT_PRE_NO
, "gg", move
, XT_MOVE_TOP
},
4669 { "^gG$", XT_PRE_NO
, "gG", move
, XT_MOVE_BOTTOM
},
4670 { "^[0-9]+%$", XT_PRE_YES
, "%", move
, XT_MOVE_PERCENT
},
4671 { "^zz$", XT_PRE_NO
, "zz", move
, XT_MOVE_CENTER
},
4672 { "^gh$", XT_PRE_NO
, "gh", go_home
, 0 },
4673 { "^m[a-zA-Z0-9]$", XT_PRE_NO
, "m", mark
, XT_MARK_SET
},
4674 { "^['][a-zA-Z0-9]$", XT_PRE_NO
, "'", mark
, XT_MARK_GOTO
},
4675 { "^[0-9]+t$", XT_PRE_YES
, "t", gototab
, 0 },
4676 { "^M[a-zA-Z0-9]$", XT_PRE_NO
, "M", qmark
, XT_QMARK_SET
},
4677 { "^go[a-zA-Z0-9]$", XT_PRE_NO
, "go", qmark
, XT_QMARK_OPEN
},
4678 { "^gn[a-zA-Z0-9]$", XT_PRE_NO
, "gn", qmark
, XT_QMARK_TAB
},
4679 { "^ZR$", XT_PRE_NO
, "ZR", restart
, 0 },
4680 { "^ZZ$", XT_PRE_NO
, "ZZ", quit
, 0 },
4681 { "^zi$", XT_PRE_NO
, "zi", resizetab
, XT_ZOOM_IN
},
4682 { "^zo$", XT_PRE_NO
, "zo", resizetab
, XT_ZOOM_OUT
},
4683 { "^z0$", XT_PRE_NO
, "z0", resizetab
, XT_ZOOM_NORMAL
},
4684 { "^[0-9]+Z$", XT_PRE_YES
, "Z", zoom_amount
, 0 },
4685 { "^[0-9]+:$", XT_PRE_YES
, ":", flip_colon
, 0 },
4689 buffercmd_init(void)
4693 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
4694 if (regcomp(&buffercmds
[i
].cregex
, buffercmds
[i
].regex
,
4695 REG_EXTENDED
| REG_NOSUB
))
4696 startpage_add("invalid buffercmd regex %s",
4697 buffercmds
[i
].regex
);
4701 buffercmd_abort(struct tab
*t
)
4705 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_abort: clearing buffer\n");
4706 for (i
= 0; i
< LENGTH(bcmd
); i
++)
4709 cmd_prefix
= 0; /* clear prefix for non-buffer commands */
4710 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
4714 buffercmd_execute(struct tab
*t
, struct buffercmd
*cmd
)
4716 struct karg arg
= {0, NULL
, -1};
4719 arg
.s
= g_strdup(bcmd
);
4721 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_execute: buffer \"%s\" "
4722 "matches regex \"%s\", executing\n", bcmd
, cmd
->regex
);
4732 buffercmd_addkey(struct tab
*t
, guint keyval
)
4735 char s
[XT_BUFCMD_SZ
];
4737 if (keyval
== GDK_Escape
) {
4739 return (XT_CB_HANDLED
);
4742 /* key with modifier or non-ascii character */
4743 if (!isascii(keyval
)) {
4745 * XXX this looks wrong but fixes some sites like
4746 * http://www.seslisozluk.com/
4747 * that eat a shift or ctrl and end putting default focus in js
4748 * instead of ignoring the keystroke
4749 * so instead of return (XT_CB_PASSTHROUGH); eat the key
4751 return (XT_CB_HANDLED
);
4754 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: adding key \"%c\" "
4755 "to buffer \"%s\"\n", keyval
, bcmd
);
4757 for (i
= 0; i
< LENGTH(bcmd
); i
++)
4758 if (bcmd
[i
] == '\0') {
4763 /* buffer full, ignore input */
4764 if (i
>= LENGTH(bcmd
) -1) {
4765 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: buffer full\n");
4767 return (XT_CB_HANDLED
);
4770 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
4772 /* find exact match */
4773 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
4774 if (regexec(&buffercmds
[i
].cregex
, bcmd
,
4775 (size_t) 0, NULL
, 0) == 0) {
4776 buffercmd_execute(t
, &buffercmds
[i
]);
4780 /* find non exact matches to see if we need to abort ot not */
4781 for (i
= 0, match
= 0; i
< LENGTH(buffercmds
); i
++) {
4782 DNPRINTF(XT_D_BUFFERCMD
, "trying: %s\n", bcmd
);
4785 if (buffercmds
[i
].precount
== XT_PRE_MAYBE
) {
4786 if (isdigit(bcmd
[0])) {
4787 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
4791 if (sscanf(bcmd
, "%s", s
) == 0)
4794 } else if (buffercmds
[i
].precount
== XT_PRE_YES
) {
4795 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
4798 if (sscanf(bcmd
, "%s", s
) == 0)
4801 if (c
== -1 && buffercmds
[i
].precount
)
4803 if (!strncmp(s
, buffercmds
[i
].cmd
, strlen(s
)))
4806 DNPRINTF(XT_D_BUFFERCMD
, "got[%d] %d <%s>: %d %s\n",
4807 i
, match
, buffercmds
[i
].cmd
, c
, s
);
4810 DNPRINTF(XT_D_BUFFERCMD
, "aborting: %s\n", bcmd
);
4815 return (XT_CB_HANDLED
);
4819 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
4821 struct key_binding
*k
;
4823 /* handle keybindings if buffercmd is empty.
4824 if not empty, allow commands like C-n */
4825 if (bcmd
[0] == '\0' || ((e
->state
& (CTRL
| MOD1
)) != 0))
4826 TAILQ_FOREACH(k
, &kbl
, entry
)
4827 if (e
->keyval
== k
->key
4828 && (entry
? k
->use_in_entry
: 1)) {
4829 /* when we are edditing eat ctrl/mod keys */
4830 if (edit_mode
== XT_EM_VI
&&
4831 t
->mode
== XT_MODE_INSERT
&&
4832 (e
->state
& CTRL
|| e
->state
& MOD1
))
4833 return (XT_CB_PASSTHROUGH
);
4836 if ((e
->state
& (CTRL
| MOD1
)) == 0)
4837 return (cmd_execute(t
, k
->cmd
));
4838 } else if ((e
->state
& k
->mask
) == k
->mask
) {
4839 return (cmd_execute(t
, k
->cmd
));
4843 if (!entry
&& ((e
->state
& (CTRL
| MOD1
)) == 0))
4844 return buffercmd_addkey(t
, e
->keyval
);
4846 return (XT_CB_PASSTHROUGH
);
4850 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
4853 void *active
= NULL
;
4855 /* don't use w directly; use t->whatever instead */
4858 show_oops(NULL
, "wv_keypress_after_cb");
4859 return (XT_CB_PASSTHROUGH
);
4864 DNPRINTF(XT_D_KEY
, "wv_keypress_after_cb: mode %d keyval 0x%x mask "
4865 "0x%x tab %d\n", t
->mode
, e
->keyval
, e
->state
, t
->tab_id
);
4867 /* Hide buffers, if they are visible, with escape. */
4868 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)) &&
4869 CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
4870 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
4872 return (XT_CB_HANDLED
);
4875 if (t
->mode
== XT_MODE_HINT
)
4876 return (XT_CB_HANDLED
);
4878 if ((CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Tab
) ||
4879 (CLEAN(e
->state
) == SHFT
&& e
->keyval
== GDK_Tab
))
4880 /* something focussy is about to happen */
4881 return (XT_CB_PASSTHROUGH
);
4883 /* check if we are some sort of text input thing in the dom */
4884 active
= input_check_mode(t
);
4886 if (t
->mode
== XT_MODE_HINT
) {
4887 /* XXX make sure cmd entry is enabled */
4888 return (XT_CB_HANDLED
);
4889 } else if (t
->mode
== XT_MODE_COMMAND
) {
4891 snprintf(s
, sizeof s
, "%c", e
->keyval
);
4892 if (CLEAN(e
->state
) == 0 && isdigit(s
[0]))
4893 cmd_prefix
= 10 * cmd_prefix
+ atoi(s
);
4894 return (handle_keypress(t
, e
, 0));
4897 return (handle_keypress(t
, e
, 1));
4901 return (XT_CB_PASSTHROUGH
);
4905 hint_continue(struct tab
*t
)
4907 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
4909 const gchar
*errstr
= NULL
;
4913 if (!(c
[0] == '.' || c
[0] == ','))
4915 if (strlen(c
) == 1) {
4916 /* XXX should not happen */
4921 if (isdigit(c
[1])) {
4923 i
= strtonum(&c
[1], 1, 4096, &errstr
);
4925 show_oops(t
, "invalid numerical hint %s", &c
[1]);
4928 s
= g_strdup_printf("hints.updateHints(%lld);", i
);
4932 /* alphanumeric input */
4933 s
= g_strdup_printf("hints.createHints('%s', '%c');",
4934 &c
[1], c
[0] == '.' ? 'f' : 'F');
4945 search_continue(struct tab
*t
)
4947 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
4948 gboolean rv
= FALSE
;
4950 if (c
[0] == ':' || c
[0] == '.' || c
[0] == ',')
4952 if (strlen(c
) == 1) {
4953 webkit_web_view_unmark_text_matches(t
->wv
);
4958 t
->search_forward
= TRUE
;
4959 else if (c
[0] == '?')
4960 t
->search_forward
= FALSE
;
4970 search_cb(struct tab
*t
)
4972 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
4975 if (search_continue(t
) == FALSE
)
4979 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, t
->search_forward
,
4981 /* not found, mark red */
4982 gdk_color_parse(XT_COLOR_RED
, &color
);
4983 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
4984 /* unmark and remove selection */
4985 webkit_web_view_unmark_text_matches(t
->wv
);
4986 /* my kingdom for a way to unselect text in webview */
4988 /* found, highlight all */
4989 webkit_web_view_unmark_text_matches(t
->wv
);
4990 webkit_web_view_mark_text_matches(t
->wv
, &c
[1], FALSE
, 0);
4991 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
4992 gdk_color_parse(XT_COLOR_WHITE
, &color
);
4993 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
5001 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5003 const gchar
*c
= gtk_entry_get_text(w
);
5006 show_oops(NULL
, "cmd_keyrelease_cb invalid parameters");
5007 return (XT_CB_PASSTHROUGH
);
5010 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
5011 e
->keyval
, e
->state
, t
->tab_id
);
5014 if (!(e
->keyval
== GDK_Tab
|| e
->keyval
== GDK_ISO_Left_Tab
)) {
5015 if (hint_continue(t
) == FALSE
)
5020 if (search_continue(t
) == FALSE
)
5023 /* if search length is > 4 then no longer play timeout games */
5024 if (strlen(c
) > 4) {
5026 g_source_remove(t
->search_id
);
5033 /* reestablish a new timer if the user types fast */
5035 g_source_remove(t
->search_id
);
5036 t
->search_id
= g_timeout_add(250, (GSourceFunc
)search_cb
, (gpointer
)t
);
5039 return (XT_CB_PASSTHROUGH
);
5043 match_uri(const gchar
*uri
, const gchar
*key
) {
5046 gboolean match
= FALSE
;
5050 if (!strncmp(key
, uri
, len
))
5053 voffset
= strstr(uri
, "/") + 2;
5054 if (!strncmp(key
, voffset
, len
))
5056 else if (g_str_has_prefix(voffset
, "www.")) {
5057 voffset
= voffset
+ strlen("www.");
5058 if (!strncmp(key
, voffset
, len
))
5067 match_session(const gchar
*name
, const gchar
*key
) {
5070 sub
= strcasestr(name
, key
);
5076 cmd_getlist(int id
, char *key
)
5083 if (cmds
[id
].type
& XT_URLARG
) {
5084 RB_FOREACH_REVERSE(h
, history_list
, &hl
)
5085 if (match_uri(h
->uri
, key
)) {
5086 cmd_status
.list
[c
] = (char *)h
->uri
;
5092 } else if (cmds
[id
].type
& XT_SESSARG
) {
5093 TAILQ_FOREACH(s
, &sessions
, entry
)
5094 if (match_session(s
->name
, key
)) {
5095 cmd_status
.list
[c
] = (char *)s
->name
;
5101 } else if (cmds
[id
].type
& XT_SETARG
) {
5102 for (i
= 0; i
< get_settings_size(); i
++)
5103 if(!strncmp(key
, get_setting_name(i
),
5105 cmd_status
.list
[c
++] = get_setting_name(i
);
5111 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
5113 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
5114 if (cmds
[i
].level
< dep
)
5116 if (cmds
[i
].level
== dep
&& !strncmp(key
, cmds
[i
].cmd
,
5117 strlen(key
)) && !isdigit(cmds
[i
].cmd
[0]))
5118 cmd_status
.list
[c
++] = cmds
[i
].cmd
;
5126 cmd_getnext(int dir
)
5128 cmd_status
.index
+= dir
;
5130 if (cmd_status
.index
< 0)
5131 cmd_status
.index
= cmd_status
.len
- 1;
5132 else if (cmd_status
.index
>= cmd_status
.len
)
5133 cmd_status
.index
= 0;
5135 return cmd_status
.list
[cmd_status
.index
];
5139 cmd_tokenize(char *s
, char *tokens
[])
5142 char *tok
, *last
= NULL
;
5143 size_t len
= strlen(s
);
5146 blank
= len
== 0 || (len
> 0 && s
[len
- 1] == ' ');
5147 for (tok
= strtok_r(s
, " ", &last
); tok
&& i
< 3;
5148 tok
= strtok_r(NULL
, " ", &last
), i
++)
5158 cmd_complete(struct tab
*t
, char *str
, int dir
)
5160 GtkEntry
*w
= GTK_ENTRY(t
->cmd
);
5161 int i
, j
, levels
, c
= 0, dep
= 0, parent
= -1;
5163 char *tok
, *match
, *s
= g_strdup(str
);
5165 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
5168 DNPRINTF(XT_D_CMD
, "%s: complete %s\n", __func__
, str
);
5171 for (i
= 0; isdigit(s
[i
]); i
++)
5174 for (; isspace(s
[i
]); i
++)
5179 levels
= cmd_tokenize(s
, tokens
);
5181 for (i
= 0; i
< levels
- 1; i
++) {
5184 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5185 if (cmds
[j
].level
< dep
)
5187 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
,
5191 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
5198 if (matchcount
== 1) {
5199 strlcat(res
, tok
, sizeof res
);
5200 strlcat(res
, " ", sizeof res
);
5210 if (cmd_status
.index
== -1)
5211 cmd_getlist(parent
, tokens
[i
]);
5213 if (cmd_status
.len
> 0) {
5214 match
= cmd_getnext(dir
);
5215 strlcat(res
, match
, sizeof res
);
5216 gtk_entry_set_text(w
, res
);
5217 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5224 cmd_execute(struct tab
*t
, char *str
)
5226 struct cmd
*cmd
= NULL
;
5227 char *tok
, *last
= NULL
, *s
= g_strdup(str
), *sc
;
5229 int j
, len
, c
= 0, dep
= 0, matchcount
= 0;
5230 int prefix
= -1, rv
= XT_CB_PASSTHROUGH
;
5231 struct karg arg
= {0, NULL
, -1};
5236 for (j
= 0; j
<3 && isdigit(s
[j
]); j
++)
5242 while (isspace(s
[0]))
5245 if (strlen(s
) > 0 && strlen(prefixstr
) > 0)
5246 prefix
= atoi(prefixstr
);
5250 for (tok
= strtok_r(s
, " ", &last
); tok
;
5251 tok
= strtok_r(NULL
, " ", &last
)) {
5253 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5254 if (cmds
[j
].level
< dep
)
5256 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1 :
5258 if (cmds
[j
].level
== dep
&&
5259 !strncmp(tok
, cmds
[j
].cmd
, len
)) {
5263 if (len
== strlen(cmds
[j
].cmd
)) {
5269 if (matchcount
== 1) {
5274 show_oops(t
, "Invalid command: %s", str
);
5280 show_oops(t
, "Empty command");
5286 arg
.precount
= prefix
;
5287 else if (cmd_prefix
> 0)
5288 arg
.precount
= cmd_prefix
;
5290 if (j
> 0 && !(cmd
->type
& XT_PREFIX
) && arg
.precount
> -1) {
5291 show_oops(t
, "No prefix allowed: %s", str
);
5295 arg
.s
= last
? g_strdup(last
) : g_strdup("");
5296 if (cmd
->type
& XT_INTARG
&& last
&& strlen(last
) > 0) {
5297 if (arg
.s
== NULL
) {
5298 show_oops(t
, "Invalid command");
5301 arg
.precount
= atoi(arg
.s
);
5302 if (arg
.precount
<= 0) {
5303 if (arg
.s
[0] == '0')
5304 show_oops(t
, "Zero count");
5306 show_oops(t
, "Trailing characters");
5311 DNPRINTF(XT_D_CMD
, "%s: prefix %d arg %s\n",
5312 __func__
, arg
.precount
, arg
.s
);
5328 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5331 show_oops(NULL
, "entry_key_cb invalid parameters");
5332 return (XT_CB_PASSTHROUGH
);
5335 DNPRINTF(XT_D_CMD
, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
5336 e
->keyval
, e
->state
, t
->tab_id
);
5340 if (e
->keyval
== GDK_Escape
) {
5341 /* don't use focus_webview(t) because we want to type :cmds */
5342 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5345 return (handle_keypress(t
, e
, 1));
5348 struct command_entry
*
5349 history_prev(struct command_list
*l
, struct command_entry
*at
)
5352 at
= TAILQ_LAST(l
, command_list
);
5354 at
= TAILQ_PREV(at
, command_list
, entry
);
5356 at
= TAILQ_LAST(l
, command_list
);
5362 struct command_entry
*
5363 history_next(struct command_list
*l
, struct command_entry
*at
)
5366 at
= TAILQ_FIRST(l
);
5368 at
= TAILQ_NEXT(at
, entry
);
5370 at
= TAILQ_FIRST(l
);
5377 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5379 int rv
= XT_CB_HANDLED
;
5380 const gchar
*c
= gtk_entry_get_text(w
);
5384 show_oops(NULL
, "cmd_keypress_cb parameters");
5385 return (XT_CB_PASSTHROUGH
);
5388 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
5389 e
->keyval
, e
->state
, t
->tab_id
);
5393 e
->keyval
= GDK_Escape
;
5394 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5395 c
[0] == '.' || c
[0] == ','))
5396 e
->keyval
= GDK_Escape
;
5398 if (e
->keyval
!= GDK_Tab
&& e
->keyval
!= GDK_Shift_L
&&
5399 e
->keyval
!= GDK_ISO_Left_Tab
)
5400 cmd_status
.index
= -1;
5402 switch (e
->keyval
) {
5405 cmd_complete(t
, (char *)&c
[1], 1);
5406 else if (c
[0] == '.' || c
[0] == ',')
5407 run_script(t
, "hints.focusNextHint();");
5409 case GDK_ISO_Left_Tab
:
5411 cmd_complete(t
, (char *)&c
[1], -1);
5412 else if (c
[0] == '.' || c
[0] == ',')
5413 run_script(t
, "hints.focusPreviousHint();");
5417 if ((search_at
= history_next(&shl
, search_at
))) {
5418 search_at
->line
[0] = c
[0];
5419 gtk_entry_set_text(w
, search_at
->line
);
5420 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5422 } else if (c
[0] == '/') {
5423 if ((search_at
= history_prev(&chl
, search_at
))) {
5424 search_at
->line
[0] = c
[0];
5425 gtk_entry_set_text(w
, search_at
->line
);
5426 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5428 } if (c
[0] == ':') {
5429 if ((history_at
= history_prev(&chl
, history_at
))) {
5430 history_at
->line
[0] = c
[0];
5431 gtk_entry_set_text(w
, history_at
->line
);
5432 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5438 if ((search_at
= history_next(&shl
, search_at
))) {
5439 search_at
->line
[0] = c
[0];
5440 gtk_entry_set_text(w
, search_at
->line
);
5441 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5443 } else if (c
[0] == '?') {
5444 if ((search_at
= history_prev(&chl
, search_at
))) {
5445 search_at
->line
[0] = c
[0];
5446 gtk_entry_set_text(w
, search_at
->line
);
5447 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5449 } if (c
[0] == ':') {
5450 if ((history_at
= history_next(&chl
, history_at
))) {
5451 history_at
->line
[0] = c
[0];
5452 gtk_entry_set_text(w
, history_at
->line
);
5453 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5458 if (!(!strcmp(c
, ":") || !strcmp(c
, "/") || !strcmp(c
, "?") ||
5459 !strcmp(c
, ".") || !strcmp(c
, ","))) {
5460 /* see if we are doing hinting and reset it */
5461 if (c
[0] == '.' || c
[0] == ',') {
5462 /* recreate hints */
5463 s
= g_strdup_printf("hints.createHints('', "
5464 "'%c');", c
[0] == '.' ? 'f' : 'F');
5477 if (c
!= NULL
&& (c
[0] == '/' || c
[0] == '?'))
5478 webkit_web_view_unmark_text_matches(t
->wv
);
5480 /* no need to cancel hints */
5484 rv
= XT_CB_PASSTHROUGH
;
5490 wv_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
5492 DNPRINTF(XT_D_CMD
, "wv_popup_cb: tab %d\n", t
->tab_id
);
5496 cmd_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
5498 /* popup menu enabled */
5503 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
5506 show_oops(NULL
, "cmd_focusout_cb invalid parameters");
5507 return (XT_CB_PASSTHROUGH
);
5510 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d popup %d\n",
5511 t
->tab_id
, t
->popup
);
5513 /* if popup is enabled don't lose focus */
5516 return (XT_CB_PASSTHROUGH
);
5523 if (show_url
== 0 || t
->focus_wv
)
5526 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
5528 return (XT_CB_PASSTHROUGH
);
5532 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
5535 const gchar
*c
= gtk_entry_get_text(entry
);
5538 show_oops(NULL
, "cmd_activate_cb invalid parameters");
5542 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
5547 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5548 c
[0] == '.' || c
[0] == ','))
5554 if (c
[0] == '/' || c
[0] == '?') {
5555 /* see if there is a timer pending */
5557 g_source_remove(t
->search_id
);
5562 if (t
->search_text
) {
5563 g_free(t
->search_text
);
5564 t
->search_text
= NULL
;
5567 t
->search_text
= g_strdup(s
);
5569 g_free(global_search
);
5570 global_search
= g_strdup(s
);
5571 t
->search_forward
= c
[0] == '/';
5573 history_add(&shl
, search_file
, s
, &search_history_count
);
5574 } else if (c
[0] == '.' || c
[0] == ',') {
5575 run_script(t
, "hints.fire();");
5576 /* XXX history for link following? */
5577 } else if (c
[0] == ':') {
5578 history_add(&chl
, command_file
, s
, &cmd_history_count
);
5579 /* can't call hide_cmd after cmd_execute */
5590 backward_cb(GtkWidget
*w
, struct tab
*t
)
5595 show_oops(NULL
, "backward_cb invalid parameters");
5599 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
5606 forward_cb(GtkWidget
*w
, struct tab
*t
)
5611 show_oops(NULL
, "forward_cb invalid parameters");
5615 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
5617 a
.i
= XT_NAV_FORWARD
;
5622 home_cb(GtkWidget
*w
, struct tab
*t
)
5625 show_oops(NULL
, "home_cb invalid parameters");
5629 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
5635 stop_cb(GtkWidget
*w
, struct tab
*t
)
5637 WebKitWebFrame
*frame
;
5640 show_oops(NULL
, "stop_cb invalid parameters");
5644 DNPRINTF(XT_D_NAV
, "stop_cb: tab %d\n", t
->tab_id
);
5646 frame
= webkit_web_view_get_main_frame(t
->wv
);
5647 if (frame
== NULL
) {
5648 show_oops(t
, "stop_cb: no frame");
5652 webkit_web_frame_stop_loading(frame
);
5653 abort_favicon_download(t
);
5657 setup_webkit(struct tab
*t
)
5659 if (is_g_object_setting(G_OBJECT(t
->settings
), "enable-dns-prefetching"))
5660 g_object_set(G_OBJECT(t
->settings
), "enable-dns-prefetching",
5661 FALSE
, (char *)NULL
);
5663 warnx("webkit does not have \"enable-dns-prefetching\" property");
5664 g_object_set(G_OBJECT(t
->settings
),
5665 "user-agent", t
->user_agent
, (char *)NULL
);
5666 g_object_set(G_OBJECT(t
->settings
),
5667 "enable-scripts", enable_scripts
, (char *)NULL
);
5668 g_object_set(G_OBJECT(t
->settings
),
5669 "enable-plugins", enable_plugins
, (char *)NULL
);
5670 g_object_set(G_OBJECT(t
->settings
),
5671 "javascript-can-open-windows-automatically", enable_scripts
,
5673 g_object_set(G_OBJECT(t
->settings
),
5674 "enable-html5-database", FALSE
, (char *)NULL
);
5675 g_object_set(G_OBJECT(t
->settings
),
5676 "enable-html5-local-storage", enable_localstorage
, (char *)NULL
);
5677 g_object_set(G_OBJECT(t
->settings
),
5678 "enable_spell_checking", enable_spell_checking
, (char *)NULL
);
5679 g_object_set(G_OBJECT(t
->settings
),
5680 "spell_checking_languages", spell_check_languages
, (char *)NULL
);
5681 g_object_set(G_OBJECT(t
->settings
),
5682 "enable-developer-extras", TRUE
, (char *)NULL
);
5683 g_object_set(G_OBJECT(t
->wv
),
5684 "full-content-zoom", TRUE
, (char *)NULL
);
5686 webkit_web_view_set_settings(t
->wv
, t
->settings
);
5690 update_statusbar_position(GtkAdjustment
* adjustment
, gpointer data
)
5692 struct tab
*ti
, *t
= NULL
;
5693 gdouble view_size
, value
, max
;
5696 TAILQ_FOREACH(ti
, &tabs
, entry
)
5697 if (ti
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
5705 if (adjustment
== NULL
)
5706 adjustment
= gtk_scrolled_window_get_vadjustment(
5707 GTK_SCROLLED_WINDOW(t
->browser_win
));
5709 view_size
= gtk_adjustment_get_page_size(adjustment
);
5710 value
= gtk_adjustment_get_value(adjustment
);
5711 max
= gtk_adjustment_get_upper(adjustment
) - view_size
;
5714 position
= g_strdup("All");
5715 else if (value
== max
)
5716 position
= g_strdup("Bot");
5717 else if (value
== 0)
5718 position
= g_strdup("Top");
5720 position
= g_strdup_printf("%d%%", (int) ((value
/ max
) * 100));
5722 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.position
), position
);
5729 create_window(const gchar
*name
)
5733 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
5734 gtk_window_set_default_size(GTK_WINDOW(w
), window_width
, window_height
);
5735 gtk_widget_set_name(w
, name
);
5736 gtk_window_set_wmclass(GTK_WINDOW(w
), name
, "XXXTerm");
5742 create_browser(struct tab
*t
)
5746 GtkAdjustment
*adjustment
;
5749 show_oops(NULL
, "create_browser invalid parameters");
5753 t
->sb_h
= GTK_SCROLLBAR(gtk_hscrollbar_new(NULL
));
5754 t
->sb_v
= GTK_SCROLLBAR(gtk_vscrollbar_new(NULL
));
5755 t
->adjust_h
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_h
));
5756 t
->adjust_v
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_v
));
5758 w
= gtk_scrolled_window_new(t
->adjust_h
, t
->adjust_v
);
5759 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
5760 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
5762 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
5763 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
5766 t
->settings
= webkit_web_settings_new();
5768 g_object_set(t
->settings
, "default-encoding", encoding
, (char *)NULL
);
5770 if (user_agent
== NULL
) {
5771 g_object_get(G_OBJECT(t
->settings
), "user-agent", &strval
,
5773 t
->user_agent
= g_strdup_printf("%s %s+", strval
, version
);
5776 t
->user_agent
= g_strdup(user_agent
);
5778 t
->stylesheet
= g_strdup_printf("file://%s/style.css", resource_dir
);
5781 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w
));
5782 g_signal_connect(G_OBJECT(adjustment
), "value-changed",
5783 G_CALLBACK(update_statusbar_position
), NULL
);
5792 create_kiosk_toolbar(struct tab
*t
)
5794 GtkWidget
*toolbar
= NULL
, *b
;
5796 b
= gtk_hbox_new(FALSE
, 0);
5798 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
5800 /* backward button */
5801 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
5802 gtk_widget_set_sensitive(t
->backward
, FALSE
);
5803 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
5804 G_CALLBACK(backward_cb
), t
);
5805 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, TRUE
, TRUE
, 0);
5807 /* forward button */
5808 t
->forward
= create_button("Forward", GTK_STOCK_GO_FORWARD
, 0);
5809 gtk_widget_set_sensitive(t
->forward
, FALSE
);
5810 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
5811 G_CALLBACK(forward_cb
), t
);
5812 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, TRUE
, TRUE
, 0);
5815 t
->gohome
= create_button("Home", GTK_STOCK_HOME
, 0);
5816 gtk_widget_set_sensitive(t
->gohome
, true);
5817 g_signal_connect(G_OBJECT(t
->gohome
), "clicked",
5818 G_CALLBACK(home_cb
), t
);
5819 gtk_box_pack_start(GTK_BOX(b
), t
->gohome
, TRUE
, TRUE
, 0);
5821 /* create widgets but don't use them */
5822 t
->uri_entry
= gtk_entry_new();
5823 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
5824 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
5825 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
5831 create_toolbar(struct tab
*t
)
5833 GtkWidget
*toolbar
= NULL
, *b
, *eb1
;
5835 b
= gtk_hbox_new(FALSE
, 0);
5837 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
5839 /* backward button */
5840 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
5841 gtk_widget_set_sensitive(t
->backward
, FALSE
);
5842 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
5843 G_CALLBACK(backward_cb
), t
);
5844 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, FALSE
, FALSE
, 0);
5846 /* forward button */
5847 t
->forward
= create_button("Forward",GTK_STOCK_GO_FORWARD
, 0);
5848 gtk_widget_set_sensitive(t
->forward
, FALSE
);
5849 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
5850 G_CALLBACK(forward_cb
), t
);
5851 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, FALSE
,
5855 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
5856 gtk_widget_set_sensitive(t
->stop
, FALSE
);
5857 g_signal_connect(G_OBJECT(t
->stop
), "clicked",
5858 G_CALLBACK(stop_cb
), t
);
5859 gtk_box_pack_start(GTK_BOX(b
), t
->stop
, FALSE
,
5863 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
5864 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
5865 gtk_widget_set_sensitive(t
->js_toggle
, TRUE
);
5866 g_signal_connect(G_OBJECT(t
->js_toggle
), "clicked",
5867 G_CALLBACK(js_toggle_cb
), t
);
5868 gtk_box_pack_start(GTK_BOX(b
), t
->js_toggle
, FALSE
, FALSE
, 0);
5870 t
->uri_entry
= gtk_entry_new();
5871 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
5872 G_CALLBACK(activate_uri_entry_cb
), t
);
5873 g_signal_connect(G_OBJECT(t
->uri_entry
), "key-press-event",
5874 G_CALLBACK(entry_key_cb
), t
);
5876 eb1
= gtk_hbox_new(FALSE
, 0);
5877 gtk_container_set_border_width(GTK_CONTAINER(eb1
), 1);
5878 gtk_box_pack_start(GTK_BOX(eb1
), t
->uri_entry
, TRUE
, TRUE
, 0);
5879 gtk_box_pack_start(GTK_BOX(b
), eb1
, TRUE
, TRUE
, 0);
5882 if (search_string
) {
5884 t
->search_entry
= gtk_entry_new();
5885 gtk_entry_set_width_chars(GTK_ENTRY(t
->search_entry
), 30);
5886 g_signal_connect(G_OBJECT(t
->search_entry
), "activate",
5887 G_CALLBACK(activate_search_entry_cb
), t
);
5888 g_signal_connect(G_OBJECT(t
->search_entry
), "key-press-event",
5889 G_CALLBACK(entry_key_cb
), t
);
5890 gtk_widget_set_size_request(t
->search_entry
, -1, -1);
5891 eb2
= gtk_hbox_new(FALSE
, 0);
5892 gtk_container_set_border_width(GTK_CONTAINER(eb2
), 1);
5893 gtk_box_pack_start(GTK_BOX(eb2
), t
->search_entry
, TRUE
, TRUE
,
5895 gtk_box_pack_start(GTK_BOX(b
), eb2
, FALSE
, FALSE
, 0);
5902 create_buffers(struct tab
*t
)
5904 GtkCellRenderer
*renderer
;
5907 view
= gtk_tree_view_new();
5909 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view
), FALSE
);
5911 renderer
= gtk_cell_renderer_text_new();
5912 gtk_tree_view_insert_column_with_attributes
5913 (GTK_TREE_VIEW(view
), -1, "Id", renderer
, "text", COL_ID
, (char *)NULL
);
5915 renderer
= gtk_cell_renderer_text_new();
5916 gtk_tree_view_insert_column_with_attributes
5917 (GTK_TREE_VIEW(view
), -1, "Title", renderer
, "text", COL_TITLE
,
5920 gtk_tree_view_set_model
5921 (GTK_TREE_VIEW(view
), GTK_TREE_MODEL(buffers_store
));
5927 row_activated_cb(GtkTreeView
*view
, GtkTreePath
*path
,
5928 GtkTreeViewColumn
*col
, struct tab
*t
)
5933 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5935 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
,
5938 (GTK_TREE_MODEL(buffers_store
), &iter
, COL_ID
, &id
, -1);
5939 set_current_tab(id
- 1);
5945 /* after tab reordering/creation/removal */
5952 TAILQ_FOREACH(t
, &tabs
, entry
) {
5953 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
5954 if (t
->tab_id
> maxid
)
5957 gtk_widget_show(t
->tab_elems
.sep
);
5960 TAILQ_FOREACH(t
, &tabs
, entry
) {
5961 if (t
->tab_id
== maxid
) {
5962 gtk_widget_hide(t
->tab_elems
.sep
);
5968 /* after active tab change */
5970 recolor_compact_tabs(void)
5976 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
5977 TAILQ_FOREACH(t
, &tabs
, entry
)
5978 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
,
5981 curid
= gtk_notebook_get_current_page(notebook
);
5982 TAILQ_FOREACH(t
, &tabs
, entry
)
5983 if (t
->tab_id
== curid
) {
5984 gdk_color_parse(XT_COLOR_CT_ACTIVE
, &color
);
5985 gtk_widget_modify_fg(t
->tab_elems
.label
,
5986 GTK_STATE_NORMAL
, &color
);
5992 set_current_tab(int page_num
)
5994 buffercmd_abort(get_current_tab());
5995 gtk_notebook_set_current_page(notebook
, page_num
);
5996 recolor_compact_tabs();
6000 undo_close_tab_save(struct tab
*t
)
6004 struct undo
*u1
, *u2
;
6006 WebKitWebHistoryItem
*item
;
6008 if ((uri
= get_uri(t
)) == NULL
)
6011 u1
= g_malloc0(sizeof(struct undo
));
6012 u1
->uri
= g_strdup(uri
);
6014 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
6016 m
= webkit_web_back_forward_list_get_forward_length(t
->bfl
);
6017 n
= webkit_web_back_forward_list_get_back_length(t
->bfl
);
6020 /* forward history */
6021 items
= webkit_web_back_forward_list_get_forward_list_with_limit(t
->bfl
, m
);
6025 u1
->history
= g_list_prepend(u1
->history
,
6026 webkit_web_history_item_copy(item
));
6027 items
= g_list_next(items
);
6032 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
6033 u1
->history
= g_list_prepend(u1
->history
,
6034 webkit_web_history_item_copy(item
));
6038 items
= webkit_web_back_forward_list_get_back_list_with_limit(t
->bfl
, n
);
6042 u1
->history
= g_list_prepend(u1
->history
,
6043 webkit_web_history_item_copy(item
));
6044 items
= g_list_next(items
);
6047 TAILQ_INSERT_HEAD(&undos
, u1
, entry
);
6049 if (undo_count
> XT_MAX_UNDO_CLOSE_TAB
) {
6050 u2
= TAILQ_LAST(&undos
, undo_tailq
);
6051 TAILQ_REMOVE(&undos
, u2
, entry
);
6053 g_list_free(u2
->history
);
6062 delete_tab(struct tab
*t
)
6066 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
6072 * no need to join thread here because it won't access t on completion
6075 TAILQ_REMOVE(&tabs
, t
, entry
);
6078 /* Halt all webkit activity. */
6079 abort_favicon_download(t
);
6080 webkit_web_view_stop_loading(t
->wv
);
6082 /* Save the tab, so we can undo the close. */
6083 undo_close_tab_save(t
);
6087 g_source_remove(t
->search_id
);
6090 bzero(&a
, sizeof a
);
6092 inspector_cmd(t
, &a
);
6094 if (browser_mode
== XT_BM_KIOSK
) {
6095 gtk_widget_destroy(t
->uri_entry
);
6096 gtk_widget_destroy(t
->stop
);
6097 gtk_widget_destroy(t
->js_toggle
);
6100 gtk_widget_destroy(t
->tab_elems
.eventbox
);
6101 gtk_widget_destroy(t
->vbox
);
6103 g_free(t
->user_agent
);
6104 g_free(t
->stylesheet
);
6109 if (TAILQ_EMPTY(&tabs
)) {
6110 if (browser_mode
== XT_BM_KIOSK
)
6111 create_new_tab(home
, NULL
, 1, -1);
6113 create_new_tab(NULL
, NULL
, 1, -1);
6116 /* recreate session */
6117 if (session_autosave
) {
6118 bzero(&a
, sizeof a
);
6120 save_tabs(NULL
, &a
);
6124 recolor_compact_tabs();
6128 update_statusbar_zoom(struct tab
*t
)
6131 char s
[16] = { '\0' };
6133 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6134 if ((zoom
<= 0.99 || zoom
>= 1.01))
6135 snprintf(s
, sizeof s
, "%d%%", (int)(zoom
* 100));
6136 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.zoom
), s
);
6140 setzoom_webkit(struct tab
*t
, int adjust
)
6142 #define XT_ZOOMPERCENT 0.04
6147 show_oops(NULL
, "setzoom_webkit invalid parameters");
6151 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6152 if (adjust
== XT_ZOOM_IN
)
6153 zoom
+= XT_ZOOMPERCENT
;
6154 else if (adjust
== XT_ZOOM_OUT
)
6155 zoom
-= XT_ZOOMPERCENT
;
6156 else if (adjust
> 0)
6157 zoom
= default_zoom_level
+ adjust
/ 100.0 - 1.0;
6159 show_oops(t
, "setzoom_webkit invalid zoom value");
6163 if (zoom
< XT_ZOOMPERCENT
)
6164 zoom
= XT_ZOOMPERCENT
;
6165 g_object_set(G_OBJECT(t
->wv
), "zoom-level", zoom
, (char *)NULL
);
6166 update_statusbar_zoom(t
);
6170 tab_clicked_cb(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
6172 struct tab
*t
= (struct tab
*) data
;
6174 DNPRINTF(XT_D_TAB
, "tab_clicked_cb: tab: %d\n", t
->tab_id
);
6176 switch (event
->button
) {
6178 set_current_tab(t
->tab_id
);
6189 append_tab(struct tab
*t
)
6194 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
6195 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
6199 create_sbe(int width
)
6203 sbe
= gtk_entry_new();
6204 gtk_entry_set_inner_border(GTK_ENTRY(sbe
), NULL
);
6205 gtk_entry_set_has_frame(GTK_ENTRY(sbe
), FALSE
);
6206 gtk_widget_set_can_focus(GTK_WIDGET(sbe
), FALSE
);
6207 gtk_widget_modify_font(GTK_WIDGET(sbe
), statusbar_font
);
6208 gtk_entry_set_alignment(GTK_ENTRY(sbe
), 1.0);
6209 gtk_widget_set_size_request(sbe
, width
, -1);
6215 create_new_tab(char *title
, struct undo
*u
, int focus
, int position
)
6220 WebKitWebHistoryItem
*item
;
6224 int sbe_p
= 0, sbe_b
= 0,
6227 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
6229 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
6230 DNPRINTF(XT_D_TAB
, "create_new_tab: new tab rejected\n");
6234 t
= g_malloc0(sizeof *t
);
6236 if (title
== NULL
) {
6237 title
= "(untitled)";
6241 t
->vbox
= gtk_vbox_new(FALSE
, 0);
6243 /* label + button for tab */
6244 b
= gtk_hbox_new(FALSE
, 0);
6247 #if GTK_CHECK_VERSION(2, 20, 0)
6248 t
->spinner
= gtk_spinner_new();
6250 t
->label
= gtk_label_new(title
);
6251 bb
= create_button("Close", GTK_STOCK_CLOSE
, 1);
6252 gtk_widget_set_size_request(t
->label
, 100, 0);
6253 gtk_label_set_max_width_chars(GTK_LABEL(t
->label
), 20);
6254 gtk_label_set_ellipsize(GTK_LABEL(t
->label
), PANGO_ELLIPSIZE_END
);
6255 gtk_widget_set_size_request(b
, 130, 0);
6257 gtk_box_pack_start(GTK_BOX(b
), bb
, FALSE
, FALSE
, 0);
6258 gtk_box_pack_start(GTK_BOX(b
), t
->label
, FALSE
, FALSE
, 0);
6259 #if GTK_CHECK_VERSION(2, 20, 0)
6260 gtk_box_pack_start(GTK_BOX(b
), t
->spinner
, FALSE
, FALSE
, 0);
6264 if (browser_mode
== XT_BM_KIOSK
) {
6265 t
->toolbar
= create_kiosk_toolbar(t
);
6266 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
6269 t
->toolbar
= create_toolbar(t
);
6271 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
,
6279 t
->browser_win
= create_browser(t
);
6280 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
6282 /* oops message for user feedback */
6283 t
->oops
= gtk_entry_new();
6284 gtk_entry_set_inner_border(GTK_ENTRY(t
->oops
), NULL
);
6285 gtk_entry_set_has_frame(GTK_ENTRY(t
->oops
), FALSE
);
6286 gtk_widget_set_can_focus(GTK_WIDGET(t
->oops
), FALSE
);
6287 gdk_color_parse(XT_COLOR_RED
, &color
);
6288 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
6289 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
6290 gtk_widget_modify_font(GTK_WIDGET(t
->oops
), oops_font
);
6293 t
->cmd
= gtk_entry_new();
6294 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
6295 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
6296 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
6297 gtk_widget_modify_font(GTK_WIDGET(t
->cmd
), cmd_font
);
6300 t
->statusbar_box
= gtk_hbox_new(FALSE
, 0);
6302 t
->sbe
.statusbar
= gtk_entry_new();
6303 gtk_entry_set_inner_border(GTK_ENTRY(t
->sbe
.statusbar
), NULL
);
6304 gtk_entry_set_has_frame(GTK_ENTRY(t
->sbe
.statusbar
), FALSE
);
6305 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.statusbar
), FALSE
);
6306 gtk_widget_modify_font(GTK_WIDGET(t
->sbe
.statusbar
), statusbar_font
);
6308 /* create these widgets only if specified in statusbar_elems */
6310 t
->sbe
.position
= create_sbe(40);
6311 t
->sbe
.zoom
= create_sbe(40);
6312 t
->sbe
.buffercmd
= create_sbe(60);
6314 statusbar_modify_attr(t
, XT_COLOR_WHITE
, XT_COLOR_BLACK
);
6316 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), t
->sbe
.statusbar
, TRUE
,
6319 /* gtk widgets cannot be added to a box twice. sbe_* variables
6320 make sure of this */
6321 for (p
= statusbar_elems
; *p
!= '\0'; p
++) {
6325 GtkWidget
*sep
= gtk_vseparator_new();
6327 gdk_color_parse(XT_COLOR_SB_SEPARATOR
, &color
);
6328 gtk_widget_modify_bg(sep
, GTK_STATE_NORMAL
, &color
);
6329 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), sep
,
6330 FALSE
, FALSE
, FALSE
);
6335 warnx("flag \"%c\" specified more than "
6336 "once in statusbar_elems\n", *p
);
6340 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6341 t
->sbe
.position
, FALSE
, FALSE
, FALSE
);
6345 warnx("flag \"%c\" specified more than "
6346 "once in statusbar_elems\n", *p
);
6350 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6351 t
->sbe
.buffercmd
, FALSE
, FALSE
, FALSE
);
6355 warnx("flag \"%c\" specified more than "
6356 "once in statusbar_elems\n", *p
);
6360 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6361 t
->sbe
.zoom
, FALSE
, FALSE
, FALSE
);
6364 warnx("illegal flag \"%c\" in statusbar_elems\n", *p
);
6369 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar_box
, FALSE
, FALSE
, 0);
6372 t
->buffers
= create_buffers(t
);
6373 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->buffers
, FALSE
, FALSE
, 0);
6375 /* xtp meaning is normal by default */
6376 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
6378 /* set empty favicon */
6379 xt_icon_from_name(t
, "text-html");
6381 /* and show it all */
6382 gtk_widget_show_all(b
);
6383 gtk_widget_show_all(t
->vbox
);
6385 /* compact tab bar */
6386 t
->tab_elems
.label
= gtk_label_new(title
);
6387 gtk_label_set_width_chars(GTK_LABEL(t
->tab_elems
.label
), 1.0);
6388 gtk_misc_set_alignment(GTK_MISC(t
->tab_elems
.label
), 0.0, 0.0);
6389 gtk_misc_set_padding(GTK_MISC(t
->tab_elems
.label
), 4.0, 4.0);
6390 gtk_widget_modify_font(GTK_WIDGET(t
->tab_elems
.label
), tabbar_font
);
6392 t
->tab_elems
.eventbox
= gtk_event_box_new();
6393 t
->tab_elems
.box
= gtk_hbox_new(FALSE
, 0);
6394 t
->tab_elems
.sep
= gtk_vseparator_new();
6396 gdk_color_parse(XT_COLOR_CT_BACKGROUND
, &color
);
6397 gtk_widget_modify_bg(t
->tab_elems
.eventbox
, GTK_STATE_NORMAL
, &color
);
6398 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6399 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
, &color
);
6400 gdk_color_parse(XT_COLOR_CT_SEPARATOR
, &color
);
6401 gtk_widget_modify_bg(t
->tab_elems
.sep
, GTK_STATE_NORMAL
, &color
);
6403 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.label
, TRUE
,
6405 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.sep
, FALSE
,
6407 gtk_container_add(GTK_CONTAINER(t
->tab_elems
.eventbox
),
6410 gtk_box_pack_start(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
, TRUE
,
6412 gtk_widget_show_all(t
->tab_elems
.eventbox
);
6414 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
6417 id
= position
>= 0 ? position
:
6418 gtk_notebook_get_current_page(notebook
) + 1;
6419 if (id
> gtk_notebook_get_n_pages(notebook
))
6422 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
6423 gtk_notebook_insert_page(notebook
, t
->vbox
, b
, id
);
6424 gtk_box_reorder_child(GTK_BOX(tab_bar
),
6425 t
->tab_elems
.eventbox
, id
);
6430 #if GTK_CHECK_VERSION(2, 20, 0)
6431 /* turn spinner off if we are a new tab without uri */
6433 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
6434 gtk_widget_hide(t
->spinner
);
6437 /* make notebook tabs reorderable */
6438 gtk_notebook_set_tab_reorderable(notebook
, t
->vbox
, TRUE
);
6440 /* compact tabs clickable */
6441 g_signal_connect(G_OBJECT(t
->tab_elems
.eventbox
),
6442 "button_press_event", G_CALLBACK(tab_clicked_cb
), t
);
6444 g_object_connect(G_OBJECT(t
->cmd
),
6445 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb
), t
,
6446 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
6447 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb
), t
,
6448 "signal::activate", G_CALLBACK(cmd_activate_cb
), t
,
6449 "signal::populate-popup", G_CALLBACK(cmd_popup_cb
), t
,
6452 /* reuse wv_button_cb to hide oops */
6453 g_object_connect(G_OBJECT(t
->oops
),
6454 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
6457 g_signal_connect(t
->buffers
,
6458 "row-activated", G_CALLBACK(row_activated_cb
), t
);
6459 g_object_connect(G_OBJECT(t
->buffers
),
6460 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
, (char *)NULL
);
6462 g_object_connect(G_OBJECT(t
->wv
),
6463 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
,
6464 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb
), t
,
6465 "signal::download-requested", G_CALLBACK(webview_download_cb
), t
,
6466 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), t
,
6467 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
6468 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
6469 "signal::create-web-view", G_CALLBACK(webview_cwv_cb
), t
,
6470 "signal::close-web-view", G_CALLBACK(webview_closewv_cb
), t
,
6471 "signal::event", G_CALLBACK(webview_event_cb
), t
,
6472 "signal::load-finished", G_CALLBACK(webview_load_finished_cb
), t
,
6473 "signal::load-progress-changed", G_CALLBACK(webview_progress_changed_cb
), t
,
6474 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb
), t
,
6475 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
6476 "signal::button_release_event", G_CALLBACK(wv_release_button_cb
), t
,
6477 "signal::populate-popup", G_CALLBACK(wv_popup_cb
), t
,
6479 g_signal_connect(t
->wv
,
6480 "notify::load-status", G_CALLBACK(notify_load_status_cb
), t
);
6481 g_signal_connect(t
->wv
,
6482 "notify::title", G_CALLBACK(notify_title_cb
), t
);
6484 /* hijack the unused keys as if we were the browser */
6485 //g_object_connect(G_OBJECT(t->toolbar),
6486 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
6489 g_signal_connect(G_OBJECT(bb
), "button_press_event",
6490 G_CALLBACK(tab_close_cb
), t
);
6493 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
6494 /* restore the tab's history */
6495 if (u
&& u
->history
) {
6499 webkit_web_back_forward_list_add_item(t
->bfl
, item
);
6500 items
= g_list_next(items
);
6503 item
= g_list_nth_data(u
->history
, u
->back
);
6505 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
6508 g_list_free(u
->history
);
6510 webkit_web_back_forward_list_clear(t
->bfl
);
6516 url_set_visibility();
6517 statusbar_set_visibility();
6520 set_current_tab(t
->tab_id
);
6521 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
6525 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
6529 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6536 recolor_compact_tabs();
6537 setzoom_webkit(t
, XT_ZOOM_NORMAL
);
6542 notebook_switchpage_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
6548 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
6550 if (gtk_notebook_get_current_page(notebook
) == -1)
6553 TAILQ_FOREACH(t
, &tabs
, entry
) {
6554 if (t
->tab_id
== pn
) {
6555 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
6558 uri
= get_title(t
, TRUE
);
6559 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
6565 /* can't use focus_webview here */
6566 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6573 notebook_pagereordered_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
6576 struct tab
*t
= NULL
, *tt
;
6580 TAILQ_FOREACH(tt
, &tabs
, entry
)
6581 if (tt
->tab_id
== pn
) {
6587 DNPRINTF(XT_D_TAB
, "page_reordered_cb: tab: %d\n", t
->tab_id
);
6589 gtk_box_reorder_child(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
,
6594 menuitem_response(struct tab
*t
)
6596 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
6600 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
6602 GtkWidget
*menu
, *menu_items
;
6603 GdkEventButton
*bevent
;
6607 if (event
->type
== GDK_BUTTON_PRESS
) {
6608 bevent
= (GdkEventButton
*) event
;
6609 menu
= gtk_menu_new();
6611 TAILQ_FOREACH(ti
, &tabs
, entry
) {
6612 if ((uri
= get_uri(ti
)) == NULL
)
6613 /* XXX make sure there is something to print */
6614 /* XXX add gui pages in here to look purdy */
6616 menu_items
= gtk_menu_item_new_with_label(uri
);
6617 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), menu_items
);
6618 gtk_widget_show(menu_items
);
6620 g_signal_connect_swapped((menu_items
),
6621 "activate", G_CALLBACK(menuitem_response
),
6625 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
,
6626 bevent
->button
, bevent
->time
);
6628 /* unref object so it'll free itself when popped down */
6629 #if !GTK_CHECK_VERSION(3, 0, 0)
6630 /* XXX does not need unref with gtk+3? */
6631 g_object_ref_sink(menu
);
6632 g_object_unref(menu
);
6635 return (TRUE
/* eat event */);
6638 return (FALSE
/* propagate */);
6642 icon_size_map(int iconsz
)
6644 if (iconsz
<= GTK_ICON_SIZE_INVALID
||
6645 iconsz
> GTK_ICON_SIZE_DIALOG
)
6646 return (GTK_ICON_SIZE_SMALL_TOOLBAR
);
6652 create_button(char *name
, char *stockid
, int size
)
6654 GtkWidget
*button
, *image
;
6658 rcstring
= g_strdup_printf(
6659 "style \"%s-style\"\n"
6661 " GtkWidget::focus-padding = 0\n"
6662 " GtkWidget::focus-line-width = 0\n"
6666 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
6667 gtk_rc_parse_string(rcstring
);
6669 button
= gtk_button_new();
6670 gtk_button_set_focus_on_click(GTK_BUTTON(button
), FALSE
);
6671 gtk_icon_size
= icon_size_map(size
? size
: icon_size
);
6673 image
= gtk_image_new_from_stock(stockid
, gtk_icon_size
);
6674 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
6675 gtk_container_set_border_width(GTK_CONTAINER(button
), 1);
6676 gtk_container_add(GTK_CONTAINER(button
), GTK_WIDGET(image
));
6677 gtk_widget_set_name(button
, name
);
6678 gtk_button_set_relief(GTK_BUTTON(button
), GTK_RELIEF_NONE
);
6684 button_set_stockid(GtkWidget
*button
, char *stockid
)
6688 image
= gtk_image_new_from_stock(stockid
, icon_size_map(icon_size
));
6689 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
6690 gtk_button_set_image(GTK_BUTTON(button
), image
);
6694 clipb_primary_cb(GtkClipboard
*primary
, GdkEvent
*event
, gpointer notused
)
6697 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
6700 if (xterm_workaround
== 0)
6704 * xterm doesn't play nice with clipboards because it clears the
6705 * primary when clicked. We rely on primary being set to properly
6706 * handle middle mouse button clicks (paste). So when someone clears
6707 * primary copy whatever is in CUT_BUFFER0 into primary to simualte
6708 * other application behavior (as in DON'T clear primary).
6711 p
= gtk_clipboard_wait_for_text(primary
);
6713 if (gdk_property_get(gdk_get_default_root_window(),
6715 gdk_atom_intern("STRING", FALSE
),
6717 1024 * 1024 /* picked out of my butt */,
6723 /* yes sir, we need to NUL the string */
6725 gtk_clipboard_set_text(primary
, p
, -1);
6739 char file
[PATH_MAX
];
6742 vbox
= gtk_vbox_new(FALSE
, 0);
6743 gtk_box_set_spacing(GTK_BOX(vbox
), 0);
6744 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
6745 #if !GTK_CHECK_VERSION(3, 0, 0)
6746 /* XXX seems to be needed with gtk+2 */
6747 gtk_notebook_set_tab_hborder(notebook
, 0);
6748 gtk_notebook_set_tab_vborder(notebook
, 0);
6750 gtk_notebook_set_scrollable(notebook
, TRUE
);
6751 gtk_notebook_set_show_border(notebook
, FALSE
);
6752 gtk_widget_set_can_focus(GTK_WIDGET(notebook
), FALSE
);
6754 abtn
= gtk_button_new();
6755 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
6756 gtk_widget_set_size_request(arrow
, -1, -1);
6757 gtk_container_add(GTK_CONTAINER(abtn
), arrow
);
6758 gtk_widget_set_size_request(abtn
, -1, 20);
6760 #if GTK_CHECK_VERSION(2, 20, 0)
6761 gtk_notebook_set_action_widget(notebook
, abtn
, GTK_PACK_END
);
6763 gtk_widget_set_size_request(GTK_WIDGET(notebook
), -1, -1);
6765 /* compact tab bar */
6766 tab_bar
= gtk_hbox_new(TRUE
, 0);
6768 gtk_box_pack_start(GTK_BOX(vbox
), tab_bar
, FALSE
, FALSE
, 0);
6769 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
6770 gtk_widget_set_size_request(vbox
, -1, -1);
6772 g_object_connect(G_OBJECT(notebook
),
6773 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb
), NULL
,
6775 g_object_connect(G_OBJECT(notebook
),
6776 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb
),
6777 NULL
, (char *)NULL
);
6778 g_signal_connect(G_OBJECT(abtn
), "button_press_event",
6779 G_CALLBACK(arrow_cb
), NULL
);
6781 main_window
= create_window("xxxterm");
6782 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
6783 g_signal_connect(G_OBJECT(main_window
), "delete_event",
6784 G_CALLBACK(gtk_main_quit
), NULL
);
6787 for (i
= 0; i
< LENGTH(icons
); i
++) {
6788 snprintf(file
, sizeof file
, "%s/%s", resource_dir
, icons
[i
]);
6789 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
6790 l
= g_list_append(l
, pb
);
6792 gtk_window_set_default_icon_list(l
);
6794 /* clipboard work around */
6795 if (xterm_workaround
)
6797 G_OBJECT(gtk_clipboard_get(GDK_SELECTION_PRIMARY
)),
6798 "owner-change", G_CALLBACK(clipb_primary_cb
), NULL
);
6800 gtk_widget_show_all(abtn
);
6801 gtk_widget_show_all(main_window
);
6802 notebook_tab_set_visibility();
6806 send_cmd_to_socket(char *cmd
)
6809 struct sockaddr_un sa
;
6811 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
6812 warnx("%s: socket", __func__
);
6816 sa
.sun_family
= AF_UNIX
;
6817 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
6818 work_dir
, XT_SOCKET_FILE
);
6821 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
6822 warnx("%s: connect", __func__
);
6826 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
6827 warnx("%s: send", __func__
);
6838 socket_watcher(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
6841 char str
[XT_MAX_URL_LENGTH
];
6842 socklen_t t
= sizeof(struct sockaddr_un
);
6843 struct sockaddr_un sa
;
6848 gint fd
= g_io_channel_unix_get_fd(source
);
6850 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
6855 if (getpeereid(s
, &uid
, &gid
) == -1) {
6859 if (uid
!= getuid() || gid
!= getgid()) {
6860 warnx("unauthorized user");
6866 warnx("not a valid user");
6870 n
= recv(s
, str
, sizeof(str
), 0);
6874 tt
= TAILQ_LAST(&tabs
, tab_list
);
6875 cmd_execute(tt
, str
);
6883 struct sockaddr_un sa
;
6885 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
6886 warn("is_running: socket");
6890 sa
.sun_family
= AF_UNIX
;
6891 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
6892 work_dir
, XT_SOCKET_FILE
);
6895 /* connect to see if there is a listener */
6896 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
6897 rv
= 0; /* not running */
6899 rv
= 1; /* already running */
6910 struct sockaddr_un sa
;
6912 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
6913 warn("build_socket: socket");
6917 sa
.sun_family
= AF_UNIX
;
6918 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
6919 work_dir
, XT_SOCKET_FILE
);
6922 /* connect to see if there is a listener */
6923 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
6924 /* no listener so we will */
6925 unlink(sa
.sun_path
);
6927 if (bind(s
, (struct sockaddr
*)&sa
, len
) == -1) {
6928 warn("build_socket: bind");
6932 if (listen(s
, 1) == -1) {
6933 warn("build_socket: listen");
6946 completion_select_cb(GtkEntryCompletion
*widget
, GtkTreeModel
*model
,
6947 GtkTreeIter
*iter
, struct tab
*t
)
6951 gtk_tree_model_get(model
, iter
, 0, &value
, -1);
6959 completion_hover_cb(GtkEntryCompletion
*widget
, GtkTreeModel
*model
,
6960 GtkTreeIter
*iter
, struct tab
*t
)
6964 gtk_tree_model_get(model
, iter
, 0, &value
, -1);
6965 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), value
);
6966 gtk_editable_set_position(GTK_EDITABLE(t
->uri_entry
), -1);
6973 completion_add_uri(const gchar
*uri
)
6977 /* add uri to list_store */
6978 gtk_list_store_append(completion_model
, &iter
);
6979 gtk_list_store_set(completion_model
, &iter
, 0, uri
, -1);
6983 completion_match(GtkEntryCompletion
*completion
, const gchar
*key
,
6984 GtkTreeIter
*iter
, gpointer user_data
)
6987 gboolean match
= FALSE
;
6989 gtk_tree_model_get(GTK_TREE_MODEL(completion_model
), iter
, 0, &value
,
6995 match
= match_uri(value
, key
);
7002 completion_add(struct tab
*t
)
7004 /* enable completion for tab */
7005 t
->completion
= gtk_entry_completion_new();
7006 gtk_entry_completion_set_text_column(t
->completion
, 0);
7007 gtk_entry_set_completion(GTK_ENTRY(t
->uri_entry
), t
->completion
);
7008 gtk_entry_completion_set_model(t
->completion
,
7009 GTK_TREE_MODEL(completion_model
));
7010 gtk_entry_completion_set_match_func(t
->completion
, completion_match
,
7012 gtk_entry_completion_set_minimum_key_length(t
->completion
, 1);
7013 gtk_entry_completion_set_inline_selection(t
->completion
, TRUE
);
7014 g_signal_connect(G_OBJECT (t
->completion
), "match-selected",
7015 G_CALLBACK(completion_select_cb
), t
);
7016 g_signal_connect(G_OBJECT (t
->completion
), "cursor-on-match",
7017 G_CALLBACK(completion_hover_cb
), t
);
7025 if (stat(dir
, &sb
)) {
7026 if (mkdir(dir
, S_IRWXU
) == -1)
7027 err(1, "mkdir %s", dir
);
7029 err(1, "stat %s", dir
);
7031 if (S_ISDIR(sb
.st_mode
) == 0)
7032 errx(1, "%s not a dir", dir
);
7033 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
7034 warnx("fixing invalid permissions on %s", dir
);
7035 if (chmod(dir
, S_IRWXU
) == -1)
7036 err(1, "chmod %s", dir
);
7044 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
7048 GStaticRecMutex my_gdk_mtx
= G_STATIC_REC_MUTEX_INIT
;
7049 volatile int mtx_depth
;
7053 * The linux flash plugin violates the gdk locking mechanism.
7054 * Work around the issue by using a recursive mutex with some match applied
7055 * to see if we hit a buggy condition.
7057 * The following code is painful so just don't read it. It really doesn't
7058 * make much sense but seems to work.
7063 g_static_rec_mutex_lock(&my_gdk_mtx
);
7066 if (mtx_depth
<= 0) {
7067 /* should not happen */
7068 show_oops(NULL
, "negative mutex locking bug, trying to "
7070 fprintf(stderr
, "negative mutex locking bug, trying to "
7072 g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
7073 g_static_rec_mutex_lock(&my_gdk_mtx
);
7078 if (mtx_depth
!= 1) {
7079 /* decrease mutext depth to 1 */
7081 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7083 } while (mtx_depth
> 1);
7092 /* if mutex depth isn't 1 then something went bad */
7093 if (mtx_depth
!= 1) {
7094 x
= g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
7096 /* should not happen */
7097 show_oops(NULL
, "mutex unlocking bug, trying to "
7099 fprintf(stderr
, "mutex unlocking bug, trying to "
7103 if (mtx_complain
== 0) {
7104 show_oops(NULL
, "buggy mutex implementation detected, "
7105 "work around implemented");
7106 fprintf(stderr
, "buggy mutex implementation detected, "
7107 "work around implemented");
7114 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7118 main(int argc
, char *argv
[])
7121 int c
, s
, optn
= 0, opte
= 0, focus
= 1;
7122 char conf
[PATH_MAX
] = { '\0' };
7123 char file
[PATH_MAX
];
7124 char *env_proxy
= NULL
;
7128 struct sigaction sact
;
7129 GIOChannel
*channel
;
7136 g_thread_init(NULL
);
7137 gdk_threads_set_lock_functions(mtx_lock
, mtx_unlock
);
7139 gdk_threads_enter();
7141 gcry_control (GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
7143 gtk_init(&argc
, &argv
);
7145 gnutls_global_init();
7147 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
7152 RB_INIT(&downloads
);
7154 TAILQ_INIT(&sessions
);
7157 TAILQ_INIT(&aliases
);
7164 /* fiddle with ulimits */
7165 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7168 /* just use them all */
7169 rlp
.rlim_cur
= rlp
.rlim_max
;
7170 if (setrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7172 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7174 else if (rlp
.rlim_cur
<= 256)
7175 startpage_add("%s requires at least 256 file "
7176 "descriptors, currently it has up to %d available",
7177 __progname
, rlp
.rlim_cur
);
7180 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
7189 #ifdef XXXTERM_BUILDSTR
7190 errx(0 , "Version: %s Build: %s",
7191 version
, XXXTERM_BUILDSTR
);
7193 errx(0 , "Version: %s", version
);
7197 strlcpy(conf
, optarg
, sizeof(conf
));
7200 strlcpy(named_session
, optarg
, sizeof(named_session
));
7221 xtp_generate_keys();
7224 bzero(&sact
, sizeof(sact
));
7225 sigemptyset(&sact
.sa_mask
);
7226 sact
.sa_handler
= sigchild
;
7227 sact
.sa_flags
= SA_NOCLDSTOP
;
7228 sigaction(SIGCHLD
, &sact
, NULL
);
7230 /* set download dir */
7231 pwd
= getpwuid(getuid());
7233 errx(1, "invalid user %d", getuid());
7234 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
7236 /* compile buffer command regexes */
7239 /* set default string settings */
7240 home
= g_strdup("https://www.cyphertite.com");
7241 search_string
= g_strdup("https://ssl.scroogle.org/cgi-bin/nbbwssl.cgi?Gw=%s");
7242 resource_dir
= g_strdup("/usr/local/share/xxxterm/");
7243 strlcpy(runtime_settings
, "runtime", sizeof runtime_settings
);
7244 cmd_font_name
= g_strdup("monospace normal 9");
7245 oops_font_name
= g_strdup("monospace normal 9");
7246 statusbar_font_name
= g_strdup("monospace normal 9");
7247 tabbar_font_name
= g_strdup("monospace normal 9");
7248 statusbar_elems
= g_strdup("BP");
7249 encoding
= g_strdup("UTF-8");
7251 /* read config file */
7252 if (strlen(conf
) == 0)
7253 snprintf(conf
, sizeof conf
, "%s/.%s",
7254 pwd
->pw_dir
, XT_CONF_FILE
);
7255 config_parse(conf
, 0);
7258 cmd_font
= pango_font_description_from_string(cmd_font_name
);
7259 oops_font
= pango_font_description_from_string(oops_font_name
);
7260 statusbar_font
= pango_font_description_from_string(statusbar_font_name
);
7261 tabbar_font
= pango_font_description_from_string(tabbar_font_name
);
7263 /* working directory */
7264 if (strlen(work_dir
) == 0)
7265 snprintf(work_dir
, sizeof work_dir
, "%s/%s",
7266 pwd
->pw_dir
, XT_DIR
);
7269 /* icon cache dir */
7270 snprintf(cache_dir
, sizeof cache_dir
, "%s/%s", work_dir
, XT_CACHE_DIR
);
7274 snprintf(certs_dir
, sizeof certs_dir
, "%s/%s", work_dir
, XT_CERT_DIR
);
7278 snprintf(sessions_dir
, sizeof sessions_dir
, "%s/%s",
7279 work_dir
, XT_SESSIONS_DIR
);
7280 xxx_dir(sessions_dir
);
7283 snprintf(js_dir
, sizeof js_dir
, "%s/%s", work_dir
, XT_JS_DIR
);
7286 /* runtime settings that can override config file */
7287 if (runtime_settings
[0] != '\0')
7288 config_parse(runtime_settings
, 1);
7291 if (!strcmp(download_dir
, pwd
->pw_dir
))
7292 strlcat(download_dir
, "/downloads", sizeof download_dir
);
7293 xxx_dir(download_dir
);
7295 /* favorites file */
7296 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_FAVS_FILE
);
7297 if (stat(file
, &sb
)) {
7298 warnx("favorites file doesn't exist, creating it");
7299 if ((f
= fopen(file
, "w")) == NULL
)
7300 err(1, "favorites");
7304 /* quickmarks file */
7305 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_QMARKS_FILE
);
7306 if (stat(file
, &sb
)) {
7307 warnx("quickmarks file doesn't exist, creating it");
7308 if ((f
= fopen(file
, "w")) == NULL
)
7309 err(1, "quickmarks");
7313 /* search history */
7314 if (history_autosave
) {
7315 snprintf(search_file
, sizeof search_file
, "%s/%s",
7316 work_dir
, XT_SEARCH_FILE
);
7317 if (stat(search_file
, &sb
)) {
7318 warnx("search history file doesn't exist, creating it");
7319 if ((f
= fopen(search_file
, "w")) == NULL
)
7320 err(1, "search_history");
7323 history_read(&shl
, search_file
, &search_history_count
);
7326 /* command history */
7327 if (history_autosave
) {
7328 snprintf(command_file
, sizeof command_file
, "%s/%s",
7329 work_dir
, XT_COMMAND_FILE
);
7330 if (stat(command_file
, &sb
)) {
7331 warnx("command history file doesn't exist, creating it");
7332 if ((f
= fopen(command_file
, "w")) == NULL
)
7333 err(1, "command_history");
7336 history_read(&chl
, command_file
, &cmd_history_count
);
7340 session
= webkit_get_default_session();
7345 if (stat(ssl_ca_file
, &sb
)) {
7346 warnx("no CA file: %s", ssl_ca_file
);
7347 g_free(ssl_ca_file
);
7350 g_object_set(session
,
7351 SOUP_SESSION_SSL_CA_FILE
, ssl_ca_file
,
7352 SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
7356 /* guess_search regex */
7357 if (url_regex
== NULL
)
7358 url_regex
= g_strdup(XT_URL_REGEX
);
7360 if (regcomp(&url_re
, url_regex
, REG_EXTENDED
| REG_NOSUB
))
7361 startpage_add("invalid url regex %s", url_regex
);
7364 env_proxy
= getenv("http_proxy");
7366 setup_proxy(env_proxy
);
7368 env_proxy
= getenv("HTTP_PROXY");
7370 setup_proxy(env_proxy
);
7372 setup_proxy(http_proxy
);
7376 send_cmd_to_socket(argv
[0]);
7380 /* set some connection parameters */
7381 g_object_set(session
, "max-conns", max_connections
, (char *)NULL
);
7382 g_object_set(session
, "max-conns-per-host", max_host_connections
,
7385 /* see if there is already an xxxterm running */
7386 if (single_instance
&& is_running()) {
7388 warnx("already running");
7393 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
7394 send_cmd_to_socket(cmd
);
7404 /* uri completion */
7405 completion_model
= gtk_list_store_new(1, G_TYPE_STRING
);
7408 buffers_store
= gtk_list_store_new
7409 (NUM_COLS
, G_TYPE_UINT
, G_TYPE_STRING
);
7415 notebook_tab_set_visibility();
7417 if (save_global_history
)
7418 restore_global_history();
7420 /* restore session list */
7421 restore_sessions_list();
7423 if (!strcmp(named_session
, XT_SAVED_TABS_FILE
))
7424 restore_saved_tabs();
7426 a
.s
= named_session
;
7427 a
.i
= XT_SES_DONOTHING
;
7428 open_tabs(NULL
, &a
);
7431 /* see if we have an exception */
7432 if (!TAILQ_EMPTY(&spl
)) {
7433 create_new_tab("about:startpage", NULL
, focus
, -1);
7438 create_new_tab(argv
[0], NULL
, focus
, -1);
7445 if (TAILQ_EMPTY(&tabs
))
7446 create_new_tab(home
, NULL
, 1, -1);
7449 if ((s
= build_socket()) != -1) {
7450 channel
= g_io_channel_unix_new(s
);
7451 g_io_add_watch(channel
, G_IO_IN
, socket_watcher
, NULL
);
7457 gdk_threads_leave();
7458 g_static_rec_mutex_unlock_full(&my_gdk_mtx
); /* just in case */
7461 gnutls_global_deinit();