app_fw.lua 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. laptop.apps = {}
  2. local app_class = {}
  3. app_class.__index = app_class
  4. laptop.class_lib.app = app_class
  5. -- internally used: get current app formspec
  6. function app_class:get_formspec()
  7. local app_result
  8. if self.formspec_func then
  9. app_result = self.formspec_func(self, self.os)
  10. else
  11. app_result = ""
  12. end
  13. if self.fullscreen then
  14. return app_result
  15. end
  16. if app_result == false then
  17. return false
  18. end
  19. local launcher = self.os:get_app(self.os.hwdef.custom_launcher or "launcher")
  20. local window_formspec = ""
  21. if launcher.appwindow_formspec_func then
  22. window_formspec = launcher.appwindow_formspec_func(launcher, self, self.os)
  23. end
  24. return window_formspec..app_result
  25. end
  26. -- internally used: process input
  27. function app_class:receive_data(method, reshow, sender, ...)
  28. local ret
  29. if self[method] then
  30. ret = self[method](self, self.os, sender, ...)
  31. end
  32. if method == "receive_fields_func" then
  33. local fields = ...
  34. if fields.os_back then
  35. self:back_app()
  36. elseif fields.os_exit then
  37. self:exit_app()
  38. end
  39. end
  40. return ret
  41. end
  42. -- Back to previous app in stack
  43. function app_class:back_app(fields, sender)
  44. self.os:set_app('<pop>', sender, fields)
  45. if fields then
  46. self.os:pass_to_app('receive_fields_func', true, sender, fields)
  47. end
  48. end
  49. -- Exit current app and back to launcher
  50. function app_class:exit_app()
  51. self.os:set_app() -- launcher
  52. end
  53. function app_class:get_timer()
  54. self.os.timer = self.os.timer or minetest.get_node_timer(self.os.pos)
  55. return self.os.timer
  56. end
  57. -- Register new app
  58. function laptop.register_app(name, def)
  59. def.name = name
  60. laptop.apps[name] = def
  61. end
  62. -- Register new app
  63. function laptop.register_view(name, def)
  64. def.view = true
  65. laptop.apps[name] = def
  66. end
  67. -- load all apps
  68. local app_path = minetest.get_modpath('laptop')..'/apps/'
  69. local app_list = minetest.get_dir_list(app_path, false)
  70. for _, file in ipairs(app_list) do
  71. if file:sub(-8) == '_app.lua' then
  72. dofile(app_path..file)
  73. end
  74. end
  75. dofile(app_path..'browser_app.lua')
  76. for _, file in ipairs(app_list) do
  77. if file:sub(-8) == '_app.lua' and file ~= 'browser_app.lua'
  78. then
  79. dofile(app_path..file)
  80. end
  81. end