Skip to content

Commit

Permalink
Just some atom-beautify to make code look better
Browse files Browse the repository at this point in the history
  • Loading branch information
tombh committed May 21, 2016
1 parent 8c7071f commit 09168fc
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 108 deletions.
53 changes: 26 additions & 27 deletions interfacer/interfacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package main

import (
"fmt"
"strings"
"strconv"
"path/filepath"
"time"
"github.com/tombh/termbox-go"
"math"
"os"
"os/exec"
"math"
"github.com/tombh/termbox-go"
"path/filepath"
"strconv"
"strings"
"time"
)

// Import the xzoom C code that creates an X window that zooms
Expand Down Expand Up @@ -201,8 +201,8 @@ func zoom(direction string) {
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.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()
Expand Down Expand Up @@ -294,15 +294,14 @@ func mouseEvent() {
}
}


func pan() {
if panNeedsSetup {
panStartingX = desktopXFloat
panStartingY = desktopYFloat
panNeedsSetup = false
}
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()
}

Expand Down Expand Up @@ -356,11 +355,11 @@ func getSpecialKeyPress() string {
var key string
switch curev.Key {
case termbox.KeyEnter:
key = "Return"
key = "Return"
case termbox.KeyBackspace, termbox.KeyBackspace2:
key = "BackSpace"
key = "BackSpace"
case termbox.KeySpace:
key = "Space"
key = "Space"
case termbox.KeyF1:
key = "F1"
case termbox.KeyF2:
Expand Down Expand Up @@ -427,23 +426,23 @@ func parseInput() {
}

// Run the xzoom window in a background go routine
func xzoomBackground(){
go func(){
defer close(xZoomStoppedChannel)
for {
select {
default:
C.do_iteration()
time.Sleep(40 * time.Millisecond) // 25fps
case <-stopXZoomChannel:
// Gracefully close the xzoom go routine
return
}
}
func xzoomBackground() {
go func() {
defer close(xZoomStoppedChannel)
for {
select {
default:
C.do_iteration()
time.Sleep(40 * time.Millisecond) // 25fps
case <-stopXZoomChannel:
// Gracefully close the xzoom go routine
return
}
}
}()
}

func teardown(){
func teardown() {
termbox.Close()
close(stopXZoomChannel)
<-xZoomStoppedChannel
Expand Down
66 changes: 33 additions & 33 deletions interfacer/mouse_input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
)

func TestMouseInput(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Mouse Input Suite")
RegisterFailHandler(Fail)
RunSpecs(t, "Mouse Input Suite")
}

var _ = Describe("Mouse Input", func() {
Expand All @@ -23,77 +23,77 @@ var _ = Describe("Mouse Input", func() {
setupLogging()
termbox.Init()
setupDimensions()
setCurrentDesktopCoords()
setCurrentDesktopCoords()
})

AfterEach(func(){
termbox.Close()
})
AfterEach(func() {
termbox.Close()
})

Describe("Mouse position", func(){
Describe("Mouse position", func() {
It("Should work in the top left", func() {
curev.MouseX = 30
curev.MouseY = 10
curev.MouseY = 10
setCurrentDesktopCoords()
Expect(roundedDesktopX).To(Equal(533))
Expect(roundedDesktopY).To(Equal(400))
Expect(roundedDesktopY).To(Equal(400))
})
It("Should work in the middle", func() {
curev.MouseX = 45
curev.MouseY = 15
curev.MouseY = 15
setCurrentDesktopCoords()
Expect(roundedDesktopX).To(Equal(800))
Expect(roundedDesktopY).To(Equal(600))
Expect(roundedDesktopY).To(Equal(600))
})
It("Should work in the bottom right", func() {
curev.MouseX = 60
curev.MouseY = 20
curev.MouseY = 20
setCurrentDesktopCoords()
Expect(roundedDesktopX).To(Equal(1067))
Expect(roundedDesktopY).To(Equal(800))
Expect(roundedDesktopY).To(Equal(800))
})
})

Describe("Zooming", func() {
BeforeEach(func(){
curev.MouseX = 30
curev.MouseY = 10
setCurrentDesktopCoords()
})
BeforeEach(func() {
curev.MouseX = 30
curev.MouseY = 10
setCurrentDesktopCoords()
})
It("Should zoom in once", func() {
Expect(getXGrab()).To(Equal(0))
Expect(getYGrab()).To(Equal(0))
Expect(roundedDesktopX).To(Equal(533))
Expect(roundedDesktopY).To(Equal(400))
zoom("in")
setCurrentDesktopCoords()
Expect(roundedDesktopX).To(Equal(533))
Expect(roundedDesktopY).To(Equal(400))
zoom("in")
setCurrentDesktopCoords()
Expect(getXGrab()).To(Equal(266))
Expect(getYGrab()).To(Equal(200))
Expect(roundedDesktopX).To(Equal(533))
Expect(roundedDesktopY).To(Equal(400))
Expect(roundedDesktopX).To(Equal(533))
Expect(roundedDesktopY).To(Equal(400))
})
It("Should zoom in then out", func() {
zoom("in")
setCurrentDesktopCoords()
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))
Expect(getYGrab()).To(Equal(0))
Expect(roundedDesktopX).To(Equal(533))
Expect(roundedDesktopY).To(Equal(400))
Expect(roundedDesktopX).To(Equal(533))
Expect(roundedDesktopY).To(Equal(400))
})
It("Should zoom near an edge without breaking out", func() {
curev.MouseX = 0
curev.MouseY = 0
setCurrentDesktopCoords()
zoom("in")
curev.MouseY = 0
setCurrentDesktopCoords()
zoom("in")
Expect(getXGrab()).To(Equal(0))
Expect(getYGrab()).To(Equal(0))
Expect(roundedDesktopX).To(Equal(0))
Expect(roundedDesktopY).To(Equal(0))
Expect(roundedDesktopX).To(Equal(0))
Expect(roundedDesktopY).To(Equal(0))
})
})
})
91 changes: 49 additions & 42 deletions xzoom/scale.h
Original file line number Diff line number Diff line change
@@ -1,50 +1,57 @@
/* scale image from SRC to DST - parameterized by type T */

/* get pixel address of point (x,y) in image t */
#define getP(t,x,y) \
(T *) (&ximage[t]->data[(ximage[t]->xoffset+(x))*sizeof(T) + \
(y)*ximage[t]->bytes_per_line])
#define getP(t, x, y) \
(T *)(&ximage[t]->data[(ximage[t]->xoffset + (x)) * sizeof(T) + \
(y) * ximage[t]->bytes_per_line])

{
int i, j, k;

/* copy scaled lines from SRC to DST */
j = height[SRC] - 1;
do {
T *p1;
T *p2;
int p2step;
T *p1_save;

/* p1 point to begining of scanline j*magnification in DST */
p1 = getP(DST, 0, j * magnification);
p1_save = p1;
/* p2 point to begining of scanline j in SRC */
p2 = getP(SRC, 0, j);

i = width[SRC];
do {
T c = *p2++;
k = magnification; do *p1++ = c; while (--k > 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);
int i, j, k;

/* copy scaled lines from SRC to DST */
j = height[SRC] - 1;

do {
T *p1;
T *p2;
int p2step;
T *p1_save;

/* p1 point to begining of scanline j*magnification in DST */
p1 = getP(DST, 0, j * magnification);
p1_save = p1;

/* p2 point to begining of scanline j in SRC */
p2 = getP(SRC, 0, j);

i = width[SRC];

do {
T c = *p2++;
k = magnification;

do *p1++ = c; while (--k > 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
12 changes: 6 additions & 6 deletions xzoom/xzoom.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This code largely comes from Itai Nahshon's xzoom, see:
http://git.r-36.net/xzoom
*/
This code largely comes from Itai Nahshon's xzoom, see:
http://git.r-36.net/xzoom
*/

#include <stdio.h>
#include <string.h>
Expand All @@ -20,7 +20,7 @@ Display *dpy;
Screen *scr;
Window win, root, child;
Status status;
int winX, winY;
int winX, winY;
unsigned int mask;

GC gc;
Expand All @@ -35,7 +35,7 @@ int desktop_height;
int xgrab = 0;
int ygrab = 0;

int magnification = 1;
int magnification = 1;
int old_magnification = 1;

int width[2];
Expand Down Expand Up @@ -123,7 +123,7 @@ void recreate_images_on_zoom() {
int xzoom_init() {
XSetWindowAttributes xswa;
XGCValues gcv;
char *dpyname = ":0";
char *dpyname = ":0";

atexit(destroy_images);

Expand Down

0 comments on commit 09168fc

Please sign in to comment.