Skip to content

All your computers and your phone/iPhone, working as one desk. Screens, apps, files, clipboard, calls, texts, notifications, mouse and keyboard, shared between Mac, Windows, Linux , Android, Web and ios / ipados . Free and open source. No account, no cloud, and no shared network needed.

11.2 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// macshare-linuxhelper: shared declarations. JSON lines on stdin/stdout, the same commands and
// events as windows-helper.
#ifndef MACSHARE_HELPER_H
#define MACSHARE_HELPER_H

#include <stddef.h>
#include <X11/Xlib.h>

// Two X connections: one for capture (raw events, grabs), one for injection and
// queries, so injection never waits behind event reading.
extern Display *dpy_ev, *dpy_in;
extern Window root_ev, root_in;

// ---- json.c: our flat messages (one nested object: "ev") ----
enum { J_NULL, J_BOOL, J_NUM, J_STR, J_OBJ, J_OTHER };
typedef struct JObj JObj;
typedef struct { int type; double num; char *str; JObj *obj; } JVal;
#define J_MAX 32
struct JObj { int n; char *keys[J_MAX]; JVal vals[J_MAX]; };

JObj *json_parse(const char *s);
void json_free(JObj *o);
int jhas(const JObj *o, const char *k);
const char *jstr(const JObj *o, const char *k, const char *def);
double jnum(const JObj *o, const char *k, double def);
int jbool(const JObj *o, const char *k, int def);
JObj *jobj(const JObj *o, const char *k);

typedef struct { char buf[4096]; size_t len; int first; } JOut; // keyLayout's two maps of Sinhala (3 UTF-8 bytes each) need ~2 KB
void jo_begin(JOut *o);
void jo_str(JOut *o, const char *k, const char *v);
void jo_num(JOut *o, const char *k, double v);
void jo_bool(JOut *o, const char *k, int v);
void jo_raw(JOut *o, const char *k, const char *json); // an already-built value
const char *jo_end(JOut *o);

void emit(const char *line); // one line to stdout (the only writer)

// ---- keys.c: DOM code <-> evdev (X keycode = evdev + 8) ----
int key_evdev(const char *code);   // -1 if unknown
const char *key_code(int evdev);   // NULL if unknown
void keys_layout(double id, Display *dpy); // keyLayout's reply: what each key types alone (NULL dpy: nothing)
void keys_type(Display *dpy, const char *text); // text as it is, on spare key codes (NULL dpy: nothing)

// ---- screens.c: XRandR monitors (physical px) ----
enum { SIDE_NONE = -1, SIDE_LEFT, SIDE_RIGHT, SIDE_TOP, SIDE_BOTTOM };
int side_of(const char *s);
const char *side_name(int side);
void screens_refresh(void);
int screens_contains(int x, int y);
int screens_on_outer_edge(int x, int y, int side);
double screens_outward(int side, double dx, double dy);
double screens_offset(int x, int y, int side);
void screens_edge_point(int side, double offset, int *x, int *y);
void screens_center(int *x, int *y);
void screens_clamp(int x, int y, int fromX, int fromY, int *ox, int *oy);
#define MAX_ZONES 32
int zones_parse(const char *text, double z[][3], int max);
int zones_contain(double z[][3], int n, int x, int y, int side);
void screens_walk(int x, int y, int fromX, int fromY, const int *stops, int nstops, int *ox, int *oy);
void screens_box(int *x, int *y, int *w, int *h);
void screens_set(const char *list);
int screens_monitor(int x, int y, int *mx, int *my, int *mw, int *mh);
int screens_count(void);
int screens_get(int i, int *x, int *y, int *w, int *h);

// portals.c: cursor portals (shared/cursor-portals.js), both backends
#define PORTAL_VERSION 3
void portals_config(const char *list); // kvmConfig portalList
void portals_return(const JObj *portal); // armReturn portal (NULL: none)
int portals_returning(void);
int portals_any(void);
void portals_key(int ev, int down);
int portals_covers(int x, int y, int side);
int portals_step(int x, int y, double dx, double dy, double resistance, double dwellMs, int (*may)(int, double),
                 int *idx, double *offset, int *pinX, int *pinY);
const char *portals_id(int idx);
int portals_side(int idx);
int portals_place(const JObj *c, int *x, int *y, int *side);
int portals_return_step(int x, int y, double dx, double dy, double resistance, double dwellMs, int (*may)(int, double),
                        int *side, double *offset);
int portals_walk_stop(int fromX, int fromY, int x, int y, int *ox, int *oy);

// ---- capture.c: XInput2 raw events, edge, hotkey, grabs, dead-man ----
int capture_init(void);
void capture_config(const JObj *c);
int capture_begin(int mouse, int keys);
void capture_stop(int side, double offset, int hasSide);
void capture_touch(void);
void capture_tick(void);
void capture_arm_return(int on, int side, double resistance);
#ifdef MULTI
void capture_arm_return_list(int on, const char *list, int fallback, double resistance); // several edges
#endif
void capture_feed_return(int x, int y, double dx, double dy);
void capture_arrived(int side);
void capture_return_zones(const char *text);
int capture_return_stops(int *out); // out: room for 4
void capture_xevent(XEvent *e);
int capture_active(void);
void capture_shutdown(void);

// ---- shortcut.c: the copy/paste-files shortcuts at home and the cursor lock key (both backends) ----
void shortcut_config(const JObj *c);
void shortcut_key(int ev, int down, int detect, int detectLock);
void shortcut_tick(void);

// ---- drop.c: file drags across the edge, out (X11): the edge strip, an XDND drop target ----
void drop_config(const JObj *c);
void drop_tick(int capturing, int side, int driveOther, int leftDown, int backSide);
void drop_xevent(XEvent *e);
void drop_shutdown(void);
int drop_holding(void); // a file drag is being caught on the way back: no plain return meanwhile
// capture.c, called by drop.c: the drag crossed (paths: a built JSON array) / the source let go /
// a drag caught on the way back while driven
void capture_drag_cross(const char *paths, int x, int y, int sx, int sy, int sw, int sh);
void capture_drag_dropped(void);
void capture_drag_return(const char *paths, int x, int y, int side);
void capture_watch_release(int on); // `released` at our next left button-up at home
void inject_button_up(int b);       // inject.c: an injected button's release (0 left)

// ---- xclip.c: the files on the clipboard, as an X client (XWayland on Wayland) ----
void xclip_files(double id);

// ---- inject.c: XTest ----
void inject_apply(const JObj *ev);
void inject_move(double dx, double dy); // the input lane's moves (lane.c)
void inject_move_to(int x, int y);
void inject_release_all(void);
void inject_button(int b, int down);

// ---- wins.c: top-level windows for Apps (X11) ----
void wins_list(double id, int minimized);
void wins_min(unsigned long wid);
void wins_frame(unsigned long wid, int x, int y, int w, int h);
void wins_raise(unsigned long wid);
void wins_close(unsigned long wid);
void wins_size(unsigned long wid, int w, int h);
void wins_move(unsigned long wid, double x, double y);
void wins_info(double id, int atPointer);
void wins_move_key(const char *key);     // Move windows: the key held to grab a window, "" = off
int wins_move_xevent(XEvent *e);         // the grab's core events (main.c's dpy_in loop)
int wins_grabbing(void);                 // a window follows the pointer
int wins_move_eat_up(void);              // the grab's left-button release: not passed on
unsigned long wins_move_take(int keepUp); // crossing: the grabbed window (0: none); the grab ends
void wins_move_from(int *x, int *y); // …and its visible top left when it was grabbed
void wins_park(unsigned long wid);
void wins_place(unsigned long wid, int x, int y);

// ---- wl.c: Wayland backend (evdev + uinput) ----
struct pollfd;
int wl_init(void);
int wl_fds(struct pollfd *p, int max);
void wl_read(struct pollfd *p, int n);
void wl_tick(void);
void wl_config(const JObj *c);
int wl_capture_begin(int mouse, int keys);
void wl_capture_stop(int side, double offset, int hasSide);
void wl_arrived(int side);
void wl_return_zones(const char *text);
void wl_arm_return(int on, const char *sideList, int side, double resistance);
void wl_inject(const JObj *ev);
void wl_move_rel(double dx, double dy);
void wl_move_to(int x, int y);
void wl_button(int b, int down);
void wl_pen_moved(int x, int y); // the pen put the pointer there (pen.c)
void wl_release_all(void);
void wl_watch_release(int on); // `released` at our next left button-up at home
void wl_touch(void);
int wl_active(void);
void wl_position(int *x, int *y);
void wl_shutdown(void);
const char *wl_drag_cross(const char *pathsText, int side, int back); // a caught file drag crosses (wlstrip.c, `dragCross`)
void wl_strip_caught(const char *pathsText, int side, int back);
// wlstrip.c: the drag strip on Wayland, an XWayland dock window
int wlstrip_available(void);
int wlstrip_fd(void);
void wlstrip_show(int x, int y, int w, int h, int side, int back, int fromMain);
void wlstrip_hide(void);
void wlstrip_identify(const char *list);
void wlstrip_read(void);
void wlstrip_tick(void);
void wlstrip_shutdown(void);
int wl_devices(void);

// ---- pen.c: a phone's stylus as a uinput tablet pen (both backends; wl: on Wayland) ----
void pen_at(int wl, int x, int y, const JObj *pen);
void pen_leave(int wl);
// ---- touch.c: a viewer's fingers as a uinput touch screen (both backends) ----
void touch_set(int wl, const char *p);
void touch_leave(int wl);

// ---- ble.c: Bluetooth LE through BlueZ (Ble.cs's commands and events), its own thread ----
int ble_handle(const char *line); // 1: a ble*/blp* command, taken
void ble_shutdown(void);

double now_ms(void);

// ---- crossing.c: hold a key to cross (#230), the emergency return (#178), both backends ----
void crossing_config(const JObj *c);
int crossing_bits(void);
int crossing_edge_ok(void);
void fullscreen_config(const JObj *c); // #373 (fullscreen.c)
int fullscreen_holds(void);
int crossing_return_ok(void);
void crossing_require_return(int on);
void crossing_remote(int bits);
int crossing_blocked(void); // after the emergency shortcut, until main ends the session
void crossing_watch(int capturingMouse, void (*fwd)(const char *));
void crossing_reset(void);
void crossing_session(int on);
int crossing_key(int ev, int value, int capturing, int capturingMouse, void (*fwd)(const char *));
void crossing_emergency(void);
void capture_forward(const char *k); // capture.c's forward, for crossing_watch
void wl_forward(const char *k);      // wl.c's
int capture_mouse(void);             // capturing the mouse (X11)
int wl_mouse(void);
void capture_emergency(void);        // the backend's part of the emergency shortcut
void wl_emergency(void);
// stroke 0: the defaults (shake.c); the File Shelf's is firmer (capture.c).
typedef struct { double turns[4]; int dir, nturns, head; double run, last, quiet; double stroke, window, gap; int reversals; } ShakeState;
void shake_reset(ShakeState *st);
int shake_step(ShakeState *st, double dx, double dy);
void capture_shelf_watch(int on); // File Shelf: a shaken drag here emits shelfShake (X11)
void shake_feed(double dx, double dy); // shake.c: a shake while capturing emits {"event":"shake"}

// ---- localwatch.c: driven, this computer's own mouse or touchpad takes it back (`local`) ----
void local_allow(int on); // kvmConfig takeBack
void local_watch(int on);
int local_note(double dx, double dy, int click); // 1: it took it back (the return watch stops)

// ---- lane.c: the input lane in the helper (native/tdfast; only with -DTDFAST) ----
#ifdef TDFAST
int lane_fd(void); // the main loop polls it: moves from the lane's thread
void lane_drain(void (*inject)(double dx, double dy));
void lane_handle(const JObj *c, const char **error, char *extra, size_t n, void (*inject)(double dx, double dy));
int lane_move(double dx, double dy, JOut *k);
const char *lane_fence(const char *k, char *buf, size_t n);
#endif

#endif