From e4d51e4c5904895dd599d099e857ccebdf1714fc Mon Sep 17 00:00:00 2001 From: dvs1 Date: Tue, 22 Oct 2024 13:51:59 +1000 Subject: Lots of little tweaks about how _ works. --- .aataaj_JACK.lua | 2 +- _.lua | 37 +++++++++++++++++++++++++----- aataaj.lua | 68 +++++++++++++++++++++++++++----------------------------- test_.lua | 6 +++-- 4 files changed, 70 insertions(+), 43 deletions(-) diff --git a/.aataaj_JACK.lua b/.aataaj_JACK.lua index 21e7938..91888d2 100755 --- a/.aataaj_JACK.lua +++ b/.aataaj_JACK.lua @@ -11,4 +11,4 @@ local E = _.E local C = _.C local __ = _._ -if _.exists('qsynth') then __('qsynth'):fork() end +if _.runnable'qsynth' then __'qsynth':forkOnce() end diff --git a/_.lua b/_.lua index c1710ee..42c3b4a 100755 --- a/_.lua +++ b/_.lua @@ -59,10 +59,12 @@ _._ = function(c) self.logging = true return self end + function exe:show() self.showing = true return self end + function exe:Nice(c) if nil == c then self.cmd = 'ionice -c3 nice -n 19 ' .. self.cmd @@ -71,6 +73,7 @@ _._ = function(c) end return self end + function exe:timeout(c) -- timeout returns a status of - command status if --preserve-status; "128+9" (actually 137) if --kill-after ends up being done; 124 if it had to TERM; command status if all went well. -- --kill-after means "send KILL after TERM fails. @@ -81,33 +84,40 @@ _._ = function(c) end return self end - function exe:also(c) + + function exe:also(c) -- Should be called "then" but that's a reserved word. if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. '; ' .. c .. ' ' return self end + function exe:And(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. ' && ' .. c .. ' ' return self end + function exe:Or(c) if nil == c then c = '' end self.cmd = self.cmd .. ' || ' .. c .. ' ' return self end + function exe:noErr() self.cmd = self.cmd .. ' 2>/dev/null ' return self end + function exe:noOut() self.cmd = self.cmd .. ' 1>/dev/null ' return self end + function exe:wait(w) self.cmd = self.cmd .. ' && touch ' .. w .. ' ' return self end + function exe:Do() --[[ "The condition expression of a control structure can return any value. Both false and nil are considered false. All values different @@ -135,23 +145,40 @@ _._ = function(c) end return self end + function exe:fork(host) - if nil ~= host then self.cmd = self.cmd .. '; r=$?; if [ $r -ge 124 ]; then echo "$r ' .. host .. ' failed forked command ' .. string.gsub(self.cmd, '"', "'") .. '"; fi' end +-- if nil ~= host then self.cmd = self.cmd .. '; r=$?; if [ $r -ge 124 ]; then echo "$r ' .. host .. ' failed forked command ' .. string.gsub(self.cmd, '"', "'") .. '"; fi' end self.cmd = '{ ' .. self.cmd .. ' ; } & ' if true == self.logging then D(" forking - " .. self.cmd) end os.execute(self.cmd) return self end + + function exe:forkOnce() + if _.running(self.command) then + D('Already running ' .. self.command) + else + self:fork() + end + end + return exe end + local __ = _._ +_.runnable = function(c) + return ( 0 == __('which ' .. c):Do().status ) +end -_.exists = function(c) - if 0 == __('which ' .. c):Do().status then return true end - return false +_.running = function(c) + return ( 1 ~= tonumber(__("pgrep -u $USER -cf " .. c):Do().lines[1]) ) end +_.exists = function(f) + local h, e = io.open(f, "r") + if nil == h then return false else h:close(); return true end +end _.killEmAll = function(all) for i,l in ipairs(all) do diff --git a/aataaj.lua b/aataaj.lua index a92e0fb..26db4d2 100755 --- a/aataaj.lua +++ b/aataaj.lua @@ -118,8 +118,8 @@ if 0 ~= #args then _.killEmAll{'jmcore', 'qjackctl'} -- Catia is python, and no easy way to kill it. -- Also it keeps jackdbus alive, no matter how hard you kill it. - __("pkill -TERM -u $USER -f catia"):Do() - __("sleep 2"):Do() + __"pkill -TERM -u $USER -f catia":Do() + __"sleep 2":Do() _.killEmAll{'jackdbus', 'a2jmidid'} return(0) elseif 'JACK' == args[1] then @@ -149,18 +149,18 @@ end local asoundrcPath = '/var/lib/aataaj' local asoundrc = 'asoundrc' local GUI = 'qjackctl' -if _.exists('catia') then GUI = 'catia' end +if _.runnable('catia') then GUI = 'catia' end local alias = { -- {name='Screen', dev='HDMI9'}, } local speaker = 'espeak' -if _.exists('espeak-ng') then speaker = 'espeak-ng' end +if _.runnable('espeak-ng') then speaker = 'espeak-ng' end local Cards = {} print('Scanning for audio devices.') -local cards = __('ls -d1 /proc/asound/card[0-9]*'):noErr():Do() +local cards = __'ls -d1 /proc/asound/card[0-9]*':noErr():Do() for i,l in ipairs(cards.lines) do local f, e = io.open(l .. '/id', "r") if nil == f then print("Could not open " .. l .. '/id') else @@ -182,7 +182,7 @@ for i,l in ipairs(cards.lines) do if 'JACK' ~= args[1] then print('\t\tALSA_CARD=' .. i - 1 .. ' ' .. speaker .. ' "Found playback device ' .. i - 1 .. ' : ' .. Cards[l]['name'] .. ' DEVICE: ' .. Cards[l]['playbackDevs'][j] .. ' ' .. n .. '"') __('ALSA_CARD=' .. i - 1 .. ' ' .. speaker .. ' "Found playback device ' .. i - 1 .. ' : ' .. Cards[l]['name'] .. ' DEVICE: ' .. Cards[l]['playbackDevs'][j] .. ' ' .. n .. '"'):noErr():Do() - __('sleep 1'):Do() + __'sleep 1':Do() end end end @@ -363,31 +363,29 @@ elseif 'JACK' == args[1] then print('') print("Start up JACK and friends.") print("jack_control") - __('jack_control start'):Do() - __('jack_control ds alsa'):Do() + __[[jack_control start + jack_control ds alsa]]:Do() --jack_control dps device hw:RIG,0 - local r = __('jack_control status'):Do().status - while r ~= 0 do - print("Waiting for JACK - sleep 1") - __('sleep 1'):Do() - r = __('jack_control status'):Do().status + while 0 ~= __'jack_control status':Do().status do + print("Waiting for JACK") + __'sleep 1':Do() end if nil ~= GUI then print(GUI) - __(GUI):fork() + __(GUI):forkOnce() end - if _.exists('jack-plumbing') then + if _.runnable'jack-plumbing' then print("jack-plumbing") - __('jack-plumbing -o /var/lib/aataaj/jack-plumbing 2>/dev/null'):fork() + __'jack-plumbing -o /var/lib/aataaj/jack-plumbing 2>/dev/null':fork() end - if _.exists('a2j_control') then + if _.runnable'a2j_control' then -- Bridge ALSA ports to JACK ports. Only handles MIDI. -- MIDI via a2jmidid. The --ehw enables hardware ports as well, equal to using the seq MIDI drivare according to https://freeshell.de/~murks/posts/ALSA_and_JACK_MIDI_explained_(by_a_dummy_for_dummies)/ --a2j_control actually starts a2jmidid. ----a2jmidid -e -u & -- I think the jack_control start and my current alsa config means a2jmidid gets started anyway. But seem to need this bit to get the joystick covered. print("a2j_control") - __('a2j_control --ehw && a2j_control --start'):Do() + __'a2j_control --ehw && a2j_control --start':Do() -- print("sleep 2") -- __('sleep 2'):Do() print("") @@ -395,9 +393,9 @@ elseif 'JACK' == args[1] then local AIN = "alsa_in" - if _.exists('zita-a2j') then AIN = 'zita-a2j' end + if _.runnable'zita-a2j' then AIN = 'zita-a2j' end local AOUT = "alsa_out" - if _.exists('zita-j2a') then AOUT = 'zita-j2a' end + if _.runnable'zita-j2a' then AOUT = 'zita-j2a' end print("Basic ALSA sound devices converted to JACK.") for i,C in pairs(alias) do @@ -406,15 +404,15 @@ elseif 'JACK' == args[1] then end print("HW playback: cloop\tDEVICE: cloop") -- No idea why, cloop wont work with zita-a2j. - __('alsa_in -j cloop -d cloop'):fork() - --__('sleep 1'):Do() - --__('jack_connect cloop:capture_1 system:playback_1'):Do() - --__('jack_connect cloop:capture_2 system:playback_2'):Do() + __'alsa_in -j cloop -d cloop':fork() + --__[[sleep 1 + -- jack_connect cloop:capture_1 system:playback_1o() + -- jack_connect cloop:capture_2 system:playback_2]]:Do() print("HW playback: ploop\tDEVICE: ploop") - __('alsa_out -j ploop -d ploop'):fork() - --__('sleep 1'):Do() - --__('jack_connect system:capture_1 ploop:playback_1'):Do() - --__('jack_connect system:capture_2 ploop:playback_2'):Do() + __'alsa_out -j ploop -d ploop':fork() + --__[[sleep 1 + -- jack_connect system:capture_1 ploop:playback_1 + -- jack_connect system:capture_2 ploop:playback_2]]:Do() print("") @@ -424,7 +422,7 @@ elseif 'JACK' == args[1] then for j,c in ipairs(C['playbackDevs']) do print("HW playback: " .. C['name'] .. "\tDEVICE: " .. C['playbackDevs'][j]) __(AOUT .. ' -j ' .. C['name'] .. "_" .. C['playbackDevs'][j] .. '-in -d ' .. C['name'] .. C['playbackDevs'][j]):fork() - -- __('sleep 1'):Do() + -- __'sleep 1':Do() -- __('jack_connect cloop:capture_1 ' .. C['name'] .. '_' .. C['playbackDevs'][j] .. '-in' .. ':playback_1'):Do() -- __('jack_connect cloop:capture_2 ' .. C['name'] .. '_' .. C['playbackDevs'][j] .. '-in' .. ':playback_2'):Do() end @@ -435,9 +433,9 @@ elseif 'JACK' == args[1] then end print("") - if _.exists('aseqjoy') then + if _.runnable('aseqjoy') then print("Scanning for joysticks.") - local sticks = __('ls -1 /dev/input/js[0-9]*'):noErr():Do() + local sticks = __'ls -1 /dev/input/js[0-9]*':noErr():Do() for i,l in ipairs(sticks.lines) do print("aseqjoy " .. l) -- Buttons switch to that numbered MIDI channel, defaults to 1. @@ -448,14 +446,14 @@ elseif 'JACK' == args[1] then print("") end - if _.exists('jack-plumbing') then + if _.runnable('jack-plumbing') then print('Stop our jack-plumbing, eventually.') - __('sleep 4'):Do() + __'sleep 4':Do() _.killEmAll{"jack-plumbing"} end - if _.exists('~/.aataaj_JACK.lua') then + if _.runnable('~/.aataaj_JACK.lua') then print('Running users aataaj_JACK.lua') - __('~/.aataaj_JACK.lua'):Do() + __'~/.aataaj_JACK.lua':Do() end end diff --git a/test_.lua b/test_.lua index 5e914b4..629c151 100755 --- a/test_.lua +++ b/test_.lua @@ -74,10 +74,10 @@ end local GUI = 'qjackctl' -if _.exists'catia' then GUI = 'catia' end +if _.runnable'catia' then GUI = 'catia' end local speaker = 'espeak' -if _.exists'espeak-ng' then speaker = 'espeak-ng' end +if _.runnable'espeak-ng' then speaker = 'espeak-ng' end @@ -114,3 +114,5 @@ print(__'exit 42':log():show():Do().status) print(__'return 42':log():show():Do().status) print(__'command NoSuchCommand':log():show():Do().status) +__'qpdfview':forkOnce() +__'qpdfview':forkOnce() -- cgit v1.1