X-Git-Url: https://git.danieliu.xyz/?p=surf.git;a=blobdiff_plain;f=surf.c;h=a4465d425811f178373f8028a9ad8395fb908503;hp=d48fbc98655248b3f25bced8f9f00ff01e04c3bc;hb=6b724044eef4bcd728fa0926e0b9e869bc4f4d9c;hpb=1901359efa10fe2e18794df34fc33b81da03a6f5 diff --git a/surf.c b/surf.c index d48fbc9..a4465d4 100644 --- a/surf.c +++ b/surf.c @@ -128,6 +128,12 @@ typedef struct { unsigned int stopevent; } Button; + +typedef struct { + char *token; + char *uri; +} SearchEngine; + typedef struct { const char *uri; Parameter config[ParameterLast]; @@ -175,6 +181,7 @@ static void spawn(Client *c, const Arg *a); static void msgext(Client *c, char type, const Arg *a); static void destroyclient(Client *c); static void cleanup(void); +static void updatehistory(const char *u, const char *t); /* GTK/WebKit */ static WebKitWebView *newview(Client *c, WebKitWebView *rv); @@ -214,6 +221,7 @@ static void webprocessterminated(WebKitWebView *v, Client *c); static void closeview(WebKitWebView *v, Client *c); static void destroywin(GtkWidget* w, Client *c); +static gchar *parseuri(const gchar *uri); /* Hotkeys */ static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d); @@ -231,6 +239,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 playexternal(Client *c, const Arg *a); /* Buttons */ static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h); @@ -336,10 +345,11 @@ setup(void) curconfig = defconfig; /* dirs and files */ - cookiefile = buildfile(cookiefile); - scriptfile = buildfile(scriptfile); - cachedir = buildpath(cachedir); - certdir = buildpath(certdir); + cookiefile = buildfile(cookiefile); + historyfile = buildfile(historyfile); + scriptfile = buildfile(scriptfile); + cachedir = buildpath(cachedir); + certdir = buildpath(certdir); gdkkb = gdk_seat_get_keyboard(gdk_display_get_default_seat(gdpy)); @@ -559,7 +569,7 @@ loaduri(Client *c, const Arg *a) url = g_strdup_printf("file://%s", path); free(path); } else { - url = g_strdup_printf("http://%s", uri); + url = parseuri(uri); } if (apath != uri) free(apath); @@ -1076,12 +1086,28 @@ cleanup(void) close(pipein[0]); close(pipeout[1]); g_free(cookiefile); + g_free(historyfile); g_free(scriptfile); g_free(stylefile); g_free(cachedir); XCloseDisplay(dpy); } +void +updatehistory(const char *u, const char *t) +{ + FILE *f; + f = fopen(historyfile, "a+"); + + char b[20]; + time_t now = time (0); + strftime (b, 20, "%Y-%m-%d %H:%M:%S", localtime (&now)); + fputs(b, f); + + fprintf(f, " %s %s\n", u, t); + fclose(f); +} + WebKitWebView * newview(Client *c, WebKitWebView *rv) { @@ -1209,20 +1235,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]); @@ -1489,6 +1517,7 @@ loadfailedtls(WebKitWebView *v, gchar *uri, GTlsCertificate *cert, return TRUE; } + void loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c) { @@ -1517,6 +1546,7 @@ loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c) break; case WEBKIT_LOAD_FINISHED: seturiparameters(c, uri, loadfinished); + updatehistory(uri, c->title); /* Disabled until we write some WebKitWebExtension for * manipulating the DOM directly. evalscript(c, "document.documentElement.style.overflow = '%s'", @@ -1763,6 +1793,22 @@ destroywin(GtkWidget* w, Client *c) gtk_main_quit(); } +gchar * +parseuri(const gchar *uri) { + guint i; + + for (i = 0; i < LENGTH(searchengines); i++) { + if (searchengines[i].token == NULL || searchengines[i].uri == NULL || + *(uri + strlen(searchengines[i].token)) != ' ') + continue; + if (g_str_has_prefix(uri, searchengines[i].token)) + return g_strdup_printf(searchengines[i].uri, + uri + strlen(searchengines[i].token) + 1); + } + + return g_strdup_printf("http://%s", uri); +} + void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) { @@ -1843,12 +1889,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 @@ -1963,6 +2015,15 @@ clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h) spawn(c, &arg); } +void +playexternal(Client *c, const Arg *a) +{ + Arg arg; + + arg = (Arg)VIDEOPLAY(geturi(c)); + spawn(c, &arg); +} + int main(int argc, char *argv[]) { @@ -2103,7 +2164,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);