X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=surf.c;h=724fcf316f1e34b0b48a66947dab57c789b1f827;hb=4415c475f7063ac5df0a0cca6b6696f0987f1404;hp=28bffefad3a6e579bd583ec6aa920f16eb3ff4ed;hpb=c65c8d422b0c4ab800ed92256ac033bb883140f9;p=surf.git diff --git a/surf.c b/surf.c index 28bffef..724fcf3 100644 --- a/surf.c +++ b/surf.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -16,7 +17,7 @@ #include #include #include -#include +#include #include #include #include @@ -55,6 +56,7 @@ union Arg { typedef struct Client { GtkWidget *win, *scroll, *vbox, *pane; + Window xid; WebKitWebView *view; WebKitWebInspector *inspector; char *title, *linkhover; @@ -99,10 +101,9 @@ typedef struct { static Display *dpy; static Atom atoms[AtomLast]; static Client *clients = NULL; -static GdkNativeWindow embed = 0; +static Window embed = 0; static gboolean showxid = FALSE; static char winid[64]; -static gboolean usingproxy = 0; static char togglestat[9]; static char pagestat[3]; static GTlsDatabase *tlsdb; @@ -154,7 +155,8 @@ static const char *getatom(Client *c, int a); static void gettogglestat(Client *c); static void getpagestat(Client *c); static char *geturi(Client *c); -static gchar *getstyle(const char *uri); +static const gchar *getstyle(const char *uri); +static void setstyle(Client *c, const char *style); static void handleplumb(Client *c, WebKitWebView *w, const gchar *uri); @@ -195,7 +197,6 @@ static void scroll(GtkAdjustment *a, const Arg *arg); static void setatom(Client *c, int a, const char *v); static void setup(void); static void sigchld(int unused); -static void source(Client *c, const Arg *arg); static void spawn(Client *c, const Arg *arg); static void stop(Client *c, const Arg *arg); static void titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c); @@ -272,6 +273,7 @@ buildfile(const char *path) g_free(dname); fpath = g_build_filename(bpath, bname, NULL); + g_free(bpath); g_free(bname); if (!(f = fopen(fpath, "a"))) @@ -623,9 +625,9 @@ getatom(Client *c, int a) unsigned long ldummy; unsigned char *p = NULL; - XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), - atoms[a], 0L, BUFSIZ, False, XA_STRING, - &adummy, &idummy, &ldummy, &ldummy, &p); + XGetWindowProperty(dpy, c->xid, + atoms[a], 0L, BUFSIZ, False, XA_STRING, + &adummy, &idummy, &ldummy, &ldummy, &p); if (p) strncpy(buf, (char *)p, LENGTH(buf)-1); else @@ -645,20 +647,29 @@ geturi(Client *c) return uri; } -gchar * +const gchar * getstyle(const char *uri) { int i; if (stylefile != NULL) - return g_strconcat("file://", stylefile, NULL); + return stylefile; for (i = 0; i < LENGTH(styles); i++) { if (styles[i].regex && !regexec(&(styles[i].re), uri, 0, NULL, 0)) - return g_strconcat("file://", styles[i].style, NULL); + return styles[i].style; } - return g_strdup(""); + + return ""; +} + +void +setstyle(Client *c, const char *style) +{ + WebKitWebSettings *settings = webkit_web_view_get_settings(c->view); + + g_object_set(G_OBJECT(settings), "user-stylesheet-uri", style, NULL); } void @@ -685,10 +696,12 @@ initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) void inspector(Client *c, const Arg *arg) { - if (c->isinspecting) - webkit_web_inspector_close(c->inspector); - else - webkit_web_inspector_show(c->inspector); + if (enableinspector) { + if (c->isinspecting) + webkit_web_inspector_close(c->inspector); + else + webkit_web_inspector_show(c->inspector); + } } WebKitWebView * @@ -775,7 +788,6 @@ loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) WebKitWebFrame *frame; WebKitWebDataSource *src; WebKitNetworkRequest *request; - WebKitWebSettings *set = webkit_web_view_get_settings(c->view); SoupMessage *msg; char *uri; @@ -792,10 +804,8 @@ loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) } setatom(c, AtomUri, uri); - if (enablestyles) { - g_object_set(G_OBJECT(set), "user-stylesheet-uri", - getstyle(uri), NULL); - } + if (enablestyle) + setstyle(c, getstyle(uri)); break; case WEBKIT_LOAD_FINISHED: c->progress = 100; @@ -860,6 +870,7 @@ newclient(void) WebKitWebFrame *frame; GdkGeometry hints = { 1, 1 }; GdkScreen *screen; + GdkWindow *gwin; gdouble dpi; char *ua; @@ -901,10 +912,10 @@ newclient(void) addaccelgroup(c); /* Pane */ - c->pane = gtk_vpaned_new(); + c->pane = gtk_paned_new(GTK_ORIENTATION_VERTICAL); /* VBox */ - c->vbox = gtk_vbox_new(FALSE, 0); + c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE); /* Webview */ @@ -984,10 +995,12 @@ newclient(void) gtk_widget_show(c->scroll); gtk_widget_show(GTK_WIDGET(c->view)); gtk_widget_show(c->win); + gwin = gtk_widget_get_window(GTK_WIDGET(c->win)); + c->xid = gdk_x11_window_get_xid(gwin); gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints, - GDK_HINT_MIN_SIZE); - gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK); - gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c); + GDK_HINT_MIN_SIZE); + gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK); + gdk_window_add_filter(gwin, processx, c); webkit_web_view_set_full_content_zoom(c->view, TRUE); runscript(frame); @@ -996,11 +1009,6 @@ newclient(void) if (!(ua = getenv("SURF_USERAGENT"))) ua = useragent; g_object_set(G_OBJECT(settings), "user-agent", ua, NULL); - if (enablestyles) { - g_object_set(G_OBJECT(settings), - "user-stylesheet-uri", getstyle("about:blank"), - NULL); - } g_object_set(G_OBJECT(settings), "auto-load-images", loadimages, NULL); g_object_set(G_OBJECT(settings), @@ -1017,6 +1025,8 @@ newclient(void) "default-font-size", defaultfontsize, NULL); g_object_set(G_OBJECT(settings), "resizable-text-areas", 1, NULL); + if (enablestyle) + setstyle(c, getstyle("about:blank")); /* * While stupid, CSS specifies that a pixel represents 1/96 of an inch. @@ -1024,7 +1034,7 @@ newclient(void) * It is equivalent to firefox's "layout.css.devPixelsPerPx" setting. */ if (zoomto96dpi) { - screen = gdk_window_get_screen(GTK_WIDGET(c->win)->window); + screen = gdk_window_get_screen(gwin); dpi = gdk_screen_get_resolution(screen); if (dpi != -1) { g_object_set(G_OBJECT(settings), @@ -1049,10 +1059,8 @@ newclient(void) c->isinspecting = false; } - if (runinfullscreen) { - c->fullscreen = 0; + if (runinfullscreen) fullscreen(c, NULL); - } setatom(c, AtomFind, ""); setatom(c, AtomUri, "about:blank"); @@ -1064,8 +1072,7 @@ newclient(void) if (showxid) { gdk_display_sync(gtk_widget_get_display(c->win)); - printf("%u\n", - (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window)); + printf("%lu\n", c->xid); fflush(NULL); if (fclose(stdout) != 0) { die("Error closing stdout"); @@ -1145,15 +1152,13 @@ menuactivate(GtkMenuItem *item, Client *c) * context-menu-action-12 stop */ - GtkAction *a = NULL; - const char *name, *uri; + const gchar *name, *uri; GtkClipboard *prisel, *clpbrd; - a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(item)); - if (a == NULL) + name = gtk_actionable_get_action_name(GTK_ACTIONABLE(item)); + if (name == 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); @@ -1276,18 +1281,16 @@ 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); + XChangeProperty(dpy, c->xid, + atoms[a], XA_STRING, 8, PropModeReplace, + (unsigned char *)v, strlen(v) + 1); } void setup(void) { int i; - char *proxy; - char *new_proxy; - SoupURI *puri; + char *styledirfile, *stylepath; SoupSession *s; GError *error = NULL; @@ -1295,7 +1298,7 @@ setup(void) sigchld(0); gtk_init(NULL, NULL); - dpy = GDK_DISPLAY(); + dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default()); /* atoms */ atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False); @@ -1306,8 +1309,8 @@ setup(void) cookiefile = buildfile(cookiefile); scriptfile = buildfile(scriptfile); cachefolder = buildpath(cachefolder); - styledir = buildpath(styledir); if (stylefile == NULL) { + styledir = buildpath(styledir); for (i = 0; i < LENGTH(styles); i++) { if (regcomp(&(styles[i].re), styles[i].regex, REG_EXTENDED)) { @@ -1316,11 +1319,19 @@ setup(void) styles[i].regex); styles[i].regex = NULL; } - styles[i].style = buildfile(g_strconcat(styledir, "/", - styles[i].style, NULL)); + styledirfile = g_strconcat(styledir, "/", + styles[i].style, NULL); + stylepath = buildfile(styledirfile); + styles[i].style = g_strconcat("file://", stylepath, + NULL); + g_free(styledirfile); + g_free(stylepath); } + g_free(styledir); } else { - stylefile = buildfile(stylefile); + stylepath = buildfile(stylefile); + stylefile = g_strconcat("file://", stylepath, NULL); + g_free(stylepath); } /* request handler */ @@ -1350,21 +1361,6 @@ setup(void) } g_object_set(G_OBJECT(s), "tls-database", tlsdb, 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_strrstr(proxy, "socks://") - || g_strrstr(proxy, "socks4://") - || g_strrstr(proxy, "socks5://") - ? 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); - usingproxy = 1; - } } void @@ -1375,17 +1371,6 @@ sigchld(int unused) 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) { @@ -1522,12 +1507,8 @@ togglescrollbars(Client *c, const Arg *arg) void togglestyle(Client *c, const Arg *arg) { - WebKitWebSettings *settings = webkit_web_view_get_settings(c->view); - char *uri; - - enablestyles = !enablestyles; - uri = enablestyles ? getstyle(geturi(c)) : g_strdup(""); - g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL); + enablestyle = !enablestyle; + setstyle(c, enablestyle ? getstyle(geturi(c)) : ""); updatetitle(c); } @@ -1558,7 +1539,7 @@ gettogglestat(Client *c) g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL); togglestat[p++] = value? 'V': 'v'; - togglestat[p++] = enablestyles ? 'M': 'm'; + togglestat[p++] = enablestyle ? 'M': 'm'; togglestat[p] = '\0'; } @@ -1573,8 +1554,7 @@ getpagestat(Client *c) else pagestat[0] = '-'; - pagestat[1] = usingproxy ? 'P' : '-'; - pagestat[2] = '\0'; + pagestat[1] = '\0'; } void @@ -1609,14 +1589,13 @@ updatetitle(Client *c) void updatewinid(Client *c) { - snprintf(winid, LENGTH(winid), "%u", - (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window)); + snprintf(winid, LENGTH(winid), "%lu", c->xid); } void usage(void) { - die("usage: %s [-bBfFgGiIkKnNpPsSvx] [-a cookiepolicies ] " + die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] " "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] " "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0)); } @@ -1677,10 +1656,10 @@ main(int argc, char *argv[]) embed = strtol(EARGF(usage()), NULL, 0); break; case 'f': - runinfullscreen = 1; + runinfullscreen = 0; break; case 'F': - runinfullscreen = 0; + runinfullscreen = 1; break; case 'g': allowgeolocation = 0; @@ -1701,10 +1680,10 @@ main(int argc, char *argv[]) kioskmode = 1; break; case 'm': - enablestyles = 0; + enablestyle = 0; break; case 'M': - enablestyles = 1; + enablestyle = 1; break; case 'n': enableinspector = 0;