forked from Arnie97/neko-notepad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
261 lines (228 loc) · 5.72 KB
/
main.c
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
/* A simple user interface for this project
Copyright (C) 2016 Arnie97
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <saturn.h>
#include "satstr.h"
#include <hpconio.h>
#include <hpstring.h>
#include "hp39kbd.h"
#include "stack.h"
#include "display.h"
#include "main.h"
static unsigned font_id;
int
main(void)
{
clear_screen();
if (ROM->magic != 0xC0DEBA5E) {
const char *rom_not_found = (
"\x08\xa1\x40\x01\x82\x12\x08\x00\x20\x80\x00"
"\xd0\xa7\xcf\xcf\x3f\x7f\x08\x10\xfc\xf3\x87"
"\x00\x51\x2a\x41\xa0\x12\x7f\xfe\x04\x92\x80"
"\xd8\x27\x07\x01\x82\x7f\x49\x44\xf8\xf0\x87"
"\x90\xa2\xea\xdf\x3f\x04\x49\x28\x40\x50\x81"
"\x90\x43\x80\x02\x09\x2b\x7f\x10\xfc\xf3\x87"
"\xb0\xa2\x4a\x12\x86\x12\x08\x28\x20\x10\x01"
"\x90\xd2\x2b\x9e\x19\x66\x08\xc6\x30\x08\x81"
);
for (int row = 0; row < 8; row++) {
memcpy(
&__display_buf[(row + 18) * BYTES_PER_ROW],
&rom_not_found[row * 11], 11
);
}
} else if (hash(SERIAL_NO) != VALID_HASH) {
size_t msg_len = strlen(ROM->anti_piracy) + strlen(SERIAL_NO);
char *msg = check_ptr(malloc(msg_len));
strcpy(msg, ROM->anti_piracy);
strcat(msg, SERIAL_NO);
SAT_STRING s = {
.begin = msg,
.cursor = msg,
.end = msg + msg_len,
.aligned = TRUE
};
bitmap_blit(&s, ROM->fonts[0]);
} else {
return note_explorer(NULL);
}
for (;;) {
get_key();
}
}
int
event_handler(unsigned row, unsigned col)
{
// [APLET], [HOME], [ON]
if (row == 0 && (col == 7 || col == 4) || row == 6 && col == 6) {
exit(col);
} else {
// wait until the key is released
set_indicator(INDICATOR_WAIT, TRUE);
while (any_key_pressed);
set_indicator(INDICATOR_WAIT, FALSE);
}
// [UP]: 0, [LEFT]: 1, [DOWN]: 2, [RIGHT]: 3
if (5 <= col && col <= 7) {
return (col - 4) * 10 + row;
} else if (3 <= row && row <= 5 && col <= 3) {
return col?
(6 - row) * 3 - col + 1: // number keys
row + 17; // [-]: [UP], [+]: [DOWN]
}
// unhandled keys
return 0;
}
static void
display_title(const char *str)
{
unsigned len = strlen(str) + 2;
extern int __scr_w;
int right = (__scr_w - len) / 2, left = __scr_w - len - right;
clear_screen();
while (left--) {
putchar('\x7f');
}
putchar(' ');
while (*str) {
putchar(*str++);
}
putchar(' ');
while (right--) {
putchar('\x7f');
}
for (int i = 0; i < 6; i++) {
set_indicator(i, FALSE);
}
putchar('\n');
}
static void
display_count(unsigned count, const char *name)
{
putchar(' ');
putchar(' ');
putchar('[');
putchar('0' + count);
putchar(']');
putchar(' ');
while (*name) {
putchar(*name++);
}
}
static void
display_item(unsigned count, SAT_OBJ_DSCR *obj)
{
const char *name = obj->name + (obj->name[0] == '\'');
display_count(count, name);
char buf[7];
utoa(sat_strlen(obj->addr), buf, 10);
for (unsigned i = 26 - strlen(name) - strlen(buf); i > 0; i--) {
putchar(' ');
}
puts(buf);
}
window
note_explorer(SAT_DIR_ENTRY *init)
{
display_title("Neko Notepad");
if (!init) {
__sat_cwd = _sat_find_path("/'notesdir");
if (!__sat_cwd) {
__sat_cwd = __sat_root;
}
init = __sat_cwd->object;
} else {
set_indicator(INDICATOR_LSHIFT, TRUE);
}
unsigned count = 0, entries[9];
SAT_DIR_ENTRY *next_page = NULL;
for (SAT_DIR_ENTRY *entry = init; entry; entry = entry->next) {
SAT_OBJ_DSCR *obj = entry->sat_obj;
if (sat_strlen(obj->addr) < 0) {
continue;
} else if (count == 8) {
next_page = entry;
set_indicator(INDICATOR_RSHIFT, TRUE);
break;
}
count++;
entries[count] = obj->addr;
display_item(count, obj);
}
gotoxy(0, 9);
static NODE *head;
for (;;) {
int key = get_key();
if ((key == 22 || key == 23) && next_page) {
push(&head, next_page);
return note_explorer(next_page); // page down
} else if (key == 20 || key == 21) {
pop(&head);
return note_explorer(pop(&head)); // page up
} else if (key == 31) { // [VIEWS]
return font_config(head? head->data: NULL);
} else if (1 <= key && key <= count) { // numbers
return note_viewer(entries[key], head? head->data: NULL);
}
}
}
window
note_viewer(unsigned sat_addr, SAT_DIR_ENTRY *ref)
{
NODE *head = NULL;
SAT_STRING str = sat_strdup(sat_addr);
goto refresh;
for (;;) {
int key = get_key();
if ((key == 22 || key == 23) && str.cursor != str.end) {
refresh: push(&head, str.cursor);
bitmap_blit(&str, ROM->fonts[font_id]); // page down
set_indicator(INDICATOR_LSHIFT, head->data != str.begin);
} else if (key == 20 || key == 21) {
pop(&head);
str.cursor = pop(&head); // page up
if (!str.cursor) {
str.cursor = str.begin;
}
goto refresh;
} else if (key == 16) { // [SYMB]
while (head) {
pop(&head);
}
return note_explorer(ref); // go back to the list
}
}
}
window
font_config(SAT_DIR_ENTRY *ref)
{
display_title("Select font size");
int count = 0;
while (ROM->fonts[count]) {
char buf[3];
utoa(ROM->fonts[count]->ROWS, buf, 10);
display_count(++count, buf);
puts(" px font");
}
for (;;) {
int key = get_key();
if (1 <= key && key <= count) {
font_id = key - 1;
key = 16;
}
if (key == 16 || key == 31) { // [SYMB], [VIEWS]
return note_explorer(ref); // go back to the list
}
}
}