aboutsummaryrefslogtreecommitdiffstats
path: root/command.c
blob: 67e1a6975034d362d149f57cf960349392e96ee5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* 
 * $Id: command.c,v 1.9 2004/03/17 14:59:32 kalle Exp $ 
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <glib.h>
#include <ncurses.h>

#include "config.h"
#include "command.h"

#undef DEBUG_KEYS

#ifdef DEBUG_KEYS
#define DK(x) x
#else
#define DK(x)
#endif

#define BS   KEY_BACKSPACE
#define DEL  KEY_DC
#define UP   KEY_UP
#define DWN  KEY_DOWN
#define LEFT KEY_LEFT
#define RGHT KEY_RIGHT
#define HOME KEY_HOME
#define END  KEY_END
#define PGDN KEY_NPAGE
#define PGUP KEY_PPAGE
#define TAB  0x09
#define STAB 0x5A
#define ESC  0x1B
#define F1   KEY_F(1)
#define F2   KEY_F(2)
#define F3   KEY_F(3)
#define F4   KEY_F(4)
#define F5   KEY_F(5)
#define F6   KEY_F(6)

static command_definition_t cmds[] =
{
  { {  13,   0,   0 }, CMD_PLAY, "Play/Enter directory" },
  { { 'p',   0,   0 }, CMD_PAUSE, "Pause" },
  { {  BS, ESC,   0 }, CMD_STOP, "Stop" },
  { { '>',   0,   0 }, CMD_TRACK_NEXT, "Next song" },
  { { '<',   0,   0 }, CMD_TRACK_PREVIOUS, "Previous song" },

  { { '+', RGHT,  0 }, CMD_VOLUME_UP, "Increase volume" },
  { { '-', LEFT,  0 }, CMD_VOLUME_DOWN, "Decrease volume" },

  { { ' ',   0,  0 }, CMD_SELECT, "Select/deselect song in playlist" },
  { { DEL,   0,   0 }, CMD_DELETE, "Delete song from playlist" },
  { { 's',   0,   0 }, CMD_SHUFFLE, "Shuffle playlist" },
  { { 'c',   0,   0 }, CMD_CLEAR, "Clear playlist" },
  { { 'r',   0,   0 }, CMD_REPEAT, "Toggle repeat mode" },
  { { 'z',   0,   0 }, CMD_RANDOM, "Toggle random mode" },

  { {  UP,   0,   0 }, CMD_LIST_PREVIOUS,      "Move: Up" },
  { { DWN,   0,   0 }, CMD_LIST_NEXT,          "Move: Down" },
  { { HOME,  0,   0 }, CMD_LIST_FIRST,         "Move: Home" },
  { { END,   0,   0 }, CMD_LIST_LAST,          "Move: End" },
  { { PGUP,  0,   0 }, CMD_LIST_PREVIOUS_PAGE, "Move: Page Up" },
  { { PGDN,  0,   0 }, CMD_LIST_NEXT_PAGE,     "Move: Page Down" },
  { { '/',   0,   0 }, CMD_LIST_FIND,          "Find" },
  { { 'n',   0,   0 }, CMD_LIST_FIND_NEXT,     "Find Next" },

  { { TAB,   0,   0 }, CMD_SCREEN_NEXT,   "Next screen" },
  { { STAB,  0,   0 }, CMD_SCREEN_PREVIOUS, "Previous screen" },
  { { F1, '1', 'h' }, CMD_SCREEN_HELP,   "Help screen" },
  { { F2, '2',   0 }, CMD_SCREEN_PLAY,   "Playlist screen" },
  { { F3, '3',   0 }, CMD_SCREEN_FILE,   "Browse screen" },
  //  { { F4, '4',   0 }, CMD_SCREEN_SEARCH, "Search screen" },

  { { 'q',  0,   0 }, CMD_QUIT,   "Quit " PACKAGE },  

  { { -1,  -1,  -1 }, CMD_NONE, NULL }
};

char *
key2str(int key)
{
  static char buf[2];

  buf[0] = 0;
  switch(key)
    {
    case ' ':
      return "Space";
    case 13:
      return "Enter";
    case BS:
      return "Backspace";
    case DEL:
      return "Delete";
    case UP: 
      return "Up";
    case DWN:
      return "Down";
    case LEFT:
      return "Left";
    case RGHT:
      return "Right";
    case HOME:
      return "Home";
    case END:
      return "End";
    case PGDN:
      return "PageDown";
    case PGUP:
      return "PageUp";
    case TAB: 
      return "Tab";
    case STAB:
      return "Shift+Tab";
    case ESC:
      return "Esc";
    case F1:
      return "F1";
    case F2:
      return "F2";
    case F3:
      return "F3";
    case F4:
      return "F4";
    case F5:
      return "F5";
    case F6:
      return "F6";
    default:
      snprintf(buf, 2, "%c", key);
    }
  return buf;
}

void
command_dump_keys(void)
{
  int i;

  i=0;
  while( cmds[i].description )
    {
      if( cmds[i].command != CMD_NONE )
	{
	  int j;
	  char keystr[80];

	  strcpy(keystr, key2str(cmds[i].keys[0]));
	  j=1;
	  while( j<3 && cmds[i].keys[j]>0 )
	    {
	      strcat(keystr, " ");
	      strcat(keystr, key2str(cmds[i].keys[j]));
	      j++;
	    }
	  printf(" %20s : %s\n", keystr, cmds[i].description);
 
	}
      i++;
    }
}

char *
command_get_keys(command_t command)
{
  int i;

  i=0;
  while( cmds[i].description )
    {
      if( cmds[i].command == command )
	{
	  int j;
	  static char keystr[80];

	  strcpy(keystr, key2str(cmds[i].keys[0]));
	  j=1;
	  while( j<3 && cmds[i].keys[j]>0 )
	    {
	      strcat(keystr, " ");
	      strcat(keystr, key2str(cmds[i].keys[j]));
	      j++;
	    }
	  return keystr;
 	}
      i++;
    }
  return NULL;
}

command_t
get_keyboard_command(void)
{
  int i;
  int key;

  key = wgetch(stdscr);

  if( key==ERR )
    return CMD_NONE;

  DK(fprintf(stderr, "key = 0x%02X\t", key));

  if( isalpha(key) )
    key=tolower(key);

  i=0;
  while( cmds[i].description )
    {
      if( cmds[i].keys[0] == key || 
	  cmds[i].keys[1] == key ||
	  cmds[i].keys[2] == key )
	{
	  DK(fprintf(stderr, "Match - %s\n", cmds[i].description));
	  return cmds[i].command;
	}
      i++;
    } 
  DK(fprintf(stderr, "NO MATCH\n"));
  return CMD_NONE;
}