From e3cae88fc5ada8d82c9385d1e64ec1bccdc842d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandro=20S=C3=A1nchez=20Bach?= Date: Mon, 13 Jun 2016 00:13:25 +0200 Subject: [PATCH] Implemented XCB backend --- CMakeLists.txt | 8 ++++ cmake/FindXCB.cmake | 51 ++++++++++++++++++++ wrappers/linux/main.cpp | 2 +- wrappers/linux/window-xcb.cpp | 88 ++++++++++++++++++++++++++++++++++ wrappers/linux/window-xlib.cpp | 2 +- wrappers/linux/window-xlib.h | 1 - 6 files changed, 149 insertions(+), 3 deletions(-) create mode 100644 cmake/FindXCB.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 43c2532..d2c488a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,9 +68,11 @@ if(NOT MSVC) endif() # Dependencies +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") find_package(OpenGL QUIET) find_package(ZLIB QUIET) find_package(X11 QUIET) +find_package(XCB QUIET) if (NOT OPENGL_FOUND) message(WARNING "OpenGL not found. Corresponding backend will be disabled") @@ -209,4 +211,10 @@ if (TARGET_LINUX) else() message(FATAL_ERROR "X11 could not be located") endif() + if (XCB_FOUND) + include_directories(${XCB_INCLUDE_DIRS}) + target_link_libraries(nucleus ${XCB_LIBRARIES}) + else() + message(FATAL_ERROR "XCB could not be located") + endif() endif() diff --git a/cmake/FindXCB.cmake b/cmake/FindXCB.cmake new file mode 100644 index 0000000..2311591 --- /dev/null +++ b/cmake/FindXCB.cmake @@ -0,0 +1,51 @@ +# - FindXCB +# +# Copyright (C) 2015 Valve Corporation + +find_package(PkgConfig) + +if(NOT XCB_FIND_COMPONENTS) + set(XCB_FIND_COMPONENTS xcb) +endif() + +include(FindPackageHandleStandardArgs) +set(XCB_FOUND true) +set(XCB_INCLUDE_DIRS "") +set(XCB_LIBRARIES "") +foreach(comp ${XCB_FIND_COMPONENTS}) + # component name + string(TOUPPER ${comp} compname) + string(REPLACE "-" "_" compname ${compname}) + # header name + string(REPLACE "xcb-" "" headername xcb/${comp}.h) + # library name + set(libname ${comp}) + + pkg_check_modules(PC_${comp} QUIET ${comp}) + + find_path(${compname}_INCLUDE_DIR NAMES ${headername} + HINTS + ${PC_${comp}_INCLUDEDIR} + ${PC_${comp}_INCLUDE_DIRS} + ) + + find_library(${compname}_LIBRARY NAMES ${libname} + HINTS + ${PC_${comp}_LIBDIR} + ${PC_${comp}_LIBRARY_DIRS} + ) + + find_package_handle_standard_args(${comp} + FOUND_VAR ${comp}_FOUND + REQUIRED_VARS ${compname}_INCLUDE_DIR ${compname}_LIBRARY) + mark_as_advanced(${compname}_INCLUDE_DIR ${compname}_LIBRARY) + + list(APPEND XCB_INCLUDE_DIRS ${${compname}_INCLUDE_DIR}) + list(APPEND XCB_LIBRARIES ${${compname}_LIBRARY}) + + if(NOT ${comp}_FOUND) + set(XCB_FOUND false) + endif() +endforeach() + +list(REMOVE_DUPLICATES XCB_INCLUDE_DIRS) diff --git a/wrappers/linux/main.cpp b/wrappers/linux/main.cpp index 99ada31..39393ce 100644 --- a/wrappers/linux/main.cpp +++ b/wrappers/linux/main.cpp @@ -27,7 +27,7 @@ int main(int argc, char **argv) { // Using UI if (!config.console) { // Choose Window manager - WindowBackend backend = WINDOW_BACKEND_XLIB; + WindowBackend backend = WINDOW_BACKEND_XCB; for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "--wayland") == 0) { backend = WINDOW_BACKEND_WAYLAND; diff --git a/wrappers/linux/window-xcb.cpp b/wrappers/linux/window-xcb.cpp index 68e2d87..e04e788 100644 --- a/wrappers/linux/window-xcb.cpp +++ b/wrappers/linux/window-xcb.cpp @@ -8,8 +8,96 @@ // Nucleus #include "nucleus/nucleus.h" +#include + +#include #include #include void createWindowXcb(int argc, char **argv) { + // XCB connection + int scr; + xcb_connection_t* connection = xcb_connect(NULL, &scr); + if (connection == NULL) { + printf("Cannot open a XCB connection.\nExiting...\n"); + exit(1); + } + + const xcb_setup_t *setup = xcb_get_setup(connection); + xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup); + while (scr-- > 0) { + xcb_screen_next(&iter); + } + xcb_screen_t* screen = iter.data; + + // XCB window properties + const char* title = "Nucleus"; + unsigned int width = 960; + unsigned int height = 544; + uint32_t value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; + uint32_t value_list[32]; + value_list[0] = screen->black_pixel; + value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | + XCB_EVENT_MASK_STRUCTURE_NOTIFY; + + xcb_window_t window = xcb_generate_id(connection); + xcb_create_window(connection, XCB_COPY_FROM_PARENT, window, screen->root, 0, 0, + width, height, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual, value_mask, value_list); + xcb_change_property(connection, XCB_PROP_MODE_REPLACE, window, + XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, strlen(title), title); + + // Notification when window is destroyed + xcb_intern_atom_cookie_t cookie1 = xcb_intern_atom(connection, 1, 12, "WM_PROTOCOLS"); + xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(connection, cookie1, 0); + xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(connection, 0, 16, "WM_DELETE_WINDOW"); + xcb_intern_atom_reply_t* atom_wm_delete_window = xcb_intern_atom_reply(connection, cookie2, 0); + xcb_change_property(connection, XCB_PROP_MODE_REPLACE, window, + (*reply).atom, 4, 32, 1, &(*atom_wm_delete_window).atom); + free(reply); + + const uint32_t coords[] = {100, 100}; + xcb_map_window(connection, window); + xcb_configure_window(connection, window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords); + xcb_flush(connection); + + // Events + bool quit = false; + while (!quit) { + xcb_generic_event_t* event; + event = xcb_poll_for_event(connection); + if (!event) { + continue; + } + + uint8_t code = event->response_type & 0x7f; + switch (code) { + case XCB_EXPOSE: + // TODO: Resize window + break; + case XCB_CLIENT_MESSAGE: { + const auto& msg = *(const xcb_client_message_event_t*)event; + if (msg.data.data32[0] == atom_wm_delete_window->atom) { + quit = true; + } + break; + } + case XCB_KEY_RELEASE: { + const auto& key = *(const xcb_key_release_event_t*)event; + // TODO: Handle event + break; + } + case XCB_CONFIGURE_NOTIFY: { + const auto& cfg = *(const xcb_configure_notify_event_t*)event; + // TODO: Handle event + break; + } + default: + break; + } + + free(event); + } + + xcb_destroy_window(connection, window); + xcb_disconnect(connection); } diff --git a/wrappers/linux/window-xlib.cpp b/wrappers/linux/window-xlib.cpp index 07f7e8f..182026a 100644 --- a/wrappers/linux/window-xlib.cpp +++ b/wrappers/linux/window-xlib.cpp @@ -29,7 +29,7 @@ void createWindowXlib(int argc, char **argv) { hint.flags = PSize; Window window = XCreateSimpleWindow(display, DefaultRootWindow(display), - hint.x, hint.y, hint.width, hint.height, 5, foreground, background); + hint.x, hint.y, hint.width, hint.height, 5, foreground, background); const char appName[] = "Nucleus"; XSetStandardProperties(display, window, appName, appName, None, argv, argc, &hint); diff --git a/wrappers/linux/window-xlib.h b/wrappers/linux/window-xlib.h index 375e311..7e15d6f 100644 --- a/wrappers/linux/window-xlib.h +++ b/wrappers/linux/window-xlib.h @@ -5,6 +5,5 @@ #pragma once - // Creates a window with Xlib and initializes Nucleus void createWindowXlib(int argc, char** argv);