-
Notifications
You must be signed in to change notification settings - Fork 5
/
graphics.zig
287 lines (273 loc) · 10.7 KB
/
graphics.zig
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
const std = @import("std");
const x = @import("./x.zig");
const common = @import("common.zig");
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const allocator = arena.allocator();
const window_width = 400;
const window_height = 400;
pub fn main() !u8 {
try x.wsaStartup();
const conn = try common.connect(allocator);
defer std.posix.shutdown(conn.sock, .both) catch {};
const screen = blk: {
const fixed = conn.setup.fixed();
inline for (@typeInfo(@TypeOf(fixed.*)).Struct.fields) |field| {
std.log.debug("{s}: {any}", .{field.name, @field(fixed, field.name)});
}
std.log.debug("vendor: {s}", .{try conn.setup.getVendorSlice(fixed.vendor_len)});
const format_list_offset = x.ConnectSetup.getFormatListOffset(fixed.vendor_len);
const format_list_limit = x.ConnectSetup.getFormatListLimit(format_list_offset, fixed.format_count);
std.log.debug("fmt list off={} limit={}", .{format_list_offset, format_list_limit});
const formats = try conn.setup.getFormatList(format_list_offset, format_list_limit);
for (formats, 0..) |format, i| {
std.log.debug("format[{}] depth={:3} bpp={:3} scanpad={:3}", .{i, format.depth, format.bits_per_pixel, format.scanline_pad});
}
const screen = conn.setup.getFirstScreenPtr(format_list_limit);
inline for (@typeInfo(@TypeOf(screen.*)).Struct.fields) |field| {
std.log.debug("SCREEN 0| {s}: {any}", .{field.name, @field(screen, field.name)});
}
break :blk screen;
};
// TODO: maybe need to call conn.setup.verify or something?
const window_id = conn.setup.fixed().resource_id_base;
{
var msg_buf: [x.create_window.max_len]u8 = undefined;
const len = x.create_window.serialize(&msg_buf, .{
.window_id = window_id,
.parent_window_id = screen.root,
.x = 0, .y = 0,
.width = window_width, .height = window_height,
.border_width = 0, // TODO: what is this?
.class = .input_output,
.visual_id = screen.root_visual,
.depth = 24, // sure I guess
}, .{
// .bg_pixmap = .copy_from_parent,
.bg_pixel = 0xaabbccdd,
// //.border_pixmap =
// .border_pixel = 0x01fa8ec9,
// .bit_gravity = .north_west,
// .win_gravity = .east,
// .backing_store = .when_mapped,
// .backing_planes = 0x1234,
// .backing_pixel = 0xbbeeeeff,
// .override_redirect = true,
// .save_under = true,
.event_mask =
x.event.key_press
| x.event.key_release
| x.event.button_press
| x.event.button_release
| x.event.enter_window
| x.event.leave_window
| x.event.pointer_motion
// | x.event.pointer_motion_hint WHAT THIS DO?
// | x.event.button1_motion WHAT THIS DO?
// | x.event.button2_motion WHAT THIS DO?
// | x.event.button3_motion WHAT THIS DO?
// | x.event.button4_motion WHAT THIS DO?
// | x.event.button5_motion WHAT THIS DO?
// | x.event.button_motion WHAT THIS DO?
| x.event.keymap_state
| x.event.exposure
,
// .dont_propagate = 1,
});
try conn.send(msg_buf[0..len]);
}
const bg_gc_id = window_id + 1;
{
var msg_buf: [x.create_gc.max_len]u8 = undefined;
const len = x.create_gc.serialize(&msg_buf, .{
.gc_id = bg_gc_id,
.drawable_id = window_id,
}, .{
.foreground = screen.black_pixel,
});
try conn.send(msg_buf[0..len]);
}
const fg_gc_id = window_id + 2;
{
var msg_buf: [x.create_gc.max_len]u8 = undefined;
const len = x.create_gc.serialize(&msg_buf, .{
.gc_id = fg_gc_id,
.drawable_id = window_id,
}, .{
.background = screen.black_pixel,
.foreground = 0xffaadd,
//.line_width = 10,
});
try conn.send(msg_buf[0..len]);
}
// get some font information
{
const text_literal = [_]u16 { 'm' };
const text = x.Slice(u16, [*]const u16) { .ptr = &text_literal, .len = text_literal.len };
var msg: [x.query_text_extents.getLen(text.len)]u8 = undefined;
x.query_text_extents.serialize(&msg, fg_gc_id, text);
try conn.send(&msg);
}
const double_buf = try x.DoubleBuffer.init(
std.mem.alignForward(usize, 1000, std.mem.page_size),
.{ .memfd_name = "ZigX11DoubleBuffer" },
);
defer double_buf.deinit(); // not necessary but good to test
std.log.info("read buffer capacity is {}", .{double_buf.half_len});
var buf = double_buf.contiguousReadBuffer();
const font_dims: FontDims = blk: {
_ = try x.readOneMsg(conn.reader(), @alignCast(buf.nextReadBuffer()));
switch (x.serverMsgTaggedUnion(@alignCast(buf.double_buffer_ptr))) {
.reply => |msg_reply| {
const msg: *x.ServerMsg.QueryTextExtents = @ptrCast(msg_reply);
break :blk .{
.width = @intCast(msg.overall_width),
.height = @intCast(msg.font_ascent + msg.font_descent),
.font_left = @intCast(msg.overall_left),
.font_ascent = msg.font_ascent,
};
},
else => |msg| {
std.log.err("expected a reply but got {}", .{msg});
return 1;
},
}
};
{
var msg: [x.map_window.len]u8 = undefined;
x.map_window.serialize(&msg, window_id);
try conn.send(&msg);
}
while (true) {
{
const recv_buf = buf.nextReadBuffer();
if (recv_buf.len == 0) {
std.log.err("buffer size {} not big enough!", .{buf.half_len});
return 1;
}
const len = try x.readSock(conn.sock, recv_buf, 0);
if (len == 0) {
std.log.info("X server connection closed", .{});
return 0;
}
buf.reserve(len);
}
while (true) {
const data = buf.nextReservedBuffer();
if (data.len < 32)
break;
const msg_len = x.parseMsgLen(data[0..32].*);
if (data.len < msg_len)
break;
buf.release(msg_len);
//buf.resetIfEmpty();
switch (x.serverMsgTaggedUnion(@alignCast(data.ptr))) {
.err => |msg| {
std.log.err("{}", .{msg});
return 1;
},
.reply => |msg| {
std.log.info("todo: handle a reply message {}", .{msg});
return error.TodoHandleReplyMessage;
},
.key_press => |msg| {
std.log.info("key_press: {}", .{msg.keycode});
},
.key_release => |msg| {
std.log.info("key_release: {}", .{msg.keycode});
},
.button_press => |msg| {
std.log.info("button_press: {}", .{msg});
},
.button_release => |msg| {
std.log.info("button_release: {}", .{msg});
},
.enter_notify => |msg| {
std.log.info("enter_window: {}", .{msg});
},
.leave_notify => |msg| {
std.log.info("leave_window: {}", .{msg});
},
.motion_notify => |msg| {
// too much logging
_ = msg;
//std.log.info("pointer_motion: {}", .{msg});
},
.keymap_notify => |msg| {
std.log.info("keymap_state: {}", .{msg});
},
.expose => |msg| {
std.log.info("expose: {}", .{msg});
try render(conn.sock, window_id, bg_gc_id, fg_gc_id, font_dims);
},
.mapping_notify => |msg| {
std.log.info("mapping_notify: {}", .{msg});
},
.no_exposure => |msg| std.debug.panic("unexpected no_exposure {}", .{msg}),
.unhandled => |msg| {
std.log.info("todo: server msg {}", .{msg});
return error.UnhandledServerMsg;
},
.map_notify,
.reparent_notify,
.configure_notify,
=> unreachable, // did not register for these
}
}
}
}
const FontDims = struct {
width: u8,
height: u8,
font_left: i16, // pixels to the left of the text basepoint
font_ascent: i16, // pixels up from the text basepoint to the top of the text
};
fn render(sock: std.posix.socket_t, drawable_id: u32, bg_gc_id: u32, fg_gc_id: u32, font_dims: FontDims) !void {
// {
// var msg: [x.poly_fill_rectangle.getLen(1)]u8 = undefined;
// x.poly_fill_rectangle.serialize(&msg, .{
// .drawable_id = drawable_id,
// .gc_id = bg_gc_id,
// }, &[_]x.Rectangle {
// .{ .x = 100, .y = 100, .width = 200, .height = 200 },
// });
// try common.send(sock, &msg);
// }
_ = bg_gc_id;
{
var msg: [x.clear_area.len]u8 = undefined;
x.clear_area.serialize(&msg, false, drawable_id, .{
.x = 0, .y = 0, .width = 0, .height = 0,
});
try common.send(sock, &msg);
}
_ = font_dims;
{
var msg: [x.poly_line.getLen(3)]u8 = undefined;
x.poly_line.serialize(&msg, .{
.coordinate_mode = .origin,
.drawable_id = drawable_id,
.gc_id = fg_gc_id,
}, &[_]x.Point {
.{ .x = 10, .y = 10 },
.{ .x = 110, .y = 10 },
.{ .x = 55, .y = 55 },
});
try common.send(sock, &msg);
}
// {
// const text_literal: []const u8 = "Hello X!";
// const text = x.Slice(u8, [*]const u8) { .ptr = text_literal.ptr, .len = text_literal.len };
// var msg: [x.image_text8.getLen(text.len)]u8 = undefined;
//
// const text_width = font_dims.width * text_literal.len;
//
// x.image_text8.serialize(&msg, .{
// .drawable_id = drawable_id,
// .gc_id = fg_gc_id,
// .x = @divTrunc((window_width - @intCast(i16, text_width)), 2) + font_dims.font_left,
// .y = @divTrunc((window_height - @intCast(i16, font_dims.height)), 2) + font_dims.font_ascent,
// .text = text,
// });
// try common.send(sock, &msg);
// }
}