Compare commits

..

29 Commits

Author SHA1 Message Date
d0f2b5e8a6 quick hack to replace epoll with poll 2026-06-04 03:32:31 +03:00
a6a581347e Fix X-Resource debug printing 2026-06-04 03:27:06 +03:00
e843be0a1c Cleanup the build system and add porting instructions 2026-06-04 02:26:29 +03:00
421e0a6897 Add very basic X-Resource extension
This only supports XResQueryVersion and XResQueryClientIds but it is
enough to get steam running
2026-06-04 01:30:41 +03:00
e4bbc31c7b Keep track of client's pid 2026-06-04 01:29:04 +03:00
a1f00aa443 Use 12 bits for client id and 20 bits for resource ids
This is more sensible than limiting ourselves to <255 clients
2026-06-04 01:28:15 +03:00
d0606a1939 Remove banan-os's LibInput dependency
Platforms now define their own keymaps
2026-06-03 19:50:45 +03:00
aa70815f5f Implement very basic grab pointer
This is definitely not correct with child windows but works with single
window apps (minecraft)
2026-06-01 22:24:17 +03:00
19f043744a Allow querying global cursor position
If a platform implements this, e.g. xeyes can track cursor when the
window is not receiving mouse movement events
2026-06-01 22:04:01 +03:00
af2132315d Implement WarpPointer
This allows playing games that capture the cursor
2026-06-01 22:04:01 +03:00
83af554e8c Add support for global window position
This allows proper TranslateCoords and event root coordinate reporting
2026-06-01 20:39:47 +03:00
b41d979dcb Port to SDL3 instead of SDL2
Main reason is that SDL3 allows getting keyboard mappings with modifiers
2026-06-01 20:35:56 +03:00
11ebced96a Destroy client's windows on disconnect
This makes sure client's windows are removed from the root window and
all of the destroy events are sent.
2026-06-01 20:35:56 +03:00
504eb2716b Fix QueryPointer
QueryPointer is supposed to return first child of the window containing
the pointer, not the deepest child. This fixes gtk getting into an
invalid state when pressing a mouse button
2026-06-01 20:35:56 +03:00
82edc9c76b Handle external window leave events 2026-06-01 20:35:56 +03:00
92e02dcadf Don't create platform window for small windows
These are usually internal event windows which should not be visible.
I'm not sure what the correct check for this would be...
gtk and other toolkits create a lot of dummy windows that should not be
visible.
2026-06-01 20:35:56 +03:00
97e82afd4d Optimize {,Shm}PutImage
Don't calculate clipping when its not needed. This can still be improved
by a ton, but it can wait :)
2026-06-01 04:55:43 +03:00
d787afb7e3 Disable shared pixmaps on MIT-SHM
Our internal pixmap representation does not match what we advertice in
out pixmap formats, and there is no way to only enable 24 and 32 depth
pixmaps for SHM so just disable them all together. MIT-SHM can still be
used through Shm{Get,Put}Image. We probably should store pixmaps as we
say we do, but its too much work for now :^)
2026-06-01 04:55:43 +03:00
f6a87f064f Add support for system cursors
When a client creates a cursor from the default cursor font, we try to
detect which cursor it is and create a native system cursor based on
that.
2026-06-01 04:55:43 +03:00
7fe5b0174f Allow sending enter/leave events between different native windows
This fixes hovering on popups
2026-06-01 03:01:31 +03:00
6450fd9a2f Add initial support for window types
This makes popup windows work much better
2026-06-01 03:01:31 +03:00
2ddd67dede Start work on making xbanan portable
This will allow usage of xbanan on non banan-os platforms. I added a
"native" SDL2 port so it can be used without the window manager
2026-06-01 01:27:49 +03:00
b2c642f03d Don't make SHM extension dependent option
This was here because I though banan-os didn't define them, but they do
exist there :^)
2026-04-15 19:23:34 +03:00
654e878165 Allow compiling with just xorgproto installed 2026-04-15 19:22:56 +03:00
5076d3bbaf Add missing sys/socket.h include 2026-04-15 18:53:45 +03:00
7be8edada7 Add child window id to its parent
I had accidentally removed this in b228ef13c4 and it broke all programs
that were using child windows :D
2026-04-15 18:43:03 +03:00
07f2b5dbb7 Add hacky way to die when WindowServer dies
We keep a dummy window around which will receive a close event that
exits once WindowServer dies :^)
2026-04-15 18:21:03 +03:00
a497e4267f Add README and LICENSE 2026-04-15 17:54:40 +03:00
7252c985b8 Add banan-os as a submodule instead of manually copying libraries 2026-04-15 17:54:31 +03:00
27 changed files with 2775 additions and 1602 deletions

9
.clangd Normal file
View File

@@ -0,0 +1,9 @@
Diagnostics:
Suppress: [
c99-designator,
]
CompileFlags:
Add: [
-std=c++20,
]

View File

@@ -1,6 +1,6 @@
From 075f6a11926e247ba64efd25a3acf7a3d3be0bbc Mon Sep 17 00:00:00 2001 From c9dd87198ce5262e6ddf6bf3b0c18eb84784d35e Mon Sep 17 00:00:00 2001
From: Oskari Alaranta <oskari.alaranta@bananymous.com> From: Oskari Alaranta <oskari.alaranta@bananymous.com>
Date: Wed, 15 Apr 2026 16:51:16 +0300 Date: Wed, 15 Apr 2026 17:52:54 +0300
Subject: [PATCH] linux-window-server-sdl2 Subject: [PATCH] linux-window-server-sdl2
--- ---
@@ -13,8 +13,8 @@ Subject: [PATCH] linux-window-server-sdl2
userspace/programs/WindowServer/Window.cpp | 29 +- userspace/programs/WindowServer/Window.cpp | 29 +-
.../programs/WindowServer/WindowServer.cpp | 47 ++- .../programs/WindowServer/WindowServer.cpp | 47 ++-
.../programs/WindowServer/WindowServer.h | 1 + .../programs/WindowServer/WindowServer.h | 1 +
userspace/programs/WindowServer/main.cpp | 362 ++++++++++++------ userspace/programs/WindowServer/main.cpp | 357 ++++++++++++------
10 files changed, 338 insertions(+), 173 deletions(-) 10 files changed, 340 insertions(+), 166 deletions(-)
diff --git a/userspace/libraries/LibGUI/Widget/Widget.cpp b/userspace/libraries/LibGUI/Widget/Widget.cpp diff --git a/userspace/libraries/LibGUI/Widget/Widget.cpp b/userspace/libraries/LibGUI/Widget/Widget.cpp
index d6489d87..c532fb04 100644 index d6489d87..c532fb04 100644
@@ -376,7 +376,7 @@ index 94fbc774..bcd7a6b9 100644
private: private:
void on_mouse_move_impl(int32_t new_x, int32_t new_y); void on_mouse_move_impl(int32_t new_x, int32_t new_y);
diff --git a/userspace/programs/WindowServer/main.cpp b/userspace/programs/WindowServer/main.cpp diff --git a/userspace/programs/WindowServer/main.cpp b/userspace/programs/WindowServer/main.cpp
index 41bd7e8c..cb066bd3 100644 index 46f2ba6d..520c8e7d 100644
--- a/userspace/programs/WindowServer/main.cpp --- a/userspace/programs/WindowServer/main.cpp
+++ b/userspace/programs/WindowServer/main.cpp +++ b/userspace/programs/WindowServer/main.cpp
@@ -10,7 +10,6 @@ @@ -10,7 +10,6 @@
@@ -425,7 +425,7 @@ index 41bd7e8c..cb066bd3 100644
signal(sig, exit); signal(sig, exit);
for (int sig : non_terminating_signals) for (int sig : non_terminating_signals)
signal(sig, SIG_DFL); signal(sig, SIG_DFL);
@@ -208,55 +202,10 @@ int main() @@ -208,51 +202,15 @@ int main()
signal(sig, SIG_IGN); signal(sig, SIG_IGN);
MUST(LibInput::KeyboardLayout::initialize()); MUST(LibInput::KeyboardLayout::initialize());
@@ -469,20 +469,18 @@ index 41bd7e8c..cb066bd3 100644
dprintln("Window server started"); dprintln("Window server started");
- if (access("/usr/bin/xbanan", X_OK) == 0) - if (access("/usr/bin/xbanan", X_OK) == 0)
- { + if (access("./build/xbanan/xbanan", X_OK) == 0)
- if (fork() == 0) {
- { if (fork() == 0)
{
- dup2(STDDBG_FILENO, STDOUT_FILENO); - dup2(STDDBG_FILENO, STDOUT_FILENO);
- dup2(STDDBG_FILENO, STDERR_FILENO); - dup2(STDDBG_FILENO, STDERR_FILENO);
- execl("/usr/bin/xbanan", "xbanan", NULL); - execl("/usr/bin/xbanan", "xbanan", NULL);
- exit(1); + execl("./build/xbanan/xbanan", "xbanan", NULL);
- } exit(1);
- } }
- }
auto config = parse_config(); @@ -276,26 +234,23 @@ int main()
WindowServer window_server(framebuffer, config.corner_radius);
@@ -276,26 +225,23 @@ int main()
uint64_t last_sync_us = get_current_us() - sync_interval_us; uint64_t last_sync_us = get_current_us() - sync_interval_us;
while (!window_server.is_stopped()) while (!window_server.is_stopped())
{ {
@@ -520,7 +518,7 @@ index 41bd7e8c..cb066bd3 100644
if (epoll_events == -1 && errno != EINTR) if (epoll_events == -1 && errno != EINTR)
{ {
dwarnln("epoll_pwait2: {}", strerror(errno)); dwarnln("epoll_pwait2: {}", strerror(errno));
@@ -333,48 +279,6 @@ int main() @@ -333,48 +288,6 @@ int main()
continue; continue;
} }
@@ -569,7 +567,7 @@ index 41bd7e8c..cb066bd3 100644
const int client_fd = events[i].data.fd; const int client_fd = events[i].data.fd;
if (events[i].events & (EPOLLHUP | EPOLLERR)) if (events[i].events & (EPOLLHUP | EPOLLERR))
{ {
@@ -500,3 +404,237 @@ int main() @@ -500,3 +413,237 @@ int main()
} }
} }
} }

View File

@@ -26,7 +26,14 @@ set(CMAKE_CXX_STANDARD 20)
add_compile_definitions(-Dstddbg=stdout) add_compile_definitions(-Dstddbg=stdout)
add_compile_options(-g) add_compile_options(-g)
if(BANAN_OS) set(PLATFORM "banan-os" CACHE STRING "target platform")
set(VALID_PLATFORMS "banan-os" "SDL3")
if(NOT PLATFORM IN_LIST VALID_PLATFORMS)
message(FATAL_ERROR "platform \"${PLATFORM}\" not known, valid platforms are ${VALID_PLATFORMS}")
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "banan-os")
find_library(BAN ban REQUIRED) find_library(BAN ban REQUIRED)
add_library(ban SHARED IMPORTED) add_library(ban SHARED IMPORTED)
set_target_properties(ban PROPERTIES IMPORTED_LOCATION "${BAN}") set_target_properties(ban PROPERTIES IMPORTED_LOCATION "${BAN}")
@@ -48,8 +55,10 @@ else()
set_target_properties(libc PROPERTIES IMPORTED_LOCATION "${LIBC}") set_target_properties(libc PROPERTIES IMPORTED_LOCATION "${LIBC}")
add_subdirectory(banan-os/BAN) add_subdirectory(banan-os/BAN)
add_subdirectory(banan-os/userspace/libraries/LibClipboard)
add_subdirectory(banan-os/userspace/libraries/LibDEFLATE) add_subdirectory(banan-os/userspace/libraries/LibDEFLATE)
if(PLATFORM STREQUAL "banan-os")
add_subdirectory(banan-os/userspace/libraries/LibClipboard)
add_subdirectory(banan-os/userspace/libraries/LibFont) add_subdirectory(banan-os/userspace/libraries/LibFont)
add_subdirectory(banan-os/userspace/libraries/LibGUI) add_subdirectory(banan-os/userspace/libraries/LibGUI)
add_subdirectory(banan-os/userspace/libraries/LibImage) add_subdirectory(banan-os/userspace/libraries/LibImage)
@@ -59,5 +68,6 @@ else()
add_subdirectory(banan-os/userspace/programs/Terminal) add_subdirectory(banan-os/userspace/programs/Terminal)
add_subdirectory(banan-os/userspace/programs/WindowServer) add_subdirectory(banan-os/userspace/programs/WindowServer)
endif() endif()
endif()
add_subdirectory(xbanan) add_subdirectory(xbanan)

24
LICENSE Normal file
View File

@@ -0,0 +1,24 @@
BSD 2-Clause License
Copyright (c) 2026, Oskari Alaranta
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

45
README.md Normal file
View File

@@ -0,0 +1,45 @@
# xbanan
A portable X11 compatibility layer. There are 2 officially supported platforms: [banan-os's](https://git.bananymous.com/Bananymous/banan-os) native GUI windowing system and SDL3.
## Running on linux
### Prerequisites
You need to have SDL3 development library and X11 headers installed on your system.
### Building
```sh
git clone --recursive https://git.bananymous.com/Bananymous/xbanan.git
cd xbanan
cmake -B build -S . -DPLATFORM=SDL3
cmake --build build --parallel $(nproc)
```
### Running
To start xbanan, run the following command in project root
```sh
./build/xbanan/xbanan
```
To run X11 apps, specify `DISPLAY=:69` environment variable. For example
```sh
DISPLAY=:69 xeyes
```
## Porting to a new platform
Add your platform to the `VALID_PLATFORMS` list in the root `CMakeLists.txt` file. Create a directory for your platform in `xbanan/<your platform>` and define the needed source files and libraries in `xbanan/CMakeLists.txt` based on your platform.
### Barebones
For the simplest port, you need to implement 3 functions: `initialize`, `create_window` and `invalidate`. Define the variable `g_platform_ops` declared in `xbanan/Platform.h` with function pointers to the your functions. You can look at the existing platforms and the `xbanan/Platform.h` header for help.
### Events
To handle events, you have to hook an event receiver fd for your platform using `register_event_fd` declared in `xbanan/Events.h` and define the `poll_events` function in `g_platform_ops`. When the registered fd becomes readable, xbanan will call your platform's `poll_events` function. This function should drain all of the pending events and call appropriate `on_*_event` function declared in the events header. If your platform does not expose fd(s) that can be polled for events, you can do what the SDL3 port does and create an eventfd/pipe that gets signaled periodically.
### Installation
To install xbanan to sysroot, just copy the `build/xbanan/xbanan` to the destination. xbanan is statically linked against the libraries from this repository so you don't have to care about those. **DO NOT** use the cmake's install target, as that installs banan-os libraries to the root of the sysroot. You also need to have the x11 misc fonts at `FONT_PATH/misc` in your sysroot if you want server side font support. The fonts are shipped (public domain) with xbanan, so you can just copy `fonts/misc` into `FONT_PATH`. You can specify the `FONT_PATH` with cmake using `-DFONT_PATH=...` when configuring.

File diff suppressed because it is too large Load Diff

View File

@@ -6,3 +6,18 @@ BAN::ErrorOr<void> setup_client_conneciton(Client& client_info, const xConnClien
BAN::ErrorOr<void> handle_packet(Client& client_info, BAN::ConstByteSpan packet); BAN::ErrorOr<void> handle_packet(Client& client_info, BAN::ConstByteSpan packet);
void invalidate_window(WINDOW wid, int32_t x, int32_t y, int32_t w, int32_t h); void invalidate_window(WINDOW wid, int32_t x, int32_t y, int32_t w, int32_t h);
void send_exposure_recursive(WINDOW wid);
void update_cursor(WINDOW wid, int32_t x, int32_t y);
BAN::ErrorOr<void> destroy_window(Client& client_info, WINDOW wid);
WINDOW find_child_window(WINDOW wid, int32_t& x, int32_t& y);
xPoint get_window_position(WINDOW wid);
static constexpr uint32_t COLOR_INVISIBLE = 0x69000000;
extern CARD16 g_keymask;
extern CARD16 g_butmask;
extern WINDOW g_focus_window;

View File

@@ -1,37 +1,49 @@
set(SOURCES set(XBANAN_SOURCES
main.cpp main.cpp
Base.cpp Base.cpp
Drawing.cpp Drawing.cpp
Events.cpp
Extensions.cpp Extensions.cpp
ExtBigReg.cpp ExtBigReg.cpp
ExtRANDR.cpp ExtRANDR.cpp
ExtXRes.cpp
Font.cpp Font.cpp
Image.cpp Image.cpp
Keymap.cpp Keymap.cpp
SafeGetters.cpp SafeGetters.cpp
) )
set(FONT_PATH ${CMAKE_SOURCE_DIR}/fonts CACHE STRING "path to X11 fonts")
option(ENABLE_GLX "enable glx extension" ON) option(ENABLE_GLX "enable glx extension" ON)
include(CheckSymbolExists)
include(CMakeDependentOption)
check_symbol_exists(shmat "sys/shm.h" HAVE_SHMAT)
check_symbol_exists(shmdt "sys/shm.h" HAVE_SHMDT)
cmake_dependent_option(ENABLE_SHM "enable shm extension" ON "HAVE_SHMAT;HAVE_SHMDT" OFF)
if(ENABLE_GLX) if(ENABLE_GLX)
set(SOURCES ${SOURCES} ExtGLX.cpp) list(APPEND XBANAN_SOURCES ExtGLX.cpp)
endif() endif()
option(ENABLE_SHM "enable shm extension" ON)
if(ENABLE_SHM) if(ENABLE_SHM)
set(SOURCES ${SOURCES} ExtSHM.cpp) list(APPEND XBANAN_SOURCES ExtSHM.cpp)
endif() endif()
add_executable(xbanan ${SOURCES}) if(PLATFORM STREQUAL "banan-os")
list(APPEND XBANAN_SOURCES banan-os/banan-os.cpp)
elseif(PLATFORM STREQUAL "SDL3")
list(APPEND XBANAN_SOURCES SDL3/sdl3.cpp)
endif()
add_executable(xbanan ${XBANAN_SOURCES})
banan_link_library(xbanan ban) banan_link_library(xbanan ban)
banan_link_library(xbanan libdeflate) banan_link_library(xbanan libdeflate)
target_compile_definitions(xbanan PRIVATE FONT_PATH="${FONT_PATH}")
if(PLATFORM STREQUAL "banan-os")
banan_link_library(xbanan libgui) banan_link_library(xbanan libgui)
banan_link_library(xbanan libinput) banan_link_library(xbanan libinput)
elseif(PLATFORM STREQUAL "SDL3")
find_package(SDL3 REQUIRED)
banan_link_library(xbanan SDL3::SDL3)
endif()
target_compile_options(xbanan PRIVATE -Wall -Wextra) target_compile_options(xbanan PRIVATE -Wall -Wextra)
target_compile_options(xbanan PRIVATE target_compile_options(xbanan PRIVATE

View File

@@ -1,13 +1,14 @@
#pragma once #pragma once
#include "Font.h" #include "Font.h"
#include "Platform.h"
#include "Types.h"
#include <BAN/Vector.h>
#include <BAN/UniqPtr.h>
#include <BAN/HashMap.h> #include <BAN/HashMap.h>
#include <BAN/HashSet.h> #include <BAN/HashSet.h>
#include <BAN/String.h>
#include <LibGUI/Window.h> #include <BAN/UniqPtr.h>
#include <BAN/Vector.h>
#include <X11/Xproto.h> #include <X11/Xproto.h>
@@ -18,15 +19,6 @@
#define dprintln(...) #define dprintln(...)
#endif #endif
typedef CARD32 ATOM;
typedef CARD32 BITGRAVITY;
typedef CARD32 COLORMAP;
typedef CARD32 CURSOR;
typedef CARD32 PIXMAP;
typedef CARD32 VISUALID;
typedef CARD32 WINDOW;
typedef CARD32 WINGRAVITY;
struct Property struct Property
{ {
CARD8 format; CARD8 format;
@@ -49,20 +41,15 @@ struct Object
Type type; Type type;
struct Cursor
{
uint32_t width;
uint32_t height;
int32_t origin_x;
int32_t origin_y;
BAN::Vector<uint32_t> pixels;
};
struct Window struct Window
{ {
Client& owner;
bool mapped { false }; bool mapped { false };
bool focused { false }; bool focused { false };
bool fullscreen { false }; bool fullscreen { false };
bool hovered { false };
uint8_t depth { 0 }; uint8_t depth { 0 };
int32_t x { 0 }; int32_t x { 0 };
int32_t y { 0 }; int32_t y { 0 };
@@ -72,33 +59,19 @@ struct Object
CURSOR cursor; CURSOR cursor;
CARD16 c_class; CARD16 c_class;
BAN::Vector<WINDOW> children; BAN::Vector<WINDOW> children;
BAN::Variant<
BAN::UniqPtr<LibGUI::Window>, uint32_t width { 0 };
LibGUI::Texture uint32_t height { 0 };
> window; BAN::Vector<uint32_t> pixels;
uint32_t background { 0 };
BAN::UniqPtr<PlatformWindow> platform_window;
BAN::HashMap<Client*, uint32_t> event_masks; BAN::HashMap<Client*, uint32_t> event_masks;
BAN::HashMap<ATOM, Property> properties; BAN::HashMap<ATOM, Property> properties;
LibGUI::Texture& texture()
{
if (window.has<LibGUI::Texture>())
return window.get<LibGUI::Texture>();
if (window.has<BAN::UniqPtr<LibGUI::Window>>())
return window.get<BAN::UniqPtr<LibGUI::Window>>()->texture();
ASSERT_NOT_REACHED();
}
const LibGUI::Texture& texture() const
{
if (window.has<LibGUI::Texture>())
return window.get<LibGUI::Texture>();
if (window.has<BAN::UniqPtr<LibGUI::Window>>())
return window.get<BAN::UniqPtr<LibGUI::Window>>()->texture();
ASSERT_NOT_REACHED();
}
uint32_t full_event_mask() const; uint32_t full_event_mask() const;
BAN::ErrorOr<void> send_event(xEvent event, uint32_t event_mask); BAN::ErrorOr<void> send_event(xEvent event, uint32_t event_mask);
}; };
@@ -108,8 +81,7 @@ struct Object
CARD8 depth; CARD8 depth;
CARD32 width; CARD32 width;
CARD32 height; CARD32 height;
BAN::ByteSpan data; BAN::Vector<uint8_t> data;
BAN::Vector<uint8_t> owned_data;
}; };
struct GraphicsContext struct GraphicsContext
@@ -156,7 +128,7 @@ struct Object
void (*destructor)(Extension&); void (*destructor)(Extension&);
}; };
BAN::Variant<Cursor, Window, Pixmap, GraphicsContext, BAN::RefPtr<PCFFont>, Extension> object; BAN::Variant<Window, Pixmap, GraphicsContext, BAN::RefPtr<PCFFont>, BAN::UniqPtr<PlatformCursor>, Extension> object;
}; };
struct Client struct Client
@@ -168,30 +140,30 @@ 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::Vector<uint8_t> input_buffer; BAN::Vector<uint8_t> input_buffer;
BAN::Vector<uint8_t> output_buffer; BAN::Vector<uint8_t> output_buffer;
BAN::HashSet<CARD32> objects; BAN::HashSet<CARD32> objects;
}; };
struct EpollThingy struct Pollable
{ {
enum class Type enum class Type
{ {
Client, Client,
Window, Event,
}; };
Type type; Type type;
BAN::Variant<Client, LibGUI::Window*> value; BAN::Variant<Client, void*> value;
}; };
extern const xPixmapFormat g_formats[6]; extern const xPixmapFormat g_formats[6];
extern const xWindowRoot g_root;
extern const xDepth g_depth; extern const xDepth g_depth;
extern const xVisualType g_visual; extern const xVisualType g_visual;
extern xWindowRoot g_root;
extern BAN::HashMap<CARD32, BAN::UniqPtr<Object>> g_objects; extern BAN::HashMap<CARD32, BAN::UniqPtr<Object>> g_objects;
@@ -199,7 +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;

View File

@@ -300,6 +300,9 @@ BAN::ErrorOr<void> poly_fill_rectangle(Client& client_info, BAN::ConstByteSpan p
const int32_t max_x = BAN::Math::min<int32_t>(rect.x + rect.width, out_w); const int32_t max_x = BAN::Math::min<int32_t>(rect.x + rect.width, out_w);
const int32_t max_y = BAN::Math::min<int32_t>(rect.y + rect.height, out_h); const int32_t max_y = BAN::Math::min<int32_t>(rect.y + rect.height, out_h);
if (min_x >= max_x || min_y >= max_y)
continue;
for (int32_t y = min_y; y < max_y; y++) for (int32_t y = min_y; y < max_y; y++)
for (int32_t x = min_x; x < max_x; x++) for (int32_t x = min_x; x < max_x; x++)
if (!gc.is_clipped(x, y)) if (!gc.is_clipped(x, y))
@@ -351,6 +354,9 @@ BAN::ErrorOr<void> poly_fill_arc(Client& client_info, BAN::ConstByteSpan packet)
const int32_t max_x = BAN::Math::min<int32_t>(out_w, arc.x + arc.width); const int32_t max_x = BAN::Math::min<int32_t>(out_w, arc.x + arc.width);
const int32_t max_y = BAN::Math::min<int32_t>(out_h, arc.y + arc.height); const int32_t max_y = BAN::Math::min<int32_t>(out_h, arc.y + arc.height);
if (min_x >= max_x || min_y >= max_y)
continue;
const auto rx = arc.width / 2; const auto rx = arc.width / 2;
const auto ry = arc.height / 2; const auto ry = arc.height / 2;

597
xbanan/Events.cpp Normal file
View File

@@ -0,0 +1,597 @@
#include "Base.h"
#include "Definitions.h"
#include "Keymap.h"
#include "Utils.h"
#include <X11/X.h>
#include <X11/Xatom.h>
#include <time.h>
void register_event_fd(int fd, void* data)
{
MUST(g_pollables.insert(fd, {
.type = Pollable::Type::Event,
.value = data,
}));
}
void unregister_event_fd(int fd)
{
g_pollables.remove(fd);
}
void on_window_close_event(WINDOW wid)
{
static CARD32 WM_PROTOCOLS = g_atoms_name_to_id["WM_PROTOCOLS"_sv];
static CARD32 WM_DELETE_WINDOW = g_atoms_name_to_id["WM_DELETE_WINDOW"_sv];
auto& object = *g_objects[wid];
ASSERT(object.type == Object::Type::Window);
auto& window = object.object.get<Object::Window>();
const bool supports_wm_delete_winow =
[&window]
{
auto wm_protocols_it = window.properties.find(WM_PROTOCOLS);
if (wm_protocols_it == window.properties.end())
return false;
const auto& wm_protocols = wm_protocols_it->value;
if (wm_protocols.type != XA_ATOM || wm_protocols.format != 32)
return false;
const auto atoms = BAN::ConstByteSpan(wm_protocols.data.span()).as_span<const CARD32>();
for (auto atom : atoms)
if (atom == WM_DELETE_WINDOW)
return true;
return false;
}();
if (!supports_wm_delete_winow)
MUST(destroy_window(window.owner, wid));
else
{
xEvent event { .u = {
.clientMessage = {
.window = wid,
.u = { .l = {
.type = WM_PROTOCOLS,
.longs0 = static_cast<INT32>(WM_DELETE_WINDOW),
.longs1 = static_cast<INT32>(time(nullptr)),
}}
}
}};
event.u.u.type = ClientMessage;
event.u.u.detail = 32;
event.u.u.sequenceNumber = window.owner.sequence;
MUST(encode(window.owner.output_buffer, event));
}
}
static void send_window_configure(WINDOW wid, Object::Window& window)
{
{
xEvent event = { .u = {
.configureNotify = {
.event = wid,
.window = wid,
.aboveSibling = xFalse,
.x = static_cast<INT16>(window.x),
.y = static_cast<INT16>(window.y),
.width = static_cast<CARD16>(window.width),
.height = static_cast<CARD16>(window.height),
.borderWidth = 0,
.override = xFalse,
}
}};
event.u.u.type = ConfigureNotify;
MUST(window.send_event(event, StructureNotifyMask));
}
auto& parent_object = *g_objects[window.parent];
ASSERT(parent_object.type == Object::Type::Window);
auto& parent_window = parent_object.object.get<Object::Window>();
{
xEvent event = { .u = {
.configureNotify = {
.event = window.parent,
.window = wid,
.aboveSibling = xFalse,
.x = static_cast<INT16>(window.x),
.y = static_cast<INT16>(window.y),
.width = static_cast<CARD16>(window.width),
.height = static_cast<CARD16>(window.height),
.borderWidth = 0,
.override = xFalse,
}
}};
event.u.u.type = ConfigureNotify;
MUST(parent_window.send_event(event, SubstructureNotifyMask));
}
}
void on_window_resize_event(WINDOW wid, uint32_t new_width, uint32_t new_height)
{
auto& object = *g_objects[wid];
ASSERT(object.type == Object::Type::Window);
auto& window = object.object.get<Object::Window>();
{
window.width = new_width;
window.height = new_height;
MUST(window.pixels.resize(new_width * new_height));
for (auto& pixel : window.pixels)
pixel = window.background;
}
send_window_configure(wid, window);
send_exposure_recursive(wid);
invalidate_window(wid, 0, 0, window.width, window.height);
}
void on_window_move_event(WINDOW wid, int32_t x, int32_t y)
{
auto& object = *g_objects[wid];
ASSERT(object.type == Object::Type::Window);
auto& window = object.object.get<Object::Window>();
window.x = x;
window.y = y;
send_window_configure(wid, window);
}
void on_window_focus_event(WINDOW wid, bool focused)
{
if (focused)
g_focus_window = wid;
auto& object = *g_objects[wid];
ASSERT(object.type == Object::Type::Window);
auto& window = object.object.get<Object::Window>();
if (window.focused == focused)
return;
window.focused = focused;
// FIXME: handle children
xEvent event { .u = {
.focus = {
.window = wid,
.mode = NotifyNormal,
}
}};
event.u.u.type = focused ? FocusIn : FocusOut,
event.u.u.detail = NotifyNonlinear;
MUST(window.send_event(event, FocusChangeMask));
}
void on_window_fullscreen_event(WINDOW wid, bool is_fullscreen)
{
static CARD32 _NET_WM_STATE = g_atoms_name_to_id["_NET_WM_STATE"_sv];
static CARD32 _NET_WM_STATE_FULLSCREEN = g_atoms_name_to_id["_NET_WM_STATE_FULLSCREEN"_sv];
auto window_it = g_objects.find(wid);
if (window_it == g_objects.end() || window_it->value->type != Object::Type::Window)
return;
auto& window = window_it->value->object.get<Object::Window>();
window.fullscreen = is_fullscreen;
auto it = window.properties.find(_NET_WM_STATE);
if (it == window.properties.end())
it = MUST(window.properties.emplace(_NET_WM_STATE));
auto& _net_wm_state = it->value;
if (_net_wm_state.type != XA_ATOM || _net_wm_state.format != 32)
_net_wm_state = {};
_net_wm_state.type = XA_ATOM;
_net_wm_state.format = 32;
for (size_t i = 0; i + 4 <= _net_wm_state.data.size(); i += 4)
{
const auto atom = *reinterpret_cast<const CARD32*>(_net_wm_state.data.data() + i);
if (atom != _NET_WM_STATE_FULLSCREEN)
continue;
_net_wm_state.data.remove(i);
_net_wm_state.data.remove(i);
_net_wm_state.data.remove(i);
_net_wm_state.data.remove(i);
i -= 4;
}
if (is_fullscreen)
{
const size_t atom_count = _net_wm_state.data.size() / 4;
MUST(_net_wm_state.data.resize(_net_wm_state.data.size() + 4));
reinterpret_cast<CARD32*>(_net_wm_state.data.data())[atom_count] = _NET_WM_STATE_FULLSCREEN;
}
xEvent event = { .u = {
.property = {
.window = wid,
.atom = _NET_WM_STATE,
.time = static_cast<CARD32>(time(nullptr)),
.state = PropertyNewValue,
}
}};
event.u.u.type = PropertyNotify;
MUST(window.send_event(event, PropertyChangeMask));
}
static void send_key_button_pointer_event(WINDOW root_wid, BYTE detail, uint32_t event_mask, BYTE event_type, KeyButMask state)
{
int32_t event_x, event_y;
{
auto& object = *g_objects[root_wid];
ASSERT(object.type == Object::Type::Window);
auto& window = object.object.get<Object::Window>();
event_x = window.cursor_x;
event_y = window.cursor_y;
}
const auto child_wid = find_child_window(root_wid, event_x, event_y);
auto wid = child_wid;
while (wid != None)
{
auto& object = *g_objects[wid];
ASSERT(object.type == Object::Type::Window);
auto& window = object.object.get<Object::Window>();
if (window.full_event_mask() & event_mask)
break;
event_x += window.x;
event_y += window.y;
wid = window.parent;
}
if (wid == None)
{
if (event_type == MotionNotify && g_butmask == 0)
return;
wid = root_wid;
}
auto& object = *g_objects[wid];
ASSERT(object.type == Object::Type::Window);
auto& window = object.object.get<Object::Window>();
const auto [root_x, root_y] = get_window_position(wid);
xEvent event { .u = {
.keyButtonPointer = {
.time = static_cast<CARD32>(time(nullptr)),
.root = g_root.windowId,
.event = wid,
.child = static_cast<CARD32>(child_wid == wid ? None : child_wid),
.rootX = static_cast<INT16>(root_x + event_x),
.rootY = static_cast<INT16>(root_y + event_y),
.eventX = static_cast<INT16>(event_x),
.eventY = static_cast<INT16>(event_y),
.state = state,
.sameScreen = xTrue,
}
}};
event.u.u.type = event_type,
event.u.u.detail = detail;
MUST(window.send_event(event, event_mask));
}
static void update_cursor_position_recursive(WINDOW wid, int32_t new_x, int32_t new_y)
{
auto& object = *g_objects[wid];
ASSERT(object.type == Object::Type::Window);
auto& window = object.object.get<Object::Window>();
window.cursor_x = new_x;
window.cursor_y = new_y;
for (auto child_wid : window.children)
{
auto& child_object = *g_objects[child_wid];
ASSERT(child_object.type == Object::Type::Window);
auto& child_window = child_object.object.get<Object::Window>();
update_cursor_position_recursive(child_wid, new_x - child_window.x, new_y - child_window.y);
}
}
static BAN::Vector<WINDOW> get_path_to_child(WINDOW wid, int32_t x, int32_t y)
{
BAN::Vector<WINDOW> result;
const auto window_contains =
[](const Object::Window& window, int32_t x, int32_t y) -> bool
{
return x >= 0 && y >= 0 && x < window.width && y < window.height;
};
for (;;)
{
const auto& object = *g_objects[wid];
ASSERT(object.type == Object::Type::Window);
const auto& window = object.object.get<Object::Window>();
if (!window_contains(window, x, y))
break;
MUST(result.push_back(wid));
const WINDOW old_wid = wid;
for (auto child_wid : window.children)
{
const auto& child_object = *g_objects[child_wid];
ASSERT(child_object.type == Object::Type::Window);
const auto& child_window = child_object.object.get<Object::Window>();
if (!window_contains(child_window, x - child_window.x, y - child_window.y))
continue;
wid = child_wid;
break;
}
if (old_wid == wid)
break;
}
return result;
}
static void send_enter_and_leave_events(WINDOW old_wid, int32_t old_x, int32_t old_y, WINDOW new_wid, int32_t new_x, int32_t new_y)
{
// FIXME: correct event_x and event_y values in events
const auto old_child_path = get_path_to_child(old_wid, old_x, old_y);
const auto new_child_path = get_path_to_child(new_wid, new_x, new_y);
size_t common_ancestors = 0;
while (common_ancestors < old_child_path.size() && common_ancestors < new_child_path.size())
{
if (old_child_path[common_ancestors] != new_child_path[common_ancestors])
break;
common_ancestors++;
}
if (old_child_path.size() == common_ancestors && new_child_path.size() == common_ancestors)
return;
const bool linear = (common_ancestors == old_child_path.size() || common_ancestors == new_child_path.size());
const auto get_flags =
[](WINDOW wid) -> BYTE
{
return ELFlagSameScreen | (g_focus_window == wid ? ELFlagFocus : 0);
};
size_t leave_events = old_child_path.size() - common_ancestors;
if (linear && common_ancestors && old_child_path.size() < new_child_path.size())
leave_events++;
size_t enter_events = new_child_path.size() - common_ancestors;
if (linear && common_ancestors && new_child_path.size() < old_child_path.size())
enter_events++;
for (size_t i = 0; i < leave_events; i++)
{
const bool first = (i == 0);
const BYTE detail =
[&]() -> BYTE {
if (!linear)
return first ? NotifyNonlinear : NotifyNonlinearVirtual;
if (!first)
return NotifyVirtual;
return (old_child_path.size() > new_child_path.size())
? NotifyAncestor
: NotifyInferior;
}();
const auto& wid = old_child_path[old_child_path.size() - i - 1];
auto& object = *g_objects[wid];
ASSERT(object.type == Object::Type::Window);
auto& window = object.object.get<Object::Window>();
const auto [root_x, root_y] = get_window_position(wid);
xEvent event { .u = {
.enterLeave = {
.time = static_cast<CARD32>(time(nullptr)),
.root = g_root.windowId,
.event = wid,
.child = first ? static_cast<WINDOW>(None) : old_child_path.back(),
.rootX = static_cast<INT16>(root_x + old_x),
.rootY = static_cast<INT16>(root_y + old_y),
.eventX = static_cast<INT16>(old_x),
.eventY = static_cast<INT16>(old_y),
.state = static_cast<KeyButMask>(g_keymask | g_butmask),
.mode = NotifyNormal,
.flags = get_flags(wid),
}
}};
event.u.u.type = LeaveNotify,
event.u.u.detail = detail;
MUST(window.send_event(event, LeaveWindowMask));
}
for (size_t i = 0; i < enter_events; i++)
{
const bool last = (i == enter_events - 1);
const BYTE detail =
[&]() -> BYTE {
if (!linear)
return last ? NotifyNonlinear : NotifyNonlinearVirtual;
if (!last)
return NotifyVirtual;
return (old_child_path.size() > new_child_path.size())
? NotifyInferior
: NotifyAncestor;
}();
const auto& wid = new_child_path[new_child_path.size() - enter_events + i];
auto& object = *g_objects[wid];
ASSERT(object.type == Object::Type::Window);
auto& window = object.object.get<Object::Window>();
const auto [root_x, root_y] = get_window_position(wid);
xEvent event { .u = {
.enterLeave = {
.time = static_cast<CARD32>(time(nullptr)),
.root = g_root.windowId,
.event = wid,
.child = last ? static_cast<WINDOW>(None) : new_child_path.back(),
.rootX = static_cast<INT16>(root_x + new_x),
.rootY = static_cast<INT16>(root_y + new_y),
.eventX = static_cast<INT16>(new_x),
.eventY = static_cast<INT16>(new_y),
.state = static_cast<KeyButMask>(g_keymask | g_butmask),
.mode = NotifyNormal,
.flags = get_flags(wid),
}
}};
event.u.u.type = EnterNotify,
event.u.u.detail = detail;
MUST(window.send_event(event, EnterWindowMask));
}
for (const auto wid : old_child_path)
g_objects[wid]->object.get<Object::Window>().hovered = false;
for (const auto wid : new_child_path)
g_objects[wid]->object.get<Object::Window>().hovered = true;
}
static WINDOW s_current_top_level_window = None;
void on_window_leave_event(WINDOW wid)
{
if (s_current_top_level_window != wid)
return;
auto& object = *g_objects[wid];
ASSERT(object.type == Object::Type::Window);
auto& window = object.object.get<Object::Window>();
send_enter_and_leave_events(wid, window.cursor_x, window.cursor_y, wid, -1, -1);
s_current_top_level_window = None;
}
void on_mouse_move_event(WINDOW wid, int32_t x, int32_t y)
{
auto& object = *g_objects[wid];
ASSERT(object.type == Object::Type::Window);
auto& window = object.object.get<Object::Window>();
update_cursor(wid, x, y);
if (auto it = g_objects.find(s_current_top_level_window); it == g_objects.end())
send_enter_and_leave_events(wid, -1, -1, wid, x, y);
else
{
ASSERT(it->value->type == Object::Type::Window);
auto& old_window = it->value->object.get<Object::Window>();
send_enter_and_leave_events(s_current_top_level_window, old_window.cursor_x, old_window.cursor_y, wid, x, y);
}
s_current_top_level_window = wid;
update_cursor_position_recursive(wid, x, y);
// TODO: optimize with PointerMotionHint
uint32_t event_mask = PointerMotionMask | PointerMotionHintMask;
if (g_butmask) event_mask |= ButtonMotionMask;
if (g_butmask & Button1Mask) event_mask |= Button1MotionMask;
if (g_butmask & Button2Mask) event_mask |= Button2MotionMask;
if (g_butmask & Button3Mask) event_mask |= Button3MotionMask;
if (g_butmask & Button4Mask) event_mask |= Button4MotionMask;
if (g_butmask & Button5Mask) event_mask |= Button5MotionMask;
send_key_button_pointer_event(wid, NotifyNormal, event_mask, MotionNotify, g_keymask | g_butmask);
}
void on_mouse_button_event(WINDOW wid, uint8_t xbutton, bool pressed)
{
uint16_t mask = 0;
switch (xbutton)
{
case Button1: mask = Button1Mask; break;
case Button2: mask = Button2Mask; break;
case Button3: mask = Button3Mask; break;
case Button4: mask = Button4Mask; break;
case Button5: mask = Button5Mask; break;
}
const auto old_state = g_keymask | g_butmask;
if (pressed)
g_butmask |= mask;
else
g_butmask &= ~mask;
send_key_button_pointer_event(
wid,
xbutton,
pressed ? ButtonPressMask : ButtonReleaseMask,
pressed ? ButtonPress : ButtonRelease,
old_state
);
}
void on_key_event(WINDOW wid, uint8_t scancode, uint8_t xmod, bool pressed)
{
const uint8_t xkeycode = scancode + g_keymap_min_keycode;
if (xkeycode < g_keymap_min_keycode || xkeycode > g_keymap_max_keycode)
return;
{
const uint8_t byte = xkeycode / 8;
const uint8_t mask = 1 << (xkeycode % 8);
if (pressed)
g_pressed_keys[byte] |= mask;
else
g_pressed_keys[byte] &= ~mask;
}
const auto old_state = g_keymask | g_butmask;
g_keymask = xmod;
send_key_button_pointer_event(
wid,
xkeycode,
pressed ? KeyPressMask : KeyReleaseMask,
pressed ? KeyPress : KeyRelease,
old_state
);
}
void on_keymap_changed()
{
for (auto& [id, object] : g_objects)
{
if (object->type != Object::Type::Window)
continue;
auto& client_info = object->object.get<Object::Window>().owner;
xEvent event {};
event.u.u.type = MappingNotify;
event.u.u.sequenceNumber = client_info.sequence;
event.u.mappingNotify.request = MappingKeyboard,
event.u.mappingNotify.firstKeyCode = g_keymap_min_keycode,
event.u.mappingNotify.count = g_keymap_size,
MUST(encode(client_info.output_buffer, event));
event.u.mappingNotify.request = MappingModifier;
MUST(encode(client_info.output_buffer, event));
}
}

19
xbanan/Events.h Normal file
View File

@@ -0,0 +1,19 @@
#pragma once
#include "Types.h"
void register_event_fd(int fd, void* data);
void unregister_event_fd(int fd);
void on_window_close_event(WINDOW wid);
void on_window_resize_event(WINDOW wid, uint32_t width, uint32_t height);
void on_window_move_event(WINDOW wid, int32_t x, int32_t y);
void on_window_focus_event(WINDOW wid, bool focused);
void on_window_fullscreen_event(WINDOW wid, bool is_fullscreen);
void on_window_leave_event(WINDOW wid);
void on_mouse_move_event(WINDOW wid, int32_t x, int32_t y);
void on_mouse_button_event(WINDOW wid, uint8_t xbutton, bool pressed);
void on_key_event(WINDOW wid, uint8_t scancode, uint8_t xmod, bool pressed);
void on_keymap_changed();

View File

@@ -1,8 +1,8 @@
#include "Extensions.h" #include "Extensions.h"
#include "Utils.h" #include "Utils.h"
#include <GL/glx.h>
#include <GL/glxproto.h> #include <GL/glxproto.h>
#include <GL/glxtokens.h>
using BOOL32 = CARD32; using BOOL32 = CARD32;
@@ -12,11 +12,11 @@ CARD32 g_fb_configs[2][24][2] {
{ GLX_VISUAL_ID, g_visual.visualID }, { GLX_VISUAL_ID, g_visual.visualID },
{ GLX_BUFFER_SIZE, 32 }, { GLX_BUFFER_SIZE, 32 },
{ GLX_LEVEL, 0 }, { GLX_LEVEL, 0 },
{ GLX_DOUBLEBUFFER, True }, { GLX_DOUBLEBUFFER, xTrue },
{ GLX_STEREO, False }, { GLX_STEREO, xFalse },
{ GLX_RENDER_TYPE, GLX_RGBA_BIT }, { GLX_RENDER_TYPE, GLX_RGBA_BIT },
{ GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT }, { GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT },
{ GLX_X_RENDERABLE, True }, { GLX_X_RENDERABLE, xTrue },
{ GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR }, { GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR },
{ GLX_CONFIG_CAVEAT, GLX_NONE }, { GLX_CONFIG_CAVEAT, GLX_NONE },
{ GLX_TRANSPARENT_TYPE, GLX_NONE }, { GLX_TRANSPARENT_TYPE, GLX_NONE },
@@ -30,19 +30,19 @@ CARD32 g_fb_configs[2][24][2] {
{ GLX_ACCUM_GREEN_SIZE, 0 }, { GLX_ACCUM_GREEN_SIZE, 0 },
{ GLX_ACCUM_BLUE_SIZE, 0 }, { GLX_ACCUM_BLUE_SIZE, 0 },
{ GLX_ACCUM_ALPHA_SIZE, 0 }, { GLX_ACCUM_ALPHA_SIZE, 0 },
{ GLX_SAMPLE_BUFFERS, 0 }, { GLX_SAMPLE_BUFFERS_SGIS, 0 },
{ GLX_SAMPLES, 0 }, { GLX_SAMPLES_SGIS, 0 },
}, },
{ {
{ GLX_FBCONFIG_ID, 2 }, { GLX_FBCONFIG_ID, 2 },
{ GLX_VISUAL_ID, g_visual.visualID }, { GLX_VISUAL_ID, g_visual.visualID },
{ GLX_BUFFER_SIZE, 32 }, { GLX_BUFFER_SIZE, 32 },
{ GLX_LEVEL, 0 }, { GLX_LEVEL, 0 },
{ GLX_DOUBLEBUFFER, False }, { GLX_DOUBLEBUFFER, xFalse },
{ GLX_STEREO, False }, { GLX_STEREO, xFalse },
{ GLX_RENDER_TYPE, GLX_RGBA_BIT }, { GLX_RENDER_TYPE, GLX_RGBA_BIT },
{ GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT }, { GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT },
{ GLX_X_RENDERABLE, True }, { GLX_X_RENDERABLE, xTrue },
{ GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR }, { GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR },
{ GLX_CONFIG_CAVEAT, GLX_NONE }, { GLX_CONFIG_CAVEAT, GLX_NONE },
{ GLX_TRANSPARENT_TYPE, GLX_NONE }, { GLX_TRANSPARENT_TYPE, GLX_NONE },
@@ -56,8 +56,8 @@ CARD32 g_fb_configs[2][24][2] {
{ GLX_ACCUM_GREEN_SIZE, 0 }, { GLX_ACCUM_GREEN_SIZE, 0 },
{ GLX_ACCUM_BLUE_SIZE, 0 }, { GLX_ACCUM_BLUE_SIZE, 0 },
{ GLX_ACCUM_ALPHA_SIZE, 0 }, { GLX_ACCUM_ALPHA_SIZE, 0 },
{ GLX_SAMPLE_BUFFERS, 0 }, { GLX_SAMPLE_BUFFERS_SGIS, 0 },
{ GLX_SAMPLES, 0 }, { GLX_SAMPLES_SGIS, 0 },
}, },
}; };
@@ -337,7 +337,7 @@ BAN::ErrorOr<void> extension_glx(Client& client_info, BAN::ConstByteSpan packet)
const auto& object = g_objects[request.drawable]; const auto& object = g_objects[request.drawable];
ASSERT(object->type == Object::Type::Window); ASSERT(object->type == Object::Type::Window);
const auto& texture = object->object.get<Object::Window>().texture(); const auto& window = object->object.get<Object::Window>();
xGLXGetDrawableAttributesReply reply { xGLXGetDrawableAttributesReply reply {
.type = X_Reply, .type = X_Reply,
@@ -348,16 +348,16 @@ BAN::ErrorOr<void> extension_glx(Client& client_info, BAN::ConstByteSpan packet)
TRY(encode(client_info.output_buffer, reply)); TRY(encode(client_info.output_buffer, reply));
TRY(encode<CARD32>(client_info.output_buffer, GLX_WIDTH)); TRY(encode<CARD32>(client_info.output_buffer, GLX_WIDTH));
TRY(encode<CARD32>(client_info.output_buffer, texture.width())); TRY(encode<CARD32>(client_info.output_buffer, window.width));
TRY(encode<CARD32>(client_info.output_buffer, GLX_HEIGHT)); TRY(encode<CARD32>(client_info.output_buffer, GLX_HEIGHT));
TRY(encode<CARD32>(client_info.output_buffer, texture.height())); TRY(encode<CARD32>(client_info.output_buffer, window.height));
TRY(encode<CARD32>(client_info.output_buffer, GLX_PRESERVED_CONTENTS)); TRY(encode<CARD32>(client_info.output_buffer, GLX_PRESERVED_CONTENTS));
TRY(encode<CARD32>(client_info.output_buffer, xTrue)); TRY(encode<CARD32>(client_info.output_buffer, xTrue));
TRY(encode<CARD32>(client_info.output_buffer, GLX_LARGEST_PBUFFER)); TRY(encode<CARD32>(client_info.output_buffer, GLX_LARGEST_PBUFFER));
TRY(encode<CARD32>(client_info.output_buffer, texture.width() * texture.height())); TRY(encode<CARD32>(client_info.output_buffer, window.width * window.height));
TRY(encode<CARD32>(client_info.output_buffer, GLX_FBCONFIG_ID)); TRY(encode<CARD32>(client_info.output_buffer, GLX_FBCONFIG_ID));
TRY(encode<CARD32>(client_info.output_buffer, 1)); TRY(encode<CARD32>(client_info.output_buffer, 1));

View File

@@ -7,42 +7,13 @@
#include <X11/X.h> #include <X11/X.h>
#include <X11/extensions/shmproto.h> #include <X11/extensions/shmproto.h>
#include <netinet/in.h>
#include <sys/shm.h> #include <sys/shm.h>
#include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
static BYTE s_shm_event_base; static BYTE s_shm_event_base;
static BYTE s_shm_error_base; static BYTE s_shm_error_base;
static BYTE s_shm_major_opcode; static BYTE s_shm_major_opcode;
static bool is_local_socket(int socket)
{
sockaddr_storage addr;
socklen_t addr_len = sizeof(addr);
if (getpeername(socket, reinterpret_cast<sockaddr*>(&addr), &addr_len) == -1)
return false;
switch (addr.ss_family)
{
case AF_UNIX:
return true;
case AF_INET:
{
const auto* addr_in = reinterpret_cast<const sockaddr_in*>(&addr);
const auto ipv4 = ntohl(addr_in->sin_addr.s_addr);
return (ipv4 & IN_CLASSA_NET) == IN_LOOPBACKNET;
}
case AF_INET6:
{
const auto* addr_in6 = reinterpret_cast<const sockaddr_in6*>(&addr);
return IN6_IS_ADDR_LOOPBACK(&addr_in6->sin6_addr);
}
}
return false;
}
static BAN::ErrorOr<void*> get_shmseg(Client& client_info, CARD32 shmseg, BYTE op_major, BYTE op_minor) static BAN::ErrorOr<void*> get_shmseg(Client& client_info, CARD32 shmseg, BYTE op_major, BYTE op_minor)
{ {
auto it = g_objects.find(shmseg); auto it = g_objects.find(shmseg);
@@ -78,7 +49,7 @@ static BAN::ErrorOr<void> extension_shm(Client& client_info, BAN::ConstByteSpan
xShmQueryVersionReply reply { xShmQueryVersionReply reply {
.type = X_Reply, .type = X_Reply,
.sharedPixmaps = is_local_socket(client_info.fd), .sharedPixmaps = false,
.sequenceNumber = client_info.sequence, .sequenceNumber = client_info.sequence,
.length = 0, .length = 0,
.majorVersion = 1, .majorVersion = 1,
@@ -159,7 +130,6 @@ static BAN::ErrorOr<void> extension_shm(Client& client_info, BAN::ConstByteSpan
{ {
auto request = decode<xShmPutImageReq>(packet).value(); auto request = decode<xShmPutImageReq>(packet).value();
#if 0
dprintln("ShmPutImage"); dprintln("ShmPutImage");
dprintln(" drawable: {}", request.drawable); dprintln(" drawable: {}", request.drawable);
dprintln(" gc: {}", request.gc); dprintln(" gc: {}", request.gc);
@@ -176,7 +146,6 @@ static BAN::ErrorOr<void> extension_shm(Client& client_info, BAN::ConstByteSpan
dprintln(" sendEvent: {}", request.sendEvent); dprintln(" sendEvent: {}", request.sendEvent);
dprintln(" shmseg: {}", request.shmseg); dprintln(" shmseg: {}", request.shmseg);
dprintln(" offset: {}", request.offset); dprintln(" offset: {}", request.offset);
#endif
void* shm_segment = TRY(get_shmseg(client_info, request.shmseg, op_major, op_minor)); void* shm_segment = TRY(get_shmseg(client_info, request.shmseg, op_major, op_minor));
@@ -267,40 +236,6 @@ static BAN::ErrorOr<void> extension_shm(Client& client_info, BAN::ConstByteSpan
break; break;
} }
case X_ShmCreatePixmap:
{
auto request = decode<xShmCreatePixmapReq>(packet).value();
dprintln("ShmCreatePixmap");
dprintln(" depth: {}", request.depth);
dprintln(" pid: {}", request.pid);
dprintln(" drawable: {}", request.drawable);
dprintln(" width: {}", request.width);
dprintln(" height: {}", request.height);
dprintln(" shmseg: {}", request.shmseg);
dprintln(" offset: {}", request.offset);
ASSERT(request.depth == 24 || request.depth == 32);
void* shm_segment = TRY(get_shmseg(client_info, request.shmseg, op_major, op_minor));
TRY(client_info.objects.insert(request.pid));
TRY(g_objects.insert(
request.pid,
TRY(BAN::UniqPtr<Object>::create(Object {
.type = Object::Type::Pixmap,
.object = Object::Pixmap {
.depth = request.depth,
.width = request.width,
.height = request.height,
.data = BAN::ByteSpan(static_cast<uint8_t*>(shm_segment) + request.offset, request.width * request.height * 4),
.owned_data = {},
}
}))
));
break;
}
default: default:
dwarnln("unsupported shm minor opcode {}", packet[1]); dwarnln("unsupported shm minor opcode {}", packet[1]);
break; break;

119
xbanan/ExtXRes.cpp Normal file
View File

@@ -0,0 +1,119 @@
#include "Extensions.h"
#include "Utils.h"
#include <X11/X.h>
#include <X11/extensions/XResproto.h>
template<typename F>
static BAN::ErrorOr<void> for_each_client(uint32_t target_spec, const F& callback)
{
for (auto [fd, thingy] : g_pollables)
{
if (thingy.type != Pollable::Type::Client)
continue;
Client& client_info = thingy.value.get<Client>();
if (target_spec && (target_spec >> 20) != client_info.fd)
continue;
TRY(callback(client_info, target_spec ? target_spec : (client_info.fd << 20)));
}
return {};
}
BAN::ErrorOr<void> extension_xres(Client& client_info, BAN::ConstByteSpan packet)
{
const uint8_t major_opcode = packet[0];
const uint8_t minor_opcode = packet[1];
switch (minor_opcode)
{
case X_XResQueryVersion:
{
auto request = decode<xXResQueryVersionReq>(packet).value();
dprintln("XResQueryVersion");
dprintln(" clientMajor: {}", request.client_major);
dprintln(" clientMinor: {}", request.client_minor);
xXResQueryVersionReply reply {
.type = X_Reply,
.sequenceNumber = client_info.sequence,
.length = 0,
.server_major = 1,
.server_minor = 2,
};
TRY(encode(client_info.output_buffer, reply));
break;
}
case X_XResQueryClientIds:
{
auto request = decode<xXResQueryClientIdsReq>(packet).value();
dprintln("XResQueryClientIds");
dprintln(" numSpecs: {}", request.numSpecs);
uint32_t num_ids { 0 };
BAN::Vector<uint8_t> query_result;
for (size_t i = 0; i < request.numSpecs; i++)
{
auto spec = decode<xXResClientIdSpec>(packet).value();
TRY(for_each_client(spec.client, [&](Client& client_info, uint32_t client_spec) -> BAN::ErrorOr<void> {
if (spec.mask == None || (spec.mask & X_XResClientXIDMask))
{
xXResClientIdValue value {
.spec = {
.client = client_spec,
.mask = X_XResClientXIDMask,
},
.length = 0,
};
TRY(encode(query_result, value));
num_ids++;
}
if ((spec.mask == None || (spec.mask & X_XResLocalClientPIDMask)) && client_info.pid.has_value())
{
xXResClientIdValue value {
.spec = {
.client = client_spec,
.mask = X_XResLocalClientPIDMask,
},
.length = 4,
};
TRY(encode(query_result, value));
TRY(encode(query_result, client_info.pid.value()));
num_ids++;
}
return {};
}));
}
xXResQueryClientIdsReply reply {
.type = X_Reply,
.sequenceNumber = client_info.sequence,
.length = static_cast<uint32_t>(query_result.size() / 4),
.numIds = num_ids,
};
TRY(encode(client_info.output_buffer, reply));
TRY(encode(client_info.output_buffer, query_result));
break;
}
default:
dwarnln("unsupported opcode XRes minor opcode {}", minor_opcode);
break;
}
return {};
}
static struct XResInstaller
{
XResInstaller()
{
install_extension(XRES_NAME, 6, 0, extension_xres);
}
} installer;

View File

@@ -385,13 +385,15 @@ static BAN::ErrorOr<BAN::RefPtr<PCFFont>> parse_font(const BAN::String& path)
font->font_ascent = font->max_bounds.ascent; font->font_ascent = font->max_bounds.ascent;
font->font_descent = font->max_bounds.descent; font->font_descent = font->max_bounds.descent;
font->is_cursor_font = (path == FONT_PATH "/misc/cursor.pcf.gz"_sv);
return font; return font;
} }
__attribute__((constructor)) __attribute__((constructor))
static void initialize_fonts() static void initialize_fonts()
{ {
const char* font_path = "fonts/misc"; const char* font_path = FONT_PATH "/misc";
do do
{ {
@@ -784,7 +786,7 @@ static void write_text(WriteTextInfo& info)
? info.gc.foreground ? info.gc.foreground
: info.gc.background; : info.gc.background;
if (color != LibGUI::Texture::color_invisible) if (color != COLOR_INVISIBLE)
info.out_data_u32[out_y * info.out_w + out_x] = color; info.out_data_u32[out_y * info.out_w + out_x] = color;
} }
} }
@@ -938,6 +940,49 @@ BAN::ErrorOr<void> create_glyph_cursor(Client& client_info, BAN::ConstByteSpan p
const auto& source_font = TRY(get_fontable(client_info, request.source, X_CreateGlyphCursor)); const auto& source_font = TRY(get_fontable(client_info, request.source, X_CreateGlyphCursor));
// Try to use system cursor for known cursors
if (source_font->is_cursor_font && request.mask == request.source && request.sourceChar + 1 == request.maskChar && g_platform_ops.create_system_cursor)
{
BAN::Optional<SystemCursorType> type;
switch (request.sourceChar)
{
case 68: type = SystemCursorType::Pointer; break;
case 152: type = SystemCursorType::Text; break;
case 150: type = SystemCursorType::Wait; break;
case 60: type = SystemCursorType::Hand; break;
case 92: type = SystemCursorType::Help; break;
case 52: type = SystemCursorType::Move; break;
case 88: type = SystemCursorType::Forbidden; break;
case 138: type = SystemCursorType::ResizeN; break;
case 96: type = SystemCursorType::ResizeE; break;
case 16: type = SystemCursorType::ResizeS; break;
case 70: type = SystemCursorType::ResizeW; break;
case 134: type = SystemCursorType::ResizeNW; break;
case 136: type = SystemCursorType::ResizeNE; break;
case 12: type = SystemCursorType::ResizeSW; break;
case 14: type = SystemCursorType::ResizeSE; break;
case 108: type = SystemCursorType::ResizeHorizontal; break;
case 116: type = SystemCursorType::ResizeVertical; break;
}
if (type.has_value())
{
auto cursor_or_error = g_platform_ops.create_system_cursor(type.value());
if (!cursor_or_error.is_error())
{
TRY(client_info.objects.insert(request.cid));
TRY(g_objects.insert(
request.cid,
TRY(BAN::UniqPtr<Object>::create(Object {
.type = Object::Type::Cursor,
.object = BAN::move(cursor_or_error.value()),
}))
));
return {};
}
}
}
auto source_glyph_index = source_font->find_glyph(request.sourceChar); auto source_glyph_index = source_font->find_glyph(request.sourceChar);
if (!source_glyph_index.has_value()) if (!source_glyph_index.has_value())
{ {
@@ -958,19 +1003,14 @@ BAN::ErrorOr<void> create_glyph_cursor(Client& client_info, BAN::ConstByteSpan p
const uint32_t source_width = source_ci.rightSideBearing - source_ci.leftSideBearing; const uint32_t source_width = source_ci.rightSideBearing - source_ci.leftSideBearing;
const uint32_t source_height = source_ci.ascent + source_ci.descent; const uint32_t source_height = source_ci.ascent + source_ci.descent;
Object::Cursor cursor { BAN::Vector<uint32_t> pixels;
.width = source_width, TRY(pixels.resize(source_width * source_height));
.height = source_height,
.origin_x = -source_ci.leftSideBearing,
.origin_y = source_ci.ascent,
};
TRY(cursor.pixels.resize(cursor.width * cursor.height));
for (uint32_t y = 0; y < source_height; y++) for (uint32_t y = 0; y < source_height; y++)
{ {
const uint8_t* row_base = source_font->bitmap.data() + source_glyph.bitmap_offset + (source_width + 7) / 8 * y; const uint8_t* row_base = source_font->bitmap.data() + source_glyph.bitmap_offset + (source_width + 7) / 8 * y;
for (uint32_t x = 0; x < source_width; x++) for (uint32_t x = 0; x < source_width; x++)
cursor.pixels[y * source_width + x] = 0xFF000000 | ((row_base[x / 8] & (1 << (x % 8))) ? foreground : background); pixels[y * source_width + x] = 0xFF000000 | ((row_base[x / 8] & (1 << (x % 8))) ? foreground : background);
} }
if (request.mask != None) if (request.mask != None)
@@ -1008,11 +1048,19 @@ BAN::ErrorOr<void> create_glyph_cursor(Client& client_info, BAN::ConstByteSpan p
const uint8_t* row_base = mask_font->bitmap.data() + mask_glyph.bitmap_offset + (mask_width + 7) / 8 * mask_y; const uint8_t* row_base = mask_font->bitmap.data() + mask_glyph.bitmap_offset + (mask_width + 7) / 8 * mask_y;
if (!(row_base[mask_x / 8] & (1 << (mask_x % 8)))) if (!(row_base[mask_x / 8] & (1 << (mask_x % 8))))
cursor.pixels[src_y * source_width + src_x] = 0; pixels[src_y * source_width + src_x] = 0;
} }
} }
} }
BAN::UniqPtr<PlatformCursor> cursor;
if (g_platform_ops.create_bitmap_cursor)
{
auto cursor_or_error = g_platform_ops.create_bitmap_cursor(pixels.data(), source_width, source_height, -source_ci.leftSideBearing, source_ci.ascent);
if (!cursor_or_error.is_error())
cursor = cursor_or_error.release_value();
}
TRY(client_info.objects.insert(request.cid)); TRY(client_info.objects.insert(request.cid));
TRY(g_objects.insert( TRY(g_objects.insert(
request.cid, request.cid,

View File

@@ -40,6 +40,8 @@ struct PCFFont : public BAN::RefCounted<PCFFont>, public BAN::Weakable<PCFFont>
BAN::Vector<MapEntry> map; BAN::Vector<MapEntry> map;
BAN::Vector<uint8_t> bitmap; BAN::Vector<uint8_t> bitmap;
bool is_cursor_font;
BAN::Optional<uint16_t> find_glyph(uint16_t codepoint) const BAN::Optional<uint16_t> find_glyph(uint16_t codepoint) const
{ {
size_t l = 0; size_t l = 0;

View File

@@ -60,8 +60,33 @@ void put_image(const PutImageInfo& info)
} }
case ZPixmap: case ZPixmap:
{ {
bool needs_clipping = true;
if (info.gc.clip_mask == None)
{
needs_clipping = false;
}
else if (info.gc.clip_mask == UINT32_MAX)
{
const uint32_t clip_min_x = min_x - info.gc.clip_origin_x;
const uint32_t clip_min_y = min_y - info.gc.clip_origin_y;
const uint32_t clip_max_x = max_x - info.gc.clip_origin_x;
const uint32_t clip_max_y = max_y - info.gc.clip_origin_y;
for (const auto& rect : info.gc.clip_rects)
{
if (clip_min_x < rect.x || clip_max_x > rect.x + rect.width)
continue;
if (clip_min_y < rect.y || clip_max_y > rect.y + rect.height)
continue;
needs_clipping = false;
break;
}
}
ASSERT(info.left_pad == 0); ASSERT(info.left_pad == 0);
if (in_bpp == 32 && info.gc.clip_mask == None) if (in_bpp == 32 && !needs_clipping)
{ {
const auto bytes_per_row = (max_x - min_x) * 4; const auto bytes_per_row = (max_x - min_x) * 4;
for (int32_t y = min_y; y < max_y; y++) for (int32_t y = min_y; y < max_y; y++)

View File

@@ -1,347 +1,5 @@
#include "Keymap.h" #include "Keymap.h"
#include <BAN/StringView.h> uint32_t g_keymap[g_keymap_size][g_keymap_layers] {};
uint8_t g_modifier_map[8][2] {};
#include <LibInput/KeyboardLayout.h> uint8_t g_pressed_keys[32] {};
#include <X11/X.h>
#include <X11/keysym.h>
#include <X11/XF86keysym.h>
#include <ctype.h>
#undef None
uint32_t g_keymap[0x100][g_keymap_layers];
static constexpr uint32_t my_key_to_x_keysym(LibInput::Key key)
{
using LibInput::Key;
switch (key)
{
case Key::A:
return XK_A;
case Key::B:
return XK_B;
case Key::C:
return XK_C;
case Key::D:
return XK_D;
case Key::E:
return XK_E;
case Key::F:
return XK_F;
case Key::G:
return XK_G;
case Key::H:
return XK_H;
case Key::I:
return XK_I;
case Key::J:
return XK_J;
case Key::K:
return XK_K;
case Key::L:
return XK_L;
case Key::M:
return XK_M;
case Key::N:
return XK_N;
case Key::O:
return XK_O;
case Key::P:
return XK_P;
case Key::Q:
return XK_Q;
case Key::R:
return XK_R;
case Key::S:
return XK_S;
case Key::T:
return XK_T;
case Key::U:
return XK_U;
case Key::V:
return XK_V;
case Key::W:
return XK_W;
case Key::X:
return XK_X;
case Key::Y:
return XK_Y;
case Key::Z:
return XK_Z;
case Key::A_Ring:
return XK_Aring;
case Key::A_Umlaut:
return XK_Adiaeresis;
case Key::O_Umlaut:
return XK_Odiaeresis;
case Key::_0:
return XK_0;
case Key::_1:
return XK_1;
case Key::_2:
return XK_2;
case Key::_3:
return XK_3;
case Key::_4:
return XK_4;
case Key::_5:
return XK_5;
case Key::_6:
return XK_6;
case Key::_7:
return XK_7;
case Key::_8:
return XK_8;
case Key::_9:
return XK_9;
case Key::F1:
return XK_F1;
case Key::F2:
return XK_F2;
case Key::F3:
return XK_F3;
case Key::F4:
return XK_F4;
case Key::F5:
return XK_F5;
case Key::F6:
return XK_F6;
case Key::F7:
return XK_F7;
case Key::F8:
return XK_F8;
case Key::F9:
return XK_F9;
case Key::F10:
return XK_F10;
case Key::F11:
return XK_F11;
case Key::F12:
return XK_F12;
case Key::Insert:
return XK_Insert;
case Key::PrintScreen:
return XK_Print;
case Key::Delete:
return XK_Delete;
case Key::Home:
return XK_Home;
case Key::End:
return XK_End;
case Key::PageUp:
return XK_Page_Up;
case Key::PageDown:
return XK_Page_Down;
case Key::Enter:
return XK_Return;
case Key::Space:
return XK_space;
case Key::ExclamationMark:
return XK_exclam;
case Key::DoubleQuote:
return XK_quotedbl;
case Key::Hashtag:
return XK_numbersign;
case Key::Currency:
return XK_currency;
case Key::Percent:
return XK_percent;
case Key::Ampersand:
return XK_ampersand;
case Key::Slash:
return XK_slash;
case Key::Section:
return XK_section;
case Key::Half:
return XK_onehalf;
case Key::OpenParenthesis:
return '(';
case Key::CloseParenthesis:
return ')';
case Key::OpenSquareBracket:
return '[';
case Key::CloseSquareBracket:
return ']';
case Key::OpenCurlyBracket:
return '{';
case Key::CloseCurlyBracket:
return '}';
case Key::Equals:
return '=';
case Key::QuestionMark:
return '?';
case Key::Plus:
return '+';
case Key::BackSlash:
return '\\';
case Key::Acute:
return XK_acute;
case Key::BackTick:
return '`';
case Key::TwoDots:
return XK_diaeresis;
case Key::Cedilla:
return XK_Ccedilla;
case Key::Backspace:
return XK_BackSpace;
case Key::AtSign:
return XK_at;
case Key::Pound:
return XK_sterling;
case Key::Dollar:
return XK_dollar;
case Key::Euro:
return XK_EuroSign;
case Key::Escape:
return XK_Escape;
case Key::Tab:
return XK_Tab;
case Key::CapsLock:
return XK_Caps_Lock;
case Key::LeftShift:
return XK_Shift_L;
case Key::LeftCtrl:
return XK_Control_L;
case Key::Super:
return XK_Super_L;
case Key::LeftAlt:
return XK_Alt_L;
case Key::RightAlt:
return XK_Alt_R;
case Key::RightCtrl:
return XK_Control_R;
case Key::RightShift:
return XK_Shift_R;
case Key::SingleQuote:
return '\'';
case Key::Asterix:
return '*';
case Key::Caret:
return '^';
case Key::Tilde:
return '~';
case Key::ArrowUp:
return XK_Up;
case Key::ArrowDown:
return XK_Down;
case Key::ArrowLeft:
return XK_Left;
case Key::ArrowRight:
return XK_Right;
case Key::Comma:
return ',';
case Key::Semicolon:
return ';';
case Key::Period:
return '.';
case Key::Colon:
return ':';
case Key::Hyphen:
return '-';
case Key::Underscore:
return '_';
case Key::NumLock:
return XK_Num_Lock;
case Key::ScrollLock:
return XK_Scroll_Lock;
case Key::LessThan:
return '<';
case Key::GreaterThan:
return '>';
case Key::Pipe:
return '|';
case Key::Negation:
return XK_notsign;
case Key::BrokenBar:
return XK_brokenbar;
case Key::Numpad0:
return XK_KP_0;
case Key::Numpad1:
return XK_KP_1;
case Key::Numpad2:
return XK_KP_2;
case Key::Numpad3:
return XK_KP_3;
case Key::Numpad4:
return XK_KP_4;
case Key::Numpad5:
return XK_KP_5;
case Key::Numpad6:
return XK_KP_6;
case Key::Numpad7:
return XK_KP_7;
case Key::Numpad8:
return XK_KP_8;
case Key::Numpad9:
return XK_KP_9;
case Key::NumpadPlus:
return XK_KP_Add;
case Key::NumpadMinus:
return XK_KP_Subtract;
case Key::NumpadMultiply:
return XK_KP_Multiply;
case Key::NumpadDivide:
return XK_KP_Divide;
case Key::NumpadEnter:
return XK_KP_Enter;
case Key::NumpadDecimal:
return XK_KP_Decimal;
case Key::VolumeMute:
return XF86XK_AudioMute;
case Key::VolumeUp:
return XF86XK_AudioRaiseVolume;
case Key::VolumeDown:
return XF86XK_AudioLowerVolume;
case Key::Calculator:
return XF86XK_Calculator;
case Key::MediaPlayPause:
return XF86XK_AudioPlay;
case Key::MediaStop:
return XF86XK_AudioStop;
case Key::MediaPrevious:
return XF86XK_AudioPrev;
case Key::MediaNext:
return XF86XK_AudioNext;
case Key::Invalid:
case Key::None:
case Key::Count:
break;
}
return NoSymbol;
}
BAN::ErrorOr<void> initialize_keymap()
{
for (auto& keycode : g_keymap)
for (auto& keysym : keycode)
keysym = NoSymbol;
// FIXME: get this from somewhere (gui command? enviroment? tmp file?)
const auto keymap_path = "./us.keymap"_sv;
TRY(LibInput::KeyboardLayout::initialize());
auto& keyboard_layout = LibInput::KeyboardLayout::get();
TRY(keyboard_layout.load_from_file(keymap_path));
const BAN::Span<const LibInput::Key> my_keymaps[] {
keyboard_layout.keymap_normal(),
keyboard_layout.keymap_shift(),
keyboard_layout.keymap_altgr(),
keyboard_layout.keymap_altgr(), // add shift+altgr map?
};
for (size_t keycode = g_keymap_min_keycode; keycode < g_keymap_max_keycode; keycode++)
for (size_t layer = 0; layer < g_keymap_layers; layer++)
if (auto my_key = my_keymaps[layer][keycode - g_keymap_min_keycode]; my_key != LibInput::Key::None)
if (auto keysym = my_key_to_x_keysym(my_key); keysym != NoSymbol)
g_keymap[keycode][layer] = (layer % 2) ? toupper(keysym) : tolower(keysym);
return {};
}

View File

@@ -1,13 +1,17 @@
#pragma once #pragma once
#include <BAN/Errors.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
BAN::ErrorOr<void> initialize_keymap();
constexpr size_t g_keymap_min_keycode = 8; constexpr size_t g_keymap_min_keycode = 8;
constexpr size_t g_keymap_max_keycode = 255; constexpr size_t g_keymap_max_keycode = 255;
constexpr size_t g_keymap_size = g_keymap_max_keycode - g_keymap_min_keycode + 1;
constexpr size_t g_keymap_layers = 4; constexpr size_t g_keymap_layers = 4;
extern uint32_t g_keymap[0x100][g_keymap_layers]; extern uint32_t g_keymap[g_keymap_size][g_keymap_layers];
// shift, capslock, control, alt, numlock, level5 shift, super, altgr with 2 keycodes per modifier
constexpr size_t g_modifier_layers = 2;
extern uint8_t g_modifier_map[8][g_modifier_layers];
extern uint8_t g_pressed_keys[32];

76
xbanan/Platform.h Normal file
View File

@@ -0,0 +1,76 @@
#pragma once
#include "Types.h"
#include <BAN/UniqPtr.h>
struct PlatformWindow
{
virtual ~PlatformWindow() = default;
};
struct PlatformCursor
{
virtual ~PlatformCursor() = default;
};
enum class WindowType
{
Normal,
Utility,
Popup,
};
enum class SystemCursorType
{
Pointer,
Text,
Wait,
Hand,
Help,
Move,
Forbidden,
ResizeN,
ResizeE,
ResizeS,
ResizeW,
ResizeNW,
ResizeNE,
ResizeSW,
ResizeSE,
ResizeVertical,
ResizeHorizontal,
};
// initialize, create_window and invalidate are required
// poll_events is required when an event fd is registered
struct PlatformOps
{
/* Do platform initialization */
bool (*initialize)(uint32_t* width, uint32_t* height);
/* Handle pending events */
void (*poll_events)(void*);
/* Create a window with given size */
BAN::ErrorOr<BAN::UniqPtr<PlatformWindow>> (*create_window)(PlatformWindow* parent, WindowType, WINDOW wid, int32_t x, int32_t y, uint32_t width, uint32_t height);
/* Invaldate part of a window */
void (*invalidate)(PlatformWindow*, const uint32_t* pixels, uint32_t x, uint32_t y, uint32_t width, uint32_t height);
/* Request resize of a window, can be async */
void (*request_resize)(PlatformWindow*, uint32_t width, uint32_t height);
/* Request window repositioning */
void (*request_reposition)(PlatformWindow*, int32_t x, int32_t y);
/* Request new fullscreen state, can be async */
void (*request_fullscreen)(PlatformWindow*, bool fullscreen);
/* Warp pointer */
void (*warp_pointer)(int32_t x, int32_t y, bool relative);
/* Get global position of the pointer */
void (*query_pointer)(int32_t* x, int32_t* y);
/* Grab pointer events on window */
void (*set_pointer_grab)(PlatformWindow*, bool grabbed);
/* Create a system cursor */
BAN::ErrorOr<BAN::UniqPtr<PlatformCursor>> (*create_system_cursor)(SystemCursorType);
/* Create cursor from custom bitmap */
BAN::ErrorOr<BAN::UniqPtr<PlatformCursor>> (*create_bitmap_cursor)(const uint32_t* pixels, uint32_t width, uint32_t height, int32_t origin_x, int32_t origin_y);
/* Set custom cursor */
void (*set_cursor)(PlatformWindow*, PlatformCursor*);
};
extern PlatformOps g_platform_ops;

703
xbanan/SDL3/sdl3.cpp Normal file
View File

@@ -0,0 +1,703 @@
#include "../Events.h"
#include "../Keymap.h"
#include "../Platform.h"
#include <BAN/Atomic.h>
#include <BAN/HashMap.h>
#include <SDL3/SDL.h>
#include <pthread.h>
#include <sys/eventfd.h>
#include <unistd.h>
BAN::HashMap<uint32_t, struct SDLWindow*> s_window_map;
struct SDLWindow final : public PlatformWindow
{
~SDLWindow()
{
if (texture != nullptr)
SDL_DestroyTexture(texture);
if (renderer != nullptr)
SDL_DestroyRenderer(renderer);
if (window != nullptr)
{
s_window_map.remove(SDL_GetWindowID(window));
SDL_DestroyWindow(window);
}
}
WINDOW wid;
uint32_t width;
uint32_t height;
SDL_Window* window { nullptr };
SDL_Renderer* renderer { nullptr };
SDL_Texture* texture { nullptr };
};
struct SDLCursor final : public PlatformCursor
{
~SDLCursor()
{
if (cursor != nullptr)
SDL_DestroyCursor(cursor);
}
SDL_Cursor* cursor { nullptr };
};
static int s_eventfd { -1 };
static SDL_Cursor* s_default_cursor { nullptr };
static void* sdl3_thread(void*)
{
for (;;)
{
const uint64_t value { 1 };
write(s_eventfd, &value, sizeof(value));
usleep(16'666);
}
return nullptr;
}
static void sdl3_initialize_keymap();
static bool sdl3_initialize(uint32_t* display_w, uint32_t* display_h)
{
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS))
{
dwarnln("Could not initialize SDL: {}", SDL_GetError());
return false;
}
*display_w = *display_h = 0;
const SDL_DisplayID* display_ids = SDL_GetDisplays(nullptr);
for (int i = 0; display_ids[i]; i++)
{
SDL_Rect rect;
SDL_GetDisplayBounds(display_ids[i], &rect);
*display_w = BAN::Math::max<uint32_t>(*display_w, rect.x + rect.w);
*display_h = BAN::Math::max<uint32_t>(*display_h, rect.y + rect.h);
}
sdl3_initialize_keymap();
s_default_cursor = SDL_GetCursor();
s_eventfd = eventfd(0, 0);
if (s_eventfd == -1)
{
dwarnln("Could not create eventfd: {}", strerror(errno));
return false;
}
register_event_fd(s_eventfd, nullptr);
pthread_t thread;
pthread_create(&thread, nullptr, sdl3_thread, nullptr);
return true;
}
static BAN::ErrorOr<BAN::UniqPtr<PlatformWindow>> sdl3_create_window(PlatformWindow* parent, WindowType type, WINDOW wid, int32_t x, int32_t y, uint32_t width, uint32_t height)
{
auto window = TRY(BAN::UniqPtr<SDLWindow>::create());
window->wid = wid;
window->width = width;
window->height = height;
int flags;
switch (type)
{
case WindowType::Normal:
case WindowType::Utility:
flags = SDL_WINDOW_RESIZABLE;
break;
case WindowType::Popup:
flags = SDL_WINDOW_BORDERLESS;
break;
}
if (parent == nullptr || type != WindowType::Popup)
{
if (type != WindowType::Normal)
flags |= SDL_WINDOW_UTILITY;
window->window = SDL_CreateWindow("", width, height, flags);
}
else
{
auto& sdl_parent = *static_cast<SDLWindow*>(parent);
int parent_x, parent_y;
SDL_GetWindowPosition(sdl_parent.window, &parent_x, &parent_y);
window->window = SDL_CreatePopupWindow(sdl_parent.window, x - parent_x, y - parent_y, width, height, flags | SDL_WINDOW_POPUP_MENU);
}
if (window->window == nullptr)
{
dwarnln("Could not create SDL window: {}", SDL_GetError());
return BAN::Error::from_errno(EFAULT);
}
window->renderer = SDL_CreateRenderer(window->window, nullptr);
if (window->renderer == nullptr)
{
dwarnln("Could not create SDL renderer: {}", SDL_GetError());
return BAN::Error::from_errno(EFAULT);
}
window->texture = SDL_CreateTexture(window->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height);
if (window->texture == nullptr)
{
dwarnln("Could not create SDL texture: {}", SDL_GetError());
return BAN::Error::from_errno(EFAULT);
}
TRY(s_window_map.insert(SDL_GetWindowID(window->window), window.ptr()));
return BAN::UniqPtr<PlatformWindow>(BAN::move(window));
}
static void sdl3_request_resize(PlatformWindow* window, uint32_t width, uint32_t height)
{
auto& sdl_window = *static_cast<SDLWindow*>(window);
SDL_SetWindowSize(sdl_window.window, width, height);
}
static void sdl3_request_reposition(PlatformWindow* window, int32_t x, int32_t y)
{
auto& sdl_window = *static_cast<SDLWindow*>(window);
SDL_SetWindowPosition(sdl_window.window, x, y);
}
static void sdl3_poll_events(void*)
{
uint64_t dummy;
read(s_eventfd, &dummy, sizeof(dummy));
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
if (auto it = s_window_map.find(event.window.windowID); it != s_window_map.end())
on_window_close_event(it->value->wid);
break;
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
if (auto it = s_window_map.find(event.window.windowID); it != s_window_map.end())
{
auto& window = *it->value;
window.width = event.window.data1;
window.height = event.window.data2;
SDL_DestroyTexture(window.texture);
window.texture = SDL_CreateTexture(window.renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, window.width, window.height);
ASSERT(window.texture);
on_window_resize_event(window.wid, window.width, window.height);
}
break;
case SDL_EVENT_WINDOW_MOVED:
if (auto it = s_window_map.find(event.motion.windowID); it != s_window_map.end())
on_window_move_event(it->value->wid, event.window.data1, event.window.data2);
break;
case SDL_EVENT_WINDOW_FOCUS_GAINED:
case SDL_EVENT_WINDOW_FOCUS_LOST:
if (auto it = s_window_map.find(event.motion.windowID); it != s_window_map.end())
on_window_focus_event(it->value->wid, (event.type == SDL_EVENT_WINDOW_FOCUS_GAINED));
break;
case SDL_EVENT_WINDOW_ENTER_FULLSCREEN:
case SDL_EVENT_WINDOW_LEAVE_FULLSCREEN:
if (auto it = s_window_map.find(event.motion.windowID); it != s_window_map.end())
on_window_fullscreen_event(it->value->wid, (event.type == SDL_EVENT_WINDOW_ENTER_FULLSCREEN));
break;
case SDL_EVENT_WINDOW_MOUSE_LEAVE:
if (auto it = s_window_map.find(event.motion.windowID); it != s_window_map.end())
on_window_leave_event(it->value->wid);
break;
case SDL_EVENT_MOUSE_MOTION:
if (auto it = s_window_map.find(event.motion.windowID); it != s_window_map.end())
on_mouse_move_event(it->value->wid, event.motion.x, event.motion.y);
break;
case SDL_EVENT_MOUSE_BUTTON_UP:
case SDL_EVENT_MOUSE_BUTTON_DOWN:
{
uint8_t xbutton { 0 };
switch (event.button.button)
{
case SDL_BUTTON_LEFT: xbutton = 1; break;
case SDL_BUTTON_MIDDLE: xbutton = 2; break;
case SDL_BUTTON_RIGHT: xbutton = 3; break;
case SDL_BUTTON_X1: xbutton = 8; break;
case SDL_BUTTON_X2: xbutton = 9; break;
}
auto it = s_window_map.find(event.window.windowID);
if (xbutton && it != s_window_map.end())
on_mouse_button_event(it->value->wid, xbutton, (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN));
break;
}
case SDL_EVENT_MOUSE_WHEEL:
{
uint8_t xbutton { 0 };
if (event.wheel.y > 0)
xbutton = 4;
if (event.wheel.y < 0)
xbutton = 5;
auto it = s_window_map.find(event.window.windowID);
if (xbutton && it != s_window_map.end())
{
on_mouse_button_event(it->value->wid, xbutton, true);
on_mouse_button_event(it->value->wid, xbutton, false);
}
break;
}
case SDL_EVENT_KEY_UP:
case SDL_EVENT_KEY_DOWN:
{
const uint8_t xmod =
((event.key.mod & SDL_KMOD_SHIFT) ? (1 << 0) : 0) |
((event.key.mod & SDL_KMOD_CAPS) ? (1 << 1) : 0) |
((event.key.mod & SDL_KMOD_CTRL) ? (1 << 2) : 0) |
((event.key.mod & SDL_KMOD_ALT) ? (1 << 3) : 0) |
((event.key.mod & SDL_KMOD_NUM) ? (1 << 4) : 0) |
((event.key.mod & SDL_KMOD_LEVEL5) ? (1 << 5) : 0) |
((event.key.mod & SDL_KMOD_GUI) ? (1 << 6) : 0) |
((event.key.mod & SDL_KMOD_MODE) ? (1 << 7) : 0);
auto it = s_window_map.find(event.window.windowID);
if (it != s_window_map.end())
on_key_event(it->value->wid, event.key.scancode, xmod, (event.type == SDL_EVENT_KEY_DOWN));
break;
}
case SDL_EVENT_KEYMAP_CHANGED:
sdl3_initialize_keymap();
on_keymap_changed();
break;
}
}
}
static void sdl3_invalidate(PlatformWindow* window, const uint32_t* src_pixels, uint32_t x, uint32_t y, uint32_t width, uint32_t height)
{
auto& sdl_window = *static_cast<SDLWindow*>(window);
ASSERT(x < sdl_window.width && width <= sdl_window.width - x);
ASSERT(y < sdl_window.height && height <= sdl_window.height - y);
const SDL_Rect rect {
.x = static_cast<int>(x),
.y = static_cast<int>(y),
.w = static_cast<int>(width),
.h = static_cast<int>(height),
};
void* dst_pixels;
int dst_pitch;
if (!SDL_LockTexture(sdl_window.texture, &rect, &dst_pixels, &dst_pitch))
{
dwarnln("Could not lock texture: {}", SDL_GetError());
return;
}
for (int32_t y_off = 0; y_off < rect.h; y_off++)
{
memcpy(
static_cast<uint8_t*>(dst_pixels) + y_off * dst_pitch,
&src_pixels[(rect.y + y_off) * sdl_window.width + rect.x],
rect.w * sizeof(uint32_t)
);
}
SDL_UnlockTexture(sdl_window.texture);
SDL_RenderClear(sdl_window.renderer);
SDL_RenderTexture(sdl_window.renderer, sdl_window.texture, NULL, NULL);
SDL_RenderPresent(sdl_window.renderer);
}
static void sdl3_request_fullscreen(PlatformWindow* window, bool fullscreen)
{
auto& sdl_window = *static_cast<SDLWindow*>(window);
SDL_SetWindowFullscreen(sdl_window.window, fullscreen);
}
static void sdl3_warp_pointer(int32_t x, int32_t y, bool relative)
{
if (relative)
{
float cx, cy;
SDL_GetGlobalMouseState(&cx, &cy);
x += cx;
x += cy;
}
SDL_WarpMouseGlobal(x, y);
}
static void sdl3_query_pointer(int32_t* x, int32_t* y)
{
float cx, cy;
SDL_GetGlobalMouseState(&cx, &cy);
*x = cx;
*y = cy;
}
static void sdl3_set_pointer_grab(PlatformWindow* window, bool grabbed)
{
auto& sdl_window = *static_cast<SDLWindow*>(window);
SDL_SetWindowMouseGrab(sdl_window.window, grabbed);
}
static BAN::ErrorOr<BAN::UniqPtr<PlatformCursor>> sdl3_create_system_cursor(SystemCursorType type)
{
static constexpr SDL_SystemCursor cursor_type_map[] {
[static_cast<size_t>(SystemCursorType::Pointer)] = SDL_SYSTEM_CURSOR_DEFAULT,
[static_cast<size_t>(SystemCursorType::Text)] = SDL_SYSTEM_CURSOR_TEXT,
[static_cast<size_t>(SystemCursorType::Wait)] = SDL_SYSTEM_CURSOR_WAIT,
[static_cast<size_t>(SystemCursorType::Hand)] = SDL_SYSTEM_CURSOR_POINTER,
[static_cast<size_t>(SystemCursorType::Help)] = SDL_SYSTEM_CURSOR_DEFAULT, // :(
[static_cast<size_t>(SystemCursorType::Move)] = SDL_SYSTEM_CURSOR_MOVE,
[static_cast<size_t>(SystemCursorType::Forbidden)] = SDL_SYSTEM_CURSOR_NOT_ALLOWED,
[static_cast<size_t>(SystemCursorType::ResizeN)] = SDL_SYSTEM_CURSOR_N_RESIZE,
[static_cast<size_t>(SystemCursorType::ResizeE)] = SDL_SYSTEM_CURSOR_E_RESIZE,
[static_cast<size_t>(SystemCursorType::ResizeS)] = SDL_SYSTEM_CURSOR_S_RESIZE,
[static_cast<size_t>(SystemCursorType::ResizeW)] = SDL_SYSTEM_CURSOR_W_RESIZE,
[static_cast<size_t>(SystemCursorType::ResizeNW)] = SDL_SYSTEM_CURSOR_NW_RESIZE,
[static_cast<size_t>(SystemCursorType::ResizeNE)] = SDL_SYSTEM_CURSOR_NE_RESIZE,
[static_cast<size_t>(SystemCursorType::ResizeSW)] = SDL_SYSTEM_CURSOR_SW_RESIZE,
[static_cast<size_t>(SystemCursorType::ResizeSE)] = SDL_SYSTEM_CURSOR_SE_RESIZE,
[static_cast<size_t>(SystemCursorType::ResizeVertical)] = SDL_SYSTEM_CURSOR_NS_RESIZE,
[static_cast<size_t>(SystemCursorType::ResizeHorizontal)] = SDL_SYSTEM_CURSOR_EW_RESIZE,
};
auto cursor = TRY(BAN::UniqPtr<SDLCursor>::create());
cursor->cursor = SDL_CreateSystemCursor(cursor_type_map[static_cast<size_t>(type)]);
if (cursor->cursor == nullptr)
{
dwarnln("Could not create SDL system cursor: {}", SDL_GetError());
return BAN::Error::from_errno(EFAULT);
}
return BAN::UniqPtr<PlatformCursor>(BAN::move(cursor));
}
static BAN::ErrorOr<BAN::UniqPtr<PlatformCursor>> sdl3_create_bitmap_cursor(const uint32_t* pixels, uint32_t width, uint32_t height, int32_t origin_x, int32_t origin_y)
{
auto cursor = TRY(BAN::UniqPtr<SDLCursor>::create());
SDL_Surface* surface = SDL_CreateSurfaceFrom(width, height, SDL_PIXELFORMAT_ARGB8888, const_cast<uint32_t*>(pixels), width * 4);
if (surface == nullptr)
{
dwarnln("Could not create SDL surface for cursor: {}", SDL_GetError());
return BAN::Error::from_errno(EFAULT);
}
origin_x = BAN::Math::clamp<int32_t>(origin_x, 0, width - 1);
origin_y = BAN::Math::clamp<int32_t>(origin_y, 0, height - 1);
cursor->cursor = SDL_CreateColorCursor(surface, origin_x, origin_y);
SDL_DestroySurface(surface);
if (cursor->cursor == nullptr)
{
dwarnln("Could not create SDL color cursor: {}", SDL_GetError());
return BAN::Error::from_errno(EFAULT);
}
return BAN::UniqPtr<PlatformCursor>(BAN::move(cursor));
}
static void sdl3_set_cursor(PlatformWindow*, PlatformCursor* cursor)
{
if (cursor == nullptr)
SDL_SetCursor(s_default_cursor);
else
{
auto& sdl_cursor = *static_cast<SDLCursor*>(cursor);
SDL_SetCursor(sdl_cursor.cursor);
}
}
PlatformOps g_platform_ops = {
.initialize = sdl3_initialize,
.poll_events = sdl3_poll_events,
.create_window = sdl3_create_window,
.invalidate = sdl3_invalidate,
.request_resize = sdl3_request_resize,
.request_reposition = sdl3_request_reposition,
.request_fullscreen = sdl3_request_fullscreen,
.warp_pointer = sdl3_warp_pointer,
.query_pointer = sdl3_query_pointer,
.set_pointer_grab = sdl3_set_pointer_grab,
.create_system_cursor = sdl3_create_system_cursor,
.create_bitmap_cursor = sdl3_create_bitmap_cursor,
.set_cursor = sdl3_set_cursor,
};
static uint32_t sdl3_keycode_to_x_keysym(SDL_Keycode keycode);
static void sdl3_initialize_keymap()
{
static constexpr SDL_Keymod modifier_map[] {
SDL_KMOD_NONE,
SDL_KMOD_SHIFT,
SDL_KMOD_MODE,
SDL_KMOD_MODE | SDL_KMOD_SHIFT,
};
memset(g_keymap, 0, sizeof(g_keymap));
memset(g_modifier_map, 0, sizeof(g_modifier_map));
for (size_t scancode = 0; scancode < g_keymap_size; scancode++)
for (size_t layer = 0; layer < g_keymap_layers; layer++)
if (const auto sdl_key = SDL_GetKeyFromScancode(static_cast<SDL_Scancode>(scancode), modifier_map[layer], false); sdl_key != SDLK_UNKNOWN)
g_keymap[scancode][layer] = sdl3_keycode_to_x_keysym(sdl_key);
const auto get_scancode = [](SDL_Keycode keycode) -> uint8_t {
const auto scancode = SDL_GetScancodeFromKey(keycode, nullptr);;
if (scancode != SDL_SCANCODE_UNKNOWN && scancode < g_keymap_size)
return scancode + g_keymap_min_keycode;
return 0;
};
g_modifier_map[0][0] = get_scancode(SDLK_LSHIFT);
g_modifier_map[0][1] = get_scancode(SDLK_RSHIFT);
g_modifier_map[1][0] = get_scancode(SDLK_CAPSLOCK);
g_modifier_map[2][0] = get_scancode(SDLK_LCTRL);
g_modifier_map[2][1] = get_scancode(SDLK_RCTRL);
g_modifier_map[3][0] = get_scancode(SDLK_LALT);
g_modifier_map[3][1] = get_scancode(SDLK_RALT);
g_modifier_map[4][0] = get_scancode(SDLK_NUMLOCKCLEAR);
g_modifier_map[5][0] = get_scancode(SDLK_LEVEL5_SHIFT);
g_modifier_map[6][0] = get_scancode(SDLK_LGUI);
g_modifier_map[6][1] = get_scancode(SDLK_RGUI);
g_modifier_map[7][0] = get_scancode(SDLK_MODE);
}
#include <X11/keysym.h>
#include <X11/XF86keysym.h>
#include <wctype.h>
static uint32_t sdl3_keycode_to_x_keysym(SDL_Keycode keycode)
{
if (iswprint(keycode))
{
if ((keycode >= 0x20 && keycode <= 0x7E) || (keycode >= 0xA0 && keycode <= 0xFF))
return keycode;
return 0x01000000 | keycode;
}
switch (keycode)
{
case SDLK_RETURN: return XK_Return;
case SDLK_ESCAPE: return XK_Escape;
case SDLK_BACKSPACE: return XK_BackSpace;
case SDLK_TAB: return XK_Tab;
case SDLK_DELETE: return XK_Delete;
case SDLK_CAPSLOCK: return XK_Caps_Lock;
case SDLK_F1: return XK_F1;
case SDLK_F2: return XK_F2;
case SDLK_F3: return XK_F3;
case SDLK_F4: return XK_F4;
case SDLK_F5: return XK_F5;
case SDLK_F6: return XK_F6;
case SDLK_F7: return XK_F7;
case SDLK_F8: return XK_F8;
case SDLK_F9: return XK_F9;
case SDLK_F10: return XK_F10;
case SDLK_F11: return XK_F11;
case SDLK_F12: return XK_F12;
case SDLK_PRINTSCREEN: return XK_Print;
case SDLK_SCROLLLOCK: return XK_Scroll_Lock;
case SDLK_PAUSE: return XK_Pause;
case SDLK_INSERT: return XK_Insert;
case SDLK_HOME: return XK_Home;
case SDLK_PAGEUP: return XK_Page_Up;
case SDLK_END: return XK_End;
case SDLK_PAGEDOWN: return XK_Page_Down;
case SDLK_RIGHT: return XK_Right;
case SDLK_LEFT: return XK_Left;
case SDLK_DOWN: return XK_Down;
case SDLK_UP: return XK_Up;
case SDLK_NUMLOCKCLEAR: return XK_Num_Lock;
case SDLK_KP_DIVIDE: return XK_KP_Divide;
case SDLK_KP_MULTIPLY: return XK_KP_Multiply;
case SDLK_KP_MINUS: return XK_KP_Subtract;
case SDLK_KP_PLUS: return XK_KP_Add;
case SDLK_KP_ENTER: return XK_KP_Enter;
case SDLK_KP_1: return XK_KP_1;
case SDLK_KP_2: return XK_KP_2;
case SDLK_KP_3: return XK_KP_3;
case SDLK_KP_4: return XK_KP_4;
case SDLK_KP_5: return XK_KP_5;
case SDLK_KP_6: return XK_KP_6;
case SDLK_KP_7: return XK_KP_7;
case SDLK_KP_8: return XK_KP_8;
case SDLK_KP_9: return XK_KP_9;
case SDLK_KP_0: return XK_KP_0;
case SDLK_KP_PERIOD: return XK_KP_Decimal;
case SDLK_APPLICATION: return XF86XK_ApplicationLeft;
case SDLK_POWER: return XF86XK_PowerOff;
case SDLK_KP_EQUALS: return XK_KP_Equal;
case SDLK_F13: return XK_F13;
case SDLK_F14: return XK_F14;
case SDLK_F15: return XK_F15;
case SDLK_F16: return XK_F16;
case SDLK_F17: return XK_F17;
case SDLK_F18: return XK_F18;
case SDLK_F19: return XK_F19;
case SDLK_F20: return XK_F20;
case SDLK_F21: return XK_F21;
case SDLK_F22: return XK_F22;
case SDLK_F23: return XK_F23;
case SDLK_F24: return XK_F24;
case SDLK_EXECUTE: return XK_Execute;
case SDLK_HELP: return XK_Help;
case SDLK_MENU: return XK_Menu;
case SDLK_SELECT: return XK_Select;
case SDLK_STOP: return XF86XK_Stop;
case SDLK_AGAIN: return XK_Redo;
case SDLK_UNDO: return XK_Undo;
case SDLK_CUT: return XF86XK_Cut;
case SDLK_COPY: return XF86XK_Copy;
case SDLK_PASTE: return XF86XK_Paste;
case SDLK_FIND: return XK_Find;
case SDLK_MUTE: return XF86XK_AudioMute;
case SDLK_VOLUMEUP: return XF86XK_AudioRaiseVolume;
case SDLK_VOLUMEDOWN: return XF86XK_AudioLowerVolume;
case SDLK_KP_COMMA: return XK_KP_Separator;
//case SDLK_KP_EQUALSAS400: return ;
//case SDLK_ALTERASE: return ;
case SDLK_SYSREQ: return XK_Sys_Req;
case SDLK_CANCEL: return XK_Cancel;
case SDLK_CLEAR: return XK_Clear;
case SDLK_PRIOR: return XK_Prior;
case SDLK_RETURN2: return XK_Return;
//case SDLK_SEPARATOR: return ;
//case SDLK_OUT: return ;
//case SDLK_OPER: return ;
//case SDLK_CLEARAGAIN: return ;
//case SDLK_CRSEL: return ;
//case SDLK_EXSEL: return ;
//case SDLK_KP_00: return ;
//case SDLK_KP_000: return ;
//case SDLK_THOUSANDSSEPARATOR: return ;
//case SDLK_DECIMALSEPARATOR: return ;
//case SDLK_CURRENCYUNIT: return ;
//case SDLK_CURRENCYSUBUNIT: return ;
//case SDLK_KP_LEFTPAREN: return ;
//case SDLK_KP_RIGHTPAREN: return ;
//case SDLK_KP_LEFTBRACE: return ;
//case SDLK_KP_RIGHTBRACE: return ;
case SDLK_KP_TAB: return XK_KP_Tab;
//case SDLK_KP_BACKSPACE: return ;
//case SDLK_KP_A: return ;
//case SDLK_KP_B: return ;
//case SDLK_KP_C: return ;
//case SDLK_KP_D: return ;
//case SDLK_KP_E: return ;
//case SDLK_KP_F: return ;
//case SDLK_KP_XOR: return ;
//case SDLK_KP_POWER: return ;
//case SDLK_KP_PERCENT: return ;
//case SDLK_KP_LESS: return ;
//case SDLK_KP_GREATER: return ;
//case SDLK_KP_AMPERSAND: return ;
//case SDLK_KP_DBLAMPERSAND: return ;
//case SDLK_KP_VERTICALBAR: return ;
//case SDLK_KP_DBLVERTICALBAR: return ;
//case SDLK_KP_COLON: return ;
//case SDLK_KP_HASH: return ;
//case SDLK_KP_SPACE: return ;
//case SDLK_KP_AT: return ;
//case SDLK_KP_EXCLAM: return ;
//case SDLK_KP_MEMSTORE: return ;
//case SDLK_KP_MEMRECALL: return ;
//case SDLK_KP_MEMCLEAR: return ;
//case SDLK_KP_MEMADD: return ;
//case SDLK_KP_MEMSUBTRACT: return ;
//case SDLK_KP_MEMMULTIPLY: return ;
//case SDLK_KP_MEMDIVIDE: return ;
//case SDLK_KP_PLUSMINUS: return ;
//case SDLK_KP_CLEAR: return ;
//case SDLK_KP_CLEARENTRY: return ;
//case SDLK_KP_BINARY: return ;
//case SDLK_KP_OCTAL: return ;
//case SDLK_KP_DECIMAL: return ;
//case SDLK_KP_HEXADECIMAL: return ;
case SDLK_LCTRL: return XK_Control_L;
case SDLK_LSHIFT: return XK_Shift_L;
case SDLK_LALT: return XK_Alt_L;
case SDLK_LGUI: return XK_Super_L;
case SDLK_RCTRL: return XK_Control_R;
case SDLK_RSHIFT: return XK_Shift_R;
case SDLK_RALT: return XK_Alt_R;
case SDLK_RGUI: return XK_Super_R;
case SDLK_MODE: return XK_Mode_switch;
case SDLK_SLEEP: return XF86XK_Sleep;
case SDLK_WAKE: return XF86XK_WakeUp;
//case SDLK_CHANNEL_INCREMENT: return XF86XK_ChannelUp;
//case SDLK_CHANNEL_DECREMENT: return XF86XK_ChannelDown;
case SDLK_MEDIA_PLAY: return XF86XK_AudioPlay;
case SDLK_MEDIA_PAUSE: return XF86XK_AudioPause;
case SDLK_MEDIA_RECORD: return XF86XK_AudioRecord;
case SDLK_MEDIA_FAST_FORWARD: return XF86XK_AudioForward;
case SDLK_MEDIA_REWIND: return XF86XK_AudioRewind;
case SDLK_MEDIA_NEXT_TRACK: return XF86XK_AudioNext;
case SDLK_MEDIA_PREVIOUS_TRACK: return XF86XK_AudioPrev;
case SDLK_MEDIA_STOP: return XF86XK_AudioStop;
case SDLK_MEDIA_EJECT: return XF86XK_Eject;
//case SDLK_MEDIA_PLAY_PAUSE: return XF86XK_MediaPlayPause;
case SDLK_MEDIA_SELECT: return XF86XK_AudioMedia;
case SDLK_AC_NEW: return XF86XK_New;
case SDLK_AC_OPEN: return XF86XK_Open;
case SDLK_AC_CLOSE: return XF86XK_Close;
//case SDLK_AC_EXIT: return XF86XK_Exit;
case SDLK_AC_SAVE: return XF86XK_Save;
case SDLK_AC_PRINT: return XK_Print;
//case SDLK_AC_PROPERTIES: return ;
case SDLK_AC_SEARCH: return XF86XK_Search;
case SDLK_AC_HOME: return XF86XK_HomePage; // ?
case SDLK_AC_BACK: return XF86XK_Back;
case SDLK_AC_FORWARD: return XF86XK_Forward;
case SDLK_AC_STOP: return XF86XK_Stop;
case SDLK_AC_REFRESH: return XF86XK_Refresh;
case SDLK_AC_BOOKMARKS: return XF86XK_Book; // ?
//case SDLK_SOFTLEFT: return ;
//case SDLK_SOFTRIGHT: return ;
//case SDLK_CALL: return ;
//case SDLK_ENDCALL: return ;
case SDLK_LEFT_TAB: return XK_ISO_Left_Tab;
case SDLK_LEVEL5_SHIFT: return XK_ISO_Level5_Shift;
case SDLK_MULTI_KEY_COMPOSE: return XK_Multi_key;
case SDLK_LMETA: return XK_Meta_L;
case SDLK_RMETA: return XK_Meta_R;
case SDLK_LHYPER: return XK_Hyper_L;
case SDLK_RHYPER: return XK_Hyper_R;
}
return 0;
}

View File

@@ -87,10 +87,9 @@ BAN::ErrorOr<DrawableInfo> get_drawable_info(Client& client_info, CARD32 drawabl
case Object::Type::Window: case Object::Type::Window:
{ {
auto& window = drawable_it->value->object.get<Object::Window>(); auto& window = drawable_it->value->object.get<Object::Window>();
auto& texture = window.texture(); info.data_u32 = window.pixels.data();
info.data_u32 = texture.pixels().data(); info.w = window.width;
info.w = texture.width(); info.h = window.height;
info.h = texture.height();
info.depth = window.depth; info.depth = window.depth;
break; break;
} }
@@ -109,3 +108,13 @@ BAN::ErrorOr<DrawableInfo> get_drawable_info(Client& client_info, CARD32 drawabl
return info; return info;
} }
PlatformCursor* get_cursor_safe(CURSOR cid)
{
auto it = g_objects.find(cid);
if (it == g_objects.end())
return nullptr;
if (it->value->type != Object::Type::Cursor)
return nullptr;
return it->value->object.get<BAN::UniqPtr<PlatformCursor>>().ptr();
}

View File

@@ -13,3 +13,5 @@ BAN::ErrorOr<Object::Window&> get_window(Client& client_info, CARD32 wid, BYTE o
BAN::ErrorOr<Object::Pixmap&> get_pixmap(Client& client_info, CARD32 pid, BYTE op_major, BYTE op_minor = 0); BAN::ErrorOr<Object::Pixmap&> get_pixmap(Client& client_info, CARD32 pid, BYTE op_major, BYTE op_minor = 0);
BAN::ErrorOr<Object::GraphicsContext&> get_gc(Client& client_info, CARD32 gc, BYTE op_major, BYTE op_minor = 0); BAN::ErrorOr<Object::GraphicsContext&> get_gc(Client& client_info, CARD32 gc, BYTE op_major, BYTE op_minor = 0);
BAN::ErrorOr<DrawableInfo> get_drawable_info(Client& client_info, CARD32 drawable, BYTE op_major, BYTE op_minor = 0); BAN::ErrorOr<DrawableInfo> get_drawable_info(Client& client_info, CARD32 drawable, BYTE op_major, BYTE op_minor = 0);
PlatformCursor* get_cursor_safe(CURSOR cid);

12
xbanan/Types.h Normal file
View File

@@ -0,0 +1,12 @@
#pragma once
#include <stdint.h>
using ATOM = uint32_t;
using BITGRAVITY = uint32_t;
using COLORMAP = uint32_t;
using CURSOR = uint32_t;
using PIXMAP = uint32_t;
using VISUALID = uint32_t;
using WINDOW = uint32_t;
using WINGRAVITY = uint32_t;

View File

@@ -0,0 +1,345 @@
#include "../Events.h"
#include "../Keymap.h"
#include "../Platform.h"
#include <BAN/Vector.h>
#include <LibGUI/Window.h>
struct BananWindow final : public PlatformWindow
{
~BananWindow()
{
if (window)
unregister_event_fd(window->server_fd());
}
BAN::UniqPtr<LibGUI::Window> window;
};
struct BananCursor final : public PlatformCursor
{
BAN::Vector<uint32_t> pixels;
uint32_t width;
uint32_t height;
int32_t origin_x;
int32_t origin_y;
};
static BAN::ErrorOr<void> bananos_initialize_keymap();
static bool bananos_initialize(uint32_t* display_w, uint32_t* display_h)
{
auto attributes = LibGUI::Window::default_attributes;
attributes.shown = false;
auto dummy_or_error = LibGUI::Window::create(0, 0, ""_sv, attributes);
if (dummy_or_error.is_error())
{
dwarnln("Could not get display size: {}", dummy_or_error.error());
return false;
}
*display_w = dummy_or_error.value()->width();
*display_h = dummy_or_error.value()->height();
if (auto ret = bananos_initialize_keymap(); ret.is_error())
{
dwarnln("Could not initialize keymap: {}", ret.error());
return false;
}
return true;
}
static void bananos_poll_events(void* window)
{
auto& banan_window = *static_cast<BananWindow*>(window);
banan_window.window->poll_events();
}
static BAN::ErrorOr<BAN::UniqPtr<PlatformWindow>> bananos_create_window(PlatformWindow* parent, WindowType type, WINDOW wid, int32_t x, int32_t y, uint32_t width, uint32_t height)
{
(void)parent;
(void)type;
(void)x;
(void)y;
auto window = TRY(BAN::UniqPtr<BananWindow>::create());
auto attributes = LibGUI::Window::default_attributes;
attributes.shown = true;
attributes.title_bar = false;
attributes.resizable = true;
auto gui_window = TRY(LibGUI::Window::create(width, height, ""_sv, attributes));
auto* winp = gui_window.ptr();
gui_window->set_close_window_event_callback([wid] {
on_window_close_event(wid);
});
gui_window->set_resize_window_event_callback([wid, winp]() {
on_window_resize_event(wid, winp->width(), winp->height());
});
gui_window->set_window_focus_event_callback([wid](auto event) {
on_window_focus_event(wid, event.focused);
});
gui_window->set_window_fullscreen_event_callback([wid](auto event) {
on_window_fullscreen_event(wid, event.fullscreen);
});
gui_window->set_mouse_move_event_callback([wid](auto event) {
on_mouse_move_event(wid, event.x, event.y);
});
gui_window->set_mouse_button_event_callback([wid](auto event) {
uint8_t xbutton = 0;
switch (event.button)
{
case LibInput::MouseButton::Left: xbutton = 1; break;
case LibInput::MouseButton::Middle: xbutton = 2; break;
case LibInput::MouseButton::Right: xbutton = 3; break;
case LibInput::MouseButton::Extra1: xbutton = 8; break;
case LibInput::MouseButton::Extra2: xbutton = 9; break;
}
if (xbutton)
on_mouse_button_event(wid, xbutton, event.pressed);
});
gui_window->set_mouse_scroll_event_callback([wid](auto event) {
on_mouse_button_event(wid, event.scroll > 0 ? 4 : 5, true);
on_mouse_button_event(wid, event.scroll > 0 ? 4 : 5, false);
});
gui_window->set_key_event_callback([wid](auto event) {
const uint8_t xmod =
(event.shift() ? (1 << 0) : 0) |
(event.caps_lock() ? (1 << 1) : 0) |
(event.ctrl() ? (1 << 2) : 0) |
(event.alt() ? (1 << 3) : 0) |
(event.num_lock() ? (1 << 4) : 0) |
// level5 shift
// super
(event.ralt() ? (1 << 7) : 0); // FIXME: altgr
on_key_event(wid, event.scancode, xmod, event.pressed());
});
window->window = BAN::move(gui_window);
register_event_fd(window->window->server_fd(), window.ptr());
return BAN::UniqPtr<PlatformWindow>(BAN::move(window));
}
static void bananos_request_resize(PlatformWindow* window, uint32_t width, uint32_t height)
{
auto& banan_window = *static_cast<BananWindow*>(window);
banan_window.window->request_resize(width, height);
}
static void bananos_invalidate(PlatformWindow* window, const uint32_t* pixels, uint32_t x, uint32_t y, uint32_t width, uint32_t height)
{
auto& banan_window = *static_cast<BananWindow*>(window);
const uint32_t win_width = banan_window.window->width();
auto* out_pixels = banan_window.window->texture().pixels().data();
for (uint32_t y_off = 0; y_off < height; y_off++)
for (uint32_t x_off = 0; x_off < width; x_off++)
out_pixels[(y + y_off) * win_width + (x + x_off)] = pixels[(y + y_off) * win_width + (x + x_off)];
banan_window.window->invalidate(x, y, width, height);
}
static void bananos_request_fullscreen(PlatformWindow* window, bool fullscreen)
{
auto& banan_window = *static_cast<BananWindow*>(window);
banan_window.window->set_fullscreen(fullscreen);
}
static BAN::ErrorOr<BAN::UniqPtr<PlatformCursor>> bananos_create_bitmap_cursor(const uint32_t* pixels, uint32_t width, uint32_t height, int32_t origin_x, int32_t origin_y)
{
auto cursor = TRY(BAN::UniqPtr<BananCursor>::create());
cursor->width = width;
cursor->height = height;
cursor->origin_x = origin_x;
cursor->origin_y = origin_y;
TRY(cursor->pixels.resize(width * height));
memcpy(cursor->pixels.data(), pixels, cursor->pixels.size() * 4);
return BAN::UniqPtr<PlatformCursor>(BAN::move(cursor));
}
static void bananos_set_cursor(PlatformWindow* window, PlatformCursor* cursor)
{
if (window == nullptr)
return;
auto& banan_window = *static_cast<BananWindow*>(window);
if (cursor == nullptr)
banan_window.window->set_cursor(0, 0, {}, 0, 0);
else
{
auto& banan_cursor = *static_cast<BananCursor*>(cursor);
banan_window.window->set_cursor(banan_cursor.width, banan_cursor.height, banan_cursor.pixels.span(), banan_cursor.origin_x, banan_cursor.origin_y);
}
}
PlatformOps g_platform_ops = {
.initialize = bananos_initialize,
.poll_events = bananos_poll_events,
.create_window = bananos_create_window,
.invalidate = bananos_invalidate,
.request_resize = bananos_request_resize,
.request_reposition = nullptr,
.request_fullscreen = bananos_request_fullscreen,
.warp_pointer = nullptr,
.query_pointer = nullptr,
.set_pointer_grab = nullptr,
.create_system_cursor = nullptr,
.create_bitmap_cursor = bananos_create_bitmap_cursor,
.set_cursor = bananos_set_cursor,
};
#include <LibInput/KeyboardLayout.h>
#include <LibInput/KeyEvent.h>
static uint32_t bananos_key_to_x_keysym(LibInput::Key, bool upper);
static BAN::ErrorOr<void> bananos_initialize_keymap()
{
// FIXME: get this from somewhere (gui command? enviroment? tmp file?)
const auto keymap_path = "./us.keymap"_sv;
TRY(LibInput::KeyboardLayout::initialize());
auto& keyboard_layout = LibInput::KeyboardLayout::get();
TRY(keyboard_layout.load_from_file(keymap_path));
const BAN::Span<const LibInput::Key> banan_keymaps[] {
keyboard_layout.keymap_normal(),
keyboard_layout.keymap_shift(),
keyboard_layout.keymap_altgr(),
keyboard_layout.keymap_altgr(), // add shift+altgr map?
};
for (size_t scancode = 0; scancode < g_keymap_size; scancode++)
for (size_t layer = 0; layer < g_keymap_layers; layer++)
if (const auto banan_key = banan_keymaps[layer][scancode]; banan_key != LibInput::Key::None)
g_keymap[scancode][layer] = bananos_key_to_x_keysym(banan_key, layer % 2);
using LibInput::keycode_normal;
using LibInput::keycode_numpad;
g_modifier_map[0][0] = keycode_normal(3, 0) + g_keymap_min_keycode; // lshift
g_modifier_map[0][1] = keycode_normal(3, 12) + g_keymap_min_keycode; // rshift
g_modifier_map[1][0] = keycode_normal(2, 0) + g_keymap_min_keycode; // caps lock
g_modifier_map[2][0] = keycode_normal(4, 0) + g_keymap_min_keycode; // lctrl
g_modifier_map[2][1] = keycode_normal(4, 6) + g_keymap_min_keycode; // rctrl
g_modifier_map[3][0] = keycode_normal(4, 2) + g_keymap_min_keycode; // lalt
g_modifier_map[3][1] = keycode_normal(4, 5) + g_keymap_min_keycode; // ralt
g_modifier_map[4][0] = keycode_numpad(0, 0) + g_keymap_min_keycode; // num lock
//g_modifier_map[5][0] = level5 shift;
g_modifier_map[6][0] = keycode_normal(4, 1) + g_keymap_min_keycode; // lsuper
g_modifier_map[6][1] = keycode_normal(4, 4) + g_keymap_min_keycode; // rsuper
g_modifier_map[7][0] = keycode_normal(4, 5) + g_keymap_min_keycode; // FIXME: altgr
return {};
}
#include <BAN/UTF8.h>
#include <X11/keysym.h>
#include <X11/XF86keysym.h>
#include <wctype.h>
static uint32_t bananos_key_to_x_keysym(LibInput::Key key, bool upper)
{
using namespace LibInput;
if (const char* utf8 = key_to_utf8(key, upper ? KeyEvent::LShift : 0))
{
const uint32_t codepoint = BAN::UTF8::to_codepoint(utf8);
if (codepoint != BAN::UTF8::invalid && iswprint(codepoint))
{
if ((codepoint >= 0x20 && codepoint <= 0x7E) || (codepoint >= 0xA0 && codepoint <= 0xFF))
return codepoint;
return 0x01000000 | codepoint;
}
}
switch (key)
{
case Key::F1: return XK_F1;
case Key::F2: return XK_F2;
case Key::F3: return XK_F3;
case Key::F4: return XK_F4;
case Key::F5: return XK_F5;
case Key::F6: return XK_F6;
case Key::F7: return XK_F7;
case Key::F8: return XK_F8;
case Key::F9: return XK_F9;
case Key::F10: return XK_F10;
case Key::F11: return XK_F11;
case Key::F12: return XK_F12;
case Key::Insert: return XK_Insert;
case Key::PrintScreen: return XK_Print;
case Key::Delete: return XK_Delete;
case Key::Home: return XK_Home;
case Key::End: return XK_End;
case Key::PageUp: return XK_Page_Up;
case Key::PageDown: return XK_Page_Down;
case Key::Enter: return XK_Return;
case Key::Backspace: return XK_BackSpace;
case Key::Escape: return XK_Escape;
case Key::Tab: return XK_Tab;
case Key::CapsLock: return XK_Caps_Lock;
case Key::LeftShift: return XK_Shift_L;
case Key::LeftCtrl: return XK_Control_L;
case Key::Super: return XK_Super_L;
case Key::LeftAlt: return XK_Alt_L;
case Key::RightAlt: return XK_Alt_R;
case Key::RightCtrl: return XK_Control_R;
case Key::RightShift: return XK_Shift_R;
case Key::ArrowUp: return XK_Up;
case Key::ArrowDown: return XK_Down;
case Key::ArrowLeft: return XK_Left;
case Key::ArrowRight: return XK_Right;
case Key::NumLock: return XK_Num_Lock;
case Key::ScrollLock: return XK_Scroll_Lock;
case Key::Numpad0: return XK_KP_0;
case Key::Numpad1: return XK_KP_1;
case Key::Numpad2: return XK_KP_2;
case Key::Numpad3: return XK_KP_3;
case Key::Numpad4: return XK_KP_4;
case Key::Numpad5: return XK_KP_5;
case Key::Numpad6: return XK_KP_6;
case Key::Numpad7: return XK_KP_7;
case Key::Numpad8: return XK_KP_8;
case Key::Numpad9: return XK_KP_9;
case Key::NumpadPlus: return XK_KP_Add;
case Key::NumpadMinus: return XK_KP_Subtract;
case Key::NumpadMultiply: return XK_KP_Multiply;
case Key::NumpadDivide: return XK_KP_Divide;
case Key::NumpadEnter: return XK_KP_Enter;
case Key::NumpadDecimal: return XK_KP_Decimal;
case Key::VolumeMute: return XF86XK_AudioMute;
case Key::VolumeUp: return XF86XK_AudioRaiseVolume;
case Key::VolumeDown: return XF86XK_AudioLowerVolume;
case Key::Calculator: return XF86XK_Calculator;
case Key::MediaPlayPause: return XF86XK_AudioPlay;
case Key::MediaStop: return XF86XK_AudioStop;
case Key::MediaPrevious: return XF86XK_AudioPrev;
case Key::MediaNext: return XF86XK_AudioNext;
default: break;
}
static_assert(static_cast<size_t>(Key::Count) == 145, "update keymap");
return 0;
}

View File

@@ -1,16 +1,17 @@
#include "Base.h" #include "Base.h"
#include "Definitions.h" #include "Definitions.h"
#include "Keymap.h"
#include <X11/X.h> #include <X11/X.h>
#include <X11/Xatom.h> #include <X11/Xatom.h>
#include <locale.h>
#include <poll.h>
#include <signal.h> #include <signal.h>
#include <sys/epoll.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#define USE_UNIX_SOCKET 0 #define USE_UNIX_SOCKET 1
#if USE_UNIX_SOCKET #if USE_UNIX_SOCKET
#include <sys/un.h> #include <sys/un.h>
@@ -18,19 +19,6 @@
#include <netinet/in.h> #include <netinet/in.h>
#endif #endif
static const xRectangle s_screen_bounds =
[]() {
auto attributes = LibGUI::Window::default_attributes;
attributes.shown = false;
auto window = MUST(LibGUI::Window::create(0, 0, ""_sv, attributes));
return xRectangle {
.x = 0,
.y = 0,
.width = static_cast<CARD16>(window->width()),
.height = static_cast<CARD16>(window->height()),
};
}();
const xPixmapFormat g_formats[6] { const xPixmapFormat g_formats[6] {
{ {
.depth = 1, .depth = 1,
@@ -79,16 +67,16 @@ const xVisualType g_visual {
.blueMask = 0x0000FF, .blueMask = 0x0000FF,
}; };
const xWindowRoot g_root { xWindowRoot g_root {
.windowId = 2, .windowId = 2,
.defaultColormap = 0, .defaultColormap = 0,
.whitePixel = 0xFFFFFF, .whitePixel = 0xFFFFFF,
.blackPixel = 0x000000, .blackPixel = 0x000000,
.currentInputMask = 0, .currentInputMask = 0,
.pixWidth = s_screen_bounds.width, .pixWidth = 0,
.pixHeight = s_screen_bounds.height, .pixHeight = 0,
.mmWidth = static_cast<CARD16>(s_screen_bounds.width * 254 / 960), // 96 DPI .mmWidth = 0,
.mmHeight = static_cast<CARD16>(s_screen_bounds.height * 254 / 960), // 96 DPI .mmHeight = 0,
.minInstalledMaps = 1, .minInstalledMaps = 1,
.maxInstalledMaps = 1, .maxInstalledMaps = 1,
.rootVisualID = g_visual.visualID, .rootVisualID = g_visual.visualID,
@@ -104,13 +92,14 @@ 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;
int main() int main()
{ {
setlocale(LC_ALL, "");
for (int sig = 1; sig < NSIG; sig++) for (int sig = 1; sig < NSIG; sig++)
if (sig != SIGWINCH) if (sig != SIGWINCH)
signal(sig, exit); signal(sig, exit);
@@ -165,20 +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)); \
@@ -262,18 +237,29 @@ int main()
APPEND_ATOM_CUSTOM(WM_DELETE_WINDOW); APPEND_ATOM_CUSTOM(WM_DELETE_WINDOW);
APPEND_ATOM_CUSTOM(_NET_WM_STATE); APPEND_ATOM_CUSTOM(_NET_WM_STATE);
APPEND_ATOM_CUSTOM(_NET_WM_STATE_FULLSCREEN); APPEND_ATOM_CUSTOM(_NET_WM_STATE_FULLSCREEN);
APPEND_ATOM_CUSTOM(_NET_WM_WINDOW_TYPE);
APPEND_ATOM_CUSTOM(_NET_WM_WINDOW_TYPE_DIALOG);
APPEND_ATOM_CUSTOM(_NET_WM_WINDOW_TYPE_NORMAL);
APPEND_ATOM_CUSTOM(_NET_WM_WINDOW_TYPE_SPLASH);
APPEND_ATOM_CUSTOM(_NET_WM_WINDOW_TYPE_UTILITY);
#undef APPEND_ATOM_CUSTOM #undef APPEND_ATOM_CUSTOM
MUST(initialize_keymap()); uint32_t display_w, display_h;
if (!g_platform_ops.initialize(&display_w, &display_h))
return 1;
g_root.pixWidth = display_w;
g_root.pixHeight = display_h;
g_root.mmWidth = static_cast<CARD16>(display_w * 254 / 960); // 96 DPI
g_root.mmHeight = static_cast<CARD16>(display_h * 254 / 960); // 96 DPI
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);
@@ -287,6 +273,18 @@ int main()
window.event_masks.remove(&client_info); window.event_masks.remove(&client_info);
} }
for (auto it = client_info.objects.begin(); it != client_info.objects.end();)
{
auto obj_it = g_objects.find(*it);
if (obj_it == g_objects.end() || obj_it->value->type != Object::Type::Window)
it++;
else
{
(void)destroy_window(client_info, *it);
it = client_info.objects.begin();
}
}
for (auto id : client_info.objects) for (auto id : client_info.objects)
{ {
auto it = g_objects.find(id); auto it = g_objects.find(id);
@@ -294,59 +292,44 @@ int main()
continue; continue;
auto& object = *it->value; auto& object = *it->value;
if (object.type == Object::Type::Window) switch (object.type)
{
auto& window = object.object.get<Object::Window>();
if (window.window.has<BAN::UniqPtr<LibGUI::Window>>())
{
auto& gui_window = window.window.get<BAN::UniqPtr<LibGUI::Window>>();
epoll_ctl(g_epoll_fd, EPOLL_CTL_DEL, gui_window->server_fd(), nullptr);
g_epoll_thingies.remove(gui_window->server_fd());
}
}
if (object.type == Object::Type::Extension)
{ {
case Object::Type::Visual:
case Object::Type::Cursor:
case Object::Type::Pixmap:
case Object::Type::GraphicsContext:
case Object::Type::Font:
break;
case Object::Type::Window:
ASSERT_NOT_REACHED();
case Object::Type::Extension:
auto& extension = object.object.get<Object::Extension>(); auto& extension = object.object.get<Object::Extension>();
extension.destructor(extension); extension.destructor(extension);
break;
} }
g_objects.remove(it); g_objects.remove(it);
} }
epoll_ctl(g_epoll_fd, EPOLL_CTL_DEL, client_fd, nullptr); g_pollables.remove(client_fd);
close(client_fd); close(client_fd);
g_epoll_thingies.remove(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(g_objects.insert(g_root.windowId,
MUST(BAN::UniqPtr<Object>::create(Object { MUST(BAN::UniqPtr<Object>::create(Object {
.type = Object::Type::Window, .type = Object::Type::Window,
.object = Object::Window { .object = Object::Window {
.owner = dummy_client,
.mapped = true, .mapped = true,
.depth = g_root.rootDepth, .depth = g_root.rootDepth,
.parent = None, .parent = None,
.c_class = InputOutput, .c_class = InputOutput,
.window = {}, .width = g_root.pixWidth,
.height = g_root.pixHeight,
} }
})) }))
)); ));
@@ -359,12 +342,37 @@ int main()
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)
@@ -373,53 +381,50 @@ int main()
continue; continue;
} }
MUST(g_epoll_thingies.insert(client_sock, { BAN::Optional<uint32_t> client_pid;
.type = EpollThingy::Type::Client,
#ifdef SO_PEERCRED
ucred client_cred;
socklen_t client_cred_len = sizeof(client_cred);
if (getsockopt(client_sock, SOL_SOCKET, SO_PEERCRED, &client_cred, &client_cred_len) == 0)
client_pid = client_cred.pid;
#endif
MUST(g_pollables.insert(client_sock, {
.type = Pollable::Type::Client,
.value = Client { .value = Client {
.fd = client_sock, .fd = client_sock,
.state = Client::State::ConnectionSetup, .state = Client::State::ConnectionSetup,
.pid = client_pid,
} }
})); }));
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::Window) if (pollable.type == Pollable::Type::Event)
{ {
auto* window = epoll_thingy.value.get<LibGUI::Window*>(); g_platform_ops.poll_events(pollable.value.get<void*>());
window->poll_events();
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,
@@ -446,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)
@@ -579,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;
}
} }
} }