- Add an AstalGreet login UI with PAM messages, reboot and shutdown. - Run it under cage via start-greeter.sh and start a uwsm-managed Hyprland session. - Include install script, polkit rule, fallback theme and README.
372 lines
11 KiB
TypeScript
372 lines
11 KiB
TypeScript
import app from "ags/gtk4/app"
|
|
import { Gtk } from "ags/gtk4"
|
|
import Greet from "gi://AstalGreet?version=0.1"
|
|
import GLib from "gi://GLib?version=2.0"
|
|
import Gio from "gi://Gio?version=2.0"
|
|
import { execAsync } from "ags/process"
|
|
|
|
// Filled in by greeter/install.sh. Falls back to the local user for preview.
|
|
const DEFAULT_USER_FILE = "/etc/greetd/ags/default-user"
|
|
|
|
// Matches /usr/share/wayland-sessions/hyprland-uwsm.desktop.
|
|
// If you ever stop using uwsm, replace this with ["/usr/bin/start-hyprland"].
|
|
const SESSION_CMD = [
|
|
"/usr/bin/uwsm",
|
|
"start",
|
|
"-e",
|
|
"-D",
|
|
"Hyprland",
|
|
"hyprland.desktop",
|
|
]
|
|
const SESSION_ENV = [
|
|
"XDG_SESSION_TYPE=wayland",
|
|
"XDG_CURRENT_DESKTOP=Hyprland",
|
|
"XDG_SESSION_DESKTOP=Hyprland",
|
|
]
|
|
|
|
// Written by exportGreeterTheme() in theme.ts whenever the shell theme changes.
|
|
const THEME_CSS = "/var/tmp/ags-greeter/greeter.css"
|
|
const FALLBACK_CSS = "/etc/greetd/ags/fallback.css"
|
|
const WALLPAPER = "/var/tmp/ags-greeter/wallpaper.jpg"
|
|
|
|
const INLINE_CSS = `
|
|
window.Greeter { background-color: #1e1e1e; color: #d4d4d4; }
|
|
window.Greeter .scrim { background-color: rgba(30, 30, 30, 0.45); padding: 32px; }
|
|
window.Greeter .login-card {
|
|
background-color: rgba(30, 30, 30, 0.9);
|
|
border: 1px solid rgba(212, 212, 212, 0.15);
|
|
border-radius: 16px;
|
|
padding: 32px;
|
|
min-width: 320px;
|
|
}
|
|
window.Greeter .greeting { font-size: 2em; font-weight: bold; }
|
|
window.Greeter .subtitle { opacity: 0.65; margin-bottom: 8px; }
|
|
window.Greeter entry {
|
|
background-color: rgba(212, 212, 212, 0.08);
|
|
border: 1px solid rgba(212, 212, 212, 0.15);
|
|
border-radius: 8px;
|
|
padding: 10px 12px;
|
|
color: #d4d4d4;
|
|
}
|
|
window.Greeter button.login-btn {
|
|
background-color: #6a9955;
|
|
color: #1e1e1e;
|
|
border: none;
|
|
border-radius: 8px;
|
|
padding: 10px;
|
|
font-weight: bold;
|
|
}
|
|
window.Greeter .messages { min-height: 1.2em; }
|
|
window.Greeter .message { font-size: 0.9em; opacity: 0.85; }
|
|
window.Greeter .message.error { color: #f44747; }
|
|
window.Greeter .message.info { color: #569cd6; }
|
|
window.Greeter .power-row { margin-top: 4px; }
|
|
window.Greeter button.power-action {
|
|
background-color: rgba(212, 212, 212, 0.06);
|
|
border: 1px solid rgba(212, 212, 212, 0.1);
|
|
border-radius: 8px;
|
|
padding: 8px;
|
|
min-width: 36px;
|
|
min-height: 36px;
|
|
}
|
|
window.Greeter button.power-action:hover {
|
|
background-color: rgba(212, 212, 212, 0.12);
|
|
}
|
|
window.Greeter button.power-action image { -gtk-icon-size: 18px; }
|
|
`
|
|
|
|
// greetd only reports PAM result codes in the error description, e.g.
|
|
// "authenticate: AUTH_ERR". Map them to something human readable.
|
|
const AUTH_ERROR_MESSAGES: Record<string, string> = {
|
|
AUTH_ERR: "Incorrect password. Please try again.",
|
|
MAXTRIES: "Too many failed attempts.",
|
|
CRED_EXPIRED: "Your credentials have expired.",
|
|
ACCT_EXPIRED: "Your account has expired.",
|
|
CRED_INSUFFICIENT: "Insufficient credentials.",
|
|
USER_UNKNOWN: "Unknown username.",
|
|
PERM_DENIED: "Permission denied.",
|
|
SERVICE_ERR: "Authentication service error.",
|
|
ABORT: "Authentication aborted.",
|
|
}
|
|
|
|
function errorDescription(error: any): string {
|
|
try {
|
|
if (typeof error?.get_description === "function")
|
|
return error.get_description() ?? ""
|
|
return typeof error?.description === "string" ? error.description : ""
|
|
} catch {
|
|
return ""
|
|
}
|
|
}
|
|
|
|
function errorType(error: any): number | null {
|
|
try {
|
|
if (typeof error?.get_error_type === "function")
|
|
return error.get_error_type()
|
|
return typeof error?.error_type === "number" ? error.error_type : null
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
function humanizeError(error: any): string {
|
|
const description = errorDescription(error)
|
|
const code = description.match(/\b([A-Z][A-Z_]+)\b\s*$/)?.[1]
|
|
|
|
if (code && AUTH_ERROR_MESSAGES[code]) return AUTH_ERROR_MESSAGES[code]
|
|
|
|
// Only show the raw description when it is not a cryptic PAM result code.
|
|
if (description && !/^[a-z_]+: [A-Z_]+$/.test(description)) return description
|
|
|
|
// AstalGreet.ErrorType.AUTH_ERROR is 0
|
|
if (errorType(error) === 0) return "Authentication failed. Please try again."
|
|
|
|
return description || "Login failed"
|
|
}
|
|
|
|
function readTextFile(path: string): string | null {
|
|
try {
|
|
const [ok, contents] = GLib.file_get_contents(path)
|
|
if (!ok) return null
|
|
let text = ""
|
|
for (const byte of contents) text += String.fromCharCode(byte)
|
|
return text.trim()
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
function defaultUser() {
|
|
return readTextFile(DEFAULT_USER_FILE) || "pascal"
|
|
}
|
|
|
|
function themeCss() {
|
|
if (GLib.file_test(THEME_CSS, GLib.FileTest.EXISTS)) return THEME_CSS
|
|
if (GLib.file_test(FALLBACK_CSS, GLib.FileTest.EXISTS)) return FALLBACK_CSS
|
|
return INLINE_CSS
|
|
}
|
|
|
|
function Greeter() {
|
|
const user = defaultUser()
|
|
const underGreetd = GLib.getenv("GREETD_SOCK") !== null
|
|
const greeter = new Greet.Greeter()
|
|
|
|
let userEntry: Gtk.Entry | null = null
|
|
let passEntry: Gtk.Entry | null = null
|
|
let messageList: Gtk.Box | null = null
|
|
let loginButton: Gtk.Button | null = null
|
|
let busy = false
|
|
let lastPamError = ""
|
|
|
|
const messageWidgets: Gtk.Label[] = []
|
|
|
|
const appendMessage = (text: string, kind: "error" | "info" = "info") => {
|
|
if (!messageList || !text) return
|
|
|
|
const label = new Gtk.Label({ label: text, wrap: true, xalign: 0 })
|
|
label.set_css_classes(["message", kind])
|
|
messageList.append(label)
|
|
messageWidgets.push(label)
|
|
|
|
while (messageWidgets.length > 4) {
|
|
const old = messageWidgets.shift()
|
|
if (old) messageList.remove(old)
|
|
}
|
|
}
|
|
|
|
const clearMessages = () => {
|
|
if (!messageList) return
|
|
for (const label of messageWidgets) messageList.remove(label)
|
|
messageWidgets.length = 0
|
|
}
|
|
|
|
const reset = () => {
|
|
busy = false
|
|
if (loginButton) loginButton.sensitive = true
|
|
if (passEntry) {
|
|
passEntry.set_text("")
|
|
passEntry.grab_focus()
|
|
}
|
|
}
|
|
|
|
const runPower = (action: "reboot" | "poweroff") => {
|
|
if (!underGreetd) {
|
|
appendMessage("This greeter must be started by greetd", "error")
|
|
return
|
|
}
|
|
|
|
appendMessage(action === "reboot" ? "Rebooting…" : "Shutting down…")
|
|
execAsync(["/usr/bin/systemctl", action]).catch((error: unknown) => {
|
|
appendMessage(`Failed to ${action}: ${`${error}`.trim()}`, "error")
|
|
})
|
|
}
|
|
|
|
const login = () => {
|
|
if (busy) return
|
|
|
|
if (!underGreetd) {
|
|
appendMessage("This greeter must be started by greetd", "error")
|
|
return
|
|
}
|
|
|
|
const username = userEntry?.get_text().trim() || user
|
|
if (!passEntry?.get_text()) {
|
|
appendMessage("Enter your password", "error")
|
|
passEntry?.grab_focus()
|
|
return
|
|
}
|
|
|
|
busy = true
|
|
if (loginButton) loginButton.sensitive = false
|
|
lastPamError = ""
|
|
clearMessages()
|
|
greeter.create_session(username)
|
|
}
|
|
|
|
greeter.connect("visible-request", (_src: unknown, _message: string) => {
|
|
lastPamError = ""
|
|
greeter.post_auth(userEntry?.get_text().trim() || user)
|
|
})
|
|
|
|
greeter.connect("secret-request", (_src: unknown, _message: string) => {
|
|
lastPamError = ""
|
|
greeter.post_auth(passEntry?.get_text() ?? "")
|
|
})
|
|
|
|
greeter.connect("info-message", (_src: unknown, message: string) => {
|
|
appendMessage(message)
|
|
})
|
|
|
|
greeter.connect("error-message", (_src: unknown, message: string) => {
|
|
lastPamError = message
|
|
appendMessage(message, "error")
|
|
})
|
|
|
|
greeter.connect("cancelled", (_src: unknown, error: any) => {
|
|
if (!lastPamError) appendMessage(humanizeError(error), "error")
|
|
lastPamError = ""
|
|
reset()
|
|
})
|
|
|
|
greeter.connect("authenticated", () => {
|
|
greeter.start_session(
|
|
SESSION_CMD,
|
|
SESSION_ENV,
|
|
(_src: unknown, res: any) => {
|
|
try {
|
|
greeter.start_session_finish(res)
|
|
} catch (error: any) {
|
|
appendMessage(error?.message ?? `${error}`, "error")
|
|
reset()
|
|
return
|
|
}
|
|
|
|
// greetd starts the session once the greeter process exits.
|
|
app.quit()
|
|
},
|
|
)
|
|
})
|
|
|
|
return (
|
|
<Gtk.ApplicationWindow
|
|
application={app}
|
|
class="Greeter"
|
|
title="Login"
|
|
decorated={false}
|
|
defaultWidth={800}
|
|
defaultHeight={500}
|
|
$={(window: Gtk.ApplicationWindow) => {
|
|
window.present()
|
|
GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
|
|
passEntry?.grab_focus()
|
|
return GLib.SOURCE_REMOVE
|
|
})
|
|
}}
|
|
>
|
|
<overlay
|
|
$={(overlay: Gtk.Overlay) => {
|
|
const content = overlay.get_child()
|
|
const picture = new Gtk.Picture()
|
|
picture.set_content_fit(Gtk.ContentFit.COVER)
|
|
picture.set_hexpand(true)
|
|
picture.set_vexpand(true)
|
|
if (GLib.file_test(WALLPAPER, GLib.FileTest.EXISTS)) {
|
|
picture.set_file(Gio.File.new_for_path(WALLPAPER))
|
|
}
|
|
overlay.set_child(picture)
|
|
if (content) overlay.add_overlay(content)
|
|
}}
|
|
>
|
|
<box cssClasses={["scrim"]} orientation={Gtk.Orientation.VERTICAL}>
|
|
<box vexpand />
|
|
<box
|
|
cssClasses={["login-card"]}
|
|
orientation={Gtk.Orientation.VERTICAL}
|
|
spacing={12}
|
|
halign={Gtk.Align.CENTER}
|
|
>
|
|
<label label="Welcome back" cssClasses={["greeting"]} />
|
|
<label
|
|
label="Log in to start your session"
|
|
cssClasses={["subtitle"]}
|
|
/>
|
|
<entry
|
|
text={user}
|
|
placeholderText="Username"
|
|
onActivate={login}
|
|
$={(entry: Gtk.Entry) => (userEntry = entry)}
|
|
/>
|
|
<entry
|
|
placeholderText="Password"
|
|
visibility={false}
|
|
onActivate={login}
|
|
$={(entry: Gtk.Entry) => (passEntry = entry)}
|
|
/>
|
|
<button
|
|
cssClasses={["login-btn"]}
|
|
onClicked={login}
|
|
$={(button: Gtk.Button) => (loginButton = button)}
|
|
>
|
|
<label label="Log in" />
|
|
</button>
|
|
<box
|
|
cssClasses={["messages"]}
|
|
orientation={Gtk.Orientation.VERTICAL}
|
|
spacing={4}
|
|
$={(box: Gtk.Box) => (messageList = box)}
|
|
/>
|
|
<box
|
|
cssClasses={["power-row"]}
|
|
spacing={8}
|
|
halign={Gtk.Align.CENTER}
|
|
>
|
|
<button
|
|
cssClasses={["power-action"]}
|
|
tooltipText="Reboot"
|
|
onClicked={() => runPower("reboot")}
|
|
>
|
|
<image iconName="system-reboot-symbolic" />
|
|
</button>
|
|
<button
|
|
cssClasses={["power-action"]}
|
|
tooltipText="Shut down"
|
|
onClicked={() => runPower("poweroff")}
|
|
>
|
|
<image iconName="system-shutdown-symbolic" />
|
|
</button>
|
|
</box>
|
|
</box>
|
|
<box vexpand />
|
|
</box>
|
|
</overlay>
|
|
</Gtk.ApplicationWindow>
|
|
)
|
|
}
|
|
|
|
app.start({
|
|
css: themeCss(),
|
|
main() {
|
|
Greeter()
|
|
},
|
|
})
|