From a08af8e729bd9967cd2d5741f96dd0f0964a8e08 Mon Sep 17 00:00:00 2001 From: Peter Hofmann Date: Sat, 14 Jun 2014 19:58:20 +0200 Subject: [PATCH] Cooperative instances --- README | 1 + zea.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/README b/README index 44f4389..c01cb45 100644 --- a/README +++ b/README @@ -15,6 +15,7 @@ Features: - Searching the current page for a word - Adblock - Support for Flash and Java + - Cooperative instances using FIFOs (can be turned off) Planned features: diff --git a/zea.c b/zea.c index 679979d..91b2d95 100644 --- a/zea.c +++ b/zea.c @@ -1,13 +1,19 @@ #include #include +#include +#include +#include +#include #include #include #include +#include #include #define DOWNLOAD_DIR "/tmp/tmp" +#define ZEA_FIFO "/tmp/zea.fifo" static void zea_adblock(WebKitWebView *, WebKitWebFrame *, WebKitWebResource *, @@ -26,7 +32,9 @@ static gboolean zea_new_client_request(WebKitWebView *, WebKitWebFrame *, WebKitNetworkRequest *, WebKitWebNavigationAction *, WebKitWebPolicyDecision *, gpointer); +static gboolean zea_remote_msg(GIOChannel *, GIOCondition, gpointer); static void zea_search(gpointer, gint); +static void zea_setup_cooperation(void); static void zea_scroll(GtkAdjustment *, gint, gdouble); static void zea_title_changed(GObject *, GParamSpec *, gpointer); static void zea_uri_changed(GObject *, GParamSpec *, gpointer); @@ -41,6 +49,9 @@ static gchar *search_text = NULL; static gchar *first_uri = NULL; static gboolean show_all_requests = FALSE; static GSList *adblock_patterns = NULL; +static gboolean cooperative_instances = TRUE; +static int cooperative_pipe_fp = 0; +static gboolean alone = TRUE; struct Client @@ -195,6 +206,13 @@ zea_location_key(GtkWidget *widget, GdkEvent *event, gpointer data) void zea_new_client(const gchar *uri) { + if (cooperative_instances && !alone) + { + write(cooperative_pipe_fp, uri, strlen(uri)); + write(cooperative_pipe_fp, "\n", 1); + return; + } + struct Client *c = malloc(sizeof(struct Client)); if (!c) { @@ -288,6 +306,24 @@ zea_new_client_request(WebKitWebView *web_view, WebKitWebFrame *frame, return TRUE; } +gboolean +zea_remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data) +{ + gchar *uri = NULL; + + (void)condition; + (void)data; + + g_io_channel_read_line(channel, &uri, NULL, NULL, NULL); + if (uri) + { + g_strstrip(uri); + zea_new_client(uri); + g_free(uri); + } + return TRUE; +} + void zea_search(gpointer data, gint direction) { @@ -300,6 +336,33 @@ zea_search(gpointer data, gint direction) FALSE, direction == 1, TRUE); } +void +zea_setup_cooperation(void) +{ + GIOChannel *towatch; + + if (!g_file_test(ZEA_FIFO, G_FILE_TEST_EXISTS)) + mkfifo(ZEA_FIFO, 0600); + + cooperative_pipe_fp = open(ZEA_FIFO, O_WRONLY | O_NONBLOCK); + if (!cooperative_pipe_fp) + { + fprintf(stderr, "zea: Can't open FIFO at all.\n"); + return; + } + + if (write(cooperative_pipe_fp, "", 0) == -1) + { + /* Could not do an empty write to the FIFO which means there's + * no one listening. */ + close(cooperative_pipe_fp); + towatch = g_io_channel_new_file(ZEA_FIFO, "r+", NULL); + g_io_add_watch(towatch, G_IO_IN, (GIOFunc)zea_remote_msg, NULL); + } + else + alone = FALSE; +} + void zea_scroll(GtkAdjustment *a, gint step_type, gdouble factor) { @@ -502,7 +565,7 @@ main(int argc, char **argv) gtk_init(&argc, &argv); - while ((opt = getopt(argc, argv, "z:e:R")) != -1) + while ((opt = getopt(argc, argv, "z:e:Rs")) != -1) { switch (opt) { @@ -515,6 +578,9 @@ main(int argc, char **argv) case 'R': show_all_requests = TRUE; break; + case 's': + cooperative_instances = FALSE; + break; } } @@ -525,10 +591,12 @@ main(int argc, char **argv) } zea_load_adblock(); + zea_setup_cooperation(); first_uri = g_strdup(argv[optind]); for (i = optind; i < argc; i++) zea_new_client(argv[i]); - gtk_main(); + if (!cooperative_instances || alone) + gtk_main(); exit(EXIT_SUCCESS); } -- 2.39.2