diff options
Diffstat (limited to 'uzbl.c')
-rw-r--r-- | uzbl.c | 210 |
1 files changed, 135 insertions, 75 deletions
@@ -43,38 +43,59 @@ #include <unistd.h> #include <stdlib.h> +/* housekeeping / internal variables */ static GtkWidget* main_window; static GtkWidget* mainbar; static GtkWidget* mainbar_label; static WebKitWebView* web_view; static gchar* main_title; static gchar selected_url[500]; +static gint load_progress; +static guint status_context_id; +static Window xwin = 0; +static char fifopath[64]; + +/* state variables (initial values coming from command line arguments but may be changed later) */ +static gchar* uri = NULL; +static gchar* config_file = NULL; +static gboolean verbose = FALSE; -/* Behaviour variables */ -static gchar* history_file = NULL; +/* settings from config: group behaviour */ +static gchar* history_handler = NULL; static gchar* fifodir = NULL; static gchar* download_handler = NULL; static gboolean always_insert_mode = FALSE; +static gboolean show_status = FALSE; static gboolean insert_mode = FALSE; static gboolean show_status = FALSE; static gboolean status_top = FALSE; static gchar* modkey = NULL; -static char fifopath[64]; -static gint load_progress; -static guint status_context_id; -static Window xwin = 0; -static gchar* uri = NULL; -static gboolean verbose = FALSE; +typedef struct +{ + const char *binding; + const char *action; +} Binding; + +/* settings from config: group bindings_internal */ +static Binding internal_bindings[256]; +static int num_internal_bindings = 0; + +/* settings from config: group bindings_external */ +static Binding external_bindings[256]; +static int num_external_bindings = 0; +/* commandline arguments (set initial values for the state variables) */ static GOptionEntry entries[] = { - { "uri", 'u', 0, G_OPTION_ARG_STRING, &uri, "Uri to load", NULL }, - { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL }, + { "uri", 'u', 0, G_OPTION_ARG_STRING, &uri, "Uri to load", NULL }, + { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL }, + { "config", 'c', 0, G_OPTION_ARG_STRING, &config_file, "Config file", NULL }, { NULL } }; +/* for internal list of commands */ typedef struct { const char *command; @@ -82,20 +103,13 @@ typedef struct void (*func_2_params)(WebKitWebView*, char *); } Command; -typedef struct -{ - const char *binding; - const char *action; -} Binding; - -static Binding internal_bindings[256]; -static Binding external_bindings[256]; -static int num_internal_bindings = 0; -static int num_external_bindings = 0; static void update_title (GtkWindow* window); +static gboolean +run_command(const char *command, const char *args); + /* --- CALLBACKS --- */ static void @@ -151,7 +165,7 @@ progress_change_cb (WebKitWebView* page, gint progress, gpointer data) { static void load_commit_cb (WebKitWebView* page, WebKitWebFrame* frame, gpointer data) { - const gchar* uri = webkit_web_frame_get_uri(frame); + strcpy (uri, webkit_web_frame_get_uri (frame)); } static void @@ -161,20 +175,15 @@ destroy_cb (GtkWidget* widget, gpointer data) { static void log_history_cb () { - FILE * output_file = fopen (history_file, "a"); - if (output_file == NULL) { - fprintf (stderr, "Cannot open %s for logging\n", history_file); - } else { - time_t rawtime; - struct tm * timeinfo; - char buffer [80]; - time ( &rawtime ); - timeinfo = localtime ( &rawtime ); - strftime (buffer, 80, "%Y-%m-%d %H:%M:%S", timeinfo); - - fprintf (output_file, "%s %s\n", buffer, uri); - fclose (output_file); - } + time_t rawtime; + struct tm * timeinfo; + char date [80]; + time ( &rawtime ); + timeinfo = localtime ( &rawtime ); + strftime (date, 80, "%Y-%m-%d %H:%M:%S", timeinfo); + GString* args = g_string_new (""); + g_string_printf (args, "'%s' '%s' '%s'", uri, "TODO:page title here", date); + run_command(history_handler, args->str); } /* -- command to callback/function map for things we cannot attach to any signals */ @@ -194,6 +203,29 @@ static Command commands[] = /* -- CORE FUNCTIONS -- */ +static bool +file_exists (const char * filename) +{ + FILE *file = fopen (filename, "r"); + if (file) { + fclose (file); + return true; + } + return false; +} + +// 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] + GString* to_execute = g_string_new (""); + gboolean result; + g_string_printf (to_execute, "%s '%s' '%i' '%i' '%s' %s", command, config_file, (int) getpid() , (int) xwin, "/tmp/uzbl_25165827", args); + result = system(to_execute->str); + printf("Called %s. Result: %s\n", to_execute->str, (result ? "FALSE" : "TRUE" )); + return result; +} + static void parse_command(const char *command) { int i; @@ -201,7 +233,7 @@ parse_command(const char *command) { char * command_name = strtok (command, " "); char * command_param = strtok (NULL, " ,"); //dunno how this works, but it seems to work - Command *c_tmp; + Command *c_tmp = NULL; for (i = 0; i < LENGTH (commands); i++) { c_tmp = &commands[i]; if (strncmp (command_name, c_tmp->command, strlen (c_tmp->command)) == 0) { @@ -390,68 +422,96 @@ add_binding (char *binding, char *action, bool internal) { static void settings_init () { - GKeyFile* config = g_key_file_new (); - gboolean res = g_key_file_load_from_file (config, "./sampleconfig", G_KEY_FILE_NONE, NULL); //TODO: pass config file as argument - if (res) { - printf ("Config loaded\n"); + GKeyFile* config; + gboolean res = NULL; + gchar** keysi = NULL; + gchar** keyse = NULL; + + if (! config_file) { + const char* xdg = getenv ("XDG_CONFIG_HOME"); + char* conf[256]; + if (xdg) { + printf("XDG_CONFIG_DIR: %s\n", xdg); + strcpy (conf, xdg); + strcat (conf, "/uzbl"); + if (file_exists (conf)) { + printf ("Config file %s found.\n", conf); + config_file = &conf; + } + } + } + + if (config_file) { + config = g_key_file_new (); + res = g_key_file_load_from_file (config, config_file, G_KEY_FILE_NONE, NULL); + if(res) { + printf ("Config %s loaded\n", config_file); + } else { + fprintf (stderr, "Config %s loading failed\n", config_file); + } } else { - fprintf (stderr, "Config loading failed\n"); //TODO: exit codes with gtk? + printf ("No configuration.\n"); } - history_file = g_key_file_get_value (config, "behavior", "history_file", NULL); - if (history_file) { - printf ("History file: %s\n", history_file); + if (res) { + history_handler = g_key_file_get_value (config, "behavior", "history_handler", NULL); + download_handler = g_key_file_get_value (config, "behavior", "download_handler", NULL); + always_insert_mode = g_key_file_get_boolean (config, "behavior", "always_insert_mode", NULL); + show_status = g_key_file_get_boolean (config, "behavior", "show_status", NULL); + modkey = g_key_file_get_value (config, "behavior", "modkey", NULL); + keysi = g_key_file_get_keys (config, "bindings_internal", NULL, NULL); + keyse = g_key_file_get_keys (config, "bindings_external", NULL, NULL); + if (! fifodir) + fifodir = g_key_file_get_value (config, "behavior", "fifodir", NULL); + } + + if (history_handler) { + printf ("History handler: %s\n", history_handler); } else { - printf ("History logging disabled\n"); + printf ("History handler disabled\n"); } - download_handler = g_key_file_get_value (config, "behavior", "download_handler", NULL); if (download_handler) { printf ("Download manager: %s\n", download_handler); } else { printf ("Download manager disabled\n"); } - if (! fifodir) - fifodir = g_key_file_get_value (config, "behavior", "fifodir", NULL); if (fifodir) { printf ("Fifo directory: %s\n", fifodir); } else { printf ("Fifo directory: /tmp\n"); } - always_insert_mode = g_key_file_get_boolean (config, "behavior", "always_insert_mode", NULL); printf ("Always insert mode: %s\n", (always_insert_mode ? "TRUE" : "FALSE")); - show_status = g_key_file_get_boolean (config, "behavior", "show_status", NULL); printf ("Show status: %s\n", (show_status ? "TRUE" : "FALSE")); status_top = g_key_file_get_boolean (config, "behavior", "status_top", NULL); printf ("Status top: %s\n", (status_top ? "TRUE" : "FALSE")); - modkey = g_key_file_get_value (config, "behavior", "modkey", NULL); if (modkey) { - printf ("Mod key: %s\n", modkey); + printf ("Modkey: %s\n", modkey); } else { - printf ("Mod key disabled/\n"); + printf ("Modkey disabled\n"); } - gchar **keysi = g_key_file_get_keys (config, "bindings_internal", NULL, NULL); - int i = 0; - for (i = 0; keysi[i]; i++) - { - gchar *binding = g_key_file_get_string(config, "bindings_internal", keysi[i], NULL); - printf("Action: %s, Binding: %s (internal)\n", g_strdup (keysi[i]), binding); - add_binding (binding, g_strdup (keysi[i]), true); - } - - gchar **keyse = g_key_file_get_keys (config, "bindings_external", NULL, NULL); - for (i = 0; keyse[i]; i++) - { - gchar *binding = g_key_file_get_string(config, "bindings_external", keyse[i], NULL); - printf("Action: %s, Binding: %s (external)\n", g_strdup (keyse[i]), binding); - add_binding (binding, g_strdup (keyse[i]), false); - } + if (keysi) { + int i = 0; + for (i = 0; keysi[i]; i++) { + gchar *binding = g_key_file_get_string (config, "bindings_internal", keysi[i], NULL); + printf ("Action: %s, Binding: %s (internal)\n", g_strdup (keysi[i]), binding); + add_binding (binding, g_strdup (keysi[i]), true); + } + } + if (keyse) { + int i = 0; + for (i = 0; keyse[i]; i++) { + gchar *binding = g_key_file_get_string (config, "bindings_external", keyse[i], NULL); + printf ("Action: %s, Binding: %s (external)\n", g_strdup (keyse[i]), binding); + add_binding (binding, g_strdup (keyse[i]), false); + } + } } int @@ -460,6 +520,12 @@ main (int argc, char* argv[]) { if (!g_thread_supported ()) g_thread_init (NULL); + GError *error = NULL; + GOptionContext* context = g_option_context_new ("- some stuff here maybe someday"); + g_option_context_add_main_entries (context, entries, NULL); + g_option_context_add_group (context, gtk_get_option_group (TRUE)); + g_option_context_parse (context, &argc, &argv, &error); + settings_init (); if (always_insert_mode) insert_mode = TRUE; @@ -473,12 +539,6 @@ main (int argc, char* argv[]) { main_window = create_window (); gtk_container_add (GTK_CONTAINER (main_window), vbox); - GError *error = NULL; - - GOptionContext* context = g_option_context_new ("- some stuff here maybe someday"); - g_option_context_add_main_entries (context, entries, NULL); - g_option_context_add_group (context, gtk_get_option_group (TRUE)); - g_option_context_parse (context, &argc, &argv, &error); webkit_web_view_load_uri (web_view, uri); |