Implemented Xorg initialization code

This commit is contained in:
Lorenzo Torres 2025-08-04 02:46:36 +02:00
parent f894fb317d
commit 097a6a9b5c
11 changed files with 96 additions and 29 deletions

View file

@ -51,13 +51,27 @@ pub fn build(b: *std.Build) void {
}),
});
exe.root_module.addImport("sideros", sideros);
exe.root_module.addCSourceFile(.{ .file = b.path("ext/xdg-shell.c") });
exe.root_module.addIncludePath(b.path("ext"));
sideros.addIncludePath(b.path("ext"));
exe.linkSystemLibrary("vulkan");
exe.linkSystemLibrary("wayland-client");
exe.linkLibC();
const options = b.addOptions();
if (target.result.os.tag == .linux) {
const wayland = b.option(bool, "wayland", "Use Wayland to create the main window") orelse false;
if (wayland) {
exe.linkSystemLibrary("wayland-client");
exe.root_module.addCSourceFile(.{ .file = b.path("ext/xdg-shell.c") });
} else {
exe.linkSystemLibrary("xcb");
exe.linkSystemLibrary("xcb-icccm");
}
options.addOption(bool, "wayland", wayland);
}
sideros.addOptions("config", options);
b.installArtifact(exe);
const root_lib = b.addLibrary(.{

View file

@ -1,4 +1,9 @@
pub const c = @cImport({
@cInclude("wayland-client.h");
@cInclude("xdg-shell.h");
@cInclude("xcb/xcb.h");
@cInclude("vulkan/vulkan.h");
@cInclude("vulkan/vulkan_wayland.h");
@cInclude("vulkan/vulkan_xcb.h");
@cInclude("xcb/xcb_icccm.h");
});

View file

@ -1,11 +1,12 @@
const std = @import("std");
const config = @import("config");
const config = @import("sideros").config;
const math = @import("sideros").math;
const Input = @import("sideros").Input;
const mods = @import("sideros").mods;
const ecs = @import("sideros").ecs;
//const Renderer = @import("sideros").Renderer;
const wayland = @import("wayland.zig");
const builtin = @import("builtin");
const platform = if (builtin.target.os.tag == .linux) (if (config.wayland) @import("wayland.zig") else @import("xorg.zig")) else @import("xorg.zig");
//fn testSystem2(pool: *ecs.Pool) void {
// std.debug.print("{any}\n", .{pool.resources.input.isKeyDown(.a)});
@ -66,5 +67,6 @@ pub fn main() !void {
// try pool.addComponent(entity, ecs.components.Position{ .x = 1.0, .y = 0.5, .z = 3.0 });
// try pool.addComponent(entity, ecs.components.Speed{ .speed = 5.0 });
// }
try wayland.init(allocator);
try platform.init(allocator);
}

View file

@ -1,4 +1,4 @@
const c = @import("c.zig").c;
const c = @import("sideros").c;
const std = @import("std");
const vk = @import("vulkan.zig");
const gltf = @import("gltf.zig");

View file

@ -1,4 +1,4 @@
const c = @import("c.zig");
const c = @import("sideros").c;
const ecs = @import("ecs");
const std = @import("std");
const vk = @import("vulkan.zig");
@ -19,10 +19,10 @@ current_frame: u32,
vertex_buffer: vk.Buffer,
index_buffer: vk.Buffer,
pub fn init(allocator: Allocator, display: ?*anyopaque, s: ?*anyopaque) !Renderer {
pub fn init(comptime C: type, comptime S: type, allocator: Allocator, display: C, s: S) !Renderer {
const instance = try vk.Instance.create(allocator);
const surface = try vk.Surface.create(instance, display, s);
const surface = try vk.Surface.create(C, S, instance, display, s);
var physical_device = try vk.PhysicalDevice.pick(allocator, instance);
const device = try physical_device.create_device(surface, allocator, 2);

View file

@ -1,4 +1,4 @@
const c = @import("c.zig").c;
const c = @import("sideros").c;
const ecs = @import("ecs");
const std = @import("std");

View file

@ -1,5 +0,0 @@
pub const c = @cImport({
@cInclude("vulkan/vulkan.h");
@cInclude("vulkan/vulkan_wayland.h");
@cInclude("wayland-client.h");
});

View file

@ -1,5 +1,5 @@
const std = @import("std");
const c = @import("c.zig").c;
const c = @import("sideros").c;
const Window = @import("Window.zig");
const Mesh = @import("Mesh.zig");
const sideros = @import("sideros");
@ -7,6 +7,7 @@ const Camera = @import("Camera.zig");
const math = sideros.math;
const Allocator = std.mem.Allocator;
const config = sideros.config;
const builtin = @import("builtin");
const debug = (builtin.mode == .Debug);
@ -73,8 +74,7 @@ pub const Instance = struct {
handle: c.VkInstance,
pub fn create(allocator: Allocator) !Instance {
const extensions = [_][*c]const u8 {c.VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, c.VK_KHR_SURFACE_EXTENSION_NAME};
//const extensions = [_][:0]const u8 {"VK_KHR_wayland_surface\0", "VK_KHR_surface\0"};
const extensions = [_][*c]const u8 {if (config.wayland) c.VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME else c.VK_KHR_XCB_SURFACE_EXTENSION_NAME, c.VK_KHR_SURFACE_EXTENSION_NAME};
// Querry avaliable extensions size
var avaliableExtensionsCount: u32 = 0;
@ -757,15 +757,25 @@ pub fn Swapchain(comptime n: usize) type {
pub const Surface = struct {
handle: c.VkSurfaceKHR,
pub fn create(instance: Instance, display: ?*anyopaque, surface: ?*anyopaque) !Surface {
pub fn create(comptime C: type, comptime S: type, instance: Instance, display: C, surface: S) !Surface {
var handle: c.VkSurfaceKHR = undefined;
const create_info: c.VkWaylandSurfaceCreateInfoKHR = .{
.sType = c.VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR,
.display = @ptrCast(display),
.surface = @ptrCast(surface),
};
if (config.wayland) {
const create_info: c.VkWaylandSurfaceCreateInfoKHR = .{
.sType = c.VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR,
.display = display,
.surface = surface,
};
try mapError(c.vkCreateWaylandSurfaceKHR(instance.handle, &create_info, null, &handle));
} else {
const create_info: c.VkXcbSurfaceCreateInfoKHR = .{
.sType = c.VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR,
.connection = display,
.window = surface,
};
try mapError(c.vkCreateXcbSurfaceKHR(instance.handle, &create_info, null, &handle));
}
try mapError(c.vkCreateWaylandSurfaceKHR(instance.handle, &create_info, null, &handle));
return .{
.handle = handle,
};

View file

@ -3,3 +3,5 @@ pub const Input = @import("Input.zig");
pub const mods = @import("mods");
pub const ecs = @import("ecs");
pub const Renderer = @import("renderer");
pub const config = @import("config");
pub const c = @import("c.zig").c;

View file

@ -1,4 +1,4 @@
const c = @import("c.zig").c;
const c = @import("sideros").c;
const std = @import("std");
const Renderer = @import("sideros").Renderer;
@ -136,7 +136,7 @@ pub fn init(allocator: std.mem.Allocator) !void {
_ = c.wl_display_dispatch(display);
}
var renderer = try Renderer.init(allocator, @ptrCast(display), @ptrCast(surface));
var renderer = try Renderer.init(@TypeOf(display), @TypeOf(surface), allocator, display, surface);
defer renderer.deinit();
try renderer.render();

39
src/xorg.zig Normal file
View file

@ -0,0 +1,39 @@
const std = @import("std");
const Renderer = @import("sideros").Renderer;
const c = @import("sideros").c;
pub fn init(allocator: std.mem.Allocator) !void {
const connection = c.xcb_connect(null, null);
defer c.xcb_disconnect(connection);
const setup = c.xcb_get_setup(connection);
const iter = c.xcb_setup_roots_iterator(setup);
const screen = iter.data;
const mask = c.XCB_CW_EVENT_MASK;
const value = c.XCB_EVENT_MASK_EXPOSURE;
const window = c.xcb_generate_id(connection);
_ = c.xcb_create_window(connection, c.XCB_COPY_FROM_PARENT, window, screen.*.root, 0, 0, 800, 600, 10, c.XCB_WINDOW_CLASS_INPUT_OUTPUT, screen.*.root_visual, mask, &value);
var hints: c.xcb_size_hints_t = undefined;
c.xcb_icccm_size_hints_set_min_size(&hints, 800, 600);
c.xcb_icccm_size_hints_set_max_size(&hints, 800, 600);
_ = c.xcb_icccm_set_wm_size_hints(connection, window, c.XCB_ATOM_WM_NORMAL_HINTS, &hints);
_ = c.xcb_map_window(connection, window);
_ = c.xcb_flush(connection);
var renderer = try Renderer.init(@TypeOf(connection), @TypeOf(window), allocator, connection, window);
defer renderer.deinit();
while (c.xcb_wait_for_event(connection)) |e| {
switch (e.*.response_type & ~@as(u32, 0x80)) {
else => {},
}
try renderer.render();
std.c.free(e);
}
}