1 /*
2  * Copyright (c) 2010, 2011 Ryan Flannery <ryan.flannery@gmail.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #ifndef KEYBINDINGS_H
18 #define KEYBINDINGS_H
19 
20 #include "debug.h"
21 #include "enums.h"
22 #include "paint.h"
23 #include "vitunes.h"
24 
25 #include "compat.h"
26 
27 /*
28  * List of all actions that can be bound by keybindings.
29  * NOTE: Using "count" trick (see last element), so no enum should be defined.
30  */
31 typedef enum {
32    /* scrolling vertically */
33    scroll_up,
34    scroll_down,
35    scroll_up_page,
36    scroll_down_page,
37    scroll_up_halfpage,
38    scroll_down_halfpage,
39    scroll_up_wholepage,
40    scroll_down_wholepage,
41    /* scrolling horizontally */
42    scroll_left,
43    scroll_right,
44    scroll_leftmost,
45    scroll_rightmost,
46    /* jumping within screen */
47    jumpto_screen_top,
48    jumpto_screen_middle,
49    jumpto_screen_bottom,
50    /* jumping within whole playlist/library */
51    jumpto_line,
52    jumpto_percent,
53    /* searching */
54    search_forward,
55    search_backward,
56    find_next_forward,
57    find_next_backward,
58    /* copy/cut/paste & undo/redo */
59    visual,
60    cut,
61    yank,
62    paste_after,
63    paste_before,
64    undo,
65    redo,
66    /* misc. */
67    go,
68    quit,
69    redraw,
70    command_mode,
71    shell,
72    /* vitunes specific stuff */
73    switch_windows,
74    show_file_info,
75    load_playlist,
76    media_play,
77    media_pause,
78    media_stop,
79    media_next,
80    media_prev,
81    volume_increase,
82    volume_decrease,
83    seek_forward_seconds,
84    seek_backward_seconds,
85    seek_forward_minutes,
86    seek_backward_minutes,
87    toggle_forward,
88    toggle_backward
89 } KeyAction;
90 
91 typedef int KeyCode;
92 
93 
94 /* Keybinding initializing and binding routines */
95 void kb_init();
96 void kb_free();
97 void kb_bind(KeyAction, KeyCode);
98 void kb_unbind_action(KeyAction);
99 void kb_unbind_key(KeyCode);
100 void kb_unbind_all();
101 bool kb_execute(KeyCode);
102 bool kb_execute_by_name(const char *);
103 
104 bool    kb_str2action(const char*, KeyAction*);
105 KeyCode kb_str2keycode(char*);
106 KeyCode kb_str2keycode2(char*, char*);
107 
108 
109 /*
110  * This is the generic argument structure for all keybinding action handlers.
111  * This is used liberally to prevent any intricate parsing and make heavy use
112  * of code-reuse.
113  */
114 typedef struct {
115    Direction   direction;
116    Scale       scale;
117    Amount      amount;
118    Placement   placement;
119    int         num;
120 } KbaArgs;
121 typedef void(*ActionHandler)(KbaArgs a);
122 
123 /* Individual keybinding action handlers */
124 void kba_scroll_row(KbaArgs a);
125 void kba_scroll_page(KbaArgs a);
126 void kba_scroll_col(KbaArgs a);
127 
128 void kba_jumpto_screen(KbaArgs a);
129 void kba_jumpto_file(KbaArgs a);
130 
131 void kba_search(KbaArgs a);
132 void kba_search_find(KbaArgs a);
133 
134 void kba_visual(KbaArgs a);
135 void kba_cut(KbaArgs a);
136 void kba_yank(KbaArgs a);
137 void kba_paste(KbaArgs a);
138 void kba_undo(KbaArgs a);
139 void kba_redo(KbaArgs a);
140 
141 void kba_command_mode(KbaArgs a);
142 void kba_go(KbaArgs a);
143 void kba_shell(KbaArgs a);
144 void kba_quit(KbaArgs a);
145 void kba_redraw(KbaArgs a);
146 
147 void kba_switch_windows(KbaArgs a);
148 void kba_show_file_info(KbaArgs a);
149 void kba_load_playlist(KbaArgs a);
150 void kba_play(KbaArgs a);
151 void kba_pause(KbaArgs a);
152 void kba_stop(KbaArgs a);
153 void kba_play_next(KbaArgs a);
154 void kba_play_prev(KbaArgs a);
155 void kba_volume(KbaArgs a);
156 void kba_seek(KbaArgs a);
157 void kba_toggle(KbaArgs a);
158 
159 
160 /*
161  * The 'gnum' is the number n that can be applied to many keybindings.  E.g.
162  * how the sequence "15j" will move down 15 lines.  "15" is the gnum here.
163  * These are the routines used to init/set/clear the gnum.
164  */
165 void gnum_clear();
166 void gnum_set(int x);
167 void gnum_add(int x);
168 int  gnum_get();        /* Return current gnum. */
169 int  gnum_retrieve();   /* Return current gnum and then clear it. */
170 
171 
172 /* These are used to set/use the search direction */
173 Direction search_dir_get();
174 void  search_dir_set(Direction d);
175 
176 
177 /* This is the copy/cut buffer and the routines used to manipulate it. */
178 #define YANK_BUFFER_CHUNK_SIZE 100
179 typedef struct {
180    meta_info   **files;
181    int           nfiles;
182    int           capacity;
183 } yank_buffer;
184 extern yank_buffer _yank_buffer;
185 
186 void ybuffer_init();
187 void ybuffer_clear();
188 void ybuffer_free();
189 void ybuffer_add(meta_info *f);
190 
191 
192 /* Misc. handy functions used frequently */
193 void redraw_active();
194 bool match_command_name(const char *s, const char *cmd);
195 void execute_external_command(const char *cmd);
196 
197 #endif
198