Skip to content

Commit d70377e

Browse files
committed
all: bump to go1.17
- Use runtime/cgo - Improve error message - Retry on Linux - Update workflow Fixes #4
1 parent 2997bd2 commit d70377e

File tree

12 files changed

+55
-247
lines changed

12 files changed

+55
-247
lines changed

.github/workflows/hotkey.yml

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,40 @@ on:
1414

1515
jobs:
1616
platform_test:
17-
17+
env:
18+
DISPLAY: ':0.0'
1819
runs-on: ${{ matrix.os }}
1920
strategy:
2021
fail-fast: false
2122
matrix:
2223
os: [ubuntu-latest, macos-latest, windows-latest]
23-
2424
steps:
25-
26-
- name: install xvfb libx11-dev
25+
- name: Install and run dependencies (xvfb libx11-dev)
26+
if: ${{ runner.os == 'Linux' }}
2727
run: |
2828
sudo apt update
29-
sudo apt install -y xvfb libx11-dev
30-
if: ${{ runner.os == 'Linux' }}
31-
29+
sudo apt install -y xvfb libx11-dev libegl1-mesa-dev libgles2-mesa-dev
30+
Xvfb :0 -screen 0 1024x768x24 > /dev/null 2>&1 &
31+
# Wait for Xvfb
32+
MAX_ATTEMPTS=120 # About 60 seconds
33+
COUNT=0
34+
echo -n "Waiting for Xvfb to be ready..."
35+
while ! xdpyinfo -display "${DISPLAY}" >/dev/null 2>&1; do
36+
echo -n "."
37+
sleep 0.50s
38+
COUNT=$(( COUNT + 1 ))
39+
if [ "${COUNT}" -ge "${MAX_ATTEMPTS}" ]; then
40+
echo " Gave up waiting for X server on ${DISPLAY}"
41+
exit 1
42+
fi
43+
done
44+
echo "Done - Xvfb is ready!"
3245
- uses: actions/checkout@v2
3346
- uses: actions/setup-go@v2
3447
with:
3548
stable: 'false'
36-
go-version: '1.16.0'
37-
38-
- name: TestLinux
39-
run: |
40-
Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
41-
export DISPLAY=:99.0
42-
sleep 5s
43-
go test -v -covermode=atomic ./...
44-
if: ${{ runner.os == 'Linux' }}
49+
go-version: '1.17.x'
4550

46-
- name: TestOthers
51+
- name: Run Tests
4752
run: |
4853
go test -v -covermode=atomic ./...

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module golang.design/x/hotkey
22

3-
go 1.16
3+
go 1.17
44

55
require (
66
golang.design/x/mainthread v0.2.1

hotkey_darwin.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,37 @@
55
// Written by Changkun Ou <changkun.de>
66

77
//go:build darwin
8-
// +build darwin
98

109
package hotkey
1110

1211
/*
1312
#cgo CFLAGS: -x objective-c
1413
#cgo LDFLAGS: -framework Cocoa -framework Carbon
14+
#include <stdint.h>
1515
#import <Cocoa/Cocoa.h>
1616
#import <Carbon/Carbon.h>
1717
18-
extern void hotkeyCallback(unsigned long long handle);
18+
extern void hotkeyCallback(uintptr_t handle);
1919
20-
int registerHotKey(int mod, int key, unsigned long long handle);
20+
int registerHotKey(int mod, int key, uintptr_t handle);
2121
void runApp();
2222
void stopApp();
2323
*/
2424
import "C"
2525
import (
2626
"context"
2727
"errors"
28-
29-
"golang.design/x/hotkey/internal/cgo"
28+
"runtime/cgo"
3029
)
3130

3231
// handle handles the hotkey event loop.
3332
func (hk *Hotkey) handle(ctx context.Context) {
34-
// KNOWN ISSUE: This application never ends.
33+
// Note: This call never returns.
3534
C.runApp()
3635
}
3736

3837
func (hk *Hotkey) register() error {
39-
// KNOWN ISSUE: we use handle number as hotkey id in the C side.
38+
// Note: we use handle number as hotkey id in the C side.
4039
// A cgo handle could ran out of space, but since in hotkey purpose
4140
// we won't have that much number of hotkeys. So this should be fine.
4241

@@ -46,7 +45,7 @@ func (hk *Hotkey) register() error {
4645
mod += m
4746
}
4847

49-
ret := C.registerHotKey(C.int(mod), C.int(hk.key), C.ulonglong(h))
48+
ret := C.registerHotKey(C.int(mod), C.int(hk.key), C.uintptr_t(h))
5049
if ret == C.int(-1) {
5150
return errors.New("register failed")
5251
}
@@ -61,7 +60,7 @@ func (hk *Hotkey) unregister() {
6160
}
6261

6362
//export hotkeyCallback
64-
func hotkeyCallback(h C.ulonglong) {
63+
func hotkeyCallback(h uintptr) {
6564
ch := cgo.Handle(h).Value().(chan<- Event)
6665
ch <- Event{}
6766
}

hotkey_darwin.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55
// Written by Changkun Ou <changkun.de>
66

77
//go:build darwin
8-
// +build darwin
98

9+
#include <stdint.h>
1010
#import <Cocoa/Cocoa.h>
1111
#import <Carbon/Carbon.h>
1212

13-
extern void hotkeyCallback(unsigned long long handle);
13+
extern void hotkeyCallback(uintptr_t handle);
1414

1515
static OSStatus
1616
eventHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData) {
1717
EventHotKeyID k;
1818
GetEventParameter(theEvent, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(k), NULL, &k);
19-
hotkeyCallback((unsigned long long)k.id); // use id as handle
19+
hotkeyCallback((uintptr_t)k.id); // use id as handle
2020
return noErr;
2121
}
2222

2323
// registerHotkeyWithCallback registers a global system hotkey for callbacks.
24-
int registerHotKey(int mod, int key, unsigned long long handle) {
24+
int registerHotKey(int mod, int key, uintptr_t handle) {
2525
EventTypeSpec eventType;
2626
eventType.eventClass = kEventClassKeyboard;
2727
eventType.eventKind = kEventHotKeyPressed;

hotkey_darwin_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Written by Changkun Ou <changkun.de>
66

77
//go:build darwin
8-
// +build darwin
98

109
package hotkey_test
1110

hotkey_linux.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Written by Changkun Ou <changkun.de>
66

77
//go:build linux
8-
// +build linux
98

109
#include <stdio.h>
1110
#include <X11/Xlib.h>
@@ -44,7 +43,12 @@ int waitHotkey(unsigned int mod, int key) {
4443
// FIXME: handle registered hotkey properly.
4544
// XSetErrorHandler(handleErrors);
4645

47-
Display* d = XOpenDisplay(0);
46+
Display* d = NULL;
47+
for (int i = 0; i < 42; i++) {
48+
d = XOpenDisplay(0);
49+
if (d == NULL) continue;
50+
break;
51+
}
4852
if (d == NULL) {
4953
return -1;
5054
}

hotkey_linux.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Written by Changkun Ou <changkun.de>
66

77
//go:build linux
8-
// +build linux
98

109
package hotkey
1110

@@ -18,9 +17,22 @@ int waitHotkey(unsigned int mod, int key);
1817
import "C"
1918
import "context"
2019

20+
const errmsg = `Failed to initialize the X11 display, and the clipboard package
21+
will not work properly. Install the following dependency may help:
22+
23+
apt install -y libx11-dev
24+
If the clipboard package is in an environment without a frame buffer,
25+
such as a cloud server, it may also be necessary to install xvfb:
26+
apt install -y xvfb
27+
and initialize a virtual frame buffer:
28+
Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
29+
export DISPLAY=:99.0
30+
Then this package should be ready to use.
31+
`
32+
2133
func init() {
2234
if C.displayTest() != 0 {
23-
panic("cannot use hotkey package")
35+
panic(errmsg)
2436
}
2537
}
2638

hotkey_linux_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Written by Changkun Ou <changkun.de>
66

77
//go:build linux
8-
// +build linux
98

109
package hotkey_test
1110

hotkey_windows.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Written by Changkun Ou <changkun.de>
66

77
//go:build windows
8-
// +build windows
98

109
package hotkey
1110

hotkey_windows_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Written by Changkun Ou <changkun.de>
66

77
//go:build windows
8-
// +build windows
98

109
package hotkey_test
1110

0 commit comments

Comments
 (0)