X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=surf.c;h=726369a02f511624f064772f2efaf87de0841d38;hb=c4f01cd18bf69dd24153f110be500ccc59eff498;hp=6c83405dbe7bd0dcb88d7007f5b4e2c99ca8c0d4;hpb=b4e78555d020bd33981c50805f5480da22c8136d;p=surf.git diff --git a/surf.c b/surf.c index 6c83405..726369a 100644 --- a/surf.c +++ b/surf.c @@ -65,7 +65,7 @@ typedef struct Client { const char *needle; gint progress; struct Client *next; - gboolean zoomed, fullscreen, isinspecting; + gboolean zoomed, fullscreen; } Client; typedef struct { @@ -107,7 +107,7 @@ static char *buildfile(const char *path); static char *buildpath(const char *path); static gboolean buttonreleased(GtkWidget *w, GdkEventKey *e, Client *c); static void cleanup(void); -static void clipboard(Client *c, const Arg *arg); +static void clipboard(Client *c, const Arg *a); static WebKitCookieAcceptPolicy cookiepolicy_get(void); static char cookiepolicy_set(const WebKitCookieAcceptPolicy p); @@ -127,7 +127,7 @@ static void die(const char *errstr, ...); static void evalscript(Client *c, const char *jsstr, ...); static void runscript(Client *c); static void find(Client *c, const Arg *arg); -static void fullscreen(Client *c, const Arg *arg); +static void togglefullscreen(Client *c, const Arg *a); static gboolean permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c); static const char *getatom(Client *c, int a); @@ -135,7 +135,7 @@ static void gettogglestat(Client *c); static void getpagestat(Client *c); static char *geturi(Client *c); static const gchar *getstyle(const char *uri); -static void setstyle(Client *c, const char *style); +static void setstyle(Client *c, const char *stylefile); static void handleplumb(Client *c, const gchar *uri); @@ -144,12 +144,7 @@ static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d, static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c); static void download(Client *c, WebKitURIResponse *r); -static void inspector(Client *c, const Arg *arg); -static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v, - Client *c); -static gboolean inspector_show(WebKitWebInspector *i, Client *c); -static gboolean inspector_close(WebKitWebInspector *i, Client *c); -static void inspector_finished(WebKitWebInspector *i, Client *c); +static void toggleinspector(Client *c, const Arg *a); static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods, Client *c); @@ -164,7 +159,7 @@ static void showview(WebKitWebView *v, Client *c); static void newwindow(Client *c, const Arg *arg, gboolean noembed); static GtkWidget *createwindow(Client *c); static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d); -static void print(Client *c, const Arg *arg); +static void print(Client *c, const Arg *a); static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer d); static gboolean winevent(GtkWidget *w, GdkEvent *e, Client *c); @@ -362,18 +357,16 @@ runscript(Client *c) } void -clipboard(Client *c, const Arg *arg) +clipboard(Client *c, const Arg *a) { - gboolean paste = *(gboolean *)arg; - - if (paste) { + if (a->b) { /* load clipboard uri */ gtk_clipboard_request_text(gtk_clipboard_get( GDK_SELECTION_PRIMARY), pasteuri, c); - } else { + } else { /* copy uri */ gtk_clipboard_set_text(gtk_clipboard_get( - GDK_SELECTION_PRIMARY), c->linkhover - ? c->linkhover : geturi(c), -1); + GDK_SELECTION_PRIMARY), c->targeturi + ? c->targeturi : geturi(c), -1); } } @@ -600,13 +593,13 @@ find(Client *c, const Arg *arg) } void -fullscreen(Client *c, const Arg *arg) +togglefullscreen(Client *c, const Arg *a) { + /* toggling value is handled in winevent() */ if (c->fullscreen) gtk_window_unfullscreen(GTK_WINDOW(c->win)); else gtk_window_fullscreen(GTK_WINDOW(c->win)); - c->fullscreen = !c->fullscreen; } gboolean @@ -672,11 +665,23 @@ getstyle(const char *uri) } void -setstyle(Client *c, const char *style) +setstyle(Client *c, const char *stylefile) { - WebKitWebSettings *settings = webkit_web_view_get_settings(c->view); + gchar *style; - g_object_set(G_OBJECT(settings), "user-stylesheet-uri", style, NULL); + if (!g_file_get_contents(stylefile, &style, NULL, NULL)) { + fprintf(stderr, "Could not read style file: %s\n", stylefile); + return; + } + + webkit_user_content_manager_add_style_sheet( + webkit_web_view_get_user_content_manager(c->view), + webkit_user_style_sheet_new(style, + WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, + WEBKIT_USER_STYLE_LEVEL_USER, + NULL, NULL)); + + g_free(style); } void @@ -712,60 +717,16 @@ download(Client *c, WebKitURIResponse *r) } void -inspector(Client *c, const Arg *arg) +toggleinspector(Client *c, const Arg *a) { if (enableinspector) { - if (c->isinspecting) + if (webkit_web_inspector_is_attached(c->inspector)) webkit_web_inspector_close(c->inspector); else webkit_web_inspector_show(c->inspector); } } -WebKitWebView * -inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c) -{ - return WEBKIT_WEB_VIEW(webkit_web_view_new()); -} - -gboolean -inspector_show(WebKitWebInspector *i, Client *c) -{ - 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; -} - -gboolean -inspector_close(WebKitWebInspector *i, Client *c) -{ - 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; -} - -void -inspector_finished(WebKitWebInspector *i, Client *c) -{ - g_free(c->inspector); -} - gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods, Client *c) @@ -1015,6 +976,9 @@ showview(WebKitWebView *v, Client *c) c->win = createwindow(c); + if (enableinspector) + c->inspector = webkit_web_view_get_inspector(c->view); + if (!kioskmode) addaccelgroup(c); @@ -1037,7 +1001,7 @@ showview(WebKitWebView *v, Client *c) webkit_web_view_set_zoom_level(c->view, zoomlevel); if (runinfullscreen) - fullscreen(c, NULL); + togglefullscreen(c, NULL); setatom(c, AtomFind, ""); setatom(c, AtomUri, "about:blank"); @@ -1127,6 +1091,8 @@ createwindow(Client *c) G_CALLBACK(destroywin), c); g_signal_connect(G_OBJECT(w), "leave-notify-event", G_CALLBACK(winevent), c); + g_signal_connect(G_OBJECT(w), "window-state-event", + G_CALLBACK(winevent), c); return w; } @@ -1140,9 +1106,10 @@ pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) } void -print(Client *c, const Arg *arg) +print(Client *c, const Arg *a) { - webkit_web_frame_print(webkit_web_view_get_main_frame(c->view)); + webkit_print_operation_run_dialog(webkit_print_operation_new(c->view), + GTK_WINDOW(c->win)); } GdkFilterReturn @@ -1251,7 +1218,6 @@ void setup(void) { int i; - char *styledirfile, *stylepath; WebKitWebContext *context; GError *error = NULL; @@ -1270,6 +1236,7 @@ setup(void) cookiefile = buildfile(cookiefile); scriptfile = buildfile(scriptfile); cachedir = buildpath(cachedir); + if (stylefile == NULL) { styledir = buildpath(styledir); for (i = 0; i < LENGTH(styles); i++) { @@ -1280,19 +1247,12 @@ setup(void) styles[i].regex); styles[i].regex = 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); + styles[i].style = g_strconcat(styledir, "/", + styles[i].style, NULL); } g_free(styledir); } else { - stylepath = buildfile(stylefile); - stylefile = g_strconcat("file://", stylepath, NULL); - g_free(stylepath); + stylefile = buildfile(stylefile); } } @@ -1339,6 +1299,13 @@ winevent(GtkWidget *w, GdkEvent *e, Client *c) c->targeturi = NULL; updatetitle(c); break; + case GDK_WINDOW_STATE: /* fallthrough */ + if (e->window_state.changed_mask == + GDK_WINDOW_STATE_FULLSCREEN) { + c->fullscreen = e->window_state.new_window_state & + GDK_WINDOW_STATE_FULLSCREEN; + break; + } default: return FALSE; }