1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "screen_keydef.h"
21 #include "screen_interface.h"
22 #include "screen_status.h"
23 #include "screen_find.h"
27 #include "screen_utils.h"
35 static struct list_window
*lw
;
37 static command_definition_t
*cmds
= NULL
;
39 /** the number of commands */
40 static unsigned command_n_commands
= 0;
43 * the position of the "apply" item. It's the same as command_n_commands,
44 * because array subscripts start at 0, while numbers of items start at 1.
47 static inline unsigned
48 command_item_apply(void)
50 return command_n_commands
;
53 /** the position of the "apply and save" item */
55 static inline unsigned
56 command_item_save(void)
58 return command_item_apply() + 1;
61 /** the number of items in the "command" view */
63 static inline unsigned
66 return command_item_save() + 1;
71 * The command being edited, represented by a array subscript to @cmds, or -1,
72 * if no command is being edited
74 static int subcmd
= -1;
76 /** The number of keys assigned to the current command */
77 static unsigned subcmd_n_keys
= 0;
79 /** The position of the up ("[..]") item */
81 static inline unsigned
87 /** The position of the "add a key" item */
89 static inline unsigned
92 return subcmd_n_keys
+ 1;
95 /** The number of items in the list_window, if there's a command being edited */
97 static inline unsigned
100 return subcmd_item_add() + 1;
103 /** Check whether a given item is a key */
106 subcmd_item_is_key(unsigned i
)
108 return (i
> subcmd_item_up() && i
< subcmd_item_add());
112 * Convert an item id (as in lw->selected) into a "key id", which is an array
113 * subscript to cmds[subcmd].keys.
116 static inline unsigned
117 subcmd_item_to_key_id(unsigned i
)
124 keybindings_changed(void)
126 command_definition_t
*orginal_cmds
= get_command_definitions();
127 size_t size
= command_n_commands
* sizeof(command_definition_t
);
129 return memcmp(orginal_cmds
, cmds
, size
);
135 if (keybindings_changed()) {
136 command_definition_t
*orginal_cmds
= get_command_definitions();
137 size_t size
= command_n_commands
* sizeof(command_definition_t
);
139 memcpy(orginal_cmds
, cmds
, size
);
140 screen_status_printf(_("You have new key bindings"));
142 screen_status_printf(_("Keybindings unchanged."));
148 if (check_user_conf_dir()) {
149 screen_status_printf(_("Error: Unable to create directory ~/.ncmpc - %s"),
155 char *filename
= build_user_key_binding_filename();
156 FILE *f
= fopen(filename
, "w");
158 screen_status_printf(_("Error: %s - %s"), filename
, strerror(errno
));
164 if (write_key_bindings(f
, KEYDEF_WRITE_HEADER
))
165 screen_status_printf(_("Wrote %s"), filename
);
167 screen_status_printf(_("Error: %s - %s"), filename
, strerror(errno
));
173 /* TODO: rename to check_n_keys / subcmd_count_keys? */
175 check_subcmd_length(void)
179 /* this loops counts the continous valid keys at the start of the the keys
180 array, so make sure you don't have gaps */
181 for (i
= 0; i
< MAX_COMMAND_KEYS
; i
++)
182 if (cmds
[subcmd
].keys
[i
] == 0)
186 list_window_set_length(lw
, subcmd_length());
199 /** lw->start the last time switch_to_subcmd_mode() was called */
200 static unsigned saved_start
= 0;
203 switch_to_subcmd_mode(int cmd
)
205 assert(subcmd
== -1);
207 saved_start
= lw
->start
;
210 list_window_reset(lw
);
211 check_subcmd_length();
217 switch_to_command_mode(void)
219 assert(subcmd
!= -1);
221 list_window_set_length(lw
, command_length());
222 list_window_set_cursor(lw
, subcmd
);
225 lw
->start
= saved_start
;
231 * Delete a key from a given command's definition
232 * @param cmd_index the command
233 * @param key_index the key (see below)
236 delete_key(int cmd_index
, int key_index
)
238 /* shift the keys to close the gap that appeared */
240 while (i
< MAX_COMMAND_KEYS
&& cmds
[cmd_index
].keys
[i
])
241 cmds
[cmd_index
].keys
[key_index
++] = cmds
[cmd_index
].keys
[i
++];
243 /* As key_index now holds the index of the last key slot that contained
244 a key, we use it to empty this slot, because this key has been copied
245 to the previous slot in the loop above */
246 cmds
[cmd_index
].keys
[key_index
] = 0;
248 cmds
[cmd_index
].flags
|= COMMAND_KEY_MODIFIED
;
249 check_subcmd_length();
251 screen_status_printf(_("Deleted"));
256 /* update key conflict flags */
257 check_key_bindings(cmds
, NULL
, 0);
260 /* assigns a new key to a key slot */
262 overwrite_key(int cmd_index
, int key_index
)
264 assert(key_index
< MAX_COMMAND_KEYS
);
266 char *buf
= g_strdup_printf(_("Enter new key for %s: "),
267 cmds
[cmd_index
].name
);
268 const int key
= screen_getch(buf
);
272 screen_status_printf(_("Aborted"));
277 screen_status_printf(_("Ctrl-Space can't be used"));
281 const command_t cmd
= find_key_command(key
, cmds
);
282 if (cmd
!= CMD_NONE
) {
283 screen_status_printf(_("Error: key %s is already used for %s"),
284 key2str(key
), get_key_command_name(cmd
));
289 cmds
[cmd_index
].keys
[key_index
] = key
;
290 cmds
[cmd_index
].flags
|= COMMAND_KEY_MODIFIED
;
292 screen_status_printf(_("Assigned %s to %s"),
293 key2str(key
),cmds
[cmd_index
].name
);
294 check_subcmd_length();
299 /* update key conflict flags */
300 check_key_bindings(cmds
, NULL
, 0);
303 /* assign a new key to a new slot */
305 add_key(int cmd_index
)
307 if (subcmd_n_keys
< MAX_COMMAND_KEYS
)
308 overwrite_key(cmd_index
, subcmd_n_keys
);
312 list_callback(unsigned idx
, gcc_unused
void *data
)
314 static char buf
[256];
317 if (idx
== command_item_apply())
318 return _("===> Apply key bindings ");
319 if (idx
== command_item_save())
320 return _("===> Apply & Save key bindings ");
322 assert(idx
< (unsigned) command_n_commands
);
325 * Format the lines in two aligned columnes for the key name and
326 * the description, like this:
328 * this-command - do this
331 size_t len
= strlen(cmds
[idx
].name
);
332 strncpy(buf
, cmds
[idx
].name
, sizeof(buf
));
334 if (len
< get_cmds_max_name_width(cmds
))
335 memset(buf
+ len
, ' ', get_cmds_max_name_width(cmds
) - len
);
337 g_snprintf(buf
+ get_cmds_max_name_width(cmds
),
338 sizeof(buf
) - get_cmds_max_name_width(cmds
),
339 " - %s", _(cmds
[idx
].description
));
343 if (idx
== subcmd_item_up())
346 if (idx
== subcmd_item_add()) {
347 g_snprintf(buf
, sizeof(buf
), "%d. %s",
348 idx
, _("Add new key"));
352 assert(subcmd_item_is_key(idx
));
354 g_snprintf(buf
, sizeof(buf
),
355 "%d. %-20s (%d) ", idx
,
356 key2str(cmds
[subcmd
].keys
[subcmd_item_to_key_id(idx
)]),
357 cmds
[subcmd
].keys
[subcmd_item_to_key_id(idx
)]);
363 keydef_init(WINDOW
*w
, int cols
, int rows
)
365 lw
= list_window_init(w
, cols
, rows
);
369 keydef_resize(int cols
, int rows
)
371 list_window_resize(lw
, cols
, rows
);
377 list_window_free(lw
);
385 keydef_open(gcc_unused
struct mpdclient
*c
)
388 command_definition_t
*current_cmds
= get_command_definitions();
389 command_n_commands
= 0;
390 while (current_cmds
[command_n_commands
].name
)
391 command_n_commands
++;
393 /* +1 for the terminator element */
394 size_t cmds_size
= (command_n_commands
+ 1)
395 * sizeof(command_definition_t
);
396 cmds
= g_malloc0(cmds_size
);
397 memcpy(cmds
, current_cmds
, cmds_size
);
401 list_window_set_length(lw
, command_length());
407 if (cmds
&& !keybindings_changed()) {
411 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
415 keydef_title(char *str
, size_t size
)
418 return _("Edit key bindings");
420 g_snprintf(str
, size
, _("Edit keys for %s"), cmds
[subcmd
].name
);
427 list_window_paint(lw
, list_callback
, NULL
);
431 keydef_cmd(gcc_unused
struct mpdclient
*c
, command_t cmd
)
433 if (cmd
== CMD_LIST_RANGE_SELECT
)
436 if (list_window_cmd(lw
, cmd
)) {
444 if (lw
->selected
== command_item_apply()) {
446 } else if (lw
->selected
== command_item_save()) {
450 switch_to_subcmd_mode(lw
->selected
);
453 if (lw
->selected
== subcmd_item_up()) {
454 switch_to_command_mode();
455 } else if (lw
->selected
== subcmd_item_add()) {
458 /* just to be sure ;-) */
459 assert(subcmd_item_is_key(lw
->selected
));
460 overwrite_key(subcmd
, subcmd_item_to_key_id(lw
->selected
));
464 case CMD_GO_PARENT_DIRECTORY
:
465 case CMD_GO_ROOT_DIRECTORY
:
467 switch_to_command_mode();
470 if (subcmd
!= -1 && subcmd_item_is_key(lw
->selected
))
471 delete_key(subcmd
, subcmd_item_to_key_id(lw
->selected
));
478 case CMD_SAVE_PLAYLIST
:
484 case CMD_LIST_FIND_NEXT
:
485 case CMD_LIST_RFIND_NEXT
:
486 screen_find(lw
, cmd
, list_callback
, NULL
);
499 const struct screen_functions screen_keydef
= {
503 .close
= keydef_close
,
504 .resize
= keydef_resize
,
505 .paint
= keydef_paint
,
507 .get_title
= keydef_title
,