-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.c
More file actions
108 lines (98 loc) · 2.64 KB
/
input.c
File metadata and controls
108 lines (98 loc) · 2.64 KB
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
#include "cMusix.h"
// Handle keyboard input
void userInput() {
fd_set fds;
struct timeval tv;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
tv.tv_sec = 0;
tv.tv_usec = 0;
// Check if input is available
if (select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv) <= 0) {
return;
}
char ch;
if (read(STDIN_FILENO, &ch, 1) != 1) {
return;
}
// Handle escape sequences (arrow keys, etc.)
if (ch == 27) { // ESC
// Check if this is part of an escape sequence
char seq[3];
// Set a very short timeout to check for sequence
tv.tv_sec = 0;
tv.tv_usec = 10000; // 10ms
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
if (select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv) > 0) {
// This is an escape sequence, read and ignore it
if (read(STDIN_FILENO, &seq[0], 1) == 1) {
if (seq[0] == '[') {
read(STDIN_FILENO, &seq[1], 1);
// Ignore the escape sequence
return;
}
}
return;
}
// If no sequence follows, treat as actual ESC key press
// Fall through to quit
}
switch (ch) {
case ' ':
if (!player.is_playing && player.count > 0) {
playSong();
} else {
pauseResume();
}
break;
case 'n':
case 'N':
nextSong();
break;
case 'p':
case 'P':
previousSong();
break;
case '+':
case '=':
set_volume(player.volume + 0.1f);
break;
case '-':
case '_':
set_volume(player.volume - 0.1f);
break;
case 's':
case 'S':
shuffleFunction();
break;
case 'r':
case 'R':
repeatFunction();
break;
case 'q':
case 'Q':
cleanup();
exit(0);
break;
case 27: // ESC key (only reaches here if not part of sequence)
cleanup();
exit(0);
break;
case 'j':
case 'J':
if (player.list_offset + DISPLAY_SONGS < player.count) {
player.list_offset++;
}
break;
case 'k':
case 'K':
if (player.list_offset > 0) {
player.list_offset--;
}
break;
default:
// Ignore unrecognized keys instead of crashing
break;
}
}