Skip to content

Instantly share code, notes, and snippets.

@arjan
Last active May 24, 2016 20:53
Show Gist options
  • Save arjan/14e398dfa426b60d865132df3c00cec7 to your computer and use it in GitHub Desktop.
Save arjan/14e398dfa426b60d865132df3c00cec7 to your computer and use it in GitHub Desktop.
Load webkit url from stdin (note: does not compile, meant as example code)
#include <unistd.h>
#include <stdio.h>
#include <glib.h>
#include <string.h>
#define nullptr NULL
GMainLoop* loop;
void rstrip(char *string) {
int l;
if (!string)
return;
l = strlen(string) - 1;
while (g_ascii_isspace(string[l]) && l >= 0) {
string[l--] = 0;
}
}
void lstrip(char *string) {
int i, l;
if (!string)
return;
l = strlen(string);
while (g_ascii_isspace(string[(i = 0)])) {
while(i++ < l) {
string[i-1] = string[i];
}
}
}
void strip(char *string) {
lstrip(string);
rstrip(string);
}
static gboolean
io_callback (GIOChannel * io, GIOCondition condition, gpointer data)
{
GError *error = NULL;
gchar *value;
char *url;
WKViewRef *webView = (WKViewRef *)data;
switch (g_io_channel_read_line (io, &value, NULL, NULL, &error)) {
case G_IO_STATUS_NORMAL:
if (!g_ascii_strncasecmp("LOAD ", value, 5)) {
url = value + 5;
strip(url);
WKPageLoadURL(WKViewGetPage(webView), WKURLCreateWithURL(url));
g_print("OK\n");
} else {
g_print("ERROR: Invalid command\n");
}
g_free(value);
return TRUE;
case G_IO_STATUS_ERROR:
g_print("ERROR: IO error: %s\n", error->message);
g_error_free (error);
return FALSE;
case G_IO_STATUS_EOF:
g_print("ERROR: No input data available\n");
return TRUE;
case G_IO_STATUS_AGAIN:
return TRUE;
default:
g_return_val_if_reached (FALSE);
break;
}
return FALSE;
}
void setup_stdin(WKViewRef *webView) {
GIOChannel *io = NULL;
guint io_watch_id = 0;
/* standard input callback */
io = g_io_channel_unix_new (STDIN_FILENO);
io_watch_id = g_io_add_watch (io, G_IO_IN, io_callback, webView);
g_io_channel_unref (io);
}
int main(int argc, char** argv) {
loop = g_main_loop_new(nullptr, FALSE);
setup_stdin();
g_main_loop_run(loop);
g_main_loop_unref(loop);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment