// Command 100gex is a Linux client for Superlogical's Rex. // // It shows a server's sessions in a sidebar, the shown session's windows as // tabs, and the active window's blocks in their layout. package main import ( "context" "errors" "flag" "log" "os" "os/signal" "runtime/pprof" "sync/atomic" "syscall" "time" "gioui.org/app" "gioui.org/io/system" "gioui.org/op" "gioui.org/unit" "100gex/internal/hosts" "100gex/internal/primary" "100gex/internal/rex" "100gex/internal/theme" "100gex/internal/ui" "100gex/internal/workspace" ) func main() { server := flag.String("server", "", "Rex server to connect to: a host's label, as rex hosts lists them, or an address, such as ssh://alice@studio, studio.tail1234.ts.net, https://studio.tail1234.ts.net, or host:7297 (default: $REX_SERVER, else the local server)") session := flag.String("session", "", "session to show first, by ID, label, or the end of its ID as rex ls shows it (default: the one the server picks)") cpuprofile := flag.String("cpuprofile", "", "write a CPU profile to this file, for go tool pprof, until the window closes") slowFrames := flag.Bool("slowframes", false, "log every frame whose layout and hand-off to the GPU take longer than 240 frames a second allows") showFPS := flag.Bool("fps", false, "show the frame rate, and what the latest frames took to draw, in the title bar") flag.Parse() stopProfile := func() {} if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { log.Fatal(err) } if err := pprof.StartCPUProfile(f); err != nil { log.Fatal(err) } stopProfile = func() { pprof.StopCPUProfile() f.Close() } } go func() { w := new(app.Window) w.Option( app.Title("100gex"), app.Size(unit.Dp(1200), unit.Dp(760)), // The window draws its own title bar, with the tabs in it. app.Decorated(false), ) err := run(w, *server, *session, options{slowFrames: *slowFrames, showFPS: *showFPS}) stopProfile() if err != nil { log.Fatal(err) } os.Exit(0) }() app.Main() } // terminalTheme is the colours every terminal is asked to use: Barbican. var terminalTheme = &rex.TerminalTheme{ Foreground: theme.TerminalForeground, Background: theme.TerminalBackground, Cursor: theme.TerminalCursor, Palette: theme.TerminalPalette, Scheme: "light", } // run connects to the server, then drives the window's event loop until it // is closed. // options are the command line's switches for how the window runs. type options struct { slowFrames bool // log frames over ui.FrameBudget showFPS bool // show frame times } func run(w *app.Window, server, sessionQuery string, opts options) error { ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() conn, ep, label, err := hosts.Dial(ctx, server) if err != nil { return err } ws, err := workspace.Start(ctx, conn, ep, sessionQuery, terminalTheme, w.Invalidate) if err != nil { conn.Close() return err } // From here the UI owns the server: it follows its events, reconnects // when the connection drops, switches to others, and closes it. u := ui.New(ws, conn, theme.NewShaper(), w.Invalidate) defer u.Close() u.TerminalTheme = terminalTheme u.ShowFPS = opts.showFPS u.SetTarget(server, label) u.KeepHosts() // Ctrl+C or SIGTERM closes the window, so that the server sees this // client leave instead of losing its connection. Window.Perform isn't // safe to call from another goroutine, so the signal only asks the // event loop to close the window. If the window doesn't close, leave // anyway: closing the connection ends this client and its streams. var quit atomic.Bool go func() { sig := make(chan os.Signal, 1) signal.Notify(sig, os.Interrupt, syscall.SIGTERM) <-sig quit.Store(true) w.Invalidate() time.Sleep(2 * time.Second) log.Print("the window didn't close; leaving without it") if c := u.Conn(); c != nil { c.Close() } os.Exit(1) }() // Gio offers no primary selection, so it gets a Wayland connection of // its own. Without one, selecting doesn't copy and middle click doesn't // paste. if sel, err := primary.Open(); err != nil { if !errors.Is(err, primary.ErrUnsupported) { log.Printf("primary selection: %v", err) } } else { defer sel.Close() u.Primary = sel } var ops op.Ops title := "" for { switch e := w.Event().(type) { case app.DestroyEvent: return e.Err case app.FrameEvent: if quit.Load() { w.Perform(system.ActionClose) } // Streams are parsed into the terminals in the background; // a frame holds them only while Layout draws them. start := time.Now() gtx := app.NewContext(&ops, e) _, actions, err := u.Layout(gtx) if err != nil { return err } laidOut := time.Now() e.Frame(gtx.Ops) d := time.Since(start) u.FrameDrawn(start, d) if opts.slowFrames && d > ui.FrameBudget { log.Printf("slow frame: %v (layout %v, handing to the GPU %v; the GPU's own drawing isn't counted), %dx%d", d.Round(10*time.Microsecond), laidOut.Sub(start).Round(10*time.Microsecond), time.Since(laidOut).Round(10*time.Microsecond), e.Size.X, e.Size.Y) } if actions != 0 { w.Perform(actions) } if t := u.Title(); t != title { title = t w.Option(app.Title(t)) } } } }