Merge branch 'experimental' of git://github.com/Dieterbe/uzbl into replace-expand...
[uzbl-mobile] / uzbl.h
1 /* -*- c-basic-offset: 4; -*-
2
3  * See LICENSE for license details
4  *
5  * Changelog:
6  * ---------
7  *
8  * (c) 2009 by Robert Manea
9  *     - introduced struct concept
10  *     - statusbar template
11  *
12  */
13
14 enum {
15   /* statusbar symbols */
16   SYM_TITLE, SYM_URI, SYM_NAME,
17   SYM_LOADPRGS, SYM_LOADPRGSBAR,
18   SYM_KEYCMD, SYM_MODE, SYM_MSG,
19   SYM_SELECTED_URI,
20 };
21
22 const struct {
23     gchar *symbol_name;
24     guint symbol_token;
25 } symbols[] = {
26     {"KEYCMD",               SYM_KEYCMD},
27
28     {NULL,                   0}
29 }, *symp = symbols;
30
31 /* status bar elements */
32 typedef struct {
33     gint           load_progress;
34     gchar          *msg;
35     gchar          *progress_s, *progress_u;
36     int            progress_w;
37     gchar          *progress_bar;
38     gchar          *mode_indicator;
39 } StatusBar;
40
41
42 /* gui elements */
43 typedef struct {
44     GtkWidget*     main_window;
45     GtkPlug*       plug;
46     GtkWidget*     scrolled_win;
47     GtkWidget*     vbox;
48     GtkWidget*     mainbar;
49     GtkWidget*     mainbar_label;
50     GtkScrollbar*  scbar_v;   // Horizontal and Vertical Scrollbar
51     GtkScrollbar*  scbar_h;   // (These are still hidden)
52     GtkAdjustment* bar_v; // Information about document length
53     GtkAdjustment* bar_h; // and scrolling position
54     WebKitWebView* web_view;
55     gchar*         main_title;
56     gchar*         icon;
57
58     /* WebInspector */
59     GtkWidget *inspector_window;
60     WebKitWebInspector *inspector;
61
62     StatusBar sbar;
63 } GUI;
64
65
66 /* external communication*/
67 enum { FIFO, SOCKET};
68 typedef struct {
69     gchar          *fifo_path;
70     gchar          *socket_path;
71     /* stores (key)"variable name" -> (value)"pointer to this var*/
72     GHashTable     *proto_var;
73
74     gchar          *sync_stdout;
75 } Communication;
76
77
78 /* internal state */
79 typedef struct {
80     gchar    *uri;
81     gchar    *config_file;
82     int      socket_id;
83     char     *instance_name;
84     gchar    *selected_url;
85     gchar    *executable_path;
86     GString* keycmd;
87     gchar*   searchtx;
88     gboolean verbose;
89 } State;
90
91
92 /* networking */
93 typedef struct {
94     SoupSession *soup_session;
95     SoupLogger *soup_logger;
96     char *proxy_url;
97     char *useragent;
98     gint max_conns;
99     gint max_conns_host;
100 } Network;
101
102
103 /* behaviour */
104 typedef struct {
105     gchar*   load_finish_handler;
106     gchar*   load_start_handler;
107     gchar*   load_commit_handler;
108     gchar*   status_format;
109     gchar*   title_format_short;
110     gchar*   title_format_long;
111     gchar*   status_background;
112     gchar*   history_handler;
113     gchar*   fifo_dir;
114     gchar*   socket_dir;
115     gchar*   download_handler;
116     gchar*   cookie_handler;
117     gchar*   new_window;
118     gboolean always_insert_mode;
119     gboolean show_status;
120     gboolean insert_mode;
121     gboolean status_top;
122     gboolean reset_command_mode;
123     gchar*   modkey;
124     guint    modmask;
125     guint    http_debug;
126     gchar*   shell_cmd;
127     /* WebKitWebSettings exports */
128     guint    font_size;
129     guint    monospace_size;
130     guint    minimum_font_size;
131     gfloat   zoom_level;
132     guint    disable_plugins;
133     guint    disable_scripts;
134     guint    autoload_img;
135     guint    autoshrink_img;
136     guint    enable_spellcheck;
137     guint    enable_private;
138     guint    print_bg;
139     gchar*   style_uri;
140     guint    resizable_txt;
141     gchar*   default_encoding;
142     guint    enforce_96dpi;
143     gchar    *inject_html;
144     guint    caret_browsing;
145     guint    mode;
146     gchar*   base_url;
147     gchar*   html_endmarker;
148     gchar*   insert_indicator;
149     gchar*   cmd_indicator;
150     GString* html_buffer;
151     guint    html_timeout;
152     gboolean print_version;
153
154     /* command list: name -> Command  */
155     GHashTable* commands;
156 } Behaviour;
157
158 /* javascript */
159 typedef struct {
160     gboolean            initialized;
161     JSClassDefinition   classdef;
162     JSClassRef          classref;
163 } Javascript;
164
165 /* static information */
166 typedef struct {
167     int   webkit_major;
168     int   webkit_minor;
169     int   webkit_micro;
170     gchar *arch;
171     gchar *commit;
172 } Info;
173
174 /* main uzbl data structure */
175 typedef struct {
176     GUI           gui;
177     State         state;
178     Network       net;
179     Behaviour     behave;
180     Communication comm;
181     Javascript    js;
182     Info          info;
183
184     Window        xwin;
185     GScanner      *scan;
186
187     /* group bindings: key -> action */
188     GHashTable* bindings;
189 } Uzbl;
190
191
192 typedef struct {
193     char* name;
194     char* param;
195 } Action;
196
197 typedef void sigfunc(int);
198
199 /* XDG Stuff */
200
201 typedef struct {
202     gchar* environmental;
203     gchar* default_value;
204 } XDG_Var;
205
206 XDG_Var XDG[] =
207 {
208     { "XDG_CONFIG_HOME", "~/.config" },
209     { "XDG_DATA_HOME",   "~/.local/share" },
210     { "XDG_CACHE_HOME",  "~/.cache" },
211     { "XDG_CONFIG_DIRS", "/etc/xdg" },
212     { "XDG_DATA_DIRS",   "/usr/local/share/:/usr/share/" },
213 };
214
215 /* Functions */
216 void
217 setup_scanner();
218
219 char *
220 itos(int val);
221
222 static char *
223 str_replace (const char* search, const char* replace, const char* string);
224
225 static GArray*
226 read_file_by_line (gchar *path);
227
228 static
229 gchar* parseenv (char* string);
230
231 static void
232 clean_up(void);
233
234 static void
235 catch_sigterm(int s);
236
237 static sigfunc *
238 setup_signal(int signe, sigfunc *shandler);
239
240 gboolean
241 set_var_value(gchar *name, gchar *val);
242
243 static void
244 print(WebKitWebView *page, GArray *argv, GString *result);
245
246 static gboolean
247 new_window_cb (WebKitWebView *web_view, WebKitWebFrame *frame, WebKitNetworkRequest *request, WebKitWebNavigationAction *navigation_action, WebKitWebPolicyDecision *policy_decision, gpointer user_data);
248
249 static gboolean
250 mime_policy_cb(WebKitWebView *web_view, WebKitWebFrame *frame, WebKitNetworkRequest *request, gchar *mime_type,  WebKitWebPolicyDecision *policy_decision, gpointer user_data);
251
252 WebKitWebView*
253 create_web_view_cb (WebKitWebView  *web_view, WebKitWebFrame *frame, gpointer user_data);
254
255 static gboolean
256 download_cb (WebKitWebView *web_view, GObject *download, gpointer user_data);
257
258 static void
259 toggle_zoom_type (WebKitWebView* page, GArray *argv, GString *result);
260
261 static void
262 toggle_status_cb (WebKitWebView* page, GArray *argv, GString *result);
263
264 static void
265 link_hover_cb (WebKitWebView* page, const gchar* title, const gchar* link, gpointer data);
266
267 static void
268 title_change_cb (WebKitWebView* web_view, GParamSpec param_spec);
269
270 void
271 progress_change_cb (WebKitWebView* page, gint progress, gpointer data);
272
273 static void
274 load_commit_cb (WebKitWebView* page, WebKitWebFrame* frame, gpointer data);
275
276 static void
277 load_start_cb (WebKitWebView* page, WebKitWebFrame* frame, gpointer data);
278
279 static void
280 load_finish_cb (WebKitWebView* page, WebKitWebFrame* frame, gpointer data);
281
282 static void
283 destroy_cb (GtkWidget* widget, gpointer data);
284
285 static void
286 log_history_cb ();
287
288 static void
289 commands_hash(void);
290
291 void
292 free_action(gpointer act);
293
294 Action*
295 new_action(const gchar *name, const gchar *param);
296
297 static bool
298 file_exists (const char * filename);
299
300 void set_insert_mode(gboolean mode);
301
302 static void
303 toggle_insert_mode(WebKitWebView *page, GArray *argv, GString *result);
304
305 static void
306 load_uri (WebKitWebView * web_view, GArray *argv, GString *result);
307
308 static void
309 new_window_load_uri (const gchar * uri);
310
311 static void
312 chain (WebKitWebView *page, GArray *argv, GString *result);
313
314 static void
315 keycmd (WebKitWebView *page, GArray *argv, GString *result);
316
317 static void
318 keycmd_nl (WebKitWebView *page, GArray *argv, GString *result);
319
320 static void
321 keycmd_bs (WebKitWebView *page, GArray *argv, GString *result);
322
323 static void
324 close_uzbl (WebKitWebView *page, GArray *argv, GString *result);
325
326 static gboolean
327 run_command(const gchar *command, const guint npre,
328             const gchar **args, const gboolean sync, char **output_stdout);
329
330 static char*
331 build_progressbar_ascii(int percent);
332
333 static void
334 spawn(WebKitWebView *web_view, GArray *argv, GString *result);
335
336 static void
337 spawn_sh(WebKitWebView *web_view, GArray *argv, GString *result);
338
339 static void
340 spawn_sync(WebKitWebView *web_view, GArray *argv, GString *result);
341
342 static void
343 spawn_sh_sync(WebKitWebView *web_view, GArray *argv, GString *result);
344
345 static void
346 parse_command(const char *cmd, const char *param, GString *result);
347
348 static void
349 parse_cmd_line(const char *ctl_line, GString *result);
350
351 static gchar*
352 build_stream_name(int type, const gchar *dir);
353
354 static gboolean
355 control_fifo(GIOChannel *gio, GIOCondition condition);
356
357 static gchar*
358 init_fifo(gchar *dir);
359
360 static gboolean
361 control_stdin(GIOChannel *gio, GIOCondition condition);
362
363 static void
364 create_stdin();
365
366 static gchar*
367 init_socket(gchar *dir);
368
369 static gboolean
370 control_socket(GIOChannel *chan);
371
372 static gboolean
373 control_client_socket(GIOChannel *chan);
374
375 static void
376 update_title (void);
377
378 static gboolean
379 key_press_cb (GtkWidget* window, GdkEventKey* event);
380
381 static void
382 run_keycmd(const gboolean key_ret);
383
384 static void
385 exec_paramcmd(const Action* act, const guint i);
386
387 void
388 initialize ();
389
390 GtkWidget*
391 create_browser ();
392
393 static GtkWidget*
394 create_mainbar ();
395
396 static
397 GtkWidget* create_window ();
398
399 static
400 GtkPlug* create_plug ();
401
402 static void
403 run_handler (const gchar *act, const gchar *args);
404
405 static void
406 add_binding (const gchar *key, const gchar *act);
407
408 static gchar*
409 get_xdg_var (XDG_Var xdg);
410
411 static gchar*
412 find_xdg_file (int xdg_type, char* filename);
413
414 static void
415 settings_init ();
416
417 static void
418 search_text (WebKitWebView *page, GArray *argv, const gboolean forward);
419
420 static void
421 search_forward_text (WebKitWebView *page, GArray *argv, GString *result);
422
423 static void
424 search_reverse_text (WebKitWebView *page, GArray *argv, GString *result);
425
426 static void
427 dehilight (WebKitWebView *page, GArray *argv, GString *result);
428
429 static void
430 run_js (WebKitWebView * web_view, GArray *argv, GString *result);
431
432 static void
433 run_external_js (WebKitWebView * web_view, GArray *argv, GString *result);
434
435 static void
436 eval_js(WebKitWebView * web_view, gchar *script, GString *result);
437
438 static void handle_cookies (SoupSession *session,
439                             SoupMessage *msg,
440                             gpointer     user_data);
441 static void
442 save_cookies (SoupMessage *msg,
443                 gpointer     user_data);
444
445 static void
446 set_var(WebKitWebView *page, GArray *argv, GString *result);
447
448 static void
449 act_bind(WebKitWebView *page, GArray *argv, GString *result);
450
451 static void
452 act_dump_config();
453
454 static void
455 render_html();
456
457 static void
458 set_timeout(int seconds);
459
460 static void
461 dump_var_hash(gpointer k, gpointer v, gpointer ud);
462
463 static void
464 dump_key_hash(gpointer k, gpointer v, gpointer ud);
465
466 static void
467 dump_config();
468
469
470 typedef void (*Command)(WebKitWebView*, GArray *argv, GString *result);
471 typedef struct {
472     Command function;
473     gboolean no_split;
474 } CommandInfo;
475
476 /* Command callbacks */
477 static void
478 cmd_load_uri();
479
480 static void
481 cmd_set_status();
482
483 static void
484 set_proxy_url();
485
486 static void
487 set_icon();
488
489 static void
490 cmd_cookie_handler();
491
492 static void
493 cmd_new_window();
494
495 static void
496 move_statusbar();
497
498 static void
499 cmd_always_insert_mode();
500
501 static void
502 cmd_http_debug();
503
504 static void
505 cmd_max_conns();
506
507 static void
508 cmd_max_conns_host();
509
510 /* exported WebKitWebSettings properties */
511
512 static void
513 cmd_font_size();
514
515 static void
516 cmd_zoom_level();
517
518 static void
519 cmd_disable_plugins();
520
521 static void
522 cmd_disable_scripts();
523
524 static void
525 cmd_minimum_font_size();
526
527 static void
528 cmd_fifo_dir();
529
530 static void
531 cmd_socket_dir();
532
533 static void
534 cmd_modkey();
535
536 void
537 cmd_useragent() ;
538
539 static void
540 cmd_autoload_img();
541
542 static void
543 cmd_autoshrink_img();
544
545 static void
546 cmd_enable_spellcheck();
547
548 static void
549 cmd_enable_private();
550
551 static void
552 cmd_print_bg();
553
554 static void
555 cmd_style_uri();
556
557 static void
558 cmd_resizable_txt();
559
560 static void
561 cmd_default_encoding();
562
563 static void
564 cmd_enforce_96dpi();
565
566 static void
567 cmd_inject_html();
568
569 static void
570 cmd_caret_browsing();
571
572 /* vi: set et ts=4: */