Skip to content
ladiskPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

iPad Pen

Use an iPad with an Apple Pencil as a display tablet for a Linux laptop (Hyprland). The connection is the USB cable only.

  • Lecture mode (default): the iPad shows exactly what goes to the projector (or any other laptop output), and the Pencil draws on it.
  • Desk mode (--mode desk): the iPad is an extra monitor.
  • Pen only (--no-display): a pressure-sensitive pen tablet without a picture.

Parts:

  • ios/: iPad app. It sends Pencil samples (position, pressure, tilt, double tap) at up to 240 Hz and shows the H.264 video from the laptop. Fingers and the palm are ignored.
  • src/ipad_pen/: laptop daemon ipad-pen. It connects to the app through usbmux (the USB channel that iTunes and libimobiledevice use), creates a virtual tablet "iPad Pen" with uinput, and streams the picture to the iPad.
  • PROTOCOL.md: the wire format.

How the display works

When the iPad connects, the daemon:

  1. picks the output to show:
    • lecture mode: --output NAME, or with --output auto (default) the output the daemon remembers: the first time it takes the focused output and saves its name in $XDG_STATE_HOME/ipad-pen/output (default ~/.local/state/ipad-pen/output). Later connects and restarts use the saved output while it exists, else the focused one (the log says [saved] or [focused output]). An explicit --output NAME is not saved. To change the saved output, focus the other one and send the daemon SIGUSR1 (see below);
    • desk mode: it creates a Hyprland headless output (a monitor without a screen) named IPAD, 1366x1024 at 60 Hz, scale 1, right of the rightmost monitor (--desk-size 2732x2048 is sharper, scale 2);
  2. maps the tablet to that output (input.tablet.output);
  3. starts the capture program, which captures the output and encodes it with the Intel GPU (VAAPI H.264, 20 Mbit/s constant bit rate, no B-frames, a keyframe every 60 pictures). In lecture mode the GPU scales the picture to at most 1920 px wide (--max-width). Every picture goes to the iPad on a second USB connection (port 27016).

The iPad shows the picture aspect-fit, with black bars when the aspect ratio differs (a 16:9 projector on the 4:3 iPad). The pen position is relative to the picture, so the pen lands where you see it. A stroke that starts in a black bar does nothing.

Local ink: the video comes back from the laptop with a short delay. To hide it, the iPad draws the current stroke itself as a thin grey line and fades it out about 300 ms after the pen goes up, when the real stroke is in the video. --no-local-ink turns it off.

When the iPad goes away (cable, app closed or in the background) or the daemon stops, it stops the encoder and maps the tablet back to current. In desk mode it also removes IPAD; windows on it then move to another monitor.

The encoder uses about 10-20 % of one CPU core.

Security

The iPad app listens only on its loopback address (127.0.0.1), so it is not reachable over Wi-Fi; only usbmux (USB) can reach it. Nothing listens on the laptop: the laptop connects out to the iPad.

One-time setup on the laptop

  1. Load the uinput kernel module now and at every boot, and give your user access to /dev/uinput (now root only). The commands run in order:

    cd /data/projects/ipad-pen
    sudo modprobe uinput
    echo uinput | sudo tee /etc/modules-load.d/uinput.conf
    sudo install -m 644 linux/60-ipad-pen-uinput.rules /etc/udev/rules.d/
    sudo udevadm control --reload
    sudo udevadm trigger --name-match=uinput
    getfacl /dev/uinput

    getfacl must show user:<you>:rw-. No re-login is necessary (the uaccess tag is applied on the trigger). uinput is a module on this kernel, not built in; udevadm trigger /dev/uinput does not work, use --name-match=uinput.

  2. Install wf-recorder (screen capture):

    sudo pacman -S wf-recorder

    Without it the daemon works as a pen tablet only. Lecture mode uses gpu-screen-recorder when it is installed (--capture wf-recorder turns that off). It reads the screen through KMS (the kernel display API), so Hyprland does not copy every frame; wf-recorder's per-frame copy kept Hyprland near 100 % of one core. gpu-screen-recorder cannot see the headless output of desk mode, so desk mode always uses wf-recorder. If gpu-screen-recorder is missing or exits at once, the daemon logs it and uses wf-recorder. Its command: -c flv -k h264 -f 60 -fm cfr -bm cbr -q 20000 -keyint 1 -cursor no -s WxH (no pointer in the iPad picture).

  3. Install the daemon and start it as a user service:

    cd /data/projects/ipad-pen
    uv tool install .
    install -Dm 644 linux/ipad-pen.service ~/.config/systemd/user/ipad-pen.service
    systemctl --user daemon-reload
    systemctl --user enable --now ipad-pen.service

    Without the service: uv run ipad-pen -v.

  4. Hyprland: keep a fallback mapping for pen-only mode in ~/.config/hypr/input.lua:

    hl.config({ input = { tablet = { output = "current" } } })

    The daemon sets input.tablet.output at run time with hyprctl eval (Hyprland 0.56 with the Lua config: hyprctl keyword does not work). Do not map the device ipad-pen to a fixed monitor with hl.device(...): a per-device output wins over the global one. A config reload (hyprctl reload) resets the mapping to current; the daemon listens to Hyprland's event socket and sets its mapping again after each reload.

Usage

Plug in the iPad (it must trust this laptop; usbmuxd starts by itself when an iPad is plugged in), open the iPad Pen app. The status line on the iPad shows "Waiting for laptop", then the picture replaces it. The daemon reconnects by itself when the cable or the app goes away.

Lecture mode: connect the projector (mirrored or extended), put the focus on it, then connect the iPad. Draw in the presentation app or a whiteboard app (Saber, Xournal++) on the projector output.

Switch the lecture output while the daemon runs: focus the output you want (move the mouse to it), then run

systemctl --user kill --kill-whom=main -s USR1 ipad-pen

The daemon saves that output, restarts the capture on it, sends the iPad the new picture size and maps the tablet to it. Without an iPad it only saves. --kill-whom=main keeps the signal away from gpu-screen-recorder, which uses SIGUSR1 itself. Ignored with an explicit --output NAME and in desk mode.

Desk mode: put things on IPAD like on any other monitor:

  • drag a window across the right edge of the rightmost monitor;
  • focused window: hyprctl dispatch 'hl.dsp.window.move({ monitor = "IPAD" })';
  • whole workspace 5: hyprctl dispatch 'hl.dsp.workspace.move({ workspace = "5", monitor = "IPAD" })';
  • hyprctl dispatch 'hl.dsp.focus({ monitor = "IPAD" })', then open the app.

(Hyprland 0.56 with the Lua config takes Lua after dispatch; the old movewindow mon:IPAD syntax does not work.)

ipad-pen                              # lecture mode, focused output
ipad-pen --output HDMI-A-1            # lecture mode, this output
ipad-pen --mode desk [--desk-size 2732x2048]
ipad-pen --pressure                   # send Pencil pressure (default: constant, so every app draws a constant width)
ipad-pen --no-display --aspect 16:9   # pen only, mapped to a 16:9 monitor without stretching
ipad-pen --tcp HOST:PORT              # plain TCP source, for testing without an iPad

Pen-only mode (--no-display, an old app, or no wf-recorder) uses the fallback mapping from input.lua. The iPad is 4:3; --aspect 16:9 then uses the centred 16:9 part of the iPad. Other options: --bitrate, --render-node, --no-local-ink, --desk-scale, --desk-name, --video-port, --recorder (ipad-pen --help). For the service, put the options in ExecStart in ~/.config/systemd/user/ipad-pen.service. Logs: journalctl --user -u ipad-pen -f (-v adds the encoder output).

Fingers: one finger and palms are ignored. Two fingers scroll (the content follows the fingers, both directions) and pinch zooms (Ctrl + wheel). The daemon makes a second virtual device, "iPad Pen Scroll" (wheel only), for this; the scroll goes to the window under the pointer. By default the scroll is 1:1: the content moves as far as the fingers. The daemon computes the gain from the iPad view size, the picture size, the output width and scale, and the pixels one wheel detent scrolls (--scroll-px-per-detent, default 80 = Saber, measured; another app needs its own value). The iPad also shifts the picture at once by the finger movement of the last --scroll-latency-ms (default 100) until the real video catches up. --scroll-gain (wheel units per iPad point, 120 = one detent; negative inverts) overrides the computed gain, which breaks the 1:1 (the shift is scaled to it); remove an old --scroll-gain 0.5 from the service drop-in. Other options: --pinch-gain (wheel units per unit of ln(scale)); --no-gestures turns it off Ctrl is released at the end of the pinch and when the connection ends.

Taps: a one-finger tap is a left click at that point (hover with the pen, then a click on "iPad Pen Scroll"; nothing is drawn), a two-finger tap is undo (Ctrl+Z), a three-finger tap is redo (Ctrl+Y), sent as keys from the same "iPad Pen Scroll" device; --no-taps turns them off. Because the key code of Z is Y on a Slovenian layout, the daemon pins the layout of that device to US with hl.device({ name = "ipad-pen-scroll", kb_layout = "us" }) (hyprctl eval) at start and after every Hyprland config reload.

Events: ABS_X/ABS_Y (0..32767), ABS_PRESSURE (0..4095), ABS_TILT_X/ABS_TILT_Y (-90..90 deg), BTN_TOOL_PEN, BTN_TOUCH, BTN_STYLUS (Pencil double tap).

iPad app build

The app can only be built on macOS, so GitHub Actions builds it. Start a build:

gh workflow run ios-release.yml -R ladisk/ipad-pen

The workflow generates the Xcode project with XcodeGen (ios/project.yml), builds it unsigned and publishes IPadPen_<version>_unsigned.ipa as release v<version>.<run number>. The existing sideload timer signs and installs it with the app ID si.ladisk.ipadpen.<TEAMID>.

Development

uv run pytest
uv run ruff check

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages