mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
move server to server directory.
This commit is contained in:
2498
server/pkg/xorg/keysymdef.go
Normal file
2498
server/pkg/xorg/keysymdef.go
Normal file
File diff suppressed because it is too large
Load Diff
6
server/pkg/xorg/keysymdef.sh
Executable file
6
server/pkg/xorg/keysymdef.sh
Executable file
@ -0,0 +1,6 @@
|
||||
#!/bin/sh
|
||||
|
||||
wget https://cgit.freedesktop.org/xorg/proto/x11proto/plain/keysymdef.h
|
||||
sed -i -E 's/\#define (XK_[a-zA-Z_0-9]+\s+)(0x[0-9a-f]+)/const \1 = \2/g' keysymdef.h
|
||||
sed -i -E 's/^\#/\/\//g' keysymdef.h
|
||||
echo "package xorg" | cat - keysymdef.h > keysymdef.go && rm keysymdef.h
|
407
server/pkg/xorg/xorg.c
Normal file
407
server/pkg/xorg/xorg.c
Normal file
@ -0,0 +1,407 @@
|
||||
#include "xorg.h"
|
||||
|
||||
static Display *DISPLAY = NULL;
|
||||
|
||||
Display *getXDisplay(void) {
|
||||
return DISPLAY;
|
||||
}
|
||||
|
||||
int XDisplayOpen(char *name) {
|
||||
DISPLAY = XOpenDisplay(name);
|
||||
return DISPLAY == NULL;
|
||||
}
|
||||
|
||||
void XDisplayClose(void) {
|
||||
XCloseDisplay(DISPLAY);
|
||||
}
|
||||
|
||||
void XMove(int x, int y) {
|
||||
Display *display = getXDisplay();
|
||||
XWarpPointer(display, None, DefaultRootWindow(display), 0, 0, 0, 0, x, y);
|
||||
XSync(display, 0);
|
||||
}
|
||||
|
||||
void XCursorPosition(int *x, int *y) {
|
||||
Display *display = getXDisplay();
|
||||
Window root = DefaultRootWindow(display);
|
||||
Window window;
|
||||
int i;
|
||||
unsigned mask;
|
||||
XQueryPointer(display, root, &root, &window, x, y, &i, &i, &mask);
|
||||
}
|
||||
|
||||
void XScroll(int deltaX, int deltaY) {
|
||||
Display *display = getXDisplay();
|
||||
|
||||
int ydir;
|
||||
if (deltaY > 0) {
|
||||
ydir = 4; // button 4 is up
|
||||
} else {
|
||||
ydir = 5; // button 5 is down
|
||||
}
|
||||
|
||||
int xdir;
|
||||
if (deltaX > 0) {
|
||||
xdir = 6; // button 6 is right
|
||||
} else {
|
||||
xdir = 7; // button 7 is left
|
||||
}
|
||||
|
||||
for (int i = 0; i < abs(deltaY); i++) {
|
||||
XTestFakeButtonEvent(display, ydir, 1, CurrentTime);
|
||||
XTestFakeButtonEvent(display, ydir, 0, CurrentTime);
|
||||
}
|
||||
|
||||
for (int i = 0; i < abs(deltaX); i++) {
|
||||
XTestFakeButtonEvent(display, xdir, 1, CurrentTime);
|
||||
XTestFakeButtonEvent(display, xdir, 0, CurrentTime);
|
||||
}
|
||||
|
||||
XSync(display, 0);
|
||||
}
|
||||
|
||||
void XButton(unsigned int button, int down) {
|
||||
if (button == 0)
|
||||
return;
|
||||
|
||||
Display *display = getXDisplay();
|
||||
XTestFakeButtonEvent(display, button, down, CurrentTime);
|
||||
XSync(display, 0);
|
||||
}
|
||||
|
||||
static xkeyentry_t *xKeysHead = NULL;
|
||||
|
||||
// add keycode->keysym mapping to list
|
||||
void XKeyEntryAdd(KeySym keysym, KeyCode keycode) {
|
||||
xkeyentry_t *entry = (xkeyentry_t *) malloc(sizeof(xkeyentry_t));
|
||||
if (entry == NULL)
|
||||
return;
|
||||
|
||||
entry->keysym = keysym;
|
||||
entry->keycode = keycode;
|
||||
entry->next = xKeysHead;
|
||||
xKeysHead = entry;
|
||||
}
|
||||
|
||||
// get keycode for keysym from list
|
||||
KeyCode XKeyEntryGet(KeySym keysym) {
|
||||
xkeyentry_t *prev = NULL;
|
||||
xkeyentry_t *curr = xKeysHead;
|
||||
|
||||
KeyCode keycode = 0;
|
||||
while (curr != NULL) {
|
||||
if (curr->keysym == keysym) {
|
||||
keycode = curr->keycode;
|
||||
|
||||
if (prev == NULL) {
|
||||
xKeysHead = curr->next;
|
||||
} else {
|
||||
prev->next = curr->next;
|
||||
}
|
||||
|
||||
free(curr);
|
||||
return keycode;
|
||||
}
|
||||
|
||||
prev = curr;
|
||||
curr = curr->next;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// From https://github.com/TigerVNC/tigervnc/blob/0946e298075f8f7b6d63e552297a787c5f84d27c/unix/x0vncserver/XDesktop.cxx#L343-L379
|
||||
KeyCode XkbKeysymToKeycode(Display* dpy, KeySym keysym) {
|
||||
XkbDescPtr xkb;
|
||||
XkbStateRec state;
|
||||
unsigned int mods;
|
||||
unsigned keycode;
|
||||
|
||||
xkb = XkbGetMap(dpy, XkbAllComponentsMask, XkbUseCoreKbd);
|
||||
if (!xkb)
|
||||
return 0;
|
||||
|
||||
XkbGetState(dpy, XkbUseCoreKbd, &state);
|
||||
// XkbStateFieldFromRec() doesn't work properly because
|
||||
// state.lookup_mods isn't properly updated, so we do this manually
|
||||
mods = XkbBuildCoreState(XkbStateMods(&state), state.group);
|
||||
|
||||
for (keycode = xkb->min_key_code;
|
||||
keycode <= xkb->max_key_code;
|
||||
keycode++) {
|
||||
KeySym cursym;
|
||||
unsigned int out_mods;
|
||||
XkbTranslateKeyCode(xkb, keycode, mods, &out_mods, &cursym);
|
||||
if (cursym == keysym)
|
||||
break;
|
||||
}
|
||||
|
||||
if (keycode > xkb->max_key_code)
|
||||
keycode = 0;
|
||||
|
||||
XkbFreeKeyboard(xkb, XkbAllComponentsMask, True);
|
||||
|
||||
// Shift+Tab is usually ISO_Left_Tab, but RFB hides this fact. Do
|
||||
// another attempt if we failed the initial lookup
|
||||
if ((keycode == 0) && (keysym == XK_Tab) && (mods & ShiftMask))
|
||||
return XkbKeysymToKeycode(dpy, XK_ISO_Left_Tab);
|
||||
|
||||
return keycode;
|
||||
}
|
||||
|
||||
// From https://github.com/TigerVNC/tigervnc/blob/a434ef3377943e89165ac13c537cd0f28be97f84/unix/x0vncserver/XDesktop.cxx#L401-L453
|
||||
KeyCode XkbAddKeyKeysym(Display* dpy, KeySym keysym) {
|
||||
int types[1];
|
||||
unsigned int key;
|
||||
XkbDescPtr xkb;
|
||||
XkbMapChangesRec changes;
|
||||
KeySym *syms;
|
||||
KeySym upper, lower;
|
||||
|
||||
xkb = XkbGetMap(dpy, XkbAllComponentsMask, XkbUseCoreKbd);
|
||||
|
||||
if (!xkb)
|
||||
return 0;
|
||||
|
||||
for (key = xkb->max_key_code; key >= xkb->min_key_code; key--) {
|
||||
if (XkbKeyNumGroups(xkb, key) == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
// no free keycodes
|
||||
if (key < xkb->min_key_code)
|
||||
return 0;
|
||||
|
||||
// assign empty structure
|
||||
changes = *(XkbMapChangesRec *) malloc(sizeof(XkbMapChangesRec));
|
||||
for (int i = 0; i < sizeof(changes); i++) ((char *) &changes)[i] = 0;
|
||||
|
||||
XConvertCase(keysym, &lower, &upper);
|
||||
|
||||
if (upper == lower)
|
||||
types[XkbGroup1Index] = XkbOneLevelIndex;
|
||||
else
|
||||
types[XkbGroup1Index] = XkbAlphabeticIndex;
|
||||
|
||||
XkbChangeTypesOfKey(xkb, key, 1, XkbGroup1Mask, types, &changes);
|
||||
|
||||
syms = XkbKeySymsPtr(xkb,key);
|
||||
if (upper == lower)
|
||||
syms[0] = keysym;
|
||||
else {
|
||||
syms[0] = lower;
|
||||
syms[1] = upper;
|
||||
}
|
||||
|
||||
changes.changed |= XkbKeySymsMask;
|
||||
changes.first_key_sym = key;
|
||||
changes.num_key_syms = 1;
|
||||
|
||||
if (XkbChangeMap(dpy, xkb, &changes)) {
|
||||
return key;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void XKey(KeySym keysym, int down) {
|
||||
if (keysym == 0)
|
||||
return;
|
||||
|
||||
Display *display = getXDisplay();
|
||||
KeyCode keycode = 0;
|
||||
|
||||
if (!down)
|
||||
keycode = XKeyEntryGet(keysym);
|
||||
|
||||
// Try to get keysyms from existing keycodes
|
||||
if (keycode == 0)
|
||||
keycode = XkbKeysymToKeycode(display, keysym);
|
||||
|
||||
// Map non-existing keysyms to new keycodes
|
||||
if (keycode == 0)
|
||||
keycode = XkbAddKeyKeysym(display, keysym);
|
||||
|
||||
if (down)
|
||||
XKeyEntryAdd(keysym, keycode);
|
||||
|
||||
XTestFakeKeyEvent(display, keycode, down, CurrentTime);
|
||||
XSync(display, 0);
|
||||
}
|
||||
|
||||
Status XSetScreenConfiguration(int width, int height, short rate) {
|
||||
Display *display = getXDisplay();
|
||||
Window root = DefaultRootWindow(display);
|
||||
XRRScreenConfiguration *conf = XRRGetScreenInfo(display, root);
|
||||
|
||||
XRRScreenSize *xrrs;
|
||||
int num_sizes;
|
||||
xrrs = XRRConfigSizes(conf, &num_sizes);
|
||||
|
||||
int size_index = -1;
|
||||
for (int i = 0; i < num_sizes; i++) {
|
||||
if (xrrs[i].width == width && xrrs[i].height == height) {
|
||||
size_index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// if we cannot find the size
|
||||
if (size_index == -1) {
|
||||
return RRSetConfigFailed;
|
||||
}
|
||||
|
||||
Status status;
|
||||
status = XRRSetScreenConfigAndRate(display, conf, root, size_index, RR_Rotate_0, rate, CurrentTime);
|
||||
|
||||
XRRFreeScreenConfigInfo(conf);
|
||||
return status;
|
||||
}
|
||||
|
||||
void XGetScreenConfiguration(int *width, int *height, short *rate) {
|
||||
Display *display = getXDisplay();
|
||||
Window root = DefaultRootWindow(display);
|
||||
XRRScreenConfiguration *conf = XRRGetScreenInfo(display, root);
|
||||
|
||||
Rotation current_rotation;
|
||||
SizeID current_size_id = XRRConfigCurrentConfiguration(conf, ¤t_rotation);
|
||||
|
||||
XRRScreenSize *xrrs;
|
||||
int num_sizes;
|
||||
xrrs = XRRConfigSizes(conf, &num_sizes);
|
||||
|
||||
// if we cannot find the size
|
||||
if (current_size_id >= num_sizes) {
|
||||
return;
|
||||
}
|
||||
|
||||
*width = xrrs[current_size_id].width;
|
||||
*height = xrrs[current_size_id].height;
|
||||
*rate = XRRConfigCurrentRate(conf);
|
||||
|
||||
XRRFreeScreenConfigInfo(conf);
|
||||
}
|
||||
|
||||
void XGetScreenConfigurations() {
|
||||
Display *display = getXDisplay();
|
||||
Window root = DefaultRootWindow(display);
|
||||
XRRScreenSize *xrrs;
|
||||
int num_sizes;
|
||||
|
||||
xrrs = XRRSizes(display, 0, &num_sizes);
|
||||
for (int i = 0; i < num_sizes; i++) {
|
||||
short *rates;
|
||||
int num_rates;
|
||||
|
||||
goCreateScreenSize(i, xrrs[i].width, xrrs[i].height, xrrs[i].mwidth, xrrs[i].mheight);
|
||||
rates = XRRRates(display, 0, i, &num_rates);
|
||||
for (int j = 0; j < num_rates; j++) {
|
||||
goSetScreenRates(i, j, rates[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Inspired by https://github.com/raboof/xrandr/blob/master/xrandr.c
|
||||
void XCreateScreenMode(int width, int height, short rate) {
|
||||
Display *display = getXDisplay();
|
||||
Window root = DefaultRootWindow(display);
|
||||
|
||||
// create new mode info
|
||||
XRRModeInfo *mode_info = XCreateScreenModeInfo(width, height, rate);
|
||||
|
||||
// create new mode
|
||||
RRMode mode = XRRCreateMode(display, root, mode_info);
|
||||
XSync(display, 0);
|
||||
|
||||
// add new mode to all outputs
|
||||
XRRScreenResources *resources = XRRGetScreenResources(display, root);
|
||||
for (int i = 0; i < resources->noutput; ++i) {
|
||||
XRRAddOutputMode(display, resources->outputs[i], mode);
|
||||
}
|
||||
|
||||
XRRFreeScreenResources(resources);
|
||||
XRRFreeModeInfo(mode_info);
|
||||
}
|
||||
|
||||
// Inspired by https://fossies.org/linux/xwayland/hw/xwayland/xwayland-cvt.c
|
||||
XRRModeInfo *XCreateScreenModeInfo(int hdisplay, int vdisplay, short vrefresh) {
|
||||
char name[128];
|
||||
snprintf(name, sizeof name, "%dx%d_%d", hdisplay, vdisplay, vrefresh);
|
||||
XRRModeInfo *modeinfo = XRRAllocModeInfo(name, strlen(name));
|
||||
|
||||
#ifdef _LIBCVT_H_
|
||||
struct libxcvt_mode_info *mode_info;
|
||||
|
||||
// get screen mode from libxcvt, if available
|
||||
mode_info = libxcvt_gen_mode_info(hdisplay, vdisplay, vrefresh, false, false);
|
||||
|
||||
modeinfo->width = mode_info->hdisplay;
|
||||
modeinfo->height = mode_info->vdisplay;
|
||||
modeinfo->dotClock = mode_info->dot_clock * 1000;
|
||||
modeinfo->hSyncStart = mode_info->hsync_start;
|
||||
modeinfo->hSyncEnd = mode_info->hsync_end;
|
||||
modeinfo->hTotal = mode_info->htotal;
|
||||
modeinfo->vSyncStart = mode_info->vsync_start;
|
||||
modeinfo->vSyncEnd = mode_info->vsync_end;
|
||||
modeinfo->vTotal = mode_info->vtotal;
|
||||
modeinfo->modeFlags = mode_info->mode_flags;
|
||||
|
||||
free(mode_info);
|
||||
#else
|
||||
// fallback to a simple mode without refresh rate
|
||||
modeinfo->width = hdisplay;
|
||||
modeinfo->height = vdisplay;
|
||||
#endif
|
||||
|
||||
return modeinfo;
|
||||
}
|
||||
|
||||
void XSetKeyboardModifier(unsigned char mod, int on) {
|
||||
Display *display = getXDisplay();
|
||||
XkbLockModifiers(display, XkbUseCoreKbd, mod, on ? mod : 0);
|
||||
XFlush(display);
|
||||
}
|
||||
|
||||
unsigned char XGetKeyboardModifiers() {
|
||||
XkbStateRec xkbState;
|
||||
Display *display = getXDisplay();
|
||||
XkbGetState(display, XkbUseCoreKbd, &xkbState);
|
||||
// XkbStateFieldFromRec() doesn't work properly because
|
||||
// state.lookup_mods isn't properly updated, so we do this manually
|
||||
return XkbBuildCoreState(XkbStateMods(&xkbState), xkbState.group);
|
||||
}
|
||||
|
||||
XFixesCursorImage *XGetCursorImage(void) {
|
||||
Display *display = getXDisplay();
|
||||
return XFixesGetCursorImage(display);
|
||||
}
|
||||
|
||||
char *XGetScreenshot(int *w, int *h) {
|
||||
Display *display = getXDisplay();
|
||||
Window root = DefaultRootWindow(display);
|
||||
|
||||
XWindowAttributes attr;
|
||||
XGetWindowAttributes(display, root, &attr);
|
||||
int width = attr.width;
|
||||
int height = attr.height;
|
||||
|
||||
XImage *ximage = XGetImage(display, root, 0, 0, width, height, AllPlanes, ZPixmap);
|
||||
|
||||
*w = width;
|
||||
*h = height;
|
||||
char *pixels = (char *)malloc(width * height * 3);
|
||||
|
||||
for (int row = 0; row < height; row++) {
|
||||
for (int col = 0; col < width; col++) {
|
||||
int pos = ((row * width) + col) * 3;
|
||||
unsigned long pixel = XGetPixel(ximage, col, row);
|
||||
|
||||
pixels[pos] = (pixel & ximage->red_mask) >> 16;
|
||||
pixels[pos+1] = (pixel & ximage->green_mask) >> 8;
|
||||
pixels[pos+2] = pixel & ximage->blue_mask;
|
||||
}
|
||||
}
|
||||
|
||||
XDestroyImage(ximage);
|
||||
return pixels;
|
||||
}
|
349
server/pkg/xorg/xorg.go
Normal file
349
server/pkg/xorg/xorg.go
Normal file
@ -0,0 +1,349 @@
|
||||
package xorg
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -lX11 -lXrandr -lXtst -lXfixes -lxcvt
|
||||
|
||||
#include "xorg.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"sync"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"github.com/demodesk/neko/pkg/types"
|
||||
)
|
||||
|
||||
//go:generate ./keysymdef.sh
|
||||
|
||||
type KbdMod uint8
|
||||
|
||||
const (
|
||||
KbdModShift KbdMod = C.ShiftMask
|
||||
KbdModCapsLock KbdMod = C.LockMask
|
||||
KbdModControl KbdMod = C.ControlMask
|
||||
KbdModAlt KbdMod = C.Mod1Mask
|
||||
KbdModNumLock KbdMod = C.Mod2Mask
|
||||
KbdModMeta KbdMod = C.Mod3Mask
|
||||
KbdModSuper KbdMod = C.Mod4Mask
|
||||
KbdModAltGr KbdMod = C.Mod5Mask
|
||||
)
|
||||
|
||||
type ScreenConfiguration struct {
|
||||
Width int
|
||||
Height int
|
||||
Rates map[int]int16
|
||||
}
|
||||
|
||||
var ScreenConfigurations = make(map[int]ScreenConfiguration)
|
||||
|
||||
var debounce_button = make(map[uint32]time.Time)
|
||||
var debounce_key = make(map[uint32]time.Time)
|
||||
var mu = sync.Mutex{}
|
||||
|
||||
func GetScreenConfigurations() {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
C.XGetScreenConfigurations()
|
||||
}
|
||||
|
||||
func DisplayOpen(display string) bool {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
displayUnsafe := C.CString(display)
|
||||
defer C.free(unsafe.Pointer(displayUnsafe))
|
||||
|
||||
ok := C.XDisplayOpen(displayUnsafe)
|
||||
return int(ok) == 1
|
||||
}
|
||||
|
||||
func DisplayClose() {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
C.XDisplayClose()
|
||||
}
|
||||
|
||||
func Move(x, y int) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
C.XMove(C.int(x), C.int(y))
|
||||
}
|
||||
|
||||
func GetCursorPosition() (int, int) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
var x C.int
|
||||
var y C.int
|
||||
C.XCursorPosition(&x, &y)
|
||||
|
||||
return int(x), int(y)
|
||||
}
|
||||
|
||||
func Scroll(deltaX, deltaY int, controlKey bool) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
if controlKey {
|
||||
C.XSetKeyboardModifier(C.uchar(C.ControlMask), 1)
|
||||
defer C.XSetKeyboardModifier(C.uchar(C.ControlMask), 0)
|
||||
}
|
||||
|
||||
C.XScroll(C.int(deltaX), C.int(deltaY))
|
||||
}
|
||||
|
||||
func ButtonDown(code uint32) error {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
if _, ok := debounce_button[code]; ok {
|
||||
return fmt.Errorf("debounced button %v", code)
|
||||
}
|
||||
|
||||
debounce_button[code] = time.Now()
|
||||
|
||||
C.XButton(C.uint(code), C.int(1))
|
||||
return nil
|
||||
}
|
||||
|
||||
func KeyDown(code uint32) error {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
if _, ok := debounce_key[code]; ok {
|
||||
return fmt.Errorf("debounced key %v", code)
|
||||
}
|
||||
|
||||
debounce_key[code] = time.Now()
|
||||
|
||||
C.XKey(C.KeySym(code), C.int(1))
|
||||
return nil
|
||||
}
|
||||
|
||||
func ButtonUp(code uint32) error {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
if _, ok := debounce_button[code]; !ok {
|
||||
return fmt.Errorf("debounced button %v", code)
|
||||
}
|
||||
|
||||
delete(debounce_button, code)
|
||||
|
||||
C.XButton(C.uint(code), C.int(0))
|
||||
return nil
|
||||
}
|
||||
|
||||
func KeyUp(code uint32) error {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
if _, ok := debounce_key[code]; !ok {
|
||||
return fmt.Errorf("debounced key %v", code)
|
||||
}
|
||||
|
||||
delete(debounce_key, code)
|
||||
|
||||
C.XKey(C.KeySym(code), C.int(0))
|
||||
return nil
|
||||
}
|
||||
|
||||
func ResetKeys() {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
for code := range debounce_button {
|
||||
C.XButton(C.uint(code), C.int(0))
|
||||
delete(debounce_button, code)
|
||||
}
|
||||
|
||||
for code := range debounce_key {
|
||||
C.XKey(C.KeySym(code), C.int(0))
|
||||
delete(debounce_key, code)
|
||||
}
|
||||
}
|
||||
|
||||
func CheckKeys(duration time.Duration) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
t := time.Now()
|
||||
for code, start := range debounce_button {
|
||||
if t.Sub(start) < duration {
|
||||
continue
|
||||
}
|
||||
|
||||
C.XButton(C.uint(code), C.int(0))
|
||||
delete(debounce_button, code)
|
||||
}
|
||||
|
||||
for code, start := range debounce_key {
|
||||
if t.Sub(start) < duration {
|
||||
continue
|
||||
}
|
||||
|
||||
C.XKey(C.KeySym(code), C.int(0))
|
||||
delete(debounce_key, code)
|
||||
}
|
||||
}
|
||||
|
||||
// set screen configuration, create new one if not exists
|
||||
func ChangeScreenSize(s types.ScreenSize) (types.ScreenSize, error) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
// round width to 8, because of Xorg
|
||||
s.Width = s.Width - (s.Width % 8)
|
||||
|
||||
// if rate is 0, set it to 60
|
||||
if s.Rate == 0 {
|
||||
s.Rate = 60
|
||||
}
|
||||
|
||||
// convert variables to C types
|
||||
c_width, c_height, c_rate := C.int(s.Width), C.int(s.Height), C.short(s.Rate)
|
||||
|
||||
// if screen configuration already exists, just set it
|
||||
status := C.XSetScreenConfiguration(c_width, c_height, c_rate)
|
||||
if status != C.RRSetConfigSuccess {
|
||||
// create new screen configuration
|
||||
C.XCreateScreenMode(c_width, c_height, c_rate)
|
||||
|
||||
// screen configuration should exist now, set it
|
||||
status = C.XSetScreenConfiguration(c_width, c_height, c_rate)
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
// if screen configuration was not set successfully, return error
|
||||
if status != C.RRSetConfigSuccess {
|
||||
err = fmt.Errorf("unknown screen configuration %s", s.String())
|
||||
}
|
||||
|
||||
// if specified rate is not supported a BadValue error is returned
|
||||
if status == C.BadValue {
|
||||
err = fmt.Errorf("unsupported screen rate %d", s.Rate)
|
||||
}
|
||||
|
||||
return s, err
|
||||
}
|
||||
|
||||
func GetScreenSize() types.ScreenSize {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
c_width, c_height, c_rate := C.int(0), C.int(0), C.short(0)
|
||||
C.XGetScreenConfiguration(&c_width, &c_height, &c_rate)
|
||||
|
||||
return types.ScreenSize{
|
||||
Width: int(c_width),
|
||||
Height: int(c_height),
|
||||
Rate: int16(c_rate),
|
||||
}
|
||||
}
|
||||
|
||||
func SetKeyboardModifier(mod KbdMod, active bool) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
num := C.int(0)
|
||||
if active {
|
||||
num = C.int(1)
|
||||
}
|
||||
|
||||
C.XSetKeyboardModifier(C.uchar(mod), num)
|
||||
}
|
||||
|
||||
func GetKeyboardModifiers() KbdMod {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
return KbdMod(C.XGetKeyboardModifiers())
|
||||
}
|
||||
|
||||
func GetCursorImage() *types.CursorImage {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
cur := C.XGetCursorImage()
|
||||
defer C.XFree(unsafe.Pointer(cur))
|
||||
|
||||
width := int(cur.width)
|
||||
height := int(cur.height)
|
||||
|
||||
// Xlib stores 32-bit data in longs, even if longs are 64-bits long.
|
||||
pixels := C.GoBytes(unsafe.Pointer(cur.pixels), C.int(width*height*8))
|
||||
|
||||
img := image.NewRGBA(image.Rect(0, 0, width, height))
|
||||
for y := 0; y < height; y++ {
|
||||
for x := 0; x < width; x++ {
|
||||
pos := ((y * width) + x) * 8
|
||||
|
||||
img.SetRGBA(x, y, color.RGBA{
|
||||
A: pixels[pos+3],
|
||||
R: pixels[pos+2],
|
||||
G: pixels[pos+1],
|
||||
B: pixels[pos+0],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return &types.CursorImage{
|
||||
Width: uint16(width),
|
||||
Height: uint16(height),
|
||||
Xhot: uint16(cur.xhot),
|
||||
Yhot: uint16(cur.yhot),
|
||||
Serial: uint64(cur.cursor_serial),
|
||||
Image: img,
|
||||
}
|
||||
}
|
||||
|
||||
func GetScreenshotImage() *image.RGBA {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
var w, h C.int
|
||||
pixelsUnsafe := C.XGetScreenshot(&w, &h)
|
||||
pixels := C.GoBytes(unsafe.Pointer(pixelsUnsafe), w*h*3)
|
||||
defer C.free(unsafe.Pointer(pixelsUnsafe))
|
||||
|
||||
width := int(w)
|
||||
height := int(h)
|
||||
img := image.NewRGBA(image.Rect(0, 0, width, height))
|
||||
for row := 0; row < height; row++ {
|
||||
for col := 0; col < width; col++ {
|
||||
pos := ((row * width) + col) * 3
|
||||
|
||||
img.SetRGBA(col, row, color.RGBA{
|
||||
R: uint8(pixels[pos]),
|
||||
G: uint8(pixels[pos+1]),
|
||||
B: uint8(pixels[pos+2]),
|
||||
A: 0xFF,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return img
|
||||
}
|
||||
|
||||
//export goCreateScreenSize
|
||||
func goCreateScreenSize(index C.int, width C.int, height C.int, mwidth C.int, mheight C.int) {
|
||||
ScreenConfigurations[int(index)] = ScreenConfiguration{
|
||||
Width: int(width),
|
||||
Height: int(height),
|
||||
Rates: make(map[int]int16),
|
||||
}
|
||||
}
|
||||
|
||||
//export goSetScreenRates
|
||||
func goSetScreenRates(index C.int, rate_index C.int, rateC C.short) {
|
||||
ScreenConfigurations[int(index)].Rates[int(rate_index)] = int16(rateC)
|
||||
}
|
49
server/pkg/xorg/xorg.h
Normal file
49
server/pkg/xorg/xorg.h
Normal file
@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/XKBlib.h>
|
||||
#include <X11/Xutil.h>
|
||||
#include <X11/extensions/Xrandr.h>
|
||||
#include <X11/extensions/XTest.h>
|
||||
#include <X11/extensions/Xfixes.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// for computing xrandr modelines at runtime
|
||||
#include <libxcvt/libxcvt.h>
|
||||
|
||||
extern void goCreateScreenSize(int index, int width, int height, int mwidth, int mheight);
|
||||
extern void goSetScreenRates(int index, int rate_index, short rate);
|
||||
|
||||
Display *getXDisplay(void);
|
||||
int XDisplayOpen(char *input);
|
||||
void XDisplayClose(void);
|
||||
|
||||
void XMove(int x, int y);
|
||||
void XCursorPosition(int *x, int *y);
|
||||
void XScroll(int deltaX, int deltaY);
|
||||
void XButton(unsigned int button, int down);
|
||||
|
||||
typedef struct xkeyentry_t {
|
||||
KeySym keysym;
|
||||
KeyCode keycode;
|
||||
struct xkeyentry_t *next;
|
||||
} xkeyentry_t;
|
||||
|
||||
static void XKeyEntryAdd(KeySym keysym, KeyCode keycode);
|
||||
static KeyCode XKeyEntryGet(KeySym keysym);
|
||||
static KeyCode XkbKeysymToKeycode(Display *dpy, KeySym keysym);
|
||||
void XKey(KeySym keysym, int down);
|
||||
|
||||
Status XSetScreenConfiguration(int width, int height, short rate);
|
||||
void XGetScreenConfiguration(int *width, int *height, short *rate);
|
||||
void XGetScreenConfigurations();
|
||||
void XCreateScreenMode(int width, int height, short rate);
|
||||
XRRModeInfo *XCreateScreenModeInfo(int hdisplay, int vdisplay, short vrefresh);
|
||||
|
||||
void XSetKeyboardModifier(unsigned char mod, int on);
|
||||
unsigned char XGetKeyboardModifiers();
|
||||
XFixesCursorImage *XGetCursorImage(void);
|
||||
|
||||
char *XGetScreenshot(int *w, int *h);
|
Reference in New Issue
Block a user