forked from DadSchoorse/vkBasalt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
106 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters