fbd9905cb4b23ac43762486ea5f5efcf5a7c130d
1 /*
2 * (c) 2004 by Kalle Wallin <kaw@linux.se>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
19 #include "config.h"
21 #ifndef DISABLE_KEYDEF_SCREEN
22 #include "ncmpc.h"
23 #include "mpdclient.h"
24 #include "options.h"
25 #include "conf.h"
26 #include "command.h"
27 #include "screen.h"
28 #include "screen_utils.h"
29 #include "gcc.h"
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <glib.h>
35 #include <ncurses.h>
37 #define STATIC_ITEMS 0
38 #define STATIC_SUB_ITEMS 1
39 #define BUFSIZE 256
41 #define LIST_ITEM_APPLY() ((unsigned)command_list_length)
42 #define LIST_ITEM_SAVE() (LIST_ITEM_APPLY()+1)
43 #define LIST_LENGTH() (LIST_ITEM_SAVE()+1)
45 #define LIST_ITEM_SAVE_LABEL _("===> Apply & Save key bindings ")
46 #define LIST_ITEM_APPLY_LABEL _("===> Apply key bindings ")
49 static list_window_t *lw = NULL;
50 static unsigned command_list_length = 0;
51 static command_definition_t *cmds = NULL;
53 static int subcmd = -1;
54 static unsigned subcmd_length = 0;
55 static unsigned subcmd_addpos = 0;
57 static int
58 keybindings_changed(void)
59 {
60 command_definition_t *orginal_cmds = get_command_definitions();
61 size_t size = command_list_length * sizeof(command_definition_t);
63 return memcmp(orginal_cmds, cmds, size);
64 }
66 static void
67 apply_keys(void)
68 {
69 if (keybindings_changed()) {
70 command_definition_t *orginal_cmds = get_command_definitions();
71 size_t size = command_list_length * sizeof(command_definition_t);
73 memcpy(orginal_cmds, cmds, size);
74 screen_status_printf(_("You have new key bindings!"));
75 } else
76 screen_status_printf(_("Keybindings unchanged."));
77 }
79 static int
80 save_keys(void)
81 {
82 FILE *f;
83 char *filename;
85 if (check_user_conf_dir()) {
86 screen_status_printf(_("Error: Unable to create direcory ~/.ncmpc - %s"),
87 strerror(errno));
88 screen_bell();
89 return -1;
90 }
92 filename = get_user_key_binding_filename();
94 if ((f = fopen(filename,"w")) == NULL) {
95 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
96 screen_bell();
97 g_free(filename);
98 return -1;
99 }
101 if (write_key_bindings(f, KEYDEF_WRITE_HEADER))
102 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
103 else
104 screen_status_printf(_("Wrote %s"), filename);
106 g_free(filename);
107 return fclose(f);
108 }
110 static void
111 check_subcmd_length(void)
112 {
113 subcmd_length = 0;
114 while (subcmd_length < MAX_COMMAND_KEYS &&
115 cmds[subcmd].keys[subcmd_length] > 0)
116 ++subcmd_length;
118 if (subcmd_length < MAX_COMMAND_KEYS) {
119 subcmd_addpos = subcmd_length;
120 subcmd_length++;
121 } else
122 subcmd_addpos = 0;
123 subcmd_length += STATIC_SUB_ITEMS;
124 }
126 static void
127 delete_key(int cmd_index, int key_index)
128 {
129 int i = key_index+1;
131 screen_status_printf(_("Deleted"));
132 while (i < MAX_COMMAND_KEYS && cmds[cmd_index].keys[i])
133 cmds[cmd_index].keys[key_index++] = cmds[cmd_index].keys[i++];
134 cmds[cmd_index].keys[key_index] = 0;
135 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
136 check_subcmd_length();
137 lw->repaint = 1;
138 /* update key conflict flags */
139 check_key_bindings(cmds, NULL, 0);
140 }
142 static void
143 assign_new_key(WINDOW *w, int cmd_index, int key_index)
144 {
145 int key;
146 char *buf;
147 command_t cmd;
149 buf = g_strdup_printf(_("Enter new key for %s: "), cmds[cmd_index].name);
150 key = screen_getch(w, buf);
151 g_free(buf);
153 if (key == KEY_RESIZE)
154 screen_resize();
156 if (key==ERR) {
157 screen_status_printf(_("Aborted!"));
158 return;
159 }
161 cmd = find_key_command(key, cmds);
162 if (cmd != CMD_NONE && cmd != cmds[cmd_index].command) {
163 screen_status_printf(_("Error: key %s is already used for %s"),
164 key2str(key),
165 get_key_command_name(cmd));
166 screen_bell();
167 return;
168 }
170 cmds[cmd_index].keys[key_index] = key;
171 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
173 screen_status_printf(_("Assigned %s to %s"),
174 key2str(key),cmds[cmd_index].name);
175 check_subcmd_length();
176 lw->repaint = 1;
177 /* update key conflict flags */
178 check_key_bindings(cmds, NULL, 0);
179 }
181 static const char *
182 list_callback(unsigned idx, int *highlight, mpd_unused void *data)
183 {
184 static char buf[BUFSIZE];
186 if (subcmd < 0) {
187 if (idx < (unsigned)command_list_length) {
188 if (cmds[idx].flags & COMMAND_KEY_CONFLICT)
189 *highlight = 1;
190 return cmds[idx].name;
191 } else if (idx == LIST_ITEM_APPLY())
192 return LIST_ITEM_APPLY_LABEL;
193 else if (idx == LIST_ITEM_SAVE())
194 return LIST_ITEM_SAVE_LABEL;
195 } else {
196 if (idx == 0)
197 return "[..]";
198 idx--;
199 if (idx < MAX_COMMAND_KEYS && cmds[subcmd].keys[idx] > 0) {
200 g_snprintf(buf,
201 BUFSIZE, "%d. %-20s (%d) ",
202 idx + 1,
203 key2str(cmds[subcmd].keys[idx]),
204 cmds[subcmd].keys[idx]);
205 return buf;
206 } else if (idx == subcmd_addpos) {
207 g_snprintf(buf, BUFSIZE, _("%d. Add new key "), idx + 1);
208 return buf;
209 }
210 }
212 return NULL;
213 }
215 static void
216 keydef_init(WINDOW *w, int cols, int rows)
217 {
218 lw = list_window_init(w, cols, rows);
219 }
221 static void
222 keydef_resize(int cols, int rows)
223 {
224 lw->cols = cols;
225 lw->rows = rows;
226 }
228 static void
229 keydef_exit(void)
230 {
231 list_window_free(lw);
232 if (cmds)
233 g_free(cmds);
234 cmds = NULL;
235 lw = NULL;
236 }
238 static void
239 keydef_open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
240 {
241 if (cmds == NULL) {
242 command_definition_t *current_cmds = get_command_definitions();
243 size_t cmds_size;
245 command_list_length = 0;
246 while (current_cmds[command_list_length].name)
247 command_list_length++;
249 cmds_size = (command_list_length+1) * sizeof(command_definition_t);
250 cmds = g_malloc0(cmds_size);
251 memcpy(cmds, current_cmds, cmds_size);
252 command_list_length += STATIC_ITEMS;
253 screen_status_printf(_("Welcome to the key editor!"));
254 }
256 subcmd = -1;
257 list_window_check_selected(lw, LIST_LENGTH());
258 }
260 static void
261 keydef_close(void)
262 {
263 if (cmds && !keybindings_changed()) {
264 g_free(cmds);
265 cmds = NULL;
266 } else
267 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
268 }
270 static const char *
271 keydef_title(char *str, size_t size)
272 {
273 if (subcmd < 0)
274 return _("Edit key bindings");
276 g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
277 return str;
278 }
280 static void
281 keydef_paint(mpd_unused mpdclient_t *c)
282 {
283 list_window_paint(lw, list_callback, NULL);
284 }
286 static void
287 keydef_update(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
288 {
289 if (lw->repaint) {
290 list_window_paint(lw, list_callback, NULL);
291 lw->repaint = 0;
292 }
293 }
295 static int
296 keydef_cmd(screen_t *screen, mpd_unused mpdclient_t *c, command_t cmd)
297 {
298 int length = LIST_LENGTH();
300 if (subcmd >= 0)
301 length = subcmd_length;
303 switch(cmd) {
304 case CMD_PLAY:
305 if (subcmd < 0) {
306 if (lw->selected == LIST_ITEM_APPLY())
307 apply_keys();
308 else if (lw->selected == LIST_ITEM_SAVE()) {
309 apply_keys();
310 save_keys();
311 } else {
312 subcmd = lw->selected;
313 lw->selected=0;
314 check_subcmd_length();
315 }
316 } else {
317 if (lw->selected == 0) { /* up */
318 lw->selected = subcmd;
319 subcmd = -1;
320 } else
321 assign_new_key(screen->status_window.w,
322 subcmd,
323 lw->selected - STATIC_SUB_ITEMS);
324 }
325 lw->repaint = 1;
326 return 1;
327 case CMD_DELETE:
328 if (subcmd >= 0 && lw->selected >= STATIC_SUB_ITEMS)
329 delete_key(subcmd, lw->selected - STATIC_SUB_ITEMS);
330 return 1;
331 break;
332 case CMD_SAVE_PLAYLIST:
333 apply_keys();
334 save_keys();
335 break;
336 case CMD_LIST_FIND:
337 case CMD_LIST_RFIND:
338 case CMD_LIST_FIND_NEXT:
339 case CMD_LIST_RFIND_NEXT:
340 return screen_find(screen,
341 lw, length,
342 cmd, list_callback, NULL);
344 default:
345 break;
346 }
348 return list_window_cmd(lw, length, cmd);
349 }
351 const struct screen_functions screen_keydef = {
352 .init = keydef_init,
353 .exit = keydef_exit,
354 .open = keydef_open,
355 .close = keydef_close,
356 .resize = keydef_resize,
357 .paint = keydef_paint,
358 .update = keydef_update,
359 .cmd = keydef_cmd,
360 .get_title = keydef_title,
361 };
363 #endif