const std = @import("std"); const CROWS = 5; const CCOLS = 4; const MAX_DISPLAYSTR_LEN = 64; const RGB_WHITE = 16777215; const RGB_GREY = 12632256; const RGB_KBD_TEXT = 986895; const RGB_RESULT = 6052991; var gdisplaystr: [MAX_DISPLAYSTR_LEN]u8 = undefined; var gdisplaystrlen: usize = 0; var gfisnumberentering: bool = false; var gbinop: u8 = '+'; var gax: f64 = 0; var gbx: f64 = 0; var gfnotexit: bool = true; const kbdstr = [CROWS * CCOLS][]const u8{ "Off", "C", " { myputs("O 0 0 0\n"); gfnotexit = false; }, 1 => { gdisplaystrlen = 0; gfisnumberentering = false; displaystr("0", RGB_RESULT); }, 2 => { if (gdisplaystrlen > 0) { gdisplaystrlen -= 1; displaystr(if (gdisplaystrlen == 0) "0" else gdisplaystr[0..gdisplaystrlen], RGB_RESULT); } }, 18 => { if (gdisplaystrlen > 0) { if (gdisplaystr[0] == '-') { std.mem.copyForwards(u8, gdisplaystr[0..], gdisplaystr[1..gdisplaystrlen]); gdisplaystrlen -= 1; } else if (gdisplaystrlen < MAX_DISPLAYSTR_LEN - 1) { std.mem.copyBackwards(u8, gdisplaystr[1..], gdisplaystr[0..gdisplaystrlen]); gdisplaystr[0] = '-'; gdisplaystrlen += 1; } displaystr(gdisplaystr[0..gdisplaystrlen], RGB_RESULT); } }, 3, 7, 11, 15 => { gbinop = key[0]; gax = std.fmt.parseFloat(f64, gdisplaystr[0..gdisplaystrlen]) catch 0; gfisnumberentering = false; }, 19 => { if (gfisnumberentering) { gbx = std.fmt.parseFloat(f64, gdisplaystr[0..gdisplaystrlen]) catch 0; gfisnumberentering = false; } switch (gbinop) { '+' => gax += gbx, '-' => gax -= gbx, '*' => gax *= gbx, '/' => gax /= gbx, else => {}, } const result_slice = try std.fmt.bufPrint(&gdisplaystr, "{d}", .{gax}); gdisplaystrlen = result_slice.len; displaystr(gdisplaystr[0..gdisplaystrlen], RGB_RESULT); }, else => { if (!gfisnumberentering) { gdisplaystr[0] = key[0]; gdisplaystrlen = 1; gfisnumberentering = true; } else if (gdisplaystrlen < MAX_DISPLAYSTR_LEN - 1) { gdisplaystr[gdisplaystrlen] = key[0]; gdisplaystrlen += 1; } displaystr(gdisplaystr[0..gdisplaystrlen], RGB_RESULT); }, } } const KeyMap = struct { c: u8, idx: usize }; const key_mapping = [_]KeyMap{ .{ .c = 'o', .idx = 0 }, .{ .c = 'O', .idx = 0 }, .{ .c = 'c', .idx = 1 }, .{ .c = 'C', .idx = 1 }, .{ .c = 8, .idx = 2 }, .{ .c = '*', .idx = 3 }, .{ .c = '7', .idx = 4 }, .{ .c = '8', .idx = 5 }, .{ .c = '9', .idx = 6 }, .{ .c = '/', .idx = 7 }, .{ .c = '4', .idx = 8 }, .{ .c = '5', .idx = 9 }, .{ .c = '6', .idx = 10 }, .{ .c = '+', .idx = 11 }, .{ .c = '1', .idx = 12 }, .{ .c = '2', .idx = 13 }, .{ .c = '3', .idx = 14 }, .{ .c = '-', .idx = 15 }, .{ .c = '0', .idx = 16 }, .{ .c = '.', .idx = 17 }, .{ .c = 's', .idx = 18 }, .{ .c = '=', .idx = 19 }, }; pub fn main() !void { const stdin = std.io.getStdIn().reader(); var line_buf: [4096]u8 = undefined; while (gfnotexit) { const line = try stdin.readUntilDelimiterOrEof(&line_buf, '\n') orelse break; const trimmed = std.mem.trim(u8, line, "\r \n"); if (trimmed.len == 0) continue; if (std.mem.eql(u8, trimmed, "drawapp")) { myputs("Fo 0 611 3\nFo 1 400 3\nFo 2 140 3\nF 0 16777215\nFR 0 0 0 10000 10000\nZ \nB 0 12632256\n"); var i: usize = 0; while (i < 20) : (i += 1) { const r = i / CCOLS; const c = i % CCOLS; var b2: [128]u8 = undefined; myputs(try std.fmt.bufPrint(&b2, "F 0 10526880\nFR 0 {d} {d} {d} {d}\n", .{ c * 2000 + 220, r * 1200 + 1220, c * 2000 + 2080, r * 1200 + 2280 })); myputs(try std.fmt.bufPrint(&b2, "F 0 986895\nR 0 {d} {d} {d} {d}\n", .{ c * 2000 + 200, r * 1200 + 1200, c * 2000 + 2100, r * 1200 + 2300 })); myputs(try std.fmt.bufPrint(&b2, "F 0 {d}\nC$ 0 {d} {d} \"{s}\"\n", .{ kbdcolor[i], c * 2000 + 1100, r * 1200 + 1700, kbdstr[i] })); } myputs("F 1 255\nC$ 1 5000 800 \"zig cloud calc app\"\nZ \n"); myputs("F 2 255\n" ); myputs("C$ 2 5000 7200\"Corrie Zucker Technologies LLC.\"\n"); myputs("C$ 2 5000 7340\"d/b/a CZ Technologies\"\n"); displaystr("0", RGB_RESULT); } else if (std.mem.eql(u8, trimmed, "QUERY_REMOTE")) { myputs("Rz 1200 900\n"); } else { var it = std.mem.tokenizeAny(u8, trimmed, " "); const btn_s = it.next() orelse continue; const x_s = it.next() orelse continue; const y_s = it.next() orelse continue; const btn = std.fmt.parseInt(i32, btn_s, 10) catch 0; const x = std.fmt.parseInt(i32, x_s, 10) catch 0; const y = std.fmt.parseInt(i32, y_s, 10) catch 0; if (x >= 0) { const row_i: i32 = @divFloor(y - 1200, 1200); const col_i: i32 = @divFloor(x - 200, 2000); if (row_i >= 0 and col_i >= 0) { const row: usize = @intCast(row_i); const col: usize = @intCast(col_i); if (row < CROWS and col < CCOLS) try keyToExe(row * CCOLS + col); } } else if (x == -2) { const key_char = @as(u8, @intCast(btn)); for (key_mapping) |map| { if (map.c == key_char) { try keyToExe(map.idx); break; } } } else if (x == -4) { var b2: [128]u8 = undefined; myputs(try std.fmt.bufPrint(&b2, "Rz {d} {d}\n", .{ btn, y })); } else if (x == -5) { var b2: [128]u8 = undefined; myputs(try std.fmt.bufPrint(&b2, "Mv {d} {d}\n", .{ btn, y })); } } } }