2 * Copyright (c) 2011 Conformal Systems LLC <info@conformal.com>
3 * Copyright (c) 2011 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.
33 #include <sys/types.h>
34 #if defined(__linux__)
35 #include "linux/util.h"
36 #include "linux/tree.h"
37 #include <bsd/stdlib.h>
38 # if !defined(sane_libbsd_headers)
39 void arc4random_buf(void *, size_t);
40 u_int32_t
arc4random_uniform(u_int32_t
);
42 #elif defined(__FreeBSD__)
44 #include "freebsd/util.h"
46 #elif defined(__OpenBSD__)
50 #include <sys/queue.h>
51 #include <sys/resource.h>
52 #include <sys/socket.h>
58 #include <gdk/gdkkeysyms.h>
60 #if GTK_CHECK_VERSION(3,0,0)
61 /* we still use GDK_* instead of GDK_KEY_* */
62 #include <gdk/gdkkeysyms-compat.h>
65 #include <webkit/webkit.h>
66 #include <libsoup/soup.h>
67 #include <JavaScriptCore/JavaScript.h>
68 #include <gnutls/gnutls.h>
69 #include <gnutls/x509.h>
71 /* comment if you don't want to use threads */
79 /* comment if you don't want to use the external editor */
80 #define XT_ENABLE_EXTERNAL_EDITOR (1)
82 #ifdef XT_ENABLE_EXTERNAL_EDITOR
86 #include "javascript.h"
88 javascript.h borrowed from vimprobable2 under the following license:
90 Copyright (c) 2009 Leon Winter
91 Copyright (c) 2009-2011 Hannes Schueller
92 Copyright (c) 2009-2010 Matto Fransen
93 Copyright (c) 2010-2011 Hans-Peter Deifel
94 Copyright (c) 2010-2011 Thomas Adam
95 Copyright (c) 2011 Albert Kim
96 Copyright (c) 2011 Daniel Carl
98 Permission is hereby granted, free of charge, to any person obtaining a copy
99 of this software and associated documentation files (the "Software"), to deal
100 in the Software without restriction, including without limitation the rights
101 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
102 copies of the Software, and to permit persons to whom the Software is
103 furnished to do so, subject to the following conditions:
105 The above copyright notice and this permission notice shall be included in
106 all copies or substantial portions of the Software.
108 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
109 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
110 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
111 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
112 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
113 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
119 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
120 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
121 #define XT_D_MOVE 0x0001
122 #define XT_D_KEY 0x0002
123 #define XT_D_TAB 0x0004
124 #define XT_D_URL 0x0008
125 #define XT_D_CMD 0x0010
126 #define XT_D_NAV 0x0020
127 #define XT_D_DOWNLOAD 0x0040
128 #define XT_D_CONFIG 0x0080
129 #define XT_D_JS 0x0100
130 #define XT_D_FAVORITE 0x0200
131 #define XT_D_PRINTING 0x0400
132 #define XT_D_COOKIE 0x0800
133 #define XT_D_KEYBINDING 0x1000
134 #define XT_D_CLIP 0x2000
135 #define XT_D_BUFFERCMD 0x4000
136 #define XT_D_INSPECTOR 0x8000
137 #define XT_D_VISITED 0x10000
138 #define XT_D_HISTORY 0x20000
139 extern u_int32_t swm_debug
;
141 #define DPRINTF(x...)
142 #define DNPRINTF(n,x...)
145 #define LENGTH(x) (sizeof x / sizeof x[0])
146 #define CLEAN(mask) (mask & ~(GDK_MOD2_MASK) & \
147 ~(GDK_BUTTON1_MASK) & \
148 ~(GDK_BUTTON2_MASK) & \
149 ~(GDK_BUTTON3_MASK) & \
150 ~(GDK_BUTTON4_MASK) & \
153 #define XT_NOMARKS (('z' - 'a' + 1) * 2 + 10)
156 TAILQ_ENTRY(tab
) entry
;
158 GtkWidget
*tab_content
;
168 GtkWidget
*uri_entry
;
169 GtkWidget
*search_entry
;
171 GtkWidget
*browser_win
;
172 GtkWidget
*statusbar_box
;
174 GtkWidget
*statusbar
;
175 GtkWidget
*buffercmd
;
186 GtkWidget
*js_toggle
;
187 GtkEntryCompletion
*completion
;
191 WebKitWebHistoryItem
*item
;
192 WebKitWebBackForwardList
*bfl
;
195 WebKitNetworkRequest
*icon_request
;
196 WebKitDownload
*icon_download
;
197 gchar
*icon_dest_uri
;
199 /* adjustments for browser */
202 GtkAdjustment
*adjust_h
;
203 GtkAdjustment
*adjust_v
;
207 #define XT_MODE_COMMAND (0)
208 #define XT_MODE_INSERT (1)
209 #define XT_MODE_HINT (2)
210 #define XT_MODE_PASSTHROUGH (3)
211 int (*mode_cb
)(struct tab
*, GdkEventKey
*, gpointer
);
212 gpointer mode_cb_data
;
216 int xtp_meaning
; /* identifies dls/favorites */
218 int popup
; /* 1 if cmd_entry has popup visible */
220 /* https thread stuff */
227 /* custom stylesheet */
237 WebKitWebSettings
*settings
;
239 gboolean load_images
;
242 double mark
[XT_NOMARKS
];
245 WebKitWebInspector
*inspector
;
246 GtkWidget
*inspector_window
;
247 GtkWidget
*inspector_view
;
249 TAILQ_HEAD(tab_list
, tab
);
258 RB_ENTRY(download
) entry
;
260 WebKitDownload
*download
;
263 RB_HEAD(download_list
, download
);
264 RB_PROTOTYPE(download_list
, download
, entry
, download_rb_cmp
);
267 RB_ENTRY(history
) entry
;
270 time_t time
; /* When the item was added. */
272 RB_HEAD(history_list
, history
);
273 RB_PROTOTYPE(history_list
, history
, entry
, history_rb_cmp
);
276 #define XT_NAME ("XXXTerm")
277 #define XT_CB_HANDLED (TRUE)
278 #define XT_CB_PASSTHROUGH (FALSE)
279 #define XT_FAVS_FILE ("favorites")
281 int run_script(struct tab
*, char *);
282 void xt_icon_from_file(struct tab
*, char *);
283 GtkWidget
*create_window(const gchar
*);
284 void show_oops(struct tab
*, const char *, ...);
285 gchar
*get_html_page(gchar
*, gchar
*, gchar
*, bool);
286 const gchar
*get_uri(struct tab
*);
287 const gchar
*get_title(struct tab
*, bool);
288 void load_uri(struct tab
*t
, gchar
*uri
);
289 gboolean
match_uri(const gchar
*uri
, const gchar
*key
);
290 int valid_url_type(char *);
292 void load_webkit_string(struct tab
*, const char *, gchar
*);
293 void button_set_stockid(GtkWidget
*, char *);
296 int remove_cookie(int);
297 int remove_cookie_domain(int);
298 void print_cookie(char *msg
, SoupCookie
*);
299 void setup_cookies(void);
302 int insert_history_item(const gchar
*uri
, const gchar
*title
, time_t time
);
303 int save_global_history_to_disk(struct tab
*t
);
304 int restore_global_history(void);
305 char *color_visited_helper(void);
306 int color_visited(struct tab
*t
, char *visited
);
309 void completion_add(struct tab
*);
310 void completion_add_uri(const gchar
*uri
);
312 /* external editor */
313 #define XT_EE_BUFSZ (64 * 1024)
314 int edit_src(struct tab
*t
, struct karg
*args
);
315 int edit_element(struct tab
*t
, struct karg
*a
);
318 #define XT_PRXY_SHOW (1<<0)
319 #define XT_PRXY_TOGGLE (1<<1)
322 #define XT_INS_SHOW (1<<0)
323 #define XT_INS_HIDE (1<<1)
324 #define XT_INS_CLOSE (1<<2)
326 WebKitWebView
* inspector_inspect_web_view_cb(WebKitWebInspector
*,
327 WebKitWebView
*, struct tab
*);
328 void setup_inspector(struct tab
*);
329 int inspector_cmd(struct tab
*, struct karg
*);
331 /* tld public suffix */
332 void tld_tree_init();
333 char *tld_get_suffix(const char *);
336 #define XT_XTP_STR "xxxt://"
337 #define XT_URI_ABOUT ("about:")
338 #define XT_URI_ABOUT_LEN (strlen(XT_URI_ABOUT))
339 #define XT_URI_ABOUT_ABOUT ("about")
340 #define XT_URI_ABOUT_BLANK ("blank")
341 #define XT_URI_ABOUT_CERTS ("certs")
342 #define XT_URI_ABOUT_COOKIEWL ("cookiewl")
343 #define XT_URI_ABOUT_COOKIEJAR ("cookiejar")
344 #define XT_URI_ABOUT_DOWNLOADS ("downloads")
345 #define XT_URI_ABOUT_FAVORITES ("favorites")
346 #define XT_URI_ABOUT_HELP ("help")
347 #define XT_URI_ABOUT_HISTORY ("history")
348 #define XT_URI_ABOUT_JSWL ("jswl")
349 #define XT_URI_ABOUT_PLUGINWL ("plwl")
350 #define XT_URI_ABOUT_SET ("set")
351 #define XT_URI_ABOUT_STATS ("stats")
352 #define XT_URI_ABOUT_MARCO ("marco")
353 #define XT_URI_ABOUT_STARTPAGE ("startpage")
357 int (*func
)(struct tab
*, struct karg
*);
362 TAILQ_ENTRY(sp
) entry
;
364 TAILQ_HEAD(sp_list
, sp
);
366 int blank(struct tab
*, struct karg
*);
367 int help(struct tab
*, struct karg
*);
368 int about(struct tab
*, struct karg
*);
369 int stats(struct tab
*, struct karg
*);
370 void show_certs(struct tab
*, gnutls_x509_crt_t
*,
372 int ca_cmd(struct tab
*, struct karg
*);
373 int cookie_show_wl(struct tab
*, struct karg
*);
374 int xtp_page_cl(struct tab
*, struct karg
*);
375 int xtp_page_dl(struct tab
*, struct karg
*);
376 int xtp_page_fl(struct tab
*, struct karg
*);
377 int xtp_page_hl(struct tab
*, struct karg
*);
378 int parse_xtp_url(struct tab
*, const char *);
379 int add_favorite(struct tab
*, struct karg
*);
380 void update_favorite_tabs(struct tab
*);
381 void update_history_tabs(struct tab
*);
382 void update_download_tabs(struct tab
*);
383 void xtp_generate_keys(void);
384 size_t about_list_size(void);
385 int cookie_cmd(struct tab
*, struct karg
*);
386 int js_cmd(struct tab
*, struct karg
*);
387 int pl_cmd(struct tab
*, struct karg
*);
388 void startpage_add(const char *, ...);
392 * identifies which tabs have xtp pages in (corresponding to about_list indices)
394 #define XT_XTP_TAB_MEANING_NORMAL (-1) /* normal url */
395 #define XT_XTP_TAB_MEANING_BL (1) /* about:blank in this tab */
396 #define XT_XTP_TAB_MEANING_CL (4) /* cookie manager in this tab */
397 #define XT_XTP_TAB_MEANING_DL (5) /* download manager in this tab */
398 #define XT_XTP_TAB_MEANING_FL (6) /* favorite manager in this tab */
399 #define XT_XTP_TAB_MEANING_HL (8) /* history manager in this tab */
402 #define XT_WL_TOGGLE (1<<0)
403 #define XT_WL_ENABLE (1<<1)
404 #define XT_WL_DISABLE (1<<2)
405 #define XT_WL_FQDN (1<<3) /* default */
406 #define XT_WL_TOPLEVEL (1<<4)
407 #define XT_WL_PERSISTENT (1<<5)
408 #define XT_WL_SESSION (1<<6)
409 #define XT_WL_RELOAD (1<<7)
410 #define XT_SHOW (1<<8)
411 #define XT_DELETE (1<<9)
412 #define XT_SAVE (1<<10)
413 #define XT_OPEN (1<<11)
415 #define XT_WL_INVALID (0)
416 #define XT_WL_JAVASCRIPT (1)
417 #define XT_WL_COOKIE (2)
418 #define XT_WL_PLUGIN (3)
421 RB_ENTRY(domain
) entry
;
423 int handy
; /* app use */
425 RB_HEAD(domain_list
, domain
);
426 RB_PROTOTYPE(domain_list
, domain
, entry
, domain_rb_cmp
);
428 int wl_show(struct tab
*, struct karg
*, char *,
429 struct domain_list
*);
435 TAILQ_ENTRY(alias
) entry
;
437 TAILQ_HEAD(alias_list
, alias
);
445 TAILQ_ENTRY(mime_type
) entry
;
447 TAILQ_HEAD(mime_type_list
, mime_type
);
449 struct domain
* wl_find(const gchar
*, struct domain_list
*);
450 int wl_save(struct tab
*, struct karg
*, int);
451 int toggle_cwl(struct tab
*, struct karg
*);
452 int toggle_js(struct tab
*, struct karg
*);
453 int toggle_pl(struct tab
*, struct karg
*);
455 /* input autofocus */
456 void input_autofocus(struct tab
*);
457 void input_focus_blur(struct tab
*, void *);
458 void *input_check_mode(struct tab
*);
459 int command_mode(struct tab
*, struct karg
*);
462 #define XT_BM_NORMAL (0)
463 #define XT_BM_WHITELIST (1)
464 #define XT_BM_KIOSK (2)
466 #define XT_GM_CLASSIC (0)
467 #define XT_GM_MINIMAL (1)
469 #define XT_TABS_NORMAL (0)
470 #define XT_TABS_COMPACT (1)
472 #define XT_EM_HYBRID (0)
475 #define XT_DM_START (0)
476 #define XT_DM_ASK (1)
477 #define XT_DM_ADD (2)
479 #define XT_REFERER_ALWAYS (0)
480 #define XT_REFERER_NEVER (1)
481 #define XT_REFERER_SAME_DOMAIN (2)
482 #define XT_REFERER_CUSTOM (3)
484 #define CTRL GDK_CONTROL_MASK
485 #define MOD1 GDK_MOD1_MASK
486 #define SHFT GDK_SHIFT_MASK
493 TAILQ_ENTRY(key_binding
) entry
; /* in bss so no need to init */
495 TAILQ_HEAD(keybinding_list
, key_binding
);
499 TAILQ_ENTRY(user_agent
) entry
;
501 TAILQ_HEAD(user_agent_list
, user_agent
);
506 #define XT_S_INVALID (0)
509 #define XT_S_FLOAT (3)
511 #define XT_SF_RESTART (1<<0)
512 #define XT_SF_RUNTIME (1<<1)
517 int (*activate
)(char *);
520 int set(struct tab
*, struct karg
*);
521 size_t get_settings_size(void);
522 int settings_add(char *, char *);
523 void setup_proxy(char *);
524 int set_browser_mode(struct settings
*, char *);
525 int set_gui_mode(struct settings
*, char *);
526 int set_cookie_policy(struct settings
*, char *);
527 char *get_browser_mode(struct settings
*);
528 char *get_gui_mode(struct settings
*);
529 char *get_cookie_policy(struct settings
*);
530 void init_keybindings(void);
531 void config_parse(char *, int);
532 char *get_setting_name(int);
534 #define XT_DL_START (0)
535 #define XT_DL_RESTART (1)
536 int download_start(struct tab
*, struct download
*, int flag
);
539 extern int enable_socket
;
540 extern int single_instance
;
541 extern int fancy_bar
;
542 extern int browser_mode
;
543 extern int enable_localstorage
;
544 extern char *statusbar_elems
;
546 extern int show_tabs
;
547 extern int tab_style
;
549 extern int show_statusbar
;
550 extern int ctrl_click_focus
;
551 extern int cookies_enabled
;
552 extern int read_only_cookies
;
553 extern int enable_scripts
;
554 extern int enable_plugins
;
555 extern gfloat default_zoom_level
;
556 extern char default_script
[PATH_MAX
];
557 extern int window_height
;
558 extern int window_width
;
559 extern int window_maximize
;
560 extern int icon_size
;
561 extern int refresh_interval
;
562 extern int enable_plugin_whitelist
;
563 extern int enable_cookie_whitelist
;
564 extern int enable_js_whitelist
;
565 extern int session_timeout
;
566 extern int cookie_policy
;
567 extern char *ssl_ca_file
;
568 extern char *resource_dir
;
569 extern gboolean ssl_strict_certs
;
570 extern int append_next
;
572 extern char *search_string
;
573 extern char *http_proxy
;
574 extern char *external_editor
;
575 extern char download_dir
[PATH_MAX
];
576 extern int download_mode
;
577 extern char runtime_settings
[PATH_MAX
];
578 extern int allow_volatile_cookies
;
579 extern int color_visited_uris
;
580 extern int save_global_history
;
581 extern struct user_agent
*user_agent
;
582 extern int save_rejected_cookies
;
583 extern int session_autosave
;
584 extern int guess_search
;
585 extern int dns_prefetch
;
586 extern gint max_connections
;
587 extern gint max_host_connections
;
588 extern gint enable_spell_checking
;
589 extern char *spell_check_languages
;
590 extern int xterm_workaround
;
591 extern char *url_regex
;
592 extern int history_autosave
;
593 extern char search_file
[PATH_MAX
];
594 extern char command_file
[PATH_MAX
];
595 extern char *encoding
;
596 extern int autofocus_onload
;
597 extern int js_autorun_enabled
;
598 extern char *cmd_font_name
;
599 extern char *oops_font_name
;
600 extern char *statusbar_font_name
;
601 extern char *tabbar_font_name
;
602 extern int edit_mode
;
603 extern int userstyle_global
;
604 extern int auto_load_images
;
605 extern int enable_autoscroll
;
606 extern int enable_favicon_entry
;
607 extern int enable_favicon_tabs
;
608 extern int referer_mode
;
609 extern char *referer_custom
;
612 extern char *version
;
613 extern char *icons
[];
614 extern char rc_fname
[PATH_MAX
];
615 extern char work_dir
[PATH_MAX
];
616 extern char temp_dir
[PATH_MAX
];
617 extern struct passwd
*pwd
;
618 long long unsigned int blocked_cookies
;
619 extern SoupCookieJar
*s_cookiejar
;
620 extern SoupCookieJar
*p_cookiejar
;
621 extern SoupSession
*session
;
622 extern GtkNotebook
*notebook
;
623 extern GtkListStore
*completion_model
;
625 extern void (*_soup_cookie_jar_add_cookie
)(SoupCookieJar
*, SoupCookie
*);
627 extern struct history_list hl
;
628 extern int hl_purge_count
;
629 extern struct download_list downloads
;
630 extern struct tab_list tabs
;
631 extern struct about_type about_list
[];
632 extern struct domain_list c_wl
;
633 extern struct domain_list js_wl
;
634 extern struct domain_list pl_wl
;
635 extern struct alias_list aliases
;
636 extern struct mime_type_list mtl
;
637 extern struct keybinding_list kbl
;
638 extern struct sp_list spl
;
639 extern struct user_agent_list ua_list
;
640 extern int user_agent_count
;
642 extern PangoFontDescription
*cmd_font
;
643 extern PangoFontDescription
*oops_font
;
644 extern PangoFontDescription
*statusbar_font
;
645 extern PangoFontDescription
*tabbar_font
;