Jump to content




OS windows and Multitasking


  • You cannot reply to this topic
24 replies to this topic

#1 MadBudderKing

  • Members
  • 18 posts

Posted 03 May 2014 - 10:47 PM

Hi,

Im a lot more experienced with lua now, and im creating my own OS now.
The thing I want to know is how to create windows that run programs, and mostly how to multitask programs, like a windowed multi-tasking OS. :P
A small example and explanation would be fine

Thanks - MadBudder

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 03 May 2014 - 11:14 PM

This is not something that can be demonstrated with "a small example and explanation", just so you know. This is a relatively complex thing to do, as far as ComputerCraft is concerned.

#3 Zacklogan

  • Members
  • 6 posts
  • Locationterm.getPlayerLocation("Zacklogan")

Posted 04 May 2014 - 12:18 AM

Hi. What I would recommend is to is watch these videos, they are very indepth and would be a good place to start: Building a UI / OS.

Hope this helps!

#4 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 04 May 2014 - 01:11 AM

View PostZacklogan, on 04 May 2014 - 12:18 AM, said:

Hi. What I would recommend is to is watch these videos, they are very indepth and would be a good place to start: Building a UI / OS.

Hope this helps!

They're pretty good for getting the concept of how GUI work in CC, but it's not the best. Once you've watched those take a look at some of the OSs that use windowing, CraftBang, OneOS and LyqydOS (I'd recommend you look at them in that order, ranked by easiness to understand)

#5 MadBudderKing

  • Members
  • 18 posts

Posted 04 May 2014 - 03:00 AM

View PostZacklogan, on 04 May 2014 - 12:18 AM, said:

Hi. What I would recommend is to is watch these videos, they are very indepth and would be a good place to start: Building a UI / OS.

Hope this helps!

I've watched all of those before, :)
But thanks anyways!

View Postoeed, on 04 May 2014 - 01:11 AM, said:

View PostZacklogan, on 04 May 2014 - 12:18 AM, said:

Hi. What I would recommend is to is watch these videos, they are very indepth and would be a good place to start: Building a UI / OS.

Hope this helps!

They're pretty good for getting the concept of how GUI work in CC, but it's not the best. Once you've watched those take a look at some of the OSs that use windowing, CraftBang, OneOS and LyqydOS (I'd recommend you look at them in that order, ranked by easiness to understand)

I was looking at craftbang, and it seems like theres a function that over rides the term API to suit it needs to put everything in the right position and everything, but I dont exactly understand 'how it does over rides the term API'.

#6 OczkoSX

  • Members
  • 29 posts

Posted 04 May 2014 - 07:29 AM

If you want to run multiple functions in one time, you can use parallel API

#7 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 04 May 2014 - 07:35 AM

View PostMadBudderKing, on 04 May 2014 - 03:00 AM, said:

I was looking at craftbang, and it seems like theres a function that over rides the term API to suit it needs to put everything in the right position and everything, but I dont exactly understand 'how it does over rides the term API'.

What it does, as does OneOS, although not quite to the same extent as OneOS which completely sandboxes everything, is it runs each program in it's own environment. This means that you can set the term API for each program to be your custom one. Take a look at setfenv, or the code I use to do this in OneOS. (the term API overriding is in TermRedirect in the same folder)

#8 viluon

  • Members
  • 183 posts
  • LocationCzech Republic

Posted 04 May 2014 - 08:58 AM

It's definitely good to learn from code of OSes here at the forums (as oeed mentioned). However, I don't recommend you to start your own project if you don't understand multitasking. Start with something less complicated, multitasking is the main part of OS's kernel and asking for the code would sound like copy paste (for me). Have a look on how others do it, then try yourself, then ask if you don't understand.

#9 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 04 May 2014 - 09:06 AM

View Postviluon, on 04 May 2014 - 08:58 AM, said:

It's definitely good to learn from code of OSes here at the forums (as oeed mentioned). However, I don't recommend you to start your own project if you don't understand multitasking. Start with something less complicated, multitasking is the main part of OS's kernel and asking for the code would sound like copy paste (for me). Have a look on how others do it, then try yourself, then ask if you don't understand.

Yea, there's no problem with looking at peoples code and learning from it.

I used the NDF-Jay tutorials to learn how CC OSs worked before making PearOS, then I used CraftBang to learn how to do shell based multitasking.

#10 MadBudderKing

  • Members
  • 18 posts

Posted 04 May 2014 - 01:20 PM

View Postoeed, on 04 May 2014 - 07:35 AM, said:

View PostMadBudderKing, on 04 May 2014 - 03:00 AM, said:

I was looking at craftbang, and it seems like theres a function that over rides the term API to suit it needs to put everything in the right position and everything, but I dont exactly understand 'how it does over rides the term API'.

What it does, as does OneOS, although not quite to the same extent as OneOS which completely sandboxes everything, is it runs each program in it's own environment. This means that you can set the term API for each program to be your custom one. Take a look at setfenv, or the code I use to do this in OneOS. (the term API overriding is in TermRedirect in the same folder)

Funny enough, I was just looking at this, but ill try it out. :)

#11 MadBudderKing

  • Members
  • 18 posts

Posted 04 May 2014 - 02:27 PM

local buffer =
{
setCursorPos = function(x, y)
term.native.setCursorPos(x+1, y+1)
end;
write = function(text)
term.native.write(text)
end
}
setmetatable(buffer, {__index = _G})
setfenv(1, buffer)
term.setCursorPos(1,1)
term.write("Hii!!")

Ive tried this, but it just prints at 1,1. As its supposed to at 2,2. I might not be getting something, anything Im doing wrong here?

#12 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 04 May 2014 - 02:35 PM

You are defining global 'setCursorPos' and 'write' functions, not 'term.setCursorPos' and 'term.write'. This might work (not tested):

local env = setmetatable({
  term = setmetatable({
    setCursorPos = function (x, y)
      _G.term.native.setCursorPos(x + 1, y + 1)
    end
  }, {__index = _G.term.native})
}, {__index = _G})

setfenv(1, env)
term.setCursorPos(1,1)
term.write("Hii!!")

Don't forget that in CC 1.6 and above 'term.native' was changed from a table to a function.

#13 MadBudderKing

  • Members
  • 18 posts

Posted 04 May 2014 - 02:40 PM

View PostMKlegoman357, on 04 May 2014 - 02:35 PM, said:

You are defining global 'setCursorPos' and 'write' functions, not 'term.setCursorPos' and 'term.write'. This might work (not tested):

local env = setmetatable({
  term = setmetatable({
	setCursorPos = function (x, y)
	  _G.term.native.setCursorPos(x + 1, y + 1)
	end
  }, {__index = _G.term.native})
}, {__index = _G})

setfenv(1, env)
term.setCursorPos(1,1)
term.write("Hii!!")

Don't forget that in CC 1.6 and above 'term.native' was changed from a table to a function.

Ya, It errors.
Line 4, attempt to index ? (a function value)

Edited by MadBudderKing, 04 May 2014 - 02:40 PM.


#14 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 04 May 2014 - 02:59 PM

View PostMadBudderKing, on 04 May 2014 - 02:40 PM, said:

Ya, It errors.
Line 4, attempt to index ? (a function value)

View PostMKlegoman357, on 04 May 2014 - 02:35 PM, said:

Don't forget that in CC 1.6 and above 'term.native' was changed from a table to a function.

Here's a fixed example (for CC 1.6+):

local env = setmetatable({
  term = setmetatable({
    setCursorPos = function (x, y)
      _G.term.setCursorPos(x + 1, y + 1)
    end
  }, {__index = _G.term})
}, {__index = _G})

local function func ()
  term.setCursorPos(1, 1)
  term.write("Hii!!")
end

setfenv(func, env)

func()

Edited by MKlegoman357, 04 May 2014 - 03:00 PM.


#15 MadBudderKing

  • Members
  • 18 posts

Posted 04 May 2014 - 03:12 PM

Thanks, it works. Now, what If I want to run it on another program?
Running shell.run after the setfenv errors; Line 11 (Line w/ the shell.run on it), attempt to index ? (a nil value)

#16 CometWolf

  • Members
  • 1,283 posts

Posted 04 May 2014 - 03:28 PM

Shell is not indexed in _G by default. So unless you put it in there, you won't be able to acess it through the index meta to _G. I would suggest putting it in the new environment.

#17 MadBudderKing

  • Members
  • 18 posts

Posted 04 May 2014 - 03:40 PM

I might get you entirly, but something like this?
local env = setmetatable({
  term = setmetatable({
    setCursorPos = function (x, y)
	  _G.term.setCursorPos(x + 1, y + 1)
    end
  }, {__index = _G.term}),
  shell = setmetatable({
    run = function (prg)
	  shell.run(prg)
    end
  }, {__index = _G.shell})
}, {__index = _G})
function func()
shell.run("firewolf")
end
setfenv(func, env)
func()
When ran, firewolf will stay at 1,1

#18 CometWolf

  • Members
  • 1,283 posts

Posted 04 May 2014 - 04:55 PM

I was thinkin more along the lines of just sticking the shell table in there, instead of putting the run function in there. Also as i noted earlier, there is no _G.shell. Anyways, i believe shell.run sets the environment to the environment of the shell, not the environment of the function calling it. Therefore, you're overrides won't work with shell.run unless you replace the global functions. You'll have to use os.run instead.

#19 MadBudderKing

  • Members
  • 18 posts

Posted 04 May 2014 - 05:17 PM

Yay, it works!
Im pretty much using this code:
local env = setmetatable({
  term = setmetatable({
	setCursorPos = function (x, y)
	  _G.term.setCursorPos(x + 1, y + 1)
	end
  }, {__index = _G.term}),
}, {__index = _G})
function func()
os.run(env, "ptest")
end
setfenv(func, env)
func()
ptest just has a simple term.setCursorPos(2,2) and write("hi") in it.

Now, to implement all the other drawing functions (paintutils.drawLine, drawPixel image, etc)

Oh, and while im doing that, what about multitasking?
No-one talked about that, really in here yet

Edited by MadBudderKing, 04 May 2014 - 05:18 PM.


#20 CometWolf

  • Members
  • 1,283 posts

Posted 04 May 2014 - 05:28 PM

function func()
os.run(env, "ptest")
end
setfenv(func, env)
func()
This, is pointless. os.run sets the environment of the called program to the first argument anyways, so you might aswell just do just
os.run(env, "ptest")






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users