aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/ncurses.zig
blob: 9b626b322fda2130fac0b07669b99ab1656f80a8 (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
//! This module provides an implementation of IO using the ncurses library.
const std = @import("std");
const Action = @import("../actions.zig").Action;
const ncurses = @cImport({
    @cInclude("ncurses.h");
});
const locale = @cImport({
    @cInclude("locale.h");
});

const TICK_MS = 33;

const MAIN_PANEL_WIDTH = 100;
const MAIN_PANEL_HEIGHT = 41;
const INSTRUCTION_PANEL_WIDTH = 32;
const INSTRUCTION_PANEL_HEIGHT = 39;
const MESSAGE_PANEL_WIDTH = 100;
const MESSAGE_PANEL_HEIGHT = 3;
const STATUS_PANEL_WIDTH = 32;
const STATUS_PANEL_HEIGHT = 5;
const MIN_WIDTH = MAIN_PANEL_WIDTH + INSTRUCTION_PANEL_WIDTH;
const MIN_HEIGHT = MAIN_PANEL_HEIGHT + MESSAGE_PANEL_HEIGHT;

const KEY_MOVE_UP: i32 = 'k';
const KEY_MOVE_DOWN: i32 = 'j';
const KEY_MOVE_LEFT: i32 = 'h';
const KEY_MOVE_RIGHT: i32 = 'l';
const KEY_STAIR_UP: i32 = '<';
const KEY_STAIR_DOWN: i32 = '>';
const KEY_QUIT: i32 = ncurses.KEY_F(1);

/// Provide an IO struct that manages the state of the display and user input
pub const IO = struct {
    allocator: std.mem.Allocator,
    main: ?*ncurses.WINDOW,
    inst: ?*ncurses.WINDOW,
    msgs: ?*ncurses.WINDOW,
    stat: ?*ncurses.WINDOW,
    prev_tick_time: i64,

    fn validTermSize(self: *IO) bool {
        _ = self; // should be uncallable without initialization
        return ncurses.LINES >= MIN_HEIGHT and
            ncurses.COLS >= MIN_WIDTH;
    }

    fn colorSupport(self: *IO) bool {
        _ = self; // should be uncallable without initialization
        return ncurses.has_colors();
    }

    fn createNewWin(self: *IO, height: i32, width: i32, y: i32, x: i32) ?*ncurses.WINDOW {
        _ = self; // should be uncallable without initialization
        const local_win = ncurses.newwin(height, width, y, x);
        _ = ncurses.box(local_win, 0, 0);
        _ = ncurses.wrefresh(local_win);
        return local_win;
    }

    fn formatInstruction(self: *IO, pos: i32, key: i32, description: [:0]const u8) !void {
        const c_description: ?[*:0]const u8 = description.ptr;

        if (ncurses.KEY_F0 <= key and key <= ncurses.KEY_F(64)) {
            _ = ncurses.mvwprintw(self.inst, pos, 2, "F%d - %s", key - ncurses.KEY_F0, c_description);
        } else if (key < 128) {
            _ = ncurses.mvwprintw(self.inst, pos, 2, "%c - %s", key, c_description);
        } else {
            const str = std.fmt.allocPrint(self.allocator, "Invalid key name: {d}", .{key}) catch {
                return error.OutOfMemory;
            };
            defer self.allocator.free(str);
            try self.displayMessage(str);
        }
    }

    fn createPanels(self: *IO) !void {
        try self.createInstructionPanel();
        self.createMessagePanel();
        self.createStatisticsPanel();
        self.createMainPanel();
    }

    fn handleResize(self: *IO) !Action {
        const lines = @as(usize, @intCast(ncurses.LINES));
        const cols = @as(usize, @intCast(ncurses.COLS));
        for (0..lines) |i| {
            for (0..cols) |j| {
                const i_32 = @as(i32, @intCast(i));
                const j_32 = @as(i32, @intCast(j));
                _ = ncurses.mvaddch(i_32, j_32, ' ');
            }
        }
        _ = ncurses.refresh();

        self.deleteWindows();

        if (!self.validTermSize()) {
            _ = ncurses.mvprintw(0, 0, "Terminal must be at least %dx%d", @as(i32, @intCast(MIN_WIDTH)), @as(i32, @intCast(MIN_HEIGHT)));
        } else {
            try self.createPanels();
        }

        _ = ncurses.refresh();

        return Action.illegal;
    }

    fn displayInstructions(self: *IO) !void {
        try self.formatInstruction(1, KEY_MOVE_LEFT, "move left");
        try self.formatInstruction(2, KEY_MOVE_DOWN, "move down");
        try self.formatInstruction(3, KEY_MOVE_UP, "move up");
        try self.formatInstruction(4, KEY_MOVE_RIGHT, "move right");
        try self.formatInstruction(5, KEY_STAIR_DOWN, "move down staircase");
        try self.formatInstruction(6, KEY_STAIR_UP, "move up staircase");
        try self.formatInstruction(7, KEY_QUIT, "quit");
        _ = ncurses.wrefresh(self.inst);
    }

    /// Display a message in the message box.
    /// Takes a pre-formatted null-terminated string
    /// If the message is too wide for the box, display "Message too long" instead.
    pub fn displayMessage(self: *IO, str: []const u8) !void {
        if (self.msgs == null) return;

        for (1..MESSAGE_PANEL_WIDTH - 1) |i| {
            const i_32 = @as(i32, @intCast(i));
            _ = ncurses.mvwaddch(self.msgs, 1, i_32, ' ');
        }

        if (str.len > MESSAGE_PANEL_WIDTH - 2) {
            try self.displayMessage("Message too long");
            return;
        }

        var buf: [MESSAGE_PANEL_WIDTH:0]u8 = undefined;
        std.mem.copyForwards(u8, &buf, str);
        buf[str.len] = 0;

        const msg: [:0]u8 = &buf;

        const c_msg: ?[*:0]const u8 = msg.ptr;
        _ = ncurses.mvwprintw(self.msgs, 1, 1, c_msg);
        _ = ncurses.wrefresh(self.msgs);
    }

    fn displayStatus(self: *IO) void {
        for (1..STATUS_PANEL_HEIGHT - 1) |i| {
            for (1..STATUS_PANEL_WIDTH - 1) |j| {
                const i_32 = @as(i32, @intCast(i));
                const j_32 = @as(i32, @intCast(j));
                _ = ncurses.mvwaddch(self.stat, i_32, j_32, ' ');
            }
        }

        // TODO: Implement

        _ = ncurses.wrefresh(self.stat);
    }

    /// An interface for user input and time processing.
    /// Waits for the end of a tick and returns a tick action.
    /// If input is given before the end of the tick, return that instead.
    pub fn waitForTick(self: *IO) !Action {
        var new = std.time.milliTimestamp();

        while (new - self.prev_tick_time <= TICK_MS) {
            const action = self.processInput() catch |err| {
                return err;
            };
            if (action != Action.illegal) return action;

            new = std.time.milliTimestamp();
        }

        self.prev_tick_time = new;
        return Action.tick;
    }

    fn processInput(self: *IO) !Action {
        const ch = ncurses.getch();

        if (!self.validTermSize()) {
            if (ch != KEY_QUIT and ch != ncurses.KEY_RESIZE) return Action.illegal;
        }

        return switch (ch) {
            KEY_MOVE_UP => Action.move_up,
            KEY_MOVE_DOWN => Action.move_down,
            KEY_MOVE_LEFT => Action.move_left,
            KEY_MOVE_RIGHT => Action.move_right,
            KEY_STAIR_DOWN => Action.down_stair,
            KEY_STAIR_UP => Action.up_stair,
            KEY_QUIT => Action.exit,
            ncurses.KEY_RESIZE => self.handleResize(),
            else => Action.illegal,
        };
    }

    fn createInstructionPanel(self: *IO) !void {
        self.inst = self.createNewWin(
            INSTRUCTION_PANEL_HEIGHT,
            INSTRUCTION_PANEL_WIDTH,
            @divTrunc(ncurses.LINES - INSTRUCTION_PANEL_HEIGHT - STATUS_PANEL_HEIGHT, 2) + 1,
            @divTrunc(ncurses.COLS - MAIN_PANEL_WIDTH - INSTRUCTION_PANEL_WIDTH, 2) + MAIN_PANEL_WIDTH - 1,
        );
        if (self.inst != null) {
            try self.displayInstructions();
        }
    }

    fn createMessagePanel(self: *IO) void {
        self.msgs = self.createNewWin(
            MESSAGE_PANEL_HEIGHT,
            MESSAGE_PANEL_WIDTH,
            @divTrunc(ncurses.LINES - MAIN_PANEL_HEIGHT - MESSAGE_PANEL_HEIGHT, 2) + MAIN_PANEL_HEIGHT,
            @divTrunc(ncurses.COLS - MESSAGE_PANEL_WIDTH - INSTRUCTION_PANEL_WIDTH, 2),
        );
    }

    fn createStatisticsPanel(self: *IO) void {
        self.stat = self.createNewWin(
            STATUS_PANEL_HEIGHT,
            STATUS_PANEL_WIDTH,
            @divTrunc(ncurses.LINES - INSTRUCTION_PANEL_HEIGHT - STATUS_PANEL_HEIGHT, 2) + INSTRUCTION_PANEL_HEIGHT,
            @divTrunc(ncurses.COLS - MAIN_PANEL_WIDTH - INSTRUCTION_PANEL_WIDTH, 2) + MAIN_PANEL_WIDTH - 1,
        );
    }

    fn createMainPanel(self: *IO) void {
        self.main = self.createNewWin(
            MAIN_PANEL_HEIGHT,
            MAIN_PANEL_WIDTH,
            @divTrunc(ncurses.LINES - MAIN_PANEL_HEIGHT - MESSAGE_PANEL_HEIGHT, 2) + 1,
            @divTrunc(ncurses.COLS - MAIN_PANEL_WIDTH - INSTRUCTION_PANEL_WIDTH, 2),
        );
    }

    pub fn init(allocator: std.mem.Allocator) !IO {
        _ = locale.setlocale(locale.LC_ALL, "");

        var io = IO{
            .allocator = allocator,
            .inst = null,
            .msgs = null,
            .stat = null,
            .main = null,
            .prev_tick_time = std.time.milliTimestamp(),
        };

        if (ncurses.initscr() == null) {
            return error.CursesInitFail;
        }

        if (!io.colorSupport()) {
            _ = ncurses.endwin();
            return error.NoColorSupport;
        }

        _ = ncurses.raw();
        _ = ncurses.keypad(ncurses.stdscr, true);
        _ = ncurses.noecho();
        _ = ncurses.curs_set(0);
        _ = ncurses.start_color();
        _ = ncurses.timeout(1);

        _ = ncurses.init_pair(1, ncurses.COLOR_WHITE, ncurses.COLOR_BLACK);
        _ = ncurses.init_pair(2, ncurses.COLOR_BLACK, ncurses.COLOR_RED);
        _ = ncurses.wattron(ncurses.stdscr, ncurses.COLOR_PAIR(1));
        _ = ncurses.refresh();

        if (!io.validTermSize()) {
            _ = ncurses.mvprintw(0, 0, "Terminal must be at least %dx%d", @as(i32, @intCast(MIN_WIDTH)), @as(i32, @intCast(MIN_HEIGHT)));
        } else {
            try io.createPanels();
        }

        return io;
    }

    fn deleteWindows(self: *IO) void {
        _ = ncurses.delwin(self.main);
        self.main = null;
        _ = ncurses.delwin(self.inst);
        self.inst = null;
        _ = ncurses.delwin(self.msgs);
        self.msgs = null;
        _ = ncurses.delwin(self.stat);
        self.stat = null;
    }

    pub fn deinit(self: *IO) void {
        self.deleteWindows();
        _ = ncurses.endwin();
    }
};