From 093b5ad07eeef6e21130b609fa239e56be56b417 Mon Sep 17 00:00:00 2001 From: Thomas Buckley-Houston Date: Sat, 21 May 2016 14:11:25 +0900 Subject: [PATCH] Expands texttop to full size of current terminal --- interfacer/interfacer.go | 78 +++++++++++++++++++--------------- interfacer/mouse_input_test.go | 18 +++++--- run.sh | 9 +++- xzoom/xzoom.h | 10 ++--- 4 files changed, 68 insertions(+), 47 deletions(-) diff --git a/interfacer/interfacer.go b/interfacer/interfacer.go index 65a2c7a..da3e750 100644 --- a/interfacer/interfacer.go +++ b/interfacer/interfacer.go @@ -3,6 +3,7 @@ package main import ( "fmt" "strings" + "strconv" "path/filepath" "time" "os" @@ -25,8 +26,13 @@ var logfile string var current string var curev termbox.Event var lastMouseButton string -var desktopWidth = float32(C.WIDTH) -var desktopHeight = float32(C.HEIGHT) + +var hipWidth int +var hipHeight int +var envDesktopWidth int +var envDesktopHeight int +var desktopWidth float32 +var desktopHeight float32 var desktopXFloat float32 var desktopYFloat float32 var roundedDesktopX int @@ -36,10 +42,6 @@ var roundedDesktopY int var stopXZoomChannel = make(chan struct{}) var xZoomStoppedChannel = make(chan struct{}) -// Dimensions of hiptext output, can be slightly different from terminal dimensions -var hipWidth int -var hipHeight int - var panNeedsSetup bool var panStartingX float32 var panStartingY float32 @@ -48,11 +50,36 @@ func initialise() { setupLogging() log("Starting...") setupTermbox() - calculateHipDimensions() + setupDimensions() C.xzoom_init() xzoomBackground() } +func parseENVVar(variable string) int { + value, err := strconv.Atoi(os.Getenv(variable)) + if err != nil { + panic(err) + } + return value +} + +func setupDimensions() { + hipWidth = parseENVVar("TTY_WIDTH") + hipHeight = parseENVVar("TTY_HEIGHT") + envDesktopWidth = parseENVVar("DESKTOP_WIDTH") + envDesktopHeight = parseENVVar("DESKTOP_HEIGHT") + C.desktop_width = C.int(envDesktopWidth) + C.width[C.SRC] = C.desktop_width + C.width[C.DST] = C.desktop_width + C.desktop_height = C.int(envDesktopHeight) + C.height[C.SRC] = C.desktop_height + C.height[C.DST] = C.desktop_height + desktopWidth = float32(envDesktopWidth) + desktopHeight = float32(envDesktopHeight) + log(fmt.Sprintf("Desktop dimensions: W: %d, H: %d", envDesktopWidth, envDesktopHeight)) + log(fmt.Sprintf("Term dimensions: W: %d, H: %d", hipWidth, hipHeight)) +} + func setupTermbox() { err := termbox.Init() if err != nil { @@ -86,23 +113,6 @@ func log(msg string) { } } -// Hiptext needs to render the aspect ratio faithfully. So firstly it tries to fill -// the terminal as much as it can. And secondly it treats a row as representing twice -// as much as a column - thus why there are some multiplications/divisions by 2. -func calculateHipDimensions() { - _tw, _th := termbox.Size() - tw := float32(_tw) - th := float32(_th * 2) - ratio := desktopWidth / desktopHeight - bestHeight := min(th, (tw / ratio)) - bestWidth := min(tw, (bestHeight * ratio)) - // Not sure why the +1 and -1 are needed, but they are. - hipWidth = roundToInt(bestWidth) + 1 - hipHeight = roundToInt(bestHeight / 2) - 1 - log(fmt.Sprintf("Term dimensions: W: %d, H: %d", _tw, _th)) - log(fmt.Sprintf("Hiptext dimensions: W: %d, H: %d", hipWidth, hipHeight)) -} - func min(a float32, b float32) float32 { if a < b { return a @@ -191,8 +201,8 @@ func zoom(direction string) { C.magnification-- } } - C.width[C.SRC] = (C.WIDTH + C.magnification - 1) / C.magnification; - C.height[C.SRC] = (C.HEIGHT + C.magnification - 1) / C.magnification; + C.width[C.SRC] = (C.desktop_width + C.magnification - 1) / C.magnification; + C.height[C.SRC] = (C.desktop_height + C.magnification - 1) / C.magnification; moveViewportForZoom(oldZoom) keepViewportInDesktop() @@ -217,26 +227,26 @@ func manageViewportSize() { if C.width[C.SRC] < 1 { C.width[C.SRC] = 1 } - if C.width[C.SRC] > C.WIDTH { - C.width[C.SRC] = C.WIDTH + if C.width[C.SRC] > C.desktop_width { + C.width[C.SRC] = C.desktop_width } if C.height[C.SRC] < 1 { C.height[C.SRC] = 1 } - if C.height[C.SRC] > C.HEIGHT { - C.height[C.SRC] = C.HEIGHT + if C.height[C.SRC] > C.desktop_height { + C.height[C.SRC] = C.desktop_height } } func manageViewportPosition() { - if C.xgrab > (C.WIDTH - C.width[C.SRC]) { - C.xgrab = C.WIDTH - C.width[C.SRC] + if C.xgrab > (C.desktop_width - C.width[C.SRC]) { + C.xgrab = C.desktop_width - C.width[C.SRC] } if C.xgrab < 0 { C.xgrab = 0 } - if C.ygrab > (C.HEIGHT - C.height[C.SRC]) { - C.ygrab = C.HEIGHT - C.height[C.SRC] + if C.ygrab > (C.desktop_height - C.height[C.SRC]) { + C.ygrab = C.desktop_height - C.height[C.SRC] } if C.ygrab < 0 { C.ygrab = 0 diff --git a/interfacer/mouse_input_test.go b/interfacer/mouse_input_test.go index 8775156..4290b4e 100644 --- a/interfacer/mouse_input_test.go +++ b/interfacer/mouse_input_test.go @@ -5,6 +5,8 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "testing" + + "os" ) func TestMouseInput(t *testing.T) { @@ -14,13 +16,13 @@ func TestMouseInput(t *testing.T) { var _ = Describe("Mouse Input", func() { BeforeEach(func() { - err := termbox.Init() - if err != nil { - panic(err) - } - initialise() - hipWidth = 90 - hipHeight = 30 + os.Setenv("DESKTOP_WIDTH", "1600") + os.Setenv("DESKTOP_HEIGHT", "1200") + os.Setenv("TTY_WIDTH", "90") + os.Setenv("TTY_HEIGHT", "30") + setupLogging() + termbox.Init() + setupDimensions() setCurrentDesktopCoords() }) @@ -74,6 +76,8 @@ var _ = Describe("Mouse Input", func() { zoom("in") setCurrentDesktopCoords() zoom("out") + // Shouldn't need to do this a second time, but this test helped me + // figure out a different bug, so I'm leaving it like this for now. zoom("out") setCurrentDesktopCoords() Expect(getXGrab()).To(Equal(0)) diff --git a/run.sh b/run.sh index f8ede54..6e794ed 100755 --- a/run.sh +++ b/run.sh @@ -4,7 +4,14 @@ export LC_ALL=C export LANG=C export DESKTOP_WIDTH='1600' -export DESKTOP_HEIGHT='1200' +export TTY_WIDTH=$(( $(stty size | cut -d' ' -f2) - 1)) +export TTY_HEIGHT=$(( $(stty size | cut -d' ' -f1) - 1)) +# Hiptext uses a row to represent twice as much as a column in order +# to more faithfully project the aspect ratio of the image/video. +ratio=$(echo "scale=5; $TTY_HEIGHT * 2 / $TTY_WIDTH" | bc) +height_float=$(echo "scale=5; $ratio*$DESKTOP_WIDTH" | bc) +export DESKTOP_HEIGHT=$(printf "%.0f\n" "$height_float") + export DISPLAY=:0 DESKTOP_RES="$DESKTOP_WIDTH"x"$DESKTOP_HEIGHT" UDP_URI='udp://127.0.0.1:1234' diff --git a/xzoom/xzoom.h b/xzoom/xzoom.h index 5161ec9..12b7e05 100644 --- a/xzoom/xzoom.h +++ b/xzoom/xzoom.h @@ -28,8 +28,8 @@ GC gc; #define SRC 0 #define DST 1 -#define WIDTH 1600 -#define HEIGHT 1200 +int desktop_width; +int desktop_height; // The top left of the area that we zoom in on int xgrab = 0; @@ -38,8 +38,8 @@ int ygrab = 0; int magnification = 1; int old_magnification = 1; -int width[2] = { WIDTH, WIDTH }; -int height[2] = { HEIGHT, HEIGHT }; +int width[2]; +int height[2]; unsigned depth = 0; XImage *ximage[2]; @@ -137,7 +137,7 @@ int xzoom_init() { depth = DefaultDepthOfScreen(scr); win = XCreateWindow(dpy, RootWindowOfScreen(scr), - WIDTH, 0, width[DST], height[DST], 0, + desktop_width, 0, width[DST], height[DST], 0, DefaultDepthOfScreen(scr), InputOutput, DefaultVisualOfScreen(scr), CWEventMask | CWBackPixel, &xswa);