3 * Copyright (c) 2010 Marco Peereboom <marco@peereboom.us>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * inverse color browsing
22 * download files status
23 * multi letter commands
24 * pre and post counts for commands
29 * autocompletion on various inputs
40 #include <sys/queue.h>
41 #include <sys/types.h>
45 #include <gdk/gdkkeysyms.h>
46 #include <webkit/webkit.h>
47 #include <libsoup/soup.h>
48 #include <JavaScriptCore/JavaScript.h>
50 static char *version
= "$xxxterm$";
53 /* #define XT_DEBUG */
55 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
56 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
57 #define XT_D_MOVE 0x0001
58 #define XT_D_KEY 0x0002
59 #define XT_D_TAB 0x0004
60 #define XT_D_URL 0x0008
61 #define XT_D_CMD 0x0010
62 #define XT_D_NAV 0x0020
63 #define XT_D_DOWNLOAD 0x0040
64 #define XT_D_CONFIG 0x0080
65 u_int32_t swm_debug
= 0
77 #define DNPRINTF(n,x...)
80 #define LENGTH(x) (sizeof x / sizeof x[0])
81 #define CLEAN(mask) (mask & ~(GDK_MOD2_MASK) & \
82 ~(GDK_BUTTON1_MASK) & \
83 ~(GDK_BUTTON2_MASK) & \
84 ~(GDK_BUTTON3_MASK) & \
85 ~(GDK_BUTTON4_MASK) & \
89 TAILQ_ENTRY(tab
) entry
;
94 GtkWidget
*browser_win
;
98 /* adjustments for browser */
101 GtkAdjustment
*adjust_h
;
102 GtkAdjustment
*adjust_v
;
110 WebKitWebSettings
*settings
;
112 TAILQ_HEAD(tab_list
, tab
);
120 #define XT_DIR (".xxxterm")
121 #define XT_CONF_FILE ("xxxterm.conf")
122 #define XT_CB_HANDLED (TRUE)
123 #define XT_CB_PASSTHROUGH (FALSE)
126 #define XT_MOVE_INVALID (0)
127 #define XT_MOVE_DOWN (1)
128 #define XT_MOVE_UP (2)
129 #define XT_MOVE_BOTTOM (3)
130 #define XT_MOVE_TOP (4)
131 #define XT_MOVE_PAGEDOWN (5)
132 #define XT_MOVE_PAGEUP (6)
133 #define XT_MOVE_LEFT (7)
134 #define XT_MOVE_FARLEFT (8)
135 #define XT_MOVE_RIGHT (9)
136 #define XT_MOVE_FARRIGHT (10)
138 #define XT_TAB_PREV (-2)
139 #define XT_TAB_NEXT (-1)
140 #define XT_TAB_INVALID (0)
141 #define XT_TAB_NEW (1)
142 #define XT_TAB_DELETE (2)
143 #define XT_TAB_DELQUIT (3)
144 #define XT_TAB_OPEN (4)
146 #define XT_NAV_INVALID (0)
147 #define XT_NAV_BACK (1)
148 #define XT_NAV_FORWARD (2)
149 #define XT_NAV_RELOAD (3)
151 #define XT_FOCUS_INVALID (0)
152 #define XT_FOCUS_URI (1)
155 extern char *__progname
;
157 GtkWidget
*main_window
;
158 GtkNotebook
*notebook
;
159 struct tab_list tabs
;
162 int showtabs
= 1; /* show tabs on notebook */
163 int showurl
= 1; /* show url toolbar on notebook */
164 int tabless
= 0; /* allow only 1 tab */
165 int ctrl_click_focus
= 0; /* ctrl click gets focus */
166 int cookies_enabled
= 1; /* enable cookies */
167 int read_only_cookies
= 0; /* enable to not write cookies */
168 int enable_scripts
= 1;
169 int enable_plugins
= 1;
170 int default_font_size
= 12;
172 char *home
= "http://www.peereboom.us";
173 char *http_proxy
= NULL
;
174 SoupURI
*proxy_uri
= NULL
;
175 char work_dir
[PATH_MAX
];
176 char cookie_file
[PATH_MAX
];
177 char download_dir
[PATH_MAX
];
178 SoupSession
*session
;
179 SoupCookieJar
*cookiejar
;
182 void create_new_tab(char *, int);
183 void delete_tab(struct tab
*);
185 struct valid_url_types
{
195 valid_url_type(char *url
)
199 for (i
= 0; i
< LENGTH(vut
); i
++)
200 if (!strncasecmp(vut
[i
].type
, url
, strlen(vut
[i
].type
)))
207 guess_url_type(char *url_in
)
210 char *url_out
= NULL
;
212 /* XXX not sure about this heuristic */
213 if (stat(url_in
, &sb
) == 0) {
214 if (asprintf(&url_out
, "file://%s", url_in
) == -1)
215 err(1, "aprintf file");
218 if (asprintf(&url_out
, "http://%s", url_in
) == -1)
219 err(1, "aprintf http");
223 err(1, "asprintf pointer");
225 DNPRINTF(XT_D_URL
, "guess_url_type: guessed %s\n", url_out
);
232 config_parse(char *filename
)
235 char *line
, *cp
, *var
, *val
;
236 size_t len
, lineno
= 0;
238 DNPRINTF(XT_D_CONFIG
, "config_parse: filename %s\n", filename
);
240 if (filename
== NULL
)
243 if ((config
= fopen(filename
, "r")) == NULL
) {
244 warn("config_parse: cannot open %s", filename
);
249 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
254 cp
+= (long)strspn(cp
, WS
);
261 if ((var
= strsep(&cp
, WS
)) == NULL
|| cp
== NULL
)
264 cp
+= (long)strspn(cp
, WS
);
265 if ((val
= strsep(&cp
, WS
)) == NULL
)
268 DNPRINTF(XT_D_CONFIG
, "config_parse: %s=%s\n",var
,val
);
271 if (!strcmp(var
, "home"))
273 else if (!strcmp(var
, "ctrl_click_focus"))
274 ctrl_click_focus
= atoi(val
);
275 else if (!strcmp(var
, "read_only_cookies"))
276 read_only_cookies
= atoi(val
);
277 else if (!strcmp(var
, "cookies_enabled"))
278 cookies_enabled
= atoi(val
);
279 else if (!strcmp(var
, "enable_scripts"))
280 enable_scripts
= atoi(val
);
281 else if (!strcmp(var
, "enable_plugins"))
282 enable_plugins
= atoi(val
);
283 else if (!strcmp(var
, "default_font_size"))
284 default_font_size
= atoi(val
);
285 else if (!strcmp(var
, "http_proxy")) {
286 http_proxy
= strdup(val
);
287 if (http_proxy
== NULL
)
288 err(1, "http_proxy");
289 } else if (!strcmp(var
, "download_dir")) {
291 snprintf(download_dir
, sizeof download_dir
,
292 "%s/%s", pwd
->pw_dir
, &val
[1]);
294 strlcpy(download_dir
, val
, sizeof download_dir
);
295 fprintf(stderr
, "download dir: %s\n", download_dir
);
297 errx(1, "invalid conf file entry: %s=%s", var
, val
);
305 quit(struct tab
*t
, struct karg
*args
)
313 focus(struct tab
*t
, struct karg
*args
)
318 if (args
->i
== XT_FOCUS_URI
)
319 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
325 help(struct tab
*t
, struct karg
*args
)
330 webkit_web_view_load_string(t
->wv
,
331 "<html><body><h1>XXXTerm</h1></body></html>",
340 navaction(struct tab
*t
, struct karg
*args
)
342 DNPRINTF(XT_D_NAV
, "navaction: tab %d opcode %d\n",
347 webkit_web_view_go_back(t
->wv
);
350 webkit_web_view_go_forward(t
->wv
);
353 webkit_web_view_reload(t
->wv
);
356 return (XT_CB_PASSTHROUGH
);
360 move(struct tab
*t
, struct karg
*args
)
362 GtkAdjustment
*adjust
;
363 double pi
, si
, pos
, ps
, upper
, lower
, max
;
370 case XT_MOVE_PAGEDOWN
:
372 adjust
= t
->adjust_v
;
375 adjust
= t
->adjust_h
;
379 pos
= gtk_adjustment_get_value(adjust
);
380 ps
= gtk_adjustment_get_page_size(adjust
);
381 upper
= gtk_adjustment_get_upper(adjust
);
382 lower
= gtk_adjustment_get_lower(adjust
);
383 si
= gtk_adjustment_get_step_increment(adjust
);
384 pi
= gtk_adjustment_get_page_increment(adjust
);
387 DNPRINTF(XT_D_MOVE
, "move: opcode %d %s pos %f ps %f upper %f lower %f "
388 "max %f si %f pi %f\n",
389 args
->i
, adjust
== t
->adjust_h
? "horizontal" : "vertical",
390 pos
, ps
, upper
, lower
, max
, si
, pi
);
396 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
401 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
404 case XT_MOVE_FARRIGHT
:
405 gtk_adjustment_set_value(adjust
, max
);
408 case XT_MOVE_FARLEFT
:
409 gtk_adjustment_set_value(adjust
, lower
);
411 case XT_MOVE_PAGEDOWN
:
413 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
417 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
420 return (XT_CB_PASSTHROUGH
);
423 DNPRINTF(XT_D_MOVE
, "move: new pos %f %f\n", pos
, MIN(pos
, max
));
425 return (XT_CB_HANDLED
);
429 getparams(char *cmd
, char *cmp
)
434 if (!strncmp(cmd
, cmp
, strlen(cmp
))) {
435 rv
= cmd
+ strlen(cmp
);
447 tabaction(struct tab
*t
, struct karg
*args
)
449 int rv
= XT_CB_HANDLED
;
450 char *url
= NULL
, *newuri
= NULL
;
452 DNPRINTF(XT_D_TAB
, "tabaction: %p %d %d\n", t
, args
->i
, t
->focus_wv
);
455 return (XT_CB_PASSTHROUGH
);
459 if ((url
= getparams(args
->s
, "tabnew")))
460 create_new_tab(url
, 1);
462 create_new_tab(NULL
, 1);
468 if (gtk_notebook_get_n_pages(notebook
) > 1)
474 if ((url
= getparams(args
->s
, "open")) ||
475 ((url
= getparams(args
->s
, "op"))) ||
476 ((url
= getparams(args
->s
, "o"))))
479 rv
= XT_CB_PASSTHROUGH
;
483 if (valid_url_type(url
)) {
484 newuri
= guess_url_type(url
);
487 webkit_web_view_load_uri(t
->wv
, url
);
492 rv
= XT_CB_PASSTHROUGH
;
506 movetab(struct tab
*t
, struct karg
*args
)
511 DNPRINTF(XT_D_TAB
, "movetab: %p %d\n", t
, args
->i
);
514 return (XT_CB_PASSTHROUGH
);
516 if (args
->i
== XT_TAB_INVALID
)
517 return (XT_CB_PASSTHROUGH
);
519 if (args
->i
< XT_TAB_INVALID
) {
520 /* next or previous tab */
521 if (TAILQ_EMPTY(&tabs
))
522 return (XT_CB_PASSTHROUGH
);
524 if (args
->i
== XT_TAB_NEXT
)
525 gtk_notebook_next_page(notebook
);
527 gtk_notebook_prev_page(notebook
);
529 return (XT_CB_HANDLED
);
534 if (t
->tab_id
== x
) {
535 DNPRINTF(XT_D_TAB
, "movetab: do nothing\n");
536 return (XT_CB_HANDLED
);
539 TAILQ_FOREACH(tt
, &tabs
, entry
) {
540 if (tt
->tab_id
== x
) {
541 gtk_notebook_set_current_page(notebook
, x
);
542 DNPRINTF(XT_D_TAB
, "movetab: going to %d\n", x
);
544 gtk_widget_grab_focus(GTK_WIDGET(tt
->wv
));
548 return (XT_CB_HANDLED
);
552 command(struct tab
*t
, struct karg
*args
)
554 DNPRINTF(XT_D_CMD
, "command:\n");
556 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), ":");
557 gtk_widget_show(t
->cmd
);
558 gtk_widget_grab_focus(GTK_WIDGET(t
->cmd
));
559 gtk_editable_set_position(GTK_EDITABLE(t
->cmd
), -1);
561 return (XT_CB_HANDLED
);
564 /* inherent to GTK not all keys will be caught at all times */
569 int (*func
)(struct tab
*, struct karg
*);
572 { GDK_SHIFT_MASK
, 0, GDK_colon
, command
, {0} },
573 { GDK_CONTROL_MASK
, 0, GDK_q
, quit
, {0} },
576 { 0, 0, GDK_F6
, focus
, {.i
= XT_FOCUS_URI
} },
579 { 0, 0, GDK_BackSpace
, navaction
, {.i
= XT_NAV_BACK
} },
580 { GDK_MOD1_MASK
, 0, GDK_Left
, navaction
, {.i
= XT_NAV_BACK
} },
581 { GDK_SHIFT_MASK
, 0, GDK_BackSpace
, navaction
, {.i
= XT_NAV_FORWARD
} },
582 { GDK_MOD1_MASK
, 0, GDK_Right
, navaction
, {.i
= XT_NAV_FORWARD
} },
583 { 0, 0, GDK_F5
, navaction
, {.i
= XT_NAV_RELOAD
} },
585 /* vertical movement */
586 { 0, 0, GDK_j
, move
, {.i
= XT_MOVE_DOWN
} },
587 { 0, 0, GDK_Down
, move
, {.i
= XT_MOVE_DOWN
} },
588 { 0, 0, GDK_Up
, move
, {.i
= XT_MOVE_UP
} },
589 { 0, 0, GDK_k
, move
, {.i
= XT_MOVE_UP
} },
590 { GDK_SHIFT_MASK
, 0, GDK_G
, move
, {.i
= XT_MOVE_BOTTOM
} },
591 { 0, 0, GDK_End
, move
, {.i
= XT_MOVE_BOTTOM
} },
592 { 0, 0, GDK_Home
, move
, {.i
= XT_MOVE_TOP
} },
593 { 0, GDK_g
, GDK_g
, move
, {.i
= XT_MOVE_TOP
} }, /* XXX make this work */
594 { 0, 0, GDK_space
, move
, {.i
= XT_MOVE_PAGEDOWN
} },
595 { 0, 0, GDK_Page_Down
, move
, {.i
= XT_MOVE_PAGEDOWN
} },
596 { 0, 0, GDK_Page_Up
, move
, {.i
= XT_MOVE_PAGEUP
} },
597 /* horizontal movement */
598 { 0, 0, GDK_l
, move
, {.i
= XT_MOVE_RIGHT
} },
599 { 0, 0, GDK_Right
, move
, {.i
= XT_MOVE_RIGHT
} },
600 { 0, 0, GDK_Left
, move
, {.i
= XT_MOVE_LEFT
} },
601 { 0, 0, GDK_h
, move
, {.i
= XT_MOVE_LEFT
} },
602 { GDK_SHIFT_MASK
, 0, GDK_dollar
, move
, {.i
= XT_MOVE_FARRIGHT
} },
603 { 0, 0, GDK_0
, move
, {.i
= XT_MOVE_FARLEFT
} },
606 { GDK_CONTROL_MASK
, 0, GDK_t
, tabaction
, {.i
= XT_TAB_NEW
} },
607 { GDK_CONTROL_MASK
, 0, GDK_w
, tabaction
, {.i
= XT_TAB_DELETE
} },
608 { GDK_CONTROL_MASK
, 0, GDK_1
, movetab
, {.i
= 1} },
609 { GDK_CONTROL_MASK
, 0, GDK_2
, movetab
, {.i
= 2} },
610 { GDK_CONTROL_MASK
, 0, GDK_3
, movetab
, {.i
= 3} },
611 { GDK_CONTROL_MASK
, 0, GDK_4
, movetab
, {.i
= 4} },
612 { GDK_CONTROL_MASK
, 0, GDK_5
, movetab
, {.i
= 5} },
613 { GDK_CONTROL_MASK
, 0, GDK_6
, movetab
, {.i
= 6} },
614 { GDK_CONTROL_MASK
, 0, GDK_7
, movetab
, {.i
= 7} },
615 { GDK_CONTROL_MASK
, 0, GDK_8
, movetab
, {.i
= 8} },
616 { GDK_CONTROL_MASK
, 0, GDK_9
, movetab
, {.i
= 9} },
617 { GDK_CONTROL_MASK
, 0, GDK_0
, movetab
, {.i
= 10} },
623 int (*func
)(struct tab
*, struct karg
*);
626 { "q!", 0, quit
, {0} },
627 { "qa", 0, quit
, {0} },
628 { "qa!", 0, quit
, {0} },
629 { "help", 0, help
, {0} },
632 { "o", 1, tabaction
, {.i
= XT_TAB_OPEN
} },
633 { "op", 1, tabaction
, {.i
= XT_TAB_OPEN
} },
634 { "open", 1, tabaction
, {.i
= XT_TAB_OPEN
} },
635 { "tabnew", 1, tabaction
, {.i
= XT_TAB_NEW
} },
636 { "tabedit", 0, tabaction
, {.i
= XT_TAB_NEW
} },
637 { "tabe", 0, tabaction
, {.i
= XT_TAB_NEW
} },
638 { "tabclose", 0, tabaction
, {.i
= XT_TAB_DELETE
} },
639 { "quit", 0, tabaction
, {.i
= XT_TAB_DELQUIT
} },
640 { "q", 0, tabaction
, {.i
= XT_TAB_DELQUIT
} },
641 /* XXX add count to these commands and add tabl and friends */
642 { "tabprevious", 0, movetab
, {.i
= XT_TAB_PREV
} },
643 { "tabp", 0, movetab
, {.i
= XT_TAB_PREV
} },
644 { "tabnext", 0, movetab
, {.i
= XT_TAB_NEXT
} },
645 { "tabn", 0, movetab
, {.i
= XT_TAB_NEXT
} },
649 focus_uri_entry_cb(GtkWidget
* w
, GtkDirectionType direction
, struct tab
*t
)
651 DNPRINTF(XT_D_URL
, "focus_uri_entry_cb: tab %d focus_wv %d\n",
652 t
->tab_id
, t
->focus_wv
);
655 errx(1, "focus_uri_entry_cb");
657 /* focus on wv instead */
659 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
663 activate_uri_entry_cb(GtkWidget
* entry
, struct tab
*t
)
665 const gchar
*uri
= gtk_entry_get_text(GTK_ENTRY(entry
));
668 DNPRINTF(XT_D_URL
, "activate_uri_entry_cb: %s\n", uri
);
671 errx(1, "activate_uri_entry_cb");
676 if (valid_url_type((char *)uri
)) {
677 newuri
= guess_url_type((char *)uri
);
681 webkit_web_view_load_uri(t
->wv
, uri
);
682 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
689 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
691 WebKitWebFrame
*frame
;
695 errx(1, "notify_load_status_cb");
697 switch (webkit_web_view_get_load_status(wview
)) {
698 case WEBKIT_LOAD_COMMITTED
:
699 frame
= webkit_web_view_get_main_frame(wview
);
700 uri
= webkit_web_frame_get_uri(frame
);
702 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
705 /* take focus if we are visible */
706 if (gtk_notebook_get_current_page(notebook
) == t
->tab_id
)
707 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
709 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
710 uri
= webkit_web_view_get_title(wview
);
712 frame
= webkit_web_view_get_main_frame(wview
);
713 uri
= webkit_web_frame_get_uri(frame
);
715 gtk_label_set_text(GTK_LABEL(t
->label
), uri
);
717 case WEBKIT_LOAD_PROVISIONAL
:
718 case WEBKIT_LOAD_FINISHED
:
719 case WEBKIT_LOAD_FAILED
:
726 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
727 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
728 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
733 errx(1, "webview_npd_cb");
735 DNPRINTF(XT_D_NAV
, "webview_npd_cb: %s\n",
736 webkit_network_request_get_uri(request
));
739 uri
= (char *)webkit_network_request_get_uri(request
);
740 create_new_tab(uri
, ctrl_click_focus
);
742 webkit_web_policy_decision_ignore(pd
);
744 return (TRUE
); /* we made the decission */
751 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
753 /* we can not eat the event without throwing gtk off so defer it */
755 /* catch ctrl click */
756 if (e
->type
== GDK_BUTTON_RELEASE
&&
757 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
762 return (XT_CB_PASSTHROUGH
);
766 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
767 WebKitNetworkRequest
*request
, char *mime_type
,
768 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
771 errx(1, "webview_mimetype_cb");
773 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
774 t
->tab_id
, mime_type
);
776 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
777 webkit_web_policy_decision_download(decision
);
785 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*download
, struct tab
*t
)
787 const gchar
*filename
;
790 if (download
== NULL
|| t
== NULL
)
791 errx(1, "webview_download_cb: invalid pointers");
793 filename
= webkit_download_get_suggested_filename(download
);
794 if (filename
== NULL
)
795 return (FALSE
); /* abort download */
797 if (asprintf(&uri
, "file://%s/%s", download_dir
, filename
) == -1)
798 err(1, "aprintf uri");
800 DNPRINTF(XT_D_DOWNLOAD
, "webview_download_cb: tab %d filename %s "
802 t
->tab_id
, filename
, uri
);
804 webkit_download_set_destination_uri(download
, uri
);
809 webkit_download_start(download
);
811 return (TRUE
); /* start download */
815 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
817 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
820 errx(1, "webview_hover_cb");
827 t
->hover
= strdup(uri
);
828 } else if (t
->hover
) {
835 webview_keypress_cb(GtkWidget
*w
, GdkEventKey
*e
, struct tab
*t
)
839 /* don't use w directly; use t->whatever instead */
842 errx(1, "webview_keypress_cb");
844 DNPRINTF(XT_D_KEY
, "webview_keypress_cb: keyval 0x%x mask 0x%x t %p\n",
845 e
->keyval
, e
->state
, t
);
847 for (i
= 0; i
< LENGTH(keys
); i
++)
848 if (e
->keyval
== keys
[i
].key
&& CLEAN(e
->state
) ==
850 keys
[i
].func(t
, &keys
[i
].arg
);
851 return (XT_CB_HANDLED
);
854 return (XT_CB_PASSTHROUGH
);
858 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
860 int rv
= XT_CB_HANDLED
;
861 const gchar
*c
= gtk_entry_get_text(w
);
864 errx(1, "cmd_keypress_cb");
866 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x t %p\n",
867 e
->keyval
, e
->state
, t
);
871 e
->keyval
= GDK_Escape
;
872 else if (c
[0] != ':')
873 e
->keyval
= GDK_Escape
;
881 gtk_widget_hide(t
->cmd
);
882 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
886 rv
= XT_CB_PASSTHROUGH
;
892 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
895 errx(1, "cmd_focusout_cb");
897 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d focus_wv %d\n",
898 t
->tab_id
, t
->focus_wv
);
900 /* abort command when losing focus */
901 gtk_widget_hide(t
->cmd
);
903 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
905 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
907 return (XT_CB_PASSTHROUGH
);
911 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
915 const gchar
*c
= gtk_entry_get_text(entry
);
918 errx(1, "cmd_activate_cb");
920 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
925 else if (c
[0] != ':')
931 for (i
= 0; i
< LENGTH(cmds
); i
++)
932 if (cmds
[i
].params
) {
933 if (!strncmp(s
, cmds
[i
].cmd
, strlen(cmds
[i
].cmd
))) {
934 cmds
[i
].arg
.s
= strdup(s
);
935 cmds
[i
].func(t
, &cmds
[i
].arg
);
938 if (!strcmp(s
, cmds
[i
].cmd
))
939 cmds
[i
].func(t
, &cmds
[i
].arg
);
943 gtk_widget_hide(t
->cmd
);
947 create_browser(struct tab
*t
)
952 errx(1, "create_browser");
954 t
->sb_h
= GTK_SCROLLBAR(gtk_hscrollbar_new(NULL
));
955 t
->sb_v
= GTK_SCROLLBAR(gtk_vscrollbar_new(NULL
));
956 t
->adjust_h
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_h
));
957 t
->adjust_v
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_v
));
959 w
= gtk_scrolled_window_new(t
->adjust_h
, t
->adjust_v
);
960 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
961 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
963 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
964 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
966 g_signal_connect(t
->wv
, "notify::load-status",
967 G_CALLBACK(notify_load_status_cb
), t
);
977 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
978 gtk_window_set_default_size(GTK_WINDOW(w
), 800, 600);
979 gtk_widget_set_name(w
, "xxxterm");
980 gtk_window_set_wmclass(GTK_WINDOW(w
), "xxxterm", "XXXTerm");
986 create_toolbar(struct tab
*t
)
988 GtkWidget
*toolbar
= gtk_toolbar_new();
991 #if GTK_CHECK_VERSION(2,15,0)
992 gtk_orientable_set_orientation(GTK_ORIENTABLE(toolbar
),
993 GTK_ORIENTATION_HORIZONTAL
);
995 gtk_toolbar_set_orientation(GTK_TOOLBAR(toolbar
),
996 GTK_ORIENTATION_HORIZONTAL
);
998 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar
), GTK_TOOLBAR_BOTH_HORIZ
);
1000 i
= gtk_tool_item_new();
1001 gtk_tool_item_set_expand(i
, TRUE
);
1002 t
->uri_entry
= gtk_entry_new();
1003 gtk_container_add(GTK_CONTAINER(i
), t
->uri_entry
);
1004 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
1005 G_CALLBACK(activate_uri_entry_cb
), t
);
1006 gtk_toolbar_insert(GTK_TOOLBAR(toolbar
), i
, -1);
1012 delete_tab(struct tab
*t
)
1014 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
1019 TAILQ_REMOVE(&tabs
, t
, entry
);
1020 if (TAILQ_EMPTY(&tabs
))
1021 create_new_tab(NULL
, 1);
1023 gtk_widget_destroy(t
->vbox
);
1028 setup_webkit(struct tab
*t
)
1033 t
->settings
= webkit_web_settings_new();
1034 g_object_get((GObject
*)t
->settings
, "user-agent", &strval
, NULL
);
1035 if (strval
== NULL
) {
1036 warnx("setup_webkit: can't get user-agent property");
1040 if (asprintf(&ua
, "%s %s+", strval
, version
) == -1)
1041 err(1, "aprintf user-agent");
1043 g_object_set((GObject
*)t
->settings
,
1044 "user-agent", ua
, NULL
);
1045 g_object_set((GObject
*)t
->settings
,
1046 "enable-scripts", enable_scripts
, NULL
);
1047 g_object_set((GObject
*)t
->settings
,
1048 "enable-plugins", enable_plugins
, NULL
);
1049 g_object_set((GObject
*)t
->settings
,
1050 "default-font-size", default_font_size
, NULL
);
1052 webkit_web_view_set_settings(t
->wv
, t
->settings
);
1059 create_new_tab(char *title
, int focus
)
1063 char *newuri
= NULL
;
1065 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
1067 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
1068 DNPRINTF(XT_D_TAB
, "create_new_tab: new tab rejected\n");
1072 t
= g_malloc0(sizeof *t
);
1073 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
1075 if (title
== NULL
) {
1076 title
= "(untitled)";
1079 if (valid_url_type(title
)) {
1080 newuri
= guess_url_type(title
);
1085 t
->vbox
= gtk_vbox_new(FALSE
, 0);
1088 t
->label
= gtk_label_new(title
);
1089 gtk_widget_set_size_request(t
->label
, 100, -1);
1092 t
->toolbar
= create_toolbar(t
);
1093 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
, 0);
1096 t
->browser_win
= create_browser(t
);
1097 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
1101 t
->cmd
= gtk_entry_new();
1102 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
1103 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
1104 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
1106 /* and show it all */
1107 gtk_widget_show_all(t
->vbox
);
1108 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
,
1111 g_object_connect((GObject
*)t
->cmd
,
1112 "signal::key-press-event", (GCallback
)cmd_keypress_cb
, t
,
1113 "signal::focus-out-event", (GCallback
)cmd_focusout_cb
, t
,
1114 "signal::activate", (GCallback
)cmd_activate_cb
, t
,
1117 g_object_connect((GObject
*)t
->wv
,
1118 "signal-after::key-press-event", (GCallback
)webview_keypress_cb
, t
,
1119 /* "signal::hovering-over-link", (GCallback)webview_hover_cb, t, */
1120 "signal::download-requested", (GCallback
)webview_download_cb
, t
,
1121 "signal::mime-type-policy-decision-requested", (GCallback
)webview_mimetype_cb
, t
,
1122 "signal::navigation-policy-decision-requested", (GCallback
)webview_npd_cb
, t
,
1123 "signal::event", (GCallback
)webview_event_cb
, t
,
1126 /* hijack the unused keys as if we were the browser */
1127 g_object_connect((GObject
*)t
->toolbar
,
1128 "signal-after::key-press-event", (GCallback
)webview_keypress_cb
, t
,
1131 g_signal_connect(G_OBJECT(t
->uri_entry
), "focus",
1132 G_CALLBACK(focus_uri_entry_cb
), t
);
1135 gtk_widget_hide(t
->cmd
);
1137 gtk_widget_hide(t
->toolbar
);
1140 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
1141 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
1146 webkit_web_view_load_uri(t
->wv
, title
);
1148 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
1155 notebook_switchpage_cb(GtkNotebook
*nb
, GtkNotebookPage
*nbp
, guint pn
,
1160 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
1162 TAILQ_FOREACH(t
, &tabs
, entry
) {
1163 if (t
->tab_id
== pn
) {
1164 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
1166 gtk_widget_hide(t
->cmd
);
1176 vbox
= gtk_vbox_new(FALSE
, 0);
1177 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
1179 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook
), FALSE
);
1181 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
1183 g_object_connect((GObject
*)notebook
,
1184 "signal::switch-page", (GCallback
)notebook_switchpage_cb
, NULL
,
1187 main_window
= create_window();
1188 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
1189 gtk_widget_show_all(main_window
);
1196 soup_session_remove_feature(session
,
1197 (SoupSessionFeature
*)cookiejar
);
1198 g_object_unref(cookiejar
);
1202 if (cookies_enabled
== 0)
1205 cookiejar
= soup_cookie_jar_text_new(cookie_file
, read_only_cookies
);
1206 soup_session_add_feature(session
, (SoupSessionFeature
*)cookiejar
);
1210 setup_proxy(char *uri
)
1213 g_object_set(session
, "proxy_uri", NULL
, NULL
);
1214 soup_uri_free(proxy_uri
);
1219 http_proxy
= strdup(uri
);
1220 if (http_proxy
== NULL
)
1221 err(1, "http_proxy");
1227 DNPRINTF(XT_D_CONFIG
, "setup_proxy: %s\n", uri
);
1228 proxy_uri
= soup_uri_new(uri
);
1230 g_object_set(session
, "proxy-uri", proxy_uri
, NULL
);
1237 "%s [-STVt][-f file] url ...\n", __progname
);
1242 main(int argc
, char *argv
[])
1246 char conf
[PATH_MAX
] = { '\0' };
1247 char *env_proxy
= NULL
;
1249 while ((c
= getopt(argc
, argv
, "STVf:t")) != -1) {
1258 errx(0 , "Version: %s", version
);
1261 strlcpy(conf
, optarg
, sizeof(conf
));
1277 gtk_init(&argc
, &argv
);
1278 if (!g_thread_supported())
1279 g_thread_init(NULL
);
1281 pwd
= getpwuid(getuid());
1283 errx(1, "invalid user %d", getuid());
1285 /* set download dir */
1286 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
1288 /* read config file */
1289 if (strlen(conf
) == 0)
1290 snprintf(conf
, sizeof conf
, "%s/.%s",
1291 pwd
->pw_dir
, XT_CONF_FILE
);
1294 if (stat(download_dir
, &sb
))
1295 errx(1, "must specify a valid download_dir");
1297 /* working directory */
1298 snprintf(work_dir
, sizeof work_dir
, "%s/%s", pwd
->pw_dir
, XT_DIR
);
1299 if (stat(work_dir
, &sb
)) {
1300 if (mkdir(work_dir
, S_IRWXU
) == -1)
1302 } else if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
1303 warnx("fixing invalid permissions on %s", work_dir
);
1304 if (chmod(work_dir
, S_IRWXU
) == -1)
1309 session
= webkit_get_default_session();
1310 snprintf(cookie_file
, sizeof cookie_file
, "%s/cookies.txt", work_dir
);
1314 env_proxy
= getenv("http_proxy");
1316 http_proxy
= strdup(env_proxy
);
1317 if (http_proxy
== NULL
)
1318 err(1, "http_proxy");
1320 setup_proxy(http_proxy
);
1325 create_new_tab(argv
[0], focus
);
1332 create_new_tab(home
, 1);