-
Notifications
You must be signed in to change notification settings - Fork 2
/
jukebox.c
executable file
·180 lines (150 loc) · 3.38 KB
/
jukebox.c
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
/** \file jukebox.c
* \author david.siorpaes@gmail.com
*
* Implements a very simple client that drives a VLC instance as follows:
*
* /home/pi/jukebox/jukebox /home/pi/music | runuser -l pi -c "/usr/bin/vlc" &
*
* or, using TCP/IP server mode, using netcat:
*
* vlc -vvv --intf rc --rc-host :50000
* /home/pi/jukebox/jukebox /home/pi/music | /bin/nc localhost 50000 &
*
* A numeric keypad is used as player controller. Takes as first argument the
* path of the media files root directory to be played. All media files must
* be contained in directories named with numbers (e.g.: 1, 10, 45,...) which
* are played back when user enters the corresponding number with the keypad
* and presses enter.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include "jukebox.h"
int state = IDLE;
char jbKeys[3];
int jbKeyIndex;
char mediaPath[256];
int main(int argc, char** argv)
{
int hrawfd;
int ret, i;
uint8_t hrBuffer[128];
if(argc != 2){
fprintf(stderr, "Usage: %s <mediapath>\n", argv[0]);
return -1;
}
strcpy(mediaPath, argv[1]);
hrawfd = open("/dev/hidraw1", O_RDONLY);
if(hrawfd < 0){
fprintf(stderr, "Error opening hidraw device: %s\n", strerror(errno));
return errno;
}
while(1){
/* Process input keys */
ret = read(hrawfd, hrBuffer, 8);
if(ret < 0){
fprintf(stderr, "Cannot read hidraw: %s\n", strerror(errno));
return errno;
}
if(ret != 8)
fprintf(stderr, "Warning!! Read %i bytes\n", ret); //TODO cope with this
for(i=0; i<8; i++){
if(hrBuffer[i] != 0){
processEntry(hrBuffer[i]);
}
}
}
return 0;
}
int processEntry(uint8_t entry)
{
int number;
int ret, i;
char digit;
ret = isNumber(entry, &number);
/* Manage numbers entry */
if(ret){
if(state == IDLE){
jbKeys[0] = number;
jbKeyIndex = 1;
state = ENTERING;
}
else{
if(jbKeyIndex < sizeof(jbKeys)){
jbKeys[jbKeyIndex] = number;
jbKeyIndex++;
}
}
}
/* Not a number */
else{
if(number == ENTER){
/* Clear playlist */
sendCommand("clear\n");
if(state == IDLE){
jbKeyIndex = 0;
}
else{
sendCommand("add ");
sendCommand(mediaPath);
digit = '/';
sendCommand(&digit);
for(i=0; i<jbKeyIndex; i++){
digit = '0' + jbKeys[i];
sendCommand(&digit);
}
sendCommand("\n");
state = IDLE;
}
}
if(number == PLUS){
sendCommand("volup\n");
}
if(number == MINUS){
sendCommand("voldown\n");
}
if(number == DOT){
sendCommand("pause\n");
}
if(number == SLASH){
sendCommand("prev\n");
sendCommand("get_title\n");
}
if(number == STAR){
sendCommand("next\n");
sendCommand("get_title\n");
}
if(number == NLOCK){
sendCommand("fastforward\n");
sendCommand("get_time\n");
}
}
return 0;
}
/** If a number is pressed, return its value. Otherwise, pass hidraw code
*/
int isNumber(int hrcode, int* number)
{
if(hrcode == ZERO){
*number = 0;
return 1;
}
if(hrcode >= ONE && hrcode <= NINE){
*number = hrcode - ONE + 1;
return 1;
}
*number = hrcode;
return 0;
}
int sendCommand(char* command)
{
int ret;
ret = write(STDOUT_FILENO, command, strlen(command));
return ret;
}