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, 2012 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.
25 char *version
= XXXTERM_VERSION
;
28 uint32_t swm_debug
= 0
51 GCRY_THREAD_OPTION_PTHREAD_IMPL
;
63 TAILQ_ENTRY(session
) entry
;
66 TAILQ_HEAD(session_list
, session
);
69 TAILQ_ENTRY(undo
) entry
;
72 int back
; /* Keeps track of how many back
73 * history items there are. */
75 TAILQ_HEAD(undo_tailq
, undo
);
77 struct command_entry
{
79 TAILQ_ENTRY(command_entry
) entry
;
81 TAILQ_HEAD(command_list
, command_entry
);
84 #define XT_DIR (".xxxterm")
85 #define XT_CACHE_DIR ("cache")
86 #define XT_CERT_DIR ("certs")
87 #define XT_JS_DIR ("js")
88 #define XT_SESSIONS_DIR ("sessions")
89 #define XT_TEMP_DIR ("tmp")
90 #define XT_CONF_FILE ("xxxterm.conf")
91 #define XT_QMARKS_FILE ("quickmarks")
92 #define XT_SAVED_TABS_FILE ("main_session")
93 #define XT_RESTART_TABS_FILE ("restart_tabs")
94 #define XT_SOCKET_FILE ("socket")
95 #define XT_SAVE_SESSION_ID ("SESSION_NAME=")
96 #define XT_SEARCH_FILE ("search_history")
97 #define XT_COMMAND_FILE ("command_history")
98 #define XT_DLMAN_REFRESH "10"
99 #define XT_MAX_URL_LENGTH (4096) /* 1 page is atomic, don't make bigger */
100 #define XT_MAX_UNDO_CLOSE_TAB (32)
101 #define XT_RESERVED_CHARS "$&+,/:;=?@ \"<>#%%{}|^~[]`"
102 #define XT_PRINT_EXTRA_MARGIN 10
103 #define XT_URL_REGEX ("^[[:blank:]]*[^[:blank:]]*([[:alnum:]-]+\\.)+[[:alnum:]-][^[:blank:]]*[[:blank:]]*$")
104 #define XT_INVALID_MARK (-1) /* XXX this is a double, maybe use something else, like a nan */
107 #define XT_COLOR_RED "#cc0000"
108 #define XT_COLOR_YELLOW "#ffff66"
109 #define XT_COLOR_BLUE "lightblue"
110 #define XT_COLOR_GREEN "#99ff66"
111 #define XT_COLOR_WHITE "white"
112 #define XT_COLOR_BLACK "black"
114 #define XT_COLOR_CT_BACKGROUND "#000000"
115 #define XT_COLOR_CT_INACTIVE "#dddddd"
116 #define XT_COLOR_CT_ACTIVE "#bbbb00"
117 #define XT_COLOR_CT_SEPARATOR "#555555"
119 #define XT_COLOR_SB_SEPARATOR "#555555"
121 #define XT_PROTO_DELIM "://"
124 #define XT_MOVE_INVALID (0)
125 #define XT_MOVE_DOWN (1)
126 #define XT_MOVE_UP (2)
127 #define XT_MOVE_BOTTOM (3)
128 #define XT_MOVE_TOP (4)
129 #define XT_MOVE_PAGEDOWN (5)
130 #define XT_MOVE_PAGEUP (6)
131 #define XT_MOVE_HALFDOWN (7)
132 #define XT_MOVE_HALFUP (8)
133 #define XT_MOVE_LEFT (9)
134 #define XT_MOVE_FARLEFT (10)
135 #define XT_MOVE_RIGHT (11)
136 #define XT_MOVE_FARRIGHT (12)
137 #define XT_MOVE_PERCENT (13)
138 #define XT_MOVE_CENTER (14)
140 #define XT_QMARK_SET (0)
141 #define XT_QMARK_OPEN (1)
142 #define XT_QMARK_TAB (2)
144 #define XT_MARK_SET (0)
145 #define XT_MARK_GOTO (1)
147 #define XT_GO_UP_ROOT (999)
149 #define XT_NAV_INVALID (0)
150 #define XT_NAV_BACK (1)
151 #define XT_NAV_FORWARD (2)
152 #define XT_NAV_RELOAD (3)
153 #define XT_NAV_STOP (4)
155 #define XT_FOCUS_INVALID (0)
156 #define XT_FOCUS_URI (1)
157 #define XT_FOCUS_SEARCH (2)
159 #define XT_SEARCH_INVALID (0)
160 #define XT_SEARCH_NEXT (1)
161 #define XT_SEARCH_PREV (2)
163 #define XT_STYLE_CURRENT_TAB (0)
164 #define XT_STYLE_GLOBAL (1)
166 #define XT_PASTE_CURRENT_TAB (0)
167 #define XT_PASTE_NEW_TAB (1)
169 #define XT_ZOOM_IN (-1)
170 #define XT_ZOOM_OUT (-2)
171 #define XT_ZOOM_NORMAL (100)
173 #define XT_CMD_OPEN (0)
174 #define XT_CMD_OPEN_CURRENT (1)
175 #define XT_CMD_TABNEW (2)
176 #define XT_CMD_TABNEW_CURRENT (3)
178 #define XT_STATUS_NOTHING (0)
179 #define XT_STATUS_LINK (1)
180 #define XT_STATUS_URI (2)
181 #define XT_STATUS_LOADING (3)
183 #define XT_SES_DONOTHING (0)
184 #define XT_SES_CLOSETABS (1)
186 #define XT_PREFIX (1<<0)
187 #define XT_USERARG (1<<1)
188 #define XT_URLARG (1<<2)
189 #define XT_INTARG (1<<3)
190 #define XT_SESSARG (1<<4)
191 #define XT_SETARG (1<<5)
193 #define XT_HINT_NEWTAB (1<<0)
195 #define XT_BUFCMD_SZ (8)
197 #define XT_EJS_SHOW (1<<0)
199 GtkWidget
* create_button(char *, char *, int);
201 void recalc_tabs(void);
202 void recolor_compact_tabs(void);
203 void set_current_tab(int page_num
);
204 gboolean
update_statusbar_position(GtkAdjustment
*, gpointer
);
205 void marks_clear(struct tab
*t
);
208 extern char *__progname
;
209 char * const *start_argv
;
211 GtkWidget
*main_window
;
212 GtkNotebook
*notebook
;
214 GtkWidget
*arrow
, *abtn
;
215 struct tab_list tabs
;
216 struct history_list hl
;
217 int hl_purge_count
= 0;
218 struct session_list sessions
;
219 struct domain_list c_wl
;
220 struct domain_list js_wl
;
221 struct domain_list pl_wl
;
222 struct strict_transport_tree st_tree
;
223 struct undo_tailq undos
;
224 struct keybinding_list kbl
;
226 struct user_agent_list ua_list
;
227 int user_agent_count
= 0;
228 struct command_list chl
;
229 struct command_list shl
;
230 struct command_entry
*history_at
;
231 struct command_entry
*search_at
;
233 int cmd_history_count
= 0;
234 int search_history_count
= 0;
236 uint64_t blocked_cookies
= 0;
237 char named_session
[PATH_MAX
];
238 GtkListStore
*completion_model
;
239 GtkListStore
*buffers_store
;
241 char *qmarks
[XT_NOQMARKS
];
242 int btn_down
; /* M1 down in any wv */
243 regex_t url_re
; /* guess_search regex */
245 /* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
246 int next_download_id
= 1;
248 void xxx_dir(char *);
249 int icon_size_map(int);
250 void activate_uri_entry_cb(GtkWidget
*, struct tab
*);
253 history_delete(struct command_list
*l
, int *counter
)
255 struct command_entry
*c
;
257 if (l
== NULL
|| counter
== NULL
)
260 c
= TAILQ_LAST(l
, command_list
);
264 TAILQ_REMOVE(l
, c
, entry
);
271 history_add(struct command_list
*list
, char *file
, char *l
, int *counter
)
273 struct command_entry
*c
;
276 if (list
== NULL
|| l
== NULL
|| counter
== NULL
)
279 /* don't add the same line */
280 c
= TAILQ_FIRST(list
);
282 if (!strcmp(c
->line
+ 1 /* skip space */, l
))
285 c
= g_malloc0(sizeof *c
);
286 c
->line
= g_strdup_printf(" %s", l
);
289 TAILQ_INSERT_HEAD(list
, c
, entry
);
292 history_delete(list
, counter
);
294 if (history_autosave
&& file
) {
295 f
= fopen(file
, "w");
297 show_oops(NULL
, "couldn't write history %s", file
);
301 TAILQ_FOREACH_REVERSE(c
, list
, command_list
, entry
) {
303 fprintf(f
, "%s\n", c
->line
);
311 history_read(struct command_list
*list
, char *file
, int *counter
)
314 char *s
, line
[65536];
316 if (list
== NULL
|| file
== NULL
)
319 f
= fopen(file
, "r");
321 startpage_add("couldn't open history file %s", file
);
326 s
= fgets(line
, sizeof line
, f
);
327 if (s
== NULL
|| feof(f
) || ferror(f
))
329 if ((s
= strchr(line
, '\n')) == NULL
) {
330 startpage_add("invalid history file %s", file
);
336 history_add(list
, NULL
, line
+ 1, counter
);
344 /* marks array storage. */
348 if (i
< 0 || i
>= XT_NOMARKS
)
359 if ((ret
= strchr(XT_MARKS
, m
)) != NULL
)
360 return ret
- XT_MARKS
;
365 /* quickmarks array storage. */
369 if (i
< 0 || i
>= XT_NOQMARKS
)
380 if ((ret
= strchr(XT_QMARKS
, m
)) != NULL
)
381 return ret
- XT_QMARKS
;
386 #ifndef XT_SIGNALS_DISABLE
390 int saved_errno
, status
;
395 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
399 if (errno
!= ECHILD
) {
401 clog_warn("sigchild: waitpid:");
407 if (WIFEXITED(status
)) {
408 if (WEXITSTATUS(status
) != 0) {
410 clog_warnx("sigchild: child exit status: %d",
411 WEXITSTATUS(status));
416 clog_warnx("sigchild: child is terminated abnormally");
426 is_g_object_setting(GObject
*o
, char *str
)
428 guint n_props
= 0, i
;
429 GParamSpec
**proplist
;
435 proplist
= g_object_class_list_properties(G_OBJECT_GET_CLASS(o
),
438 for (i
= 0; i
< n_props
; i
++) {
439 if (! strcmp(proplist
[i
]->name
, str
)) {
450 get_current_tab(void)
454 TAILQ_FOREACH(t
, &tabs
, entry
) {
455 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
459 warnx("%s: no current tab", __func__
);
465 set_status(struct tab
*t
, gchar
*s
, int status
)
473 case XT_STATUS_LOADING
:
474 type
= g_strdup_printf("Loading: %s", s
);
478 type
= g_strdup_printf("Link: %s", s
);
480 t
->status
= g_strdup(gtk_entry_get_text(
481 GTK_ENTRY(t
->sbe
.statusbar
)));
485 type
= g_strdup_printf("%s", s
);
487 t
->status
= g_strdup(type
);
491 t
->status
= g_strdup(s
);
493 case XT_STATUS_NOTHING
:
498 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.statusbar
), s
);
504 hide_cmd(struct tab
*t
)
506 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
508 history_at
= NULL
; /* just in case */
509 search_at
= NULL
; /* just in case */
510 gtk_widget_hide(t
->cmd
);
514 show_cmd(struct tab
*t
)
516 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
520 gtk_widget_hide(t
->oops
);
521 gtk_widget_show(t
->cmd
);
525 hide_buffers(struct tab
*t
)
527 gtk_widget_hide(t
->buffers
);
528 gtk_list_store_clear(buffers_store
);
539 sort_tabs_by_page_num(struct tab
***stabs
)
544 num_tabs
= gtk_notebook_get_n_pages(notebook
);
546 *stabs
= g_malloc0(num_tabs
* sizeof(struct tab
*));
548 TAILQ_FOREACH(t
, &tabs
, entry
)
549 (*stabs
)[gtk_notebook_page_num(notebook
, t
->vbox
)] = t
;
555 buffers_make_list(void)
558 const gchar
*title
= NULL
;
560 struct tab
**stabs
= NULL
;
562 num_tabs
= sort_tabs_by_page_num(&stabs
);
564 for (i
= 0; i
< num_tabs
; i
++)
566 gtk_list_store_append(buffers_store
, &iter
);
567 title
= get_title(stabs
[i
], FALSE
);
568 gtk_list_store_set(buffers_store
, &iter
,
569 COL_ID
, i
+ 1, /* Enumerate the tabs starting from 1
571 COL_FAVICON
, gtk_image_get_pixbuf
572 (GTK_IMAGE(stabs
[i
]->tab_elems
.favicon
)),
581 show_buffers(struct tab
*t
)
583 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)))
587 gtk_widget_show(t
->buffers
);
588 gtk_widget_grab_focus(GTK_WIDGET(t
->buffers
));
592 toggle_buffers(struct tab
*t
)
594 if (gtk_widget_get_visible(t
->buffers
))
601 buffers(struct tab
*t
, struct karg
*args
)
609 hide_oops(struct tab
*t
)
611 gtk_widget_hide(t
->oops
);
615 show_oops(struct tab
*at
, const char *fmt
, ...)
619 struct tab
*t
= NULL
;
625 if ((t
= get_current_tab()) == NULL
)
631 if (vasprintf(&msg
, fmt
, ap
) == -1)
632 errx(1, "show_oops failed");
635 gtk_entry_set_text(GTK_ENTRY(t
->oops
), msg
);
636 gtk_widget_hide(t
->cmd
);
637 gtk_widget_show(t
->oops
);
643 char work_dir
[PATH_MAX
];
644 char certs_dir
[PATH_MAX
];
645 char js_dir
[PATH_MAX
];
646 char cache_dir
[PATH_MAX
];
647 char sessions_dir
[PATH_MAX
];
648 char temp_dir
[PATH_MAX
];
649 char cookie_file
[PATH_MAX
];
650 char *strict_transport_file
= NULL
;
651 SoupSession
*session
;
652 SoupCookieJar
*s_cookiejar
;
653 SoupCookieJar
*p_cookiejar
;
654 char rc_fname
[PATH_MAX
];
656 struct mime_type_list mtl
;
657 struct alias_list aliases
;
660 struct tab
*create_new_tab(char *, struct undo
*, int, int);
661 void delete_tab(struct tab
*);
662 void setzoom_webkit(struct tab
*, int);
663 int download_rb_cmp(struct download
*, struct download
*);
664 gboolean
cmd_execute(struct tab
*t
, char *str
);
667 history_rb_cmp(struct history
*h1
, struct history
*h2
)
669 return (strcmp(h1
->uri
, h2
->uri
));
671 RB_GENERATE(history_list
, history
, entry
, history_rb_cmp
);
674 domain_rb_cmp(struct domain
*d1
, struct domain
*d2
)
676 return (strcmp(d1
->d
, d2
->d
));
678 RB_GENERATE(domain_list
, domain
, entry
, domain_rb_cmp
);
681 download_rb_cmp(struct download
*e1
, struct download
*e2
)
683 return (e1
->id
< e2
->id
? -1 : e1
->id
> e2
->id
);
685 RB_GENERATE(download_list
, download
, entry
, download_rb_cmp
);
687 struct valid_url_types
{
698 valid_url_type(char *url
)
702 for (i
= 0; i
< LENGTH(vut
); i
++)
703 if (!strncasecmp(vut
[i
].type
, url
, strlen(vut
[i
].type
)))
710 match_alias(char *url_in
)
714 char *url_out
= NULL
, *search
, *enc_arg
;
716 search
= g_strdup(url_in
);
718 if (strsep(&arg
, " \t") == NULL
) {
719 show_oops(NULL
, "match_alias: NULL URL");
723 TAILQ_FOREACH(a
, &aliases
, entry
) {
724 if (!strcmp(search
, a
->a_name
))
729 DNPRINTF(XT_D_URL
, "match_alias: matched alias %s\n",
732 enc_arg
= soup_uri_encode(arg
, XT_RESERVED_CHARS
);
733 url_out
= g_strdup_printf(a
->a_uri
, enc_arg
);
736 url_out
= g_strdup_printf(a
->a_uri
, "");
744 guess_url_type(char *url_in
)
747 char *url_out
= NULL
, *enc_search
= NULL
;
752 /* substitute aliases */
753 url_out
= match_alias(url_in
);
757 /* see if we are an about page */
758 if (!strncmp(url_in
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
))
759 for (i
= 0; i
< about_list_size(); i
++)
760 if (!strcmp(&url_in
[XT_URI_ABOUT_LEN
],
761 about_list
[i
].name
)) {
762 url_out
= g_strdup(url_in
);
766 if (guess_search
&& url_regex
&&
767 !(g_str_has_prefix(url_in
, "http://") ||
768 g_str_has_prefix(url_in
, "https://"))) {
769 if (regexec(&url_re
, url_in
, 0, NULL
, 0)) {
770 /* invalid URI so search instead */
771 enc_search
= soup_uri_encode(url_in
, XT_RESERVED_CHARS
);
772 url_out
= g_strdup_printf(search_string
, enc_search
);
778 /* XXX not sure about this heuristic */
779 if (stat(url_in
, &sb
) == 0) {
780 if (url_in
[0] == '/')
781 url_out
= g_strdup_printf("file://%s", url_in
);
783 cwd
= malloc(PATH_MAX
);
784 if (getcwd(cwd
, PATH_MAX
) != NULL
) {
785 url_out
= g_strdup_printf("file://%s/%s",cwd
, url_in
);
790 url_out
= g_strdup_printf("http://%s", url_in
); /* guess http */
792 DNPRINTF(XT_D_URL
, "guess_url_type: guessed %s\n", url_out
);
798 load_uri(struct tab
*t
, gchar
*uri
)
801 gchar
*newuri
= NULL
;
807 /* Strip leading spaces. */
808 while (*uri
&& isspace(*uri
))
811 if (strlen(uri
) == 0) {
816 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
818 if (valid_url_type(uri
)) {
819 newuri
= guess_url_type(uri
);
823 if (!strncmp(uri
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
)) {
824 for (i
= 0; i
< about_list_size(); i
++)
825 if (!strcmp(&uri
[XT_URI_ABOUT_LEN
], about_list
[i
].name
)) {
826 bzero(&args
, sizeof args
);
827 about_list
[i
].func(t
, &args
);
828 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
),
832 show_oops(t
, "invalid about page");
836 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
838 webkit_web_view_load_uri(t
->wv
, uri
);
845 get_uri(struct tab
*t
)
847 const gchar
*uri
= NULL
;
849 if (webkit_web_view_get_load_status(t
->wv
) == WEBKIT_LOAD_FAILED
)
851 if (t
->xtp_meaning
== XT_XTP_TAB_MEANING_NORMAL
) {
852 uri
= webkit_web_view_get_uri(t
->wv
);
854 /* use tmp_uri to make sure it is g_freed */
857 t
->tmp_uri
=g_strdup_printf("%s%s", XT_URI_ABOUT
,
858 about_list
[t
->xtp_meaning
].name
);
865 get_title(struct tab
*t
, bool window
)
867 const gchar
*set
= NULL
, *title
= NULL
;
868 WebKitLoadStatus status
= webkit_web_view_get_load_status(t
->wv
);
870 if (status
== WEBKIT_LOAD_PROVISIONAL
|| status
== WEBKIT_LOAD_FAILED
||
871 t
->xtp_meaning
== XT_XTP_TAB_MEANING_BL
)
874 title
= webkit_web_view_get_title(t
->wv
);
875 if ((set
= title
? title
: get_uri(t
)))
879 set
= window
? XT_NAME
: "(untitled)";
885 find_mime_type(char *mime_type
)
887 struct mime_type
*m
, *def
= NULL
, *rv
= NULL
;
889 TAILQ_FOREACH(m
, &mtl
, entry
) {
891 !strncmp(mime_type
, m
->mt_type
, strlen(m
->mt_type
)))
894 if (m
->mt_default
== 0 && !strcmp(mime_type
, m
->mt_type
)) {
907 wl_find_uri(const gchar
*s
, struct domain_list
*wl
)
913 if (s
== NULL
|| wl
== NULL
)
916 if (!strncmp(s
, "http://", strlen("http://")))
917 s
= &s
[strlen("http://")];
918 else if (!strncmp(s
, "https://", strlen("https://")))
919 s
= &s
[strlen("https://")];
924 for (i
= 0; i
< strlen(s
) + 1 /* yes er need this */; i
++)
925 /* chop string at first slash */
926 if (s
[i
] == '/' || s
[i
] == ':' || s
[i
] == '\0') {
938 js_ref_to_string(JSContextRef context
, JSValueRef ref
)
944 jsref
= JSValueToStringCopy(context
, ref
, NULL
);
948 l
= JSStringGetMaximumUTF8CStringSize(jsref
);
951 JSStringGetUTF8CString(jsref
, s
, l
);
952 JSStringRelease(jsref
);
957 #define XT_JS_DONE ("done;")
958 #define XT_JS_DONE_LEN (strlen(XT_JS_DONE))
959 #define XT_JS_INSERT ("insert;")
960 #define XT_JS_INSERT_LEN (strlen(XT_JS_INSERT))
963 run_script(struct tab
*t
, char *s
)
965 JSGlobalContextRef ctx
;
966 WebKitWebFrame
*frame
;
968 JSValueRef val
, exception
;
971 DNPRINTF(XT_D_JS
, "%s: tab %d %s\n", __func__
,
972 t
->tab_id
, s
== (char *)JS_HINTING
? "JS_HINTING" : s
);
974 frame
= webkit_web_view_get_main_frame(t
->wv
);
975 ctx
= webkit_web_frame_get_global_context(frame
);
977 str
= JSStringCreateWithUTF8CString(s
);
978 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
979 NULL
, 0, &exception
);
980 JSStringRelease(str
);
982 DNPRINTF(XT_D_JS
, "%s: val %p\n", __func__
, val
);
984 es
= js_ref_to_string(ctx
, exception
);
986 DNPRINTF(XT_D_JS
, "%s: exception %s\n", __func__
, es
);
991 es
= js_ref_to_string(ctx
, val
);
994 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
996 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
1000 DNPRINTF(XT_D_JS
, "%s: val %s\n", __func__
, es
);
1009 enable_hints(struct tab
*t
)
1011 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
1014 run_script(t
, "hints.createHints('', 'F');");
1016 run_script(t
, "hints.createHints('', 'f');");
1017 t
->mode
= XT_MODE_HINT
;
1021 disable_hints(struct tab
*t
)
1023 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
1025 run_script(t
, "hints.clearHints();");
1026 t
->mode
= XT_MODE_COMMAND
;
1031 passthrough(struct tab
*t
, struct karg
*args
)
1033 t
->mode
= XT_MODE_PASSTHROUGH
;
1038 modurl(struct tab
*t
, struct karg
*args
)
1040 const gchar
*uri
= NULL
;
1043 /* XXX kind of a bad hack, but oh well */
1044 if (GTK_WIDGET_HAS_FOCUS(t
->uri_entry
)) {
1045 if ((uri
= gtk_entry_get_text(GTK_ENTRY(t
->uri_entry
))) &&
1047 u
= g_strdup_printf("www.%s.com", uri
);
1048 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), u
);
1050 activate_uri_entry_cb(t
->uri_entry
, t
);
1057 hint(struct tab
*t
, struct karg
*args
)
1060 DNPRINTF(XT_D_JS
, "hint: tab %d args %d\n", t
->tab_id
, args
->i
);
1062 if (t
->mode
== XT_MODE_HINT
) {
1063 if (args
->i
== XT_HINT_NEWTAB
)
1073 apply_style(struct tab
*t
)
1076 g_object_set(G_OBJECT(t
->settings
),
1077 "user-stylesheet-uri", t
->stylesheet
, (char *)NULL
);
1081 remove_style(struct tab
*t
)
1084 g_object_set(G_OBJECT(t
->settings
),
1085 "user-stylesheet-uri", NULL
, (char *)NULL
);
1089 userstyle(struct tab
*t
, struct karg
*args
)
1093 DNPRINTF(XT_D_JS
, "userstyle: tab %d\n", t
->tab_id
);
1096 case XT_STYLE_CURRENT_TAB
:
1102 case XT_STYLE_GLOBAL
:
1103 if (userstyle_global
) {
1104 userstyle_global
= 0;
1105 TAILQ_FOREACH(tt
, &tabs
, entry
)
1108 userstyle_global
= 1;
1109 TAILQ_FOREACH(tt
, &tabs
, entry
)
1119 quit(struct tab
*t
, struct karg
*args
)
1121 if (save_global_history
)
1122 save_global_history_to_disk(t
);
1130 restore_sessions_list(void)
1133 struct dirent
*dp
= NULL
;
1137 sdir
= opendir(sessions_dir
);
1139 while ((dp
= readdir(sdir
)) != NULL
) {
1140 #if defined __MINGW32__
1141 reg
= 1; /* windows only has regular files */
1143 reg
= dp
->d_type
== DT_REG
;
1146 s
= g_malloc(sizeof(struct session
));
1147 s
->name
= g_strdup(dp
->d_name
);
1148 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
1156 open_tabs(struct tab
*t
, struct karg
*a
)
1158 char file
[PATH_MAX
];
1162 struct tab
*ti
, *tt
;
1167 ti
= TAILQ_LAST(&tabs
, tab_list
);
1169 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, a
->s
);
1170 if ((f
= fopen(file
, "r")) == NULL
)
1174 if ((uri
= fparseln(f
, NULL
, NULL
, "\0\0\0", 0)) == NULL
)
1175 if (feof(f
) || ferror(f
))
1178 /* retrieve session name */
1179 if (uri
&& g_str_has_prefix(uri
, XT_SAVE_SESSION_ID
)) {
1180 strlcpy(named_session
,
1181 &uri
[strlen(XT_SAVE_SESSION_ID
)],
1182 sizeof named_session
);
1186 if (uri
&& strlen(uri
))
1187 create_new_tab(uri
, NULL
, 1, -1);
1193 /* close open tabs */
1194 if (a
->i
== XT_SES_CLOSETABS
&& ti
!= NULL
) {
1196 tt
= TAILQ_FIRST(&tabs
);
1217 restore_saved_tabs(void)
1219 char file
[PATH_MAX
];
1220 int unlink_file
= 0;
1225 snprintf(file
, sizeof file
, "%s" PS
"%s",
1226 sessions_dir
, XT_RESTART_TABS_FILE
);
1227 if (stat(file
, &sb
) == -1)
1228 a
.s
= XT_SAVED_TABS_FILE
;
1231 a
.s
= XT_RESTART_TABS_FILE
;
1234 a
.i
= XT_SES_DONOTHING
;
1235 rv
= open_tabs(NULL
, &a
);
1244 save_tabs(struct tab
*t
, struct karg
*a
)
1246 char file
[PATH_MAX
];
1248 int num_tabs
= 0, i
;
1249 struct tab
**stabs
= NULL
;
1251 /* tab may be null here */
1256 snprintf(file
, sizeof file
, "%s" PS
"%s",
1257 sessions_dir
, named_session
);
1259 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, a
->s
);
1261 if ((f
= fopen(file
, "w")) == NULL
) {
1262 show_oops(t
, "Can't open save_tabs file: %s", strerror(errno
));
1266 /* save session name */
1267 fprintf(f
, "%s%s\n", XT_SAVE_SESSION_ID
, named_session
);
1269 /* Save tabs, in the order they are arranged in the notebook. */
1270 num_tabs
= sort_tabs_by_page_num(&stabs
);
1272 for (i
= 0; i
< num_tabs
; i
++)
1274 if (get_uri(stabs
[i
]) != NULL
)
1275 fprintf(f
, "%s\n", get_uri(stabs
[i
]));
1276 else if (gtk_entry_get_text(GTK_ENTRY(
1277 stabs
[i
]->uri_entry
)))
1278 fprintf(f
, "%s\n", gtk_entry_get_text(GTK_ENTRY(
1279 stabs
[i
]->uri_entry
)));
1284 /* try and make sure this gets to disk NOW. XXX Backup first? */
1285 if (fflush(f
) != 0 || fsync(fileno(f
)) != 0) {
1286 show_oops(t
, "May not have managed to save session: %s",
1296 save_tabs_and_quit(struct tab
*t
, struct karg
*args
)
1308 run_page_script(struct tab
*t
, struct karg
*args
)
1311 char *tmp
, script
[PATH_MAX
];
1313 tmp
= args
->s
!= NULL
&& strlen(args
->s
) > 0 ? args
->s
: default_script
;
1314 if (tmp
[0] == '\0') {
1315 show_oops(t
, "no script specified");
1319 if ((uri
= get_uri(t
)) == NULL
) {
1320 show_oops(t
, "tab is empty, not running script");
1325 snprintf(script
, sizeof script
, "%s" PS
"%s",
1326 pwd
->pw_dir
, &tmp
[1]);
1328 strlcpy(script
, tmp
, sizeof script
);
1330 return (fork_exec(t
, script
, uri
, "can't launch external script", 1));
1334 yank_uri(struct tab
*t
, struct karg
*args
)
1337 GtkClipboard
*clipboard
;
1339 if ((uri
= get_uri(t
)) == NULL
)
1342 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1343 gtk_clipboard_set_text(clipboard
, uri
, -1);
1349 paste_uri(struct tab
*t
, struct karg
*args
)
1351 GtkClipboard
*clipboard
;
1352 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
1354 gchar
*p
= NULL
, *uri
;
1356 /* try primary clipboard first */
1357 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1358 p
= gtk_clipboard_wait_for_text(clipboard
);
1360 /* if it failed get whatever text is in cut_buffer0 */
1361 if (p
== NULL
&& xterm_workaround
)
1362 if (gdk_property_get(gdk_get_default_root_window(),
1364 gdk_atom_intern("STRING", FALSE
),
1366 1024 * 1024 /* picked out of my butt */,
1374 /* yes sir, we need to NUL the string */
1380 while (*uri
&& isspace(*uri
))
1382 if (strlen(uri
) == 0) {
1383 show_oops(t
, "empty paste buffer");
1386 if (guess_search
== 0 && valid_url_type(uri
)) {
1387 /* we can be clever and paste this in search box */
1388 show_oops(t
, "not a valid URL");
1392 if (args
->i
== XT_PASTE_CURRENT_TAB
)
1394 else if (args
->i
== XT_PASTE_NEW_TAB
)
1395 create_new_tab(uri
, NULL
, 1, -1);
1406 js_toggle_cb(GtkWidget
*w
, struct tab
*t
)
1411 g_object_get(G_OBJECT(t
->settings
),
1412 "enable-scripts", &es
, (char *)NULL
);
1417 set
= XT_WL_DISABLE
;
1419 a
.i
= set
| XT_WL_TOPLEVEL
;
1422 a
.i
= set
| XT_WL_TOPLEVEL
;
1425 a
.i
= XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
;
1430 toggle_src(struct tab
*t
, struct karg
*args
)
1437 mode
= webkit_web_view_get_view_source_mode(t
->wv
);
1438 webkit_web_view_set_view_source_mode(t
->wv
, !mode
);
1439 webkit_web_view_reload(t
->wv
);
1445 focus_webview(struct tab
*t
)
1450 /* only grab focus if we are visible */
1451 if (gtk_notebook_get_current_page(notebook
) == t
->tab_id
)
1452 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
1456 focus(struct tab
*t
, struct karg
*args
)
1458 if (t
== NULL
|| args
== NULL
)
1464 if (args
->i
== XT_FOCUS_URI
)
1465 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
1466 else if (args
->i
== XT_FOCUS_SEARCH
)
1467 gtk_widget_grab_focus(GTK_WIDGET(t
->search_entry
));
1473 connect_socket_from_uri(const gchar
*uri
, const gchar
**error_str
, char *domain
,
1477 struct addrinfo hints
, *res
= NULL
, *ai
;
1478 int rv
= -1, s
= -1, on
, error
;
1480 static gchar myerror
[256]; /* this is not thread safe */
1483 *error_str
= myerror
;
1484 if (uri
&& !g_str_has_prefix(uri
, "https://")) {
1485 *error_str
= "invalid URI";
1489 su
= soup_uri_new(uri
);
1491 *error_str
= "invalid soup URI";
1494 if (!SOUP_URI_VALID_FOR_HTTP(su
)) {
1495 *error_str
= "invalid HTTPS URI";
1499 snprintf(port
, sizeof port
, "%d", su
->port
);
1500 bzero(&hints
, sizeof(struct addrinfo
));
1501 hints
.ai_flags
= AI_CANONNAME
;
1502 hints
.ai_family
= AF_UNSPEC
;
1503 hints
.ai_socktype
= SOCK_STREAM
;
1505 if ((error
= getaddrinfo(su
->host
, port
, &hints
, &res
))) {
1506 snprintf(myerror
, sizeof myerror
, "getaddrinfo failed: %s",
1507 gai_strerror(errno
));
1511 for (ai
= res
; ai
; ai
= ai
->ai_next
) {
1517 if (ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
)
1519 s
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
1522 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &on
,
1525 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) == 0)
1529 snprintf(myerror
, sizeof myerror
,
1530 "could not obtain certificates from: %s",
1536 strlcpy(domain
, su
->host
, domain_sz
);
1543 if (rv
== -1 && s
!= -1)
1551 custom_gnutls_push(void *s
, const void *buf
, size_t len
)
1553 return send((size_t)s
, buf
, len
, 0);
1557 custom_gnutls_pull(void *s
, void *buf
, size_t len
)
1559 return recv((size_t)s
, buf
, len
, 0);
1564 stop_tls(gnutls_session_t gsession
, gnutls_certificate_credentials_t xcred
)
1567 gnutls_deinit(gsession
);
1569 gnutls_certificate_free_credentials(xcred
);
1575 start_tls(const gchar
**error_str
, int s
, gnutls_session_t
*gs
,
1576 gnutls_certificate_credentials_t
*xc
)
1578 gnutls_certificate_credentials_t xcred
;
1579 gnutls_session_t gsession
;
1581 static gchar myerror
[1024]; /* this is not thread safe */
1583 if (gs
== NULL
|| xc
== NULL
)
1590 gnutls_certificate_allocate_credentials(&xcred
);
1591 gnutls_certificate_set_x509_trust_file(xcred
, ssl_ca_file
,
1592 GNUTLS_X509_FMT_PEM
);
1594 gnutls_init(&gsession
, GNUTLS_CLIENT
);
1595 gnutls_priority_set_direct(gsession
, "PERFORMANCE", NULL
);
1596 gnutls_credentials_set(gsession
, GNUTLS_CRD_CERTIFICATE
, xcred
);
1597 gnutls_transport_set_ptr(gsession
, (gnutls_transport_ptr_t
)(long)s
);
1599 /* sockets on windows don't use file descriptors */
1600 gnutls_transport_set_push_function(gsession
, custom_gnutls_push
);
1601 gnutls_transport_set_pull_function(gsession
, custom_gnutls_pull
);
1603 if ((rv
= gnutls_handshake(gsession
)) < 0) {
1604 snprintf(myerror
, sizeof myerror
,
1605 "gnutls_handshake failed %d fatal %d %s",
1607 gnutls_error_is_fatal(rv
),
1608 #if LIBGNUTLS_VERSION_MAJOR >= 2 && LIBGNUTLS_VERSION_MINOR >= 6
1609 gnutls_strerror_name(rv
));
1611 "GNUTLS version is too old to provide human readable error");
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" PS
"%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(XT_COLOR_BLUE
, &color
);
1733 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
1734 statusbar_modify_attr(t
, XT_COLOR_BLACK
, XT_COLOR_BLUE
);
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" PS
"%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 LIBGNUTLS_VERSION_MAJOR >= 2 && LIBGNUTLS_VERSION_MINOR >= 6
1836 if (error
& GNUTLS_CERT_NOT_ACTIVATED
)
1837 strlcat(serr
, "not activated, ", sizeof serr
);
1838 if (error
& GNUTLS_CERT_EXPIRED
)
1839 strlcat(serr
, "expired, ", sizeof serr
);
1841 for (i
= strlen(serr
) - 1; i
> 0; i
--)
1842 if (serr
[i
] == ',') {
1849 stop_tls(gsession
, xcred
);
1855 cert_cmd(struct tab
*t
, struct karg
*args
)
1857 const gchar
*uri
, *error_str
= NULL
;
1861 gnutls_session_t gsession
;
1862 gnutls_x509_crt_t
*certs
;
1863 gnutls_certificate_credentials_t xcred
;
1868 if (ssl_ca_file
== NULL
) {
1869 show_oops(t
, "Can't open CA file: %s", ssl_ca_file
);
1873 if ((uri
= get_uri(t
)) == NULL
) {
1874 show_oops(t
, "Invalid URI");
1878 if ((s
= connect_socket_from_uri(uri
, &error_str
, domain
,
1879 sizeof domain
)) == -1) {
1880 show_oops(t
, "%s", error_str
);
1885 if (start_tls(&error_str
, s
, &gsession
, &xcred
))
1889 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
1890 show_oops(t
, "get_connection_certs failed");
1894 if (args
->i
& XT_SHOW
)
1895 show_certs(t
, certs
, cert_count
, "Certificate Chain");
1896 else if (args
->i
& XT_SAVE
)
1897 save_certs(t
, certs
, cert_count
, domain
);
1899 free_connection_certs(certs
, cert_count
);
1901 /* we close the socket first for speed */
1904 stop_tls(gsession
, xcred
);
1905 if (error_str
&& strlen(error_str
))
1906 show_oops(t
, "%s", error_str
);
1911 remove_cookie(int index
)
1917 DNPRINTF(XT_D_COOKIE
, "remove_cookie: %d\n", index
);
1919 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
1921 for (i
= 1; cf
; cf
= cf
->next
, i
++) {
1925 print_cookie("remove cookie", c
);
1926 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
1931 soup_cookies_free(cf
);
1937 remove_cookie_domain(int domain_id
)
1939 int domain_count
, rv
= 1;
1944 DNPRINTF(XT_D_COOKIE
, "remove_cookie_domain: %d\n", domain_id
);
1947 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
1949 for (domain_count
= 0; cf
; cf
= cf
->next
) {
1952 if (strcmp(last_domain
, c
->domain
) != 0) {
1954 last_domain
= c
->domain
;
1957 if (domain_count
< domain_id
)
1959 else if (domain_count
> domain_id
)
1962 print_cookie("remove cookie", c
);
1963 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
1967 soup_cookies_free(cf
);
1979 DNPRINTF(XT_D_COOKIE
, "remove_cookie_all\n");
1981 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
1983 for (; cf
; cf
= cf
->next
) {
1986 print_cookie("remove cookie", c
);
1987 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
1991 soup_cookies_free(cf
);
1997 toplevel_cmd(struct tab
*t
, struct karg
*args
)
1999 js_toggle_cb(t
->js_toggle
, t
);
2005 can_go_back_for_real(struct tab
*t
)
2008 WebKitWebHistoryItem
*item
;
2014 /* rely on webkit to make sure we can go backward when on an about page */
2016 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
2017 return (webkit_web_view_can_go_back(t
->wv
));
2019 /* the back/forwars list is stupid so help determine if we can go back */
2020 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2022 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2023 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
2031 can_go_forward_for_real(struct tab
*t
)
2034 WebKitWebHistoryItem
*item
;
2040 /* rely on webkit to make sure we can go forward when on an about page */
2042 if (uri
== NULL
|| g_str_has_prefix(uri
, "about:"))
2043 return (webkit_web_view_can_go_forward(t
->wv
));
2045 /* the back/forwars list is stupid so help selecting a different item */
2046 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2048 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2049 if (strcmp(webkit_web_history_item_get_uri(item
), uri
))
2057 go_back_for_real(struct tab
*t
)
2060 WebKitWebHistoryItem
*item
;
2068 webkit_web_view_go_back(t
->wv
);
2071 /* the back/forwars list is stupid so help selecting a different item */
2072 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2074 i
--, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2075 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2076 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2083 go_forward_for_real(struct tab
*t
)
2086 WebKitWebHistoryItem
*item
;
2094 webkit_web_view_go_forward(t
->wv
);
2097 /* the back/forwars list is stupid so help selecting a different item */
2098 for (i
= 0, item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2100 i
++, item
= webkit_web_back_forward_list_get_nth_item(t
->bfl
, i
)) {
2101 if (strcmp(webkit_web_history_item_get_uri(item
), uri
)) {
2102 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2109 navaction(struct tab
*t
, struct karg
*args
)
2111 WebKitWebHistoryItem
*item
;
2112 WebKitWebFrame
*frame
;
2114 DNPRINTF(XT_D_NAV
, "navaction: tab %d opcode %d\n",
2115 t
->tab_id
, args
->i
);
2117 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
2119 if (args
->i
== XT_NAV_BACK
)
2120 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2122 item
= webkit_web_back_forward_list_get_forward_item(t
->bfl
);
2124 return (XT_CB_PASSTHROUGH
);
2125 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2127 return (XT_CB_PASSTHROUGH
);
2133 go_back_for_real(t
);
2135 case XT_NAV_FORWARD
:
2137 go_forward_for_real(t
);
2140 frame
= webkit_web_view_get_main_frame(t
->wv
);
2141 webkit_web_frame_reload(frame
);
2144 frame
= webkit_web_view_get_main_frame(t
->wv
);
2145 webkit_web_frame_stop_loading(frame
);
2148 return (XT_CB_PASSTHROUGH
);
2152 move(struct tab
*t
, struct karg
*args
)
2154 GtkAdjustment
*adjust
;
2155 double pi
, si
, pos
, ps
, upper
, lower
, max
;
2161 case XT_MOVE_BOTTOM
:
2163 case XT_MOVE_PAGEDOWN
:
2164 case XT_MOVE_PAGEUP
:
2165 case XT_MOVE_HALFDOWN
:
2166 case XT_MOVE_HALFUP
:
2167 case XT_MOVE_PERCENT
:
2168 case XT_MOVE_CENTER
:
2169 adjust
= t
->adjust_v
;
2172 adjust
= t
->adjust_h
;
2176 pos
= gtk_adjustment_get_value(adjust
);
2177 ps
= gtk_adjustment_get_page_size(adjust
);
2178 upper
= gtk_adjustment_get_upper(adjust
);
2179 lower
= gtk_adjustment_get_lower(adjust
);
2180 si
= gtk_adjustment_get_step_increment(adjust
);
2181 pi
= gtk_adjustment_get_page_increment(adjust
);
2184 DNPRINTF(XT_D_MOVE
, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2185 "max %f si %f pi %f\n",
2186 args
->i
, adjust
== t
->adjust_h
? "horizontal" : "vertical",
2187 pos
, ps
, upper
, lower
, max
, si
, pi
);
2193 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2198 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2200 case XT_MOVE_BOTTOM
:
2201 case XT_MOVE_FARRIGHT
:
2202 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2203 gtk_adjustment_set_value(adjust
, max
);
2206 case XT_MOVE_FARLEFT
:
2207 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2208 gtk_adjustment_set_value(adjust
, lower
);
2210 case XT_MOVE_PAGEDOWN
:
2212 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2214 case XT_MOVE_PAGEUP
:
2216 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2218 case XT_MOVE_HALFDOWN
:
2220 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2222 case XT_MOVE_HALFUP
:
2224 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2226 case XT_MOVE_CENTER
:
2227 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2228 args
->s
= g_strdup("50.0");
2230 case XT_MOVE_PERCENT
:
2231 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2232 percent
= atoi(args
->s
) / 100.0;
2233 pos
= max
* percent
;
2234 if (pos
< 0.0 || pos
> max
)
2236 gtk_adjustment_set_value(adjust
, pos
);
2239 return (XT_CB_PASSTHROUGH
);
2242 DNPRINTF(XT_D_MOVE
, "move: new pos %f %f\n", pos
, MIN(pos
, max
));
2244 return (XT_CB_HANDLED
);
2248 url_set_visibility(void)
2252 TAILQ_FOREACH(t
, &tabs
, entry
)
2253 if (show_url
== 0) {
2254 gtk_widget_hide(t
->toolbar
);
2257 gtk_widget_show(t
->toolbar
);
2261 notebook_tab_set_visibility(void)
2263 if (show_tabs
== 0) {
2264 gtk_widget_hide(tab_bar
);
2265 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2267 if (tab_style
== XT_TABS_NORMAL
) {
2268 gtk_widget_hide(tab_bar
);
2269 gtk_notebook_set_show_tabs(notebook
, TRUE
);
2270 } else if (tab_style
== XT_TABS_COMPACT
) {
2271 gtk_widget_show(tab_bar
);
2272 gtk_notebook_set_show_tabs(notebook
, FALSE
);
2278 statusbar_set_visibility(void)
2282 TAILQ_FOREACH(t
, &tabs
, entry
){
2283 if (show_statusbar
== 0)
2284 gtk_widget_hide(t
->statusbar_box
);
2286 gtk_widget_show(t
->statusbar_box
);
2293 url_set(struct tab
*t
, int enable_url_entry
)
2298 show_url
= enable_url_entry
;
2300 if (enable_url_entry
) {
2301 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
2302 GTK_ENTRY_ICON_PRIMARY
, NULL
);
2303 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
), 0);
2305 pixbuf
= gtk_entry_get_icon_pixbuf(GTK_ENTRY(t
->uri_entry
),
2306 GTK_ENTRY_ICON_PRIMARY
);
2308 gtk_entry_get_progress_fraction(GTK_ENTRY(t
->uri_entry
));
2309 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
2310 GTK_ENTRY_ICON_PRIMARY
, pixbuf
);
2311 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
2317 fullscreen(struct tab
*t
, struct karg
*args
)
2319 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2322 return (XT_CB_PASSTHROUGH
);
2324 if (show_url
== 0) {
2332 url_set_visibility();
2333 notebook_tab_set_visibility();
2335 return (XT_CB_HANDLED
);
2339 statustoggle(struct tab
*t
, struct karg
*args
)
2341 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2343 if (show_statusbar
== 1) {
2345 statusbar_set_visibility();
2346 } else if (show_statusbar
== 0) {
2348 statusbar_set_visibility();
2350 return (XT_CB_HANDLED
);
2354 urlaction(struct tab
*t
, struct karg
*args
)
2356 int rv
= XT_CB_HANDLED
;
2358 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2361 return (XT_CB_PASSTHROUGH
);
2365 if (show_url
== 0) {
2367 url_set_visibility();
2371 if (show_url
== 1) {
2373 url_set_visibility();
2381 tabaction(struct tab
*t
, struct karg
*args
)
2383 int rv
= XT_CB_HANDLED
;
2384 char *url
= args
->s
;
2388 DNPRINTF(XT_D_TAB
, "tabaction: %p %d\n", t
, args
->i
);
2391 return (XT_CB_PASSTHROUGH
);
2395 if (strlen(url
) > 0)
2396 create_new_tab(url
, NULL
, 1, args
->precount
);
2398 create_new_tab(NULL
, NULL
, 1, args
->precount
);
2401 if (args
->precount
< 0)
2404 TAILQ_FOREACH(tt
, &tabs
, entry
)
2405 if (tt
->tab_id
== args
->precount
- 1) {
2410 case XT_TAB_DELQUIT
:
2411 if (gtk_notebook_get_n_pages(notebook
) > 1)
2417 if (strlen(url
) > 0)
2420 rv
= XT_CB_PASSTHROUGH
;
2426 if (show_tabs
== 0) {
2428 notebook_tab_set_visibility();
2432 if (show_tabs
== 1) {
2434 notebook_tab_set_visibility();
2437 case XT_TAB_NEXTSTYLE
:
2438 if (tab_style
== XT_TABS_NORMAL
) {
2439 tab_style
= XT_TABS_COMPACT
;
2440 recolor_compact_tabs();
2443 tab_style
= XT_TABS_NORMAL
;
2444 notebook_tab_set_visibility();
2446 case XT_TAB_UNDO_CLOSE
:
2447 if (undo_count
== 0) {
2448 DNPRINTF(XT_D_TAB
, "%s: no tabs to undo close",
2453 u
= TAILQ_FIRST(&undos
);
2454 create_new_tab(u
->uri
, u
, 1, -1);
2456 TAILQ_REMOVE(&undos
, u
, entry
);
2458 /* u->history is freed in create_new_tab() */
2462 case XT_TAB_LOAD_IMAGES
:
2464 if (!auto_load_images
) {
2466 /* Enable auto-load images (this will load all
2467 * previously unloaded images). */
2468 g_object_set(G_OBJECT(t
->settings
),
2469 "auto-load-images", TRUE
, (char *)NULL
);
2470 webkit_web_view_set_settings(t
->wv
, t
->settings
);
2472 webkit_web_view_reload(t
->wv
);
2474 /* Webkit triggers an event when we change the setting,
2475 * so we can't disable the auto-loading at once.
2477 * Unfortunately, webkit does not tell us when it's done.
2478 * Instead, we wait until the next request, and then
2479 * disable autoloading again.
2481 t
->load_images
= TRUE
;
2485 rv
= XT_CB_PASSTHROUGH
;
2499 resizetab(struct tab
*t
, struct karg
*args
)
2501 if (t
== NULL
|| args
== NULL
) {
2502 show_oops(NULL
, "resizetab invalid parameters");
2503 return (XT_CB_PASSTHROUGH
);
2506 DNPRINTF(XT_D_TAB
, "resizetab: tab %d %d\n",
2507 t
->tab_id
, args
->i
);
2509 setzoom_webkit(t
, args
->i
);
2511 return (XT_CB_HANDLED
);
2515 movetab(struct tab
*t
, struct karg
*args
)
2519 if (t
== NULL
|| args
== NULL
) {
2520 show_oops(NULL
, "movetab invalid parameters");
2521 return (XT_CB_PASSTHROUGH
);
2524 DNPRINTF(XT_D_TAB
, "movetab: tab %d opcode %d\n",
2525 t
->tab_id
, args
->i
);
2527 if (args
->i
>= XT_TAB_INVALID
)
2528 return (XT_CB_PASSTHROUGH
);
2530 if (TAILQ_EMPTY(&tabs
))
2531 return (XT_CB_PASSTHROUGH
);
2533 n
= gtk_notebook_get_n_pages(notebook
);
2534 dest
= gtk_notebook_get_current_page(notebook
);
2538 if (args
->precount
< 0)
2539 dest
= dest
== n
- 1 ? 0 : dest
+ 1;
2541 dest
= args
->precount
- 1;
2545 if (args
->precount
< 0)
2548 dest
-= args
->precount
% n
;
2561 return (XT_CB_PASSTHROUGH
);
2564 if (dest
< 0 || dest
>= n
)
2565 return (XT_CB_PASSTHROUGH
);
2566 if (t
->tab_id
== dest
) {
2567 DNPRINTF(XT_D_TAB
, "movetab: do nothing\n");
2568 return (XT_CB_HANDLED
);
2571 set_current_tab(dest
);
2573 return (XT_CB_HANDLED
);
2580 command(struct tab
*t
, struct karg
*args
)
2582 char *s
= NULL
, *ss
= NULL
;
2587 if (t
== NULL
|| args
== NULL
) {
2588 show_oops(NULL
, "command invalid parameters");
2589 return (XT_CB_PASSTHROUGH
);
2600 if (cmd_prefix
== 0)
2603 ss
= g_strdup_printf(":%d", cmd_prefix
);
2609 t
->mode
= XT_MODE_HINT
;
2610 bzero(&a
, sizeof a
);
2616 t
->mode
= XT_MODE_HINT
;
2617 bzero(&a
, sizeof a
);
2618 a
.i
= XT_HINT_NEWTAB
;
2628 case XT_CMD_OPEN_CURRENT
:
2631 case XT_CMD_TABNEW_CURRENT
:
2632 if (!s
) /* FALL THROUGH? */
2634 if ((uri
= get_uri(t
)) != NULL
) {
2635 ss
= g_strdup_printf("%s%s", s
, uri
);
2640 show_oops(t
, "command: invalid opcode %d", args
->i
);
2641 return (XT_CB_PASSTHROUGH
);
2644 DNPRINTF(XT_D_CMD
, "%s: tab %d type %s\n", __func__
, t
->tab_id
, s
);
2646 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), s
);
2647 text
= gdk_color_to_string(&t
->default_style
->text
[GTK_STATE_NORMAL
]);
2648 base
= gdk_color_to_string(&t
->default_style
->base
[GTK_STATE_NORMAL
]);
2649 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
2650 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
2651 statusbar_modify_attr(t
, text
, base
);
2655 gtk_widget_grab_focus(GTK_WIDGET(t
->cmd
));
2656 gtk_editable_set_position(GTK_EDITABLE(t
->cmd
), -1);
2661 return (XT_CB_HANDLED
);
2665 search(struct tab
*t
, struct karg
*args
)
2669 if (t
== NULL
|| args
== NULL
) {
2670 show_oops(NULL
, "search invalid parameters");
2675 case XT_SEARCH_NEXT
:
2676 d
= t
->search_forward
;
2678 case XT_SEARCH_PREV
:
2679 d
= !t
->search_forward
;
2682 return (XT_CB_PASSTHROUGH
);
2685 if (t
->search_text
== NULL
) {
2686 if (global_search
== NULL
)
2687 return (XT_CB_PASSTHROUGH
);
2689 d
= t
->search_forward
= TRUE
;
2690 t
->search_text
= g_strdup(global_search
);
2691 webkit_web_view_mark_text_matches(t
->wv
, global_search
, FALSE
, 0);
2692 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
2696 DNPRINTF(XT_D_CMD
, "search: tab %d opc %d forw %d text %s\n",
2697 t
->tab_id
, args
->i
, t
->search_forward
, t
->search_text
);
2699 webkit_web_view_search_text(t
->wv
, t
->search_text
, FALSE
, d
, TRUE
);
2701 return (XT_CB_HANDLED
);
2705 session_save(struct tab
*t
, char *filename
)
2711 if (strlen(filename
) == 0)
2714 if (filename
[0] == '.' || filename
[0] == '/')
2718 if (save_tabs(t
, &a
))
2720 strlcpy(named_session
, filename
, sizeof named_session
);
2722 /* add the new session to the list of sessions */
2723 s
= g_malloc(sizeof(struct session
));
2724 s
->name
= g_strdup(filename
);
2725 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
2733 session_open(struct tab
*t
, char *filename
)
2738 if (strlen(filename
) == 0)
2741 if (filename
[0] == '.' || filename
[0] == '/')
2745 a
.i
= XT_SES_CLOSETABS
;
2746 if (open_tabs(t
, &a
))
2749 strlcpy(named_session
, filename
, sizeof named_session
);
2757 session_delete(struct tab
*t
, char *filename
)
2759 char file
[PATH_MAX
];
2763 if (strlen(filename
) == 0)
2766 if (filename
[0] == '.' || filename
[0] == '/')
2769 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, filename
);
2773 if (!strcmp(filename
, named_session
))
2774 strlcpy(named_session
, XT_SAVED_TABS_FILE
,
2775 sizeof named_session
);
2777 /* remove session from sessions list */
2778 TAILQ_FOREACH(s
, &sessions
, entry
) {
2779 if (!strcmp(s
->name
, filename
))
2784 TAILQ_REMOVE(&sessions
, s
, entry
);
2785 g_free((gpointer
) s
->name
);
2794 session_cmd(struct tab
*t
, struct karg
*args
)
2796 char *filename
= args
->s
;
2801 if (args
->i
& XT_SHOW
)
2802 show_oops(t
, "Current session: %s", named_session
[0] == '\0' ?
2803 XT_SAVED_TABS_FILE
: named_session
);
2804 else if (args
->i
& XT_SAVE
) {
2805 if (session_save(t
, filename
)) {
2806 show_oops(t
, "Can't save session: %s",
2807 filename
? filename
: "INVALID");
2810 } else if (args
->i
& XT_OPEN
) {
2811 if (session_open(t
, filename
)) {
2812 show_oops(t
, "Can't open session: %s",
2813 filename
? filename
: "INVALID");
2816 } else if (args
->i
& XT_DELETE
) {
2817 if (session_delete(t
, filename
)) {
2818 show_oops(t
, "Can't delete session: %s",
2819 filename
? filename
: "INVALID");
2824 return (XT_CB_PASSTHROUGH
);
2828 script_cmd(struct tab
*t
, struct karg
*args
)
2837 if ((f
= fopen(args
->s
, "r")) == NULL
) {
2838 show_oops(t
, "Can't open script file: %s", args
->s
);
2842 if (fstat(fileno(f
), &sb
) == -1) {
2843 show_oops(t
, "Can't stat script file: %s", args
->s
);
2847 buf
= g_malloc0(sb
.st_size
+ 1);
2848 if (fread(buf
, 1, sb
.st_size
, f
) != sb
.st_size
) {
2849 show_oops(t
, "Can't read script file: %s", args
->s
);
2853 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
2862 return (XT_CB_PASSTHROUGH
);
2866 * Make a hardcopy of the page
2869 print_page(struct tab
*t
, struct karg
*args
)
2871 WebKitWebFrame
*frame
;
2873 GtkPrintOperation
*op
;
2874 GtkPrintOperationAction action
;
2875 GtkPrintOperationResult print_res
;
2876 GError
*g_err
= NULL
;
2877 int marg_l
, marg_r
, marg_t
, marg_b
;
2879 DNPRINTF(XT_D_PRINTING
, "%s:", __func__
);
2881 ps
= gtk_page_setup_new();
2882 op
= gtk_print_operation_new();
2883 action
= GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
;
2884 frame
= webkit_web_view_get_main_frame(t
->wv
);
2886 /* the default margins are too small, so we will bump them */
2887 marg_l
= gtk_page_setup_get_left_margin(ps
, GTK_UNIT_MM
) +
2888 XT_PRINT_EXTRA_MARGIN
;
2889 marg_r
= gtk_page_setup_get_right_margin(ps
, GTK_UNIT_MM
) +
2890 XT_PRINT_EXTRA_MARGIN
;
2891 marg_t
= gtk_page_setup_get_top_margin(ps
, GTK_UNIT_MM
) +
2892 XT_PRINT_EXTRA_MARGIN
;
2893 marg_b
= gtk_page_setup_get_bottom_margin(ps
, GTK_UNIT_MM
) +
2894 XT_PRINT_EXTRA_MARGIN
;
2897 gtk_page_setup_set_left_margin(ps
, marg_l
, GTK_UNIT_MM
);
2898 gtk_page_setup_set_right_margin(ps
, marg_r
, GTK_UNIT_MM
);
2899 gtk_page_setup_set_top_margin(ps
, marg_t
, GTK_UNIT_MM
);
2900 gtk_page_setup_set_bottom_margin(ps
, marg_b
, GTK_UNIT_MM
);
2902 gtk_print_operation_set_default_page_setup(op
, ps
);
2904 /* this appears to free 'op' and 'ps' */
2905 print_res
= webkit_web_frame_print_full(frame
, op
, action
, &g_err
);
2907 /* check it worked */
2908 if (print_res
== GTK_PRINT_OPERATION_RESULT_ERROR
) {
2909 show_oops(NULL
, "can't print: %s", g_err
->message
);
2910 g_error_free (g_err
);
2918 go_home(struct tab
*t
, struct karg
*args
)
2925 set_encoding(struct tab
*t
, struct karg
*args
)
2929 if (args
->s
&& strlen(g_strstrip(args
->s
)) == 0) {
2930 e
= webkit_web_view_get_custom_encoding(t
->wv
);
2932 e
= webkit_web_view_get_encoding(t
->wv
);
2933 show_oops(t
, "encoding: %s", e
? e
: "N/A");
2935 webkit_web_view_set_custom_encoding(t
->wv
, args
->s
);
2941 restart(struct tab
*t
, struct karg
*args
)
2945 a
.s
= XT_RESTART_TABS_FILE
;
2947 execvp(start_argv
[0], start_argv
);
2953 char *http_proxy_save
; /* not a setting, used to toggle */
2956 proxy_cmd(struct tab
*t
, struct karg
*args
)
2958 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
2965 if (http_proxy_save
)
2966 g_free(http_proxy_save
);
2967 http_proxy_save
= g_strdup(http_proxy
);
2970 if (args
->i
& XT_PRXY_SHOW
) {
2972 show_oops(t
, "http_proxy = %s", http_proxy
);
2974 show_oops(t
, "proxy is currently disabled");
2975 } else if (args
->i
& XT_PRXY_TOGGLE
) {
2976 if (http_proxy_save
== NULL
&& http_proxy
== NULL
) {
2977 show_oops(t
, "can't toggle proxy");
2983 setup_proxy(http_proxy_save
);
2986 return (XT_CB_PASSTHROUGH
);
2991 int (*func
)(struct tab
*, struct karg
*);
2995 { "command_mode", 0, command_mode
, XT_MODE_COMMAND
, 0 },
2996 { "insert_mode", 0, command_mode
, XT_MODE_INSERT
, 0 },
2997 { "command", 0, command
, ':', 0 },
2998 { "search", 0, command
, '/', 0 },
2999 { "searchb", 0, command
, '?', 0 },
3000 { "hinting", 0, command
, '.', 0 },
3001 { "hinting_newtab", 0, command
, ',', 0 },
3002 { "togglesrc", 0, toggle_src
, 0, 0 },
3003 { "editsrc", 0, edit_src
, 0, 0 },
3004 { "editelement", 0, edit_element
, 0, 0 },
3005 { "passthrough", 0, passthrough
, 0, 0 },
3006 { "modurl", 0, modurl
, 0, 0 },
3008 /* yanking and pasting */
3009 { "yankuri", 0, yank_uri
, 0, 0 },
3010 /* XXX: pasteuri{cur,new} do not work from the cmd_entry? */
3011 { "pasteuricur", 0, paste_uri
, XT_PASTE_CURRENT_TAB
, 0 },
3012 { "pasteurinew", 0, paste_uri
, XT_PASTE_NEW_TAB
, 0 },
3015 { "searchnext", 0, search
, XT_SEARCH_NEXT
, 0 },
3016 { "searchprevious", 0, search
, XT_SEARCH_PREV
, 0 },
3019 { "focusaddress", 0, focus
, XT_FOCUS_URI
, 0 },
3020 { "focussearch", 0, focus
, XT_FOCUS_SEARCH
, 0 },
3023 { "hinting", 0, hint
, 0, 0 },
3024 { "hinting_newtab", 0, hint
, XT_HINT_NEWTAB
, 0 },
3026 /* custom stylesheet */
3027 { "userstyle", 0, userstyle
, XT_STYLE_CURRENT_TAB
, 0 },
3028 { "userstyle_global", 0, userstyle
, XT_STYLE_GLOBAL
, 0 },
3031 { "goback", 0, navaction
, XT_NAV_BACK
, 0 },
3032 { "goforward", 0, navaction
, XT_NAV_FORWARD
, 0 },
3033 { "reload", 0, navaction
, XT_NAV_RELOAD
, 0 },
3034 { "stop", 0, navaction
, XT_NAV_STOP
, 0 },
3036 /* vertical movement */
3037 { "scrolldown", 0, move
, XT_MOVE_DOWN
, 0 },
3038 { "scrollup", 0, move
, XT_MOVE_UP
, 0 },
3039 { "scrollbottom", 0, move
, XT_MOVE_BOTTOM
, 0 },
3040 { "scrolltop", 0, move
, XT_MOVE_TOP
, 0 },
3041 { "1", 0, move
, XT_MOVE_TOP
, 0 },
3042 { "scrollhalfdown", 0, move
, XT_MOVE_HALFDOWN
, 0 },
3043 { "scrollhalfup", 0, move
, XT_MOVE_HALFUP
, 0 },
3044 { "scrollpagedown", 0, move
, XT_MOVE_PAGEDOWN
, 0 },
3045 { "scrollpageup", 0, move
, XT_MOVE_PAGEUP
, 0 },
3046 /* horizontal movement */
3047 { "scrollright", 0, move
, XT_MOVE_RIGHT
, 0 },
3048 { "scrollleft", 0, move
, XT_MOVE_LEFT
, 0 },
3049 { "scrollfarright", 0, move
, XT_MOVE_FARRIGHT
, 0 },
3050 { "scrollfarleft", 0, move
, XT_MOVE_FARLEFT
, 0 },
3052 { "favorites", 0, xtp_page_fl
, 0, 0 },
3053 { "fav", 0, xtp_page_fl
, 0, 0 },
3054 { "favadd", 0, add_favorite
, 0, 0 },
3056 { "qall", 0, quit
, 0, 0 },
3057 { "quitall", 0, quit
, 0, 0 },
3058 { "w", 0, save_tabs
, 0, 0 },
3059 { "wq", 0, save_tabs_and_quit
, 0, 0 },
3060 { "help", 0, help
, 0, 0 },
3061 { "about", 0, about
, 0, 0 },
3062 { "stats", 0, stats
, 0, 0 },
3063 { "version", 0, about
, 0, 0 },
3066 { "js", 0, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3067 { "save", 1, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3068 { "domain", 2, js_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3069 { "fqdn", 2, js_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3070 { "show", 1, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3071 { "all", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3072 { "persistent", 2, js_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3073 { "session", 2, js_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3074 { "toggle", 1, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3075 { "domain", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3076 { "fqdn", 2, js_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3078 /* cookie command */
3079 { "cookie", 0, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3080 { "save", 1, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3081 { "domain", 2, cookie_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3082 { "fqdn", 2, cookie_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3083 { "show", 1, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3084 { "all", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3085 { "persistent", 2, cookie_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3086 { "session", 2, cookie_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3087 { "toggle", 1, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3088 { "domain", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3089 { "fqdn", 2, cookie_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3090 { "purge", 1, cookie_cmd
, XT_DELETE
, 0 },
3092 /* plugin command */
3093 { "plugin", 0, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3094 { "save", 1, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3095 { "domain", 2, pl_cmd
, XT_SAVE
| XT_WL_TOPLEVEL
, 0 },
3096 { "fqdn", 2, pl_cmd
, XT_SAVE
| XT_WL_FQDN
, 0 },
3097 { "show", 1, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3098 { "all", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
, 0 },
3099 { "persistent", 2, pl_cmd
, XT_SHOW
| XT_WL_PERSISTENT
, 0 },
3100 { "session", 2, pl_cmd
, XT_SHOW
| XT_WL_SESSION
, 0 },
3101 { "toggle", 1, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3102 { "domain", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
, 0 },
3103 { "fqdn", 2, pl_cmd
, XT_WL_TOGGLE
| XT_WL_FQDN
, 0 },
3105 /* toplevel (domain) command */
3106 { "toplevel", 0, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3107 { "toggle", 1, toplevel_cmd
, XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
, 0 },
3110 { "cookiejar", 0, xtp_page_cl
, 0, 0 },
3113 { "cert", 0, cert_cmd
, XT_SHOW
, 0 },
3114 { "save", 1, cert_cmd
, XT_SAVE
, 0 },
3115 { "show", 1, cert_cmd
, XT_SHOW
, 0 },
3117 { "ca", 0, ca_cmd
, 0, 0 },
3118 { "downloadmgr", 0, xtp_page_dl
, 0, 0 },
3119 { "dl", 0, xtp_page_dl
, 0, 0 },
3120 { "h", 0, xtp_page_hl
, 0, 0 },
3121 { "history", 0, xtp_page_hl
, 0, 0 },
3122 { "home", 0, go_home
, 0, 0 },
3123 { "restart", 0, restart
, 0, 0 },
3124 { "urlhide", 0, urlaction
, XT_URL_HIDE
, 0 },
3125 { "urlshow", 0, urlaction
, XT_URL_SHOW
, 0 },
3126 { "statustoggle", 0, statustoggle
, 0, 0 },
3127 { "run_script", 0, run_page_script
, 0, XT_USERARG
},
3129 { "print", 0, print_page
, 0, 0 },
3132 { "focusin", 0, resizetab
, XT_ZOOM_IN
, 0 },
3133 { "focusout", 0, resizetab
, XT_ZOOM_OUT
, 0 },
3134 { "focusreset", 0, resizetab
, XT_ZOOM_NORMAL
, 0 },
3135 { "q", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3136 { "quit", 0, tabaction
, XT_TAB_DELQUIT
, 0 },
3137 { "open", 0, tabaction
, XT_TAB_OPEN
, XT_URLARG
},
3138 { "tabclose", 0, tabaction
, XT_TAB_DELETE
, XT_PREFIX
| XT_INTARG
},
3139 { "tabedit", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3140 { "tabfirst", 0, movetab
, XT_TAB_FIRST
, 0 },
3141 { "tabhide", 0, tabaction
, XT_TAB_HIDE
, 0 },
3142 { "tablast", 0, movetab
, XT_TAB_LAST
, 0 },
3143 { "tabnew", 0, tabaction
, XT_TAB_NEW
, XT_PREFIX
| XT_URLARG
},
3144 { "tabnext", 0, movetab
, XT_TAB_NEXT
, XT_PREFIX
| XT_INTARG
},
3145 { "tabnextstyle", 0, tabaction
, XT_TAB_NEXTSTYLE
, 0 },
3146 { "tabprevious", 0, movetab
, XT_TAB_PREV
, XT_PREFIX
| XT_INTARG
},
3147 { "tabrewind", 0, movetab
, XT_TAB_FIRST
, 0 },
3148 { "tabshow", 0, tabaction
, XT_TAB_SHOW
, 0 },
3149 { "tabs", 0, buffers
, 0, 0 },
3150 { "tabundoclose", 0, tabaction
, XT_TAB_UNDO_CLOSE
, 0 },
3151 { "buffers", 0, buffers
, 0, 0 },
3152 { "ls", 0, buffers
, 0, 0 },
3153 { "encoding", 0, set_encoding
, 0, XT_USERARG
},
3154 { "loadimages", 0, tabaction
, XT_TAB_LOAD_IMAGES
, 0 },
3156 /* command aliases (handy when -S flag is used) */
3157 { "promptopen", 0, command
, XT_CMD_OPEN
, 0 },
3158 { "promptopencurrent", 0, command
, XT_CMD_OPEN_CURRENT
, 0 },
3159 { "prompttabnew", 0, command
, XT_CMD_TABNEW
, 0 },
3160 { "prompttabnewcurrent",0, command
, XT_CMD_TABNEW_CURRENT
, 0 },
3163 { "set", 0, set
, 0, XT_SETARG
},
3165 { "fullscreen", 0, fullscreen
, 0, 0 },
3166 { "f", 0, fullscreen
, 0, 0 },
3169 { "session", 0, session_cmd
, XT_SHOW
, 0 },
3170 { "delete", 1, session_cmd
, XT_DELETE
, XT_SESSARG
},
3171 { "open", 1, session_cmd
, XT_OPEN
, XT_SESSARG
},
3172 { "save", 1, session_cmd
, XT_SAVE
, XT_USERARG
},
3173 { "show", 1, session_cmd
, XT_SHOW
, 0 },
3175 /* external javascript */
3176 { "script", 0, script_cmd
, XT_EJS_SHOW
, XT_USERARG
},
3179 { "inspector", 0, inspector_cmd
, XT_INS_SHOW
, 0 },
3180 { "show", 1, inspector_cmd
, XT_INS_SHOW
, 0 },
3181 { "hide", 1, inspector_cmd
, XT_INS_HIDE
, 0 },
3184 { "proxy", 0, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3185 { "show", 1, proxy_cmd
, XT_PRXY_SHOW
, 0 },
3186 { "toggle", 1, proxy_cmd
, XT_PRXY_TOGGLE
, 0 },
3193 } cmd_status
= {-1, 0};
3196 wv_release_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3199 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 1)
3206 wv_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3209 WebKitHitTestResult
*hit_test_result
;
3212 hit_test_result
= webkit_web_view_get_hit_test_result(t
->wv
, e
);
3213 g_object_get(hit_test_result
, "context", &context
, NULL
);
3218 if (context
& WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE
)
3219 t
->mode
= XT_MODE_INSERT
;
3221 t
->mode
= XT_MODE_COMMAND
;
3223 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3225 else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 8 /* btn 4 */) {
3231 } else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 9 /* btn 5 */) {
3233 a
.i
= XT_NAV_FORWARD
;
3243 tab_close_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3245 DNPRINTF(XT_D_TAB
, "tab_close_cb: tab %d\n", t
->tab_id
);
3247 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3256 activate_uri_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3258 const gchar
*uri
= gtk_entry_get_text(GTK_ENTRY(entry
));
3260 DNPRINTF(XT_D_URL
, "activate_uri_entry_cb: %s\n", uri
);
3263 show_oops(NULL
, "activate_uri_entry_cb invalid parameters");
3268 show_oops(t
, "activate_uri_entry_cb no uri");
3272 uri
+= strspn(uri
, "\t ");
3274 /* if xxxt:// treat specially */
3275 if (parse_xtp_url(t
, uri
))
3278 /* otherwise continue to load page normally */
3279 load_uri(t
, (gchar
*)uri
);
3284 activate_search_entry_cb(GtkWidget
* entry
, struct tab
*t
)
3286 const gchar
*search
= gtk_entry_get_text(GTK_ENTRY(entry
));
3287 char *newuri
= NULL
;
3290 DNPRINTF(XT_D_URL
, "activate_search_entry_cb: %s\n", search
);
3293 show_oops(NULL
, "activate_search_entry_cb invalid parameters");
3297 if (search_string
== NULL
) {
3298 show_oops(t
, "no search_string");
3302 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
3304 enc_search
= soup_uri_encode(search
, XT_RESERVED_CHARS
);
3305 newuri
= g_strdup_printf(search_string
, enc_search
);
3309 webkit_web_view_load_uri(t
->wv
, newuri
);
3317 check_and_set_cookie(const gchar
*uri
, struct tab
*t
)
3319 struct domain
*d
= NULL
;
3322 if (uri
== NULL
|| t
== NULL
)
3325 if ((d
= wl_find_uri(uri
, &c_wl
)) == NULL
)
3330 DNPRINTF(XT_D_COOKIE
, "check_and_set_cookie: %s %s\n",
3331 es
? "enable" : "disable", uri
);
3333 g_object_set(G_OBJECT(t
->settings
),
3334 "enable-html5-local-storage", es
, (char *)NULL
);
3335 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3339 check_and_set_js(const gchar
*uri
, struct tab
*t
)
3341 struct domain
*d
= NULL
;
3344 if (uri
== NULL
|| t
== NULL
)
3347 if ((d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
3352 DNPRINTF(XT_D_JS
, "check_and_set_js: %s %s\n",
3353 es
? "enable" : "disable", uri
);
3355 g_object_set(G_OBJECT(t
->settings
),
3356 "enable-scripts", es
, (char *)NULL
);
3357 g_object_set(G_OBJECT(t
->settings
),
3358 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
3359 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3361 button_set_stockid(t
->js_toggle
,
3362 es
? GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
);
3366 check_and_set_pl(const gchar
*uri
, struct tab
*t
)
3368 struct domain
*d
= NULL
;
3371 if (uri
== NULL
|| t
== NULL
)
3374 if ((d
= wl_find_uri(uri
, &pl_wl
)) == NULL
)
3379 DNPRINTF(XT_D_JS
, "check_and_set_pl: %s %s\n",
3380 es
? "enable" : "disable", uri
);
3382 g_object_set(G_OBJECT(t
->settings
),
3383 "enable-plugins", es
, (char *)NULL
);
3384 webkit_web_view_set_settings(t
->wv
, t
->settings
);
3388 color_address_bar(gpointer p
)
3391 struct tab
*tt
, *t
= p
;
3392 gchar
*col_str
= XT_COLOR_WHITE
, *text
, *base
;
3393 const gchar
*uri
, *u
= NULL
, *error_str
= NULL
;
3396 gdk_threads_enter();
3398 DNPRINTF(XT_D_URL
, "%s:\n", __func__
);
3400 /* make sure t still exists */
3403 TAILQ_FOREACH(tt
, &tabs
, entry
)
3409 if ((uri
= get_uri(t
)) == NULL
)
3414 gdk_threads_leave();
3417 col_str
= XT_COLOR_YELLOW
;
3418 switch (load_compare_cert(u
, &error_str
)) {
3420 col_str
= XT_COLOR_BLUE
;
3423 col_str
= XT_COLOR_GREEN
;
3425 case CERT_UNTRUSTED
:
3426 col_str
= XT_COLOR_YELLOW
;
3429 col_str
= XT_COLOR_RED
;
3434 gdk_threads_enter();
3436 /* make sure t isn't deleted */
3437 TAILQ_FOREACH(tt
, &tabs
, entry
)
3444 /* test to see if the user navigated away and canceled the thread */
3445 if (t
->thread
!= g_thread_self())
3447 if ((uri
= get_uri(t
)) == NULL
) {
3451 if (strcmp(uri
, u
)) {
3452 /* make sure we are still the same url */
3459 if (!strcmp(col_str
, XT_COLOR_WHITE
)) {
3460 text
= gdk_color_to_string(
3461 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3462 base
= gdk_color_to_string(
3463 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3464 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3465 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3466 statusbar_modify_attr(t
, text
, base
);
3470 gdk_color_parse(col_str
, &color
);
3471 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3472 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3475 if (error_str
&& error_str
[0] != '\0')
3476 show_oops(t
, "%s", error_str
);
3481 /* t is invalid at this point */
3483 g_free((gpointer
)u
);
3485 gdk_threads_leave();
3490 show_ca_status(struct tab
*t
, const char *uri
)
3493 gchar
*col_str
= XT_COLOR_WHITE
, *text
, *base
;
3495 DNPRINTF(XT_D_URL
, "show_ca_status: %d %s %s\n",
3496 ssl_strict_certs
, ssl_ca_file
, uri
);
3503 if (ssl_ca_file
== NULL
) {
3504 if (g_str_has_prefix(uri
, "http://"))
3506 if (g_str_has_prefix(uri
, "https://")) {
3507 col_str
= XT_COLOR_RED
;
3512 if (g_str_has_prefix(uri
, "http://") ||
3513 !g_str_has_prefix(uri
, "https://"))
3517 * It is not necessary to see if the thread is already running.
3518 * If the thread is in progress setting it to something else aborts it
3522 /* thread the coloring of the address bar */
3523 t
->thread
= g_thread_create((GThreadFunc
)color_address_bar
, t
, TRUE
, NULL
);
3525 color_address_bar(t
);
3532 if (!strcmp(col_str
, XT_COLOR_WHITE
)) {
3533 text
= gdk_color_to_string(
3534 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3535 base
= gdk_color_to_string(
3536 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3537 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3538 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3539 statusbar_modify_attr(t
, text
, base
);
3543 gdk_color_parse(col_str
, &color
);
3544 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
3545 statusbar_modify_attr(t
, XT_COLOR_BLACK
, col_str
);
3551 free_favicon(struct tab
*t
)
3553 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p req %p\n",
3554 __func__
, t
->icon_download
, t
->icon_request
);
3556 if (t
->icon_request
)
3557 g_object_unref(t
->icon_request
);
3558 if (t
->icon_dest_uri
)
3559 g_free(t
->icon_dest_uri
);
3561 t
->icon_request
= NULL
;
3562 t
->icon_dest_uri
= NULL
;
3566 xt_icon_from_name(struct tab
*t
, gchar
*name
)
3568 if (!enable_favicon_entry
)
3571 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->uri_entry
),
3572 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3574 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3575 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3577 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3578 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3582 xt_icon_from_pixbuf(struct tab
*t
, GdkPixbuf
*pb
)
3584 GdkPixbuf
*pb_scaled
;
3586 if (gdk_pixbuf_get_width(pb
) > 16 || gdk_pixbuf_get_height(pb
) > 16)
3587 pb_scaled
= gdk_pixbuf_scale_simple(pb
, 16, 16,
3588 GDK_INTERP_BILINEAR
);
3592 if (enable_favicon_entry
) {
3595 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
3596 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3599 if (show_url
== 0) {
3600 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.statusbar
),
3601 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3603 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.statusbar
),
3604 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3607 /* XXX: Only supports the minimal tabs atm. */
3608 if (enable_favicon_tabs
)
3609 gtk_image_set_from_pixbuf(GTK_IMAGE(t
->tab_elems
.favicon
),
3612 if (pb_scaled
!= pb
)
3613 g_object_unref(pb_scaled
);
3617 xt_icon_from_file(struct tab
*t
, char *file
)
3621 if (g_str_has_prefix(file
, "file://"))
3622 file
+= strlen("file://");
3624 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
3626 xt_icon_from_pixbuf(t
, pb
);
3629 xt_icon_from_name(t
, "text-html");
3633 is_valid_icon(char *file
)
3636 const char *mime_type
;
3640 gf
= g_file_new_for_path(file
);
3641 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
3643 mime_type
= g_file_info_get_content_type(fi
);
3644 valid
= g_strcmp0(mime_type
, "image/x-ico") == 0 ||
3645 g_strcmp0(mime_type
, "image/vnd.microsoft.icon") == 0 ||
3646 g_strcmp0(mime_type
, "image/png") == 0 ||
3647 g_strcmp0(mime_type
, "image/gif") == 0 ||
3648 g_strcmp0(mime_type
, "application/octet-stream") == 0;
3656 set_favicon_from_file(struct tab
*t
, char *file
)
3660 if (t
== NULL
|| file
== NULL
)
3663 if (g_str_has_prefix(file
, "file://"))
3664 file
+= strlen("file://");
3665 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading %s\n", __func__
, file
);
3667 if (!stat(file
, &sb
)) {
3668 if (sb
.st_size
== 0 || !is_valid_icon(file
)) {
3669 /* corrupt icon so trash it */
3670 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3673 /* no need to set icon to default here */
3677 xt_icon_from_file(t
, file
);
3681 favicon_download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
3684 WebKitDownloadStatus status
= webkit_download_get_status(download
);
3685 struct tab
*tt
= NULL
, *t
= NULL
;
3688 * find the webview instead of passing in the tab as it could have been
3689 * deleted from underneath us.
3691 TAILQ_FOREACH(tt
, &tabs
, entry
) {
3700 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d status %d\n",
3701 __func__
, t
->tab_id
, status
);
3704 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
3706 t
->icon_download
= NULL
;
3709 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
3712 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
3715 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
3717 DNPRINTF(XT_D_DOWNLOAD
, "%s: freeing favicon %d\n",
3718 __func__
, t
->tab_id
);
3719 t
->icon_download
= NULL
;
3722 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
3725 DNPRINTF(XT_D_DOWNLOAD
, "%s: setting icon to %s\n",
3726 __func__
, t
->icon_dest_uri
);
3727 set_favicon_from_file(t
, t
->icon_dest_uri
);
3728 /* these will be freed post callback */
3729 t
->icon_request
= NULL
;
3730 t
->icon_download
= NULL
;
3738 abort_favicon_download(struct tab
*t
)
3740 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p\n", __func__
, t
->icon_download
);
3742 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
3743 if (t
->icon_download
) {
3744 g_signal_handlers_disconnect_by_func(G_OBJECT(t
->icon_download
),
3745 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3746 webkit_download_cancel(t
->icon_download
);
3747 t
->icon_download
= NULL
;
3752 xt_icon_from_name(t
, "text-html");
3756 notify_icon_loaded_cb(WebKitWebView
*wv
, gchar
*uri
, struct tab
*t
)
3758 DNPRINTF(XT_D_DOWNLOAD
, "%s %s\n", __func__
, uri
);
3760 if (uri
== NULL
|| t
== NULL
)
3763 #if WEBKIT_CHECK_VERSION(1, 4, 0)
3764 /* take icon from WebKitIconDatabase */
3767 pb
= webkit_web_view_get_icon_pixbuf(wv
);
3769 xt_icon_from_pixbuf(t
, pb
);
3772 xt_icon_from_name(t
, "text-html");
3773 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
3774 /* download icon to cache dir */
3775 gchar
*name_hash
, file
[PATH_MAX
];
3778 if (t
->icon_request
) {
3779 DNPRINTF(XT_D_DOWNLOAD
, "%s: download in progress\n", __func__
);
3783 /* check to see if we got the icon in cache */
3784 name_hash
= g_compute_checksum_for_string(G_CHECKSUM_SHA256
, uri
, -1);
3785 snprintf(file
, sizeof file
, "%s" PS
"%s.ico", cache_dir
, name_hash
);
3788 if (!stat(file
, &sb
)) {
3789 if (sb
.st_size
> 0) {
3790 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading from cache %s\n",
3792 set_favicon_from_file(t
, file
);
3796 /* corrupt icon so trash it */
3797 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
3802 /* create download for icon */
3803 t
->icon_request
= webkit_network_request_new(uri
);
3804 if (t
->icon_request
== NULL
) {
3805 DNPRINTF(XT_D_DOWNLOAD
, "%s: invalid uri %s\n",
3810 t
->icon_download
= webkit_download_new(t
->icon_request
);
3811 if (t
->icon_download
== NULL
)
3814 /* we have to free icon_dest_uri later */
3815 t
->icon_dest_uri
= g_strdup_printf("file://%s", file
);
3816 webkit_download_set_destination_uri(t
->icon_download
,
3819 if (webkit_download_get_status(t
->icon_download
) ==
3820 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
3821 g_object_unref(t
->icon_request
);
3822 g_free(t
->icon_dest_uri
);
3823 t
->icon_request
= NULL
;
3824 t
->icon_dest_uri
= NULL
;
3828 g_signal_connect(G_OBJECT(t
->icon_download
), "notify::status",
3829 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
3831 webkit_download_start(t
->icon_download
);
3836 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3838 const gchar
*uri
= NULL
;
3839 struct history
*h
, find
;
3843 DNPRINTF(XT_D_URL
, "notify_load_status_cb: %d %s\n",
3844 webkit_web_view_get_load_status(wview
),
3845 get_uri(t
) ? get_uri(t
) : "NOTHING");
3848 show_oops(NULL
, "notify_load_status_cb invalid parameters");
3852 switch (webkit_web_view_get_load_status(wview
)) {
3853 case WEBKIT_LOAD_PROVISIONAL
:
3855 abort_favicon_download(t
);
3856 #if GTK_CHECK_VERSION(2, 20, 0)
3857 gtk_widget_show(t
->spinner
);
3858 gtk_spinner_start(GTK_SPINNER(t
->spinner
));
3860 gtk_label_set_text(GTK_LABEL(t
->label
), "Loading");
3862 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), TRUE
);
3864 /* assume we are a new address */
3865 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
,
3866 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3867 text
= gdk_color_to_string(
3868 &t
->default_style
->text
[GTK_STATE_NORMAL
]);
3869 base
= gdk_color_to_string(
3870 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
3871 statusbar_modify_attr(t
, text
, base
);
3875 /* take focus if we are visible */
3881 /* kill color thread */
3886 case WEBKIT_LOAD_COMMITTED
:
3891 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
3897 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
3899 /* check if js white listing is enabled */
3900 if (enable_plugin_whitelist
)
3901 check_and_set_pl(uri
, t
);
3902 if (enable_cookie_whitelist
)
3903 check_and_set_cookie(uri
, t
);
3904 if (enable_js_whitelist
)
3905 check_and_set_js(uri
, t
);
3911 /* we know enough to autosave the session */
3912 if (session_autosave
) {
3917 show_ca_status(t
, uri
);
3918 run_script(t
, JS_HINTING
);
3919 if (enable_autoscroll
)
3920 run_script(t
, JS_AUTOSCROLL
);
3923 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
3925 if (color_visited_uris
) {
3926 color_visited(t
, color_visited_helper());
3928 /* This colors the links you middle-click (open in new
3929 * tab) in the current tab. */
3930 if (t
->tab_id
!= gtk_notebook_get_current_page(notebook
) &&
3931 (uri
= get_uri(t
)) != NULL
)
3932 color_visited(get_current_tab(),
3933 g_strdup_printf("{'%s' : 'dummy'}", uri
));
3937 case WEBKIT_LOAD_FINISHED
:
3939 if ((uri
= get_uri(t
)) == NULL
)
3942 if (!strncmp(uri
, "http://", strlen("http://")) ||
3943 !strncmp(uri
, "https://", strlen("https://")) ||
3944 !strncmp(uri
, "file://", strlen("file://"))) {
3945 find
.uri
= (gchar
*)uri
;
3946 h
= RB_FIND(history_list
, &hl
, &find
);
3948 insert_history_item(uri
,
3949 get_title(t
, FALSE
), time(NULL
));
3951 h
->time
= time(NULL
);
3954 set_status(t
, (char *)uri
, XT_STATUS_URI
);
3955 #if WEBKIT_CHECK_VERSION(1, 1, 18)
3956 case WEBKIT_LOAD_FAILED
:
3959 #if GTK_CHECK_VERSION(2, 20, 0)
3960 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
3961 gtk_widget_hide(t
->spinner
);
3964 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
3969 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
), TRUE
);
3971 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
),
3972 can_go_back_for_real(t
));
3974 gtk_widget_set_sensitive(GTK_WIDGET(t
->forward
),
3975 can_go_forward_for_real(t
));
3979 notify_title_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
3981 const gchar
*title
= NULL
, *win_title
= NULL
;
3983 title
= get_title(t
, FALSE
);
3984 win_title
= get_title(t
, TRUE
);
3986 gtk_label_set_text(GTK_LABEL(t
->label
), title
);
3987 gtk_label_set_text(GTK_LABEL(t
->tab_elems
.label
), title
);
3990 if (win_title
&& t
->tab_id
== gtk_notebook_get_current_page(notebook
))
3991 gtk_window_set_title(GTK_WINDOW(main_window
), win_title
);
3995 get_domain(const gchar
*host
)
4000 /* handle silly domains like .co.uk */
4002 if ((x
= strlen(host
)) <= 6)
4003 return (g_strdup(host
));
4005 if (host
[x
- 3] == '.' && host
[x
- 6] == '.') {
4011 return (g_strdup(&host
[x
+ 1]));
4015 p
= g_strrstr(host
, ".");
4017 return (g_strdup(""));
4023 return (g_strdup(p
+ 1));
4025 return (g_strdup(host
));
4029 js_autorun(struct tab
*t
)
4033 size_t got_default
= 0, got_host
= 0;
4035 char deff
[PATH_MAX
], hostf
[PATH_MAX
];
4036 char *js
= NULL
, *jsat
, *domain
= NULL
;
4037 FILE *deffile
= NULL
, *hostfile
= NULL
;
4039 if (js_autorun_enabled
== 0)
4044 !(g_str_has_prefix(uri
, "http://") ||
4045 g_str_has_prefix(uri
, "https://")))
4048 su
= soup_uri_new(uri
);
4051 if (!SOUP_URI_VALID_FOR_HTTP(su
))
4054 DNPRINTF(XT_D_JS
, "%s: host: %s domain: %s\n", __func__
,
4056 domain
= get_domain(su
->host
);
4058 snprintf(deff
, sizeof deff
, "%s" PS
"default.js", js_dir
);
4059 if ((deffile
= fopen(deff
, "r")) != NULL
) {
4060 if (fstat(fileno(deffile
), &sb
) == -1) {
4061 show_oops(t
, "can't stat default JS file");
4064 got_default
= sb
.st_size
;
4067 /* try host first followed by domain */
4068 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, su
->host
);
4069 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4070 if ((hostfile
= fopen(hostf
, "r")) == NULL
) {
4071 snprintf(hostf
, sizeof hostf
, "%s" PS
"%s.js", js_dir
, domain
);
4072 DNPRINTF(XT_D_JS
, "trying file: %s\n", hostf
);
4073 if ((hostfile
= fopen(hostf
, "r")) == NULL
)
4076 DNPRINTF(XT_D_JS
, "file: %s\n", hostf
);
4077 if (fstat(fileno(hostfile
), &sb
) == -1) {
4078 show_oops(t
, "can't stat %s JS file", hostf
);
4081 got_host
= sb
.st_size
;
4084 if (got_default
+ got_host
== 0)
4087 js
= g_malloc0(got_default
+ got_host
+ 1);
4091 if (fread(js
, got_default
, 1, deffile
) != 1) {
4092 show_oops(t
, "default file read error");
4095 jsat
= js
+ got_default
;
4099 if (fread(jsat
, got_host
, 1, hostfile
) != 1) {
4100 show_oops(t
, "host file read error");
4105 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
4122 webview_load_finished_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4124 /* autorun some js if enabled */
4132 webview_progress_changed_cb(WebKitWebView
*wv
, int progress
, struct tab
*t
)
4134 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->uri_entry
),
4135 progress
== 100 ? 0 : (double)progress
/ 100);
4136 if (show_url
== 0) {
4137 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->sbe
.statusbar
),
4138 progress
== 100 ? 0 : (double)progress
/ 100);
4141 update_statusbar_position(NULL
, NULL
);
4145 strict_transport_rb_cmp(struct strict_transport
*a
, struct strict_transport
*b
)
4150 /* compare strings from the end */
4151 l1
= strlen(a
->host
);
4152 l2
= strlen(b
->host
);
4156 for (; *p1
== *p2
&& p1
> a
->host
&& p2
> b
->host
;
4161 * Check if we need to do pattern expansion,
4162 * or if we're just keeping the tree in order
4164 if (a
->flags
& XT_STS_FLAGS_EXPAND
&&
4165 b
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) {
4166 /* Check if we're matching the
4167 * 'host.xyz' part in '*.host.xyz'
4169 if (p2
== b
->host
&& (p1
== a
->host
|| *(p1
-1) == '.')) {
4174 if (p1
== a
->host
&& p2
== b
->host
)
4188 RB_GENERATE(strict_transport_tree
, strict_transport
, entry
,
4189 strict_transport_rb_cmp
);
4192 strict_transport_add(const char *domain
, time_t timeout
, int subdomains
)
4194 struct strict_transport
*d
, find
;
4198 if (enable_strict_transport
== FALSE
)
4201 DPRINTF("strict_transport_add(%s,%lld,%d)\n", domain
,
4202 (long long)timeout
, subdomains
);
4208 find
.host
= (char *)domain
;
4210 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4214 /* check if update is needed */
4215 if (d
->timeout
== timeout
&&
4216 (d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) == subdomains
)
4219 d
->timeout
= timeout
;
4221 d
->flags
|= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4223 /* We're still initializing */
4224 if (strict_transport_file
== NULL
)
4227 if ((f
= fopen(strict_transport_file
, "w")) == NULL
) {
4229 "can't open strict-transport rules file");
4233 fprintf(f
, "# Generated file - do not update unless you know "
4234 "what you're doing\n");
4235 RB_FOREACH(d
, strict_transport_tree
, &st_tree
) {
4236 if (d
->timeout
< now
)
4238 fprintf(f
, "%s\t%lld\t%d\n", d
->host
, (long long)d
->timeout
,
4239 d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
);
4243 d
= g_malloc(sizeof *d
);
4244 d
->host
= g_strdup(domain
);
4245 d
->timeout
= timeout
;
4247 d
->flags
= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4250 RB_INSERT(strict_transport_tree
, &st_tree
, d
);
4252 /* We're still initializing */
4253 if (strict_transport_file
== NULL
)
4256 if ((f
= fopen(strict_transport_file
, "a+")) == NULL
) {
4258 "can't open strict-transport rules file");
4262 fseek(f
, 0, SEEK_END
);
4263 fprintf(f
,"%s\t%lld\t%d\n", d
->host
, (long long)timeout
, subdomains
);
4270 strict_transport_check(const char *host
)
4272 static struct strict_transport
*d
= NULL
;
4273 struct strict_transport find
;
4275 if (enable_strict_transport
== FALSE
)
4278 find
.host
= (char *)host
;
4280 /* match for domains that include subdomains */
4281 find
.flags
= XT_STS_FLAGS_EXPAND
;
4283 /* First, check if we're already at the right node */
4284 if (d
!= NULL
&& strict_transport_rb_cmp(&find
, d
) == 0) {
4288 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4296 strict_transport_init()
4298 char file
[PATH_MAX
];
4304 time_t timeout
, now
;
4307 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_STS_FILE
);
4308 if ((f
= fopen(file
, "r")) == NULL
) {
4309 strict_transport_file
= g_strdup(file
);
4320 if ((rule
= fparseln(f
, &len
, NULL
, delim
, 0)) == NULL
) {
4321 if (!feof(f
) || ferror(f
))
4327 /* get second entry */
4328 if ((ptr
= strpbrk(rule
, " \t")) == NULL
)
4332 timeout
= atoi(ptr
);
4334 /* get third entry */
4335 if ((ptr
= strpbrk(ptr
, " \t")) == NULL
)
4339 subdomains
= atoi(ptr
);
4342 strict_transport_add(rule
, timeout
, subdomains
);
4347 strict_transport_file
= g_strdup(file
);
4351 startpage_add("strict-transport rules file ('%s') is corrupt", file
);
4359 strict_transport_security_cb(SoupMessage
*msg
, gpointer data
)
4365 int subdomains
= FALSE
;
4370 sts
= soup_message_headers_get_one(msg
->response_headers
,
4371 "Strict-Transport-Security");
4372 uri
= soup_message_get_uri(msg
);
4374 if (sts
== NULL
|| uri
== NULL
)
4377 if ((ptr
= strcasestr(sts
, "max-age="))) {
4378 ptr
+= strlen("max-age=");
4379 timeout
= atoll(ptr
);
4381 return; /* malformed header - max-age must be included */
4383 if ((ptr
= strcasestr(sts
, "includeSubDomains")))
4386 strict_transport_add(uri
->host
, timeout
+ time(NULL
), subdomains
);
4390 session_rq_cb(SoupSession
*s
, SoupMessage
*msg
, SoupSocket
*socket
,
4400 if (s
== NULL
|| msg
== NULL
)
4403 if (referer_mode
== XT_REFERER_ALWAYS
)
4406 /* Check if referer is set - and what the user requested for referers */
4407 ref
= soup_message_headers_get_one(msg
->request_headers
, "Referer");
4409 DNPRINTF(XT_D_NAV
, "session_rq_cb: Referer: %s\n", ref
);
4410 switch (referer_mode
) {
4411 case XT_REFERER_NEVER
:
4412 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing referer\n");
4413 soup_message_headers_remove(msg
->request_headers
,
4416 case XT_REFERER_SAME_DOMAIN
:
4417 ref_uri
= soup_uri_new(ref
);
4418 dest
= soup_message_get_uri(msg
);
4420 ref_suffix
= tld_get_suffix(ref_uri
->host
);
4421 dest_suffix
= tld_get_suffix(dest
->host
);
4423 if (dest
&& strcmp(ref_suffix
, dest_suffix
) != 0) {
4424 soup_message_headers_remove(msg
->request_headers
,
4426 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4427 "referer (not same domain) (suffixes: %s - %s)\n",
4428 ref_suffix
, dest_suffix
);
4430 soup_uri_free(ref_uri
);
4432 case XT_REFERER_SAME_FQDN
:
4433 ref_uri
= soup_uri_new(ref
);
4434 dest
= soup_message_get_uri(msg
);
4435 if (dest
&& strcmp(ref_uri
->host
, dest
->host
) != 0) {
4436 soup_message_headers_remove(msg
->request_headers
,
4438 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4439 "referer (not same fqdn) (should be %s)\n",
4442 soup_uri_free(ref_uri
);
4444 case XT_REFERER_CUSTOM
:
4445 DNPRINTF(XT_D_NAV
, "session_rq_cb: setting referer "
4446 "to %s\n", referer_custom
);
4447 soup_message_headers_replace(msg
->request_headers
,
4448 "Referer", referer_custom
);
4453 if (enable_strict_transport
) {
4454 soup_message_add_header_handler(msg
, "finished",
4455 "Strict-Transport-Security",
4456 G_CALLBACK(strict_transport_security_cb
), NULL
);
4461 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
4462 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
4463 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
4466 WebKitWebNavigationReason reason
;
4467 struct domain
*d
= NULL
;
4470 show_oops(NULL
, "webview_npd_cb invalid parameters");
4474 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
4476 webkit_network_request_get_uri(request
));
4478 uri
= (char *)webkit_network_request_get_uri(request
);
4480 if (!auto_load_images
&& t
->load_images
) {
4482 /* Disable autoloading of images, now that we're done loading
4484 g_object_set(G_OBJECT(t
->settings
),
4485 "auto-load-images", FALSE
, (char *)NULL
);
4486 webkit_web_view_set_settings(t
->wv
, t
->settings
);
4488 t
->load_images
= FALSE
;
4491 /* If this is an xtp url, we don't load anything else. */
4492 if (parse_xtp_url(t
, uri
))
4495 if ((t
->mode
== XT_MODE_HINT
&& t
->new_tab
) || t
->ctrl_click
) {
4497 create_new_tab(uri
, NULL
, ctrl_click_focus
, -1);
4498 webkit_web_policy_decision_ignore(pd
);
4499 return (TRUE
); /* we made the decission */
4502 /* Change user agent if more than one has been given. */
4503 if (user_agent_count
> 1) {
4504 struct user_agent
*ua
;
4506 if ((ua
= TAILQ_NEXT(user_agent
, entry
)) == NULL
)
4507 user_agent
= TAILQ_FIRST(&ua_list
);
4511 free(t
->user_agent
);
4512 t
->user_agent
= g_strdup(user_agent
->value
);
4514 DNPRINTF(XT_D_NAV
, "user-agent: %s\n", t
->user_agent
);
4516 g_object_set(G_OBJECT(t
->settings
),
4517 "user-agent", t
->user_agent
, (char *)NULL
);
4519 webkit_web_view_set_settings(wv
, t
->settings
);
4523 * This is a little hairy but it comes down to this:
4524 * when we run in whitelist mode we have to assist the browser in
4525 * opening the URL that it would have opened in a new tab.
4527 reason
= webkit_web_navigation_action_get_reason(na
);
4528 if (reason
== WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED
) {
4529 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
4530 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1)
4531 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4533 webkit_web_policy_decision_use(pd
);
4534 return (TRUE
); /* we made the decision */
4541 webview_rrs_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, WebKitWebResource
*res
,
4542 WebKitNetworkRequest
*request
, WebKitNetworkResponse
*response
,
4548 msg
= webkit_network_request_get_message(request
);
4551 uri
= soup_message_get_uri(msg
);
4554 if (strcmp(soup_uri_get_scheme(uri
), SOUP_URI_SCHEME_HTTP
) == 0) {
4555 if (strict_transport_check(uri
->host
)) {
4556 DNPRINTF(XT_D_NAV
, "webview_rrs_cb: force https for %s\n",
4558 soup_uri_set_scheme(uri
, SOUP_URI_SCHEME_HTTPS
);
4564 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4567 struct domain
*d
= NULL
;
4569 WebKitWebView
*webview
= NULL
;
4572 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
4573 webkit_web_view_get_uri(wv
));
4576 /* open in current tab */
4578 } else if (enable_scripts
== 0 && enable_js_whitelist
== 1) {
4579 uri
= webkit_web_view_get_uri(wv
);
4580 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4583 if (t
->ctrl_click
) {
4584 x
= ctrl_click_focus
;
4587 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4589 } else if (enable_scripts
== 1) {
4590 if (t
->ctrl_click
) {
4591 x
= ctrl_click_focus
;
4594 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4602 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
4605 struct domain
*d
= NULL
;
4607 DNPRINTF(XT_D_NAV
, "webview_close_cb: %d\n", t
->tab_id
);
4609 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
4610 uri
= webkit_web_view_get_uri(wv
);
4611 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
4615 } else if (enable_scripts
== 1)
4622 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
4624 /* we can not eat the event without throwing gtk off so defer it */
4626 /* catch middle click */
4627 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 2) {
4632 /* catch ctrl click */
4633 if (e
->type
== GDK_BUTTON_RELEASE
&&
4634 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
4639 return (XT_CB_PASSTHROUGH
);
4643 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
4645 struct mime_type
*m
;
4647 m
= find_mime_type(mime_type
);
4653 return (fork_exec(t
, m
->mt_action
,
4654 webkit_network_request_get_uri(request
),
4655 "can't launch MIME handler", 0));
4659 get_mime_type(const char *file
)
4662 char *mime_type
= NULL
;
4666 if (g_str_has_prefix(file
, "file://"))
4667 file
+= strlen("file://");
4669 gf
= g_file_new_for_path(file
);
4670 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
4672 if ((m
= g_file_info_get_content_type(fi
)) != NULL
)
4673 mime_type
= g_strdup(m
);
4681 run_download_mimehandler(char *mime_type
, char *file
)
4683 struct mime_type
*m
;
4685 m
= find_mime_type(mime_type
);
4689 return (fork_exec(NULL
, m
->mt_action
, file
,
4690 "can't launch download MIME handler", 0));
4694 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
4697 WebKitDownloadStatus status
;
4698 const char *file
= NULL
;
4701 if (download
== NULL
)
4703 status
= webkit_download_get_status(download
);
4704 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
4707 file
= webkit_download_get_destination_uri(download
);
4710 mime
= get_mime_type(file
);
4714 if (g_str_has_prefix(file
, "file://"))
4715 file
+= strlen("file://");
4716 run_download_mimehandler((char *)mime
, (char *)file
);
4721 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
4722 WebKitNetworkRequest
*request
, char *mime_type
,
4723 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
4726 show_oops(NULL
, "webview_mimetype_cb invalid parameters");
4730 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
4731 t
->tab_id
, mime_type
);
4733 if (run_mimehandler(t
, mime_type
, request
) == 0) {
4734 webkit_web_policy_decision_ignore(decision
);
4739 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
4740 webkit_web_policy_decision_download(decision
);
4748 download_start(struct tab
*t
, struct download
*d
, int flag
)
4750 WebKitNetworkRequest
*req
;
4752 const gchar
*suggested_name
;
4753 gchar
*filename
= NULL
;
4758 if (d
== NULL
|| t
== NULL
) {
4759 show_oops(NULL
, "%s invalid parameters", __func__
);
4763 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
4764 if (suggested_name
== NULL
)
4765 return (FALSE
); /* abort download */
4776 filename
= g_strdup_printf("%d%s", i
, suggested_name
);
4779 uri
= g_strdup_printf("%s\\%s", download_dir
, i
?
4780 filename
: suggested_name
);
4782 uri
= g_strdup_printf("file://%s/%s", download_dir
, i
?
4783 filename
: suggested_name
);
4787 } while (!stat(uri
, &sb
));
4789 } while (!stat(uri
+ strlen("file://"), &sb
)); /* XXX is the + strlen right? */
4792 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d filename %s "
4793 "local %s\n", __func__
, t
->tab_id
, filename
, uri
);
4795 /* if we're restarting the download, or starting
4796 * it after doing something else, we need to recreate
4797 * the download request.
4799 if (flag
== XT_DL_RESTART
) {
4800 req
= webkit_network_request_new(webkit_download_get_uri(d
->download
));
4801 webkit_download_cancel(d
->download
);
4802 g_object_unref(d
->download
);
4803 d
->download
= webkit_download_new(req
);
4806 webkit_download_set_destination_uri(d
->download
, uri
);
4808 if (webkit_download_get_status(d
->download
) ==
4809 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
4810 show_oops(t
, "%s: download failed to start", __func__
);
4812 gtk_label_set_text(GTK_LABEL(t
->label
), "Download Failed");
4814 /* connect "download first" mime handler */
4815 g_signal_connect(G_OBJECT(d
->download
), "notify::status",
4816 G_CALLBACK(download_status_changed_cb
), NULL
);
4818 /* get from history */
4819 g_object_ref(d
->download
);
4820 gtk_label_set_text(GTK_LABEL(t
->label
), "Downloading");
4821 show_oops(t
, "Download of '%s' started...",
4822 basename((char *)webkit_download_get_destination_uri(d
->download
)));
4825 if (flag
!= XT_DL_START
)
4826 webkit_download_start(d
->download
);
4828 DNPRINTF(XT_D_DOWNLOAD
, "download status : %d",
4829 webkit_download_get_status(d
->download
));
4831 /* sync other download manager tabs */
4832 update_download_tabs(NULL
);
4844 download_ask_cb(struct tab
*t
, GdkEventKey
*e
, gpointer data
)
4846 struct download
*d
= data
;
4850 t
->mode_cb_data
= NULL
;
4853 e
->keyval
= GDK_Escape
;
4854 return (XT_CB_PASSTHROUGH
);
4857 DPRINTF("download_ask_cb: User pressed %c\n", e
->keyval
);
4858 if (e
->keyval
== 'y' || e
->keyval
== 'Y' || e
->keyval
== GDK_Return
)
4859 /* We need to do a RESTART, because we're not calling from
4860 * webview_download_cb
4862 download_start(t
, d
, XT_DL_RESTART
);
4864 /* for all other keyvals, we just let the download be */
4865 e
->keyval
= GDK_Escape
;
4866 return (XT_CB_HANDLED
);
4870 download_ask(struct tab
*t
, struct download
*d
)
4872 const gchar
*suggested_name
;
4874 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
4875 if (suggested_name
== NULL
)
4876 return (FALSE
); /* abort download */
4878 show_oops(t
, "download file %s [y/n] ?", suggested_name
);
4879 t
->mode_cb
= download_ask_cb
;
4880 t
->mode_cb_data
= d
;
4886 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
4889 const gchar
*suggested_name
;
4890 struct download
*download_entry
;
4893 if (wk_download
== NULL
|| t
== NULL
) {
4894 show_oops(NULL
, "%s invalid parameters", __func__
);
4898 suggested_name
= webkit_download_get_suggested_filename(wk_download
);
4899 if (suggested_name
== NULL
)
4900 return (FALSE
); /* abort download */
4902 download_entry
= g_malloc(sizeof(struct download
));
4903 download_entry
->download
= wk_download
;
4904 download_entry
->tab
= t
;
4905 download_entry
->id
= next_download_id
++;
4906 RB_INSERT(download_list
, &downloads
, download_entry
);
4908 if (download_mode
== XT_DM_START
)
4909 ret
= download_start(t
, download_entry
, XT_DL_START
);
4910 else if (download_mode
== XT_DM_ASK
)
4911 ret
= download_ask(t
, download_entry
);
4912 else if (download_mode
== XT_DM_ADD
)
4913 show_oops(t
, "added %s to download manager",
4916 /* sync other download manager tabs */
4917 update_download_tabs(NULL
);
4920 * NOTE: never redirect/render the current tab before this
4921 * function returns. This will cause the download to never start.
4923 return (ret
); /* start download */
4927 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
4929 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
4932 show_oops(NULL
, "webview_hover_cb");
4937 set_status(t
, uri
, XT_STATUS_LINK
);
4940 set_status(t
, t
->status
, XT_STATUS_NOTHING
);
4945 mark(struct tab
*t
, struct karg
*arg
)
4952 if ((index
= marktoindex(mark
)) == -1)
4955 if (arg
->i
== XT_MARK_SET
)
4956 t
->mark
[index
] = gtk_adjustment_get_value(t
->adjust_v
);
4957 else if (arg
->i
== XT_MARK_GOTO
) {
4958 if (t
->mark
[index
] == XT_INVALID_MARK
) {
4959 show_oops(t
, "mark '%c' does not exist", mark
);
4962 /* XXX t->mark[index] can be bigger than the maximum if ajax or
4963 something changes the document size */
4964 pos
= gtk_adjustment_get_value(t
->adjust_v
);
4965 gtk_adjustment_set_value(t
->adjust_v
, t
->mark
[index
]);
4966 t
->mark
[marktoindex('\'')] = pos
;
4973 marks_clear(struct tab
*t
)
4977 for (i
= 0; i
< LENGTH(t
->mark
); i
++)
4978 t
->mark
[i
] = XT_INVALID_MARK
;
4984 char file
[PATH_MAX
];
4985 char *line
= NULL
, *p
;
4990 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
4991 if ((f
= fopen(file
, "r+")) == NULL
) {
4992 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
4996 for (i
= 1; ; i
++) {
4997 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
4999 if (strlen(line
) == 0 || line
[0] == '#') {
5005 p
= strtok(line
, " \t");
5007 if (p
== NULL
|| strlen(p
) != 1 ||
5008 (index
= qmarktoindex(*p
)) == -1) {
5009 warnx("corrupt quickmarks file, line %d", i
);
5013 p
= strtok(NULL
, " \t");
5014 if (qmarks
[index
] != NULL
)
5015 g_free(qmarks
[index
]);
5016 qmarks
[index
] = g_strdup(p
);
5027 char file
[PATH_MAX
];
5031 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
5032 if ((f
= fopen(file
, "r+")) == NULL
) {
5033 show_oops(NULL
, "Can't open quickmarks file: %s", strerror(errno
));
5037 for (i
= 0; i
< XT_NOQMARKS
; i
++)
5038 if (qmarks
[i
] != NULL
)
5039 fprintf(f
, "%c %s\n", indextoqmark(i
), qmarks
[i
]);
5047 qmark(struct tab
*t
, struct karg
*arg
)
5052 mark
= arg
->s
[strlen(arg
->s
)-1];
5053 index
= qmarktoindex(mark
);
5059 if (qmarks
[index
] != NULL
) {
5060 g_free(qmarks
[index
]);
5061 qmarks
[index
] = NULL
;
5064 qmarks_load(); /* sync if multiple instances */
5065 qmarks
[index
] = g_strdup(get_uri(t
));
5069 if (qmarks
[index
] != NULL
)
5070 load_uri(t
, qmarks
[index
]);
5072 show_oops(t
, "quickmark \"%c\" does not exist",
5078 if (qmarks
[index
] != NULL
)
5079 create_new_tab(qmarks
[index
], NULL
, 1, -1);
5081 show_oops(t
, "quickmark \"%c\" does not exist",
5092 go_up(struct tab
*t
, struct karg
*args
)
5100 if (args
->i
== XT_GO_UP_ROOT
)
5101 levels
= XT_GO_UP_ROOT
;
5102 else if ((levels
= atoi(args
->s
)) == 0)
5105 uri
= g_strdup(get_uri(t
));
5109 if ((tmp
= strstr(uri
, XT_PROTO_DELIM
)) == NULL
)
5112 tmp
+= strlen(XT_PROTO_DELIM
);
5114 /* it makes no sense to strip the last slash from ".../dir/", skip it */
5115 lastidx
= strlen(tmp
) - 1;
5117 if (tmp
[lastidx
] == '/')
5118 tmp
[lastidx
] = '\0';
5122 p
= strrchr(tmp
, '/');
5123 if (p
== tmp
) { /* Are we at the root of a file://-path? */
5126 } else if (p
!= NULL
)
5139 gototab(struct tab
*t
, struct karg
*args
)
5142 struct karg arg
= {0, NULL
, -1};
5144 tab
= atoi(args
->s
);
5147 arg
.i
= XT_TAB_NEXT
;
5159 zoom_amount(struct tab
*t
, struct karg
*arg
)
5161 struct karg narg
= {0, NULL
, -1};
5163 narg
.i
= atoi(arg
->s
);
5164 resizetab(t
, &narg
);
5170 flip_colon(struct tab
*t
, struct karg
*arg
)
5172 struct karg narg
= {0, NULL
, -1};
5175 if (t
== NULL
|| arg
== NULL
)
5178 p
= strstr(arg
->s
, ":");
5190 /* buffer commands receive the regex that triggered them in arg.s */
5191 char bcmd
[XT_BUFCMD_SZ
];
5195 #define XT_PRE_NO (0)
5196 #define XT_PRE_YES (1)
5197 #define XT_PRE_MAYBE (2)
5199 int (*func
)(struct tab
*, struct karg
*);
5203 { "^[0-9]*gu$", XT_PRE_MAYBE
, "gu", go_up
, 0 },
5204 { "^gU$", XT_PRE_NO
, "gU", go_up
, XT_GO_UP_ROOT
},
5205 { "^gg$", XT_PRE_NO
, "gg", move
, XT_MOVE_TOP
},
5206 { "^gG$", XT_PRE_NO
, "gG", move
, XT_MOVE_BOTTOM
},
5207 { "^[0-9]+%$", XT_PRE_YES
, "%", move
, XT_MOVE_PERCENT
},
5208 { "^zz$", XT_PRE_NO
, "zz", move
, XT_MOVE_CENTER
},
5209 { "^gh$", XT_PRE_NO
, "gh", go_home
, 0 },
5210 { "^m[a-zA-Z0-9]$", XT_PRE_NO
, "m", mark
, XT_MARK_SET
},
5211 { "^['][a-zA-Z0-9']$", XT_PRE_NO
, "'", mark
, XT_MARK_GOTO
},
5212 { "^[0-9]+t$", XT_PRE_YES
, "t", gototab
, 0 },
5213 { "^g0$", XT_PRE_YES
, "g0", movetab
, XT_TAB_FIRST
},
5214 { "^g[$]$", XT_PRE_YES
, "g$", movetab
, XT_TAB_LAST
},
5215 { "^[0-9]*gt$", XT_PRE_YES
, "t", movetab
, XT_TAB_NEXT
},
5216 { "^[0-9]*gT$", XT_PRE_YES
, "T", movetab
, XT_TAB_PREV
},
5217 { "^M[a-zA-Z0-9]$", XT_PRE_NO
, "M", qmark
, XT_QMARK_SET
},
5218 { "^go[a-zA-Z0-9]$", XT_PRE_NO
, "go", qmark
, XT_QMARK_OPEN
},
5219 { "^gn[a-zA-Z0-9]$", XT_PRE_NO
, "gn", qmark
, XT_QMARK_TAB
},
5220 { "^ZR$", XT_PRE_NO
, "ZR", restart
, 0 },
5221 { "^ZZ$", XT_PRE_NO
, "ZZ", quit
, 0 },
5222 { "^zi$", XT_PRE_NO
, "zi", resizetab
, XT_ZOOM_IN
},
5223 { "^zo$", XT_PRE_NO
, "zo", resizetab
, XT_ZOOM_OUT
},
5224 { "^z0$", XT_PRE_NO
, "z0", resizetab
, XT_ZOOM_NORMAL
},
5225 { "^[0-9]+Z$", XT_PRE_YES
, "Z", zoom_amount
, 0 },
5226 { "^[0-9]+:$", XT_PRE_YES
, ":", flip_colon
, 0 },
5230 buffercmd_init(void)
5234 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5235 if (regcomp(&buffercmds
[i
].cregex
, buffercmds
[i
].regex
,
5236 REG_EXTENDED
| REG_NOSUB
))
5237 startpage_add("invalid buffercmd regex %s",
5238 buffercmds
[i
].regex
);
5242 buffercmd_abort(struct tab
*t
)
5249 DNPRINTF(XT_D_BUFFERCMD
, "%s: clearing buffer\n", __func__
);
5251 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5254 cmd_prefix
= 0; /* clear prefix for non-buffer commands */
5255 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
5259 buffercmd_execute(struct tab
*t
, struct buffercmd
*cmd
)
5261 struct karg arg
= {0, NULL
, -1};
5264 arg
.s
= g_strdup(bcmd
);
5266 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_execute: buffer \"%s\" "
5267 "matches regex \"%s\", executing\n", bcmd
, cmd
->regex
);
5277 buffercmd_addkey(struct tab
*t
, guint keyval
)
5280 char s
[XT_BUFCMD_SZ
];
5282 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
))) {
5284 return (XT_CB_PASSTHROUGH
);
5287 if (keyval
== GDK_Escape
) {
5289 return (XT_CB_HANDLED
);
5292 /* key with modifier or non-ascii character */
5293 if (!isascii(keyval
)) {
5295 * XXX this looks wrong but fixes some sites like
5296 * http://www.seslisozluk.com/
5297 * that eat a shift or ctrl and end putting default focus in js
5298 * instead of ignoring the keystroke
5299 * so instead of return (XT_CB_PASSTHROUGH); eat the key
5301 return (XT_CB_HANDLED
);
5304 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: adding key \"%c\" "
5305 "to buffer \"%s\"\n", keyval
, bcmd
);
5307 for (i
= 0; i
< LENGTH(bcmd
); i
++)
5308 if (bcmd
[i
] == '\0') {
5313 /* buffer full, ignore input */
5314 if (i
>= LENGTH(bcmd
) -1) {
5315 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: buffer full\n");
5317 return (XT_CB_HANDLED
);
5320 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.buffercmd
), bcmd
);
5322 /* find exact match */
5323 for (i
= 0; i
< LENGTH(buffercmds
); i
++)
5324 if (regexec(&buffercmds
[i
].cregex
, bcmd
,
5325 (size_t) 0, NULL
, 0) == 0) {
5326 buffercmd_execute(t
, &buffercmds
[i
]);
5330 /* find non exact matches to see if we need to abort ot not */
5331 for (i
= 0, match
= 0; i
< LENGTH(buffercmds
); i
++) {
5332 DNPRINTF(XT_D_BUFFERCMD
, "trying: %s\n", bcmd
);
5335 if (buffercmds
[i
].precount
== XT_PRE_MAYBE
) {
5336 if (isdigit(bcmd
[0])) {
5337 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5341 if (sscanf(bcmd
, "%s", s
) == 0)
5344 } else if (buffercmds
[i
].precount
== XT_PRE_YES
) {
5345 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5348 if (sscanf(bcmd
, "%s", s
) == 0)
5351 if (c
== -1 && buffercmds
[i
].precount
)
5353 if (!strncmp(s
, buffercmds
[i
].cmd
, strlen(s
)))
5356 DNPRINTF(XT_D_BUFFERCMD
, "got[%d] %d <%s>: %d %s\n",
5357 i
, match
, buffercmds
[i
].cmd
, c
, s
);
5360 DNPRINTF(XT_D_BUFFERCMD
, "aborting: %s\n", bcmd
);
5365 return (XT_CB_HANDLED
);
5369 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
5371 struct key_binding
*k
;
5373 /* handle keybindings if buffercmd is empty.
5374 if not empty, allow commands like C-n */
5375 if (bcmd
[0] == '\0' || ((e
->state
& (CTRL
| MOD1
)) != 0))
5376 TAILQ_FOREACH(k
, &kbl
, entry
)
5377 if (e
->keyval
== k
->key
5378 && (entry
? k
->use_in_entry
: 1)) {
5379 /* when we are edditing eat ctrl/mod keys */
5380 if (edit_mode
== XT_EM_VI
&&
5381 t
->mode
== XT_MODE_INSERT
&&
5382 (e
->state
& CTRL
|| e
->state
& MOD1
))
5383 return (XT_CB_PASSTHROUGH
);
5386 if ((e
->state
& (CTRL
| MOD1
)) == 0)
5387 return (cmd_execute(t
, k
->cmd
));
5388 } else if ((e
->state
& k
->mask
) == k
->mask
) {
5389 return (cmd_execute(t
, k
->cmd
));
5393 if (!entry
&& ((e
->state
& (CTRL
| MOD1
)) == 0))
5394 return buffercmd_addkey(t
, e
->keyval
);
5396 return (XT_CB_PASSTHROUGH
);
5400 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5404 /* don't use w directly; use t->whatever instead */
5407 show_oops(NULL
, "wv_keypress_cb");
5408 return (XT_CB_PASSTHROUGH
);
5414 return t
->mode_cb(t
, e
, t
->mode_cb_data
);
5416 DNPRINTF(XT_D_KEY
, "wv_keypress_cb: mode %d keyval 0x%x mask "
5417 "0x%x tab %d\n", t
->mode
, e
->keyval
, e
->state
, t
->tab_id
);
5419 /* Hide buffers, if they are visible, with escape. */
5420 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)) &&
5421 CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
5422 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5424 return (XT_CB_HANDLED
);
5427 if (t
->mode
== XT_MODE_HINT
)
5428 return (XT_CB_HANDLED
);
5430 if ((CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Tab
) ||
5431 (CLEAN(e
->state
) == SHFT
&& e
->keyval
== GDK_Tab
))
5432 /* something focussy is about to happen */
5433 return (XT_CB_PASSTHROUGH
);
5435 /* check if we are some sort of text input thing in the dom */
5436 input_check_mode(t
);
5438 if (t
->mode
== XT_MODE_HINT
) {
5439 /* XXX make sure cmd entry is enabled */
5440 return (XT_CB_HANDLED
);
5441 } else if (t
->mode
== XT_MODE_PASSTHROUGH
) {
5442 if (CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
)
5443 t
->mode
= XT_MODE_COMMAND
;
5444 return (XT_CB_PASSTHROUGH
);
5445 } else if (t
->mode
== XT_MODE_COMMAND
) {
5447 snprintf(s
, sizeof s
, "%c", e
->keyval
);
5448 if (CLEAN(e
->state
) == 0 && isdigit(s
[0]))
5449 cmd_prefix
= 10 * cmd_prefix
+ atoi(s
);
5450 return (handle_keypress(t
, e
, 0));
5453 return (handle_keypress(t
, e
, 1));
5457 return (XT_CB_PASSTHROUGH
);
5461 hint_continue(struct tab
*t
)
5463 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5465 const gchar
*errstr
= NULL
;
5469 if (!(c
[0] == '.' || c
[0] == ','))
5471 if (strlen(c
) == 1) {
5472 /* XXX should not happen */
5477 if (isdigit(c
[1])) {
5479 i
= strtonum(&c
[1], 1, 4096, &errstr
);
5481 show_oops(t
, "invalid numerical hint %s", &c
[1]);
5484 s
= g_strdup_printf("hints.updateHints(%d);", i
);
5488 /* alphanumeric input */
5489 s
= g_strdup_printf("hints.createHints('%s', '%c');",
5490 &c
[1], c
[0] == '.' ? 'f' : 'F');
5501 search_continue(struct tab
*t
)
5503 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5504 gboolean rv
= FALSE
;
5506 if (c
[0] == ':' || c
[0] == '.' || c
[0] == ',')
5508 if (strlen(c
) == 1) {
5509 webkit_web_view_unmark_text_matches(t
->wv
);
5514 t
->search_forward
= TRUE
;
5515 else if (c
[0] == '?')
5516 t
->search_forward
= FALSE
;
5526 search_cb(struct tab
*t
)
5528 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5531 if (search_continue(t
) == FALSE
)
5535 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, t
->search_forward
,
5537 /* not found, mark red */
5538 gdk_color_parse(XT_COLOR_RED
, &color
);
5539 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
5540 /* unmark and remove selection */
5541 webkit_web_view_unmark_text_matches(t
->wv
);
5542 /* my kingdom for a way to unselect text in webview */
5544 /* found, highlight all */
5545 webkit_web_view_unmark_text_matches(t
->wv
);
5546 webkit_web_view_mark_text_matches(t
->wv
, &c
[1], FALSE
, 0);
5547 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
5548 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
5549 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
5557 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5559 const gchar
*c
= gtk_entry_get_text(w
);
5562 show_oops(NULL
, "cmd_keyrelease_cb invalid parameters");
5563 return (XT_CB_PASSTHROUGH
);
5566 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
5567 e
->keyval
, e
->state
, t
->tab_id
);
5570 if (!(e
->keyval
== GDK_Tab
|| e
->keyval
== GDK_ISO_Left_Tab
)) {
5571 if (hint_continue(t
) == FALSE
)
5576 if (search_continue(t
) == FALSE
)
5579 /* if search length is > 4 then no longer play timeout games */
5580 if (strlen(c
) > 4) {
5582 g_source_remove(t
->search_id
);
5589 /* reestablish a new timer if the user types fast */
5591 g_source_remove(t
->search_id
);
5592 t
->search_id
= g_timeout_add(250, (GSourceFunc
)search_cb
, (gpointer
)t
);
5595 return (XT_CB_PASSTHROUGH
);
5599 match_uri(const gchar
*uri
, const gchar
*key
) {
5602 gboolean match
= FALSE
;
5606 if (!strncmp(key
, uri
, len
))
5609 voffset
= strstr(uri
, "/") + 2;
5610 if (!strncmp(key
, voffset
, len
))
5612 else if (g_str_has_prefix(voffset
, "www.")) {
5613 voffset
= voffset
+ strlen("www.");
5614 if (!strncmp(key
, voffset
, len
))
5623 match_session(const gchar
*name
, const gchar
*key
) {
5626 sub
= strcasestr(name
, key
);
5632 cmd_getlist(int id
, char *key
)
5639 if (cmds
[id
].type
& XT_URLARG
) {
5640 RB_FOREACH_REVERSE(h
, history_list
, &hl
)
5641 if (match_uri(h
->uri
, key
)) {
5642 cmd_status
.list
[c
] = (char *)h
->uri
;
5648 } else if (cmds
[id
].type
& XT_SESSARG
) {
5649 TAILQ_FOREACH(s
, &sessions
, entry
)
5650 if (match_session(s
->name
, key
)) {
5651 cmd_status
.list
[c
] = (char *)s
->name
;
5657 } else if (cmds
[id
].type
& XT_SETARG
) {
5658 for (i
= 0; i
< get_settings_size(); i
++)
5659 if (!strncmp(key
, get_setting_name(i
),
5661 cmd_status
.list
[c
++] =
5662 get_setting_name(i
);
5668 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
5670 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
5671 if (cmds
[i
].level
< dep
)
5673 if (cmds
[i
].level
== dep
&& !strncmp(key
, cmds
[i
].cmd
,
5674 strlen(key
)) && !isdigit(cmds
[i
].cmd
[0]))
5675 cmd_status
.list
[c
++] = cmds
[i
].cmd
;
5683 cmd_getnext(int dir
)
5685 cmd_status
.index
+= dir
;
5687 if (cmd_status
.index
< 0)
5688 cmd_status
.index
= cmd_status
.len
- 1;
5689 else if (cmd_status
.index
>= cmd_status
.len
)
5690 cmd_status
.index
= 0;
5692 return cmd_status
.list
[cmd_status
.index
];
5696 cmd_tokenize(char *s
, char *tokens
[])
5699 char *tok
, *last
= NULL
;
5700 size_t len
= strlen(s
);
5703 blank
= len
== 0 || (len
> 0 && s
[len
- 1] == ' ');
5704 for (tok
= strtok_r(s
, " ", &last
); tok
&& i
< 3;
5705 tok
= strtok_r(NULL
, " ", &last
), i
++)
5715 cmd_complete(struct tab
*t
, char *str
, int dir
)
5717 GtkEntry
*w
= GTK_ENTRY(t
->cmd
);
5718 int i
, j
, levels
, c
= 0, dep
= 0, parent
= -1;
5720 char *tok
, *match
, *s
= g_strdup(str
);
5722 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
5725 DNPRINTF(XT_D_CMD
, "%s: complete %s\n", __func__
, str
);
5728 for (i
= 0; isdigit(s
[i
]); i
++)
5731 for (; isspace(s
[i
]); i
++)
5736 levels
= cmd_tokenize(s
, tokens
);
5738 for (i
= 0; i
< levels
- 1; i
++) {
5741 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5742 if (cmds
[j
].level
< dep
)
5744 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
,
5748 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
5755 if (matchcount
== 1) {
5756 strlcat(res
, tok
, sizeof res
);
5757 strlcat(res
, " ", sizeof res
);
5767 if (cmd_status
.index
== -1)
5768 cmd_getlist(parent
, tokens
[i
]);
5770 if (cmd_status
.len
> 0) {
5771 match
= cmd_getnext(dir
);
5772 strlcat(res
, match
, sizeof res
);
5773 gtk_entry_set_text(w
, res
);
5774 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5781 cmd_execute(struct tab
*t
, char *str
)
5783 struct cmd
*cmd
= NULL
;
5784 char *tok
, *last
= NULL
, *s
= g_strdup(str
), *sc
;
5786 int j
, len
, c
= 0, dep
= 0, matchcount
= 0;
5787 int prefix
= -1, rv
= XT_CB_PASSTHROUGH
;
5788 struct karg arg
= {0, NULL
, -1};
5793 for (j
= 0; j
<3 && isdigit(s
[j
]); j
++)
5799 while (isspace(s
[0]))
5802 if (strlen(s
) > 0 && strlen(prefixstr
) > 0)
5803 prefix
= atoi(prefixstr
);
5807 for (tok
= strtok_r(s
, " ", &last
); tok
;
5808 tok
= strtok_r(NULL
, " ", &last
)) {
5810 for (j
= c
; j
< LENGTH(cmds
); j
++) {
5811 if (cmds
[j
].level
< dep
)
5813 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1 :
5815 if (cmds
[j
].level
== dep
&&
5816 !strncmp(tok
, cmds
[j
].cmd
, len
)) {
5820 if (len
== strlen(cmds
[j
].cmd
)) {
5826 if (matchcount
== 1) {
5831 show_oops(t
, "Invalid command: %s", str
);
5837 show_oops(t
, "Empty command");
5843 arg
.precount
= prefix
;
5844 else if (cmd_prefix
> 0)
5845 arg
.precount
= cmd_prefix
;
5847 if (j
> 0 && !(cmd
->type
& XT_PREFIX
) && arg
.precount
> -1) {
5848 show_oops(t
, "No prefix allowed: %s", str
);
5852 arg
.s
= last
? g_strdup(last
) : g_strdup("");
5853 if (cmd
->type
& XT_INTARG
&& last
&& strlen(last
) > 0) {
5854 if (arg
.s
== NULL
) {
5855 show_oops(t
, "Invalid command");
5858 arg
.precount
= atoi(arg
.s
);
5859 if (arg
.precount
<= 0) {
5860 if (arg
.s
[0] == '0')
5861 show_oops(t
, "Zero count");
5863 show_oops(t
, "Trailing characters");
5868 DNPRINTF(XT_D_CMD
, "%s: prefix %d arg %s\n",
5869 __func__
, arg
.precount
, arg
.s
);
5885 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5888 show_oops(NULL
, "entry_key_cb invalid parameters");
5889 return (XT_CB_PASSTHROUGH
);
5892 DNPRINTF(XT_D_CMD
, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
5893 e
->keyval
, e
->state
, t
->tab_id
);
5897 if (e
->keyval
== GDK_Escape
) {
5898 /* don't use focus_webview(t) because we want to type :cmds */
5899 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
5902 return (handle_keypress(t
, e
, 1));
5905 struct command_entry
*
5906 history_prev(struct command_list
*l
, struct command_entry
*at
)
5909 at
= TAILQ_LAST(l
, command_list
);
5911 at
= TAILQ_PREV(at
, command_list
, entry
);
5913 at
= TAILQ_LAST(l
, command_list
);
5919 struct command_entry
*
5920 history_next(struct command_list
*l
, struct command_entry
*at
)
5923 at
= TAILQ_FIRST(l
);
5925 at
= TAILQ_NEXT(at
, entry
);
5927 at
= TAILQ_FIRST(l
);
5934 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5936 int rv
= XT_CB_HANDLED
;
5937 const gchar
*c
= gtk_entry_get_text(w
);
5941 show_oops(NULL
, "cmd_keypress_cb parameters");
5942 return (XT_CB_PASSTHROUGH
);
5945 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
5946 e
->keyval
, e
->state
, t
->tab_id
);
5950 e
->keyval
= GDK_Escape
;
5951 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
5952 c
[0] == '.' || c
[0] == ','))
5953 e
->keyval
= GDK_Escape
;
5955 if (e
->keyval
!= GDK_Tab
&& e
->keyval
!= GDK_Shift_L
&&
5956 e
->keyval
!= GDK_ISO_Left_Tab
)
5957 cmd_status
.index
= -1;
5959 switch (e
->keyval
) {
5962 cmd_complete(t
, (char *)&c
[1], 1);
5963 else if (c
[0] == '.' || c
[0] == ',')
5964 run_script(t
, "hints.focusNextHint();");
5966 case GDK_ISO_Left_Tab
:
5968 cmd_complete(t
, (char *)&c
[1], -1);
5969 else if (c
[0] == '.' || c
[0] == ',')
5970 run_script(t
, "hints.focusPreviousHint();");
5974 if ((search_at
= history_next(&shl
, search_at
))) {
5975 search_at
->line
[0] = c
[0];
5976 gtk_entry_set_text(w
, search_at
->line
);
5977 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5979 } else if (c
[0] == '/') {
5980 if ((search_at
= history_prev(&shl
, search_at
))) {
5981 search_at
->line
[0] = c
[0];
5982 gtk_entry_set_text(w
, search_at
->line
);
5983 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5985 } if (c
[0] == ':') {
5986 if ((history_at
= history_prev(&chl
, history_at
))) {
5987 history_at
->line
[0] = c
[0];
5988 gtk_entry_set_text(w
, history_at
->line
);
5989 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
5995 if ((search_at
= history_next(&shl
, search_at
))) {
5996 search_at
->line
[0] = c
[0];
5997 gtk_entry_set_text(w
, search_at
->line
);
5998 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6000 } else if (c
[0] == '?') {
6001 if ((search_at
= history_prev(&shl
, search_at
))) {
6002 search_at
->line
[0] = c
[0];
6003 gtk_entry_set_text(w
, search_at
->line
);
6004 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6006 } if (c
[0] == ':') {
6007 if ((history_at
= history_next(&chl
, history_at
))) {
6008 history_at
->line
[0] = c
[0];
6009 gtk_entry_set_text(w
, history_at
->line
);
6010 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6015 if (!(!strcmp(c
, ":") || !strcmp(c
, "/") || !strcmp(c
, "?") ||
6016 !strcmp(c
, ".") || !strcmp(c
, ","))) {
6017 /* see if we are doing hinting and reset it */
6018 if (c
[0] == '.' || c
[0] == ',') {
6019 /* recreate hints */
6020 s
= g_strdup_printf("hints.createHints('', "
6021 "'%c');", c
[0] == '.' ? 'f' : 'F');
6034 if (c
!= NULL
&& (c
[0] == '/' || c
[0] == '?'))
6035 webkit_web_view_unmark_text_matches(t
->wv
);
6037 /* no need to cancel hints */
6041 rv
= XT_CB_PASSTHROUGH
;
6047 wv_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
6049 DNPRINTF(XT_D_CMD
, "wv_popup_cb: tab %d\n", t
->tab_id
);
6053 cmd_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
6055 /* popup menu enabled */
6060 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
6063 show_oops(NULL
, "cmd_focusout_cb invalid parameters");
6064 return (XT_CB_PASSTHROUGH
);
6067 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d popup %d\n",
6068 t
->tab_id
, t
->popup
);
6070 /* if popup is enabled don't lose focus */
6073 return (XT_CB_PASSTHROUGH
);
6080 if (show_url
== 0 || t
->focus_wv
)
6083 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6085 return (XT_CB_PASSTHROUGH
);
6089 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
6092 const gchar
*c
= gtk_entry_get_text(entry
);
6095 show_oops(NULL
, "cmd_activate_cb invalid parameters");
6099 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
6104 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
6105 c
[0] == '.' || c
[0] == ','))
6111 if (c
[0] == '/' || c
[0] == '?') {
6112 /* see if there is a timer pending */
6114 g_source_remove(t
->search_id
);
6119 if (t
->search_text
) {
6120 g_free(t
->search_text
);
6121 t
->search_text
= NULL
;
6124 t
->search_text
= g_strdup(s
);
6126 g_free(global_search
);
6127 global_search
= g_strdup(s
);
6128 t
->search_forward
= c
[0] == '/';
6130 history_add(&shl
, search_file
, s
, &search_history_count
);
6131 } else if (c
[0] == '.' || c
[0] == ',') {
6132 run_script(t
, "hints.fire();");
6133 /* XXX history for link following? */
6134 } else if (c
[0] == ':') {
6135 history_add(&chl
, command_file
, s
, &cmd_history_count
);
6136 /* can't call hide_cmd after cmd_execute */
6147 backward_cb(GtkWidget
*w
, struct tab
*t
)
6152 show_oops(NULL
, "backward_cb invalid parameters");
6156 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
6163 forward_cb(GtkWidget
*w
, struct tab
*t
)
6168 show_oops(NULL
, "forward_cb invalid parameters");
6172 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
6174 a
.i
= XT_NAV_FORWARD
;
6179 home_cb(GtkWidget
*w
, struct tab
*t
)
6182 show_oops(NULL
, "home_cb invalid parameters");
6186 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
6192 stop_cb(GtkWidget
*w
, struct tab
*t
)
6194 WebKitWebFrame
*frame
;
6197 show_oops(NULL
, "stop_cb invalid parameters");
6201 DNPRINTF(XT_D_NAV
, "stop_cb: tab %d\n", t
->tab_id
);
6203 frame
= webkit_web_view_get_main_frame(t
->wv
);
6204 if (frame
== NULL
) {
6205 show_oops(t
, "stop_cb: no frame");
6209 webkit_web_frame_stop_loading(frame
);
6210 abort_favicon_download(t
);
6214 setup_webkit(struct tab
*t
)
6216 if (is_g_object_setting(G_OBJECT(t
->settings
), "enable-dns-prefetching"))
6217 g_object_set(G_OBJECT(t
->settings
), "enable-dns-prefetching",
6218 FALSE
, (char *)NULL
);
6220 warnx("webkit does not have \"enable-dns-prefetching\" property");
6221 g_object_set(G_OBJECT(t
->settings
),
6222 "user-agent", t
->user_agent
, (char *)NULL
);
6223 g_object_set(G_OBJECT(t
->settings
),
6224 "enable-scripts", enable_scripts
, (char *)NULL
);
6225 g_object_set(G_OBJECT(t
->settings
),
6226 "enable-plugins", enable_plugins
, (char *)NULL
);
6227 g_object_set(G_OBJECT(t
->settings
),
6228 "javascript-can-open-windows-automatically", enable_scripts
,
6230 g_object_set(G_OBJECT(t
->settings
),
6231 "enable-html5-database", FALSE
, (char *)NULL
);
6232 g_object_set(G_OBJECT(t
->settings
),
6233 "enable-html5-local-storage", enable_localstorage
, (char *)NULL
);
6234 g_object_set(G_OBJECT(t
->settings
),
6235 "enable_spell_checking", enable_spell_checking
, (char *)NULL
);
6236 g_object_set(G_OBJECT(t
->settings
),
6237 "spell_checking_languages", spell_check_languages
, (char *)NULL
);
6238 g_object_set(G_OBJECT(t
->settings
),
6239 "enable-developer-extras", TRUE
, (char *)NULL
);
6240 g_object_set(G_OBJECT(t
->wv
),
6241 "full-content-zoom", TRUE
, (char *)NULL
);
6242 g_object_set(G_OBJECT(t
->settings
),
6243 "auto-load-images", auto_load_images
, (char *)NULL
);
6245 webkit_web_view_set_settings(t
->wv
, t
->settings
);
6249 update_statusbar_position(GtkAdjustment
* adjustment
, gpointer data
)
6251 struct tab
*ti
, *t
= NULL
;
6252 gdouble view_size
, value
, max
;
6255 TAILQ_FOREACH(ti
, &tabs
, entry
)
6256 if (ti
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
6264 if (adjustment
== NULL
)
6265 adjustment
= gtk_scrolled_window_get_vadjustment(
6266 GTK_SCROLLED_WINDOW(t
->browser_win
));
6268 view_size
= gtk_adjustment_get_page_size(adjustment
);
6269 value
= gtk_adjustment_get_value(adjustment
);
6270 max
= gtk_adjustment_get_upper(adjustment
) - view_size
;
6273 position
= g_strdup("All");
6274 else if (value
== max
)
6275 position
= g_strdup("Bot");
6276 else if (value
== 0)
6277 position
= g_strdup("Top");
6279 position
= g_strdup_printf("%d%%", (int) ((value
/ max
) * 100));
6281 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.position
), position
);
6288 create_window(const gchar
*name
)
6292 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
6293 if (window_maximize
)
6294 gtk_window_maximize(GTK_WINDOW(w
));
6296 gtk_window_set_default_size(GTK_WINDOW(w
), window_width
, window_height
);
6297 gtk_widget_set_name(w
, name
);
6298 gtk_window_set_wmclass(GTK_WINDOW(w
), name
, "XXXTerm");
6304 create_browser(struct tab
*t
)
6308 GtkAdjustment
*adjustment
;
6311 show_oops(NULL
, "create_browser invalid parameters");
6315 t
->sb_h
= GTK_SCROLLBAR(gtk_hscrollbar_new(NULL
));
6316 t
->sb_v
= GTK_SCROLLBAR(gtk_vscrollbar_new(NULL
));
6317 t
->adjust_h
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_h
));
6318 t
->adjust_v
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_v
));
6320 w
= gtk_scrolled_window_new(t
->adjust_h
, t
->adjust_v
);
6321 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
6322 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
6324 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
6325 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
6328 t
->settings
= webkit_web_settings_new();
6330 g_object_set(t
->settings
, "default-encoding", encoding
, (char *)NULL
);
6332 if (user_agent
== NULL
) {
6333 g_object_get(G_OBJECT(t
->settings
), "user-agent", &strval
,
6335 t
->user_agent
= g_strdup_printf("%s %s+", strval
, version
);
6338 t
->user_agent
= g_strdup(user_agent
->value
);
6340 t
->stylesheet
= g_strdup_printf("file://%s/style.css", resource_dir
);
6341 t
->load_images
= auto_load_images
;
6344 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w
));
6345 g_signal_connect(G_OBJECT(adjustment
), "value-changed",
6346 G_CALLBACK(update_statusbar_position
), NULL
);
6355 create_kiosk_toolbar(struct tab
*t
)
6357 GtkWidget
*toolbar
= NULL
, *b
;
6359 b
= gtk_hbox_new(FALSE
, 0);
6361 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
6363 /* backward button */
6364 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
6365 gtk_widget_set_sensitive(t
->backward
, FALSE
);
6366 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
6367 G_CALLBACK(backward_cb
), t
);
6368 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, TRUE
, TRUE
, 0);
6370 /* forward button */
6371 t
->forward
= create_button("Forward", GTK_STOCK_GO_FORWARD
, 0);
6372 gtk_widget_set_sensitive(t
->forward
, FALSE
);
6373 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
6374 G_CALLBACK(forward_cb
), t
);
6375 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, TRUE
, TRUE
, 0);
6378 t
->gohome
= create_button("Home", GTK_STOCK_HOME
, 0);
6379 gtk_widget_set_sensitive(t
->gohome
, true);
6380 g_signal_connect(G_OBJECT(t
->gohome
), "clicked",
6381 G_CALLBACK(home_cb
), t
);
6382 gtk_box_pack_start(GTK_BOX(b
), t
->gohome
, TRUE
, TRUE
, 0);
6384 /* create widgets but don't use them */
6385 t
->uri_entry
= gtk_entry_new();
6386 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
6387 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
6388 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
6389 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
6395 create_toolbar(struct tab
*t
)
6397 GtkWidget
*toolbar
= NULL
, *b
, *eb1
;
6399 b
= gtk_hbox_new(FALSE
, 0);
6401 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
6403 /* backward button */
6404 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
6405 gtk_widget_set_sensitive(t
->backward
, FALSE
);
6406 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
6407 G_CALLBACK(backward_cb
), t
);
6408 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, FALSE
, FALSE
, 0);
6410 /* forward button */
6411 t
->forward
= create_button("Forward",GTK_STOCK_GO_FORWARD
, 0);
6412 gtk_widget_set_sensitive(t
->forward
, FALSE
);
6413 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
6414 G_CALLBACK(forward_cb
), t
);
6415 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, FALSE
,
6419 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
6420 gtk_widget_set_sensitive(t
->stop
, FALSE
);
6421 g_signal_connect(G_OBJECT(t
->stop
), "clicked",
6422 G_CALLBACK(stop_cb
), t
);
6423 gtk_box_pack_start(GTK_BOX(b
), t
->stop
, FALSE
,
6427 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
6428 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
6429 gtk_widget_set_sensitive(t
->js_toggle
, TRUE
);
6430 g_signal_connect(G_OBJECT(t
->js_toggle
), "clicked",
6431 G_CALLBACK(js_toggle_cb
), t
);
6432 gtk_box_pack_start(GTK_BOX(b
), t
->js_toggle
, FALSE
, FALSE
, 0);
6434 t
->uri_entry
= gtk_entry_new();
6435 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
6436 G_CALLBACK(activate_uri_entry_cb
), t
);
6437 g_signal_connect(G_OBJECT(t
->uri_entry
), "key-press-event",
6438 G_CALLBACK(entry_key_cb
), t
);
6440 eb1
= gtk_hbox_new(FALSE
, 0);
6441 gtk_container_set_border_width(GTK_CONTAINER(eb1
), 1);
6442 gtk_box_pack_start(GTK_BOX(eb1
), t
->uri_entry
, TRUE
, TRUE
, 0);
6443 gtk_box_pack_start(GTK_BOX(b
), eb1
, TRUE
, TRUE
, 0);
6446 if (search_string
) {
6448 t
->search_entry
= gtk_entry_new();
6449 gtk_entry_set_width_chars(GTK_ENTRY(t
->search_entry
), 30);
6450 g_signal_connect(G_OBJECT(t
->search_entry
), "activate",
6451 G_CALLBACK(activate_search_entry_cb
), t
);
6452 g_signal_connect(G_OBJECT(t
->search_entry
), "key-press-event",
6453 G_CALLBACK(entry_key_cb
), t
);
6454 gtk_widget_set_size_request(t
->search_entry
, -1, -1);
6455 eb2
= gtk_hbox_new(FALSE
, 0);
6456 gtk_container_set_border_width(GTK_CONTAINER(eb2
), 1);
6457 gtk_box_pack_start(GTK_BOX(eb2
), t
->search_entry
, TRUE
, TRUE
,
6459 gtk_box_pack_start(GTK_BOX(b
), eb2
, FALSE
, FALSE
, 0);
6461 t
->default_style
= gtk_rc_get_style(t
->uri_entry
);
6467 create_buffers(struct tab
*t
)
6469 GtkCellRenderer
*renderer
;
6472 view
= gtk_tree_view_new();
6474 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view
), FALSE
);
6476 renderer
= gtk_cell_renderer_text_new();
6477 gtk_tree_view_insert_column_with_attributes
6478 (GTK_TREE_VIEW(view
), -1, "Id", renderer
, "text", COL_ID
, (char *)NULL
);
6480 renderer
= gtk_cell_renderer_pixbuf_new();
6481 gtk_tree_view_insert_column_with_attributes
6482 (GTK_TREE_VIEW(view
), -1, "Favicon", renderer
, "pixbuf", COL_FAVICON
,
6485 renderer
= gtk_cell_renderer_text_new();
6486 gtk_tree_view_insert_column_with_attributes
6487 (GTK_TREE_VIEW(view
), -1, "Title", renderer
, "text", COL_TITLE
,
6490 gtk_tree_view_set_model
6491 (GTK_TREE_VIEW(view
), GTK_TREE_MODEL(buffers_store
));
6497 row_activated_cb(GtkTreeView
*view
, GtkTreePath
*path
,
6498 GtkTreeViewColumn
*col
, struct tab
*t
)
6503 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6505 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
,
6508 (GTK_TREE_MODEL(buffers_store
), &iter
, COL_ID
, &id
, -1);
6509 set_current_tab(id
- 1);
6515 /* after tab reordering/creation/removal */
6522 TAILQ_FOREACH(t
, &tabs
, entry
) {
6523 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
6524 if (t
->tab_id
> maxid
)
6527 gtk_widget_show(t
->tab_elems
.sep
);
6530 TAILQ_FOREACH(t
, &tabs
, entry
) {
6531 if (t
->tab_id
== maxid
) {
6532 gtk_widget_hide(t
->tab_elems
.sep
);
6538 /* after active tab change */
6540 recolor_compact_tabs(void)
6546 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6547 TAILQ_FOREACH(t
, &tabs
, entry
)
6548 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
,
6551 curid
= gtk_notebook_get_current_page(notebook
);
6552 TAILQ_FOREACH(t
, &tabs
, entry
)
6553 if (t
->tab_id
== curid
) {
6554 gdk_color_parse(XT_COLOR_CT_ACTIVE
, &color
);
6555 gtk_widget_modify_fg(t
->tab_elems
.label
,
6556 GTK_STATE_NORMAL
, &color
);
6562 set_current_tab(int page_num
)
6564 buffercmd_abort(get_current_tab());
6565 gtk_notebook_set_current_page(notebook
, page_num
);
6566 recolor_compact_tabs();
6570 undo_close_tab_save(struct tab
*t
)
6574 struct undo
*u1
, *u2
;
6576 WebKitWebHistoryItem
*item
;
6578 if ((uri
= get_uri(t
)) == NULL
)
6581 u1
= g_malloc0(sizeof(struct undo
));
6582 u1
->uri
= g_strdup(uri
);
6584 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
6586 m
= webkit_web_back_forward_list_get_forward_length(t
->bfl
);
6587 n
= webkit_web_back_forward_list_get_back_length(t
->bfl
);
6590 /* forward history */
6591 items
= webkit_web_back_forward_list_get_forward_list_with_limit(t
->bfl
, m
);
6595 u1
->history
= g_list_prepend(u1
->history
,
6596 webkit_web_history_item_copy(item
));
6597 items
= g_list_next(items
);
6602 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
6603 u1
->history
= g_list_prepend(u1
->history
,
6604 webkit_web_history_item_copy(item
));
6608 items
= webkit_web_back_forward_list_get_back_list_with_limit(t
->bfl
, n
);
6612 u1
->history
= g_list_prepend(u1
->history
,
6613 webkit_web_history_item_copy(item
));
6614 items
= g_list_next(items
);
6617 TAILQ_INSERT_HEAD(&undos
, u1
, entry
);
6619 if (undo_count
> XT_MAX_UNDO_CLOSE_TAB
) {
6620 u2
= TAILQ_LAST(&undos
, undo_tailq
);
6621 TAILQ_REMOVE(&undos
, u2
, entry
);
6623 g_list_free(u2
->history
);
6632 delete_tab(struct tab
*t
)
6636 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
6642 * no need to join thread here because it won't access t on completion
6645 TAILQ_REMOVE(&tabs
, t
, entry
);
6648 /* Halt all webkit activity. */
6649 abort_favicon_download(t
);
6650 webkit_web_view_stop_loading(t
->wv
);
6652 /* Save the tab, so we can undo the close. */
6653 undo_close_tab_save(t
);
6657 g_source_remove(t
->search_id
);
6660 bzero(&a
, sizeof a
);
6662 inspector_cmd(t
, &a
);
6664 if (browser_mode
== XT_BM_KIOSK
) {
6665 gtk_widget_destroy(t
->uri_entry
);
6666 gtk_widget_destroy(t
->stop
);
6667 gtk_widget_destroy(t
->js_toggle
);
6670 gtk_widget_destroy(t
->tab_elems
.eventbox
);
6671 gtk_widget_destroy(t
->vbox
);
6673 g_free(t
->user_agent
);
6674 g_free(t
->stylesheet
);
6679 if (TAILQ_EMPTY(&tabs
)) {
6680 if (browser_mode
== XT_BM_KIOSK
)
6681 create_new_tab(home
, NULL
, 1, -1);
6683 create_new_tab(NULL
, NULL
, 1, -1);
6686 /* recreate session */
6687 if (session_autosave
) {
6688 bzero(&a
, sizeof a
);
6690 save_tabs(NULL
, &a
);
6694 recolor_compact_tabs();
6698 update_statusbar_zoom(struct tab
*t
)
6701 char s
[16] = { '\0' };
6703 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6704 if ((zoom
<= 0.99 || zoom
>= 1.01))
6705 snprintf(s
, sizeof s
, "%d%%", (int)(zoom
* 100));
6706 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.zoom
), s
);
6710 setzoom_webkit(struct tab
*t
, int adjust
)
6712 #define XT_ZOOMPERCENT 0.04
6717 show_oops(NULL
, "setzoom_webkit invalid parameters");
6721 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
6722 if (adjust
== XT_ZOOM_IN
)
6723 zoom
+= XT_ZOOMPERCENT
;
6724 else if (adjust
== XT_ZOOM_OUT
)
6725 zoom
-= XT_ZOOMPERCENT
;
6726 else if (adjust
> 0)
6727 zoom
= default_zoom_level
+ adjust
/ 100.0 - 1.0;
6729 show_oops(t
, "setzoom_webkit invalid zoom value");
6733 if (zoom
< XT_ZOOMPERCENT
)
6734 zoom
= XT_ZOOMPERCENT
;
6735 g_object_set(G_OBJECT(t
->wv
), "zoom-level", zoom
, (char *)NULL
);
6736 update_statusbar_zoom(t
);
6740 tab_clicked_cb(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
6742 struct tab
*t
= (struct tab
*) data
;
6744 DNPRINTF(XT_D_TAB
, "tab_clicked_cb: tab: %d\n", t
->tab_id
);
6746 switch (event
->button
) {
6748 set_current_tab(t
->tab_id
);
6759 append_tab(struct tab
*t
)
6764 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
6765 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
6769 create_sbe(int width
)
6773 sbe
= gtk_entry_new();
6774 gtk_entry_set_inner_border(GTK_ENTRY(sbe
), NULL
);
6775 gtk_entry_set_has_frame(GTK_ENTRY(sbe
), FALSE
);
6776 gtk_widget_set_can_focus(GTK_WIDGET(sbe
), FALSE
);
6777 gtk_widget_modify_font(GTK_WIDGET(sbe
), statusbar_font
);
6778 gtk_entry_set_alignment(GTK_ENTRY(sbe
), 1.0);
6779 gtk_widget_set_size_request(sbe
, width
, -1);
6785 create_new_tab(char *title
, struct undo
*u
, int focus
, int position
)
6790 WebKitWebHistoryItem
*item
;
6794 int sbe_p
= 0, sbe_b
= 0,
6797 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
6799 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
6800 DNPRINTF(XT_D_TAB
, "create_new_tab: new tab rejected\n");
6804 t
= g_malloc0(sizeof *t
);
6806 if (title
== NULL
) {
6807 title
= "(untitled)";
6811 t
->vbox
= gtk_vbox_new(FALSE
, 0);
6813 /* label + button for tab */
6814 b
= gtk_hbox_new(FALSE
, 0);
6817 #if GTK_CHECK_VERSION(2, 20, 0)
6818 t
->spinner
= gtk_spinner_new();
6820 t
->label
= gtk_label_new(title
);
6821 bb
= create_button("Close", GTK_STOCK_CLOSE
, 1);
6822 gtk_widget_set_size_request(t
->label
, 100, 0);
6823 gtk_label_set_max_width_chars(GTK_LABEL(t
->label
), 20);
6824 gtk_label_set_ellipsize(GTK_LABEL(t
->label
), PANGO_ELLIPSIZE_END
);
6825 gtk_widget_set_size_request(b
, 130, 0);
6827 gtk_box_pack_start(GTK_BOX(b
), bb
, FALSE
, FALSE
, 0);
6828 gtk_box_pack_start(GTK_BOX(b
), t
->label
, FALSE
, FALSE
, 0);
6829 #if GTK_CHECK_VERSION(2, 20, 0)
6830 gtk_box_pack_start(GTK_BOX(b
), t
->spinner
, FALSE
, FALSE
, 0);
6834 if (browser_mode
== XT_BM_KIOSK
) {
6835 t
->toolbar
= create_kiosk_toolbar(t
);
6836 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
6839 t
->toolbar
= create_toolbar(t
);
6841 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
,
6849 t
->browser_win
= create_browser(t
);
6850 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
6852 /* oops message for user feedback */
6853 t
->oops
= gtk_entry_new();
6854 gtk_entry_set_inner_border(GTK_ENTRY(t
->oops
), NULL
);
6855 gtk_entry_set_has_frame(GTK_ENTRY(t
->oops
), FALSE
);
6856 gtk_widget_set_can_focus(GTK_WIDGET(t
->oops
), FALSE
);
6857 gdk_color_parse(XT_COLOR_RED
, &color
);
6858 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
6859 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
6860 gtk_widget_modify_font(GTK_WIDGET(t
->oops
), oops_font
);
6863 t
->cmd
= gtk_entry_new();
6864 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
6865 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
6866 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
6867 gtk_widget_modify_font(GTK_WIDGET(t
->cmd
), cmd_font
);
6870 t
->statusbar_box
= gtk_hbox_new(FALSE
, 0);
6872 t
->sbe
.statusbar
= gtk_entry_new();
6873 gtk_entry_set_inner_border(GTK_ENTRY(t
->sbe
.statusbar
), NULL
);
6874 gtk_entry_set_has_frame(GTK_ENTRY(t
->sbe
.statusbar
), FALSE
);
6875 gtk_widget_set_can_focus(GTK_WIDGET(t
->sbe
.statusbar
), FALSE
);
6876 gtk_widget_modify_font(GTK_WIDGET(t
->sbe
.statusbar
), statusbar_font
);
6878 /* create these widgets only if specified in statusbar_elems */
6880 t
->sbe
.position
= create_sbe(40);
6881 t
->sbe
.zoom
= create_sbe(40);
6882 t
->sbe
.buffercmd
= create_sbe(60);
6884 statusbar_modify_attr(t
, XT_COLOR_WHITE
, XT_COLOR_BLACK
);
6886 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), t
->sbe
.statusbar
, TRUE
,
6889 /* gtk widgets cannot be added to a box twice. sbe_* variables
6890 make sure of this */
6891 for (p
= statusbar_elems
; *p
!= '\0'; p
++) {
6895 GtkWidget
*sep
= gtk_vseparator_new();
6897 gdk_color_parse(XT_COLOR_SB_SEPARATOR
, &color
);
6898 gtk_widget_modify_bg(sep
, GTK_STATE_NORMAL
, &color
);
6899 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
), sep
,
6900 FALSE
, FALSE
, FALSE
);
6905 warnx("flag \"%c\" specified more than "
6906 "once in statusbar_elems\n", *p
);
6910 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6911 t
->sbe
.position
, FALSE
, FALSE
, FALSE
);
6915 warnx("flag \"%c\" specified more than "
6916 "once in statusbar_elems\n", *p
);
6920 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6921 t
->sbe
.buffercmd
, FALSE
, FALSE
, FALSE
);
6925 warnx("flag \"%c\" specified more than "
6926 "once in statusbar_elems\n", *p
);
6930 gtk_box_pack_start(GTK_BOX(t
->statusbar_box
),
6931 t
->sbe
.zoom
, FALSE
, FALSE
, FALSE
);
6934 warnx("illegal flag \"%c\" in statusbar_elems\n", *p
);
6939 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar_box
, FALSE
, FALSE
, 0);
6942 t
->buffers
= create_buffers(t
);
6943 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->buffers
, FALSE
, FALSE
, 0);
6945 /* xtp meaning is normal by default */
6946 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
6948 /* set empty favicon */
6949 xt_icon_from_name(t
, "text-html");
6951 /* and show it all */
6952 gtk_widget_show_all(b
);
6953 gtk_widget_show_all(t
->vbox
);
6955 /* compact tab bar */
6956 t
->tab_elems
.label
= gtk_label_new(title
);
6957 t
->tab_elems
.favicon
= gtk_image_new();
6958 gtk_label_set_width_chars(GTK_LABEL(t
->tab_elems
.label
), 1.0);
6959 gtk_misc_set_alignment(GTK_MISC(t
->tab_elems
.label
), 0.0, 0.0);
6960 gtk_misc_set_padding(GTK_MISC(t
->tab_elems
.label
), 4.0, 4.0);
6961 gtk_widget_modify_font(GTK_WIDGET(t
->tab_elems
.label
), tabbar_font
);
6963 t
->tab_elems
.eventbox
= gtk_event_box_new();
6964 t
->tab_elems
.box
= gtk_hbox_new(FALSE
, 0);
6965 t
->tab_elems
.sep
= gtk_vseparator_new();
6967 gdk_color_parse(XT_COLOR_CT_BACKGROUND
, &color
);
6968 gtk_widget_modify_bg(t
->tab_elems
.eventbox
, GTK_STATE_NORMAL
, &color
);
6969 gdk_color_parse(XT_COLOR_CT_INACTIVE
, &color
);
6970 gtk_widget_modify_fg(t
->tab_elems
.label
, GTK_STATE_NORMAL
, &color
);
6971 gdk_color_parse(XT_COLOR_CT_SEPARATOR
, &color
);
6972 gtk_widget_modify_bg(t
->tab_elems
.sep
, GTK_STATE_NORMAL
, &color
);
6974 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.favicon
, FALSE
,
6976 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.label
, TRUE
,
6978 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.sep
, FALSE
,
6980 gtk_container_add(GTK_CONTAINER(t
->tab_elems
.eventbox
),
6983 gtk_box_pack_start(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
, TRUE
,
6985 gtk_widget_show_all(t
->tab_elems
.eventbox
);
6987 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
6990 id
= position
>= 0 ? position
:
6991 gtk_notebook_get_current_page(notebook
) + 1;
6992 if (id
> gtk_notebook_get_n_pages(notebook
))
6995 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
6996 gtk_notebook_insert_page(notebook
, t
->vbox
, b
, id
);
6997 gtk_box_reorder_child(GTK_BOX(tab_bar
),
6998 t
->tab_elems
.eventbox
, id
);
7003 #if GTK_CHECK_VERSION(2, 20, 0)
7004 /* turn spinner off if we are a new tab without uri */
7006 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
7007 gtk_widget_hide(t
->spinner
);
7010 /* make notebook tabs reorderable */
7011 gtk_notebook_set_tab_reorderable(notebook
, t
->vbox
, TRUE
);
7013 /* compact tabs clickable */
7014 g_signal_connect(G_OBJECT(t
->tab_elems
.eventbox
),
7015 "button_press_event", G_CALLBACK(tab_clicked_cb
), t
);
7017 g_object_connect(G_OBJECT(t
->cmd
),
7018 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb
), t
,
7019 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
7020 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb
), t
,
7021 "signal::activate", G_CALLBACK(cmd_activate_cb
), t
,
7022 "signal::populate-popup", G_CALLBACK(cmd_popup_cb
), t
,
7025 /* reuse wv_button_cb to hide oops */
7026 g_object_connect(G_OBJECT(t
->oops
),
7027 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7030 g_signal_connect(t
->buffers
,
7031 "row-activated", G_CALLBACK(row_activated_cb
), t
);
7032 g_object_connect(G_OBJECT(t
->buffers
),
7033 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
, (char *)NULL
);
7035 g_object_connect(G_OBJECT(t
->wv
),
7036 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
,
7037 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb
), t
,
7038 "signal::download-requested", G_CALLBACK(webview_download_cb
), t
,
7039 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), t
,
7040 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7041 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7042 "signal::resource-request-starting", G_CALLBACK(webview_rrs_cb
), t
,
7043 "signal::create-web-view", G_CALLBACK(webview_cwv_cb
), t
,
7044 "signal::close-web-view", G_CALLBACK(webview_closewv_cb
), t
,
7045 "signal::event", G_CALLBACK(webview_event_cb
), t
,
7046 "signal::load-finished", G_CALLBACK(webview_load_finished_cb
), t
,
7047 "signal::load-progress-changed", G_CALLBACK(webview_progress_changed_cb
), t
,
7048 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb
), t
,
7049 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7050 "signal::button_release_event", G_CALLBACK(wv_release_button_cb
), t
,
7051 "signal::populate-popup", G_CALLBACK(wv_popup_cb
), t
,
7053 g_signal_connect(t
->wv
,
7054 "notify::load-status", G_CALLBACK(notify_load_status_cb
), t
);
7055 g_signal_connect(t
->wv
,
7056 "notify::title", G_CALLBACK(notify_title_cb
), t
);
7058 /* hijack the unused keys as if we were the browser */
7059 //g_object_connect(G_OBJECT(t->toolbar),
7060 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
7063 g_signal_connect(G_OBJECT(bb
), "button_press_event",
7064 G_CALLBACK(tab_close_cb
), t
);
7067 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
7068 /* restore the tab's history */
7069 if (u
&& u
->history
) {
7073 webkit_web_back_forward_list_add_item(t
->bfl
, item
);
7074 items
= g_list_next(items
);
7077 item
= g_list_nth_data(u
->history
, u
->back
);
7079 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
7082 g_list_free(u
->history
);
7084 webkit_web_back_forward_list_clear(t
->bfl
);
7090 url_set_visibility();
7091 statusbar_set_visibility();
7094 set_current_tab(t
->tab_id
);
7095 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
7099 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
7103 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
7110 if (userstyle_global
)
7113 recolor_compact_tabs();
7114 setzoom_webkit(t
, XT_ZOOM_NORMAL
);
7119 notebook_switchpage_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7125 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
7127 if (gtk_notebook_get_current_page(notebook
) == -1)
7130 TAILQ_FOREACH(t
, &tabs
, entry
) {
7131 if (t
->tab_id
== pn
) {
7132 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
7135 uri
= get_title(t
, TRUE
);
7136 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
7142 /* can't use focus_webview here */
7143 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
7150 notebook_pagereordered_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7153 struct tab
*t
= NULL
, *tt
;
7157 TAILQ_FOREACH(tt
, &tabs
, entry
)
7158 if (tt
->tab_id
== pn
) {
7164 DNPRINTF(XT_D_TAB
, "page_reordered_cb: tab: %d\n", t
->tab_id
);
7166 gtk_box_reorder_child(GTK_BOX(tab_bar
), t
->tab_elems
.eventbox
,
7171 menuitem_response(struct tab
*t
)
7173 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
7177 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
7179 GtkWidget
*menu
, *menu_items
;
7180 GdkEventButton
*bevent
;
7184 if (event
->type
== GDK_BUTTON_PRESS
) {
7185 bevent
= (GdkEventButton
*) event
;
7186 menu
= gtk_menu_new();
7188 TAILQ_FOREACH(ti
, &tabs
, entry
) {
7189 if ((uri
= get_uri(ti
)) == NULL
)
7190 /* XXX make sure there is something to print */
7191 /* XXX add gui pages in here to look purdy */
7193 menu_items
= gtk_menu_item_new_with_label(uri
);
7194 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), menu_items
);
7195 gtk_widget_show(menu_items
);
7197 g_signal_connect_swapped((menu_items
),
7198 "activate", G_CALLBACK(menuitem_response
),
7202 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
,
7203 bevent
->button
, bevent
->time
);
7205 /* unref object so it'll free itself when popped down */
7206 #if !GTK_CHECK_VERSION(3, 0, 0)
7207 /* XXX does not need unref with gtk+3? */
7208 g_object_ref_sink(menu
);
7209 g_object_unref(menu
);
7212 return (TRUE
/* eat event */);
7215 return (FALSE
/* propagate */);
7219 icon_size_map(int iconsz
)
7221 if (iconsz
<= GTK_ICON_SIZE_INVALID
||
7222 iconsz
> GTK_ICON_SIZE_DIALOG
)
7223 return (GTK_ICON_SIZE_SMALL_TOOLBAR
);
7229 create_button(char *name
, char *stockid
, int size
)
7231 GtkWidget
*button
, *image
;
7235 rcstring
= g_strdup_printf(
7236 "style \"%s-style\"\n"
7238 " GtkWidget::focus-padding = 0\n"
7239 " GtkWidget::focus-line-width = 0\n"
7243 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
7244 gtk_rc_parse_string(rcstring
);
7246 button
= gtk_button_new();
7247 gtk_button_set_focus_on_click(GTK_BUTTON(button
), FALSE
);
7248 gtk_icon_size
= icon_size_map(size
? size
: icon_size
);
7250 image
= gtk_image_new_from_stock(stockid
, gtk_icon_size
);
7251 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
7252 gtk_container_set_border_width(GTK_CONTAINER(button
), 1);
7253 gtk_container_add(GTK_CONTAINER(button
), GTK_WIDGET(image
));
7254 gtk_widget_set_name(button
, name
);
7255 gtk_button_set_relief(GTK_BUTTON(button
), GTK_RELIEF_NONE
);
7261 button_set_stockid(GtkWidget
*button
, char *stockid
)
7265 image
= gtk_image_new_from_stock(stockid
, icon_size_map(icon_size
));
7266 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
7267 gtk_button_set_image(GTK_BUTTON(button
), image
);
7271 clipb_primary_cb(GtkClipboard
*primary
, GdkEvent
*event
, gpointer notused
)
7274 GdkAtom atom
= gdk_atom_intern("CUT_BUFFER0", FALSE
);
7277 if (xterm_workaround
== 0)
7281 * xterm doesn't play nice with clipboards because it clears the
7282 * primary when clicked. We rely on primary being set to properly
7283 * handle middle mouse button clicks (paste). So when someone clears
7284 * primary copy whatever is in CUT_BUFFER0 into primary to simualte
7285 * other application behavior (as in DON'T clear primary).
7288 p
= gtk_clipboard_wait_for_text(primary
);
7290 if (gdk_property_get(gdk_get_default_root_window(),
7292 gdk_atom_intern("STRING", FALSE
),
7294 1024 * 1024 /* picked out of my butt */,
7302 /* yes sir, we need to NUL the string */
7304 gtk_clipboard_set_text(primary
, p
, -1);
7318 char file
[PATH_MAX
];
7321 vbox
= gtk_vbox_new(FALSE
, 0);
7322 gtk_box_set_spacing(GTK_BOX(vbox
), 0);
7323 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
7324 #if !GTK_CHECK_VERSION(3, 0, 0)
7325 /* XXX seems to be needed with gtk+2 */
7326 gtk_notebook_set_tab_hborder(notebook
, 0);
7327 gtk_notebook_set_tab_vborder(notebook
, 0);
7329 gtk_notebook_set_scrollable(notebook
, TRUE
);
7330 gtk_notebook_set_show_border(notebook
, FALSE
);
7331 gtk_widget_set_can_focus(GTK_WIDGET(notebook
), FALSE
);
7333 abtn
= gtk_button_new();
7334 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
7335 gtk_widget_set_size_request(arrow
, -1, -1);
7336 gtk_container_add(GTK_CONTAINER(abtn
), arrow
);
7337 gtk_widget_set_size_request(abtn
, -1, 20);
7339 #if GTK_CHECK_VERSION(2, 20, 0)
7340 gtk_notebook_set_action_widget(notebook
, abtn
, GTK_PACK_END
);
7342 gtk_widget_set_size_request(GTK_WIDGET(notebook
), -1, -1);
7344 /* compact tab bar */
7345 tab_bar
= gtk_hbox_new(TRUE
, 0);
7347 gtk_box_pack_start(GTK_BOX(vbox
), tab_bar
, FALSE
, FALSE
, 0);
7348 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
7349 gtk_widget_set_size_request(vbox
, -1, -1);
7351 g_object_connect(G_OBJECT(notebook
),
7352 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb
), NULL
,
7354 g_object_connect(G_OBJECT(notebook
),
7355 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb
),
7356 NULL
, (char *)NULL
);
7357 g_signal_connect(G_OBJECT(abtn
), "button_press_event",
7358 G_CALLBACK(arrow_cb
), NULL
);
7360 main_window
= create_window("xxxterm");
7361 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
7362 g_signal_connect(G_OBJECT(main_window
), "delete_event",
7363 G_CALLBACK(gtk_main_quit
), NULL
);
7366 for (i
= 0; i
< LENGTH(icons
); i
++) {
7367 snprintf(file
, sizeof file
, "%s" PS
"%s", resource_dir
, icons
[i
]);
7368 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
7369 l
= g_list_append(l
, pb
);
7371 gtk_window_set_default_icon_list(l
);
7373 /* clipboard work around */
7374 if (xterm_workaround
)
7376 G_OBJECT(gtk_clipboard_get(GDK_SELECTION_PRIMARY
)),
7377 "owner-change", G_CALLBACK(clipb_primary_cb
), NULL
);
7379 gtk_widget_show_all(abtn
);
7380 gtk_widget_show_all(main_window
);
7381 notebook_tab_set_visibility();
7384 #ifndef XT_SOCKET_DISABLE
7386 send_cmd_to_socket(char *cmd
)
7389 struct sockaddr_un sa
;
7391 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7392 warnx("%s: socket", __func__
);
7396 sa
.sun_family
= AF_UNIX
;
7397 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
7398 work_dir
, XT_SOCKET_FILE
);
7401 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7402 warnx("%s: connect", __func__
);
7406 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
7407 warnx("%s: send", __func__
);
7418 socket_watcher(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
7421 char str
[XT_MAX_URL_LENGTH
];
7422 socklen_t t
= sizeof(struct sockaddr_un
);
7423 struct sockaddr_un sa
;
7428 gint fd
= g_io_channel_unix_get_fd(source
);
7430 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
7435 if (getpeereid(s
, &uid
, &gid
) == -1) {
7439 if (uid
!= getuid() || gid
!= getgid()) {
7440 warnx("unauthorized user");
7446 warnx("not a valid user");
7450 n
= recv(s
, str
, sizeof(str
), 0);
7454 tt
= TAILQ_LAST(&tabs
, tab_list
);
7455 cmd_execute(tt
, str
);
7463 struct sockaddr_un sa
;
7465 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7466 warn("is_running: socket");
7470 sa
.sun_family
= AF_UNIX
;
7471 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
7472 work_dir
, XT_SOCKET_FILE
);
7475 /* connect to see if there is a listener */
7476 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
7477 rv
= 0; /* not running */
7479 rv
= 1; /* already running */
7490 struct sockaddr_un sa
;
7492 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7493 warn("build_socket: socket");
7497 sa
.sun_family
= AF_UNIX
;
7498 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
7499 work_dir
, XT_SOCKET_FILE
);
7502 /* connect to see if there is a listener */
7503 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7504 /* no listener so we will */
7505 unlink(sa
.sun_path
);
7507 if (bind(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7508 warn("build_socket: bind");
7512 if (listen(s
, 1) == -1) {
7513 warn("build_socket: listen");
7531 if (stat(dir
, &sb
)) {
7532 #if defined __MINGW32__
7533 printf("making: %s\n", dir
);
7534 if (mkdir(dir
) == -1)
7536 if (mkdir(dir
, S_IRWXU
) == -1)
7538 err(1, "mkdir %s", dir
);
7540 err(1, "stat %s", dir
);
7542 if (S_ISDIR(sb
.st_mode
) == 0)
7543 errx(1, "%s not a dir", dir
);
7544 #if !defined __MINGW32__
7545 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
7546 warnx("fixing invalid permissions on %s", dir
);
7547 if (chmod(dir
, S_IRWXU
) == -1)
7548 err(1, "chmod %s", dir
);
7557 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
7561 GStaticRecMutex my_gdk_mtx
= G_STATIC_REC_MUTEX_INIT
;
7562 volatile int mtx_depth
;
7566 * The linux flash plugin violates the gdk locking mechanism.
7567 * Work around the issue by using a recursive mutex with some match applied
7568 * to see if we hit a buggy condition.
7570 * The following code is painful so just don't read it. It really doesn't
7571 * make much sense but seems to work.
7576 g_static_rec_mutex_lock(&my_gdk_mtx
);
7579 if (mtx_depth
<= 0) {
7580 /* should not happen */
7581 show_oops(NULL
, "negative mutex locking bug, trying to "
7583 fprintf(stderr
, "negative mutex locking bug, trying to "
7585 g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
7586 g_static_rec_mutex_lock(&my_gdk_mtx
);
7591 if (mtx_depth
!= 1) {
7592 /* decrease mutext depth to 1 */
7594 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7596 } while (mtx_depth
> 1);
7605 /* if mutex depth isn't 1 then something went bad */
7606 if (mtx_depth
!= 1) {
7607 x
= g_static_rec_mutex_unlock_full(&my_gdk_mtx
);
7609 /* should not happen */
7610 show_oops(NULL
, "mutex unlocking bug, trying to "
7612 fprintf(stderr
, "mutex unlocking bug, trying to "
7616 if (mtx_complain
== 0) {
7617 show_oops(NULL
, "buggy mutex implementation detected, "
7618 "work around implemented");
7619 fprintf(stderr
, "buggy mutex implementation detected, "
7620 "work around implemented");
7627 g_static_rec_mutex_unlock(&my_gdk_mtx
);
7633 startpage_add("<b>Welcome to xxxterm %s!</b><p>", version
);
7634 startpage_add("Beware that this is the final version that will use the"
7635 " xxxterm name.<br>Moving forward the browser will be called "
7636 "xombrero.<p>", version
);
7637 startpage_add("Details will soon appear on the "
7638 "<a href=https://opensource.conformal.com/wiki/xombrero>xombrero "
7639 "wiki page</a><p>");
7640 startpage_add("Unfortunately scroogle has shut it's doors and due to "
7641 "that one has to edit search_string in ~/.xxxterm.conf.<br>"
7642 "There are various examples in the configuration file.<br>"
7643 "The authors of xxxterm are not in a position to suggest a search "
7648 main(int argc
, char **argv
)
7651 int c
, optn
= 0, opte
= 0, focus
= 1;
7652 char conf
[PATH_MAX
] = { '\0' };
7653 char file
[PATH_MAX
];
7654 char sodversion
[32];
7655 char *env_proxy
= NULL
;
7659 start_argv
= (char * const *)argv
;
7663 g_thread_init(NULL
);
7664 gdk_threads_set_lock_functions(mtx_lock
, mtx_unlock
);
7666 gdk_threads_enter();
7668 gcry_control (GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
7670 gtk_init(&argc
, &argv
);
7672 gnutls_global_init();
7674 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
7679 RB_INIT(&downloads
);
7682 TAILQ_INIT(&sessions
);
7685 TAILQ_INIT(&aliases
);
7691 TAILQ_INIT(&ua_list
);
7693 #ifndef XT_RESOURCE_LIMITS_DISABLE
7697 GIOChannel
*channel
;
7699 /* fiddle with ulimits */
7700 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7703 /* just use them all */
7704 rlp
.rlim_cur
= rlp
.rlim_max
;
7705 if (setrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7707 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
7709 else if (rlp
.rlim_cur
< 1024)
7710 startpage_add("%s requires at least 1024 "
7711 "(2048 recommended) file " "descriptors, "
7712 "currently it has up to %d available",
7713 __progname
, rlp
.rlim_cur
);
7717 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
7726 #ifdef XXXTERM_BUILDSTR
7727 errx(0 , "Version: %s Build: %s",
7728 version
, XXXTERM_BUILDSTR
);
7730 errx(0 , "Version: %s", version
);
7734 strlcpy(conf
, optarg
, sizeof(conf
));
7737 strlcpy(named_session
, optarg
, sizeof(named_session
));
7758 xtp_generate_keys();
7760 /* XXX this hammer is way too big but treat all signaling the same */
7761 #ifndef XT_SIGNALS_DISABLE
7763 struct sigaction sact
;
7765 bzero(&sact
, sizeof(sact
));
7766 sigemptyset(&sact
.sa_mask
);
7767 sact
.sa_handler
= sigchild
;
7768 sact
.sa_flags
= SA_NOCLDSTOP
;
7769 sigaction(SIGCHLD
, &sact
, NULL
);
7772 /* set download dir */
7773 pwd
= getpwuid(getuid());
7775 errx(1, "invalid user %d", getuid());
7776 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
7778 /* compile buffer command regexes */
7781 /* set default string settings */
7782 home
= g_strdup("https://www.cyphertite.com");
7783 search_string
= g_strdup("https://ssl.scroogle.org/cgi-bin/nbbwssl.cgi?Gw=%s");
7784 resource_dir
= g_strdup("/usr/local/share/xxxterm/");
7785 strlcpy(runtime_settings
, "runtime", sizeof runtime_settings
);
7786 cmd_font_name
= g_strdup("monospace normal 9");
7787 oops_font_name
= g_strdup("monospace normal 9");
7788 statusbar_font_name
= g_strdup("monospace normal 9");
7789 tabbar_font_name
= g_strdup("monospace normal 9");
7790 statusbar_elems
= g_strdup("BP");
7791 spell_check_languages
= g_strdup("en_US");
7792 encoding
= g_strdup("UTF-8");
7794 /* read config file */
7795 if (strlen(conf
) == 0)
7796 snprintf(conf
, sizeof conf
, "%s" PS
".%s",
7797 pwd
->pw_dir
, XT_CONF_FILE
);
7798 config_parse(conf
, 0);
7801 cmd_font
= pango_font_description_from_string(cmd_font_name
);
7802 oops_font
= pango_font_description_from_string(oops_font_name
);
7803 statusbar_font
= pango_font_description_from_string(statusbar_font_name
);
7804 tabbar_font
= pango_font_description_from_string(tabbar_font_name
);
7806 /* working directory */
7807 if (strlen(work_dir
) == 0)
7808 snprintf(work_dir
, sizeof work_dir
, "%s" PS
"%s",
7809 pwd
->pw_dir
, XT_DIR
);
7812 /* icon cache dir */
7813 snprintf(cache_dir
, sizeof cache_dir
, "%s" PS
"%s", work_dir
, XT_CACHE_DIR
);
7817 snprintf(certs_dir
, sizeof certs_dir
, "%s" PS
"%s", work_dir
, XT_CERT_DIR
);
7821 snprintf(sessions_dir
, sizeof sessions_dir
, "%s" PS
"%s",
7822 work_dir
, XT_SESSIONS_DIR
);
7823 xxx_dir(sessions_dir
);
7826 snprintf(js_dir
, sizeof js_dir
, "%s" PS
"%s", work_dir
, XT_JS_DIR
);
7830 snprintf(temp_dir
, sizeof temp_dir
, "%s" PS
"%s", work_dir
, XT_TEMP_DIR
);
7833 /* runtime settings that can override config file */
7834 if (runtime_settings
[0] != '\0')
7835 config_parse(runtime_settings
, 1);
7838 if (!strcmp(download_dir
, pwd
->pw_dir
))
7839 strlcat(download_dir
, PS
"downloads", sizeof download_dir
);
7840 xxx_dir(download_dir
);
7842 /* first start file */
7843 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_SOD_FILE
);
7844 if (stat(file
, &sb
)) {
7845 warnx("start of day file doesn't exist, creating it");
7846 if ((f
= fopen(file
, "w")) == NULL
)
7847 err(1, "startofday");
7848 if (fputs(version
, f
) == EOF
)
7855 if ((f
= fopen(file
, "r+")) == NULL
)
7856 err(1, "startofday");
7857 if (fgets(sodversion
, sizeof sodversion
, f
) == NULL
)
7859 sodversion
[strcspn(sodversion
, "\n")] = '\0';
7860 if (strcmp(version
, sodversion
)) {
7862 if (fputs(version
, f
) == EOF
)
7865 /* upgrade, say something smart */
7871 /* favorites file */
7872 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_FAVS_FILE
);
7873 if (stat(file
, &sb
)) {
7874 warnx("favorites file doesn't exist, creating it");
7875 if ((f
= fopen(file
, "w")) == NULL
)
7876 err(1, "favorites");
7880 /* quickmarks file */
7881 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, XT_QMARKS_FILE
);
7882 if (stat(file
, &sb
)) {
7883 warnx("quickmarks file doesn't exist, creating it");
7884 if ((f
= fopen(file
, "w")) == NULL
)
7885 err(1, "quickmarks");
7889 /* search history */
7890 if (history_autosave
) {
7891 snprintf(search_file
, sizeof search_file
, "%s" PS
"%s",
7892 work_dir
, XT_SEARCH_FILE
);
7893 if (stat(search_file
, &sb
)) {
7894 warnx("search history file doesn't exist, creating it");
7895 if ((f
= fopen(search_file
, "w")) == NULL
)
7896 err(1, "search_history");
7899 history_read(&shl
, search_file
, &search_history_count
);
7902 /* command history */
7903 if (history_autosave
) {
7904 snprintf(command_file
, sizeof command_file
, "%s" PS
"%s",
7905 work_dir
, XT_COMMAND_FILE
);
7906 if (stat(command_file
, &sb
)) {
7907 warnx("command history file doesn't exist, creating it");
7908 if ((f
= fopen(command_file
, "w")) == NULL
)
7909 err(1, "command_history");
7912 history_read(&chl
, command_file
, &cmd_history_count
);
7916 session
= webkit_get_default_session();
7921 if (stat(ssl_ca_file
, &sb
)) {
7922 warnx("no CA file: %s", ssl_ca_file
);
7923 g_free(ssl_ca_file
);
7926 g_object_set(session
,
7927 SOUP_SESSION_SSL_CA_FILE
, ssl_ca_file
,
7928 SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
7932 /* guess_search regex */
7933 if (url_regex
== NULL
)
7934 url_regex
= g_strdup(XT_URL_REGEX
);
7936 if (regcomp(&url_re
, url_regex
, REG_EXTENDED
| REG_NOSUB
))
7937 startpage_add("invalid url regex %s", url_regex
);
7940 env_proxy
= getenv("http_proxy");
7942 setup_proxy(env_proxy
);
7944 env_proxy
= getenv("HTTP_PROXY");
7946 setup_proxy(env_proxy
);
7948 setup_proxy(http_proxy
);
7950 #ifndef XT_SOCKET_DISABLE
7952 send_cmd_to_socket(argv
[0]);
7956 opte
= opte
; /* shut mingw up */
7958 /* set some connection parameters */
7959 g_object_set(session
, "max-conns", max_connections
, (char *)NULL
);
7960 g_object_set(session
, "max-conns-per-host", max_host_connections
,
7963 g_signal_connect(session
, "request-queued", G_CALLBACK(session_rq_cb
),
7966 #ifndef XT_SOCKET_DISABLE
7967 /* see if there is already an xxxterm running */
7968 if (single_instance
&& is_running()) {
7970 warnx("already running");
7975 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
7976 send_cmd_to_socket(cmd
);
7986 optn
= optn
; /* shut mingw up */
7991 if (enable_strict_transport
)
7992 strict_transport_init();
7994 /* uri completion */
7995 completion_model
= gtk_list_store_new(1, G_TYPE_STRING
);
7998 buffers_store
= gtk_list_store_new
7999 (NUM_COLS
, G_TYPE_UINT
, GDK_TYPE_PIXBUF
, G_TYPE_STRING
);
8005 notebook_tab_set_visibility();
8007 if (save_global_history
)
8008 restore_global_history();
8010 /* restore session list */
8011 restore_sessions_list();
8013 if (!strcmp(named_session
, XT_SAVED_TABS_FILE
))
8014 restore_saved_tabs();
8016 a
.s
= named_session
;
8017 a
.i
= XT_SES_DONOTHING
;
8018 open_tabs(NULL
, &a
);
8021 /* see if we have an exception */
8022 if (!TAILQ_EMPTY(&spl
)) {
8023 create_new_tab("about:startpage", NULL
, focus
, -1);
8028 create_new_tab(argv
[0], NULL
, focus
, -1);
8035 if (TAILQ_EMPTY(&tabs
))
8036 create_new_tab(home
, NULL
, 1, -1);
8037 #ifndef XT_SOCKET_DISABLE
8039 if ((s
= build_socket()) != -1) {
8040 channel
= g_io_channel_unix_new(s
);
8041 g_io_add_watch(channel
, G_IO_IN
, socket_watcher
, NULL
);
8048 gdk_threads_leave();
8049 g_static_rec_mutex_unlock_full(&my_gdk_mtx
); /* just in case */
8052 gnutls_global_deinit();