1 // Original code taken from the example webkit-gtk+ application. see notice below.
4 * Copyright (C) 2006, 2007 Apple Inc.
5 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <webkit/webkit.h>
36 #include <sys/types.h>
39 static GtkWidget* main_window;
40 static WebKitWebView* web_view;
42 static gchar* uri = NULL;
43 static gchar* fifodir = NULL;
44 static gint mechmode = 0;
45 static char fifopath[64];
47 static GOptionEntry entries[] =
49 { "uri", 'u', 0, G_OPTION_ARG_STRING, &uri, "Uri to load", NULL },
50 { "fifo-dir", 'd', 0, G_OPTION_ARG_STRING, &fifodir, "Directory to place FIFOs", NULL },
51 { "mechmode", 'm', 0, G_OPTION_ARG_INT, &mechmode, "Enable output suitable for machine processing", NULL },
55 static GtkWidget* create_browser ()
57 GtkWidget* scrolled_window = gtk_scrolled_window_new (NULL, NULL);
58 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
60 web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
61 gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));
63 return scrolled_window;
66 static GtkWidget* create_window ()
68 GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
69 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
70 gtk_widget_set_name (window, "Uzbl Browser");
71 g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
76 static void parse_command(char *command)
78 if (!strcmp(command, "forward"))
80 printf("Going forward\n");
81 webkit_web_view_go_forward (web_view);
83 else if (!strncmp(command, "back", 4)) // backward
85 printf("Going back\n");
86 webkit_web_view_go_back (web_view);
88 else if (!strcmp(command, "exit") || !strcmp(command, "quit") || !strcmp(command, "die"))
92 else if (!strncmp("http://", command, 7))
94 printf("Loading URI \"%s\"\n", command);
96 webkit_web_view_load_uri (web_view, uri);
100 printf("Unhandled command \"%s\"\n", command);
104 static void *control_fifo()
107 sprintf(fifopath, "%s/uzbl_%d", fifodir, getpid());
109 sprintf(fifopath, "/tmp/uzbl_%d", getpid());
112 if (mkfifo (fifopath, 0666) == -1) {
113 printf("Possible error creating fifo\n");
117 printf("%s\n", fifopath);
119 printf ("Opened control fifo in %s\n", fifopath);
124 FILE *fifo = fopen(fifopath, "r");
126 printf("Could not open %s for reading\n", fifopath);
131 memset(buffer, 0, sizeof(buffer));
132 while (!feof(fifo) && fgets(buffer, sizeof(buffer), fifo)) {
133 if (strcmp(buffer, "\n")) {
134 buffer[strlen(buffer)-1] = '\0'; // Remove newline
135 parse_command(buffer);
143 int main (int argc, char* argv[])
145 if (!g_thread_supported ())
146 g_thread_init (NULL);
148 gtk_init (&argc, &argv);
150 GtkWidget* vbox = gtk_vbox_new (FALSE, 0);
151 gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
153 main_window = create_window ();
154 gtk_container_add (GTK_CONTAINER (main_window), vbox);
155 GError *error = NULL;
157 GOptionContext* context = g_option_context_new ("- The Usable Browser, controlled entirely through a FIFO");
158 g_option_context_add_main_entries (context, entries, NULL);
159 g_option_context_add_group (context, gtk_get_option_group (TRUE));
160 g_option_context_parse (context, &argc, &argv, &error);
163 webkit_web_view_load_uri (web_view, uri);
165 webkit_web_view_load_uri (web_view, "http://google.com");
168 gtk_widget_grab_focus (GTK_WIDGET (web_view));
169 gtk_widget_show_all (main_window);
171 pthread_t control_thread;
173 pthread_create(&control_thread, NULL, control_fifo, NULL);
177 /*pthread_join(control_thread, NULL); For some reason it doesn't terminate upon the browser closing when this is enabled. */
179 printf("Shutting down...\n");