From 7d71d08fbe94ed557c65f976f54c36c027db5ad3 Mon Sep 17 00:00:00 2001 From: Daniel Carl Date: Thu, 1 Nov 2012 15:59:45 +0100 Subject: [PATCH] Moved all command to own c file. --- src/command.c | 217 ++++++++++++++++++++++++++++++++++++++++++-------- src/command.h | 11 +++ src/config.h | 8 +- src/keybind.c | 10 +++ src/keybind.h | 1 + src/main.c | 154 ++--------------------------------- src/main.h | 12 +-- src/setting.c | 7 ++ src/setting.h | 1 + 9 files changed, 228 insertions(+), 193 deletions(-) diff --git a/src/command.c b/src/command.c index c58cc54..403f355 100644 --- a/src/command.c +++ b/src/command.c @@ -19,43 +19,48 @@ #include "main.h" #include "command.h" +#include "keybind.h" +#include "setting.h" + +extern const char *inputbox_font[2]; +extern const char *inputbox_fg[2]; +extern const char *inputbox_bg[2]; static CommandInfo cmd_list[] = { - /* command function arg */ - {"normal", vp_set_mode, {VP_MODE_NORMAL, ""}}, - {"open", vp_open, {VP_MODE_NORMAL, ""}}, - {"input", vp_input, {0, ":"}}, - {"inputopen", vp_input, {0, ":open "}}, - {"inputopencurrent", vp_input, {VP_INPUT_CURRENT_URI, ":open "}}, - {"quit", vp_close_browser, {0}}, - {"source", vp_view_source, {0}}, - {"back", vp_navigate, {VP_NAVIG_BACK}}, - {"forward", vp_navigate, {VP_NAVIG_FORWARD}}, - {"reload", vp_navigate, {VP_NAVIG_RELOAD}}, - {"reload!", vp_navigate, {VP_NAVIG_RELOAD_FORCE}}, - {"stop", vp_navigate, {VP_NAVIG_STOP_LOADING}}, - {"jumpleft", vp_scroll, {VP_SCROLL_TYPE_JUMP | VP_SCROLL_DIRECTION_LEFT}}, - {"jumpright", vp_scroll, {VP_SCROLL_TYPE_JUMP | VP_SCROLL_DIRECTION_RIGHT}}, - {"jumptop", vp_scroll, {VP_SCROLL_TYPE_JUMP | VP_SCROLL_DIRECTION_TOP}}, - {"jumpbottom", vp_scroll, {VP_SCROLL_TYPE_JUMP | VP_SCROLL_DIRECTION_DOWN}}, - {"pageup", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_TOP | VP_SCROLL_UNIT_PAGE}}, - {"pagedown", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_DOWN | VP_SCROLL_UNIT_PAGE}}, - {"halfpageup", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_TOP | VP_SCROLL_UNIT_HALFPAGE}}, - {"halfpagedown", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_DOWN | VP_SCROLL_UNIT_HALFPAGE}}, - {"scrollleft", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_LEFT | VP_SCROLL_UNIT_LINE}}, - {"scrollright", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_RIGHT | VP_SCROLL_UNIT_LINE}}, - {"scrollup", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_TOP | VP_SCROLL_UNIT_LINE}}, - {"scrolldown", vp_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_DOWN | VP_SCROLL_UNIT_LINE}}, - {"nmap", vp_map, {VP_MODE_NORMAL}}, - {"imap", vp_map, {VP_MODE_INSERT}}, - {"cmap", vp_map, {VP_MODE_COMMAND}}, - {"nunmap", vp_unmap, {VP_MODE_NORMAL}}, - {"iunmap", vp_unmap, {VP_MODE_INSERT}}, - {"cunmap", vp_unmap, {VP_MODE_COMMAND}}, - {"set", vp_set, {0}}, + /* command function arg */ + {"open", command_open, {VP_MODE_NORMAL, ""}}, + {"input", command_input, {0, ":"}}, + {"inputopen", command_input, {0, ":open "}}, + {"inputopencurrent", command_input, {VP_INPUT_CURRENT_URI, ":open "}}, + {"quit", command_close, {0}}, + {"source", command_view_source, {0}}, + {"back", command_navigate, {VP_NAVIG_BACK}}, + {"forward", command_navigate, {VP_NAVIG_FORWARD}}, + {"reload", command_navigate, {VP_NAVIG_RELOAD}}, + {"reload!", command_navigate, {VP_NAVIG_RELOAD_FORCE}}, + {"stop", command_navigate, {VP_NAVIG_STOP_LOADING}}, + {"jumpleft", command_scroll, {VP_SCROLL_TYPE_JUMP | VP_SCROLL_DIRECTION_LEFT}}, + {"jumpright", command_scroll, {VP_SCROLL_TYPE_JUMP | VP_SCROLL_DIRECTION_RIGHT}}, + {"jumptop", command_scroll, {VP_SCROLL_TYPE_JUMP | VP_SCROLL_DIRECTION_TOP}}, + {"jumpbottom", command_scroll, {VP_SCROLL_TYPE_JUMP | VP_SCROLL_DIRECTION_DOWN}}, + {"pageup", command_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_TOP | VP_SCROLL_UNIT_PAGE}}, + {"pagedown", command_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_DOWN | VP_SCROLL_UNIT_PAGE}}, + {"halfpageup", command_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_TOP | VP_SCROLL_UNIT_HALFPAGE}}, + {"halfpagedown", command_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_DOWN | VP_SCROLL_UNIT_HALFPAGE}}, + {"scrollleft", command_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_LEFT | VP_SCROLL_UNIT_LINE}}, + {"scrollright", command_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_RIGHT | VP_SCROLL_UNIT_LINE}}, + {"scrollup", command_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_TOP | VP_SCROLL_UNIT_LINE}}, + {"scrolldown", command_scroll, {VP_SCROLL_TYPE_SCROLL | VP_SCROLL_DIRECTION_DOWN | VP_SCROLL_UNIT_LINE}}, + {"nmap", command_map, {VP_MODE_NORMAL}}, + {"imap", command_map, {VP_MODE_INSERT}}, + {"cmap", command_map, {VP_MODE_COMMAND}}, + {"nunmap", command_unmap, {VP_MODE_NORMAL}}, + {"iunmap", command_unmap, {VP_MODE_INSERT}}, + {"cunmap", command_unmap, {VP_MODE_COMMAND}}, + {"set", command_set, {0}}, }; -void command_init() +void command_init(void) { guint i; vp.behave.commands = g_hash_table_new(g_str_hash, g_str_equal); @@ -65,6 +70,13 @@ void command_init() } } +void command_cleanup(void) +{ + if (vp.behave.commands) { + g_hash_table_destroy(vp.behave.commands); + } +} + gboolean command_exists(const gchar* name) { return g_hash_table_contains(vp.behave.commands, name); @@ -91,3 +103,142 @@ gboolean command_run(const gchar* name, const gchar* param) return result; } + +gboolean command_open(const Arg* arg) +{ + return vp_load_uri(arg); +} + +gboolean command_input(const Arg* arg) +{ + gint pos = 0; + const gchar* url; + + /* reset the colors and fonts to defalts */ + vp_set_widget_font(vp.gui.inputbox, inputbox_font[0], inputbox_bg[0], inputbox_fg[0]); + + /* remove content from input box */ + gtk_entry_set_text(GTK_ENTRY(vp.gui.inputbox), ""); + + /* insert string from arg */ + gtk_editable_insert_text(GTK_EDITABLE(vp.gui.inputbox), arg->s, -1, &pos); + + /* add current url if requested */ + if (VP_INPUT_CURRENT_URI == arg->i + && (url = webkit_web_view_get_uri(vp.gui.webview))) { + gtk_editable_insert_text(GTK_EDITABLE(vp.gui.inputbox), url, -1, &pos); + } + + gtk_editable_set_position(GTK_EDITABLE(vp.gui.inputbox), -1); + + Arg a = {VP_MODE_COMMAND}; + return vp_set_mode(&a); +} + +gboolean command_close(const Arg* arg) +{ + vp_clean_up(); + gtk_main_quit(); + + return TRUE; +} + +gboolean command_view_source(const Arg* arg) +{ + gboolean mode = webkit_web_view_get_view_source_mode(vp.gui.webview); + webkit_web_view_set_view_source_mode(vp.gui.webview, !mode); + webkit_web_view_reload(vp.gui.webview); + + return TRUE; +} + +gboolean command_navigate(const Arg* arg) +{ + if (arg->i <= VP_NAVIG_FORWARD) { + /* TODO allow to set a count for the navigation */ + webkit_web_view_go_back_or_forward( + vp.gui.webview, (arg->i == VP_NAVIG_BACK ? -1 : 1) + ); + } else if (arg->i == VP_NAVIG_RELOAD) { + webkit_web_view_reload(vp.gui.webview); + } else if (arg->i == VP_NAVIG_RELOAD_FORCE) { + webkit_web_view_reload_bypass_cache(vp.gui.webview); + } else { + webkit_web_view_stop_loading(vp.gui.webview); + } + + return TRUE; +} + +gboolean command_scroll(const Arg* arg) +{ + GtkAdjustment *adjust = (arg->i & VP_SCROLL_AXIS_H) ? vp.gui.adjust_h : vp.gui.adjust_v; + + gint direction = (arg->i & (1 << 2)) ? 1 : -1; + + /* type scroll */ + if (arg->i & VP_SCROLL_TYPE_SCROLL) { + gdouble value; + gint count = vp.state.count ? vp.state.count : 1; + if (arg->i & VP_SCROLL_UNIT_LINE) { + /* make scroll step configurable */ + value = 40; + } else if (arg->i & VP_SCROLL_UNIT_HALFPAGE) { + value = gtk_adjustment_get_page_size(adjust) / 2; + } else { + value = gtk_adjustment_get_page_size(adjust); + } + gtk_adjustment_set_value(adjust, gtk_adjustment_get_value(adjust) + direction * value * count); + } else if (vp.state.count) { + /* jump - if count is set to count% of page */ + gdouble max = gtk_adjustment_get_upper(adjust) - gtk_adjustment_get_page_size(adjust); + gtk_adjustment_set_value(adjust, max * vp.state.count / 100); + } else if (direction == 1) { + /* jump to top */ + gtk_adjustment_set_value(adjust, gtk_adjustment_get_upper(adjust)); + } else { + /* jump to bottom */ + gtk_adjustment_set_value(adjust, gtk_adjustment_get_lower(adjust)); + } + + return TRUE; +} + +gboolean command_map(const Arg* arg) +{ + return keybind_add_from_string(arg->s, arg->i); +} + +gboolean command_unmap(const Arg* arg) +{ + return keybind_remove_from_string(arg->s, arg->i); +} + +gboolean command_set(const Arg* arg) +{ + gboolean success; + gchar* line = NULL; + gchar** token; + + if (!arg->s || !strlen(arg->s)) { + return FALSE; + } + + line = g_strdup(arg->s); + g_strstrip(line); + + /* split the input string into paramete and value part */ + token = g_strsplit(line, "=", 2); + g_free(line); + + if (!token[1]) { + /* TODO display current value */ + g_strfreev(token); + vp_echo(VP_MSG_ERROR, "No param given"); + return FALSE; + } + success = setting_run(token[0], token[1] ? token[1] : NULL); + g_strfreev(token); + + return success; +} diff --git a/src/command.h b/src/command.h index f150954..5cb7d8b 100644 --- a/src/command.h +++ b/src/command.h @@ -30,7 +30,18 @@ typedef struct { void command_init(void); +void command_cleanup(void); gboolean command_exists(const gchar* name); gboolean command_run(const gchar* name, const gchar* param); +gboolean command_open(const Arg* arg); +gboolean command_input(const Arg* arg); +gboolean command_close(const Arg* arg); +gboolean command_view_source(const Arg* arg); +gboolean command_navigate(const Arg* arg); +gboolean command_scroll(const Arg* arg); +gboolean command_map(const Arg* arg); +gboolean command_unmap(const Arg* arg); +gboolean command_set(const Arg* arg); + #endif /* end of include guard: COMMAND_H */ diff --git a/src/config.h b/src/config.h index 2cf0e49..3061064 100644 --- a/src/config.h +++ b/src/config.h @@ -29,17 +29,15 @@ #define STATUS_BAR_FONT "monospace bold 8" /* normal error */ -static const char *inputbox_font[] = { "monospace normal 8", "monospace bold 8"}; -static const char *inputbox_fg[] = { "#000000", "#000000" }; -static const char *inputbox_bg[] = { "#ffffff", "#ff0000" }; +const char *inputbox_font[2] = { "monospace normal 8", "monospace bold 8"}; +const char *inputbox_fg[2] = { "#000000", "#000000" }; +const char *inputbox_bg[2] = { "#ffffff", "#ff0000" }; #define SETTING_DEFAUL_FONT_SIZE 12 #define SETTING_USER_AGENT PROJECT "/" VERSION " (X11; Linux i686) AppleWebKit/535.22+ Compatible (Safari)" #define SETTING_MAX_CONNS 25 #define SETTING_MAX_CONNS_PER_HOST 5 -#define SCROLLSTEP (40) /* cursor difference in pixel for scrolling */ - const struct { char* command; } default_config[] = { diff --git a/src/keybind.c b/src/keybind.c index 135519c..5cdac86 100644 --- a/src/keybind.c +++ b/src/keybind.c @@ -36,6 +36,16 @@ void keybind_init(void) g_signal_connect(G_OBJECT(vp.gui.window), "key-press-event", G_CALLBACK(keybind_keypress_callback), NULL); } +void keybind_cleanup(void) +{ + if (keys) { + g_slist_free(keys); + } + if (modkeys) { + g_string_free(modkeys, TRUE); + } +} + gboolean keybind_add_from_string(const gchar* str, const Mode mode) { if (str == NULL || *str == '\0') { diff --git a/src/keybind.h b/src/keybind.h index c6e27a3..06e8048 100644 --- a/src/keybind.h +++ b/src/keybind.h @@ -40,6 +40,7 @@ typedef struct { } Keybind; void keybind_init(void); +void keybind_cleanup(void); gboolean keybind_add_from_string(const gchar* str, const Mode mode); gboolean keybind_remove_from_string(const gchar* str, const Mode mode); diff --git a/src/main.c b/src/main.c index ab6dee0..c21c8af 100644 --- a/src/main.c +++ b/src/main.c @@ -18,11 +18,11 @@ */ #include "main.h" -#include "config.h" #include "util.h" #include "command.h" #include "keybind.h" #include "setting.h" +#include "config.h" /* variables */ VpCore vp; @@ -47,14 +47,11 @@ static void vp_init(void); static void vp_read_config(void); static void vp_init_gui(void); static void vp_init_files(void); -static void vp_set_widget_font(GtkWidget* widget, const gchar* font_definition, const gchar* bg_color, const gchar* fg_color); static void vp_setup_signals(void); -static gboolean vp_load_uri(const Arg* arg); #ifdef FEATURE_COOKIE static void vp_set_cookie(SoupCookie* cookie); static const gchar* vp_get_cookies(SoupURI *uri); #endif -static void vp_clean_up(void); static gboolean vp_hide_message(void); static void vp_webview_load_status_cb(WebKitWebView* view, GParamSpec* pspec, gpointer user_data) @@ -83,7 +80,7 @@ static void vp_webview_load_commited_cb(WebKitWebView *webview, WebKitWebFrame * static void vp_destroy_window_cb(GtkWidget* widget, GtkWidget* window, gpointer user_data) { - vp_close_browser(0); + command_close(0); } static gboolean vp_frame_scrollbar_policy_changed_cb(void) @@ -189,7 +186,7 @@ static gboolean vp_process_input(const char* input) return success; } -static gboolean vp_load_uri(const Arg* arg) +gboolean vp_load_uri(const Arg* arg) { char* uri; char* line = arg->s; @@ -243,66 +240,7 @@ static const gchar* vp_get_cookies(SoupURI *uri) } #endif -gboolean vp_navigate(const Arg* arg) -{ - if (arg->i <= VP_NAVIG_FORWARD) { - /* TODO allow to set a count for the navigation */ - webkit_web_view_go_back_or_forward( - vp.gui.webview, (arg->i == VP_NAVIG_BACK ? -1 : 1) - ); - } else if (arg->i == VP_NAVIG_RELOAD) { - webkit_web_view_reload(vp.gui.webview); - } else if (arg->i == VP_NAVIG_RELOAD_FORCE) { - webkit_web_view_reload_bypass_cache(vp.gui.webview); - } else { - webkit_web_view_stop_loading(vp.gui.webview); - } - - return TRUE; -} - -gboolean vp_scroll(const Arg* arg) -{ - GtkAdjustment *adjust = (arg->i & VP_SCROLL_AXIS_H) ? vp.gui.adjust_h : vp.gui.adjust_v; - - gint direction = (arg->i & (1 << 2)) ? 1 : -1; - - /* type scroll */ - if (arg->i & VP_SCROLL_TYPE_SCROLL) { - gdouble value; - gint count = vp.state.count ? vp.state.count : 1; - if (arg->i & VP_SCROLL_UNIT_LINE) { - value = SCROLLSTEP; - } else if (arg->i & VP_SCROLL_UNIT_HALFPAGE) { - value = gtk_adjustment_get_page_size(adjust) / 2; - } else { - value = gtk_adjustment_get_page_size(adjust); - } - gtk_adjustment_set_value(adjust, gtk_adjustment_get_value(adjust) + direction * value * count); - } else if (vp.state.count) { - /* jump - if count is set to count% of page */ - gdouble max = gtk_adjustment_get_upper(adjust) - gtk_adjustment_get_page_size(adjust); - gtk_adjustment_set_value(adjust, max * vp.state.count / 100); - } else if (direction == 1) { - /* jump to top */ - gtk_adjustment_set_value(adjust, gtk_adjustment_get_upper(adjust)); - } else { - /* jump to bottom */ - gtk_adjustment_set_value(adjust, gtk_adjustment_get_lower(adjust)); - } - - return TRUE; -} - -gboolean vp_close_browser(const Arg* arg) -{ - vp_clean_up(); - gtk_main_quit(); - - return TRUE; -} - -static void vp_clean_up(void) +void vp_clean_up(void) { if (vp.behave.commands) { g_hash_table_destroy(vp.behave.commands); @@ -311,6 +249,9 @@ static void vp_clean_up(void) for (int i = FILES_FIRST; i < FILES_LAST; i++) { g_free(vp.files[i]); } + command_cleanup(); + setting_cleanup(); + keybind_cleanup(); } static gboolean vp_hide_message(void) @@ -326,25 +267,6 @@ static gboolean vp_hide_message(void) return FALSE; } -gboolean vp_view_source(const Arg* arg) -{ - gboolean mode = webkit_web_view_get_view_source_mode(vp.gui.webview); - webkit_web_view_set_view_source_mode(vp.gui.webview, !mode); - webkit_web_view_reload(vp.gui.webview); - - return TRUE; -} - -gboolean vp_map(const Arg* arg) -{ - return keybind_add_from_string(arg->s, arg->i); -} - -gboolean vp_unmap(const Arg* arg) -{ - return keybind_remove_from_string(arg->s, arg->i); -} - gboolean vp_set_mode(const Arg* arg) { vp.state.mode = arg->i; @@ -378,66 +300,6 @@ gboolean vp_set_mode(const Arg* arg) return TRUE; } -gboolean vp_input(const Arg* arg) -{ - gint pos = 0; - const gchar* url; - - /* reset the colors and fonts to defalts */ - vp_set_widget_font(vp.gui.inputbox, inputbox_font[0], inputbox_bg[0], inputbox_fg[0]); - - /* remove content from input box */ - gtk_entry_set_text(GTK_ENTRY(vp.gui.inputbox), ""); - - /* insert string from arg */ - gtk_editable_insert_text(GTK_EDITABLE(vp.gui.inputbox), arg->s, -1, &pos); - - /* add current url if requested */ - if (VP_INPUT_CURRENT_URI == arg->i - && (url = webkit_web_view_get_uri(vp.gui.webview))) { - gtk_editable_insert_text(GTK_EDITABLE(vp.gui.inputbox), url, -1, &pos); - } - - gtk_editable_set_position(GTK_EDITABLE(vp.gui.inputbox), -1); - - Arg a = {VP_MODE_COMMAND}; - return vp_set_mode(&a); -} - -gboolean vp_open(const Arg* arg) -{ - return vp_load_uri(arg); -} - -gboolean vp_set(const Arg* arg) -{ - gboolean success; - gchar* line = NULL; - gchar** token; - - if (!arg->s || !strlen(arg->s)) { - return FALSE; - } - - line = g_strdup(arg->s); - g_strstrip(line); - - /* split the input string into paramete and value part */ - token = g_strsplit(line, "=", 2); - g_free(line); - - if (!token[1]) { - /* TODO display current value */ - g_strfreev(token); - vp_echo(VP_MSG_ERROR, "No param given"); - return FALSE; - } - success = setting_run(token[0], token[1] ? token[1] : NULL); - g_strfreev(token); - - return success; -} - void vp_update_urlbar(const gchar* uri) { gchar* markup; @@ -651,7 +513,7 @@ static void vp_init_files(void) g_free(path); } -static void vp_set_widget_font(GtkWidget* widget, const gchar* font_definition, const gchar* bg_color, const gchar* fg_color) +void vp_set_widget_font(GtkWidget* widget, const gchar* font_definition, const gchar* bg_color, const gchar* fg_color) { GdkColor fg, bg; PangoFontDescription *font; diff --git a/src/main.h b/src/main.h index a20125c..719f205 100644 --- a/src/main.h +++ b/src/main.h @@ -180,15 +180,9 @@ extern VpCore vp; void vp_update_statusbar(void); void vp_update_urlbar(const gchar* uri); void vp_echo(const MessageType type, const char *error, ...); -gboolean vp_navigate(const Arg* arg); -gboolean vp_scroll(const Arg* arg); -gboolean vp_close_browser(const Arg* arg); -gboolean vp_view_source(const Arg* arg); -gboolean vp_map(const Arg* arg); -gboolean vp_unmap(const Arg* arg); gboolean vp_set_mode(const Arg* arg); -gboolean vp_input(const Arg* arg); -gboolean vp_open(const Arg* arg); -gboolean vp_set(const Arg* arg); +void vp_set_widget_font(GtkWidget* widget, const gchar* font_definition, const gchar* bg_color, const gchar* fg_color); +gboolean vp_load_uri(const Arg* arg); +void vp_clean_up(void); #endif /* end of include guard: MAIN_H */ diff --git a/src/setting.c b/src/setting.c index 8062ede..b64bb31 100644 --- a/src/setting.c +++ b/src/setting.c @@ -91,6 +91,13 @@ void setting_init(void) } } +void setting_cleanup(void) +{ + if (settings) { + g_hash_table_destroy(settings); + } +} + gboolean setting_run(const gchar* name, const gchar* param) { gboolean result = FALSE; diff --git a/src/setting.h b/src/setting.h index d3150b0..8004c16 100644 --- a/src/setting.h +++ b/src/setting.h @@ -33,6 +33,7 @@ struct _Setting { }; void setting_init(void); +void setting_cleanup(void); gboolean setting_run(const gchar* name, const gchar* param); #endif /* end of include guard: SETTING_H */ -- 2.20.1