Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/app_dirs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <filesystem>

// Where the player keeps its files: ~/.config/videoframevr (or under
// $XDG_CONFIG_HOME). A folder from the former name "vrplayer" is moved there.
std::filesystem::path configDir();
// Where it keeps what it collects (the media library): ~/.local/share/videoframevr
// (or under $XDG_DATA_HOME).
std::filesystem::path dataDir();
// Where it keeps what can be fetched again (Stash's images): ~/.cache/videoframevr
// (or under $XDG_CACHE_HOME).
std::filesystem::path cacheDir();
11 changes: 9 additions & 2 deletions src/cairo_canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ CairoCanvas::~CairoCanvas() {
if (spare_.image) uploader_->gpu().retire(spare_);
}

bool CairoCanvas::init(Uploader& uploader, int width, int height, float scale, bool scrolls) {
uploader_ = &uploader;
bool CairoCanvas::init(int width, int height, float scale) {
width_ = width;
height_ = height;
scale_ = scale;
Expand All @@ -56,6 +55,12 @@ bool CairoCanvas::init(Uploader& uploader, int width, int height, float scale, b
if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) return false;
cairo_surface_set_device_scale(surface_, scale, scale);
cr_ = cairo_create(surface_);
return true;
}

bool CairoCanvas::init(Uploader& uploader, int width, int height, float scale, bool scrolls) {
uploader_ = &uploader;
if (!init(width, height, scale)) return false;

// Down to an eighth of the size at scale 1, a sixteenth at 2.
const uint32_t full =
Expand Down Expand Up @@ -90,6 +95,7 @@ void CairoCanvas::clear(int firstRow, int endRow) {
}

void CairoCanvas::upload(int firstRow, int endRow, int firstColumn, int endColumn) {
if (!uploader_) return;
if (endRow < 0) endRow = height_;
if (endColumn < 0) endColumn = width_;
// In the surface's pixels.
Expand Down Expand Up @@ -135,6 +141,7 @@ void CairoCanvas::finishUpdate() {
}

void CairoCanvas::shiftUp(int x0, int y0, int x1, int y1, double dy) {
if (!uploader_) return;
if (!spare_.image) {
std::fprintf(stderr, "CairoCanvas: shiftUp() on a canvas made without scrolling\n");
return;
Expand Down
3 changes: 3 additions & 0 deletions src/cairo_canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class CairoCanvas {
// pixels per unit (sharper text for the same drawing). scrolls: shiftUp()
// is used, which needs a spare image of the same size.
bool init(Uploader& uploader, int width, int height, float scale = 1.0f, bool scrolls = false);
// The surface alone, without an image: uploads and shifts are dropped and
// texture() has nothing (drawing and measuring without a GPU).
bool init(int width, int height, float scale = 1.0f);

cairo_t* cr() const { return cr_; }
cairo_surface_t* surface() const { return surface_; }
Expand Down
230 changes: 230 additions & 0 deletions src/control_map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
#pragma once

// The controllers' buttons, the jobs they can do and the gestures, and what
// the user chose for each controller type (ControlMap, GestureMap), as the
// settings keep them. The rest is in controls.h.

#include <cstdint>
#include <memory>
#include <string>
#include <vector>

// The buttons OpenXR is asked for, one boolean action each, on both hands.
// The thumbstick (a vector) and the pointer's pose are read on their own.
enum class Control : uint8_t {
Trigger, Bumper, Grip, A, B, X, Y, Menu, View, DpadUp, DpadDown, DpadLeft, DpadRight, StickClick,
Count
};
constexpr int kControls = static_cast<int>(Control::Count);
constexpr uint32_t controlBit(Control c) { return 1u << static_cast<int>(c); }

// What a button can do, in the order the chooser shows them.
enum class Job : uint8_t {
None,
Menu, Recenter, Choose,
PlayPause, Stop, SkipBack, SkipForward, BigSkipBack, BigSkipForward, FrameBack, FrameForward,
PreviousChapter, NextChapter, FromStart, Scripts,
PreviousVideo, NextVideo, AfterVideo, RatingUp, RatingDown, DeleteVideo,
VolumeUp, VolumeDown, Mute, NextAudioTrack, Subtitles,
Passthrough, PeekRoom, SeeThrough, MatchRoom, RemoveBackground, ToneLook, TvGamma, Motion, ZoomIn, ZoomOut,
StereoFocus, ShowFocusTarget, ResetScreen, ResetPicture,
Count
};
constexpr int kJobs = static_cast<int>(Job::Count);

// The controller types with a layout of their own, by OpenXR interaction
// profile. Others reach the app through SteamVR's remapping onto one of
// them.
enum class ControllerType : uint8_t { Frame, Index, Touch, Simple, Count };
constexpr int kControllerTypes = static_cast<int>(ControllerType::Count);

// A changeable control of a type, alone or with grip held, as a menu
// command carries it (MenuCommand::option).
struct ControlSlot {
ControllerType type = ControllerType::Count;
int hand = 0;
Control control = Control::Count;
bool grip = false;
int pack() const {
return static_cast<int>(type) << 6 | grip << 5 | hand << 4 | static_cast<int>(control);
}
static ControlSlot unpack(int packed) {
return {static_cast<ControllerType>(packed >> 6 & 7), packed >> 4 & 1,
static_cast<Control>(packed & 15), (packed >> 5 & 1) != 0};
}
bool operator==(const ControlSlot& o) const {
return type == o.type && hand == o.hand && control == o.control && grip == o.grip;
}
};

// What each controller type's buttons do: the user's choices, else the
// layout's defaults. Kept in the settings as each type's changes. With
// that hand's grip held, a button does its grip job if it has one (none by
// default; Nothing is one), else its own.
class ControlMap {
public:
ControlMap();
// Job::None for a control the type hasn't on that hand; on an unknown
// type, the trigger chooses and nothing else does anything.
Job job(ControllerType type, int hand, Control control) const;
// A changeable control's job; false if it is fixed or not there.
bool set(ControllerType type, int hand, Control control, Job job);
// How many of the type's controls do it.
int count(ControllerType type, Job job) const;
// A change Menu allows: it always keeps a button (one of them fixed, or
// the last one left).
bool allowed(ControllerType type, int hand, Control control, Job job) const;
// A grip job of its own (Nothing too), and which: Job::None without
// one. A stick's click (with grip held it clears a rating) and a button
// that chooses (with grip held it gives volume, as the trigger does) take
// none; a button's grip job goes when it is set to choose.
bool hasGrip(ControllerType type, int hand, Control control) const;
Job gripJob(ControllerType type, int hand, Control control) const;
bool takesGrip(ControllerType type, int hand, Control control) const;
bool setGrip(ControllerType type, int hand, Control control, Job job);
// Back to doing with grip held what it does alone.
bool clearGrip(ControllerType type, int hand, Control control);
// What a press does, with that hand's grip held or not.
Job pressJob(ControllerType type, int hand, Control control, bool gripHeld) const {
return gripHeld && hasGrip(type, hand, control) ? gripJob(type, hand, control)
: job(type, hand, control);
}
// A change of a slot's job, alone or with grip held.
bool set(const ControlSlot& s, Job job) {
return s.grip ? setGrip(s.type, s.hand, s.control, job) : set(s.type, s.hand, s.control, job);
}
// A button's jobs alone and with grip held (none of its own: -1) as one
// number, and those put back: an Undo's.
int button(ControllerType type, int hand, Control control) const;
bool restore(ControllerType type, int hand, Control control, int button);
// Every button alone and with grip held back to its default.
void reset(ControllerType type);
// The changes from the defaults as one line ("right_b_button:none,...",
// grip jobs as "grip_left_x_button:stop"), and such a line read: unknown
// controls and jobs are left out, so they keep their defaults.
std::string line(ControllerType type) const;
void read(ControllerType type, const std::string& line);
bool operator==(const ControlMap& o) const;
bool operator!=(const ControlMap& o) const { return !(*this == o); }

private:
static constexpr uint8_t kDefault = 0xff;
uint8_t chosen_[kControllerTypes][2][kControls]; // a Job, or kDefault
uint8_t grip_[kControllerTypes][2][kControls]; // a Job, or kDefault: none
// Menu always keeps a button: a type's read without one gets the layout's.
void keepMenu(ControllerType type);
};

// How a gesture's hand moves while its buttons are held: only the wrist's
// twist for now. Saved by id, so others can be added.
enum class Movement : uint8_t { Twist, Count };
constexpr int kMovements = static_cast<int>(Movement::Count);
const char* movementId(Movement movement);

// What a gesture changes: a value the menu also sets, in the chooser's
// order.
enum class GestureValue : uint8_t {
Volume,
Opacity, MatchBrightness, MatchColour, KeyStrength, KeyEdges, KeySpill, KeyShrink,
Brightness, Contrast, Saturation, Warmth, Tint, Exposure, Sharpen, Zoom,
Viewpoint, ScreenDistance, ScreenHeight, ScreenCurve,
Scrub, Rating,
ScriptTiming,
SubtitleSize, SubtitleRaise, SubtitleDistance,
Count
};
constexpr int kGestureValues = static_cast<int>(GestureValue::Count);

// A gesture: one button, or two on one controller, held, and the hand's
// movement changes a value. The trigger is only ever the second; the
// trigger alone isn't one (it chooses and drags).
struct Gesture {
uint8_t hands = 0; // a bit per hand that does it: 1 left, 2 right, 3 either
Control first = Control::Count;
Control second = Control::Count; // Count: one button
Movement movement = Movement::Twist;
GestureValue value = GestureValue::Count;

bool has(Control c) const { return c != Control::Count && (first == c || second == c); }
int controls() const { return second == Control::Count ? 1 : 2; }
// Its buttons as bits (controlBit()).
uint32_t bits() const {
return controlBit(first) | (second == Control::Count ? 0u : controlBit(second));
}
// The same buttons and movement on a hand of both: one replaces the other.
bool sameHold(const Gesture& o) const {
return (hands & o.hands) != 0 && bits() == o.bits() && movement == o.movement;
}
// As a menu command carries it (MenuCommand::option, with the type).
int pack() const {
return static_cast<int>(value) << 12 | static_cast<int>(movement) << 10 |
static_cast<int>(second) << 6 | static_cast<int>(first) << 2 | hands;
}
static Gesture unpack(int packed) {
Gesture g;
g.hands = static_cast<uint8_t>(packed & 3);
g.first = static_cast<Control>(packed >> 2 & 15);
g.second = static_cast<Control>(packed >> 6 & 15);
g.movement = static_cast<Movement>(packed >> 10 & 3);
g.value = static_cast<GestureValue>(packed >> 12 & 31);
return g;
}
bool operator==(const Gesture& o) const {
return hands == o.hands && first == o.first && second == o.second && movement == o.movement &&
value == o.value;
}
bool operator!=(const Gesture& o) const { return !(*this == o); }
};
constexpr int kMaxGestures = 12; // a controller type's

// Each controller type's gestures: the user's, else the layout's defaults.
// Kept in the settings as a type's whole list once it differs from them.
// Each type's list is shared between copies until one of them changes, so
// the menu's copy each frame costs no more than a pointer's.
class GestureMap {
public:
GestureMap();
const std::vector<Gesture>& list(ControllerType type) const;
static const std::vector<Gesture>& defaults(ControllerType type);
// Whether the type can have it: a known value and movement, its buttons
// on each of its hands and changeable or the grip, the trigger only
// second, not one button twice.
static bool valid(ControllerType type, const Gesture& g);
// A new one, in place of any with the same hold (those go into
// `replaced`, for Undo; one of either hand's stays on the hand `g` leaves
// it); false if it isn't valid, or that would be more than kMaxGestures or
// than its column has room for.
bool add(ControllerType type, const Gesture& g, std::vector<Gesture>* replaced = nullptr);
// The index-th changed into `g` (another value, or other buttons), also in
// place of any other with its hold, as add() does; `replaced` gets the old
// one first.
bool replace(ControllerType type, size_t index, const Gesture& g,
std::vector<Gesture>* replaced = nullptr);
bool remove(ControllerType type, size_t index);
// A list put back (Undo); those not valid are left out.
void restore(ControllerType type, const std::vector<Gesture>& gestures);
void reset(ControllerType type); // the defaults again
bool full(ControllerType type) const { return list(type).size() >= kMaxGestures; }
// The gesture of exactly these buttons on that hand (b: Count for one), or null.
const Gesture* find(ControllerType type, int hand, Control a, Control b = Control::Count,
Movement movement = Movement::Twist) const;
// A press of `control` on that hand waits to do its job until it is let go
// (and does it only if no gesture got under way meanwhile): it is a
// gesture's button, with the grip only while that is held (`held`:
// controlBit()s). Never the trigger's.
bool tapOnRelease(ControllerType type, int hand, Control control, uint32_t held) const;
// The list as one line ("right:y_button:twist:opacity,either:grip+trigger:
// twist:volume"); "none" without any. line() is empty while it is the
// defaults. A line read: entries with unknown ids, or not valid, are left
// out; an empty one is the defaults.
std::string line(ControllerType type) const;
std::string listLine(ControllerType type) const;
void read(ControllerType type, const std::string& line);
bool operator==(const GestureMap& o) const;
bool operator!=(const GestureMap& o) const { return !(*this == o); }

private:
using List = std::shared_ptr<const std::vector<Gesture>>;
static const List& defaultList(ControllerType type);
List lists_[kControllerTypes];
};
6 changes: 0 additions & 6 deletions src/controls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -938,12 +938,6 @@ void GestureMap::reset(ControllerType type) {
if (type != ControllerType::Count) lists_[static_cast<int>(type)] = defaultList(type);
}

bool GestureMap::hasGesture(ControllerType type, int hand, Control control) const {
for (const Gesture& g : list(type))
if ((g.hands >> hand & 1) && g.has(control)) return true;
return false;
}

const Gesture* GestureMap::find(ControllerType type, int hand, Control a, Control b,
Movement movement) const {
const uint32_t bits = controlBit(a) | (b == Control::Count ? 0u : controlBit(b));
Expand Down
Loading