Compare commits

..

1 Commits

Author SHA1 Message Date
d0f2b5e8a6 quick hack to replace epoll with poll 2026-06-04 03:32:31 +03:00
9 changed files with 263 additions and 474 deletions

View File

@@ -12,7 +12,6 @@
#include <X11/Xatom.h> #include <X11/Xatom.h>
#include <time.h> #include <time.h>
#include <sys/epoll.h>
struct Selection struct Selection
{ {
@@ -159,16 +158,6 @@ static const char* s_opcode_to_name[] {
[X_NoOperation] = "X_NoOperation", [X_NoOperation] = "X_NoOperation",
}; };
void register_display(int32_t x, int32_t y, uint32_t width, uint32_t height)
{
MUST(g_displays.push_back({
.x = x,
.y = y,
.w = width,
.h = height,
}));
}
uint32_t Object::Window::full_event_mask() const uint32_t Object::Window::full_event_mask() const
{ {
uint32_t full_event_mask = 0; uint32_t full_event_mask = 0;
@@ -210,8 +199,8 @@ BAN::ErrorOr<void> setup_client_conneciton(Client& client_info, const xConnClien
xConnSetupPrefix setup_prefix { xConnSetupPrefix setup_prefix {
.success = 1, .success = 1,
.lengthReason = 0, // wtf is this .lengthReason = 0, // wtf is this
.majorVersion = 11, .majorVersion = client_prefix.majorVersion,
.minorVersion = 0, .minorVersion = client_prefix.minorVersion,
.length = 8 + 2*format_count + (8 + 0 + sz_xWindowRoot + sz_xDepth + sz_xVisualType) / 4, .length = 8 + 2*format_count + (8 + 0 + sz_xWindowRoot + sz_xDepth + sz_xVisualType) / 4,
}; };
TRY(encode(client_info.output_buffer, setup_prefix)); TRY(encode(client_info.output_buffer, setup_prefix));
@@ -1373,7 +1362,15 @@ BAN::ErrorOr<void> handle_packet(Client& client_info, BAN::ConstByteSpan packet)
CARD16 width, height; CARD16 width, height;
CARD8 depth; CARD8 depth;
if (drawable.type == Object::Type::Window) if (drawable_id == g_root.windowId)
{
width = g_root.pixWidth;
height = g_root.pixHeight;
depth = g_root.rootDepth;
x = 0;
y = 0;
}
else if (drawable.type == Object::Type::Window)
{ {
const auto& window = drawable.object.get<Object::Window>(); const auto& window = drawable.object.get<Object::Window>();
width = window.width; width = window.width;
@@ -1816,9 +1813,9 @@ BAN::ErrorOr<void> handle_packet(Client& client_info, BAN::ConstByteSpan packet)
(void)TRY_REF(get_window(client_info, wid, X_SendEvent)); (void)TRY_REF(get_window(client_info, wid, X_SendEvent));
Client* target_client = nullptr; Client* target_client = nullptr;
for (auto& [_, thingy] : g_epoll_thingies) for (auto& [_, thingy] : g_pollables)
{ {
if (thingy.type != EpollThingy::Type::Client) if (thingy.type != Pollable::Type::Client)
continue; continue;
auto& other_client = thingy.value.get<Client>(); auto& other_client = thingy.value.get<Client>();
if (!other_client.objects.contains(wid)) if (!other_client.objects.contains(wid))
@@ -1940,47 +1937,11 @@ BAN::ErrorOr<void> handle_packet(Client& client_info, BAN::ConstByteSpan packet)
case X_GrabServer: case X_GrabServer:
{ {
g_server_grabber_fd = client_info.fd; g_server_grabber_fd = client_info.fd;
for (const auto& [_, thingy] : g_epoll_thingies)
{
if (thingy.type != EpollThingy::Type::Client)
continue;
auto& other_client = thingy.value.get<Client>();
if (client_info.fd == other_client.fd)
continue;
uint32_t events = 0;
if (other_client.has_epollout)
events |= EPOLLOUT;
epoll_event event { .events = events, .data = { .fd = other_client.fd } };
epoll_ctl(g_epoll_fd, EPOLL_CTL_MOD, other_client.fd, &event);
}
break; break;
} }
case X_UngrabServer: case X_UngrabServer:
{ {
g_server_grabber_fd = -1; g_server_grabber_fd = -1;
for (const auto& [_, thingy] : g_epoll_thingies)
{
if (thingy.type != EpollThingy::Type::Client)
continue;
auto& other_client = thingy.value.get<Client>();
if (client_info.fd == other_client.fd)
continue;
uint32_t events = EPOLLIN;
if (other_client.has_epollout)
events |= EPOLLOUT;
epoll_event event { .events = events, .data = { .fd = other_client.fd } };
epoll_ctl(g_epoll_fd, EPOLL_CTL_MOD, other_client.fd, &event);
}
break; break;
} }
case X_QueryPointer: case X_QueryPointer:

View File

@@ -140,7 +140,6 @@ struct Client
}; };
int fd; int fd;
State state; State state;
bool has_epollout { false };
bool has_bigrequests { false }; bool has_bigrequests { false };
CARD16 sequence { 0 }; CARD16 sequence { 0 };
BAN::Optional<uint32_t> pid; BAN::Optional<uint32_t> pid;
@@ -149,7 +148,7 @@ struct Client
BAN::HashSet<CARD32> objects; BAN::HashSet<CARD32> objects;
}; };
struct EpollThingy struct Pollable
{ {
enum class Type enum class Type
{ {
@@ -161,12 +160,6 @@ struct EpollThingy
BAN::Variant<Client, void*> value; BAN::Variant<Client, void*> value;
}; };
struct DisplayInfo
{
int32_t x, y;
uint32_t w, h;
};
extern const xPixmapFormat g_formats[6]; extern const xPixmapFormat g_formats[6];
extern const xDepth g_depth; extern const xDepth g_depth;
extern const xVisualType g_visual; extern const xVisualType g_visual;
@@ -178,11 +171,6 @@ extern BAN::HashMap<BAN::String, ATOM> g_atoms_name_to_id;
extern BAN::HashMap<ATOM, BAN::String> g_atoms_id_to_name; extern BAN::HashMap<ATOM, BAN::String> g_atoms_id_to_name;
extern ATOM g_atom_value; extern ATOM g_atom_value;
extern int g_epoll_fd; extern BAN::HashMap<int, Pollable> g_pollables;
extern BAN::HashMap<int, EpollThingy> g_epoll_thingies;
extern int g_server_grabber_fd; extern int g_server_grabber_fd;
extern BAN::Vector<DisplayInfo> g_displays;
extern CARD32 g_next_global_id;

View File

@@ -7,23 +7,18 @@
#include <X11/Xatom.h> #include <X11/Xatom.h>
#include <time.h> #include <time.h>
#include <sys/epoll.h>
void register_event_fd(int fd, void* data) void register_event_fd(int fd, void* data)
{ {
epoll_event event { .events = EPOLLIN, .data = { .fd = fd } }; MUST(g_pollables.insert(fd, {
epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &event); .type = Pollable::Type::Event,
MUST(g_epoll_thingies.insert(fd, {
.type = EpollThingy::Type::Event,
.value = data, .value = data,
})); }));
} }
void unregister_event_fd(int fd) void unregister_event_fd(int fd)
{ {
epoll_ctl(g_epoll_fd, EPOLL_CTL_DEL, fd, nullptr); g_pollables.remove(fd);
g_epoll_thingies.remove(fd);
} }
void on_window_close_event(WINDOW wid) void on_window_close_event(WINDOW wid)

View File

@@ -6,64 +6,12 @@
#include <time.h> #include <time.h>
struct RANDRDisplay
{
CARD32 crtc_id;
CARD32 output_id;
CARD32 mode_id;
ATOM name_atom;
BAN::String output_str;
BAN::String mode_str;
const DisplayInfo& info;
};
static BAN::Vector<RANDRDisplay> s_randr_displays;
static CARD32 s_timestamp { 0 };
static void initialize_displays()
{
for (size_t i = 0; i < g_displays.size(); i++)
{
auto name = MUST(BAN::String::formatted("B-OUT-{}", i));
const auto name_atom = g_atom_value++;
MUST(g_atoms_name_to_id.insert(name, name_atom));
MUST(g_atoms_id_to_name.insert(name_atom, name));
MUST(s_randr_displays.push_back({
.crtc_id = g_next_global_id++,
.output_id = g_next_global_id++,
.mode_id = g_next_global_id++,
.name_atom = name_atom,
.output_str = BAN::move(name),
.mode_str = MUST(BAN::String::formatted("{}x{}", g_displays[i].w, g_displays[i].h)),
.info = g_displays[i],
}));
}
s_timestamp = time(nullptr);
}
static const RANDRDisplay& find_display_by_output(CARD32 output)
{
for (const auto& display : s_randr_displays)
if (display.output_id == output)
return display;
ASSERT_NOT_REACHED();
}
static const RANDRDisplay& find_display_by_crtc(CARD32 crtc)
{
for (const auto& display : s_randr_displays)
if (display.crtc_id == crtc)
return display;
ASSERT_NOT_REACHED();
}
static BAN::ErrorOr<void> extension_randr(Client& client_info, BAN::ConstByteSpan packet) static BAN::ErrorOr<void> extension_randr(Client& client_info, BAN::ConstByteSpan packet)
{ {
if (s_randr_displays.empty()) static CARD32 crtc_id = 5;
initialize_displays(); static CARD32 output_id = 6;
static CARD32 mode_id = 7;
static CARD32 timestamp = time(nullptr);
static xRenderTransform transform { static xRenderTransform transform {
1 << 16, 0, 0, 1 << 16, 0, 0,
@@ -114,8 +62,8 @@ static BAN::ErrorOr<void> extension_randr(Client& client_info, BAN::ConstByteSpa
.sequenceNumber = client_info.sequence, .sequenceNumber = client_info.sequence,
.length = 3, .length = 3,
.root = g_root.windowId, .root = g_root.windowId,
.timestamp = s_timestamp, .timestamp = timestamp,
.configTimestamp = s_timestamp, .configTimestamp = timestamp,
.nSizes = 1, .nSizes = 1,
.sizeID = 0, .sizeID = 0,
.rotation = RR_Rotate_0, .rotation = RR_Rotate_0,
@@ -167,45 +115,39 @@ static BAN::ErrorOr<void> extension_randr(Client& client_info, BAN::ConstByteSpa
dprintln("RRGetScreenResources{}", current ? "Current" : ""); dprintln("RRGetScreenResources{}", current ? "Current" : "");
dprintln(" window: {}", request.window); dprintln(" window: {}", request.window);
size_t mode_name_bytes = 0; const auto mode_name = TRY(BAN::String::formatted("{}x{}", g_root.pixWidth, g_root.pixHeight));
for (const auto& display : s_randr_displays)
mode_name_bytes += display.mode_str.size();
xRRGetScreenResourcesReply reply { xRRGetScreenResourcesReply reply {
.type = X_Reply, .type = X_Reply,
.sequenceNumber = client_info.sequence, .sequenceNumber = client_info.sequence,
.length = static_cast<CARD32>(s_randr_displays.size() * (1 + 1 + 8) + (mode_name_bytes + 3) / 4), .length = static_cast<CARD32>(1 + 1 + 8 + (mode_name.size() + 3) / 4),
.timestamp = s_timestamp, .timestamp = timestamp,
.configTimestamp = s_timestamp, .configTimestamp = timestamp,
.nCrtcs = static_cast<CARD16>(s_randr_displays.size()), .nCrtcs = 1,
.nOutputs = static_cast<CARD16>(s_randr_displays.size()), .nOutputs = 1,
.nModes = static_cast<CARD16>(s_randr_displays.size()), .nModes = 1,
.nbytesNames = static_cast<CARD16>(mode_name_bytes), .nbytesNames = static_cast<CARD16>(mode_name.size()),
}; };
TRY(encode(client_info.output_buffer, reply)); TRY(encode(client_info.output_buffer, reply));
for (const auto& display : s_randr_displays) TRY(encode(client_info.output_buffer, crtc_id));
TRY(encode(client_info.output_buffer, display.crtc_id));
for (const auto& display : s_randr_displays) TRY(encode(client_info.output_buffer, output_id));
TRY(encode(client_info.output_buffer, display.output_id));
for (const auto& display : s_randr_displays) const CARD16 hsync_start = g_root.pixWidth;
{ const CARD16 hsync_end = g_root.pixWidth + 1;
const CARD16 hsync_start = display.info.w; const CARD16 htotal = g_root.pixWidth + 1;
const CARD16 hsync_end = display.info.w + 1;
const CARD16 htotal = display.info.w + 1;
const CARD16 vsync_start = display.info.h; const CARD16 vsync_start = g_root.pixHeight;
const CARD16 vsync_end = display.info.h + 1; const CARD16 vsync_end = g_root.pixHeight + 1;
const CARD16 vtotal = display.info.h + 1; const CARD16 vtotal = g_root.pixHeight + 1;
const CARD32 clock = htotal * vtotal * 60; const CARD32 clock = htotal * vtotal * 60;
xRRModeInfo mode_info { xRRModeInfo mode_info {
.id = display.mode_id, .id = mode_id,
.width = static_cast<CARD16>(display.info.w), .width = g_root.pixWidth,
.height = static_cast<CARD16>(display.info.h), .height = g_root.pixHeight,
.dotClock = clock, .dotClock = clock,
.hSyncStart = hsync_start, .hSyncStart = hsync_start,
.hSyncEnd = hsync_end, .hSyncEnd = hsync_end,
@@ -214,16 +156,14 @@ static BAN::ErrorOr<void> extension_randr(Client& client_info, BAN::ConstByteSpa
.vSyncStart = vsync_start, .vSyncStart = vsync_start,
.vSyncEnd = vsync_end, .vSyncEnd = vsync_end,
.vTotal = vtotal, .vTotal = vtotal,
.nameLength = static_cast<CARD16>(display.mode_str.size()), .nameLength = static_cast<CARD16>(mode_name.size()),
.modeFlags = RR_HSyncPositive | RR_VSyncPositive, .modeFlags = RR_HSyncPositive | RR_VSyncPositive,
}; };
TRY(encode(client_info.output_buffer, mode_info)); TRY(encode(client_info.output_buffer, mode_info));
}
for (const auto& display : s_randr_displays) TRY(encode(client_info.output_buffer, mode_name));
TRY(encode(client_info.output_buffer, display.mode_str)); for (size_t i = 0; (mode_name.size() + i) % 4; i++)
while (client_info.output_buffer.size() % 4) TRY(encode(client_info.output_buffer, '\0'));
TRY(encode<BYTE>(client_info.output_buffer, 0));
break; break;
} }
@@ -235,31 +175,27 @@ static BAN::ErrorOr<void> extension_randr(Client& client_info, BAN::ConstByteSpa
dprintln(" output: {}", request.output); dprintln(" output: {}", request.output);
dprintln(" configTimestamp: {}", request.configTimestamp); dprintln(" configTimestamp: {}", request.configTimestamp);
const auto& display = find_display_by_output(request.output);
xRRGetOutputInfoReply reply { xRRGetOutputInfoReply reply {
.type = X_Reply, .type = X_Reply,
.status = Success, .status = Success,
.sequenceNumber = client_info.sequence, .sequenceNumber = client_info.sequence,
.length = static_cast<CARD32>(1 + 1 + 1 + (display.output_str.size() + 3) / 4), .length = 1 + 1 + 1 + 2,
.timestamp = s_timestamp, .timestamp = timestamp,
.crtc = display.crtc_id, .crtc = crtc_id,
.mmWidth = display.info.w * 254 / 960, // 96 DPI .mmWidth = g_root.mmWidth,
.mmHeight = display.info.h * 254 / 960, // 96 DPI .mmHeight = g_root.mmHeight,
.connection = RR_Connected, .connection = RR_Connected,
.subpixelOrder = SubPixelUnknown, .subpixelOrder = SubPixelUnknown,
.nCrtcs = 1, .nCrtcs = 1,
.nModes = 1, .nModes = 1,
.nPreferred = 1, .nPreferred = 1,
.nClones = 0, .nClones = 0,
.nameLength = static_cast<CARD16>(display.output_str.size()), .nameLength = 5,
}; };
TRY(encode(client_info.output_buffer, reply)); TRY(encode(client_info.output_buffer, reply));
TRY(encode(client_info.output_buffer, display.crtc_id)); TRY(encode(client_info.output_buffer, crtc_id));
TRY(encode(client_info.output_buffer, display.mode_id)); TRY(encode(client_info.output_buffer, mode_id));
TRY(encode(client_info.output_buffer, display.output_str)); TRY(encode(client_info.output_buffer, "B-OUT\0\0\0"_sv));
while (client_info.output_buffer.size() % 4)
TRY(encode<BYTE>(client_info.output_buffer, 0));
break; break;
} }
@@ -314,27 +250,25 @@ static BAN::ErrorOr<void> extension_randr(Client& client_info, BAN::ConstByteSpa
dprintln(" crtc: {}", request.crtc); dprintln(" crtc: {}", request.crtc);
dprintln(" configTimestamp: {}", request.configTimestamp); dprintln(" configTimestamp: {}", request.configTimestamp);
const auto& display = find_display_by_crtc(request.crtc);
xRRGetCrtcInfoReply reply { xRRGetCrtcInfoReply reply {
.type = X_Reply, .type = X_Reply,
.status = Success, .status = Success,
.sequenceNumber = client_info.sequence, .sequenceNumber = client_info.sequence,
.length = 2, .length = 2,
.timestamp = s_timestamp, .timestamp = timestamp,
.x = static_cast<INT16>(display.info.x), .x = 0,
.y = static_cast<INT16>(display.info.y), .y = 0,
.width = static_cast<CARD16>(display.info.w), .width = g_root.pixWidth,
.height = static_cast<CARD16>(display.info.h), .height = g_root.pixHeight,
.mode = display.mode_id, .mode = mode_id,
.rotation = RR_Rotate_0, .rotation = RR_Rotate_0,
.rotations = RR_Rotate_0, .rotations = RR_Rotate_0,
.nOutput = 1, .nOutput = 1,
.nPossibleOutput = 1, .nPossibleOutput = 1,
}; };
TRY(encode(client_info.output_buffer, reply)); TRY(encode(client_info.output_buffer, reply));
TRY(encode(client_info.output_buffer, display.output_id)); TRY(encode(client_info.output_buffer, output_id));
TRY(encode(client_info.output_buffer, display.output_id)); TRY(encode(client_info.output_buffer, output_id));
break; break;
} }
@@ -351,13 +285,13 @@ static BAN::ErrorOr<void> extension_randr(Client& client_info, BAN::ConstByteSpa
dprintln(" mode: {}", request.mode); dprintln(" mode: {}", request.mode);
dprintln(" rotation: {}", request.rotation); dprintln(" rotation: {}", request.rotation);
s_timestamp = time(nullptr); timestamp = time(nullptr);
xRRSetCrtcConfigReply reply { xRRSetCrtcConfigReply reply {
.type = X_Reply, .type = X_Reply,
.status = Success, .status = Success,
.sequenceNumber = client_info.sequence, .sequenceNumber = client_info.sequence,
.length = 0, .length = 0,
.newTimestamp = s_timestamp, .newTimestamp = timestamp,
}; };
TRY(encode(client_info.output_buffer, reply)); TRY(encode(client_info.output_buffer, reply));
@@ -367,15 +301,15 @@ static BAN::ErrorOr<void> extension_randr(Client& client_info, BAN::ConstByteSpa
{ {
auto request = decode<xRRGetCrtcGammaSizeReq>(packet).value(); auto request = decode<xRRGetCrtcGammaSizeReq>(packet).value();
dwarnln("RRGetCrtcGammaSize"); dprintln("RRGetCrtcGammaSize");
dwarnln(" crtc: {}", request.crtc); dprintln(" crtc: {}", request.crtc);
xRRGetCrtcGammaSizeReply reply { xRRGetCrtcGammaSizeReply reply {
.type = X_Reply, .type = X_Reply,
.status = Success, .status = Success,
.sequenceNumber = client_info.sequence, .sequenceNumber = client_info.sequence,
.length = 0, .length = 0,
.size = 256, .size = 1,
}; };
TRY(encode(client_info.output_buffer, reply)); TRY(encode(client_info.output_buffer, reply));
@@ -392,13 +326,14 @@ static BAN::ErrorOr<void> extension_randr(Client& client_info, BAN::ConstByteSpa
.type = X_Reply, .type = X_Reply,
.status = Success, .status = Success,
.sequenceNumber = client_info.sequence, .sequenceNumber = client_info.sequence,
.length = (6 * 256) / 4, .length = 2,
.size = 256, .size = 1,
}; };
TRY(encode(client_info.output_buffer, reply)); TRY(encode(client_info.output_buffer, reply));
for (size_t i = 0; i < 3; i++) TRY(encode<CARD16>(client_info.output_buffer, 1));
for (size_t j = 0; j < 256; j++) TRY(encode<CARD16>(client_info.output_buffer, 1));
TRY(encode<CARD16>(client_info.output_buffer, j * 65535 / 255)); TRY(encode<CARD16>(client_info.output_buffer, 1));
TRY(encode<CARD16>(client_info.output_buffer, 0));
break; break;
} }
@@ -438,7 +373,7 @@ static BAN::ErrorOr<void> extension_randr(Client& client_info, BAN::ConstByteSpa
.status = Success, .status = Success,
.sequenceNumber = client_info.sequence, .sequenceNumber = client_info.sequence,
.length = 1, .length = 1,
.timestamp = s_timestamp, .timestamp = timestamp,
.left = 0, .left = 0,
.top = 0, .top = 0,
.width = 0, .width = 0,
@@ -467,7 +402,7 @@ static BAN::ErrorOr<void> extension_randr(Client& client_info, BAN::ConstByteSpa
.type = X_Reply, .type = X_Reply,
.sequenceNumber = client_info.sequence, .sequenceNumber = client_info.sequence,
.length = 0, .length = 0,
.output = s_randr_displays.front().output_id, .output = output_id,
}; };
TRY(encode(client_info.output_buffer, reply)); TRY(encode(client_info.output_buffer, reply));
@@ -484,7 +419,7 @@ static BAN::ErrorOr<void> extension_randr(Client& client_info, BAN::ConstByteSpa
.type = X_Reply, .type = X_Reply,
.sequenceNumber = client_info.sequence, .sequenceNumber = client_info.sequence,
.length = 0, .length = 0,
.timestamp = s_timestamp, .timestamp = timestamp,
.nProviders = 0, .nProviders = 0,
}; };
TRY(encode(client_info.output_buffer, reply)); TRY(encode(client_info.output_buffer, reply));
@@ -503,30 +438,28 @@ static BAN::ErrorOr<void> extension_randr(Client& client_info, BAN::ConstByteSpa
.type = X_Reply, .type = X_Reply,
.status = Success, .status = Success,
.sequenceNumber = client_info.sequence, .sequenceNumber = client_info.sequence,
.length = static_cast<CARD32>(6 * s_randr_displays.size() + s_randr_displays.size()), .length = 6 + 1,
.timestamp = s_timestamp, .timestamp = timestamp,
.nmonitors = static_cast<CARD32>(s_randr_displays.size()), .nmonitors = 1,
.noutputs = static_cast<CARD32>(s_randr_displays.size()), .noutputs = 1,
}; };
TRY(encode(client_info.output_buffer, reply)); TRY(encode(client_info.output_buffer, reply));
for (const auto& display : s_randr_displays)
{
xRRMonitorInfo monitor { xRRMonitorInfo monitor {
.name = display.name_atom, .name = None,
.primary = (&display == &s_randr_displays.front()), .primary = xTrue,
.automatic = xTrue, .automatic = xTrue,
.noutput = 1, .noutput = 1,
.x = static_cast<INT16>(display.info.x), .x = 0,
.y = static_cast<INT16>(display.info.y), .y = 0,
.width = static_cast<CARD16>(display.info.w), .width = g_root.pixWidth,
.height = static_cast<CARD16>(display.info.h), .height = g_root.pixHeight,
.widthInMillimeters = display.info.w * 254 / 96, // 96 DPI .widthInMillimeters = g_root.mmWidth,
.heightInMillimeters = display.info.h * 254 / 96, // 96 DPI .heightInMillimeters = g_root.mmHeight,
}; };
TRY(encode(client_info.output_buffer, monitor)); TRY(encode(client_info.output_buffer, monitor));
TRY(encode(client_info.output_buffer, display.output_id));
} TRY(encode(client_info.output_buffer, output_id));
break; break;
} }

View File

@@ -7,12 +7,12 @@
template<typename F> template<typename F>
static BAN::ErrorOr<void> for_each_client(uint32_t target_spec, const F& callback) static BAN::ErrorOr<void> for_each_client(uint32_t target_spec, const F& callback)
{ {
for (const auto& [fd, thingy] : g_epoll_thingies) for (auto [fd, thingy] : g_pollables)
{ {
if (thingy.type != EpollThingy::Type::Client) if (thingy.type != Pollable::Type::Client)
continue; continue;
const auto& client_info = thingy.value.get<Client>(); Client& client_info = thingy.value.get<Client>();
if (target_spec && (target_spec >> 20) != client_info.fd) if (target_spec && (target_spec >> 20) != client_info.fd)
continue; continue;
@@ -61,7 +61,7 @@ BAN::ErrorOr<void> extension_xres(Client& client_info, BAN::ConstByteSpan packet
{ {
auto spec = decode<xXResClientIdSpec>(packet).value(); auto spec = decode<xXResClientIdSpec>(packet).value();
TRY(for_each_client(spec.client, [&](const Client& client_info, uint32_t client_spec) -> BAN::ErrorOr<void> { TRY(for_each_client(spec.client, [&](Client& client_info, uint32_t client_spec) -> BAN::ErrorOr<void> {
if (spec.mask == None || (spec.mask & X_XResClientXIDMask)) if (spec.mask == None || (spec.mask & X_XResClientXIDMask))
{ {
xXResClientIdValue value { xXResClientIdValue value {

View File

@@ -47,7 +47,7 @@ enum class SystemCursorType
struct PlatformOps struct PlatformOps
{ {
/* Do platform initialization */ /* Do platform initialization */
bool (*initialize)(); bool (*initialize)(uint32_t* width, uint32_t* height);
/* Handle pending events */ /* Handle pending events */
void (*poll_events)(void*); void (*poll_events)(void*);
/* Create a window with given size */ /* Create a window with given size */
@@ -74,5 +74,3 @@ struct PlatformOps
void (*set_cursor)(PlatformWindow*, PlatformCursor*); void (*set_cursor)(PlatformWindow*, PlatformCursor*);
}; };
extern PlatformOps g_platform_ops; extern PlatformOps g_platform_ops;
void register_display(int32_t x, int32_t y, uint32_t width, uint32_t height);

View File

@@ -69,7 +69,7 @@ static void* sdl3_thread(void*)
static void sdl3_initialize_keymap(); static void sdl3_initialize_keymap();
static bool sdl3_initialize() static bool sdl3_initialize(uint32_t* display_w, uint32_t* display_h)
{ {
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS))
{ {
@@ -77,16 +77,15 @@ static bool sdl3_initialize()
return false; return false;
} }
*display_w = *display_h = 0;
const SDL_DisplayID* display_ids = SDL_GetDisplays(nullptr); const SDL_DisplayID* display_ids = SDL_GetDisplays(nullptr);
for (int i = 0; display_ids[i]; i++) for (int i = 0; display_ids[i]; i++)
{ {
SDL_Rect rect; SDL_Rect rect;
if (!SDL_GetDisplayBounds(display_ids[i], &rect)) SDL_GetDisplayBounds(display_ids[i], &rect);
{ *display_w = BAN::Math::max<uint32_t>(*display_w, rect.x + rect.w);
dwarnln("Could not display {} bounds: {}", SDL_GetError()); *display_h = BAN::Math::max<uint32_t>(*display_h, rect.y + rect.h);
continue;
}
register_display(rect.x, rect.y, rect.w, rect.h);
} }
sdl3_initialize_keymap(); sdl3_initialize_keymap();

View File

@@ -28,7 +28,7 @@ struct BananCursor final : public PlatformCursor
static BAN::ErrorOr<void> bananos_initialize_keymap(); static BAN::ErrorOr<void> bananos_initialize_keymap();
static bool bananos_initialize() static bool bananos_initialize(uint32_t* display_w, uint32_t* display_h)
{ {
auto attributes = LibGUI::Window::default_attributes; auto attributes = LibGUI::Window::default_attributes;
attributes.shown = false; attributes.shown = false;
@@ -40,7 +40,8 @@ static bool bananos_initialize()
return false; return false;
} }
register_display(0, 0, dummy_or_error.value()->width(), dummy_or_error.value()->height()); *display_w = dummy_or_error.value()->width();
*display_h = dummy_or_error.value()->height();
if (auto ret = bananos_initialize_keymap(); ret.is_error()) if (auto ret = bananos_initialize_keymap(); ret.is_error())
{ {

View File

@@ -5,8 +5,8 @@
#include <X11/Xatom.h> #include <X11/Xatom.h>
#include <locale.h> #include <locale.h>
#include <poll.h>
#include <signal.h> #include <signal.h>
#include <sys/epoll.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
@@ -19,8 +19,6 @@
#include <netinet/in.h> #include <netinet/in.h>
#endif #endif
CARD32 g_next_global_id { 1 };
const xPixmapFormat g_formats[6] { const xPixmapFormat g_formats[6] {
{ {
.depth = 1, .depth = 1,
@@ -60,7 +58,7 @@ const xDepth g_depth {
}; };
const xVisualType g_visual { const xVisualType g_visual {
.visualID = g_next_global_id++, .visualID = 1,
.c_class = TrueColor, .c_class = TrueColor,
.bitsPerRGB = 8, .bitsPerRGB = 8,
.colormapEntries = 256, .colormapEntries = 256,
@@ -70,7 +68,7 @@ const xVisualType g_visual {
}; };
xWindowRoot g_root { xWindowRoot g_root {
.windowId = g_next_global_id++, .windowId = 2,
.defaultColormap = 0, .defaultColormap = 0,
.whitePixel = 0xFFFFFF, .whitePixel = 0xFFFFFF,
.blackPixel = 0x000000, .blackPixel = 0x000000,
@@ -84,20 +82,17 @@ xWindowRoot g_root {
.rootVisualID = g_visual.visualID, .rootVisualID = g_visual.visualID,
.backingStore = 0, .backingStore = 0,
.saveUnders = 0, .saveUnders = 0,
.rootDepth = g_depth.depth, .rootDepth = 24,
.nDepths = 1, .nDepths = 1,
}; };
BAN::Vector<DisplayInfo> g_displays;
BAN::HashMap<CARD32, BAN::UniqPtr<Object>> g_objects; BAN::HashMap<CARD32, BAN::UniqPtr<Object>> g_objects;
BAN::HashMap<BAN::String, ATOM> g_atoms_name_to_id; BAN::HashMap<BAN::String, ATOM> g_atoms_name_to_id;
BAN::HashMap<ATOM, BAN::String> g_atoms_id_to_name; BAN::HashMap<ATOM, BAN::String> g_atoms_id_to_name;
ATOM g_atom_value = XA_LAST_PREDEFINED + 1; ATOM g_atom_value = XA_LAST_PREDEFINED + 1;
int g_epoll_fd; BAN::HashMap<int, Pollable> g_pollables;
BAN::HashMap<int, EpollThingy> g_epoll_thingies;
int g_server_grabber_fd = -1; int g_server_grabber_fd = -1;
@@ -159,22 +154,6 @@ int main()
return 1; return 1;
} }
g_epoll_fd = epoll_create1(0);
if (g_epoll_fd == -1)
{
perror("xbanan: epoll_create1");
return 1;
}
{
epoll_event event { .events = EPOLLIN, .data = { .ptr = nullptr } };
if (epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, server_sock, &event) == -1)
{
perror("xbanan: epoll_ctl");
return 1;
}
}
#define APPEND_ATOM(name) do { \ #define APPEND_ATOM(name) do { \
MUST(g_atoms_id_to_name.insert(name, #name##_sv.substring(3))); \ MUST(g_atoms_id_to_name.insert(name, #name##_sv.substring(3))); \
MUST(g_atoms_name_to_id.insert(#name##_sv.substring(3), name)); \ MUST(g_atoms_name_to_id.insert(#name##_sv.substring(3), name)); \
@@ -265,62 +244,22 @@ int main()
APPEND_ATOM_CUSTOM(_NET_WM_WINDOW_TYPE_UTILITY); APPEND_ATOM_CUSTOM(_NET_WM_WINDOW_TYPE_UTILITY);
#undef APPEND_ATOM_CUSTOM #undef APPEND_ATOM_CUSTOM
if (!g_platform_ops.initialize()) uint32_t display_w, display_h;
if (!g_platform_ops.initialize(&display_w, &display_h))
return 1; return 1;
g_root.pixWidth = display_w;
if (g_displays.empty()) g_root.pixHeight = display_h;
{ g_root.mmWidth = static_cast<CARD16>(display_w * 254 / 960); // 96 DPI
dwarnln("No displays windows initilized"); g_root.mmHeight = static_cast<CARD16>(display_h * 254 / 960); // 96 DPI
return 1;
}
int32_t display_min_x { INT32_MAX }, display_min_y { INT32_MAX };
int32_t display_max_x { INT32_MIN }, display_max_y { INT32_MIN };
for (const auto& display : g_displays)
{
display_min_x = BAN::Math::min<int32_t>(display_min_x, display.x);
display_min_y = BAN::Math::min<int32_t>(display_min_y, display.y);
display_max_x = BAN::Math::max<int32_t>(display_max_x, display.x + display.w);
display_max_y = BAN::Math::max<int32_t>(display_max_y, display.y + display.h);
}
g_root.pixWidth = display_max_x - display_min_x;
g_root.pixHeight = display_max_y - display_min_y;
g_root.mmWidth = g_root.pixWidth * 254 / 960; // 96 DPI
g_root.mmHeight = g_root.pixHeight * 254 / 960; // 96 DPI
Client dummy_owner;
MUST(g_objects.insert(g_root.windowId, MUST(BAN::UniqPtr<Object>::create(Object {
.type = Object::Type::Window,
.object = Object::Window {
.owner = dummy_owner,
.depth = g_root.rootDepth,
.x = display_min_x,
.y = display_min_y,
.parent = None,
.cursor = None,
.c_class = InputOutput,
.width = g_root.pixWidth,
.height = g_root.pixHeight,
.background = None,
}
}))));
MUST(g_objects.insert(g_visual.visualID,
MUST(BAN::UniqPtr<Object>::create(Object {
.type = Object::Type::Visual,
}))
));
printf("xbanan started\n"); printf("xbanan started\n");
const auto close_client = const auto close_client =
[](int client_fd) [](int client_fd)
{ {
auto& epoll_thingy = g_epoll_thingies[client_fd]; auto& pollable = g_pollables[client_fd];
ASSERT(epoll_thingy.type == EpollThingy::Type::Client); ASSERT(pollable.type == Pollable::Type::Client);
auto& client_info = epoll_thingy.value.get<Client>(); auto& client_info = pollable.value.get<Client>();
dprintln("client {} disconnected", client_fd); dprintln("client {} disconnected", client_fd);
@@ -372,39 +311,68 @@ int main()
g_objects.remove(it); g_objects.remove(it);
} }
epoll_ctl(g_epoll_fd, EPOLL_CTL_DEL, client_fd, nullptr); g_pollables.remove(client_fd);
g_epoll_thingies.remove(client_fd);
close(client_fd); close(client_fd);
if (client_fd == g_server_grabber_fd) if (g_server_grabber_fd == client_fd)
{
g_server_grabber_fd = -1; g_server_grabber_fd = -1;
for (const auto& [_, thingy] : g_epoll_thingies)
{
if (thingy.type != EpollThingy::Type::Client)
continue;
auto& other_client = thingy.value.get<Client>();
uint32_t events = EPOLLIN;
if (other_client.has_epollout)
events |= EPOLLOUT;
epoll_event event { .events = events, .data = { .fd = other_client.fd } };
epoll_ctl(g_epoll_fd, EPOLL_CTL_MOD, other_client.fd, &event);
}
}
}; };
Client dummy_client {};
MUST(g_objects.insert(g_root.windowId,
MUST(BAN::UniqPtr<Object>::create(Object {
.type = Object::Type::Window,
.object = Object::Window {
.owner = dummy_client,
.mapped = true,
.depth = g_root.rootDepth,
.parent = None,
.c_class = InputOutput,
.width = g_root.pixWidth,
.height = g_root.pixHeight,
}
}))
));
MUST(g_objects.insert(g_visual.visualID,
MUST(BAN::UniqPtr<Object>::create(Object {
.type = Object::Type::Visual,
}))
));
for (;;) for (;;)
{ {
epoll_event events[16]; BAN::Vector<pollfd> pollfds;
const int event_count = epoll_wait(g_epoll_fd, events, 16, -1); MUST(pollfds.reserve(g_pollables.size() + 1));
MUST(pollfds.push_back({
for (int i = 0; i < event_count; i++) .fd = server_sock,
.events = POLLIN,
.revents = 0,
}));
for (auto& [fd, pollable] : g_pollables)
{ {
if (events[i].data.ptr == nullptr) short events = 0;
if (g_server_grabber_fd == -1 || g_server_grabber_fd == fd)
events |= POLLIN;
if (pollable.type == Pollable::Type::Client && !pollable.value.get<Client>().output_buffer.empty())
events |= POLLOUT;
if (events == 0)
continue;
MUST(pollfds.push_back(pollfd {
.fd = fd,
.events = events,
.revents = 0,
}));
}
const int event_count = poll(pollfds.data(), pollfds.size(), -1);
for (const auto& pollfd : pollfds)
{
if (pollfd.revents == 0)
continue;
if (pollfd.fd == server_sock)
{ {
int client_sock = accept(server_sock, nullptr, nullptr); int client_sock = accept(server_sock, nullptr, nullptr);
if (client_sock == -1) if (client_sock == -1)
@@ -422,8 +390,8 @@ int main()
client_pid = client_cred.pid; client_pid = client_cred.pid;
#endif #endif
MUST(g_epoll_thingies.insert(client_sock, { MUST(g_pollables.insert(client_sock, {
.type = EpollThingy::Type::Client, .type = Pollable::Type::Client,
.value = Client { .value = Client {
.fd = client_sock, .fd = client_sock,
.state = Client::State::ConnectionSetup, .state = Client::State::ConnectionSetup,
@@ -431,44 +399,32 @@ int main()
} }
})); }));
uint32_t events = 0;
if (g_server_grabber_fd == -1)
events |= EPOLLIN;
epoll_event event = { .events = events, .data = { .fd = client_sock } };
if (epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, client_sock, &event) == -1)
{
perror("xbanan: epoll_ctl");
close(client_sock);
continue;
}
dprintln("client {} connected", client_sock); dprintln("client {} connected", client_sock);
continue; continue;
} }
auto it = g_epoll_thingies.find(events[i].data.fd); auto it = g_pollables.find(pollfd.fd);
if (it == g_epoll_thingies.end()) if (it == g_pollables.end())
continue; continue;
auto& epoll_thingy = it->value; auto& pollable = it->value;
if (epoll_thingy.type == EpollThingy::Type::Event) if (pollable.type == Pollable::Type::Event)
{ {
g_platform_ops.poll_events(epoll_thingy.value.get<void*>()); g_platform_ops.poll_events(pollable.value.get<void*>());
continue; continue;
} }
ASSERT(epoll_thingy.type == EpollThingy::Type::Client); ASSERT(pollable.type == Pollable::Type::Client);
auto& client_info = epoll_thingy.value.get<Client>(); auto& client_info = pollable.value.get<Client>();
if (events[i].events & EPOLLHUP) if (pollfd.revents & POLLHUP)
{ {
close_client(client_info.fd); close_client(client_info.fd);
continue; continue;
} }
if (events[i].events & EPOLLOUT) if (pollfd.revents & POLLOUT)
{ {
const ssize_t nsend = send( const ssize_t nsend = send(
client_info.fd, client_info.fd,
@@ -495,28 +451,11 @@ int main()
); );
MUST(client_info.output_buffer.resize(client_info.output_buffer.size() - nsend)); MUST(client_info.output_buffer.resize(client_info.output_buffer.size() - nsend));
if (client_info.output_buffer.empty())
{
uint32_t events = 0;
if (g_server_grabber_fd == -1 || g_server_grabber_fd == client_info.fd)
events |= EPOLLIN;
epoll_event event { .events = events, .data = { .fd = client_info.fd } };
if (epoll_ctl(g_epoll_fd, EPOLL_CTL_MOD, client_info.fd, &event) == -1)
{
perror("xbanan: epoll_ctl");
close_client(client_info.fd);
continue;
}
client_info.has_epollout = false;
}
send_done: send_done:
(void)0; (void)0;
} }
if (!(events[i].events & EPOLLIN)) if (!(pollfd.revents & POLLIN))
continue; continue;
if (g_server_grabber_fd != -1 && g_server_grabber_fd != client_info.fd) if (g_server_grabber_fd != -1 && g_server_grabber_fd != client_info.fd)
@@ -628,30 +567,5 @@ int main()
} }
} }
} }
iterator_invalidated:
for (auto& [_, thingy] : g_epoll_thingies)
{
if (thingy.type != EpollThingy::Type::Client)
continue;
auto& client_info = thingy.value.get<Client>();
if (client_info.output_buffer.empty() || client_info.has_epollout)
continue;
uint32_t events = EPOLLOUT;
if (g_server_grabber_fd == -1 || g_server_grabber_fd == client_info.fd)
events |= EPOLLIN;
epoll_event event { .events = events, .data = { .fd = client_info.fd } };
if (epoll_ctl(g_epoll_fd, EPOLL_CTL_MOD, client_info.fd, &event) == -1)
{
perror("xbanan: epoll_ctl");
close_client(client_info.fd);
goto iterator_invalidated;
}
client_info.has_epollout = true;
}
} }
} }