184bool cae::X11::create(
const std::string &name,
const WindowSize size)
186 m_display = XOpenDisplay(
nullptr);
187 if (m_display ==
nullptr)
193 const int screen = DefaultScreen(m_display);
194 const Window root = RootWindow(m_display, screen);
196 m_window = XCreateSimpleWindow(m_display, root, 0, 0, size.width, size.height, 1, BlackPixel(m_display, screen),
197 WhitePixel(m_display, screen));
205 XStoreName(m_display, m_window, name.c_str());
207 XSelectInput(m_display, m_window,
208 ExposureMask | KeyPressMask | KeyReleaseMask | StructureNotifyMask | PointerMotionMask |
209 ButtonPressMask | ButtonReleaseMask);
210 m_wmDeleteMessage = XInternAtom(m_display,
"WM_DELETE_WINDOW", False);
211 XSetWMProtocols(m_display, m_window, &m_wmDeleteMessage, 1);
213 XMapWindow(m_display, m_window);
216 m_frameBufferSize = size;
221void cae::X11::close()
223 if (m_display !=
nullptr && m_window != 0U)
225 XDestroyWindow(m_display, m_window);
226 XCloseDisplay(m_display);
234 if (m_display ==
nullptr || m_window == 0U)
236 return m_frameBufferSize;
239 XWindowAttributes attrs;
240 XGetWindowAttributes(m_display, m_window, &attrs);
241 return {.width =
static_cast<uint16_t
>(attrs.width), .height =
static_cast<uint16_t
>(attrs.height)};
244void cae::X11::setIcon(
const std::string &path)
const
246 if ((m_display ==
nullptr) || m_window == 0)
256 const auto pixelCount = image.width * image.height;
257 std::vector<unsigned long> iconData;
258 iconData.reserve(2 + pixelCount);
260 iconData.push_back(
static_cast<unsigned long>(image.width));
261 iconData.push_back(
static_cast<unsigned long>(image.height));
263 const uint8_t *pixels = image.pixels;
264 for (
size_t i = 0; std::cmp_less(i, pixelCount); ++i)
266 const uint8_t r = pixels[(i * 4) + 0];
267 const uint8_t g = pixels[(i * 4) + 1];
268 const uint8_t b = pixels[(i * 4) + 2];
269 const uint8_t a = pixels[(i * 4) + 3];
271 const unsigned long argb = (
static_cast<unsigned long>(a) << 24) | (
static_cast<unsigned long>(r) << 16) |
272 (
static_cast<unsigned long>(g) << 8) | (
static_cast<unsigned long>(b));
274 iconData.push_back(argb);
277 const Atom netWmIcon = XInternAtom(m_display,
"_NET_WM_ICON", False);
278 const Atom cardinal = XInternAtom(m_display,
"CARDINAL", False);
280 XChangeProperty(m_display, m_window, netWmIcon, cardinal, 32, PropModeReplace,
281 reinterpret_cast<const unsigned char *
>(iconData.data()),
static_cast<int>(iconData.size()));
285 catch (
const std::exception &e)
291bool cae::X11::shouldClose()
const {
return m_shouldClose; }
293void cae::X11::pollEvents() {}
295bool cae::X11::pollEvent(WindowEvent &outEvent)
297 if (m_eventQueue.empty() && XPending(m_display) == 0)
302 while (XPending(m_display) > 0)
305 XNextEvent(m_display, &event);
312 e.type = WindowEventType::KeyDown;
314 const KeySym sym = XkbKeycodeToKeysym(m_display, event.xkey.keycode, 0, 0);
317 m_eventQueue.push(e);
323 e.type = WindowEventType::KeyUp;
325 const KeySym sym = XkbKeycodeToKeysym(m_display, event.xkey.keycode, 0, 0);
328 m_eventQueue.push(e);
332 case ConfigureNotify:
333 m_frameBufferResized =
true;
334 m_frameBufferSize.width =
event.xconfigure.width;
335 m_frameBufferSize.height =
event.xconfigure.height;
336 e.type = WindowEventType::Resize;
337 e.resize.w =
event.xconfigure.width;
338 e.resize.h =
event.xconfigure.height;
339 m_eventQueue.push(e);
343 if (std::cmp_equal(event.xclient.data.l[0], m_wmDeleteMessage))
345 m_shouldClose =
true;
346 e.type = WindowEventType::Close;
347 m_eventQueue.push(e);
352 e.type = WindowEventType::MouseMove;
353 e.mouseMove.x =
event.xmotion.x;
354 e.mouseMove.y =
event.xmotion.y;
355 m_eventQueue.push(e);
362 e.mouseButton.button =
static_cast<MouseButton>(
event.xbutton.button);
363 m_eventQueue.push(e);
374 if (!m_eventQueue.empty())
376 outEvent = m_eventQueue.front();
This file contains image struct.
This file contains the Logger class.
static void log(const std::string &message, const LogLevel &logLevel)
Log a message with a specific log level.
WindowEventType
Enum for window event types.
static cae::KeyCode translateKeysym(const KeySym sym)