removed reading from stdin in favor of Xproperties.

This commit is contained in:
Enno Boland (Gottox) 2009-06-05 17:46:11 +02:00
parent efdd0dd584
commit da509312df
1 changed files with 61 additions and 27 deletions

84
surf.c
View File

@ -1,4 +1,8 @@
#include <X11/X.h>
#include <X11/Xatom.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <gdk/gdk.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
@ -18,12 +22,16 @@
#define ARGCHR() (*_a) #define ARGCHR() (*_a)
#define ARGC() _c #define ARGC() _c
Display *dpy;
Atom urlprop;
GtkWidget *win; GtkWidget *win;
GtkWidget *browser; GtkWidget *browser;
WebKitWebView *view; WebKitWebView *view;
gchar *title; gchar *title;
gint progress = 100; gint progress = 100;
gboolean embed = FALSE; gboolean embed = FALSE;
gboolean showxid = FALSE;
gboolean ignore_once = FALSE;
static void setup(void); static void setup(void);
static void cleanup(void); static void cleanup(void);
@ -42,31 +50,36 @@ static gboolean newwindow(WebKitWebView *view, WebKitWebFrame *f,
static gboolean download(WebKitWebView *view, GObject *o, gpointer d); static gboolean download(WebKitWebView *view, GObject *o, gpointer d);
static void loaduri(gchar *uri); static void loaduri(gchar *uri);
static void loadfile(gchar *f); static void loadfile(gchar *f);
static void setupstdin(); static void setupx();
static gboolean readstdin(GIOChannel *c, GIOCondition con); GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer data);
gboolean GdkFilterReturn
readstdin(GIOChannel *c, GIOCondition con) { processx(GdkXEvent *e, GdkEvent *event, gpointer data) {
gchar *line, *p; XPropertyEvent *ev;
GIOStatus ret; Atom adummy;
int idummy;
ret = g_io_channel_read_line(c, &line, NULL, NULL, NULL); unsigned long ldummy;
if(ret == G_IO_STATUS_ERROR || ret == G_IO_STATUS_EOF) unsigned char *buf = NULL;
return FALSE; if(((XEvent *)e)->type == PropertyNotify) {
for(p = line; *p && *p != '\n'; p++); ev = &((XEvent *)e)->xproperty;
*p = '\0'; if(ignore_once == FALSE && ev->atom == urlprop && ev->state == PropertyNewValue) {
loaduri(line); XGetWindowProperty(dpy, ev->window, urlprop, 0L, BUFSIZ, False, XA_STRING,
g_free(line); &adummy, &idummy, &ldummy, &ldummy, &buf);
return TRUE; loaduri((gchar *)buf);
XFree(buf);
return GDK_FILTER_REMOVE;
}
ignore_once = FALSE;
}
return GDK_FILTER_CONTINUE;
} }
void void
setupstdin() { setupx() {
GIOChannel *c = NULL; dpy = GDK_WINDOW_XDISPLAY(GTK_WIDGET(win)->window);
urlprop = XInternAtom(dpy, "_SURF_URL", False);
c = g_io_channel_unix_new(STDIN_FILENO); gdk_window_add_filter(GTK_WIDGET(win)->window, processx, NULL);
if(c && !g_io_add_watch(c, G_IO_IN|G_IO_HUP, (GIOFunc) readstdin, NULL)) gdk_window_set_events(GTK_WIDGET(win)->window, GDK_ALL_EVENTS_MASK);
g_error("Stdin: could not add watch\n");
} }
void void
@ -89,6 +102,7 @@ loadfile(gchar *f) {
webkit_web_view_load_html_string(view, code->str, NULL); webkit_web_view_load_html_string(view, code->str, NULL);
g_io_channel_shutdown(c, FALSE, NULL); g_io_channel_shutdown(c, FALSE, NULL);
} }
} }
static void loaduri(gchar *uri) { static void loaduri(gchar *uri) {
@ -119,12 +133,18 @@ linkhover(WebKitWebView* page, const gchar* t, const gchar* l, gpointer d) {
void void
loadstart(WebKitWebView *view, WebKitWebFrame *f, gpointer d) { loadstart(WebKitWebView *view, WebKitWebFrame *f, gpointer d) {
/* ??? TODO */
} }
void void
loadcommit(WebKitWebView *view, WebKitWebFrame *f, gpointer d) { loadcommit(WebKitWebView *view, WebKitWebFrame *f, gpointer d) {
/* ??? TODO */ gchar *uri;
if(!(uri = (gchar *)webkit_web_view_get_uri(view)))
uri = "(null)";
ignore_once = TRUE;
XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(win)->window), urlprop,
XA_STRING, 8, PropModeReplace, (unsigned char *)uri,
strlen(uri) + 1);
} }
void void
@ -141,6 +161,9 @@ progresschange(WebKitWebView* view, gint p, gpointer d) {
void void
updatetitle() { updatetitle() {
char t[512]; char t[512];
if(progress == 100)
snprintf(t, LENGTH(t), "%s", title);
else
snprintf(t, LENGTH(t), "%s [%i%%]", title, progress); snprintf(t, LENGTH(t), "%s [%i%%]", title, progress);
gtk_window_set_title(GTK_WINDOW(win), t); gtk_window_set_title(GTK_WINDOW(win), t);
} }
@ -165,9 +188,14 @@ keypress(GtkWidget* w, GdkEventKey *ev) {
} }
void setup(void) { void setup(void) {
if(embed) {
win = gtk_plug_new(0);
}
else {
win = gtk_window_new(GTK_WINDOW_TOPLEVEL); win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_wmclass(GTK_WINDOW(win), "surf", "surf");
}
gtk_window_set_default_size(GTK_WINDOW(win), 800, 600); gtk_window_set_default_size(GTK_WINDOW(win), 800, 600);
gtk_widget_set_name(win, "surf window");
browser = gtk_scrolled_window_new(NULL, NULL); browser = gtk_scrolled_window_new(NULL, NULL);
g_signal_connect (G_OBJECT(win), "destroy", G_CALLBACK(windestroy), NULL); g_signal_connect (G_OBJECT(win), "destroy", G_CALLBACK(windestroy), NULL);
g_signal_connect (G_OBJECT(win), "key-press-event", G_CALLBACK(keypress), NULL); g_signal_connect (G_OBJECT(win), "key-press-event", G_CALLBACK(keypress), NULL);
@ -190,6 +218,8 @@ void setup(void) {
gtk_container_add(GTK_CONTAINER(win), browser); gtk_container_add(GTK_CONTAINER(win), browser);
gtk_widget_grab_focus(GTK_WIDGET(view)); gtk_widget_grab_focus(GTK_WIDGET(view));
gtk_widget_show_all(win); gtk_widget_show_all(win);
if(showxid)
printf("%u\n", (unsigned int)GDK_WINDOW_XID(GTK_WIDGET(win)->window));
} }
void cleanup() { void cleanup() {
@ -199,7 +229,11 @@ int main(int argc, char *argv[]) {
gchar *uri = NULL, *file = NULL; gchar *uri = NULL, *file = NULL;
ARG { ARG {
case 'x':
showxid = TRUE;
break;
case 'e': case 'e':
showxid = TRUE;
embed = TRUE; embed = TRUE;
break; break;
case 'u': case 'u':
@ -222,11 +256,11 @@ int main(int argc, char *argv[]) {
if (!g_thread_supported()) if (!g_thread_supported())
g_thread_init(NULL); g_thread_init(NULL);
setup(); setup();
setupx();
if(uri) if(uri)
loaduri(uri); loaduri(uri);
else if(file) else if(file)
loadfile(file); loadfile(file);
setupstdin();
updatetitle(); updatetitle();
gtk_main(); gtk_main();
cleanup(); cleanup();