-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
214 lines (191 loc) · 6.96 KB
/
init.lua
File metadata and controls
214 lines (191 loc) · 6.96 KB
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
-- DaniX GUI proof-of-concept
-- Simple login then desktop with a single Terminal app
local kernel = require("src.api.kernel")
local gui = require("DaniX.gui")
-- Login prompt (runs in user space). After login, create the desktop as a kernel process.
local function prompt_login()
-- Try to use Basalt if available
local ok, basalt = pcall(require, "basalt")
if ok and basalt then
local main = basalt.getMainFrame()
if main then
-- different Basalt versions expose different API to clear the main frame.
-- Prefer removeChildren if available, otherwise fall back to clear().
if main.removeChildren then
pcall(function() main:removeChildren() end)
elseif main.clear then
pcall(function() main:clear() end)
end
if main.setBackground then pcall(function() main:setBackground(colors.black) end) end
end
local lbl = main:addLabel()
:setPosition(4,2)
:setText("DaniX Login")
local userInput = main:addInput()
:setPosition(4,4)
:setSize(30,1)
local passInput = main:addInput()
:setPosition(4,6)
:setSize(30,1)
-- some Basalt versions do not implement setPassword on Input; guard the call
if passInput and passInput.setPassword then
pcall(function() passInput:setPassword(true) end)
end
local msg = main:addLabel():setPosition(4,9):setText("")
local loggedUser = nil
local loggedAdmin = false
local function attempt()
local user = userInput:getText() or ""
local pass = passInput:getText() or ""
local users_table = _G and (_G.users or _G.USERS) or nil
if not users_table then
msg:setText("Auth unavailable")
return false
end
local u = users_table[user]
if not u then
msg:setText("Unknown user")
return false
end
if tostring(u.password or "") ~= tostring(pass or "") then
msg:setText("Invalid password")
return false
end
loggedUser = user
loggedAdmin = (u.group == "admin" or u.group == "trusteddaniel")
return true
end
local btn = main:addButton()
:setPosition(4,8)
:setText("Login")
:onClick(function()
if attempt() then
basalt.stop() -- stop basalt so starter continues
end
end)
basalt.run()
if loggedUser then
return loggedUser, loggedAdmin
end
return nil
end
-- Fallback: previous text-mode prompt
-- Draw a centered login box
local w, h = term.getSize()
local box_w, box_h = math.min(40, w - 4), 10
local box_x = math.floor((w - box_w) / 2) + 1
local box_y = math.floor((h - box_h) / 2) + 1
term.setBackgroundColor(colors.lightGray)
term.clear()
for yy = 1, h do
term.setCursorPos(1, yy)
term.write(string.rep(" ", w))
end
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.setCursorPos(box_x + 2, box_y + 1)
term.write("DaniX Login")
term.setCursorPos(box_x + 2, box_y + 3)
write("Username: ")
-- simple line editor using os.pullEvent so we don't rely on global read
local function read_line(mask)
local buf = ""
local start_x, start_y = term.getCursorPos()
local function redraw()
term.setCursorPos(start_x, start_y)
local display = mask and string.rep("*", #buf) or buf
term.write(display)
-- clear trailing char (simple one-char clear to avoid artifacts)
term.write(" ")
term.setCursorPos(start_x + #display, start_y)
end
local last_char = nil
local last_time = 0
local function now()
if type(os.clock) == "function" then return os.clock() end
if type(os.time) == "function" then return os.time() end
return 0
end
local debounce_threshold = 0.06
while true do
redraw()
local ev = { os.pullEvent() }
if ev[1] == "char" then
local ch = ev[2]
local t = now()
if last_char == ch and (t - last_time) < debounce_threshold then
-- ignore duplicate rapid char
else
buf = buf .. ch
last_char = ch
last_time = t
end
elseif ev[1] == "key" then
local k = ev[2]
-- common Enter/Return key codes: 13, 28
if k == 13 or k == 28 then
break
end
-- Backspace codes: 8, 14
if k == 8 or k == 14 then
if #buf > 0 then
buf = string.sub(buf, 1, -2)
end
end
elseif ev[1] == "terminate" then
error("terminate")
end
end
return buf
end
local user = read_line(false)
term.setCursorPos(box_x + 2, box_y + 5)
write("Password: ")
local pass = read_line(true)
-- Require the kernel's users table. If missing, refuse login (safer)
local users_table = _G and (_G.users or _G.USERS) or nil
if not users_table then
term.setCursorPos(box_x + 2, box_y + 7)
term.write("Auth system unavailable")
os.sleep(1.5)
return nil
end
local u = users_table[user]
if not u then
term.setCursorPos(box_x + 2, box_y + 7)
term.write("Unknown user")
os.sleep(1.5)
return nil
end
if tostring(u.password or "") ~= tostring(pass or "") then
term.setCursorPos(box_x + 2, box_y + 7)
term.write("Invalid password")
os.sleep(1.5)
return nil
end
local isAdmin = (u.group == "admin" or u.group == "trusteddaniel")
return user, isAdmin
end
local function run()
while true do
local user, isAdmin = prompt_login()
if user then
-- start scheduler if available and not running
if kernel.start_scheduler then pcall(kernel.start_scheduler) end
-- create desktop process via kernel so DaniX follows kernel docs
local pid, err = kernel.create_process(function()
local desktop = require("DaniX.gui")
desktop.start(user, isAdmin)
end, "DaniX_desktop", user, {"basic"})
if not pid then
print("Failed to create desktop process:", err)
os.sleep(2)
-- try again (loop)
else
-- Desktop launched; return so starter can continue and drive scheduler
return pid
end
end
end
end
return { run = run }