+ GtkAction *a = NULL;
+ const char *name;
+ GtkClipboard *prisel;
+
+ a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
+ if(a == NULL)
+ return;
+
+ name = gtk_action_get_name(a);
+ if(!g_strcmp0(name, "context-menu-action-3")) {
+ prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
+ gtk_clipboard_set_text(prisel, c->linkhover, -1);
+ }
+}
+
+void
+pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
+ Arg arg = {.v = text };
+ if(text != NULL)
+ loaduri((Client *) d, &arg);
+}
+
+void
+print(Client *c, const Arg *arg) {
+ webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
+}
+
+GdkFilterReturn
+processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
+ Client *c = (Client *)d;
+ XPropertyEvent *ev;
+ Arg arg;
+
+ if(((XEvent *)e)->type == PropertyNotify) {
+ ev = &((XEvent *)e)->xproperty;
+ if(ev->state == PropertyNewValue) {
+ if(ev->atom == atoms[AtomFind]) {
+ arg.b = TRUE;
+ find(c, &arg);
+ return GDK_FILTER_REMOVE;
+ }
+ else if(ev->atom == atoms[AtomGo]) {
+ arg.v = getatom(c, AtomGo);
+ loaduri(c, &arg);
+ return GDK_FILTER_REMOVE;
+ }
+ }
+ }
+ return GDK_FILTER_CONTINUE;
+}
+
+void
+progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
+ c->progress = webkit_web_view_get_progress(c->view) * 100;
+ update(c);
+}
+
+void
+reload(Client *c, const Arg *arg) {
+ gboolean nocache = *(gboolean *)arg;
+ if(nocache)
+ webkit_web_view_reload_bypass_cache(c->view);
+ else
+ webkit_web_view_reload(c->view);
+}
+
+void
+scroll_h(Client *c, const Arg *arg) {
+ scroll(gtk_scrolled_window_get_hadjustment(
+ GTK_SCROLLED_WINDOW(c->scroll)), arg);
+}
+
+void
+scroll_v(Client *c, const Arg *arg) {
+ scroll(gtk_scrolled_window_get_vadjustment(
+ GTK_SCROLLED_WINDOW(c->scroll)), arg);
+}
+
+void
+scroll(GtkAdjustment *a, const Arg *arg) {
+ gdouble v;
+
+ v = gtk_adjustment_get_value(a);
+ switch (arg->i){
+ case +10000:
+ case -10000:
+ v += gtk_adjustment_get_page_increment(a) *
+ (arg->i / 10000);
+ break;
+ case +20000:
+ case -20000:
+ default:
+ v += gtk_adjustment_get_step_increment(a) * arg->i;
+ }
+
+ v = MAX(v, 0.0);
+ v = MIN(v, gtk_adjustment_get_upper(a) -
+ gtk_adjustment_get_page_size(a));
+ gtk_adjustment_set_value(a, v);
+}
+
+void
+setatom(Client *c, int a, const char *v) {
+ XSync(dpy, False);
+ XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), atoms[a],
+ XA_STRING, 8, PropModeReplace, (unsigned char *)v,
+ strlen(v) + 1);
+}
+
+void
+setup(void) {
+ char *proxy;
+ char *new_proxy;
+ SoupURI *puri;
+ SoupSession *s;
+
+ /* clean up any zombies immediately */
+ sigchld(0);
+ gtk_init(NULL, NULL);
+ if (!g_thread_supported())
+ g_thread_init(NULL);
+
+ dpy = GDK_DISPLAY();
+
+ /* atoms */
+ atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
+ atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
+ atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
+
+ /* dirs and files */
+ cookiefile = buildpath(cookiefile);
+ scriptfile = buildpath(scriptfile);
+ stylefile = buildpath(stylefile);
+
+ /* request handler */
+ s = webkit_get_default_session();
+
+ /* cookie jar */
+ soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar_new(cookiefile, FALSE)));
+
+ /* ssl */
+ g_object_set(G_OBJECT(s), "ssl-ca-file", cafile, NULL);
+ g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
+
+ /* proxy */
+ if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
+ new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
+ g_strdup_printf("http://%s", proxy);
+ puri = soup_uri_new(new_proxy);
+ g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
+ soup_uri_free(puri);
+ g_free(new_proxy);
+ using_proxy = 1;
+ }
+}
+
+void
+sigchld(int unused) {
+ if(signal(SIGCHLD, sigchld) == SIG_ERR)
+ die("Can't install SIGCHLD handler");
+ while(0 < waitpid(-1, NULL, WNOHANG));
+}
+
+void
+source(Client *c, const Arg *arg) {
+ Arg a = { .b = FALSE };
+ gboolean s;
+
+ s = webkit_web_view_get_view_source_mode(c->view);
+ webkit_web_view_set_view_source_mode(c->view, !s);
+ reload(c, &a);
+}
+
+void
+spawn(Client *c, const Arg *arg) {
+ if(fork() == 0) {
+ if(dpy)
+ close(ConnectionNumber(dpy));
+ setsid();
+ execvp(((char **)arg->v)[0], (char **)arg->v);
+ fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
+ perror(" failed");
+ exit(0);
+ }
+}
+
+void
+eval(Client *c, const Arg *arg) {
+ WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
+ evalscript(webkit_web_frame_get_global_context(frame),
+ ((char **)arg->v)[0], "");
+}
+
+void
+stop(Client *c, const Arg *arg) {
+ webkit_web_view_stop_loading(c->view);
+}
+
+void
+titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
+ c->title = copystr(&c->title, t);
+ update(c);
+}
+
+void
+toggle(Client *c, const Arg *arg) {
+ WebKitWebSettings *settings;
+ char *name = (char *)arg->v;
+ gboolean value;
+ Arg a = { .b = FALSE };
+
+ settings = webkit_web_view_get_settings(c->view);
+ g_object_get(G_OBJECT(settings), name, &value, NULL);
+ g_object_set(G_OBJECT(settings), name, !value, NULL);
+
+ reload(c,&a);
+}
+
+void
+gettogglestat(Client *c){
+ gboolean value;
+ WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
+
+ togglestat[4] = '\0';
+ g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
+ togglestat[0] = value?'I':'i';
+ g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
+ togglestat[1] = value?'S':'s';
+ g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
+ togglestat[2] = value?'V':'v';
+ g_object_get(G_OBJECT(settings), "enable-caret-browsing",
+ &value, NULL);
+ togglestat[3] = value?'C':'c';
+
+ togglestat[4] = insertmode? '+' : '-';
+ togglestat[5] = '\0';
+}
+
+
+void
+update(Client *c) {
+ char *t;
+
+ gettogglestat(c);
+
+ if(c->linkhover) {
+ t = g_strdup_printf("%s| %s", togglestat, c->linkhover);
+ } else if(c->progress != 100) {
+ drawindicator(c);
+ gtk_widget_show(c->indicator);
+ t = g_strdup_printf("[%i%%] %s| %s", c->progress, togglestat,
+ c->title);
+ } else {
+ gtk_widget_hide_all(c->indicator);
+ t = g_strdup_printf("%s| %s", togglestat, c->title);
+ }
+
+ gtk_window_set_title(GTK_WINDOW(c->win), t);
+ g_free(t);
+}
+
+void
+updatewinid(Client *c) {
+ snprintf(winid, LENGTH(winid), "%u",
+ (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
+}
+
+void
+usage(void) {
+ fputs("surf - simple browser\n", stderr);
+ die("usage: surf [-c cookiefile] [-e xid] [-i] [-p] [-r scriptfile]"
+ " [-s] [-t stylefile] [-u useragent] [-v] [-x] [uri]\n");
+}
+
+void
+windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c) {
+ runscript(frame);
+}
+
+void
+zoom(Client *c, const Arg *arg) {
+ c->zoomed = TRUE;
+ if(arg->i < 0) /* zoom out */
+ webkit_web_view_zoom_out(c->view);
+ else if(arg->i > 0) /* zoom in */
+ webkit_web_view_zoom_in(c->view);
+ else { /* reset */
+ c->zoomed = FALSE;
+ webkit_web_view_set_zoom_level(c->view, 1.0);
+ }
+}
+
+int
+main(int argc, char *argv[]) {
+ Arg arg;
+
+ memset(&arg, 0, sizeof(arg));
+
+ /* command line args */
+ ARGBEGIN {
+ case 'c':
+ cookiefile = EARGF(usage());