Skip to content

Commit

Permalink
all: improve error message when CGO_ENABLED=0
Browse files Browse the repository at this point in the history
  • Loading branch information
changkun committed Sep 20, 2021
1 parent d70377e commit 9cf98f7
Show file tree
Hide file tree
Showing 14 changed files with 402 additions and 9 deletions.
15 changes: 13 additions & 2 deletions .github/workflows/hotkey.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ jobs:
stable: 'false'
go-version: '1.17.x'

- name: Run Tests
- name: Run Tests with CGO_ENABLED=1
if: ${{ runner.os == 'Linux' || runner.os == 'macOS'}}
run: |
go test -v -covermode=atomic ./...
CGO_ENABLED=1 go test -v -covermode=atomic .
- name: Run Tests with CGO_ENABLED=0
if: ${{ runner.os == 'Linux' || runner.os == 'macOS'}}
run: |
CGO_ENABLED=0 go test -v -covermode=atomic .
- name: Run Tests on Windows
if: ${{ runner.os == 'Windows'}}
run: |
go test -v -covermode=atomic .
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module golang.design/x/hotkey
go 1.17

require (
golang.design/x/mainthread v0.2.1
golang.design/x/mainthread v0.3.0
golang.org/x/sys v0.0.0-20210122093101-04d7465088b8 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
golang.design/x/mainthread v0.2.1 h1:IUGVW1acDfKoQtFeeS/RD/YYiKK8jxwkJXIQuKuL+ig=
golang.design/x/mainthread v0.2.1/go.mod h1:vYX7cF2b3pTJMGM/hc13NmN6kblKnf4/IyvHeu259L0=
golang.design/x/mainthread v0.3.0 h1:UwFus0lcPodNpMOGoQMe87jSFwbSsEY//CA7yVmu4j8=
golang.design/x/mainthread v0.3.0/go.mod h1:vYX7cF2b3pTJMGM/hc13NmN6kblKnf4/IyvHeu259L0=
golang.org/x/sys v0.0.0-20201022201747-fb209a7c41cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210122093101-04d7465088b8 h1:de2yTH1xuxjmGB7i6Z5o2z3RCHVa0XlpSZzjd8Fe6bE=
golang.org/x/sys v0.0.0-20210122093101-04d7465088b8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1 change: 1 addition & 0 deletions hotkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func newEventChan() (chan<- Event, <-chan Event) {
for len(q) > 0 {
select {
case out <- q[0]:
q[0] = Event{}
q = q[1:]
case e, ok := <-in:
if ok {
Expand Down
2 changes: 1 addition & 1 deletion hotkey_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//
// Written by Changkun Ou <changkun.de>

//go:build darwin
//go:build darwin && cgo

package hotkey_test

Expand Down
8 changes: 6 additions & 2 deletions hotkey_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
#include <X11/Xutil.h>

int displayTest() {
Display* d = XOpenDisplay(0);
Display* d = NULL;
for (int i = 0; i < 42; i++) {
d = XOpenDisplay(0);
if (d == NULL) continue;
break;
}
if (d == NULL) {
return -1;
}
XCloseDisplay(d);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion hotkey_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//
// Written by Changkun Ou <changkun.de>

//go:build linux
//go:build linux && cgo

package hotkey_test

Expand Down
31 changes: 31 additions & 0 deletions hotkey_nocgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2021 The golang.design Initiative Authors.
// All rights reserved. Use of this source code is governed
// by a MIT license that can be found in the LICENSE file.
//
// Written by Changkun Ou <changkun.de>

//go:build !windows && !cgo

package hotkey

import "context"

// Modifier represents a modifier
type Modifier uint32

// Key represents a key.
type Key uint8

func (hk *Hotkey) register() error {
panic("hotkey: cannot use when CGO_ENABLED=0")
}

// unregister deregisteres a system hotkey.
func (hk *Hotkey) unregister() {
panic("hotkey: cannot use when CGO_ENABLED=0")
}

// handle handles the hotkey event loop.
func (hk *Hotkey) handle(ctx context.Context) {
panic("hotkey: cannot use when CGO_ENABLED=0")
}
29 changes: 29 additions & 0 deletions hotkey_nocgo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2021 The golang.design Initiative Authors.
// All rights reserved. Use of this source code is governed
// by a MIT license that can be found in the LICENSE file.
//
// Written by Changkun Ou <changkun.de>

//go:build (linux || darwin) && !cgo

package hotkey_test

import (
"testing"

"golang.design/x/hotkey"
)

// TestHotkey should always run success.
// This is a test to run and for manually testing, registered combination:
// Ctrl+Alt+A (Ctrl+Mod2+Mod4+A on Linux)
func TestHotkey(t *testing.T) {
defer func() {
if r := recover(); r != nil {
return
}
t.Fatalf("expect to fail when CGO_ENABLED=0")
}()

hotkey.Register([]hotkey.Modifier{}, hotkey.Key(0))
}
15 changes: 15 additions & 0 deletions vendor/golang.design/x/mainthread/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
21 changes: 21 additions & 0 deletions vendor/golang.design/x/mainthread/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Changkun Ou <contact@changkun.de>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
106 changes: 106 additions & 0 deletions vendor/golang.design/x/mainthread/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# mainthread [![PkgGoDev](https://pkg.go.dev/badge/golang.design/x/mainthread)](https://pkg.go.dev/golang.design/x/mainthread) ![mainthread](https://github.com/golang-design/mainthread/workflows/mainthread/badge.svg?branch=main) ![](https://changkun.de/urlstat?mode=github&repo=golang-design/mainthread)

schedule functions to run on the main thread

```go
import "golang.design/x/mainthread"
```

## Features

- Main thread scheduling
- Schedule functions without memory allocation

## API Usage

Package mainthread offers facilities to schedule functions
on the main thread. To use this package properly, one must
call `mainthread.Init` from the main package. For example:

```go
package main

import "golang.design/x/mainthread"

func main() { mainthread.Init(fn) }

// fn is the actual main function
func fn() {
// ... do stuff ...

// mainthread.Call returns when f1 returns. Note that if f1 blocks
// it will also block the execution of any subsequent calls on the
// main thread.
mainthread.Call(f1)

// ... do stuff ...


// mainthread.Go returns immediately and f2 is scheduled to be
// executed in the future.
mainthread.Go(f2)

// ... do stuff ...
}

func f1() { ... }
func f2() { ... }
```

If the given function triggers a panic, and called via `mainthread.Call`,
then the panic will be propagated to the same goroutine. One can capture
that panic, when possible:

```go
defer func() {
if r := recover(); r != nil {
println(r)
}
}()

mainthread.Call(func() { ... }) // if panic
```

If the given function triggers a panic, and called via `mainthread.Go`,
then the panic will be cached internally, until a call to the `Error()` method:

```go
mainthread.Go(func() { ... }) // if panics

// ... do stuff ...

if err := mainthread.Error(); err != nil { // can be captured here.
println(err)
}
```

Note that a panic happens before `mainthread.Error()` returning the
panicked error. If one needs to guarantee `mainthread.Error()` indeed
captured the panic, a dummy function can be used as synchornization:

```go
mainthread.Go(func() { panic("die") }) // if panics
mainthread.Call(func() {}) // for execution synchronization
err := mainthread.Error() // err must be non-nil
```


It is possible to cache up to a maximum of 42 panicked errors.
More errors are ignored.

## When do you need this package?

Read this to learn more about the design purpose of this package:
https://golang.design/research/zero-alloc-call-sched/

## Who is using this package?

The initial purpose of building this package is to support writing
graphical applications in Go. To know projects that are using this
package, check our [wiki](https://github.com/golang-design/mainthread/wiki)
page.


## License

MIT | &copy; 2021 The golang.design Initiative Authors, written by [Changkun Ou](https://changkun.de).
Loading

0 comments on commit 9cf98f7

Please sign in to comment.