This commit is contained in:
Nico 2021-01-30 21:45:50 +00:00
parent 1c2309e9bf
commit 1023aa1bc2
1 changed files with 66 additions and 71 deletions

137
main.go
View File

@ -1,25 +1,24 @@
package main
import (
"github.com/fogleman/gg"
"github.com/shermp/go-fbink-v2/gofbink"
"github.com/shermp/go-kobo-input/koboin"
"image"
"git.sr.ht/~adnano/go-gemini"
"git.sr.ht/~adnano/go-gemini/tofu"
"bufio"
"bytes"
"crypto/x509"
"errors"
"net/url"
"fmt"
"git.sr.ht/~adnano/go-gemini"
"git.sr.ht/~adnano/go-gemini/tofu"
"github.com/fogleman/gg"
"github.com/golang/freetype/truetype"
"github.com/shermp/go-fbink-v2/gofbink"
"github.com/shermp/go-kobo-input/koboin"
"golang.org/x/image/font"
"bufio"
"image"
"io/ioutil"
"fmt"
"time"
"os"
"log"
"bytes"
"github.com/golang/freetype/truetype"
"net/url"
"os"
"time"
)
var (
@ -30,10 +29,9 @@ var (
// Visual options
const (
width int = 1080
height int = 1440 // TODO get from device instead of hardcoding
linewidth float64 = 10
width int = 1080
height int = 1440 // TODO get from device instead of hardcoding
linewidth float64 = 10
)
var f *truetype.Font
@ -41,23 +39,23 @@ var face font.Face
// drawErrorBox prints the given error on screen, and only proceeds after touch input.
func drawErrorBox(e error, fb *gofbink.FBInk, t *koboin.TouchDevice, opts *gofbink.FBInkConfig) {
boxheight := height/4
boxwidth := width/2
i := image.NewRGBA(image.Rect(0,0, boxwidth, boxheight))
boxheight := height / 4
boxwidth := width / 2
i := image.NewRGBA(image.Rect(0, 0, boxwidth, boxheight))
dc := gg.NewContextForRGBA(i)
if face != nil {
dc.SetFontFace(face)
}
dc.SetLineWidth(linewidth)
dc.DrawRectangle(0,0, float64(boxwidth),float64(boxheight))
dc.SetRGB(1,1,1)
dc.FillPreserve()
dc.SetRGB(0,0,0)
dc.Stroke()
dc.SetRGB(0,0,0)
dc.DrawStringWrapped("Error:\n" + e.Error(), float64(boxwidth/2), float64(boxheight/2), 0.5, 0.5, float64(boxwidth), 1.0, gg.AlignCenter)
dc.Fill()
fb.PrintRBGA(int16(width/2-(boxwidth/2)), int16(height/2-(boxheight/2)), i, opts)
dc.DrawRectangle(0, 0, float64(boxwidth), float64(boxheight))
dc.SetRGB(1, 1, 1)
dc.FillPreserve()
dc.SetRGB(0, 0, 0)
dc.Stroke()
dc.SetRGB(0, 0, 0)
dc.DrawStringWrapped("Error:\n"+e.Error(), float64(boxwidth/2), float64(boxheight/2), 0.5, 0.5, float64(boxwidth), 1.0, gg.AlignCenter)
dc.Fill()
fb.PrintRBGA(int16(width/2-(boxwidth/2)), int16(height/2-(boxheight/2)), i, opts)
t.GetInput()
}
@ -70,12 +68,12 @@ func trustCertificate(hostname string, cert *x509.Certificate) error {
return nil
}
return errors.New("error: fingerprint does not match!")
} else { // Expired cert or new host: TOFU
hosts.Add(host)
hostsfile.WriteHost(host)
return nil
}
return errors.New("error: fingerprint does not match!")
} else { // Expired cert or new host: TOFU
hosts.Add(host)
hostsfile.WriteHost(host)
return nil
}
return errors.New("error: fingerprint does not match!")
}
func do(req *gemini.Request, via []*gemini.Request) (*gemini.Response, error) {
@ -88,7 +86,7 @@ func do(req *gemini.Request, via []*gemini.Request) (*gemini.Response, error) {
}
switch resp.Status.Class() {
case gemini.StatusClassInput: // TODO implement input
case gemini.StatusClassInput: // TODO implement input
/* input, ok := getInput(resp.Meta, resp.Status == gemini.StatusSensitiveInput)
if !ok {
break
@ -97,7 +95,6 @@ func do(req *gemini.Request, via []*gemini.Request) (*gemini.Response, error) {
req.URL.RawQuery = gemini.QueryEscape(input)
return do(req, via) */
case gemini.StatusClassRedirect:
via = append(via, req)
if len(via) > 5 {
@ -118,21 +115,21 @@ func do(req *gemini.Request, via []*gemini.Request) (*gemini.Response, error) {
}
func main() {
// Framebuffer setup
// Framebuffer setup
fbinkOpts := gofbink.FBInkConfig{}
rOpts := gofbink.RestrictedConfig{}
fb := gofbink.New(&fbinkOpts, &rOpts)
fb.Open()
defer fb.Close()
defer fb.Close()
fb.Init(&fbinkOpts)
fb.ClearScreen(&fbinkOpts)
fb.Refresh(0,0,0,0, gofbink.DitherPassthrough, &fbinkOpts)
fb.ClearScreen(&fbinkOpts)
fb.Refresh(0, 0, 0, 0, gofbink.DitherPassthrough, &fbinkOpts)
// Touchscreen setup
touchPath := "/dev/input/event1"
t := koboin.New(touchPath, height, width)
// Touchscreen setup
touchPath := "/dev/input/event1"
t := koboin.New(touchPath, height, width)
if t == nil {
return
return
}
defer t.Close()
@ -141,17 +138,16 @@ func main() {
if err != nil {
drawErrorBox(err, fb, t, &fbinkOpts)
}
var logger *log.Logger = log.New(logFile, "gemini", log.LstdFlags )
var logger *log.Logger = log.New(logFile, "gemini", log.LstdFlags)
defer logFile.Close()
path := "/mnt/onboard/.adds/gemini/known-hosts" // TODO don't hardcode
// Reload or create known hosts file
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Create(path)
os.Create(path)
}
err = hosts.Load(path)
err = hosts.Load(path)
if err != nil {
drawErrorBox(err, fb, t, &fbinkOpts)
logger.Fatal(err)
@ -164,20 +160,19 @@ func main() {
}
// Load Fonts, Create font face
b, err := ioutil.ReadFile("/mnt/onboard/.adds/gemini/NotoSerif.ttf")
if err != nil {
logger.Println(err)
return
}
f, err := truetype.Parse(b)
if err != nil {
logger.Println(err)
return
}
face = truetype.NewFace(f, &truetype.Options{Size:32})
b, err := ioutil.ReadFile("/mnt/onboard/.adds/gemini/NotoSerif.ttf")
if err != nil {
logger.Println(err)
return
}
f, err := truetype.Parse(b)
if err != nil {
logger.Println(err)
return
}
face = truetype.NewFace(f, &truetype.Options{Size: 32})
url := "gemini://gemini.circumlunar.space"
url := "gemini://gemini.circumlunar.space"
req, err := gemini.NewRequest(url)
if err != nil {
drawErrorBox(err, fb, t, &fbinkOpts)
@ -201,17 +196,17 @@ func main() {
} else {
fb.Println(fmt.Sprintf("%d %s\n", resp.Status, resp.Meta))
}
for {
x, y, err := t.GetInput()
x, y, err := t.GetInput()
if err != nil {
continue
}
if x < 100 && y < 100 {
fb.Println("Exiting...")
break
}
}
hostsfile.Close()
if x < 100 && y < 100 {
fb.Println("Exiting...")
break
}
}
hostsfile.Close()
}