commit e3e9ddf8c4986444d24c29ce44d25eeae23bec89
parent 45a2265ec39628cd0d7c81232533c6f1a1539da8
Author: Sivaldo Davi <sivaldo@sivaldodavi>
Date: Sun, 11 Oct 2026 19:05:03 -0300
feat(projecao): abre sent no monitor HDMI-A-1
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Diffstat:
3 files changed, 153 insertions(+), 3 deletions(-)
diff --git a/README.md b/README.md
@@ -52,9 +52,9 @@ Sobrescritas específicas: `CULTO_BIBLE_FILE`, `CULTO_MUSIC_FILE`, `CULTO_PIX_FI
- No editor, use `:wq` para salvar e sair; `:q!` para descartar.
- **Ctrl+C** encerra a interface.
-Na visualização de uma música, cada parte é identificada com o número do slide e o número da estrofe. Se houver uma capa automática (hinos e salmos), ela aparece como slide 01 e a primeira estrofe como slide 02. Esses rótulos são apenas informativos e não aparecem na projeção.
+Na visualização de uma música, cada parte é identificada com o número do slide e o número da estrofe. Se houver uma capa automática (hinos e salmos), ela aparece como slide 01 e a primeira estrofe como slide 02. Esses rótulos são apenas informativos e não aparecem na projeção. Na tela de pré-visualização, `←` e `→` passam o sent para o slide anterior ou seguinte; `g` continua disponível para ir diretamente a um número.
-A TUI não encerra processos `sent`. Se não puder atualizar a apresentação existente, exibe um erro em vez de matar processos ou abrir outra instância por engano. A gravação de letras é atômica e arquivos JSON inválidos não são substituídos.
+A TUI não encerra processos `sent`. Ao abrir uma nova apresentação em uma sessão Hyprland, o app inicia o `sent` diretamente no monitor `HDMI-A-1`; se esse monitor não estiver conectado, mostra um erro em vez de abrir a apresentação em outra tela. Se não puder atualizar uma apresentação existente, também mostra um erro em vez de matar processos ou abrir outra instância por engano. A gravação de letras é atômica e arquivos JSON inválidos não são substituídos.
## Testes
diff --git a/internal/culto/presenter.go b/internal/culto/presenter.go
@@ -1,6 +1,7 @@
package culto
import (
+ "encoding/json"
"fmt"
"os"
"os/exec"
@@ -172,7 +173,55 @@ func refreshOrStart(path string, startIfMissing bool) error {
if err != nil {
return fmt.Errorf("sent não está aberto e não foi encontrado no PATH")
}
- cmd := exec.Command(sent, path)
+ return startSent(sent, path)
+}
+
+func startSent(sent, slideFile string) error {
+ if os.Getenv("HYPRLAND_INSTANCE_SIGNATURE") == "" {
+ return startDetached(sent, slideFile)
+ }
+
+ hyprctl, err := exec.LookPath("hyprctl")
+ if err != nil {
+ return fmt.Errorf("hyprctl não foi encontrado; não é possível iniciar sent no monitor HDMI-A-1")
+ }
+ output, err := exec.Command(hyprctl, "monitors", "-j").Output()
+ if err != nil {
+ return fmt.Errorf("não foi possível consultar os monitores do Hyprland: %w", err)
+ }
+ var monitors []struct {
+ Name string `json:"name"`
+ }
+ if err := json.Unmarshal(output, &monitors); err != nil {
+ return fmt.Errorf("resposta inválida ao consultar monitores do Hyprland: %w", err)
+ }
+ found := false
+ for _, monitor := range monitors {
+ if monitor.Name == "HDMI-A-1" {
+ found = true
+ break
+ }
+ }
+ if !found {
+ return fmt.Errorf("o monitor HDMI-A-1 não está conectado; sent não foi iniciado")
+ }
+
+ command := shellQuote(sent) + " " + shellQuote(slideFile)
+ dispatch := fmt.Sprintf("hl.dsp.exec_cmd(%s, { monitor = %s })",
+ strconv.Quote(command), strconv.Quote("HDMI-A-1"))
+ output, err = exec.Command(hyprctl, "dispatch", dispatch).CombinedOutput()
+ if err != nil {
+ return fmt.Errorf("não foi possível iniciar sent no monitor HDMI-A-1: %w: %s",
+ err, strings.TrimSpace(string(output)))
+ }
+ if response := strings.TrimSpace(string(output)); response != "ok" {
+ return fmt.Errorf("Hyprland não confirmou a abertura do sent no monitor HDMI-A-1: %s", response)
+ }
+ return nil
+}
+
+func startDetached(sent, slideFile string) error {
+ cmd := exec.Command(sent, slideFile)
if err := cmd.Start(); err != nil {
return fmt.Errorf("iniciando sent: %w", err)
}
@@ -182,6 +231,10 @@ func refreshOrStart(path string, startIfMissing bool) error {
return nil
}
+func shellQuote(value string) string {
+ return "'" + strings.ReplaceAll(value, "'", "'\"'\"'") + "'"
+}
+
func OpenPix(path string) error {
info, err := os.Stat(path)
if err != nil {
diff --git a/internal/culto/presenter_test.go b/internal/culto/presenter_test.go
@@ -60,3 +60,100 @@ func TestJumpToSlideRejectsZero(t *testing.T) {
t.Fatal("slide zero deveria ser rejeitado")
}
}
+
+func TestMoveSlideSendsLeftOrRightToSentWindow(t *testing.T) {
+ for _, test := range []struct {
+ name string
+ direction int
+ want string
+ }{
+ {name: "anterior", direction: -1, want: "Left"},
+ {name: "seguinte", direction: 1, want: "Right"},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ dir := t.TempDir()
+ argsFile := filepath.Join(dir, "args")
+ xdotool := filepath.Join(dir, "xdotool")
+ script := "#!/bin/sh\n" +
+ "if [ \"$1\" = search ]; then printf '4242\\n'; exit 0; fi\n" +
+ "printf '%s\\n' \"$@\" > \"$CULTO_TEST_XDOTOOL_ARGS\"\n"
+ if err := os.WriteFile(xdotool, []byte(script), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ t.Setenv("PATH", dir)
+ t.Setenv("CULTO_TEST_XDOTOOL_ARGS", argsFile)
+
+ if err := MoveSlide(test.direction); err != nil {
+ t.Fatal(err)
+ }
+ got, err := os.ReadFile(argsFile)
+ if err != nil {
+ t.Fatal(err)
+ }
+ want := "key\n--window\n4242\n--clearmodifiers\n" + test.want + "\n"
+ if string(got) != want {
+ t.Fatalf("argumentos enviados ao xdotool:\n%s\nesperado:\n%s", got, want)
+ }
+ })
+ }
+}
+
+func TestMoveSlideRejectsInvalidDirection(t *testing.T) {
+ if err := MoveSlide(0); err == nil {
+ t.Fatal("direção inválida deveria ser rejeitada")
+ }
+}
+
+func TestStartSentUsesHDMIMonitorWhenRunningHyprland(t *testing.T) {
+ dir := t.TempDir()
+ dispatchFile := filepath.Join(dir, "dispatch")
+ hyprctl := filepath.Join(dir, "hyprctl")
+ script := "#!/bin/sh\n" +
+ "if [ \"$1\" = monitors ]; then printf '[{\"name\":\"eDP-1\"},{\"name\":\"HDMI-A-1\"}]'; exit 0; fi\n" +
+ "if [ \"$1\" = dispatch ]; then printf '%s' \"$2\" > \"$CULTO_TEST_HYPR_DISPATCH\"; printf 'ok'; exit 0; fi\n" +
+ "exit 1\n"
+ if err := os.WriteFile(hyprctl, []byte(script), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ t.Setenv("PATH", dir)
+ t.Setenv("HYPRLAND_INSTANCE_SIGNATURE", "test-instance")
+ t.Setenv("CULTO_TEST_HYPR_DISPATCH", dispatchFile)
+
+ sentPath := "/tmp/sent's build/sent"
+ slideFile := "/tmp/slides for culto"
+ if err := startSent(sentPath, slideFile); err != nil {
+ t.Fatal(err)
+ }
+ got, err := os.ReadFile(dispatchFile)
+ if err != nil {
+ t.Fatal(err)
+ }
+ want := `hl.dsp.exec_cmd("'/tmp/sent'\"'\"'s build/sent' '/tmp/slides for culto'", { monitor = "HDMI-A-1" })`
+ if string(got) != want {
+ t.Fatalf("dispatch Hyprland:\n%s\nesperado:\n%s", got, want)
+ }
+}
+
+func TestStartSentDoesNotLaunchOnWrongMonitorWhenHDMIIsMissing(t *testing.T) {
+ dir := t.TempDir()
+ dispatchFile := filepath.Join(dir, "dispatch")
+ hyprctl := filepath.Join(dir, "hyprctl")
+ script := "#!/bin/sh\n" +
+ "if [ \"$1\" = monitors ]; then printf '[{\"name\":\"eDP-1\"}]'; exit 0; fi\n" +
+ "if [ \"$1\" = dispatch ]; then printf called > \"$CULTO_TEST_HYPR_DISPATCH\"; printf 'ok'; exit 0; fi\n" +
+ "exit 1\n"
+ if err := os.WriteFile(hyprctl, []byte(script), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ t.Setenv("PATH", dir)
+ t.Setenv("HYPRLAND_INSTANCE_SIGNATURE", "test-instance")
+ t.Setenv("CULTO_TEST_HYPR_DISPATCH", dispatchFile)
+
+ err := startSent("/usr/bin/sent", "/tmp/slides")
+ if err == nil || !strings.Contains(err.Error(), "HDMI-A-1") {
+ t.Fatalf("erro inesperado para monitor ausente: %v", err)
+ }
+ if _, err := os.Stat(dispatchFile); !os.IsNotExist(err) {
+ t.Fatal("tentou iniciar sent mesmo sem o monitor HDMI-A-1")
+ }
+}