-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuzbl.c
2805 lines (2408 loc) · 93.1 KB
/
uzbl.c
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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- c-basic-offset: 4; -*- */
// Original code taken from the example webkit-gtk+ application. see notice below.
// Modified code is licensed under the GPL 3. See LICENSE file.
/*
* Copyright (C) 2006, 2007 Apple Inc.
* Copyright (C) 2007 Alp Toker <alp@atoker.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define LENGTH(x) (sizeof x / sizeof x[0])
#define MAX_BINDINGS 256
#define _POSIX_SOURCE
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <gdk/gdkkeysyms.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/utsname.h>
#include <sys/time.h>
#include <webkit/webkit.h>
#include <libsoup/soup.h>
#include <JavaScriptCore/JavaScript.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include "uzbl.h"
#include "config.h"
static Uzbl uzbl;
/* commandline arguments (set initial values for the state variables) */
static const
GOptionEntry entries[] =
{
{ "uri", 'u', 0, G_OPTION_ARG_STRING, &uzbl.state.uri,
"Uri to load at startup (equivalent to 'set uri = URI')", "URI" },
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &uzbl.state.verbose,
"Whether to print all messages or just errors.", NULL },
{ "name", 'n', 0, G_OPTION_ARG_STRING, &uzbl.state.instance_name,
"Name of the current instance (defaults to Xorg window id)", "NAME" },
{ "config", 'c', 0, G_OPTION_ARG_STRING, &uzbl.state.config_file,
"Config file (this is pretty much equivalent to uzbl < FILE )", "FILE" },
{ "socket", 's', 0, G_OPTION_ARG_INT, &uzbl.state.socket_id,
"Socket ID", "SOCKET" },
{ NULL, 0, 0, 0, NULL, NULL, NULL }
};
/* associate command names to their properties */
typedef const struct {
void **ptr;
int type;
int dump;
void (*func)(void);
} uzbl_cmdprop;
enum {TYPE_INT, TYPE_STR, TYPE_FLOAT};
/* an abbreviation to help keep the table's width humane */
#define PTR(var, t, d, fun) { .ptr = (void*)&(var), .type = TYPE_##t, .dump = d, .func = fun }
const struct {
char *name;
uzbl_cmdprop cp;
} var_name_to_ptr[] = {
/* variable name pointer to variable in code type dump callback function */
/* --------------------------------------------------------------------------------------- */
{ "uri", PTR(uzbl.state.uri, STR, 1, cmd_load_uri)},
{ "verbose", PTR(uzbl.state.verbose, INT, 1, NULL)},
{ "mode", PTR(uzbl.behave.mode, INT, 0, NULL)},
{ "inject_html", PTR(uzbl.behave.inject_html, STR, 0, cmd_inject_html)},
{ "base_url", PTR(uzbl.behave.base_url, STR, 1, NULL)},
{ "html_endmarker", PTR(uzbl.behave.html_endmarker, STR, 1, NULL)},
{ "html_mode_timeout", PTR(uzbl.behave.html_timeout, INT, 1, NULL)},
{ "status_message", PTR(uzbl.gui.sbar.msg, STR, 1, update_title)},
{ "show_status", PTR(uzbl.behave.show_status, INT, 1, cmd_set_status)},
{ "status_top", PTR(uzbl.behave.status_top, INT, 1, move_statusbar)},
{ "status_format", PTR(uzbl.behave.status_format, STR, 1, update_title)},
{ "status_pbar_done", PTR(uzbl.gui.sbar.progress_s, STR, 1, update_title)},
{ "status_pbar_pending", PTR(uzbl.gui.sbar.progress_u, STR, 1, update_title)},
{ "status_pbar_width", PTR(uzbl.gui.sbar.progress_w, INT, 1, update_title)},
{ "status_background", PTR(uzbl.behave.status_background, STR, 1, update_title)},
{ "insert_indicator", PTR(uzbl.behave.insert_indicator, STR, 1, update_title)},
{ "command_indicator", PTR(uzbl.behave.cmd_indicator, STR, 1, update_title)},
{ "title_format_long", PTR(uzbl.behave.title_format_long, STR, 1, update_title)},
{ "title_format_short", PTR(uzbl.behave.title_format_short, STR, 1, update_title)},
{ "icon", PTR(uzbl.gui.icon, STR, 1, set_icon)},
{ "insert_mode", PTR(uzbl.behave.insert_mode, INT, 1, NULL)},
{ "always_insert_mode", PTR(uzbl.behave.always_insert_mode, INT, 1, cmd_always_insert_mode)},
{ "reset_command_mode", PTR(uzbl.behave.reset_command_mode, INT, 1, NULL)},
{ "modkey", PTR(uzbl.behave.modkey, STR, 1, cmd_modkey)},
{ "load_finish_handler", PTR(uzbl.behave.load_finish_handler, STR, 1, NULL)},
{ "load_start_handler", PTR(uzbl.behave.load_start_handler, STR, 1, NULL)},
{ "load_commit_handler", PTR(uzbl.behave.load_commit_handler, STR, 1, NULL)},
{ "history_handler", PTR(uzbl.behave.history_handler, STR, 1, NULL)},
{ "download_handler", PTR(uzbl.behave.download_handler, STR, 1, NULL)},
{ "cookie_handler", PTR(uzbl.behave.cookie_handler, STR, 1, cmd_cookie_handler)},
{ "fifo_dir", PTR(uzbl.behave.fifo_dir, STR, 1, cmd_fifo_dir)},
{ "socket_dir", PTR(uzbl.behave.socket_dir, STR, 1, cmd_socket_dir)},
{ "http_debug", PTR(uzbl.behave.http_debug, INT, 1, cmd_http_debug)},
{ "shell_cmd", PTR(uzbl.behave.shell_cmd, STR, 1, NULL)},
{ "proxy_url", PTR(uzbl.net.proxy_url, STR, 1, set_proxy_url)},
{ "max_conns", PTR(uzbl.net.max_conns, INT, 1, cmd_max_conns)},
{ "max_conns_host", PTR(uzbl.net.max_conns_host, INT, 1, cmd_max_conns_host)},
{ "useragent", PTR(uzbl.net.useragent, STR, 1, cmd_useragent)},
/* exported WebKitWebSettings properties */
{ "zoom_level", PTR(uzbl.behave.zoom_level, FLOAT,1, cmd_zoom_level)},
{ "font_size", PTR(uzbl.behave.font_size, INT, 1, cmd_font_size)},
{ "monospace_size", PTR(uzbl.behave.monospace_size, INT, 1, cmd_font_size)},
{ "minimum_font_size", PTR(uzbl.behave.minimum_font_size, INT, 1, cmd_minimum_font_size)},
{ "disable_plugins", PTR(uzbl.behave.disable_plugins, INT, 1, cmd_disable_plugins)},
{ "disable_scripts", PTR(uzbl.behave.disable_scripts, INT, 1, cmd_disable_scripts)},
{ "autoload_images", PTR(uzbl.behave.autoload_img, INT, 1, cmd_autoload_img)},
{ "autoshrink_images", PTR(uzbl.behave.autoshrink_img, INT, 1, cmd_autoshrink_img)},
{ "enable_spellcheck", PTR(uzbl.behave.enable_spellcheck, INT, 1, cmd_enable_spellcheck)},
{ "enable_private", PTR(uzbl.behave.enable_private, INT, 1, cmd_enable_private)},
{ "print_backgrounds", PTR(uzbl.behave.print_bg, INT, 1, cmd_print_bg)},
{ "stylesheet_uri", PTR(uzbl.behave.style_uri, STR, 1, cmd_style_uri)},
{ "resizable_text_areas",PTR(uzbl.behave.resizable_txt, INT, 1, cmd_resizable_txt)},
{ "default_encoding", PTR(uzbl.behave.default_encoding, STR, 1, cmd_default_encoding)},
{ "enforce_96_dpi", PTR(uzbl.behave.enforce_96dpi, INT, 1, cmd_enforce_96dpi)},
{ "caret_browsing", PTR(uzbl.behave.caret_browsing, INT, 1, cmd_caret_browsing)},
{ NULL, {.ptr = NULL, .type = TYPE_INT, .dump = 0, .func = NULL}}
}, *n2v_p = var_name_to_ptr;
const struct {
char *key;
guint mask;
} modkeys[] = {
{ "SHIFT", GDK_SHIFT_MASK }, // shift
{ "LOCK", GDK_LOCK_MASK }, // capslock or shiftlock, depending on xserver's modmappings
{ "CONTROL", GDK_CONTROL_MASK }, // control
{ "MOD1", GDK_MOD1_MASK }, // 4th mod - normally alt but depends on modmappings
{ "MOD2", GDK_MOD2_MASK }, // 5th mod
{ "MOD3", GDK_MOD3_MASK }, // 6th mod
{ "MOD4", GDK_MOD4_MASK }, // 7th mod
{ "MOD5", GDK_MOD5_MASK }, // 8th mod
{ "BUTTON1", GDK_BUTTON1_MASK }, // 1st mouse button
{ "BUTTON2", GDK_BUTTON2_MASK }, // 2nd mouse button
{ "BUTTON3", GDK_BUTTON3_MASK }, // 3rd mouse button
{ "BUTTON4", GDK_BUTTON4_MASK }, // 4th mouse button
{ "BUTTON5", GDK_BUTTON5_MASK }, // 5th mouse button
{ "SUPER", GDK_SUPER_MASK }, // super (since 2.10)
{ "HYPER", GDK_HYPER_MASK }, // hyper (since 2.10)
{ "META", GDK_META_MASK }, // meta (since 2.10)
{ NULL, 0 }
};
/* construct a hash from the var_name_to_ptr array for quick access */
static void
make_var_to_name_hash() {
uzbl.comm.proto_var = g_hash_table_new(g_str_hash, g_str_equal);
while(n2v_p->name) {
g_hash_table_insert(uzbl.comm.proto_var, n2v_p->name, (gpointer) &n2v_p->cp);
n2v_p++;
}
}
/* --- UTILITY FUNCTIONS --- */
enum {EXP_ERR, EXP_SIMPLE_VAR, EXP_BRACED_VAR, EXP_EXPR, EXP_JS};
static guint
get_exp_type(gchar *s) {
/* variables */
if(*(s+1) == '(')
return EXP_EXPR;
else if(*(s+1) == '{')
return EXP_BRACED_VAR;
else if(*(s+1) == '<')
return EXP_JS;
else
return EXP_SIMPLE_VAR;
return EXP_ERR;
}
/*
* recurse == 1: don't expand '@(command)@'
* recurse == 2: don't expand '@<java script>@'
*/
static gchar *
expand(char *s, guint recurse) {
uzbl_cmdprop *c;
guint etype;
char upto = ' ';
char *end_simple_var = "^°!\"§$%&/()=?'`'+~*'#-.:,;@<>| \\{}[]¹²³¼½";
char str_end[2];
char ret[4096];
char *vend;
GError *err = NULL;
gchar *cmd_stdout = NULL;
gchar *mycmd = NULL;
GString *buf = g_string_new("");
GString *js_ret = g_string_new("");
while(*s) {
switch(*s) {
case '\\':
g_string_append_c(buf, *++s);
s++;
break;
case '@':
etype = get_exp_type(s);
s++;
switch(etype) {
case EXP_SIMPLE_VAR:
if( (vend = strpbrk(s, end_simple_var)) ||
(vend = strchr(s, '\0')) ) {
strncpy(ret, s, vend-s);
ret[vend-s] = '\0';
}
break;
case EXP_BRACED_VAR:
s++; upto = '}';
if( (vend = strchr(s, upto)) ||
(vend = strchr(s, '\0')) ) {
strncpy(ret, s, vend-s);
ret[vend-s] = '\0';
}
break;
case EXP_EXPR:
s++;
strcpy(str_end, ")@");
str_end[2] = '\0';
if( (vend = strstr(s, str_end)) ||
(vend = strchr(s, '\0')) ) {
strncpy(ret, s, vend-s);
ret[vend-s] = '\0';
}
break;
case EXP_JS:
s++;
strcpy(str_end, ">@");
str_end[2] = '\0';
if( (vend = strstr(s, str_end)) ||
(vend = strchr(s, '\0')) ) {
strncpy(ret, s, vend-s);
ret[vend-s] = '\0';
}
break;
}
if(etype == EXP_SIMPLE_VAR ||
etype == EXP_BRACED_VAR) {
if( (c = g_hash_table_lookup(uzbl.comm.proto_var, ret)) ) {
if(c->type == TYPE_STR)
g_string_append(buf, (gchar *)*c->ptr);
else if(c->type == TYPE_INT) {
char *b = itos((int)*c->ptr);
g_string_append(buf, b);
g_free(b);
}
}
if(etype == EXP_SIMPLE_VAR)
s = vend;
else
s = vend+1;
}
else if(recurse != 1 &&
etype == EXP_EXPR) {
mycmd = expand(ret, 1);
g_spawn_command_line_sync(mycmd, &cmd_stdout, NULL, NULL, &err);
g_free(mycmd);
if (err) {
g_printerr("error on running command: %s\n", err->message);
g_error_free (err);
}
else if (*cmd_stdout) {
g_string_append(buf, cmd_stdout);
g_free(cmd_stdout);
}
s = vend+2;
}
else if(recurse != 2 &&
etype == EXP_JS) {
mycmd = expand(ret, 2);
eval_js(uzbl.gui.web_view, mycmd, js_ret);
g_free(mycmd);
if(js_ret->str) {
g_string_append(buf, js_ret->str);
g_string_free(js_ret, TRUE);
js_ret = g_string_new("");
}
s = vend+2;
}
break;
default:
g_string_append_c(buf, *s);
s++;
break;
}
}
g_string_free(js_ret, TRUE);
return g_string_free(buf, FALSE);
}
char *
itos(int val) {
char tmp[20];
snprintf(tmp, sizeof(tmp), "%i", val);
return g_strdup(tmp);
}
static gchar*
strfree(gchar *str) { g_free(str); return NULL; } // for freeing & setting to null in one go
static gchar*
argv_idx(const GArray *a, const guint idx) { return g_array_index(a, gchar*, idx); }
static char *
str_replace (const char* search, const char* replace, const char* string) {
gchar **buf;
char *ret;
buf = g_strsplit (string, search, -1);
ret = g_strjoinv (replace, buf);
g_strfreev(buf); // somebody said this segfaults
return ret;
}
static GArray*
read_file_by_line (gchar *path) {
GIOChannel *chan = NULL;
gchar *readbuf = NULL;
gsize len;
GArray *lines = g_array_new(TRUE, FALSE, sizeof(gchar*));
int i = 0;
chan = g_io_channel_new_file(path, "r", NULL);
if (chan) {
while (g_io_channel_read_line(chan, &readbuf, &len, NULL, NULL) == G_IO_STATUS_NORMAL) {
const gchar* val = g_strdup (readbuf);
g_array_append_val (lines, val);
g_free (readbuf);
i ++;
}
g_io_channel_unref (chan);
} else {
fprintf(stderr, "File '%s' not be read.\n", path);
}
return lines;
}
static
gchar* parseenv (char* string) {
extern char** environ;
gchar* tmpstr = NULL;
int i = 0;
while (environ[i] != NULL) {
gchar** env = g_strsplit (environ[i], "=", 2);
gchar* envname = g_strconcat ("$", env[0], NULL);
if (g_strrstr (string, envname) != NULL) {
tmpstr = g_strdup(string);
g_free (string);
string = str_replace(envname, env[1], tmpstr);
g_free (tmpstr);
}
g_free (envname);
g_strfreev (env); // somebody said this breaks uzbl
i++;
}
return string;
}
static sigfunc*
setup_signal(int signr, sigfunc *shandler) {
struct sigaction nh, oh;
nh.sa_handler = shandler;
sigemptyset(&nh.sa_mask);
nh.sa_flags = 0;
if(sigaction(signr, &nh, &oh) < 0)
return SIG_ERR;
return NULL;
}
static void
clean_up(void) {
if (uzbl.behave.fifo_dir)
unlink (uzbl.comm.fifo_path);
if (uzbl.behave.socket_dir)
unlink (uzbl.comm.socket_path);
g_free(uzbl.state.executable_path);
g_string_free(uzbl.state.keycmd, TRUE);
g_hash_table_destroy(uzbl.bindings);
g_hash_table_destroy(uzbl.behave.commands);
}
/* used for html_mode_timeout
* be sure to extend this function to use
* more timers if needed in other places
*/
static void
set_timeout(int seconds) {
struct itimerval t;
memset(&t, 0, sizeof t);
t.it_value.tv_sec = seconds;
t.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &t, NULL);
}
/* --- SIGNAL HANDLER --- */
static void
catch_sigterm(int s) {
(void) s;
clean_up();
}
static void
catch_sigint(int s) {
(void) s;
clean_up();
exit(EXIT_SUCCESS);
}
static void
catch_alrm(int s) {
(void) s;
set_var_value("mode", "0");
render_html();
}
/* --- CALLBACKS --- */
static gboolean
new_window_cb (WebKitWebView *web_view, WebKitWebFrame *frame, WebKitNetworkRequest *request, WebKitWebNavigationAction *navigation_action, WebKitWebPolicyDecision *policy_decision, gpointer user_data) {
(void) web_view;
(void) frame;
(void) navigation_action;
(void) policy_decision;
(void) user_data;
const gchar* uri = webkit_network_request_get_uri (request);
if (uzbl.state.verbose)
printf("New window requested -> %s \n", uri);
new_window_load_uri(uri);
return (FALSE);
}
static gboolean
mime_policy_cb(WebKitWebView *web_view, WebKitWebFrame *frame, WebKitNetworkRequest *request, gchar *mime_type, WebKitWebPolicyDecision *policy_decision, gpointer user_data) {
(void) frame;
(void) request;
(void) user_data;
/* If we can display it, let's display it... */
if (webkit_web_view_can_show_mime_type (web_view, mime_type)) {
webkit_web_policy_decision_use (policy_decision);
return TRUE;
}
/* ...everything we can't displayed is downloaded */
webkit_web_policy_decision_download (policy_decision);
return TRUE;
}
WebKitWebView*
create_web_view_cb (WebKitWebView *web_view, WebKitWebFrame *frame, gpointer user_data) {
(void) web_view;
(void) frame;
(void) user_data;
if (uzbl.state.selected_url != NULL) {
if (uzbl.state.verbose)
printf("\nNew web view -> %s\n",uzbl.state.selected_url);
new_window_load_uri(uzbl.state.selected_url);
} else {
if (uzbl.state.verbose)
printf("New web view -> %s\n","Nothing to open, exiting");
}
return (NULL);
}
static gboolean
download_cb (WebKitWebView *web_view, GObject *download, gpointer user_data) {
(void) web_view;
(void) user_data;
if (uzbl.behave.download_handler) {
const gchar* uri = webkit_download_get_uri ((WebKitDownload*)download);
if (uzbl.state.verbose)
printf("Download -> %s\n",uri);
/* if urls not escaped, we may have to escape and quote uri before this call */
run_handler(uzbl.behave.download_handler, uri);
}
return (FALSE);
}
/* scroll a bar in a given direction */
static void
scroll (GtkAdjustment* bar, GArray *argv) {
gchar *end;
gdouble max_value;
gdouble page_size = gtk_adjustment_get_page_size(bar);
gdouble value = gtk_adjustment_get_value(bar);
gdouble amount = g_ascii_strtod(g_array_index(argv, gchar*, 0), &end);
if (*end == '%')
value += page_size * amount * 0.01;
else
value += amount;
max_value = gtk_adjustment_get_upper(bar) - page_size;
if (value > max_value)
value = max_value; /* don't scroll past the end of the page */
gtk_adjustment_set_value (bar, value);
}
static void
scroll_begin(WebKitWebView* page, GArray *argv, GString *result) {
(void) page; (void) argv; (void) result;
gtk_adjustment_set_value (uzbl.gui.bar_v, gtk_adjustment_get_lower(uzbl.gui.bar_v));
}
static void
scroll_end(WebKitWebView* page, GArray *argv, GString *result) {
(void) page; (void) argv; (void) result;
gtk_adjustment_set_value (uzbl.gui.bar_v, gtk_adjustment_get_upper(uzbl.gui.bar_v) -
gtk_adjustment_get_page_size(uzbl.gui.bar_v));
}
static void
scroll_vert(WebKitWebView* page, GArray *argv, GString *result) {
(void) page; (void) result;
scroll(uzbl.gui.bar_v, argv);
}
static void
scroll_horz(WebKitWebView* page, GArray *argv, GString *result) {
(void) page; (void) result;
scroll(uzbl.gui.bar_h, argv);
}
static void
cmd_set_status() {
if (!uzbl.behave.show_status) {
gtk_widget_hide(uzbl.gui.mainbar);
} else {
gtk_widget_show(uzbl.gui.mainbar);
}
update_title();
}
static void
toggle_zoom_type (WebKitWebView* page, GArray *argv, GString *result) {
(void)page;
(void)argv;
(void)result;
webkit_web_view_set_full_content_zoom (page, !webkit_web_view_get_full_content_zoom (page));
}
static void
toggle_status_cb (WebKitWebView* page, GArray *argv, GString *result) {
(void)page;
(void)argv;
(void)result;
if (uzbl.behave.show_status) {
gtk_widget_hide(uzbl.gui.mainbar);
} else {
gtk_widget_show(uzbl.gui.mainbar);
}
uzbl.behave.show_status = !uzbl.behave.show_status;
update_title();
}
static void
link_hover_cb (WebKitWebView* page, const gchar* title, const gchar* link, gpointer data) {
(void) page;
(void) title;
(void) data;
//Set selected_url state variable
g_free(uzbl.state.selected_url);
uzbl.state.selected_url = NULL;
if (link) {
uzbl.state.selected_url = g_strdup(link);
}
update_title();
}
static void
title_change_cb (WebKitWebView* web_view, GParamSpec param_spec) {
(void) web_view;
(void) param_spec;
const gchar *title = webkit_web_view_get_title(web_view);
if (uzbl.gui.main_title)
g_free (uzbl.gui.main_title);
uzbl.gui.main_title = title ? g_strdup (title) : g_strdup ("(no title)");
update_title();
}
static void
progress_change_cb (WebKitWebView* page, gint progress, gpointer data) {
(void) page;
(void) data;
uzbl.gui.sbar.load_progress = progress;
update_title();
}
static void
load_finish_cb (WebKitWebView* page, WebKitWebFrame* frame, gpointer data) {
(void) page;
(void) frame;
(void) data;
if (uzbl.behave.load_finish_handler)
run_handler(uzbl.behave.load_finish_handler, "");
}
static void
load_start_cb (WebKitWebView* page, WebKitWebFrame* frame, gpointer data) {
(void) page;
(void) frame;
(void) data;
uzbl.gui.sbar.load_progress = 0;
g_string_truncate(uzbl.state.keycmd, 0); // don't need old commands to remain on new page?
if (uzbl.behave.load_start_handler)
run_handler(uzbl.behave.load_start_handler, "");
}
static void
load_commit_cb (WebKitWebView* page, WebKitWebFrame* frame, gpointer data) {
(void) page;
(void) data;
g_free (uzbl.state.uri);
GString* newuri = g_string_new (webkit_web_frame_get_uri (frame));
uzbl.state.uri = g_string_free (newuri, FALSE);
if (uzbl.behave.reset_command_mode && uzbl.behave.insert_mode) {
uzbl.behave.insert_mode = uzbl.behave.always_insert_mode;
update_title();
}
if (uzbl.behave.load_commit_handler)
run_handler(uzbl.behave.load_commit_handler, uzbl.state.uri);
}
static void
destroy_cb (GtkWidget* widget, gpointer data) {
(void) widget;
(void) data;
gtk_main_quit ();
}
static void
log_history_cb () {
if (uzbl.behave.history_handler) {
time_t rawtime;
struct tm * timeinfo;
char date [80];
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strftime (date, 80, "\"%Y-%m-%d %H:%M:%S\"", timeinfo);
run_handler(uzbl.behave.history_handler, date);
}
}
/* VIEW funcs (little webkit wrappers) */
#define VIEWFUNC(name) static void view_##name(WebKitWebView *page, GArray *argv, GString *result){(void)argv; (void)result; webkit_web_view_##name(page);}
VIEWFUNC(reload)
VIEWFUNC(reload_bypass_cache)
VIEWFUNC(stop_loading)
VIEWFUNC(zoom_in)
VIEWFUNC(zoom_out)
VIEWFUNC(go_back)
VIEWFUNC(go_forward)
#undef VIEWFUNC
/* -- command to callback/function map for things we cannot attach to any signals */
static struct {char *key; CommandInfo value;} cmdlist[] =
{ /* key function no_split */
{ "back", {view_go_back, 0} },
{ "forward", {view_go_forward, 0} },
{ "scroll_vert", {scroll_vert, 0} },
{ "scroll_horz", {scroll_horz, 0} },
{ "scroll_begin", {scroll_begin, 0} },
{ "scroll_end", {scroll_end, 0} },
{ "reload", {view_reload, 0}, },
{ "reload_ign_cache", {view_reload_bypass_cache, 0} },
{ "stop", {view_stop_loading, 0}, },
{ "zoom_in", {view_zoom_in, 0}, }, //Can crash (when max zoom reached?).
{ "zoom_out", {view_zoom_out, 0}, },
{ "toggle_zoom_type", {toggle_zoom_type, 0}, },
{ "uri", {load_uri, TRUE} },
{ "js", {run_js, TRUE} },
{ "script", {run_external_js, 0} },
{ "toggle_status", {toggle_status_cb, 0} },
{ "spawn", {spawn, 0} },
{ "sync_spawn", {spawn_sync, 0} }, // needed for cookie handler
{ "sh", {spawn_sh, 0} },
{ "sync_sh", {spawn_sh_sync, 0} }, // needed for cookie handler
{ "exit", {close_uzbl, 0} },
{ "search", {search_forward_text, TRUE} },
{ "search_reverse", {search_reverse_text, TRUE} },
{ "dehilight", {dehilight, 0} },
{ "toggle_insert_mode", {toggle_insert_mode, 0} },
{ "set", {set_var, TRUE} },
//{ "get", {get_var, TRUE} },
{ "bind", {act_bind, TRUE} },
{ "dump_config", {act_dump_config, 0} },
{ "keycmd", {keycmd, TRUE} },
{ "keycmd_nl", {keycmd_nl, TRUE} },
{ "keycmd_bs", {keycmd_bs, 0} },
{ "chain", {chain, 0} },
{ "print", {print, TRUE} }
};
static void
commands_hash(void)
{
unsigned int i;
uzbl.behave.commands = g_hash_table_new(g_str_hash, g_str_equal);
for (i = 0; i < LENGTH(cmdlist); i++)
g_hash_table_insert(uzbl.behave.commands, cmdlist[i].key, &cmdlist[i].value);
}
/* -- CORE FUNCTIONS -- */
void
free_action(gpointer act) {
Action *action = (Action*)act;
g_free(action->name);
if (action->param)
g_free(action->param);
g_free(action);
}
Action*
new_action(const gchar *name, const gchar *param) {
Action *action = g_new(Action, 1);
action->name = g_strdup(name);
if (param)
action->param = g_strdup(param);
else
action->param = NULL;
return action;
}
static bool
file_exists (const char * filename) {
return (access(filename, F_OK) == 0);
}
static void
set_var(WebKitWebView *page, GArray *argv, GString *result) {
(void) page; (void) result;
gchar **split = g_strsplit(argv_idx(argv, 0), "=", 2);
if (split[0] != NULL) {
gchar *value = parseenv(g_strdup(split[1] ? g_strchug(split[1]) : " "));
set_var_value(g_strstrip(split[0]), value);
g_free(value);
}
g_strfreev(split);
}
static void
print(WebKitWebView *page, GArray *argv, GString *result) {
(void) page; (void) result;
gchar* buf;
buf = expand(argv_idx(argv, 0), 0);
g_string_assign(result, buf);
g_free(buf);
}
static void
act_bind(WebKitWebView *page, GArray *argv, GString *result) {
(void) page; (void) result;
gchar **split = g_strsplit(argv_idx(argv, 0), " = ", 2);
gchar *value = parseenv(g_strdup(split[1] ? g_strchug(split[1]) : " "));
add_binding(g_strstrip(split[0]), value);
g_free(value);
g_strfreev(split);
}
static void
act_dump_config() {
dump_config();
}
static void
toggle_insert_mode(WebKitWebView *page, GArray *argv, GString *result) {
(void) page; (void) result;
if (argv_idx(argv, 0)) {
if (strcmp (argv_idx(argv, 0), "0") == 0) {
uzbl.behave.insert_mode = FALSE;
} else {
uzbl.behave.insert_mode = TRUE;
}
} else {
uzbl.behave.insert_mode = ! uzbl.behave.insert_mode;
}
update_title();
}
static void
load_uri (WebKitWebView *web_view, GArray *argv, GString *result) {
(void) result;
if (argv_idx(argv, 0)) {
GString* newuri = g_string_new (argv_idx(argv, 0));
if (g_strstr_len (argv_idx(argv, 0), 11, "javascript:") != NULL) {
run_js(web_view, argv, NULL);
return;
}
if (g_strrstr (argv_idx(argv, 0), "://") == NULL && g_strstr_len (argv_idx(argv, 0), 5, "data:") == NULL)
g_string_prepend (newuri, "http://");
/* if we do handle cookies, ask our handler for them */
webkit_web_view_load_uri (web_view, newuri->str);
g_string_free (newuri, TRUE);
}
}
/* Javascript*/
static JSValueRef
js_run_command (JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
size_t argumentCount, const JSValueRef arguments[],
JSValueRef* exception) {
(void) function;
(void) thisObject;
(void) exception;
JSStringRef js_result_string;
GString *result = g_string_new("");
if (argumentCount >= 1) {
JSStringRef arg = JSValueToStringCopy(ctx, arguments[0], NULL);
size_t arg_size = JSStringGetMaximumUTF8CStringSize(arg);
char ctl_line[arg_size];
JSStringGetUTF8CString(arg, ctl_line, arg_size);
parse_cmd_line(ctl_line, result);
JSStringRelease(arg);
}
js_result_string = JSStringCreateWithUTF8CString(result->str);
g_string_free(result, TRUE);
return JSValueMakeString(ctx, js_result_string);
}
static JSStaticFunction js_static_functions[] = {
{"run", js_run_command, kJSPropertyAttributeNone},
};
static void
js_init() {
/* This function creates the class and its definition, only once */
if (!uzbl.js.initialized) {
/* it would be pretty cool to make this dynamic */
uzbl.js.classdef = kJSClassDefinitionEmpty;
uzbl.js.classdef.staticFunctions = js_static_functions;
uzbl.js.classref = JSClassCreate(&uzbl.js.classdef);
}
}
static void
eval_js(WebKitWebView * web_view, gchar *script, GString *result) {
WebKitWebFrame *frame;
JSGlobalContextRef context;
JSObjectRef globalobject;
JSStringRef var_name;
JSStringRef js_script;
JSValueRef js_result;
JSStringRef js_result_string;
size_t js_result_size;
js_init();
frame = webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(web_view));
context = webkit_web_frame_get_global_context(frame);
globalobject = JSContextGetGlobalObject(context);
/* uzbl javascript namespace */
var_name = JSStringCreateWithUTF8CString("Uzbl");
JSObjectSetProperty(context, globalobject, var_name,
JSObjectMake(context, uzbl.js.classref, NULL),
kJSClassAttributeNone, NULL);
/* evaluate the script and get return value*/
js_script = JSStringCreateWithUTF8CString(script);
js_result = JSEvaluateScript(context, js_script, globalobject, NULL, 0, NULL);
if (js_result && !JSValueIsUndefined(context, js_result)) {
js_result_string = JSValueToStringCopy(context, js_result, NULL);
js_result_size = JSStringGetMaximumUTF8CStringSize(js_result_string);
if (js_result_size) {
char js_result_utf8[js_result_size];
JSStringGetUTF8CString(js_result_string, js_result_utf8, js_result_size);
g_string_assign(result, js_result_utf8);
}
JSStringRelease(js_result_string);
}
/* cleanup */
JSObjectDeleteProperty(context, globalobject, var_name, NULL);
JSStringRelease(var_name);
JSStringRelease(js_script);
}
static void
run_js (WebKitWebView * web_view, GArray *argv, GString *result) {
if (argv_idx(argv, 0))
eval_js(web_view, argv_idx(argv, 0), result);
}
static void
run_external_js (WebKitWebView * web_view, GArray *argv, GString *result) {
(void) result;
if (argv_idx(argv, 0)) {
GArray* lines = read_file_by_line (argv_idx (argv, 0));
gchar* js = NULL;
int i = 0;
gchar* line;
while ((line = g_array_index(lines, gchar*, i))) {
if (js == NULL) {
js = g_strdup (line);
} else {
gchar* newjs = g_strconcat (js, line, NULL);
js = newjs;
}
i ++;
g_free (line);
}
if (uzbl.state.verbose)
printf ("External JavaScript file %s loaded\n", argv_idx(argv, 0));
if (argv_idx (argv, 1)) {