X-Git-Url: https://git.danieliu.xyz/?p=surf.git;a=blobdiff_plain;f=surf.c;h=9d2dacb8e8a7da84cdd12881d189186d72b03011;hp=25940f9f8d241c6b846689355ad1ca5b40cc8c1a;hb=c6f48298df2ff2c4a3c7bf2948bbc5fad7628ebc;hpb=7ea0c2f7f8c5cc4616d8dc0676f7b4b59351667b diff --git a/surf.c b/surf.c index 25940f9..9d2dacb 100644 --- a/surf.c +++ b/surf.c @@ -30,12 +30,12 @@ #include #include "arg.h" +#include "common.h" #define LENGTH(x) (sizeof(x) / sizeof(x[0])) #define CLEANMASK(mask) (mask & (MODKEY|GDK_SHIFT_MASK)) -#define MSGBUFSZ 32 -enum { AtomFind, AtomGo, AtomUri, AtomLast }; +enum { AtomFind, AtomSearch, AtomGo, AtomUri, AtomLast }; enum { OnDoc = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT, @@ -142,7 +142,6 @@ typedef struct { /* Surf */ static void usage(void); -static void die(const char *errstr, ...); static void setup(void); static void sigchld(int unused); static void sighup(int unused); @@ -232,6 +231,7 @@ static void togglefullscreen(Client *c, const Arg *a); static void togglecookiepolicy(Client *c, const Arg *a); static void toggleinspector(Client *c, const Arg *a); static void find(Client *c, const Arg *a); +static void search(Client *c, const Arg *a); /* Buttons */ static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h); @@ -310,17 +310,6 @@ usage(void) "[-r scriptfile] [-u useragent] [-z zoomlevel] [uri]\n"); } -void -die(const char *errstr, ...) -{ - va_list ap; - - va_start(ap, errstr); - vfprintf(stderr, errstr, ap); - va_end(ap); - exit(1); -} - void setup(void) { @@ -338,6 +327,7 @@ setup(void) /* atoms */ atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False); + atoms[AtomSearch] = XInternAtom(dpy, "_SURF_SEARCH", False); atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False); atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False); @@ -589,6 +579,19 @@ loaduri(Client *c, const Arg *a) g_free(url); } +void +search(Client *c, const Arg *a) +{ + Arg arg; + char *url; + + url = g_strdup_printf(searchurl, a->v); + arg.v = url; + loaduri(c, &arg); + + g_free(url); +} + const char * geturi(Client *c) { @@ -1221,20 +1224,22 @@ newview(Client *c, WebKitWebView *rv) static gboolean readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused) { - char msg[MSGBUFSZ]; - gsize msgsz; + static char msg[MSGBUFSZ], msgsz; GError *gerr = NULL; - if (g_io_channel_read_chars(s, msg, sizeof(msg), &msgsz, &gerr) != + if (g_io_channel_read_chars(s, msg, sizeof(msg), NULL, &gerr) != G_IO_STATUS_NORMAL) { fprintf(stderr, "surf: error reading pipe: %s\n", gerr->message); g_error_free(gerr); return TRUE; } - msg[msgsz] = '\0'; + if ((msgsz = msg[0]) < 3) { + fprintf(stderr, "surf: message too short: %d\n", msgsz); + return TRUE; + } - switch (msg[1]) { + switch (msg[2]) { case 'i': close(pipein[1]); close(pipeout[0]); @@ -1321,6 +1326,9 @@ processx(GdkXEvent *e, GdkEvent *event, gpointer d) find(c, NULL); return GDK_FILTER_REMOVE; + } else if (ev->atom == atoms[AtomSearch]) { + a.v = getatom(c, AtomSearch); + search(c, &a); } else if (ev->atom == atoms[AtomGo]) { a.v = getatom(c, AtomGo); loaduri(c, &a); @@ -1855,12 +1863,18 @@ zoom(Client *c, const Arg *a) static void msgext(Client *c, char type, const Arg *a) { - char msg[MSGBUFSZ] = { c->pageid, type, a->i, '\0' }; + static char msg[MSGBUFSZ]; + int ret; - if (pipeout[1]) { - if (write(pipeout[1], msg, sizeof(msg)) < 0) - fprintf(stderr, "surf: error sending: %s\n", msg); + if ((ret = snprintf(msg, sizeof(msg), "%c%c%c%c", + 4, c->pageid, type, a->i)) + >= sizeof(msg)) { + fprintf(stderr, "surf: message too long: %d\n", ret); + return; } + + if (pipeout[1] && write(pipeout[1], msg, sizeof(msg)) < 0) + fprintf(stderr, "surf: error sending: %.*s\n", ret-2, msg+2); } void @@ -2115,7 +2129,11 @@ main(int argc, char *argv[]) if (argc > 0) arg.v = argv[0]; else +#ifdef HOMEPAGE + arg.v = HOMEPAGE; +#else arg.v = "about:blank"; +#endif setup(); c = newclient(NULL);