aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/variables.c
blob: e4763bcc1999c8ecfa22712e5bbd99a2c14b9a93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
#include "variables.h"
#include "uzbl-core.h"
#include "callbacks.h"
#include "events.h"
#include "io.h"
#include "util.h"

uzbl_cmdprop *
get_var_c(const gchar *name) {
    return g_hash_table_lookup(uzbl.behave.proto_var, name);
}

void
send_set_var_event(const char *name, const uzbl_cmdprop *c) {
    /* check for the variable type */
    switch(c->type) {
    case TYPE_STR:
    {
        gchar *v = get_var_value_string_c(c);
        send_event (VARIABLE_SET, NULL,
            TYPE_NAME, name,
            TYPE_NAME, "str",
            TYPE_STR, v,
            NULL);
        g_free(v);
        break;
    }
    case TYPE_INT:
        send_event (VARIABLE_SET, NULL,
            TYPE_NAME, name,
            TYPE_NAME, "int",
            TYPE_INT, get_var_value_int_c(c),
            NULL);
        break;
    case TYPE_FLOAT:
        send_event (VARIABLE_SET, NULL,
            TYPE_NAME, name,
            TYPE_NAME, "float",
            TYPE_FLOAT, get_var_value_float_c(c),
            NULL);
        break;
    default:
        g_assert_not_reached();
    }
}

void
expand_variable(GString *buf, const gchar *name) {
    uzbl_cmdprop* c = get_var_c(name);
    if(c) {
        if(c->type == TYPE_STR) {
            gchar *v = get_var_value_string_c(c);
            g_string_append(buf, v);
            g_free(v);
        } else if(c->type == TYPE_INT)
            g_string_append_printf(buf, "%d", get_var_value_int_c(c));
        else if(c->type == TYPE_FLOAT)
            g_string_append_printf(buf, "%f", get_var_value_float_c(c));
    }
}

void
set_var_value_string_c(uzbl_cmdprop *c, const gchar *val) {
    if(c->setter)
        ((void (*)(const gchar *))c->setter)(val);
    else {
        g_free(*(c->ptr.s));
        *(c->ptr.s) = g_strdup(val);
    }
}

void
set_var_value_int_c(uzbl_cmdprop *c, int i) {
    if(c->setter)
        ((void (*)(int))c->setter)(i);
    else
        *(c->ptr.i) = i;
}

void
set_var_value_float_c(uzbl_cmdprop *c, float f) {
    if(c->setter)
        ((void (*)(float))c->setter)(f);
    else
        *(c->ptr.f) = f;
}

gboolean
set_var_value(const gchar *name, gchar *val) {
    g_assert(val != NULL);

    uzbl_cmdprop *c = get_var_c(name);

    if(c) {
        if(!c->writeable) return FALSE;

        switch(c->type) {
        case TYPE_STR:
            set_var_value_string_c(c, val);
            break;
        case TYPE_INT:
        {
            int i = (int)strtoul(val, NULL, 10);
            set_var_value_int_c(c, i);
            break;
        }
        case TYPE_FLOAT:
        {
            float f = strtod(val, NULL);
            set_var_value_float_c(c, f);
            break;
        }
        default:
            g_assert_not_reached();
        }

        send_set_var_event(name, c);
    } else {
        /* a custom var that has not been set. */
        /* check whether name violates our naming scheme */
        if(!valid_name(name)) {
            if (uzbl.state.verbose)
                printf("Invalid variable name: %s\n", name);
            return FALSE;
        }

        /* create the cmdprop */
        c = g_malloc(sizeof(uzbl_cmdprop));
        c->type       = TYPE_STR;
        c->dump       = 0;
        c->getter     = NULL;
        c->setter     = NULL;
        c->writeable  = 1;

        c->ptr.s    = g_malloc(sizeof(gchar*));

        g_hash_table_insert(uzbl.behave.proto_var,
                g_strdup(name), (gpointer) c);

        /* set the value. */
        *(c->ptr.s) = g_strdup(val);

        send_set_var_event(name, c);
    }
    update_title();
    return TRUE;
}

gchar*
get_var_value_string_c(const uzbl_cmdprop *c) {
    if(!c) return NULL;

    gchar *result = NULL;

    if(c->getter) {
        result = ((gchar *(*)())c->getter)();
    } else if(c->ptr.s)
        result = g_strdup(*(c->ptr.s));

    return result ? result : g_strdup("");
}

gchar*
get_var_value_string(const gchar *name) {
    uzbl_cmdprop *c = get_var_c(name);
    return get_var_value_string_c(c);
}

int
get_var_value_int_c(const uzbl_cmdprop *c) {
    if(!c) return 0;

    if(c->getter) {
        return ((int (*)())c->getter)();
    } else if(c->ptr.i)
        return *(c->ptr.i);

    return 0;
}

int
get_var_value_int(const gchar *name) {
    uzbl_cmdprop *c = get_var_c(name);
    return get_var_value_int_c(c);
}

float
get_var_value_float_c(const uzbl_cmdprop *c) {
    if(!c) return 0;

    if(c->getter) {
        return ((float (*)())c->getter)();
    } else if(c->ptr.f)
        return *(c->ptr.f);

    return 0;
}

float
get_var_value_float(const gchar *name) {
    uzbl_cmdprop *c = get_var_c(name);
    return get_var_value_float_c(c);
}

void
dump_var_hash(gpointer k, gpointer v, gpointer ud) {
    (void) ud;
    uzbl_cmdprop *c = v;

    if(!c->dump)
        return;

    if(c->type == TYPE_STR) {
        gchar *v = get_var_value_string_c(c);
        printf("set %s = %s\n", (char *)k, v);
        g_free(v);
    } else if(c->type == TYPE_INT)
        printf("set %s = %d\n", (char *)k, get_var_value_int_c(c));
    else if(c->type == TYPE_FLOAT)
        printf("set %s = %f\n", (char *)k, get_var_value_float_c(c));
}

void
dump_config() {
    g_hash_table_foreach(uzbl.behave.proto_var, dump_var_hash, NULL);
}

void
dump_var_hash_as_event(gpointer k, gpointer v, gpointer ud) {
    (void) ud;
    uzbl_cmdprop *c = v;

    if(c->dump)
        send_set_var_event(k, c);
}

void
dump_config_as_events() {
    g_hash_table_foreach(uzbl.behave.proto_var, dump_var_hash_as_event, NULL);
}

/* is the given string made up entirely of decimal digits? */
gboolean
string_is_integer(const char *s) {
    return (strspn(s, "0123456789") == strlen(s));
}


GObject*
view_settings() {
    return G_OBJECT(webkit_web_view_get_settings(uzbl.gui.web_view));
}

void
set_window_property(const gchar* prop, const gchar* value) {
    if(GTK_IS_WIDGET(uzbl.gui.main_window)) {
        gdk_property_change(
            gtk_widget_get_window (GTK_WIDGET (uzbl.gui.main_window)),
            gdk_atom_intern_static_string(prop),
            gdk_atom_intern_static_string("STRING"),
            8,
            GDK_PROP_MODE_REPLACE,
            (const guchar*)value,
            strlen(value));
    }
}

void
uri_change_cb (WebKitWebView *web_view, GParamSpec param_spec) {
    (void) param_spec;

    g_free (uzbl.state.uri);
    g_object_get (web_view, "uri", &uzbl.state.uri, NULL);

    g_setenv("UZBL_URI", uzbl.state.uri, TRUE);
    set_window_property("UZBL_URI", uzbl.state.uri);
}

gchar *
make_uri_from_user_input(const gchar *uri) {
    gchar *result = NULL;

    SoupURI *soup_uri = soup_uri_new(uri);
    if (soup_uri) {
        /* this looks like a valid URI. */
        if(soup_uri->host == NULL && string_is_integer(soup_uri->path))
            /* the user probably typed in a host:port without a scheme. */
            result = g_strconcat("http://", uri, NULL);
        else
            result = g_strdup(uri);

        soup_uri_free(soup_uri);

        return result;
    }

    /* it's not a valid URI, maybe it's a path on the filesystem?
     * check to see if such a path exists. */
    if (file_exists(uri)) {
        if (g_path_is_absolute (uri))
            return g_strconcat("file://", uri, NULL);

        /* make it into an absolute path */
        gchar *wd = g_get_current_dir ();
        result = g_strconcat("file://", wd, "/", uri, NULL);
        g_free(wd);

        return result;
    }

    /* not a path on the filesystem, just assume it's an HTTP URL. */
    return g_strconcat("http://", uri, NULL);
}

void
set_uri(const gchar *uri) {
    /* Strip leading whitespace */
    while (*uri && isspace(*uri))
        uri++;

    /* don't do anything when given a blank URL */
    if(uri[0] == 0)
        return;

    g_free(uzbl.state.uri);
    uzbl.state.uri = g_strdup(uri);

    /* evaluate javascript: URIs */
    if (!strncmp (uri, "javascript:", 11)) {
        eval_js(uzbl.gui.web_view, uri, NULL, "javascript:");
        return;
    }

    /* attempt to parse the URI */
    gchar *newuri = make_uri_from_user_input(uri);

    set_window_property("UZBL_URI", newuri);
    webkit_web_view_load_uri (uzbl.gui.web_view, newuri);

    g_free (newuri);
}

void
set_max_conns(int max_conns) {
    uzbl.net.max_conns = max_conns;

    g_object_set(G_OBJECT(uzbl.net.soup_session),
            SOUP_SESSION_MAX_CONNS, uzbl.net.max_conns, NULL);
}

void
set_max_conns_host(int max_conns_host) {
    uzbl.net.max_conns_host = max_conns_host;

    g_object_set(G_OBJECT(uzbl.net.soup_session),
            SOUP_SESSION_MAX_CONNS_PER_HOST, uzbl.net.max_conns_host, NULL);
}

void
set_http_debug(int debug) {
    uzbl.behave.http_debug = debug;

    if(uzbl.net.soup_logger) {
        soup_session_remove_feature
            (uzbl.net.soup_session, SOUP_SESSION_FEATURE(uzbl.net.soup_logger));
        g_object_unref (uzbl.net.soup_logger);
    }

    uzbl.net.soup_logger = soup_logger_new(uzbl.behave.http_debug, -1);
    soup_session_add_feature(uzbl.net.soup_session,
            SOUP_SESSION_FEATURE(uzbl.net.soup_logger));
}

void
set_ca_file(gchar *path) {
    g_object_set (uzbl.net.soup_session, "ssl-ca-file", path, NULL);
}

gchar *
get_ca_file() {
    gchar *path;
    g_object_get (uzbl.net.soup_session, "ssl-ca-file", &path, NULL);
    return path;
}

void
set_verify_cert(int strict) {
    g_object_set (uzbl.net.soup_session, "ssl-strict", strict, NULL);
}

int
get_verify_cert() {
    int strict;
    g_object_get (uzbl.net.soup_session, "ssl-strict", &strict, NULL);
    return strict;
}

#define EXPOSE_WEBKIT_VIEW_SETTINGS(SYM, PROPERTY, TYPE) \
void set_##SYM(TYPE val) { \
  g_object_set(view_settings(), (PROPERTY), val, NULL); \
} \
TYPE get_##SYM() { \
  TYPE val; \
  g_object_get(view_settings(), (PROPERTY), &val, NULL); \
  return val; \
}

EXPOSE_WEBKIT_VIEW_SETTINGS(default_font_family,    "default-font-family",    gchar *)
EXPOSE_WEBKIT_VIEW_SETTINGS(monospace_font_family,  "monospace-font-family",  gchar *)
EXPOSE_WEBKIT_VIEW_SETTINGS(sans_serif_font_family, "sans_serif-font-family", gchar *)
EXPOSE_WEBKIT_VIEW_SETTINGS(serif_font_family,      "serif-font-family",      gchar *)
EXPOSE_WEBKIT_VIEW_SETTINGS(cursive_font_family,    "cursive-font-family",    gchar *)
EXPOSE_WEBKIT_VIEW_SETTINGS(fantasy_font_family,    "fantasy-font-family",    gchar *)

EXPOSE_WEBKIT_VIEW_SETTINGS(minimum_font_size,      "minimum-font-size",            int)
EXPOSE_WEBKIT_VIEW_SETTINGS(font_size,              "default-font-size",            int)
EXPOSE_WEBKIT_VIEW_SETTINGS(monospace_size,         "default-monospace-font-size",  int)

EXPOSE_WEBKIT_VIEW_SETTINGS(enable_plugins, "enable-plugins", int)
EXPOSE_WEBKIT_VIEW_SETTINGS(enable_scripts, "enable-scripts", int)

EXPOSE_WEBKIT_VIEW_SETTINGS(javascript_windows, "javascript-can-open-windows-automatically", int)

EXPOSE_WEBKIT_VIEW_SETTINGS(autoload_images,       "auto-load-images",   int)
EXPOSE_WEBKIT_VIEW_SETTINGS(autoshrink_images,     "auto-shrink-images", int)

EXPOSE_WEBKIT_VIEW_SETTINGS(enable_pagecache,   "enable-page-cache",       int)
EXPOSE_WEBKIT_VIEW_SETTINGS(enable_private,     "enable-private-browsing", int)

EXPOSE_WEBKIT_VIEW_SETTINGS(enable_spellcheck,     "enable-spell-checking",    int)
EXPOSE_WEBKIT_VIEW_SETTINGS(spellcheck_languages,  "spell-checking-languages", gchar *)
EXPOSE_WEBKIT_VIEW_SETTINGS(resizable_text_areas,  "resizable-text-areas",     int)

EXPOSE_WEBKIT_VIEW_SETTINGS(stylesheet_uri,   "user-stylesheet-uri", gchar *)
EXPOSE_WEBKIT_VIEW_SETTINGS(print_bg,         "print-backgrounds",   int)
EXPOSE_WEBKIT_VIEW_SETTINGS(enforce_96_dpi,   "enforce-96-dpi",      int)

EXPOSE_WEBKIT_VIEW_SETTINGS(caret_browsing,   "enable-caret-browsing", int)

EXPOSE_WEBKIT_VIEW_SETTINGS(enable_cross_file_access, "enable-file-access-from-file-uris", int)

EXPOSE_WEBKIT_VIEW_SETTINGS(default_encoding, "default-encoding", gchar *)

void
set_proxy_url(const gchar *proxy_url) {
    g_free(uzbl.net.proxy_url);
    uzbl.net.proxy_url = g_strdup(proxy_url);

    const gchar *url = uzbl.net.proxy_url;
    SoupSession *session  = uzbl.net.soup_session;
    SoupURI     *soup_uri = NULL;

    if (url != NULL || *url != 0 || *url != ' ')
        soup_uri = soup_uri_new(url);

    g_object_set(G_OBJECT(session), SOUP_SESSION_PROXY_URI, soup_uri, NULL);

    if(soup_uri)
        soup_uri_free(soup_uri);
}

void
set_authentication_handler(const gchar *handler) {
    /* Check if WEBKIT_TYPE_SOUP_AUTH_DIALOG feature is set */
    GSList *flist = soup_session_get_features (uzbl.net.soup_session, (GType) WEBKIT_TYPE_SOUP_AUTH_DIALOG);
    guint feature_is_set = g_slist_length(flist);
    g_slist_free(flist);

    g_free(uzbl.behave.authentication_handler);
    uzbl.behave.authentication_handler = g_strdup(handler);

    if (uzbl.behave.authentication_handler == NULL || *uzbl.behave.authentication_handler == 0) {
        if (!feature_is_set)
            soup_session_add_feature_by_type
                (uzbl.net.soup_session, (GType) WEBKIT_TYPE_SOUP_AUTH_DIALOG);
    } else {
        if (feature_is_set)
            soup_session_remove_feature_by_type
                (uzbl.net.soup_session, (GType) WEBKIT_TYPE_SOUP_AUTH_DIALOG);
    }
}

void
set_status_background(const gchar *background) {
    /* labels and hboxes do not draw their own background. applying this
     * on the vbox/main_window is ok as the statusbar is the only affected
     * widget. (if not, we could also use GtkEventBox) */
    GtkWidget* widget = uzbl.gui.main_window ? uzbl.gui.main_window : GTK_WIDGET (uzbl.gui.plug);

    g_free(uzbl.behave.status_background);
    uzbl.behave.status_background = g_strdup(background);

#if GTK_CHECK_VERSION(2,91,0)
    GdkRGBA color;
    gdk_rgba_parse (&color, uzbl.behave.status_background);
    gtk_widget_override_background_color (widget, GTK_STATE_NORMAL, &color);
#else
    GdkColor color;
    gdk_color_parse (uzbl.behave.status_background, &color);
    gtk_widget_modify_bg (widget, GTK_STATE_NORMAL, &color);
#endif
}

void
set_icon(const gchar *icon) {
    if(file_exists(icon) && uzbl.gui.main_window) {
        g_free(uzbl.gui.icon);
        uzbl.gui.icon = g_strdup(icon);

        gtk_window_set_icon_from_file (GTK_WINDOW (uzbl.gui.main_window), uzbl.gui.icon, NULL);
    } else {
        g_printerr ("Icon \"%s\" not found. ignoring.\n", icon);
    }
}

void
set_window_role(const gchar *role) {
    if (!uzbl.gui.main_window)
        return;

    gtk_window_set_role(GTK_WINDOW (uzbl.gui.main_window), role);
}

gchar *
get_window_role() {
    if (!uzbl.gui.main_window)
        return NULL;

    const gchar* role = gtk_window_get_role(GTK_WINDOW (uzbl.gui.main_window));
    return g_strdup(role);
}

gchar *
get_geometry() {
    int w, h, x, y;
    GString *buf = g_string_new("");

    if(uzbl.gui.main_window) {
      gtk_window_get_size(GTK_WINDOW(uzbl.gui.main_window), &w, &h);
      gtk_window_get_position(GTK_WINDOW(uzbl.gui.main_window), &x, &y);

      g_string_printf(buf, "%dx%d+%d+%d", w, h, x, y);
    }

    return g_string_free(buf, FALSE);
}

void
set_geometry(const gchar *geometry) {
    if(!geometry)
        return;

    if(geometry[0] == 'm') { /* m/maximize/maximized */
        gtk_window_maximize((GtkWindow *)(uzbl.gui.main_window));
    } else {
        int x=0, y=0;
        unsigned int w=0, h=0;

        /* we used to use gtk_window_parse_geometry() but that didn't work
         * how it was supposed to. */
        int ret = XParseGeometry(uzbl.gui.geometry, &x, &y, &w, &h);

        if(ret & XValue)
            gtk_window_move((GtkWindow *)uzbl.gui.main_window, x, y);

        if(ret & WidthValue)
            gtk_window_resize((GtkWindow *)uzbl.gui.main_window, w, h);
    }

    /* get the actual geometry (which might be different from what was
     * specified) and store it (since the GEOMETRY_CHANGED event needs to
     * know what it changed from) */
    g_free(uzbl.gui.geometry);
    uzbl.gui.geometry = get_geometry();
}

void
set_show_status(int show_status) {
    gtk_widget_set_visible(uzbl.gui.status_bar, show_status);
    update_title();
}

int
get_show_status() {
  return gtk_widget_get_visible(uzbl.gui.status_bar);
}

void
set_status_top(int status_top) {
    if (!uzbl.gui.scrolled_win && !uzbl.gui.status_bar)
        return;

    uzbl.behave.status_top = status_top;

    g_object_ref(uzbl.gui.scrolled_win);
    g_object_ref(uzbl.gui.status_bar);
    gtk_container_remove(GTK_CONTAINER(uzbl.gui.vbox), uzbl.gui.scrolled_win);
    gtk_container_remove(GTK_CONTAINER(uzbl.gui.vbox), uzbl.gui.status_bar);

    if(uzbl.behave.status_top) {
        gtk_box_pack_start (GTK_BOX (uzbl.gui.vbox), uzbl.gui.status_bar,   FALSE, TRUE, 0);
        gtk_box_pack_start (GTK_BOX (uzbl.gui.vbox), uzbl.gui.scrolled_win, TRUE,  TRUE, 0);
    } else {
        gtk_box_pack_start (GTK_BOX (uzbl.gui.vbox), uzbl.gui.scrolled_win, TRUE,  TRUE, 0);
        gtk_box_pack_start (GTK_BOX (uzbl.gui.vbox), uzbl.gui.status_bar,   FALSE, TRUE, 0);
    }

    g_object_unref(uzbl.gui.scrolled_win);
    g_object_unref(uzbl.gui.status_bar);

    if (!uzbl.state.plug_mode)
        gtk_widget_grab_focus (GTK_WIDGET (uzbl.gui.web_view));
}

void
set_current_encoding(const gchar *encoding) {
    if(strlen(encoding) == 0)
        encoding = NULL;

    webkit_web_view_set_custom_encoding(uzbl.gui.web_view, encoding);
}

gchar *
get_current_encoding() {
    const gchar *encoding = webkit_web_view_get_custom_encoding(uzbl.gui.web_view);
    return g_strdup(encoding);
}

void
set_fifo_dir(const gchar *fifo_dir) {
    g_free(uzbl.behave.fifo_dir);

    if(init_fifo(fifo_dir))
      uzbl.behave.fifo_dir = g_strdup(fifo_dir);
    else
      uzbl.behave.fifo_dir = NULL;
}

void
set_socket_dir(const gchar *socket_dir) {
    g_free(uzbl.behave.socket_dir);

    if(init_socket(socket_dir))
      uzbl.behave.socket_dir = g_strdup(socket_dir);
    else
      uzbl.behave.socket_dir = NULL;
}

void
set_inject_html(const gchar *html) {
    webkit_web_view_load_html_string (uzbl.gui.web_view, html, NULL);
}

void
set_useragent(const gchar *useragent) {
    g_free(uzbl.net.useragent);

    if (*useragent == ' ') {
        uzbl.net.useragent = NULL;
    } else {
        uzbl.net.useragent = g_strdup(useragent);

        g_object_set(G_OBJECT(uzbl.net.soup_session), SOUP_SESSION_USER_AGENT,
            uzbl.net.useragent, NULL);
    }
}

void
set_accept_languages(const gchar *accept_languages) {
    g_free(uzbl.net.accept_languages);

    if (*accept_languages == ' ') {
        uzbl.net.accept_languages = NULL;
    } else {
        uzbl.net.accept_languages = g_strdup(accept_languages);

        g_object_set(G_OBJECT(uzbl.net.soup_session),
            SOUP_SESSION_ACCEPT_LANGUAGE, uzbl.net.accept_languages, NULL);
    }
}

/* requires webkit >=1.1.14 */
void
set_view_source(int view_source) {
    uzbl.behave.view_source = view_source;

    webkit_web_view_set_view_source_mode(uzbl.gui.web_view,
            (gboolean) uzbl.behave.view_source);
}

void
set_zoom_type (int type) {
    webkit_web_view_set_full_content_zoom (uzbl.gui.web_view, type);
}

int
get_zoom_type () {
    return webkit_web_view_get_full_content_zoom (uzbl.gui.web_view);
}

void
set_zoom_level(float zoom_level) {
    webkit_web_view_set_zoom_level (uzbl.gui.web_view, zoom_level);
}

float
get_zoom_level() {
    return webkit_web_view_get_zoom_level (uzbl.gui.web_view);
}

/* abbreviations to help keep the table's width humane */

/* variables */
#define PTR_V_STR(var, d, set)   { .ptr = { .s = &(var) },       .type = TYPE_STR,   .dump = d, .writeable = 1, .getter = NULL, .setter = (uzbl_fp)set }
#define PTR_V_INT(var, d, set)   { .ptr = { .i = (int*)&(var) }, .type = TYPE_INT,   .dump = d, .writeable = 1, .getter = NULL, .setter = (uzbl_fp)set }
#define PTR_V_FLOAT(var, d, set) { .ptr = { .f = &(var) },       .type = TYPE_FLOAT, .dump = d, .writeable = 1, .getter = NULL, .setter = (uzbl_fp)set }

#define PTR_V_STR_GETSET(var)    { .type = TYPE_STR,   .dump = 1, .writeable = 1, .getter = (uzbl_fp) get_##var, .setter = (uzbl_fp)set_##var }
#define PTR_V_INT_GETSET(var)    { .type = TYPE_INT,   .dump = 1, .writeable = 1, .getter = (uzbl_fp) get_##var, .setter = (uzbl_fp)set_##var }
#define PTR_V_FLOAT_GETSET(var)  { .type = TYPE_FLOAT, .dump = 1, .writeable = 1, .getter = (uzbl_fp) get_##var, .setter = (uzbl_fp)set_##var }

/* constants */
#define PTR_C_STR(var)           { .ptr = { .s = &(var) },       .type = TYPE_STR,   .dump = 0, .writeable = 0, .getter = NULL, .setter = NULL }
#define PTR_C_INT(var)           { .ptr = { .i = (int*)&(var) }, .type = TYPE_INT,   .dump = 0, .writeable = 0, .getter = NULL, .setter = NULL }
#define PTR_C_FLOAT(var)         { .ptr = { .f = &(var) },       .type = TYPE_FLOAT, .dump = 0, .writeable = 0, .getter = NULL, .setter = NULL }

const struct var_name_to_ptr_t {
    const char *name;
    uzbl_cmdprop cp;
} var_name_to_ptr[] = {
/*    variable name            pointer to variable in code                     dump callback function    */
/*  ---------------------------------------------------------------------------------------------- */
    { "uri",                    PTR_V_STR(uzbl.state.uri,                       1,   set_uri)},

    { "verbose",                PTR_V_INT(uzbl.state.verbose,                   1,   NULL)},
    { "print_events",           PTR_V_INT(uzbl.state.events_stdout,             1,   NULL)},

    { "handle_multi_button",    PTR_V_INT(uzbl.state.handle_multi_button,        1,   NULL)},

    { "show_status",            PTR_V_INT_GETSET(show_status)},
    { "status_top",             PTR_V_INT(uzbl.behave.status_top,               1,   set_status_top)},
    { "status_format",          PTR_V_STR(uzbl.behave.status_format,            1,   NULL)},
    { "status_format_right",    PTR_V_STR(uzbl.behave.status_format_right,      1,   NULL)},
    { "status_background",      PTR_V_STR(uzbl.behave.status_background,        1,   set_status_background)},
    { "title_format_long",      PTR_V_STR(uzbl.behave.title_format_long,        1,   NULL)},
    { "title_format_short",     PTR_V_STR(uzbl.behave.title_format_short,       1,   NULL)},

    { "geometry",               PTR_V_STR_GETSET(geometry)},
    { "icon",                   PTR_V_STR(uzbl.gui.icon,                        1,   set_icon)},
    { "window_role",            PTR_V_STR_GETSET(window_role)},

    { "forward_keys",           PTR_V_INT(uzbl.behave.forward_keys,             1,   NULL)},

    { "authentication_handler", PTR_V_STR(uzbl.behave.authentication_handler,   1,   set_authentication_handler)},
    { "scheme_handler",         PTR_V_STR(uzbl.behave.scheme_handler,           1,   NULL)},
    { "request_handler",        PTR_V_STR(uzbl.behave.request_handler,          1,   NULL)},
    { "download_handler",       PTR_V_STR(uzbl.behave.download_handler,         1,   NULL)},

    { "fifo_dir",               PTR_V_STR(uzbl.behave.fifo_dir,                 1,   set_fifo_dir)},
    { "socket_dir",             PTR_V_STR(uzbl.behave.socket_dir,               1,   set_socket_dir)},

    { "shell_cmd",              PTR_V_STR(uzbl.behave.shell_cmd,                1,   NULL)},

    { "http_debug",             PTR_V_INT(uzbl.behave.http_debug,               1,   set_http_debug)},
    { "proxy_url",              PTR_V_STR(uzbl.net.proxy_url,                   1,   set_proxy_url)},
    { "max_conns",              PTR_V_INT(uzbl.net.max_conns,                   1,   set_max_conns)},
    { "max_conns_host",         PTR_V_INT(uzbl.net.max_conns_host,              1,   set_max_conns_host)},
    { "useragent",              PTR_V_STR(uzbl.net.useragent,                   1,   set_useragent)},
    { "accept_languages",       PTR_V_STR(uzbl.net.accept_languages,            1,   set_accept_languages)},

    { "view_source",            PTR_V_INT(uzbl.behave.view_source,              0,   set_view_source)},

    { "ssl_ca_file",            PTR_V_STR_GETSET(ca_file)},
    { "ssl_verify",             PTR_V_INT_GETSET(verify_cert)},

    /* exported WebKitWebSettings properties */
    { "javascript_windows",     PTR_V_INT_GETSET(javascript_windows)},
    { "zoom_level",             PTR_V_FLOAT_GETSET(zoom_level)},
    { "zoom_type",              PTR_V_INT_GETSET(zoom_type)},

    { "default_font_family",    PTR_V_STR_GETSET(default_font_family)},
    { "monospace_font_family",  PTR_V_STR_GETSET(monospace_font_family)},
    { "cursive_font_family",    PTR_V_STR_GETSET(cursive_font_family)},
    { "sans_serif_font_family", PTR_V_STR_GETSET(sans_serif_font_family)},
    { "serif_font_family",      PTR_V_STR_GETSET(serif_font_family)},
    { "fantasy_font_family",    PTR_V_STR_GETSET(fantasy_font_family)},

    { "monospace_size",         PTR_V_INT_GETSET(monospace_size)},
    { "font_size",              PTR_V_INT_GETSET(font_size)},
    { "minimum_font_size",      PTR_V_INT_GETSET(minimum_font_size)},

    { "enable_pagecache",       PTR_V_INT_GETSET(enable_pagecache)},
    { "enable_plugins",         PTR_V_INT_GETSET(enable_plugins)},
    { "enable_scripts",         PTR_V_INT_GETSET(enable_scripts)},
    { "autoload_images",        PTR_V_INT_GETSET(autoload_images)},
    { "autoshrink_images",      PTR_V_INT_GETSET(autoshrink_images)},
    { "enable_spellcheck",      PTR_V_INT_GETSET(enable_spellcheck)},
    { "spellcheck_languages",   PTR_V_STR_GETSET(spellcheck_languages)},
    { "enable_private",         PTR_V_INT_GETSET(enable_private)},
    { "print_backgrounds",      PTR_V_INT_GETSET(print_bg)},
    { "stylesheet_uri",         PTR_V_STR_GETSET(stylesheet_uri)},
    { "resizable_text_areas",   PTR_V_INT_GETSET(resizable_text_areas)},
    { "default_encoding",       PTR_V_STR_GETSET(default_encoding)},
    { "current_encoding",       PTR_V_STR_GETSET(current_encoding)},
    { "enforce_96_dpi",         PTR_V_INT_GETSET(enforce_96_dpi)},
    { "caret_browsing",         PTR_V_INT_GETSET(caret_browsing)},
    { "enable_cross_file_access", PTR_V_INT_GETSET(enable_cross_file_access)},

    { "inject_html",            { .type = TYPE_STR, .dump = 0, .writeable = 1, .getter = NULL, .setter = (uzbl_fp) set_inject_html }},

    /* constants (not dumpable or writeable) */
    { "WEBKIT_MAJOR",           PTR_C_INT(uzbl.info.webkit_major)},
    { "WEBKIT_MINOR",           PTR_C_INT(uzbl.info.webkit_minor)},
    { "WEBKIT_MICRO",           PTR_C_INT(uzbl.info.webkit_micro)},
    { "ARCH_UZBL",              PTR_C_STR(uzbl.info.arch)},
    { "COMMIT",                 PTR_C_STR(uzbl.info.commit)},
    { "TITLE",                  PTR_C_STR(uzbl.gui.main_title)},
    { "SELECTED_URI",           PTR_C_STR(uzbl.state.selected_url)},
    { "NAME",                   PTR_C_STR(uzbl.state.instance_name)},
    { "PID",                    PTR_C_STR(uzbl.info.pid_str)},
    { "_",                      PTR_C_STR(uzbl.state.last_result)},

    /* and we terminate the whole thing with the closest thing we have to NULL.
     * it's important that dump = 0. */
    { NULL,                     {.ptr = { .i = NULL }, .type = TYPE_INT, .dump = 0, .writeable = 0}}
};

/* construct a hash from the var_name_to_ptr array for quick access */
void
variables_hash() {
    const struct var_name_to_ptr_t *n2v_p = var_name_to_ptr;
    uzbl.behave.proto_var = g_hash_table_new(g_str_hash, g_str_equal);
    while(n2v_p->name) {
        g_hash_table_insert(uzbl.behave.proto_var,
                (gpointer) n2v_p->name,
                (gpointer) &n2v_p->cp);
        n2v_p++;
    }
}