This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KaoS's profile picture

task manager

Started by KaoS, 14 September 2012 - 09:03 AM
KaoS #1
Posted 14 September 2012 - 11:03 AM
Hey Pro's, I am currently trying to make a multitasking system where you can call multiple coroutines and run one at a time, essentially minimizing and switching between windows. I know there is an existing utility for this but I need to make my own so I can adapt it to work with my OS… at the moment it does not work and I am not sure why. you can create a new window but after that you cannot switch between them or open another window. please take a look

http://pastebin.com/YmznkxvM
Spoiler

local function record(existing)
local funcs={}
local temp=existing or {}
for k,v in pairs(term.native) do
funcs[k]=function(...) table.insert(temp,{v,[2]={...}}) return v(...) end
funcs[k..'2']=v
end
term.redirect(funcs)
return temp
end

local function assign(record)
funcs={}
for k,v in pairs(term.native) do
funcs[k]=function(...) table.insert(tThreads[record][2],{v,[2]={...}}) return v(...) end
end
term.redirect(funcs)
end

local function replay(record)
for k,v in pairs(tThreads[record][2]) do
v[1](unpack(v[2]))
sleep(0)
end
end

local function manage()
local running=1
while true do
local events={os.pullEvent()}
if events[1]=='key' and events[2]==15 then
term.native.clear()
term.native.setCursorPos(1,1)
tRunning['current']=coroutine.create(function() shell.run('shell') end)
table.insert(tThreads,{tRunning['current'],{{term.native.clear,{}},{term.native.setCursorPos,{1,1}}}})
elseif events[1]=='char' and events[2]=='>' and running~=#tThreads then
running=running+1
tRunning['current']=tThreads[running]
replay(running)
assign(running)
elseif events[1]=='char' and events[2]=='<' and running~=1 then
running=running-1
tRunning['current']=tThreads[running]
replay(running)
assign(running)
end
end
end

local tRunning={}
tRunning['main']=coroutine.create(function() shell.run('shell') end)
tRunning['back']=coroutine.create(manage)
local tThreads={{tRunning['current'],{{term.native.clear,{}},{term.native.setCursorPos,{1,1}}}}}



while true do
local events={os.pullEvent()}
for k,v in pairs(tRunning) do
coroutine.resume(v,unpack(events))
end
end
Lyqyd #2
Posted 15 September 2012 - 12:55 AM
You're going to have to be a lot more specific. What other things are you trying to switch to, and what is the code you're using to create those other things, and what are you trying to do to cause the switch?
KaoS #3
Posted 15 September 2012 - 05:26 AM
essentially when you start it then it launches 2 coroutines, one is the normal shell and the other is the task manager, you continue as normal (for now as it is just a prototype) and then when you want to do something else as well you press TAB, this pauses your current shell session (no-longer resumes its coroutine) and creates another one so you can do something else and then switch between it and your original one with the > and < keys

I do this by having a table of all coroutines you create and when you press tab then it creates a new one and sets it as active, when you press > or < it checks if there is another table value and sets it as the active coroutine in stead. the first function is not in use, I was just checking something

the assign function starts recording everything you do with the term api so it can record what is currently on the screen, the replay function replays the changes made on the coroutine to return the screen to how it was when you minimized it
Gamerhqweb #4
Posted 15 September 2012 - 09:22 AM
One possible way which you might be able to do it:
1 - Write an API which contains the task manager program, (Include the key event which opens it here)
2 - Edit the BIOS to tell it to load the API.
As far as i can tell, there may be other solutions but this one worked the best for me. If you loop the API so it constantly checks for key press then you may be able to run it while other programs are running. In my test when i press TAB it would just display a message then pause and resume to the program i was running.
KaoS #5
Posted 15 September 2012 - 09:41 AM
That is what I am currently doing, I loop a function that detects keypresses. that part of the code works too, when it detects that I pressed tab then it all gets messed up
KaoS #6
Posted 18 September 2012 - 07:47 AM
No ideas? I'm incredibly lost here