2017-11-23 15:51:31 +01:00
|
|
|
-----------------------------------------------------
|
|
|
|
-- Operating System class
|
|
|
|
-----------------------------------------------------
|
|
|
|
local os_class = {}
|
|
|
|
os_class.__index = os_class
|
2017-12-05 23:15:55 +01:00
|
|
|
laptop.class_lib.os = os_class
|
2017-11-23 15:51:31 +01:00
|
|
|
|
2017-11-24 21:54:13 +01:00
|
|
|
-- Swap the node
|
|
|
|
function os_class:swap_node(new_node_name)
|
|
|
|
local node = minetest.get_node(self.pos)
|
2017-12-13 00:16:30 +01:00
|
|
|
if new_node_name then
|
|
|
|
node.name = new_node_name
|
2017-12-13 11:54:30 +01:00
|
|
|
self.hwdef = laptop.node_config[self.node.name]
|
|
|
|
end
|
|
|
|
if self.hwdef.paramtype2 == "colorfacedir" then
|
|
|
|
local fdir = math.floor(node.param2 % 32)
|
|
|
|
node.param2 = fdir + self.theme.node_color * 32
|
2017-12-13 00:16:30 +01:00
|
|
|
end
|
2017-12-16 13:04:33 +01:00
|
|
|
self:set_infotext(self.hwdef.infotext)
|
2017-11-24 21:54:13 +01:00
|
|
|
minetest.swap_node(self.pos, node)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Power on the system and start the launcher
|
2017-11-23 15:51:31 +01:00
|
|
|
function os_class:power_on(new_node_name)
|
2017-12-15 00:30:35 +01:00
|
|
|
self.bdev:free_ram_disk()
|
2017-12-15 08:54:36 +01:00
|
|
|
-- update current instance with reinitialized data
|
|
|
|
for k,v in pairs(laptop.os_get(self.pos)) do
|
|
|
|
self[k] = v
|
|
|
|
end
|
2017-12-13 00:16:30 +01:00
|
|
|
self:swap_node(new_node_name)
|
2017-12-05 23:15:55 +01:00
|
|
|
self:set_app() --launcher
|
2017-11-24 21:54:13 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Power on the system / and resume last running app
|
|
|
|
function os_class:resume(new_node_name)
|
2017-12-19 17:00:56 +01:00
|
|
|
self.sysram.current_app = self:appstack_pop()
|
2017-12-13 00:16:30 +01:00
|
|
|
self:swap_node(new_node_name)
|
2017-12-15 00:30:35 +01:00
|
|
|
self:set_app(self.sysram.current_app)
|
2017-11-23 15:51:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Power off the system
|
|
|
|
function os_class:power_off(new_node_name)
|
2017-12-13 00:16:30 +01:00
|
|
|
self:swap_node(new_node_name)
|
2017-12-19 17:00:56 +01:00
|
|
|
self:set_app('os:power_off')
|
2017-11-23 15:51:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Set infotext for system
|
|
|
|
function os_class:set_infotext(infotext)
|
|
|
|
self.meta:set_string('infotext', infotext)
|
|
|
|
end
|
|
|
|
|
2017-12-03 22:24:45 +01:00
|
|
|
-- Get given or current theme
|
|
|
|
function os_class:get_theme(theme)
|
2017-12-15 00:30:35 +01:00
|
|
|
if not theme and self.sysdata then
|
|
|
|
theme = self.sysdata.theme
|
|
|
|
end
|
|
|
|
return laptop.get_theme(theme)
|
2017-12-03 22:24:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Set current theme
|
|
|
|
function os_class:set_theme(theme)
|
2018-02-23 08:56:35 +01:00
|
|
|
if laptop.themes[theme] then
|
|
|
|
if self.sysdata then
|
|
|
|
self.sysdata.theme = theme
|
|
|
|
end
|
2017-12-03 22:24:45 +01:00
|
|
|
self.theme = self:get_theme()
|
2017-12-13 00:16:30 +01:00
|
|
|
self:swap_node()
|
2017-12-15 00:30:35 +01:00
|
|
|
self:save()
|
2017-12-03 22:24:45 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-05 23:15:55 +01:00
|
|
|
-- Add app to stack (before starting new)
|
|
|
|
function os_class:appstack_add(appname)
|
2017-12-15 00:30:35 +01:00
|
|
|
table.insert(self.sysram.stack, appname)
|
2017-12-05 23:15:55 +01:00
|
|
|
end
|
2017-11-24 22:11:45 +01:00
|
|
|
|
2017-12-05 23:15:55 +01:00
|
|
|
-- Get last app from stack
|
|
|
|
function os_class:appstack_pop()
|
|
|
|
local ret
|
2017-12-15 00:30:35 +01:00
|
|
|
if #self.sysram.stack > 0 then
|
|
|
|
ret = self.sysram.stack[#self.sysram.stack]
|
|
|
|
table.remove(self.sysram.stack, #self.sysram.stack)
|
2017-11-23 17:17:59 +01:00
|
|
|
end
|
2017-12-05 23:15:55 +01:00
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Free stack
|
|
|
|
function os_class:appstack_free()
|
2017-12-15 00:30:35 +01:00
|
|
|
self.sysram.stack = {}
|
2017-12-05 23:15:55 +01:00
|
|
|
end
|
2017-12-03 23:31:04 +01:00
|
|
|
|
2017-12-05 23:15:55 +01:00
|
|
|
-- Get new app instance
|
|
|
|
function os_class:get_app(name)
|
|
|
|
local template = laptop.apps[name]
|
|
|
|
if not template then
|
|
|
|
return
|
2017-12-03 23:31:04 +01:00
|
|
|
end
|
2017-12-05 23:15:55 +01:00
|
|
|
local app = setmetatable(table.copy(template), laptop.class_lib.app)
|
|
|
|
app.name = name
|
|
|
|
app.os = self
|
|
|
|
return app
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Activate the app
|
|
|
|
function os_class:set_app(appname)
|
2017-12-06 19:12:10 +01:00
|
|
|
local launcher = self.hwdef.custom_launcher or "launcher"
|
2017-12-05 23:15:55 +01:00
|
|
|
local newapp = appname or launcher
|
|
|
|
if newapp == launcher then
|
|
|
|
self:appstack_free()
|
2017-12-15 00:30:35 +01:00
|
|
|
elseif self.sysram.current_app and
|
|
|
|
self.sysram.current_app ~= launcher and
|
|
|
|
self.sysram.current_app ~= newapp then
|
|
|
|
self:appstack_add(self.sysram.current_app)
|
2017-12-05 23:15:55 +01:00
|
|
|
end
|
2017-12-19 19:15:29 +01:00
|
|
|
|
|
|
|
-- suspend timer from previous app and resume the new one
|
|
|
|
if self.sysram.current_app ~= newapp then
|
|
|
|
self.timer = minetest.get_node_timer(self.pos)
|
|
|
|
if self.sysram.current_app then
|
|
|
|
if self.timer:is_started() then
|
|
|
|
self.sysram.app_timer[self.sysram.current_app] = {
|
|
|
|
timeout = self.timer:get_timeout(),
|
|
|
|
elapsed = self.timer:get_elapsed(),
|
|
|
|
}
|
|
|
|
else
|
|
|
|
self.sysram.app_timer[self.sysram.current_app] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- restore the timer of current app
|
|
|
|
if self.sysram.app_timer[newapp] then
|
|
|
|
local data = self.sysram.app_timer[newapp]
|
|
|
|
self.timer:set(data.timeout, data.elapsed)
|
|
|
|
else
|
|
|
|
self.timer:stop()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-15 00:30:35 +01:00
|
|
|
self.sysram.current_app = newapp
|
2017-12-05 23:15:55 +01:00
|
|
|
local app = self:get_app(newapp)
|
2017-12-06 01:14:43 +01:00
|
|
|
local formspec = app:get_formspec()
|
|
|
|
if formspec ~= false then
|
|
|
|
self.meta:set_string('formspec', formspec)
|
|
|
|
end
|
2017-11-23 17:17:59 +01:00
|
|
|
self:save()
|
|
|
|
end
|
|
|
|
|
2017-12-05 23:15:55 +01:00
|
|
|
-- Handle input processing
|
2017-12-11 17:47:33 +01:00
|
|
|
function os_class:pass_to_app(method, reshow, sender, ...)
|
2017-12-15 00:30:35 +01:00
|
|
|
local appname = self.sysram.current_app or self.hwdef.custom_launcher or "launcher"
|
2017-12-05 23:15:55 +01:00
|
|
|
local app = self:get_app(appname)
|
2018-02-23 08:40:35 +01:00
|
|
|
if not app then
|
|
|
|
self:set_app()
|
|
|
|
end
|
2017-12-11 17:47:33 +01:00
|
|
|
local ret = app:receive_data(method, reshow, sender, ...)
|
2017-12-19 19:15:29 +01:00
|
|
|
if sender then
|
|
|
|
self.sysram.last_player = sender:get_player_name()
|
|
|
|
end
|
2017-12-19 17:00:56 +01:00
|
|
|
if self.sysram.current_app == appname and reshow then
|
2017-12-06 01:14:43 +01:00
|
|
|
local formspec = app:get_formspec()
|
|
|
|
if formspec ~= false then
|
|
|
|
self.meta:set_string('formspec', formspec)
|
|
|
|
end
|
2017-11-23 18:08:07 +01:00
|
|
|
end
|
|
|
|
self:save()
|
2017-12-11 17:47:33 +01:00
|
|
|
return ret
|
2017-11-23 17:17:59 +01:00
|
|
|
end
|
2017-12-06 01:14:43 +01:00
|
|
|
|
2017-12-12 16:28:49 +01:00
|
|
|
function os_class:save()
|
2017-12-15 00:30:35 +01:00
|
|
|
self.bdev:sync()
|
2017-12-12 09:16:24 +01:00
|
|
|
end
|
|
|
|
|
2017-12-16 12:54:49 +01:00
|
|
|
-- Use parameter and launch the select_file dialog
|
|
|
|
-- Return values will be send as fields to the called app
|
|
|
|
function os_class:select_file_dialog(param)
|
|
|
|
local store = self.bdev:get_app_storage('ram', 'os:select_file')
|
|
|
|
store.param = param
|
|
|
|
self:set_app('os:select_file')
|
|
|
|
end
|
2017-12-21 21:32:09 +01:00
|
|
|
|
|
|
|
-- Use parameter and launch the select_file dialog
|
|
|
|
-- Return values will be send as fields to the called app
|
|
|
|
function os_class:print_file_dialog(param)
|
|
|
|
local store = self.bdev:get_app_storage('ram', 'printer:app')
|
|
|
|
store.param = param
|
|
|
|
self:set_app('printer:app')
|
|
|
|
end
|
|
|
|
|
2017-11-23 15:51:31 +01:00
|
|
|
-----------------------------------------------------
|
|
|
|
-- Get Operating system object
|
|
|
|
-----------------------------------------------------
|
|
|
|
function laptop.os_get(pos)
|
2017-11-23 23:40:41 +01:00
|
|
|
local self = setmetatable({}, os_class)
|
2017-11-23 17:17:59 +01:00
|
|
|
self.__index = os_class
|
|
|
|
self.pos = pos
|
2017-12-06 19:12:10 +01:00
|
|
|
self.node = minetest.get_node(pos)
|
|
|
|
self.hwdef = laptop.node_config[self.node.name]
|
2017-12-15 08:54:36 +01:00
|
|
|
if not self.hwdef then
|
|
|
|
return nil -- not compatible node
|
|
|
|
end
|
2017-11-23 17:17:59 +01:00
|
|
|
self.meta = minetest.get_meta(pos)
|
2017-12-15 00:30:35 +01:00
|
|
|
self.bdev = laptop.get_bdev_handler(self)
|
|
|
|
self.sysram = self.bdev:get_app_storage('ram', 'os')
|
2017-12-15 10:01:12 +01:00
|
|
|
self.sysram.stack = self.sysram.stack or {}
|
2017-12-19 19:15:29 +01:00
|
|
|
self.sysram.app_timer = self.sysram.app_timer or {}
|
2017-12-15 00:30:35 +01:00
|
|
|
self.sysdata = self.bdev:get_app_storage('system', 'os')
|
2017-12-03 22:24:45 +01:00
|
|
|
self.theme = self:get_theme()
|
2017-11-23 17:17:59 +01:00
|
|
|
return self
|
2017-11-23 15:51:31 +01:00
|
|
|
end
|