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