-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
422 lines (397 loc) · 15.1 KB
/
main.cpp
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
* pipette
* main.cpp
* created 11-13-21
-------------------
(C) Luminoso 2021/MIT License
* */
#include <iostream>
#include "include/kissnet/kissnet.hpp"
#undef DrawText
#undef DrawTextEx
#undef LoadImage
#undef RLCloseWindow
#undef ShowCursor
#undef Rectangle
#include "include/raylib/raylib.h"
#include "gemtxt.h"
#include <vector>
#include <thread>
#include <fstream>
#include <chrono>
#include <filesystem>
#include "portable_endian.h"
namespace kn = kissnet;
namespace gm = gemtext;
using namespace std::chrono_literals;
#define VERSION "21D319-DEV"
#define SCREEN_WIDTH (800)
#define SCREEN_HEIGHT (450)
#define WINDOW_TITLE "Pipette - the tiny piper browser"
#define MAX_INPUT_CHARS 64
void browse(std::vector<gm::GemLine> *result, std::string url, int *status, int *contentstatus) {
/* set up key strings */
std::vector<std::string> parts = util::split(url, "/");
std::string host = "";
if (parts[0].find(":") != std::string::npos) {
host = parts[0];
} else {
host = parts[0] + ":60";
}
//clean out the first part
parts.erase(parts.begin());
//pull together the URI
std::string uri = "/" + util::join(parts, '/');
std::cout << host << " path " << uri << std::endl;
//== Special case: Pipette URLs
if (host == "$pipette:60"){
if (uri == "/attribution"){
*status = 0;
result->clear();
*result = gm::attributionTextGen();
return;
} else if (uri == "/about"){
*status = 0;
result->clear();
result->push_back(
gm::GemLine{
"Pipette " +std::string(VERSION), 0,
""
}
);
return;
} else if (uri == "/home"){
*status = 0;
result->clear();
*result = gm::introTextGen();
return;
}
}
/* socket time! */
//== Send
try {
kn::tcp_socket tcpsocket((kn::endpoint(host)));
if (!tcpsocket.is_valid()) {
std::cout << "Invalid Socket!" << std::endl;
*status = 3;
return;
}
kn::socket_status sstat = tcpsocket.connect();
std::vector<char> request;
short len = (short) uri.length();
request.push_back(char(len & 0xff));
request.push_back(char((len >> 8) & 0xff));
std::vector<char> uribytes(uri.begin(), uri.end());
request.insert(request.end(), uribytes.begin(), uribytes.end());
std::vector<std::byte> brequest;
for (char c:request) {
brequest.push_back(std::byte(c));
}
//abuse the C++ spec requiring vecs being stored contigously to convert
//god i hate all the casts in this mess.
tcpsocket.send(&brequest[0], brequest.size());
//== Recieve
bool continue_rec = true;
bool has_parsed_size = false;
std::byte static_buffer[16384];
int data_size = 0;
while(continue_rec) {
auto [size, valid] = tcpsocket.recv(static_buffer,sizeof(static_buffer));
data_size += size;
if (valid) {
//buffer magik:tm:
if (!has_parsed_size && data_size >= 9){
//behold, cursedness!
uint64_t contentlen = (uint64_t)(static_buffer[1]) |
(uint64_t)(static_buffer[2]) |
(uint64_t)(static_buffer[3]) |
(uint64_t)(static_buffer[4]) |
(uint64_t)(static_buffer[5]) |
(uint64_t)(static_buffer[6]) |
(uint64_t)(static_buffer[7]) |
(uint64_t)(static_buffer[8]);
std::cout << "content len: " << contentlen << "\n";
//now do the array resizing (ugh)
//kn::buffer<9+contentlen> new_buffer;
has_parsed_size = true;
}
//find out if we're done
if (valid.value == kn::socket_status::cleanly_disconnected) {
continue_rec = false;
}
} else {
continue_rec = false;
}
}
*status = 2;
int contenttype = (int) static_buffer[0];
*contentstatus = contenttype;
std::string res = "";
bool error = false;
bool warn = false;
for (int i = 9; i < data_size; i++) {
if (sizeof(static_buffer)>i) {
res += (char) static_buffer[i];
} else{
warn = true;
}
}
switch (contenttype) {
case 0x0:
result->clear();
*result = gm::parse(res, true);
break;
case 0x01:
result->clear();
*result = gm::parse(res, false);
break;
case 0x10: {
result->clear();
result->push_back(
gm::GemLine{
"Saving " + parts[parts.size() - 1] + " to ./downloads/" + parts[parts.size() - 1], 1,
""
}
);
result->push_back(
gm::GemLine{
std::to_string(res.size()) + " bytes.", 6, ""
}
);
*status = 4;
std::filesystem::path path{"./downloads"};
path /= parts[parts.size() - 1];
std::filesystem::create_directories(path.parent_path());
std::ofstream file(path);
file << res;
file.close();
}
break;
case 0x20: {
*status = 1;
std::thread thread(browse, result, res, status, contentstatus);
thread.detach();
}
break;
case 0x22:
result->clear();
result->push_back(
gm::GemLine{
"0x22 Resource Not Found", 1, ""
}
);
error = true;
break;
default:
result->clear();
result->push_back(
gm::GemLine{
"Unknown Content Type " + std::to_string(contenttype), 1, ""
}
);
break;
}
if (error) {
*status = 3;
} else if (warn){
*status = 5;
} else {
*status = 0;
}
} catch (const std::exception &e) {
*status = 3;
result->clear();
result->push_back(
gm::GemLine{
"Error Encountered: " + std::string(e.what()), 0, ""
}
);
return;
}
}
int main(void) {
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, WINDOW_TITLE);
char target_url[MAX_INPUT_CHARS + 1] = "\0";
int letterCount = 0;
int framesCounter = 0;
int status = 0;
int fontsize = 15;
int contentstatus = 0;
int scrollbarOffset = 0;
bool debug = false;
int scrollSpeed = 4;
Font font = LoadFontEx("font.ttf", 32, 0, 250);
// int rendermode = 1;
std::vector<gm::GemLine> gemlines = gm::introTextGen();
SetTargetFPS(60);
while (!WindowShouldClose()) {
int key = GetCharPressed();
while (key > 0) {
if ((key >= 32) && (key <= 125) && (letterCount < MAX_INPUT_CHARS)) {
target_url[letterCount] = (char) key;
target_url[letterCount + 1] = '\0';
letterCount++;
}
key = GetCharPressed();
}
if (IsKeyDown(KEY_BACKSPACE) && framesCounter % 4 == 0) {
letterCount--;
if (letterCount < 0) letterCount = 0;
target_url[letterCount] = '\0';
}
if (IsKeyPressed(KEY_ENTER) && status != 1) {
status = 1;
scrollbarOffset = 0;
std::thread thread(browse, &gemlines, std::string(target_url), &status, &contentstatus);
thread.detach();
}
scrollbarOffset -= (GetMouseWheelMove() * scrollSpeed);
if (scrollbarOffset < 0) {
scrollbarOffset = 0;
}
if (IsKeyDown(KEY_UP)) {
fontsize += 1;
}
if (IsKeyDown(KEY_DOWN)) {
fontsize -= 1;
}
if (IsKeyPressed(KEY_LEFT_BRACKET)) { debug = !debug; }
if (IsKeyPressed(KEY_RIGHT_BRACKET)) {
status = 1;
std::thread thread(browse, &gemlines, std::string("localhost/big.txt"), &status, &contentstatus);
thread.detach();
}
framesCounter++;
//draw
BeginDrawing();
ClearBackground(RAYWHITE);
//StatusBar
DrawTextEx(font, "piper://", Vector2{5, 5}, 20, 2, GRAY);
DrawTextEx(font, target_url, Vector2{100, 5}, 20, 2, BLUE);
switch (status) {
case 0:
DrawTextEx(font, "idle", Vector2{(float)GetScreenWidth() - 50, 5}, 20, 2, BLACK);
break;
case 1:
DrawTextEx(font, "load", Vector2{(float)GetScreenWidth() - 50, 5}, 20, 2, BLACK);
break;
case 2:
DrawTextEx(font, "parse", Vector2{(float)GetScreenWidth() - 70, 5}, 20, 2, BLACK);
break;
case 3:
DrawTextEx(font, "error", Vector2{(float)GetScreenWidth() - 70, 5}, 20, 2, RED);
break;
case 4:
DrawTextEx(font, "save", Vector2{(float)GetScreenWidth() - 70, 5}, 20, 2, BLACK);
break;
case 5:
//overflow!
DrawTextEx(font, "ovfl!", Vector2{(float)GetScreenWidth() - 70, 5}, 20, 2,ORANGE);
break;
}
DrawLine(0, 25, GetScreenWidth(), 25, GRAY);
//Main
int y = 35;
//hacky way to implement scrolling
y -= scrollbarOffset * fontsize;
for (gm::GemLine line:gemlines) {
bool render = true;
if (y < 25) {
render = false;
}
if (debug && render){
DrawTextEx(font, (std::to_string(line.rendertype)+" y:"+std::to_string(y)).c_str(), Vector2{(float)GetScreenWidth()-80, (float) y}, fontsize, 2, PURPLE);
}
switch (line.rendertype) {
case 0:
if (render) {
DrawTextEx(font, line.content.c_str(), Vector2{5, (float) y}, fontsize, 2, BLACK);
}
y += fontsize + 5;
break;
case 1:
if (render) {
DrawTextEx(font, line.content.c_str(), Vector2{5, (float) y}, fontsize + 8, 2, BLACK);
}
y += fontsize + 13;
break;
case 2:
if (render) {
DrawTextEx(font, line.content.c_str(), Vector2{5, (float) y}, fontsize + 4, 2, BLACK);
}
y += fontsize + 13;
break;
case 3:
if (render) {
DrawTextEx(font, line.content.c_str(), Vector2{5, (float) y}, fontsize + 2, 2, BLACK);
}
y += fontsize + 7;
break;
case 4:
if (render) {
DrawTextEx(font, line.content.c_str(), Vector2{5, (float) y}, fontsize, 2, GRAY);
}
y += fontsize + 5;
break;
case 6:
if (render) {
DrawRectangle(5, y + 1, 3, fontsize - 2, GRAY);
DrawTextEx(font, line.content.c_str(), Vector2{10, (float) y}, fontsize, 2, GRAY);
}
y += fontsize + 5;
break;
case 7:
if (render) {
float x1 = 5;
float y1 = y;
float x2 = 5 + (MeasureText(line.content.c_str(), fontsize) * 1.5);
float y2 = y + fontsize + 3;
Vector2 mousePos = GetMousePosition();
bool touching = (mousePos.x > x1 && mousePos.x < x2 && mousePos.y > y1 && mousePos.y < y2);
if (debug) {
if (touching) {
DrawRectangle(5, y, MeasureText(line.content.c_str(), fontsize) * 1.5, fontsize + 3,
GREEN);
} else {
DrawRectangle(5, y, MeasureText(line.content.c_str(), fontsize) * 1.5, fontsize + 3,
YELLOW);
}
DrawTextEx(font, ("[Dbg] " + line.metadata).c_str(), Vector2{5, (float) y - 10}, 10, 2,
PURPLE);
}
if (touching && IsMouseButtonDown(MouseButton::MOUSE_LEFT_BUTTON)) {
std::string clean = line.metadata.replace(line.metadata.find("piper://"),
sizeof("piper://") - 1, "");
std::cout << clean << std::endl;
strcpy(target_url, clean.c_str());
status = 1;
scrollbarOffset = 0;
std::thread thread(browse, &gemlines, clean, &status, &contentstatus);
thread.detach();
}
DrawTextEx(font, line.content.c_str(), Vector2{5, (float) y}, fontsize, 2, SKYBLUE);
}
y += fontsize + 5;
}
}
if (gemlines.size() * 15 > GetScreenHeight()) {
int estimatesize = gemlines.size() * 15;
DrawRectangle(GetScreenWidth() - 10, 35 + scrollbarOffset, 10, (estimatesize / GetScreenHeight()) * 3, GRAY);
}
if (scrollbarOffset > GetScreenHeight()) {
int estimatesize = gemlines.size() * 15;
DrawRectangle(GetScreenWidth() - 10, GetScreenHeight()-((estimatesize / GetScreenHeight()) * 3), 10, (estimatesize / GetScreenHeight()) * 3, ORANGE);
DrawTextEx(font, "!", Vector2{(float)GetScreenWidth() - 8, (float)GetScreenHeight() - 15}, 14, 2, WHITE);
}
if (debug) {
std::string dbg =
"ContentType: " + std::to_string(contentstatus) + " FPS: " + std::to_string(GetFPS()) +
" (of target 60) URLBuffer: " + std::to_string(letterCount) + "(of max 64) / Pipette " + VERSION;
DrawTextEx(font, dbg.c_str(), Vector2{3, (float)GetScreenHeight() - 20}, 12, 2, PURPLE);
}
EndDrawing();
}
RLCloseWindow();
return 0;
}