refactor(display): use chrome devtools protocol (#38)

This commit is contained in:
2026-01-30 15:26:57 +01:00
committed by GitHub
parent cbbf50e5a4
commit 71f152ef2a
24 changed files with 570 additions and 504 deletions
@@ -51,6 +51,17 @@ export async function show_html(ip: string, html: string): Promise<void> {
await request_display(ip, '/showHTML', options);
}
export async function open_website(ip: string, url: string): Promise<void> {
const options = {
method: 'PATCH',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
url: url
})
};
await request_display(ip, '/openWebsite', options);
}
export async function get_file_data(
ip: string,
path: string
@@ -21,7 +21,8 @@
show_blackscreen,
shutdown,
startup,
show_html
show_html,
open_website
} from '$lib/ts/api_handler';
import {
get_display_by_id,
@@ -144,7 +145,7 @@
async function send_website() {
popup_content.open = false;
await run_on_all_selected_displays((d) =>
show_html(d.ip, `<iframe src="${website_url}"></iframe>`)
open_website(d.ip, website_url)
);
}
</script>
+1 -1
View File
@@ -64,7 +64,7 @@ func main() {
os.Exit(1)
}
}()
err = shared.OpenBrowserWindow("http://localhost:"+port, false, "control")
err = openBrowserWindow("http://localhost:" + port)
if err != nil {
slog.Error("Failed to open browser window", "error", err)
os.Exit(1)
@@ -1,4 +1,4 @@
package shared
package main
import (
"errors"
@@ -6,35 +6,30 @@ import (
"os"
"os/exec"
"path/filepath"
"plg-mudics/shared"
)
func OpenBrowserWindow(url string, fullscreen bool, profile string) error {
func openBrowserWindow(url string) error {
bins := []string{"chromium", "chromium-browser"}
home, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("unable to determine user home directory: %w", err)
}
browserProfileDirPath := filepath.Join(home, ".local", "share", "plg-mudics", fmt.Sprintf("browser-%s", profile))
browserProfileDirPath := filepath.Join(home, ".local", "share", "plg-mudics", "browser-control")
if err := os.MkdirAll(browserProfileDirPath, os.ModePerm); err != nil {
return fmt.Errorf("failed to create local config directory: %w", err)
}
args := []string{
fmt.Sprintf("--app=%s", url),
"--autoplay-policy=no-user-gesture-required",
fmt.Sprintf("--user-data-dir=%s", browserProfileDirPath),
"--allow-running-insecure-content",
"--disable-features=XFrameOptions",
}
if fullscreen {
args = append(args, "--start-fullscreen")
}
errs := []string{}
for _, bin := range bins {
cmd := exec.Command(bin, args...)
commandOutput := RunShellCommand(cmd)
commandOutput := shared.RunShellCommand(cmd)
if commandOutput.ExitCode == 0 {
return nil
}
+6
View File
@@ -62,6 +62,12 @@ Even when the command itself fails.
The screenshot as binary in the response body.
## PATCH `/openWebsite`
### Request Body
- `url`: string
## POST `/file/<path>` - Upload File
### Responses
+72
View File
@@ -0,0 +1,72 @@
package browser
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/chromedp/chromedp"
)
var Browser BrowserType = BrowserType{}
type BrowserType struct {
Ctx context.Context
Cancel context.CancelFunc
}
func (b *BrowserType) Init() error {
home, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("unable to determine user home directory: %w", err)
}
browserProfileDirPath := filepath.Join(home, ".local", "share", "plg-mudics", "browser-display")
if err := os.MkdirAll(browserProfileDirPath, os.ModePerm); err != nil {
return fmt.Errorf("failed to create local config directory: %w", err)
}
opts := []chromedp.ExecAllocatorOption{
chromedp.Flag("headless", false),
chromedp.Flag("app", "http://example.com"), // app mode prevents a few unwanted features of chrome, the start url is directly overwritten
chromedp.Flag("start-fullscreen", true),
chromedp.Flag("hide-scrollbars", true),
chromedp.Flag("allow-file-access-from-files", true),
chromedp.Flag("user-data-dir", browserProfileDirPath),
chromedp.Flag("autoplay-policy", "no-user-gesture-required"),
}
initCtx, _ := chromedp.NewExecAllocator(context.Background(), opts...)
b.Ctx, b.Cancel = chromedp.NewContext(initCtx)
return nil
}
func (b *BrowserType) OpenPage(url string) {
chromedp.Run(b.Ctx, chromedp.Navigate(url))
}
// Yes, we need that trick with creating a temp file and not directly sending html since
// chrome only allows us to access local files via other local files
func (b *BrowserType) OpenHTML(html string) error {
var err error
tempFile, err := os.CreateTemp("", "mudics-*.html")
if err != nil {
return fmt.Errorf("could not create tempfile: %w", err)
}
defer tempFile.Close()
_, err = tempFile.WriteString(html)
if err != nil {
return fmt.Errorf("could not write to tempfile: %w", err)
}
chromedp.Run(b.Ctx, chromedp.Navigate("file://"+tempFile.Name()))
return nil
}
func (b *BrowserType) OpenPDF(path string) {
b.OpenPage("file://" + path + "#toolbar=0&view=Fit")
}
+10 -3
View File
@@ -1,9 +1,10 @@
module plg-mudics/display
go 1.24.4
go 1.25
require (
github.com/a-h/templ v0.3.960
github.com/a-h/templ v0.3.977
github.com/chromedp/chromedp v0.14.2
github.com/gabriel-vasile/mimetype v1.4.11
github.com/labstack/echo/v4 v4.15.0
github.com/micmonay/keybd_event v1.1.2
@@ -14,9 +15,15 @@ require (
github.com/a-h/parse v0.0.0-20250122154542-74294addb73e // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/chromedp/cdproto v0.0.0-20250803210736-d308e07a266d // indirect
github.com/chromedp/sysutil v1.1.0 // indirect
github.com/cli/browser v1.3.0 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-json-experiment/json v0.0.0-20251027170946-4849db3c2f7e // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/gobwas/ws v1.4.0 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
@@ -27,7 +34,7 @@ require (
golang.org/x/mod v0.30.0 // indirect
golang.org/x/net v0.48.0 // indirect
golang.org/x/sync v0.19.0 // indirect
golang.org/x/sys v0.39.0 // indirect
golang.org/x/sys v0.40.0 // indirect
golang.org/x/text v0.32.0 // indirect
golang.org/x/time v0.14.0 // indirect
golang.org/x/tools v0.39.0 // indirect
+18
View File
@@ -2,10 +2,18 @@ github.com/a-h/parse v0.0.0-20250122154542-74294addb73e h1:HjVbSQHy+dnlS6C3XajZ6
github.com/a-h/parse v0.0.0-20250122154542-74294addb73e/go.mod h1:3mnrkvGpurZ4ZrTDbYU84xhwXW2TjTKShSwjRi2ihfQ=
github.com/a-h/templ v0.3.960 h1:trshEpGa8clF5cdI39iY4ZrZG8Z/QixyzEyUnA7feTM=
github.com/a-h/templ v0.3.960/go.mod h1:oCZcnKRf5jjsGpf2yELzQfodLphd2mwecwG4Crk5HBo=
github.com/a-h/templ v0.3.977 h1:kiKAPXTZE2Iaf8JbtM21r54A8bCNsncrfnokZZSrSDg=
github.com/a-h/templ v0.3.977/go.mod h1:oCZcnKRf5jjsGpf2yELzQfodLphd2mwecwG4Crk5HBo=
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/chromedp/cdproto v0.0.0-20250803210736-d308e07a266d h1:ZtA1sedVbEW7EW80Iz2GR3Ye6PwbJAJXjv7D74xG6HU=
github.com/chromedp/cdproto v0.0.0-20250803210736-d308e07a266d/go.mod h1:NItd7aLkcfOA/dcMXvl8p1u+lQqioRMq/SqDp71Pb/k=
github.com/chromedp/chromedp v0.14.2 h1:r3b/WtwM50RsBZHMUm9fsNhhzRStTHrKdr2zmwbZSzM=
github.com/chromedp/chromedp v0.14.2/go.mod h1:rHzAv60xDE7VNy/MYtTUrYreSc0ujt2O1/C3bzctYBo=
github.com/chromedp/sysutil v1.1.0 h1:PUFNv5EcprjqXZD9nJb9b/c9ibAbxiYo4exNWZyipwM=
github.com/chromedp/sysutil v1.1.0/go.mod h1:WiThHUdltqCNKGc4gaU50XgYjwjYIhKWoHGPTUfWTJ8=
github.com/cli/browser v1.3.0 h1:LejqCrpWr+1pRqmEPDGnTZOjsMe7sehifLynZJuqJpo=
github.com/cli/browser v1.3.0/go.mod h1:HH8s+fOAxjhQoBUAsKuPCbqUuxZDhQ2/aD+SzsEfBTk=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -16,6 +24,14 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gabriel-vasile/mimetype v1.4.11 h1:AQvxbp830wPhHTqc1u7nzoLT+ZFxGY7emj5DR5DYFik=
github.com/gabriel-vasile/mimetype v1.4.11/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
github.com/go-json-experiment/json v0.0.0-20251027170946-4849db3c2f7e h1:Lf/gRkoycfOBPa42vU2bbgPurFong6zXeFtPoxholzU=
github.com/go-json-experiment/json v0.0.0-20251027170946-4849db3c2f7e/go.mod h1:uNVvRXArCGbZ508SxYYTC5v1JWoz2voff5pm25jU1Ok=
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
github.com/gobwas/ws v1.4.0 h1:CTaoG1tojrh4ucGPcoJFiAQUAsEWekEWvLy7GsVNqGs=
github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakrc=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/labstack/echo/v4 v4.13.4 h1:oTZZW+T3s9gAu5L8vmzihV7/lkXGZuITzTQkTEhcXEA=
@@ -64,6 +80,8 @@ golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=
golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4=
golang.org/x/text v0.27.0/go.mod h1:1D28KMCvyooCX9hBiosv5Tz/+YLxj0j7XhWjpSUF7CU=
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
+7 -7
View File
@@ -4,9 +4,9 @@ import (
"log/slog"
"os"
"plg-mudics/display/browser"
"plg-mudics/display/pkg"
"plg-mudics/display/web"
"plg-mudics/shared"
)
//go:generate go tool templ generate
@@ -25,10 +25,10 @@ func main() {
// the order is important, the open browser command exitsts as soon as the winodw is closed
// and since its the last action in the main go func all other goroutines (e.g. the webserver) are killed
go web.StartWebServer(shared.Version, port)
err = shared.OpenBrowserWindow("http://localhost:"+port, true, "display")
if err != nil {
slog.Error("Failed to open browser window", "error", err)
os.Exit(1)
}
go web.StartWebServer(port)
browser.Browser.Init()
pkg.OpenStartScreen()
defer browser.Browser.Cancel()
<-browser.Browser.Ctx.Done()
}
-75
View File
@@ -1,75 +0,0 @@
package pkg
import (
"fmt"
"log/slog"
"os"
"os/exec"
"path/filepath"
"plg-mudics/shared"
"syscall"
"github.com/gabriel-vasile/mimetype"
)
var FileHandler fileHandler = fileHandler{}
type fileHandler struct {
runningProgram *exec.Cmd
}
func (fh *fileHandler) OpenFile(path string) error {
var err error
mType, err := mimetype.DetectFile(path)
if err != nil {
return fmt.Errorf("failed to detect mime type: %w", err)
}
tempDirPath, err := os.MkdirTemp("", "plg-mudics-program-profile-")
if err != nil {
return fmt.Errorf("failed to create temporary profile directory: %w", err)
}
err = fh.CloseRunningProgram()
if err != nil {
return err
}
switch mType.String() {
case "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.oasis.opendocument.presentation":
// yes, we need this weird workaround to delete lock files since libreoffice
// doesn't expose an option to ignore them or prevent their creation
// the --view argument for some reason doesn't work with --show
parent := filepath.Dir(path)
cmd := exec.Command("find", parent, "-name", ".~lock*", "-type", "f", "-delete")
result := shared.RunShellCommand(cmd)
if result.ExitCode != 0 {
slog.Warn("could not remove lock files", "path", parent, "stderr", result.Stderr, "exitCode", result.ExitCode)
}
fh.runningProgram = exec.Command("soffice", "--show", path, "--nologo", "--norestore", fmt.Sprintf("-env:UserInstallation=file://%s", tempDirPath))
case "application/pdf":
fh.runningProgram = exec.Command("xreader", path, "--presentation")
default:
return fmt.Errorf("unsupported file type: %s", mType.String())
}
fh.runningProgram.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
result := shared.RunShellCommandNonBlocking(fh.runningProgram)
if result.ExitCode != 0 {
return fmt.Errorf("could not open pdf: %s (%d)", result.Stderr, result.ExitCode)
}
return nil
}
func (fh *fileHandler) CloseRunningProgram() error {
if fh.runningProgram == nil {
return nil
}
err := syscall.Kill(-fh.runningProgram.Process.Pid, syscall.SIGTERM)
fh.runningProgram = nil
return err
}
+21 -32
View File
@@ -1,49 +1,21 @@
package pkg
import (
"bytes"
"context"
"errors"
"fmt"
"net"
"log/slog"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"plg-mudics/display/browser"
"plg-mudics/shared"
)
func GetDeviceIp() (string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", fmt.Errorf("failed to get network interfaces: %w", err)
}
for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if ok && !ipNet.IP.IsLoopback() && ipNet.IP.To4() != nil {
return ipNet.IP.String(), nil
}
}
return "", fmt.Errorf("no suitable IP address found")
}
func GetDeviceMac() (string, error) {
interfaces, err := net.Interfaces()
if err != nil {
return "", fmt.Errorf("failed to get network interfaces: %w", err)
}
for _, interf := range interfaces {
mac := interf.HardwareAddr.String()
if mac != "" {
return mac, nil
}
}
return "", fmt.Errorf("no suitable MAC address found")
}
func TakeScreenshot() (string, error) {
tempFilePath := filepath.Join(os.TempDir(), fmt.Sprintf("screenshot_%d.png", time.Now().Unix()))
@@ -106,3 +78,20 @@ func ResolveStorageFilePath(pathParam string) (string, bool, error) {
return fullPath, true, nil
}
func ShowHTML(html string) error {
ResetView()
var templateBuffer bytes.Buffer
htmlTemplate(html).Render(context.Background(), &templateBuffer)
err := browser.Browser.OpenHTML(templateBuffer.String())
return err
}
func ResetView() {
err := fileHandler.closeRunningProgram()
if err != nil {
slog.Error("Failed to close running program", "error", err)
}
}
+148
View File
@@ -0,0 +1,148 @@
package pkg
templ basicTemplate() {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>PLG MuDiCS Display</title>
<style>
:root {
--background-color: black;
--foreground-color: oklch(92.3% 0.003 48.717);
--font-size: 5rem;
}
body {
display: flex;
justify-content: center;
/* centers horizontally */
align-items: center;
/* centers vertically */
width: 100vw;
/* Viewport width */
height: 100vh;
/* Viewport height */
margin: 0;
padding: 0;
overflow: hidden;
background-color: var(--background-color);
color: var(--foreground-color);
font-family: ui-sans-serif, system-ui, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
cursor: none;
}
video,
img {
width: 100vw;
/* Viewport width */
height: 100vh;
/* Viewport height */
object-fit: contain;
}
p,
li {
font-size: var(--font-size);
line-height: 1.2;
text-wrap: balance;
max-width: 90vw;
word-break: break-word;
margin: 0 0 calc(var(--font-size) * 0.4) 0;
}
ul,
ol {
padding-left: calc(var(--font-size)*1.5);
}
</style>
</head>
<bod>
{ children... }
</bod>
</html>
}
templ videoTemplate(path string) {
@basicTemplate() {
<video autoplay>
<source src={ "file://" + path } type="video/mp4"/>
</video>
}
}
templ htmlTemplate(html string) {
@basicTemplate() {
@templ.Raw(html)
}
}
templ imageTemplate(path string) {
@basicTemplate() {
<img src={ "file://" + path }/>
}
}
templ deviceInfoTemplate(ip string, mac string, showQR bool) {
@basicTemplate() {
<div style="width: 100vw; height: 100vh; display: flex; flex-direction: row; justify-content: space-between;">
<div
style="display: flex; flex-direction: column; gap: 1rem; justify-content: end; padding: 2rem; font-size: 4rem;"
>
{ ip }
<span style="text-transform: uppercase;">{ mac }</span>
</div>
if showQR {
<div style="display: flex; justify-content: end; align-items: end; padding: 2rem;">
<div style="padding: 1rem; background-color: var(--foreground-color); border-radius: 1rem;">
<img
style="height: 30vh; width: auto; image-rendering: pixelated; image-rendering: crisp-edges;"
src={ "/qr?data=http://" + ip + ":8080" }
alt="QR-Code"
/>
</div>
</div>
}
</div>
<style>
:root {
--splash-bg: transparent !important;
--splash-fade-out-state: paused !important;
--background-color: oklch(21.6% 0.006 56.043);
}
</style>
}
}
templ startScreenTemplate(splashScreenHtml string, ip string, mac string, qrPath string) {
@basicTemplate() {
<div style="width: 100vw; height: 100vh; display: flex; flex-direction: row; justify-content: space-between;">
<div
style="display: flex; flex-direction: column; gap: 1rem; justify-content: end; padding: 2rem; font-size: 4rem;"
>
{ ip }
<span style="text-transform: uppercase;">{ mac }</span>
</div>
if qrPath != "" {
<div style="display: flex; justify-content: end; align-items: end; padding: 2rem;">
<div style="padding: 1rem; background-color: var(--foreground-color); border-radius: 1rem;">
<img
style="height: 30vh; width: auto; image-rendering: pixelated; image-rendering: crisp-edges;"
src={ "file://" + qrPath }
alt="QR-Code"
/>
</div>
</div>
}
</div>
@templ.Raw(splashScreenHtml)
<style>
:root {
--splash-bg: transparent !important;
--splash-fade-out-state: paused !important;
--background-color: oklch(21.6% 0.006 56.043);
}
</style>
}
}
+108
View File
@@ -0,0 +1,108 @@
package pkg
import (
"bytes"
"context"
"fmt"
"log/slog"
"os"
"os/exec"
"path/filepath"
"plg-mudics/shared"
"syscall"
"github.com/gabriel-vasile/mimetype"
"plg-mudics/display/browser"
)
var fileHandler fileHandlerType = fileHandlerType{}
type fileHandlerType struct {
runningProgram *exec.Cmd
}
func OpenFile(path string) error {
ResetView()
mType, err := mimetype.DetectFile(path)
if err != nil {
slog.Error("Failed to detect mime type", "file", path, "error", err)
}
switch mType.String() {
case "video/mp4":
var templateBuffer bytes.Buffer
videoTemplate(path).Render(context.Background(), &templateBuffer)
browser.Browser.OpenHTML(templateBuffer.String())
case "image/jpeg", "image/png", "image/gif":
var templateBuffer bytes.Buffer
imageTemplate(path).Render(context.Background(), &templateBuffer)
browser.Browser.OpenHTML(templateBuffer.String())
case "application/pdf":
browser.Browser.OpenPDF(path)
case "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.oasis.opendocument.presentation":
err = fileHandler.openFileWithApp(path)
default:
return fmt.Errorf("unsupported file type: %s", mType.String())
}
return nil
}
func (fh *fileHandlerType) openFileWithApp(path string) error {
var err error
mType, err := mimetype.DetectFile(path)
if err != nil {
return fmt.Errorf("failed to detect mime type: %w", err)
}
switch mType.String() {
case "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.oasis.opendocument.presentation":
err = fh.openLibreoffice(path)
if err != nil {
return err
}
default:
return fmt.Errorf("unsupported file type: %s", mType.String())
}
fh.runningProgram.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
result := shared.RunShellCommandNonBlocking(fh.runningProgram)
if result.ExitCode != 0 {
return fmt.Errorf("could not open pdf: %s (%d)", result.Stderr, result.ExitCode)
}
return nil
}
func (fh *fileHandlerType) openLibreoffice(path string) error {
// yes, we need this weird workaround to delete lock files since libreoffice
// doesn't expose an option to ignore them or prevent their creation
// the --view argument for some reason doesn't work with --show
parent := filepath.Dir(path)
cmd := exec.Command("find", parent, "-name", ".~lock*", "-type", "f", "-delete")
result := shared.RunShellCommand(cmd)
if result.ExitCode != 0 {
slog.Warn("could not remove lock files", "path", parent, "stderr", result.Stderr, "exitCode", result.ExitCode)
}
tempDirPath, err := os.MkdirTemp("", "plg-mudics-program-profile-")
if err != nil {
return fmt.Errorf("failed to create temporary profile directory: %w", err)
}
fh.runningProgram = exec.Command("soffice", "--show", path, "--nologo", "--norestore", fmt.Sprintf("-env:UserInstallation=file://%s", tempDirPath))
return nil
}
func (fh *fileHandlerType) closeRunningProgram() error {
if fh.runningProgram == nil {
return nil
}
err := syscall.Kill(-fh.runningProgram.Process.Pid, syscall.SIGTERM)
fh.runningProgram = nil
return err
}
+117
View File
@@ -0,0 +1,117 @@
package pkg
import (
"bytes"
"context"
"fmt"
"image/color"
"log/slog"
"net"
"os"
"plg-mudics/shared"
"strings"
"plg-mudics/display/browser"
"github.com/skip2/go-qrcode"
)
func OpenStartScreen() {
var err error
raw := shared.RawSplashScreenTemplate
html := strings.ReplaceAll(raw, "%%APP-VERSION%%", shared.Version)
ip, err := getDeviceIp()
if err != nil {
slog.Error("Failed to get device IP", "error", err)
}
mac, err := getDeviceMac()
if err != nil {
slog.Error("Failed to get device MAC address", "error", err)
}
port := 8080
showQrCode := !isPortFree(port)
qrCodePath := ""
if showQrCode {
qrCodePath, err = generateQRCode(fmt.Sprintf("http://%s:%d", ip, port))
if err != nil {
slog.Error("could not generate qr code", "error", err)
}
}
var templateBuffer bytes.Buffer
startScreenTemplate(html, ip, mac, qrCodePath).Render(context.Background(), &templateBuffer)
browser.Browser.OpenHTML(templateBuffer.String())
}
func getDeviceIp() (string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", fmt.Errorf("failed to get network interfaces: %w", err)
}
for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if ok && !ipNet.IP.IsLoopback() && ipNet.IP.To4() != nil {
return ipNet.IP.String(), nil
}
}
return "", fmt.Errorf("no suitable IP address found")
}
func isPortFree(port int) bool {
addr := fmt.Sprintf("127.0.0.1:%d", port)
l, err := net.Listen("tcp", addr)
if err != nil {
return false
}
_ = l.Close()
return true
}
func getDeviceMac() (string, error) {
interfaces, err := net.Interfaces()
if err != nil {
return "", fmt.Errorf("failed to get network interfaces: %w", err)
}
for _, interf := range interfaces {
mac := interf.HardwareAddr.String()
if mac != "" {
return mac, nil
}
}
return "", fmt.Errorf("no suitable MAC address found")
}
func generateQRCode(data string) (string, error) {
qr, err := qrcode.New(data, qrcode.Medium)
if err != nil {
return "", fmt.Errorf("could not generate qr code: %w", err)
}
qr.DisableBorder = true
qr.ForegroundColor = color.RGBA{R: 0x1c, G: 0x19, B: 0x17, A: 0xff}
qr.BackgroundColor = color.RGBA{R: 0xe7, G: 0xe5, B: 0xe4, A: 0xff}
png, err := qr.PNG(-1)
if err != nil {
return "", fmt.Errorf("could not render qr code: %w", err)
}
file, err := os.CreateTemp("", "mudics-qr-code-*.png")
if err != nil {
return "", fmt.Errorf("could not save qr code: %w", err)
}
defer file.Close()
_, err = file.Write(png)
if err != nil {
return "", fmt.Errorf("could not write qr code to file: %w", err)
}
return file.Name(), nil
}
+22
View File
@@ -0,0 +1,22 @@
meta {
name: openWebsite
type: http
seq: 10
}
patch {
url: 127.0.0.1:1323/api/openWebsite
body: json
auth: inherit
}
body:json {
{
"url": "https://example.com"
}
}
settings {
encodeUrl: true
timeout: 0
}
-12
View File
@@ -1,12 +0,0 @@
package web
import (
"embed"
"io/fs"
)
//go:embed all:static
var staticDir embed.FS
// BuildDirFS contains the embedded dist directory files (without the "build" prefix)
var StaticDirFS, _ = fs.Sub(staticDir, "static")
+18 -160
View File
@@ -1,53 +1,27 @@
package web
import (
"bytes"
"context"
"errors"
"fmt"
"image/color"
"io"
"log/slog"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
shared "plg-mudics/shared"
"strings"
"github.com/gabriel-vasile/mimetype"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/skip2/go-qrcode"
"plg-mudics/display/browser"
"plg-mudics/display/pkg"
)
var version string
var sseConnection chan string
func StartWebServer(v string, port string) {
version = v
func StartWebServer(port string) {
e := echo.New()
e.GET("/", indexRoute)
e.GET("/sse", sseRoute)
e.GET("/splash", func(ctx echo.Context) error {
html := shared.SplashScreenTemplate
html = strings.ReplaceAll(html, "%%APP-VERSION%%", version)
return ctx.HTML(http.StatusOK, html)
})
e.GET("/qr", qrRoute)
staticGroup := e.Group("/static")
staticGroup.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Filesystem: http.FS(StaticDirFS),
HTML5: true,
}))
apiGroup := e.Group("/api")
apiGroup.Use(middleware.CORS())
apiGroup.GET("/ping", pingRoute)
@@ -55,6 +29,7 @@ func StartWebServer(v string, port string) {
apiGroup.PATCH("/keyboardInput", keyboardInputRoute)
apiGroup.PATCH("/showHTML", showHTMLRoute)
apiGroup.PATCH("/takeScreenshot", takeScreenshotRoute)
apiGroup.PATCH("/openWebsite", openWebsiteRoute)
fileGroup := apiGroup.Group("/file")
fileGroup.Use(extractFilePathMiddleware)
@@ -69,85 +44,6 @@ func StartWebServer(v string, port string) {
}
}
func indexRoute(ctx echo.Context) error {
return indexTemplate().Render(ctx.Request().Context(), ctx.Response().Writer)
}
func sseRoute(ctx echo.Context) error {
slog.Info("SSE client connected")
w := ctx.Response()
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.WriteHeader(http.StatusOK)
flusher, _ := w.Writer.(http.Flusher)
sseConnection = make(chan string)
// init display
ip, err := pkg.GetDeviceIp()
if err != nil {
slog.Error("Failed to get device IP address", "error", err)
}
mac, err := pkg.GetDeviceMac()
if err != nil {
slog.Error("Failed to get device MAC address", "error", err)
}
showQR := !isPortFree(8080)
var status bytes.Buffer
deviceInfoTemplate(ip, mac, showQR).Render(context.Background(), &status)
connectedEvent := Event{
Data: status.Bytes(),
}
connectedEvent.MarshalTo(w)
flusher.Flush()
for {
select {
case <-ctx.Request().Context().Done():
slog.Info("SSE client disconnected")
sseConnection = nil
return nil
case event := <-sseConnection:
rawEvent := Event{
Event: []byte(""),
Data: []byte(event),
}
if err := rawEvent.MarshalTo(w); err != nil {
slog.Warn("Error writing to client", "error", err)
return err
}
flusher.Flush()
}
}
}
func qrRoute(c echo.Context) error {
data := c.QueryParam("data")
if data == "" {
return c.String(http.StatusBadRequest, "missing data")
}
qr, err := qrcode.New(data, qrcode.Medium)
if err != nil {
return c.String(http.StatusInternalServerError, "could not generate qr")
}
qr.DisableBorder = true
qr.ForegroundColor = color.RGBA{R: 0x1c, G: 0x19, B: 0x17, A: 0xff}
qr.BackgroundColor = color.RGBA{R: 0xe7, G: 0xe5, B: 0xe4, A: 0xff}
png, err := qr.PNG(-1)
if err != nil {
return c.String(http.StatusInternalServerError, "could not encode png")
}
return c.Blob(http.StatusOK, "image/png", png)
}
func extractFilePathMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
raw := ctx.Param("path")
@@ -308,38 +204,7 @@ func openFileRoute(ctx echo.Context) error {
return ctx.JSON(http.StatusNotFound, shared.ErrorResponse{Description: "File not found"})
}
if sseConnection == nil {
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Cant connect to display browser client"})
}
err = resetView()
if err != nil {
slog.Error("Failed to reset view", "error", err)
}
mType, err := mimetype.DetectFile(fullPath)
if err != nil {
slog.Error("Failed to detect mime type", "file", pathParam, "error", err)
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Failed to detect file type"})
}
switch mType.String() {
case "video/mp4":
var templateBuffer bytes.Buffer
videoTemplate(pathParam).Render(context.Background(), &templateBuffer)
sseConnection <- templateBuffer.String()
case "image/jpeg", "image/png", "image/gif":
var templateBuffer bytes.Buffer
imageTemplate(pathParam).Render(context.Background(), &templateBuffer)
sseConnection <- templateBuffer.String()
case "application/pdf", "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.oasis.opendocument.presentation":
err = pkg.FileHandler.OpenFile(fullPath)
default:
slog.Info("Unsupported file type", "type", mType)
return ctx.JSON(http.StatusUnsupportedMediaType, shared.ErrorResponse{Description: "Unsupported file type: " + mType.String()})
}
err = pkg.OpenFile(fullPath)
if err != nil {
slog.Error("Failed to open file", "file", pathParam, "error", err)
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Failed to open file"})
@@ -358,13 +223,12 @@ func showHTMLRoute(ctx echo.Context) error {
return ctx.JSON(http.StatusBadRequest, shared.ErrorResponse{Description: shared.BadRequestDescription})
}
err := resetView()
err := pkg.ShowHTML(request.HTML)
if err != nil {
slog.Error("Failed to reset view", "error", err)
slog.Error("Failed to open html", "error", err)
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Failed to open html"})
}
sseConnection <- request.HTML
slog.Info("HTML content sent to client")
return ctx.JSON(http.StatusOK, struct{}{})
}
@@ -372,7 +236,7 @@ func showHTMLRoute(ctx echo.Context) error {
func pingRoute(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, struct {
Version string `json:"version"`
}{Version: version})
}{Version: shared.Version})
}
func takeScreenshotRoute(ctx echo.Context) error {
@@ -415,24 +279,18 @@ func previewRoute(ctx echo.Context) error {
return ctx.File(outputFilePath)
}
// Reset previous file views so they dont collide with the new one
func resetView() error {
err := pkg.FileHandler.CloseRunningProgram()
if err != nil {
return fmt.Errorf("failed to close running program: %w", err)
func openWebsiteRoute(ctx echo.Context) error {
var request struct {
URL string `json:"url"`
}
if err := ctx.Bind(&request); err != nil {
slog.Error("Failed to parse website input", "error", err)
return ctx.JSON(http.StatusBadRequest, shared.ErrorResponse{Description: shared.BadRequestDescription})
}
sseConnection <- ""
slog.Info("Opening url")
return nil
}
browser.Browser.OpenPage(request.URL)
func isPortFree(port int) bool {
addr := fmt.Sprintf("127.0.0.1:%d", port)
l, err := net.Listen("tcp", addr)
if err != nil {
return false
}
_ = l.Close()
return true
return ctx.JSON(http.StatusOK, struct{}{})
}
-123
View File
@@ -1,123 +0,0 @@
package web
templ indexTemplate() {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>PLG MuDiCS Display</title>
<script src="/static/htmx.min.js"></script>
<script src="/static/htmx-ext-sse.min.js"></script>
<style>
:root {
--background-color: black;
--foreground-color: oklch(92.3% 0.003 48.717);
--font-size: 5rem;
}
body {
display: flex;
justify-content: center;
/* centers horizontally */
align-items: center;
/* centers vertically */
width: 100vw;
/* Viewport width */
height: 100vh;
/* Viewport height */
margin: 0;
padding: 0;
overflow: hidden;
background-color: var(--background-color);
color: var(--foreground-color);
font-family: ui-sans-serif, system-ui, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
cursor: none;
}
video,
img,
iframe {
width: 100vw;
/* Viewport width */
height: 100vh;
/* Viewport height */
object-fit: contain;
}
p,
li {
font-size: var(--font-size);
line-height: 1.2;
text-wrap: balance;
max-width: 90vw;
word-break: break-word;
margin: 0 0 calc(var(--font-size) * 0.4) 0;
}
ul,
ol {
padding-left: calc(var(--font-size)*1.5);
}
</style>
<script>
document.addEventListener('keydown', function (event) {
if (event.code === 'Space') {
event.preventDefault();
var video = document.querySelector('video');
if (video) {
if (video.paused) {
video.play();
} else {
video.pause();
}
}
}
});
</script>
</head>
<bod>
<div hx-get="/splash" hx-trigger="load"></div>
<main hx-ext="sse" sse-connect="/sse" sse-swap="message"></main>
</bod>
</html>
}
templ videoTemplate(path string) {
<video autoplay>
<source src={ "/api/file/" + path } type="video/mp4"/>
</video>
}
templ imageTemplate(path string) {
<img src={ "/api/file/" + path }/>
}
templ deviceInfoTemplate(ip string, mac string, showQR bool) {
<div style="width: 100vw; height: 100vh; display: flex; flex-direction: row; justify-content: space-between;">
<div
style="display: flex; flex-direction: column; gap: 1rem; justify-content: end; padding: 2rem; font-size: 4rem;"
>
{ ip }
<span style="text-transform: uppercase;">{ mac }</span>
</div>
if showQR {
<div style="display: flex; justify-content: end; align-items: end; padding: 2rem;">
<div style="padding: 1rem; background-color: var(--foreground-color); border-radius: 1rem;">
<img
style="height: 30vh; width: auto; image-rendering: pixelated; image-rendering: crisp-edges;"
src={ "/qr?data=http://" + ip + ":8080" }
alt="QR-Code"
/>
</div>
</div>
}
</div>
<style>
:root {
--splash-bg: transparent !important;
--splash-fade-out-state: paused !important;
--background-color: oklch(21.6% 0.006 56.043);
}
</style>
}
-75
View File
@@ -1,75 +0,0 @@
package web
import (
"bytes"
"fmt"
"io"
)
// Event represents Server-Sent Event.
// SSE explanation: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event_stream_format
type Event struct {
// ID is used to set the EventSource object's last event ID value.
ID []byte
// Data field is for the message. When the EventSource receives multiple consecutive lines
// that begin with data:, it concatenates them, inserting a newline character between each one.
// Trailing newlines are removed.
Data []byte
// Event is a string identifying the type of event described. If this is specified, an event
// will be dispatched on the browser to the listener for the specified event name; the website
// source code should use addEventListener() to listen for named events. The onmessage handler
// is called if no event name is specified for a message.
Event []byte
// Retry is the reconnection time. If the connection to the server is lost, the browser will
// wait for the specified time before attempting to reconnect. This must be an integer, specifying
// the reconnection time in milliseconds. If a non-integer value is specified, the field is ignored.
Retry []byte
// Comment line can be used to prevent connections from timing out; a server can send a comment
// periodically to keep the connection alive.
Comment []byte
}
// MarshalTo marshals Event to given Writer
func (ev *Event) MarshalTo(w io.Writer) error {
// Marshalling part is taken from: https://github.com/r3labs/sse/blob/c6d5381ee3ca63828b321c16baa008fd6c0b4564/http.go#L16
if len(ev.Data) == 0 && len(ev.Comment) == 0 {
return nil
}
if len(ev.Data) > 0 {
if _, err := fmt.Fprintf(w, "id: %s\n", ev.ID); err != nil {
return err
}
sd := bytes.Split(ev.Data, []byte("\n"))
for i := range sd {
if _, err := fmt.Fprintf(w, "data: %s\n", sd[i]); err != nil {
return err
}
}
if len(ev.Event) > 0 {
if _, err := fmt.Fprintf(w, "event: %s\n", ev.Event); err != nil {
return err
}
}
if len(ev.Retry) > 0 {
if _, err := fmt.Fprintf(w, "retry: %s\n", ev.Retry); err != nil {
return err
}
}
}
if len(ev.Comment) > 0 {
if _, err := fmt.Fprintf(w, ": %s\n", ev.Comment); err != nil {
return err
}
}
if _, err := fmt.Fprint(w, "\n"); err != nil {
return err
}
return nil
}
-1
View File
@@ -1 +0,0 @@
(function(){var g;htmx.defineExtension("sse",{init:function(e){g=e;if(htmx.createEventSource==undefined){htmx.createEventSource=t}},getSelectors:function(){return["[sse-connect]","[data-sse-connect]","[sse-swap]","[data-sse-swap]"]},onEvent:function(e,t){var r=t.target||t.detail.elt;switch(e){case"htmx:beforeCleanupElement":var n=g.getInternalData(r);var s=n.sseEventSource;if(s){g.triggerEvent(r,"htmx:sseClose",{source:s,type:"nodeReplaced"});n.sseEventSource.close()}return;case"htmx:afterProcessNode":i(r)}}});function t(e){return new EventSource(e,{withCredentials:true})}function a(n){if(g.getAttributeValue(n,"sse-swap")){var s=g.getClosestMatch(n,v);if(s==null){return null}var e=g.getInternalData(s);var a=e.sseEventSource;var t=g.getAttributeValue(n,"sse-swap");var r=t.split(",");for(var i=0;i<r.length;i++){const u=r[i].trim();const c=function(e){if(l(s)){return}if(!g.bodyContains(n)){a.removeEventListener(u,c);return}if(!g.triggerEvent(n,"htmx:sseBeforeMessage",e)){return}f(n,e.data);g.triggerEvent(n,"htmx:sseMessage",e)};g.getInternalData(n).sseEventListener=c;a.addEventListener(u,c)}}if(g.getAttributeValue(n,"hx-trigger")){var s=g.getClosestMatch(n,v);if(s==null){return null}var e=g.getInternalData(s);var a=e.sseEventSource;var o=g.getTriggerSpecs(n);o.forEach(function(t){if(t.trigger.slice(0,4)!=="sse:"){return}var r=function(e){if(l(s)){return}if(!g.bodyContains(n)){a.removeEventListener(t.trigger.slice(4),r)}htmx.trigger(n,t.trigger,e);htmx.trigger(n,"htmx:sseMessage",e)};g.getInternalData(n).sseEventListener=r;a.addEventListener(t.trigger.slice(4),r)})}}function i(e,t){if(e==null){return null}if(g.getAttributeValue(e,"sse-connect")){var r=g.getAttributeValue(e,"sse-connect");if(r==null){return}n(e,r,t)}a(e)}function n(r,e,n){var s=htmx.createEventSource(e);s.onerror=function(e){g.triggerErrorEvent(r,"htmx:sseError",{error:e,source:s});if(l(r)){return}if(s.readyState===EventSource.CLOSED){n=n||0;n=Math.max(Math.min(n*2,128),1);var t=n*500;window.setTimeout(function(){i(r,n)},t)}};s.onopen=function(e){g.triggerEvent(r,"htmx:sseOpen",{source:s});if(n&&n>0){const t=r.querySelectorAll("[sse-swap], [data-sse-swap], [hx-trigger], [data-hx-trigger]");for(let e=0;e<t.length;e++){a(t[e])}n=0}};g.getInternalData(r).sseEventSource=s;var t=g.getAttributeValue(r,"sse-close");if(t){s.addEventListener(t,function(){g.triggerEvent(r,"htmx:sseClose",{source:s,type:"message"});s.close()})}}function l(e){if(!g.bodyContains(e)){var t=g.getInternalData(e).sseEventSource;if(t!=undefined){g.triggerEvent(e,"htmx:sseClose",{source:t,type:"nodeMissing"});t.close();return true}}return false}function f(t,r){g.withExtensions(t,function(e){r=e.transformResponse(r,null,t)});var e=g.getSwapSpecification(t);var n=g.getTarget(t);g.swap(n,r,e,{contextElement:t})}function v(e){return g.getInternalData(e).sseEventSource!=null}})();
File diff suppressed because one or more lines are too long
+2
View File
@@ -1,4 +1,6 @@
github.com/a-h/htmlformat v0.0.0-20250209131833-673be874c677/go.mod h1:FMIm5afKmEfarNbIXOaPHFY8X7fo+fRQB6I9MPG2nB0=
github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs=
github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0=
github.com/rs/cors v1.11.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
-1
View File
@@ -92,7 +92,6 @@
nushell
unzip
iputils
xreader
tree
jq
+1 -1
View File
@@ -6,7 +6,7 @@ import (
)
//go:embed splash_screen.html
var SplashScreenTemplate string
var RawSplashScreenTemplate string
//go:embed version.txt
var versionNotTrimmed string