Communicate through a Unix socket instead of a pipe pair
This commit is contained in:
parent
f61cfc720c
commit
e92fd1aa5f
2 changed files with 65 additions and 57 deletions
58
surf.c
58
surf.c
|
@ -3,6 +3,7 @@
|
||||||
* To understand surf, start reading main().
|
* To understand surf, start reading main().
|
||||||
*/
|
*/
|
||||||
#include <sys/file.h>
|
#include <sys/file.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
|
@ -187,7 +188,7 @@ static gboolean buttonreleased(GtkWidget *w, GdkEvent *e, Client *c);
|
||||||
static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
|
static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
|
||||||
gpointer d);
|
gpointer d);
|
||||||
static gboolean winevent(GtkWidget *w, GdkEvent *e, Client *c);
|
static gboolean winevent(GtkWidget *w, GdkEvent *e, Client *c);
|
||||||
static gboolean readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused);
|
static gboolean readsock(GIOChannel *s, GIOCondition ioc, gpointer unused);
|
||||||
static void showview(WebKitWebView *v, Client *c);
|
static void showview(WebKitWebView *v, Client *c);
|
||||||
static GtkWidget *createwindow(Client *c);
|
static GtkWidget *createwindow(Client *c);
|
||||||
static gboolean loadfailedtls(WebKitWebView *v, gchar *uri,
|
static gboolean loadfailedtls(WebKitWebView *v, gchar *uri,
|
||||||
|
@ -253,7 +254,7 @@ static char *stylefile;
|
||||||
static const char *useragent;
|
static const char *useragent;
|
||||||
static Parameter *curconfig;
|
static Parameter *curconfig;
|
||||||
static int modparams[ParameterLast];
|
static int modparams[ParameterLast];
|
||||||
static int pipein[2], pipeout[2];
|
static int spair[2];
|
||||||
char *argv0;
|
char *argv0;
|
||||||
|
|
||||||
static ParamName loadtransient[] = {
|
static ParamName loadtransient[] = {
|
||||||
|
@ -359,13 +360,15 @@ setup(void)
|
||||||
|
|
||||||
gdkkb = gdk_seat_get_keyboard(gdk_display_get_default_seat(gdpy));
|
gdkkb = gdk_seat_get_keyboard(gdk_display_get_default_seat(gdpy));
|
||||||
|
|
||||||
if (pipe(pipeout) < 0 || pipe(pipein) < 0) {
|
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, spair) < 0) {
|
||||||
fputs("Unable to create pipes\n", stderr);
|
fputs("Unable to create sockets\n", stderr);
|
||||||
|
spair[0] = spair[1] = -1;
|
||||||
} else {
|
} else {
|
||||||
gchanin = g_io_channel_unix_new(pipein[0]);
|
gchanin = g_io_channel_unix_new(spair[0]);
|
||||||
g_io_channel_set_encoding(gchanin, NULL, NULL);
|
g_io_channel_set_encoding(gchanin, NULL, NULL);
|
||||||
|
g_io_channel_set_flags(gchanin, G_IO_FLAG_NONBLOCK, NULL);
|
||||||
g_io_channel_set_close_on_unref(gchanin, TRUE);
|
g_io_channel_set_close_on_unref(gchanin, TRUE);
|
||||||
g_io_add_watch(gchanin, G_IO_IN, readpipe, NULL);
|
g_io_add_watch(gchanin, G_IO_IN, readsock, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1054,8 +1057,8 @@ spawn(Client *c, const Arg *a)
|
||||||
if (fork() == 0) {
|
if (fork() == 0) {
|
||||||
if (dpy)
|
if (dpy)
|
||||||
close(ConnectionNumber(dpy));
|
close(ConnectionNumber(dpy));
|
||||||
close(pipein[0]);
|
close(spair[0]);
|
||||||
close(pipeout[1]);
|
close(spair[1]);
|
||||||
setsid();
|
setsid();
|
||||||
execvp(((char **)a->v)[0], (char **)a->v);
|
execvp(((char **)a->v)[0], (char **)a->v);
|
||||||
fprintf(stderr, "%s: execvp %s", argv0, ((char **)a->v)[0]);
|
fprintf(stderr, "%s: execvp %s", argv0, ((char **)a->v)[0]);
|
||||||
|
@ -1089,8 +1092,8 @@ cleanup(void)
|
||||||
while (clients)
|
while (clients)
|
||||||
destroyclient(clients);
|
destroyclient(clients);
|
||||||
|
|
||||||
close(pipein[0]);
|
close(spair[0]);
|
||||||
close(pipeout[1]);
|
close(spair[1]);
|
||||||
g_free(cookiefile);
|
g_free(cookiefile);
|
||||||
g_free(scriptfile);
|
g_free(scriptfile);
|
||||||
g_free(stylefile);
|
g_free(stylefile);
|
||||||
|
@ -1233,30 +1236,26 @@ newview(Client *c, WebKitWebView *rv)
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused)
|
readsock(GIOChannel *s, GIOCondition ioc, gpointer unused)
|
||||||
{
|
{
|
||||||
static char msg[MSGBUFSZ], msgsz;
|
static char msg[MSGBUFSZ];
|
||||||
GError *gerr = NULL;
|
GError *gerr = NULL;
|
||||||
|
gsize msgsz;
|
||||||
|
|
||||||
if (g_io_channel_read_chars(s, msg, sizeof(msg), NULL, &gerr) !=
|
if (g_io_channel_read_chars(s, msg, sizeof(msg), &msgsz, &gerr) !=
|
||||||
G_IO_STATUS_NORMAL) {
|
G_IO_STATUS_NORMAL) {
|
||||||
fprintf(stderr, "surf: error reading pipe: %s\n",
|
if (gerr) {
|
||||||
|
fprintf(stderr, "surf: error reading socket: %s\n",
|
||||||
gerr->message);
|
gerr->message);
|
||||||
g_error_free(gerr);
|
g_error_free(gerr);
|
||||||
|
}
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
if ((msgsz = msg[0]) < 3) {
|
if (msgsz < 2) {
|
||||||
fprintf(stderr, "surf: message too short: %d\n", msgsz);
|
fprintf(stderr, "surf: message too short: %d\n", msgsz);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (msg[2]) {
|
|
||||||
case 'i':
|
|
||||||
close(pipein[1]);
|
|
||||||
close(pipeout[0]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1265,10 +1264,10 @@ initwebextensions(WebKitWebContext *wc, Client *c)
|
||||||
{
|
{
|
||||||
GVariant *gv;
|
GVariant *gv;
|
||||||
|
|
||||||
if (!pipeout[0] || !pipein[1])
|
if (spair[1] < 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
gv = g_variant_new("(ii)", pipeout[0], pipein[1]);
|
gv = g_variant_new("i", spair[1]);
|
||||||
|
|
||||||
webkit_web_context_set_web_extensions_initialization_user_data(wc, gv);
|
webkit_web_context_set_web_extensions_initialization_user_data(wc, gv);
|
||||||
webkit_web_context_set_web_extensions_directory(wc, WEBEXTDIR);
|
webkit_web_context_set_web_extensions_directory(wc, WEBEXTDIR);
|
||||||
|
@ -1874,15 +1873,18 @@ msgext(Client *c, char type, const Arg *a)
|
||||||
static char msg[MSGBUFSZ];
|
static char msg[MSGBUFSZ];
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if ((ret = snprintf(msg, sizeof(msg), "%c%c%c%c",
|
if (spair[0] < 0)
|
||||||
4, c->pageid, type, a->i))
|
return;
|
||||||
|
|
||||||
|
if ((ret = snprintf(msg, sizeof(msg), "%c%c%c", c->pageid, type, a->i))
|
||||||
>= sizeof(msg)) {
|
>= sizeof(msg)) {
|
||||||
fprintf(stderr, "surf: message too long: %d\n", ret);
|
fprintf(stderr, "surf: message too long: %d\n", ret);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pipeout[1] && write(pipeout[1], msg, sizeof(msg)) < 0)
|
if (send(spair[0], msg, ret, 0) != ret)
|
||||||
fprintf(stderr, "surf: error sending: %.*s\n", ret-2, msg+2);
|
fprintf(stderr, "surf: error sending: %d%c%d (%dB)\n",
|
||||||
|
c->pageid, type, a->i, ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
|
#include <sys/socket.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include <gio/gio.h>
|
#include <gio/gio.h>
|
||||||
|
@ -18,7 +20,7 @@ typedef struct Page {
|
||||||
struct Page *next;
|
struct Page *next;
|
||||||
} Page;
|
} Page;
|
||||||
|
|
||||||
static int pipein, pipeout;
|
static int sock;
|
||||||
static Page *pages;
|
static Page *pages;
|
||||||
|
|
||||||
Page *
|
Page *
|
||||||
|
@ -47,60 +49,64 @@ msgsurf(Page *p, const char *s)
|
||||||
size_t sln = strlen(s);
|
size_t sln = strlen(s);
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if ((ret = snprintf(msg, sizeof(msg), "%c%c%s",
|
if ((ret = snprintf(msg, sizeof(msg), "%c%s", p ? p->id : 0, s))
|
||||||
2 + sln, p ? p->id : 0, s))
|
|
||||||
>= sizeof(msg)) {
|
>= sizeof(msg)) {
|
||||||
fprintf(stderr, "webext: message too long: %d\n", ret);
|
fprintf(stderr, "webext: msg: message too long: %d\n", ret);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pipeout && write(pipeout, msg, sizeof(msg)) < 0)
|
if (send(sock, msg, ret, 0) < 0)
|
||||||
fprintf(stderr, "webext: error sending: %.*s\n", ret-2, msg+2);
|
fprintf(stderr, "webext: error sending: %s\n", msg+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
readpipe(GIOChannel *s, GIOCondition c, gpointer unused)
|
readsock(GIOChannel *s, GIOCondition c, gpointer unused)
|
||||||
{
|
{
|
||||||
static char msg[MSGBUFSZ], msgsz;
|
static char msg[MSGBUFSZ];
|
||||||
WebKitDOMDOMWindow *view;
|
WebKitDOMDOMWindow *view;
|
||||||
GError *gerr = NULL;
|
GError *gerr = NULL;
|
||||||
|
gsize msgsz;
|
||||||
glong wh, ww;
|
glong wh, ww;
|
||||||
Page *p;
|
Page *p;
|
||||||
|
|
||||||
if (g_io_channel_read_chars(s, msg, LENGTH(msg), NULL, &gerr) !=
|
if (g_io_channel_read_chars(s, msg, LENGTH(msg), &msgsz, &gerr) !=
|
||||||
G_IO_STATUS_NORMAL) {
|
G_IO_STATUS_NORMAL) {
|
||||||
fprintf(stderr, "webext: error reading pipe: %s\n",
|
if (gerr) {
|
||||||
|
fprintf(stderr, "webext: error reading socket: %s\n",
|
||||||
gerr->message);
|
gerr->message);
|
||||||
g_error_free(gerr);
|
g_error_free(gerr);
|
||||||
|
}
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
if ((msgsz = msg[0]) < 3) {
|
|
||||||
fprintf(stderr, "webext: message too short: %d\n", msgsz);
|
if (msgsz < 2) {
|
||||||
|
fprintf(stderr, "webext: readsock: message too short: %d\n",
|
||||||
|
msgsz);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (p = pages; p; p = p->next) {
|
for (p = pages; p; p = p->next) {
|
||||||
if (p->id == msg[1])
|
if (p->id == msg[0])
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!p || !(view = webkit_dom_document_get_default_view(
|
if (!p || !(view = webkit_dom_document_get_default_view(
|
||||||
webkit_web_page_get_dom_document(p->webpage))))
|
webkit_web_page_get_dom_document(p->webpage))))
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
|
||||||
switch (msg[2]) {
|
switch (msg[1]) {
|
||||||
case 'h':
|
case 'h':
|
||||||
if (msgsz != 4)
|
if (msgsz != 3)
|
||||||
return TRUE;
|
return TRUE;
|
||||||
ww = webkit_dom_dom_window_get_inner_width(view);
|
ww = webkit_dom_dom_window_get_inner_width(view);
|
||||||
webkit_dom_dom_window_scroll_by(view,
|
webkit_dom_dom_window_scroll_by(view,
|
||||||
(ww / 100) * msg[3], 0);
|
(ww / 100) * msg[2], 0);
|
||||||
break;
|
break;
|
||||||
case 'v':
|
case 'v':
|
||||||
if (msgsz != 4)
|
if (msgsz != 3)
|
||||||
return TRUE;
|
return TRUE;
|
||||||
wh = webkit_dom_dom_window_get_inner_height(view);
|
wh = webkit_dom_dom_window_get_inner_height(view);
|
||||||
webkit_dom_dom_window_scroll_by(view,
|
webkit_dom_dom_window_scroll_by(view,
|
||||||
0, (wh / 100) * msg[3]);
|
0, (wh / 100) * msg[2]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,15 +122,15 @@ webpagecreated(WebKitWebExtension *e, WebKitWebPage *wp, gpointer unused)
|
||||||
G_MODULE_EXPORT void
|
G_MODULE_EXPORT void
|
||||||
webkit_web_extension_initialize_with_user_data(WebKitWebExtension *e, GVariant *gv)
|
webkit_web_extension_initialize_with_user_data(WebKitWebExtension *e, GVariant *gv)
|
||||||
{
|
{
|
||||||
GIOChannel *gchanpipe;
|
GIOChannel *gchansock;
|
||||||
|
|
||||||
g_signal_connect(e, "page-created", G_CALLBACK(webpagecreated), NULL);
|
g_signal_connect(e, "page-created", G_CALLBACK(webpagecreated), NULL);
|
||||||
|
|
||||||
g_variant_get(gv, "(ii)", &pipein, &pipeout);
|
g_variant_get(gv, "i", &sock);
|
||||||
msgsurf(NULL, "i");
|
|
||||||
|
|
||||||
gchanpipe = g_io_channel_unix_new(pipein);
|
gchansock = g_io_channel_unix_new(sock);
|
||||||
g_io_channel_set_encoding(gchanpipe, NULL, NULL);
|
g_io_channel_set_encoding(gchansock, NULL, NULL);
|
||||||
g_io_channel_set_close_on_unref(gchanpipe, TRUE);
|
g_io_channel_set_flags(gchansock, G_IO_FLAG_NONBLOCK, NULL);
|
||||||
g_io_add_watch(gchanpipe, G_IO_IN, readpipe, NULL);
|
g_io_channel_set_close_on_unref(gchansock, TRUE);
|
||||||
|
g_io_add_watch(gchansock, G_IO_IN, readsock, NULL);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue