|
Line 0
Link Here
|
|
|
1 |
--- surf.c.orig 2015-12-19 14:59:30 UTC |
| 2 |
+++ surf.c |
| 3 |
@@ -6,6 +6,9 @@ |
| 4 |
#include <X11/X.h> |
| 5 |
#include <X11/Xatom.h> |
| 6 |
#include <gtk/gtk.h> |
| 7 |
+#if GTK_MAJOR_VERSION >= 3 |
| 8 |
+#include <gtk/gtkx.h> |
| 9 |
+#endif |
| 10 |
#include <gdk/gdkx.h> |
| 11 |
#include <gdk/gdk.h> |
| 12 |
#include <gdk/gdkkeysyms.h> |
| 13 |
@@ -99,7 +102,11 @@ typedef struct { |
| 14 |
static Display *dpy; |
| 15 |
static Atom atoms[AtomLast]; |
| 16 |
static Client *clients = NULL; |
| 17 |
+#if GTK_MAJOR_VERSION >= 3 |
| 18 |
+static Window embed = 0; |
| 19 |
+#else |
| 20 |
static GdkNativeWindow embed = 0; |
| 21 |
+#endif |
| 22 |
static gboolean showxid = FALSE; |
| 23 |
static char winid[64]; |
| 24 |
static gboolean usingproxy = 0; |
| 25 |
@@ -213,6 +220,10 @@ static void windowobjectcleared(GtkWidge |
| 26 |
JSContextRef js, JSObjectRef win, Client *c); |
| 27 |
static void zoom(Client *c, const Arg *arg); |
| 28 |
|
| 29 |
+#ifdef BUILTIN_DOWNLOAD |
| 30 |
+static void download(WebKitDownload *o, GParamSpec *pspec, Client *c); |
| 31 |
+#endif |
| 32 |
+ |
| 33 |
/* configuration, allows nested code to access above variables */ |
| 34 |
#include "config.h" |
| 35 |
|
| 36 |
@@ -451,6 +462,52 @@ cookiepolicy_set(const SoupCookieJarAcce |
| 37 |
return 'A'; |
| 38 |
} |
| 39 |
|
| 40 |
+#ifdef BUILTIN_DOWNLOAD |
| 41 |
+struct client_size_tuple { |
| 42 |
+ Client* c; |
| 43 |
+ gint s; |
| 44 |
+}; |
| 45 |
+ |
| 46 |
+static void |
| 47 |
+late_download_update(WebKitWebView* view, GParamSpec *pspec, struct client_size_tuple* t){ |
| 48 |
+ char script[1024]; char* s= script; |
| 49 |
+ snprintf(script, 1024, "c(%d, %d)", t->s, t->s); |
| 50 |
+ const Arg a= {.v = (void*) &s}; |
| 51 |
+ eval(t->c, &a); |
| 52 |
+ free(t); |
| 53 |
+} |
| 54 |
+ |
| 55 |
+static void |
| 56 |
+download(WebKitDownload *o, GParamSpec *pspec, Client *c) { |
| 57 |
+ WebKitDownloadStatus status; |
| 58 |
+ char script[2048]; char* s= script; |
| 59 |
+ |
| 60 |
+ status = webkit_download_get_status(o); |
| 61 |
+ if(status == WEBKIT_DOWNLOAD_STATUS_STARTED || status == WEBKIT_DOWNLOAD_STATUS_CREATED) { |
| 62 |
+ snprintf(script, 2048, "u(%d, %d, %d)", |
| 63 |
+ (gint)webkit_download_get_current_size(o), |
| 64 |
+ (gint)webkit_download_get_total_size(o), |
| 65 |
+ (gint)(webkit_download_get_progress(o) * 100)); |
| 66 |
+ const Arg a= {.v = (void*) &s}; |
| 67 |
+ eval(c, &a); |
| 68 |
+ } |
| 69 |
+ else if (status == WEBKIT_DOWNLOAD_STATUS_FINISHED){ |
| 70 |
+ if( webkit_web_view_get_load_status(c->view) == WEBKIT_LOAD_FINISHED ){ |
| 71 |
+ snprintf(script, 2048, "c(%d, %d)", |
| 72 |
+ (gint)webkit_download_get_current_size(o), |
| 73 |
+ (gint)webkit_download_get_total_size(o)); |
| 74 |
+ const Arg a= {.v = (void*) &s}; |
| 75 |
+ eval(c, &a); |
| 76 |
+ } |
| 77 |
+ else { |
| 78 |
+ struct client_size_tuple* t= calloc(1, sizeof(struct client_size_tuple)); |
| 79 |
+ t->c= c; t->s= (gint)webkit_download_get_current_size(o); |
| 80 |
+ g_signal_connect(c->view, "document-load-finished", G_CALLBACK(late_download_update), t); |
| 81 |
+ } |
| 82 |
+ } |
| 83 |
+} |
| 84 |
+#endif |
| 85 |
+ |
| 86 |
void |
| 87 |
evalscript(JSContextRef js, char *script, char* scriptname) |
| 88 |
{ |
| 89 |
@@ -625,7 +682,11 @@ getatom(Client *c, int a) |
| 90 |
unsigned long ldummy; |
| 91 |
unsigned char *p = NULL; |
| 92 |
|
| 93 |
+#if GTK_MAJOR_VERSION >= 3 |
| 94 |
+ XGetWindowProperty(dpy, GDK_WINDOW_XID(gtk_widget_get_window(GTK_WIDGET(c->win))), |
| 95 |
+#else |
| 96 |
XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), |
| 97 |
+#endif |
| 98 |
atoms[a], 0L, BUFSIZ, False, XA_STRING, |
| 99 |
&adummy, &idummy, &ldummy, &ldummy, &p); |
| 100 |
if (p) |
| 101 |
@@ -685,12 +746,114 @@ handleplumb(Client *c, WebKitWebView *w, |
| 102 |
gboolean |
| 103 |
initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) |
| 104 |
{ |
| 105 |
+#ifdef BUILTIN_DOWNLOAD |
| 106 |
+ gchar *uri, *path; |
| 107 |
+ const gchar *filename; |
| 108 |
+ Client *n; |
| 109 |
+ const char template[] = |
| 110 |
+"<html>" \ |
| 111 |
+"<head>" \ |
| 112 |
+"<title>Download - %s</title>" \ |
| 113 |
+"<script>" \ |
| 114 |
+"function formText(x){" \ |
| 115 |
+" if(x >= 1073741824) { return (Math.floor(x/10737418.24)/100) + \"G\"; }" \ |
| 116 |
+" else if(x >= 1048576){ return (Math.floor(x/10485.76)/100) + \"M\"; }" \ |
| 117 |
+" else if(x >= 1024) { return (Math.floor(x/10.24)/100) + \"k\"; }" \ |
| 118 |
+" else { return x+\"b\"; }" \ |
| 119 |
+"}" \ |
| 120 |
+"function updateText(c,t){" \ |
| 121 |
+" txt= formText(c) + \"/\" + formText(t);" \ |
| 122 |
+" DLTEXT.textContent= txt;" \ |
| 123 |
+" /* center text in bar */" \ |
| 124 |
+" DLTEXT.setAttribute('x', 102-4.4*txt.length)" \ |
| 125 |
+"}" \ |
| 126 |
+"function c(c, t){" \ |
| 127 |
+" DLGRAD.setAttribute('x2', 230);" \ |
| 128 |
+" DLGRAD.setAttribute('x1', 205);" \ |
| 129 |
+" updateText(c,t);" \ |
| 130 |
+" document.getElementById('stop1').setAttribute('style', \"stop-color:#2020ff;\");" \ |
| 131 |
+"}" \ |
| 132 |
+"function u(c,t,p){" \ |
| 133 |
+" DLGRAD.setAttribute('x2', Math.floor(p*205/100) + 25);" \ |
| 134 |
+" DLGRAD.setAttribute('x1', Math.floor(p*205/100));" \ |
| 135 |
+" updateText(c,t);" \ |
| 136 |
+"}" \ |
| 137 |
+"</script>" \ |
| 138 |
+"</head>" \ |
| 139 |
+"<body>" \ |
| 140 |
+"<center>" \ |
| 141 |
+"<h2>Downloading</h2>" \ |
| 142 |
+"<h3>%s</h3>" \ |
| 143 |
+"to %s<br/>" \ |
| 144 |
+"<svg" \ |
| 145 |
+" xmlns:cc=\"http://creativecommons.org/ns#\"" \ |
| 146 |
+" xmlns:svg=\"http://www.w3.org/2000/svg\"" \ |
| 147 |
+" xmlns=\"http://www.w3.org/2000/svg\"" \ |
| 148 |
+" xmlns:xlink=\"http://www.w3.org/1999/xlink\"" \ |
| 149 |
+" width=\"210\"" \ |
| 150 |
+" height=\"60\"" \ |
| 151 |
+" id=\"download\">" \ |
| 152 |
+" <defs>" \ |
| 153 |
+" <linearGradient" \ |
| 154 |
+" id=\"dlgradient\"" \ |
| 155 |
+" x1=\"0\"" \ |
| 156 |
+" y1=\"0\"" \ |
| 157 |
+" x2=\"25\"" \ |
| 158 |
+" y2=\"0\"" \ |
| 159 |
+" gradientUnits=\"userSpaceOnUse\">" \ |
| 160 |
+" <stop style=\"stop-color:#00ff00;\" offset=\"0\" id=\"stop1\" />" \ |
| 161 |
+" <stop style=\"stop-color:#00ff00;stop-opacity:0;\" offset=\"1\" id=\"stop2\" />" \ |
| 162 |
+" </linearGradient>" \ |
| 163 |
+" </defs>" \ |
| 164 |
+" <rect" \ |
| 165 |
+" style=\"fill:url(#dlgradient);stroke:#000000;stroke-width:3\"" \ |
| 166 |
+" id=\"rect2985\"" \ |
| 167 |
+" width=\"200\"" \ |
| 168 |
+" height=\"50\"" \ |
| 169 |
+" x=\"5\"" \ |
| 170 |
+" y=\"5\"" \ |
| 171 |
+" ry=\"25\" />" \ |
| 172 |
+" <text id=\"dltext\" x=\"92\" y=\"35\">0/0</text>" \ |
| 173 |
+"</svg>" \ |
| 174 |
+"</center>" \ |
| 175 |
+"<script>" \ |
| 176 |
+"DLGRAD= document.getElementById('dlgradient');" \ |
| 177 |
+"DLTEXT= document.getElementById('dltext');" \ |
| 178 |
+"</script>" \ |
| 179 |
+"</body>" \ |
| 180 |
+"</html>"; |
| 181 |
+ char html[sizeof(template)+2048]; |
| 182 |
+ filename = webkit_download_get_suggested_filename(o); |
| 183 |
+ |
| 184 |
+ path = g_build_filename(downdir, filename, NULL); |
| 185 |
+ uri = g_filename_to_uri(path, NULL, NULL); |
| 186 |
+ |
| 187 |
+ webkit_download_set_destination_uri(o, uri); |
| 188 |
+ webkit_download_start(o); |
| 189 |
+ |
| 190 |
+ n = newclient(); |
| 191 |
+ snprintf(html, sizeof(template)+2048, template, filename, filename, path); |
| 192 |
+ webkit_web_view_load_string(n->view, html, NULL, NULL, NULL); |
| 193 |
+ |
| 194 |
+ g_signal_connect(o, "notify::progress", G_CALLBACK(download), n); |
| 195 |
+ g_signal_connect(o, "notify::status", G_CALLBACK(download), n); |
| 196 |
+ |
| 197 |
+ n->title = g_strdup_printf("Downloading %s", filename); |
| 198 |
+ n->progress = 0; |
| 199 |
+ updatetitle(n); |
| 200 |
+ |
| 201 |
+ g_free(path); |
| 202 |
+ g_free(uri); |
| 203 |
+ |
| 204 |
+ return TRUE; |
| 205 |
+#else |
| 206 |
Arg arg; |
| 207 |
|
| 208 |
updatewinid(c); |
| 209 |
arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c)); |
| 210 |
spawn(c, &arg); |
| 211 |
return FALSE; |
| 212 |
+#endif |
| 213 |
} |
| 214 |
|
| 215 |
void |
| 216 |
@@ -873,6 +1036,9 @@ newclient(void) |
| 217 |
GdkScreen *screen; |
| 218 |
gdouble dpi; |
| 219 |
char *ua; |
| 220 |
+#if GTK_MAJOR_VERSION >= 3 |
| 221 |
+ GdkWindow *window; |
| 222 |
+#endif |
| 223 |
|
| 224 |
if (!(c = calloc(1, sizeof(Client)))) |
| 225 |
die("Cannot malloc!\n"); |
| 226 |
@@ -900,6 +1066,10 @@ newclient(void) |
| 227 |
*/ |
| 228 |
gtk_window_set_role(GTK_WINDOW(c->win), "Surf"); |
| 229 |
} |
| 230 |
+#if GTK_MAJOR_VERSION >= 3 |
| 231 |
+ gtk_widget_realize(GTK_WIDGET(c->win)); |
| 232 |
+ window = gtk_widget_get_window(GTK_WIDGET(c->win)); |
| 233 |
+#endif |
| 234 |
gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600); |
| 235 |
g_signal_connect(G_OBJECT(c->win), |
| 236 |
"destroy", |
| 237 |
@@ -912,10 +1082,18 @@ newclient(void) |
| 238 |
addaccelgroup(c); |
| 239 |
|
| 240 |
/* Pane */ |
| 241 |
+#if GTK_MAJOR_VERSION >= 3 |
| 242 |
+ c->pane = gtk_paned_new(GTK_ORIENTATION_VERTICAL); |
| 243 |
+#else |
| 244 |
c->pane = gtk_vpaned_new(); |
| 245 |
+#endif |
| 246 |
|
| 247 |
/* VBox */ |
| 248 |
+#if GTK_MAJOR_VERSION >= 3 |
| 249 |
+ c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 250 |
+#else |
| 251 |
c->vbox = gtk_vbox_new(FALSE, 0); |
| 252 |
+#endif |
| 253 |
gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE); |
| 254 |
|
| 255 |
/* Webview */ |
| 256 |
@@ -997,8 +1175,13 @@ newclient(void) |
| 257 |
gtk_widget_show(c->win); |
| 258 |
gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints, |
| 259 |
GDK_HINT_MIN_SIZE); |
| 260 |
+#if GTK_MAJOR_VERSION >= 3 |
| 261 |
+ gdk_window_set_events(window, GDK_ALL_EVENTS_MASK); |
| 262 |
+ gdk_window_add_filter(window, processx, c); |
| 263 |
+#else |
| 264 |
gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK); |
| 265 |
gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c); |
| 266 |
+#endif |
| 267 |
webkit_web_view_set_full_content_zoom(c->view, TRUE); |
| 268 |
|
| 269 |
runscript(frame); |
| 270 |
@@ -1032,7 +1215,11 @@ newclient(void) |
| 271 |
* It is equivalent to firefox's "layout.css.devPixelsPerPx" setting. |
| 272 |
*/ |
| 273 |
if (zoomto96dpi) { |
| 274 |
+#if GTK_MAJOR_VERSION >= 3 |
| 275 |
+ screen = gdk_window_get_screen(window); |
| 276 |
+#else |
| 277 |
screen = gdk_window_get_screen(GTK_WIDGET(c->win)->window); |
| 278 |
+#endif |
| 279 |
dpi = gdk_screen_get_resolution(screen); |
| 280 |
if (dpi != -1) { |
| 281 |
g_object_set(G_OBJECT(settings), |
| 282 |
@@ -1071,7 +1258,11 @@ newclient(void) |
| 283 |
if (showxid) { |
| 284 |
gdk_display_sync(gtk_widget_get_display(c->win)); |
| 285 |
printf("%u\n", |
| 286 |
+#if GTK_MAJOR_VERSION >= 3 |
| 287 |
+ (guint)GDK_WINDOW_XID(window)); |
| 288 |
+#else |
| 289 |
(guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window)); |
| 290 |
+#endif |
| 291 |
fflush(NULL); |
| 292 |
if (fclose(stdout) != 0) { |
| 293 |
die("Error closing stdout"); |
| 294 |
@@ -1282,7 +1473,11 @@ void |
| 295 |
setatom(Client *c, int a, const char *v) |
| 296 |
{ |
| 297 |
XSync(dpy, False); |
| 298 |
+#if GTK_MAJOR_VERSION >= 3 |
| 299 |
+ XChangeProperty(dpy, GDK_WINDOW_XID(gtk_widget_get_window(GTK_WIDGET(c->win))), |
| 300 |
+#else |
| 301 |
XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), |
| 302 |
+#endif |
| 303 |
atoms[a], XA_STRING, 8, PropModeReplace, |
| 304 |
(unsigned char *)v, strlen(v) + 1); |
| 305 |
} |
| 306 |
@@ -1301,7 +1496,11 @@ setup(void) |
| 307 |
sigchld(0); |
| 308 |
gtk_init(NULL, NULL); |
| 309 |
|
| 310 |
+#if GTK_MAJOR_VERSION >= 3 |
| 311 |
+ dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default()); |
| 312 |
+#else |
| 313 |
dpy = GDK_DISPLAY(); |
| 314 |
+#endif |
| 315 |
|
| 316 |
/* atoms */ |
| 317 |
atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False); |
| 318 |
@@ -1624,7 +1823,11 @@ void |
| 319 |
updatewinid(Client *c) |
| 320 |
{ |
| 321 |
snprintf(winid, LENGTH(winid), "%u", |
| 322 |
+#if GTK_MAJOR_VERSION >= 3 |
| 323 |
+ (int)GDK_WINDOW_XID(gtk_widget_get_window(GTK_WIDGET(c->win)))); |
| 324 |
+#else |
| 325 |
(int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window)); |
| 326 |
+#endif |
| 327 |
} |
| 328 |
|
| 329 |
void |