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-12-06 01:14:43 +01:00
|
|
|
local mod_storage = minetest.get_mod_storage()
|
|
|
|
|
2017-12-11 22:07:47 +01:00
|
|
|
-- Setup internal inventory slot
|
|
|
|
function os_class:get_node_inventory()
|
|
|
|
local inv = self.meta:get_inventory()
|
|
|
|
inv:set_size("main", 1) -- 1 disk supported
|
|
|
|
return inv
|
|
|
|
end
|
|
|
|
|
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)
|
|
|
|
node.name = new_node_name
|
|
|
|
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)
|
|
|
|
if new_node_name then
|
2017-11-24 21:54:13 +01:00
|
|
|
self:swap_node(new_node_name)
|
|
|
|
end
|
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)
|
|
|
|
if new_node_name then
|
|
|
|
self:swap_node(new_node_name)
|
2017-11-23 15:51:31 +01:00
|
|
|
end
|
2017-12-03 22:35:39 +01:00
|
|
|
self:set_app(self.appdata.os.current_app)
|
2017-11-23 15:51:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Power off the system
|
|
|
|
function os_class:power_off(new_node_name)
|
|
|
|
if new_node_name then
|
2017-11-24 21:54:13 +01:00
|
|
|
self:swap_node(new_node_name)
|
2017-11-23 15:51:31 +01:00
|
|
|
end
|
|
|
|
self.meta:set_string('formspec', "")
|
2017-11-24 22:11:45 +01:00
|
|
|
self:save()
|
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
|
|
|
-- Save the data
|
2017-11-23 17:17:59 +01:00
|
|
|
function os_class:save()
|
|
|
|
self.meta:set_string('laptop_appdata', minetest.serialize(self.appdata))
|
2017-12-06 01:14:43 +01:00
|
|
|
if self.cloud_store then
|
|
|
|
for store, value in pairs(self.cloud_store) do
|
|
|
|
mod_storage:set_string(store, minetest.serialize(value))
|
|
|
|
end
|
|
|
|
self.cloud_store = nil
|
|
|
|
end
|
2017-12-12 09:16:24 +01:00
|
|
|
|
|
|
|
if self.removable_store then
|
|
|
|
local stack = self:get_node_inventory():get_stack("main", 1)
|
|
|
|
if stack then
|
|
|
|
for store, value in pairs(self.removable_store) do
|
|
|
|
stack:get_meta():set_string(store, minetest.serialize(value))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.removable_store = nil
|
|
|
|
end
|
2017-11-23 17:17:59 +01:00
|
|
|
end
|
|
|
|
|
2017-12-03 22:24:45 +01:00
|
|
|
-- Get given or current theme
|
|
|
|
function os_class:get_theme(theme)
|
2017-12-03 22:35:39 +01:00
|
|
|
local theme_sel = theme or self.appdata.os.theme
|
2017-12-11 08:41:59 +01:00
|
|
|
return laptop.get_theme(theme_sel)
|
2017-12-03 22:24:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Set current theme
|
|
|
|
function os_class:set_theme(theme)
|
|
|
|
if laptop.themes[theme] then
|
2017-12-03 22:35:39 +01:00
|
|
|
self.appdata.os.theme = theme
|
2017-12-03 22:24:45 +01:00
|
|
|
self.theme = self:get_theme()
|
|
|
|
self:save()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-05 23:15:55 +01:00
|
|
|
-- Add app to stack (before starting new)
|
|
|
|
function os_class:appstack_add(appname)
|
|
|
|
table.insert(self.appdata.os.stack, appname)
|
|
|
|
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
|
|
|
|
if #self.appdata.os.stack > 0 then
|
|
|
|
ret = self.appdata.os.stack[#self.appdata.os.stack]
|
|
|
|
table.remove(self.appdata.os.stack, #self.appdata.os.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()
|
|
|
|
self.appdata.os.stack = {}
|
|
|
|
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()
|
|
|
|
elseif self.appdata.os.current_app and
|
|
|
|
self.appdata.os.current_app ~= launcher and
|
|
|
|
self.appdata.os.current_app ~= newapp then
|
2017-12-06 01:14:43 +01:00
|
|
|
self:appstack_add(self.appdata.os.current_app)
|
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
|
|
|
self.appdata.os.current_app = newapp
|
|
|
|
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-06 19:12:10 +01:00
|
|
|
local appname = self.appdata.os.current_app or self.hwdef.custom_launcher or "launcher"
|
2017-12-05 23:15:55 +01:00
|
|
|
local app = self:get_app(appname)
|
2017-12-11 17:47:33 +01:00
|
|
|
local ret = app:receive_data(method, reshow, sender, ...)
|
2017-12-04 07:12:48 +01:00
|
|
|
self.appdata.os.last_player = sender:get_player_name()
|
2017-12-11 17:47:33 +01:00
|
|
|
if self.appdata.os.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
|
|
|
|
|
|
|
-- Get mod storage as (=internet / cloud)
|
|
|
|
function os_class:connect_to_cloud(store_name)
|
|
|
|
self.cloud_store = self.cloud_store or {}
|
|
|
|
self.cloud_store[store_name] = self.cloud_store[store_name] or
|
|
|
|
minetest.deserialize(mod_storage:get_string(store_name)) or {}
|
|
|
|
return self.cloud_store[store_name]
|
|
|
|
end
|
|
|
|
|
2017-12-12 09:16:24 +01:00
|
|
|
-- Get item storage as (=floppy/usb)
|
|
|
|
function os_class:connect_to_removable(store_name)
|
|
|
|
local stack = self:get_node_inventory():get_stack("main", 1)
|
|
|
|
if not stack then
|
|
|
|
self.removable_store = nil
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
self.removable_store = self.removable_store or {}
|
|
|
|
self.removable_store[store_name] = self.removable_store[store_name] or
|
|
|
|
minetest.deserialize(stack:get_meta():get_string(store_name))
|
|
|
|
return self.removable_store[store_name]
|
|
|
|
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-11-23 17:17:59 +01:00
|
|
|
self.meta = minetest.get_meta(pos)
|
|
|
|
self.appdata = minetest.deserialize(self.meta:get_string('laptop_appdata')) or {}
|
|
|
|
self.appdata.launcher = self.appdata.launcher or {}
|
2017-12-03 22:35:39 +01:00
|
|
|
self.appdata.os = self.appdata.os or {}
|
2017-12-03 23:31:04 +01:00
|
|
|
self.appdata.os.stack = self.appdata.os.stack or {}
|
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
|