diff options
| -rw-r--r-- | include/window.hpp | 20 | ||||
| -rw-r--r-- | src/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | src/main.cpp | 3 | ||||
| -rw-r--r-- | src/utils.cpp | 5 | ||||
| -rw-r--r-- | src/window.cpp | 52 |
5 files changed, 79 insertions, 4 deletions
diff --git a/include/window.hpp b/include/window.hpp index dae4f00..85f0a18 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -10,24 +10,39 @@ class vkrenderer; #if defined(LINUX) #include <xcb/xcb.h> + +#include <xkbcommon/xkbcommon.h> #elif defined(WINDOWS) #include <Windows.h> #endif +enum KEYS { + SPACE, + Z, + Q, + S, + D, +}; + enum EVENT_TYPES: int32_t { QUIT, RESIZE, + KEY_PRESS, + KEY_RELEASE, }; struct event { uint32_t width; uint32_t height; + // KEYS key; + uint32_t keycode; EVENT_TYPES type; }; class window { public: window(uint32_t width, uint32_t height); + ~window(); void poll_events(); @@ -42,6 +57,11 @@ class window { xcb_connection_t* connection; xcb_window_t win; xcb_atom_t win_delete_atom; + + xkb_context* xkb_ctx; + int32_t keyboard_device_id; + xkb_keymap* keyboard_keymap; + xkb_state* keyboard_state; #endif std::vector<event> events; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 77614b2..a270ef0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,4 +22,7 @@ add_executable( if(UNIX) target_link_libraries(path-tracer ${CMAKE_DL_LIBS} ${X11_xcb_LIB}) + target_link_libraries(path-tracer ${CMAKE_DL_LIBS} ${X11_xkbcommon_LIB}) + target_link_libraries(path-tracer ${CMAKE_DL_LIBS} ${X11_xkbcommon_X11_LIB}) + endif() diff --git a/src/main.cpp b/src/main.cpp index 0de69f7..a2134bd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -71,6 +71,9 @@ int main() { } break; } + case EVENT_TYPES::KEY_PRESS: { + std::cerr << (uint8_t)event.keycode << std::endl; + } default: { } } diff --git a/src/utils.cpp b/src/utils.cpp index f0849b7..1c6fac1 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -42,8 +42,9 @@ void random_scene(sphere *world) { uint32_t world_sphere_index = 1; - for (int a = -11; a < 11; a++) { - for (int b = -11; b < 11; b++) { + int32_t spheres_count = 11; + for (int a = -spheres_count; a < spheres_count; a++) { + for (int b = -spheres_count; b < spheres_count; b++) { auto choose_mat = randd(); point3 center(a + 0.9 * randd(), 0.2, b + 0.9 * randd()); diff --git a/src/window.cpp b/src/window.cpp index 77e8f1c..8a68612 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -3,16 +3,20 @@ #include "utils.hpp" #ifdef LINUX +#include <xkbcommon/xkbcommon-x11.h> + window::window(uint32_t width, uint32_t height) :width(width), height(height) { + connection = xcb_connect(NULL, NULL); win = xcb_generate_id(connection); auto setup = xcb_get_setup (connection); auto screen_info = xcb_setup_roots_iterator(setup).data; - xcb_event_mask_t events[] = { - XCB_EVENT_MASK_STRUCTURE_NOTIFY, + uint32_t events[] = { + XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_KEY_PRESS | + XCB_EVENT_MASK_KEY_RELEASE, }; xcb_create_window( connection, @@ -54,9 +58,33 @@ window::window(uint32_t width, uint32_t height) xcb_map_window(connection, win); xcb_flush(connection); + xkb_ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + + if (!xkb_ctx) { + std::cerr << "Cannot create xkb context" << std::endl; + } + + keyboard_device_id = xkb_x11_get_core_keyboard_device_id(connection); + + if (keyboard_device_id == -1) { + std::cerr << "Cannot access keyboard device !" << std::endl; + exit(1); + } + + keyboard_keymap = xkb_x11_keymap_new_from_device(xkb_ctx, connection, keyboard_device_id, XKB_KEYMAP_COMPILE_NO_FLAGS); + + keyboard_state = xkb_x11_state_new_from_device(keyboard_keymap, connection, keyboard_device_id); + isOpen = true; } +window::~window() { + xkb_state_unref(keyboard_state); + xkb_keymap_unref(keyboard_keymap); + xkb_context_unref(xkb_ctx); + xcb_disconnect(connection); +} + void window::poll_events() { xcb_generic_event_t *event; while((event = xcb_poll_for_event (connection))) { @@ -77,6 +105,26 @@ void window::poll_events() { break; } + case XCB_KEY_PRESS: { + auto key_press_event = (xcb_key_press_event_t*)event; + auto keysym = xkb_state_key_get_one_sym(keyboard_state, key_press_event->detail); + struct event new_event = { + .type = EVENT_TYPES::KEY_PRESS + }; + + new_event.keycode = keysym; + + events.push_back(new_event); + break; + } + case XCB_KEY_RELEASE: { + auto key_press_event = (xcb_key_release_event_t*)event; + events.push_back({ + .keycode = key_press_event->detail, + .type = EVENT_TYPES::KEY_RELEASE + }); + break; + } case XCB_CLIENT_MESSAGE: { auto client_message_event = (xcb_client_message_event_t*)event; if (client_message_event->data.data32[0] == win_delete_atom) { |
