diff options
author | Dieter Plaetinck <dieter@plaetinck.be> | 2009-04-29 21:50:32 +0200 |
---|---|---|
committer | Dieter Plaetinck <dieter@plaetinck.be> | 2009-04-29 21:50:32 +0200 |
commit | 8337a50dcb30a0e1b5fb5654fc69d52383a14bae (patch) | |
tree | f659152938a5af6ec0b9cd86bc691c6080c5c605 /uzbl.c | |
parent | 37e1857e8aa4d77647af8055954b7a2c240cb20e (diff) | |
parent | c11d95bcaac99e5e62f99cdeace46737a57bdde6 (diff) |
merged from barrucadu
Diffstat (limited to 'uzbl.c')
-rw-r--r-- | uzbl.c | 164 |
1 files changed, 100 insertions, 64 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; @@ -52,18 +57,17 @@ static gchar* main_title; 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; @@ -341,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); } @@ -356,62 +360,93 @@ run_command(const char *command, const char *args) { static void parse_command(const char *cmd) { - Command *c = NULL; - char buffer[512]; - strcpy (buffer, cmd); - char * saveptr; - char * command_name = strtok_r (buffer, " ", &saveptr); - gchar * command_param = strtok_r (NULL, " ,", &saveptr); + Command *c = NULL; + char buffer[512]; + strcpy (buffer, cmd); + char * saveptr; + char * command_name = strtok_r (buffer, " ", &saveptr); + gchar * command_param = strtok_r (NULL, " ,", &saveptr); - if((c = g_hash_table_lookup(commands, command_name)) != NULL){ - if (c->func_2_params != NULL) { - if (command_param != NULL) { - printf ("command executing: \"%s %s\"\n", command_name, command_param); - c->func_2_params (web_view, command_param); - } else { - if (c->func_1_param != NULL) { - printf ("command executing: \"%s\"\n", command_name); - c->func_1_param (web_view); - } else - fprintf (stderr, "command needs a parameter. \"%s\" is not complete\n", command_name); - } - } else if (c->func_1_param != NULL) { - printf ("command executing: \"%s\"\n", command_name); - c->func_1_param (web_view); - } - } else - fprintf (stderr, "command \"%s\" not understood. ignoring.\n", cmd); + if((c = g_hash_table_lookup(commands, command_name)) != NULL){ + if (c->func_2_params != NULL) { + if (command_param != NULL) { + printf ("command executing: \"%s %s\"\n", command_name, command_param); + c->func_2_params (web_view, command_param); + } else { + if (c->func_1_param != NULL) { + printf ("command executing: \"%s\"\n", command_name); + c->func_1_param (web_view); + } else + fprintf (stderr, "command needs a parameter. \"%s\" is not complete\n", command_name); + } + } else if (c->func_1_param != NULL) { + printf ("command executing: \"%s\"\n", command_name); + c->func_1_param (web_view); + } + } else + fprintf (stderr, "command \"%s\" not understood. ignoring.\n", 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]; + char buffer[512]; + char temp[128]; + int done, n; + for(;;) { 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 { + memset (temp, 0, sizeof (temp)); + n = recv (clientsock, temp, 128, 0); + if (n == 0) { + buffer[strlen (buffer)] = '\0'; + done = 1; } + + if (!done) + strcat (buffer, temp); + } while (!done); + + if (strcmp (buffer, "\n") < 0) { + buffer[strlen (buffer) - 1] = '\0'; + } else { + buffer[strlen (buffer)] = '\0'; } + + parse_command (buffer); + close (clientsock); } return NULL; @@ -420,7 +455,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 @@ -460,7 +495,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,17 +508,17 @@ key_press_cb (WebKitWebView* page, GdkEventKey* event) //INTERNAL BINDINGS if((act = g_hash_table_lookup(internal_bindings, event->string)) != NULL) - if (!insert_mode || (event->state == modmask)) { - parse_command (act); - result = TRUE; - } - + if (!insert_mode || (event->state == modmask)) { + parse_command (act); + result = TRUE; + } + //EXTERNAL BINDINGS if((act = g_hash_table_lookup(external_bindings, event->string)) != NULL) - if (!insert_mode || (event->state == modmask)) { - run_command (act, NULL); - result = TRUE; - } + if (!insert_mode || (event->state == modmask)) { + run_command (act, NULL); + result = TRUE; + } if (!result) result = (insert_mode ? FALSE : TRUE); @@ -602,13 +638,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")); @@ -707,7 +743,7 @@ main (int argc, char* argv[]) { gtk_main (); - unlink (fifopath); + unlink (socket_path); return 0; } |