lets build javascript.h in the obj/ dir if present...
[xombrero.git] / xxxterm.c
blobe4dc4c433c1cd28010eca8aa3357da5ed5dd4a6c
1 /*
2 * Copyright (c) 2010, 2011 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2011 Stevan Andjelkovic <stevan@student.chalmers.se>
4 * Copyright (c) 2010, 2011 Edd Barrett <vext01@gmail.com>
5 * Copyright (c) 2011 Todd T. Fries <todd@fries.net>
6 * Copyright (c) 2011 Raphael Graf <r@undefined.ch>
7 * Copyright (c) 2011 Michal Mazurek <akfaew@jasminek.net>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include "xxxterm.h"
24 char *version = XXXTERM_VERSION;
26 #ifdef XT_DEBUG
27 u_int32_t swm_debug = 0
28 | XT_D_MOVE
29 | XT_D_KEY
30 | XT_D_TAB
31 | XT_D_URL
32 | XT_D_CMD
33 | XT_D_NAV
34 | XT_D_DOWNLOAD
35 | XT_D_CONFIG
36 | XT_D_JS
37 | XT_D_FAVORITE
38 | XT_D_PRINTING
39 | XT_D_COOKIE
40 | XT_D_KEYBINDING
41 | XT_D_CLIP
42 | XT_D_BUFFERCMD
43 | XT_D_INSPECTOR
44 | XT_D_VISITED
45 | XT_D_HISTORY
47 #endif
49 #ifdef USE_THREADS
50 GCRY_THREAD_OPTION_PTHREAD_IMPL;
51 #endif
53 char *icons[] = {
54 "xxxtermicon16.png",
55 "xxxtermicon32.png",
56 "xxxtermicon48.png",
57 "xxxtermicon64.png",
58 "xxxtermicon128.png"
61 struct session {
62 TAILQ_ENTRY(session) entry;
63 const gchar *name;
65 TAILQ_HEAD(session_list, session);
67 struct undo {
68 TAILQ_ENTRY(undo) entry;
69 gchar *uri;
70 GList *history;
71 int back; /* Keeps track of how many back
72 * history items there are. */
74 TAILQ_HEAD(undo_tailq, undo);
76 struct command_entry {
77 char *line;
78 TAILQ_ENTRY(command_entry) entry;
80 TAILQ_HEAD(command_list, command_entry);
82 /* defines */
83 #define XT_DIR (".xxxterm")
84 #define XT_CACHE_DIR ("cache")
85 #define XT_CERT_DIR ("certs/")
86 #define XT_JS_DIR ("js/")
87 #define XT_SESSIONS_DIR ("sessions/")
88 #define XT_TEMP_DIR ("tmp")
89 #define XT_CONF_FILE ("xxxterm.conf")
90 #define XT_QMARKS_FILE ("quickmarks")
91 #define XT_SAVED_TABS_FILE ("main_session")
92 #define XT_RESTART_TABS_FILE ("restart_tabs")
93 #define XT_SOCKET_FILE ("socket")
94 #define XT_SAVE_SESSION_ID ("SESSION_NAME=")
95 #define XT_SEARCH_FILE ("search_history")
96 #define XT_COMMAND_FILE ("command_history")
97 #define XT_DLMAN_REFRESH "10"
98 #define XT_MAX_URL_LENGTH (4096) /* 1 page is atomic, don't make bigger */
99 #define XT_MAX_UNDO_CLOSE_TAB (32)
100 #define XT_RESERVED_CHARS "$&+,/:;=?@ \"<>#%%{}|^~[]`"
101 #define XT_PRINT_EXTRA_MARGIN 10
102 #define XT_URL_REGEX ("^[[:blank:]]*[^[:blank:]]*([[:alnum:]-]+\\.)+[[:alnum:]-][^[:blank:]]*[[:blank:]]*$")
103 #define XT_INVALID_MARK (-1) /* XXX this is a double, maybe use something else, like a nan */
105 /* colors */
106 #define XT_COLOR_RED "#cc0000"
107 #define XT_COLOR_YELLOW "#ffff66"
108 #define XT_COLOR_BLUE "lightblue"
109 #define XT_COLOR_GREEN "#99ff66"
110 #define XT_COLOR_WHITE "white"
111 #define XT_COLOR_BLACK "black"
113 #define XT_COLOR_CT_BACKGROUND "#000000"
114 #define XT_COLOR_CT_INACTIVE "#dddddd"
115 #define XT_COLOR_CT_ACTIVE "#bbbb00"
116 #define XT_COLOR_CT_SEPARATOR "#555555"
118 #define XT_COLOR_SB_SEPARATOR "#555555"
120 #define XT_PROTO_DELIM "://"
122 /* actions */
123 #define XT_MOVE_INVALID (0)
124 #define XT_MOVE_DOWN (1)
125 #define XT_MOVE_UP (2)
126 #define XT_MOVE_BOTTOM (3)
127 #define XT_MOVE_TOP (4)
128 #define XT_MOVE_PAGEDOWN (5)
129 #define XT_MOVE_PAGEUP (6)
130 #define XT_MOVE_HALFDOWN (7)
131 #define XT_MOVE_HALFUP (8)
132 #define XT_MOVE_LEFT (9)
133 #define XT_MOVE_FARLEFT (10)
134 #define XT_MOVE_RIGHT (11)
135 #define XT_MOVE_FARRIGHT (12)
136 #define XT_MOVE_PERCENT (13)
137 #define XT_MOVE_CENTER (14)
139 #define XT_QMARK_SET (0)
140 #define XT_QMARK_OPEN (1)
141 #define XT_QMARK_TAB (2)
143 #define XT_MARK_SET (0)
144 #define XT_MARK_GOTO (1)
146 #define XT_GO_UP_ROOT (999)
148 #define XT_TAB_LAST (-4)
149 #define XT_TAB_FIRST (-3)
150 #define XT_TAB_PREV (-2)
151 #define XT_TAB_NEXT (-1)
152 #define XT_TAB_INVALID (0)
153 #define XT_TAB_NEW (1)
154 #define XT_TAB_DELETE (2)
155 #define XT_TAB_DELQUIT (3)
156 #define XT_TAB_OPEN (4)
157 #define XT_TAB_UNDO_CLOSE (5)
158 #define XT_TAB_SHOW (6)
159 #define XT_TAB_HIDE (7)
160 #define XT_TAB_NEXTSTYLE (8)
161 #define XT_TAB_LOAD_IMAGES (9)
163 #define XT_NAV_INVALID (0)
164 #define XT_NAV_BACK (1)
165 #define XT_NAV_FORWARD (2)
166 #define XT_NAV_RELOAD (3)
167 #define XT_NAV_STOP (4)
169 #define XT_FOCUS_INVALID (0)
170 #define XT_FOCUS_URI (1)
171 #define XT_FOCUS_SEARCH (2)
173 #define XT_SEARCH_INVALID (0)
174 #define XT_SEARCH_NEXT (1)
175 #define XT_SEARCH_PREV (2)
177 #define XT_STYLE_CURRENT_TAB (0)
178 #define XT_STYLE_GLOBAL (1)
180 #define XT_PASTE_CURRENT_TAB (0)
181 #define XT_PASTE_NEW_TAB (1)
183 #define XT_ZOOM_IN (-1)
184 #define XT_ZOOM_OUT (-2)
185 #define XT_ZOOM_NORMAL (100)
187 #define XT_URL_SHOW (1)
188 #define XT_URL_HIDE (2)
190 #define XT_CMD_OPEN (0)
191 #define XT_CMD_OPEN_CURRENT (1)
192 #define XT_CMD_TABNEW (2)
193 #define XT_CMD_TABNEW_CURRENT (3)
195 #define XT_STATUS_NOTHING (0)
196 #define XT_STATUS_LINK (1)
197 #define XT_STATUS_URI (2)
198 #define XT_STATUS_LOADING (3)
200 #define XT_SES_DONOTHING (0)
201 #define XT_SES_CLOSETABS (1)
203 #define XT_PREFIX (1<<0)
204 #define XT_USERARG (1<<1)
205 #define XT_URLARG (1<<2)
206 #define XT_INTARG (1<<3)
207 #define XT_SESSARG (1<<4)
208 #define XT_SETARG (1<<5)
210 #define XT_HINT_NEWTAB (1<<0)
212 #define XT_BUFCMD_SZ (8)
214 #define XT_EJS_SHOW (1<<0)
216 GtkWidget * create_button(char *, char *, int);
218 void recalc_tabs(void);
219 void recolor_compact_tabs(void);
220 void set_current_tab(int page_num);
221 gboolean update_statusbar_position(GtkAdjustment*, gpointer);
222 void marks_clear(struct tab *t);
224 /* globals */
225 extern char *__progname;
226 char **start_argv;
227 struct passwd *pwd;
228 GtkWidget *main_window;
229 GtkNotebook *notebook;
230 GtkWidget *tab_bar;
231 GtkWidget *arrow, *abtn;
232 struct tab_list tabs;
233 struct history_list hl;
234 int hl_purge_count = 0;
235 struct session_list sessions;
236 struct domain_list c_wl;
237 struct domain_list js_wl;
238 struct domain_list pl_wl;
239 struct undo_tailq undos;
240 struct keybinding_list kbl;
241 struct sp_list spl;
242 struct user_agent_list ua_list;
243 int user_agent_count = 0;
244 struct command_list chl;
245 struct command_list shl;
246 struct command_entry *history_at;
247 struct command_entry *search_at;
248 int undo_count;
249 int cmd_history_count = 0;
250 int search_history_count = 0;
251 char *global_search;
252 long long unsigned int blocked_cookies = 0;
253 char named_session[PATH_MAX];
254 GtkListStore *completion_model;
255 GtkListStore *buffers_store;
257 char *qmarks[XT_NOMARKS];
258 int btn_down; /* M1 down in any wv */
259 regex_t url_re; /* guess_search regex */
261 /* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
262 int next_download_id = 1;
264 void xxx_dir(char *);
265 int icon_size_map(int);
267 void
268 history_delete(struct command_list *l, int *counter)
270 struct command_entry *c;
272 if (l == NULL || counter == NULL)
273 return;
275 c = TAILQ_LAST(l, command_list);
276 if (c == NULL)
277 return;
279 TAILQ_REMOVE(l, c, entry);
280 *counter -= 1;
281 g_free(c->line);
282 g_free(c);
285 void
286 history_add(struct command_list *list, char *file, char *l, int *counter)
288 struct command_entry *c;
289 FILE *f;
291 if (list == NULL || l == NULL || counter == NULL)
292 return;
294 /* don't add the same line */
295 c = TAILQ_FIRST(list);
296 if (c)
297 if (!strcmp(c->line + 1 /* skip space */, l))
298 return;
300 c = g_malloc0(sizeof *c);
301 c->line = g_strdup_printf(" %s", l);
303 *counter += 1;
304 TAILQ_INSERT_HEAD(list, c, entry);
306 if (*counter > 1000)
307 history_delete(list, counter);
309 if (history_autosave && file) {
310 f = fopen(file, "w");
311 if (f == NULL) {
312 show_oops(NULL, "couldn't write history %s", file);
313 return;
316 TAILQ_FOREACH_REVERSE(c, list, command_list, entry) {
317 c->line[0] = ' ';
318 fprintf(f, "%s\n", c->line);
321 fclose(f);
326 history_read(struct command_list *list, char *file, int *counter)
328 FILE *f;
329 char *s, line[65536];
331 if (list == NULL || file == NULL)
332 return (1);
334 f = fopen(file, "r");
335 if (f == NULL) {
336 startpage_add("couldn't open history file %s", file);
337 return (1);
340 for (;;) {
341 s = fgets(line, sizeof line, f);
342 if (s == NULL || feof(f) || ferror(f))
343 break;
344 if ((s = strchr(line, '\n')) == NULL) {
345 startpage_add("invalid history file %s", file);
346 fclose(f);
347 return (1);
349 *s = '\0';
351 history_add(list, NULL, line + 1, counter);
354 fclose(f);
356 return (0);
359 /* marks and quickmarks array storage.
360 * first a-z, then A-Z, then 0-9 */
361 char
362 indextomark(int i)
364 if (i < 0)
365 return (0);
367 if (i >= 0 && i <= 'z' - 'a')
368 return 'a' + i;
370 i -= 'z' - 'a' + 1;
371 if (i >= 0 && i <= 'Z' - 'A')
372 return 'A' + i;
374 i -= 'Z' - 'A' + 1;
375 if (i >= 10)
376 return (0);
378 return i + '0';
382 marktoindex(char m)
384 int ret = 0;
386 if (m >= 'a' && m <= 'z')
387 return ret + m - 'a';
389 ret += 'z' - 'a' + 1;
390 if (m >= 'A' && m <= 'Z')
391 return ret + m - 'A';
393 ret += 'Z' - 'A' + 1;
394 if (m >= '0' && m <= '9')
395 return ret + m - '0';
397 return (-1);
401 void
402 sigchild(int sig)
404 int saved_errno, status;
405 pid_t pid;
407 saved_errno = errno;
409 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
410 if (pid == -1) {
411 if (errno == EINTR)
412 continue;
413 if (errno != ECHILD) {
415 clog_warn("sigchild: waitpid:");
418 break;
421 if (WIFEXITED(status)) {
422 if (WEXITSTATUS(status) != 0) {
424 clog_warnx("sigchild: child exit status: %d",
425 WEXITSTATUS(status));
428 } else {
430 clog_warnx("sigchild: child is terminated abnormally");
435 errno = saved_errno;
439 is_g_object_setting(GObject *o, char *str)
441 guint n_props = 0, i;
442 GParamSpec **proplist;
444 if (! G_IS_OBJECT(o))
445 return (0);
447 proplist = g_object_class_list_properties(G_OBJECT_GET_CLASS(o),
448 &n_props);
450 for (i=0; i < n_props; i++) {
451 if (! strcmp(proplist[i]->name, str))
452 return (1);
454 return (0);
457 struct tab *
458 get_current_tab(void)
460 struct tab *t;
462 TAILQ_FOREACH(t, &tabs, entry) {
463 if (t->tab_id == gtk_notebook_get_current_page(notebook))
464 return (t);
467 warnx("%s: no current tab", __func__);
469 return (NULL);
472 void
473 set_status(struct tab *t, gchar *s, int status)
475 gchar *type = NULL;
477 if (s == NULL)
478 return;
480 switch (status) {
481 case XT_STATUS_LOADING:
482 type = g_strdup_printf("Loading: %s", s);
483 s = type;
484 break;
485 case XT_STATUS_LINK:
486 type = g_strdup_printf("Link: %s", s);
487 if (!t->status)
488 t->status = g_strdup(gtk_entry_get_text(
489 GTK_ENTRY(t->sbe.statusbar)));
490 s = type;
491 break;
492 case XT_STATUS_URI:
493 type = g_strdup_printf("%s", s);
494 if (!t->status) {
495 t->status = g_strdup(type);
497 s = type;
498 if (!t->status)
499 t->status = g_strdup(s);
500 break;
501 case XT_STATUS_NOTHING:
502 /* FALL THROUGH */
503 default:
504 break;
506 gtk_entry_set_text(GTK_ENTRY(t->sbe.statusbar), s);
507 if (type)
508 g_free(type);
511 void
512 hide_cmd(struct tab *t)
514 DNPRINTF(XT_D_CMD, "%s: tab %d\n", __func__, t->tab_id);
516 history_at = NULL; /* just in case */
517 search_at = NULL; /* just in case */
518 gtk_widget_hide(t->cmd);
521 void
522 show_cmd(struct tab *t)
524 DNPRINTF(XT_D_CMD, "%s: tab %d\n", __func__, t->tab_id);
526 history_at = NULL;
527 search_at = NULL;
528 gtk_widget_hide(t->oops);
529 gtk_widget_show(t->cmd);
532 void
533 hide_buffers(struct tab *t)
535 gtk_widget_hide(t->buffers);
536 gtk_list_store_clear(buffers_store);
539 enum {
540 COL_ID = 0,
541 COL_FAVICON,
542 COL_TITLE,
543 NUM_COLS
547 sort_tabs_by_page_num(struct tab ***stabs)
549 int num_tabs = 0;
550 struct tab *t;
552 num_tabs = gtk_notebook_get_n_pages(notebook);
554 *stabs = g_malloc0(num_tabs * sizeof(struct tab *));
556 TAILQ_FOREACH(t, &tabs, entry)
557 (*stabs)[gtk_notebook_page_num(notebook, t->vbox)] = t;
559 return (num_tabs);
562 void
563 buffers_make_list(void)
565 int i, num_tabs;
566 const gchar *title = NULL;
567 GtkTreeIter iter;
568 struct tab **stabs = NULL;
570 num_tabs = sort_tabs_by_page_num(&stabs);
572 for (i = 0; i < num_tabs; i++)
573 if (stabs[i]) {
574 gtk_list_store_append(buffers_store, &iter);
575 title = get_title(stabs[i], FALSE);
576 gtk_list_store_set(buffers_store, &iter,
577 COL_ID, i + 1, /* Enumerate the tabs starting from 1
578 * rather than 0. */
579 COL_FAVICON, gtk_image_get_pixbuf
580 (GTK_IMAGE(stabs[i]->tab_elems.favicon)),
581 COL_TITLE, title,
582 -1);
585 g_free(stabs);
588 void
589 show_buffers(struct tab *t)
591 if (gtk_widget_get_visible(GTK_WIDGET(t->buffers)))
592 return;
594 buffers_make_list();
595 gtk_widget_show(t->buffers);
596 gtk_widget_grab_focus(GTK_WIDGET(t->buffers));
599 void
600 toggle_buffers(struct tab *t)
602 if (gtk_widget_get_visible(t->buffers))
603 hide_buffers(t);
604 else
605 show_buffers(t);
609 buffers(struct tab *t, struct karg *args)
611 show_buffers(t);
613 return (0);
616 void
617 hide_oops(struct tab *t)
619 gtk_widget_hide(t->oops);
622 void
623 show_oops(struct tab *at, const char *fmt, ...)
625 va_list ap;
626 char *msg = NULL;
627 struct tab *t = NULL;
629 if (fmt == NULL)
630 return;
632 if (at == NULL) {
633 if ((t = get_current_tab()) == NULL)
634 return;
635 } else
636 t = at;
638 va_start(ap, fmt);
639 if (vasprintf(&msg, fmt, ap) == -1)
640 errx(1, "show_oops failed");
641 va_end(ap);
643 gtk_entry_set_text(GTK_ENTRY(t->oops), msg);
644 gtk_widget_hide(t->cmd);
645 gtk_widget_show(t->oops);
647 if (msg)
648 free(msg);
651 char work_dir[PATH_MAX];
652 char certs_dir[PATH_MAX];
653 char js_dir[PATH_MAX];
654 char cache_dir[PATH_MAX];
655 char sessions_dir[PATH_MAX];
656 char temp_dir[PATH_MAX];
657 char cookie_file[PATH_MAX];
658 SoupSession *session;
659 SoupCookieJar *s_cookiejar;
660 SoupCookieJar *p_cookiejar;
661 char rc_fname[PATH_MAX];
663 struct mime_type_list mtl;
664 struct alias_list aliases;
666 /* protos */
667 struct tab *create_new_tab(char *, struct undo *, int, int);
668 void delete_tab(struct tab *);
669 void setzoom_webkit(struct tab *, int);
670 int download_rb_cmp(struct download *, struct download *);
671 gboolean cmd_execute(struct tab *t, char *str);
674 history_rb_cmp(struct history *h1, struct history *h2)
676 return (strcmp(h1->uri, h2->uri));
678 RB_GENERATE(history_list, history, entry, history_rb_cmp);
681 domain_rb_cmp(struct domain *d1, struct domain *d2)
683 return (strcmp(d1->d, d2->d));
685 RB_GENERATE(domain_list, domain, entry, domain_rb_cmp);
688 download_rb_cmp(struct download *e1, struct download *e2)
690 return (e1->id < e2->id ? -1 : e1->id > e2->id);
692 RB_GENERATE(download_list, download, entry, download_rb_cmp);
694 struct valid_url_types {
695 char *type;
696 } vut[] = {
697 { "http://" },
698 { "https://" },
699 { "ftp://" },
700 { "file://" },
701 { XT_XTP_STR },
705 valid_url_type(char *url)
707 int i;
709 for (i = 0; i < LENGTH(vut); i++)
710 if (!strncasecmp(vut[i].type, url, strlen(vut[i].type)))
711 return (0);
713 return (1);
716 char *
717 match_alias(char *url_in)
719 struct alias *a;
720 char *arg;
721 char *url_out = NULL, *search, *enc_arg;
723 search = g_strdup(url_in);
724 arg = search;
725 if (strsep(&arg, " \t") == NULL) {
726 show_oops(NULL, "match_alias: NULL URL");
727 goto done;
730 TAILQ_FOREACH(a, &aliases, entry) {
731 if (!strcmp(search, a->a_name))
732 break;
735 if (a != NULL) {
736 DNPRINTF(XT_D_URL, "match_alias: matched alias %s\n",
737 a->a_name);
738 if (arg != NULL) {
739 enc_arg = soup_uri_encode(arg, XT_RESERVED_CHARS);
740 url_out = g_strdup_printf(a->a_uri, enc_arg);
741 g_free(enc_arg);
742 } else
743 url_out = g_strdup_printf(a->a_uri, "");
745 done:
746 g_free(search);
747 return (url_out);
750 char *
751 guess_url_type(char *url_in)
753 struct stat sb;
754 char *url_out = NULL, *enc_search = NULL;
755 int i;
756 char *cwd;
759 /* substitute aliases */
760 url_out = match_alias(url_in);
761 if (url_out != NULL)
762 return (url_out);
764 /* see if we are an about page */
765 if (!strncmp(url_in, XT_URI_ABOUT, XT_URI_ABOUT_LEN))
766 for (i = 0; i < about_list_size(); i++)
767 if (!strcmp(&url_in[XT_URI_ABOUT_LEN],
768 about_list[i].name)) {
769 url_out = g_strdup(url_in);
770 goto done;
773 if (guess_search && url_regex &&
774 !(g_str_has_prefix(url_in, "http://") ||
775 g_str_has_prefix(url_in, "https://"))) {
776 if (regexec(&url_re, url_in, 0, NULL, 0)) {
777 /* invalid URI so search instead */
778 enc_search = soup_uri_encode(url_in, XT_RESERVED_CHARS);
779 url_out = g_strdup_printf(search_string, enc_search);
780 g_free(enc_search);
781 goto done;
785 /* XXX not sure about this heuristic */
786 if (stat(url_in, &sb) == 0) {
787 if (url_in[0] == '/')
788 url_out = g_strdup_printf("file://%s", url_in);
789 else {
790 cwd = malloc(PATH_MAX);
791 if (getcwd(cwd, PATH_MAX) != NULL) {
792 url_out = g_strdup_printf("file://%s/%s",cwd, url_in);
794 free(cwd);
796 } else
797 url_out = g_strdup_printf("http://%s", url_in); /* guess http */
798 done:
799 DNPRINTF(XT_D_URL, "guess_url_type: guessed %s\n", url_out);
801 return (url_out);
804 void
805 load_uri(struct tab *t, gchar *uri)
807 struct karg args;
808 gchar *newuri = NULL;
809 int i;
811 if (uri == NULL)
812 return;
814 /* Strip leading spaces. */
815 while (*uri && isspace(*uri))
816 uri++;
818 if (strlen(uri) == 0) {
819 blank(t, NULL);
820 return;
823 t->xtp_meaning = XT_XTP_TAB_MEANING_NORMAL;
825 if (valid_url_type(uri)) {
826 newuri = guess_url_type(uri);
827 uri = newuri;
830 if (!strncmp(uri, XT_URI_ABOUT, XT_URI_ABOUT_LEN)) {
831 for (i = 0; i < about_list_size(); i++)
832 if (!strcmp(&uri[XT_URI_ABOUT_LEN], about_list[i].name)) {
833 bzero(&args, sizeof args);
834 about_list[i].func(t, &args);
835 gtk_widget_set_sensitive(GTK_WIDGET(t->stop),
836 FALSE);
837 goto done;
839 show_oops(t, "invalid about page");
840 goto done;
843 set_status(t, (char *)uri, XT_STATUS_LOADING);
844 marks_clear(t);
845 webkit_web_view_load_uri(t->wv, uri);
846 done:
847 if (newuri)
848 g_free(newuri);
851 const gchar *
852 get_uri(struct tab *t)
854 const gchar *uri = NULL;
856 if (webkit_web_view_get_load_status(t->wv) == WEBKIT_LOAD_FAILED)
857 return (NULL);
858 if (t->xtp_meaning == XT_XTP_TAB_MEANING_NORMAL) {
859 uri = webkit_web_view_get_uri(t->wv);
860 } else {
861 /* use tmp_uri to make sure it is g_freed */
862 if (t->tmp_uri)
863 g_free(t->tmp_uri);
864 t->tmp_uri =g_strdup_printf("%s%s", XT_URI_ABOUT,
865 about_list[t->xtp_meaning].name);
866 uri = t->tmp_uri;
868 return uri;
871 const gchar *
872 get_title(struct tab *t, bool window)
874 const gchar *set = NULL, *title = NULL;
875 WebKitLoadStatus status = webkit_web_view_get_load_status(t->wv);
877 if (status == WEBKIT_LOAD_PROVISIONAL || status == WEBKIT_LOAD_FAILED ||
878 t->xtp_meaning == XT_XTP_TAB_MEANING_BL)
879 goto notitle;
881 title = webkit_web_view_get_title(t->wv);
882 if ((set = title ? title : get_uri(t)))
883 return set;
885 notitle:
886 set = window ? XT_NAME : "(untitled)";
888 return set;
891 struct mime_type *
892 find_mime_type(char *mime_type)
894 struct mime_type *m, *def = NULL, *rv = NULL;
896 TAILQ_FOREACH(m, &mtl, entry) {
897 if (m->mt_default &&
898 !strncmp(mime_type, m->mt_type, strlen(m->mt_type)))
899 def = m;
901 if (m->mt_default == 0 && !strcmp(mime_type, m->mt_type)) {
902 rv = m;
903 break;
907 if (rv == NULL)
908 rv = def;
910 return (rv);
913 struct domain *
914 wl_find_uri(const gchar *s, struct domain_list *wl)
916 int i;
917 char *ss;
918 struct domain *r;
920 if (s == NULL || wl == NULL)
921 return (NULL);
923 if (!strncmp(s, "http://", strlen("http://")))
924 s = &s[strlen("http://")];
925 else if (!strncmp(s, "https://", strlen("https://")))
926 s = &s[strlen("https://")];
928 if (strlen(s) < 2)
929 return (NULL);
931 for (i = 0; i < strlen(s) + 1 /* yes er need this */; i++)
932 /* chop string at first slash */
933 if (s[i] == '/' || s[i] == ':' || s[i] == '\0') {
934 ss = g_strdup(s);
935 ss[i] = '\0';
936 r = wl_find(ss, wl);
937 g_free(ss);
938 return (r);
941 return (NULL);
944 char *
945 js_ref_to_string(JSContextRef context, JSValueRef ref)
947 char *s = NULL;
948 size_t l;
949 JSStringRef jsref;
951 jsref = JSValueToStringCopy(context, ref, NULL);
952 if (jsref == NULL)
953 return (NULL);
955 l = JSStringGetMaximumUTF8CStringSize(jsref);
956 s = g_malloc(l);
957 if (s)
958 JSStringGetUTF8CString(jsref, s, l);
959 JSStringRelease(jsref);
961 return (s);
964 #define XT_JS_DONE ("done;")
965 #define XT_JS_DONE_LEN (strlen(XT_JS_DONE))
966 #define XT_JS_INSERT ("insert;")
967 #define XT_JS_INSERT_LEN (strlen(XT_JS_INSERT))
970 run_script(struct tab *t, char *s)
972 JSGlobalContextRef ctx;
973 WebKitWebFrame *frame;
974 JSStringRef str;
975 JSValueRef val, exception;
976 char *es;
978 DNPRINTF(XT_D_JS, "%s: tab %d %s\n", __func__,
979 t->tab_id, s == (char *)JS_HINTING ? "JS_HINTING" : s);
981 frame = webkit_web_view_get_main_frame(t->wv);
982 ctx = webkit_web_frame_get_global_context(frame);
984 str = JSStringCreateWithUTF8CString(s);
985 val = JSEvaluateScript(ctx, str, JSContextGetGlobalObject(ctx),
986 NULL, 0, &exception);
987 JSStringRelease(str);
989 DNPRINTF(XT_D_JS, "%s: val %p\n", __func__, val);
990 if (val == NULL) {
991 es = js_ref_to_string(ctx, exception);
992 if (es) {
993 DNPRINTF(XT_D_JS, "%s: exception %s\n", __func__, es);
994 g_free(es);
996 return (1);
997 } else {
998 es = js_ref_to_string(ctx, val);
999 #if 0
1000 /* return values */
1001 if (!strncmp(es, XT_JS_DONE, XT_JS_DONE_LEN))
1002 ; /* do nothing */
1003 if (!strncmp(es, XT_JS_INSERT, XT_JS_INSERT_LEN))
1004 ; /* do nothing */
1005 #endif
1006 if (es) {
1007 DNPRINTF(XT_D_JS, "%s: val %s\n", __func__, es);
1008 g_free(es);
1012 return (0);
1015 void
1016 enable_hints(struct tab *t)
1018 DNPRINTF(XT_D_JS, "%s: tab %d\n", __func__, t->tab_id);
1020 if (t->new_tab)
1021 run_script(t, "hints.createHints('', 'F');");
1022 else
1023 run_script(t, "hints.createHints('', 'f');");
1024 t->mode = XT_MODE_HINT;
1027 void
1028 disable_hints(struct tab *t)
1030 DNPRINTF(XT_D_JS, "%s: tab %d\n", __func__, t->tab_id);
1032 run_script(t, "hints.clearHints();");
1033 t->mode = XT_MODE_COMMAND;
1034 t->new_tab = 0;
1038 hint(struct tab *t, struct karg *args)
1041 DNPRINTF(XT_D_JS, "hint: tab %d args %d\n", t->tab_id, args->i);
1043 if (t->mode == XT_MODE_HINT) {
1044 if (args->i == XT_HINT_NEWTAB)
1045 t->new_tab = 1;
1046 enable_hints(t);
1047 } else
1048 disable_hints(t);
1050 return (0);
1053 void
1054 apply_style(struct tab *t)
1056 t->styled = 1;
1057 g_object_set(G_OBJECT(t->settings),
1058 "user-stylesheet-uri", t->stylesheet, (char *)NULL);
1061 void
1062 remove_style(struct tab *t)
1064 t->styled = 0;
1065 g_object_set(G_OBJECT(t->settings),
1066 "user-stylesheet-uri", NULL, (char *)NULL);
1070 userstyle(struct tab *t, struct karg *args)
1072 struct tab *tt;
1074 DNPRINTF(XT_D_JS, "userstyle: tab %d\n", t->tab_id);
1076 switch (args->i) {
1077 case XT_STYLE_CURRENT_TAB:
1078 if (t->styled)
1079 remove_style(t);
1080 else
1081 apply_style(t);
1082 break;
1083 case XT_STYLE_GLOBAL:
1084 if (userstyle_global) {
1085 userstyle_global = 0;
1086 TAILQ_FOREACH(tt, &tabs, entry)
1087 remove_style(tt);
1088 } else {
1089 userstyle_global = 1;
1090 TAILQ_FOREACH(tt, &tabs, entry)
1091 apply_style(tt);
1093 break;
1096 return (0);
1100 quit(struct tab *t, struct karg *args)
1102 if (save_global_history)
1103 save_global_history_to_disk(t);
1105 gtk_main_quit();
1107 return (1);
1110 void
1111 restore_sessions_list(void)
1113 DIR *sdir = NULL;
1114 struct dirent *dp = NULL;
1115 struct session *s;
1117 sdir = opendir(sessions_dir);
1118 if (sdir) {
1119 while ((dp = readdir(sdir)) != NULL)
1120 if (dp->d_type == DT_REG) {
1121 s = g_malloc(sizeof(struct session));
1122 s->name = g_strdup(dp->d_name);
1123 TAILQ_INSERT_TAIL(&sessions, s, entry);
1125 closedir(sdir);
1130 open_tabs(struct tab *t, struct karg *a)
1132 char file[PATH_MAX];
1133 FILE *f = NULL;
1134 char *uri = NULL;
1135 int rv = 1;
1136 struct tab *ti, *tt;
1138 if (a == NULL)
1139 goto done;
1141 ti = TAILQ_LAST(&tabs, tab_list);
1143 snprintf(file, sizeof file, "%s/%s", sessions_dir, a->s);
1144 if ((f = fopen(file, "r")) == NULL)
1145 goto done;
1147 for (;;) {
1148 if ((uri = fparseln(f, NULL, NULL, "\0\0\0", 0)) == NULL)
1149 if (feof(f) || ferror(f))
1150 break;
1152 /* retrieve session name */
1153 if (uri && g_str_has_prefix(uri, XT_SAVE_SESSION_ID)) {
1154 strlcpy(named_session,
1155 &uri[strlen(XT_SAVE_SESSION_ID)],
1156 sizeof named_session);
1157 continue;
1160 if (uri && strlen(uri))
1161 create_new_tab(uri, NULL, 1, -1);
1163 free(uri);
1164 uri = NULL;
1167 /* close open tabs */
1168 if (a->i == XT_SES_CLOSETABS && ti != NULL) {
1169 for (;;) {
1170 tt = TAILQ_FIRST(&tabs);
1171 if (tt == NULL)
1172 break;
1173 if (tt != ti) {
1174 delete_tab(tt);
1175 continue;
1177 delete_tab(tt);
1178 break;
1182 rv = 0;
1183 done:
1184 if (f)
1185 fclose(f);
1187 return (rv);
1191 restore_saved_tabs(void)
1193 char file[PATH_MAX];
1194 int unlink_file = 0;
1195 struct stat sb;
1196 struct karg a;
1197 int rv = 0;
1199 snprintf(file, sizeof file, "%s/%s",
1200 sessions_dir, XT_RESTART_TABS_FILE);
1201 if (stat(file, &sb) == -1)
1202 a.s = XT_SAVED_TABS_FILE;
1203 else {
1204 unlink_file = 1;
1205 a.s = XT_RESTART_TABS_FILE;
1208 a.i = XT_SES_DONOTHING;
1209 rv = open_tabs(NULL, &a);
1211 if (unlink_file)
1212 unlink(file);
1214 return (rv);
1218 save_tabs(struct tab *t, struct karg *a)
1220 char file[PATH_MAX];
1221 FILE *f;
1222 int num_tabs = 0, i;
1223 struct tab **stabs = NULL;
1225 /* tab may be null here */
1227 if (a == NULL)
1228 return (1);
1229 if (a->s == NULL)
1230 snprintf(file, sizeof file, "%s/%s",
1231 sessions_dir, named_session);
1232 else
1233 snprintf(file, sizeof file, "%s/%s", sessions_dir, a->s);
1235 if ((f = fopen(file, "w")) == NULL) {
1236 show_oops(t, "Can't open save_tabs file: %s", strerror(errno));
1237 return (1);
1240 /* save session name */
1241 fprintf(f, "%s%s\n", XT_SAVE_SESSION_ID, named_session);
1243 /* Save tabs, in the order they are arranged in the notebook. */
1244 num_tabs = sort_tabs_by_page_num(&stabs);
1246 for (i = 0; i < num_tabs; i++)
1247 if (stabs[i]) {
1248 if (get_uri(stabs[i]) != NULL)
1249 fprintf(f, "%s\n", get_uri(stabs[i]));
1250 else if (gtk_entry_get_text(GTK_ENTRY(
1251 stabs[i]->uri_entry)))
1252 fprintf(f, "%s\n", gtk_entry_get_text(GTK_ENTRY(
1253 stabs[i]->uri_entry)));
1256 g_free(stabs);
1258 /* try and make sure this gets to disk NOW. XXX Backup first? */
1259 if (fflush(f) != 0 || fsync(fileno(f)) != 0) {
1260 show_oops(t, "May not have managed to save session: %s",
1261 strerror(errno));
1264 fclose(f);
1266 return (0);
1270 save_tabs_and_quit(struct tab *t, struct karg *args)
1272 struct karg a;
1274 a.s = NULL;
1275 save_tabs(t, &a);
1276 quit(t, NULL);
1278 return (1);
1282 run_page_script(struct tab *t, struct karg *args)
1284 const gchar *uri;
1285 char *tmp, script[PATH_MAX];
1287 tmp = args->s != NULL && strlen(args->s) > 0 ? args->s : default_script;
1288 if (tmp[0] == '\0') {
1289 show_oops(t, "no script specified");
1290 return (1);
1293 if ((uri = get_uri(t)) == NULL) {
1294 show_oops(t, "tab is empty, not running script");
1295 return (1);
1298 if (tmp[0] == '~')
1299 snprintf(script, sizeof script, "%s/%s",
1300 pwd->pw_dir, &tmp[1]);
1301 else
1302 strlcpy(script, tmp, sizeof script);
1304 switch (fork()) {
1305 case -1:
1306 show_oops(t, "can't fork to run script");
1307 return (1);
1308 /* NOTREACHED */
1309 case 0:
1310 break;
1311 default:
1312 return (0);
1315 /* child */
1316 execlp(script, script, uri, (void *)NULL);
1318 _exit(0);
1320 /* NOTREACHED */
1322 return (0);
1326 yank_uri(struct tab *t, struct karg *args)
1328 const gchar *uri;
1329 GtkClipboard *clipboard;
1331 if ((uri = get_uri(t)) == NULL)
1332 return (1);
1334 clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1335 gtk_clipboard_set_text(clipboard, uri, -1);
1337 return (0);
1341 paste_uri(struct tab *t, struct karg *args)
1343 GtkClipboard *clipboard;
1344 GdkAtom atom = gdk_atom_intern("CUT_BUFFER0", FALSE);
1345 gint len;
1346 gchar *p = NULL, *uri;
1348 /* try primary clipboard first */
1349 clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1350 p = gtk_clipboard_wait_for_text(clipboard);
1352 /* if it failed get whatever text is in cut_buffer0 */
1353 if (p == NULL && xterm_workaround)
1354 if (gdk_property_get(gdk_get_default_root_window(),
1355 atom,
1356 gdk_atom_intern("STRING", FALSE),
1358 1024 * 1024 /* picked out of my butt */,
1359 FALSE,
1360 NULL,
1361 NULL,
1362 &len,
1363 (guchar **)&p)) {
1364 /* yes sir, we need to NUL the string */
1365 p[len] = '\0';
1368 if (p) {
1369 uri = p;
1370 while (*uri && isspace(*uri))
1371 uri++;
1372 if (strlen(uri) == 0) {
1373 show_oops(t, "empty paste buffer");
1374 goto done;
1376 if (guess_search == 0 && valid_url_type(uri)) {
1377 /* we can be clever and paste this in search box */
1378 show_oops(t, "not a valid URL");
1379 goto done;
1382 if (args->i == XT_PASTE_CURRENT_TAB)
1383 load_uri(t, uri);
1384 else if (args->i == XT_PASTE_NEW_TAB)
1385 create_new_tab(uri, NULL, 1, -1);
1388 done:
1389 if (p)
1390 g_free(p);
1392 return (0);
1395 void
1396 js_toggle_cb(GtkWidget *w, struct tab *t)
1398 struct karg a;
1399 int es, set;
1401 g_object_get(G_OBJECT(t->settings),
1402 "enable-scripts", &es, (char *)NULL);
1403 es = !es;
1404 if (es)
1405 set = XT_WL_ENABLE;
1406 else
1407 set = XT_WL_DISABLE;
1409 a.i = set | XT_WL_TOPLEVEL;
1410 toggle_pl(t, &a);
1412 a.i = set | XT_WL_TOPLEVEL;
1413 toggle_cwl(t, &a);
1415 a.i = XT_WL_TOGGLE | XT_WL_TOPLEVEL | XT_WL_RELOAD;
1416 toggle_js(t, &a);
1420 toggle_src(struct tab *t, struct karg *args)
1422 gboolean mode;
1424 if (t == NULL)
1425 return (0);
1427 mode = webkit_web_view_get_view_source_mode(t->wv);
1428 webkit_web_view_set_view_source_mode(t->wv, !mode);
1429 webkit_web_view_reload(t->wv);
1431 return (0);
1434 void
1435 focus_webview(struct tab *t)
1437 if (t == NULL)
1438 return;
1440 /* only grab focus if we are visible */
1441 if (gtk_notebook_get_current_page(notebook) == t->tab_id)
1442 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
1446 focus(struct tab *t, struct karg *args)
1448 if (t == NULL || args == NULL)
1449 return (1);
1451 if (show_url == 0)
1452 return (0);
1454 if (args->i == XT_FOCUS_URI)
1455 gtk_widget_grab_focus(GTK_WIDGET(t->uri_entry));
1456 else if (args->i == XT_FOCUS_SEARCH)
1457 gtk_widget_grab_focus(GTK_WIDGET(t->search_entry));
1459 return (0);
1463 connect_socket_from_uri(const gchar *uri, const gchar **error_str, char *domain,
1464 size_t domain_sz)
1466 SoupURI *su = NULL;
1467 struct addrinfo hints, *res = NULL, *ai;
1468 int rv = -1, s = -1, on, error;
1469 char port[8];
1470 static gchar myerror[256]; /* this is not thread safe */
1472 myerror[0] = '\0';
1473 *error_str = myerror;
1474 if (uri && !g_str_has_prefix(uri, "https://")) {
1475 *error_str = "invalid URI";
1476 goto done;
1479 su = soup_uri_new(uri);
1480 if (su == NULL) {
1481 *error_str = "invalid soup URI";
1482 goto done;
1484 if (!SOUP_URI_VALID_FOR_HTTP(su)) {
1485 *error_str = "invalid HTTPS URI";
1486 goto done;
1489 snprintf(port, sizeof port, "%d", su->port);
1490 bzero(&hints, sizeof(struct addrinfo));
1491 hints.ai_flags = AI_CANONNAME;
1492 hints.ai_family = AF_UNSPEC;
1493 hints.ai_socktype = SOCK_STREAM;
1495 if ((error = getaddrinfo(su->host, port, &hints, &res))) {
1496 snprintf(myerror, sizeof myerror, "getaddrinfo failed: %s",
1497 gai_strerror(errno));
1498 goto done;
1501 for (ai = res; ai; ai = ai->ai_next) {
1502 if (s != -1) {
1503 close(s);
1504 s = -1;
1507 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1508 continue;
1509 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1510 if (s == -1)
1511 continue;
1512 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on,
1513 sizeof(on)) == -1)
1514 continue;
1515 if (connect(s, ai->ai_addr, ai->ai_addrlen) == 0)
1516 break;
1518 if (s == -1) {
1519 snprintf(myerror, sizeof myerror,
1520 "could not obtain certificates from: %s",
1521 su->host);
1522 goto done;
1525 if (domain)
1526 strlcpy(domain, su->host, domain_sz);
1527 rv = s;
1528 done:
1529 if (su)
1530 soup_uri_free(su);
1531 if (res)
1532 freeaddrinfo(res);
1533 if (rv == -1 && s != -1)
1534 close(s);
1536 return (rv);
1540 stop_tls(gnutls_session_t gsession, gnutls_certificate_credentials_t xcred)
1542 if (gsession)
1543 gnutls_deinit(gsession);
1544 if (xcred)
1545 gnutls_certificate_free_credentials(xcred);
1547 return (0);
1551 start_tls(const gchar **error_str, int s, gnutls_session_t *gs,
1552 gnutls_certificate_credentials_t *xc)
1554 gnutls_certificate_credentials_t xcred;
1555 gnutls_session_t gsession;
1556 int rv = 1;
1557 static gchar myerror[1024]; /* this is not thread safe */
1559 if (gs == NULL || xc == NULL)
1560 goto done;
1562 myerror[0] = '\0';
1563 *gs = NULL;
1564 *xc = NULL;
1566 gnutls_certificate_allocate_credentials(&xcred);
1567 gnutls_certificate_set_x509_trust_file(xcred, ssl_ca_file,
1568 GNUTLS_X509_FMT_PEM);
1570 gnutls_init(&gsession, GNUTLS_CLIENT);
1571 gnutls_priority_set_direct(gsession, "PERFORMANCE", NULL);
1572 gnutls_credentials_set(gsession, GNUTLS_CRD_CERTIFICATE, xcred);
1573 gnutls_transport_set_ptr(gsession, (gnutls_transport_ptr_t)(long)s);
1574 if ((rv = gnutls_handshake(gsession)) < 0) {
1575 snprintf(myerror, sizeof myerror,
1576 "gnutls_handshake failed %d fatal %d %s",
1578 gnutls_error_is_fatal(rv),
1579 gnutls_strerror_name(rv));
1580 stop_tls(gsession, xcred);
1581 goto done;
1584 gnutls_credentials_type_t cred;
1585 cred = gnutls_auth_get_type(gsession);
1586 if (cred != GNUTLS_CRD_CERTIFICATE) {
1587 snprintf(myerror, sizeof myerror,
1588 "gnutls_auth_get_type failed %d",
1589 (int)cred);
1590 stop_tls(gsession, xcred);
1591 goto done;
1594 *gs = gsession;
1595 *xc = xcred;
1596 rv = 0;
1597 done:
1598 *error_str = myerror;
1599 return (rv);
1603 get_connection_certs(gnutls_session_t gsession, gnutls_x509_crt_t **certs,
1604 size_t *cert_count)
1606 unsigned int len;
1607 const gnutls_datum_t *cl;
1608 gnutls_x509_crt_t *all_certs;
1609 int i, rv = 1;
1611 if (certs == NULL || cert_count == NULL)
1612 goto done;
1613 if (gnutls_certificate_type_get(gsession) != GNUTLS_CRT_X509)
1614 goto done;
1615 cl = gnutls_certificate_get_peers(gsession, &len);
1616 if (len == 0)
1617 goto done;
1619 all_certs = g_malloc(sizeof(gnutls_x509_crt_t) * len);
1620 for (i = 0; i < len; i++) {
1621 gnutls_x509_crt_init(&all_certs[i]);
1622 if (gnutls_x509_crt_import(all_certs[i], &cl[i],
1623 GNUTLS_X509_FMT_PEM < 0)) {
1624 g_free(all_certs);
1625 goto done;
1629 *certs = all_certs;
1630 *cert_count = len;
1631 rv = 0;
1632 done:
1633 return (rv);
1636 void
1637 free_connection_certs(gnutls_x509_crt_t *certs, size_t cert_count)
1639 int i;
1641 for (i = 0; i < cert_count; i++)
1642 gnutls_x509_crt_deinit(certs[i]);
1643 g_free(certs);
1646 void
1647 statusbar_modify_attr(struct tab *t, const char *text, const char *base)
1649 GdkColor c_text, c_base;
1651 gdk_color_parse(text, &c_text);
1652 gdk_color_parse(base, &c_base);
1654 gtk_widget_modify_text(t->sbe.statusbar, GTK_STATE_NORMAL, &c_text);
1655 gtk_widget_modify_text(t->sbe.buffercmd, GTK_STATE_NORMAL, &c_text);
1656 gtk_widget_modify_text(t->sbe.zoom, GTK_STATE_NORMAL, &c_text);
1657 gtk_widget_modify_text(t->sbe.position, GTK_STATE_NORMAL, &c_text);
1659 gtk_widget_modify_base(t->sbe.statusbar, GTK_STATE_NORMAL, &c_base);
1660 gtk_widget_modify_base(t->sbe.buffercmd, GTK_STATE_NORMAL, &c_base);
1661 gtk_widget_modify_base(t->sbe.zoom, GTK_STATE_NORMAL, &c_base);
1662 gtk_widget_modify_base(t->sbe.position, GTK_STATE_NORMAL, &c_base);
1665 void
1666 save_certs(struct tab *t, gnutls_x509_crt_t *certs,
1667 size_t cert_count, char *domain)
1669 size_t cert_buf_sz;
1670 char cert_buf[64 * 1024], file[PATH_MAX];
1671 int i;
1672 FILE *f;
1673 GdkColor color;
1675 if (t == NULL || certs == NULL || cert_count <= 0 || domain == NULL)
1676 return;
1678 snprintf(file, sizeof file, "%s/%s", certs_dir, domain);
1679 if ((f = fopen(file, "w")) == NULL) {
1680 show_oops(t, "Can't create cert file %s %s",
1681 file, strerror(errno));
1682 return;
1685 for (i = 0; i < cert_count; i++) {
1686 cert_buf_sz = sizeof cert_buf;
1687 if (gnutls_x509_crt_export(certs[i], GNUTLS_X509_FMT_PEM,
1688 cert_buf, &cert_buf_sz)) {
1689 show_oops(t, "gnutls_x509_crt_export failed");
1690 goto done;
1692 if (fwrite(cert_buf, cert_buf_sz, 1, f) != 1) {
1693 show_oops(t, "Can't write certs: %s", strerror(errno));
1694 goto done;
1698 /* not the best spot but oh well */
1699 gdk_color_parse("lightblue", &color);
1700 gtk_widget_modify_base(t->uri_entry, GTK_STATE_NORMAL, &color);
1701 statusbar_modify_attr(t, XT_COLOR_BLACK, "lightblue");
1702 done:
1703 fclose(f);
1706 enum cert_trust {
1707 CERT_LOCAL,
1708 CERT_TRUSTED,
1709 CERT_UNTRUSTED,
1710 CERT_BAD
1713 enum cert_trust
1714 load_compare_cert(const gchar *uri, const gchar **error_str)
1716 char domain[8182], file[PATH_MAX];
1717 char cert_buf[64 * 1024], r_cert_buf[64 * 1024];
1718 int s = -1, i;
1719 unsigned int error = 0;
1720 FILE *f = NULL;
1721 size_t cert_buf_sz, cert_count;
1722 enum cert_trust rv = CERT_UNTRUSTED;
1723 static gchar serr[80]; /* this isn't thread safe */
1724 gnutls_session_t gsession;
1725 gnutls_x509_crt_t *certs;
1726 gnutls_certificate_credentials_t xcred;
1728 DNPRINTF(XT_D_URL, "%s: %s\n", __func__, uri);
1730 serr[0] = '\0';
1731 *error_str = serr;
1732 if ((s = connect_socket_from_uri(uri, error_str, domain,
1733 sizeof domain)) == -1)
1734 return (rv);
1736 DNPRINTF(XT_D_URL, "%s: fd %d\n", __func__, s);
1738 /* go ssl/tls */
1739 if (start_tls(error_str, s, &gsession, &xcred))
1740 goto done;
1741 DNPRINTF(XT_D_URL, "%s: got tls\n", __func__);
1743 /* verify certs in case cert file doesn't exist */
1744 if (gnutls_certificate_verify_peers2(gsession, &error) !=
1745 GNUTLS_E_SUCCESS) {
1746 *error_str = "Invalid certificates";
1747 goto done;
1750 /* get certs */
1751 if (get_connection_certs(gsession, &certs, &cert_count)) {
1752 *error_str = "Can't get connection certificates";
1753 goto done;
1756 snprintf(file, sizeof file, "%s/%s", certs_dir, domain);
1757 if ((f = fopen(file, "r")) == NULL) {
1758 if (!error)
1759 rv = CERT_TRUSTED;
1760 goto freeit;
1763 for (i = 0; i < cert_count; i++) {
1764 cert_buf_sz = sizeof cert_buf;
1765 if (gnutls_x509_crt_export(certs[i], GNUTLS_X509_FMT_PEM,
1766 cert_buf, &cert_buf_sz)) {
1767 goto freeit;
1769 if (fread(r_cert_buf, cert_buf_sz, 1, f) != 1) {
1770 rv = CERT_BAD; /* critical */
1771 goto freeit;
1773 if (bcmp(r_cert_buf, cert_buf, sizeof cert_buf_sz)) {
1774 rv = CERT_BAD; /* critical */
1775 goto freeit;
1777 rv = CERT_LOCAL;
1780 freeit:
1781 if (f)
1782 fclose(f);
1783 free_connection_certs(certs, cert_count);
1784 done:
1785 /* we close the socket first for speed */
1786 if (s != -1)
1787 close(s);
1789 /* only complain if we didn't save it locally */
1790 if (error && rv != CERT_LOCAL) {
1791 strlcpy(serr, "Certificate exception(s): ", sizeof serr);
1792 if (error & GNUTLS_CERT_INVALID)
1793 strlcat(serr, "invalid, ", sizeof serr);
1794 if (error & GNUTLS_CERT_REVOKED)
1795 strlcat(serr, "revoked, ", sizeof serr);
1796 if (error & GNUTLS_CERT_SIGNER_NOT_FOUND)
1797 strlcat(serr, "signer not found, ", sizeof serr);
1798 if (error & GNUTLS_CERT_SIGNER_NOT_CA)
1799 strlcat(serr, "not signed by CA, ", sizeof serr);
1800 if (error & GNUTLS_CERT_INSECURE_ALGORITHM)
1801 strlcat(serr, "insecure algorithm, ", sizeof serr);
1802 if (error & GNUTLS_CERT_NOT_ACTIVATED)
1803 strlcat(serr, "not activated, ", sizeof serr);
1804 if (error & GNUTLS_CERT_EXPIRED)
1805 strlcat(serr, "expired, ", sizeof serr);
1806 for (i = strlen(serr) - 1; i > 0; i--)
1807 if (serr[i] == ',') {
1808 serr[i] = '\0';
1809 break;
1811 *error_str = serr;
1814 stop_tls(gsession, xcred);
1816 return (rv);
1820 cert_cmd(struct tab *t, struct karg *args)
1822 const gchar *uri, *error_str = NULL;
1823 char domain[8182];
1824 int s = -1;
1825 size_t cert_count;
1826 gnutls_session_t gsession;
1827 gnutls_x509_crt_t *certs;
1828 gnutls_certificate_credentials_t xcred;
1830 if (t == NULL)
1831 return (1);
1833 if (ssl_ca_file == NULL) {
1834 show_oops(t, "Can't open CA file: %s", ssl_ca_file);
1835 return (1);
1838 if ((uri = get_uri(t)) == NULL) {
1839 show_oops(t, "Invalid URI");
1840 return (1);
1843 if ((s = connect_socket_from_uri(uri, &error_str, domain,
1844 sizeof domain)) == -1) {
1845 show_oops(t, "%s", error_str);
1846 return (1);
1849 /* go ssl/tls */
1850 if (start_tls(&error_str, s, &gsession, &xcred))
1851 goto done;
1853 /* get certs */
1854 if (get_connection_certs(gsession, &certs, &cert_count)) {
1855 show_oops(t, "get_connection_certs failed");
1856 goto done;
1859 if (args->i & XT_SHOW)
1860 show_certs(t, certs, cert_count, "Certificate Chain");
1861 else if (args->i & XT_SAVE)
1862 save_certs(t, certs, cert_count, domain);
1864 free_connection_certs(certs, cert_count);
1865 done:
1866 /* we close the socket first for speed */
1867 if (s != -1)
1868 close(s);
1869 stop_tls(gsession, xcred);
1870 if (error_str && strlen(error_str))
1871 show_oops(t, "%s", error_str);
1872 return (0);
1876 remove_cookie(int index)
1878 int i, rv = 1;
1879 GSList *cf;
1880 SoupCookie *c;
1882 DNPRINTF(XT_D_COOKIE, "remove_cookie: %d\n", index);
1884 cf = soup_cookie_jar_all_cookies(s_cookiejar);
1886 for (i = 1; cf; cf = cf->next, i++) {
1887 if (i != index)
1888 continue;
1889 c = cf->data;
1890 print_cookie("remove cookie", c);
1891 soup_cookie_jar_delete_cookie(s_cookiejar, c);
1892 rv = 0;
1893 break;
1896 soup_cookies_free(cf);
1898 return (rv);
1902 remove_cookie_domain(int domain_id)
1904 int domain_count, rv = 1;
1905 GSList *cf;
1906 SoupCookie *c;
1907 char *last_domain;
1909 DNPRINTF(XT_D_COOKIE, "remove_cookie_domain: %d\n", domain_id);
1911 last_domain = "";
1912 cf = soup_cookie_jar_all_cookies(s_cookiejar);
1914 for (domain_count = 0; cf; cf = cf->next) {
1915 c = cf->data;
1917 if (strcmp(last_domain, c->domain) != 0) {
1918 domain_count++;
1919 last_domain = c->domain;
1922 if (domain_count < domain_id)
1923 continue;
1924 else if (domain_count > domain_id)
1925 break;
1927 print_cookie("remove cookie", c);
1928 soup_cookie_jar_delete_cookie(s_cookiejar, c);
1929 rv = 0;
1932 soup_cookies_free(cf);
1934 return (rv);
1938 toplevel_cmd(struct tab *t, struct karg *args)
1940 js_toggle_cb(t->js_toggle, t);
1942 return (0);
1946 can_go_back_for_real(struct tab *t)
1948 int i;
1949 WebKitWebHistoryItem *item;
1950 const gchar *uri;
1952 if (t == NULL)
1953 return (FALSE);
1955 /* rely on webkit to make sure we can go backward when on an about page */
1956 uri = get_uri(t);
1957 if (uri == NULL || g_str_has_prefix(uri, "about:"))
1958 return (webkit_web_view_can_go_back(t->wv));
1960 /* the back/forwars list is stupid so help determine if we can go back */
1961 for (i = 0, item = webkit_web_back_forward_list_get_current_item(t->bfl);
1962 item != NULL;
1963 i--, item = webkit_web_back_forward_list_get_nth_item(t->bfl, i)) {
1964 if (strcmp(webkit_web_history_item_get_uri(item), uri))
1965 return (TRUE);
1968 return (FALSE);
1972 can_go_forward_for_real(struct tab *t)
1974 int i;
1975 WebKitWebHistoryItem *item;
1976 const gchar *uri;
1978 if (t == NULL)
1979 return (FALSE);
1981 /* rely on webkit to make sure we can go forward when on an about page */
1982 uri = get_uri(t);
1983 if (uri == NULL || g_str_has_prefix(uri, "about:"))
1984 return (webkit_web_view_can_go_forward(t->wv));
1986 /* the back/forwars list is stupid so help selecting a different item */
1987 for (i = 0, item = webkit_web_back_forward_list_get_current_item(t->bfl);
1988 item != NULL;
1989 i++, item = webkit_web_back_forward_list_get_nth_item(t->bfl, i)) {
1990 if (strcmp(webkit_web_history_item_get_uri(item), uri))
1991 return (TRUE);
1994 return (FALSE);
1997 void
1998 go_back_for_real(struct tab *t)
2000 int i;
2001 WebKitWebHistoryItem *item;
2002 const gchar *uri;
2004 if (t == NULL)
2005 return;
2007 uri = get_uri(t);
2008 if (uri == NULL) {
2009 webkit_web_view_go_back(t->wv);
2010 return;
2012 /* the back/forwars list is stupid so help selecting a different item */
2013 for (i = 0, item = webkit_web_back_forward_list_get_current_item(t->bfl);
2014 item != NULL;
2015 i--, item = webkit_web_back_forward_list_get_nth_item(t->bfl, i)) {
2016 if (strcmp(webkit_web_history_item_get_uri(item), uri)) {
2017 webkit_web_view_go_to_back_forward_item(t->wv, item);
2018 break;
2023 void
2024 go_forward_for_real(struct tab *t)
2026 int i;
2027 WebKitWebHistoryItem *item;
2028 const gchar *uri;
2030 if (t == NULL)
2031 return;
2033 uri = get_uri(t);
2034 if (uri == NULL) {
2035 webkit_web_view_go_forward(t->wv);
2036 return;
2038 /* the back/forwars list is stupid so help selecting a different item */
2039 for (i = 0, item = webkit_web_back_forward_list_get_current_item(t->bfl);
2040 item != NULL;
2041 i++, item = webkit_web_back_forward_list_get_nth_item(t->bfl, i)) {
2042 if (strcmp(webkit_web_history_item_get_uri(item), uri)) {
2043 webkit_web_view_go_to_back_forward_item(t->wv, item);
2044 break;
2050 navaction(struct tab *t, struct karg *args)
2052 WebKitWebHistoryItem *item;
2053 WebKitWebFrame *frame;
2055 DNPRINTF(XT_D_NAV, "navaction: tab %d opcode %d\n",
2056 t->tab_id, args->i);
2058 t->xtp_meaning = XT_XTP_TAB_MEANING_NORMAL;
2059 if (t->item) {
2060 if (args->i == XT_NAV_BACK)
2061 item = webkit_web_back_forward_list_get_current_item(t->bfl);
2062 else
2063 item = webkit_web_back_forward_list_get_forward_item(t->bfl);
2064 if (item == NULL)
2065 return (XT_CB_PASSTHROUGH);
2066 webkit_web_view_go_to_back_forward_item(t->wv, item);
2067 t->item = NULL;
2068 return (XT_CB_PASSTHROUGH);
2071 switch (args->i) {
2072 case XT_NAV_BACK:
2073 marks_clear(t);
2074 go_back_for_real(t);
2075 break;
2076 case XT_NAV_FORWARD:
2077 marks_clear(t);
2078 go_forward_for_real(t);
2079 break;
2080 case XT_NAV_RELOAD:
2081 frame = webkit_web_view_get_main_frame(t->wv);
2082 webkit_web_frame_reload(frame);
2083 break;
2084 case XT_NAV_STOP:
2085 frame = webkit_web_view_get_main_frame(t->wv);
2086 webkit_web_frame_stop_loading(frame);
2087 break;
2089 return (XT_CB_PASSTHROUGH);
2093 move(struct tab *t, struct karg *args)
2095 GtkAdjustment *adjust;
2096 double pi, si, pos, ps, upper, lower, max;
2097 double percent;
2099 switch (args->i) {
2100 case XT_MOVE_DOWN:
2101 case XT_MOVE_UP:
2102 case XT_MOVE_BOTTOM:
2103 case XT_MOVE_TOP:
2104 case XT_MOVE_PAGEDOWN:
2105 case XT_MOVE_PAGEUP:
2106 case XT_MOVE_HALFDOWN:
2107 case XT_MOVE_HALFUP:
2108 case XT_MOVE_PERCENT:
2109 case XT_MOVE_CENTER:
2110 adjust = t->adjust_v;
2111 break;
2112 default:
2113 adjust = t->adjust_h;
2114 break;
2117 pos = gtk_adjustment_get_value(adjust);
2118 ps = gtk_adjustment_get_page_size(adjust);
2119 upper = gtk_adjustment_get_upper(adjust);
2120 lower = gtk_adjustment_get_lower(adjust);
2121 si = gtk_adjustment_get_step_increment(adjust);
2122 pi = gtk_adjustment_get_page_increment(adjust);
2123 max = upper - ps;
2125 DNPRINTF(XT_D_MOVE, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2126 "max %f si %f pi %f\n",
2127 args->i, adjust == t->adjust_h ? "horizontal" : "vertical",
2128 pos, ps, upper, lower, max, si, pi);
2130 switch (args->i) {
2131 case XT_MOVE_DOWN:
2132 case XT_MOVE_RIGHT:
2133 pos += si;
2134 gtk_adjustment_set_value(adjust, MIN(pos, max));
2135 break;
2136 case XT_MOVE_UP:
2137 case XT_MOVE_LEFT:
2138 pos -= si;
2139 gtk_adjustment_set_value(adjust, MAX(pos, lower));
2140 break;
2141 case XT_MOVE_BOTTOM:
2142 case XT_MOVE_FARRIGHT:
2143 gtk_adjustment_set_value(adjust, max);
2144 break;
2145 case XT_MOVE_TOP:
2146 case XT_MOVE_FARLEFT:
2147 gtk_adjustment_set_value(adjust, lower);
2148 break;
2149 case XT_MOVE_PAGEDOWN:
2150 pos += pi;
2151 gtk_adjustment_set_value(adjust, MIN(pos, max));
2152 break;
2153 case XT_MOVE_PAGEUP:
2154 pos -= pi;
2155 gtk_adjustment_set_value(adjust, MAX(pos, lower));
2156 break;
2157 case XT_MOVE_HALFDOWN:
2158 pos += pi / 2;
2159 gtk_adjustment_set_value(adjust, MIN(pos, max));
2160 break;
2161 case XT_MOVE_HALFUP:
2162 pos -= pi / 2;
2163 gtk_adjustment_set_value(adjust, MAX(pos, lower));
2164 break;
2165 case XT_MOVE_CENTER:
2166 args->s = g_strdup("50.0");
2167 /* FALLTHROUGH */
2168 case XT_MOVE_PERCENT:
2169 percent = atoi(args->s) / 100.0;
2170 pos = max * percent;
2171 if (pos < 0.0 || pos > max)
2172 break;
2173 gtk_adjustment_set_value(adjust, pos);
2174 break;
2175 default:
2176 return (XT_CB_PASSTHROUGH);
2179 DNPRINTF(XT_D_MOVE, "move: new pos %f %f\n", pos, MIN(pos, max));
2181 return (XT_CB_HANDLED);
2184 void
2185 url_set_visibility(void)
2187 struct tab *t;
2189 TAILQ_FOREACH(t, &tabs, entry)
2190 if (show_url == 0) {
2191 gtk_widget_hide(t->toolbar);
2192 focus_webview(t);
2193 } else
2194 gtk_widget_show(t->toolbar);
2197 void
2198 notebook_tab_set_visibility(void)
2200 if (show_tabs == 0) {
2201 gtk_widget_hide(tab_bar);
2202 gtk_notebook_set_show_tabs(notebook, FALSE);
2203 } else {
2204 if (tab_style == XT_TABS_NORMAL) {
2205 gtk_widget_hide(tab_bar);
2206 gtk_notebook_set_show_tabs(notebook, TRUE);
2207 } else if (tab_style == XT_TABS_COMPACT) {
2208 gtk_widget_show(tab_bar);
2209 gtk_notebook_set_show_tabs(notebook, FALSE);
2214 void
2215 statusbar_set_visibility(void)
2217 struct tab *t;
2219 TAILQ_FOREACH(t, &tabs, entry){
2220 if (show_statusbar == 0)
2221 gtk_widget_hide(t->statusbar_box);
2222 else
2223 gtk_widget_show(t->statusbar_box);
2225 focus_webview(t);
2229 void
2230 url_set(struct tab *t, int enable_url_entry)
2232 GdkPixbuf *pixbuf;
2233 int progress;
2235 show_url = enable_url_entry;
2237 if (enable_url_entry) {
2238 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.statusbar),
2239 GTK_ENTRY_ICON_PRIMARY, NULL);
2240 gtk_entry_set_progress_fraction(GTK_ENTRY(t->sbe.statusbar), 0);
2241 } else {
2242 pixbuf = gtk_entry_get_icon_pixbuf(GTK_ENTRY(t->uri_entry),
2243 GTK_ENTRY_ICON_PRIMARY);
2244 progress =
2245 gtk_entry_get_progress_fraction(GTK_ENTRY(t->uri_entry));
2246 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t->sbe.statusbar),
2247 GTK_ENTRY_ICON_PRIMARY, pixbuf);
2248 gtk_entry_set_progress_fraction(GTK_ENTRY(t->sbe.statusbar),
2249 progress);
2254 fullscreen(struct tab *t, struct karg *args)
2256 DNPRINTF(XT_D_TAB, "%s: %p %d\n", __func__, t, args->i);
2258 if (t == NULL)
2259 return (XT_CB_PASSTHROUGH);
2261 if (show_url == 0) {
2262 url_set(t, 1);
2263 show_tabs = 1;
2264 } else {
2265 url_set(t, 0);
2266 show_tabs = 0;
2269 url_set_visibility();
2270 notebook_tab_set_visibility();
2272 return (XT_CB_HANDLED);
2276 statustoggle(struct tab *t, struct karg *args)
2278 DNPRINTF(XT_D_TAB, "%s: %p %d\n", __func__, t, args->i);
2280 if (show_statusbar == 1) {
2281 show_statusbar = 0;
2282 statusbar_set_visibility();
2283 } else if (show_statusbar == 0) {
2284 show_statusbar = 1;
2285 statusbar_set_visibility();
2287 return (XT_CB_HANDLED);
2291 urlaction(struct tab *t, struct karg *args)
2293 int rv = XT_CB_HANDLED;
2295 DNPRINTF(XT_D_TAB, "%s: %p %d\n", __func__, t, args->i);
2297 if (t == NULL)
2298 return (XT_CB_PASSTHROUGH);
2300 switch (args->i) {
2301 case XT_URL_SHOW:
2302 if (show_url == 0) {
2303 url_set(t, 1);
2304 url_set_visibility();
2306 break;
2307 case XT_URL_HIDE:
2308 if (show_url == 1) {
2309 url_set(t, 0);
2310 url_set_visibility();
2312 break;
2314 return (rv);
2318 tabaction(struct tab *t, struct karg *args)
2320 int rv = XT_CB_HANDLED;
2321 char *url = args->s;
2322 struct undo *u;
2323 struct tab *tt;
2325 DNPRINTF(XT_D_TAB, "tabaction: %p %d\n", t, args->i);
2327 if (t == NULL)
2328 return (XT_CB_PASSTHROUGH);
2330 switch (args->i) {
2331 case XT_TAB_NEW:
2332 if (strlen(url) > 0)
2333 create_new_tab(url, NULL, 1, args->precount);
2334 else
2335 create_new_tab(NULL, NULL, 1, args->precount);
2336 break;
2337 case XT_TAB_DELETE:
2338 if (args->precount < 0)
2339 delete_tab(t);
2340 else
2341 TAILQ_FOREACH(tt, &tabs, entry)
2342 if (tt->tab_id == args->precount - 1) {
2343 delete_tab(tt);
2344 break;
2346 break;
2347 case XT_TAB_DELQUIT:
2348 if (gtk_notebook_get_n_pages(notebook) > 1)
2349 delete_tab(t);
2350 else
2351 quit(t, args);
2352 break;
2353 case XT_TAB_OPEN:
2354 if (strlen(url) > 0)
2356 else {
2357 rv = XT_CB_PASSTHROUGH;
2358 goto done;
2360 load_uri(t, url);
2361 break;
2362 case XT_TAB_SHOW:
2363 if (show_tabs == 0) {
2364 show_tabs = 1;
2365 notebook_tab_set_visibility();
2367 break;
2368 case XT_TAB_HIDE:
2369 if (show_tabs == 1) {
2370 show_tabs = 0;
2371 notebook_tab_set_visibility();
2373 break;
2374 case XT_TAB_NEXTSTYLE:
2375 if (tab_style == XT_TABS_NORMAL) {
2376 tab_style = XT_TABS_COMPACT;
2377 recolor_compact_tabs();
2379 else
2380 tab_style = XT_TABS_NORMAL;
2381 notebook_tab_set_visibility();
2382 break;
2383 case XT_TAB_UNDO_CLOSE:
2384 if (undo_count == 0) {
2385 DNPRINTF(XT_D_TAB, "%s: no tabs to undo close",
2386 __func__);
2387 goto done;
2388 } else {
2389 undo_count--;
2390 u = TAILQ_FIRST(&undos);
2391 create_new_tab(u->uri, u, 1, -1);
2393 TAILQ_REMOVE(&undos, u, entry);
2394 g_free(u->uri);
2395 /* u->history is freed in create_new_tab() */
2396 g_free(u);
2398 break;
2399 case XT_TAB_LOAD_IMAGES:
2401 if (!auto_load_images) {
2403 /* Enable auto-load images (this will load all
2404 * previously unloaded images). */
2405 g_object_set(G_OBJECT(t->settings),
2406 "auto-load-images", TRUE, (char *)NULL);
2407 webkit_web_view_set_settings(t->wv, t->settings);
2409 webkit_web_view_reload(t->wv);
2411 /* Webkit triggers an event when we change the setting,
2412 * so we can't disable the auto-loading at once.
2414 * Unfortunately, webkit does not tell us when it's done.
2415 * Instead, we wait until the next request, and then
2416 * disable autoloading again.
2418 t->load_images = TRUE;
2420 break;
2421 default:
2422 rv = XT_CB_PASSTHROUGH;
2423 goto done;
2426 done:
2427 if (args->s) {
2428 g_free(args->s);
2429 args->s = NULL;
2432 return (rv);
2436 resizetab(struct tab *t, struct karg *args)
2438 if (t == NULL || args == NULL) {
2439 show_oops(NULL, "resizetab invalid parameters");
2440 return (XT_CB_PASSTHROUGH);
2443 DNPRINTF(XT_D_TAB, "resizetab: tab %d %d\n",
2444 t->tab_id, args->i);
2446 setzoom_webkit(t, args->i);
2448 return (XT_CB_HANDLED);
2452 movetab(struct tab *t, struct karg *args)
2454 int n, dest;
2456 if (t == NULL || args == NULL) {
2457 show_oops(NULL, "movetab invalid parameters");
2458 return (XT_CB_PASSTHROUGH);
2461 DNPRINTF(XT_D_TAB, "movetab: tab %d opcode %d\n",
2462 t->tab_id, args->i);
2464 if (args->i >= XT_TAB_INVALID)
2465 return (XT_CB_PASSTHROUGH);
2467 if (TAILQ_EMPTY(&tabs))
2468 return (XT_CB_PASSTHROUGH);
2470 n = gtk_notebook_get_n_pages(notebook);
2471 dest = gtk_notebook_get_current_page(notebook);
2473 switch (args->i) {
2474 case XT_TAB_NEXT:
2475 if (args->precount < 0)
2476 dest = dest == n - 1 ? 0 : dest + 1;
2477 else
2478 dest = args->precount - 1;
2480 break;
2481 case XT_TAB_PREV:
2482 if (args->precount < 0)
2483 dest -= 1;
2484 else
2485 dest -= args->precount % n;
2487 if (dest < 0)
2488 dest += n;
2490 break;
2491 case XT_TAB_FIRST:
2492 dest = 0;
2493 break;
2494 case XT_TAB_LAST:
2495 dest = n - 1;
2496 break;
2497 default:
2498 return (XT_CB_PASSTHROUGH);
2501 if (dest < 0 || dest >= n)
2502 return (XT_CB_PASSTHROUGH);
2503 if (t->tab_id == dest) {
2504 DNPRINTF(XT_D_TAB, "movetab: do nothing\n");
2505 return (XT_CB_HANDLED);
2508 set_current_tab(dest);
2510 return (XT_CB_HANDLED);
2513 int cmd_prefix = 0;
2517 command(struct tab *t, struct karg *args)
2519 char *s = NULL, *ss = NULL;
2520 GdkColor color;
2521 const gchar *uri;
2522 struct karg a;
2524 if (t == NULL || args == NULL) {
2525 show_oops(NULL, "command invalid parameters");
2526 return (XT_CB_PASSTHROUGH);
2529 switch (args->i) {
2530 case '/':
2531 s = "/";
2532 break;
2533 case '?':
2534 s = "?";
2535 break;
2536 case ':':
2537 if (cmd_prefix == 0)
2538 s = ":";
2539 else {
2540 ss = g_strdup_printf(":%d", cmd_prefix);
2541 s = ss;
2542 cmd_prefix = 0;
2544 break;
2545 case '.':
2546 t->mode = XT_MODE_HINT;
2547 bzero(&a, sizeof a);
2548 a.i = 0;
2549 hint(t, &a);
2550 s = ".";
2551 break;
2552 case ',':
2553 t->mode = XT_MODE_HINT;
2554 bzero(&a, sizeof a);
2555 a.i = XT_HINT_NEWTAB;
2556 hint(t, &a);
2557 s = ",";
2558 break;
2559 case XT_CMD_OPEN:
2560 s = ":open ";
2561 break;
2562 case XT_CMD_TABNEW:
2563 s = ":tabnew ";
2564 break;
2565 case XT_CMD_OPEN_CURRENT:
2566 s = ":open ";
2567 /* FALL THROUGH */
2568 case XT_CMD_TABNEW_CURRENT:
2569 if (!s) /* FALL THROUGH? */
2570 s = ":tabnew ";
2571 if ((uri = get_uri(t)) != NULL) {
2572 ss = g_strdup_printf("%s%s", s, uri);
2573 s = ss;
2575 break;
2576 default:
2577 show_oops(t, "command: invalid opcode %d", args->i);
2578 return (XT_CB_PASSTHROUGH);
2581 DNPRINTF(XT_D_CMD, "%s: tab %d type %s\n", __func__, t->tab_id, s);
2583 gtk_entry_set_text(GTK_ENTRY(t->cmd), s);
2584 gdk_color_parse(XT_COLOR_WHITE, &color);
2585 gtk_widget_modify_base(t->cmd, GTK_STATE_NORMAL, &color);
2586 show_cmd(t);
2587 gtk_widget_grab_focus(GTK_WIDGET(t->cmd));
2588 gtk_editable_set_position(GTK_EDITABLE(t->cmd), -1);
2590 if (ss)
2591 g_free(ss);
2593 return (XT_CB_HANDLED);
2597 search(struct tab *t, struct karg *args)
2599 gboolean d;
2601 if (t == NULL || args == NULL) {
2602 show_oops(NULL, "search invalid parameters");
2603 return (1);
2606 switch (args->i) {
2607 case XT_SEARCH_NEXT:
2608 d = t->search_forward;
2609 break;
2610 case XT_SEARCH_PREV:
2611 d = !t->search_forward;
2612 break;
2613 default:
2614 return (XT_CB_PASSTHROUGH);
2617 if (t->search_text == NULL) {
2618 if (global_search == NULL)
2619 return (XT_CB_PASSTHROUGH);
2620 else {
2621 d = t->search_forward = TRUE;
2622 t->search_text = g_strdup(global_search);
2623 webkit_web_view_mark_text_matches(t->wv, global_search, FALSE, 0);
2624 webkit_web_view_set_highlight_text_matches(t->wv, TRUE);
2628 DNPRINTF(XT_D_CMD, "search: tab %d opc %d forw %d text %s\n",
2629 t->tab_id, args->i, t->search_forward, t->search_text);
2631 webkit_web_view_search_text(t->wv, t->search_text, FALSE, d, TRUE);
2633 return (XT_CB_HANDLED);
2637 session_save(struct tab *t, char *filename)
2639 struct karg a;
2640 int rv = 1;
2641 struct session *s;
2643 if (strlen(filename) == 0)
2644 goto done;
2646 if (filename[0] == '.' || filename[0] == '/')
2647 goto done;
2649 a.s = filename;
2650 if (save_tabs(t, &a))
2651 goto done;
2652 strlcpy(named_session, filename, sizeof named_session);
2654 /* add the new session to the list of sessions */
2655 s = g_malloc(sizeof(struct session));
2656 s->name = g_strdup(filename);
2657 TAILQ_INSERT_TAIL(&sessions, s, entry);
2659 rv = 0;
2660 done:
2661 return (rv);
2665 session_open(struct tab *t, char *filename)
2667 struct karg a;
2668 int rv = 1;
2670 if (strlen(filename) == 0)
2671 goto done;
2673 if (filename[0] == '.' || filename[0] == '/')
2674 goto done;
2676 a.s = filename;
2677 a.i = XT_SES_CLOSETABS;
2678 if (open_tabs(t, &a))
2679 goto done;
2681 strlcpy(named_session, filename, sizeof named_session);
2683 rv = 0;
2684 done:
2685 return (rv);
2689 session_delete(struct tab *t, char *filename)
2691 char file[PATH_MAX];
2692 int rv = 1;
2693 struct session *s;
2695 if (strlen(filename) == 0)
2696 goto done;
2698 if (filename[0] == '.' || filename[0] == '/')
2699 goto done;
2701 snprintf(file, sizeof file, "%s/%s", sessions_dir, filename);
2702 if (unlink(file))
2703 goto done;
2705 if (!strcmp(filename, named_session))
2706 strlcpy(named_session, XT_SAVED_TABS_FILE,
2707 sizeof named_session);
2709 /* remove session from sessions list */
2710 TAILQ_FOREACH(s, &sessions, entry) {
2711 if (!strcmp(s->name, filename))
2712 break;
2714 if (s == NULL)
2715 goto done;
2716 TAILQ_REMOVE(&sessions, s, entry);
2717 g_free((gpointer) s->name);
2718 g_free(s);
2720 rv = 0;
2721 done:
2722 return (rv);
2726 session_cmd(struct tab *t, struct karg *args)
2728 char *filename = args->s;
2730 if (t == NULL)
2731 return (1);
2733 if (args->i & XT_SHOW)
2734 show_oops(t, "Current session: %s", named_session[0] == '\0' ?
2735 XT_SAVED_TABS_FILE : named_session);
2736 else if (args->i & XT_SAVE) {
2737 if (session_save(t, filename)) {
2738 show_oops(t, "Can't save session: %s",
2739 filename ? filename : "INVALID");
2740 goto done;
2742 } else if (args->i & XT_OPEN) {
2743 if (session_open(t, filename)) {
2744 show_oops(t, "Can't open session: %s",
2745 filename ? filename : "INVALID");
2746 goto done;
2748 } else if (args->i & XT_DELETE) {
2749 if (session_delete(t, filename)) {
2750 show_oops(t, "Can't delete session: %s",
2751 filename ? filename : "INVALID");
2752 goto done;
2755 done:
2756 return (XT_CB_PASSTHROUGH);
2760 script_cmd(struct tab *t, struct karg *args)
2762 struct stat sb;
2763 FILE *f = NULL;
2764 char *buf = NULL;
2766 if (t == NULL)
2767 goto done;
2769 if ((f = fopen(args->s, "r")) == NULL) {
2770 show_oops(t, "Can't open script file: %s", args->s);
2771 goto done;
2774 if (fstat(fileno(f), &sb) == -1) {
2775 show_oops(t, "Can't stat script file: %s", args->s);
2776 goto done;
2779 buf = g_malloc0(sb.st_size + 1);
2780 if (fread(buf, 1, sb.st_size, f) != sb.st_size) {
2781 show_oops(t, "Can't read script file: %s", args->s);
2782 goto done;
2785 DNPRINTF(XT_D_JS, "%s: about to run script\n", __func__);
2786 run_script(t, buf);
2788 done:
2789 if (f)
2790 fclose(f);
2791 if (buf)
2792 g_free(buf);
2794 return (XT_CB_PASSTHROUGH);
2798 * Make a hardcopy of the page
2801 print_page(struct tab *t, struct karg *args)
2803 WebKitWebFrame *frame;
2804 GtkPageSetup *ps;
2805 GtkPrintOperation *op;
2806 GtkPrintOperationAction action;
2807 GtkPrintOperationResult print_res;
2808 GError *g_err = NULL;
2809 int marg_l, marg_r, marg_t, marg_b;
2811 DNPRINTF(XT_D_PRINTING, "%s:", __func__);
2813 ps = gtk_page_setup_new();
2814 op = gtk_print_operation_new();
2815 action = GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG;
2816 frame = webkit_web_view_get_main_frame(t->wv);
2818 /* the default margins are too small, so we will bump them */
2819 marg_l = gtk_page_setup_get_left_margin(ps, GTK_UNIT_MM) +
2820 XT_PRINT_EXTRA_MARGIN;
2821 marg_r = gtk_page_setup_get_right_margin(ps, GTK_UNIT_MM) +
2822 XT_PRINT_EXTRA_MARGIN;
2823 marg_t = gtk_page_setup_get_top_margin(ps, GTK_UNIT_MM) +
2824 XT_PRINT_EXTRA_MARGIN;
2825 marg_b = gtk_page_setup_get_bottom_margin(ps, GTK_UNIT_MM) +
2826 XT_PRINT_EXTRA_MARGIN;
2828 /* set margins */
2829 gtk_page_setup_set_left_margin(ps, marg_l, GTK_UNIT_MM);
2830 gtk_page_setup_set_right_margin(ps, marg_r, GTK_UNIT_MM);
2831 gtk_page_setup_set_top_margin(ps, marg_t, GTK_UNIT_MM);
2832 gtk_page_setup_set_bottom_margin(ps, marg_b, GTK_UNIT_MM);
2834 gtk_print_operation_set_default_page_setup(op, ps);
2836 /* this appears to free 'op' and 'ps' */
2837 print_res = webkit_web_frame_print_full(frame, op, action, &g_err);
2839 /* check it worked */
2840 if (print_res == GTK_PRINT_OPERATION_RESULT_ERROR) {
2841 show_oops(NULL, "can't print: %s", g_err->message);
2842 g_error_free (g_err);
2843 return (1);
2846 return (0);
2850 go_home(struct tab *t, struct karg *args)
2852 load_uri(t, home);
2853 return (0);
2857 set_encoding(struct tab *t, struct karg *args)
2859 const gchar *e;
2861 if (args->s && strlen(g_strstrip(args->s)) == 0) {
2862 e = webkit_web_view_get_custom_encoding(t->wv);
2863 if (e == NULL)
2864 e = webkit_web_view_get_encoding(t->wv);
2865 show_oops(t, "encoding: %s", e ? e : "N/A");
2866 } else
2867 webkit_web_view_set_custom_encoding(t->wv, args->s);
2869 return (0);
2873 restart(struct tab *t, struct karg *args)
2875 struct karg a;
2877 a.s = XT_RESTART_TABS_FILE;
2878 save_tabs(t, &a);
2879 execvp(start_argv[0], start_argv);
2880 /* NOTREACHED */
2882 return (0);
2885 char *http_proxy_save; /* not a setting, used to toggle */
2888 proxy_cmd(struct tab *t, struct karg *args)
2890 DNPRINTF(XT_D_CMD, "%s: tab %d\n", __func__, t->tab_id);
2892 if (t == NULL)
2893 return (1);
2895 /* setup */
2896 if (http_proxy) {
2897 if (http_proxy_save)
2898 g_free(http_proxy_save);
2899 http_proxy_save = g_strdup(http_proxy);
2902 if (args->i & XT_PRXY_SHOW) {
2903 if (http_proxy)
2904 show_oops(t, "http_proxy = %s", http_proxy);
2905 else
2906 show_oops(t, "proxy is currently disabled");
2907 } else if (args->i & XT_PRXY_TOGGLE) {
2908 if (http_proxy_save == NULL && http_proxy == NULL) {
2909 show_oops(t, "can't toggle proxy");
2910 goto done;
2912 if (http_proxy)
2913 setup_proxy(NULL);
2914 else
2915 setup_proxy(http_proxy_save);
2917 done:
2918 return (XT_CB_PASSTHROUGH);
2920 struct cmd {
2921 char *cmd;
2922 int level;
2923 int (*func)(struct tab *, struct karg *);
2924 int arg;
2925 int type;
2926 } cmds[] = {
2927 { "command_mode", 0, command_mode, XT_MODE_COMMAND, 0 },
2928 { "insert_mode", 0, command_mode, XT_MODE_INSERT, 0 },
2929 { "command", 0, command, ':', 0 },
2930 { "search", 0, command, '/', 0 },
2931 { "searchb", 0, command, '?', 0 },
2932 { "hinting", 0, command, '.', 0 },
2933 { "hinting_newtab", 0, command, ',', 0 },
2934 { "togglesrc", 0, toggle_src, 0, 0 },
2935 { "editsrc", 0, edit_src, 0, 0 },
2936 { "editelement", 0, edit_element, 0, 0 },
2938 /* yanking and pasting */
2939 { "yankuri", 0, yank_uri, 0, 0 },
2940 /* XXX: pasteuri{cur,new} do not work from the cmd_entry? */
2941 { "pasteuricur", 0, paste_uri, XT_PASTE_CURRENT_TAB, 0 },
2942 { "pasteurinew", 0, paste_uri, XT_PASTE_NEW_TAB, 0 },
2944 /* search */
2945 { "searchnext", 0, search, XT_SEARCH_NEXT, 0 },
2946 { "searchprevious", 0, search, XT_SEARCH_PREV, 0 },
2948 /* focus */
2949 { "focusaddress", 0, focus, XT_FOCUS_URI, 0 },
2950 { "focussearch", 0, focus, XT_FOCUS_SEARCH, 0 },
2952 /* hinting */
2953 { "hinting", 0, hint, 0, 0 },
2954 { "hinting_newtab", 0, hint, XT_HINT_NEWTAB, 0 },
2956 /* custom stylesheet */
2957 { "userstyle", 0, userstyle, XT_STYLE_CURRENT_TAB, 0 },
2958 { "userstyle_global", 0, userstyle, XT_STYLE_GLOBAL, 0 },
2960 /* navigation */
2961 { "goback", 0, navaction, XT_NAV_BACK, 0 },
2962 { "goforward", 0, navaction, XT_NAV_FORWARD, 0 },
2963 { "reload", 0, navaction, XT_NAV_RELOAD, 0 },
2964 { "stop", 0, navaction, XT_NAV_STOP, 0 },
2966 /* vertical movement */
2967 { "scrolldown", 0, move, XT_MOVE_DOWN, 0 },
2968 { "scrollup", 0, move, XT_MOVE_UP, 0 },
2969 { "scrollbottom", 0, move, XT_MOVE_BOTTOM, 0 },
2970 { "scrolltop", 0, move, XT_MOVE_TOP, 0 },
2971 { "1", 0, move, XT_MOVE_TOP, 0 },
2972 { "scrollhalfdown", 0, move, XT_MOVE_HALFDOWN, 0 },
2973 { "scrollhalfup", 0, move, XT_MOVE_HALFUP, 0 },
2974 { "scrollpagedown", 0, move, XT_MOVE_PAGEDOWN, 0 },
2975 { "scrollpageup", 0, move, XT_MOVE_PAGEUP, 0 },
2976 /* horizontal movement */
2977 { "scrollright", 0, move, XT_MOVE_RIGHT, 0 },
2978 { "scrollleft", 0, move, XT_MOVE_LEFT, 0 },
2979 { "scrollfarright", 0, move, XT_MOVE_FARRIGHT, 0 },
2980 { "scrollfarleft", 0, move, XT_MOVE_FARLEFT, 0 },
2982 { "favorites", 0, xtp_page_fl, 0, 0 },
2983 { "fav", 0, xtp_page_fl, 0, 0 },
2984 { "favadd", 0, add_favorite, 0, 0 },
2986 { "qall", 0, quit, 0, 0 },
2987 { "quitall", 0, quit, 0, 0 },
2988 { "w", 0, save_tabs, 0, 0 },
2989 { "wq", 0, save_tabs_and_quit, 0, 0 },
2990 { "help", 0, help, 0, 0 },
2991 { "about", 0, about, 0, 0 },
2992 { "stats", 0, stats, 0, 0 },
2993 { "version", 0, about, 0, 0 },
2995 /* js command */
2996 { "js", 0, js_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
2997 { "save", 1, js_cmd, XT_SAVE | XT_WL_FQDN, 0 },
2998 { "domain", 2, js_cmd, XT_SAVE | XT_WL_TOPLEVEL, 0 },
2999 { "fqdn", 2, js_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3000 { "show", 1, js_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3001 { "all", 2, js_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3002 { "persistent", 2, js_cmd, XT_SHOW | XT_WL_PERSISTENT, 0 },
3003 { "session", 2, js_cmd, XT_SHOW | XT_WL_SESSION, 0 },
3004 { "toggle", 1, js_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3005 { "domain", 2, js_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL, 0 },
3006 { "fqdn", 2, js_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3008 /* cookie command */
3009 { "cookie", 0, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3010 { "save", 1, cookie_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3011 { "domain", 2, cookie_cmd, XT_SAVE | XT_WL_TOPLEVEL, 0 },
3012 { "fqdn", 2, cookie_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3013 { "show", 1, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3014 { "all", 2, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3015 { "persistent", 2, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT, 0 },
3016 { "session", 2, cookie_cmd, XT_SHOW | XT_WL_SESSION, 0 },
3017 { "toggle", 1, cookie_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3018 { "domain", 2, cookie_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL, 0 },
3019 { "fqdn", 2, cookie_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3021 /* plugin command */
3022 { "plugin", 0, pl_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3023 { "save", 1, pl_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3024 { "domain", 2, pl_cmd, XT_SAVE | XT_WL_TOPLEVEL, 0 },
3025 { "fqdn", 2, pl_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3026 { "show", 1, pl_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3027 { "all", 2, pl_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3028 { "persistent", 2, pl_cmd, XT_SHOW | XT_WL_PERSISTENT, 0 },
3029 { "session", 2, pl_cmd, XT_SHOW | XT_WL_SESSION, 0 },
3030 { "toggle", 1, pl_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3031 { "domain", 2, pl_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL, 0 },
3032 { "fqdn", 2, pl_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3034 /* toplevel (domain) command */
3035 { "toplevel", 0, toplevel_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL | XT_WL_RELOAD, 0 },
3036 { "toggle", 1, toplevel_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL | XT_WL_RELOAD, 0 },
3038 /* cookie jar */
3039 { "cookiejar", 0, xtp_page_cl, 0, 0 },
3041 /* cert command */
3042 { "cert", 0, cert_cmd, XT_SHOW, 0 },
3043 { "save", 1, cert_cmd, XT_SAVE, 0 },
3044 { "show", 1, cert_cmd, XT_SHOW, 0 },
3046 { "ca", 0, ca_cmd, 0, 0 },
3047 { "downloadmgr", 0, xtp_page_dl, 0, 0 },
3048 { "dl", 0, xtp_page_dl, 0, 0 },
3049 { "h", 0, xtp_page_hl, 0, 0 },
3050 { "history", 0, xtp_page_hl, 0, 0 },
3051 { "home", 0, go_home, 0, 0 },
3052 { "restart", 0, restart, 0, 0 },
3053 { "urlhide", 0, urlaction, XT_URL_HIDE, 0 },
3054 { "urlshow", 0, urlaction, XT_URL_SHOW, 0 },
3055 { "statustoggle", 0, statustoggle, 0, 0 },
3056 { "run_script", 0, run_page_script, 0, XT_USERARG },
3058 { "print", 0, print_page, 0, 0 },
3060 /* tabs */
3061 { "focusin", 0, resizetab, XT_ZOOM_IN, 0 },
3062 { "focusout", 0, resizetab, XT_ZOOM_OUT, 0 },
3063 { "focusreset", 0, resizetab, XT_ZOOM_NORMAL, 0 },
3064 { "q", 0, tabaction, XT_TAB_DELQUIT, 0 },
3065 { "quit", 0, tabaction, XT_TAB_DELQUIT, 0 },
3066 { "open", 0, tabaction, XT_TAB_OPEN, XT_URLARG },
3067 { "tabclose", 0, tabaction, XT_TAB_DELETE, XT_PREFIX | XT_INTARG},
3068 { "tabedit", 0, tabaction, XT_TAB_NEW, XT_PREFIX | XT_URLARG },
3069 { "tabfirst", 0, movetab, XT_TAB_FIRST, 0 },
3070 { "tabhide", 0, tabaction, XT_TAB_HIDE, 0 },
3071 { "tablast", 0, movetab, XT_TAB_LAST, 0 },
3072 { "tabnew", 0, tabaction, XT_TAB_NEW, XT_PREFIX | XT_URLARG },
3073 { "tabnext", 0, movetab, XT_TAB_NEXT, XT_PREFIX | XT_INTARG},
3074 { "tabnextstyle", 0, tabaction, XT_TAB_NEXTSTYLE, 0 },
3075 { "tabprevious", 0, movetab, XT_TAB_PREV, XT_PREFIX | XT_INTARG},
3076 { "tabrewind", 0, movetab, XT_TAB_FIRST, 0 },
3077 { "tabshow", 0, tabaction, XT_TAB_SHOW, 0 },
3078 { "tabs", 0, buffers, 0, 0 },
3079 { "tabundoclose", 0, tabaction, XT_TAB_UNDO_CLOSE, 0 },
3080 { "buffers", 0, buffers, 0, 0 },
3081 { "ls", 0, buffers, 0, 0 },
3082 { "encoding", 0, set_encoding, 0, XT_USERARG },
3083 { "loadimages", 0, tabaction, XT_TAB_LOAD_IMAGES, 0 },
3085 /* command aliases (handy when -S flag is used) */
3086 { "promptopen", 0, command, XT_CMD_OPEN, 0 },
3087 { "promptopencurrent", 0, command, XT_CMD_OPEN_CURRENT, 0 },
3088 { "prompttabnew", 0, command, XT_CMD_TABNEW, 0 },
3089 { "prompttabnewcurrent",0, command, XT_CMD_TABNEW_CURRENT, 0 },
3091 /* settings */
3092 { "set", 0, set, 0, XT_SETARG },
3094 { "fullscreen", 0, fullscreen, 0, 0 },
3095 { "f", 0, fullscreen, 0, 0 },
3097 /* sessions */
3098 { "session", 0, session_cmd, XT_SHOW, 0 },
3099 { "delete", 1, session_cmd, XT_DELETE, XT_SESSARG },
3100 { "open", 1, session_cmd, XT_OPEN, XT_SESSARG },
3101 { "save", 1, session_cmd, XT_SAVE, XT_USERARG },
3102 { "show", 1, session_cmd, XT_SHOW, 0 },
3104 /* external javascript */
3105 { "script", 0, script_cmd, XT_EJS_SHOW, XT_USERARG },
3107 /* inspector */
3108 { "inspector", 0, inspector_cmd, XT_INS_SHOW, 0 },
3109 { "show", 1, inspector_cmd, XT_INS_SHOW, 0 },
3110 { "hide", 1, inspector_cmd, XT_INS_HIDE, 0 },
3112 /* proxy */
3113 { "proxy", 0, proxy_cmd, XT_PRXY_SHOW, 0 },
3114 { "show", 1, proxy_cmd, XT_PRXY_SHOW, 0 },
3115 { "toggle", 1, proxy_cmd, XT_PRXY_TOGGLE, 0 },
3118 struct {
3119 int index;
3120 int len;
3121 gchar *list[256];
3122 } cmd_status = {-1, 0};
3124 gboolean
3125 wv_release_button_cb(GtkWidget *btn, GdkEventButton *e, struct tab *t)
3128 if (e->type == GDK_BUTTON_RELEASE && e->button == 1)
3129 btn_down = 0;
3131 return (FALSE);
3134 gboolean
3135 wv_button_cb(GtkWidget *btn, GdkEventButton *e, struct tab *t)
3137 struct karg a;
3138 WebKitHitTestResult *hit_test_result;
3139 guint context;
3141 hit_test_result = webkit_web_view_get_hit_test_result(t->wv, e);
3142 g_object_get(hit_test_result, "context", &context, NULL);
3144 hide_oops(t);
3145 hide_buffers(t);
3147 if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE)
3148 t->mode = XT_MODE_INSERT;
3149 else
3150 t->mode = XT_MODE_COMMAND;
3152 if (e->type == GDK_BUTTON_PRESS && e->button == 1)
3153 btn_down = 1;
3154 else if (e->type == GDK_BUTTON_PRESS && e->button == 8 /* btn 4 */) {
3155 /* go backward */
3156 a.i = XT_NAV_BACK;
3157 navaction(t, &a);
3159 return (TRUE);
3160 } else if (e->type == GDK_BUTTON_PRESS && e->button == 9 /* btn 5 */) {
3161 /* go forward */
3162 a.i = XT_NAV_FORWARD;
3163 navaction(t, &a);
3165 return (TRUE);
3168 return (FALSE);
3171 gboolean
3172 tab_close_cb(GtkWidget *btn, GdkEventButton *e, struct tab *t)
3174 DNPRINTF(XT_D_TAB, "tab_close_cb: tab %d\n", t->tab_id);
3176 if (e->type == GDK_BUTTON_PRESS && e->button == 1)
3177 delete_tab(t);
3179 return (FALSE);
3184 void
3185 activate_uri_entry_cb(GtkWidget* entry, struct tab *t)
3187 const gchar *uri = gtk_entry_get_text(GTK_ENTRY(entry));
3189 DNPRINTF(XT_D_URL, "activate_uri_entry_cb: %s\n", uri);
3191 if (t == NULL) {
3192 show_oops(NULL, "activate_uri_entry_cb invalid parameters");
3193 return;
3196 if (uri == NULL) {
3197 show_oops(t, "activate_uri_entry_cb no uri");
3198 return;
3201 uri += strspn(uri, "\t ");
3203 /* if xxxt:// treat specially */
3204 if (parse_xtp_url(t, uri))
3205 return;
3207 /* otherwise continue to load page normally */
3208 load_uri(t, (gchar *)uri);
3209 focus_webview(t);
3212 void
3213 activate_search_entry_cb(GtkWidget* entry, struct tab *t)
3215 const gchar *search = gtk_entry_get_text(GTK_ENTRY(entry));
3216 char *newuri = NULL;
3217 gchar *enc_search;
3219 DNPRINTF(XT_D_URL, "activate_search_entry_cb: %s\n", search);
3221 if (t == NULL) {
3222 show_oops(NULL, "activate_search_entry_cb invalid parameters");
3223 return;
3226 if (search_string == NULL) {
3227 show_oops(t, "no search_string");
3228 return;
3231 t->xtp_meaning = XT_XTP_TAB_MEANING_NORMAL;
3233 enc_search = soup_uri_encode(search, XT_RESERVED_CHARS);
3234 newuri = g_strdup_printf(search_string, enc_search);
3235 g_free(enc_search);
3237 marks_clear(t);
3238 webkit_web_view_load_uri(t->wv, newuri);
3239 focus_webview(t);
3241 if (newuri)
3242 g_free(newuri);
3245 void
3246 check_and_set_cookie(const gchar *uri, struct tab *t)
3248 struct domain *d = NULL;
3249 int es = 0;
3251 if (uri == NULL || t == NULL)
3252 return;
3254 if ((d = wl_find_uri(uri, &c_wl)) == NULL)
3255 es = 0;
3256 else
3257 es = 1;
3259 DNPRINTF(XT_D_COOKIE, "check_and_set_cookie: %s %s\n",
3260 es ? "enable" : "disable", uri);
3262 g_object_set(G_OBJECT(t->settings),
3263 "enable-html5-local-storage", es, (char *)NULL);
3264 webkit_web_view_set_settings(t->wv, t->settings);
3267 void
3268 check_and_set_js(const gchar *uri, struct tab *t)
3270 struct domain *d = NULL;
3271 int es = 0;
3273 if (uri == NULL || t == NULL)
3274 return;
3276 if ((d = wl_find_uri(uri, &js_wl)) == NULL)
3277 es = 0;
3278 else
3279 es = 1;
3281 DNPRINTF(XT_D_JS, "check_and_set_js: %s %s\n",
3282 es ? "enable" : "disable", uri);
3284 g_object_set(G_OBJECT(t->settings),
3285 "enable-scripts", es, (char *)NULL);
3286 g_object_set(G_OBJECT(t->settings),
3287 "javascript-can-open-windows-automatically", es, (char *)NULL);
3288 webkit_web_view_set_settings(t->wv, t->settings);
3290 button_set_stockid(t->js_toggle,
3291 es ? GTK_STOCK_MEDIA_PLAY : GTK_STOCK_MEDIA_PAUSE);
3294 void
3295 check_and_set_pl(const gchar *uri, struct tab *t)
3297 struct domain *d = NULL;
3298 int es = 0;
3300 if (uri == NULL || t == NULL)
3301 return;
3303 if ((d = wl_find_uri(uri, &pl_wl)) == NULL)
3304 es = 0;
3305 else
3306 es = 1;
3308 DNPRINTF(XT_D_JS, "check_and_set_pl: %s %s\n",
3309 es ? "enable" : "disable", uri);
3311 g_object_set(G_OBJECT(t->settings),
3312 "enable-plugins", es, (char *)NULL);
3313 webkit_web_view_set_settings(t->wv, t->settings);
3316 void
3317 color_address_bar(gpointer p)
3319 GdkColor color;
3320 struct tab *tt, *t = p;
3321 gchar *col_str = XT_COLOR_WHITE;
3322 const gchar *uri, *u = NULL, *error_str = NULL;
3324 #ifdef USE_THREADS
3325 gdk_threads_enter();
3326 #endif
3327 DNPRINTF(XT_D_URL, "%s:\n", __func__);
3329 /* make sure t still exists */
3330 if (t == NULL)
3331 return;
3332 TAILQ_FOREACH(tt, &tabs, entry)
3333 if (t == tt)
3334 break;
3335 if (t != tt)
3336 goto done;
3338 if ((uri = get_uri(t)) == NULL)
3339 goto white;
3340 u = g_strdup(uri);
3342 #ifdef USE_THREADS
3343 gdk_threads_leave();
3344 #endif
3346 col_str = XT_COLOR_YELLOW;
3347 switch (load_compare_cert(u, &error_str)) {
3348 case CERT_LOCAL:
3349 col_str = XT_COLOR_BLUE;
3350 break;
3351 case CERT_TRUSTED:
3352 col_str = XT_COLOR_GREEN;
3353 break;
3354 case CERT_UNTRUSTED:
3355 col_str = XT_COLOR_YELLOW;
3356 break;
3357 case CERT_BAD:
3358 col_str = XT_COLOR_RED;
3359 break;
3362 #ifdef USE_THREADS
3363 gdk_threads_enter();
3364 #endif
3365 /* make sure t isn't deleted */
3366 TAILQ_FOREACH(tt, &tabs, entry)
3367 if (t == tt)
3368 break;
3369 if (t != tt)
3370 goto done;
3372 #ifdef USE_THREADS
3373 /* test to see if the user navigated away and canceled the thread */
3374 if (t->thread != g_thread_self())
3375 goto done;
3376 if ((uri = get_uri(t)) == NULL) {
3377 t->thread = NULL;
3378 goto done;
3380 if (strcmp(uri, u)) {
3381 /* make sure we are still the same url */
3382 t->thread = NULL;
3383 goto done;
3385 #endif
3386 white:
3387 gdk_color_parse(col_str, &color);
3388 gtk_widget_modify_base(t->uri_entry, GTK_STATE_NORMAL, &color);
3390 if (!strcmp(col_str, XT_COLOR_WHITE))
3391 statusbar_modify_attr(t, col_str, XT_COLOR_BLACK);
3392 else
3393 statusbar_modify_attr(t, XT_COLOR_BLACK, col_str);
3395 if (error_str && error_str[0] != '\0')
3396 show_oops(t, "%s", error_str);
3397 #ifdef USE_THREADS
3398 t->thread = NULL;
3399 #endif
3400 done:
3401 /* t is invalid at this point */
3402 if (u)
3403 g_free((gpointer)u);
3404 #ifdef USE_THREADS
3405 gdk_threads_leave();
3406 #endif
3409 void
3410 show_ca_status(struct tab *t, const char *uri)
3412 GdkColor color;
3413 gchar *col_str = XT_COLOR_WHITE;
3415 DNPRINTF(XT_D_URL, "show_ca_status: %d %s %s\n",
3416 ssl_strict_certs, ssl_ca_file, uri);
3418 if (t == NULL)
3419 return;
3421 if (uri == NULL)
3422 goto done;
3423 if (ssl_ca_file == NULL) {
3424 if (g_str_has_prefix(uri, "http://"))
3425 goto done;
3426 if (g_str_has_prefix(uri, "https://")) {
3427 col_str = XT_COLOR_RED;
3428 goto done;
3430 return;
3432 if (g_str_has_prefix(uri, "http://") ||
3433 !g_str_has_prefix(uri, "https://"))
3434 goto done;
3435 #ifdef USE_THREADS
3437 * It is not necessary to see if the thread is already running.
3438 * If the thread is in progress setting it to something else aborts it
3439 * on the way out.
3442 /* thread the coloring of the address bar */
3443 t->thread = g_thread_create((GThreadFunc)color_address_bar, t, TRUE, NULL);
3444 #else
3445 color_address_bar(t);
3446 #endif
3447 return;
3449 done:
3450 if (col_str) {
3451 gdk_color_parse(col_str, &color);
3452 gtk_widget_modify_base(t->uri_entry, GTK_STATE_NORMAL, &color);
3454 if (!strcmp(col_str, XT_COLOR_WHITE))
3455 statusbar_modify_attr(t, col_str, XT_COLOR_BLACK);
3456 else
3457 statusbar_modify_attr(t, XT_COLOR_BLACK, col_str);
3461 void
3462 free_favicon(struct tab *t)
3464 DNPRINTF(XT_D_DOWNLOAD, "%s: down %p req %p\n",
3465 __func__, t->icon_download, t->icon_request);
3467 if (t->icon_request)
3468 g_object_unref(t->icon_request);
3469 if (t->icon_dest_uri)
3470 g_free(t->icon_dest_uri);
3472 t->icon_request = NULL;
3473 t->icon_dest_uri = NULL;
3476 void
3477 xt_icon_from_name(struct tab *t, gchar *name)
3479 if (!enable_favicon_entry)
3480 return;
3482 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->uri_entry),
3483 GTK_ENTRY_ICON_PRIMARY, "text-html");
3484 if (show_url == 0)
3485 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.statusbar),
3486 GTK_ENTRY_ICON_PRIMARY, "text-html");
3487 else
3488 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.statusbar),
3489 GTK_ENTRY_ICON_PRIMARY, NULL);
3492 void
3493 xt_icon_from_pixbuf(struct tab *t, GdkPixbuf *pb)
3495 GdkPixbuf *pb_scaled;
3497 if (gdk_pixbuf_get_width(pb) > 16 || gdk_pixbuf_get_height(pb) > 16)
3498 pb_scaled = gdk_pixbuf_scale_simple(pb, 16, 16,
3499 GDK_INTERP_BILINEAR);
3500 else
3501 pb_scaled = pb;
3503 if (enable_favicon_entry) {
3505 /* Classic tabs. */
3506 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t->uri_entry),
3507 GTK_ENTRY_ICON_PRIMARY, pb_scaled);
3509 /* Minimal tabs. */
3510 if (show_url == 0) {
3511 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t->sbe.statusbar),
3512 GTK_ENTRY_ICON_PRIMARY, pb_scaled);
3513 } else
3514 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.statusbar),
3515 GTK_ENTRY_ICON_PRIMARY, NULL);
3518 /* XXX: Only supports the minimal tabs atm. */
3519 if (enable_favicon_tabs)
3520 gtk_image_set_from_pixbuf(GTK_IMAGE(t->tab_elems.favicon),
3521 pb_scaled);
3523 if (pb_scaled != pb)
3524 g_object_unref(pb_scaled);
3527 void
3528 xt_icon_from_file(struct tab *t, char *file)
3530 GdkPixbuf *pb;
3532 if (g_str_has_prefix(file, "file://"))
3533 file += strlen("file://");
3535 pb = gdk_pixbuf_new_from_file(file, NULL);
3536 if (pb) {
3537 xt_icon_from_pixbuf(t, pb);
3538 g_object_unref(pb);
3539 } else
3540 xt_icon_from_name(t, "text-html");
3543 gboolean
3544 is_valid_icon(char *file)
3546 gboolean valid = 0;
3547 const char *mime_type;
3548 GFileInfo *fi;
3549 GFile *gf;
3551 gf = g_file_new_for_path(file);
3552 fi = g_file_query_info(gf, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, 0,
3553 NULL, NULL);
3554 mime_type = g_file_info_get_content_type(fi);
3555 valid = g_strcmp0(mime_type, "image/x-ico") == 0 ||
3556 g_strcmp0(mime_type, "image/vnd.microsoft.icon") == 0 ||
3557 g_strcmp0(mime_type, "image/png") == 0 ||
3558 g_strcmp0(mime_type, "image/gif") == 0 ||
3559 g_strcmp0(mime_type, "application/octet-stream") == 0;
3560 g_object_unref(fi);
3561 g_object_unref(gf);
3563 return (valid);
3566 void
3567 set_favicon_from_file(struct tab *t, char *file)
3569 struct stat sb;
3571 if (t == NULL || file == NULL)
3572 return;
3574 if (g_str_has_prefix(file, "file://"))
3575 file += strlen("file://");
3576 DNPRINTF(XT_D_DOWNLOAD, "%s: loading %s\n", __func__, file);
3578 if (!stat(file, &sb)) {
3579 if (sb.st_size == 0 || !is_valid_icon(file)) {
3580 /* corrupt icon so trash it */
3581 DNPRINTF(XT_D_DOWNLOAD, "%s: corrupt icon %s\n",
3582 __func__, file);
3583 unlink(file);
3584 /* no need to set icon to default here */
3585 return;
3588 xt_icon_from_file(t, file);
3591 void
3592 favicon_download_status_changed_cb(WebKitDownload *download, GParamSpec *spec,
3593 WebKitWebView *wv)
3595 WebKitDownloadStatus status = webkit_download_get_status(download);
3596 struct tab *tt = NULL, *t = NULL;
3599 * find the webview instead of passing in the tab as it could have been
3600 * deleted from underneath us.
3602 TAILQ_FOREACH(tt, &tabs, entry) {
3603 if (tt->wv == wv) {
3604 t = tt;
3605 break;
3608 if (t == NULL)
3609 return;
3611 DNPRINTF(XT_D_DOWNLOAD, "%s: tab %d status %d\n",
3612 __func__, t->tab_id, status);
3614 switch (status) {
3615 case WEBKIT_DOWNLOAD_STATUS_ERROR:
3616 /* -1 */
3617 t->icon_download = NULL;
3618 free_favicon(t);
3619 break;
3620 case WEBKIT_DOWNLOAD_STATUS_CREATED:
3621 /* 0 */
3622 break;
3623 case WEBKIT_DOWNLOAD_STATUS_STARTED:
3624 /* 1 */
3625 break;
3626 case WEBKIT_DOWNLOAD_STATUS_CANCELLED:
3627 /* 2 */
3628 DNPRINTF(XT_D_DOWNLOAD, "%s: freeing favicon %d\n",
3629 __func__, t->tab_id);
3630 t->icon_download = NULL;
3631 free_favicon(t);
3632 break;
3633 case WEBKIT_DOWNLOAD_STATUS_FINISHED:
3634 /* 3 */
3636 DNPRINTF(XT_D_DOWNLOAD, "%s: setting icon to %s\n",
3637 __func__, t->icon_dest_uri);
3638 set_favicon_from_file(t, t->icon_dest_uri);
3639 /* these will be freed post callback */
3640 t->icon_request = NULL;
3641 t->icon_download = NULL;
3642 break;
3643 default:
3644 break;
3648 void
3649 abort_favicon_download(struct tab *t)
3651 DNPRINTF(XT_D_DOWNLOAD, "%s: down %p\n", __func__, t->icon_download);
3653 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
3654 if (t->icon_download) {
3655 g_signal_handlers_disconnect_by_func(G_OBJECT(t->icon_download),
3656 G_CALLBACK(favicon_download_status_changed_cb), t->wv);
3657 webkit_download_cancel(t->icon_download);
3658 t->icon_download = NULL;
3659 } else
3660 free_favicon(t);
3661 #endif
3663 xt_icon_from_name(t, "text-html");
3666 void
3667 notify_icon_loaded_cb(WebKitWebView *wv, gchar *uri, struct tab *t)
3669 DNPRINTF(XT_D_DOWNLOAD, "%s %s\n", __func__, uri);
3671 if (uri == NULL || t == NULL)
3672 return;
3674 #if WEBKIT_CHECK_VERSION(1, 4, 0)
3675 /* take icon from WebKitIconDatabase */
3676 GdkPixbuf *pb;
3678 pb = webkit_web_view_get_icon_pixbuf(wv);
3679 if (pb) {
3680 xt_icon_from_pixbuf(t, pb);
3681 g_object_unref(pb);
3682 } else
3683 xt_icon_from_name(t, "text-html");
3684 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
3685 /* download icon to cache dir */
3686 gchar *name_hash, file[PATH_MAX];
3687 struct stat sb;
3689 if (t->icon_request) {
3690 DNPRINTF(XT_D_DOWNLOAD, "%s: download in progress\n", __func__);
3691 return;
3694 /* check to see if we got the icon in cache */
3695 name_hash = g_compute_checksum_for_string(G_CHECKSUM_SHA256, uri, -1);
3696 snprintf(file, sizeof file, "%s/%s.ico", cache_dir, name_hash);
3697 g_free(name_hash);
3699 if (!stat(file, &sb)) {
3700 if (sb.st_size > 0) {
3701 DNPRINTF(XT_D_DOWNLOAD, "%s: loading from cache %s\n",
3702 __func__, file);
3703 set_favicon_from_file(t, file);
3704 return;
3707 /* corrupt icon so trash it */
3708 DNPRINTF(XT_D_DOWNLOAD, "%s: corrupt icon %s\n",
3709 __func__, file);
3710 unlink(file);
3713 /* create download for icon */
3714 t->icon_request = webkit_network_request_new(uri);
3715 if (t->icon_request == NULL) {
3716 DNPRINTF(XT_D_DOWNLOAD, "%s: invalid uri %s\n",
3717 __func__, uri);
3718 return;
3721 t->icon_download = webkit_download_new(t->icon_request);
3722 if (t->icon_download == NULL)
3723 return;
3725 /* we have to free icon_dest_uri later */
3726 t->icon_dest_uri = g_strdup_printf("file://%s", file);
3727 webkit_download_set_destination_uri(t->icon_download,
3728 t->icon_dest_uri);
3730 if (webkit_download_get_status(t->icon_download) ==
3731 WEBKIT_DOWNLOAD_STATUS_ERROR) {
3732 g_object_unref(t->icon_request);
3733 g_free(t->icon_dest_uri);
3734 t->icon_request = NULL;
3735 t->icon_dest_uri = NULL;
3736 return;
3739 g_signal_connect(G_OBJECT(t->icon_download), "notify::status",
3740 G_CALLBACK(favicon_download_status_changed_cb), t->wv);
3742 webkit_download_start(t->icon_download);
3743 #endif
3746 void
3747 notify_load_status_cb(WebKitWebView* wview, GParamSpec* pspec, struct tab *t)
3749 const gchar *uri = NULL;
3750 struct history *h, find;
3751 struct karg a;
3752 GdkColor color;
3754 DNPRINTF(XT_D_URL, "notify_load_status_cb: %d %s\n",
3755 webkit_web_view_get_load_status(wview),
3756 get_uri(t) ? get_uri(t) : "NOTHING");
3758 if (t == NULL) {
3759 show_oops(NULL, "notify_load_status_cb invalid parameters");
3760 return;
3763 switch (webkit_web_view_get_load_status(wview)) {
3764 case WEBKIT_LOAD_PROVISIONAL:
3765 /* 0 */
3766 abort_favicon_download(t);
3767 #if GTK_CHECK_VERSION(2, 20, 0)
3768 gtk_widget_show(t->spinner);
3769 gtk_spinner_start(GTK_SPINNER(t->spinner));
3770 #endif
3771 gtk_label_set_text(GTK_LABEL(t->label), "Loading");
3773 gtk_widget_set_sensitive(GTK_WIDGET(t->stop), TRUE);
3775 /* assume we are a new address */
3776 gdk_color_parse("white", &color);
3777 gtk_widget_modify_base(t->uri_entry, GTK_STATE_NORMAL, &color);
3778 statusbar_modify_attr(t, "white", XT_COLOR_BLACK);
3780 /* take focus if we are visible */
3781 focus_webview(t);
3782 t->focus_wv = 1;
3784 marks_clear(t);
3785 #ifdef USE_THREAD
3786 /* kill color thread */
3787 t->thread = NULL;
3788 #endif
3789 break;
3791 case WEBKIT_LOAD_COMMITTED:
3792 /* 1 */
3793 uri = get_uri(t);
3794 if (uri == NULL)
3795 return;
3796 gtk_entry_set_text(GTK_ENTRY(t->uri_entry), uri);
3798 if (t->status) {
3799 g_free(t->status);
3800 t->status = NULL;
3802 set_status(t, (char *)uri, XT_STATUS_LOADING);
3804 /* check if js white listing is enabled */
3805 if (enable_plugin_whitelist)
3806 check_and_set_pl(uri, t);
3807 if (enable_cookie_whitelist)
3808 check_and_set_cookie(uri, t);
3809 if (enable_js_whitelist)
3810 check_and_set_js(uri, t);
3812 if (t->styled)
3813 apply_style(t);
3816 /* we know enough to autosave the session */
3817 if (session_autosave) {
3818 a.s = NULL;
3819 save_tabs(t, &a);
3822 show_ca_status(t, uri);
3823 run_script(t, JS_HINTING);
3824 if (enable_autoscroll)
3825 run_script(t, JS_AUTOSCROLL);
3826 break;
3828 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT:
3829 /* 3 */
3830 if (color_visited_uris) {
3831 color_visited(t, color_visited_helper());
3833 /* This colors the links you middle-click (open in new
3834 * tab) in the current tab. */
3835 if (t->tab_id != gtk_notebook_get_current_page(notebook) &&
3836 (uri = get_uri(t)) != NULL)
3837 color_visited(get_current_tab(),
3838 g_strdup_printf("{'%s' : 'dummy'}", uri));
3840 break;
3842 case WEBKIT_LOAD_FINISHED:
3843 /* 2 */
3844 if ((uri = get_uri(t)) == NULL)
3845 return;
3847 if (!strncmp(uri, "http://", strlen("http://")) ||
3848 !strncmp(uri, "https://", strlen("https://")) ||
3849 !strncmp(uri, "file://", strlen("file://"))) {
3850 find.uri = (gchar *)uri;
3851 h = RB_FIND(history_list, &hl, &find);
3852 if (!h)
3853 insert_history_item(uri,
3854 get_title(t, FALSE), time(NULL));
3855 else
3856 h->time = time(NULL);
3859 set_status(t, (char *)uri, XT_STATUS_URI);
3860 #if WEBKIT_CHECK_VERSION(1, 1, 18)
3861 case WEBKIT_LOAD_FAILED:
3862 /* 4 */
3863 #endif
3864 #if GTK_CHECK_VERSION(2, 20, 0)
3865 gtk_spinner_stop(GTK_SPINNER(t->spinner));
3866 gtk_widget_hide(t->spinner);
3867 #endif
3868 default:
3869 gtk_widget_set_sensitive(GTK_WIDGET(t->stop), FALSE);
3870 break;
3873 if (t->item)
3874 gtk_widget_set_sensitive(GTK_WIDGET(t->backward), TRUE);
3875 else
3876 gtk_widget_set_sensitive(GTK_WIDGET(t->backward),
3877 can_go_back_for_real(t));
3879 gtk_widget_set_sensitive(GTK_WIDGET(t->forward),
3880 can_go_forward_for_real(t));
3883 void
3884 notify_title_cb(WebKitWebView* wview, GParamSpec* pspec, struct tab *t)
3886 const gchar *title = NULL, *win_title = NULL;
3888 title = get_title(t, FALSE);
3889 win_title = get_title(t, TRUE);
3890 if (title) {
3891 gtk_label_set_text(GTK_LABEL(t->label), title);
3892 gtk_label_set_text(GTK_LABEL(t->tab_elems.label), title);
3895 if (win_title && t->tab_id == gtk_notebook_get_current_page(notebook))
3896 gtk_window_set_title(GTK_WINDOW(main_window), win_title);
3899 char *
3900 get_domain(const gchar *host)
3902 size_t x;
3903 char *p;
3905 /* handle silly domains like .co.uk */
3907 if ((x = strlen(host)) <= 6)
3908 return (g_strdup(host));
3910 if (host[x - 3] == '.' && host[x - 6] == '.') {
3911 x = x - 7;
3912 while (x > 0)
3913 if (host[x] != '.')
3914 x--;
3915 else
3916 return (g_strdup(&host[x + 1]));
3919 /* find first . */
3920 p = g_strrstr(host, ".");
3921 if (p == NULL)
3922 return (g_strdup(""));
3923 p--;
3924 while (p > host)
3925 if (*p != '.')
3926 p--;
3927 else
3928 return (g_strdup(p + 1));
3930 return (g_strdup(host));
3933 void
3934 js_autorun(struct tab *t)
3936 SoupURI *su = NULL;
3937 const gchar *uri;
3938 size_t got_default = 0, got_host = 0;
3939 struct stat sb;
3940 char deff[PATH_MAX], hostf[PATH_MAX];
3941 char *js = NULL, *jsat, *domain = NULL;
3942 FILE *deffile = NULL, *hostfile = NULL;
3944 if (js_autorun_enabled == 0)
3945 return;
3947 uri = get_uri(t);
3948 if (uri &&
3949 !(g_str_has_prefix(uri, "http://") ||
3950 g_str_has_prefix(uri, "https://")))
3951 goto done;
3953 su = soup_uri_new(uri);
3954 if (su == NULL)
3955 goto done;
3956 if (!SOUP_URI_VALID_FOR_HTTP(su))
3957 goto done;
3959 DNPRINTF(XT_D_JS, "%s: host: %s domain: %s\n", __func__,
3960 su->host, domain);
3961 domain = get_domain(su->host);
3963 snprintf(deff, sizeof deff, "%s/default.js", js_dir);
3964 if ((deffile = fopen(deff, "r")) != NULL) {
3965 if (fstat(fileno(deffile), &sb) == -1) {
3966 show_oops(t, "can't stat default JS file");
3967 goto done;
3969 got_default = sb.st_size;
3972 /* try host first followed by domain */
3973 snprintf(hostf, sizeof hostf, "%s/%s.js", js_dir, su->host);
3974 DNPRINTF(XT_D_JS, "trying file: %s\n", hostf);
3975 if ((hostfile = fopen(hostf, "r")) == NULL) {
3976 snprintf(hostf, sizeof hostf, "%s/%s.js", js_dir, domain);
3977 DNPRINTF(XT_D_JS, "trying file: %s\n", hostf);
3978 if ((hostfile = fopen(hostf, "r")) == NULL)
3979 goto nofile;
3981 DNPRINTF(XT_D_JS, "file: %s\n", hostf);
3982 if (fstat(fileno(hostfile), &sb) == -1) {
3983 show_oops(t, "can't stat %s JS file", hostf);
3984 goto done;
3986 got_host = sb.st_size;
3988 nofile:
3989 if (got_default + got_host == 0)
3990 goto done;
3992 js = g_malloc0(got_default + got_host + 1);
3993 jsat = js;
3995 if (got_default) {
3996 if (fread(js, got_default, 1, deffile) != 1) {
3997 show_oops(t, "default file read error");
3998 goto done;
4000 jsat = js + got_default;
4003 if (got_host) {
4004 if (fread(jsat, got_host, 1, hostfile) != 1) {
4005 show_oops(t, "host file read error");
4006 goto done;
4010 DNPRINTF(XT_D_JS, "%s: about to run script\n", __func__);
4011 run_script(t, js);
4013 done:
4014 if (su)
4015 soup_uri_free(su);
4016 if (js)
4017 g_free(js);
4018 if (deffile)
4019 fclose(deffile);
4020 if (hostfile)
4021 fclose(hostfile);
4022 if (domain)
4023 g_free(domain);
4026 void
4027 webview_load_finished_cb(WebKitWebView *wv, WebKitWebFrame *wf, struct tab *t)
4029 /* autorun some js if enabled */
4030 js_autorun(t);
4032 input_autofocus(t);
4036 void
4037 webview_progress_changed_cb(WebKitWebView *wv, int progress, struct tab *t)
4039 gtk_entry_set_progress_fraction(GTK_ENTRY(t->uri_entry),
4040 progress == 100 ? 0 : (double)progress / 100);
4041 if (show_url == 0) {
4042 gtk_entry_set_progress_fraction(GTK_ENTRY(t->sbe.statusbar),
4043 progress == 100 ? 0 : (double)progress / 100);
4046 update_statusbar_position(NULL, NULL);
4050 webview_npd_cb(WebKitWebView *wv, WebKitWebFrame *wf,
4051 WebKitNetworkRequest *request, WebKitWebNavigationAction *na,
4052 WebKitWebPolicyDecision *pd, struct tab *t)
4054 char *uri;
4055 WebKitWebNavigationReason reason;
4056 struct domain *d = NULL;
4058 if (t == NULL) {
4059 show_oops(NULL, "webview_npd_cb invalid parameters");
4060 return (FALSE);
4063 DNPRINTF(XT_D_NAV, "webview_npd_cb: ctrl_click %d %s\n",
4064 t->ctrl_click,
4065 webkit_network_request_get_uri(request));
4067 uri = (char *)webkit_network_request_get_uri(request);
4069 if (!auto_load_images && t->load_images) {
4071 /* Disable autoloading of images, now that we're done loading
4072 * them. */
4073 g_object_set(G_OBJECT(t->settings),
4074 "auto-load-images", FALSE, (char *)NULL);
4075 webkit_web_view_set_settings(t->wv, t->settings);
4077 t->load_images = FALSE;
4080 /* If this is an xtp url, we don't load anything else. */
4081 if (parse_xtp_url(t, uri))
4082 return (TRUE);
4084 if ((t->mode == XT_MODE_HINT && t->new_tab) || t->ctrl_click) {
4085 t->ctrl_click = 0;
4086 create_new_tab(uri, NULL, ctrl_click_focus, -1);
4087 webkit_web_policy_decision_ignore(pd);
4088 return (TRUE); /* we made the decission */
4091 /* Change user agent if more than one has been given. */
4092 if (user_agent_count > 1) {
4093 struct user_agent *ua;
4095 if ((ua = TAILQ_NEXT(user_agent, entry)) == NULL)
4096 user_agent = TAILQ_FIRST(&ua_list);
4097 else
4098 user_agent = ua;
4100 free(t->user_agent);
4101 t->user_agent = g_strdup(user_agent->value);
4103 DNPRINTF(XT_D_NAV, "user-agent: %s\n", t->user_agent);
4105 g_object_set(G_OBJECT(t->settings),
4106 "user-agent", t->user_agent, (char *)NULL);
4108 webkit_web_view_set_settings(wv, t->settings);
4112 * This is a little hairy but it comes down to this:
4113 * when we run in whitelist mode we have to assist the browser in
4114 * opening the URL that it would have opened in a new tab.
4116 reason = webkit_web_navigation_action_get_reason(na);
4117 if (reason == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
4118 t->xtp_meaning = XT_XTP_TAB_MEANING_NORMAL;
4119 if (enable_scripts == 0 && enable_cookie_whitelist == 1)
4120 if (uri && (d = wl_find_uri(uri, &js_wl)) == NULL)
4121 load_uri(t, uri);
4122 webkit_web_policy_decision_use(pd);
4123 return (TRUE); /* we made the decision */
4126 return (FALSE);
4129 WebKitWebView *
4130 webview_cwv_cb(WebKitWebView *wv, WebKitWebFrame *wf, struct tab *t)
4132 struct tab *tt;
4133 struct domain *d = NULL;
4134 const gchar *uri;
4135 WebKitWebView *webview = NULL;
4136 int x = 1;
4138 DNPRINTF(XT_D_NAV, "webview_cwv_cb: %s\n",
4139 webkit_web_view_get_uri(wv));
4141 if (tabless) {
4142 /* open in current tab */
4143 webview = t->wv;
4144 } else if (enable_scripts == 0 && enable_js_whitelist == 1) {
4145 uri = webkit_web_view_get_uri(wv);
4146 if (uri && (d = wl_find_uri(uri, &js_wl)) == NULL)
4147 return (NULL);
4149 if (t->ctrl_click) {
4150 x = ctrl_click_focus;
4151 t->ctrl_click = 0;
4153 tt = create_new_tab(NULL, NULL, x, -1);
4154 webview = tt->wv;
4155 } else if (enable_scripts == 1) {
4156 if (t->ctrl_click) {
4157 x = ctrl_click_focus;
4158 t->ctrl_click = 0;
4160 tt = create_new_tab(NULL, NULL, x, -1);
4161 webview = tt->wv;
4164 return (webview);
4167 gboolean
4168 webview_closewv_cb(WebKitWebView *wv, struct tab *t)
4170 const gchar *uri;
4171 struct domain *d = NULL;
4173 DNPRINTF(XT_D_NAV, "webview_close_cb: %d\n", t->tab_id);
4175 if (enable_scripts == 0 && enable_cookie_whitelist == 1) {
4176 uri = webkit_web_view_get_uri(wv);
4177 if (uri && (d = wl_find_uri(uri, &js_wl)) == NULL)
4178 return (FALSE);
4180 delete_tab(t);
4181 } else if (enable_scripts == 1)
4182 delete_tab(t);
4184 return (TRUE);
4188 webview_event_cb(GtkWidget *w, GdkEventButton *e, struct tab *t)
4190 /* we can not eat the event without throwing gtk off so defer it */
4192 /* catch middle click */
4193 if (e->type == GDK_BUTTON_RELEASE && e->button == 2) {
4194 t->ctrl_click = 1;
4195 goto done;
4198 /* catch ctrl click */
4199 if (e->type == GDK_BUTTON_RELEASE &&
4200 CLEAN(e->state) == GDK_CONTROL_MASK)
4201 t->ctrl_click = 1;
4202 else
4203 t->ctrl_click = 0;
4204 done:
4205 return (XT_CB_PASSTHROUGH);
4209 run_mimehandler(struct tab *t, char *mime_type, WebKitNetworkRequest *request)
4211 struct mime_type *m;
4213 m = find_mime_type(mime_type);
4214 if (m == NULL)
4215 return (1);
4216 if (m->mt_download)
4217 return (1);
4219 switch (fork()) {
4220 case -1:
4221 show_oops(t, "can't fork mime handler");
4222 return (1);
4223 /* NOTREACHED */
4224 case 0:
4225 break;
4226 default:
4227 return (0);
4230 /* child */
4231 execlp(m->mt_action, m->mt_action,
4232 webkit_network_request_get_uri(request), (void *)NULL);
4234 _exit(0);
4236 /* NOTREACHED */
4237 return (0);
4240 char *
4241 get_mime_type(const char *file)
4243 const gchar *m;
4244 char *mime_type = NULL;
4245 GFileInfo *fi;
4246 GFile *gf;
4248 if (g_str_has_prefix(file, "file://"))
4249 file += strlen("file://");
4251 gf = g_file_new_for_path(file);
4252 fi = g_file_query_info(gf, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, 0,
4253 NULL, NULL);
4254 if ((m = g_file_info_get_content_type(fi)) != NULL)
4255 mime_type = g_strdup(m);
4256 g_object_unref(fi);
4257 g_object_unref(gf);
4259 return (mime_type);
4263 run_download_mimehandler(char *mime_type, char *file)
4265 struct mime_type *m;
4267 m = find_mime_type(mime_type);
4268 if (m == NULL)
4269 return (1);
4271 switch (fork()) {
4272 case -1:
4273 show_oops(NULL, "can't fork download mime handler");
4274 return (1);
4275 /* NOTREACHED */
4276 case 0:
4277 break;
4278 default:
4279 return (0);
4282 /* child */
4283 if (g_str_has_prefix(file, "file://"))
4284 file += strlen("file://");
4285 execlp(m->mt_action, m->mt_action, file, (void *)NULL);
4287 _exit(0);
4289 /* NOTREACHED */
4290 return (0);
4293 void
4294 download_status_changed_cb(WebKitDownload *download, GParamSpec *spec,
4295 WebKitWebView *wv)
4297 WebKitDownloadStatus status;
4298 const char *file = NULL;
4299 char *mime = NULL;
4301 if (download == NULL)
4302 return;
4303 status = webkit_download_get_status(download);
4304 if (status != WEBKIT_DOWNLOAD_STATUS_FINISHED)
4305 return;
4307 file = webkit_download_get_destination_uri(download);
4308 if (file == NULL)
4309 return;
4310 mime = get_mime_type(file);
4311 if (mime == NULL)
4312 return;
4314 run_download_mimehandler((char *)mime, (char *)file);
4315 g_free(mime);
4319 webview_mimetype_cb(WebKitWebView *wv, WebKitWebFrame *frame,
4320 WebKitNetworkRequest *request, char *mime_type,
4321 WebKitWebPolicyDecision *decision, struct tab *t)
4323 if (t == NULL) {
4324 show_oops(NULL, "webview_mimetype_cb invalid parameters");
4325 return (FALSE);
4328 DNPRINTF(XT_D_DOWNLOAD, "webview_mimetype_cb: tab %d mime %s\n",
4329 t->tab_id, mime_type);
4331 if (run_mimehandler(t, mime_type, request) == 0) {
4332 webkit_web_policy_decision_ignore(decision);
4333 focus_webview(t);
4334 return (TRUE);
4337 if (webkit_web_view_can_show_mime_type(wv, mime_type) == FALSE) {
4338 webkit_web_policy_decision_download(decision);
4339 return (TRUE);
4342 return (FALSE);
4346 webview_download_cb(WebKitWebView *wv, WebKitDownload *wk_download,
4347 struct tab *t)
4349 struct stat sb;
4350 const gchar *suggested_name;
4351 gchar *filename = NULL;
4352 char *uri = NULL;
4353 struct download *download_entry;
4354 int i, ret = TRUE;
4356 if (wk_download == NULL || t == NULL) {
4357 show_oops(NULL, "%s invalid parameters", __func__);
4358 return (FALSE);
4361 suggested_name = webkit_download_get_suggested_filename(wk_download);
4362 if (suggested_name == NULL)
4363 return (FALSE); /* abort download */
4365 i = 0;
4366 do {
4367 if (filename) {
4368 g_free(filename);
4369 filename = NULL;
4371 if (i) {
4372 g_free(uri);
4373 uri = NULL;
4374 filename = g_strdup_printf("%d%s", i, suggested_name);
4376 uri = g_strdup_printf("file://%s/%s", download_dir, i ?
4377 filename : suggested_name);
4378 i++;
4379 } while (!stat(uri + strlen("file://"), &sb));
4381 DNPRINTF(XT_D_DOWNLOAD, "%s: tab %d filename %s "
4382 "local %s\n", __func__, t->tab_id, filename, uri);
4384 webkit_download_set_destination_uri(wk_download, uri);
4386 if (webkit_download_get_status(wk_download) ==
4387 WEBKIT_DOWNLOAD_STATUS_ERROR) {
4388 show_oops(t, "%s: download failed to start", __func__);
4389 ret = FALSE;
4390 gtk_label_set_text(GTK_LABEL(t->label), "Download Failed");
4391 } else {
4392 /* connect "download first" mime handler */
4393 g_signal_connect(G_OBJECT(wk_download), "notify::status",
4394 G_CALLBACK(download_status_changed_cb), NULL);
4396 download_entry = g_malloc(sizeof(struct download));
4397 download_entry->download = wk_download;
4398 download_entry->tab = t;
4399 download_entry->id = next_download_id++;
4400 RB_INSERT(download_list, &downloads, download_entry);
4401 /* get from history */
4402 g_object_ref(wk_download);
4403 gtk_label_set_text(GTK_LABEL(t->label), "Downloading");
4404 show_oops(t, "Download of '%s' started...",
4405 basename((char *)webkit_download_get_destination_uri(wk_download)));
4408 if (uri)
4409 g_free(uri);
4411 if (filename)
4412 g_free(filename);
4414 /* sync other download manager tabs */
4415 update_download_tabs(NULL);
4418 * NOTE: never redirect/render the current tab before this
4419 * function returns. This will cause the download to never start.
4421 return (ret); /* start download */
4424 void
4425 webview_hover_cb(WebKitWebView *wv, gchar *title, gchar *uri, struct tab *t)
4427 DNPRINTF(XT_D_KEY, "webview_hover_cb: %s %s\n", title, uri);
4429 if (t == NULL) {
4430 show_oops(NULL, "webview_hover_cb");
4431 return;
4434 if (uri)
4435 set_status(t, uri, XT_STATUS_LINK);
4436 else {
4437 if (t->status)
4438 set_status(t, t->status, XT_STATUS_NOTHING);
4443 mark(struct tab *t, struct karg *arg)
4445 char mark;
4446 int index;
4448 mark = arg->s[1];
4449 if ((index = marktoindex(mark)) == -1)
4450 return (-1);
4452 if (arg->i == XT_MARK_SET)
4453 t->mark[index] = gtk_adjustment_get_value(t->adjust_v);
4454 else if (arg->i == XT_MARK_GOTO) {
4455 if (t->mark[index] == XT_INVALID_MARK) {
4456 show_oops(t, "mark '%c' does not exist", mark);
4457 return (-1);
4459 /* XXX t->mark[index] can be bigger than the maximum if ajax or
4460 something changes the document size */
4461 gtk_adjustment_set_value(t->adjust_v, t->mark[index]);
4464 return (0);
4467 void
4468 marks_clear(struct tab *t)
4470 int i;
4472 for (i = 0; i < LENGTH(t->mark); i++)
4473 t->mark[i] = XT_INVALID_MARK;
4477 qmarks_load(void)
4479 char file[PATH_MAX];
4480 char *line = NULL, *p;
4481 int index, i;
4482 FILE *f;
4483 size_t linelen;
4485 snprintf(file, sizeof file, "%s/%s", work_dir, XT_QMARKS_FILE);
4486 if ((f = fopen(file, "r+")) == NULL) {
4487 show_oops(NULL, "Can't open quickmarks file: %s", strerror(errno));
4488 return (1);
4491 for (i = 1; ; i++) {
4492 if ((line = fparseln(f, &linelen, NULL, NULL, 0)) == NULL)
4493 break;
4494 if (strlen(line) == 0 || line[0] == '#') {
4495 free(line);
4496 line = NULL;
4497 continue;
4500 p = strtok(line, " \t");
4502 if (p == NULL || strlen(p) != 1 ||
4503 (index = marktoindex(*p)) == -1) {
4504 warnx("corrupt quickmarks file, line %d", i);
4505 break;
4508 p = strtok(NULL, " \t");
4509 if (qmarks[index] != NULL)
4510 g_free(qmarks[index]);
4511 qmarks[index] = g_strdup(p);
4514 fclose(f);
4516 return (0);
4520 qmarks_save(void)
4522 char file[PATH_MAX];
4523 int i;
4524 FILE *f;
4526 snprintf(file, sizeof file, "%s/%s", work_dir, XT_QMARKS_FILE);
4527 if ((f = fopen(file, "r+")) == NULL) {
4528 show_oops(NULL, "Can't open quickmarks file: %s", strerror(errno));
4529 return (1);
4532 for (i = 0; i < XT_NOMARKS; i++)
4533 if (qmarks[i] != NULL)
4534 fprintf(f, "%c %s\n", indextomark(i), qmarks[i]);
4536 fclose(f);
4538 return (0);
4542 qmark(struct tab *t, struct karg *arg)
4544 char mark;
4545 int index;
4547 mark = arg->s[strlen(arg->s)-1];
4548 index = marktoindex(mark);
4549 if (index == -1)
4550 return (-1);
4552 switch (arg->i) {
4553 case XT_QMARK_SET:
4554 if (qmarks[index] != NULL)
4555 g_free(qmarks[index]);
4557 qmarks_load(); /* sync if multiple instances */
4558 qmarks[index] = g_strdup(get_uri(t));
4559 qmarks_save();
4560 break;
4561 case XT_QMARK_OPEN:
4562 if (qmarks[index] != NULL)
4563 load_uri(t, qmarks[index]);
4564 else {
4565 show_oops(t, "quickmark \"%c\" does not exist",
4566 mark);
4567 return (-1);
4569 break;
4570 case XT_QMARK_TAB:
4571 if (qmarks[index] != NULL)
4572 create_new_tab(qmarks[index], NULL, 1, -1);
4573 else {
4574 show_oops(t, "quickmark \"%c\" does not exist",
4575 mark);
4576 return (-1);
4578 break;
4581 return (0);
4585 go_up(struct tab *t, struct karg *args)
4587 int levels;
4588 char *uri;
4589 char *tmp;
4590 char *p;
4592 if (args->i == XT_GO_UP_ROOT)
4593 levels = XT_GO_UP_ROOT;
4594 else if ((levels = atoi(args->s)) == 0)
4595 levels = 1;
4597 uri = g_strdup(get_uri(t));
4599 if ((tmp = strstr(uri, XT_PROTO_DELIM)) == NULL)
4600 return (1);
4602 tmp += strlen(XT_PROTO_DELIM);
4604 /* if an uri starts with a slash, leave it alone (for file:///) */
4605 if (tmp[0] == '/')
4606 tmp++;
4608 while (levels--) {
4609 p = strrchr(tmp, '/');
4610 if (p != NULL)
4611 *p = '\0';
4612 else
4613 break;
4616 load_uri(t, uri);
4617 g_free(uri);
4619 return (0);
4623 gototab(struct tab *t, struct karg *args)
4625 int tab;
4626 struct karg arg = {0, NULL, -1};
4628 tab = atoi(args->s);
4630 if (args->i == 0)
4631 arg.i = XT_TAB_NEXT;
4632 else
4633 arg.i = args->i;
4635 arg.precount = tab;
4637 movetab(t, &arg);
4639 return (0);
4643 gotonexttab(struct tab *t, struct karg *args)
4645 int count, n_tabs, dest;
4646 struct karg arg = {0, NULL, -1};
4648 count = atoi(args->s);
4649 if (count == 0)
4650 count = 1;
4652 arg.i = XT_TAB_NEXT;
4654 n_tabs = gtk_notebook_get_n_pages(notebook);
4655 dest = gtk_notebook_get_current_page(notebook);
4657 dest += (count + 1) % n_tabs;
4658 if (dest > n_tabs)
4659 dest -= n_tabs;
4660 arg.precount = dest;
4662 DNPRINTF(XT_D_BUFFERCMD, "gotonexttab: count: %d - dest : %d \n", count, dest);
4664 movetab(t, &arg);
4666 return (0);
4670 gotoprevtab(struct tab *t, struct karg *args)
4672 int count;
4673 struct karg arg = {0, NULL, -1};
4675 count = atoi(args->s);
4676 if (count == 0)
4677 count = 1;
4679 arg.i = XT_TAB_PREV;
4680 arg.precount = count;
4682 DNPRINTF(XT_D_BUFFERCMD, "gotoprevtab: count: %d\n", count);
4683 movetab(t, &arg);
4685 return (0);
4689 zoom_amount(struct tab *t, struct karg *arg)
4691 struct karg narg = {0, NULL, -1};
4693 narg.i = atoi(arg->s);
4694 resizetab(t, &narg);
4696 return (0);
4700 flip_colon(struct tab *t, struct karg *arg)
4702 struct karg narg = {0, NULL, -1};
4703 char *p;
4705 if (t == NULL || arg == NULL)
4706 return (1);
4708 p = strstr(arg->s, ":");
4709 if (p == NULL)
4710 return (1);
4711 *p = '\0';
4713 narg.i = ':';
4714 narg.s = arg->s;
4715 command(t, &narg);
4717 return (0);
4720 /* buffer commands receive the regex that triggered them in arg.s */
4721 char bcmd[XT_BUFCMD_SZ];
4722 struct buffercmd {
4723 char *regex;
4724 int precount;
4725 #define XT_PRE_NO (0)
4726 #define XT_PRE_YES (1)
4727 #define XT_PRE_MAYBE (2)
4728 char *cmd;
4729 int (*func)(struct tab *, struct karg *);
4730 int arg;
4731 regex_t cregex;
4732 } buffercmds[] = {
4733 { "^[0-9]*gu$", XT_PRE_MAYBE, "gu", go_up, 0 },
4734 { "^gU$", XT_PRE_NO, "gU", go_up, XT_GO_UP_ROOT },
4735 { "^gg$", XT_PRE_NO, "gg", move, XT_MOVE_TOP },
4736 { "^gG$", XT_PRE_NO, "gG", move, XT_MOVE_BOTTOM },
4737 { "^[0-9]+%$", XT_PRE_YES, "%", move, XT_MOVE_PERCENT },
4738 { "^zz$", XT_PRE_NO, "zz", move, XT_MOVE_CENTER },
4739 { "^gh$", XT_PRE_NO, "gh", go_home, 0 },
4740 { "^m[a-zA-Z0-9]$", XT_PRE_NO, "m", mark, XT_MARK_SET },
4741 { "^['][a-zA-Z0-9]$", XT_PRE_NO, "'", mark, XT_MARK_GOTO },
4742 { "^[0-9]+t$", XT_PRE_YES, "t", gototab, 0 },
4743 { "^g0$", XT_PRE_YES, "g0", gototab, XT_TAB_FIRST },
4744 { "^g[$]$", XT_PRE_YES, "g$", gototab, XT_TAB_LAST },
4745 { "^[0-9]*gt$", XT_PRE_YES, "t", gotonexttab, 0 },
4746 { "^[0-9]*gT$", XT_PRE_YES, "T", gotoprevtab, 0 },
4747 { "^M[a-zA-Z0-9]$", XT_PRE_NO, "M", qmark, XT_QMARK_SET },
4748 { "^go[a-zA-Z0-9]$", XT_PRE_NO, "go", qmark, XT_QMARK_OPEN },
4749 { "^gn[a-zA-Z0-9]$", XT_PRE_NO, "gn", qmark, XT_QMARK_TAB },
4750 { "^ZR$", XT_PRE_NO, "ZR", restart, 0 },
4751 { "^ZZ$", XT_PRE_NO, "ZZ", quit, 0 },
4752 { "^zi$", XT_PRE_NO, "zi", resizetab, XT_ZOOM_IN },
4753 { "^zo$", XT_PRE_NO, "zo", resizetab, XT_ZOOM_OUT },
4754 { "^z0$", XT_PRE_NO, "z0", resizetab, XT_ZOOM_NORMAL },
4755 { "^[0-9]+Z$", XT_PRE_YES, "Z", zoom_amount, 0 },
4756 { "^[0-9]+:$", XT_PRE_YES, ":", flip_colon, 0 },
4759 void
4760 buffercmd_init(void)
4762 int i;
4764 for (i = 0; i < LENGTH(buffercmds); i++)
4765 if (regcomp(&buffercmds[i].cregex, buffercmds[i].regex,
4766 REG_EXTENDED | REG_NOSUB))
4767 startpage_add("invalid buffercmd regex %s",
4768 buffercmds[i].regex);
4771 void
4772 buffercmd_abort(struct tab *t)
4774 int i;
4776 if (t == NULL)
4777 return;
4779 DNPRINTF(XT_D_BUFFERCMD, "%s: clearing buffer\n", __func__);
4781 for (i = 0; i < LENGTH(bcmd); i++)
4782 bcmd[i] = '\0';
4784 cmd_prefix = 0; /* clear prefix for non-buffer commands */
4785 gtk_entry_set_text(GTK_ENTRY(t->sbe.buffercmd), bcmd);
4788 void
4789 buffercmd_execute(struct tab *t, struct buffercmd *cmd)
4791 struct karg arg = {0, NULL, -1};
4793 arg.i = cmd->arg;
4794 arg.s = g_strdup(bcmd);
4796 DNPRINTF(XT_D_BUFFERCMD, "buffercmd_execute: buffer \"%s\" "
4797 "matches regex \"%s\", executing\n", bcmd, cmd->regex);
4798 cmd->func(t, &arg);
4800 if (arg.s)
4801 g_free(arg.s);
4803 buffercmd_abort(t);
4806 gboolean
4807 buffercmd_addkey(struct tab *t, guint keyval)
4809 int i, c, match ;
4810 char s[XT_BUFCMD_SZ];
4812 if (gtk_widget_get_visible(GTK_WIDGET(t->buffers))) {
4813 buffercmd_abort(t);
4814 return (XT_CB_PASSTHROUGH);
4817 if (keyval == GDK_Escape) {
4818 buffercmd_abort(t);
4819 return (XT_CB_HANDLED);
4822 /* key with modifier or non-ascii character */
4823 if (!isascii(keyval)) {
4825 * XXX this looks wrong but fixes some sites like
4826 * http://www.seslisozluk.com/
4827 * that eat a shift or ctrl and end putting default focus in js
4828 * instead of ignoring the keystroke
4829 * so instead of return (XT_CB_PASSTHROUGH); eat the key
4831 return (XT_CB_HANDLED);
4834 DNPRINTF(XT_D_BUFFERCMD, "buffercmd_addkey: adding key \"%c\" "
4835 "to buffer \"%s\"\n", keyval, bcmd);
4837 for (i = 0; i < LENGTH(bcmd); i++)
4838 if (bcmd[i] == '\0') {
4839 bcmd[i] = keyval;
4840 break;
4843 /* buffer full, ignore input */
4844 if (i >= LENGTH(bcmd) -1) {
4845 DNPRINTF(XT_D_BUFFERCMD, "buffercmd_addkey: buffer full\n");
4846 buffercmd_abort(t);
4847 return (XT_CB_HANDLED);
4850 gtk_entry_set_text(GTK_ENTRY(t->sbe.buffercmd), bcmd);
4852 /* find exact match */
4853 for (i = 0; i < LENGTH(buffercmds); i++)
4854 if (regexec(&buffercmds[i].cregex, bcmd,
4855 (size_t) 0, NULL, 0) == 0) {
4856 buffercmd_execute(t, &buffercmds[i]);
4857 goto done;
4860 /* find non exact matches to see if we need to abort ot not */
4861 for (i = 0, match = 0; i < LENGTH(buffercmds); i++) {
4862 DNPRINTF(XT_D_BUFFERCMD, "trying: %s\n", bcmd);
4863 c = -1;
4864 s[0] = '\0';
4865 if (buffercmds[i].precount == XT_PRE_MAYBE) {
4866 if (isdigit(bcmd[0])) {
4867 if (sscanf(bcmd, "%d%s", &c, s) == 0)
4868 continue;
4869 } else {
4870 c = 0;
4871 if (sscanf(bcmd, "%s", s) == 0)
4872 continue;
4874 } else if (buffercmds[i].precount == XT_PRE_YES) {
4875 if (sscanf(bcmd, "%d%s", &c, s) == 0)
4876 continue;
4877 } else {
4878 if (sscanf(bcmd, "%s", s) == 0)
4879 continue;
4881 if (c == -1 && buffercmds[i].precount)
4882 continue;
4883 if (!strncmp(s, buffercmds[i].cmd, strlen(s)))
4884 match++;
4886 DNPRINTF(XT_D_BUFFERCMD, "got[%d] %d <%s>: %d %s\n",
4887 i, match, buffercmds[i].cmd, c, s);
4889 if (match == 0) {
4890 DNPRINTF(XT_D_BUFFERCMD, "aborting: %s\n", bcmd);
4891 buffercmd_abort(t);
4894 done:
4895 return (XT_CB_HANDLED);
4898 gboolean
4899 handle_keypress(struct tab *t, GdkEventKey *e, int entry)
4901 struct key_binding *k;
4903 /* handle keybindings if buffercmd is empty.
4904 if not empty, allow commands like C-n */
4905 if (bcmd[0] == '\0' || ((e->state & (CTRL | MOD1)) != 0))
4906 TAILQ_FOREACH(k, &kbl, entry)
4907 if (e->keyval == k->key
4908 && (entry ? k->use_in_entry : 1)) {
4909 /* when we are edditing eat ctrl/mod keys */
4910 if (edit_mode == XT_EM_VI &&
4911 t->mode == XT_MODE_INSERT &&
4912 (e->state & CTRL || e->state & MOD1))
4913 return (XT_CB_PASSTHROUGH);
4915 if (k->mask == 0) {
4916 if ((e->state & (CTRL | MOD1)) == 0)
4917 return (cmd_execute(t, k->cmd));
4918 } else if ((e->state & k->mask) == k->mask) {
4919 return (cmd_execute(t, k->cmd));
4923 if (!entry && ((e->state & (CTRL | MOD1)) == 0))
4924 return buffercmd_addkey(t, e->keyval);
4926 return (XT_CB_PASSTHROUGH);
4930 wv_keypress_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
4932 char s[2];
4933 void *active = NULL;
4935 /* don't use w directly; use t->whatever instead */
4937 if (t == NULL) {
4938 show_oops(NULL, "wv_keypress_cb");
4939 return (XT_CB_PASSTHROUGH);
4942 hide_oops(t);
4944 DNPRINTF(XT_D_KEY, "wv_keypress_cb: mode %d keyval 0x%x mask "
4945 "0x%x tab %d\n", t->mode, e->keyval, e->state, t->tab_id);
4947 /* Hide buffers, if they are visible, with escape. */
4948 if (gtk_widget_get_visible(GTK_WIDGET(t->buffers)) &&
4949 CLEAN(e->state) == 0 && e->keyval == GDK_Escape) {
4950 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
4951 hide_buffers(t);
4952 return (XT_CB_HANDLED);
4955 if (t->mode == XT_MODE_HINT)
4956 return (XT_CB_HANDLED);
4958 if ((CLEAN(e->state) == 0 && e->keyval == GDK_Tab) ||
4959 (CLEAN(e->state) == SHFT && e->keyval == GDK_Tab))
4960 /* something focussy is about to happen */
4961 return (XT_CB_PASSTHROUGH);
4963 /* check if we are some sort of text input thing in the dom */
4964 active = input_check_mode(t);
4966 if (t->mode == XT_MODE_HINT) {
4967 /* XXX make sure cmd entry is enabled */
4968 return (XT_CB_HANDLED);
4969 } else if (t->mode == XT_MODE_COMMAND) {
4970 /* prefix input */
4971 snprintf(s, sizeof s, "%c", e->keyval);
4972 if (CLEAN(e->state) == 0 && isdigit(s[0]))
4973 cmd_prefix = 10 * cmd_prefix + atoi(s);
4974 return (handle_keypress(t, e, 0));
4975 } else {
4976 /* insert mode */
4977 return (handle_keypress(t, e, 1));
4980 /* NOTREACHED */
4981 return (XT_CB_PASSTHROUGH);
4984 gboolean
4985 hint_continue(struct tab *t)
4987 const gchar *c = gtk_entry_get_text(GTK_ENTRY(t->cmd));
4988 char *s;
4989 const gchar *errstr = NULL;
4990 gboolean rv = TRUE;
4991 long long i;
4993 if (!(c[0] == '.' || c[0] == ','))
4994 goto done;
4995 if (strlen(c) == 1) {
4996 /* XXX should not happen */
4997 rv = TRUE;
4998 goto done;
5001 if (isdigit(c[1])) {
5002 /* numeric input */
5003 i = strtonum(&c[1], 1, 4096, &errstr);
5004 if (errstr) {
5005 show_oops(t, "invalid numerical hint %s", &c[1]);
5006 goto done;
5008 s = g_strdup_printf("hints.updateHints(%lld);", i);
5009 run_script(t, s);
5010 g_free(s);
5011 } else {
5012 /* alphanumeric input */
5013 s = g_strdup_printf("hints.createHints('%s', '%c');",
5014 &c[1], c[0] == '.' ? 'f' : 'F');
5015 run_script(t, s);
5016 g_free(s);
5019 rv = FALSE;
5020 done:
5021 return (rv);
5024 gboolean
5025 search_continue(struct tab *t)
5027 const gchar *c = gtk_entry_get_text(GTK_ENTRY(t->cmd));
5028 gboolean rv = FALSE;
5030 if (c[0] == ':' || c[0] == '.' || c[0] == ',')
5031 goto done;
5032 if (strlen(c) == 1) {
5033 webkit_web_view_unmark_text_matches(t->wv);
5034 goto done;
5037 if (c[0] == '/')
5038 t->search_forward = TRUE;
5039 else if (c[0] == '?')
5040 t->search_forward = FALSE;
5041 else
5042 goto done;
5044 rv = TRUE;
5045 done:
5046 return (rv);
5049 gboolean
5050 search_cb(struct tab *t)
5052 const gchar *c = gtk_entry_get_text(GTK_ENTRY(t->cmd));
5053 GdkColor color;
5055 if (search_continue(t) == FALSE)
5056 goto done;
5058 /* search */
5059 if (webkit_web_view_search_text(t->wv, &c[1], FALSE, t->search_forward,
5060 TRUE) == FALSE) {
5061 /* not found, mark red */
5062 gdk_color_parse(XT_COLOR_RED, &color);
5063 gtk_widget_modify_base(t->cmd, GTK_STATE_NORMAL, &color);
5064 /* unmark and remove selection */
5065 webkit_web_view_unmark_text_matches(t->wv);
5066 /* my kingdom for a way to unselect text in webview */
5067 } else {
5068 /* found, highlight all */
5069 webkit_web_view_unmark_text_matches(t->wv);
5070 webkit_web_view_mark_text_matches(t->wv, &c[1], FALSE, 0);
5071 webkit_web_view_set_highlight_text_matches(t->wv, TRUE);
5072 gdk_color_parse(XT_COLOR_WHITE, &color);
5073 gtk_widget_modify_base(t->cmd, GTK_STATE_NORMAL, &color);
5075 done:
5076 t->search_id = 0;
5077 return (FALSE);
5081 cmd_keyrelease_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
5083 const gchar *c = gtk_entry_get_text(w);
5085 if (t == NULL) {
5086 show_oops(NULL, "cmd_keyrelease_cb invalid parameters");
5087 return (XT_CB_PASSTHROUGH);
5090 DNPRINTF(XT_D_CMD, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
5091 e->keyval, e->state, t->tab_id);
5093 /* hinting */
5094 if (!(e->keyval == GDK_Tab || e->keyval == GDK_ISO_Left_Tab)) {
5095 if (hint_continue(t) == FALSE)
5096 goto done;
5099 /* search */
5100 if (search_continue(t) == FALSE)
5101 goto done;
5103 /* if search length is > 4 then no longer play timeout games */
5104 if (strlen(c) > 4) {
5105 if (t->search_id) {
5106 g_source_remove(t->search_id);
5107 t->search_id = 0;
5109 search_cb(t);
5110 goto done;
5113 /* reestablish a new timer if the user types fast */
5114 if (t->search_id)
5115 g_source_remove(t->search_id);
5116 t->search_id = g_timeout_add(250, (GSourceFunc)search_cb, (gpointer)t);
5118 done:
5119 return (XT_CB_PASSTHROUGH);
5122 gboolean
5123 match_uri(const gchar *uri, const gchar *key) {
5124 gchar *voffset;
5125 size_t len;
5126 gboolean match = FALSE;
5128 len = strlen(key);
5130 if (!strncmp(key, uri, len))
5131 match = TRUE;
5132 else {
5133 voffset = strstr(uri, "/") + 2;
5134 if (!strncmp(key, voffset, len))
5135 match = TRUE;
5136 else if (g_str_has_prefix(voffset, "www.")) {
5137 voffset = voffset + strlen("www.");
5138 if (!strncmp(key, voffset, len))
5139 match = TRUE;
5143 return (match);
5146 gboolean
5147 match_session(const gchar *name, const gchar *key) {
5148 char *sub;
5150 sub = strcasestr(name, key);
5152 return sub == name;
5155 void
5156 cmd_getlist(int id, char *key)
5158 int i, dep, c = 0;
5159 struct history *h;
5160 struct session *s;
5162 if (id >= 0) {
5163 if (cmds[id].type & XT_URLARG) {
5164 RB_FOREACH_REVERSE(h, history_list, &hl)
5165 if (match_uri(h->uri, key)) {
5166 cmd_status.list[c] = (char *)h->uri;
5167 if (++c > 255)
5168 break;
5170 cmd_status.len = c;
5171 return;
5172 } else if (cmds[id].type & XT_SESSARG) {
5173 TAILQ_FOREACH(s, &sessions, entry)
5174 if (match_session(s->name, key)) {
5175 cmd_status.list[c] = (char *)s->name;
5176 if (++c > 255)
5177 break;
5179 cmd_status.len = c;
5180 return;
5181 } else if (cmds[id].type & XT_SETARG) {
5182 for (i = 0; i < get_settings_size(); i++)
5183 if (!strncmp(key, get_setting_name(i),
5184 strlen(key)))
5185 cmd_status.list[c++] =
5186 get_setting_name(i);
5187 cmd_status.len = c;
5188 return;
5192 dep = (id == -1) ? 0 : cmds[id].level + 1;
5194 for (i = id + 1; i < LENGTH(cmds); i++) {
5195 if (cmds[i].level < dep)
5196 break;
5197 if (cmds[i].level == dep && !strncmp(key, cmds[i].cmd,
5198 strlen(key)) && !isdigit(cmds[i].cmd[0]))
5199 cmd_status.list[c++] = cmds[i].cmd;
5203 cmd_status.len = c;
5206 char *
5207 cmd_getnext(int dir)
5209 cmd_status.index += dir;
5211 if (cmd_status.index < 0)
5212 cmd_status.index = cmd_status.len - 1;
5213 else if (cmd_status.index >= cmd_status.len)
5214 cmd_status.index = 0;
5216 return cmd_status.list[cmd_status.index];
5220 cmd_tokenize(char *s, char *tokens[])
5222 int i = 0;
5223 char *tok, *last = NULL;
5224 size_t len = strlen(s);
5225 bool blank;
5227 blank = len == 0 || (len > 0 && s[len - 1] == ' ');
5228 for (tok = strtok_r(s, " ", &last); tok && i < 3;
5229 tok = strtok_r(NULL, " ", &last), i++)
5230 tokens[i] = tok;
5232 if (blank && i < 3)
5233 tokens[i++] = "";
5235 return (i);
5238 void
5239 cmd_complete(struct tab *t, char *str, int dir)
5241 GtkEntry *w = GTK_ENTRY(t->cmd);
5242 int i, j, levels, c = 0, dep = 0, parent = -1;
5243 int matchcount = 0;
5244 char *tok, *match, *s = g_strdup(str);
5245 char *tokens[3];
5246 char res[XT_MAX_URL_LENGTH + 32] = ":";
5247 char *sc = s;
5249 DNPRINTF(XT_D_CMD, "%s: complete %s\n", __func__, str);
5251 /* copy prefix*/
5252 for (i = 0; isdigit(s[i]); i++)
5253 res[i + 1] = s[i];
5255 for (; isspace(s[i]); i++)
5256 res[i + 1] = s[i];
5258 s += i;
5260 levels = cmd_tokenize(s, tokens);
5262 for (i = 0; i < levels - 1; i++) {
5263 tok = tokens[i];
5264 matchcount = 0;
5265 for (j = c; j < LENGTH(cmds); j++) {
5266 if (cmds[j].level < dep)
5267 break;
5268 if (cmds[j].level == dep && !strncmp(tok, cmds[j].cmd,
5269 strlen(tok))) {
5270 matchcount++;
5271 c = j + 1;
5272 if (strlen(tok) == strlen(cmds[j].cmd)) {
5273 matchcount = 1;
5274 break;
5279 if (matchcount == 1) {
5280 strlcat(res, tok, sizeof res);
5281 strlcat(res, " ", sizeof res);
5282 dep++;
5283 } else {
5284 g_free(sc);
5285 return;
5288 parent = c - 1;
5291 if (cmd_status.index == -1)
5292 cmd_getlist(parent, tokens[i]);
5294 if (cmd_status.len > 0) {
5295 match = cmd_getnext(dir);
5296 strlcat(res, match, sizeof res);
5297 gtk_entry_set_text(w, res);
5298 gtk_editable_set_position(GTK_EDITABLE(w), -1);
5301 g_free(sc);
5304 gboolean
5305 cmd_execute(struct tab *t, char *str)
5307 struct cmd *cmd = NULL;
5308 char *tok, *last = NULL, *s = g_strdup(str), *sc;
5309 char prefixstr[4];
5310 int j, len, c = 0, dep = 0, matchcount = 0;
5311 int prefix = -1, rv = XT_CB_PASSTHROUGH;
5312 struct karg arg = {0, NULL, -1};
5314 sc = s;
5316 /* copy prefix*/
5317 for (j = 0; j<3 && isdigit(s[j]); j++)
5318 prefixstr[j]=s[j];
5320 prefixstr[j]='\0';
5322 s += j;
5323 while (isspace(s[0]))
5324 s++;
5326 if (strlen(s) > 0 && strlen(prefixstr) > 0)
5327 prefix = atoi(prefixstr);
5328 else
5329 s = sc;
5331 for (tok = strtok_r(s, " ", &last); tok;
5332 tok = strtok_r(NULL, " ", &last)) {
5333 matchcount = 0;
5334 for (j = c; j < LENGTH(cmds); j++) {
5335 if (cmds[j].level < dep)
5336 break;
5337 len = (tok[strlen(tok) - 1] == '!') ? strlen(tok) - 1 :
5338 strlen(tok);
5339 if (cmds[j].level == dep &&
5340 !strncmp(tok, cmds[j].cmd, len)) {
5341 matchcount++;
5342 c = j + 1;
5343 cmd = &cmds[j];
5344 if (len == strlen(cmds[j].cmd)) {
5345 matchcount = 1;
5346 break;
5350 if (matchcount == 1) {
5351 if (cmd->type > 0)
5352 goto execute_cmd;
5353 dep++;
5354 } else {
5355 show_oops(t, "Invalid command: %s", str);
5356 goto done;
5359 execute_cmd:
5360 if (cmd == NULL) {
5361 show_oops(t, "Empty command");
5362 goto done;
5364 arg.i = cmd->arg;
5366 if (prefix != -1)
5367 arg.precount = prefix;
5368 else if (cmd_prefix > 0)
5369 arg.precount = cmd_prefix;
5371 if (j > 0 && !(cmd->type & XT_PREFIX) && arg.precount > -1) {
5372 show_oops(t, "No prefix allowed: %s", str);
5373 goto done;
5375 if (cmd->type > 1)
5376 arg.s = last ? g_strdup(last) : g_strdup("");
5377 if (cmd->type & XT_INTARG && last && strlen(last) > 0) {
5378 if (arg.s == NULL) {
5379 show_oops(t, "Invalid command");
5380 goto done;
5382 arg.precount = atoi(arg.s);
5383 if (arg.precount <= 0) {
5384 if (arg.s[0] == '0')
5385 show_oops(t, "Zero count");
5386 else
5387 show_oops(t, "Trailing characters");
5388 goto done;
5392 DNPRINTF(XT_D_CMD, "%s: prefix %d arg %s\n",
5393 __func__, arg.precount, arg.s);
5395 cmd->func(t, &arg);
5397 rv = XT_CB_HANDLED;
5398 done:
5399 if (j > 0)
5400 cmd_prefix = 0;
5401 g_free(sc);
5402 if (arg.s)
5403 g_free(arg.s);
5405 return (rv);
5409 entry_key_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
5411 if (t == NULL) {
5412 show_oops(NULL, "entry_key_cb invalid parameters");
5413 return (XT_CB_PASSTHROUGH);
5416 DNPRINTF(XT_D_CMD, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
5417 e->keyval, e->state, t->tab_id);
5419 hide_oops(t);
5421 if (e->keyval == GDK_Escape) {
5422 /* don't use focus_webview(t) because we want to type :cmds */
5423 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
5426 return (handle_keypress(t, e, 1));
5429 struct command_entry *
5430 history_prev(struct command_list *l, struct command_entry *at)
5432 if (at == NULL)
5433 at = TAILQ_LAST(l, command_list);
5434 else {
5435 at = TAILQ_PREV(at, command_list, entry);
5436 if (at == NULL)
5437 at = TAILQ_LAST(l, command_list);
5440 return (at);
5443 struct command_entry *
5444 history_next(struct command_list *l, struct command_entry *at)
5446 if (at == NULL)
5447 at = TAILQ_FIRST(l);
5448 else {
5449 at = TAILQ_NEXT(at, entry);
5450 if (at == NULL)
5451 at = TAILQ_FIRST(l);
5454 return (at);
5458 cmd_keypress_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
5460 int rv = XT_CB_HANDLED;
5461 const gchar *c = gtk_entry_get_text(w);
5462 char *s;
5464 if (t == NULL) {
5465 show_oops(NULL, "cmd_keypress_cb parameters");
5466 return (XT_CB_PASSTHROUGH);
5469 DNPRINTF(XT_D_CMD, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
5470 e->keyval, e->state, t->tab_id);
5472 /* sanity */
5473 if (c == NULL)
5474 e->keyval = GDK_Escape;
5475 else if (!(c[0] == ':' || c[0] == '/' || c[0] == '?' ||
5476 c[0] == '.' || c[0] == ','))
5477 e->keyval = GDK_Escape;
5479 if (e->keyval != GDK_Tab && e->keyval != GDK_Shift_L &&
5480 e->keyval != GDK_ISO_Left_Tab)
5481 cmd_status.index = -1;
5483 switch (e->keyval) {
5484 case GDK_Tab:
5485 if (c[0] == ':')
5486 cmd_complete(t, (char *)&c[1], 1);
5487 else if (c[0] == '.' || c[0] == ',')
5488 run_script(t, "hints.focusNextHint();");
5489 goto done;
5490 case GDK_ISO_Left_Tab:
5491 if (c[0] == ':')
5492 cmd_complete(t, (char *)&c[1], -1);
5493 else if (c[0] == '.' || c[0] == ',')
5494 run_script(t, "hints.focusPreviousHint();");
5495 goto done;
5496 case GDK_Down:
5497 if (c[0] == '?') {
5498 if ((search_at = history_next(&shl, search_at))) {
5499 search_at->line[0] = c[0];
5500 gtk_entry_set_text(w, search_at->line);
5501 gtk_editable_set_position(GTK_EDITABLE(w), -1);
5503 } else if (c[0] == '/') {
5504 if ((search_at = history_prev(&chl, search_at))) {
5505 search_at->line[0] = c[0];
5506 gtk_entry_set_text(w, search_at->line);
5507 gtk_editable_set_position(GTK_EDITABLE(w), -1);
5509 } if (c[0] == ':') {
5510 if ((history_at = history_prev(&chl, history_at))) {
5511 history_at->line[0] = c[0];
5512 gtk_entry_set_text(w, history_at->line);
5513 gtk_editable_set_position(GTK_EDITABLE(w), -1);
5516 goto done;
5517 case GDK_Up:
5518 if (c[0] == '/') {
5519 if ((search_at = history_next(&shl, search_at))) {
5520 search_at->line[0] = c[0];
5521 gtk_entry_set_text(w, search_at->line);
5522 gtk_editable_set_position(GTK_EDITABLE(w), -1);
5524 } else if (c[0] == '?') {
5525 if ((search_at = history_prev(&chl, search_at))) {
5526 search_at->line[0] = c[0];
5527 gtk_entry_set_text(w, search_at->line);
5528 gtk_editable_set_position(GTK_EDITABLE(w), -1);
5530 } if (c[0] == ':') {
5531 if ((history_at = history_next(&chl, history_at))) {
5532 history_at->line[0] = c[0];
5533 gtk_entry_set_text(w, history_at->line);
5534 gtk_editable_set_position(GTK_EDITABLE(w), -1);
5537 goto done;
5538 case GDK_BackSpace:
5539 if (!(!strcmp(c, ":") || !strcmp(c, "/") || !strcmp(c, "?") ||
5540 !strcmp(c, ".") || !strcmp(c, ","))) {
5541 /* see if we are doing hinting and reset it */
5542 if (c[0] == '.' || c[0] == ',') {
5543 /* recreate hints */
5544 s = g_strdup_printf("hints.createHints('', "
5545 "'%c');", c[0] == '.' ? 'f' : 'F');
5546 run_script(t, s);
5547 g_free(s);
5549 break;
5552 /* FALLTHROUGH */
5553 case GDK_Escape:
5554 hide_cmd(t);
5555 focus_webview(t);
5557 /* cancel search */
5558 if (c != NULL && (c[0] == '/' || c[0] == '?'))
5559 webkit_web_view_unmark_text_matches(t->wv);
5561 /* no need to cancel hints */
5562 goto done;
5565 rv = XT_CB_PASSTHROUGH;
5566 done:
5567 return (rv);
5570 void
5571 wv_popup_cb(GtkEntry *entry, GtkMenu *menu, struct tab *t)
5573 DNPRINTF(XT_D_CMD, "wv_popup_cb: tab %d\n", t->tab_id);
5576 void
5577 cmd_popup_cb(GtkEntry *entry, GtkMenu *menu, struct tab *t)
5579 /* popup menu enabled */
5580 t->popup = 1;
5584 cmd_focusout_cb(GtkWidget *w, GdkEventFocus *e, struct tab *t)
5586 if (t == NULL) {
5587 show_oops(NULL, "cmd_focusout_cb invalid parameters");
5588 return (XT_CB_PASSTHROUGH);
5591 DNPRINTF(XT_D_CMD, "cmd_focusout_cb: tab %d popup %d\n",
5592 t->tab_id, t->popup);
5594 /* if popup is enabled don't lose focus */
5595 if (t->popup) {
5596 t->popup = 0;
5597 return (XT_CB_PASSTHROUGH);
5600 hide_cmd(t);
5601 hide_oops(t);
5602 disable_hints(t);
5604 if (show_url == 0 || t->focus_wv)
5605 focus_webview(t);
5606 else
5607 gtk_widget_grab_focus(GTK_WIDGET(t->uri_entry));
5609 return (XT_CB_PASSTHROUGH);
5612 void
5613 cmd_activate_cb(GtkEntry *entry, struct tab *t)
5615 char *s;
5616 const gchar *c = gtk_entry_get_text(entry);
5618 if (t == NULL) {
5619 show_oops(NULL, "cmd_activate_cb invalid parameters");
5620 return;
5623 DNPRINTF(XT_D_CMD, "cmd_activate_cb: tab %d %s\n", t->tab_id, c);
5625 /* sanity */
5626 if (c == NULL)
5627 goto done;
5628 else if (!(c[0] == ':' || c[0] == '/' || c[0] == '?' ||
5629 c[0] == '.' || c[0] == ','))
5630 goto done;
5631 if (strlen(c) < 1)
5632 goto done;
5633 s = (char *)&c[1];
5635 if (c[0] == '/' || c[0] == '?') {
5636 /* see if there is a timer pending */
5637 if (t->search_id) {
5638 g_source_remove(t->search_id);
5639 t->search_id = 0;
5640 search_cb(t);
5643 if (t->search_text) {
5644 g_free(t->search_text);
5645 t->search_text = NULL;
5648 t->search_text = g_strdup(s);
5649 if (global_search)
5650 g_free(global_search);
5651 global_search = g_strdup(s);
5652 t->search_forward = c[0] == '/';
5654 history_add(&shl, search_file, s, &search_history_count);
5655 } else if (c[0] == '.' || c[0] == ',') {
5656 run_script(t, "hints.fire();");
5657 /* XXX history for link following? */
5658 } else if (c[0] == ':') {
5659 history_add(&chl, command_file, s, &cmd_history_count);
5660 /* can't call hide_cmd after cmd_execute */
5661 hide_cmd(t);
5662 cmd_execute(t, s);
5663 return;
5666 done:
5667 hide_cmd(t);
5670 void
5671 backward_cb(GtkWidget *w, struct tab *t)
5673 struct karg a;
5675 if (t == NULL) {
5676 show_oops(NULL, "backward_cb invalid parameters");
5677 return;
5680 DNPRINTF(XT_D_NAV, "backward_cb: tab %d\n", t->tab_id);
5682 a.i = XT_NAV_BACK;
5683 navaction(t, &a);
5686 void
5687 forward_cb(GtkWidget *w, struct tab *t)
5689 struct karg a;
5691 if (t == NULL) {
5692 show_oops(NULL, "forward_cb invalid parameters");
5693 return;
5696 DNPRINTF(XT_D_NAV, "forward_cb: tab %d\n", t->tab_id);
5698 a.i = XT_NAV_FORWARD;
5699 navaction(t, &a);
5702 void
5703 home_cb(GtkWidget *w, struct tab *t)
5705 if (t == NULL) {
5706 show_oops(NULL, "home_cb invalid parameters");
5707 return;
5710 DNPRINTF(XT_D_NAV, "home_cb: tab %d\n", t->tab_id);
5712 load_uri(t, home);
5715 void
5716 stop_cb(GtkWidget *w, struct tab *t)
5718 WebKitWebFrame *frame;
5720 if (t == NULL) {
5721 show_oops(NULL, "stop_cb invalid parameters");
5722 return;
5725 DNPRINTF(XT_D_NAV, "stop_cb: tab %d\n", t->tab_id);
5727 frame = webkit_web_view_get_main_frame(t->wv);
5728 if (frame == NULL) {
5729 show_oops(t, "stop_cb: no frame");
5730 return;
5733 webkit_web_frame_stop_loading(frame);
5734 abort_favicon_download(t);
5737 void
5738 setup_webkit(struct tab *t)
5740 if (is_g_object_setting(G_OBJECT(t->settings), "enable-dns-prefetching"))
5741 g_object_set(G_OBJECT(t->settings), "enable-dns-prefetching",
5742 FALSE, (char *)NULL);
5743 else
5744 warnx("webkit does not have \"enable-dns-prefetching\" property");
5745 g_object_set(G_OBJECT(t->settings),
5746 "user-agent", t->user_agent, (char *)NULL);
5747 g_object_set(G_OBJECT(t->settings),
5748 "enable-scripts", enable_scripts, (char *)NULL);
5749 g_object_set(G_OBJECT(t->settings),
5750 "enable-plugins", enable_plugins, (char *)NULL);
5751 g_object_set(G_OBJECT(t->settings),
5752 "javascript-can-open-windows-automatically", enable_scripts,
5753 (char *)NULL);
5754 g_object_set(G_OBJECT(t->settings),
5755 "enable-html5-database", FALSE, (char *)NULL);
5756 g_object_set(G_OBJECT(t->settings),
5757 "enable-html5-local-storage", enable_localstorage, (char *)NULL);
5758 g_object_set(G_OBJECT(t->settings),
5759 "enable_spell_checking", enable_spell_checking, (char *)NULL);
5760 g_object_set(G_OBJECT(t->settings),
5761 "spell_checking_languages", spell_check_languages, (char *)NULL);
5762 g_object_set(G_OBJECT(t->settings),
5763 "enable-developer-extras", TRUE, (char *)NULL);
5764 g_object_set(G_OBJECT(t->wv),
5765 "full-content-zoom", TRUE, (char *)NULL);
5766 g_object_set(G_OBJECT(t->settings),
5767 "auto-load-images", auto_load_images, (char *)NULL);
5769 webkit_web_view_set_settings(t->wv, t->settings);
5772 gboolean
5773 update_statusbar_position(GtkAdjustment* adjustment, gpointer data)
5775 struct tab *ti, *t = NULL;
5776 gdouble view_size, value, max;
5777 gchar *position;
5779 TAILQ_FOREACH(ti, &tabs, entry)
5780 if (ti->tab_id == gtk_notebook_get_current_page(notebook)) {
5781 t = ti;
5782 break;
5785 if (t == NULL)
5786 return FALSE;
5788 if (adjustment == NULL)
5789 adjustment = gtk_scrolled_window_get_vadjustment(
5790 GTK_SCROLLED_WINDOW(t->browser_win));
5792 view_size = gtk_adjustment_get_page_size(adjustment);
5793 value = gtk_adjustment_get_value(adjustment);
5794 max = gtk_adjustment_get_upper(adjustment) - view_size;
5796 if (max == 0)
5797 position = g_strdup("All");
5798 else if (value == max)
5799 position = g_strdup("Bot");
5800 else if (value == 0)
5801 position = g_strdup("Top");
5802 else
5803 position = g_strdup_printf("%d%%", (int) ((value / max) * 100));
5805 gtk_entry_set_text(GTK_ENTRY(t->sbe.position), position);
5806 g_free(position);
5808 return (TRUE);
5811 GtkWidget *
5812 create_window(const gchar *name)
5814 GtkWidget *w;
5816 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
5817 if (window_maximize)
5818 gtk_window_maximize(GTK_WINDOW(w));
5819 else
5820 gtk_window_set_default_size(GTK_WINDOW(w), window_width, window_height);
5821 gtk_widget_set_name(w, name);
5822 gtk_window_set_wmclass(GTK_WINDOW(w), name, "XXXTerm");
5824 return (w);
5827 GtkWidget *
5828 create_browser(struct tab *t)
5830 GtkWidget *w;
5831 gchar *strval;
5832 GtkAdjustment *adjustment;
5834 if (t == NULL) {
5835 show_oops(NULL, "create_browser invalid parameters");
5836 return (NULL);
5839 t->sb_h = GTK_SCROLLBAR(gtk_hscrollbar_new(NULL));
5840 t->sb_v = GTK_SCROLLBAR(gtk_vscrollbar_new(NULL));
5841 t->adjust_h = gtk_range_get_adjustment(GTK_RANGE(t->sb_h));
5842 t->adjust_v = gtk_range_get_adjustment(GTK_RANGE(t->sb_v));
5844 w = gtk_scrolled_window_new(t->adjust_h, t->adjust_v);
5845 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w),
5846 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
5848 t->wv = WEBKIT_WEB_VIEW(webkit_web_view_new());
5849 gtk_container_add(GTK_CONTAINER(w), GTK_WIDGET(t->wv));
5851 /* set defaults */
5852 t->settings = webkit_web_settings_new();
5854 g_object_set(t->settings, "default-encoding", encoding, (char *)NULL);
5856 if (user_agent == NULL) {
5857 g_object_get(G_OBJECT(t->settings), "user-agent", &strval,
5858 (char *)NULL);
5859 t->user_agent = g_strdup_printf("%s %s+", strval, version);
5860 g_free(strval);
5861 } else
5862 t->user_agent = g_strdup(user_agent->value);
5864 t->stylesheet = g_strdup_printf("file://%s/style.css", resource_dir);
5865 t->load_images = auto_load_images;
5867 adjustment =
5868 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w));
5869 g_signal_connect(G_OBJECT(adjustment), "value-changed",
5870 G_CALLBACK(update_statusbar_position), NULL);
5872 setup_webkit(t);
5873 setup_inspector(t);
5875 return (w);
5878 GtkWidget *
5879 create_kiosk_toolbar(struct tab *t)
5881 GtkWidget *toolbar = NULL, *b;
5883 b = gtk_hbox_new(FALSE, 0);
5884 toolbar = b;
5885 gtk_container_set_border_width(GTK_CONTAINER(toolbar), 0);
5887 /* backward button */
5888 t->backward = create_button("Back", GTK_STOCK_GO_BACK, 0);
5889 gtk_widget_set_sensitive(t->backward, FALSE);
5890 g_signal_connect(G_OBJECT(t->backward), "clicked",
5891 G_CALLBACK(backward_cb), t);
5892 gtk_box_pack_start(GTK_BOX(b), t->backward, TRUE, TRUE, 0);
5894 /* forward button */
5895 t->forward = create_button("Forward", GTK_STOCK_GO_FORWARD, 0);
5896 gtk_widget_set_sensitive(t->forward, FALSE);
5897 g_signal_connect(G_OBJECT(t->forward), "clicked",
5898 G_CALLBACK(forward_cb), t);
5899 gtk_box_pack_start(GTK_BOX(b), t->forward, TRUE, TRUE, 0);
5901 /* home button */
5902 t->gohome = create_button("Home", GTK_STOCK_HOME, 0);
5903 gtk_widget_set_sensitive(t->gohome, true);
5904 g_signal_connect(G_OBJECT(t->gohome), "clicked",
5905 G_CALLBACK(home_cb), t);
5906 gtk_box_pack_start(GTK_BOX(b), t->gohome, TRUE, TRUE, 0);
5908 /* create widgets but don't use them */
5909 t->uri_entry = gtk_entry_new();
5910 t->stop = create_button("Stop", GTK_STOCK_STOP, 0);
5911 t->js_toggle = create_button("JS-Toggle", enable_scripts ?
5912 GTK_STOCK_MEDIA_PLAY : GTK_STOCK_MEDIA_PAUSE, 0);
5914 return (toolbar);
5917 GtkWidget *
5918 create_toolbar(struct tab *t)
5920 GtkWidget *toolbar = NULL, *b, *eb1;
5922 b = gtk_hbox_new(FALSE, 0);
5923 toolbar = b;
5924 gtk_container_set_border_width(GTK_CONTAINER(toolbar), 0);
5926 /* backward button */
5927 t->backward = create_button("Back", GTK_STOCK_GO_BACK, 0);
5928 gtk_widget_set_sensitive(t->backward, FALSE);
5929 g_signal_connect(G_OBJECT(t->backward), "clicked",
5930 G_CALLBACK(backward_cb), t);
5931 gtk_box_pack_start(GTK_BOX(b), t->backward, FALSE, FALSE, 0);
5933 /* forward button */
5934 t->forward = create_button("Forward",GTK_STOCK_GO_FORWARD, 0);
5935 gtk_widget_set_sensitive(t->forward, FALSE);
5936 g_signal_connect(G_OBJECT(t->forward), "clicked",
5937 G_CALLBACK(forward_cb), t);
5938 gtk_box_pack_start(GTK_BOX(b), t->forward, FALSE,
5939 FALSE, 0);
5941 /* stop button */
5942 t->stop = create_button("Stop", GTK_STOCK_STOP, 0);
5943 gtk_widget_set_sensitive(t->stop, FALSE);
5944 g_signal_connect(G_OBJECT(t->stop), "clicked",
5945 G_CALLBACK(stop_cb), t);
5946 gtk_box_pack_start(GTK_BOX(b), t->stop, FALSE,
5947 FALSE, 0);
5949 /* JS button */
5950 t->js_toggle = create_button("JS-Toggle", enable_scripts ?
5951 GTK_STOCK_MEDIA_PLAY : GTK_STOCK_MEDIA_PAUSE, 0);
5952 gtk_widget_set_sensitive(t->js_toggle, TRUE);
5953 g_signal_connect(G_OBJECT(t->js_toggle), "clicked",
5954 G_CALLBACK(js_toggle_cb), t);
5955 gtk_box_pack_start(GTK_BOX(b), t->js_toggle, FALSE, FALSE, 0);
5957 t->uri_entry = gtk_entry_new();
5958 g_signal_connect(G_OBJECT(t->uri_entry), "activate",
5959 G_CALLBACK(activate_uri_entry_cb), t);
5960 g_signal_connect(G_OBJECT(t->uri_entry), "key-press-event",
5961 G_CALLBACK(entry_key_cb), t);
5962 completion_add(t);
5963 eb1 = gtk_hbox_new(FALSE, 0);
5964 gtk_container_set_border_width(GTK_CONTAINER(eb1), 1);
5965 gtk_box_pack_start(GTK_BOX(eb1), t->uri_entry, TRUE, TRUE, 0);
5966 gtk_box_pack_start(GTK_BOX(b), eb1, TRUE, TRUE, 0);
5968 /* search entry */
5969 if (search_string) {
5970 GtkWidget *eb2;
5971 t->search_entry = gtk_entry_new();
5972 gtk_entry_set_width_chars(GTK_ENTRY(t->search_entry), 30);
5973 g_signal_connect(G_OBJECT(t->search_entry), "activate",
5974 G_CALLBACK(activate_search_entry_cb), t);
5975 g_signal_connect(G_OBJECT(t->search_entry), "key-press-event",
5976 G_CALLBACK(entry_key_cb), t);
5977 gtk_widget_set_size_request(t->search_entry, -1, -1);
5978 eb2 = gtk_hbox_new(FALSE, 0);
5979 gtk_container_set_border_width(GTK_CONTAINER(eb2), 1);
5980 gtk_box_pack_start(GTK_BOX(eb2), t->search_entry, TRUE, TRUE,
5982 gtk_box_pack_start(GTK_BOX(b), eb2, FALSE, FALSE, 0);
5985 return (toolbar);
5988 GtkWidget *
5989 create_buffers(struct tab *t)
5991 GtkCellRenderer *renderer;
5992 GtkWidget *view;
5994 view = gtk_tree_view_new();
5996 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
5998 renderer = gtk_cell_renderer_text_new();
5999 gtk_tree_view_insert_column_with_attributes
6000 (GTK_TREE_VIEW(view), -1, "Id", renderer, "text", COL_ID, (char *)NULL);
6002 renderer = gtk_cell_renderer_pixbuf_new();
6003 gtk_tree_view_insert_column_with_attributes
6004 (GTK_TREE_VIEW(view), -1, "Favicon", renderer, "pixbuf", COL_FAVICON,
6005 (char *)NULL);
6007 renderer = gtk_cell_renderer_text_new();
6008 gtk_tree_view_insert_column_with_attributes
6009 (GTK_TREE_VIEW(view), -1, "Title", renderer, "text", COL_TITLE,
6010 (char *)NULL);
6012 gtk_tree_view_set_model
6013 (GTK_TREE_VIEW(view), GTK_TREE_MODEL(buffers_store));
6015 return (view);
6018 void
6019 row_activated_cb(GtkTreeView *view, GtkTreePath *path,
6020 GtkTreeViewColumn *col, struct tab *t)
6022 GtkTreeIter iter;
6023 guint id;
6025 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
6027 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store), &iter,
6028 path)) {
6029 gtk_tree_model_get
6030 (GTK_TREE_MODEL(buffers_store), &iter, COL_ID, &id, -1);
6031 set_current_tab(id - 1);
6034 hide_buffers(t);
6037 /* after tab reordering/creation/removal */
6038 void
6039 recalc_tabs(void)
6041 struct tab *t;
6042 int maxid = 0;
6044 TAILQ_FOREACH(t, &tabs, entry) {
6045 t->tab_id = gtk_notebook_page_num(notebook, t->vbox);
6046 if (t->tab_id > maxid)
6047 maxid = t->tab_id;
6049 gtk_widget_show(t->tab_elems.sep);
6052 TAILQ_FOREACH(t, &tabs, entry) {
6053 if (t->tab_id == maxid) {
6054 gtk_widget_hide(t->tab_elems.sep);
6055 break;
6060 /* after active tab change */
6061 void
6062 recolor_compact_tabs(void)
6064 struct tab *t;
6065 int curid = 0;
6066 GdkColor color;
6068 gdk_color_parse(XT_COLOR_CT_INACTIVE, &color);
6069 TAILQ_FOREACH(t, &tabs, entry)
6070 gtk_widget_modify_fg(t->tab_elems.label, GTK_STATE_NORMAL,
6071 &color);
6073 curid = gtk_notebook_get_current_page(notebook);
6074 TAILQ_FOREACH(t, &tabs, entry)
6075 if (t->tab_id == curid) {
6076 gdk_color_parse(XT_COLOR_CT_ACTIVE, &color);
6077 gtk_widget_modify_fg(t->tab_elems.label,
6078 GTK_STATE_NORMAL, &color);
6079 break;
6083 void
6084 set_current_tab(int page_num)
6086 buffercmd_abort(get_current_tab());
6087 gtk_notebook_set_current_page(notebook, page_num);
6088 recolor_compact_tabs();
6092 undo_close_tab_save(struct tab *t)
6094 int m, n;
6095 const gchar *uri;
6096 struct undo *u1, *u2;
6097 GList *items;
6098 WebKitWebHistoryItem *item;
6100 if ((uri = get_uri(t)) == NULL)
6101 return (1);
6103 u1 = g_malloc0(sizeof(struct undo));
6104 u1->uri = g_strdup(uri);
6106 t->bfl = webkit_web_view_get_back_forward_list(t->wv);
6108 m = webkit_web_back_forward_list_get_forward_length(t->bfl);
6109 n = webkit_web_back_forward_list_get_back_length(t->bfl);
6110 u1->back = n;
6112 /* forward history */
6113 items = webkit_web_back_forward_list_get_forward_list_with_limit(t->bfl, m);
6115 while (items) {
6116 item = items->data;
6117 u1->history = g_list_prepend(u1->history,
6118 webkit_web_history_item_copy(item));
6119 items = g_list_next(items);
6122 /* current item */
6123 if (m) {
6124 item = webkit_web_back_forward_list_get_current_item(t->bfl);
6125 u1->history = g_list_prepend(u1->history,
6126 webkit_web_history_item_copy(item));
6129 /* back history */
6130 items = webkit_web_back_forward_list_get_back_list_with_limit(t->bfl, n);
6132 while (items) {
6133 item = items->data;
6134 u1->history = g_list_prepend(u1->history,
6135 webkit_web_history_item_copy(item));
6136 items = g_list_next(items);
6139 TAILQ_INSERT_HEAD(&undos, u1, entry);
6141 if (undo_count > XT_MAX_UNDO_CLOSE_TAB) {
6142 u2 = TAILQ_LAST(&undos, undo_tailq);
6143 TAILQ_REMOVE(&undos, u2, entry);
6144 g_free(u2->uri);
6145 g_list_free(u2->history);
6146 g_free(u2);
6147 } else
6148 undo_count++;
6150 return (0);
6153 void
6154 delete_tab(struct tab *t)
6156 struct karg a;
6158 DNPRINTF(XT_D_TAB, "delete_tab: %p\n", t);
6160 if (t == NULL)
6161 return;
6164 * no need to join thread here because it won't access t on completion
6167 TAILQ_REMOVE(&tabs, t, entry);
6168 buffercmd_abort(t);
6170 /* Halt all webkit activity. */
6171 abort_favicon_download(t);
6172 webkit_web_view_stop_loading(t->wv);
6174 /* Save the tab, so we can undo the close. */
6175 undo_close_tab_save(t);
6177 /* just in case */
6178 if (t->search_id)
6179 g_source_remove(t->search_id);
6181 /* inspector */
6182 bzero(&a, sizeof a);
6183 a.i = XT_INS_CLOSE;
6184 inspector_cmd(t, &a);
6186 if (browser_mode == XT_BM_KIOSK) {
6187 gtk_widget_destroy(t->uri_entry);
6188 gtk_widget_destroy(t->stop);
6189 gtk_widget_destroy(t->js_toggle);
6192 gtk_widget_destroy(t->tab_elems.eventbox);
6193 gtk_widget_destroy(t->vbox);
6195 g_free(t->user_agent);
6196 g_free(t->stylesheet);
6197 g_free(t->tmp_uri);
6198 g_free(t);
6199 t = NULL;
6201 if (TAILQ_EMPTY(&tabs)) {
6202 if (browser_mode == XT_BM_KIOSK)
6203 create_new_tab(home, NULL, 1, -1);
6204 else
6205 create_new_tab(NULL, NULL, 1, -1);
6208 /* recreate session */
6209 if (session_autosave) {
6210 bzero(&a, sizeof a);
6211 a.s = NULL;
6212 save_tabs(NULL, &a);
6215 recalc_tabs();
6216 recolor_compact_tabs();
6219 void
6220 update_statusbar_zoom(struct tab *t)
6222 gfloat zoom;
6223 char s[16] = { '\0' };
6225 g_object_get(G_OBJECT(t->wv), "zoom-level", &zoom, (char *)NULL);
6226 if ((zoom <= 0.99 || zoom >= 1.01))
6227 snprintf(s, sizeof s, "%d%%", (int)(zoom * 100));
6228 gtk_entry_set_text(GTK_ENTRY(t->sbe.zoom), s);
6231 void
6232 setzoom_webkit(struct tab *t, int adjust)
6234 #define XT_ZOOMPERCENT 0.04
6236 gfloat zoom;
6238 if (t == NULL) {
6239 show_oops(NULL, "setzoom_webkit invalid parameters");
6240 return;
6243 g_object_get(G_OBJECT(t->wv), "zoom-level", &zoom, (char *)NULL);
6244 if (adjust == XT_ZOOM_IN)
6245 zoom += XT_ZOOMPERCENT;
6246 else if (adjust == XT_ZOOM_OUT)
6247 zoom -= XT_ZOOMPERCENT;
6248 else if (adjust > 0)
6249 zoom = default_zoom_level + adjust / 100.0 - 1.0;
6250 else {
6251 show_oops(t, "setzoom_webkit invalid zoom value");
6252 return;
6255 if (zoom < XT_ZOOMPERCENT)
6256 zoom = XT_ZOOMPERCENT;
6257 g_object_set(G_OBJECT(t->wv), "zoom-level", zoom, (char *)NULL);
6258 update_statusbar_zoom(t);
6261 gboolean
6262 tab_clicked_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
6264 struct tab *t = (struct tab *) data;
6266 DNPRINTF(XT_D_TAB, "tab_clicked_cb: tab: %d\n", t->tab_id);
6268 switch (event->button) {
6269 case 1:
6270 set_current_tab(t->tab_id);
6271 break;
6272 case 2:
6273 delete_tab(t);
6274 break;
6277 return TRUE;
6280 void
6281 append_tab(struct tab *t)
6283 if (t == NULL)
6284 return;
6286 TAILQ_INSERT_TAIL(&tabs, t, entry);
6287 t->tab_id = gtk_notebook_append_page(notebook, t->vbox, t->tab_content);
6290 GtkWidget *
6291 create_sbe(int width)
6293 GtkWidget *sbe;
6295 sbe = gtk_entry_new();
6296 gtk_entry_set_inner_border(GTK_ENTRY(sbe), NULL);
6297 gtk_entry_set_has_frame(GTK_ENTRY(sbe), FALSE);
6298 gtk_widget_set_can_focus(GTK_WIDGET(sbe), FALSE);
6299 gtk_widget_modify_font(GTK_WIDGET(sbe), statusbar_font);
6300 gtk_entry_set_alignment(GTK_ENTRY(sbe), 1.0);
6301 gtk_widget_set_size_request(sbe, width, -1);
6303 return sbe;
6306 struct tab *
6307 create_new_tab(char *title, struct undo *u, int focus, int position)
6309 struct tab *t;
6310 int load = 1, id;
6311 GtkWidget *b, *bb;
6312 WebKitWebHistoryItem *item;
6313 GList *items;
6314 GdkColor color;
6315 char *p;
6316 int sbe_p = 0, sbe_b = 0,
6317 sbe_z = 0;
6319 DNPRINTF(XT_D_TAB, "create_new_tab: title %s focus %d\n", title, focus);
6321 if (tabless && !TAILQ_EMPTY(&tabs)) {
6322 DNPRINTF(XT_D_TAB, "create_new_tab: new tab rejected\n");
6323 return (NULL);
6326 t = g_malloc0(sizeof *t);
6328 if (title == NULL) {
6329 title = "(untitled)";
6330 load = 0;
6333 t->vbox = gtk_vbox_new(FALSE, 0);
6335 /* label + button for tab */
6336 b = gtk_hbox_new(FALSE, 0);
6337 t->tab_content = b;
6339 #if GTK_CHECK_VERSION(2, 20, 0)
6340 t->spinner = gtk_spinner_new();
6341 #endif
6342 t->label = gtk_label_new(title);
6343 bb = create_button("Close", GTK_STOCK_CLOSE, 1);
6344 gtk_widget_set_size_request(t->label, 100, 0);
6345 gtk_label_set_max_width_chars(GTK_LABEL(t->label), 20);
6346 gtk_label_set_ellipsize(GTK_LABEL(t->label), PANGO_ELLIPSIZE_END);
6347 gtk_widget_set_size_request(b, 130, 0);
6349 gtk_box_pack_start(GTK_BOX(b), bb, FALSE, FALSE, 0);
6350 gtk_box_pack_start(GTK_BOX(b), t->label, FALSE, FALSE, 0);
6351 #if GTK_CHECK_VERSION(2, 20, 0)
6352 gtk_box_pack_start(GTK_BOX(b), t->spinner, FALSE, FALSE, 0);
6353 #endif
6355 /* toolbar */
6356 if (browser_mode == XT_BM_KIOSK) {
6357 t->toolbar = create_kiosk_toolbar(t);
6358 gtk_box_pack_start(GTK_BOX(t->vbox), t->toolbar, FALSE, FALSE,
6360 } else {
6361 t->toolbar = create_toolbar(t);
6362 if (fancy_bar)
6363 gtk_box_pack_start(GTK_BOX(t->vbox), t->toolbar, FALSE,
6364 FALSE, 0);
6367 /* marks */
6368 marks_clear(t);
6370 /* browser */
6371 t->browser_win = create_browser(t);
6372 gtk_box_pack_start(GTK_BOX(t->vbox), t->browser_win, TRUE, TRUE, 0);
6374 /* oops message for user feedback */
6375 t->oops = gtk_entry_new();
6376 gtk_entry_set_inner_border(GTK_ENTRY(t->oops), NULL);
6377 gtk_entry_set_has_frame(GTK_ENTRY(t->oops), FALSE);
6378 gtk_widget_set_can_focus(GTK_WIDGET(t->oops), FALSE);
6379 gdk_color_parse(XT_COLOR_RED, &color);
6380 gtk_widget_modify_base(t->oops, GTK_STATE_NORMAL, &color);
6381 gtk_box_pack_end(GTK_BOX(t->vbox), t->oops, FALSE, FALSE, 0);
6382 gtk_widget_modify_font(GTK_WIDGET(t->oops), oops_font);
6384 /* command entry */
6385 t->cmd = gtk_entry_new();
6386 gtk_entry_set_inner_border(GTK_ENTRY(t->cmd), NULL);
6387 gtk_entry_set_has_frame(GTK_ENTRY(t->cmd), FALSE);
6388 gtk_box_pack_end(GTK_BOX(t->vbox), t->cmd, FALSE, FALSE, 0);
6389 gtk_widget_modify_font(GTK_WIDGET(t->cmd), cmd_font);
6391 /* status bar */
6392 t->statusbar_box = gtk_hbox_new(FALSE, 0);
6394 t->sbe.statusbar = gtk_entry_new();
6395 gtk_entry_set_inner_border(GTK_ENTRY(t->sbe.statusbar), NULL);
6396 gtk_entry_set_has_frame(GTK_ENTRY(t->sbe.statusbar), FALSE);
6397 gtk_widget_set_can_focus(GTK_WIDGET(t->sbe.statusbar), FALSE);
6398 gtk_widget_modify_font(GTK_WIDGET(t->sbe.statusbar), statusbar_font);
6400 /* create these widgets only if specified in statusbar_elems */
6402 t->sbe.position = create_sbe(40);
6403 t->sbe.zoom = create_sbe(40);
6404 t->sbe.buffercmd = create_sbe(60);
6406 statusbar_modify_attr(t, XT_COLOR_WHITE, XT_COLOR_BLACK);
6408 gtk_box_pack_start(GTK_BOX(t->statusbar_box), t->sbe.statusbar, TRUE,
6409 TRUE, FALSE);
6411 /* gtk widgets cannot be added to a box twice. sbe_* variables
6412 make sure of this */
6413 for (p = statusbar_elems; *p != '\0'; p++) {
6414 switch (*p) {
6415 case '|':
6417 GtkWidget *sep = gtk_vseparator_new();
6419 gdk_color_parse(XT_COLOR_SB_SEPARATOR, &color);
6420 gtk_widget_modify_bg(sep, GTK_STATE_NORMAL, &color);
6421 gtk_box_pack_start(GTK_BOX(t->statusbar_box), sep,
6422 FALSE, FALSE, FALSE);
6423 break;
6425 case 'P':
6426 if (sbe_p) {
6427 warnx("flag \"%c\" specified more than "
6428 "once in statusbar_elems\n", *p);
6429 break;
6431 sbe_p = 1;
6432 gtk_box_pack_start(GTK_BOX(t->statusbar_box),
6433 t->sbe.position, FALSE, FALSE, FALSE);
6434 break;
6435 case 'B':
6436 if (sbe_b) {
6437 warnx("flag \"%c\" specified more than "
6438 "once in statusbar_elems\n", *p);
6439 break;
6441 sbe_b = 1;
6442 gtk_box_pack_start(GTK_BOX(t->statusbar_box),
6443 t->sbe.buffercmd, FALSE, FALSE, FALSE);
6444 break;
6445 case 'Z':
6446 if (sbe_z) {
6447 warnx("flag \"%c\" specified more than "
6448 "once in statusbar_elems\n", *p);
6449 break;
6451 sbe_z = 1;
6452 gtk_box_pack_start(GTK_BOX(t->statusbar_box),
6453 t->sbe.zoom, FALSE, FALSE, FALSE);
6454 break;
6455 default:
6456 warnx("illegal flag \"%c\" in statusbar_elems\n", *p);
6457 break;
6461 gtk_box_pack_end(GTK_BOX(t->vbox), t->statusbar_box, FALSE, FALSE, 0);
6463 /* buffer list */
6464 t->buffers = create_buffers(t);
6465 gtk_box_pack_end(GTK_BOX(t->vbox), t->buffers, FALSE, FALSE, 0);
6467 /* xtp meaning is normal by default */
6468 t->xtp_meaning = XT_XTP_TAB_MEANING_NORMAL;
6470 /* set empty favicon */
6471 xt_icon_from_name(t, "text-html");
6473 /* and show it all */
6474 gtk_widget_show_all(b);
6475 gtk_widget_show_all(t->vbox);
6477 /* compact tab bar */
6478 t->tab_elems.label = gtk_label_new(title);
6479 t->tab_elems.favicon = gtk_image_new();
6480 gtk_label_set_width_chars(GTK_LABEL(t->tab_elems.label), 1.0);
6481 gtk_misc_set_alignment(GTK_MISC(t->tab_elems.label), 0.0, 0.0);
6482 gtk_misc_set_padding(GTK_MISC(t->tab_elems.label), 4.0, 4.0);
6483 gtk_widget_modify_font(GTK_WIDGET(t->tab_elems.label), tabbar_font);
6485 t->tab_elems.eventbox = gtk_event_box_new();
6486 t->tab_elems.box = gtk_hbox_new(FALSE, 0);
6487 t->tab_elems.sep = gtk_vseparator_new();
6489 gdk_color_parse(XT_COLOR_CT_BACKGROUND, &color);
6490 gtk_widget_modify_bg(t->tab_elems.eventbox, GTK_STATE_NORMAL, &color);
6491 gdk_color_parse(XT_COLOR_CT_INACTIVE, &color);
6492 gtk_widget_modify_fg(t->tab_elems.label, GTK_STATE_NORMAL, &color);
6493 gdk_color_parse(XT_COLOR_CT_SEPARATOR, &color);
6494 gtk_widget_modify_bg(t->tab_elems.sep, GTK_STATE_NORMAL, &color);
6496 gtk_box_pack_start(GTK_BOX(t->tab_elems.box), t->tab_elems.favicon, FALSE,
6497 FALSE, 0);
6498 gtk_box_pack_start(GTK_BOX(t->tab_elems.box), t->tab_elems.label, TRUE,
6499 TRUE, 0);
6500 gtk_box_pack_start(GTK_BOX(t->tab_elems.box), t->tab_elems.sep, FALSE,
6501 FALSE, 0);
6502 gtk_container_add(GTK_CONTAINER(t->tab_elems.eventbox),
6503 t->tab_elems.box);
6505 gtk_box_pack_start(GTK_BOX(tab_bar), t->tab_elems.eventbox, TRUE,
6506 TRUE, 0);
6507 gtk_widget_show_all(t->tab_elems.eventbox);
6509 if (append_next == 0 || gtk_notebook_get_n_pages(notebook) == 0)
6510 append_tab(t);
6511 else {
6512 id = position >= 0 ? position :
6513 gtk_notebook_get_current_page(notebook) + 1;
6514 if (id > gtk_notebook_get_n_pages(notebook))
6515 append_tab(t);
6516 else {
6517 TAILQ_INSERT_TAIL(&tabs, t, entry);
6518 gtk_notebook_insert_page(notebook, t->vbox, b, id);
6519 gtk_box_reorder_child(GTK_BOX(tab_bar),
6520 t->tab_elems.eventbox, id);
6521 recalc_tabs();
6525 #if GTK_CHECK_VERSION(2, 20, 0)
6526 /* turn spinner off if we are a new tab without uri */
6527 if (!load) {
6528 gtk_spinner_stop(GTK_SPINNER(t->spinner));
6529 gtk_widget_hide(t->spinner);
6531 #endif
6532 /* make notebook tabs reorderable */
6533 gtk_notebook_set_tab_reorderable(notebook, t->vbox, TRUE);
6535 /* compact tabs clickable */
6536 g_signal_connect(G_OBJECT(t->tab_elems.eventbox),
6537 "button_press_event", G_CALLBACK(tab_clicked_cb), t);
6539 g_object_connect(G_OBJECT(t->cmd),
6540 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb), t,
6541 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb), t,
6542 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb), t,
6543 "signal::activate", G_CALLBACK(cmd_activate_cb), t,
6544 "signal::populate-popup", G_CALLBACK(cmd_popup_cb), t,
6545 (char *)NULL);
6547 /* reuse wv_button_cb to hide oops */
6548 g_object_connect(G_OBJECT(t->oops),
6549 "signal::button_press_event", G_CALLBACK(wv_button_cb), t,
6550 (char *)NULL);
6552 g_signal_connect(t->buffers,
6553 "row-activated", G_CALLBACK(row_activated_cb), t);
6554 g_object_connect(G_OBJECT(t->buffers),
6555 "signal::key-press-event", G_CALLBACK(wv_keypress_cb), t, (char *)NULL);
6557 g_object_connect(G_OBJECT(t->wv),
6558 "signal::key-press-event", G_CALLBACK(wv_keypress_cb), t,
6559 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb), t,
6560 "signal::download-requested", G_CALLBACK(webview_download_cb), t,
6561 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb), t,
6562 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb), t,
6563 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb), t,
6564 "signal::create-web-view", G_CALLBACK(webview_cwv_cb), t,
6565 "signal::close-web-view", G_CALLBACK(webview_closewv_cb), t,
6566 "signal::event", G_CALLBACK(webview_event_cb), t,
6567 "signal::load-finished", G_CALLBACK(webview_load_finished_cb), t,
6568 "signal::load-progress-changed", G_CALLBACK(webview_progress_changed_cb), t,
6569 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb), t,
6570 "signal::button_press_event", G_CALLBACK(wv_button_cb), t,
6571 "signal::button_release_event", G_CALLBACK(wv_release_button_cb), t,
6572 "signal::populate-popup", G_CALLBACK(wv_popup_cb), t,
6573 (char *)NULL);
6574 g_signal_connect(t->wv,
6575 "notify::load-status", G_CALLBACK(notify_load_status_cb), t);
6576 g_signal_connect(t->wv,
6577 "notify::title", G_CALLBACK(notify_title_cb), t);
6579 /* hijack the unused keys as if we were the browser */
6580 //g_object_connect(G_OBJECT(t->toolbar),
6581 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
6582 // (char *)NULL);
6584 g_signal_connect(G_OBJECT(bb), "button_press_event",
6585 G_CALLBACK(tab_close_cb), t);
6587 /* setup history */
6588 t->bfl = webkit_web_view_get_back_forward_list(t->wv);
6589 /* restore the tab's history */
6590 if (u && u->history) {
6591 items = u->history;
6592 while (items) {
6593 item = items->data;
6594 webkit_web_back_forward_list_add_item(t->bfl, item);
6595 items = g_list_next(items);
6598 item = g_list_nth_data(u->history, u->back);
6599 if (item)
6600 webkit_web_view_go_to_back_forward_item(t->wv, item);
6602 g_list_free(items);
6603 g_list_free(u->history);
6604 } else
6605 webkit_web_back_forward_list_clear(t->bfl);
6607 /* hide stuff */
6608 hide_cmd(t);
6609 hide_oops(t);
6610 hide_buffers(t);
6611 url_set_visibility();
6612 statusbar_set_visibility();
6614 if (focus) {
6615 set_current_tab(t->tab_id);
6616 DNPRINTF(XT_D_TAB, "create_new_tab: going to tab: %d\n",
6617 t->tab_id);
6619 if (load) {
6620 gtk_entry_set_text(GTK_ENTRY(t->uri_entry), title);
6621 load_uri(t, title);
6622 } else {
6623 if (show_url == 1)
6624 gtk_widget_grab_focus(GTK_WIDGET(t->uri_entry));
6625 else
6626 focus_webview(t);
6628 } else if (load)
6629 load_uri(t, title);
6631 if (userstyle_global)
6632 apply_style(t);
6634 recolor_compact_tabs();
6635 setzoom_webkit(t, XT_ZOOM_NORMAL);
6636 return (t);
6639 void
6640 notebook_switchpage_cb(GtkNotebook *nb, GtkWidget *nbp, guint pn,
6641 gpointer *udata)
6643 struct tab *t;
6644 const gchar *uri;
6646 DNPRINTF(XT_D_TAB, "notebook_switchpage_cb: tab: %d\n", pn);
6648 if (gtk_notebook_get_current_page(notebook) == -1)
6649 recalc_tabs();
6651 TAILQ_FOREACH(t, &tabs, entry) {
6652 if (t->tab_id == pn) {
6653 DNPRINTF(XT_D_TAB, "notebook_switchpage_cb: going to "
6654 "%d\n", pn);
6656 uri = get_title(t, TRUE);
6657 gtk_window_set_title(GTK_WINDOW(main_window), uri);
6659 hide_cmd(t);
6660 hide_oops(t);
6662 if (t->focus_wv) {
6663 /* can't use focus_webview here */
6664 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
6670 void
6671 notebook_pagereordered_cb(GtkNotebook *nb, GtkWidget *nbp, guint pn,
6672 gpointer *udata)
6674 struct tab *t = NULL, *tt;
6676 recalc_tabs();
6678 TAILQ_FOREACH(tt, &tabs, entry)
6679 if (tt->tab_id == pn) {
6680 t = tt;
6681 break;
6683 if (t == NULL)
6684 return;
6685 DNPRINTF(XT_D_TAB, "page_reordered_cb: tab: %d\n", t->tab_id);
6687 gtk_box_reorder_child(GTK_BOX(tab_bar), t->tab_elems.eventbox,
6688 t->tab_id);
6691 void
6692 menuitem_response(struct tab *t)
6694 gtk_notebook_set_current_page(notebook, t->tab_id);
6697 gboolean
6698 arrow_cb(GtkWidget *w, GdkEventButton *event, gpointer user_data)
6700 GtkWidget *menu, *menu_items;
6701 GdkEventButton *bevent;
6702 const gchar *uri;
6703 struct tab *ti;
6705 if (event->type == GDK_BUTTON_PRESS) {
6706 bevent = (GdkEventButton *) event;
6707 menu = gtk_menu_new();
6709 TAILQ_FOREACH(ti, &tabs, entry) {
6710 if ((uri = get_uri(ti)) == NULL)
6711 /* XXX make sure there is something to print */
6712 /* XXX add gui pages in here to look purdy */
6713 uri = "(untitled)";
6714 menu_items = gtk_menu_item_new_with_label(uri);
6715 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_items);
6716 gtk_widget_show(menu_items);
6718 g_signal_connect_swapped((menu_items),
6719 "activate", G_CALLBACK(menuitem_response),
6720 (gpointer)ti);
6723 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
6724 bevent->button, bevent->time);
6726 /* unref object so it'll free itself when popped down */
6727 #if !GTK_CHECK_VERSION(3, 0, 0)
6728 /* XXX does not need unref with gtk+3? */
6729 g_object_ref_sink(menu);
6730 g_object_unref(menu);
6731 #endif
6733 return (TRUE /* eat event */);
6736 return (FALSE /* propagate */);
6740 icon_size_map(int iconsz)
6742 if (iconsz <= GTK_ICON_SIZE_INVALID ||
6743 iconsz > GTK_ICON_SIZE_DIALOG)
6744 return (GTK_ICON_SIZE_SMALL_TOOLBAR);
6746 return (iconsz);
6749 GtkWidget *
6750 create_button(char *name, char *stockid, int size)
6752 GtkWidget *button, *image;
6753 gchar *rcstring;
6754 int gtk_icon_size;
6756 rcstring = g_strdup_printf(
6757 "style \"%s-style\"\n"
6758 "{\n"
6759 " GtkWidget::focus-padding = 0\n"
6760 " GtkWidget::focus-line-width = 0\n"
6761 " xthickness = 0\n"
6762 " ythickness = 0\n"
6763 "}\n"
6764 "widget \"*.%s\" style \"%s-style\"", name, name, name);
6765 gtk_rc_parse_string(rcstring);
6766 g_free(rcstring);
6767 button = gtk_button_new();
6768 gtk_button_set_focus_on_click(GTK_BUTTON(button), FALSE);
6769 gtk_icon_size = icon_size_map(size ? size : icon_size);
6771 image = gtk_image_new_from_stock(stockid, gtk_icon_size);
6772 gtk_widget_set_size_request(GTK_WIDGET(image), -1, -1);
6773 gtk_container_set_border_width(GTK_CONTAINER(button), 1);
6774 gtk_container_add(GTK_CONTAINER(button), GTK_WIDGET(image));
6775 gtk_widget_set_name(button, name);
6776 gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
6778 return (button);
6781 void
6782 button_set_stockid(GtkWidget *button, char *stockid)
6784 GtkWidget *image;
6786 image = gtk_image_new_from_stock(stockid, icon_size_map(icon_size));
6787 gtk_widget_set_size_request(GTK_WIDGET(image), -1, -1);
6788 gtk_button_set_image(GTK_BUTTON(button), image);
6791 void
6792 clipb_primary_cb(GtkClipboard *primary, GdkEvent *event, gpointer notused)
6794 gchar *p = NULL;
6795 GdkAtom atom = gdk_atom_intern("CUT_BUFFER0", FALSE);
6796 gint len;
6798 if (xterm_workaround == 0)
6799 return;
6802 * xterm doesn't play nice with clipboards because it clears the
6803 * primary when clicked. We rely on primary being set to properly
6804 * handle middle mouse button clicks (paste). So when someone clears
6805 * primary copy whatever is in CUT_BUFFER0 into primary to simualte
6806 * other application behavior (as in DON'T clear primary).
6809 p = gtk_clipboard_wait_for_text(primary);
6810 if (p == NULL) {
6811 if (gdk_property_get(gdk_get_default_root_window(),
6812 atom,
6813 gdk_atom_intern("STRING", FALSE),
6815 1024 * 1024 /* picked out of my butt */,
6816 FALSE,
6817 NULL,
6818 NULL,
6819 &len,
6820 (guchar **)&p)) {
6821 /* yes sir, we need to NUL the string */
6822 p[len] = '\0';
6823 gtk_clipboard_set_text(primary, p, -1);
6827 if (p)
6828 g_free(p);
6831 void
6832 create_canvas(void)
6834 GtkWidget *vbox;
6835 GList *l = NULL;
6836 GdkPixbuf *pb;
6837 char file[PATH_MAX];
6838 int i;
6840 vbox = gtk_vbox_new(FALSE, 0);
6841 gtk_box_set_spacing(GTK_BOX(vbox), 0);
6842 notebook = GTK_NOTEBOOK(gtk_notebook_new());
6843 #if !GTK_CHECK_VERSION(3, 0, 0)
6844 /* XXX seems to be needed with gtk+2 */
6845 gtk_notebook_set_tab_hborder(notebook, 0);
6846 gtk_notebook_set_tab_vborder(notebook, 0);
6847 #endif
6848 gtk_notebook_set_scrollable(notebook, TRUE);
6849 gtk_notebook_set_show_border(notebook, FALSE);
6850 gtk_widget_set_can_focus(GTK_WIDGET(notebook), FALSE);
6852 abtn = gtk_button_new();
6853 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
6854 gtk_widget_set_size_request(arrow, -1, -1);
6855 gtk_container_add(GTK_CONTAINER(abtn), arrow);
6856 gtk_widget_set_size_request(abtn, -1, 20);
6858 #if GTK_CHECK_VERSION(2, 20, 0)
6859 gtk_notebook_set_action_widget(notebook, abtn, GTK_PACK_END);
6860 #endif
6861 gtk_widget_set_size_request(GTK_WIDGET(notebook), -1, -1);
6863 /* compact tab bar */
6864 tab_bar = gtk_hbox_new(TRUE, 0);
6866 gtk_box_pack_start(GTK_BOX(vbox), tab_bar, FALSE, FALSE, 0);
6867 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(notebook), TRUE, TRUE, 0);
6868 gtk_widget_set_size_request(vbox, -1, -1);
6870 g_object_connect(G_OBJECT(notebook),
6871 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb), NULL,
6872 (char *)NULL);
6873 g_object_connect(G_OBJECT(notebook),
6874 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb),
6875 NULL, (char *)NULL);
6876 g_signal_connect(G_OBJECT(abtn), "button_press_event",
6877 G_CALLBACK(arrow_cb), NULL);
6879 main_window = create_window("xxxterm");
6880 gtk_container_add(GTK_CONTAINER(main_window), vbox);
6881 g_signal_connect(G_OBJECT(main_window), "delete_event",
6882 G_CALLBACK(gtk_main_quit), NULL);
6884 /* icons */
6885 for (i = 0; i < LENGTH(icons); i++) {
6886 snprintf(file, sizeof file, "%s/%s", resource_dir, icons[i]);
6887 pb = gdk_pixbuf_new_from_file(file, NULL);
6888 l = g_list_append(l, pb);
6890 gtk_window_set_default_icon_list(l);
6892 /* clipboard work around */
6893 if (xterm_workaround)
6894 g_signal_connect(
6895 G_OBJECT(gtk_clipboard_get(GDK_SELECTION_PRIMARY)),
6896 "owner-change", G_CALLBACK(clipb_primary_cb), NULL);
6898 gtk_widget_show_all(abtn);
6899 gtk_widget_show_all(main_window);
6900 notebook_tab_set_visibility();
6904 send_cmd_to_socket(char *cmd)
6906 int s, len, rv = 1;
6907 struct sockaddr_un sa;
6909 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
6910 warnx("%s: socket", __func__);
6911 return (rv);
6914 sa.sun_family = AF_UNIX;
6915 snprintf(sa.sun_path, sizeof(sa.sun_path), "%s/%s",
6916 work_dir, XT_SOCKET_FILE);
6917 len = SUN_LEN(&sa);
6919 if (connect(s, (struct sockaddr *)&sa, len) == -1) {
6920 warnx("%s: connect", __func__);
6921 goto done;
6924 if (send(s, cmd, strlen(cmd) + 1, 0) == -1) {
6925 warnx("%s: send", __func__);
6926 goto done;
6929 rv = 0;
6930 done:
6931 close(s);
6932 return (rv);
6935 gboolean
6936 socket_watcher(GIOChannel *source, GIOCondition condition, gpointer data)
6938 int s, n;
6939 char str[XT_MAX_URL_LENGTH];
6940 socklen_t t = sizeof(struct sockaddr_un);
6941 struct sockaddr_un sa;
6942 struct passwd *p;
6943 uid_t uid;
6944 gid_t gid;
6945 struct tab *tt;
6946 gint fd = g_io_channel_unix_get_fd(source);
6948 if ((s = accept(fd, (struct sockaddr *)&sa, &t)) == -1) {
6949 warn("accept");
6950 return (FALSE);
6953 if (getpeereid(s, &uid, &gid) == -1) {
6954 warn("getpeereid");
6955 return (FALSE);
6957 if (uid != getuid() || gid != getgid()) {
6958 warnx("unauthorized user");
6959 return (FALSE);
6962 p = getpwuid(uid);
6963 if (p == NULL) {
6964 warnx("not a valid user");
6965 return (FALSE);
6968 n = recv(s, str, sizeof(str), 0);
6969 if (n <= 0)
6970 return (TRUE);
6972 tt = TAILQ_LAST(&tabs, tab_list);
6973 cmd_execute(tt, str);
6974 return (TRUE);
6978 is_running(void)
6980 int s, len, rv = 1;
6981 struct sockaddr_un sa;
6983 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
6984 warn("is_running: socket");
6985 return (-1);
6988 sa.sun_family = AF_UNIX;
6989 snprintf(sa.sun_path, sizeof(sa.sun_path), "%s/%s",
6990 work_dir, XT_SOCKET_FILE);
6991 len = SUN_LEN(&sa);
6993 /* connect to see if there is a listener */
6994 if (connect(s, (struct sockaddr *)&sa, len) == -1)
6995 rv = 0; /* not running */
6996 else
6997 rv = 1; /* already running */
6999 close(s);
7001 return (rv);
7005 build_socket(void)
7007 int s, len;
7008 struct sockaddr_un sa;
7010 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
7011 warn("build_socket: socket");
7012 return (-1);
7015 sa.sun_family = AF_UNIX;
7016 snprintf(sa.sun_path, sizeof(sa.sun_path), "%s/%s",
7017 work_dir, XT_SOCKET_FILE);
7018 len = SUN_LEN(&sa);
7020 /* connect to see if there is a listener */
7021 if (connect(s, (struct sockaddr *)&sa, len) == -1) {
7022 /* no listener so we will */
7023 unlink(sa.sun_path);
7025 if (bind(s, (struct sockaddr *)&sa, len) == -1) {
7026 warn("build_socket: bind");
7027 goto done;
7030 if (listen(s, 1) == -1) {
7031 warn("build_socket: listen");
7032 goto done;
7035 return (s);
7038 done:
7039 close(s);
7040 return (-1);
7043 void
7044 xxx_dir(char *dir)
7046 struct stat sb;
7048 if (stat(dir, &sb)) {
7049 if (mkdir(dir, S_IRWXU) == -1)
7050 err(1, "mkdir %s", dir);
7051 if (stat(dir, &sb))
7052 err(1, "stat %s", dir);
7054 if (S_ISDIR(sb.st_mode) == 0)
7055 errx(1, "%s not a dir", dir);
7056 if (((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))) != S_IRWXU) {
7057 warnx("fixing invalid permissions on %s", dir);
7058 if (chmod(dir, S_IRWXU) == -1)
7059 err(1, "chmod %s", dir);
7063 void
7064 usage(void)
7066 fprintf(stderr,
7067 "%s [-nSTVt][-f file][-s session] url ...\n", __progname);
7068 exit(0);
7071 GStaticRecMutex my_gdk_mtx = G_STATIC_REC_MUTEX_INIT;
7072 volatile int mtx_depth;
7073 int mtx_complain;
7076 * The linux flash plugin violates the gdk locking mechanism.
7077 * Work around the issue by using a recursive mutex with some match applied
7078 * to see if we hit a buggy condition.
7080 * The following code is painful so just don't read it. It really doesn't
7081 * make much sense but seems to work.
7083 void
7084 mtx_lock(void)
7086 g_static_rec_mutex_lock(&my_gdk_mtx);
7087 mtx_depth++;
7089 if (mtx_depth <= 0) {
7090 /* should not happen */
7091 show_oops(NULL, "negative mutex locking bug, trying to "
7092 "correct");
7093 fprintf(stderr, "negative mutex locking bug, trying to "
7094 "correct\n");
7095 g_static_rec_mutex_unlock_full(&my_gdk_mtx);
7096 g_static_rec_mutex_lock(&my_gdk_mtx);
7097 mtx_depth = 1;
7098 return;
7101 if (mtx_depth != 1) {
7102 /* decrease mutext depth to 1 */
7103 do {
7104 g_static_rec_mutex_unlock(&my_gdk_mtx);
7105 mtx_depth--;
7106 } while (mtx_depth > 1);
7110 void
7111 mtx_unlock(void)
7113 guint x;
7115 /* if mutex depth isn't 1 then something went bad */
7116 if (mtx_depth != 1) {
7117 x = g_static_rec_mutex_unlock_full(&my_gdk_mtx);
7118 if (x != 1) {
7119 /* should not happen */
7120 show_oops(NULL, "mutex unlocking bug, trying to "
7121 "correct");
7122 fprintf(stderr, "mutex unlocking bug, trying to "
7123 "correct\n");
7125 mtx_depth = 0;
7126 if (mtx_complain == 0) {
7127 show_oops(NULL, "buggy mutex implementation detected, "
7128 "work around implemented");
7129 fprintf(stderr, "buggy mutex implementation detected, "
7130 "work around implemented");
7131 mtx_complain = 1;
7133 return;
7136 mtx_depth--;
7137 g_static_rec_mutex_unlock(&my_gdk_mtx);
7141 main(int argc, char *argv[])
7143 struct stat sb;
7144 int c, s, optn = 0, opte = 0, focus = 1;
7145 char conf[PATH_MAX] = { '\0' };
7146 char file[PATH_MAX];
7147 char *env_proxy = NULL;
7148 char *cmd = NULL;
7149 FILE *f = NULL;
7150 struct karg a;
7151 struct sigaction sact;
7152 GIOChannel *channel;
7153 struct rlimit rlp;
7155 start_argv = argv;
7157 /* prepare gtk */
7158 #ifdef USE_THREADS
7159 g_thread_init(NULL);
7160 gdk_threads_set_lock_functions(mtx_lock, mtx_unlock);
7161 gdk_threads_init();
7162 gdk_threads_enter();
7164 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
7165 #endif
7166 gtk_init(&argc, &argv);
7168 gnutls_global_init();
7170 strlcpy(named_session, XT_SAVED_TABS_FILE, sizeof named_session);
7172 RB_INIT(&hl);
7173 RB_INIT(&js_wl);
7174 RB_INIT(&pl_wl);
7175 RB_INIT(&downloads);
7177 TAILQ_INIT(&sessions);
7178 TAILQ_INIT(&tabs);
7179 TAILQ_INIT(&mtl);
7180 TAILQ_INIT(&aliases);
7181 TAILQ_INIT(&undos);
7182 TAILQ_INIT(&kbl);
7183 TAILQ_INIT(&spl);
7184 TAILQ_INIT(&chl);
7185 TAILQ_INIT(&shl);
7186 TAILQ_INIT(&ua_list);
7188 /* fiddle with ulimits */
7189 if (getrlimit(RLIMIT_NOFILE, &rlp) == -1)
7190 warn("getrlimit");
7191 else {
7192 /* just use them all */
7193 rlp.rlim_cur = rlp.rlim_max;
7194 if (setrlimit(RLIMIT_NOFILE, &rlp) == -1)
7195 warn("setrlimit");
7196 if (getrlimit(RLIMIT_NOFILE, &rlp) == -1)
7197 warn("getrlimit");
7198 else if (rlp.rlim_cur <= 256)
7199 startpage_add("%s requires at least 256 file "
7200 "descriptors, currently it has up to %d available",
7201 __progname, rlp.rlim_cur);
7204 while ((c = getopt(argc, argv, "STVf:s:tne")) != -1) {
7205 switch (c) {
7206 case 'S':
7207 show_url = 0;
7208 break;
7209 case 'T':
7210 show_tabs = 0;
7211 break;
7212 case 'V':
7213 #ifdef XXXTERM_BUILDSTR
7214 errx(0 , "Version: %s Build: %s",
7215 version, XXXTERM_BUILDSTR);
7216 #else
7217 errx(0 , "Version: %s", version);
7218 #endif
7219 break;
7220 case 'f':
7221 strlcpy(conf, optarg, sizeof(conf));
7222 break;
7223 case 's':
7224 strlcpy(named_session, optarg, sizeof(named_session));
7225 break;
7226 case 't':
7227 tabless = 1;
7228 break;
7229 case 'n':
7230 optn = 1;
7231 break;
7232 case 'e':
7233 opte = 1;
7234 break;
7235 default:
7236 usage();
7237 /* NOTREACHED */
7240 argc -= optind;
7241 argv += optind;
7243 init_keybindings();
7245 xtp_generate_keys();
7247 /* signals */
7248 bzero(&sact, sizeof(sact));
7249 sigemptyset(&sact.sa_mask);
7250 sact.sa_handler = sigchild;
7251 sact.sa_flags = SA_NOCLDSTOP;
7252 sigaction(SIGCHLD, &sact, NULL);
7254 /* set download dir */
7255 pwd = getpwuid(getuid());
7256 if (pwd == NULL)
7257 errx(1, "invalid user %d", getuid());
7258 strlcpy(download_dir, pwd->pw_dir, sizeof download_dir);
7260 /* compile buffer command regexes */
7261 buffercmd_init();
7263 /* set default string settings */
7264 home = g_strdup("https://www.cyphertite.com");
7265 search_string = g_strdup("https://ssl.scroogle.org/cgi-bin/nbbwssl.cgi?Gw=%s");
7266 resource_dir = g_strdup("/usr/local/share/xxxterm/");
7267 strlcpy(runtime_settings, "runtime", sizeof runtime_settings);
7268 cmd_font_name = g_strdup("monospace normal 9");
7269 oops_font_name = g_strdup("monospace normal 9");
7270 statusbar_font_name = g_strdup("monospace normal 9");
7271 tabbar_font_name = g_strdup("monospace normal 9");
7272 statusbar_elems = g_strdup("BP");
7273 spell_check_languages = g_strdup("en_US");
7274 encoding = g_strdup("UTF-8");
7276 /* read config file */
7277 if (strlen(conf) == 0)
7278 snprintf(conf, sizeof conf, "%s/.%s",
7279 pwd->pw_dir, XT_CONF_FILE);
7280 config_parse(conf, 0);
7282 /* init fonts */
7283 cmd_font = pango_font_description_from_string(cmd_font_name);
7284 oops_font = pango_font_description_from_string(oops_font_name);
7285 statusbar_font = pango_font_description_from_string(statusbar_font_name);
7286 tabbar_font = pango_font_description_from_string(tabbar_font_name);
7288 /* working directory */
7289 if (strlen(work_dir) == 0)
7290 snprintf(work_dir, sizeof work_dir, "%s/%s",
7291 pwd->pw_dir, XT_DIR);
7292 xxx_dir(work_dir);
7294 /* icon cache dir */
7295 snprintf(cache_dir, sizeof cache_dir, "%s/%s", work_dir, XT_CACHE_DIR);
7296 xxx_dir(cache_dir);
7298 /* certs dir */
7299 snprintf(certs_dir, sizeof certs_dir, "%s/%s", work_dir, XT_CERT_DIR);
7300 xxx_dir(certs_dir);
7302 /* sessions dir */
7303 snprintf(sessions_dir, sizeof sessions_dir, "%s/%s",
7304 work_dir, XT_SESSIONS_DIR);
7305 xxx_dir(sessions_dir);
7307 /* js dir */
7308 snprintf(js_dir, sizeof js_dir, "%s/%s", work_dir, XT_JS_DIR);
7309 xxx_dir(js_dir);
7311 /* temp dir */
7312 snprintf(temp_dir, sizeof temp_dir, "%s/%s", work_dir, XT_TEMP_DIR);
7313 xxx_dir(temp_dir);
7315 /* runtime settings that can override config file */
7316 if (runtime_settings[0] != '\0')
7317 config_parse(runtime_settings, 1);
7319 /* download dir */
7320 if (!strcmp(download_dir, pwd->pw_dir))
7321 strlcat(download_dir, "/downloads", sizeof download_dir);
7322 xxx_dir(download_dir);
7324 /* favorites file */
7325 snprintf(file, sizeof file, "%s/%s", work_dir, XT_FAVS_FILE);
7326 if (stat(file, &sb)) {
7327 warnx("favorites file doesn't exist, creating it");
7328 if ((f = fopen(file, "w")) == NULL)
7329 err(1, "favorites");
7330 fclose(f);
7333 /* quickmarks file */
7334 snprintf(file, sizeof file, "%s/%s", work_dir, XT_QMARKS_FILE);
7335 if (stat(file, &sb)) {
7336 warnx("quickmarks file doesn't exist, creating it");
7337 if ((f = fopen(file, "w")) == NULL)
7338 err(1, "quickmarks");
7339 fclose(f);
7342 /* search history */
7343 if (history_autosave) {
7344 snprintf(search_file, sizeof search_file, "%s/%s",
7345 work_dir, XT_SEARCH_FILE);
7346 if (stat(search_file, &sb)) {
7347 warnx("search history file doesn't exist, creating it");
7348 if ((f = fopen(search_file, "w")) == NULL)
7349 err(1, "search_history");
7350 fclose(f);
7352 history_read(&shl, search_file, &search_history_count);
7355 /* command history */
7356 if (history_autosave) {
7357 snprintf(command_file, sizeof command_file, "%s/%s",
7358 work_dir, XT_COMMAND_FILE);
7359 if (stat(command_file, &sb)) {
7360 warnx("command history file doesn't exist, creating it");
7361 if ((f = fopen(command_file, "w")) == NULL)
7362 err(1, "command_history");
7363 fclose(f);
7365 history_read(&chl, command_file, &cmd_history_count);
7368 /* cookies */
7369 session = webkit_get_default_session();
7370 setup_cookies();
7372 /* certs */
7373 if (ssl_ca_file) {
7374 if (stat(ssl_ca_file, &sb)) {
7375 warnx("no CA file: %s", ssl_ca_file);
7376 g_free(ssl_ca_file);
7377 ssl_ca_file = NULL;
7378 } else
7379 g_object_set(session,
7380 SOUP_SESSION_SSL_CA_FILE, ssl_ca_file,
7381 SOUP_SESSION_SSL_STRICT, ssl_strict_certs,
7382 (void *)NULL);
7385 /* guess_search regex */
7386 if (url_regex == NULL)
7387 url_regex = g_strdup(XT_URL_REGEX);
7388 if (url_regex)
7389 if (regcomp(&url_re, url_regex, REG_EXTENDED | REG_NOSUB))
7390 startpage_add("invalid url regex %s", url_regex);
7392 /* proxy */
7393 env_proxy = getenv("http_proxy");
7394 if (env_proxy)
7395 setup_proxy(env_proxy);
7396 else {
7397 env_proxy = getenv("HTTP_PROXY");
7398 if (env_proxy)
7399 setup_proxy(env_proxy);
7400 else
7401 setup_proxy(http_proxy);
7404 if (opte) {
7405 send_cmd_to_socket(argv[0]);
7406 exit(0);
7409 /* set some connection parameters */
7410 g_object_set(session, "max-conns", max_connections, (char *)NULL);
7411 g_object_set(session, "max-conns-per-host", max_host_connections,
7412 (char *)NULL);
7414 /* see if there is already an xxxterm running */
7415 if (single_instance && is_running()) {
7416 optn = 1;
7417 warnx("already running");
7420 if (optn) {
7421 while (argc) {
7422 cmd = g_strdup_printf("%s %s", "tabnew", argv[0]);
7423 send_cmd_to_socket(cmd);
7424 if (cmd)
7425 g_free(cmd);
7427 argc--;
7428 argv++;
7430 exit(0);
7433 /* uri completion */
7434 completion_model = gtk_list_store_new(1, G_TYPE_STRING);
7436 /* buffers */
7437 buffers_store = gtk_list_store_new
7438 (NUM_COLS, G_TYPE_UINT, GDK_TYPE_PIXBUF, G_TYPE_STRING);
7440 qmarks_load();
7442 /* go graphical */
7443 create_canvas();
7444 notebook_tab_set_visibility();
7446 if (save_global_history)
7447 restore_global_history();
7449 /* restore session list */
7450 restore_sessions_list();
7452 if (!strcmp(named_session, XT_SAVED_TABS_FILE))
7453 restore_saved_tabs();
7454 else {
7455 a.s = named_session;
7456 a.i = XT_SES_DONOTHING;
7457 open_tabs(NULL, &a);
7460 /* see if we have an exception */
7461 if (!TAILQ_EMPTY(&spl)) {
7462 create_new_tab("about:startpage", NULL, focus, -1);
7463 focus = 0;
7466 while (argc) {
7467 create_new_tab(argv[0], NULL, focus, -1);
7468 focus = 0;
7470 argc--;
7471 argv++;
7474 if (TAILQ_EMPTY(&tabs))
7475 create_new_tab(home, NULL, 1, -1);
7477 if (enable_socket)
7478 if ((s = build_socket()) != -1) {
7479 channel = g_io_channel_unix_new(s);
7480 g_io_add_watch(channel, G_IO_IN, socket_watcher, NULL);
7483 gtk_main();
7485 #ifdef USE_THREADS
7486 gdk_threads_leave();
7487 g_static_rec_mutex_unlock_full(&my_gdk_mtx); /* just in case */
7488 #endif
7490 gnutls_global_deinit();
7492 if (url_regex)
7493 regfree(&url_re);
7495 return (0);