6 #include <webkit/webkit.h>
8 #define LENGTH(x) (sizeof x / sizeof x[0])
9 /* Plan9-style Argument parsing */
10 /* Vars: _c -> count; _b -> break; _a -> argument */
11 #define ARG int _c, _b; char *_a; \
12 for(_c = 1; _c < argc && argv[_c][0] == '-' && argv[_c][1] && \
13 (strcmp(argv[_c], "--") != 0); _c++) \
14 for(_a = &argv[_c][1], _b = 0; !_b && *_a; _a++ ) \
16 #define ARGVAL() (!_b && _a[1] && (_b = 1) ? &_a[1] : _c + 1 == argc ? \
18 #define ARGCHR() (*_a)
26 gboolean embed = FALSE;
28 static void setup(void);
29 static void cleanup(void);
30 static void updatetitle(void);
31 static void windestroy(GtkWidget* w, gpointer d);
32 static gboolean keypress(GtkWidget* w, GdkEventKey *ev);
33 static void titlechange(WebKitWebView* view, WebKitWebFrame* frame, const gchar* title, gpointer d);
34 static void progresschange(WebKitWebView *view, gint p, gpointer d);
35 static void loadcommit(WebKitWebView *view, WebKitWebFrame *f, gpointer d);
36 static void loadstart(WebKitWebView *view, WebKitWebFrame *f, gpointer d);
37 static void loadfinish(WebKitWebView *view, WebKitWebFrame *f, gpointer d);
38 static void linkhover(WebKitWebView* page, const gchar* t, const gchar* l, gpointer d);
39 static gboolean newwindow(WebKitWebView *view, WebKitWebFrame *f,
40 WebKitNetworkRequest *r, WebKitWebNavigationAction *n,
41 WebKitWebPolicyDecision *p, gpointer d);
42 static gboolean download(WebKitWebView *view, GObject *o, gpointer d);
43 static void loaduri(gchar *uri);
44 static void loadfile(gchar *f);
45 static void setupstdin();
46 static gboolean readstdin(GIOChannel *c, GIOCondition con);
49 readstdin(GIOChannel *c, GIOCondition con) {
53 ret = g_io_channel_read_line(c, &line, NULL, NULL, NULL);
54 if(ret == G_IO_STATUS_ERROR || ret == G_IO_STATUS_EOF)
56 for(p = line; *p && *p != '\n'; p++);
67 c = g_io_channel_unix_new(STDIN_FILENO);
68 if(c && !g_io_add_watch(c, G_IO_IN|G_IO_HUP, (GIOFunc) readstdin, NULL))
69 g_error("Stdin: could not add watch\n");
76 GString *code = g_string_new("");
79 /* cannot use fileno in c99 - workaround*/
80 if(strcmp(f, "-") == 0)
81 c = g_io_channel_unix_new(STDIN_FILENO);
83 c = g_io_channel_new_file(f, "r", NULL);
85 while(g_io_channel_read_line(c, &line, NULL, NULL, &e) == G_IO_STATUS_NORMAL) {
86 g_string_append(code, line);
89 webkit_web_view_load_html_string(view, code->str, NULL);
90 g_io_channel_shutdown(c, FALSE, NULL);
94 static void loaduri(gchar *uri) {
95 GString* u = g_string_new(uri);
96 if(g_strrstr(u->str, "://") == NULL)
97 g_string_prepend(u, "http://");
98 webkit_web_view_load_uri(view, u->str);
99 g_string_free(u, TRUE);
103 download(WebKitWebView *view, GObject *o, gpointer d) {
109 newwindow(WebKitWebView *view, WebKitWebFrame *f,
110 WebKitNetworkRequest *r, WebKitWebNavigationAction *n,
111 WebKitWebPolicyDecision *p, gpointer d) {
116 linkhover(WebKitWebView* page, const gchar* t, const gchar* l, gpointer d) {
121 loadstart(WebKitWebView *view, WebKitWebFrame *f, gpointer d) {
126 loadcommit(WebKitWebView *view, WebKitWebFrame *f, gpointer d) {
131 loadfinish(WebKitWebView *view, WebKitWebFrame *f, gpointer d) {
136 progresschange(WebKitWebView* view, gint p, gpointer d) {
144 snprintf(t, LENGTH(t), "%s [%i%%]", title, progress);
145 gtk_window_set_title(GTK_WINDOW(win), t);
149 titlechange(WebKitWebView *v, WebKitWebFrame *f, const gchar *t, gpointer d) {
157 windestroy(GtkWidget* w, gpointer d) {
162 keypress(GtkWidget* w, GdkEventKey *ev) {
168 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
169 gtk_window_set_default_size(GTK_WINDOW(win), 800, 600);
170 gtk_widget_set_name(win, "surf window");
171 browser = gtk_scrolled_window_new(NULL, NULL);
172 g_signal_connect (G_OBJECT(win), "destroy", G_CALLBACK(windestroy), NULL);
173 g_signal_connect (G_OBJECT(win), "key-press-event", G_CALLBACK(keypress), NULL);
175 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(browser),
176 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
177 view = WEBKIT_WEB_VIEW(webkit_web_view_new());
178 gtk_container_add(GTK_CONTAINER(browser), GTK_WIDGET(view));
180 g_signal_connect(G_OBJECT(view), "title-changed", G_CALLBACK(titlechange), view);
181 g_signal_connect(G_OBJECT(view), "load-progress-changed", G_CALLBACK(progresschange), view);
182 g_signal_connect(G_OBJECT(view), "load-committed", G_CALLBACK(loadcommit), view);
183 g_signal_connect(G_OBJECT(view), "load-started", G_CALLBACK(loadstart), view);
184 g_signal_connect(G_OBJECT(view), "load-finished", G_CALLBACK(loadfinish), view);
185 g_signal_connect(G_OBJECT(view), "hovering-over-link", G_CALLBACK(linkhover), view);
186 g_signal_connect(G_OBJECT(view), "new-window-policy-decision-requested", G_CALLBACK(newwindow), view);
187 g_signal_connect(G_OBJECT(view), "download-requested", G_CALLBACK(download), view);
188 /* g_signal_connect(G_OBJECT(view), "create-web-view", G_CALLBACK(createwebview), view); */
190 gtk_container_add(GTK_CONTAINER(win), browser);
191 gtk_widget_grab_focus(GTK_WIDGET(view));
192 gtk_widget_show_all(win);
198 int main(int argc, char *argv[]) {
199 gchar *uri = NULL, *file = NULL;
206 if(!(uri = ARGVAL()))
210 if(!(file = ARGVAL()))
215 puts("surf - simple browser");
216 printf("usage: %s [-e] [-u uri] [-f file]", argv[0]);
221 gtk_init(NULL, NULL);
222 if (!g_thread_supported())