Add Inter-Process Communication

This commit is contained in:
visionmercer 2026-06-23 10:21:17 +02:00
commit 19bf4b5288
4 changed files with 423 additions and 1 deletions

145
cimp.bas
View file

@ -3,6 +3,11 @@ declare library"terminkey"
sub echooff()
sub echoon()
function termwidth()
function ipc_init&()
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
@ -22,6 +27,65 @@ if command$=""then
goto quit
end if
dim ipc_status as long
ipc_status = ipc_init
if ipc_status = 0 then
' --- CLIENT MODE ---
dim cmd_msg as string
if command$(1) = "--next" then
cmd_msg = "NEXT"
elseif command$(1) = "--prev" then
cmd_msg = "PREV"
elseif command$(1) = "-v" or command$(1) = "--volume" then
cmd_msg = "VOL:" + command$(2)
elseif command$(1) = "--add" then
cmd_msg = "ADD:" + get_abs_path(command$(2))
elseif command$(1) = "--playlist" then
cmd_msg = "GET_PLAYLIST"
else
' Default behavior: Resolve target file/playlist and replace active queue
cmd_msg = "PLAY:" + get_abs_path(command$(1))
end if
' Send instruction to the main player instance
ipc_send_message cmd_msg
' --- Two-way Client feedback for --playlist ---
if cmd_msg = "GET_PLAYLIST" then
ipc_cleanup
_delay 0.1
dim client_listen as long
client_listen = ipc_init
if client_listen = 1 then
dim reply_buf as string
reply_buf = space$(4096) + chr$(0)
dim start_wait as double
start_wait = timer
do while timer - start_wait < 2
dim reply_len as long
reply_len = ipc_check_message(_offset(reply_buf), 4096)
if reply_len > 0 then
print left$(reply_buf, reply_len)
exit do
end if
_limit 30
loop
ipc_cleanup
end if
end if
system ' Exit client instance completely
elseif ipc_status = -1 then
print "Error initializing Inter-Process Communication."
goto quit
end if
' --- SERVER MODE (Main Player Execution continues below) ---
echooff
cursoroff
chdir _startdir$
@ -258,10 +322,76 @@ while keyin<>27
_limit 30
if _exit then goto quit
wend
' --- 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 = 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 = 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 = 0
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:
_sndclose musichandle
ipc_cleanup
print clearrest
print clearrest;
print cursorback;
@ -496,3 +626,16 @@ 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