First xcb implementation

Former-commit-id: 483522109b8b63fa80b9152b04bca79a65809d4d
This commit is contained in:
Youri Hubaut 2015-09-19 17:20:01 +02:00
parent 438e45c08e
commit e8cc3e357e
24 changed files with 3272 additions and 13 deletions

View File

@ -13,8 +13,9 @@
// http://msdn.microsoft.com/en-us/library/aa383751(v=vs.85).aspx
typedef void* NzWindowHandle;
#elif defined(NAZARA_PLATFORM_LINUX)
#include <xcb/xcb.h>
// http://en.wikipedia.org/wiki/Xlib#Data_types
typedef unsigned long NzWindowHandle;
using NzWindowHandle = xcb_window_t;
#else
#error Lack of implementation: WindowHandle
#endif

View File

@ -6,8 +6,8 @@
#if defined(NAZARA_PLATFORM_WINDOWS)
#include <Nazara/Utility/Win32/CursorImpl.hpp>
#elif defined(NAZARA_PLATFORM_LINUX)
#include <Nazara/Utility/Linux/CursorImpl.hpp>
#elif defined(NAZARA_PLATFORM_X11)
#include <Nazara/Utility/X11/CursorImpl.hpp>
#else
#error Lack of implementation: Cursor
#endif

View File

@ -6,8 +6,8 @@
#if defined(NAZARA_PLATFORM_WINDOWS)
#include <Nazara/Utility/Win32/IconImpl.hpp>
#elif defined(NAZARA_PLATFORM_LINUX)
#include <Nazara/Utility/Linux/IconImpl.hpp>
#elif defined(NAZARA_PLATFORM_X11)
#include <Nazara/Utility/X11/IconImpl.hpp>
#else
#error Lack of implementation: Icon
#endif

View File

@ -6,8 +6,8 @@
#if defined(NAZARA_PLATFORM_WINDOWS)
#include <Nazara/Utility/Win32/InputImpl.hpp>
#elif defined(NAZARA_PLATFORM_LINUX)
#include <Nazara/Utility/Linux/InputImpl.hpp>
#elif defined(NAZARA_PLATFORM_X11)
#include <Nazara/Utility/X11/InputImpl.hpp>
#else
#error Lack of implementation: Keyboard
#endif

View File

@ -7,8 +7,8 @@
#if defined(NAZARA_PLATFORM_WINDOWS)
#include <Nazara/Utility/Win32/InputImpl.hpp>
#elif defined(NAZARA_PLATFORM_LINUX)
#include <Nazara/Utility/Linux/InputImpl.hpp>
#elif defined(NAZARA_PLATFORM_X11)
#include <Nazara/Utility/X11/InputImpl.hpp>
#else
#error Lack of implementation: Mouse
#endif

View File

@ -3,9 +3,17 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/VideoMode.hpp>
#include <Nazara/Utility/VideoModeImpl.hpp>
#include <algorithm>
#include <functional>
#if defined(NAZARA_PLATFORM_WINDOWS)
#include <Nazara/Utility/Win32/VideoModeImpl.hpp>
#elif defined(NAZARA_PLATFORM_X11)
#include <Nazara/Utility/X11/VideoModeImpl.hpp>
#else
#error Lack of implementation: Window
#endif
#include <Nazara/Utility/Debug.hpp>
NzVideoMode::NzVideoMode() :

View File

@ -2,7 +2,7 @@
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/VideoModeImpl.hpp>
#include <Nazara/Utility/Win32/VideoModeImpl.hpp>
#include <Nazara/Utility/VideoMode.hpp>
#include <algorithm>
#include <windows.h>

View File

@ -0,0 +1,19 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_VIDEOMODEIMPL_HPP
#define NAZARA_VIDEOMODEIMPL_HPP
#include <Nazara/Utility/VideoMode.hpp>
class NzVideoModeImpl
{
public:
static NzVideoMode GetDesktopMode();
static void GetFullscreenModes(std::vector<NzVideoMode>& modes);
};
#endif // NNAZARA_VIDEOMODEIMPL_HPP

View File

@ -13,8 +13,8 @@
#if defined(NAZARA_PLATFORM_WINDOWS)
#include <Nazara/Utility/Win32/WindowImpl.hpp>
#elif defined(NAZARA_PLATFORM_LINUX)
#include <Nazara/Utility/Linux/WindowImpl.hpp>
#elif defined(NAZARA_PLATFORM_X11)
#include <Nazara/Utility/X11/WindowImpl.hpp>
#else
#error Lack of implementation: Window
#endif

View File

@ -0,0 +1,161 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/X11/CursorImpl.hpp>
#include <Nazara/Core/CallOnExit.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Utility/Image.hpp>
#include <Nazara/Utility/PixelFormat.hpp>
#include <Nazara/Utility/X11/Display.hpp>
#include <xcb/xcb_image.h>
#include <xcb/xcb_renderutil.h>
#include <Nazara/Utility/Debug.hpp>
bool NzCursorImpl::Create(const NzImage& cursor, int hotSpotX, int hotSpotY)
{
NzImage cursorImage(cursor); // Vive le COW
if (!cursorImage.Convert(nzPixelFormat_BGRA8))
{
NazaraError("Failed to convert cursor to BGRA8");
return false;
}
auto width = cursorImage.GetWidth();
auto height = cursorImage.GetHeight();
NzScopedXCBConnection connection;
xcb_screen_t* screen = X11::XCBDefaultScreen(connection);
NzScopedXCB<xcb_generic_error_t> error(nullptr);
NzScopedXCB<xcb_render_query_pict_formats_reply_t> formatsReply = xcb_render_query_pict_formats_reply(
connection,
xcb_render_query_pict_formats(connection),
&error);
if (!formatsReply || error)
{
NazaraError("Failed to get pict formats");
return false;
}
xcb_render_pictforminfo_t* fmt = xcb_render_util_find_standard_format(
formatsReply.get(),
XCB_PICT_STANDARD_ARGB_32);
if (!fmt)
{
NazaraError("Failed to find format PICT_STANDARD_ARGB_32");
return false;
}
xcb_image_t* xi = xcb_image_create(
width, height,
XCB_IMAGE_FORMAT_Z_PIXMAP,
32, 32, 32, 32,
XCB_IMAGE_ORDER_LSB_FIRST,
XCB_IMAGE_ORDER_MSB_FIRST,
0, 0, 0);
if (!xi)
{
NazaraError("Failed to create image for cursor");
return false;
}
std::unique_ptr<uint8_t[]> data(new uint8_t[xi->stride * height]);
if (!data)
{
xcb_image_destroy(xi);
NazaraError("Failed to allocate memory for cursor image");
return false;
}
xi->data = data.get();
std::copy(cursorImage.GetConstPixels(), cursorImage.GetConstPixels() + cursorImage.GetBytesPerPixel() * width * height, xi->data);
xcb_render_picture_t pic = XCB_NONE;
NzCallOnExit onExit([&](){
xcb_image_destroy(xi);
if (pic != XCB_NONE)
xcb_render_free_picture(connection, pic);
});
NzXCBPixmap pix(connection);
if (!pix.Create(32, screen->root, width, height))
{
NazaraError("Failed to create pixmap for cursor");
return false;
}
pic = xcb_generate_id(connection);
if (!X11::CheckCookie(
connection,
xcb_render_create_picture(
connection,
pic,
pix,
fmt->id,
0,
nullptr
)))
{
NazaraError("Failed to create render picture for cursor");
return false;
}
NzXCBGContext gc(connection);
if (!gc.Create(pix, 0, nullptr))
{
NazaraError("Failed to create gcontext for cursor");
return false;
}
if (!X11::CheckCookie(
connection,
xcb_image_put(
connection,
pix,
gc,
xi,
0, 0,
0
)))
{
NazaraError("Failed to put image for cursor");
return false;
}
m_cursor = xcb_generate_id(connection);
if (!X11::CheckCookie(
connection,
xcb_render_create_cursor(
connection,
m_cursor,
pic,
hotSpotX, hotSpotY
)))
{
NazaraError("Failed to create cursor");
return false;
}
return true;
}
void NzCursorImpl::Destroy()
{
NzScopedXCBConnection connection;
xcb_free_cursor(connection, m_cursor);
m_cursor = 0;
}
xcb_cursor_t NzCursorImpl::GetCursor()
{
return m_cursor;
}

View File

@ -0,0 +1,26 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_CURSORIMPL_HPP
#define NAZARA_CURSORIMPL_HPP
#include <xcb/xcb_cursor.h>
class NzImage;
class NzCursorImpl
{
public:
bool Create(const NzImage& image, int hotSpotX, int hotSpotY);
void Destroy();
xcb_cursor_t GetCursor();
private:
xcb_cursor_t m_cursor;
};
#endif // NAZARA_CURSORIMPL_HPP

View File

@ -0,0 +1,218 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/X11/Display.hpp>
#include <Nazara/Core/Error.hpp>
#include <xcb/xcb_keysyms.h>
#include <map>
#include <Nazara/Utility/Debug.hpp>
namespace
{
// The shared display and its reference counter
xcb_connection_t* sharedConnection = nullptr;
int screen_nbr = 0;
unsigned int referenceCountConnection = 0;
xcb_key_symbols_t* sharedkeySymbol;
unsigned int referenceCountKeySymbol = 0;
xcb_ewmh_connection_t* sharedEwmhConnection;
unsigned int referenceCountEwmhConnection = 0;
using AtomMap = std::map<std::string, xcb_atom_t>;
AtomMap atoms;
}
namespace X11
{
bool CheckCookie(xcb_connection_t* connection, xcb_void_cookie_t cookie)
{
NzScopedXCB<xcb_generic_error_t> error(xcb_request_check(
connection,
cookie
));
if (error)
return false;
else
return true;
}
void CloseConnection(xcb_connection_t* connection)
{
NazaraAssert(connection == sharedConnection, "The model is meant for one connection to X11 server");
--referenceCountConnection;
}
void CloseEWMHConnection(xcb_ewmh_connection_t* ewmh_connection)
{
NazaraAssert(ewmh_connection == sharedEwmhConnection, "The model is meant for one connection to X11 server");
--referenceCountEwmhConnection;
}
xcb_atom_t GetAtom(const std::string& name, bool onlyIfExists)
{
AtomMap::const_iterator iter = atoms.find(name);
if (iter != atoms.end())
return iter->second;
NzScopedXCB<xcb_generic_error_t> error(nullptr);
xcb_connection_t* connection = OpenConnection();
NzScopedXCB<xcb_intern_atom_reply_t> reply(xcb_intern_atom_reply(
connection,
xcb_intern_atom(
connection,
onlyIfExists,
name.size(),
name.c_str()
),
&error
));
CloseConnection(connection);
if (error || !reply)
{
NazaraError("Failed to get " + name + " atom.");
return XCB_ATOM_NONE;
}
atoms[name] = reply->atom;
return reply->atom;
}
void Initialize()
{
NazaraAssert(referenceCountConnection == 0, "Initialize should be called before anything");
NazaraAssert(referenceCountKeySymbol == 0, "Initialize should be called before anything");
NazaraAssert(referenceCountEwmhConnection == 0, "Initialize should be called before anything");
{
sharedConnection = xcb_connect(nullptr, &screen_nbr);
// Opening display failed: The best we can do at the moment is to output a meaningful error message
// and cause an abnormal program termination
if (!sharedConnection || xcb_connection_has_error(sharedConnection))
{
NazaraError("Failed to open xcb connection");
std::abort();
}
OpenConnection();
}
{
sharedkeySymbol = xcb_key_symbols_alloc(sharedConnection);
XCBKeySymbolsAlloc(sharedConnection);
}
{
sharedEwmhConnection = new xcb_ewmh_connection_t;
xcb_intern_atom_cookie_t* ewmh_cookie = xcb_ewmh_init_atoms(sharedConnection, sharedEwmhConnection);
if(!xcb_ewmh_init_atoms_replies(sharedEwmhConnection, ewmh_cookie, nullptr))
{
NazaraError("Could not initialize EWMH Connection");
sharedEwmhConnection = nullptr;
}
OpenEWMHConnection(sharedConnection);
}
}
xcb_key_symbols_t* XCBKeySymbolsAlloc(xcb_connection_t* connection)
{
NazaraAssert(connection == sharedConnection, "The model is meant for one connection to X11 server");
++referenceCountKeySymbol;
return sharedkeySymbol;
}
void XCBKeySymbolsFree(xcb_key_symbols_t* keySymbols)
{
NazaraAssert(keySymbols == sharedkeySymbol, "The model is meant for one connection to X11 server");
--referenceCountKeySymbol;
}
xcb_connection_t* OpenConnection()
{
++referenceCountConnection;
return sharedConnection;
}
xcb_ewmh_connection_t* OpenEWMHConnection(xcb_connection_t* connection)
{
NazaraAssert(connection == sharedConnection, "The model is meant for one connection to X11 server");
++referenceCountEwmhConnection;
return sharedEwmhConnection;
}
void Uninitialize()
{
{
NazaraAssert(referenceCountEwmhConnection == 1, "Uninitialize should be called after anything or a close is missing");
CloseEWMHConnection(sharedEwmhConnection);
xcb_ewmh_connection_wipe(sharedEwmhConnection);
delete sharedEwmhConnection;
}
{
NazaraAssert(referenceCountKeySymbol == 1, "Uninitialize should be called after anything or a free is missing");
XCBKeySymbolsFree(sharedkeySymbol);
xcb_key_symbols_free(sharedkeySymbol);
}
{
NazaraAssert(referenceCountConnection == 1, "Uninitialize should be called after anything or a close is missing");
CloseConnection(sharedConnection);
xcb_disconnect(sharedConnection);
}
}
xcb_window_t XCBDefaultRootWindow(xcb_connection_t* connection)
{
NazaraAssert(connection == sharedConnection, "The model is meant for one connection to X11 server");
xcb_screen_t* screen = XCBDefaultScreen(connection);
if (screen)
return screen->root;
return XCB_NONE;
}
xcb_screen_t* XCBDefaultScreen(xcb_connection_t* connection)
{
NazaraAssert(connection == sharedConnection, "The model is meant for one connection to X11 server");
return XCBScreenOfDisplay(connection, screen_nbr);
}
int XCBScreen(xcb_connection_t* connection)
{
NazaraAssert(connection == sharedConnection, "The model is meant for one connection to X11 server");
return screen_nbr;
}
xcb_screen_t* XCBScreenOfDisplay(xcb_connection_t* connection, int screen_nbr)
{
NazaraAssert(connection == sharedConnection, "The model is meant for one connection to X11 server");
xcb_screen_iterator_t iter = xcb_setup_roots_iterator(xcb_get_setup(connection));
for (; iter.rem; --screen_nbr, xcb_screen_next (&iter))
{
if (screen_nbr == 0)
return iter.data;
}
return nullptr;
}
}

View File

@ -0,0 +1,41 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_X11DISPLAY_HPP
#define NAZARA_X11DISPLAY_HPP
#include <Nazara/Utility/WindowHandle.hpp>
#include <Nazara/Utility/X11/ScopedXCB.hpp>
#include <xcb/xcb_ewmh.h>
#include <string>
typedef struct _XCBKeySymbols xcb_key_symbols_t;
namespace X11
{
bool CheckCookie(xcb_connection_t* connection, xcb_void_cookie_t cookie);
void CloseConnection(xcb_connection_t* connection);
void CloseEWMHConnection(xcb_ewmh_connection_t* ewmh_connection);
xcb_atom_t GetAtom(const std::string& name, bool onlyIfExists = false);
void Initialize();
xcb_key_symbols_t* XCBKeySymbolsAlloc(xcb_connection_t* connection);
void XCBKeySymbolsFree(xcb_key_symbols_t* keySymbols);
xcb_connection_t* OpenConnection();
xcb_ewmh_connection_t* OpenEWMHConnection(xcb_connection_t* connection);
void Uninitialize();
xcb_screen_t* XCBDefaultScreen(xcb_connection_t* connection);
xcb_window_t XCBDefaultRootWindow(xcb_connection_t* connection);
int XCBScreen(xcb_connection_t* connection);
xcb_screen_t* XCBScreenOfDisplay(xcb_connection_t* connection, int screen_nbr);
}
#endif // NAZARA_X11DISPLAY_HPP

View File

@ -0,0 +1,134 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/X11/IconImpl.hpp>
#include <Nazara/Core/CallOnExit.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Utility/Image.hpp>
#include <Nazara/Utility/PixelFormat.hpp>
#include <Nazara/Utility/X11/Display.hpp>
#include <Nazara/Utility/Debug.hpp>
NzIconImpl::NzIconImpl()
{
NzScopedXCBConnection connection;
m_iconPixmap.Connect(connection);
m_maskPixmap.Connect(connection);
}
bool NzIconImpl::Create(const NzImage& icon)
{
NzImage iconImage(icon); // Vive le COW
if (!iconImage.Convert(nzPixelFormat_BGRA8))
{
NazaraError("Failed to convert icon to BGRA8");
return false;
}
auto width = iconImage.GetWidth();
auto height = iconImage.GetHeight();
NzScopedXCBConnection connection;
xcb_screen_t* screen = X11::XCBDefaultScreen(connection);
if (!m_iconPixmap.Create(
screen->root_depth,
screen->root,
width,
height))
{
NazaraError("Failed to create icon pixmap");
return false;
}
NzCallOnExit onExit([this](){
Destroy();
});
NzXCBGContext iconGC(connection);
if (!iconGC.Create(
m_iconPixmap,
0,
nullptr))
{
NazaraError("Failed to create icon gc");
return false;
}
if (!X11::CheckCookie(
connection,
xcb_put_image(
connection,
XCB_IMAGE_FORMAT_Z_PIXMAP,
m_iconPixmap,
iconGC,
width,
height,
0,
0,
0,
screen->root_depth,
width * height * 4,
iconImage.GetConstPixels()
)))
{
NazaraError("Failed to put image for icon");
return false;
}
// Create the mask pixmap (must have 1 bit depth)
std::size_t pitch = (width + 7) / 8;
static std::vector<nzUInt8> maskPixels(pitch * height, 0);
for (std::size_t j = 0; j < height; ++j)
{
for (std::size_t i = 0; i < pitch; ++i)
{
for (std::size_t k = 0; k < 8; ++k)
{
if (i * 8 + k < width)
{
nzUInt8 opacity = (iconImage.GetConstPixels()[(i * 8 + k + j * width) * 4 + 3] > 0) ? 1 : 0;
maskPixels[i + j * pitch] |= (opacity << k);
}
}
}
}
if (!m_maskPixmap.CreatePixmapFromBitmapData(
X11::XCBDefaultRootWindow(connection),
reinterpret_cast<uint8_t*>(&maskPixels[0]),
width,
height,
1,
0,
1,
nullptr))
{
NazaraError("Failed to create mask pixmap for icon");
return false;
}
onExit.Reset();
return true;
}
void NzIconImpl::Destroy()
{
m_iconPixmap.Destroy();
m_maskPixmap.Destroy();
}
xcb_pixmap_t NzIconImpl::GetIcon()
{
return m_iconPixmap;
}
xcb_pixmap_t NzIconImpl::GetMask()
{
return m_maskPixmap;
}

View File

@ -0,0 +1,30 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_ICONIMPL_HPP
#define NAZARA_ICONIMPL_HPP
#include <Nazara/Utility/X11/ScopedXCB.hpp>
class NzImage;
class NzIconImpl
{
public:
NzIconImpl();
bool Create(const NzImage& image);
void Destroy();
xcb_pixmap_t GetIcon();
xcb_pixmap_t GetMask();
private:
NzXCBPixmap m_iconPixmap;
NzXCBPixmap m_maskPixmap;
};
#endif // NAZARA_ICONIMPL_HPP

View File

@ -0,0 +1,371 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/X11/InputImpl.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Utility/Window.hpp>
#include <Nazara/Utility/X11/Display.hpp>
#include <X11/keysym.h>
#include <X11/XF86keysym.h>
#include <X11/Xlib.h>
#include <xcb/xcb_keysyms.h>
#include <Nazara/Utility/Debug.hpp>
namespace
{
KeySym GetKeySym(NzKeyboard::Key key)
{
// X11 keysym correspondant
KeySym keysym = 0;
switch (key)
{
// Lettres
case NzKeyboard::A: keysym = XK_A; break;
case NzKeyboard::B: keysym = XK_B; break;
case NzKeyboard::C: keysym = XK_C; break;
case NzKeyboard::D: keysym = XK_D; break;
case NzKeyboard::E: keysym = XK_E; break;
case NzKeyboard::F: keysym = XK_F; break;
case NzKeyboard::G: keysym = XK_G; break;
case NzKeyboard::H: keysym = XK_H; break;
case NzKeyboard::I: keysym = XK_I; break;
case NzKeyboard::J: keysym = XK_J; break;
case NzKeyboard::K: keysym = XK_K; break;
case NzKeyboard::L: keysym = XK_L; break;
case NzKeyboard::M: keysym = XK_M; break;
case NzKeyboard::N: keysym = XK_N; break;
case NzKeyboard::O: keysym = XK_O; break;
case NzKeyboard::P: keysym = XK_P; break;
case NzKeyboard::Q: keysym = XK_Q; break;
case NzKeyboard::R: keysym = XK_R; break;
case NzKeyboard::S: keysym = XK_S; break;
case NzKeyboard::T: keysym = XK_T; break;
case NzKeyboard::U: keysym = XK_U; break;
case NzKeyboard::V: keysym = XK_V; break;
case NzKeyboard::W: keysym = XK_W; break;
case NzKeyboard::X: keysym = XK_X; break;
case NzKeyboard::Y: keysym = XK_Y; break;
case NzKeyboard::Z: keysym = XK_Z; break;
// Touches de fonction
case NzKeyboard::F1: keysym = XK_F1; break;
case NzKeyboard::F2: keysym = XK_F2; break;
case NzKeyboard::F3: keysym = XK_F3; break;
case NzKeyboard::F4: keysym = XK_F4; break;
case NzKeyboard::F5: keysym = XK_F5; break;
case NzKeyboard::F6: keysym = XK_F6; break;
case NzKeyboard::F7: keysym = XK_F7; break;
case NzKeyboard::F8: keysym = XK_F8; break;
case NzKeyboard::F9: keysym = XK_F9; break;
case NzKeyboard::F10: keysym = XK_F10; break;
case NzKeyboard::F11: keysym = XK_F11; break;
case NzKeyboard::F12: keysym = XK_F12; break;
case NzKeyboard::F13: keysym = XK_F13; break;
case NzKeyboard::F14: keysym = XK_F14; break;
case NzKeyboard::F15: keysym = XK_F15; break;
// Flèches directionnelles
case NzKeyboard::Down: keysym = XK_Down; break;
case NzKeyboard::Left: keysym = XK_Left; break;
case NzKeyboard::Right: keysym = XK_Right; break;
case NzKeyboard::Up: keysym = XK_Up; break;
// Pavé numérique
case NzKeyboard::Add: keysym = XK_KP_Add; break;
case NzKeyboard::Decimal: keysym = XK_KP_Decimal; break;
case NzKeyboard::Divide: keysym = XK_KP_Divide; break;
case NzKeyboard::Multiply: keysym = XK_KP_Multiply; break;
case NzKeyboard::Numpad0: keysym = XK_KP_0; break;
case NzKeyboard::Numpad1: keysym = XK_KP_1; break;
case NzKeyboard::Numpad2: keysym = XK_KP_2; break;
case NzKeyboard::Numpad3: keysym = XK_KP_3; break;
case NzKeyboard::Numpad4: keysym = XK_KP_4; break;
case NzKeyboard::Numpad5: keysym = XK_KP_5; break;
case NzKeyboard::Numpad6: keysym = XK_KP_6; break;
case NzKeyboard::Numpad7: keysym = XK_KP_7; break;
case NzKeyboard::Numpad8: keysym = XK_KP_8; break;
case NzKeyboard::Numpad9: keysym = XK_KP_9; break;
case NzKeyboard::Subtract: keysym = XK_KP_Subtract; break;
// Divers
case NzKeyboard::Backslash: keysym = XK_backslash; break;
case NzKeyboard::Backspace: keysym = XK_BackSpace; break;
case NzKeyboard::Clear: keysym = XK_Clear; break;
case NzKeyboard::Comma: keysym = XK_comma; break;
case NzKeyboard::Dash: keysym = XK_minus; break;
case NzKeyboard::Delete: keysym = XK_Delete; break;
case NzKeyboard::End: keysym = XK_End; break;
case NzKeyboard::Equal: keysym = XK_equal; break;
case NzKeyboard::Escape: keysym = XK_Escape; break;
case NzKeyboard::Home: keysym = XK_Home; break;
case NzKeyboard::Insert: keysym = XK_Insert; break;
case NzKeyboard::LAlt: keysym = XK_Alt_L; break;
case NzKeyboard::LBracket: keysym = XK_bracketleft; break;
case NzKeyboard::LControl: keysym = XK_Control_L; break;
case NzKeyboard::LShift: keysym = XK_Shift_L; break;
case NzKeyboard::LSystem: keysym = XK_Super_L; break;
case NzKeyboard::Num0: keysym = XK_0; break;
case NzKeyboard::Num1: keysym = XK_1; break;
case NzKeyboard::Num2: keysym = XK_2; break;
case NzKeyboard::Num3: keysym = XK_3; break;
case NzKeyboard::Num4: keysym = XK_4; break;
case NzKeyboard::Num5: keysym = XK_5; break;
case NzKeyboard::Num6: keysym = XK_6; break;
case NzKeyboard::Num7: keysym = XK_7; break;
case NzKeyboard::Num8: keysym = XK_8; break;
case NzKeyboard::Num9: keysym = XK_9; break;
case NzKeyboard::PageDown: keysym = XK_Page_Down; break;
case NzKeyboard::PageUp: keysym = XK_Page_Up; break;
case NzKeyboard::Pause: keysym = XK_Pause; break;
case NzKeyboard::Period: keysym = XK_period; break;
case NzKeyboard::Print: keysym = XK_Print; break;
case NzKeyboard::PrintScreen: keysym = XK_Sys_Req; break;
case NzKeyboard::Quote: keysym = XK_quotedbl; break;
case NzKeyboard::RAlt: keysym = XK_Alt_R; break;
case NzKeyboard::RBracket: keysym = XK_bracketright; break;
case NzKeyboard::RControl: keysym = XK_Control_R; break;
case NzKeyboard::Return: keysym = XK_Return; break;
case NzKeyboard::RShift: keysym = XK_Shift_R; break;
case NzKeyboard::RSystem: keysym = XK_Super_R; break;
case NzKeyboard::Semicolon: keysym = XK_semicolon; break;
case NzKeyboard::Slash: keysym = XK_slash; break;
case NzKeyboard::Space: keysym = XK_space; break;
case NzKeyboard::Tab: keysym = XK_Tab; break;
case NzKeyboard::Tilde: keysym = XK_grave; break;
// Touches navigateur
case NzKeyboard::Browser_Back: keysym = XF86XK_Back; break;
case NzKeyboard::Browser_Favorites: keysym = XF86XK_Favorites; break;
case NzKeyboard::Browser_Forward: keysym = XF86XK_Forward; break;
case NzKeyboard::Browser_Home: keysym = XF86XK_HomePage; break;
case NzKeyboard::Browser_Refresh: keysym = XF86XK_Refresh; break;
case NzKeyboard::Browser_Search: keysym = XF86XK_Search; break;
case NzKeyboard::Browser_Stop: keysym = XF86XK_Stop; break;
// Touches de contrôle
case NzKeyboard::Media_Next: keysym = XF86XK_AudioNext; break;
case NzKeyboard::Media_Play: keysym = XF86XK_AudioPlay; break;
case NzKeyboard::Media_Previous: keysym = XF86XK_AudioPrev; break;
case NzKeyboard::Media_Stop: keysym = XF86XK_AudioStop; break;
// Touches de contrôle du volume
case NzKeyboard::Volume_Down: keysym = XF86XK_AudioLowerVolume; break;
case NzKeyboard::Volume_Mute: keysym = XF86XK_AudioMute; break;
case NzKeyboard::Volume_Up: keysym = XF86XK_AudioRaiseVolume; break;
// Touches à verrouillage
case NzKeyboard::CapsLock: keysym = XK_Caps_Lock; break;
case NzKeyboard::NumLock: keysym = XK_Num_Lock; break;
case NzKeyboard::ScrollLock: keysym = XK_Scroll_Lock; break;
default: break;
}
// Sanity checks
if (key < 0 || key >= NzKeyboard::Count || keysym == 0)
NazaraWarning("Key " + NzString::Number(key) + " is not handled in NzKeyboard");
return keysym;
}
}
NzString NzEventImpl::GetKeyName(NzKeyboard::Key key)
{
KeySym keySym = GetKeySym(key);
// XKeysymToString returns a static area.
return XKeysymToString(keySym);
}
NzVector2i NzEventImpl::GetMousePosition()
{
NzScopedXCBConnection connection;
NzScopedXCB<xcb_generic_error_t> error(nullptr);
NzScopedXCB<xcb_query_pointer_reply_t> pointer(
xcb_query_pointer_reply(
connection,
xcb_query_pointer(
connection,
X11::XCBDefaultRootWindow(connection)
),
&error
)
);
if (error)
{
NazaraError("Failed to query pointer");
return NzVector2i(-1, -1);
}
return NzVector2i(pointer->root_x, pointer->root_y);
}
NzVector2i NzEventImpl::GetMousePosition(const NzWindow& relativeTo)
{
NzWindowHandle handle = relativeTo.GetHandle();
if (handle)
{
// Open a connection with the X server
NzScopedXCBConnection connection;
NzScopedXCB<xcb_generic_error_t> error(nullptr);
NzScopedXCB<xcb_query_pointer_reply_t> pointer(
xcb_query_pointer_reply(
connection,
xcb_query_pointer(
connection,
handle
),
&error
)
);
if (error)
{
NazaraError("Failed to query pointer");
return NzVector2i(-1, -1);
}
return NzVector2i(pointer->win_x, pointer->win_y);
}
else
{
NazaraError("No window handle");
return NzVector2i(-1, -1);
}
}
bool NzEventImpl::IsKeyPressed(NzKeyboard::Key key)
{
NzScopedXCBConnection connection;
xcb_keysym_t keySym = GetKeySym(key);
xcb_key_symbols_t* keySymbols = X11::XCBKeySymbolsAlloc(connection);
if (!keySymbols)
{
NazaraError("Failed to alloc key symbols");
return false;
}
NzScopedXCB<xcb_keycode_t> keyCode = xcb_key_symbols_get_keycode(keySymbols, keySym);
if (!keyCode)
{
NazaraError("Failed to get key code");
return false;
}
X11::XCBKeySymbolsFree(keySymbols);
NzScopedXCB<xcb_generic_error_t> error(nullptr);
// Get the whole keyboard state
NzScopedXCB<xcb_query_keymap_reply_t> keymap(
xcb_query_keymap_reply(
connection,
xcb_query_keymap(connection),
&error
)
);
if (error)
{
NazaraError("Failed to query keymap");
return false;
}
// Check our keycode
return (keymap->keys[*keyCode.get() / 8] & (1 << (*keyCode.get() % 8))) != 0;
}
bool NzEventImpl::IsMouseButtonPressed(NzMouse::Button button)
{
NzScopedXCBConnection connection;
NzScopedXCB<xcb_generic_error_t> error(nullptr);
// Get pointer mask
NzScopedXCB<xcb_query_pointer_reply_t> pointer(
xcb_query_pointer_reply(
connection,
xcb_query_pointer(
connection,
X11::XCBDefaultRootWindow(connection)
),
&error
)
);
if (error)
{
NazaraError("Failed to query pointer");
return false;
}
uint16_t buttons = pointer->mask;
switch (button)
{
case NzMouse::Left: return buttons & XCB_BUTTON_MASK_1;
case NzMouse::Right: return buttons & XCB_BUTTON_MASK_3;
case NzMouse::Middle: return buttons & XCB_BUTTON_MASK_2;
case NzMouse::XButton1: return false; // not supported by X
case NzMouse::XButton2: return false; // not supported by X
}
NazaraError("Mouse button not supported.");
return false;
}
void NzEventImpl::SetMousePosition(int x, int y)
{
NzScopedXCBConnection connection;
xcb_window_t root = X11::XCBDefaultRootWindow(connection);
if (!X11::CheckCookie(
connection,
xcb_warp_pointer(
connection,
None, // Source window
root, // Destination window
0, 0, // Source position
0, 0, // Source size
x, y // Destination position
))
)
NazaraError("Failed to set mouse position");
xcb_flush(connection);
}
void NzEventImpl::SetMousePosition(int x, int y, const NzWindow& relativeTo)
{
NzScopedXCBConnection connection;
NzWindowHandle handle = relativeTo.GetHandle();
if (handle)
{
if (!X11::CheckCookie(
connection,
xcb_warp_pointer(
connection,
None, // Source window
handle, // Destination window
0, 0, // Source position
0, 0, // Source size
x, y // Destination position
))
)
NazaraError("Failed to set mouse position relative to window");
xcb_flush(connection);
}
else
NazaraError("No window handle");
}

View File

@ -0,0 +1,27 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_INPUTIMPL_HPP
#define NAZARA_INPUTIMPL_HPP
#include <Nazara/Core/String.hpp>
#include <Nazara/Math/Vector2.hpp>
#include <Nazara/Utility/Keyboard.hpp>
#include <Nazara/Utility/Mouse.hpp>
class NzEventImpl
{
public:
static NzString GetKeyName(NzKeyboard::Key key);
static NzVector2i GetMousePosition();
static NzVector2i GetMousePosition(const NzWindow& relativeTo);
static bool IsKeyPressed(NzKeyboard::Key key);
static bool IsMouseButtonPressed(NzMouse::Button button);
static void SetMousePosition(int x, int y);
static void SetMousePosition(int x, int y, const NzWindow& relativeTo);
};
#endif // NAZARA_INPUTIMPL_HPP

View File

@ -0,0 +1,196 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/X11/ScopedXCB.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Utility/X11/Display.hpp>
#include <xcb/xcb_image.h>
#include <Nazara/Utility/Debug.hpp>
/***********************************************
NzScopedXCBConnection
***********************************************/
NzScopedXCBConnection::NzScopedXCBConnection() :
m_connection(nullptr)
{
m_connection = X11::OpenConnection();
}
NzScopedXCBConnection::~NzScopedXCBConnection()
{
X11::CloseConnection(m_connection);
}
NzScopedXCBConnection::operator xcb_connection_t*() const
{
return m_connection;
}
/***********************************************
NzScopedXCBEWMHConnection
***********************************************/
NzScopedXCBEWMHConnection::NzScopedXCBEWMHConnection(xcb_connection_t* connection) :
m_ewmhConnection(nullptr)
{
m_ewmhConnection = X11::OpenEWMHConnection(connection);
}
NzScopedXCBEWMHConnection::~NzScopedXCBEWMHConnection()
{
X11::CloseEWMHConnection(m_ewmhConnection);
}
xcb_ewmh_connection_t* NzScopedXCBEWMHConnection::operator ->() const
{
return m_ewmhConnection;
}
NzScopedXCBEWMHConnection::operator xcb_ewmh_connection_t*() const
{
return m_ewmhConnection;
}
/***********************************************
NzXCBGContext
***********************************************/
NzXCBGContext::NzXCBGContext(xcb_connection_t* connection) :
m_connection(connection),
m_gcontext(XCB_NONE)
{
NazaraAssert(connection, "Connection must have been established");
}
NzXCBGContext::~NzXCBGContext()
{
Destroy();
}
bool NzXCBGContext::Create(xcb_drawable_t drawable, uint32_t value_mask, const uint32_t* value_list)
{
NazaraAssert(m_gcontext == XCB_NONE, "Context must have been destroyed before or just created");
m_gcontext = xcb_generate_id(m_connection);
return X11::CheckCookie(
m_connection,
xcb_create_gc(
m_connection,
m_gcontext,
drawable,
value_mask,
value_list
));
}
void NzXCBGContext::Destroy()
{
if (m_gcontext == XCB_NONE)
return;
if (!X11::CheckCookie(
m_connection,
xcb_free_gc(
m_connection,
m_gcontext
))
)
NazaraError("Failed to free gcontext");
m_gcontext = XCB_NONE;
}
NzXCBGContext::operator xcb_gcontext_t() const
{
return m_gcontext;
}
/***********************************************
NzXCBPixmap
***********************************************/
NzXCBPixmap::NzXCBPixmap() :
m_connection(nullptr),
m_pixmap(XCB_NONE)
{
}
NzXCBPixmap::NzXCBPixmap(xcb_connection_t* connection) :
m_connection(connection),
m_pixmap(XCB_NONE)
{
}
NzXCBPixmap::~NzXCBPixmap()
{
Destroy();
}
void NzXCBPixmap::Connect(xcb_connection_t* connection)
{
NazaraAssert(connection && !m_connection, "Connection must be established");
m_connection = connection;
}
bool NzXCBPixmap::Create(uint8_t depth, xcb_drawable_t drawable, uint16_t width, uint16_t height)
{
NazaraAssert(m_pixmap == XCB_NONE, "Pixmap must have been destroyed before or just created");
m_pixmap = xcb_generate_id(m_connection);
return X11::CheckCookie(
m_connection,
xcb_create_pixmap(
m_connection,
depth,
m_pixmap,
drawable,
width,
height
));
}
bool NzXCBPixmap::CreatePixmapFromBitmapData(xcb_drawable_t drawable, uint8_t* data, uint32_t width, uint32_t height, uint32_t depth, uint32_t fg, uint32_t bg, xcb_gcontext_t* gcp)
{
NazaraAssert(m_pixmap == XCB_NONE, "Pixmap must have been destroyed before or just created");
m_pixmap = xcb_create_pixmap_from_bitmap_data(
m_connection,
drawable,
data,
width,
height,
depth,
fg,
bg,
gcp
);
return m_pixmap != XCB_NONE;
}
void NzXCBPixmap::Destroy()
{
if (m_pixmap == XCB_NONE)
return;
if (!X11::CheckCookie(
m_connection,
xcb_free_pixmap(
m_connection,
m_pixmap
))
)
NazaraError("Failed to free pixmap");
m_pixmap = XCB_NONE;
}
NzXCBPixmap::operator xcb_pixmap_t() const
{
return m_pixmap;
}

View File

@ -0,0 +1,95 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_SCOPEDXCB_HPP
#define NAZARA_SCOPEDXCB_HPP
#include <xcb/xcb_ewmh.h>
class NzScopedXCBConnection
{
public:
NzScopedXCBConnection();
~NzScopedXCBConnection();
operator xcb_connection_t*() const;
private:
xcb_connection_t* m_connection;
};
class NzScopedXCBEWMHConnection
{
public:
NzScopedXCBEWMHConnection(xcb_connection_t* connection);
~NzScopedXCBEWMHConnection();
xcb_ewmh_connection_t* operator ->() const;
operator xcb_ewmh_connection_t*() const;
private:
xcb_ewmh_connection_t* m_ewmhConnection;
};
template <typename T>
class NzScopedXCB
{
public:
NzScopedXCB(T* pointer);
~NzScopedXCB();
T* operator ->() const;
T** operator &();
operator bool() const;
T* get() const;
private:
T* m_pointer;
};
class NzXCBGContext
{
public:
NzXCBGContext(xcb_connection_t* connection);
~NzXCBGContext();
bool Create(xcb_drawable_t drawable, uint32_t value_mask, const uint32_t* value_list);
void Destroy();
operator xcb_gcontext_t() const;
private:
xcb_connection_t* m_connection;
xcb_gcontext_t m_gcontext;
};
class NzXCBPixmap
{
public:
NzXCBPixmap();
NzXCBPixmap(xcb_connection_t* connection);
~NzXCBPixmap();
void Connect(xcb_connection_t* connection);
bool Create(uint8_t depth, xcb_drawable_t drawable, uint16_t width, uint16_t height);
bool CreatePixmapFromBitmapData(xcb_drawable_t drawable, uint8_t* data, uint32_t width, uint32_t height, uint32_t depth, uint32_t fg, uint32_t bg, xcb_gcontext_t* gcp);
void Destroy();
operator xcb_pixmap_t() const;
private:
xcb_connection_t* m_connection;
xcb_pixmap_t m_pixmap;
};
#include <Nazara/Utility/X11/ScopedXCB.inl>
#endif // NAZARA_SCOPEDXCB_HPP

View File

@ -0,0 +1,44 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Error.hpp>
#include <Nazara/Utility/Debug.hpp>
template <typename T>
NzScopedXCB<T>::NzScopedXCB(T* pointer) :
m_pointer(pointer)
{
}
template <typename T>
NzScopedXCB<T>::~NzScopedXCB()
{
std::free(m_pointer);
}
template <typename T>
T* NzScopedXCB<T>::operator ->() const
{
return m_pointer;
}
template <typename T>
T** NzScopedXCB<T>::operator &()
{
return &m_pointer;
}
template <typename T>
NzScopedXCB<T>::operator bool() const
{
return m_pointer != nullptr;
}
template <typename T>
T* NzScopedXCB<T>::get() const
{
return m_pointer;
}
#include <Nazara/Utility/DebugOff.hpp>

View File

@ -0,0 +1,166 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/X11/VideoModeImpl.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Utility/VideoMode.hpp>
#include <Nazara/Utility/X11/Display.hpp>
#include <xcb/randr.h>
#include <algorithm>
#include <Nazara/Utility/Debug.hpp>
NzVideoMode NzVideoModeImpl::GetDesktopMode()
{
NzVideoMode desktopMode;
NzScopedXCBConnection connection;
// Retrieve the default screen
xcb_screen_t* screen = X11::XCBDefaultScreen(connection);
NzScopedXCB<xcb_generic_error_t> error(nullptr);
// Check if the RandR extension is present
const xcb_query_extension_reply_t* randrExt = xcb_get_extension_data(connection, &xcb_randr_id);
if (!randrExt || !randrExt->present)
{
// Randr extension is not supported: we cannot get the video modes
NazaraError("Failed to use the RandR extension while trying to get the desktop video mode");
return desktopMode;
}
// Load RandR and check its version
NzScopedXCB<xcb_randr_query_version_reply_t> randrVersion(xcb_randr_query_version_reply(
connection,
xcb_randr_query_version(
connection,
1,
1
),
&error
));
if (error)
{
NazaraError("Failed to load the RandR extension while trying to get the desktop video mode");
return desktopMode;
}
// Get the current configuration
NzScopedXCB<xcb_randr_get_screen_info_reply_t> config(xcb_randr_get_screen_info_reply(
connection,
xcb_randr_get_screen_info(
connection,
screen->root
),
&error
));
if (error)
{
// Failed to get the screen configuration
NazaraError("Failed to retrieve the screen configuration while trying to get the desktop video mode");
return desktopMode;
}
// Get the current video mode
xcb_randr_mode_t currentMode = config->sizeID;
// Get the available screen sizes
int nbSizes = xcb_randr_get_screen_info_sizes_length(config.get());
xcb_randr_screen_size_t* sizes = xcb_randr_get_screen_info_sizes(config.get());
if (sizes && (nbSizes > 0))
{
desktopMode = NzVideoMode(sizes[currentMode].width, sizes[currentMode].height, screen->root_depth);
if (config->rotation == XCB_RANDR_ROTATION_ROTATE_90 ||
config->rotation == XCB_RANDR_ROTATION_ROTATE_270)
std::swap(desktopMode.width, desktopMode.height);
}
else
{
NazaraError("Failed to retrieve any screen sizes while trying to get the desktop video mode");
}
return desktopMode;
}
void NzVideoModeImpl::GetFullscreenModes(std::vector<NzVideoMode>& modes)
{
NzScopedXCBConnection connection;
// Retrieve the default screen
xcb_screen_t* screen = X11::XCBDefaultScreen(connection);
NzScopedXCB<xcb_generic_error_t> error(nullptr);
const xcb_query_extension_reply_t* randrExt = xcb_get_extension_data(connection, &xcb_randr_id);
if (!randrExt || !randrExt->present)
{
// Randr extension is not supported: we cannot get the video modes
NazaraError("Failed to use the RandR extension while trying to get the supported video modes");
return;
}
// Load RandR and check its version
NzScopedXCB<xcb_randr_query_version_reply_t> randrVersion(xcb_randr_query_version_reply(
connection,
xcb_randr_query_version(
connection,
1,
1
),
&error
));
if (error)
{
NazaraError("Failed to load the RandR extension while trying to get the supported video modes");
return;
}
// Get the current configuration
NzScopedXCB<xcb_randr_get_screen_info_reply_t> config(xcb_randr_get_screen_info_reply(
connection,
xcb_randr_get_screen_info(
connection,
screen->root
),
&error
));
if (error)
{
// Failed to get the screen configuration
NazaraError("Failed to retrieve the screen configuration while trying to get the supported video modes");
return;
}
// Get the available screen sizes
xcb_randr_screen_size_t* sizes = xcb_randr_get_screen_info_sizes(config.get());
if (sizes && (config->nSizes > 0))
{
// Get the list of supported depths
xcb_depth_iterator_t iter = xcb_screen_allowed_depths_iterator(screen);
// Combine depths and sizes to fill the array of supported modes
for (; iter.rem; xcb_depth_next(&iter))
{
for (int j = 0; j < config->nSizes; ++j)
{
// Convert to VideoMode
NzVideoMode mode(sizes[j].width, sizes[j].height, iter.data->depth);
if (config->rotation == XCB_RANDR_ROTATION_ROTATE_90 ||
config->rotation == XCB_RANDR_ROTATION_ROTATE_270)
std::swap(mode.width, mode.height);
// Add it only if it is not already in the array
if (std::find(modes.begin(), modes.end(), mode) == modes.end())
modes.push_back(mode);
}
}
}
}

View File

@ -0,0 +1,19 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_VIDEOMODEIMPL_HPP
#define NAZARA_VIDEOMODEIMPL_HPP
#include <Nazara/Utility/VideoMode.hpp>
class NzVideoModeImpl
{
public:
static NzVideoMode GetDesktopMode();
static void GetFullscreenModes(std::vector<NzVideoMode>& modes);
};
#endif // NNAZARA_VIDEOMODEIMPL_HPP

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,126 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
// Interface inspirée de la SFML par Laurent Gomila
#pragma once
#ifndef NAZARA_WINDOWIMPL_HPP
#define NAZARA_WINDOWIMPL_HPP
#include <Nazara/Core/NonCopyable.hpp>
#include <Nazara/Core/Thread.hpp>
#include <Nazara/Math/Vector2.hpp>
#include <Nazara/Utility/Enums.hpp>
#include <Nazara/Utility/Keyboard.hpp>
#include <xcb/randr.h>
#include <xcb/xcb_icccm.h>
#include <Nazara/Utility/X11/Display.hpp>
#if NAZARA_UTILITY_THREADED_WINDOW
class NzConditionVariable;
class NzMutex;
#endif
class NzCursor;
class NzIcon;
class NzVideoMode;
class NzWindow;
class NzWindowImpl : NzNonCopyable
{
public:
NzWindowImpl(NzWindow* parent);
~NzWindowImpl();
bool Create(const NzVideoMode& mode, const NzString& title, nzUInt32 style);
bool Create(NzWindowHandle handle);
void Destroy();
void EnableKeyRepeat(bool enable);
void EnableSmoothScrolling(bool enable);
NzWindowHandle GetHandle() const;
unsigned int GetHeight() const;
NzVector2i GetPosition() const;
NzVector2ui GetSize() const;
nzUInt32 GetStyle() const;
NzString GetTitle() const;
unsigned int GetWidth() const;
bool HasFocus() const;
void IgnoreNextMouseEvent(int mouseX, int mouseY);
bool IsMinimized() const;
bool IsVisible() const;
void ProcessEvents(bool block);
void SetCursor(nzWindowCursor cursor);
void SetCursor(const NzCursor& cursor);
void SetEventListener(bool listener);
void SetFocus();
void SetIcon(const NzIcon& icon);
void SetMaximumSize(int width, int height);
void SetMinimumSize(int width, int height);
void SetPosition(int x, int y);
void SetSize(unsigned int width, unsigned int height);
void SetStayOnTop(bool stayOnTop);
void SetTitle(const NzString& title);
void SetVisible(bool visible);
static bool Initialize();
static void Uninitialize();
private:
void CleanUp();
xcb_keysym_t ConvertKeyCodeToKeySym(xcb_keycode_t keycode, uint16_t state);
NzKeyboard::Key ConvertVirtualKey(xcb_keysym_t symbol);
const char* ConvertWindowCursorToXName(nzWindowCursor cursor);
void CommonInitialize();
void ProcessEvent(xcb_generic_event_t* windowEvent);
void ResetVideoMode();
void SetCursor(xcb_cursor_t cursor);
void SetMotifHints();
void SetVideoMode(const NzVideoMode& mode);
void SwitchToFullscreen();
bool UpdateNormalHints();
void UpdateEventQueue(xcb_generic_event_t* event);
#if NAZARA_UTILITY_THREADED_WINDOW
static void WindowThread(NzWindowImpl* window, NzMutex* mutex, NzConditionVariable* condition);
#endif
xcb_window_t m_window;
xcb_screen_t* m_screen;
xcb_randr_get_screen_info_reply_t m_oldVideoMode;
xcb_size_hints_t m_size_hints;
nzUInt32 m_style;
#if NAZARA_UTILITY_THREADED_WINDOW
NzThread m_thread;
#endif
NzWindow* m_parent;
bool m_eventListener;
bool m_ownsWindow;
bool m_smoothScrolling;
#if NAZARA_UTILITY_THREADED_WINDOW
bool m_threadActive;
#endif
short m_scrolling;
NzVector2i m_mousePos;
bool m_keyRepeat;
struct
{
xcb_generic_event_t* curr = nullptr;
xcb_generic_event_t* next = nullptr;
} m_eventQueue;
};
#endif // NAZARA_WINDOWIMPL_HPP