support for binding :commands to keys
[cmus.git] / cmus / irman_config.c
blob8fdccf5f0c37ef06d3922abbb2e4ec6d8431e54a
1 /*
2 * Copyright 2004-2005 Timo Hirvonen
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 * 02111-1307, USA.
20 #include <irman_config.h>
21 #include <irman.h>
22 #include <sconf.h>
23 #include <misc.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <string.h>
29 static char *button_names[] = {
30 "btn_play",
31 "btn_stop",
32 "btn_pause",
33 "btn_prev",
34 "btn_next",
35 "btn_seek_bwd",
36 "btn_seek_fwd",
37 "btn_vol_up",
38 "btn_vol_down",
39 "btn_mute",
40 "btn_shuffle",
41 "btn_repeat",
42 "btn_continue",
43 NULL
46 int irman_config(void)
48 struct irman *irman;
49 char device[1024];
50 int i;
52 do {
53 printf("device: ");
54 fflush(stdout);
55 if (fgets(device, sizeof(device), stdin) != NULL)
56 break;
57 } while (1);
58 i = strlen(device);
59 if (i > 0 && device[i - 1] == '\n')
60 device[i - 1] = 0;
62 irman = irman_open(device);
63 if (irman == NULL) {
64 fprintf(stderr, "error initialising irman: `%s'\n",
65 strerror(errno));
66 return 1;
68 for (i = 0; button_names[i]; i++) {
69 char code[IRMAN_CODE_LEN];
70 char text[IRMAN_TEXT_SIZE];
72 printf("%s: ", button_names[i]);
73 fflush(stdout);
74 if (irman_get_code(irman, code)) {
75 if (errno == ENXIO) {
76 fprintf(stderr, "irman not initialised (bug)\n");
77 } else {
78 fprintf(stderr, "error reading code: `%s'\n",
79 strerror(errno));
81 irman_close(irman);
82 return 1;
84 irman_code_to_text(text, code);
85 sconf_set_str_option(&sconf_head, button_names[i], text);
86 printf("OK\n");
88 irman_close(irman);
89 sconf_set_str_option(&sconf_head, "irman_device", device);
90 return 0;