From 59c7f02ad05af0223a08f47e3193c331a86445d5 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 16 Apr 2023 11:44:28 +0200 Subject: Switch completely to Go and Gio UI The properties of the Go language, standard library and the Gio UI library are well suited for this project. The existing experimental Go code exceeds the GTK4 and SDL attempts by far with respect to quality and code simplicity. --- Makefile | 40 -------- README.md | 35 +------ ROADMAP.md | 3 +- go.mod | 17 ++++ go.sum | 34 +++++++ go/go.mod | 17 ---- go/go.sum | 34 ------- go/main.go | 249 ------------------------------------------------- gui.c | 115 ----------------------- gui.h | 20 ---- main.c | 85 ----------------- main.go | 249 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/get_domainpart.c | 40 -------- tools/lint.sh | 10 -- xmpp.c | 200 --------------------------------------- xmpp.h | 21 ----- 16 files changed, 302 insertions(+), 867 deletions(-) delete mode 100644 Makefile create mode 100644 go.mod create mode 100644 go.sum delete mode 100644 go/go.mod delete mode 100644 go/go.sum delete mode 100644 go/main.go delete mode 100644 gui.c delete mode 100644 gui.h delete mode 100644 main.c create mode 100644 main.go delete mode 100644 tests/get_domainpart.c delete mode 100755 tools/lint.sh delete mode 100644 xmpp.c delete mode 100644 xmpp.h diff --git a/Makefile b/Makefile deleted file mode 100644 index 2ffa1b8..0000000 --- a/Makefile +++ /dev/null @@ -1,40 +0,0 @@ -CC=gcc -CFLAGS=-g -I. -lSDL2 -SRC:=$(shell find ./ -maxdepth 1 -type f -regex ".*\.c" -not -name "main.c") -OBJ:=$(patsubst ./%.c,build/%.o,$(SRC)) -_TESTS:=$(shell find ./tests/ -maxdepth 1 -type f -regex ".*\.c") -TESTS:=$(patsubst ./%.c,build/%,$(_TESTS)) - -LIMOX=build/limox -GRAPHICS=build/icon.png build/icon.pdf -DOCS=build/docs/README.pdf build/docs/ROADMAP.pdf - -all: build $(OBJ) $(TESTS) $(LIMOX) $(DOCS) $(GRAPHICS) - -build: - mkdir -p build/tests - mkdir -p build/docs - -$(LIMOX): $(OBJ) build/main.o - $(CC) -o $@ $^ $(CFLAGS) - -build/%.o: %.c - $(CC) -c -o $@ $< $(CFLAGS) - -build/%.png: %.svg - inkscape $< -o $@ -w 256 -h 256 - -build/%.pdf: %.svg - inkscape $< -o $@ -w 256 -h 256 - -build/docs/%.pdf: %.md $(GRAPHICS) - pandoc -o $@ $< - -build/tests/%: tests/%.c $(OBJ) - $(CC) -o $@ $< $(OBJ) $(CFLAGS) - $@ - -.PHONY: clean - -clean: - rm -rf build diff --git a/README.md b/README.md index 7b13320..cffa779 100644 --- a/README.md +++ b/README.md @@ -7,40 +7,7 @@ usable on mobile linux devices like smartphones or tablets. ![LimoX icon](build/icon.pdf) -Also mind the `ROADMAP` file, the [project page][1] and my [packaging -repository][2] for Arch Linux with support for LimoX. A nice reference which -helps me a lot is [Beej's Guide to Network Programming][3] aswell as [Gigi's -SDL2 tutorial][5] and the [SDL2 documentation][4]. - - -## Building and dependencies - -Install these build dependencies: - -- GNU coreutils -- GNU make -- gcc -- pandoc -- texlive-core -- inkscape - -Install these runtime dependencies: - -- sdl2 - -Just run these commands to build, test and run: - -``` -make all -./build/limox -``` - -Run `make clean` to clean the build folder. You can get a help page by running -`./build/limox -h`. +Also mind the `ROADMAP` file and the [project page][1]. [1]: https://xengineering.eu/git/limox -[2]: https://xengineering.eu/git/pkgbuilds -[3]: https://www.beej.us/guide/bgnet/html/ -[4]: https://wiki.libsdl.org/SDL2/FrontPage -[5]: https://gigi.nullneuron.net/gigilabs/writing/sdl2-tutorials/ diff --git a/ROADMAP.md b/ROADMAP.md index d26aab2..cec6e4c 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,8 +2,7 @@ # Roadmap -- [x] develop minimal viable product with GTK4 (branch `legacy/gtk4-libstrophe`) -- [ ] develop minimal viable product with SDL2 +- [ ] develop minimal viable product - [ ] implement XMPP core client ([compliance suite 2022][1]) - [ ] RFC 6120: XMPP Core - [ ] RFC 7590: TLS for XMPP diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..de2e3d7 --- /dev/null +++ b/go.mod @@ -0,0 +1,17 @@ +module xengineering.eu/limox + +go 1.20 + +require gioui.org v0.0.0-20230206180804-32c6a9b10d0b + +require ( + gioui.org/cpu v0.0.0-20210817075930-8d6a761490d2 // indirect + gioui.org/shader v1.0.6 // indirect + github.com/benoitkugler/textlayout v0.3.0 // indirect + github.com/go-text/typesetting v0.0.0-20221214153724-0399769901d5 // indirect + golang.org/x/exp v0.0.0-20221012211006-4de253d81b95 // indirect + golang.org/x/exp/shiny v0.0.0-20220827204233-334a2380cb91 // indirect + golang.org/x/image v0.0.0-20220722155232-062f8c9fd539 // indirect + golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 // indirect + golang.org/x/text v0.3.7 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4d42bf4 --- /dev/null +++ b/go.sum @@ -0,0 +1,34 @@ +eliasnaur.com/font v0.0.0-20220124212145-832bb8fc08c3 h1:djFprmHZgrSepsHAIRMp5UJn3PzsoTg9drI+BDmif5Q= +gioui.org v0.0.0-20230206180804-32c6a9b10d0b h1:ghRvtb24ItyA7QffjaG38gH5f7bzagcz0OPq3T3FyHI= +gioui.org v0.0.0-20230206180804-32c6a9b10d0b/go.mod h1:3lLo7xMHYnnHTrgKNNctBjEKKH3wQCO2Sn7ti5Jy8mU= +gioui.org/cpu v0.0.0-20210808092351-bfe733dd3334/go.mod h1:A8M0Cn5o+vY5LTMlnRoK3O5kG+rH0kWfJjeKd9QpBmQ= +gioui.org/cpu v0.0.0-20210817075930-8d6a761490d2 h1:AGDDxsJE1RpcXTAxPG2B4jrwVUJGFDjINIPi1jtO6pc= +gioui.org/cpu v0.0.0-20210817075930-8d6a761490d2/go.mod h1:A8M0Cn5o+vY5LTMlnRoK3O5kG+rH0kWfJjeKd9QpBmQ= +gioui.org/shader v1.0.6 h1:cvZmU+eODFR2545X+/8XucgZdTtEjR3QWW6W65b0q5Y= +gioui.org/shader v1.0.6/go.mod h1:mWdiME581d/kV7/iEhLmUgUK5iZ09XR5XpduXzbePVM= +github.com/benoitkugler/pstokenizer v1.0.0/go.mod h1:l1G2Voirz0q/jj0TQfabNxVsa8HZXh/VMxFSRALWTiE= +github.com/benoitkugler/textlayout v0.3.0 h1:2ehWXEkgb6RUokTjXh1LzdGwG4dRP6X3dqhYYDYhUVk= +github.com/benoitkugler/textlayout v0.3.0/go.mod h1:o+1hFV+JSHBC9qNLIuwVoLedERU7sBPgEFcuSgfvi/w= +github.com/benoitkugler/textlayout-testdata v0.1.1 h1:AvFxBxpfrQd8v55qH59mZOJOQjtD6K2SFe9/HvnIbJk= +github.com/benoitkugler/textlayout-testdata v0.1.1/go.mod h1:i/qZl09BbUOtd7Bu/W1CAubRwTWrEXWq6JwMkw8wYxo= +github.com/go-text/typesetting v0.0.0-20221214153724-0399769901d5 h1:iOA0HmtpANn48hX2nlDNMu0VVaNza35HJG0WeetBVzQ= +github.com/go-text/typesetting v0.0.0-20221214153724-0399769901d5/go.mod h1:/cmOXaoTiO+lbCwkTZBgCvevJpbFsZ5reXIpEJVh5MI= +golang.org/x/exp v0.0.0-20221012211006-4de253d81b95 h1:sBdrWpxhGDdTAYNqbgBLAR+ULAPPhfgncLr1X0lyWtg= +golang.org/x/exp v0.0.0-20221012211006-4de253d81b95/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= +golang.org/x/exp/shiny v0.0.0-20220827204233-334a2380cb91 h1:ryT6Nf0R83ZgD8WnFFdfI8wCeyqgdXWN4+CkFVNPAT0= +golang.org/x/exp/shiny v0.0.0-20220827204233-334a2380cb91/go.mod h1:VjAR7z0ngyATZTELrBSkxOOHhhlnVUxDye4mcjx5h/8= +golang.org/x/image v0.0.0-20210504121937-7319ad40d33e/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= +golang.org/x/image v0.0.0-20220722155232-062f8c9fd539 h1:/eM0PCrQI2xd471rI+snWuu251/+/jpBpZqir2mPdnU= +golang.org/x/image v0.0.0-20220722155232-062f8c9fd539/go.mod h1:doUCurBvlfPMKfmIpRIywoHmhN3VyhnoFDbvIEWF4hY= +golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 h1:UiNENfZ8gDvpiWw7IpOMQ27spWmThO1RwwdQVbJahJM= +golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/go/go.mod b/go/go.mod deleted file mode 100644 index de2e3d7..0000000 --- a/go/go.mod +++ /dev/null @@ -1,17 +0,0 @@ -module xengineering.eu/limox - -go 1.20 - -require gioui.org v0.0.0-20230206180804-32c6a9b10d0b - -require ( - gioui.org/cpu v0.0.0-20210817075930-8d6a761490d2 // indirect - gioui.org/shader v1.0.6 // indirect - github.com/benoitkugler/textlayout v0.3.0 // indirect - github.com/go-text/typesetting v0.0.0-20221214153724-0399769901d5 // indirect - golang.org/x/exp v0.0.0-20221012211006-4de253d81b95 // indirect - golang.org/x/exp/shiny v0.0.0-20220827204233-334a2380cb91 // indirect - golang.org/x/image v0.0.0-20220722155232-062f8c9fd539 // indirect - golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 // indirect - golang.org/x/text v0.3.7 // indirect -) diff --git a/go/go.sum b/go/go.sum deleted file mode 100644 index 4d42bf4..0000000 --- a/go/go.sum +++ /dev/null @@ -1,34 +0,0 @@ -eliasnaur.com/font v0.0.0-20220124212145-832bb8fc08c3 h1:djFprmHZgrSepsHAIRMp5UJn3PzsoTg9drI+BDmif5Q= -gioui.org v0.0.0-20230206180804-32c6a9b10d0b h1:ghRvtb24ItyA7QffjaG38gH5f7bzagcz0OPq3T3FyHI= -gioui.org v0.0.0-20230206180804-32c6a9b10d0b/go.mod h1:3lLo7xMHYnnHTrgKNNctBjEKKH3wQCO2Sn7ti5Jy8mU= -gioui.org/cpu v0.0.0-20210808092351-bfe733dd3334/go.mod h1:A8M0Cn5o+vY5LTMlnRoK3O5kG+rH0kWfJjeKd9QpBmQ= -gioui.org/cpu v0.0.0-20210817075930-8d6a761490d2 h1:AGDDxsJE1RpcXTAxPG2B4jrwVUJGFDjINIPi1jtO6pc= -gioui.org/cpu v0.0.0-20210817075930-8d6a761490d2/go.mod h1:A8M0Cn5o+vY5LTMlnRoK3O5kG+rH0kWfJjeKd9QpBmQ= -gioui.org/shader v1.0.6 h1:cvZmU+eODFR2545X+/8XucgZdTtEjR3QWW6W65b0q5Y= -gioui.org/shader v1.0.6/go.mod h1:mWdiME581d/kV7/iEhLmUgUK5iZ09XR5XpduXzbePVM= -github.com/benoitkugler/pstokenizer v1.0.0/go.mod h1:l1G2Voirz0q/jj0TQfabNxVsa8HZXh/VMxFSRALWTiE= -github.com/benoitkugler/textlayout v0.3.0 h1:2ehWXEkgb6RUokTjXh1LzdGwG4dRP6X3dqhYYDYhUVk= -github.com/benoitkugler/textlayout v0.3.0/go.mod h1:o+1hFV+JSHBC9qNLIuwVoLedERU7sBPgEFcuSgfvi/w= -github.com/benoitkugler/textlayout-testdata v0.1.1 h1:AvFxBxpfrQd8v55qH59mZOJOQjtD6K2SFe9/HvnIbJk= -github.com/benoitkugler/textlayout-testdata v0.1.1/go.mod h1:i/qZl09BbUOtd7Bu/W1CAubRwTWrEXWq6JwMkw8wYxo= -github.com/go-text/typesetting v0.0.0-20221214153724-0399769901d5 h1:iOA0HmtpANn48hX2nlDNMu0VVaNza35HJG0WeetBVzQ= -github.com/go-text/typesetting v0.0.0-20221214153724-0399769901d5/go.mod h1:/cmOXaoTiO+lbCwkTZBgCvevJpbFsZ5reXIpEJVh5MI= -golang.org/x/exp v0.0.0-20221012211006-4de253d81b95 h1:sBdrWpxhGDdTAYNqbgBLAR+ULAPPhfgncLr1X0lyWtg= -golang.org/x/exp v0.0.0-20221012211006-4de253d81b95/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= -golang.org/x/exp/shiny v0.0.0-20220827204233-334a2380cb91 h1:ryT6Nf0R83ZgD8WnFFdfI8wCeyqgdXWN4+CkFVNPAT0= -golang.org/x/exp/shiny v0.0.0-20220827204233-334a2380cb91/go.mod h1:VjAR7z0ngyATZTELrBSkxOOHhhlnVUxDye4mcjx5h/8= -golang.org/x/image v0.0.0-20210504121937-7319ad40d33e/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= -golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= -golang.org/x/image v0.0.0-20220722155232-062f8c9fd539 h1:/eM0PCrQI2xd471rI+snWuu251/+/jpBpZqir2mPdnU= -golang.org/x/image v0.0.0-20220722155232-062f8c9fd539/go.mod h1:doUCurBvlfPMKfmIpRIywoHmhN3VyhnoFDbvIEWF4hY= -golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 h1:UiNENfZ8gDvpiWw7IpOMQ27spWmThO1RwwdQVbJahJM= -golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/go/main.go b/go/main.go deleted file mode 100644 index 2803e68..0000000 --- a/go/main.go +++ /dev/null @@ -1,249 +0,0 @@ -package main - -import ( - "image/color" - "log" - "net" - "os" - "reflect" - - "gioui.org/app" - "gioui.org/font/gofont" - "gioui.org/io/system" - "gioui.org/layout" - "gioui.org/op" - "gioui.org/text" - "gioui.org/unit" - "gioui.org/widget" - "gioui.org/widget/material" -) - -type XmppEvent uint8 - -const ( - XmppDisconnect XmppEvent = iota - XmppConnect -) - -type GuiEvent uint8 - -const ( - Disconnect GuiEvent = iota -) - -type LimoxState uint8 - -const ( - Disconnected LimoxState = iota - Connecting - Connected -) - -type Limox struct { - JidEditor widget.Editor - PwdEditor widget.Editor - MainButton widget.Clickable - XmppEvents chan any - GuiEvents chan GuiEvent - State LimoxState - Window *app.Window - Operations op.Ops - Theme *material.Theme -} - -func main() { - limox := NewLimox() - - go func() { - err := limox.run() - if err != nil { - log.Fatal(err) - } - os.Exit(0) - }() - - app.Main() -} - -func NewLimox() Limox { - return Limox{ - Window: app.NewWindow( - app.Title("LimoX"), - app.Size(unit.Dp(400), unit.Dp(600)), - ), - Operations: op.Ops{}, - Theme: material.NewTheme(gofont.Collection()), - XmppEvents: make(chan any), - GuiEvents: make(chan GuiEvent), - State: Disconnected, - } -} - -func (l *Limox) run() error { - for { - select { - case e := <-l.Window.Events(): - switch e := e.(type) { - case system.DestroyEvent: - return e.Err - case system.FrameEvent: - if l.MainButton.Clicked() { - l.buttonCallback() - } - l.draw(e) - } - case ev := <-l.XmppEvents: - switch ev.(type) { - case error: - log.Print(ev) - l.State = Disconnected - case XmppEvent: - switch ev { - case XmppDisconnect: - l.State = Disconnected - case XmppConnect: - l.State = Connected - default: - log.Fatalf("Unknown XmppEvent '%d'\n", ev) - } - default: - log.Fatalf("Unknown event type '%s'.\n", reflect.TypeOf(ev)) - } - } - } -} - -func (l *Limox) buttonCallback() { - switch l.State { - case Disconnected: - log.Println("Starting connection establishment") - go l.xmpp(l.JidEditor.Text(), l.PwdEditor.Text()) - l.State = Connecting - case Connecting: - log.Println("Aborted connection establishment") - l.State = Disconnected - case Connected: - log.Println("Disconnected") - l.GuiEvents <- Disconnect - l.State = Disconnected - } -} - -func (l *Limox) xmpp(jid string, pwd string) { - log.Printf("JID: '%s' PWD: '%s'\n", jid, pwd) - - domain := domainpart(jid) - log.Printf("Domain: '%s'\n", domain) - - tcpServer, err := net.ResolveTCPAddr("tcp", domain+":"+"5222") - if err != nil { - l.XmppEvents <- err - return - } - log.Printf("Server: %s\n", tcpServer) - - conn, err := net.DialTCP("tcp", nil, tcpServer) - if err != nil { - l.XmppEvents <- err - return - } - l.XmppEvents <- XmppConnect - - var closing bool = false - for { - ev := <-l.GuiEvents - switch ev { - case Disconnect: - closing = true - default: - log.Fatalf("Unknown GuiEvent '%d'!\n", ev) - } - if closing { - break - } - } - - conn.Close() - l.XmppEvents <- XmppDisconnect -} - -// domainpart extracts the domain name from a JID / XMPP address. See -// https://datatracker.ietf.org/doc/html/rfc7622#section-3.2 for details. -func domainpart(jid string) string { - list := []rune(jid) - - for i, v := range list { - if v == '/' { - list = list[:i] - break - } - } - - for i, v := range list { - if v == '@' { - list = list[i+1:] - break - } - } - - return string(list) -} - -func (l *Limox) draw(e system.FrameEvent) { - gtx := layout.NewContext(&l.Operations, e) - - flex := layout.Flex{ - Axis: layout.Vertical, - Spacing: layout.SpaceBetween, - } - - flex.Layout(gtx, - layout.Rigid( - func(gtx layout.Context) layout.Dimensions { - h2 := material.H2(l.Theme, "LimoX") - h2.Color = color.NRGBA{R: 20, G: 20, B: 20, A: 255} - h2.Alignment = text.Middle - return h2.Layout(gtx) - }, - ), - layout.Rigid( - func(gtx layout.Context) layout.Dimensions { - jid := material.Editor(l.Theme, &l.JidEditor, "user@example.com") - jid.Editor.Alignment = text.Middle - return jid.Layout(gtx) - }, - ), - layout.Rigid( - func(gtx layout.Context) layout.Dimensions { - pwd := material.Editor(l.Theme, &l.PwdEditor, "mySafePassword") - pwd.Editor.Alignment = text.Middle - pwd.Editor.Mask = '*' - return pwd.Layout(gtx) - }, - ), - layout.Rigid( - func(gtx layout.Context) layout.Dimensions { - btn := material.Button(l.Theme, &l.MainButton, - l.buttonLabel()) - return btn.Layout(gtx) - }, - ), - ) - - e.Frame(gtx.Ops) -} - -func (l *Limox) buttonLabel() string { - var label string - switch l.State { - case Disconnected: - label = "Connect" - case Connecting: - label = "Abort" - case Connected: - label = "Disconnect" - default: - log.Fatalf("Unknown Limox state '%d'\n", l.State) - } - return label -} diff --git a/gui.c b/gui.c deleted file mode 100644 index 12f8f47..0000000 --- a/gui.c +++ /dev/null @@ -1,115 +0,0 @@ -/* - * LimoX - The Linux on mobile XMPP chat client - * Copyright (C) 2022 xengineering - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -#include -#include -#include -#include - -#include "xmpp.h" - - -struct GuiContext { - bool quit; - SDL_Event event; - SDL_Window* window; - SDL_Renderer* renderer; - SDL_Texture* texture; - uint32_t* pixels; - int xmpp_fd; -}; - - -struct GuiContext *init() { - struct GuiContext *ctx = malloc(sizeof(struct GuiContext)); - - ctx->quit = false; - ctx->xmpp_fd = -1; - - ctx->pixels = malloc(sizeof(uint32_t) * 640 * 480); - memset(ctx->pixels, 255, 640 * 480 * sizeof(uint32_t)); - - SDL_Init(SDL_INIT_VIDEO); - - return ctx; -} - -void deinit(struct GuiContext *ctx) { - - // TODO this seems to end in memory access errors but ... why? - //SDL_DestroyTexture(texture); - //SDL_DestroyRenderer(renderer); - - SDL_Quit(); - - free(ctx->pixels); - free(ctx); -} - -bool create_window(struct GuiContext *ctx) { - - ctx->window = SDL_CreateWindow("LimoX", - SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, - SDL_WINDOW_RESIZABLE - ); - - ctx->renderer = SDL_CreateRenderer(ctx->window, -1, 0); - ctx->texture = SDL_CreateTexture(ctx->renderer, SDL_PIXELFORMAT_ARGB8888, - SDL_TEXTUREACCESS_STATIC, 640, 480); - - return ctx->window != NULL; -} - -void gui_run(void) { - - struct GuiContext *ctx = init(); - - fprintf(stderr, "SDL2 video driver: %s\n", SDL_GetCurrentVideoDriver()); - - if (!create_window(ctx)) { - fprintf(stderr, "Failed to create SDL2 window!\n"); - return; - } - - while (!ctx->quit) { - SDL_UpdateTexture(ctx->texture, NULL, ctx->pixels, 640 * sizeof(uint32_t)); - SDL_WaitEvent(&ctx->event); - switch (ctx->event.type) { - case SDL_QUIT: - ctx->quit = true; - break; - case SDL_MOUSEBUTTONDOWN: - if (ctx->xmpp_fd == -1) { - ctx->xmpp_fd = xmpp_connect(); - } else { - close(ctx->xmpp_fd); - ctx->xmpp_fd = -1; - printf("Closed XMPP connection.\n"); - } - break; - } - SDL_RenderClear(ctx->renderer); - SDL_RenderCopy(ctx->renderer, ctx->texture, NULL, NULL); - SDL_RenderPresent(ctx->renderer); - } - - SDL_DestroyWindow(ctx->window); - deinit(ctx); - -} diff --git a/gui.h b/gui.h deleted file mode 100644 index 331ad2c..0000000 --- a/gui.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * LimoX - The Linux on mobile XMPP chat client - * Copyright (C) 2022 xengineering - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -void gui_run(void); diff --git a/main.c b/main.c deleted file mode 100644 index bdce11b..0000000 --- a/main.c +++ /dev/null @@ -1,85 +0,0 @@ -/* - * LimoX - The Linux on mobile XMPP chat client - * Copyright (C) 2022 xengineering - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -#include -#include -#include - -#include "gui.h" - - -/* - * Configuration struct for all options - */ -struct Options { - bool unknown; - bool help; -}; - - -/* - * Parse command line arguments - */ -static void get_opts(struct Options *opts, int argc, char* argv[]) -{ - opts->unknown = false; - opts->help = false; - - int c; - while ((c=getopt(argc, argv, "h")) != -1) { - - switch (c) { - case 'h': - opts->help = true; - break; - default: - opts->unknown = true; - } - } -} - -/* - * Print help page to command line - */ -static void print_help(void) -{ - printf( - "The Linux on mobile XMPP (LimoX) client.\n" - "\n" - "Usage: limox [-h]\n" - "\n" - "Options:\n" - " -h print help page\n" - ); -} - -/* - * Run control flow of the application - */ -int main(int argc, char* argv[]) -{ - struct Options opts; - get_opts(&opts, argc, argv); - - if (opts.unknown || opts.help) { - print_help(); - } else { - gui_run(); - } -} diff --git a/main.go b/main.go new file mode 100644 index 0000000..2803e68 --- /dev/null +++ b/main.go @@ -0,0 +1,249 @@ +package main + +import ( + "image/color" + "log" + "net" + "os" + "reflect" + + "gioui.org/app" + "gioui.org/font/gofont" + "gioui.org/io/system" + "gioui.org/layout" + "gioui.org/op" + "gioui.org/text" + "gioui.org/unit" + "gioui.org/widget" + "gioui.org/widget/material" +) + +type XmppEvent uint8 + +const ( + XmppDisconnect XmppEvent = iota + XmppConnect +) + +type GuiEvent uint8 + +const ( + Disconnect GuiEvent = iota +) + +type LimoxState uint8 + +const ( + Disconnected LimoxState = iota + Connecting + Connected +) + +type Limox struct { + JidEditor widget.Editor + PwdEditor widget.Editor + MainButton widget.Clickable + XmppEvents chan any + GuiEvents chan GuiEvent + State LimoxState + Window *app.Window + Operations op.Ops + Theme *material.Theme +} + +func main() { + limox := NewLimox() + + go func() { + err := limox.run() + if err != nil { + log.Fatal(err) + } + os.Exit(0) + }() + + app.Main() +} + +func NewLimox() Limox { + return Limox{ + Window: app.NewWindow( + app.Title("LimoX"), + app.Size(unit.Dp(400), unit.Dp(600)), + ), + Operations: op.Ops{}, + Theme: material.NewTheme(gofont.Collection()), + XmppEvents: make(chan any), + GuiEvents: make(chan GuiEvent), + State: Disconnected, + } +} + +func (l *Limox) run() error { + for { + select { + case e := <-l.Window.Events(): + switch e := e.(type) { + case system.DestroyEvent: + return e.Err + case system.FrameEvent: + if l.MainButton.Clicked() { + l.buttonCallback() + } + l.draw(e) + } + case ev := <-l.XmppEvents: + switch ev.(type) { + case error: + log.Print(ev) + l.State = Disconnected + case XmppEvent: + switch ev { + case XmppDisconnect: + l.State = Disconnected + case XmppConnect: + l.State = Connected + default: + log.Fatalf("Unknown XmppEvent '%d'\n", ev) + } + default: + log.Fatalf("Unknown event type '%s'.\n", reflect.TypeOf(ev)) + } + } + } +} + +func (l *Limox) buttonCallback() { + switch l.State { + case Disconnected: + log.Println("Starting connection establishment") + go l.xmpp(l.JidEditor.Text(), l.PwdEditor.Text()) + l.State = Connecting + case Connecting: + log.Println("Aborted connection establishment") + l.State = Disconnected + case Connected: + log.Println("Disconnected") + l.GuiEvents <- Disconnect + l.State = Disconnected + } +} + +func (l *Limox) xmpp(jid string, pwd string) { + log.Printf("JID: '%s' PWD: '%s'\n", jid, pwd) + + domain := domainpart(jid) + log.Printf("Domain: '%s'\n", domain) + + tcpServer, err := net.ResolveTCPAddr("tcp", domain+":"+"5222") + if err != nil { + l.XmppEvents <- err + return + } + log.Printf("Server: %s\n", tcpServer) + + conn, err := net.DialTCP("tcp", nil, tcpServer) + if err != nil { + l.XmppEvents <- err + return + } + l.XmppEvents <- XmppConnect + + var closing bool = false + for { + ev := <-l.GuiEvents + switch ev { + case Disconnect: + closing = true + default: + log.Fatalf("Unknown GuiEvent '%d'!\n", ev) + } + if closing { + break + } + } + + conn.Close() + l.XmppEvents <- XmppDisconnect +} + +// domainpart extracts the domain name from a JID / XMPP address. See +// https://datatracker.ietf.org/doc/html/rfc7622#section-3.2 for details. +func domainpart(jid string) string { + list := []rune(jid) + + for i, v := range list { + if v == '/' { + list = list[:i] + break + } + } + + for i, v := range list { + if v == '@' { + list = list[i+1:] + break + } + } + + return string(list) +} + +func (l *Limox) draw(e system.FrameEvent) { + gtx := layout.NewContext(&l.Operations, e) + + flex := layout.Flex{ + Axis: layout.Vertical, + Spacing: layout.SpaceBetween, + } + + flex.Layout(gtx, + layout.Rigid( + func(gtx layout.Context) layout.Dimensions { + h2 := material.H2(l.Theme, "LimoX") + h2.Color = color.NRGBA{R: 20, G: 20, B: 20, A: 255} + h2.Alignment = text.Middle + return h2.Layout(gtx) + }, + ), + layout.Rigid( + func(gtx layout.Context) layout.Dimensions { + jid := material.Editor(l.Theme, &l.JidEditor, "user@example.com") + jid.Editor.Alignment = text.Middle + return jid.Layout(gtx) + }, + ), + layout.Rigid( + func(gtx layout.Context) layout.Dimensions { + pwd := material.Editor(l.Theme, &l.PwdEditor, "mySafePassword") + pwd.Editor.Alignment = text.Middle + pwd.Editor.Mask = '*' + return pwd.Layout(gtx) + }, + ), + layout.Rigid( + func(gtx layout.Context) layout.Dimensions { + btn := material.Button(l.Theme, &l.MainButton, + l.buttonLabel()) + return btn.Layout(gtx) + }, + ), + ) + + e.Frame(gtx.Ops) +} + +func (l *Limox) buttonLabel() string { + var label string + switch l.State { + case Disconnected: + label = "Connect" + case Connecting: + label = "Abort" + case Connected: + label = "Disconnect" + default: + log.Fatalf("Unknown Limox state '%d'\n", l.State) + } + return label +} diff --git a/tests/get_domainpart.c b/tests/get_domainpart.c deleted file mode 100644 index 6601330..0000000 --- a/tests/get_domainpart.c +++ /dev/null @@ -1,40 +0,0 @@ -/* - * LimoX - The Linux on mobile XMPP chat client - * Copyright (C) 2022 xengineering - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -#include -#include - -#include "xmpp.h" - - -int main(void) -{ - char *input = "test@example.org/3f"; - const char *expected = "example.org"; - - char *result = get_domainpart(input); - - if (strcmp(result, expected) == 0) { - printf("Unit test passed - \033[;32mok\033[0m\n"); - return 0; - } else { - printf("'%s' != '%s' - \033[;31mfailed\033[0m\n", result, expected); - return 1; - } -} diff --git a/tools/lint.sh b/tools/lint.sh deleted file mode 100755 index 5264112..0000000 --- a/tools/lint.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/sh - - -if ! command -v splint 2> /dev/null -then - echo "Please install splint (http://splint.org/)!" - exit 1 -fi - -find . -type f -iname '*.c' | xargs splint diff --git a/xmpp.c b/xmpp.c deleted file mode 100644 index 18bb6c5..0000000 --- a/xmpp.c +++ /dev/null @@ -1,200 +0,0 @@ -/* - * LimoX - The Linux on mobile XMPP chat client - * Copyright (C) 2022 xengineering - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -/* - * Get the domainpart of the Jabber ID (JID) - * - * See https://datatracker.ietf.org/doc/html/rfc7622#section-3.2 for details. - */ -char *get_domainpart(char *jid) -{ - int start = 0; // inclusive index - int stop = strlen(jid); // exclusive index - - for(int i=0; iai_next) { - if (p->ai_family == AF_INET) { - struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; - addr = &(ipv4->sin_addr); - inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr); - printf("Got compatible IPv4 address %s from DNS.\n", ipstr); - return p; - } else if (p->ai_family == AF_INET6) { - printf("Ignored IPv6 DNS entry which is not supported.\n"); - } else { - printf("Ignored unknown addrinfo address type.\n"); - } - } - printf("No suitable IP address found via DNS!\n"); - return NULL; -} - -/* - * Start XMPP stream via the given socket filedescriptor - * - * Returns 0 in case of success and -1 otherwise. - */ -int start_stream(int sock_fd, char *jid, char *domainpart) -{ - const char *tmpl = ""; - - char *msg; - asprintf(&msg, tmpl, jid, domainpart); - int len = strlen(msg); - - int sent = send(sock_fd, msg, len, 0); - if (sent == len) { - printf(">>> %s\n", msg); - return 0; - } else if (sent == 0) { - printf("Failed to send any bytes to server!\n"); - return -1; - } else { - printf("Could just send %d bytes of %d bytes to server!\n", sent, len); - return -1; - } -} - -/* - * Get JID and password and return true on success - */ -bool get_credentials(char **jid, char **pwd) -{ - if (getenv("LIMOX_USER") == NULL || getenv("LIMOX_PWD") == NULL) { - printf("Failed to connect.\n"); - printf("Please set the LIMOX_USER and LIMOX_PWD shell variables.\n"); - return false; - } - - *jid = getenv("LIMOX_USER"); - *pwd = getenv("LIMOX_PWD"); - - return true; -} - -/* - * Initialize the network connection to the XMPP server and return socket fd - * - * TODO: Error handling is missing. - */ -int xmpp_connect(void) -{ - printf("net_init()\n"); - - int sock = -1; - - char *jid; - char *pwd; - if (!get_credentials(&jid, &pwd)) { - return sock; - } - printf("Trying to connect as '%s' with '%s'.\n", jid, pwd); - - char *domain = get_domainpart(jid); - printf("Domainpart is '%s'.\n", domain); - - struct addrinfo *addr = resolve_dns(domain); - - sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); - if (sock == -1) { - printf("Failed to get socket from OS!."); - return sock; - } else { - printf("Got socket number %d from OS.\n", sock); - } - - if (connect(sock, addr->ai_addr, addr->ai_addrlen) == -1) { - printf("Failed to connect!\n"); - return sock; - } else { - printf("Successfully connected.\n"); - } - - if (start_stream(sock, jid, domain) == -1) { - printf("Failed to init stream!\n"); - return sock; - } - printf("Stream init sent.\n"); - - return sock; -} diff --git a/xmpp.h b/xmpp.h deleted file mode 100644 index 622881c..0000000 --- a/xmpp.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - * LimoX - The Linux on mobile XMPP chat client - * Copyright (C) 2022 xengineering - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -int xmpp_connect(void); -char *get_domainpart(char *jid); -- cgit v1.2.3-70-g09d2