/*
* XR's windows: each app is a slate of its own in the space
* around you (a hologram), the shell's screen one more in front.
*
* The scene graph stays 2D. Each window gets a slot of the layout far
* away from every output, so no output draws it and no two overlap;
* glasses.c draws its surfaces from their textures onto its slate. The
* pointer is a ray: the mouse turns it, the head carries it along at the
* edge of the view, and wherever it meets a slate the cursor is warped to
* that point of the slate's slot (or the shell's screen). From there
* everything that already works on the 2D scene does: focus, the
* implicit grab, popups, pointer constraints.
*
* Poses are in the recentred world (glasses.c's xr.centre), so
* recentring brings the whole arrangement round to where you look.
*
* Under the window you use (and the one pointed at) is a row of tiles,
* its slate bar (slatebar.c): dragging its title carries the window, Move carries it
* until the next click (the wheel brings it nearer or sends it further),
* Resize or dragging the corner handle sizes it, Follow me makes it
* drift after your gaze, Minimise puts it away, Close closes it. The app
* never sees the bar: the cursor is nowhere while the ray is on it.
*
* A minimised window (and its dialogs) isn't drawn or pointed at and
* never has the keyboard; its icon waits in a row over the charms bar,
* and a click there, or picking it anywhere else, brings it back where
* it was (flying to you if that's out of view).
*
* Where you leave an app's window is kept by its app id (placement.c),
* and its first window opens there again, as big and following or not.
*
* A window asking for fullscreen becomes the theatre: a slate filling
* most of the view where you look, at the glasses' own pixels, the
* others dimmed (glasses.c), its bar only with the ray at its top edge.
* Leaving fullscreen puts it back where it was.
*
* A window picked from elsewhere (the switcher, a Start tile of a running
* app, a toast, an app activating itself) that's out of view flies to
* where you look; one you can see stays where it is. One opening out of
* view shows its icon at the edge of the view on its side for a moment.
*
* Toasts and the level box (the shell's osd surfaces) aren't on the
* shell's screen but on a slate of their own, the HUD, whose bottom
* follows the bottom of your view: they come up wherever you look.
*/
#include <stdlib.h>
#include <string.h>
#include <linux/input-event-codes.h>
#include <wlr/types/wlr_cursor.h>
#include <wlr/types/wlr_keyboard.h>
#include <wlr/types/wlr_seat.h>
#include "server.h"
#define SLOT_ORIGIN (1 << 20) // far off any output
#define SLOT_SIZE 8192 // a window and its popups' room
#define SLOT_MARGIN 2048 // room for popups above and left of the window
#define SLOT_COUNT 64
#define NOWHERE (-(1 << 20)) // the cursor's place when the ray hits nothing
#define WINDOW_W 1280 // a new window's size, in its own pixels
#define WINDOW_H 800
#define WINDOW_MAX 4096
#define DIALOG_NEARER 0.03 // m in front of the window it belongs to
#define CASCADE_DEG 8.0 // a new window nearer than this to one moves on
#define FOLLOW_DEG 25.0 // the shell's screen follows when you look further
#define FOLLOW_RATE 0.08 // of the way there per frame
#define DIST_MIN 0.5
#define DIST_MAX 10.0
#define START_INSET_PX 64 // Start's corner from the view's top left
#define SPLASH_SLOT SLOT_COUNT // past the windows' slots: never theirs
#define SPLASH_KEEP_MS 10000 // a slow app still opens where its splash was
#define BAR_SLOT (SLOT_COUNT + 1) // the charms bar's
#define BAR_DOWN_DEG 18.0 // looking further down than this brings the bar
#define BAR_UP_DEG 10.0 // and back above this it goes
#define BAR_UP_OVER_DEG 6.0 // or this far over the top of a card on it
#define BAR_BELOW_DEG 6.0 // its middle under where you looked
#define BAR_LOWEST_DEG 75.0
#define BAR_DIST C8_BAR_DIST
#define BAR_FOLLOW_DEG 20.0 // it turns with you past this
#define BAR_CENTRED_DEG 1.0 // until it's this near in front of you again
#define BAR_SINK_DEG 0.5 // and lower as you look lower
#define BAR_IN_MS 260 // fading in and rising into place
#define BAR_OUT_MS 160 // fading away
#define BAR_RISE_M 0.05 // from this far under where it comes to rest
#define BAR_ROW_PX 99 // the charms themselves, at the box's foot (c8-shell's
// BAR_LINE + CHARM_HEIGHT); a card on the bar is over them
#define FOLLOW_ME_DEG 15.0 // a window on Follow me drifts after you past this
#define WINDOW_MIN_W 320 // the smallest a resize makes a window
#define WINDOW_MIN_H 200
#define ANCHOR_MS 1500 // after a resize: the app may answer late
#define REMEMBERED_DEG 70.0 // a kept place further off where you look: not there
#define SUMMON_VIEW 0.8 // of half the view: a window's centre past it is out of view
#define SUMMON_RATE 0.25 // of the way to where you look per frame
#define SUMMON_DONE_DEG 0.5
#define SHELL_AHEAD 0.05 // m the shell's screen comes in front of a window
#define SHELL_NEAR_DEG 60.0 // windows this far off its middle may be under it
#define THEATRE_FILL 0.9 // of the view a fullscreen window's slate takes
#define THEATRE_REVEAL_PX 60 // its bottom edge where the ray brings its controls
#define HUD_SLOT (SLOT_COUNT + 2) // toasts and the level box
#define HUD_DIST 1.0 // m: nearer than the windows, behind the charms bar
#define HUD_RATE 0.2 // of the way to the bottom of the view per frame
#define HUD_LOW 0.6 // its bottom: this much of half the view under the middle
#define HUD_OVER_BAR_DEG 2.0 // and this far above the charms bar while it's up
#define BEACON_MS 3000 // a window opening out of view: its icon at the edge
/* How far out the beacon's middle goes, of half the view. Its icon and
* chevron reach ~2 deg further, and at BEACON_DIST each eye sees it ~2 deg
* apart, so at 0.85 it was out of one eye's view and blurred at the
* glasses' edge. */
#define BEACON_REACH 0.6
#define RAD(deg) ((deg) * M_PI / 180)
bool c8_xr_active(struct c8_server *server) {
return server->xr.shell != NULL;
}
static struct vec3 direction(double yaw, double pitch) {
return quat_rotate(quat_yaw_pitch(yaw, pitch), (struct vec3){ 0, 0, -1 });
}
static struct pose pose_at(double yaw, double pitch, double distance) {
struct quat q = quat_yaw_pitch(yaw, pitch);
return (struct pose){
quat_rotate(q, (struct vec3){ 0, 0, -distance }), q };
}
/* Where the head looks: straight ahead until tracking says otherwise */
static void gaze(struct c8_server *server, double *yaw, double *pitch) {
vec3_yaw_pitch(quat_rotate(server->xr.head.orientation,
(struct vec3){ 0, 0, -1 }), yaw, pitch);
}
/* Where things open: where you look, but ahead while you're looking down
* at the charms bar (what it opens goes up there, not on the floor) */
static void look(struct c8_server *server, double *yaw, double *pitch) {
gaze(server, yaw, pitch);
if (server->xr.bar_shown && !server->xr.bar_pinned) {
*pitch = server->xr.centre_pitch;
}
}
static double distance_of(struct pose p) {
return vec3_length(p.position);
}
static double angle_between(struct vec3 a, struct vec3 b) {
double c = vec3_dot(vec3_normalize(a), vec3_normalize(b));
return acos(fmax(-1, fmin(1, c)));
}
/* Slots */
static struct wlr_box slot_box(int slot) {
return (struct wlr_box){ SLOT_ORIGIN + slot * SLOT_SIZE, SLOT_ORIGIN,
SLOT_SIZE, SLOT_SIZE };
}
/* What's shown in a slot: in from its corner, so popups fit round it */
static struct wlr_box slot_inner(int slot, int w, int h) {
struct wlr_box box = slot_box(slot);
return (struct wlr_box){ box.x + SLOT_MARGIN, box.y + SLOT_MARGIN, w, h };
}
/* A size a window may have */
static void clamp_size(int *w, int *h) {
*w = *w < WINDOW_MIN_W ? WINDOW_MIN_W : *w > WINDOW_MAX ? WINDOW_MAX : *w;
*h = *h < WINDOW_MIN_H ? WINDOW_MIN_H : *h > WINDOW_MAX ? WINDOW_MAX : *h;
}
static bool slot_take(struct c8_view *view) {
struct c8_server *server = view->server;
if (view->holo.slot) {
return true;
}
for (int i = 0; i < SLOT_COUNT; i++) {
if (!(server->xr.slots & (1ULL << i))) {
server->xr.slots |= 1ULL << i;
view->holo.slot = i + 1;
return true;
}
}
return false;
}
/* Where popups may go: the whole slot, which nothing else shares */
bool c8_holo_area(struct c8_view *view, struct wlr_box *box) {
if (!c8_xr_active(view->server) || !view->holo.slot) {
return false;
}
*box = slot_box(view->holo.slot - 1);
return true;
}
/* Placing */
/* Where you look, but not straight on top of another window: on a
* little, down and right */
static struct pose free_pose(struct c8_server *server, struct c8_view *view) {
double yaw, pitch;
look(server, &yaw, &pitch);
for (int tries = 0; tries < 12; tries++) {
struct vec3 d = direction(yaw, pitch);
bool taken = false;
struct c8_view *other;
wl_list_for_each(other, &server->views, link) {
if (other != view && other->holo.placed &&
angle_between(d, other->holo.pose.position) <
RAD(CASCADE_DEG)) {
taken = true;
break;
}
}
if (!taken) {
break;
}
yaw -= RAD(CASCADE_DEG);
pitch -= RAD(CASCADE_DEG / 2);
}
return pose_at(yaw, pitch, server->config.glasses.distance);
}
/* Where the app's window was last time, unless another of its windows
* is up already (a second one cascades from it) */
static bool place_remembered(struct c8_view *view) {
struct c8_server *server = view->server;
const char *app_id = c8_view_app_id(view);
struct c8_placement p;
if (app_id == NULL || !c8_placement_get(app_id, &p) || !p.has_pose) {
return false;
}
struct c8_view *other;
wl_list_for_each(other, &server->views, link) {
const char *id = c8_view_app_id(other);
if (other != view && other->mapped && other->holo.placed &&
id && strcmp(id, app_id) == 0) {
return false;
}
}
double d = fmax(DIST_MIN, fmin(DIST_MAX, p.distance));
double yaw, pitch;
look(server, &yaw, &pitch);
if (angle_between(direction(p.yaw, p.pitch), direction(yaw, pitch)) >
RAD(REMEMBERED_DEG)) {
/* It was where you'd turned to then: where you look now, at
* the distance you had it */
struct pose here = free_pose(server, view);
vec3_yaw_pitch(here.position, &yaw, &pitch);
view->holo.pose = pose_at(yaw, pitch, d);
} else {
view->holo.pose = pose_at(p.yaw, p.pitch, d);
}
if (p.width >= WINDOW_MIN_W && p.width <= WINDOW_MAX &&
p.height >= WINDOW_MIN_H && p.height <= WINDOW_MAX) {
view->holo.width = p.width;
view->holo.height = p.height;
}
view->holo.follow = p.follow;
view->holo.kept = true;
return true;
}
/* Keep where a window is now (centred at pose) for its app's next time:
* you put it there. One you never moved or sized doesn't count, so a
* second window closing last doesn't undo where you had the first. */
static void remember_at(struct c8_view *view, struct pose pose) {
struct c8_server *server = view->server;
view->holo.kept = true;
int width = view->holo.width, height = view->holo.height;
bool follow = view->holo.follow;
if (view->holo.theatre) {
/* Next time it opens as a window, where it was before */
pose = view->holo.before_pose;
width = view->holo.before_width;
height = view->holo.before_height;
follow = view->holo.before_follow;
}
if (!c8_xr_active(server) || !view->holo.placed ||
c8_view_root(view) != view || width <= 0) {
return;
}
struct c8_placement p = {
.has_pose = true,
.stereo = view->holo.stereo,
.distance = distance_of(pose),
.width = width,
.height = height,
.follow = follow,
};
vec3_yaw_pitch(pose.position, &p.yaw, &p.pitch);
c8_placement_put(server, c8_view_app_id(view), &p);
}
static void remember(struct c8_view *view) {
remember_at(view, view->holo.pose);
}
/* view: a root (no parent) */
static void place_root(struct c8_view *view) {
/* Its app's picture was 3D last time: a game's or emulator's every
* window, which is why it isn't just the remembered one's */
struct c8_placement known;
const char *app_id = c8_view_app_id(view);
if (app_id && c8_placement_get(app_id, &known)) {
view->holo.stereo = known.stereo;
}
struct c8_server *server = view->server;
bool splash = server->xr.splash_placed &&
c8_now_ms() - server->xr.splash_at < SPLASH_KEEP_MS;
if (place_remembered(view)) {
/* It goes back where it was; its launch's splash is done */
if (splash) {
server->xr.splash_placed = false;
}
} else if (splash) {
/* A launch's splash stood where its window goes; the first
* window after it takes its place */
view->holo.pose = server->xr.splash_pose;
server->xr.splash_placed = false;
} else {
view->holo.pose = free_pose(server, view);
}
view->holo.placed = true;
}
/* The launch splash: a slate of a new window's size, in a slot of its
* own, where the window will open */
bool c8_holo_splash_box(struct c8_server *server, struct wlr_box *box) {
if (!c8_xr_active(server)) {
return false;
}
*box = slot_inner(SPLASH_SLOT, WINDOW_W, WINDOW_H);
return true;
}
void c8_holo_splash_shown(struct c8_server *server, bool shown) {
if (!c8_xr_active(server)) {
return;
}
if (shown) {
server->xr.splash_pose = free_pose(server, NULL);
server->xr.splash_placed = true;
}
server->xr.splash_at = c8_now_ms(); // shown, or since it went
}
/* The charms bar: in a slot of its own, as big as the shell made it */
bool c8_holo_bar_box(struct c8_server *server, struct wlr_box *box) {
if (!c8_xr_active(server)) {
return false;
}
struct c8_shell_surface *bar = c8_shell_bar(server);
*box = slot_inner(BAR_SLOT, bar ? bar->surface->current.width : 0,
bar ? bar->surface->current.height : 0);
return true;
}
/* Nearer than the windows, so as many of the view's pixels as they are */
double c8_holo_bar_m(struct c8_server *server) {
return server->xr.m_per_px * BAR_DIST / server->config.glasses.distance;
}
/* The ray onto the bar's middle (not while a press is held) */
static void bar_aim(struct c8_server *server) {
if (server->seat->pointer_state.button_count == 0) {
server->xr.aim = vec3_normalize(vec3_sub(server->xr.bar_pose.position,
server->xr.head.position));
server->xr.aim_set = true;
}
}
/* The charms at yaw and pitch: with a card over them the box's middle
* is higher. Its middle there put the charms under the view's bottom. */
static void bar_place(struct c8_server *server, double yaw, double pitch) {
server->xr.bar_pose = pose_at(yaw, pitch, BAR_DIST);
server->xr.bar_sink_from = pitch;
struct wlr_box box;
if (!c8_holo_bar_box(server, &box)) {
return;
}
if (box.height > BAR_ROW_PX) {
struct pose *p = &server->xr.bar_pose;
p->position = pose_point(*p, 0, (box.height - BAR_ROW_PX) / 2.0 *
c8_holo_bar_m(server), 0);
}
server->xr.bar_h = box.height; // grown from here, it moves up
}
/* Up where you look, a little under it */
static void bar_raise(struct c8_server *server) {
double yaw, pitch;
gaze(server, &yaw, &pitch);
bar_place(server, yaw, pitch - RAD(BAR_BELOW_DEG));
server->xr.bar_shown = true;
bar_aim(server);
}
/* Where it sits for a gaze this far down: under it, but not at your feet */
static double bar_pitch(double gp) {
return fmax(-RAD(BAR_LOWEST_DEG), gp - RAD(BAR_BELOW_DEG));
}
/* The charms binding (Super+C, a swipe in from the right): the bar where
* you look, until you press elsewhere or ask again */
bool c8_holo_charms(struct c8_server *server) {
if (!c8_xr_active(server) || c8_shell_bar(server) == NULL ||
c8_locked(server)) {
return false;
}
if (server->xr.bar_pinned) {
server->xr.bar_pinned = false;
server->xr.bar_shown = false;
return true;
}
server->xr.bar_pinned = true;
bar_raise(server);
return true;
}
/* Something with the keyboard came onto the bar (the switcher, a card).
* Not up (Alt+Tab): up where you look and pinned while it has it. Up as
* you looked down (a button on it): it stays as it is, so looking up
* still puts it away, and what's on it with it. Pinned already (the
* charms binding): it stays pinned after. */
void c8_holo_bar_grab(struct c8_server *server, bool grab) {
if (!c8_xr_active(server) || c8_shell_bar(server) == NULL) {
return;
}
if (grab) {
if (server->xr.bar_grabbed) {
return;
}
server->xr.bar_grabbed = true;
server->xr.bar_grab_pinned = false;
if (server->xr.bar_pinned || server->xr.bar_shown) {
return;
}
bar_raise(server);
server->xr.bar_pinned = true;
server->xr.bar_grab_pinned = true;
} else if (server->xr.bar_grabbed) {
server->xr.bar_grabbed = false;
if (server->xr.bar_grab_pinned) {
/* Back to coming up as you look down */
server->xr.bar_grab_pinned = false;
server->xr.bar_pinned = false;
server->xr.bar_shown = false;
}
}
}
/* Looking up past this puts the bar away: with a card on it (it has the
* keyboard), past the card's top, which can be higher than BAR_UP_DEG
* (Settings is three quarters of the screen high) */
static double bar_up_pitch(struct c8_server *server) {
double up = -RAD(BAR_UP_DEG);
struct wlr_box box;
if (server->xr.bar_grabbed && c8_holo_bar_box(server, &box)) {
struct vec3 top = pose_point(server->xr.bar_pose, 0,
box.height / 2.0 * c8_holo_bar_m(server), 0);
double ty, tp;
vec3_yaw_pitch(vec3_sub(top, server->xr.head.position), &ty, &tp);
up = fmax(up, tp + RAD(BAR_UP_OVER_DEG));
}
return up;
}
/* Every frame: look down and it comes up below you, facing you, level
* with where you face; it turns with you while it's up, and goes when
* you look up again (not while a press on it is held) */
static void bar_frame(struct c8_server *server) {
struct c8_shell_surface *bar = c8_shell_bar(server);
double gy, gp;
gaze(server, &gy, &gp);
bool held = server->xr.grab_bar &&
server->seat->pointer_state.button_count > 0;
bool was_shown = server->xr.bar_shown;
if (server->xr.bar_pinned && !server->xr.bar_grabbed && !held && (server->start_visible ||
c8_shell_panel_open(server))) {
server->xr.bar_pinned = false; // what it opened is up
}
if (bar == NULL || c8_locked(server)) {
server->xr.bar_shown = false;
server->xr.bar_pinned = false;
server->xr.bar_grabbed = false;
server->xr.bar_grab_pinned = false;
} else if (server->xr.bar_pinned) {
server->xr.bar_shown = true;
} else if (!server->xr.bar_shown) {
if (gp < -RAD(BAR_DOWN_DEG)) {
bar_place(server, gy, bar_pitch(gp));
server->xr.bar_shown = true;
/* The head dragged the ray down along the view's top
* edge: it starts again on the bar */
bar_aim(server);
}
} else if (gp > bar_up_pitch(server) && !held) {
server->xr.bar_shown = false;
} else if (!held) {
double by, bp;
vec3_yaw_pitch(server->xr.bar_pose.position, &by, &bp);
double dy = remainder(gy - by, 2 * M_PI), dp = 0;
/* Once it turns, all the way back in front of you: stopping
* at the edge of the dead zone left it off to one side */
if (fabs(dy) > RAD(BAR_FOLLOW_DEG)) {
server->xr.bar_turning = true;
} else if (fabs(dy) < RAD(BAR_CENTRED_DEG)) {
server->xr.bar_turning = false;
}
if (!server->xr.bar_turning) {
dy = 0;
}
dy *= FOLLOW_RATE;
/* Down too, looking further down than where it came up (it
* stays put as you look up, until it goes). Not with a card
* on it: looking down from the card to the charms sank them
* out of view. */
double want = bar_pitch(gp), from = fmin(bp,
server->xr.bar_sink_from);
if (server->xr.bar_grabbed) {
/* Where you looked with it up: once it goes, only lower
* than that sinks the bar (it jumped down as a card
* closed, catching up) */
server->xr.bar_sink_from = fmin(server->xr.bar_sink_from,
want);
} else if (want < from - RAD(BAR_SINK_DEG)) {
dp = (want - from) * FOLLOW_RATE;
server->xr.bar_sink_from = from + dp;
}
if (dy != 0 || dp != 0) {
server->xr.bar_pose = pose_at(by + dy, bp + dp, BAR_DIST);
/* A ray on it goes with it; one the head dragged along
* the view's edge comes onto it */
if (server->seat->pointer_state.button_count > 0) {
// held: it stays
} else if (server->xr.aim_bar || server->xr.aim_mini) {
double ay, ap;
vec3_yaw_pitch(server->xr.aim, &ay, &ap);
server->xr.aim = direction(ay + dy, ap + dp);
} else {
bar_aim(server);
}
}
}
if (was_shown && !server->xr.bar_shown) {
server->xr.bar_turning = false;
/* What's on it with the keyboard (the switcher) goes too */
c8_shell_bar_dismiss(server);
}
/* Taller (the switcher over the charms): it grows upwards, the
* charms stay where they are */
struct wlr_box box;
if (bar && c8_holo_bar_box(server, &box)) {
if (server->xr.bar_h > 0 && box.height != server->xr.bar_h &&
server->xr.bar_shown) {
struct pose *p = &server->xr.bar_pose;
double up = (box.height - server->xr.bar_h) / 2.0 *
c8_holo_bar_m(server);
p->position = pose_point(*p, 0, up, 0);
}
server->xr.bar_h = box.height;
}
/* Coming up it fades in and rises into place, in time rather than
* per frame (a per-frame step at the glasses' 90-120 Hz was over
* before it was seen); going it only fades */
int64_t now = c8_now_ms();
double dt = server->xr.bar_t_ms ? (double)(now - server->xr.bar_t_ms) : 0;
server->xr.bar_t_ms = now;
if (server->xr.bar_shown) {
server->xr.bar_t = fminf(1, server->xr.bar_t + dt / BAR_IN_MS);
} else {
server->xr.bar_t = fmaxf(0, server->xr.bar_t - dt / BAR_OUT_MS);
}
double t = server->xr.bar_t, e = 1 - pow(1 - t, 3); // ease out
server->xr.bar_fade = server->xr.bar_shown ? e : t;
server->xr.bar_drawn = server->xr.bar_pose;
if (server->xr.bar_shown) {
server->xr.bar_drawn.position = pose_point(server->xr.bar_pose, 0,
-(1 - e) * BAR_RISE_M, 0);
}
if (bar) {
/* Drawn while it fades; picked only while it's shown */
wlr_scene_node_set_enabled(&bar->tree->node,
server->xr.bar_fade > 0);
}
}
/* The HUD: toasts and the level box, in a slot of their own as big as
* the shell's screen (the shell lays them out along its bottom) */
bool c8_holo_hud_box(struct c8_server *server, struct wlr_box *box) {
if (!c8_xr_active(server)) {
return false;
}
struct wlr_box out;
c8_output_box(server, &out);
*box = slot_inner(HUD_SLOT, out.width, out.height);
return !wlr_box_empty(box);
}
struct pose c8_holo_hud_pose(struct c8_server *server, double *m) {
*m = server->xr.hud_m;
return server->xr.hud_pose;
}
static bool hud_up(struct c8_server *server) {
struct c8_shell_surface *ss;
wl_list_for_each(ss, &server->shell.surfaces, link) {
if (c8_shell_osd_shown(ss)) {
return true;
}
}
return false;
}
/* Every frame: its bottom edge eases after a line well in from the bottom
* of the view (`HUD_LOW`: the very edge is blurred or cut off by the
* lenses), middle after middle; held where it is while the ray is on it,
* so a toast can be clicked. It comes up already there. */
static void hud_frame(struct c8_server *server) {
struct wlr_box box;
if (!hud_up(server) || !c8_holo_hud_box(server, &box)) {
server->xr.hud_placed = false;
return;
}
double gy, gp;
gaze(server, &gy, &gp);
double want = gp - server->xr.fov_y / 2 * HUD_LOW;
struct wlr_box bar_box;
if (server->xr.bar_shown && c8_holo_bar_box(server, &bar_box)) {
/* Not over the charms bar (or what's above it) */
struct pose bp = server->xr.bar_pose;
struct c8_view *minis[C8_MINI_MAX];
int over = c8_holo_minimised_views(server, minis, C8_MINI_MAX) ?
C8_MINI_ABOVE + C8_MINI_PX : 0; // their icons' row
struct vec3 top = pose_point(bp, 0,
(bar_box.height / 2.0 + over) * c8_holo_bar_m(server), 0);
double ty, tp;
vec3_yaw_pitch(vec3_sub(top, server->xr.head.position), &ty, &tp);
want = fmax(want, tp + RAD(HUD_OVER_BAR_DEG));
}
bool held = server->xr.aim_hud || (server->xr.grab_hud &&
server->seat->pointer_state.button_count > 0);
if (!server->xr.hud_placed) {
server->xr.hud_yaw = gy;
server->xr.hud_pitch = want;
server->xr.hud_placed = true;
} else if (!held) {
server->xr.hud_yaw += remainder(gy - server->xr.hud_yaw, 2 * M_PI) *
HUD_RATE;
server->xr.hud_pitch += (want - server->xr.hud_pitch) * HUD_RATE;
}
/* In front of any window, so in 3D it isn't behind what it covers;
* its pixels smaller by as much, so it looks the same size */
double d = HUD_DIST;
struct c8_view *view;
wl_list_for_each(view, &server->views, link) {
if (view->mapped && view->holo.slot && !c8_holo_minimised(view)) {
d = fmin(d, distance_of(c8_holo_pose(view)) - SHELL_AHEAD);
}
}
d = fmax(d, DIST_MIN / 2);
double m = server->xr.m_per_px * d / server->config.glasses.distance;
struct pose p = pose_at(server->xr.hud_yaw, server->xr.hud_pitch, d);
p.position = pose_point(p, 0, box.height * m / 2, 0);
server->xr.hud_pose = p;
server->xr.hud_m = m;
}
/* A dialog sits just in front of what it belongs to, and goes with it */
static struct pose child_pose(struct c8_view *view) {
struct c8_view *root = c8_view_root(view);
int depth = 0;
for (struct c8_view *v = view; v != root && depth < 64;
v = c8_view_parent(v)) {
depth++;
}
struct pose p = root->holo.pose;
double d = distance_of(p);
double nearer = fmax(DIST_MIN / 2, d - depth * DIALOG_NEARER);
p.position = vec3_scale(p.position, nearer / fmax(d, 1e-6));
return p;
}
struct pose c8_holo_pose(struct c8_view *view) {
return c8_view_root(view) == view ? view->holo.pose : child_pose(view);
}
/* The theatre: most of the view where you look, at the default
* distance, so its pixels are the glasses' own */
static void theatre_enter(struct c8_view *view) {
struct c8_server *server = view->server;
view->holo.before_pose = view->holo.pose;
view->holo.before_width = view->holo.width;
view->holo.before_height = view->holo.height;
view->holo.before_follow = view->holo.follow;
view->holo.theatre = true;
view->holo.follow = false;
view->holo.anchor_until = 0;
double d = server->config.glasses.distance, m = server->xr.m_per_px;
int w = 1920, h = 1080; // before the glasses' first frame
if (m > 0 && server->xr.fov_x > 0) {
w = (int)lround(2 * tan(server->xr.fov_x / 2) * d / m * THEATRE_FILL);
h = (int)lround(2 * tan(server->xr.fov_y / 2) * d / m * THEATRE_FILL);
}
clamp_size(&w, &h);
view->holo.width = w;
view->holo.height = h;
double yaw, pitch;
look(server, &yaw, &pitch);
view->holo.pose = pose_at(yaw, pitch, d);
}
static void theatre_leave(struct c8_view *view) {
view->holo.theatre = false;
view->holo.pose = view->holo.before_pose;
view->holo.width = view->holo.before_width;
view->holo.height = view->holo.before_height;
view->holo.follow = view->holo.before_follow;
view->holo.anchor_until = 0;
}
struct c8_view *c8_holo_theatre(struct c8_server *server) {
struct c8_view *top = c8_top_view(server);
struct c8_view *root = top ? c8_view_root(top) : NULL;
return c8_xr_active(server) && root && root->holo.theatre &&
!server->start_visible ? root : NULL;
}
/* Its slot, placed and sized: where view.c puts the window */
bool c8_holo_window_box(struct c8_view *view, struct wlr_box *box,
bool *fullscreen) {
*fullscreen = false;
if (!slot_take(view)) {
return false;
}
struct c8_view *root = c8_view_root(view);
if (!root->holo.placed) {
place_root(root); // a dialog's own pose follows from it
}
if (view == root) {
bool wants = view->holo.theatre_ask ? view->holo.theatre_ask > 0 :
view->impl->wants_fullscreen(view);
if (wants && !view->holo.theatre) {
if (view->holo.width <= 0) {
/* Opening fullscreen: its window size for after */
view->holo.width = WINDOW_W;
view->holo.height = WINDOW_H;
}
theatre_enter(view);
} else if (!wants && view->holo.theatre) {
theatre_leave(view);
}
*fullscreen = view->holo.theatre;
}
if (view->holo.width <= 0) {
/* A window brought in from the flat desktop keeps its size if
* that fits, else gets a comfortable one */
bool fits = view->geo.width > 0 && view->geo.width <= WINDOW_MAX &&
view->geo.height > 0 && view->geo.height <= WINDOW_MAX;
view->holo.width = fits ? view->geo.width : WINDOW_W;
view->holo.height = fits ? view->geo.height : WINDOW_H;
}
*box = slot_inner(view->holo.slot - 1, view->holo.width, view->holo.height);
return true;
}
void c8_holo_view_gone(struct c8_view *view) {
struct c8_server *server = view->server;
if (view->holo.kept) {
remember(view); // where it was when it closed
}
if (view->holo.slot) {
server->xr.slots &= ~(1ULL << (view->holo.slot - 1));
view->holo.slot = 0;
}
if (server->xr.move_view == view) {
server->xr.move_sticky = false;
}
/* Everything that points at a window */
struct c8_view **refs[] = { &server->xr.aim_view, &server->xr.grab_view,
&server->xr.move_view, &server->xr.size_view, &server->xr.aim_chrome,
&server->xr.chrome_view, &server->xr.reveal_view,
&server->xr.press_view, &server->xr.aim_mini,
&server->xr.press_mini };
for (size_t i = 0; i < sizeof(refs) / sizeof(refs[0]); i++) {
if (*refs[i] == view) {
*refs[i] = NULL;
}
}
c8_slatebar_drop(view);
c8_slatebar_beacon_drop(view);
view->holo.beacon_at = 0;
view->holo.beacon_pending = false;
view->holo.minimised = false;
}
/* Minimised windows */
bool c8_holo_minimised(struct c8_view *view) {
return c8_xr_active(view->server) && c8_view_root(view)->holo.minimised;
}
/* root: a window without a parent. The caller (c8_view_minimize) moves
* the focus on. */
void c8_holo_minimise(struct c8_view *root) {
struct c8_server *server = root->server;
if (!c8_xr_active(server) || !root->mapped) {
return;
}
root->holo.minimised = true;
root->holo.minimised_at = c8_now_ms();
root->holo.summon = false;
root->holo.anchor_until = 0;
if (server->xr.move_view == root) {
server->xr.move_view = NULL;
server->xr.move_sticky = false;
}
if (server->xr.size_view == root) {
server->xr.size_view = NULL;
}
struct c8_view **refs[] = { &server->xr.aim_view, &server->xr.grab_view,
&server->xr.aim_chrome, &server->xr.chrome_view,
&server->xr.reveal_view, &server->xr.press_view };
bool pointed = false;
for (size_t i = 0; i < sizeof(refs) / sizeof(refs[0]); i++) {
if (*refs[i] && c8_view_root(*refs[i]) == root) {
pointed |= refs[i] == &server->xr.aim_view;
*refs[i] = NULL;
}
}
struct c8_view *view;
wl_list_for_each(view, &server->views, link) {
if (c8_view_root(view) == root) {
view->holo.beacon_at = 0;
}
}
if (pointed) {
/* The pointer was on it (it was put away from a window list):
* nowhere, until the next frame aims the ray again */
server->cursor->x = server->cursor->y = NOWHERE;
c8_pointer_motion(server, (uint32_t)c8_now_ms(), false);
}
}
void c8_holo_restore(struct c8_view *view) {
struct c8_view *root = c8_view_root(view);
if (root->holo.minimised && c8_xr_active(view->server)) {
root->holo.restored_at = c8_now_ms(); // it comes back up
}
root->holo.minimised = false;
}
/* Leaving XR: flat there's nowhere they'd be */
void c8_holo_restore_all(struct c8_server *server) {
struct c8_view *view;
wl_list_for_each(view, &server->views, link) {
view->holo.minimised = false;
}
}
/* They sit at the end of the list in the order they were put away (see
* c8_view_minimize), so the row doesn't shuffle as others are used */
int c8_holo_minimised_views(struct c8_server *server, struct c8_view **views,
int max) {
int n = 0;
if (!c8_xr_active(server)) {
return 0;
}
struct c8_view *view;
wl_list_for_each(view, &server->views, link) {
if (n < max && view->mapped && view->holo.minimised &&
c8_view_root(view) == view) {
views[n++] = view;
}
}
return n;
}
/* Start stands further off than the windows; toasts and panels with them */
static double shell_distance(struct c8_server *server) {
return server->start_visible ? server->config.glasses.start_distance :
server->config.glasses.distance;
}
/* The shell's screen: where you look when Start comes up */
static void place_shell(struct c8_server *server) {
double yaw, pitch;
look(server, &yaw, &pitch);
server->xr.shell_pose = pose_at(yaw, pitch, shell_distance(server));
}
void c8_holo_recentred(struct c8_server *server) {
server->xr.shell_pose = pose_at(0, server->xr.centre_pitch,
shell_distance(server));
server->xr.shell_placed = true;
server->xr.aim_set = false; // back to where you look
}
/* Start comes up with its top left corner a little in from the view's,
* so its tiles are where you look and the rest runs off to the right
* and down: the shell's screen widens to all its groups (there's room
* all round you), and it stands further off than the windows, so it
* isn't in your face */
void c8_holo_start_shown(struct c8_server *server) {
if (!c8_xr_active(server)) {
return;
}
c8_xr_shell_size(server);
struct wlr_output *wo = server->xr.shell_made;
double m = server->xr.m_per_px, d = server->config.glasses.start_distance;
double w = wo ? wo->width : 1920, h = wo ? wo->height : 1080;
double yaw, pitch;
look(server, &yaw, &pitch);
/* The view's top left corner, in by the inset (as many of the view's
* pixels), less half Start's size at its distance: its middle */
double inset = START_INSET_PX * m / server->config.glasses.distance;
yaw += server->xr.fov_x / 2 - inset - atan(w / 2 * m / d);
pitch += server->xr.fov_y / 2 - inset - atan(h / 2 * m / d);
server->xr.shell_pose = pose_at(yaw, pitch, d);
}
/* Toasts and panels after it: back where you look, all in view */
void c8_holo_start_hidden(struct c8_server *server) {
if (c8_xr_active(server)) {
c8_xr_shell_size(server);
place_shell(server);
}
}
/* Hit testing */
/* Where the ray o + t d meets the slate (pose p, w x h pixels of m
* metres): the pixel from its corner. Only from in front. */
static bool ray_slate(struct vec3 o, struct vec3 d, struct pose p, double m,
int w, int h, double *px, double *py, double *t) {
struct vec3 n = quat_rotate(p.orientation, (struct vec3){ 0, 0, 1 });
double denom = vec3_dot(d, n);
if (denom > -1e-6) {
return false;
}
*t = vec3_dot(vec3_sub(p.position, o), n) / denom;
if (*t <= 0) {
return false;
}
struct vec3 hit = vec3_add(o, vec3_scale(d, *t));
struct vec3 l = quat_rotate(quat_conjugate(p.orientation),
vec3_sub(hit, p.position));
*px = w / 2.0 + l.x / m;
*py = h / 2.0 - l.y / m;
return true;
}
/* The same for a slate bent round a cylinder (glasses.c's curve_strip):
* its axis the slate's up through `r` in front of its middle. From
* inside it (you are), the far side is the one ahead. */
static bool ray_curved(struct vec3 o, struct vec3 d, struct pose p, double r,
double m, int w, int h, double *px, double *py, double *t) {
struct quat inv = quat_conjugate(p.orientation);
struct vec3 lo = quat_rotate(inv, vec3_sub(o, p.position));
struct vec3 ld = quat_rotate(inv, d);
double oz = lo.z - r;
double a = ld.x * ld.x + ld.z * ld.z;
double b = 2 * (lo.x * ld.x + oz * ld.z);
double c = lo.x * lo.x + oz * oz - r * r;
double disc = b * b - 4 * a * c;
if (a < 1e-12 || disc < 0) {
return false;
}
*t = (-b + sqrt(disc)) / (2 * a);
if (*t <= 0) {
return false;
}
struct vec3 hit = vec3_add(lo, vec3_scale(ld, *t));
if (hit.z > r) {
return false; // the back of the cylinder, behind you
}
*px = w / 2.0 + atan2(hit.x, r - hit.z) * r / m;
*py = h / 2.0 - hit.y / m;
return true;
}
struct hit {
struct c8_view *view; // or
bool shell; // the shell's screen, or
bool bar; // the charms bar, or neither (or the HUD, below)
double lx, ly; // layout point
double t; // metres along the ray
struct c8_view *chrome; // or a window's slate bar,
int part; // and which part of it
bool hud; // or toasts and the level box
struct c8_view *mini; // or a minimised window's icon over the bar
};
static bool shell_slate(struct c8_server *server, struct wlr_box *box) {
struct c8_output *shell = server->xr.shell;
if (shell == NULL || !shell->wlr_output->enabled) {
return false;
}
c8_output_box(server, box);
return !wlr_box_empty(box);
}
static bool hit_view(struct c8_server *server, struct c8_view *view,
struct vec3 o, struct vec3 d, bool anywhere, struct hit *out) {
double px, py, t;
if (!view->mapped || !view->holo.slot || view->geo.width <= 0 ||
c8_holo_minimised(view) ||
!ray_slate(o, d, c8_holo_pose(view), server->xr.m_per_px,
view->geo.width, view->geo.height, &px, &py, &t)) {
return false;
}
double lx = view->geo.x + px, ly = view->geo.y + py;
if (!anywhere && !wlr_scene_node_at(&view->tree->node, lx, ly,
NULL, NULL)) {
return false;
}
*out = (struct hit){ view, false, false, lx, ly, t };
return true;
}
/* Slate bars */
static bool is_focused_root(struct c8_view *view) {
struct c8_view *top = c8_top_view(view->server);
return top && c8_view_root(top) == view;
}
/* On the window you use, the one pointed at last, and one being moved or
* resized; never under Start or while locked */
bool c8_holo_chrome_shown(struct c8_view *view) {
struct c8_server *server = view->server;
if (!c8_xr_active(server) || !view->mapped || !view->holo.slot ||
c8_view_root(view) != view || view->geo.width <= 0 ||
view->holo.minimised || server->start_visible ||
c8_locked(server)) {
return false;
}
if (server->xr.move_view == view || server->xr.size_view == view) {
return true;
}
if (view->holo.theatre) {
/* Out of the way of what it plays, until you reach for it */
return server->xr.aim_chrome == view ||
server->xr.reveal_view == view;
}
return is_focused_root(view) || server->xr.chrome_view == view;
}
struct c8_slatebar_look c8_holo_chrome_look(struct c8_view *view) {
struct c8_server *server = view->server;
struct c8_slatebar_look look = {
.width = view->geo.width,
.focused = is_focused_root(view),
.follow = view->holo.follow,
.theatre = view->holo.theatre,
.stereo = view->holo.stereo,
.accent = server->config.accent,
};
if (server->xr.aim_chrome == view) {
look.hover = server->xr.aim_part;
}
if (server->xr.press_view == view) {
look.pressed = server->xr.press_part;
}
/* What it's doing stays lit while it does it */
if (server->xr.move_view == view && server->xr.move_sticky) {
look.hover = look.pressed = C8_SLATEBAR_MOVE;
}
return look;
}
static bool hit_chrome(struct c8_server *server, struct c8_view *view,
struct vec3 o, struct vec3 d, struct hit *out) {
double px, py, t;
if (!c8_holo_chrome_shown(view) || !ray_slate(o, d, view->holo.pose,
server->xr.m_per_px, view->geo.width, view->geo.height,
&px, &py, &t)) {
return false;
}
int part = c8_slatebar_part_at(view->geo.width, view->geo.height, px,
py);
if (part == C8_SLATEBAR_NONE) {
return false;
}
*out = (struct hit){ .t = t, .chrome = view, .part = part };
return true;
}
/* The window's top left corner in the world: a resize keeps it put */
static struct vec3 top_left(struct c8_view *view) {
double m = view->server->xr.m_per_px;
return pose_point(view->holo.pose, -view->geo.width * m / 2,
view->geo.height * m / 2, 0);
}
/* And back: where the middle of a window w by h is with its corner at
* the anchor */
static struct vec3 centre_from_anchor(struct c8_view *view, int w, int h) {
double m = view->server->xr.m_per_px;
struct pose corner = { view->holo.anchor, view->holo.pose.orientation };
return pose_point(corner, w * m / 2, -h * m / 2, 0);
}
static void anchor_apply(struct c8_view *view) {
if (view->server->xr.size_view != view &&
c8_now_ms() > view->holo.anchor_until) {
return;
}
view->holo.pose.position = centre_from_anchor(view, view->geo.width,
view->geo.height);
}
/* Anywhere: its plane, even past its edge (held down on it) */
static bool hit_bar(struct c8_server *server, struct vec3 o, struct vec3 d,
bool anywhere, struct hit *out) {
struct wlr_box box;
double px, py, t;
if (!server->xr.bar_shown || !c8_holo_bar_box(server, &box) ||
wlr_box_empty(&box) || !ray_curved(o, d, server->xr.bar_pose,
C8_BAR_CURVE_R, c8_holo_bar_m(server), box.width,
box.height, &px, &py, &t)) {
return false;
}
if (!anywhere && (px < 0 || py < 0 || px >= box.width ||
py >= box.height)) {
return false;
}
*out = (struct hit){ NULL, false, true, box.x + px, box.y + py, t };
return true;
}
/* The row of minimised windows' icons over the bar, on its cylinder
* (glasses.c draws each flat against it: as good as the same at their
* size). The gaps between them and under them count as the icon beside
* or above, so moving up from the bar never falls through. */
static bool hit_mini(struct c8_server *server, struct vec3 o, struct vec3 d,
struct hit *out) {
struct c8_view *views[C8_MINI_MAX];
struct wlr_box box;
double px, py, t;
if (!server->xr.bar_shown || c8_locked(server) ||
!c8_holo_bar_box(server, &box) || wlr_box_empty(&box)) {
return false;
}
int n = c8_holo_minimised_views(server, views, C8_MINI_MAX);
if (n == 0 || !ray_curved(o, d, server->xr.bar_pose, C8_BAR_CURVE_R,
c8_holo_bar_m(server), box.width, box.height, &px, &py, &t) ||
py >= 0 || py < -(C8_MINI_ABOVE + C8_MINI_PX)) {
return false;
}
double step = C8_MINI_PX + C8_MINI_GAP;
double x = px - box.width / 2.0 + n * step / 2; // from the row's left
if (x < 0 || x >= n * step) {
return false;
}
*out = (struct hit){ .t = t, .mini = views[(int)(x / step)] };
return true;
}
/* Only where a toast or the level box is, unless held down on it */
static bool hit_hud(struct c8_server *server, struct vec3 o, struct vec3 d,
bool anywhere, struct hit *out) {
struct wlr_box box;
double px, py, t;
if (!server->xr.hud_placed || !c8_holo_hud_box(server, &box) ||
!ray_slate(o, d, server->xr.hud_pose, server->xr.hud_m,
box.width, box.height, &px, &py, &t)) {
return false;
}
if (!anywhere && !c8_node_exists_at(server, box.x + px, box.y + py)) {
return false;
}
*out = (struct hit){ .hud = true, .lx = box.x + px, .ly = box.y + py,
.t = t };
return true;
}
/* The shell's screen: where it shows something (Start, a panel, the lock
* screen), unless held down on it */
static bool hit_shell(struct c8_server *server, struct vec3 o, struct vec3 d,
bool anywhere, struct hit *out) {
struct wlr_box box;
double px, py, t;
if (!shell_slate(server, &box) || !ray_slate(o, d, server->xr.shell_drawn,
server->xr.shell_m, box.width, box.height, &px, &py, &t)) {
return false;
}
if (!anywhere && (px < 0 || py < 0 || px >= box.width ||
py >= box.height ||
!c8_node_exists_at(server, box.x + px, box.y + py))) {
return false;
}
*out = (struct hit){ NULL, true, false, box.x + px, box.y + py, t };
return true;
}
static struct hit pick(struct c8_server *server, struct vec3 o,
struct vec3 d) {
struct hit best = { .t = INFINITY };
/* The bar is nearest of all, then toasts */
if (hit_bar(server, o, d, false, &best) ||
hit_mini(server, o, d, &best) ||
hit_hud(server, o, d, false, &best)) {
return best;
}
/* The shell's screen is in front of everything where it shows
* something */
if (hit_shell(server, o, d, false, &best)) {
return best;
}
if (c8_locked(server)) {
return best;
}
/* The one drawn on top: nearest (glasses.c draws furthest first,
* without depth), at the same distance the most recently used, which
* comes first in the list */
double best_d = INFINITY;
struct c8_view *view;
wl_list_for_each(view, &server->views, link) {
struct hit h;
if (!hit_view(server, view, o, d, false, &h) &&
!hit_chrome(server, view, o, d, &h)) {
continue;
}
double dist = distance_of(c8_holo_pose(view));
if (dist < best_d - 0.005) {
best = h;
best_d = dist;
}
}
return best;
}
/* Aiming */
/* Keep the ray in view: turning the head carries it along at the edge */
static void aim_in_view(struct c8_server *server) {
struct quat hq = server->xr.head.orientation;
struct vec3 h = quat_rotate(quat_conjugate(hq), server->xr.aim);
double yaw, pitch;
vec3_yaw_pitch(h, &yaw, &pitch);
double lx = server->xr.fov_x / 2 * 0.95, ly = server->xr.fov_y / 2 * 0.95;
double cy = fmax(-lx, fmin(lx, yaw)), cp = fmax(-ly, fmin(ly, pitch));
if (cy != yaw || cp != pitch) {
server->xr.aim = quat_rotate(hq, direction(cy, cp));
}
}
static bool logo_held(struct c8_server *server) {
struct wlr_keyboard *kb = wlr_seat_get_keyboard(server->seat);
return kb && (wlr_keyboard_get_modifiers(kb) & WLR_MODIFIER_LOGO);
}
/* Resizing: the window's size follows the ray across its plane from
* where it began, its top left corner staying put; the metres along the
* ray to the plane, for the dot */
static double resize_follow(struct c8_server *server, struct vec3 o,
struct vec3 d) {
struct c8_view *view = server->xr.size_view;
anchor_apply(view);
double px, py, t;
if (!ray_slate(o, d, view->holo.pose, server->xr.m_per_px,
view->geo.width, view->geo.height, &px, &py, &t)) {
return INFINITY;
}
int w = (int)lround(server->xr.size_w + px - server->xr.size_px);
int h = (int)lround(server->xr.size_h + py - server->xr.size_py);
clamp_size(&w, &h);
if (w != view->holo.width || h != view->holo.height) {
view->holo.width = w;
view->holo.height = h;
c8_views_arrange(server);
}
return t;
}
/* While scrolling, the head's sway doesn't move the pointer: every glasses
* frame the ray lands a little elsewhere, and Chromium ends a touchpad
* scroll on any pointer motion, so pages stopped a moment in. A real
* turn of the head, past the slop, still does. */
#define SCROLL_HOLD_MS 1000
#define SCROLL_SLOP_PX 24
/* Put the cursor where the ray lands and tell whatever is there; `head`
* when it's the head that moved, not the mouse */
static void aim_apply(struct c8_server *server, uint32_t time, bool head) {
struct vec3 o = server->xr.head.position, d = server->xr.aim;
struct hit h;
struct wlr_seat_pointer_state *ps = &server->seat->pointer_state;
if (server->xr.size_view) {
h = (struct hit){ .t = resize_follow(server, o, d) };
} else if (server->xr.move_view) {
/* Carrying a slate: it rides the ray at its distance */
struct c8_view *view = server->xr.move_view;
double yaw, pitch;
vec3_yaw_pitch(d, &yaw, &pitch);
view->holo.pose = pose_at(yaw + server->xr.move_dyaw,
pitch + server->xr.move_dpitch, distance_of(view->holo.pose));
h = (struct hit){ .t = distance_of(view->holo.pose) };
} else if (ps->button_count > 0 && server->xr.grab_view &&
hit_view(server, server->xr.grab_view, o, d, true, &h)) {
/* Held down on a window: its plane, even past its edge, so the
* implicit grab sees the drag go on */
} else if (ps->button_count > 0 && server->xr.grab_shell) {
if (!hit_shell(server, o, d, true, &h)) {
h = (struct hit){ .t = INFINITY };
}
} else if (ps->button_count > 0 && server->xr.grab_bar) {
if (!hit_bar(server, o, d, true, &h)) {
h = (struct hit){ .t = INFINITY };
}
} else if (ps->button_count > 0 && server->xr.grab_hud) {
/* Dragging a toast away: its plane, past its edge too */
if (!hit_hud(server, o, d, true, &h)) {
h = (struct hit){ .t = INFINITY };
}
} else {
h = pick(server, o, d);
}
server->xr.aim_view = h.view;
server->xr.aim_shell = h.shell;
server->xr.aim_bar = h.bar;
server->xr.aim_hud = h.hud;
server->xr.aim_mini = h.mini;
server->xr.aim_chrome = h.chrome;
server->xr.aim_part = h.chrome ? h.part : C8_SLATEBAR_NONE;
server->xr.reveal_view = NULL;
if (h.view) {
struct c8_view *root = c8_view_root(h.view);
if (root->holo.theatre && root->geo.y + root->geo.height - h.ly <
THEATRE_REVEAL_PX) {
server->xr.reveal_view = root;
}
}
if (h.view || h.chrome) {
server->xr.chrome_view = h.chrome ? h.chrome : c8_view_root(h.view);
} else if (!server->xr.move_view && !server->xr.size_view) {
server->xr.chrome_view = NULL;
}
server->xr.aim_t = isfinite(h.t) ? h.t : server->config.glasses.distance;
bool on = h.view || h.shell || h.bar || h.hud;
double lx = on ? h.lx : NOWHERE, ly = on ? h.ly : NOWHERE;
if (lx == server->cursor->x && ly == server->cursor->y) {
return;
}
if (head && on && c8_now_ms() - server->xr.scrolled_ms < SCROLL_HOLD_MS &&
fabs(lx - server->cursor->x) < SCROLL_SLOP_PX &&
fabs(ly - server->cursor->y) < SCROLL_SLOP_PX) {
return;
}
if (h.shell) {
wlr_cursor_warp_closest(server->cursor, NULL, lx, ly);
} else {
/* Off every output, where wlr_cursor won't go: its position is
* only read back (by the hit test and the seat), so it is set
* by hand. Its image would stay behind on the shell's screen,
* so there is none. */
server->cursor->x = lx;
server->cursor->y = ly;
}
c8_pointer_motion(server, time, false);
}
bool c8_holo_cursor_hidden(struct c8_server *server) {
/* The dot everywhere, the shell's screen too: an arrow drawn into
* its picture there and a dot elsewhere was two pointers */
return c8_xr_active(server);
}
static void aim_start(struct c8_server *server) {
if (!server->xr.aim_set) {
double yaw, pitch;
gaze(server, &yaw, &pitch);
server->xr.aim = direction(yaw, pitch);
server->xr.aim_set = true;
}
}
/* The mouse turns the ray: a pixel of motion is about a pixel of slate */
bool c8_holo_pointer_motion(struct c8_server *server, double dx, double dy,
uint32_t time) {
if (!c8_xr_active(server)) {
return false;
}
aim_start(server);
double k = server->xr.m_per_px / server->config.glasses.distance;
double yaw, pitch;
vec3_yaw_pitch(server->xr.aim, &yaw, &pitch);
yaw -= dx * k;
pitch = fmax(-1.45, fmin(1.45, pitch - dy * k));
server->xr.aim = direction(yaw, pitch);
aim_in_view(server);
aim_apply(server, time, false);
return true;
}
/* The window you'll press on is the one you use (not above its dialog) */
static void focus_root(struct c8_view *root) {
if (!is_focused_root(root)) {
c8_focus_view(root);
}
}
/* Carrying a slate (and its dialogs): it rides the ray, kept where it was
* from it; held ends on release, sticky on the next click */
static void carry_begin(struct c8_server *server, struct c8_view *root,
bool sticky) {
double ayaw, apitch, syaw, spitch;
vec3_yaw_pitch(server->xr.aim, &ayaw, &apitch);
vec3_yaw_pitch(root->holo.pose.position, &syaw, &spitch);
server->xr.move_view = root;
server->xr.move_sticky = sticky;
root->holo.summon = false;
server->xr.move_dyaw = syaw - ayaw;
server->xr.move_dpitch = spitch - apitch;
root->holo.anchor_until = 0;
focus_root(root);
}
static void carry_end(struct c8_server *server) {
remember(server->xr.move_view);
server->xr.move_view = NULL;
server->xr.move_sticky = false;
c8_cursor_rebase(server);
}
static void resize_begin(struct c8_server *server, struct c8_view *root) {
double px = root->geo.width, py = root->geo.height, t;
ray_slate(server->xr.head.position, server->xr.aim, root->holo.pose,
server->xr.m_per_px, root->geo.width, root->geo.height, &px, &py,
&t);
server->xr.size_view = root;
server->xr.size_px = px;
server->xr.size_py = py;
server->xr.size_w = root->geo.width;
server->xr.size_h = root->geo.height;
root->holo.anchor = top_left(root);
focus_root(root);
}
static void resize_end(struct c8_server *server) {
struct c8_view *view = server->xr.size_view;
/* The app may still be catching up: its corner stays put a while */
view->holo.anchor_until = c8_now_ms() + ANCHOR_MS;
/* Kept where it will be once the app has its new size */
struct pose done = view->holo.pose;
done.position = centre_from_anchor(view, view->holo.width,
view->holo.height);
remember_at(view, done);
server->xr.size_view = NULL;
c8_cursor_rebase(server);
}
/* A button of the bar, released over it */
static void chrome_act(struct c8_server *server, struct c8_view *view,
int part) {
switch (part) {
case C8_SLATEBAR_FOLLOW:
view->holo.follow = !view->holo.follow;
remember(view);
break;
case C8_SLATEBAR_MOVE:
carry_begin(server, view, true);
break;
case C8_SLATEBAR_MINIMISE:
c8_view_minimize(view);
break;
case C8_SLATEBAR_MAXIMISE:
/* Ours until the app asks something itself: an app that went
* fullscreen comes out of it, and one we put there is told
* it's fullscreen, so its own key for that brings it back */
view->holo.theatre_ask = view->holo.theatre ? -1 : 1;
c8_views_arrange(server);
break;
case C8_SLATEBAR_STEREO:
/* Flat, side by side, one over the other, and round again */
view->holo.stereo = (view->holo.stereo + 1) % 3;
c8_placement_put_stereo(server, c8_view_app_id(view),
view->holo.stereo);
break;
case C8_SLATEBAR_CLOSE:
c8_view_close(view);
break;
default:
break;
}
}
bool c8_holo_button(struct c8_server *server, uint32_t button,
bool pressed) {
if (!c8_xr_active(server)) {
return false;
}
if (!pressed && server->xr.swallow_release) {
server->xr.swallow_release = false;
return true; // the click that ended a move or resize
}
/* Moving or resizing: the press and release that do it are ours.
* Held ends on the release, sticky (Move) on a click. */
if (server->xr.size_view) {
if (!pressed) {
resize_end(server);
}
return true;
}
if (server->xr.move_view) {
if (server->xr.move_sticky ? pressed : !pressed) {
carry_end(server);
server->xr.swallow_release = pressed;
}
return true;
}
if (server->xr.press_view) {
/* A bar button acts when released over it */
if (!pressed) {
struct c8_view *view = server->xr.press_view;
int part = server->xr.press_part;
server->xr.press_view = NULL;
server->xr.press_part = C8_SLATEBAR_NONE;
if (server->xr.aim_chrome == view &&
server->xr.aim_part == part) {
chrome_act(server, view, part);
}
}
return true;
}
if (server->xr.press_mini) {
/* A minimised window's icon: back when released over it, flying
* to you if where it was is out of view */
if (!pressed) {
struct c8_view *view = server->xr.press_mini;
server->xr.press_mini = NULL;
if (server->xr.aim_mini == view && !c8_locked(server)) {
c8_view_activate(view);
}
}
return true;
}
struct wlr_seat_pointer_state *ps = &server->seat->pointer_state;
if (pressed && ps->button_count == 0) {
server->xr.swallow_release = false;
if (server->xr.aim_mini && !c8_locked(server)) {
/* As a press on the bar: it stays up */
if (button == BTN_LEFT) {
server->xr.press_mini = server->xr.aim_mini;
} else {
server->xr.swallow_release = true;
}
return true;
}
if (server->xr.aim_chrome && !c8_locked(server)) {
struct c8_view *view = server->xr.aim_chrome;
server->xr.bar_pinned = false; // pressed elsewhere
c8_shell_bar_dismiss(server);
if (button != BTN_LEFT) {
focus_root(view);
server->xr.swallow_release = true;
} else if (server->xr.aim_part == C8_SLATEBAR_TITLE) {
carry_begin(server, view, false);
} else if (server->xr.aim_part == C8_SLATEBAR_CORNER) {
resize_begin(server, view);
} else {
server->xr.press_view = view;
server->xr.press_part = server->xr.aim_part;
}
return true;
}
/* Super + drag carries a window's slate (and its dialogs) */
if (server->xr.aim_view && logo_held(server) && !c8_locked(server)) {
carry_begin(server, c8_view_root(server->xr.aim_view), false);
server->super_alone = false; // not a tap for Start
return true;
}
/* A press off Start (on a window round it, or on nothing) puts
* it away; a window pressed is the one you get. With no window
* Start stays: there's nothing else to show (c8_start_show). */
if (server->start_visible && !server->xr.aim_shell &&
!server->xr.aim_bar && !server->xr.aim_hud &&
!c8_locked(server) && c8_top_view(server)) {
c8_shell_bar_dismiss(server);
c8_start_show(server, false);
if (server->xr.aim_view) {
focus_root(c8_view_root(server->xr.aim_view));
}
server->xr.swallow_release = true;
return true;
}
server->xr.grab_view = server->xr.aim_view;
server->xr.grab_shell = server->xr.aim_shell;
server->xr.grab_bar = server->xr.aim_bar;
server->xr.grab_hud = server->xr.aim_hud;
if (!server->xr.aim_bar) {
server->xr.bar_pinned = false; // pressed elsewhere
c8_shell_bar_dismiss(server);
}
}
return false;
}
void c8_holo_button_done(struct c8_server *server) {
if (server->seat->pointer_state.button_count == 0) {
server->xr.grab_view = NULL;
server->xr.grab_shell = false;
server->xr.grab_bar = false;
server->xr.grab_hud = false;
}
}
/* Super + wheel brings the slate pointed at nearer or sends it further;
* while one is carried, the wheel alone does */
bool c8_holo_axis(struct c8_server *server, double delta) {
if (!c8_xr_active(server) || delta == 0) {
return false;
}
struct c8_view *root = server->xr.move_view;
if (root == NULL && server->xr.aim_view && logo_held(server)) {
root = c8_view_root(server->xr.aim_view);
}
if (root == NULL) {
return server->xr.size_view != NULL; // nothing to scroll then
}
root->holo.anchor_until = 0;
double d = distance_of(root->holo.pose);
double nd = fmax(DIST_MIN, fmin(DIST_MAX, d * (delta > 0 ? 1.1 : 1 / 1.1)));
root->holo.pose.position = vec3_scale(root->holo.pose.position, nd / d);
if (root != server->xr.move_view) {
remember(root); // a carried one is kept when it's put down
}
server->super_alone = false;
return true;
}
void c8_holo_summon(struct c8_view *view) {
struct c8_server *server = view ? view->server : NULL;
if (view == NULL || !c8_xr_active(server) || c8_locked(server)) {
return;
}
struct c8_view *root = c8_view_root(view);
if (!root->holo.placed) {
return; // not open yet: it opens where you look anyway
}
double gy, gp;
look(server, &gy, &gp);
double half = server->xr.fov_x > 0 ? server->xr.fov_x / 2 : 0.4;
if (angle_between(direction(gy, gp), root->holo.pose.position) >
half * SUMMON_VIEW) {
root->holo.summon = true;
root->holo.anchor_until = 0;
}
}
/* A step of the way from where a slate is to a yaw and pitch, at its
* distance: how things drift after your gaze */
static struct pose pose_toward(struct pose p, double yaw, double pitch,
double rate) {
double sy, sp;
vec3_yaw_pitch(p.position, &sy, &sp);
return pose_at(sy + remainder(yaw - sy, 2 * M_PI) * rate,
sp + (pitch - sp) * rate, distance_of(p));
}
/* A picked window flies in; true while it's on its way */
static bool summon_step(struct c8_view *view, double gy, double gp) {
double sy, sp;
vec3_yaw_pitch(view->holo.pose.position, &sy, &sp);
double dy = remainder(gy - sy, 2 * M_PI), dp = gp - sp;
if (fabs(dy) < RAD(SUMMON_DONE_DEG) &&
fabs(dp) < RAD(SUMMON_DONE_DEG)) {
view->holo.summon = false;
return false;
}
view->holo.pose = pose_toward(view->holo.pose, gy, gp, SUMMON_RATE);
return true;
}
/* Windows being resized keep their corner; picked ones fly in; Follow me
* ones drift after your gaze once it's gone far enough from them */
static void windows_frame(struct c8_server *server) {
double gy, gp;
look(server, &gy, &gp);
struct vec3 g = direction(gy, gp);
struct c8_view *view;
wl_list_for_each(view, &server->views, link) {
anchor_apply(view);
if (view->holo.summon) {
if (view == server->xr.move_view ||
view == server->xr.size_view ||
summon_step(view, gy, gp)) {
continue;
}
}
if (!view->holo.follow || !view->holo.placed ||
c8_view_root(view) != view ||
view == server->xr.move_view ||
view == server->xr.size_view ||
c8_now_ms() <= view->holo.anchor_until) {
continue;
}
double sy, sp;
vec3_yaw_pitch(view->holo.pose.position, &sy, &sp);
if (angle_between(g, direction(sy, sp)) > RAD(FOLLOW_ME_DEG)) {
view->holo.pose = pose_toward(view->holo.pose, gy, gp,
FOLLOW_RATE);
}
}
}
/* Beacons: a window that opens out of view shows its icon at the edge of
* the view, on the side where it is, for a moment */
/* Where its centre is from where the head points, as yaw and pitch */
static void from_head(struct c8_server *server, struct vec3 at, double *yaw,
double *pitch) {
struct pose head = server->xr.head;
vec3_yaw_pitch(quat_rotate(quat_conjugate(head.orientation),
vec3_sub(at, head.position)), yaw, pitch);
}
static bool in_view(struct c8_server *server, struct c8_view *view) {
double yaw, pitch;
from_head(server, c8_holo_pose(view).position, &yaw, &pitch);
return fabs(yaw) < server->xr.fov_x / 2 &&
fabs(pitch) < server->xr.fov_y / 2;
}
/* Told after it mapped; whether it's out of view is known once it's
* placed (a frame later at most) */
void c8_holo_view_mapped(struct c8_view *view) {
if (c8_xr_active(view->server) && !c8_locked(view->server)) {
view->holo.beacon_at = c8_now_ms();
view->holo.beacon_pending = true;
}
}
static void beacons_frame(struct c8_server *server) {
int64_t now = c8_now_ms();
struct c8_view *view;
wl_list_for_each(view, &server->views, link) {
if (view->holo.beacon_at == 0) {
continue;
}
struct c8_view *root = c8_view_root(view);
if (view->holo.beacon_pending) {
if (!root->holo.placed || server->xr.fov_x <= 0) {
continue;
}
view->holo.beacon_pending = false;
}
/* Gone once you see it (or it flew to you), or after a while */
if (now - view->holo.beacon_at > BEACON_MS || !view->mapped ||
root->holo.summon || in_view(server, view)) {
view->holo.beacon_at = 0;
}
}
}
/* The beacon to draw for a window, if it has one: where on the view's
* edge (from the head, as yaw and pitch) and how strong, fading out over
* its last part */
bool c8_holo_beacon(struct c8_view *view, double *yaw, double *pitch,
float *alpha) {
struct c8_server *server = view->server;
if (view->holo.beacon_at == 0 || view->holo.beacon_pending ||
c8_locked(server)) {
return false;
}
from_head(server, c8_holo_pose(view).position, yaw, pitch);
/* Onto an ellipse well in from the view's edge, so both eyes see it
* where the glasses are sharp */
double ex = server->xr.fov_x / 2 * BEACON_REACH,
ey = server->xr.fov_y / 2 * BEACON_REACH;
double r = hypot(*yaw / ex, *pitch / ey);
if (r > 0) {
*yaw /= r;
*pitch /= r;
}
double left = BEACON_MS - (c8_now_ms() - view->holo.beacon_at);
*alpha = (float)fmax(0, fmin(1, left / (BEACON_MS / 4.0)));
return true;
}
/* The shell's screen is drawn over the windows, so it must be nearer
* than any it may cover: else in 3D the eyes see something far covering
* something near. It comes in front of them, its pixels smaller by as
* much, so in one eye nothing changes. */
static void shell_front(struct c8_server *server) {
struct pose p = server->xr.shell_pose;
double d = distance_of(p), want = d;
bool locked = c8_locked(server); // no window shows then
struct c8_view *view;
wl_list_for_each(view, &server->views, link) {
if (locked || !view->mapped || !view->holo.slot ||
!view->tree->node.enabled || c8_holo_minimised(view)) {
continue;
}
struct pose vp = c8_holo_pose(view);
double vd = distance_of(vp);
if (vd - SHELL_AHEAD < want && angle_between(vp.position,
p.position) < RAD(SHELL_NEAR_DEG)) {
want = vd - SHELL_AHEAD;
}
}
want = fmax(want, DIST_MIN / 2);
server->xr.shell_drawn = (struct pose){
vec3_scale(p.position, want / fmax(d, 1e-6)), p.orientation };
server->xr.shell_m = server->xr.m_per_px * want / fmax(d, 1e-6);
}
struct pose c8_holo_shell_pose(struct c8_server *server, double *m) {
*m = server->xr.shell_m > 0 ? server->xr.shell_m : server->xr.m_per_px;
return server->xr.shell_m > 0 ? server->xr.shell_drawn :
server->xr.shell_pose;
}
/* Every glasses frame: the head moved, so the ray may land elsewhere, and
* the shell's screen drifts after your gaze while nothing is up on it */
void c8_holo_frame(struct c8_server *server, struct pose head,
double m_per_px, double fov_x, double fov_y) {
server->xr.head = head;
server->xr.m_per_px = m_per_px;
server->xr.fov_x = fov_x;
server->xr.fov_y = fov_y;
if (!server->xr.shell_placed) {
place_shell(server);
server->xr.shell_placed = true;
}
bar_frame(server);
hud_frame(server);
windows_frame(server);
beacons_frame(server);
if (!server->start_visible && !c8_shell_panel_open(server) &&
server->seat->pointer_state.button_count == 0) {
double gy, gp, sy, sp;
look(server, &gy, &gp);
vec3_yaw_pitch(server->xr.shell_pose.position, &sy, &sp);
if (angle_between(direction(gy, gp), direction(sy, sp)) >
RAD(FOLLOW_DEG)) {
server->xr.shell_pose = pose_toward(server->xr.shell_pose, gy,
gp, FOLLOW_RATE);
}
}
shell_front(server);
aim_start(server);
aim_in_view(server);
aim_apply(server, (uint32_t)c8_now_ms(), true);
}