forstio-window/driver/window_xcb.cpp

112 lines
2.9 KiB
C++

#include "window_xcb.h"
#include <cassert>
#include "device_xcb.h"
namespace saw {
xcb_window::xcb_window(xcb_device &dev, xcb_window_t xcb_win,
xcb_colormap_t xcb_colmap, const video_mode &vid_mode,
std::string_view title_view_)
: device_{dev}, xcb_window_{xcb_win}, xcb_colormap_{xcb_colmap},
video_mode_{vid_mode}, window_title_{title_view_} {}
xcb_window::~xcb_window() {
device_.window_destroyed(xcb_window_);
xcb_destroy_window(device_.xcb_connection, xcb_window_);
device_.flush();
}
void xcb_window::show() {
assert(device_.xcb_connection);
xcb_map_window(device_.xcb_connection, xcb_window_);
}
void xcb_window::hide() {
assert(device_.xcb_connection);
xcb_unmap_window(device_.xcb_connection, xcb_window_);
}
const video_mode &xcb_window::videoMode() const { return video_mode_; }
const std::string_view xcb_window::title() const { return window_title_; }
void xcb_window::resize(size_t width, size_t height) {
const uint32_t values[2] = {static_cast<uint32_t>(width),
static_cast<uint32_t>(height)};
xcb_configure_window(device_.xcb_connection, xcb_window_,
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
values);
video_mode_.width = width;
video_mode_.height = height;
}
conveyor<window::variant_event> xcb_window::onevent() {
auto caf = new_conveyor_and_feeder<window::variant_event>();
event_feeder = std::move(caf.feeder);
return std::move(caf.conveyor);
}
void xcb_window::resizeevent(size_t x, size_t y, size_t width, size_t height) {
(void)x;
(void)y;
/// @todo maybe include x and y?
video_mode_.width = width;
video_mode_.height = height;
if (event_feeder) {
event_feeder->feed(
window::variant_event{window::event::resize{width, height}});
}
}
void xcb_window::mouseevent(int16_t x, int16_t y, uint16_t state,
bool pressed) {
if (x < 0 || y < 0) {
return;
}
uint32_t ux = static_cast<uint32_t>(x);
uint32_t uy = static_cast<uint32_t>(y);
if (ux >= video_mode_.width || uy >= video_mode_.height) {
return;
}
if (event_feeder) {
event_feeder->feed(window::variant_event{
window::event::mouse{state, pressed, ux, uy}});
}
}
void xcb_window::mouse_move_event(int16_t x, int16_t y) {
if (x < 0 || y < 0) {
return;
}
uint32_t ux = static_cast<uint32_t>(x);
uint32_t uy = static_cast<uint32_t>(y);
if (ux >= video_mode_.width || uy >= video_mode_.height) {
return;
}
if (event_feeder) {
event_feeder->feed(
window::variant_event{window::event::mouse_move{ux, uy}});
}
}
void xcb_window::keyboardevent(int16_t x, int16_t y, uint32_t keycode,
bool pressed, bool repeat) {
if (x < 0 || y < 0) {
return;
}
uint32_t ux = static_cast<uint32_t>(x);
uint32_t uy = static_cast<uint32_t>(y);
if (ux >= video_mode_.width || uy >= video_mode_.height) {
return;
}
if (event_feeder) {
event_feeder->feed(window::variant_event{
window::event::keyboard{keycode, keycode, pressed, repeat}});
}
}
} // namespace saw