dang it i disabled threads by accident
[xombrero.git] / xxxterm.h
blob28bdfbd4d4166390d698ba2dafb81ddd599057b7
1 /*
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.
18 #include <ctype.h>
19 #include <dlfcn.h>
20 #include <err.h>
21 #include <errno.h>
22 #include <libgen.h>
23 #include <pwd.h>
24 #include <regex.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <dirent.h>
32 #include <sys/types.h>
33 #include <sys/wait.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);
41 # endif
42 #elif defined(__FreeBSD__)
43 #include <libutil.h>
44 #include "freebsd/util.h"
45 #include <sys/tree.h>
46 #else /* OpenBSD */
47 #include <util.h>
48 #include <sys/tree.h>
49 #endif
50 #include <sys/queue.h>
51 #include <sys/resource.h>
52 #include <sys/socket.h>
53 #include <sys/stat.h>
54 #include <sys/time.h>
55 #include <sys/un.h>
57 #include <gtk/gtk.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>
63 #endif
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 */
72 #define USE_THREADS
74 #ifdef USE_THREADS
75 #include <gcrypt.h>
76 #include <pthread.h>
77 #endif
79 #include "version.h"
80 #include "javascript.h"
82 javascript.h borrowed from vimprobable2 under the following license:
84 Copyright (c) 2009 Leon Winter
85 Copyright (c) 2009-2011 Hannes Schueller
86 Copyright (c) 2009-2010 Matto Fransen
87 Copyright (c) 2010-2011 Hans-Peter Deifel
88 Copyright (c) 2010-2011 Thomas Adam
89 Copyright (c) 2011 Albert Kim
90 Copyright (c) 2011 Daniel Carl
92 Permission is hereby granted, free of charge, to any person obtaining a copy
93 of this software and associated documentation files (the "Software"), to deal
94 in the Software without restriction, including without limitation the rights
95 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
96 copies of the Software, and to permit persons to whom the Software is
97 furnished to do so, subject to the following conditions:
99 The above copyright notice and this permission notice shall be included in
100 all copies or substantial portions of the Software.
102 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
103 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
104 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
105 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
106 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
107 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
108 THE SOFTWARE.
111 /*#define XT_DEBUG*/
112 #ifdef XT_DEBUG
113 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
114 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
115 #define XT_D_MOVE 0x0001
116 #define XT_D_KEY 0x0002
117 #define XT_D_TAB 0x0004
118 #define XT_D_URL 0x0008
119 #define XT_D_CMD 0x0010
120 #define XT_D_NAV 0x0020
121 #define XT_D_DOWNLOAD 0x0040
122 #define XT_D_CONFIG 0x0080
123 #define XT_D_JS 0x0100
124 #define XT_D_FAVORITE 0x0200
125 #define XT_D_PRINTING 0x0400
126 #define XT_D_COOKIE 0x0800
127 #define XT_D_KEYBINDING 0x1000
128 #define XT_D_CLIP 0x2000
129 #define XT_D_BUFFERCMD 0x4000
130 #define XT_D_INSPECTOR 0x8000
131 extern u_int32_t swm_debug;
132 #else
133 #define DPRINTF(x...)
134 #define DNPRINTF(n,x...)
135 #endif
137 #define LENGTH(x) (sizeof x / sizeof x[0])
138 #define CLEAN(mask) (mask & ~(GDK_MOD2_MASK) & \
139 ~(GDK_BUTTON1_MASK) & \
140 ~(GDK_BUTTON2_MASK) & \
141 ~(GDK_BUTTON3_MASK) & \
142 ~(GDK_BUTTON4_MASK) & \
143 ~(GDK_BUTTON5_MASK))
145 #define XT_NOMARKS (('z' - 'a' + 1) * 2 + 10)
147 struct tab {
148 TAILQ_ENTRY(tab) entry;
149 GtkWidget *vbox;
150 GtkWidget *tab_content;
151 struct {
152 GtkWidget *label;
153 GtkWidget *eventbox;
154 GtkWidget *box;
155 GtkWidget *sep;
156 } tab_elems;
157 GtkWidget *label;
158 GtkWidget *spinner;
159 GtkWidget *uri_entry;
160 GtkWidget *search_entry;
161 GtkWidget *toolbar;
162 GtkWidget *browser_win;
163 GtkWidget *statusbar_box;
164 struct {
165 GtkWidget *statusbar;
166 GtkWidget *buffercmd;
167 GtkWidget *zoom;
168 GtkWidget *position;
169 } sbe;
170 GtkWidget *cmd;
171 GtkWidget *buffers;
172 GtkWidget *oops;
173 GtkWidget *backward;
174 GtkWidget *forward;
175 GtkWidget *stop;
176 GtkWidget *gohome;
177 GtkWidget *js_toggle;
178 GtkEntryCompletion *completion;
179 guint tab_id;
180 WebKitWebView *wv;
182 WebKitWebHistoryItem *item;
183 WebKitWebBackForwardList *bfl;
185 /* favicon */
186 WebKitNetworkRequest *icon_request;
187 WebKitDownload *icon_download;
188 gchar *icon_dest_uri;
190 /* adjustments for browser */
191 GtkScrollbar *sb_h;
192 GtkScrollbar *sb_v;
193 GtkAdjustment *adjust_h;
194 GtkAdjustment *adjust_v;
196 /* flags */
197 int focus_wv;
198 int ctrl_click;
199 gchar *status;
200 int xtp_meaning; /* identifies dls/favorites */
201 gchar *tmp_uri;
202 int popup; /* 1 if cmd_entry has popup visible */
203 #ifdef USE_THREADS
204 /* https thread stuff */
205 GThread *thread;
206 #endif
207 /* hints */
208 int script_init;
209 int hints_on;
210 int new_tab;
212 /* custom stylesheet */
213 int styled;
214 char *stylesheet;
216 /* search */
217 char *search_text;
218 int search_forward;
219 guint search_id;
221 /* settings */
222 WebKitWebSettings *settings;
223 gchar *user_agent;
225 /* marks */
226 double mark[XT_NOMARKS];
228 /* inspector */
229 WebKitWebInspector *inspector;
230 GtkWidget *inspector_window;
231 GtkWidget *inspector_view;
233 TAILQ_HEAD(tab_list, tab);
235 struct karg {
236 int i;
237 char *s;
238 int precount;
241 struct download {
242 RB_ENTRY(download) entry;
243 int id;
244 WebKitDownload *download;
245 struct tab *tab;
247 RB_HEAD(download_list, download);
248 RB_PROTOTYPE(download_list, download, entry, download_rb_cmp);
250 struct history {
251 RB_ENTRY(history) entry;
252 const gchar *uri;
253 const gchar *title;
255 RB_HEAD(history_list, history);
256 RB_PROTOTYPE(history_list, history, entry, history_rb_cmp);
258 /* utility */
259 #define XT_NAME ("XXXTerm")
260 #define XT_CB_HANDLED (TRUE)
261 #define XT_CB_PASSTHROUGH (FALSE)
262 #define XT_FAVS_FILE ("favorites")
264 void xt_icon_from_file(struct tab *, char *);
265 GtkWidget *create_window(const gchar *);
266 void show_oops(struct tab *, const char *, ...);
267 gchar *get_html_page(gchar *, gchar *, gchar *, bool);
268 const gchar *get_uri(struct tab *);
269 const gchar *get_title(struct tab *, bool);
271 void load_webkit_string(struct tab *, const char *, gchar *);
272 void button_set_stockid(GtkWidget *, char *);
274 /* cookies */
275 int remove_cookie(int);
276 void print_cookie(char *msg, SoupCookie *);
277 void setup_cookies(void);
279 /* inspector */
280 #define XT_INS_SHOW (1<<0)
281 #define XT_INS_HIDE (1<<1)
282 #define XT_INS_CLOSE (1<<2)
284 WebKitWebView* inspector_inspect_web_view_cb(WebKitWebInspector *,
285 WebKitWebView*, struct tab *);
286 void setup_inspector(struct tab *);
287 int inspector_cmd(struct tab *, struct karg *);
289 /* about */
290 #define XT_XTP_STR "xxxt://"
291 #define XT_URI_ABOUT ("about:")
292 #define XT_URI_ABOUT_LEN (strlen(XT_URI_ABOUT))
293 #define XT_URI_ABOUT_ABOUT ("about")
294 #define XT_URI_ABOUT_BLANK ("blank")
295 #define XT_URI_ABOUT_CERTS ("certs")
296 #define XT_URI_ABOUT_COOKIEWL ("cookiewl")
297 #define XT_URI_ABOUT_COOKIEJAR ("cookiejar")
298 #define XT_URI_ABOUT_DOWNLOADS ("downloads")
299 #define XT_URI_ABOUT_FAVORITES ("favorites")
300 #define XT_URI_ABOUT_HELP ("help")
301 #define XT_URI_ABOUT_HISTORY ("history")
302 #define XT_URI_ABOUT_JSWL ("jswl")
303 #define XT_URI_ABOUT_PLUGINWL ("plwl")
304 #define XT_URI_ABOUT_SET ("set")
305 #define XT_URI_ABOUT_STATS ("stats")
306 #define XT_URI_ABOUT_MARCO ("marco")
307 #define XT_URI_ABOUT_STARTPAGE ("startpage")
309 struct about_type {
310 char *name;
311 int (*func)(struct tab *, struct karg *);
314 struct sp {
315 char *line;
316 TAILQ_ENTRY(sp) entry;
318 TAILQ_HEAD(sp_list, sp);
320 int blank(struct tab *, struct karg *);
321 int help(struct tab *, struct karg *);
322 int about(struct tab *, struct karg *);
323 int stats(struct tab *, struct karg *);
324 void show_certs(struct tab *, gnutls_x509_crt_t *,
325 size_t, char *);
326 int ca_cmd(struct tab *, struct karg *);
327 int cookie_show_wl(struct tab *, struct karg *);
328 int xtp_page_cl(struct tab *, struct karg *);
329 int xtp_page_dl(struct tab *, struct karg *);
330 int xtp_page_fl(struct tab *, struct karg *);
331 int xtp_page_hl(struct tab *, struct karg *);
332 int parse_xtp_url(struct tab *, const char *);
333 int add_favorite(struct tab *, struct karg *);
334 void update_favorite_tabs(struct tab *);
335 void update_history_tabs(struct tab *);
336 void update_download_tabs(struct tab *);
337 void xtp_generate_keys(void);
338 size_t about_list_size(void);
339 int cookie_cmd(struct tab *, struct karg *);
340 int js_cmd(struct tab *, struct karg *);
341 int pl_cmd(struct tab *, struct karg *);
342 void startpage_add(const char *, ...);
345 * xtp tab meanings
346 * identifies which tabs have xtp pages in (corresponding to about_list indices)
348 #define XT_XTP_TAB_MEANING_NORMAL (-1) /* normal url */
349 #define XT_XTP_TAB_MEANING_BL (1) /* about:blank in this tab */
350 #define XT_XTP_TAB_MEANING_CL (4) /* cookie manager in this tab */
351 #define XT_XTP_TAB_MEANING_DL (5) /* download manager in this tab */
352 #define XT_XTP_TAB_MEANING_FL (6) /* favorite manager in this tab */
353 #define XT_XTP_TAB_MEANING_HL (8) /* history manager in this tab */
355 /* whitelists */
356 #define XT_WL_TOGGLE (1<<0)
357 #define XT_WL_ENABLE (1<<1)
358 #define XT_WL_DISABLE (1<<2)
359 #define XT_WL_FQDN (1<<3) /* default */
360 #define XT_WL_TOPLEVEL (1<<4)
361 #define XT_WL_PERSISTENT (1<<5)
362 #define XT_WL_SESSION (1<<6)
363 #define XT_WL_RELOAD (1<<7)
364 #define XT_SHOW (1<<8)
365 #define XT_DELETE (1<<9)
366 #define XT_SAVE (1<<10)
367 #define XT_OPEN (1<<11)
369 #define XT_WL_INVALID (0)
370 #define XT_WL_JAVASCRIPT (1)
371 #define XT_WL_COOKIE (2)
372 #define XT_WL_PLUGIN (3)
374 struct domain {
375 RB_ENTRY(domain) entry;
376 gchar *d;
377 int handy; /* app use */
379 RB_HEAD(domain_list, domain);
380 RB_PROTOTYPE(domain_list, domain, entry, domain_rb_cmp);
382 int wl_show(struct tab *, struct karg *, char *,
383 struct domain_list *);
385 /* uri aliases */
386 struct alias {
387 char *a_name;
388 char *a_uri;
389 TAILQ_ENTRY(alias) entry;
391 TAILQ_HEAD(alias_list, alias);
393 /* mime types */
394 struct mime_type {
395 char *mt_type;
396 char *mt_action;
397 int mt_default;
398 int mt_download;
399 TAILQ_ENTRY(mime_type) entry;
401 TAILQ_HEAD(mime_type_list, mime_type);
403 struct domain * wl_find(const gchar *, struct domain_list *);
404 int wl_save(struct tab *, struct karg *, int);
405 int toggle_cwl(struct tab *, struct karg *);
406 int toggle_js(struct tab *, struct karg *);
407 int toggle_pl(struct tab *, struct karg *);
409 /* settings */
410 #define XT_BM_NORMAL (0)
411 #define XT_BM_WHITELIST (1)
412 #define XT_BM_KIOSK (2)
414 #define XT_TABS_NORMAL (0)
415 #define XT_TABS_COMPACT (1)
417 #define CTRL GDK_CONTROL_MASK
418 #define MOD1 GDK_MOD1_MASK
419 #define SHFT GDK_SHIFT_MASK
421 struct key_binding {
422 char *cmd;
423 guint mask;
424 guint use_in_entry;
425 guint key;
426 TAILQ_ENTRY(key_binding) entry; /* in bss so no need to init */
428 TAILQ_HEAD(keybinding_list, key_binding);
430 struct settings {
431 char *name;
432 int type;
433 #define XT_S_INVALID (0)
434 #define XT_S_INT (1)
435 #define XT_S_STR (2)
436 #define XT_S_FLOAT (3)
437 uint32_t flags;
438 #define XT_SF_RESTART (1<<0)
439 #define XT_SF_RUNTIME (1<<1)
440 int *ival;
441 char **sval;
442 struct special *s;
443 gfloat *fval;
444 int (*activate)(char *);
447 int set(struct tab *, struct karg *);
448 size_t get_settings_size(void);
449 int settings_add(char *, char *);
450 void setup_proxy(char *);
451 int set_browser_mode(struct settings *, char *);
452 int set_cookie_policy(struct settings *, char *);
453 char *get_browser_mode(struct settings *);
454 char *get_cookie_policy(struct settings *);
455 void init_keybindings(void);
456 void config_parse(char *, int);
457 char *get_setting_name(int);
459 extern int tabless;
460 extern int enable_socket;
461 extern int single_instance;
462 extern int fancy_bar;
463 extern int browser_mode;
464 extern int enable_localstorage;
465 extern char *statusbar_elems;
467 extern int show_tabs;
468 extern int tab_style;
469 extern int show_url;
470 extern int show_statusbar;
471 extern int ctrl_click_focus;
472 extern int cookies_enabled;
473 extern int read_only_cookies;
474 extern int enable_scripts;
475 extern int enable_plugins;
476 extern gfloat default_zoom_level;
477 extern char default_script[PATH_MAX];
478 extern int window_height;
479 extern int window_width;
480 extern int icon_size;
481 extern int refresh_interval;
482 extern int enable_plugin_whitelist;
483 extern int enable_cookie_whitelist;
484 extern int enable_js_whitelist;
485 extern int session_timeout;
486 extern int cookie_policy;
487 extern char *ssl_ca_file;
488 extern char *resource_dir;
489 extern gboolean ssl_strict_certs;
490 extern int append_next;
491 extern char *home;
492 extern char *search_string;
493 extern char *http_proxy;
494 extern char download_dir[PATH_MAX];
495 extern char runtime_settings[PATH_MAX];
496 extern int allow_volatile_cookies;
497 extern int save_global_history;
498 extern char *user_agent;
499 extern int save_rejected_cookies;
500 extern int session_autosave;
501 extern int guess_search;
502 extern int dns_prefetch;
503 extern gint max_connections;
504 extern gint max_host_connections;
505 extern gint enable_spell_checking;
506 extern char *spell_check_languages;
507 extern int xterm_workaround;
508 extern char *url_regex;
509 extern int history_autosave;
510 extern char search_file[PATH_MAX];
511 extern char command_file[PATH_MAX];
512 extern char *encoding;
513 extern int autofocus_onload;
514 extern char *cmd_font_name;
515 extern char *oops_font_name;
516 extern char *statusbar_font_name;
517 extern char *tabbar_font_name;
519 /* globals */
520 extern char *version;
521 extern char *icons[];
522 extern char rc_fname[PATH_MAX];
523 extern char work_dir[PATH_MAX];
524 extern struct passwd *pwd;
525 long long unsigned int blocked_cookies;
526 extern SoupCookieJar *s_cookiejar;
527 extern SoupCookieJar *p_cookiejar;
528 extern SoupSession *session;
530 extern void (*_soup_cookie_jar_add_cookie)(SoupCookieJar *, SoupCookie *);
532 extern struct history_list hl;
533 extern struct download_list downloads;
534 extern struct tab_list tabs;
535 extern struct about_type about_list[];
536 extern struct domain_list c_wl;
537 extern struct domain_list js_wl;
538 extern struct domain_list pl_wl;
539 extern struct alias_list aliases;
540 extern struct mime_type_list mtl;
541 extern struct keybinding_list kbl;
542 extern struct sp_list spl;
544 extern PangoFontDescription *cmd_font;
545 extern PangoFontDescription *oops_font;
546 extern PangoFontDescription *statusbar_font;
547 extern PangoFontDescription *tabbar_font;