From 2242c2c82430bb1b1fa2171fb57b67882917333b Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sun, 2 Apr 2023 14:54:44 +0200 Subject: [PATCH 1/5] grab entire screen(s), lower framerate --- src/main.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 8ce5919..a491c9c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,8 +9,10 @@ static vr::ETrackingUniverseOrigin TRACKING_UNIVERSE = vr::ETrackingUniverseOrigin::TrackingUniverseStanding; -#define WIDTH 1024 -#define HEIGHT 1024 +#define FRAMERATE 30 + +uint16_t width; +uint16_t height; Display *xdisplay; Window root_window; @@ -32,12 +34,16 @@ int main(int argc, char **argv) assert(xdisplay != nullptr); printf("Created X11 display\n"); root_window = XRootWindow(xdisplay, 0); + XWindowAttributes attributes; + XGetWindowAttributes(xdisplay, root_window, &attributes); + width = attributes.width; + height = attributes.height; } { assert(glfwInit() == true); glfwWindowHint(GLFW_VISIBLE, false); - gl_window = glfwCreateWindow(WIDTH, HEIGHT, "Overlay", nullptr, nullptr); + gl_window = glfwCreateWindow(width, height, "Overlay", nullptr, nullptr); assert(gl_window != nullptr); glfwMakeContextCurrent(gl_window); printf("Created GLFW context\n"); @@ -82,8 +88,8 @@ int main(int argc, char **argv) while (1) { - auto frame = XGetImage(xdisplay, root_window, 0, 0, WIDTH, HEIGHT, AllPlanes, ZPixmap); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, WIDTH, HEIGHT, 0, GL_BGRA, GL_UNSIGNED_BYTE, frame->data); + auto frame = XGetImage(xdisplay, root_window, 0, 0, width, height, AllPlanes, ZPixmap); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, frame->data); XDestroyImage(frame); { @@ -97,7 +103,7 @@ int main(int argc, char **argv) } glfwSwapBuffers(gl_window); - ovr_overlay->WaitFrameSync(20); + usleep(1000000 / FRAMERATE); } cleanup(); return 0; From e7df6a277692f6072946948fe84a2636a1ab1059 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sun, 2 Apr 2023 15:38:27 +0200 Subject: [PATCH 2/5] display cursor position --- src/main.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index a491c9c..4d4e515 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -61,9 +61,8 @@ int main(int argc, char **argv) auto overlay_err = ovr_overlay->CreateOverlay("deskpot", "Desktop view", &handle); assert(overlay_err == 0); ovr_overlay->ShowOverlay(handle); - ovr_overlay->SetOverlayTextureColorSpace(handle, vr::EColorSpace::ColorSpace_Gamma); - ovr_overlay->SetOverlayWidthInMeters(handle, 0.5f); - uint8_t col[4] = {20, 100, 100, 255}; + ovr_overlay->SetOverlayWidthInMeters(handle, 2.5f); + uint8_t col[4] = {20, 50, 50, 255}; ovr_overlay->SetOverlayRaw(handle, &col, 1, 1, 4); printf("Created overlay instance\n"); } @@ -101,6 +100,22 @@ int main(int argc, char **argv) assert(set_err == 0); } } + { + int pix_x, pix_y; + { + Window _t1; + int _t2; + unsigned int _t3; + XQueryPointer(xdisplay, root_window, &_t1, &_t1, &pix_x, &pix_y, &_t2, &_t2, &_t3); + } + float ratio = (float)height / (float)width; + float top_edge = 0.5f - ratio / 2.0f; + float x = pix_x / (float)width; + float y = pix_y / (float)width + top_edge; + auto pos = vr::HmdVector2_t{x, y}; + ovr_overlay->SetOverlayCursorPositionOverride(handle, &pos); + } + glfwSwapBuffers(gl_window); usleep(1000000 / FRAMERATE); @@ -111,9 +126,10 @@ int main(int argc, char **argv) void cleanup(int _sig) { - printf("\nShutting down\n"); + printf("\nCleaning up\n"); vr::VR_Shutdown(); glfwDestroyWindow(gl_window); glfwTerminate(); + printf("Shutting down\n"); exit(0); } \ No newline at end of file From ef2539290784f32dc420483eb35c537911ac190a Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sun, 2 Apr 2023 16:39:06 +0200 Subject: [PATCH 3/5] cleanup --- src/main.cpp | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 4d4e515..a7dbb0e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,6 @@ #include #include -#include #include #include @@ -19,9 +18,10 @@ Window root_window; vr::IVRSystem *ovr_sys; vr::IVROverlay *ovr_overlay; -vr::VROverlayHandle_t handle; +vr::VROverlayHandle_t main_overlay; +vr::Texture_t vr_texture; -GLuint overlaytexture; +GLuint screen_texture; GLFWwindow *gl_window; void cleanup(int _sig = 0); @@ -58,32 +58,32 @@ int main(int argc, char **argv) } { - auto overlay_err = ovr_overlay->CreateOverlay("deskpot", "Desktop view", &handle); + auto overlay_err = ovr_overlay->CreateOverlay("deskpot", "Desktop view", &main_overlay); assert(overlay_err == 0); - ovr_overlay->ShowOverlay(handle); - ovr_overlay->SetOverlayWidthInMeters(handle, 2.5f); + ovr_overlay->ShowOverlay(main_overlay); + ovr_overlay->SetOverlayWidthInMeters(main_overlay, 2.5f); uint8_t col[4] = {20, 50, 50, 255}; - ovr_overlay->SetOverlayRaw(handle, &col, 1, 1, 4); + ovr_overlay->SetOverlayRaw(main_overlay, &col, 1, 1, 4); printf("Created overlay instance\n"); } { vr::HmdMatrix34_t transform; - auto err = ovr_overlay->GetOverlayTransformAbsolute(handle, &TRACKING_UNIVERSE, &transform); + auto err = ovr_overlay->GetOverlayTransformAbsolute(main_overlay, &TRACKING_UNIVERSE, &transform); assert(err == 0); transform.m[1][1] = -1; - err = ovr_overlay->SetOverlayTransformAbsolute(handle, TRACKING_UNIVERSE, &transform); + err = ovr_overlay->SetOverlayTransformAbsolute(main_overlay, TRACKING_UNIVERSE, &transform); assert(err == 0); } { - glGenTextures(1, &overlaytexture); - glBindTexture(GL_TEXTURE_2D, overlaytexture); - } + glGenTextures(1, &screen_texture); + glBindTexture(GL_TEXTURE_2D, screen_texture); - vr::Texture_t vr_texture; - vr_texture.eColorSpace = vr::EColorSpace::ColorSpace_Auto; - vr_texture.eType = vr::ETextureType::TextureType_OpenGL; + vr_texture.eColorSpace = vr::EColorSpace::ColorSpace_Auto; + vr_texture.eType = vr::ETextureType::TextureType_OpenGL; + vr_texture.handle = (void *)(uintptr_t)screen_texture; + } while (1) { @@ -92,15 +92,15 @@ int main(int argc, char **argv) XDestroyImage(frame); { - vr_texture.handle = (void *)(uintptr_t)overlaytexture; - auto set_err = ovr_overlay->SetOverlayTexture(handle, &vr_texture); + auto set_err = ovr_overlay->SetOverlayTexture(main_overlay, &vr_texture); if (set_err) { printf("error setting texture: %d\n", set_err); assert(set_err == 0); } } - { + + { // update cursor position int pix_x, pix_y; { Window _t1; @@ -108,16 +108,16 @@ int main(int argc, char **argv) unsigned int _t3; XQueryPointer(xdisplay, root_window, &_t1, &_t1, &pix_x, &pix_y, &_t2, &_t2, &_t3); } + // TODO: make this work when aspect ratio is >1 (root window is taller than it is wide) float ratio = (float)height / (float)width; float top_edge = 0.5f - ratio / 2.0f; float x = pix_x / (float)width; float y = pix_y / (float)width + top_edge; auto pos = vr::HmdVector2_t{x, y}; - ovr_overlay->SetOverlayCursorPositionOverride(handle, &pos); + ovr_overlay->SetOverlayCursorPositionOverride(main_overlay, &pos); } glfwSwapBuffers(gl_window); - usleep(1000000 / FRAMERATE); } cleanup(); @@ -132,4 +132,4 @@ void cleanup(int _sig) glfwTerminate(); printf("Shutting down\n"); exit(0); -} \ No newline at end of file +} From 6aef8f92d78b6def631af926a1d2c23527399bc5 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sun, 2 Apr 2023 16:53:04 +0200 Subject: [PATCH 4/5] move everything to functions --- src/main.cpp | 206 +++++++++++++++++++++++++++------------------------ 1 file changed, 109 insertions(+), 97 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index a7dbb0e..57c2e6e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,7 @@ static vr::ETrackingUniverseOrigin TRACKING_UNIVERSE = vr::ETrackingUniverseOrig uint16_t width; uint16_t height; +bool should_exit = false; Display *xdisplay; Window root_window; @@ -24,98 +25,119 @@ vr::Texture_t vr_texture; GLuint screen_texture; GLFWwindow *gl_window; -void cleanup(int _sig = 0); - -int main(int argc, char **argv) +void init_x11() { - signal(SIGINT, cleanup); + xdisplay = XOpenDisplay(nullptr); + assert(xdisplay != nullptr); + printf("Created X11 display\n"); + root_window = XRootWindow(xdisplay, 0); + XWindowAttributes attributes; + XGetWindowAttributes(xdisplay, root_window, &attributes); + width = attributes.width; + height = attributes.height; +} + +void init_glfw() +{ + assert(glfwInit() == true); + glfwWindowHint(GLFW_VISIBLE, false); + gl_window = glfwCreateWindow(width, height, "Overlay", nullptr, nullptr); + assert(gl_window != nullptr); + glfwMakeContextCurrent(gl_window); + printf("Created GLFW context\n"); + + glGenTextures(1, &screen_texture); + glBindTexture(GL_TEXTURE_2D, screen_texture); + + vr_texture.eColorSpace = vr::EColorSpace::ColorSpace_Auto; + vr_texture.eType = vr::ETextureType::TextureType_OpenGL; + vr_texture.handle = (void *)(uintptr_t)screen_texture; +} + +void init_vr() +{ + vr::EVRInitError init_err; + ovr_sys = vr::VR_Init(&init_err, vr::EVRApplicationType::VRApplication_Overlay); + assert(init_err == 0); + ovr_overlay = vr::VROverlay(); + printf("Initialized openvr overlay\n"); +} + +void init_overlay() +{ + auto overlay_err = ovr_overlay->CreateOverlay("deskpot", "Desktop view", &main_overlay); + assert(overlay_err == 0); + ovr_overlay->ShowOverlay(main_overlay); + ovr_overlay->SetOverlayWidthInMeters(main_overlay, 2.5f); + uint8_t col[4] = {20, 50, 50, 255}; + ovr_overlay->SetOverlayRaw(main_overlay, &col, 1, 1, 4); + printf("Created overlay instance\n"); + + vr::HmdMatrix34_t transform; + auto err = ovr_overlay->GetOverlayTransformAbsolute(main_overlay, &TRACKING_UNIVERSE, &transform); + assert(err == 0); + transform.m[1][1] = -1; // flip Y axis + err = ovr_overlay->SetOverlayTransformAbsolute(main_overlay, TRACKING_UNIVERSE, &transform); + assert(err == 0); +} + +void cleanup() +{ + printf("\nShutting down\n"); + vr::VR_Shutdown(); + glfwDestroyWindow(gl_window); + glfwTerminate(); + exit(0); +} + +void render_desktop() +{ + auto frame = XGetImage(xdisplay, root_window, 0, 0, width, height, AllPlanes, ZPixmap); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, frame->data); + XDestroyImage(frame); + + auto set_err = ovr_overlay->SetOverlayTexture(main_overlay, &vr_texture); + // if (set_err) + // printf("error setting texture: %d\n", set_err); + assert(set_err == 0); +} + +void update_cursor() +{ + int pix_x, pix_y; { - xdisplay = XOpenDisplay(nullptr); - assert(xdisplay != nullptr); - printf("Created X11 display\n"); - root_window = XRootWindow(xdisplay, 0); - XWindowAttributes attributes; - XGetWindowAttributes(xdisplay, root_window, &attributes); - width = attributes.width; - height = attributes.height; + Window _t1; + int _t2; + unsigned int _t3; + XQueryPointer(xdisplay, root_window, &_t1, &_t1, &pix_x, &pix_y, &_t2, &_t2, &_t3); } + // TODO: make this work when aspect ratio is >1 (root window is taller than it is wide) + float ratio = (float)height / (float)width; + float top_edge = 0.5f - ratio / 2.0f; + float x = pix_x / (float)width; + float y = pix_y / (float)width + top_edge; + auto pos = vr::HmdVector2_t{x, y}; + ovr_overlay->SetOverlayCursorPositionOverride(main_overlay, &pos); +} +void interrupted(int _sig) +{ + should_exit = true; +} + +int main() +{ + init_x11(); + init_glfw(); + init_vr(); + init_overlay(); + + signal(SIGINT, interrupted); + + while (!should_exit) { - assert(glfwInit() == true); - glfwWindowHint(GLFW_VISIBLE, false); - gl_window = glfwCreateWindow(width, height, "Overlay", nullptr, nullptr); - assert(gl_window != nullptr); - glfwMakeContextCurrent(gl_window); - printf("Created GLFW context\n"); - } - - { - vr::EVRInitError init_err; - ovr_sys = vr::VR_Init(&init_err, vr::EVRApplicationType::VRApplication_Overlay); - assert(init_err == 0); - ovr_overlay = vr::VROverlay(); - printf("Initialized openvr overlay\n"); - } - - { - auto overlay_err = ovr_overlay->CreateOverlay("deskpot", "Desktop view", &main_overlay); - assert(overlay_err == 0); - ovr_overlay->ShowOverlay(main_overlay); - ovr_overlay->SetOverlayWidthInMeters(main_overlay, 2.5f); - uint8_t col[4] = {20, 50, 50, 255}; - ovr_overlay->SetOverlayRaw(main_overlay, &col, 1, 1, 4); - printf("Created overlay instance\n"); - } - - { - vr::HmdMatrix34_t transform; - auto err = ovr_overlay->GetOverlayTransformAbsolute(main_overlay, &TRACKING_UNIVERSE, &transform); - assert(err == 0); - transform.m[1][1] = -1; - err = ovr_overlay->SetOverlayTransformAbsolute(main_overlay, TRACKING_UNIVERSE, &transform); - assert(err == 0); - } - - { - glGenTextures(1, &screen_texture); - glBindTexture(GL_TEXTURE_2D, screen_texture); - - vr_texture.eColorSpace = vr::EColorSpace::ColorSpace_Auto; - vr_texture.eType = vr::ETextureType::TextureType_OpenGL; - vr_texture.handle = (void *)(uintptr_t)screen_texture; - } - - while (1) - { - auto frame = XGetImage(xdisplay, root_window, 0, 0, width, height, AllPlanes, ZPixmap); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, frame->data); - XDestroyImage(frame); - - { - auto set_err = ovr_overlay->SetOverlayTexture(main_overlay, &vr_texture); - if (set_err) - { - printf("error setting texture: %d\n", set_err); - assert(set_err == 0); - } - } - - { // update cursor position - int pix_x, pix_y; - { - Window _t1; - int _t2; - unsigned int _t3; - XQueryPointer(xdisplay, root_window, &_t1, &_t1, &pix_x, &pix_y, &_t2, &_t2, &_t3); - } - // TODO: make this work when aspect ratio is >1 (root window is taller than it is wide) - float ratio = (float)height / (float)width; - float top_edge = 0.5f - ratio / 2.0f; - float x = pix_x / (float)width; - float y = pix_y / (float)width + top_edge; - auto pos = vr::HmdVector2_t{x, y}; - ovr_overlay->SetOverlayCursorPositionOverride(main_overlay, &pos); - } + render_desktop(); + update_cursor(); glfwSwapBuffers(gl_window); usleep(1000000 / FRAMERATE); @@ -123,13 +145,3 @@ int main(int argc, char **argv) cleanup(); return 0; } - -void cleanup(int _sig) -{ - printf("\nCleaning up\n"); - vr::VR_Shutdown(); - glfwDestroyWindow(gl_window); - glfwTerminate(); - printf("Shutting down\n"); - exit(0); -} From 3f1670809c74a4b07f2fb81fea584508013460d2 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sun, 2 Apr 2023 16:59:07 +0200 Subject: [PATCH 5/5] cleanup --- src/main.cpp | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 57c2e6e..efbe2e4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ #include "openvr/openvr.h" -static vr::ETrackingUniverseOrigin TRACKING_UNIVERSE = vr::ETrackingUniverseOrigin::TrackingUniverseStanding; +auto TRACKING_UNIVERSE = vr::ETrackingUniverseOrigin::TrackingUniverseStanding; #define FRAMERATE 30 @@ -81,15 +81,6 @@ void init_overlay() assert(err == 0); } -void cleanup() -{ - printf("\nShutting down\n"); - vr::VR_Shutdown(); - glfwDestroyWindow(gl_window); - glfwTerminate(); - exit(0); -} - void render_desktop() { auto frame = XGetImage(xdisplay, root_window, 0, 0, width, height, AllPlanes, ZPixmap); @@ -127,13 +118,13 @@ void interrupted(int _sig) int main() { + signal(SIGINT, interrupted); + init_x11(); init_glfw(); init_vr(); init_overlay(); - signal(SIGINT, interrupted); - while (!should_exit) { render_desktop(); @@ -142,6 +133,10 @@ int main() glfwSwapBuffers(gl_window); usleep(1000000 / FRAMERATE); } - cleanup(); + + printf("\nShutting down\n"); + vr::VR_Shutdown(); + glfwDestroyWindow(gl_window); + glfwTerminate(); return 0; }