Just some atom-beautify to make code look better

This commit is contained in:
Thomas Buckley-Houston 2016-05-21 14:47:02 +09:00
parent 8c7071f8e0
commit 09168fc8ea
4 changed files with 109 additions and 103 deletions

View file

@ -2,14 +2,14 @@ package main
import ( import (
"fmt" "fmt"
"strings" "github.com/tombh/termbox-go"
"strconv" "math"
"path/filepath"
"time"
"os" "os"
"os/exec" "os/exec"
"math" "path/filepath"
"github.com/tombh/termbox-go" "strconv"
"strings"
"time"
) )
// Import the xzoom C code that creates an X window that zooms // Import the xzoom C code that creates an X window that zooms
@ -201,8 +201,8 @@ func zoom(direction string) {
C.magnification-- C.magnification--
} }
} }
C.width[C.SRC] = (C.desktop_width + 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; C.height[C.SRC] = (C.desktop_height + C.magnification - 1) / C.magnification
moveViewportForZoom(oldZoom) moveViewportForZoom(oldZoom)
keepViewportInDesktop() keepViewportInDesktop()
@ -294,7 +294,6 @@ func mouseEvent() {
} }
} }
func pan() { func pan() {
if panNeedsSetup { if panNeedsSetup {
panStartingX = desktopXFloat panStartingX = desktopXFloat
@ -302,7 +301,7 @@ func pan() {
panNeedsSetup = false panNeedsSetup = false
} }
C.xgrab = C.int(float32(C.xgrab) + panStartingX - desktopXFloat) C.xgrab = C.int(float32(C.xgrab) + panStartingX - desktopXFloat)
C.ygrab = C.int(float32(C.ygrab) + panStartingY - desktopYFloat) C.ygrab = C.int(float32(C.ygrab) + panStartingY - desktopYFloat)
keepViewportInDesktop() keepViewportInDesktop()
} }
@ -356,11 +355,11 @@ func getSpecialKeyPress() string {
var key string var key string
switch curev.Key { switch curev.Key {
case termbox.KeyEnter: case termbox.KeyEnter:
key = "Return" key = "Return"
case termbox.KeyBackspace, termbox.KeyBackspace2: case termbox.KeyBackspace, termbox.KeyBackspace2:
key = "BackSpace" key = "BackSpace"
case termbox.KeySpace: case termbox.KeySpace:
key = "Space" key = "Space"
case termbox.KeyF1: case termbox.KeyF1:
key = "F1" key = "F1"
case termbox.KeyF2: case termbox.KeyF2:
@ -427,23 +426,23 @@ func parseInput() {
} }
// Run the xzoom window in a background go routine // Run the xzoom window in a background go routine
func xzoomBackground(){ func xzoomBackground() {
go func(){ go func() {
defer close(xZoomStoppedChannel) defer close(xZoomStoppedChannel)
for { for {
select { select {
default: default:
C.do_iteration() C.do_iteration()
time.Sleep(40 * time.Millisecond) // 25fps time.Sleep(40 * time.Millisecond) // 25fps
case <-stopXZoomChannel: case <-stopXZoomChannel:
// Gracefully close the xzoom go routine // Gracefully close the xzoom go routine
return return
} }
} }
}() }()
} }
func teardown(){ func teardown() {
termbox.Close() termbox.Close()
close(stopXZoomChannel) close(stopXZoomChannel)
<-xZoomStoppedChannel <-xZoomStoppedChannel

View file

@ -10,8 +10,8 @@ import (
) )
func TestMouseInput(t *testing.T) { func TestMouseInput(t *testing.T) {
RegisterFailHandler(Fail) RegisterFailHandler(Fail)
RunSpecs(t, "Mouse Input Suite") RunSpecs(t, "Mouse Input Suite")
} }
var _ = Describe("Mouse Input", func() { var _ = Describe("Mouse Input", func() {
@ -23,58 +23,58 @@ var _ = Describe("Mouse Input", func() {
setupLogging() setupLogging()
termbox.Init() termbox.Init()
setupDimensions() setupDimensions()
setCurrentDesktopCoords() setCurrentDesktopCoords()
}) })
AfterEach(func(){ AfterEach(func() {
termbox.Close() termbox.Close()
}) })
Describe("Mouse position", func(){ Describe("Mouse position", func() {
It("Should work in the top left", func() { It("Should work in the top left", func() {
curev.MouseX = 30 curev.MouseX = 30
curev.MouseY = 10 curev.MouseY = 10
setCurrentDesktopCoords() setCurrentDesktopCoords()
Expect(roundedDesktopX).To(Equal(533)) Expect(roundedDesktopX).To(Equal(533))
Expect(roundedDesktopY).To(Equal(400)) Expect(roundedDesktopY).To(Equal(400))
}) })
It("Should work in the middle", func() { It("Should work in the middle", func() {
curev.MouseX = 45 curev.MouseX = 45
curev.MouseY = 15 curev.MouseY = 15
setCurrentDesktopCoords() setCurrentDesktopCoords()
Expect(roundedDesktopX).To(Equal(800)) Expect(roundedDesktopX).To(Equal(800))
Expect(roundedDesktopY).To(Equal(600)) Expect(roundedDesktopY).To(Equal(600))
}) })
It("Should work in the bottom right", func() { It("Should work in the bottom right", func() {
curev.MouseX = 60 curev.MouseX = 60
curev.MouseY = 20 curev.MouseY = 20
setCurrentDesktopCoords() setCurrentDesktopCoords()
Expect(roundedDesktopX).To(Equal(1067)) Expect(roundedDesktopX).To(Equal(1067))
Expect(roundedDesktopY).To(Equal(800)) Expect(roundedDesktopY).To(Equal(800))
}) })
}) })
Describe("Zooming", func() { Describe("Zooming", func() {
BeforeEach(func(){ BeforeEach(func() {
curev.MouseX = 30 curev.MouseX = 30
curev.MouseY = 10 curev.MouseY = 10
setCurrentDesktopCoords() setCurrentDesktopCoords()
}) })
It("Should zoom in once", func() { It("Should zoom in once", func() {
Expect(getXGrab()).To(Equal(0)) Expect(getXGrab()).To(Equal(0))
Expect(getYGrab()).To(Equal(0)) Expect(getYGrab()).To(Equal(0))
Expect(roundedDesktopX).To(Equal(533)) Expect(roundedDesktopX).To(Equal(533))
Expect(roundedDesktopY).To(Equal(400)) Expect(roundedDesktopY).To(Equal(400))
zoom("in") zoom("in")
setCurrentDesktopCoords() setCurrentDesktopCoords()
Expect(getXGrab()).To(Equal(266)) Expect(getXGrab()).To(Equal(266))
Expect(getYGrab()).To(Equal(200)) Expect(getYGrab()).To(Equal(200))
Expect(roundedDesktopX).To(Equal(533)) Expect(roundedDesktopX).To(Equal(533))
Expect(roundedDesktopY).To(Equal(400)) Expect(roundedDesktopY).To(Equal(400))
}) })
It("Should zoom in then out", func() { It("Should zoom in then out", func() {
zoom("in") zoom("in")
setCurrentDesktopCoords() setCurrentDesktopCoords()
zoom("out") zoom("out")
// Shouldn't need to do this a second time, but this test helped me // 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. // figure out a different bug, so I'm leaving it like this for now.
@ -82,18 +82,18 @@ var _ = Describe("Mouse Input", func() {
setCurrentDesktopCoords() setCurrentDesktopCoords()
Expect(getXGrab()).To(Equal(0)) Expect(getXGrab()).To(Equal(0))
Expect(getYGrab()).To(Equal(0)) Expect(getYGrab()).To(Equal(0))
Expect(roundedDesktopX).To(Equal(533)) Expect(roundedDesktopX).To(Equal(533))
Expect(roundedDesktopY).To(Equal(400)) Expect(roundedDesktopY).To(Equal(400))
}) })
It("Should zoom near an edge without breaking out", func() { It("Should zoom near an edge without breaking out", func() {
curev.MouseX = 0 curev.MouseX = 0
curev.MouseY = 0 curev.MouseY = 0
setCurrentDesktopCoords() setCurrentDesktopCoords()
zoom("in") zoom("in")
Expect(getXGrab()).To(Equal(0)) Expect(getXGrab()).To(Equal(0))
Expect(getYGrab()).To(Equal(0)) Expect(getYGrab()).To(Equal(0))
Expect(roundedDesktopX).To(Equal(0)) Expect(roundedDesktopX).To(Equal(0))
Expect(roundedDesktopY).To(Equal(0)) Expect(roundedDesktopY).To(Equal(0))
}) })
}) })
}) })

View file

@ -1,50 +1,57 @@
/* scale image from SRC to DST - parameterized by type T */ /* scale image from SRC to DST - parameterized by type T */
/* get pixel address of point (x,y) in image t */ /* get pixel address of point (x,y) in image t */
#define getP(t,x,y) \ #define getP(t, x, y) \
(T *) (&ximage[t]->data[(ximage[t]->xoffset+(x))*sizeof(T) + \ (T *)(&ximage[t]->data[(ximage[t]->xoffset + (x)) * sizeof(T) + \
(y)*ximage[t]->bytes_per_line]) (y) * ximage[t]->bytes_per_line])
{ {
int i, j, k; int i, j, k;
/* copy scaled lines from SRC to DST */ /* copy scaled lines from SRC to DST */
j = height[SRC] - 1; j = height[SRC] - 1;
do {
T *p1;
T *p2;
int p2step;
T *p1_save;
/* p1 point to begining of scanline j*magnification in DST */ do {
p1 = getP(DST, 0, j * magnification); T *p1;
p1_save = p1; T *p2;
/* p2 point to begining of scanline j in SRC */ int p2step;
p2 = getP(SRC, 0, j); T *p1_save;
i = width[SRC]; /* p1 point to begining of scanline j*magnification in DST */
do { p1 = getP(DST, 0, j * magnification);
T c = *p2++; p1_save = p1;
k = magnification; do *p1++ = c; while (--k > 0);
} while (--i > 0);
/* duplicate that line as needed */ /* p2 point to begining of scanline j in SRC */
if (magnification > 1) p2 = getP(SRC, 0, j);
{
/* p1 point to begining of scanline j*magnification in DST */
p1 = p1_save;
/* p2 points to begining of next line */
p2 = p1;
p2step = ximage[DST]->bytes_per_line / sizeof(T);
i = width[DST] * sizeof(T); i = width[SRC];
k = magnification - 1;
do { do {
p2 += p2step; T c = *p2++;
memcpy(p2, p1, i); k = magnification;
} while (--k > 0);
} do *p1++ = c; while (--k > 0);
} while (--j >= 0); } while (--i > 0);
/* duplicate that line as needed */
if (magnification > 1)
{
/* p1 point to begining of scanline j*magnification in DST */
p1 = p1_save;
/* p2 points to begining of next line */
p2 = p1;
p2step = ximage[DST]->bytes_per_line / sizeof(T);
i = width[DST] * sizeof(T);
k = magnification - 1;
do {
p2 += p2step;
memcpy(p2, p1, i);
} while (--k > 0);
}
} while (--j >= 0);
} }
#undef getP #undef getP

View file

@ -1,7 +1,7 @@
/* /*
This code largely comes from Itai Nahshon's xzoom, see: This code largely comes from Itai Nahshon's xzoom, see:
http://git.r-36.net/xzoom http://git.r-36.net/xzoom
*/ */
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -20,7 +20,7 @@ Display *dpy;
Screen *scr; Screen *scr;
Window win, root, child; Window win, root, child;
Status status; Status status;
int winX, winY; int winX, winY;
unsigned int mask; unsigned int mask;
GC gc; GC gc;
@ -35,7 +35,7 @@ int desktop_height;
int xgrab = 0; int xgrab = 0;
int ygrab = 0; int ygrab = 0;
int magnification = 1; int magnification = 1;
int old_magnification = 1; int old_magnification = 1;
int width[2]; int width[2];
@ -123,7 +123,7 @@ void recreate_images_on_zoom() {
int xzoom_init() { int xzoom_init() {
XSetWindowAttributes xswa; XSetWindowAttributes xswa;
XGCValues gcv; XGCValues gcv;
char *dpyname = ":0"; char *dpyname = ":0";
atexit(destroy_images); atexit(destroy_images);