Skip to content

Commit

Permalink
all: bump to go1.17
Browse files Browse the repository at this point in the history
- Use runtime/cgo
- Improve error message
- Retry on Linux
- Update workflow

Fixes #4
  • Loading branch information
changkun committed Aug 29, 2021
1 parent 2997bd2 commit d70377e
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 247 deletions.
39 changes: 22 additions & 17 deletions .github/workflows/hotkey.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,40 @@ on:

jobs:
platform_test:

env:
DISPLAY: ':0.0'
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]

steps:

- name: install xvfb libx11-dev
- name: Install and run dependencies (xvfb libx11-dev)
if: ${{ runner.os == 'Linux' }}
run: |
sudo apt update
sudo apt install -y xvfb libx11-dev
if: ${{ runner.os == 'Linux' }}

sudo apt install -y xvfb libx11-dev libegl1-mesa-dev libgles2-mesa-dev
Xvfb :0 -screen 0 1024x768x24 > /dev/null 2>&1 &
# Wait for Xvfb
MAX_ATTEMPTS=120 # About 60 seconds
COUNT=0
echo -n "Waiting for Xvfb to be ready..."
while ! xdpyinfo -display "${DISPLAY}" >/dev/null 2>&1; do
echo -n "."
sleep 0.50s
COUNT=$(( COUNT + 1 ))
if [ "${COUNT}" -ge "${MAX_ATTEMPTS}" ]; then
echo " Gave up waiting for X server on ${DISPLAY}"
exit 1
fi
done
echo "Done - Xvfb is ready!"
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
stable: 'false'
go-version: '1.16.0'

- name: TestLinux
run: |
Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
export DISPLAY=:99.0
sleep 5s
go test -v -covermode=atomic ./...
if: ${{ runner.os == 'Linux' }}
go-version: '1.17.x'

- name: TestOthers
- name: Run Tests
run: |
go test -v -covermode=atomic ./...
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module golang.design/x/hotkey

go 1.16
go 1.17

require (
golang.design/x/mainthread v0.2.1
Expand Down
17 changes: 8 additions & 9 deletions hotkey_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,37 @@
// Written by Changkun Ou <changkun.de>

//go:build darwin
// +build darwin

package hotkey

/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa -framework Carbon
#include <stdint.h>
#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>
extern void hotkeyCallback(unsigned long long handle);
extern void hotkeyCallback(uintptr_t handle);
int registerHotKey(int mod, int key, unsigned long long handle);
int registerHotKey(int mod, int key, uintptr_t handle);
void runApp();
void stopApp();
*/
import "C"
import (
"context"
"errors"

"golang.design/x/hotkey/internal/cgo"
"runtime/cgo"
)

// handle handles the hotkey event loop.
func (hk *Hotkey) handle(ctx context.Context) {
// KNOWN ISSUE: This application never ends.
// Note: This call never returns.
C.runApp()
}

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

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

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

//export hotkeyCallback
func hotkeyCallback(h C.ulonglong) {
func hotkeyCallback(h uintptr) {
ch := cgo.Handle(h).Value().(chan<- Event)
ch <- Event{}
}
Expand Down
8 changes: 4 additions & 4 deletions hotkey_darwin.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
// Written by Changkun Ou <changkun.de>

//go:build darwin
// +build darwin

#include <stdint.h>
#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>

extern void hotkeyCallback(unsigned long long handle);
extern void hotkeyCallback(uintptr_t handle);

static OSStatus
eventHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData) {
EventHotKeyID k;
GetEventParameter(theEvent, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(k), NULL, &k);
hotkeyCallback((unsigned long long)k.id); // use id as handle
hotkeyCallback((uintptr_t)k.id); // use id as handle
return noErr;
}

// registerHotkeyWithCallback registers a global system hotkey for callbacks.
int registerHotKey(int mod, int key, unsigned long long handle) {
int registerHotKey(int mod, int key, uintptr_t handle) {
EventTypeSpec eventType;
eventType.eventClass = kEventClassKeyboard;
eventType.eventKind = kEventHotKeyPressed;
Expand Down
1 change: 0 additions & 1 deletion hotkey_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Written by Changkun Ou <changkun.de>

//go:build darwin
// +build darwin

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 @@ -5,7 +5,6 @@
// Written by Changkun Ou <changkun.de>

//go:build linux
// +build linux

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

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;
}
Expand Down
16 changes: 14 additions & 2 deletions hotkey_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Written by Changkun Ou <changkun.de>

//go:build linux
// +build linux

package hotkey

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

const errmsg = `Failed to initialize the X11 display, and the clipboard package
will not work properly. Install the following dependency may help:
apt install -y libx11-dev
If the clipboard package is in an environment without a frame buffer,
such as a cloud server, it may also be necessary to install xvfb:
apt install -y xvfb
and initialize a virtual frame buffer:
Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
export DISPLAY=:99.0
Then this package should be ready to use.
`

func init() {
if C.displayTest() != 0 {
panic("cannot use hotkey package")
panic(errmsg)
}
}

Expand Down
1 change: 0 additions & 1 deletion hotkey_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Written by Changkun Ou <changkun.de>

//go:build linux
// +build linux

package hotkey_test

Expand Down
1 change: 0 additions & 1 deletion hotkey_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Written by Changkun Ou <changkun.de>

//go:build windows
// +build windows

package hotkey

Expand Down
1 change: 0 additions & 1 deletion hotkey_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Written by Changkun Ou <changkun.de>

//go:build windows
// +build windows

package hotkey_test

Expand Down
103 changes: 0 additions & 103 deletions internal/cgo/handle.go

This file was deleted.

Loading

0 comments on commit d70377e

Please sign in to comment.