restructure and additions to IPC
This commit is contained in:
parent
cd290f0d2b
commit
58844384f2
2 changed files with 94 additions and 109 deletions
175
cimp.bas
175
cimp.bas
|
|
@ -7,7 +7,6 @@ declare library"terminkey"
|
|||
function ipc_check_message%(byval buf as _offset, byval max_len as long)
|
||||
sub ipc_send_message(buf as string)
|
||||
sub ipc_cleanup()
|
||||
sub get_absolute_path(rel_path as string, byval abs_path as _offset, byval max_len as long)
|
||||
end declare
|
||||
|
||||
$console:only
|
||||
|
|
@ -40,13 +39,27 @@ if ipc_status = 0 then
|
|||
Select Case command$(1)
|
||||
Case "--next"
|
||||
cmd_msg = "NEXT"
|
||||
|
||||
|
||||
Case "--prev"
|
||||
cmd_msg = "PREV"
|
||||
|
||||
Case "-v", "--volume"
|
||||
cmd_msg = "VOL:" + command$(2)
|
||||
|
||||
|
||||
case "--pause"
|
||||
cmd_msg = "PAUSE"
|
||||
case "--stop"
|
||||
cmd_msg = "STOP"
|
||||
case "--play"
|
||||
cmd_msg = "PLAY"
|
||||
case "--quit"
|
||||
cmd_msg = "QUIT"
|
||||
case "--shuffle"
|
||||
cmd_msg = "SHUFFLE"
|
||||
case "--repeat"
|
||||
cmd_msg = "REPEAT"
|
||||
case "--repeat-1"
|
||||
cmd_msg = "REPEAT1"
|
||||
Case "--add"
|
||||
For i = 2 To _commandcount
|
||||
cmd_msg = "ADD:" + _cwd$ + slash + command$(i)
|
||||
|
|
@ -104,7 +117,7 @@ elseif ipc_status = -1 then
|
|||
goto quit
|
||||
end if
|
||||
|
||||
' --- SERVER MODE (Main Player Execution continues below) ---
|
||||
' ---SERVER MODE (Main Player Execution)---
|
||||
|
||||
echooff
|
||||
cursoroff
|
||||
|
|
@ -194,6 +207,83 @@ songname=beforelast(".",afterlast("/",file(i)))
|
|||
|
||||
while keyin<>27
|
||||
keyin=terminkey
|
||||
' ---Poll Client Messages---
|
||||
dim incoming_buf as string
|
||||
incoming_buf = space$(512) + chr$(0)
|
||||
dim msg_len as long
|
||||
msg_len = ipc_check_message(_offset(incoming_buf), 512)
|
||||
|
||||
if msg_len > 0 then
|
||||
dim client_cmd as string
|
||||
client_cmd = left$(incoming_buf, msg_len)
|
||||
|
||||
if client_cmd = "NEXT" then
|
||||
playnext = 1
|
||||
elseif client_cmd = "PLAY" then
|
||||
keyin=asc("x")
|
||||
elseif client_cmd = "PAUSE" then
|
||||
keyin=asc("c")
|
||||
elseif client_cmd = "STOP" then
|
||||
keyin=asc("v")
|
||||
elseif client_cmd = "QUIT" then
|
||||
keyin=27
|
||||
elseif client_cmd = "REPEAT" then
|
||||
repeat=-1
|
||||
elseif client_cmd = "REPEAT1" then
|
||||
repeat=1
|
||||
elseif client_cmd = "SHUFFLE" then
|
||||
keyin=asc("s")
|
||||
elseif left$(client_cmd, 4) = "VOL:" then
|
||||
volume = val(mid$(client_cmd, 5)) / 100
|
||||
if volume > 1 then volume = 1
|
||||
if volume < 0 then volume = 0
|
||||
_sndvol musichandle, volume
|
||||
elseif left$(client_cmd, 4) = "ADD:" then
|
||||
dim new_file as string
|
||||
new_file = _trim$(mid$(client_cmd, 5))
|
||||
if _fileexists(new_file) then
|
||||
if lcase$(right$(new_file, 4)) = ".m3u" then
|
||||
parsem3u new_file, file()
|
||||
else
|
||||
redim _preserve file(ubound(file) + 1) as string
|
||||
file(ubound(file)) = new_file
|
||||
end if
|
||||
end if
|
||||
elseif left$(client_cmd, 5) = "PLAY:" then
|
||||
dim replace_file as string
|
||||
replace_file = _trim$(mid$(client_cmd, 6))
|
||||
if _fileexists(replace_file) then
|
||||
redim file(0) as string
|
||||
if lcase$(right$(replace_file, 4)) = ".m3u" then
|
||||
parsem3u replace_file, file()
|
||||
else
|
||||
file(0) = replace_file
|
||||
end if
|
||||
i = -1
|
||||
playnext = 1
|
||||
end if
|
||||
elseif client_cmd = "GET_PLAYLIST" then
|
||||
dim p_idx as long
|
||||
dim playlist_payload as string
|
||||
playlist_payload = "=== Current Playlist ===" + chr$(10)
|
||||
for p_idx = lbound(file) to ubound(file)
|
||||
if p_idx = i then
|
||||
playlist_payload = playlist_payload + "-> " + file(p_idx) + chr$(10)
|
||||
else
|
||||
playlist_payload = playlist_payload + " " + file(p_idx) + chr$(10)
|
||||
end if
|
||||
next p_idx
|
||||
|
||||
ipc_cleanup
|
||||
_delay 0.1
|
||||
ipc_send_message playlist_payload
|
||||
_delay 0.1
|
||||
dim reinit as long
|
||||
reinit = ipc_init
|
||||
end if
|
||||
end if
|
||||
' --- End Poll Client Messages ---
|
||||
|
||||
select case keyin
|
||||
case key_up
|
||||
volume=volume+(0.01+(volume/10))
|
||||
|
|
@ -342,70 +432,6 @@ while keyin<>27
|
|||
_limit 30
|
||||
if _exit then goto quit
|
||||
|
||||
' --- Poll Client Messages ---
|
||||
dim incoming_buf as string
|
||||
incoming_buf = space$(512) + chr$(0)
|
||||
dim msg_len as long
|
||||
msg_len = ipc_check_message(_offset(incoming_buf), 512)
|
||||
|
||||
if msg_len > 0 then
|
||||
dim client_cmd as string
|
||||
client_cmd = left$(incoming_buf, msg_len)
|
||||
|
||||
if client_cmd = "NEXT" then
|
||||
playnext = 1
|
||||
elseif client_cmd = "PREV" then
|
||||
playnext = -1
|
||||
elseif left$(client_cmd, 4) = "VOL:" then
|
||||
volume = val(mid$(client_cmd, 5)) / 100
|
||||
if volume > 1 then volume = 1
|
||||
if volume < 0 then volume = 0
|
||||
_sndvol musichandle, volume
|
||||
elseif left$(client_cmd, 4) = "ADD:" then
|
||||
dim new_file as string
|
||||
new_file = _trim$(mid$(client_cmd, 5))
|
||||
if _fileexists(new_file) then
|
||||
if lcase$(right$(new_file, 4)) = ".m3u" then
|
||||
parsem3u new_file, file()
|
||||
else
|
||||
redim _preserve file(ubound(file) + 1) as string
|
||||
file(ubound(file)) = new_file
|
||||
end if
|
||||
end if
|
||||
elseif left$(client_cmd, 5) = "PLAY:" then
|
||||
dim replace_file as string
|
||||
replace_file = _trim$(mid$(client_cmd, 6))
|
||||
if _fileexists(replace_file) then
|
||||
redim file(0) as string
|
||||
if lcase$(right$(replace_file, 4)) = ".m3u" then
|
||||
parsem3u replace_file, file()
|
||||
else
|
||||
file(0) = replace_file
|
||||
end if
|
||||
i = -1
|
||||
playnext = 1
|
||||
end if
|
||||
elseif client_cmd = "GET_PLAYLIST" then
|
||||
dim p_idx as long
|
||||
dim playlist_payload as string
|
||||
playlist_payload = "=== Current Playlist ===" + chr$(10)
|
||||
for p_idx = lbound(file) to ubound(file)
|
||||
if p_idx = i then
|
||||
playlist_payload = playlist_payload + "-> " + file(p_idx) + chr$(10)
|
||||
else
|
||||
playlist_payload = playlist_payload + " " + file(p_idx) + chr$(10)
|
||||
end if
|
||||
next p_idx
|
||||
|
||||
ipc_cleanup
|
||||
_delay 0.1
|
||||
ipc_send_message playlist_payload
|
||||
_delay 0.1
|
||||
dim reinit as long
|
||||
reinit = ipc_init
|
||||
end if
|
||||
end if
|
||||
' --- End Poll Client Messages ---
|
||||
wend
|
||||
|
||||
quit:
|
||||
|
|
@ -645,16 +671,3 @@ function getcodepoint& (utf8char$)
|
|||
getcodepoint&=(b1 and &h07)*262144+(b2 and &h3f)*4096+(b3 and &h3f)*64+(b4 and &h3f)
|
||||
end select
|
||||
end function
|
||||
|
||||
function get_abs_path$ (relative_path as string)
|
||||
dim abs_buf as string
|
||||
abs_buf = space$(512) + chr$(0) ' Allocate buffer space for the C function
|
||||
get_absolute_path relative_path, _offset(abs_buf), 512
|
||||
dim null_pos as long
|
||||
null_pos = instr(abs_buf, chr$(0))
|
||||
if null_pos > 0 then
|
||||
get_abs_path$ = _trim$(left$(abs_buf, null_pos - 1))
|
||||
else
|
||||
get_abs_path$ = _trim$(abs_buf)
|
||||
end if
|
||||
end function
|
||||
|
|
|
|||
28
terminkey.h
28
terminkey.h
|
|
@ -37,7 +37,6 @@ int ipc_init();
|
|||
int ipc_check_message(uintptr_t buffer_ptr, int max_len);
|
||||
void ipc_send_message(const char* message);
|
||||
void ipc_cleanup();
|
||||
void get_absolute_path(const char* rel_path, uintptr_t abs_path_ptr, int max_len);
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
|
|
@ -195,11 +194,6 @@ void ipc_cleanup() {
|
|||
}
|
||||
}
|
||||
|
||||
void get_absolute_path(const char* rel_path, uintptr_t abs_path_ptr, int max_len) {
|
||||
char* abs_path = (char*)abs_path_ptr;
|
||||
_fullpath(abs_path, rel_path, max_len);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static int server_fd = -1;
|
||||
|
|
@ -369,26 +363,4 @@ void ipc_cleanup() {
|
|||
unlink(socket_path);
|
||||
}
|
||||
|
||||
void get_absolute_path(const char* rel_path, uintptr_t abs_path_ptr, int max_len) {
|
||||
char* abs_path = (char*)abs_path_ptr;
|
||||
char resolved[PATH_MAX];
|
||||
if (realpath(rel_path, resolved) != NULL) {
|
||||
strncpy(abs_path, resolved, max_len);
|
||||
abs_path[max_len - 1] = '\0';
|
||||
} else {
|
||||
if (rel_path[0] == '/') {
|
||||
strncpy(abs_path, rel_path, max_len);
|
||||
abs_path[max_len - 1] = '\0';
|
||||
} else {
|
||||
char cwd[PATH_MAX];
|
||||
if (getcwd(cwd, sizeof(cwd)) != NULL) {
|
||||
snprintf(abs_path, max_len, "%s/%s", cwd, rel_path);
|
||||
} else {
|
||||
strncpy(abs_path, rel_path, max_len);
|
||||
abs_path[max_len - 1] = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue