Implemented XCB backend

This commit is contained in:
Alexandro Sánchez Bach 2016-06-13 00:13:25 +02:00
parent 2b81e39c90
commit e3cae88fc5
6 changed files with 149 additions and 3 deletions

View file

@ -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()

51
cmake/FindXCB.cmake Normal file
View file

@ -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)

View file

@ -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;

View file

@ -8,8 +8,96 @@
// Nucleus
#include "nucleus/nucleus.h"
#include <xcb/xcb.h>
#include <cstdio>
#include <cstring>
#include <thread>
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);
}

View file

@ -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);

View file

@ -5,6 +5,5 @@
#pragma once
// Creates a window with Xlib and initializes Nucleus
void createWindowXlib(int argc, char** argv);