#include <webkit/webkit.h>
-#define DOWNLOAD_DIR "/tmp/tmp"
-#define LANGUAGE "en-US"
-
-
static void adblock(WebKitWebView *, WebKitWebFrame *, WebKitWebResource *,
WebKitNetworkRequest *, WebKitNetworkResponse *, gpointer);
static void adblock_load(void);
-static void client_destroy(GtkWidget *, gpointer);
-static void client_new(const gchar *uri);
-static gboolean client_new_request(WebKitWebView *, WebKitWebFrame *,
- WebKitNetworkRequest *,
- WebKitWebNavigationAction *,
- WebKitWebPolicyDecision *, gpointer);
+static void client_destroy(GtkWidget *obj, gpointer data);
+static gboolean client_destroy_request(WebKitWebView *, gpointer);
+static WebKitWebView *client_new(const gchar *uri);
+static WebKitWebView *client_new_request(WebKitWebView *, WebKitWebFrame *,
+ gpointer);
static void cooperation_setup(void);
static void changed_load_status(GObject *obj, GParamSpec *pspec,
gpointer data);
static void changed_title(GObject *, GParamSpec *, gpointer);
static void changed_uri(GObject *, GParamSpec *, gpointer);
+static gboolean download_handle(WebKitWebView *, WebKitDownload *, gpointer);
static gboolean download_request(WebKitWebView *, WebKitWebFrame *,
WebKitNetworkRequest *, gchar *,
WebKitWebPolicyDecision *, gpointer);
-static gboolean download_wget(WebKitWebView *, WebKitDownload *, gpointer);
static gchar *ensure_url_scheme(const gchar *);
+static void grab_environment_configuration(void);
static void hover_web_view(WebKitWebView *, gchar *, gchar *, gpointer);
static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
};
+static gchar *accepted_language = "en-US";
static GSList *adblock_patterns = NULL;
static gint clients = 0;
static gboolean cooperative_alone = TRUE;
static gboolean cooperative_instances = TRUE;
static int cooperative_pipe_fp = 0;
+static gchar *download_dir = "/tmp";
static Window embed = 0;
static gchar *first_uri = NULL;
static gdouble global_zoom = 1.0;
uri = webkit_network_request_get_uri(request);
if (show_all_requests)
- fprintf(stderr, "-> %s\n", uri);
+ fprintf(stderr, " -> %s\n", uri);
while (it)
{
{
webkit_network_request_set_uri(request, "about:blank");
if (show_all_requests)
- fprintf(stderr, "\tBLOCKED!\n");
+ fprintf(stderr, " BLOCKED!\n");
return;
}
it = g_slist_next(it);
struct Client *c = (struct Client *)data;
(void)obj;
+ (void)data;
- webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
- gtk_widget_destroy(c->web_view);
- gtk_widget_destroy(c->scroll);
- gtk_widget_destroy(c->status);
- gtk_widget_destroy(c->location);
- gtk_widget_destroy(c->vbox);
- gtk_widget_destroy(c->win);
free(c);
-
clients--;
+
if (clients == 0)
gtk_main_quit();
}
-void
+gboolean
+client_destroy_request(WebKitWebView *web_view, gpointer data)
+{
+ struct Client *c = (struct Client *)data;
+
+ (void)web_view;
+
+ gtk_widget_destroy(c->win);
+
+ return TRUE;
+}
+
+WebKitWebView *
client_new(const gchar *uri)
{
struct Client *c;
- gchar *capitalized_name = NULL, *f;
+ gchar *f;
- if (cooperative_instances && !cooperative_alone)
+ if (uri != NULL && cooperative_instances && !cooperative_alone)
{
write(cooperative_pipe_fp, uri, strlen(uri));
write(cooperative_pipe_fp, "\n", 1);
- return;
+ return NULL;
}
c = malloc(sizeof(struct Client));
if (c->win == NULL)
{
c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
-
- capitalized_name = g_strdup(__NAME__);
- capitalized_name[0] = g_ascii_toupper(capitalized_name[0]);
- gtk_window_set_wmclass(GTK_WINDOW(c->win), __NAME__, capitalized_name);
- g_free(capitalized_name);
+ gtk_window_set_wmclass(GTK_WINDOW(c->win), __NAME__, __NAME_CAPITALIZED__);
}
/* When using Gtk2, it only shows a white area when run in suckless'
* to work. This is not needed when using Gtk3. */
gtk_window_set_default_size(GTK_WINDOW(c->win), 1024, 768);
- g_signal_connect(G_OBJECT(c->win), "destroy",
- G_CALLBACK(client_destroy), c);
+ g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c);
gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
c->web_view = webkit_web_view_new();
- webkit_web_view_set_full_content_zoom(WEBKIT_WEB_VIEW(c->web_view), TRUE);
+
+ /* XXX I really do want to enable this option. However, I get
+ * reproducable crashes with it enabled. I've seen bug reports from
+ * 2010 about this... WebKit crashes in libpixman, so maybe it's not
+ * a WebKit issue.
+ * Yeah, well. I'll turn it off for now. */
+ /*webkit_web_view_set_full_content_zoom(WEBKIT_WEB_VIEW(c->web_view), TRUE);*/
+
webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
g_signal_connect(G_OBJECT(c->web_view), "notify::title",
G_CALLBACK(changed_title), c);
G_CALLBACK(changed_uri), c);
g_signal_connect(G_OBJECT(c->web_view), "notify::load-status",
G_CALLBACK(changed_load_status), c);
- g_signal_connect(G_OBJECT(c->web_view),
- "new-window-policy-decision-requested",
+ g_signal_connect(G_OBJECT(c->web_view), "create-web-view",
G_CALLBACK(client_new_request), NULL);
+ g_signal_connect(G_OBJECT(c->web_view), "close-web-view",
+ G_CALLBACK(client_destroy_request), c);
g_signal_connect(G_OBJECT(c->web_view),
"mime-type-policy-decision-requested",
G_CALLBACK(download_request), NULL);
g_signal_connect(G_OBJECT(c->web_view), "download-requested",
- G_CALLBACK(download_wget), NULL);
+ G_CALLBACK(download_handle), NULL);
g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
G_CALLBACK(key_web_view), c);
- g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
+ g_signal_connect(G_OBJECT(c->web_view), "button-press-event",
G_CALLBACK(key_web_view), c);
g_signal_connect(G_OBJECT(c->web_view), "hovering-over-link",
G_CALLBACK(hover_web_view), c);
if (!language_is_set)
{
- g_object_set(webkit_get_default_session(), "accept-language", LANGUAGE,
- NULL);
+ g_object_set(webkit_get_default_session(), "accept-language",
+ accepted_language, NULL);
language_is_set = TRUE;
}
gtk_widget_grab_focus(c->web_view);
gtk_widget_show_all(c->win);
- f = ensure_url_scheme(uri);
- webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
- g_free(f);
+ if (uri != NULL)
+ {
+ f = ensure_url_scheme(uri);
+ if (show_all_requests)
+ fprintf(stderr, "====> %s\n", uri);
+ webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
+ g_free(f);
+ }
clients++;
+
+ return WEBKIT_WEB_VIEW(c->web_view);
}
-gboolean
-client_new_request(WebKitWebView *web_view, WebKitWebFrame *frame,
- WebKitNetworkRequest *request,
- WebKitWebNavigationAction *navigation_action,
- WebKitWebPolicyDecision *policy_decision, gpointer user_data)
+WebKitWebView *
+client_new_request(WebKitWebView *web_view, WebKitWebFrame *frame, gpointer data)
{
(void)web_view;
(void)frame;
- (void)navigation_action;
- (void)user_data;
-
- webkit_web_policy_decision_ignore(policy_decision);
- client_new(webkit_network_request_get_uri(request));
+ (void)data;
- return TRUE;
+ return client_new(NULL);
}
void
}
gboolean
-download_request(WebKitWebView *web_view, WebKitWebFrame *frame,
- WebKitNetworkRequest *request, gchar *mime_type,
- WebKitWebPolicyDecision *policy_decision, gpointer data)
+download_handle(WebKitWebView *web_view, WebKitDownload *download, gpointer data)
{
- (void)frame;
- (void)request;
+ gchar *path, *path2 = NULL, *uri;
+ gboolean ret;
+ int suffix = 1;
+
+ (void)web_view;
(void)data;
- if (!webkit_web_view_can_show_mime_type(web_view, mime_type))
+ path = g_build_filename(download_dir,
+ webkit_download_get_suggested_filename(download),
+ NULL);
+ path2 = g_strdup(path);
+ while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
{
- webkit_web_policy_decision_download(policy_decision);
- return TRUE;
+ g_free(path2);
+
+ path2 = g_strdup_printf("%s.%d", path, suffix);
+ suffix++;
}
- return FALSE;
+
+ if (suffix == 1000)
+ {
+ fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
+ ret = FALSE;
+ }
+ else
+ {
+ uri = g_filename_to_uri(path2, NULL, NULL);
+ webkit_download_set_destination_uri(download, uri);
+ ret = TRUE;
+ g_free(uri);
+ }
+
+ g_free(path);
+ g_free(path2);
+
+ return ret;
}
gboolean
-download_wget(WebKitWebView *web_view, WebKitDownload *download, gpointer data)
+download_request(WebKitWebView *web_view, WebKitWebFrame *frame,
+ WebKitNetworkRequest *request, gchar *mime_type,
+ WebKitWebPolicyDecision *policy_decision, gpointer data)
{
- const gchar *uri;
- char id[16] = "";
- gint ret;
-
- (void)web_view;
+ (void)frame;
+ (void)request;
(void)data;
- uri = webkit_download_get_uri(download);
- if (fork() == 0)
+ if (!webkit_web_view_can_show_mime_type(web_view, mime_type))
{
- chdir(DOWNLOAD_DIR);
- if (embed == 0)
- ret = execlp("xterm", "xterm", "-hold", "-e", "wget", uri, NULL);
- else
- {
- if (snprintf(id, 16, "%ld", embed) >= 16)
- {
- fprintf(stderr, __NAME__": id for xterm embed truncated!\n");
- exit(EXIT_FAILURE);
- }
- ret = execlp("xterm", "xterm", "-hold", "-into", id, "-e", "wget",
- uri, NULL);
- }
-
- if (ret == -1)
- {
- fprintf(stderr, __NAME__": exec'ing xterm for download");
- perror(" failed");
- exit(EXIT_FAILURE);
- }
+ webkit_web_policy_decision_download(policy_decision);
+ return TRUE;
}
-
return FALSE;
}
f = g_ascii_strdown(t, -1);
if (!g_str_has_prefix(f, "http:") &&
- !g_str_has_prefix(f, "https:") &&
- !g_str_has_prefix(f, "file:") &&
- !g_str_has_prefix(f, "about:"))
+ !g_str_has_prefix(f, "https:") &&
+ !g_str_has_prefix(f, "file:") &&
+ !g_str_has_prefix(f, "about:"))
{
g_free(f);
f = g_strdup_printf("http://%s", t);
}
void
-hover_web_view(WebKitWebView *web_view, gchar *title, gchar *uri,
- gpointer data)
+grab_environment_configuration(void)
+{
+ const gchar *e;
+
+ e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
+ if (e != NULL)
+ accepted_language = g_strdup(e);
+
+ e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
+ if (e != NULL)
+ download_dir = g_strdup(e);
+
+ e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
+ if (e != NULL)
+ global_zoom = atof(e);
+}
+
+void
+hover_web_view(WebKitWebView *web_view, gchar *title, gchar *uri, gpointer data)
{
struct Client *c = (struct Client *)data;
else
{
f = ensure_url_scheme(t);
+ if (show_all_requests)
+ fprintf(stderr, "====> %s\n", f);
webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
g_free(f);
}
struct Client *c = (struct Client *)data;
WebKitHitTestResultContext ht_context;
WebKitHitTestResult *ht_result = NULL;
- char *ht_uri = NULL;
+ gchar *ht_uri = NULL, *f;
(void)widget;
search(c, -1);
return TRUE;
case GDK_KEY_g:
- webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view),
- first_uri);
+ f = ensure_url_scheme(first_uri);
+ if (show_all_requests)
+ fprintf(stderr, "====> %s\n", f);
+ webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
+ g_free(f);
return TRUE;
}
}
gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Aborted.");
}
}
- else if (event->type == GDK_BUTTON_RELEASE)
+ else if (event->type == GDK_BUTTON_PRESS)
{
switch (((GdkEventButton *)event)->button)
{
GIOChannel *tabbed_stdout_channel;
GError *err = NULL;
gchar *output = NULL;
- char *argv[] = { "tabbed", "-c", "-d", "-n", __NAME__, NULL };
+ char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
Window plug_into;
if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
gtk_init(&argc, &argv);
- while ((opt = getopt(argc, argv, "z:e:rCT")) != -1)
+ grab_environment_configuration();
+
+ while ((opt = getopt(argc, argv, "e:rCT")) != -1)
{
switch (opt)
{
- case 'z':
- global_zoom = atof(optarg);
- break;
case 'e':
embed = atol(optarg);
tabbed_automagic = FALSE;