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
|
//! The plain records the core and the terminal both name
//! (docs/next-terminal.md, Phase 1): a frame's cells, their styles and
//! colours, the pictures over them and a key read. Data that needs
//! nothing else, in a module of its own: the core (surface.zig and
//! Event.zig, under their old names) and the terminal (src/term/) both
//! import it, and neither imports the other.
const std = @import("std");
pub const Color = union(enum) { default, index: u8, rgb: [3]u8 };
pub const FontRole = enum(u8) { body, tagline };
pub const CellStyle = struct {
fg: Color = .default,
bg: Color = .default,
bold: bool = false,
dim: bool = false,
italic: bool = false,
blink: bool = false,
reverse: bool = false,
invisible: bool = false,
strikethrough: bool = false,
ul: enum(u3) { off, single, double, curly, dotted, dashed } = .off,
font_role: FontRole = .body,
};
pub const Cell = struct {
text: [7]u8 = @splat(' '),
len: u8 = 1,
style: CellStyle = .{},
default: bool = true,
pub fn grapheme(c: *const Cell) []const u8 {
return c.text[0..c.len];
}
pub fn visuallyEqual(a: *const Cell, b: *const Cell) bool {
if (a.default or b.default) return a.default and b.default;
return a.len == b.len and
std.mem.eql(u8, a.grapheme(), b.grapheme()) and
std.meta.eql(a.style, b.style);
}
pub fn printableAscii(c: *const Cell) ?u8 {
if (c.default) return ' ';
if (c.len != 1) return null;
const byte = c.text[0];
return if (byte >= ' ' and byte <= '~') byte else null;
}
};
/// A rectangle in pixels.
pub const RectPx = struct {
x: u32 = 0,
y: u32 = 0,
w: u32 = 0,
h: u32 = 0,
};
/// A rectangle in the frame's cells.
pub const RectCell = struct { x: u16 = 0, y: u16 = 0, w: u16 = 0, h: u16 = 0 };
/// The pixels a placement shows: RGBA, straight alpha, `stride` bytes a
/// row. They are the pane's, and stay put until its next frame.
pub const Raster = struct {
ptr: [*]const u8,
w: u32,
h: u32,
stride: u32,
pub fn of(rgba: []const u8, w: usize, h: usize) Raster {
return .{ .ptr = rgba.ptr, .w = @intCast(w), .h = @intCast(h), .stride = @intCast(w * 4) };
}
pub fn bytes(r: Raster) []const u8 {
return r.ptr[0 .. @as(usize, r.stride) * r.h];
}
};
/// A picture over the frame's cells, which a frontend realises: the window
/// as a texture kept by `key`, the tty as a kitty image, or either as glyph
/// art in `dst`'s cells (Art.zig, the frontends' own) where it cannot show
/// pixels or the pane asks for art. The frame lists them a pane at a time,
/// lowest first.
pub const Placement = struct {
pane: u8,
raster: Raster,
/// The part of `raster` shown.
src: RectPx,
/// The cells it lies over, which clip it.
dst: RectCell,
/// Where it goes in `dst`, in pixels from `dst`'s top left corner;
/// null: the frontend fits `src` there, no larger than it is, at the
/// top left (as glyph art, centred).
at: ?RectPx = null,
/// The pixels' identity (`keyOf`): one key is one picture, so a
/// frontend keeps what it uploaded under it.
key: u64,
/// A terminal program's kitty `z=`: below 0 it is under the text,
/// which a tty passes on to the terminal outside.
z: i32 = 0,
/// Rows of `raster` that alone differ from the picture under the key
/// `from`: a frontend still holding that one updates those rows.
dirty: ?Dirty = null,
art: Art = .{},
pub const Dirty = struct { from: u64, y: u32, h: u32 };
/// How the pane wants it drawn as glyph art: the Petscii, Palette and
/// Ascii words on an image pane, Petscii and TermImages on a terminal.
pub const Art = struct {
/// Glyph art even where pixels could be shown (Petscii on).
glyph: bool = false,
palette: Palette = .commodore,
/// Letters and signs among the glyphs, not only blocks and lines.
ascii: bool = true,
pub const Palette = enum { commodore, terminal };
};
/// A pane's picture `page` (a PDF's page, a terminal's image id; 0 for
/// an image) as of `revision`.
pub fn keyOf(serial: u32, page: u32, revision: u32) u64 {
return std.hash.Wyhash.hash(0, std.mem.asBytes(&[3]u32{ serial, page, revision }));
}
};
/// A key a frontend read (Event.zig).
pub const Key = struct {
cp: u21, // codepoint or one of the specials below
text: []const u8 = "",
ctrl: bool = false,
alt: bool = false,
shift: bool = false,
pub const enter: u21 = 0x0D;
pub const escape: u21 = 0x1B;
pub const tab: u21 = 0x09;
pub const backspace: u21 = 0x7F;
// private-use plane for non-text keys, matching no real codepoint
pub const up: u21 = 0xF0001;
pub const down: u21 = 0xF0002;
pub const left: u21 = 0xF0003;
pub const right: u21 = 0xF0004;
pub const home: u21 = 0xF0005;
pub const end: u21 = 0xF0006;
pub const page_up: u21 = 0xF0007;
pub const page_down: u21 = 0xF0008;
pub const delete: u21 = 0xF0009;
pub const insert: u21 = 0xF000A;
pub const print_screen: u21 = 0xF000B;
pub const scroll_lock: u21 = 0xF000C;
pub const pause: u21 = 0xF000D;
pub const menu: u21 = 0xF000E;
/// F1 to F25, in order: Fn is `f1 + n - 1`.
pub const f1: u21 = 0xF0011;
pub const f25: u21 = f1 + 24;
};
|