aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
blob: bcd12d6bcfa64ea3487fc657a95b506c14ebfeb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
const std = @import("std");
const mem = std.mem;
const posix = std.posix;

const c = @cImport({
    @cDefine("GL_GLEXT_PROTOTYPES", {});
    @cInclude("GL/gl.h");
    @cInclude("GL/glext.h");
});

const wayland = @import("wayland");
const wl = wayland.client.wl;
const xdg = wayland.client.xdg;

const egl = @import("egl.zig");

const vertex_shader_src: [*c]const u8 =
    \\#version 330 core
    \\layout (location = 0) in vec3 pos;
    \\layout (location = 1) in vec4 in_color;
    \\out vec4 color;
    \\
    \\void main()
    \\{
    \\  color = in_color;
    \\	gl_Position = vec4(pos.x, pos.y, pos.z, 1.0);
    \\}
;

const frag_shader_src: [*c]const u8 =
    \\#version 330 core
    \\in vec4 color;
    \\out vec4 frag_color;
    \\
    \\void main()
    \\{
    \\	frag_color = color;
    \\}
;

/// Global Wayland registry context; contains pointers to the globals we expect
/// from the compositor.
const RegistryContext = struct {
    compositor: ?*wl.Compositor = null,
    wm_base: ?*xdg.WmBase = null,
};

var registry_ctx = RegistryContext{};
var egl_display: *egl.Display = undefined;
var egl_cfg: [1]*egl.Config = undefined;

const GlGetIvFn = *const fn (
    obj: c_uint,
    pname: c.GLenum,
    params: [*c]c_int,
) callconv(.C) void;

const GlGetLogFn = *const fn (
    obj: c_uint,
    max: c_int,
    len: [*c]c_int,
    buf: [*c]u8,
) callconv(.C) void;

fn checkGlError(
    getIvFn: GlGetIvFn,
    getLogFn: GlGetLogFn,
    obj: c_uint,
    pname: c.GLenum,
) bool {
    var success: c_int = 0;
    var gl_log: [512:0]u8 = undefined;
    getIvFn(obj, pname, &success);
    const err = success == 0;
    if (err) {
        getLogFn(obj, 512, null, &gl_log);
        std.debug.print("OpenGL shader error: {s}\n", .{gl_log});
    }
    return err;
}

fn checkProgramLinkError(prog: c_uint) !void {
    if (checkGlError(
        c.glGetProgramiv,
        c.glGetProgramInfoLog,
        prog,
        c.GL_LINK_STATUS,
    )) {
        return error.GlProgramLinkError;
    }
}

fn checkShaderError(shader: c_uint) !void {
    if (checkGlError(
        c.glGetShaderiv,
        c.glGetShaderInfoLog,
        shader,
        c.GL_COMPILE_STATUS,
    )) {
        return error.GlShaderCompileError;
    }
}

/// A single Window (surface); data and methods we need to tell the compositor
/// to render a surface for us.
pub const Window = struct {
    /// The allocator that we were allocated on, needed to destroy ourselves
    allocator: mem.Allocator,
    /// Wayland surface
    wl_surface: *wl.Surface,
    /// XDG surface attached to the Wayland surface
    xdg_surface: *xdg.Surface = undefined,
    /// XDG toplevel data
    xdg_toplevel: *xdg.Toplevel = undefined,
    /// EGL Wayland display
    egl_win: *wl.EglWindow = undefined,
    /// EGL context
    egl_ctx: *egl.Context = undefined,
    /// EGL surface
    egl_surface: *egl.Surface = undefined,
    /// OpenGl triangle shader program
    shader_prog: c_uint = undefined,
    /// Triangle vertices
    vertices: [12]f32 = .{
        -0.5, -0.5, 0.0, // bl
        0.5, -0.5, 0.0, // br
        0.5, 0.5, 0.0, // tr
        -0.5, 0.5, 0.0, // tl
    },
    colors: [16]f32 = .{
        1.0, 0.0, 0.0, 1.0,
        0.0, 1.0, 0.0, 1.0,
        0.0, 0.0, 1.0, 1.0,
        1.0, 1.0, 1.0, 1.0,
    },
    /// Triangles indices
    indices: [6]c_uint = .{
        0, 1, 2,
        2, 3, 0,
    },
    /// OpenGL vertex buffer object
    vbo: c_uint = undefined,
    /// OpenGL color vbo
    cbo: c_uint = undefined,
    /// OpenGL vertex array object
    vao: c_uint = undefined,
    // OpenGL element buffer object
    ebo: c_uint = undefined,
    /// This window's title
    title: [*:0]const u8,
    /// Width in pixels
    width: i32 = 480,
    /// Height in pixels
    height: i32 = 360,
    /// Last frame's timestamp in ms
    last_frame: u32 = 0,
    /// Frames per second statistic
    // current_fps: f32 = 0.0,
    /// Should we keep this window open?
    open: bool = true,

    /// Allocate a new window and all backing resources for it
    pub fn create(allocator: mem.Allocator, title: [*:0]const u8) !*Window {
        const compositor = registry_ctx.compositor orelse return error.NoWlCompositor;
        const wm_base = registry_ctx.wm_base orelse return error.NoXdgWmBase;

        var win = try allocator.create(Window);
        win.* = Window{
            .allocator = allocator,
            .wl_surface = try compositor.createSurface(),
            .title = title,
        };
        win.xdg_surface = try wm_base.getXdgSurface(win.wl_surface);
        errdefer win.xdg_surface.destroy();
        win.xdg_toplevel = try win.xdg_surface.getToplevel();
        errdefer win.xdg_toplevel.destroy();

        win.egl_ctx = try egl_display.createContext(egl_cfg[0], null, null);
        win.egl_win = try wl.EglWindow.create(win.wl_surface, win.width, win.height);
        win.egl_surface = try egl_display.createWindowSurface(egl_cfg[0], win.egl_win, null);
        try egl_display.makeCurrent(win.egl_surface, win.egl_surface, win.egl_ctx);

        const gl_vendor = c.glGetString(c.GL_VENDOR);
        const gl_version = c.glGetString(c.GL_VERSION);
        std.debug.print("{?s} OpenGL {?s}\n", .{ gl_vendor, gl_version });

        const vertex_shader = c.glCreateShader(c.GL_VERTEX_SHADER);
        defer c.glDeleteShader(vertex_shader);
        c.glShaderSource(vertex_shader, 1, &vertex_shader_src, null);
        c.glCompileShader(vertex_shader);
        try checkShaderError(vertex_shader);

        const frag_shader = c.glCreateShader(c.GL_FRAGMENT_SHADER);
        defer c.glDeleteShader(frag_shader);
        c.glShaderSource(frag_shader, 1, &frag_shader_src, null);
        c.glCompileShader(frag_shader);
        try checkShaderError(frag_shader);

        win.shader_prog = c.glCreateProgram();
        c.glAttachShader(win.shader_prog, vertex_shader);
        c.glAttachShader(win.shader_prog, frag_shader);
        c.glLinkProgram(win.shader_prog);
        try checkProgramLinkError(win.shader_prog);

        c.glGenVertexArrays(1, &win.vao);
        c.glGenBuffers(1, &win.vbo);
        c.glGenBuffers(1, &win.cbo);
        c.glGenBuffers(1, &win.ebo);

        c.glBindVertexArray(win.vao);

        c.glBindBuffer(c.GL_ARRAY_BUFFER, win.vbo);
        c.glBufferData(
            c.GL_ARRAY_BUFFER,
            @sizeOf(@TypeOf(win.vertices)),
            &win.vertices,
            c.GL_STATIC_DRAW,
        );

        c.glVertexAttribPointer(0, 3, c.GL_FLOAT, c.GL_FALSE, 3 * @sizeOf(f32), null);
        c.glEnableVertexAttribArray(0);

        c.glBindBuffer(c.GL_ARRAY_BUFFER, win.cbo);
        c.glBufferData(
            c.GL_ARRAY_BUFFER,
            @sizeOf(@TypeOf(win.colors)),
            &win.colors,
            c.GL_DYNAMIC_DRAW,
        );

        c.glVertexAttribPointer(1, 3, c.GL_FLOAT, c.GL_FALSE, 4 * @sizeOf(f32), null);
        c.glEnableVertexAttribArray(1);

        c.glBindBuffer(c.GL_ELEMENT_ARRAY_BUFFER, win.ebo);
        c.glBufferData(
            c.GL_ELEMENT_ARRAY_BUFFER,
            @sizeOf(@TypeOf(win.indices)),
            &win.indices,
            c.GL_STATIC_DRAW,
        );

        c.glBindBuffer(c.GL_ARRAY_BUFFER, 0);
        c.glBindVertexArray(0);

        c.glViewport(@divTrunc(win.width, 2) - 160, @divTrunc(win.height, 2) - 160, 320, 320);

        win.xdg_surface.setListener(*Window, xdgSurfaceListener, win);
        win.xdg_toplevel.setListener(*Window, xdgToplevelListener, win);

        win.xdg_toplevel.setTitle(title);

        win.wl_surface.commit();

        const wl_cb = try win.wl_surface.frame();
        wl_cb.setListener(*Window, frameListener, win);

        return win;
    }

    /// Destroy this window and all its associated resources
    pub fn destroy(win: *Window) void {
        c.glDeleteVertexArrays(1, &win.vao);
        c.glDeleteBuffers(1, &win.vbo);
        c.glDeleteProgram(win.shader_prog);
        win.egl_win.destroy();
        win.xdg_toplevel.destroy();
        win.xdg_surface.destroy();
        win.wl_surface.destroy();
        win.allocator.destroy(win);
    }

    /// Draw a new frame
    pub fn drawFrame(win: *Window) !void {
        try egl_display.swapInterval(0);

        c.glClearColor(0, 0.05, 0.15, 1);
        c.glClear(c.GL_COLOR_BUFFER_BIT);

        var ts: f32 = @floatFromInt(win.last_frame);
        ts /= 1000;
        const light_sine = @sin(ts) / 2.0 + 0.5;
        const dark_sine = @sin(ts-std.math.pi/2.0) / 2.0 + 0.5;
        // red
        win.colors[0] = light_sine;
        win.colors[1] = dark_sine;
        win.colors[2] = dark_sine;
        // green
        win.colors[4] = dark_sine;
        win.colors[5] = light_sine;
        win.colors[6] = dark_sine;
        // blue
        win.colors[8] = dark_sine;
        win.colors[9] = dark_sine;
        win.colors[10] = light_sine;
        // yellow
        win.colors[12] = light_sine;
        win.colors[13] = light_sine;
        win.colors[14] = dark_sine;

        c.glUseProgram(win.shader_prog);

        c.glBindVertexArray(win.vao);

        c.glBindBuffer(c.GL_ARRAY_BUFFER, win.cbo);
        c.glBufferData(
            c.GL_ARRAY_BUFFER,
            @sizeOf(@TypeOf(win.colors)),
            &win.colors,
            c.GL_DYNAMIC_DRAW,
        );

        c.glDrawElements(c.GL_TRIANGLES, win.indices.len, c.GL_UNSIGNED_INT, null);

        try egl_display.swapBuffers(win.egl_surface);
    }

    /// Resize this window
    fn resize(win: *Window, width: i32, height: i32) !void {
        const new_height = if (height != 0) height else win.height;
        const new_width = if (width != 0) width else win.width;
        if (win.width == new_width and win.height == new_height) return;

        win.width = new_width;
        win.height = new_height;
        win.egl_win.resize(win.width, win.height, 0, 0);
        if (win.width < win.height) {
            c.glViewport(
                0,
                @divTrunc(win.height, 2) - @divTrunc(win.width, 2),
                win.width,
                win.width,
            );
        } else {
            c.glViewport(
                @divTrunc(win.width, 2) - @divTrunc(win.height, 2),
                0,
                win.height,
                win.height,
            );
        }
    }

    /// XDG Surface callbacks
    fn xdgSurfaceListener(xdg_surface: *xdg.Surface, event: xdg.Surface.Event, win: *Window) void {
        switch (event) {
            .configure => |configure| {
                xdg_surface.ackConfigure(configure.serial);
                win.drawFrame() catch |err| {
                    std.debug.print("error drawing frame {}\n", .{err});
                };
                win.wl_surface.commit();
            },
        }
    }

    /// XDG Toplevel callbacks
    fn xdgToplevelListener(_: *xdg.Toplevel, event: xdg.Toplevel.Event, win: *Window) void {
        switch (event) {
            .configure => |configure| {
                win.resize(configure.width, configure.height) catch |err| {
                    std.debug.print("error resizing window {}\n", .{err});
                    return;
                };
            },
            .close => win.open = false,
        }
    }

    /// Wayland frame callback
    fn frameListener(wl_cb: *wl.Callback, event: wl.Callback.Event, win: *Window) void {
        switch (event) {
            .done => |done| {
                wl_cb.destroy();
                const new_cb = win.wl_surface.frame() catch |err| {
                    std.debug.print("unable to get frame {}\n", .{err});
                    win.open = false;
                    return;
                };
                new_cb.setListener(*Window, frameListener, win);

                // if (win.last_frame != 0) {
                    // const elapsed: f32 = @floatFromInt(done.callback_data -% win.last_frame);
                    // win.current_fps = 1000.0 / elapsed;
                // }
                win.last_frame = done.callback_data;

                win.drawFrame() catch |err| {
                    std.debug.print("error drawing frame {}\n", .{err});
                    return;
                };
                win.wl_surface.damageBuffer(0, 0, std.math.maxInt(i32), std.math.maxInt(i32));
                win.wl_surface.commit();
            },
        }
    }
};

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gpa.allocator();
    const display = try wl.Display.connect(null);
    defer display.disconnect();
    const registry = try display.getRegistry();

    registry.setListener(*RegistryContext, registryListener, &registry_ctx);
    if (display.roundtrip() != .SUCCESS) return error.RoundTripFailed;

    const wm_base = registry_ctx.wm_base orelse return error.NoXdgWmBase;
    wm_base.setListener(?*void, xdgWmBaseListener, null);

    try egl.bindApi(.opengl);
    var attrs = [_]egl.Attribute{
        .{ .type = .red_size, .value = 8 },
        .{ .type = .green_size, .value = 8 },
        .{ .type = .blue_size, .value = 8 },
        .{ .type = .alpha_size, .value = 8 },
        .{ .type = .none, .value = 0 },
    };
    egl_display = try egl.Display.get(display);
    var major: i32 = 0;
    var minor: i32 = 0;
    try egl_display.initialize(&major, &minor);
    defer egl_display.terminate();
    std.debug.print("EGL version {}.{}\n", .{ major, minor });
    _ = try egl_display.chooseConfig(&attrs, &egl_cfg);

    const win = try Window.create(allocator, "Hello, wayland!");
    defer win.destroy();

    while (win.open) {
        if (display.dispatch() != .SUCCESS) return error.DispatchFailed;
    }
}

/// Global registry callback
fn registryListener(registry: *wl.Registry, event: wl.Registry.Event, ctx: *RegistryContext) void {
    switch (event) {
        .global => |global| {
            std.debug.print("inteface: {s}, version {}, name {}\n", .{
                global.interface,
                global.version,
                global.name,
            });
            if (mem.orderZ(u8, global.interface, wl.Compositor.getInterface().name) == .eq) {
                ctx.compositor = registry.bind(global.name, wl.Compositor, 6) catch return;
            } else if (mem.orderZ(u8, global.interface, xdg.WmBase.getInterface().name) == .eq) {
                ctx.wm_base = registry.bind(global.name, xdg.WmBase, 2) catch return;
            }
        },
        .global_remove => {},
    }
}

/// XDG Wm Base callback
fn xdgWmBaseListener(wm_base: *xdg.WmBase, event: xdg.WmBase.Event, _: ?*void) void {
    switch (event) {
        .ping => |ping| {
            wm_base.pong(ping.serial);
        },
    }
}