Skip to content

Commit

Permalink
[input] load key from config
Browse files Browse the repository at this point in the history
  • Loading branch information
DadSchoorse committed May 17, 2020
1 parent 59463d0 commit eb8833b
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 48 deletions.
2 changes: 2 additions & 0 deletions config/vkBasalt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ effects = cas
#reshadeIncludePath = *path/to/reshade-shaders/Shaders*
#depthCapture = off

#toggleKey toggles the effects on/off
toggleKey = Home

#casSharpness specifies the amount of sharpning in the CAS shader.
#0.0 less sharp, less artefacts, but not off
Expand Down
4 changes: 3 additions & 1 deletion src/basalt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,10 +559,12 @@ namespace vkBasalt
{
scoped_lock l(globalLock);

static uint32_t keySymbol = convertToKeySym(pConfig->getOption<std::string>("toggleKey", "Home"));

static bool pressed = false;
static bool presentEffect = true;

if (isKeyPressed(XK_Home))
if (isKeyPressed(keySymbol))
{
if (!pressed)
{
Expand Down
60 changes: 21 additions & 39 deletions src/keyboard_input.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,31 @@
#include "logger.hpp"

#include "keyboard_input.hpp"

#include <memory>
#include <functional>
#include "logger.hpp"

// TODO build without X11
#ifndef VKBASALT_X11
#define VKBASALT_X11 1
#endif

#include <unistd.h>
#include <cstring>
#if VKBASALT_X11
#include "keyboard_input_x11.hpp"
#endif

namespace vkBasalt
{
bool isKeyPressed(KeySym ks)
uint32_t convertToKeySym(std::string key)
{
static int usesX11 = -1;

static std::unique_ptr<Display, std::function<void(Display*)>> display;

if (usesX11 < 0)
{
const char* disVar = getenv("DISPLAY");
if (!disVar || !std::strcmp(disVar, ""))
{
usesX11 = 0;
Logger::debug("no X11 support");
}
else
{
display = std::unique_ptr<Display, std::function<void(Display*)>>(XOpenDisplay(disVar), [](Display* d) { XCloseDisplay(d); });
usesX11 = 1;
Logger::debug("X11 support");
}
}

if (!usesX11)
{
return false;
}

char keys_return[32];

XQueryKeymap(display.get(), keys_return);

KeyCode kc2 = XKeysymToKeycode(display.get(), ks);

return !!(keys_return[kc2 >> 3] & (1 << (kc2 & 7)));
#if VKBASALT_X11
return convertToKeySymX11(key);
#endif
return 0u;
}

bool isKeyPressed(uint32_t ks)
{
#if VKBASALT_X11
return isKeyPressedX11(ks);
#endif
return false;
}
} // namespace vkBasalt
14 changes: 6 additions & 8 deletions src/keyboard_input.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#ifndef KEYBOARD_INPUT_HPP_INCLUDED
#define KEYBOARD_INPUT_HPP_INCLUDED
#pragma once

#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <cstdint>
#include <string>

namespace vkBasalt
{
bool isKeyPressed(KeySym ks);
}

#endif // KEYBOARD_INPUT_HPP_INCLUDED
uint32_t convertToKeySym(std::string key);
bool isKeyPressed(uint32_t ks);
} // namespace vkBasalt
63 changes: 63 additions & 0 deletions src/keyboard_input_x11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "keyboard_input_x11.hpp"

#include "logger.hpp"

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

#include <memory>
#include <functional>

#include <unistd.h>
#include <cstring>

namespace vkBasalt
{
uint32_t convertToKeySymX11(std::string key)
{
// TODO what if X11 isn't loaded?
uint32_t result = (uint32_t) XStringToKeysym(key.c_str());
if (!result)
{
Logger::err("invalid key");
}
return result;
}

bool isKeyPressedX11(uint32_t ks)
{
static int usesX11 = -1;

static std::unique_ptr<Display, std::function<void(Display*)>> display;

if (usesX11 < 0)
{
const char* disVar = getenv("DISPLAY");
if (!disVar || !std::strcmp(disVar, ""))
{
usesX11 = 0;
Logger::debug("no X11 support");
}
else
{
display = std::unique_ptr<Display, std::function<void(Display*)>>(XOpenDisplay(disVar), [](Display* d) { XCloseDisplay(d); });
usesX11 = 1;
Logger::debug("X11 support");
}
}

if (!usesX11)
{
return false;
}

char keys_return[32];

XQueryKeymap(display.get(), keys_return);

KeyCode kc2 = XKeysymToKeycode(display.get(), (KeySym) ks);

return !!(keys_return[kc2 >> 3] & (1 << (kc2 & 7)));
}

} // namespace vkBasalt
10 changes: 10 additions & 0 deletions src/keyboard_input_x11.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <cstdint>
#include <string>

namespace vkBasalt
{
uint32_t convertToKeySymX11(std::string key);
bool isKeyPressedX11(uint32_t ks);
} // namespace vkBasalt
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ vkBasalt_src = [
'image.cpp',
'image_view.cpp',
'keyboard_input.cpp',
'keyboard_input_x11.cpp',
'logger.cpp',
'logical_swapchain.cpp',
'lut_cube.cpp',
Expand Down

0 comments on commit eb8833b

Please sign in to comment.