-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathport_soft_bleed.c
More file actions
578 lines (482 loc) · 15.2 KB
/
Copy pathport_soft_bleed.c
File metadata and controls
578 lines (482 loc) · 15.2 KB
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <devices/keyboard.h>
#include <devices/mouse.h>
#include <devices/hpet.h>
#include <fcntl.h>
#include <fs/file.h>
#include <graphics/display.h>
#include <sys/ioctl.h>
#include <syscalls/close.h>
#include <syscalls/open.h>
#include <syscalls/read.h>
#include <syscalls/ioctl.h>
#include "quake2.h"
#include "client/keys.h"
#include "ref_soft/r_local.h"
void Cbuf_AddText(char *text);
void Cbuf_Execute(void);
typedef struct {
uint8_t b;
uint8_t g;
uint8_t r;
uint8_t a;
} color_t;
#define KEYQUEUE_SIZE 64
#define HPET_DEVICE_PATH "/dev/hpet"
#define MOUSE_DEVICE_PATH "/dev/mouse"
#define TTY_ECHO (1 << 1)
#define TTY_CANNONICAL (1 << 2)
#define TTY_NONBLOCK (1 << 4)
#define TTY_FLAGS_QUAKE (TTY_NONBLOCK)
static color_t g_colors[256];
static uint16_t g_palette_565[256];
static uint32_t g_palette_xrgb8888[256];
static uint8_t g_palette_bgr24[256][3];
static int g_fb_fd = -1;
static uint8_t *g_fb_backbuffer = NULL;
static struct fb_info g_fb_info;
static uint64_t g_fb_pitch_bytes = 0;
static int g_tty_fd = -1;
static int g_mouse_fd = -1;
static int g_width = 640;
static int g_height = 480;
static char *g_screen_memory = NULL;
static unsigned short g_key_queue[KEYQUEUE_SIZE];
static unsigned int g_key_queue_write_index = 0;
static unsigned int g_key_queue_read_index = 0;
static int g_mouse_captured = 0;
static int g_mouse_dx = 0;
static int g_mouse_dy = 0;
static uint8_t g_old_mouse_buttons = 0;
static int g_hpet_fd = -2;
static uint64_t hpet_now_femtoseconds(void) {
uint64_t now = 0;
if (g_hpet_fd == -2)
g_hpet_fd = _open(HPET_DEVICE_PATH, O_RDONLY);
if (g_hpet_fd < 0)
return 0;
if (_ioctl(g_hpet_fd, HPET_IOCTL_GET_FEMTOSECONDS, &now) == 0)
return now;
if (_read(g_hpet_fd, &now, sizeof(now)) == (long)sizeof(now))
return now;
return 0;
}
static void close_fd_if_open(int *fd) {
if (*fd >= 0)
{
_close(*fd);
*fd = -1;
}
}
static void apply_new_keybinds(void) {
Cbuf_AddText("set freelook 1\n");
Cbuf_AddText("set lookspring 0\n");
Cbuf_AddText("set in_mouse 1\n");
Cbuf_AddText("bind w \"+forward\"\n");
Cbuf_AddText("bind s \"+back\"\n");
Cbuf_AddText("bind a \"+moveleft\"\n");
Cbuf_AddText("bind d \"+moveright\"\n");
Cbuf_AddText("bind UPARROW \"+lookup\"\n");
Cbuf_AddText("bind DOWNARROW \"+lookdown\"\n");
Cbuf_AddText("bind LEFTARROW \"+left\"\n");
Cbuf_AddText("bind RIGHTARROW \"+right\"\n");
Cbuf_AddText("bind CTRL \"+attack\"\n");
Cbuf_AddText("bind MOUSE1 \"+attack\"\n");
Cbuf_AddText("bind MOUSE2 \"+mlook\"\n");
Cbuf_AddText("bind SPACE \"+use\"\n");
Cbuf_AddText("bind SHIFT \"+speed\"\n");
Cbuf_AddText("bind ALT \"+strafe\"\n");
Cbuf_AddText("bind e \"invuse\"\n");
Cbuf_Execute();
}
static unsigned char map_key(const keyboard_event_t *ev) {
switch (ev->keycode)
{
case Escape: return K_ESCAPE;
case CarriageReturn: return K_ENTER;
case LineFeed: return K_ENTER;
case FileSeparator: return K_ENTER;
case HorizontalTab: return K_TAB;
case Backspace: return K_BACKSPACE;
case ArrowLeft: return K_LEFTARROW;
case ArrowRight: return K_RIGHTARROW;
case ArrowUp: return K_UPARROW;
case ArrowDown: return K_DOWNARROW;
case Home: return K_HOME;
case End: return K_END;
case Insert: return K_INS;
case Delete: return K_DEL;
case PageUp: return K_PGUP;
case PageDown: return K_PGDN;
case F1: return K_F1;
case F2: return K_F2;
case F3: return K_F3;
case F4: return K_F4;
case F5: return K_F5;
case F6: return K_F6;
case F7: return K_F7;
case F8: return K_F8;
case F9: return K_F9;
case F10: return K_F10;
case F11: return K_F11;
case F12: return K_F12;
case GroupSeparator: return K_CTRL;
case 42: return K_SHIFT; /* left shift */
case 54: return K_SHIFT;
case 56: return K_ALT;
default: break;
}
{
char ascii = tty_key_to_ascii(ev);
if (ascii >= 'A' && ascii <= 'Z')
ascii = (char)(ascii + ('a' - 'A'));
if (ascii > 0)
return (unsigned char)ascii;
}
return 0;
}
static void add_key_to_queue(int pressed, unsigned char key) {
unsigned int next_write_index = (g_key_queue_write_index + 1U) % KEYQUEUE_SIZE;
unsigned short key_data = (unsigned short)((pressed << 8) | key);
if (next_write_index == g_key_queue_read_index)
{
/* Keep the newest event when the queue overflows. */
g_key_queue_read_index = (g_key_queue_read_index + 1U) % KEYQUEUE_SIZE;
}
g_key_queue[g_key_queue_write_index] = key_data;
g_key_queue_write_index = next_write_index;
}
static void setup_video_buffer(void) {
if (g_screen_memory)
{
free(g_screen_memory);
g_screen_memory = NULL;
}
g_screen_memory = (char *)malloc((size_t)g_width * (size_t)g_height);
if (!g_screen_memory)
Sys_Error("out of memory allocating software framebuffer\n");
vid.rowbytes = g_width;
vid.buffer = (pixel_t *)g_screen_memory;
}
static void handle_keyboard_input(void) {
keyboard_event_t ev;
long read_count;
for (;;)
{
read_count = _read(0, &ev, sizeof(ev));
if (read_count != (long)sizeof(ev))
break;
{
unsigned char key = map_key(&ev);
if (key)
add_key_to_queue(ev.action == KEY_DOWN ? 1 : 0, key);
}
}
}
static void handle_mouse_input(void) {
mouse_event_t ev;
long read_count;
if (!g_mouse_captured || g_mouse_fd < 0)
return;
for (;;)
{
read_count = _read(g_mouse_fd, &ev, sizeof(ev));
if (read_count != (long)sizeof(ev))
break;
g_mouse_dx += (int)ev.dx;
g_mouse_dy += (int)ev.dy;
{
static const uint8_t button_masks[] = {
MOUSE_BTN_LEFT,
MOUSE_BTN_RIGHT,
MOUSE_BTN_MIDDLE
};
uint8_t changed = (uint8_t)(ev.buttons ^ g_old_mouse_buttons);
int i;
for (i = 0; i < (int)(sizeof(button_masks) / sizeof(button_masks[0])); ++i)
{
if (changed & button_masks[i])
Quake2_SendKey(K_MOUSE1 + i, (ev.buttons & button_masks[i]) ? true : false);
}
g_old_mouse_buttons = ev.buttons;
}
}
}
rserr_t SWimp_SetMode(int *pwidth, int *pheight, int mode, qboolean fullscreen) {
int width = 0;
int height = 0;
(void)fullscreen;
ri.Con_Printf(PRINT_ALL, "Initializing software display\n");
ri.Con_Printf(PRINT_ALL, "setting mode %d:", mode);
if (!ri.Vid_GetModeInfo(&width, &height, mode))
{
ri.Con_Printf(PRINT_ALL, " invalid mode\n");
return rserr_invalid_mode;
}
/* vbox is sensitive to larger software modes on a smaller boot FB. */
if (g_fb_info.width && g_fb_info.height &&
(width > (int)g_fb_info.width || height > (int)g_fb_info.height))
{
ri.Con_Printf(PRINT_ALL, " mode %dx%d exceeds framebuffer %llux%llu\n",
width, height,
(unsigned long long)g_fb_info.width,
(unsigned long long)g_fb_info.height);
return rserr_invalid_mode;
}
ri.Con_Printf(PRINT_ALL, " %d %d\n", width, height);
g_width = width;
g_height = height;
*pwidth = width;
*pheight = height;
setup_video_buffer();
ri.Vid_NewWindow(width, height);
return rserr_ok;
}
void SWimp_Shutdown(void) {
close_fd_if_open(&g_mouse_fd);
close_fd_if_open(&g_tty_fd);
close_fd_if_open(&g_fb_fd);
if (g_screen_memory)
{
free(g_screen_memory);
g_screen_memory = NULL;
}
if (g_fb_backbuffer)
{
free(g_fb_backbuffer);
g_fb_backbuffer = NULL;
}
g_fb_pitch_bytes = 0;
memset(&g_fb_info, 0, sizeof(g_fb_info));
g_key_queue_read_index = 0;
g_key_queue_write_index = 0;
g_mouse_dx = 0;
g_mouse_dy = 0;
g_old_mouse_buttons = 0;
g_mouse_captured = 0;
}
int SWimp_Init(void *hInstance, void *wndProc) {
uint32_t tty_flags = TTY_FLAGS_QUAKE;
(void)hInstance;
(void)wndProc;
g_fb_fd = _open("/dev/fb0", O_RDWR);
if (g_fb_fd < 0)
{
Sys_Error("failed to open /dev/fb0\n");
goto fail;
}
if (_ioctl(g_fb_fd, FB_IOC_GET_INFO, &g_fb_info) < 0)
{
Sys_Error("failed FB_IOC_GET_INFO on /dev/fb0\n");
goto fail;
}
g_fb_pitch_bytes = g_fb_info.pitch;
g_fb_backbuffer = (uint8_t *)malloc((size_t)g_fb_info.height * (size_t)g_fb_pitch_bytes);
if (!g_fb_backbuffer)
{
Sys_Error("failed to allocate framebuffer backbuffer\n");
goto fail;
}
memset(g_fb_backbuffer, 0, (size_t)g_fb_info.height * (size_t)g_fb_pitch_bytes);
_ioctl(0, TTY_IOCTL_SET_FLAGS, &tty_flags);
g_tty_fd = _open("/dev/tty0", O_RDWR);
if (g_tty_fd >= 0)
_ioctl(g_tty_fd, TTY_IOCTL_SET_FLAGS, &tty_flags);
g_mouse_fd = _open(MOUSE_DEVICE_PATH, O_RDONLY);
setup_video_buffer();
return true;
fail:
SWimp_Shutdown();
return false;
}
void SWimp_SetPalette(const unsigned char *palette) {
int i;
for (i = 0; i < 256; ++i)
{
g_colors[i].r = *palette++;
g_colors[i].g = *palette++;
g_colors[i].b = *palette++;
g_colors[i].a = 255;
palette++;
g_palette_565[i] =
(uint16_t)(((uint16_t)(g_colors[i].r >> 3) << 11) |
((uint16_t)(g_colors[i].g >> 2) << 5) |
((uint16_t)(g_colors[i].b >> 3)));
g_palette_xrgb8888[i] =
0xFF000000u |
((uint32_t)g_colors[i].r << 16) |
((uint32_t)g_colors[i].g << 8) |
(uint32_t)g_colors[i].b;
g_palette_bgr24[i][0] = g_colors[i].b;
g_palette_bgr24[i][1] = g_colors[i].g;
g_palette_bgr24[i][2] = g_colors[i].r;
}
}
void SWimp_BeginFrame(float camera_seperation) {
(void)camera_seperation;
}
void SWimp_EndFrame(void) {
int x;
int y;
int fbw;
int fbh;
int copy_width;
int copy_height;
int ox;
int oy;
int bytes_per_pixel;
int max_pitch_pixels;
const uint8_t *src_base;
qboolean needs_clear;
if (!g_fb_backbuffer || !g_screen_memory)
return;
fbw = (int)g_fb_info.width;
fbh = (int)g_fb_info.height;
bytes_per_pixel = (int)(g_fb_info.bpp / 8);
if (bytes_per_pixel <= 0)
bytes_per_pixel = 4;
max_pitch_pixels = (int)(g_fb_pitch_bytes / (uint64_t)bytes_per_pixel);
if (max_pitch_pixels < fbw)
fbw = max_pitch_pixels;
copy_width = g_width < fbw ? g_width : fbw;
copy_height = g_height < fbh ? g_height : fbh;
ox = (fbw > copy_width) ? (fbw - copy_width) / 2 : 0;
oy = (fbh > copy_height) ? (fbh - copy_height) / 2 : 0;
src_base = (const uint8_t *)g_screen_memory;
needs_clear = (copy_width != fbw || copy_height != fbh) ? true : false;
if (needs_clear)
memset(g_fb_backbuffer, 0, (size_t)fbh * (size_t)g_fb_pitch_bytes);
if (g_fb_info.bpp == 16)
{
for (y = 0; y < copy_height; ++y)
{
const uint8_t *src = src_base + (size_t)y * (size_t)g_width;
uint16_t *dst = (uint16_t *)(g_fb_backbuffer + (size_t)(y + oy) * (size_t)g_fb_pitch_bytes);
dst += ox;
for (x = 0; x < copy_width; ++x)
{
dst[x] = g_palette_565[src[x]];
}
}
}
else if (g_fb_info.bpp == 24)
{
for (y = 0; y < copy_height; ++y)
{
const uint8_t *src = src_base + (size_t)y * (size_t)g_width;
uint8_t *dst = g_fb_backbuffer + (size_t)(y + oy) * (size_t)g_fb_pitch_bytes + (size_t)ox * 3u;
for (x = 0; x < copy_width; ++x)
{
const uint8_t *bgr = g_palette_bgr24[src[x]];
dst[0] = bgr[0];
dst[1] = bgr[1];
dst[2] = bgr[2];
dst += 3;
}
}
}
else
{
for (y = 0; y < copy_height; ++y)
{
const uint8_t *src = src_base + (size_t)y * (size_t)g_width;
uint32_t *dst = (uint32_t *)(g_fb_backbuffer + (size_t)(y + oy) * (size_t)g_fb_pitch_bytes);
dst += ox;
for (x = 0; x < copy_width; ++x)
{
dst[x] = g_palette_xrgb8888[src[x]];
}
}
}
if (g_fb_fd >= 0)
_ioctl(g_fb_fd, FB_IOC_FLIP, g_fb_backbuffer);
handle_keyboard_input();
handle_mouse_input();
}
void SWimp_AppActivate(qboolean active) {
(void)active;
}
static void handle_input(void) {
handle_mouse_input();
while (g_key_queue_read_index != g_key_queue_write_index) {
unsigned short key_data = g_key_queue[g_key_queue_read_index];
int pressed = key_data >> 8;
int quake_key = key_data & 0xFF;
g_key_queue_read_index = (g_key_queue_read_index + 1U) % KEYQUEUE_SIZE;
Quake2_SendKey(quake_key, pressed ? true : false);
}
}
int QG_Milliseconds(void) {
static uint64_t start_fs = 0;
static int fallback_ms = 0;
uint64_t now_fs = hpet_now_femtoseconds();
if (!now_fs)
return ++fallback_ms;
if (!start_fs || now_fs < start_fs)
start_fs = now_fs;
return (int)((now_fs - start_fs) / femtosecondsPerMillisecond);
}
void QG_GetMouseDiff(int *dx, int *dy) {
handle_mouse_input();
*dx = g_mouse_dx;
*dy = g_mouse_dy;
g_mouse_dx = 0;
g_mouse_dy = 0;
}
void QG_CaptureMouse(void) {
g_mouse_captured = 1;
}
void QG_ReleaseMouse(void) {
g_mouse_captured = 0;
}
int main(int argc, char **argv) {
int i;
int time;
int oldtime;
int newtime;
int has_basedir = 0;
int basedir_index = -1;
char *argv_ext[128];
if (argc + 2 < (int)(sizeof(argv_ext) / sizeof(argv_ext[0])))
{
for (i = 1; i < argc; ++i)
{
if (!strcmp(argv[i], "-basedir"))
{
has_basedir = 1;
basedir_index = i + 1;
break;
}
}
if (!has_basedir)
{
for (i = 0; i < argc; ++i)
argv_ext[i] = argv[i];
argv_ext[argc] = "-basedir";
argv_ext[argc + 1] = "/initrd";
argv_ext[argc + 2] = NULL;
argc += 2;
argv = argv_ext;
}
else if (basedir_index > 0 && basedir_index < argc) {
argv[basedir_index] = "/initrd";
}
}
Quake2_Init(argc, argv);
apply_new_keybinds();
oldtime = Quake2_Milliseconds();
while (1)
{
handle_input();
do {
newtime = Quake2_Milliseconds();
time = newtime - oldtime;
} while (time < 1);
Quake2_Frame(time);
oldtime = newtime;
}
return 0;
}