diff options
Diffstat (limited to 'uzbl.c')
-rw-r--r-- | uzbl.c | 176 |
1 files changed, 122 insertions, 54 deletions
@@ -42,6 +42,11 @@ #include <sys/types.h> #include <unistd.h> #include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/un.h> /* housekeeping / internal variables */ static GtkWidget* main_window; @@ -49,19 +54,20 @@ static GtkWidget* mainbar; static GtkWidget* mainbar_label; static WebKitWebView* web_view; static gchar* main_title; -static gchar selected_url[500]; +static gchar selected_url[500] = "\0"; static gint load_progress; static Window xwin = 0; -static char fifopath[64]; +static char socket_path[108]; /* state variables (initial values coming from command line arguments but may be changed later) */ static gchar* uri = NULL; static gchar* config_file = NULL; +static gchar config_file_path[500]; static gboolean verbose = FALSE; /* settings from config: group behaviour */ static gchar* history_handler = NULL; -static gchar* fifodir = NULL; +static gchar* socket_dir = NULL; static gchar* download_handler = NULL; static gboolean always_insert_mode = FALSE; static gboolean show_status = FALSE; @@ -108,6 +114,9 @@ static void load_uri ( WebKitWebView * web_view, const gchar * uri); static void +new_window_load_uri (const gchar * uri); + +static void go_home ( WebKitWebView * web_view); static void @@ -118,6 +127,7 @@ run_command(const char *command, const char *args); /* --- CALLBACKS --- */ + static gboolean new_window_cb (WebKitWebView *web_view, WebKitWebFrame *frame, WebKitNetworkRequest *request, WebKitWebNavigationAction *navigation_action, WebKitWebPolicyDecision *policy_decision, gpointer user_data) { (void) web_view; @@ -127,18 +137,24 @@ new_window_cb (WebKitWebView *web_view, WebKitWebFrame *frame, WebKitNetworkRequ (void) user_data; const gchar* uri = webkit_network_request_get_uri (request); printf("New window requested -> %s \n", uri); - gboolean result; - GString* to_execute = g_string_new (""); - g_string_printf (to_execute, "uzbl --uri '%s'", uri); - result = g_spawn_command_line_async (to_execute->str, NULL); - if (!result) { - g_string_printf (to_execute, "./uzbl --uri '%s'", uri); - result = g_spawn_command_line_async (to_execute->str, NULL); - } - g_string_free (to_execute, TRUE); + new_window_load_uri(uri); return (FALSE); } +WebKitWebView* +create_web_view_cb (WebKitWebView *web_view, WebKitWebFrame *frame, gpointer user_data) { + (void) web_view; + (void) frame; + (void) user_data; + if (selected_url[0]!=0) { + printf("\nNew web view -> %s\n",selected_url); + new_window_load_uri(selected_url); + } else { + printf("New web view -> %s\n","Nothing to open, exiting"); + } + return (NULL); +} + static gboolean download_cb (WebKitWebView *web_view, GObject *download, gpointer user_data) { (void) web_view; @@ -294,6 +310,27 @@ load_uri (WebKitWebView * web_view, const gchar * uri) { } static void +new_window_load_uri (const gchar * uri) { + GString* to_execute = g_string_new (""); + if (!config_file) { + g_string_printf (to_execute, "uzbl --uri '%s'", uri); + } else { + g_string_printf (to_execute, "uzbl --uri '%s' --config '%s'", uri, config_file); + } + printf("Spawning %s\n",to_execute->str); + if (!g_spawn_command_line_async (to_execute->str, NULL)) { + if (!config_file) { + g_string_printf (to_execute, "./uzbl --uri '%s'", uri); + } else { + g_string_printf (to_execute, "./uzbl --uri '%s' --config '%s'", uri, config_file); + } + printf("Spawning %s\n",to_execute->str); + g_spawn_command_line_async (to_execute->str, NULL); + } + g_string_free (to_execute, TRUE); +} + +static void go_home (WebKitWebView * web_view) { if (home_page) webkit_web_view_load_uri (web_view, home_page); @@ -308,10 +345,10 @@ close_uzbl (WebKitWebView * web_view) { // make sure to put '' around args, so that if there is whitespace we can still keep arguments together. static gboolean run_command(const char *command, const char *args) { - //command <uzbl conf> <uzbl pid> <uzbl win id> <uzbl fifo file> [args] + //command <uzbl conf> <uzbl pid> <uzbl win id> <uzbl socket file> [args] GString* to_execute = g_string_new (""); gboolean result; - g_string_printf (to_execute, "%s '%s' '%i' '%i' '%s'", command, config_file, (int) getpid() , (int) xwin, fifopath); + g_string_printf (to_execute, "%s '%s' '%i' '%i' '%s'", command, config_file, (int) getpid() , (int) xwin, socket_path); if(args) { g_string_append_printf (to_execute, " %s", args); } @@ -350,34 +387,60 @@ parse_command(const char *cmd) { } static void -*control_fifo() { - if (fifodir) { - sprintf (fifopath, "%s/uzbl_%d", fifodir, (int) xwin); +*control_socket() { + if (socket_dir) { + sprintf (socket_path, "%s/uzbl_%d", socket_dir, (int) xwin); } else { - sprintf (fifopath, "/tmp/uzbl_%d", (int) xwin); + sprintf (socket_path, "/tmp/uzbl_%d", (int) xwin); } - if (mkfifo (fifopath, 0666) == -1) { - printf ("Possible error creating fifo\n"); + int sock, clientsock, len; + unsigned int t; + struct sockaddr_un local, remote; + + sock = socket (AF_UNIX, SOCK_STREAM, 0); + + local.sun_family = AF_UNIX; + strcpy (local.sun_path, socket_path); + unlink (local.sun_path); + + len = strlen (local.sun_path) + sizeof (local.sun_family); + bind (sock, (struct sockaddr *) &local, len); + + if (errno == -1) { + printf ("A problem occurred when opening a socket in %s\n", socket_path); + } else { + printf ("Control socket opened in %s\n", socket_path); } + + listen (sock, 5); - printf ("Control fifo opened in %s\n", fifopath); - - while (true) { - FILE *fifo = fopen (fifopath, "r"); - if (!fifo) { - printf ("Could not open %s for reading\n", fifopath); - return NULL; - } - - char buffer[256]; + for(;;) { + int done, n; + char buffer[512]; + char temp[128]; + memset (buffer, 0, sizeof (buffer)); - while (!feof (fifo) && fgets (buffer, sizeof (buffer), fifo)) { - if (strcmp (buffer, "\n")) { - buffer[strlen (buffer) - 1] = '\0'; // Remove newline - parse_command (buffer); - } + + t = sizeof (remote); + clientsock = accept (sock, (struct sockaddr *) &remote, &t); + printf ("Connected to client\n"); + + done = 0; + do { + n = recv (clientsock, temp, 128, 0); + if (n == 0) + done = 1; + + if (!done) + strcat (buffer, temp); + } while (!done); + + if (strcmp (buffer, "\n")) { + buffer[strlen (buffer) - 1] = '\0'; + parse_command (buffer); } + close(clientsock); } return NULL; @@ -386,7 +449,7 @@ static void static void setup_threading () { pthread_t control_thread; - pthread_create(&control_thread, NULL, control_fifo, NULL); + pthread_create(&control_thread, NULL, control_socket, NULL); } static void @@ -426,7 +489,8 @@ key_press_cb (WebKitWebView* page, GdkEventKey* event) (void) page; gpointer act; gboolean result=FALSE; //TRUE to stop other handlers from being invoked for the event. FALSE to propagate the event further. - if (event->type != GDK_KEY_PRESS) + if (event->type != GDK_KEY_PRESS || event->keyval == GDK_Page_Up || event->keyval == GDK_Page_Down + || event->keyval == GDK_Up || event->keyval == GDK_Down || event->keyval == GDK_Left || event->keyval == GDK_Right) return result; //TURN OFF/ON INSERT MODE @@ -472,6 +536,7 @@ create_browser () { g_signal_connect (G_OBJECT (web_view), "key-press-event", G_CALLBACK (key_press_cb), web_view); g_signal_connect (G_OBJECT (web_view), "new-window-policy-decision-requested", G_CALLBACK (new_window_cb), web_view); g_signal_connect (G_OBJECT (web_view), "download-requested", G_CALLBACK (download_cb), web_view); + g_signal_connect (G_OBJECT (web_view), "create-web-view", G_CALLBACK (create_web_view_cb), web_view); return scrolled_window; } @@ -509,19 +574,18 @@ settings_init () { gchar** keysi = NULL; gchar** keyse = NULL; - if (! config_file) { + if (!config_file) { const char* XDG_CONFIG_HOME = getenv ("XDG_CONFIG_HOME"); - char conf[256]; if (! XDG_CONFIG_HOME || ! strcmp (XDG_CONFIG_HOME, "")) { XDG_CONFIG_HOME = (char *)XDG_CONFIG_HOME_default; } printf("XDG_CONFIG_HOME: %s\n", XDG_CONFIG_HOME); - strcpy (conf, XDG_CONFIG_HOME); - strcat (conf, "/uzbl/config"); - if (file_exists (conf)) { - printf ("Config file %s found.\n", conf); - config_file = &conf[0]; + strcpy (config_file_path, XDG_CONFIG_HOME); + strcat (config_file_path, "/uzbl/config"); + if (file_exists (config_file_path)) { + printf ("Config file %s found.\n", config_file_path); + config_file = &config_file_path[0]; } else { // Now we check $XDG_CONFIG_DIRS char *XDG_CONFIG_DIRS = getenv ("XDG_CONFIG_DIRS"); @@ -533,12 +597,12 @@ settings_init () { char buffer[512]; strcpy (buffer, XDG_CONFIG_DIRS); const gchar* dir = strtok (buffer, ":"); - while (dir && ! file_exists (conf)) { - strcpy (conf, dir); - strcat (conf, "/uzbl/config"); - if (file_exists (conf)) { - printf ("Config file %s found.\n", conf); - config_file = &conf[0]; + while (dir && ! file_exists (config_file_path)) { + strcpy (config_file_path, dir); + strcat (config_file_path, "/uzbl/config"); + if (file_exists (config_file_path)) { + printf ("Config file %s found.\n", config_file_path); + config_file = &config_file_path[0]; } dir = strtok (NULL, ":"); } @@ -567,13 +631,13 @@ settings_init () { keyse = g_key_file_get_keys (config, "bindings_external", NULL, NULL); status_top = g_key_file_get_boolean (config, "behavior", "status_top", NULL); home_page = g_key_file_get_value (config, "behavior", "home_page", NULL); - if (! fifodir) - fifodir = g_key_file_get_value (config, "behavior", "fifodir", NULL); + if (! socket_dir) + socket_dir = g_key_file_get_value (config, "behavior", "socket_dir", NULL); } printf ("History handler: %s\n", (history_handler ? history_handler : "disabled")); printf ("Download manager: %s\n", (download_handler ? download_handler : "disabled")); - printf ("FIFO directory: %s\n", (fifodir ? fifodir : "/tmp")); + printf ("Socket directory: %s\n", (socket_dir ? socket_dir : "/tmp")); printf ("Always insert mode: %s\n", (always_insert_mode ? "TRUE" : "FALSE")); printf ("Show status: %s\n", (show_status ? "TRUE" : "FALSE")); printf ("Status top: %s\n", (status_top ? "TRUE" : "FALSE")); @@ -627,6 +691,8 @@ main (int argc, char* argv[]) { if (!g_thread_supported ()) g_thread_init (NULL); + printf("Uzbl start location: %s\n", argv[0]); + strcat ((char *) XDG_CONFIG_HOME_default, getenv ("HOME")); strcat ((char *) XDG_CONFIG_HOME_default, "/.config"); @@ -670,6 +736,8 @@ main (int argc, char* argv[]) { gtk_main (); - unlink (fifopath); + unlink (socket_path); return 0; } + +/* vi: set et ts=4: */ |