From e560192839c26ac5ad16f4c57bfcb4d364c0ea5c Mon Sep 17 00:00:00 2001 From: hyzboy Date: Wed, 10 Apr 2019 01:37:37 +0800 Subject: [PATCH] =?UTF-8?q?XCBWindow=E5=8F=AF=E6=AD=A3=E5=B8=B8=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E7=AA=97=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/XCBWindow.cpp | 73 +++++++++++++++++++++++++++++++++--- example/Vulkan/main.cpp | 2 + 2 files changed, 69 insertions(+), 6 deletions(-) diff --git a/example/Vulkan/XCBWindow.cpp b/example/Vulkan/XCBWindow.cpp index 569717c9..df9b90e7 100644 --- a/example/Vulkan/XCBWindow.cpp +++ b/example/Vulkan/XCBWindow.cpp @@ -1,5 +1,6 @@ #include"Window.h" #include +#include #include #include #include @@ -14,8 +15,25 @@ namespace hgl xcb_window_t window; xcb_intern_atom_reply_t *atom_wm_delete_window; - xcb_screen_iterator_t iter; - int scr; + private: + + bool InitConnection() + { + int scr; + + connection=xcb_connect(nullptr,&scr); + + if(!connection||xcb_connection_has_error(connection)) + return(false); + + 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); + + screen=iter.data; + return(true); + } public: @@ -24,8 +42,6 @@ namespace hgl connection=nullptr; screen=nullptr; atom_wm_delete_window=nullptr; - - scr=-1; } ~XCBWindow() @@ -40,12 +56,57 @@ namespace hgl bool CreateWindow(uint w,uint h) override { if(w<=0||h<=0)return(false); + if(!InitConnection())return(false); - xcb_connect(nullptr,&scr); + window=xcb_generate_id(connection); + + uint32_t value_mask,value_list[32]; + + value_mask=XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; + value_list[0] = screen->black_pixel; + value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE; + + width=w; + height=h; + + 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_intern_atom_cookie_t cookie = xcb_intern_atom(connection, 1, 12, "WM_PROTOCOLS"); + xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(connection, cookie, 0); + + xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(connection, 0, 16, "WM_DELETE_WINDOW"); + 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); + + xcb_change_property (connection, XCB_PROP_MODE_REPLACE, window,XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, + win_name.Length(), win_name.c_str()); + + xcb_map_window(connection, window); + + const uint32_t coords[] = + { + (screen->width_in_pixels-width)/2, + (screen->height_in_pixels-height)/2 + }; + xcb_configure_window(connection, window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords); + xcb_flush(connection); + + xcb_generic_event_t *e; + while ((e = xcb_wait_for_event(connection))) { + if ((e->response_type & ~0x80) == XCB_EXPOSE) break; + } } bool CreateFullscreen(uint,uint,uint)override{} - void CloseWindow()override{} + void CloseWindow()override + { + xcb_destroy_window(connection, window); + xcb_disconnect(connection); + } void Show()override{} void Hide()override{} diff --git a/example/Vulkan/main.cpp b/example/Vulkan/main.cpp index c4fc8da6..fddba8bc 100644 --- a/example/Vulkan/main.cpp +++ b/example/Vulkan/main.cpp @@ -11,6 +11,8 @@ int main(int,char **) { Window *win=CreateRenderWindow(OS_TEXT("VulkanTest")); + win->CreateWindow(1280,720); + vulkan::Instance inst(U8_TEXT("VulkanTest"),win); if(!inst.Init())