Skip to content
Browse files

Adding support structure for :bind command, allowing users to specify…

… their

own keybindings.

Still needs testing, though this is a simple addition.
  • Loading branch information...
1 parent 07c5395 commit 4af6f273d4379409833fed7999312600355e34d3 @ryanflannery committed
Showing with 1,094 additions and 797 deletions.
  1. +0 −106 config.h
  2. +16 −8 enums.h
  3. +900 −576 keybindings.c
  4. +128 −54 keybindings.h
  5. +4 −4 meta_info.h
  6. +3 −3 paint.c
  7. +30 −30 uinterface.c
  8. +1 −1 uinterface.h
  9. +6 −11 vitunes.c
  10. +6 −4 vitunes.h
View
106 config.h
@@ -24,112 +24,6 @@ playmode DEFAULT_PLAYER_MODE = PLAYER_MODE_LOOP;
char *DEFAULT_PLAYER_PATH = "/usr/local/bin/mplayer";
char *DEFAULT_PLAYER_ARGS[] = { "mplayer", "-slave", "-idle", "-quiet", "-msglevel", "cplayer=0:ao=0:vo=0:decaudio=0:decvideo=0:demuxer=0", NULL };
-/*
- * XXX IMPORTANT NOTE:
- * If you're having problems with the keybindings on your platform, most
- * likely it's because the key-mappings below between "CONTROL+x" are
- * different on your system. I'm trying to find a better way of mapping
- * these, but ncurses(3) does not appear to be able to handle these (via
- * keypad(3) or any other mechanism) in some reasonable fashion.
- * Thoughts? Investigate further... TODO
- */
-
-#define K_TAB 9
-#define K_ENTER 13
-#define K_CNTRL_B 2
-#define K_CNTRL_C 3
-#define K_CNTRL_D 4
-#define K_CNTRL_E 5
-#define K_CNTRL_F 6
-#define K_CNTRL_L 12
-#define K_CNTRL_P 16
-#define K_CNTRL_R 18
-#define K_CNTRL_U 21
-#define K_CNTRL_Y 25
-#define K_CNTRL_SLASH 28
-
-
-/*
- * List of keybindings. See input_handlers.h for relevant structs.
- * Each function listed here is in input_handlers.*
- */
-#define ARG_NOT_USED { .num = 0 }
-
-const keybinding KeyBindings[] = {
- { K_CNTRL_C, quit_vitunes, ARG_NOT_USED },
- { K_CNTRL_SLASH, quit_vitunes, ARG_NOT_USED },
-
- { K_ENTER, load_or_play, ARG_NOT_USED },
- { 'm', show_file_info, ARG_NOT_USED },
- { K_CNTRL_P, pause_playback, ARG_NOT_USED },
- { 'z', pause_playback, ARG_NOT_USED },
- { 's', stop_playback, ARG_NOT_USED },
- { '[', seek_playback, { .direction = BACKWARDS, .scale = SECONDS, .num = 10 }},
- { ']', seek_playback, { .direction = FORWARDS, .scale = SECONDS, .num = 10 }},
- { 'b', seek_playback, { .direction = BACKWARDS, .scale = SECONDS, .num = 10 }},
- { 'f', seek_playback, { .direction = FORWARDS, .scale = SECONDS, .num = 10 }},
- { '{', seek_playback, { .direction = BACKWARDS, .scale = MINUTES, .num = 1 }},
- { '}', seek_playback, { .direction = FORWARDS, .scale = MINUTES, .num = 1 }},
- { 'B', seek_playback, { .direction = BACKWARDS, .scale = MINUTES, .num = 1 }},
- { 'F', seek_playback, { .direction = FORWARDS, .scale = MINUTES, .num = 1 }},
-
- { K_TAB, switch_focus, ARG_NOT_USED },
- { K_CNTRL_L, redraw, ARG_NOT_USED },
-
- { 'j', scroll_row, { .direction = DOWN }},
- { KEY_DOWN, scroll_row, { .direction = DOWN }},
- { 'k', scroll_row, { .direction = UP }},
- { KEY_UP, scroll_row, { .direction = UP }},
- { '-', scroll_row, { .direction = UP }},
-
- { 'h', scroll_col, { .direction = LEFT, .amount = SINGLE }},
- { KEY_LEFT, scroll_col, { .direction = LEFT, .amount = SINGLE }},
- { KEY_BACKSPACE, scroll_col, { .direction = LEFT, .amount = SINGLE }},
- { 'l', scroll_col, { .direction = RIGHT, .amount = SINGLE }},
- { KEY_RIGHT, scroll_col, { .direction = RIGHT, .amount = SINGLE }},
- { ' ', scroll_col, { .direction = RIGHT, .amount = SINGLE }},
- { '$', scroll_col, { .direction = RIGHT, .amount = WHOLE }},
- { '^', scroll_col, { .direction = LEFT, .amount = WHOLE }},
- { '0', scroll_col, { .direction = LEFT, .amount = WHOLE }},
- { '|', scroll_col, { .direction = LEFT, .amount = WHOLE }},
-
- { K_CNTRL_E, scroll_page, { .direction = DOWN, .amount = SINGLE }},
- { K_CNTRL_Y, scroll_page, { .direction = UP, .amount = SINGLE }},
- { K_CNTRL_U, scroll_page, { .direction = UP, .amount = HALF }},
- { K_CNTRL_D, scroll_page, { .direction = DOWN, .amount = HALF }},
- { K_CNTRL_B, scroll_page, { .direction = UP, .amount = WHOLE }},
- { KEY_PPAGE, scroll_page, { .direction = UP, .amount = WHOLE }},
- { K_CNTRL_F, scroll_page, { .direction = DOWN, .amount = WHOLE }},
- { KEY_NPAGE, scroll_page, { .direction = DOWN, .amount = WHOLE }},
-
- { 'g', jumpto_file, { .scale = NUMBER, .num = 'g' }},
- { 'G', jumpto_file, { .scale = NUMBER, .num = 'G' }},
- { '%', jumpto_file, { .scale = PERCENT }},
-
- { 'H', jumpto_page, { .placement = TOP }},
- { 'M', jumpto_page, { .placement = MIDDLE }},
- { 'L', jumpto_page, { .placement = BOTTOM }},
-
- { 'y', yank, ARG_NOT_USED },
- { 'd', cut, ARG_NOT_USED },
- { 'p', paste, { .placement = AFTER }},
- { 'P', paste, { .placement = BEFORE }},
- { 'u', undo, ARG_NOT_USED },
- { K_CNTRL_R, redo, ARG_NOT_USED },
-
- { '/', search, { .direction = FORWARDS }},
- { '?', search, { .direction = BACKWARDS }},
- { 'n', search_find, { .direction = SAME }},
- { 'N', search_find, { .direction = OPPOSITE }},
-
- { ':', enter_cmd_mode, ARG_NOT_USED },
- { '!', external_command, ARG_NOT_USED }
-};
-const int KeyBindingsSize = (sizeof(KeyBindings) / sizeof(keybinding));
-
-#define ExecuteKeyBinding(i) \
- ((KeyBindings[(i)].func)(KeyBindings[(i)].args))
-
/*
* List of command-mode commands. See input_handlers.h for cmd struct.
View
24 enums.h
@@ -9,14 +9,22 @@
typedef enum {
UP, DOWN,
LEFT, RIGHT,
- TOP, MIDDLE, BOTTOM,
- SINGLE, HALF, PAGE, WHOLE,
- BEFORE, AFTER,
- FORWARDS, BACKWARDS,
- SECONDS, MINUTES,
- NUMBER, PERCENT,
+ BACKWARDS, FORWARDS,
SAME, OPPOSITE
-} venum;
-/* vitunes enum */
+} Direction;
+
+typedef enum {
+ SECONDS, MINUTES,
+ NUMBER, PERCENT
+} Scale;
+
+typedef enum {
+ SINGLE, HALF, WHOLE
+} Amount;
+
+typedef enum {
+ TOP, MIDDLE, BOTTOM,
+ BEFORE, AFTER
+} Placement;
#endif
View
1,476 keybindings.c
900 additions, 576 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
182 keybindings.h
@@ -17,31 +17,151 @@
#ifndef KEYBINDINGS_H
#define KEYBINDINGS_H
+#include "debug.h"
#include "enums.h"
#include "paint.h"
-#include "str2argv.h"
#include "vitunes.h"
-#include "debug.h"
+/* keybinding actions */
+typedef enum {
+ /* scrolling vertically */
+ scroll_up,
+ scroll_down,
+ scroll_up_page,
+ scroll_down_page,
+ scroll_up_halfpage,
+ scroll_down_halfpage,
+ scroll_up_wholepage,
+ scroll_down_wholepage,
+ /* scrolling horizontally */
+ scroll_left,
+ scroll_right,
+ scroll_leftmost,
+ scroll_rightmost,
+ /* jumping within screen */
+ jumpto_screen_top,
+ jumpto_screen_middle,
+ jumpto_screen_bottom,
+ /* jumping within whole playlist/library */
+ jumpto_line,
+ jumpto_percent,
+ /* searching */
+ search_forward,
+ search_backward,
+ find_next_forward,
+ find_next_backward,
+ /* copy/cut/paste & undo/redo */
+ cut,
+ yank,
+ paste_after,
+ paste_before,
+ undo,
+ redo,
+ /* misc. */
+ quit,
+ redraw,
+ command_mode,
+ shell,
+ /* vitunes specific stuff */
+ switch_windows,
+ show_file_info,
+ load_playlist,
+ media_play,
+ media_pause,
+ media_stop,
+ seek_forward_seconds,
+ seek_backward_seconds,
+ seek_forward_minutes,
+ seek_backward_minutes,
+
+} KeyAction;
+
+typedef int KeyCode;
+
+
+/* keybinding manipulation & retrieval routines */
+void kb_init();
+void kb_free();
+void kb_bind(KeyAction, KeyCode);
+bool kb_execute(KeyCode k);
+
+bool kb_is_action(char *s);
+KeyAction kb_str2action(char * s);
+
+
+/*
+ * This is the generic argument structure for all keybinding action handlers.
+ * This is used liberally to prevent any intricate parsing and make the use
+ * of code-reuse.
+ */
+typedef struct {
+ Direction direction;
+ Scale scale;
+ Amount amount;
+ Placement placement;
+ int num;
+} KbaArgs;
+
+/* Function used to retrieve handler for a given keycode */
+typedef void(*ActionHandler)(KbaArgs a);
+ActionHandler kb_get_handler(KeyCode k);
+
+/* Individual keybinding action handlers */
+void kba_scroll_row(KbaArgs a);
+void kba_scroll_page(KbaArgs a);
+void kba_scroll_col(KbaArgs a);
+
+void kba_jumpto_screen(KbaArgs a);
+void kba_jumpto_file(KbaArgs a);
+
+void kba_search(KbaArgs a);
+void kba_search_find(KbaArgs a);
+
+void kba_cut(KbaArgs a);
+void kba_yank(KbaArgs a);
+void kba_paste(KbaArgs a);
+void kba_undo(KbaArgs a);
+void kba_redo(KbaArgs a);
+
+void kba_command_mode(KbaArgs a);
+void kba_shell(KbaArgs a);
+void kba_quit(KbaArgs a);
+void kba_redraw(KbaArgs a);
+
+void kba_switch_windows(KbaArgs a);
+void kba_show_file_info(KbaArgs a);
+void kba_load_playlist(KbaArgs a);
+void kba_play(KbaArgs a);
+void kba_pause(KbaArgs a);
+void kba_stop(KbaArgs a);
+void kba_seek(KbaArgs a);
-/* for working with number read from user for keybindings */
+
+/*
+ * The 'gnum' is the number n that can be applied to many keybindings. E.g.
+ * how the sequence "15j" will move down 15 lines. "15" is the gnum here.
+ * These are the routines used to init/set/clear the gnum.
+ */
void gnum_clear();
+void gnum_set(int x);
int gnum_get();
+int gnum_retrieve(); /* XXX returns 1 if gnum not set, num otherwise */
void gnum_add(int x);
-/* for working with active search direction */
-venum search_dir_get();
-void search_dir_set(venum dir);
+/* These are used to set/use the search direction */
+Direction search_dir_get();
+void search_dir_set(Direction d);
-/* for working with yank/cut buffer */
+/* These work with the copy/cut buffer */
#define YANK_BUFFER_CHUNK_SIZE 100
typedef struct {
meta_info **files;
int nfiles;
int capacity;
} yank_buffer;
+extern yank_buffer _yank_buffer;
void ybuffer_init();
void ybuffer_clear();
@@ -49,55 +169,9 @@ void ybuffer_free();
void ybuffer_add(meta_info *f);
-/* misc. handy functions used frequently */
+/* Misc. handy functions used frequently */
void redraw_active();
bool match_command_name(const char *s, const char *cmd);
void execute_external_command(const char *cmd);
-
-/*
- * The arguments passed to each keybinding. No parsing required.
- */
-typedef struct {
- venum amount;
- venum direction;
- venum placement;
- venum scale;
- int num;
-} Args;
-
-typedef struct {
- int keycode;
- void (*func)(Args);
- Args args;
-} keybinding;
-
-extern const keybinding KeyBindings[];
-extern const int KeyBindingsSize;
-
-
-/* keybinding handlers */
-void quit_vitunes(Args a);
-void load_or_play(Args a);
-void show_file_info(Args a);
-void pause_playback(Args a);
-void stop_playback(Args a);
-void seek_playback(Args a);
-void switch_focus(Args a);
-void redraw(Args a);
-void enter_cmd_mode(Args a);
-void external_command(Args a);
-void scroll_row(Args a);
-void scroll_col(Args a);
-void scroll_page(Args a);
-void jumpto_file(Args a);
-void jumpto_page(Args a);
-void yank(Args a);
-void paste(Args a);
-void cut(Args a);
-void undo(Args a);
-void redo(Args a);
-void search(Args a);
-void search_find(Args a);
-
#endif
View
8 meta_info.h
@@ -163,10 +163,10 @@ int mi_compare(const void *a, const void *b);
/* structure used to describe how to display meta_info structs */
typedef struct {
- int nfields;
- int order[MI_NUM_CINFO];
- int widths[MI_NUM_CINFO];
- venum align[MI_NUM_CINFO];
+ int nfields;
+ int order[MI_NUM_CINFO];
+ int widths[MI_NUM_CINFO];
+ Direction align[MI_NUM_CINFO];
} mi_display_description;
extern mi_display_description mi_display;
View
6 paint.c
@@ -21,7 +21,7 @@ _colors colors;
bool showing_file_info = false;
char *player_get_field2show(const meta_info *mi);
-char *num2fmt(int n, venum a);
+char *num2fmt(int n, Direction d);
/*
@@ -62,7 +62,7 @@ player_get_field2show(const meta_info *mi)
* style string ("%Ns" or "%-Ns")
*/
char *
-num2fmt(int n, venum a)
+num2fmt(int n, Direction d)
{
static char format[255];
@@ -71,7 +71,7 @@ num2fmt(int n, venum a)
errx(1, "num2sfmt: invalid number %d provided", n);
}
- if (a == LEFT)
+ if (d == LEFT)
snprintf(format, sizeof(format), "%%-%d.%ds", n, n);
else
snprintf(format, sizeof(format), "%%%d.%ds", n, n);
View
60 uinterface.c
@@ -64,38 +64,38 @@ swindow_free(swindow *win)
}
void
-swindow_scroll(swindow *win, venum d, int n)
+swindow_scroll(swindow *win, Direction d, int n)
{
switch (d) {
- case UP:
- win->voffset -= n;
- if (win->voffset < 0)
- win->voffset = 0;
- break;
-
- case DOWN:
- win->voffset += n;
- if (win->voffset >= win->nrows - win->h)
- win->voffset = win->nrows - win->h;
- if (win->voffset < 0)
- win->voffset = 0;
- break;
-
- case LEFT:
- win->hoffset -= n;
- if (win->hoffset < 0)
- win->hoffset = 0;
- break;
-
- case RIGHT:
- win->hoffset += n;
- /* NOTE: "overflow" here is handled elsewhere.
- * see input_handlers.c, scroll_col()
- */
- break;
-
- default:
- err(1, "swindow_scroll: bad direction");
+ case UP:
+ win->voffset -= n;
+ if (win->voffset < 0)
+ win->voffset = 0;
+ break;
+
+ case DOWN:
+ win->voffset += n;
+ if (win->voffset >= win->nrows - win->h)
+ win->voffset = win->nrows - win->h;
+ if (win->voffset < 0)
+ win->voffset = 0;
+ break;
+
+ case LEFT:
+ win->hoffset -= n;
+ if (win->hoffset < 0)
+ win->hoffset = 0;
+ break;
+
+ case RIGHT:
+ win->hoffset += n;
+ /* NOTE: "overflow" here is handled elsewhere.
+ * see input_handlers.c, scroll_col()
+ */
+ break;
+
+ default:
+ err(1, "swindow_scroll: bad direction");
}
}
View
2 uinterface.h
@@ -48,7 +48,7 @@ typedef struct
swindow *swindow_new(int h, int w, int y, int x);
void swindow_free(swindow *win);
void swindow_resize(swindow *win, int h, int w, int y, int x);
-void swindow_scroll(swindow *win, venum d, int n);
+void swindow_scroll(swindow *win, Direction d, int n);
/* user interface struct & methods */
View
17 vitunes.c
@@ -78,7 +78,7 @@ main(int argc, char *argv[])
{
struct passwd *pwd;
int previous_command;
- int input, i;
+ int input;
#ifdef DEBUG
if ((debug_log = fopen("vitunes-debug.log", "w")) == NULL)
@@ -163,6 +163,8 @@ main(int argc, char *argv[])
/* initial painting of the display */
paint_all();
+ kb_init();
+
/* -----------------------------------------------------------------------
* begin input loop
* -------------------------------------------------------------------- */
@@ -178,16 +180,9 @@ main(int argc, char *argv[])
if (isdigit(input) && (input != '0' || gnum_get() > 0))
gnum_add(input - '0');
else if (input == '\n' && gnum_get() > 0 && previous_command >= 0)
- ExecuteKeyBinding(previous_command);
- else {
- /* check if input is bound and execute... */
- for (i = 0; i < KeyBindingsSize; i++) {
- if (KeyBindings[i].keycode == input) {
- ExecuteKeyBinding(i);
- previous_command = i;
- }
- }
- }
+ kb_execute(previous_command);
+ else
+ kb_execute(input);
}
}
View
10 vitunes.h
@@ -35,16 +35,18 @@
#include "uinterface.h"
#include "e_commands.h"
+/*
+ * These are the various things defined in vitunes.c used elsewhere.
+ */
+
/* record keeping */
extern playlist *viewing_playlist;
extern playlist *playing_playlist;
-/* signal flags (references elsewhere) */
+/* signal flags referenced elsewhere */
extern volatile sig_atomic_t VSIG_QUIT;
-extern volatile sig_atomic_t VSIG_RESIZE;
-extern volatile sig_atomic_t VSIG_PLAYER_MONITOR;
-extern volatile sig_atomic_t VSIG_PLAYER_RESTART;
+/* other */
void load_config();
void process_signals(bool);

0 comments on commit 4af6f27

Please sign in to comment.
Something went wrong with that request. Please try again.