aboutsummaryrefslogtreecommitdiff
path: root/src/sysapp/login/main.lua
blob: 3aa0695983f936d0b9b538d3a22c01e925cf6486 (plain) (blame)
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
local sys = require 'lx.sys'
local sysdef= require 'lx.sysdef'
local ioctl = require 'lx.ioctl'
local draw = require 'lx.draw'

local gui = require 'lx.gui'
local mainloop = require 'lx.mainloop'

local tk = require 'lx.tk'

print("Hello, world!")

gui.open()
gui.load_cursor('sys:/cursors/left_ptr.png')
gui.show_cursor()

for x = 0, 255, 16 do
	for y = 0, 255, 16 do
		gui.surface:fillrect(x, y, 16, 16, gui.surface:rgb(x, y, 0))
	end
end

local f = draw.load_image('root:/logo.png')
local wm = tk.window_manager()
tk.init(gui, wm)

local img = tk.image(f)
wm:add({title = "Window 1", x = 100, y = 16}, img)
local img2 = tk.box({
			center_content = true,
			vscroll = true,
			hscroll = true,
			vresize = true,
			hresize = true,
		}, tk.image(f))
wm:add({title = "Window 2", x = 16, y = 100}, img2)
local img3 = tk.box({
			constrain_size = true,
			vresize = true,
			hresize = true,
		}, tk.image(f))
wm:add({title = "Window 3", x = 300, y = 150}, img3)

local txt = tk.text({color = tk.rgb(0, 255, 0), width=200}, "Hello, world!\nThis is a long text")
wm:add({title = "Text example", x = 32, y = 32}, txt)


function alert(text, on_dismiss)
	local win = {}
	win.widget =
		tk.grid({}, 
		{ { tk.text({padding = 16}, text) },
		  { tk.box({center_content = true, height = 40},
				   tk.text({background = tk.theme.colors.button.good.bg(tk),
				   			color = tk.theme.colors.button.good.text(tk),
				   			padding = 4,
							 on_click = function() win.close() end},
							"Ok"))
		  } })
	win.close = function()
		sys.dbg_print("Hello!")
		wm:remove(win.win)
		if on_dismiss then on_dismiss() end
	end
	win.win = wm:add({title = "Alert", 
					  x = math.random(0, gui.surface:width() - 100),
					  y = math.random(0, gui.surface:height() - 100),
					 }, win.widget)
end

function loopy()
	alert("Hello, world !!", loopy)
	gui.hide_cursor()
	gui.surface:fillrect(0, 0, gui.surface:width(), gui.surface:height(), gui.surface:rgb(0, 0, 0))
	gui.show_cursor()
	wm:redraw(0, 0, gui.surface:width(), gui.surface:height())
end
loopy()

-- A small file manager test :)

local function open_listing(path)
	local function fileline(name, path, size)
		return {
			tk.text(name),
			tk.text(tostring(size)),
			tk.box({center_content = true, width = 32},
				tk.text({background = tk.theme.colors.button.action.bg(tk),
						 color = tk.theme.colors.button.action.text(tk),
						 on_click = function() open_listing(path) end},
						"open"))
		}
	end
	local griditems = { 
		  { tk.box({hresize = true, width = 200, constrain_size = true, min_width = 16, control_size = 8}, tk.text("Name")),
		    tk.box({hresize = true, width = 75, constrain_size = true, min_width = 16, control_size = 8}, tk.text("Size")),
			tk.box({constrain_size = true}, tk.text("Actions"))
		  }
		}
	local fd = sys.open(path, sysdef.FM_READDIR)
	if not fd then
		alert("Could not FM_READDIR " .. path)
		return
	end

	local i = 0
	while true do
		x = sys.readdir(fd, i)
		if not x then break end
		table.insert(griditems, fileline(x.name .. (x.type & sysdef.FT_DIR ~= 0 and "/" or ""),
										 path .. x.name .. "/",
										 x.size))
		i = i + 1
	end
	sys.close(fd)
	
	local grid = tk.grid({border_size = 1}, griditems)
	wm:add({title = path,
			x = math.random(0, gui.surface:width() - 100),
			y = math.random(0, gui.surface:height() - 100)},	
		   tk.box({vresize = true, hresize = true, vscroll = true, hscroll = true}, grid))
end

local function fsline(fs)
	return {
		tk.text(fs .. ":/"),
		tk.box({center_content = true, width = 32},
			tk.text({background = tk.theme.colors.button.action.bg(tk),
					 color = tk.theme.colors.button.action.text(tk),
					 on_click = function() open_listing(fs .. ":/") end},
					"open"))
	}
end

local grid = tk.grid({border_size = 1}, { 
	  { tk.box({hresize = true, width = 200, constrain_size = true, min_width = 16, control_size = 8}, tk.text("Name")),
		tk.box({constrain_size = true}, tk.text("Actions"))
	  },
	  fsline("io"),
	  fsline("root"),
	  fsline("config"),
	  fsline("app"),
	  fsline("sys"),
	})
wm:add({title = "Filesystems", x = 300, y = 20},	
	   tk.box({vresize = true, hresize = true, vscroll = true, hscroll = true}, grid))

function open_terminal(title, binfile)
	local term_widget = tk.gipwidget({width = 480, height = 360})
	wm:add({title = title, x = 300, y = 200}, term_widget)
	local tty_a, tty_b = sys.make_channel(false)

	local term_bin_pid = sys.new_proc()
	sys.bind_fs(term_bin_pid, "sys", "sys")
	sys.bind_fs(term_bin_pid, "config", "config")
	sys.bind_fd(term_bin_pid, sysdef.STD_FD_GIP, term_widget.ch_cli)
	sys.bind_fd(term_bin_pid, sysdef.STD_FD_TTYSRV, tty_a)
	sys.dbg_print("Start terminal.bin, pid: " .. tonumber(term_bin_pid))
	sys.proc_exec(term_bin_pid, "sys:/bin/terminal.bin")

	local shell_bin_pid = sys.new_proc()
	sys.bind_fs(shell_bin_pid, "root", "root")
	sys.bind_fs(shell_bin_pid, "sys", "sys")
	sys.bind_fs(shell_bin_pid, "config", "config")
	sys.bind_fd(shell_bin_pid, sysdef.STD_FD_TTY_STDIO, tty_b)
	sys.dbg_print("Start " .. binfile .. ", pid: " .. tonumber(shell_bin_pid))
	sys.proc_exec(shell_bin_pid, binfile)
end

open_terminal("Shell", "sys:/bin/shell.bin")
open_terminal("Lua prompt", "sys:/bin/lx.bin")

mainloop.run()

os.exit()