-
Notifications
You must be signed in to change notification settings - Fork 188
/
OS.lua
234 lines (185 loc) · 6.58 KB
/
OS.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
---------------------------------------- System initialization ----------------------------------------
-- Obtaining boot filesystem component proxy
local bootFilesystemProxy = component.proxy(component.invoke(component.list("eeprom")(), "getData"))
-- Executes file from boot HDD during OS initialization (will be overriden in filesystem library later)
function dofile(path)
local stream, reason = bootFilesystemProxy.open(path, "r")
if stream then
local data, chunk = ""
while true do
chunk = bootFilesystemProxy.read(stream, math.huge)
if chunk then
data = data .. chunk
else
break
end
end
bootFilesystemProxy.close(stream)
local result, reason = load(data, "=" .. path)
if result then
return result()
else
error(reason)
end
else
error(reason)
end
end
-- Initializing global package system
package = {
paths = {
["/Libraries/"] = true
},
loaded = {},
loading = {}
}
-- Checks existense of specified path. It will be overriden after filesystem library initialization
local requireExists = bootFilesystemProxy.exists
-- Works the similar way as native Lua require() function
function require(module)
-- For non-case-sensitive filesystems
local lowerModule = unicode.lower(module)
if package.loaded[lowerModule] then
return package.loaded[lowerModule]
elseif package.loading[lowerModule] then
error("recursive require() call found: library \"" .. module .. "\" is trying to require another library that requires it\n" .. debug.traceback())
else
local errors = {}
local function checkVariant(variant)
if requireExists(variant) then
return variant
else
table.insert(errors, " variant \"" .. variant .. "\" not exists")
end
end
local function checkVariants(path, module)
return
checkVariant(path .. module .. ".lua") or
checkVariant(path .. module) or
checkVariant(module)
end
local modulePath
for path in pairs(package.paths) do
modulePath =
checkVariants(path, module) or
checkVariants(path, unicode.upper(unicode.sub(module, 1, 1)) .. unicode.sub(module, 2, -1))
if modulePath then
package.loading[lowerModule] = true
local result = dofile(modulePath)
package.loaded[lowerModule] = result or true
package.loading[lowerModule] = nil
return result
end
end
error("unable to locate library \"" .. module .. "\":\n" .. table.concat(errors, "\n"))
end
end
local GPUAddress = component.list("gpu")()
local screenWidth, screenHeight = component.invoke(GPUAddress, "getResolution")
-- Displays title and currently required library when booting OS
local UIRequireTotal, UIRequireCounter = 14, 1
local function UIRequire(module)
local function centrize(width)
return math.floor(screenWidth / 2 - width / 2)
end
local title, width, total = "MineOS", 26, 14
local x, y, part = centrize(width), math.floor(screenHeight / 2 - 1), math.ceil(width * UIRequireCounter / UIRequireTotal)
UIRequireCounter = UIRequireCounter + 1
-- Title
component.invoke(GPUAddress, "setForeground", 0x2D2D2D)
component.invoke(GPUAddress, "set", centrize(#title), y, title)
-- Progressbar
component.invoke(GPUAddress, "setForeground", 0x878787)
component.invoke(GPUAddress, "set", x, y + 2, string.rep("─", part))
component.invoke(GPUAddress, "setForeground", 0xC3C3C3)
component.invoke(GPUAddress, "set", x + part, y + 2, string.rep("─", width - part))
return require(module)
end
-- Preparing screen for loading libraries
component.invoke(GPUAddress, "setBackground", 0xE1E1E1)
component.invoke(GPUAddress, "fill", 1, 1, screenWidth, screenHeight, " ")
-- Loading libraries
bit32 = bit32 or UIRequire("Bit32")
local paths = UIRequire("Paths")
local event = UIRequire("Event")
local filesystem = UIRequire("Filesystem")
-- Setting main filesystem proxy to what are we booting from
filesystem.setProxy(bootFilesystemProxy)
-- Replacing requireExists function after filesystem library initialization
requireExists = filesystem.exists
-- Loading other libraries
UIRequire("Component")
UIRequire("Keyboard")
UIRequire("Color")
UIRequire("Text")
UIRequire("Number")
local image = UIRequire("Image")
local screen = UIRequire("Screen")
-- Setting currently chosen GPU component as screen buffer main one
screen.setGPUAddress(GPUAddress)
local GUI = UIRequire("GUI")
local system = UIRequire("System")
UIRequire("Network")
-- Filling package.loaded with default global variables for OpenOS bitches
package.loaded.bit32 = bit32
package.loaded.computer = computer
package.loaded.component = component
package.loaded.unicode = unicode
---------------------------------------- Main loop ----------------------------------------
-- Creating OS workspace, which contains every window/menu/etc.
local workspace = GUI.workspace()
system.setWorkspace(workspace)
-- "double_touch" event handler
local doubleTouchInterval, doubleTouchX, doubleTouchY, doubleTouchButton, doubleTouchUptime, doubleTouchcomponentAddress = 0.3
event.addHandler(
function(signalType, componentAddress, x, y, button, user)
if signalType == "touch" then
local uptime = computer.uptime()
if doubleTouchX == x and doubleTouchY == y and doubleTouchButton == button and doubleTouchcomponentAddress == componentAddress and uptime - doubleTouchUptime <= doubleTouchInterval then
computer.pushSignal("double_touch", componentAddress, x, y, button, user)
event.skip("touch")
end
doubleTouchX, doubleTouchY, doubleTouchButton, doubleTouchUptime, doubleTouchcomponentAddress = x, y, button, uptime, componentAddress
end
end
)
-- Screen component attaching/detaching event handler
event.addHandler(
function(signalType, componentAddress, componentType)
if (signalType == "component_added" or signalType == "component_removed") and componentType == "screen" then
local GPUAddress = screen.getGPUAddress()
local function bindScreen(address)
screen.setScreenAddress(address, false)
screen.setColorDepth(screen.getMaxColorDepth())
workspace:draw()
end
if signalType == "component_added" then
if not component.invoke(GPUAddress, "getScreen") then
bindScreen(componentAddress)
end
else
if not component.invoke(GPUAddress, "getScreen") then
local address = component.list("screen")()
if address then
bindScreen(address)
end
end
end
end
end
)
-- Logging in
system.authorize()
-- Main loop with UI regeneration after errors
while true do
local success, path, line, traceback = system.call(workspace.start, workspace, 0)
if success then
break
else
system.updateWorkspace()
system.updateDesktop()
workspace:draw()
system.error(path, line, traceback)
workspace:draw()
end
end