X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=surf.c;h=fc613815a295142994e2bc16d8e6d1068a197794;hb=bd5bbb7fc352a4f1f5c54731e6c8ef01ff91d002;hp=682c1e299c38043f9381b31841dd98c83a5d5725;hpb=0415175e10880585c765613af0bd43e02de87116;p=surf.git diff --git a/surf.c b/surf.c index 682c1e2..fc61381 100644 --- a/surf.c +++ b/surf.c @@ -42,22 +42,16 @@ union Arg { }; typedef struct Client { - GtkWidget *win, *scroll, *vbox, *indicator; + GtkWidget *win, *scroll, *vbox, *pane; WebKitWebView *view; WebKitWebInspector *inspector; char *title, *linkhover; const char *uri, *needle; gint progress; struct Client *next; - gboolean zoomed, fullscreen, isinspector, sslfailed; + gboolean zoomed, fullscreen, isinspecting, sslfailed; } Client; -typedef struct { - char *label; - void (*func)(Client *c, const Arg *arg); - const Arg arg; -} Item; - typedef struct { guint mod; guint keyval; @@ -83,7 +77,8 @@ static GdkNativeWindow embed = 0; static gboolean showxid = FALSE; static char winid[64]; static gboolean usingproxy = 0; -static char togglestat[5]; +static char togglestat[6]; +static char pagestat[3]; static void beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r, WebKitNetworkRequest *req, @@ -113,13 +108,12 @@ static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f, static void destroyclient(Client *c); static void destroywin(GtkWidget* w, Client *c); static void die(const char *errstr, ...); -static void drawindicator(Client *c); static void eval(Client *c, const Arg *arg); -static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c); static void find(Client *c, const Arg *arg); static void fullscreen(Client *c, const Arg *arg); static const char *getatom(Client *c, int a); static void gettogglestat(Client *c); +static void getpagestat(Client *c); static char *geturi(Client *c); static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c); @@ -159,6 +153,7 @@ static void stop(Client *c, const Arg *arg); static void titlechange(WebKitWebView *v, WebKitWebFrame *frame, const char *title, Client *c); static void toggle(Client *c, const Arg *arg); +static void togglestyle(Client *c, const Arg *arg); static void update(Client *c); static void updatewinid(Client *c); static void usage(void); @@ -370,7 +365,6 @@ destroyclient(Client *c) { Client *p; webkit_web_view_stop_loading(c->view); - gtk_widget_destroy(c->indicator); gtk_widget_destroy(GTK_WIDGET(c->view)); gtk_widget_destroy(c->scroll); gtk_widget_destroy(c->vbox); @@ -402,51 +396,6 @@ die(const char *errstr, ...) { exit(EXIT_FAILURE); } -static void -drawindicator(Client *c) { - gint width; - const char *uri; - char *colorname; - GtkWidget *w; - GdkGC *gc; - GdkColor fg; - - uri = geturi(c); - w = c->indicator; - width = c->progress * w->allocation.width / 100; - gc = gdk_gc_new(w->window); - if(strstr(uri, "https://") == uri) { - if(usingproxy) { - colorname = c->sslfailed? progress_proxy_untrust : - progress_proxy_trust; - } else { - colorname = c->sslfailed? progress_untrust : - progress_trust; - } - } else { - if(usingproxy) { - colorname = progress_proxy; - } else { - colorname = progress; - } - } - - gdk_color_parse(colorname, &fg); - gdk_gc_set_rgb_fg_color(gc, &fg); - gdk_draw_rectangle(w->window, - w->style->bg_gc[GTK_WIDGET_STATE(w)], - TRUE, 0, 0, w->allocation.width, w->allocation.height); - gdk_draw_rectangle(w->window, gc, TRUE, 0, 0, width, - w->allocation.height); - g_object_unref(gc); -} - -static gboolean -exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) { - drawindicator(c); - return TRUE; -} - static void find(Client *c, const Arg *arg) { const char *s; @@ -506,28 +455,45 @@ initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) { static void inspector(Client *c, const Arg *arg) { - if(c->isinspector) - return; - webkit_web_inspector_show(c->inspector); + if(c->isinspecting) { + webkit_web_inspector_close(c->inspector); + } else { + webkit_web_inspector_show(c->inspector); + } } static WebKitWebView * inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c) { - Client *n = newclient(); - n->isinspector = true; - - return n->view; + return WEBKIT_WEB_VIEW(webkit_web_view_new()); } static gboolean inspector_show(WebKitWebInspector *i, Client *c) { - gtk_widget_show(GTK_WIDGET(webkit_web_inspector_get_web_view(i))); + WebKitWebView *w; + + if(c->isinspecting) + return false; + + w = webkit_web_inspector_get_web_view(i); + gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE); + gtk_widget_show(GTK_WIDGET(w)); + c->isinspecting = true; + return true; } static gboolean inspector_close(WebKitWebInspector *i, Client *c) { - gtk_widget_hide(GTK_WIDGET(webkit_web_inspector_get_web_view(i))); + GtkWidget *w; + + if(!c->isinspecting) + return false; + + w = GTK_WIDGET(webkit_web_inspector_get_web_view(i)); + gtk_widget_hide(w); + gtk_widget_destroy(w); + c->isinspecting = false; + return true; } @@ -671,8 +637,12 @@ newclient(void) { "key-press-event", G_CALLBACK(keypress), c); + /* Pane */ + c->pane = gtk_vpaned_new(); + /* VBox */ c->vbox = gtk_vbox_new(FALSE, 0); + gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE); /* Scrolled Window */ c->scroll = gtk_scrolled_window_new(NULL, NULL); @@ -718,24 +688,16 @@ newclient(void) { "resource-request-starting", G_CALLBACK(beforerequest), c); - /* Indicator */ - c->indicator = gtk_drawing_area_new(); - gtk_widget_set_size_request(c->indicator, 0, indicator_thickness); - g_signal_connect (G_OBJECT (c->indicator), "expose_event", - G_CALLBACK (exposeindicator), c); - /* Arranging */ gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view)); - gtk_container_add(GTK_CONTAINER(c->win), c->vbox); + gtk_container_add(GTK_CONTAINER(c->win), c->pane); gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll); - gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator); /* Setup */ - gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE, - FALSE, 0, GTK_PACK_START); gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0, GTK_PACK_START); gtk_widget_grab_focus(GTK_WIDGET(c->view)); + gtk_widget_show(c->pane); gtk_widget_show(c->vbox); gtk_widget_show(c->scroll); gtk_widget_show(GTK_WIDGET(c->view)); @@ -776,7 +738,7 @@ newclient(void) { G_CALLBACK(inspector_close), c); g_signal_connect(G_OBJECT(c->inspector), "finished", G_CALLBACK(inspector_finished), c); - c->isinspector = false; + c->isinspecting = false; } g_free(uri); @@ -789,6 +751,7 @@ newclient(void) { c->title = NULL; c->next = clients; clients = c; + if(showxid) { gdk_display_sync(gtk_widget_get_display(c->win)); printf("%u\n", @@ -798,6 +761,7 @@ newclient(void) { die("Error closing stdout"); } } + return c; } @@ -973,8 +937,6 @@ setup(void) { /* clean up any zombies immediately */ sigchld(0); gtk_init(NULL, NULL); - if (!g_thread_supported()) - g_thread_init(NULL); dpy = GDK_DISPLAY(); @@ -1077,6 +1039,7 @@ toggle(Client *c, const Arg *arg) { static void gettogglestat(Client *c){ gboolean value; + char *uri; WebKitWebSettings *settings = webkit_web_view_get_settings(c->view); g_object_get(G_OBJECT(settings), "enable-caret-browsing", @@ -1092,26 +1055,41 @@ gettogglestat(Client *c){ g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL); togglestat[3] = value? 'V': 'v'; - togglestat[4] = '\0'; + g_object_get(G_OBJECT(settings), "user-stylesheet-uri", &uri, NULL); + togglestat[4] = uri[0] ? 'M': 'm'; + + togglestat[5] = '\0'; } +static void +getpagestat(Client *c) { + const char *uri = geturi(c); + + if(strstr(uri, "https://") == uri) { + pagestat[0] = c->sslfailed ? 'U' : 'T'; + } else { + pagestat[0] = '-'; + } + + pagestat[1] = usingproxy ? 'P' : '-'; + pagestat[2] = '\0'; + +} static void update(Client *c) { char *t; gettogglestat(c); + getpagestat(c); if(c->linkhover) { - t = g_strdup_printf("%s| %s", togglestat, c->linkhover); + t = g_strdup_printf("%s:%s | %s", togglestat, pagestat, 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); + t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress, togglestat, + pagestat, c->title); } else { - gtk_widget_hide_all(c->indicator); - t = g_strdup_printf("%s| %s", togglestat, c->title); + t = g_strdup_printf("%s:%s | %s", togglestat, pagestat, c->title); } gtk_window_set_title(GTK_WINDOW(c->win), t); @@ -1210,3 +1188,15 @@ main(int argc, char *argv[]) { return EXIT_SUCCESS; } +static void +togglestyle(Client *c, const Arg *arg) { + WebKitWebSettings *settings; + char *uri; + + settings = webkit_web_view_get_settings(c->view); + g_object_get(G_OBJECT(settings), "user-stylesheet-uri", &uri, NULL); + uri = uri[0] ? g_strdup("") : g_strconcat("file://", stylefile, NULL); + g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL); + + update(c); +}