removed unneeded event listeners and reworked file opener.
[surf.git] / surf.c
1 #include <X11/X.h>
2 #include <X11/Xatom.h>
3 #include <gtk/gtk.h>
4 #include <gdk/gdkx.h>
5 #include <gdk/gdk.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <webkit/webkit.h>
11
12 #define LENGTH(x) (sizeof x / sizeof x[0])
13 /* Plan9-style Argument parsing */
14 /* Vars: _c -> count; _b -> break; _a -> argument */
15 #define ARG int _c, _b; char *_a; \
16         for(_c = 1; _c < argc && argv[_c][0] == '-' && argv[_c][1] && \
17                         (strcmp(argv[_c], "--") != 0); _c++) \
18                 for(_a = &argv[_c][1], _b = 0; !_b && *_a; _a++ ) \
19                         switch(*_a)
20 #define ARGVAL()        (!_b && _a[1] && (_b = 1) ? &_a[1] : _c + 1 == argc ? \
21                 0 : argv[++_c])
22 #define ARGCHR()        (*_a)
23 #define ARGC()          _c
24
25 Display *dpy;
26 Atom urlprop;
27 GtkWidget *win;
28 GtkWidget *browser;
29 WebKitWebView *view;
30 gchar *title;
31 gint progress = 100;
32 gboolean embed = FALSE;
33 gboolean showxid = FALSE;
34 gboolean ignore_once = FALSE;
35
36 static void setup(void);
37 static void cleanup(void);
38 static void updatetitle(void);
39 static void windestroy(GtkWidget* w, gpointer d);
40 static gboolean keypress(GtkWidget* w, GdkEventKey *ev);
41 static void titlechange(WebKitWebView* view, WebKitWebFrame* frame, const gchar* title, gpointer d);
42 static void progresschange(WebKitWebView *view, gint p, gpointer d);
43 static void loadcommit(WebKitWebView *view, WebKitWebFrame *f, gpointer d);
44 static void loadstart(WebKitWebView *view, WebKitWebFrame *f, gpointer d);
45 static void loadfinish(WebKitWebView *view, WebKitWebFrame *f, gpointer d);
46 static void linkhover(WebKitWebView* page, const gchar* t, const gchar* l, gpointer d);
47 static gboolean newwindow(WebKitWebView *view, WebKitWebFrame *f,
48                 WebKitNetworkRequest *r, WebKitWebNavigationAction *n,
49                 WebKitWebPolicyDecision *p, gpointer d);
50 static gboolean download(WebKitWebView *view, GObject *o, gpointer d);
51 static void loaduri(gchar *uri);
52 static void loadfile(gchar *f);
53 static void setupx();
54 GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer data);
55
56 GdkFilterReturn
57 processx(GdkXEvent *e, GdkEvent *event, gpointer data) {
58         XPropertyEvent *ev;
59         Atom adummy;
60         int idummy;
61         unsigned long ldummy;
62         unsigned char *buf = NULL;
63         if(((XEvent *)e)->type == PropertyNotify) {
64                 ev = &((XEvent *)e)->xproperty;
65                 if(ignore_once == FALSE && ev->atom == urlprop && ev->state == PropertyNewValue) {
66                         XGetWindowProperty(dpy, ev->window, urlprop, 0L, BUFSIZ, False, XA_STRING, 
67                         &adummy, &idummy, &ldummy, &ldummy, &buf);
68                         loaduri((gchar *)buf);
69                         XFree(buf);
70                         return GDK_FILTER_REMOVE;
71                 }
72                 ignore_once = FALSE;
73         }
74         return GDK_FILTER_CONTINUE;
75 }
76
77 void
78 setupx() {
79         dpy = GDK_WINDOW_XDISPLAY(GTK_WIDGET(win)->window);
80         urlprop = XInternAtom(dpy, "_SURF_URL", False);
81         gdk_window_add_filter(GTK_WIDGET(win)->window, processx, NULL);
82         gdk_window_set_events(GTK_WIDGET(win)->window, GDK_ALL_EVENTS_MASK);
83 }
84
85 void
86 loadfile(gchar *f) {
87         GIOChannel *c = NULL;
88         GError *e = NULL;
89         GString *code = g_string_new("");
90         GString *uri = g_string_new(f);
91         gchar *line;
92
93         /* cannot use fileno in c99 - workaround*/
94         if(strcmp(f, "-") == 0) {
95                 c = g_io_channel_unix_new(STDIN_FILENO);
96                 if (c) {
97                         while(g_io_channel_read_line(c, &line, NULL, NULL, &e) == G_IO_STATUS_NORMAL) {
98                                 g_string_append(code, line);
99                                 g_free(line);
100                         }
101                         webkit_web_view_load_html_string(view, code->str, NULL);
102                         g_io_channel_shutdown(c, FALSE, NULL);
103                 }
104         }
105         else {
106                 g_string_prepend(uri, "file://");
107                 loaduri(uri->str);
108         }
109         
110 }
111
112 static void loaduri(gchar *uri) {
113         GString* u = g_string_new(uri);
114         if(g_strrstr(u->str, "://") == NULL)
115                 g_string_prepend(u, "http://");
116         webkit_web_view_load_uri(view, u->str);
117         g_string_free(u, TRUE);
118 }
119
120 gboolean
121 download(WebKitWebView *view, GObject *o, gpointer d) {
122         /* TODO */
123         return FALSE;
124 }
125
126 gboolean
127 newwindow(WebKitWebView *view, WebKitWebFrame *f,
128                 WebKitNetworkRequest *r, WebKitWebNavigationAction *n,
129                 WebKitWebPolicyDecision *p, gpointer d) {
130         /* TODO */
131         return FALSE;
132 }
133 void
134 linkhover(WebKitWebView* page, const gchar* t, const gchar* l, gpointer d) {
135         /* TODO */
136 }
137
138 void
139 loadcommit(WebKitWebView *view, WebKitWebFrame *f, gpointer d) {
140         gchar *uri;
141
142         if(!(uri = (gchar *)webkit_web_view_get_uri(view)))
143                 uri = "(null)";
144         ignore_once = TRUE;
145         XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(win)->window), urlprop,
146                         XA_STRING, 8, PropModeReplace, (unsigned char *)uri,
147                         strlen(uri) + 1);
148 }
149
150 void
151 progresschange(WebKitWebView* view, gint p, gpointer d) {
152         progress = p;
153         updatetitle();
154 }
155
156 void
157 updatetitle() {
158         char t[512];
159         if(progress == 100)
160                 snprintf(t, LENGTH(t), "%s", title);
161         else
162                 snprintf(t, LENGTH(t), "%s [%i%%]", title, progress);
163         gtk_window_set_title(GTK_WINDOW(win), t);
164 }
165
166 void
167 titlechange(WebKitWebView *v, WebKitWebFrame *f, const gchar *t, gpointer d) {
168         if(title)
169                 g_free(title);
170         title = g_strdup(t);
171         updatetitle();
172 }
173
174 void
175 windestroy(GtkWidget* w, gpointer d) {
176         gtk_main_quit();
177 }
178
179 gboolean
180 keypress(GtkWidget* w, GdkEventKey *ev) {
181         /* TODO */
182         return FALSE;
183 }
184
185 void setup(void) {
186         if(embed) {
187                 win = gtk_plug_new(0);
188         }
189         else {
190                 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
191                 gtk_window_set_wmclass(GTK_WINDOW(win), "surf", "surf");
192         }
193         gtk_window_set_default_size(GTK_WINDOW(win), 800, 600);
194         browser = gtk_scrolled_window_new(NULL, NULL);
195         g_signal_connect (G_OBJECT(win), "destroy", G_CALLBACK(windestroy), NULL);
196         g_signal_connect (G_OBJECT(win), "key-press-event", G_CALLBACK(keypress), NULL);
197
198         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(browser),
199                         GTK_POLICY_NEVER, GTK_POLICY_NEVER);
200         view = WEBKIT_WEB_VIEW(webkit_web_view_new());
201         gtk_container_add(GTK_CONTAINER(browser), GTK_WIDGET(view));
202
203         g_signal_connect(G_OBJECT(view), "title-changed", G_CALLBACK(titlechange), view);
204         g_signal_connect(G_OBJECT(view), "load-progress-changed", G_CALLBACK(progresschange), view);
205         g_signal_connect(G_OBJECT(view), "load-committed", G_CALLBACK(loadcommit), view);
206         g_signal_connect(G_OBJECT(view), "hovering-over-link", G_CALLBACK(linkhover), view);
207         g_signal_connect(G_OBJECT(view), "new-window-policy-decision-requested", G_CALLBACK(newwindow), view);
208         g_signal_connect(G_OBJECT(view), "download-requested", G_CALLBACK(download), view);
209         /* g_signal_connect(G_OBJECT(view), "create-web-view", G_CALLBACK(createwebview), view); */
210
211         gtk_container_add(GTK_CONTAINER(win), browser);
212         gtk_widget_grab_focus(GTK_WIDGET(view));
213         gtk_widget_show_all(win);
214         if(showxid)
215                 printf("%u\n", (unsigned int)GDK_WINDOW_XID(GTK_WIDGET(win)->window));
216 }
217
218 void cleanup() {
219 }
220
221 int main(int argc, char *argv[]) {
222         gchar *uri = NULL, *file = NULL;
223
224         ARG {
225         case 'x':
226                 showxid = TRUE;
227                 break;
228         case 'e':
229                 showxid = TRUE;
230                 embed = TRUE;
231                 break;
232         case 'u':
233                 if(!(uri = ARGVAL()))
234                         goto argerr;
235                 break;
236         case 'f':
237                 if(!(file = ARGVAL()))
238                         goto argerr;
239                 break;
240         argerr:
241         default:
242                 puts("surf - simple browser");
243                 printf("usage: %s [-e] [-u uri] [-f file]", argv[0]);
244                 return EXIT_FAILURE;
245         }
246         if(argc != ARGC())
247                 goto argerr;
248         gtk_init(NULL, NULL);
249         if (!g_thread_supported())
250                 g_thread_init(NULL);
251         setup();
252         setupx();
253         if(uri)
254                 loaduri(uri);
255         else if(file)
256                 loadfile(file);
257         updatetitle();
258         gtk_main();
259         cleanup();
260         return EXIT_SUCCESS;
261 }