forked from FyshOS/fin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
331 lines (289 loc) · 8.7 KB
/
ui.go
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"fmt"
"image/color"
"io/ioutil"
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"strconv"
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/jezek/xgb"
"github.com/jezek/xgb/randr"
"github.com/jezek/xgb/xproto"
)
const (
prefSessionKey = "user.%s.session"
prefUserKey = "default.user"
)
type ui struct {
win fyne.Window
pass *widget.Entry
session *widget.Select
err *canvas.Text
hostname func() string
user string
users func() []string
sessions []*session
pref fyne.Preferences
}
func newUI(w fyne.Window, p fyne.Preferences, host func() string, users func() []string) *ui {
return &ui{win: w, hostname: host, pref: p, sessions: loadSessions(), users: users}
}
func (u *ui) askShutdown() {
var pop *widget.PopUp
message := widget.NewLabel("Are you sure you want to power off your computer?")
message.Alignment = fyne.TextAlignCenter
buttons := container.NewGridWithColumns(3,
widget.NewButtonWithIcon("Cancel", theme.CancelIcon(), func() {
pop.Hide()
}),
widget.NewButtonWithIcon("Reboot", theme.ViewRefreshIcon(), func() {
pop.Hide()
_ = exec.Command("shutdown", "-r", "now").Start()
}),
container.NewMax(newButtonBackground(theme.ErrorColor()),
widget.NewButtonWithIcon("Power off", theme.NewThemedResource(resourcePowerSvg), func() {
pop.Hide()
_ = exec.Command("shutdown", "-h", "now").Start()
})))
body := container.NewVBox(message, container.NewCenter(buttons))
title := widget.NewLabelWithStyle("Shutdown", fyne.TextAlignLeading, fyne.TextStyle{Bold: true})
prop := canvas.NewRectangle(color.Transparent)
prop.SetMinSize(body.MinSize().Add(fyne.NewSize(32, 16))) // pad to match dialog
content := container.NewVBox(title, container.NewMax(prop, body))
pop = widget.NewModalPopUp(content, u.win.Canvas())
pop.Show()
}
func (u *ui) doLogin() {
if u.user == "" || u.pass.Text == "" {
u.setError("Missing username or password")
return
}
u.pref.SetString(fmt.Sprintf(prefSessionKey, u.user), u.session.Selected)
u.pref.SetString(prefUserKey, u.user)
go func() {
pid, err := login(u.user, u.pass.Text, u.sessionExec())
if err != nil {
u.setError(err.Error())
return
}
proc, err := os.FindProcess(pid)
if err != nil {
u.setError(err.Error())
u.win.Show()
return
}
// OpenBSD: give device ownership to logged in user
if runtime.GOOS == "openbsd" {
usr, _ := user.Lookup(u.user)
uid, _ := strconv.Atoi(usr.Uid)
_ = os.Chown("/dev/console", uid, -1)
_ = os.Chown("/dev/dri/card0", uid, -1)
_ = os.Chown("/dev/dri/renderD128", uid, -1)
}
u.win.Hide()
_, _ = proc.Wait()
u.win.Show()
_ = logout()
u.pass.SetText("")
u.win.Canvas().Focus(u.pass)
u.setError("")
// OpenBSD: give device ownership back to root
if runtime.GOOS == "openbsd" {
_ = os.Chown("/dev/console", 0, -1)
_ = os.Chown("/dev/dri/card0", 0, -1)
_ = os.Chown("/dev/dri/renderD128", 0, -1)
}
}()
}
func (u *ui) setError(err string) {
u.err.Text = err
u.err.Refresh()
}
func (u *ui) loadUI() {
u.pass = widget.NewPasswordEntry()
u.pass.OnSubmitted = func(string) {
u.win.Canvas().Focus(nil)
u.doLogin()
}
u.session = widget.NewSelect(u.sessionNames(), func(string) {})
u.err = canvas.NewText("", theme.ErrorColor())
u.err.Alignment = fyne.TextAlignCenter
users := u.users()
var formItems []*widget.FormItem
if len(users) == 0 {
user := widget.NewEntry()
user.OnChanged = func(user string) {
u.user = user
}
formItems = append(formItems, widget.NewFormItem("Username", user))
}
formItems = append(formItems,
widget.NewFormItem("Password", u.pass),
widget.NewFormItem("Session", u.session))
f := widget.NewForm(formItems...)
login := widget.NewButtonWithIcon("Log In", theme.LoginIcon(), u.doLogin)
login.Importance = widget.HighImportance
buttons := container.NewGridWithColumns(2,
widget.NewButtonWithIcon("Shutdown", theme.NewThemedResource(resourcePowerSvg), u.askShutdown),
login)
bg := canvas.NewImageFromResource(background)
bgCol := fyne.CurrentApp().Settings().Theme().Color(
"fynedeskPanelBackground",
fyne.CurrentApp().Settings().ThemeVariant())
if bgCol == nil || bgCol == color.Transparent {
r, g, b, _ := theme.BackgroundColor().RGBA()
bgCol = color.NRGBA{R: uint8(r >> 8), G: uint8(g >> 8), B: uint8(b >> 8), A: 0xdd}
}
box := canvas.NewRectangle(bgCol)
var avatars []fyne.CanvasObject
for _, name := range users {
ava := newAvatar(name, func(user string) {
for _, a := range avatars {
border := a.(*fyne.Container).Objects[0].(*fyne.Container).Objects[0].(*canvas.Rectangle)
border.StrokeColor = theme.ShadowColor()
border.Refresh()
}
u.user = user
u.updateForUsername(user)
u.win.Canvas().Focus(u.pass)
})
avatars = append(avatars, ava)
}
u.win.SetContent(container.NewMax(bg,
container.NewCenter(container.NewMax(box, container.NewVBox(
widget.NewLabelWithStyle("Log in to "+u.hostname(), fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
widget.NewSeparator(),
container.NewMax(widget.NewLabel(""), u.err),
container.NewCenter(container.NewHBox(avatars...)),
container.NewBorder(nil, nil, widget.NewLabel(" "), widget.NewLabel(" "),
container.NewVBox(f, buttons)),
widget.NewLabel(""),
))),
))
matched := false
storedName := u.pref.String(prefUserKey)
for i, name := range users {
if name != storedName {
continue
}
avatars[i].(*fyne.Container).Objects[0].(*fyne.Container).Objects[1].(*widget.Button).Tapped(&fyne.PointEvent{})
matched = true
}
if matched {
u.win.Canvas().Focus(u.pass)
} else if len(users) == 0 {
u.win.Canvas().Focus(formItems[0].Widget.(*widget.Entry))
}
}
func (u *ui) sessionNames() []string {
names := make([]string, len(u.sessions))
for i, sess := range u.sessions {
names[i] = sess.name
}
return names
}
func (u *ui) sessionExec() string {
for _, sess := range u.sessions {
if sess.name == u.session.Selected {
return sess.exec
}
}
return u.sessions[0].exec
}
func (u *ui) updateForUsername(user string) {
home, _ := homedir(user)
if _, err := os.Stat(filepath.Join(home, ".xinitrc")); err != nil {
if u.sessions[len(u.sessions)-1] == xinitSession {
u.sessions = u.sessions[:len(u.sessions)-1]
u.session.Options = u.sessionNames()
u.session.Refresh()
}
} else {
if u.sessions[len(u.sessions)-1] != xinitSession {
u.sessions = append(u.sessions, xinitSession)
u.session.Options = u.sessionNames()
u.session.Refresh()
}
}
last := u.pref.String(fmt.Sprintf(prefSessionKey, user))
if last != "" {
u.session.SetSelected(last)
}
}
func getScreenSize() (uint16, uint16) {
conn, err := xgb.NewConn()
if err != nil {
log.Println("ScreenSize X connect error", err)
return 1280, 720
}
err = randr.Init(conn)
if err != nil {
log.Println("ScreenSize X RandR error", err)
return 1280, 720
}
root := xproto.Setup(conn).DefaultScreen(conn).Root
resources, _ := randr.GetScreenResources(conn, root).Reply()
// Get first connected output
// TODO: Consider multiple connected outputs in multihead mode
var crtcInfo *randr.GetCrtcInfoReply
for _, v := range resources.Outputs {
output, _ := randr.GetOutputInfo(conn, v, 0).Reply()
// 0 = "connected", 1 = "disconnected, 2 = "unknown"
if output.Connection == 0 {
crtcInfo, _ = randr.GetCrtcInfo(conn, output.Crtc, 0).Reply()
break
}
}
return crtcInfo.Width, crtcInfo.Height
}
func getUsers() []string {
data, err := ioutil.ReadFile("/etc/passwd")
if err != nil {
fyne.LogError("Failed to read password", err)
return []string{""}
}
var ret []string
for _, line := range strings.Split(string(data), "\n") {
if strings.Contains(line, "nologin") || strings.Contains(line, "/var/empty") {
continue
}
fields := strings.Split(line, ":")
if len(fields) < 7 || fields[0] == "root" || fields[6][len(fields[6])-2:] != "sh" {
continue
}
ret = append(ret, fields[0])
}
return ret
}
func newAvatar(user string, f func(string)) fyne.CanvasObject {
ava := canvas.NewImageFromResource(theme.AccountIcon())
home, _ := homedir(user)
facePath := filepath.Join(home, ".face")
if _, err := os.Stat(facePath); err == nil {
ava = canvas.NewImageFromFile(facePath)
}
ava.SetMinSize(fyne.NewSize(120, 120))
border := canvas.NewRectangle(theme.InputBackgroundColor())
border.StrokeWidth = theme.InputBorderSize()
border.StrokeColor = theme.ShadowColor()
tapper := widget.NewButton("", func() {
f(user)
border.StrokeColor = theme.PrimaryColor()
border.Refresh()
})
tapper.Importance = widget.LowImportance
img := container.NewMax(border, tapper, ava)
return container.NewVBox(img,
widget.NewLabelWithStyle(user, fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
)
}