1 | /* $Id: main.c 324 2024-10-14 17:43:25Z nishi $ */
|
---|
2 |
|
---|
3 | #define SOURCE
|
---|
4 |
|
---|
5 | #include "../config.h"
|
---|
6 |
|
---|
7 | #ifdef __BORLANDC__
|
---|
8 |
|
---|
9 | #pragma resource "gui_bcc.res"
|
---|
10 |
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | #if !defined(_MSC_VER) && !defined(__BORLANDC__)
|
---|
14 | #include <unistd.h>
|
---|
15 | #endif
|
---|
16 | #include <stdio.h>
|
---|
17 | #include <stdbool.h>
|
---|
18 | #include <string.h>
|
---|
19 | #include <signal.h>
|
---|
20 | #include <stdlib.h>
|
---|
21 |
|
---|
22 | #ifndef NO_SSL
|
---|
23 | #include <openssl/opensslv.h>
|
---|
24 | #endif
|
---|
25 |
|
---|
26 | #ifdef __OS2__
|
---|
27 | #include <types.h>
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | #include <cm_log.h>
|
---|
31 | #include <cm_string.h>
|
---|
32 |
|
---|
33 | #include "tw_config.h"
|
---|
34 | #include "tw_server.h"
|
---|
35 | #include "tw_version.h"
|
---|
36 |
|
---|
37 | #if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && !defined(__OS2__) && !defined(__NETWARE__))
|
---|
38 | #include <windows.h>
|
---|
39 | #elif defined(__NETWARE__)
|
---|
40 | #include <nwconio.h>
|
---|
41 | #include <nwthread.h>
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #ifdef _PSP
|
---|
45 | #include <pspkernel.h>
|
---|
46 | #include <pspdebug.h>
|
---|
47 | #include <pspsdk.h>
|
---|
48 | #include <psputility.h>
|
---|
49 | #include <pspctrl.h>
|
---|
50 | #include <pspnet_apctl.h>
|
---|
51 | #include <pspwlan.h>
|
---|
52 |
|
---|
53 | PSP_MODULE_INFO("Tewi HTTPd", PSP_MODULE_USER, 1, 1);
|
---|
54 | PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER);
|
---|
55 |
|
---|
56 | #define printf(...) pspDebugScreenPrintf(__VA_ARGS__)
|
---|
57 | #define STDERR_LOG(...) pspDebugScreenPrintf(__VA_ARGS__)
|
---|
58 | #elif defined(__ps2sdk__)
|
---|
59 | #include <debug.h>
|
---|
60 | #include <iopcontrol.h>
|
---|
61 | #include <sifrpc.h>
|
---|
62 | #include <kernel.h>
|
---|
63 |
|
---|
64 | #define printf(...) scr_printf(__VA_ARGS__)
|
---|
65 | #define STDERR_LOG(...) scr_printf(__VA_ARGS__)
|
---|
66 | #elif defined(__PPU__)
|
---|
67 | #include <rsx/gcm_sys.h>
|
---|
68 | #include <rsx/rsx.h>
|
---|
69 | #include <sysutil/video.h>
|
---|
70 | #include <malloc.h>
|
---|
71 | #include <sys/thread.h>
|
---|
72 | #include <stdarg.h>
|
---|
73 | #include <png.h>
|
---|
74 |
|
---|
75 | #define printf(...) tt_printf(__VA_ARGS__)
|
---|
76 | #define STDERR_LOG(...) tt_printf(__VA_ARGS__)
|
---|
77 | #elif defined(_MSC_VER) || defined(__BORLANDC__) || defined(__USLC__) || defined(REALLY_OLD)
|
---|
78 | #include <stdarg.h>
|
---|
79 |
|
---|
80 | void STDERR_LOG(const char* format, ...) {
|
---|
81 | va_list args;
|
---|
82 | va_start(args, format);
|
---|
83 | vfprintf(stderr, format, args);
|
---|
84 | va_end(args);
|
---|
85 | }
|
---|
86 | #else
|
---|
87 | #define STDERR_LOG(...) fprintf(stderr, __VA_ARGS__)
|
---|
88 | #endif
|
---|
89 |
|
---|
90 | extern bool cm_do_log;
|
---|
91 | extern struct tw_config config;
|
---|
92 | extern FILE* logfile;
|
---|
93 |
|
---|
94 | char tw_server[2048];
|
---|
95 |
|
---|
96 | int startup(int argc, char** argv);
|
---|
97 |
|
---|
98 | #if defined(__MINGW32__) || defined(_MSC_VER) || (defined(__WATCOMC__) && !defined(__OS2__) && !defined(__NETWARE__)) || defined(__BORLANDC__)
|
---|
99 | char* get_registry(const char* main, const char* sub) {
|
---|
100 | DWORD bufsize = 512;
|
---|
101 | HKEY handle;
|
---|
102 | char* value = malloc(513);
|
---|
103 | int err = RegOpenKeyEx(HKEY_LOCAL_MACHINE, main, 0, KEY_QUERY_VALUE, &handle);
|
---|
104 | if(err == ERROR_SUCCESS) {
|
---|
105 | err = RegQueryValueEx(handle, sub, NULL, NULL, value, &bufsize);
|
---|
106 | if(err != ERROR_SUCCESS) {
|
---|
107 | free(value);
|
---|
108 | RegCloseKey(handle);
|
---|
109 | return NULL;
|
---|
110 | }
|
---|
111 | RegCloseKey(handle);
|
---|
112 | return value;
|
---|
113 | } else {
|
---|
114 | free(value);
|
---|
115 | return NULL;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | #endif
|
---|
119 |
|
---|
120 | #ifdef SERVICE
|
---|
121 | SERVICE_STATUS status;
|
---|
122 | SERVICE_STATUS_HANDLE status_handle;
|
---|
123 |
|
---|
124 | void WINAPI servhandler(DWORD control) {
|
---|
125 | switch(control) {
|
---|
126 | case SERVICE_CONTROL_STOP:
|
---|
127 | case SERVICE_CONTROL_SHUTDOWN:
|
---|
128 | status.dwCurrentState = SERVICE_STOP_PENDING;
|
---|
129 | break;
|
---|
130 | }
|
---|
131 | SetServiceStatus(status_handle, &status);
|
---|
132 | }
|
---|
133 |
|
---|
134 | void WINAPI servmain(DWORD argc, LPSTR* argv) {
|
---|
135 | char* path = get_registry("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Tewi HTTPd", "InstallDir");
|
---|
136 | int st;
|
---|
137 | if(path != NULL) {
|
---|
138 | char* lpath = cm_strcat(path, "/logs/tewi.log");
|
---|
139 | logfile = fopen(lpath, "a");
|
---|
140 | free(lpath);
|
---|
141 | free(path);
|
---|
142 | } else {
|
---|
143 | logfile = fopen(PREFIX "/logs/tewi.log", "a");
|
---|
144 | }
|
---|
145 | if(logfile == NULL) logfile = stderr;
|
---|
146 | status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
---|
147 | status.dwCurrentState = SERVICE_START_PENDING;
|
---|
148 | status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
|
---|
149 | status.dwWin32ExitCode = NO_ERROR;
|
---|
150 | status.dwServiceSpecificExitCode = 0;
|
---|
151 | status.dwCheckPoint = 0;
|
---|
152 | status.dwWaitHint = 0;
|
---|
153 | status_handle = RegisterServiceCtrlHandler("Tewi HTTPd", servhandler);
|
---|
154 | if(status_handle == NULL) return;
|
---|
155 | if(SetServiceStatus(status_handle, &status) == 0) return;
|
---|
156 | st = startup(argc, argv);
|
---|
157 | if(st != -1) {
|
---|
158 | status.dwWin32ExitCode = NO_ERROR;
|
---|
159 | status.dwServiceSpecificExitCode = st;
|
---|
160 | status.dwCurrentState = SERVICE_STOPPED;
|
---|
161 | SetServiceStatus(status_handle, &status);
|
---|
162 | return;
|
---|
163 | }
|
---|
164 | status.dwCurrentState = SERVICE_RUNNING;
|
---|
165 | SetServiceStatus(status_handle, &status);
|
---|
166 | tw_server_loop();
|
---|
167 | status.dwCurrentState = SERVICE_STOPPED;
|
---|
168 | SetServiceStatus(status_handle, &status);
|
---|
169 | }
|
---|
170 | #endif
|
---|
171 |
|
---|
172 | int running = 1;
|
---|
173 | #ifdef _PSP
|
---|
174 |
|
---|
175 | int psp_exit_callback(int arg1, int arg2, void* arg3) { running = 0; }
|
---|
176 |
|
---|
177 | int psp_callback_thread(SceSize args, void* argp) {
|
---|
178 | int cid;
|
---|
179 | cid = sceKernelCreateCallback("Exit Call Back", psp_exit_callback, NULL);
|
---|
180 | sceKernelRegisterExitCallback(cid);
|
---|
181 | sceKernelSleepThreadCB();
|
---|
182 | return 0;
|
---|
183 | }
|
---|
184 | #endif
|
---|
185 |
|
---|
186 | #ifdef __PPU__
|
---|
187 | uint32_t depth_pitch;
|
---|
188 | uint32_t depth_offset;
|
---|
189 | uint32_t* depth_buffer;
|
---|
190 |
|
---|
191 | #define CB_SIZE 0x100000
|
---|
192 | #define HOST_SIZE (32 * 1024 * 1024)
|
---|
193 |
|
---|
194 | struct rsx_buffer {
|
---|
195 | int width, height, id;
|
---|
196 | uint32_t* ptr;
|
---|
197 | uint32_t offset;
|
---|
198 | };
|
---|
199 |
|
---|
200 | void wait_rsx(gcmContextData* ctx, uint32_t label) {
|
---|
201 | rsxSetWriteBackendLabel(ctx, GCM_INDEX_TYPE_32B, label);
|
---|
202 |
|
---|
203 | rsxFlushBuffer(ctx);
|
---|
204 |
|
---|
205 | while(*(uint32_t*)gcmGetLabelAddress(GCM_INDEX_TYPE_32B) != label) usleep(50);
|
---|
206 |
|
---|
207 | label++;
|
---|
208 | }
|
---|
209 |
|
---|
210 | void wait_rsx_until_idle(gcmContextData* ctx) {
|
---|
211 | uint32_t label = 1;
|
---|
212 | rsxSetWriteBackendLabel(ctx, GCM_INDEX_TYPE_32B, label);
|
---|
213 | rsxSetWaitLabel(ctx, GCM_INDEX_TYPE_32B, label);
|
---|
214 | label++;
|
---|
215 | wait_rsx(ctx, label);
|
---|
216 | }
|
---|
217 |
|
---|
218 | void get_resolution(int* width, int* height) {
|
---|
219 | videoState state;
|
---|
220 | videoResolution res;
|
---|
221 | if(videoGetState(0, 0, &state) != 0) {
|
---|
222 | return;
|
---|
223 | }
|
---|
224 |
|
---|
225 | if(state.state != 0) {
|
---|
226 | return;
|
---|
227 | }
|
---|
228 |
|
---|
229 | if(videoGetResolution(state.displayMode.resolution, &res) != 0) {
|
---|
230 | return;
|
---|
231 | }
|
---|
232 | *width = res.width;
|
---|
233 | *height = res.height;
|
---|
234 | }
|
---|
235 |
|
---|
236 | void make_buffer(struct rsx_buffer* buffer, int id) {
|
---|
237 | int w, h;
|
---|
238 | get_resolution(&w, &h);
|
---|
239 |
|
---|
240 | buffer->ptr = (uint32_t*)rsxMemalign(64, 4 * w * h);
|
---|
241 | if(buffer->ptr == NULL) return;
|
---|
242 |
|
---|
243 | if(rsxAddressToOffset(buffer->ptr, &buffer->offset) != 0) return;
|
---|
244 |
|
---|
245 | if(gcmSetDisplayBuffer(id, buffer->offset, 4 * w, w, h) != 0) return;
|
---|
246 | buffer->width = w;
|
---|
247 | buffer->height = h;
|
---|
248 | buffer->id = id;
|
---|
249 | }
|
---|
250 |
|
---|
251 | gcmContextData* init_screen(void) {
|
---|
252 | void* host = memalign(1024 * 1024, HOST_SIZE);
|
---|
253 | gcmContextData* ctx = NULL;
|
---|
254 | videoState state;
|
---|
255 | videoConfiguration vconfig;
|
---|
256 | videoResolution res;
|
---|
257 | rsxInit(&ctx, CB_SIZE, HOST_SIZE, host);
|
---|
258 | if(ctx == NULL) {
|
---|
259 | free(host);
|
---|
260 | return NULL;
|
---|
261 | }
|
---|
262 |
|
---|
263 | if(videoGetState(0, 0, &state) != 0) {
|
---|
264 | rsxFinish(ctx, 0);
|
---|
265 | free(host);
|
---|
266 | return NULL;
|
---|
267 | }
|
---|
268 |
|
---|
269 | if(state.state != 0) {
|
---|
270 | rsxFinish(ctx, 0);
|
---|
271 | free(host);
|
---|
272 | return NULL;
|
---|
273 | }
|
---|
274 |
|
---|
275 | if(videoGetResolution(state.displayMode.resolution, &res) != 0) {
|
---|
276 | rsxFinish(ctx, 0);
|
---|
277 | free(host);
|
---|
278 | return NULL;
|
---|
279 | }
|
---|
280 |
|
---|
281 | memset(&vconfig, 0, sizeof(vconfig));
|
---|
282 | vconfig.resolution = state.displayMode.resolution;
|
---|
283 | vconfig.format = VIDEO_BUFFER_FORMAT_XRGB;
|
---|
284 | vconfig.pitch = res.width * 4;
|
---|
285 | vconfig.aspect = state.displayMode.aspect;
|
---|
286 |
|
---|
287 | wait_rsx_until_idle(ctx);
|
---|
288 |
|
---|
289 | if(videoConfigure(0, &vconfig, NULL, 0) != 0) {
|
---|
290 | rsxFinish(ctx, 0);
|
---|
291 | free(host);
|
---|
292 | return NULL;
|
---|
293 | }
|
---|
294 |
|
---|
295 | if(videoGetState(0, 0, &state) != 0) {
|
---|
296 | rsxFinish(ctx, 0);
|
---|
297 | free(host);
|
---|
298 | return NULL;
|
---|
299 | }
|
---|
300 | gcmSetFlipMode(GCM_FLIP_VSYNC);
|
---|
301 |
|
---|
302 | depth_pitch = res.width * 4;
|
---|
303 | depth_buffer = (uint32_t*)rsxMemalign(64, (res.height * depth_pitch) * 2);
|
---|
304 | rsxAddressToOffset(depth_buffer, &depth_offset);
|
---|
305 |
|
---|
306 | gcmResetFlipStatus();
|
---|
307 |
|
---|
308 | return ctx;
|
---|
309 | }
|
---|
310 |
|
---|
311 | void set_render_target(gcmContextData* context, struct rsx_buffer* buffer) {
|
---|
312 | gcmSurface sf;
|
---|
313 |
|
---|
314 | sf.colorFormat = GCM_SURFACE_X8R8G8B8;
|
---|
315 | sf.colorTarget = GCM_SURFACE_TARGET_0;
|
---|
316 | sf.colorLocation[0] = GCM_LOCATION_RSX;
|
---|
317 | sf.colorOffset[0] = buffer->offset;
|
---|
318 | sf.colorPitch[0] = depth_pitch;
|
---|
319 |
|
---|
320 | sf.colorLocation[1] = GCM_LOCATION_RSX;
|
---|
321 | sf.colorLocation[2] = GCM_LOCATION_RSX;
|
---|
322 | sf.colorLocation[3] = GCM_LOCATION_RSX;
|
---|
323 | sf.colorOffset[1] = 0;
|
---|
324 | sf.colorOffset[2] = 0;
|
---|
325 | sf.colorOffset[3] = 0;
|
---|
326 | sf.colorPitch[1] = 64;
|
---|
327 | sf.colorPitch[2] = 64;
|
---|
328 | sf.colorPitch[3] = 64;
|
---|
329 |
|
---|
330 | sf.depthFormat = GCM_SURFACE_ZETA_Z16;
|
---|
331 | sf.depthLocation = GCM_LOCATION_RSX;
|
---|
332 | sf.depthOffset = depth_offset;
|
---|
333 | sf.depthPitch = depth_pitch;
|
---|
334 |
|
---|
335 | sf.type = GCM_TEXTURE_LINEAR;
|
---|
336 | sf.antiAlias = GCM_SURFACE_CENTER_1;
|
---|
337 |
|
---|
338 | sf.width = buffer->width;
|
---|
339 | sf.height = buffer->height;
|
---|
340 | sf.x = 0;
|
---|
341 | sf.y = 0;
|
---|
342 |
|
---|
343 | rsxSetSurface(context, &sf);
|
---|
344 | }
|
---|
345 |
|
---|
346 | void wait_flip(void) {
|
---|
347 | while(gcmGetFlipStatus() != 0) usleep(200);
|
---|
348 | gcmResetFlipStatus();
|
---|
349 | }
|
---|
350 |
|
---|
351 | void flip(gcmContextData* ctx, uint32_t buffer) {
|
---|
352 | if(gcmSetFlip(ctx, buffer) == 0) {
|
---|
353 | rsxFlushBuffer(ctx);
|
---|
354 | gcmSetWaitFlip(ctx);
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 | uint8_t* tvram;
|
---|
359 |
|
---|
360 | extern uint8_t font[];
|
---|
361 |
|
---|
362 | int tt_x = 0;
|
---|
363 | int tt_y = 0;
|
---|
364 | int tt_width;
|
---|
365 | int tt_height;
|
---|
366 |
|
---|
367 | void tt_putstr(const char* str) {
|
---|
368 | int i;
|
---|
369 | for(i = 0; str[i] != 0; i++) {
|
---|
370 | tvram[tt_y * tt_width + tt_x] = str[i];
|
---|
371 | if(str[i] == '\n') {
|
---|
372 | tt_x = 0;
|
---|
373 | tt_y++;
|
---|
374 | } else {
|
---|
375 | tt_x++;
|
---|
376 | if(tt_x == tt_width) {
|
---|
377 | tt_x = 0;
|
---|
378 | tt_y++;
|
---|
379 | }
|
---|
380 | }
|
---|
381 | if(tt_y == tt_height) {
|
---|
382 | tt_y--;
|
---|
383 | int x, y;
|
---|
384 | for(y = 0; y < tt_height - 1; y++) {
|
---|
385 | for(x = 0; x < tt_width; x++) {
|
---|
386 | tvram[y * tt_width + x] = tvram[(y + 1) * tt_width + x];
|
---|
387 | }
|
---|
388 | }
|
---|
389 | for(x = 0; x < tt_width; x++) {
|
---|
390 | tvram[(tt_height - 1) * tt_width + x] = 0x20;
|
---|
391 | }
|
---|
392 | }
|
---|
393 | }
|
---|
394 | }
|
---|
395 |
|
---|
396 | void tt_putchar(struct rsx_buffer* buffer, int x, int y, uint8_t c) {
|
---|
397 | int i, j;
|
---|
398 | if(c == 0) return;
|
---|
399 | if(c < 0x20) c = 0x20;
|
---|
400 | if(c >= 0x7f) c = 0x20;
|
---|
401 | for(i = 0; i < 8; i++) {
|
---|
402 | uint8_t l = i == 7 ? 0 : font[(c - 0x20) * 8 + i];
|
---|
403 | for(j = 0; j < 6; j++) {
|
---|
404 | uint32_t col = 0;
|
---|
405 | if(l & (1 << 7)) {
|
---|
406 | col = 0xffffff;
|
---|
407 | }
|
---|
408 | l = l << 1;
|
---|
409 | buffer->ptr[(y * 8 + i) * buffer->width + x * 6 + j] = col;
|
---|
410 | }
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | void draw(struct rsx_buffer* buffer, int current) {
|
---|
415 | int i, j, c;
|
---|
416 | for(i = 0; i < buffer->height / 8; i++) {
|
---|
417 | for(j = 0; j < buffer->width / 6; j++) {
|
---|
418 | uint8_t c = tvram[i * (buffer->width / 6) + j];
|
---|
419 | tt_putchar(buffer, j, i, c);
|
---|
420 | }
|
---|
421 | }
|
---|
422 | }
|
---|
423 |
|
---|
424 | #define BUFFERS 1
|
---|
425 | gcmContextData* ctx;
|
---|
426 | struct rsx_buffer buffers[BUFFERS];
|
---|
427 |
|
---|
428 | void text_thread(void* arg) {
|
---|
429 | int current = 0;
|
---|
430 | while(1) {
|
---|
431 | wait_flip();
|
---|
432 | draw(&buffers[current], current);
|
---|
433 | flip(ctx, buffers[current].id);
|
---|
434 | current++;
|
---|
435 | if(current >= BUFFERS) current = 0;
|
---|
436 | }
|
---|
437 | }
|
---|
438 |
|
---|
439 | void tt_printf(const char* tmpl, ...) {
|
---|
440 | va_list va;
|
---|
441 | va_start(va, tmpl);
|
---|
442 | int i;
|
---|
443 | char cbuf[2];
|
---|
444 | cbuf[1] = 0;
|
---|
445 | char* log = cm_strdup("");
|
---|
446 | for(i = 0; tmpl[i] != 0; i++) {
|
---|
447 | if(tmpl[i] == '%') {
|
---|
448 | i++;
|
---|
449 | if(tmpl[i] == 's') {
|
---|
450 | char* tmp = log;
|
---|
451 | log = cm_strcat(tmp, va_arg(va, char*));
|
---|
452 | free(tmp);
|
---|
453 | } else if(tmpl[i] == 'd') {
|
---|
454 | char buf[513];
|
---|
455 | sprintf(buf, "%d", va_arg(va, int));
|
---|
456 | char* tmp = log;
|
---|
457 | log = cm_strcat(tmp, buf);
|
---|
458 | free(tmp);
|
---|
459 | } else if(tmpl[i] == '%') {
|
---|
460 | char* tmp = log;
|
---|
461 | log = cm_strcat(tmp, "%");
|
---|
462 | free(tmp);
|
---|
463 | }
|
---|
464 | } else {
|
---|
465 | cbuf[0] = tmpl[i];
|
---|
466 | char* tmp = log;
|
---|
467 | log = cm_strcat(tmp, cbuf);
|
---|
468 | free(tmp);
|
---|
469 | }
|
---|
470 | }
|
---|
471 | va_end(va);
|
---|
472 | tt_putstr(log);
|
---|
473 | }
|
---|
474 |
|
---|
475 | void show_png(void) {
|
---|
476 | FILE* f = fopen(PREFIX "/pbtewi.png", "rb");
|
---|
477 | if(f == NULL) {
|
---|
478 | f = fopen(PREFIX "/../ICON0.PNG", "rb");
|
---|
479 | }
|
---|
480 | if(f == NULL) return;
|
---|
481 | png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
---|
482 | png_infop info = png_create_info_struct(png);
|
---|
483 | if(setjmp(png_jmpbuf(png))) {
|
---|
484 | png_destroy_read_struct(&png, &info, NULL);
|
---|
485 | fclose(f);
|
---|
486 | return;
|
---|
487 | }
|
---|
488 |
|
---|
489 | png_init_io(png, f);
|
---|
490 | png_read_info(png, info);
|
---|
491 |
|
---|
492 | int width = png_get_image_width(png, info);
|
---|
493 | int height = png_get_image_height(png, info);
|
---|
494 | int depth = png_get_bit_depth(png, info);
|
---|
495 | int type = png_get_color_type(png, info);
|
---|
496 |
|
---|
497 | if(depth == 16) png_set_strip_16(png);
|
---|
498 | if(type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
|
---|
499 | if(type == PNG_COLOR_TYPE_GRAY && depth < 8) png_set_expand_gray_1_2_4_to_8(png);
|
---|
500 | if(png_get_valid(png, info, PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png);
|
---|
501 | if(type == PNG_COLOR_TYPE_RGB || type == PNG_COLOR_TYPE_GRAY || type == PNG_COLOR_TYPE_PALETTE) png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
|
---|
502 | if(type == PNG_COLOR_TYPE_GRAY || type == PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_to_rgb(png);
|
---|
503 | png_read_update_info(png, info);
|
---|
504 | png_bytep* rows = (png_bytep*)malloc(sizeof(*rows) * (height));
|
---|
505 |
|
---|
506 | int i;
|
---|
507 |
|
---|
508 | for(i = 0; i < height; i++) {
|
---|
509 | rows[i] = (png_byte*)malloc(png_get_rowbytes(png, info));
|
---|
510 | }
|
---|
511 |
|
---|
512 | png_read_image(png, rows);
|
---|
513 |
|
---|
514 | for(i = 0; i < height; i++) {
|
---|
515 | int j;
|
---|
516 | for(j = 0; j < width; j++) {
|
---|
517 | png_bytep byte = &(rows[i][j * 4]);
|
---|
518 | uint32_t col = (byte[0] << 16) | (byte[1] << 8) | (byte[2]);
|
---|
519 | int k;
|
---|
520 | for(k = 0; k < BUFFERS; k++) {
|
---|
521 | buffers[k].ptr[buffers[k].width * i - width + j] = col;
|
---|
522 | }
|
---|
523 | }
|
---|
524 | }
|
---|
525 |
|
---|
526 | png_destroy_read_struct(&png, &info, NULL);
|
---|
527 | fclose(f);
|
---|
528 |
|
---|
529 | for(i = 0; i < height; i++) {
|
---|
530 | free(rows[i]);
|
---|
531 | }
|
---|
532 | free(rows);
|
---|
533 | }
|
---|
534 |
|
---|
535 | #endif
|
---|
536 |
|
---|
537 | #if !defined(BUILD_GUI_VALID)
|
---|
538 | void thread_stuff(void* pargs);
|
---|
539 |
|
---|
540 | struct arg_struct {
|
---|
541 | int argc;
|
---|
542 | char** argv;
|
---|
543 | };
|
---|
544 |
|
---|
545 | int main(int argc, char** argv) {
|
---|
546 | int st;
|
---|
547 | #ifdef __NETWARE__
|
---|
548 | struct arg_struct* parg = malloc(sizeof(*parg));
|
---|
549 | parg->argc = argc;
|
---|
550 | parg->argv = argv;
|
---|
551 | DestroyScreen(GetCurrentScreen());
|
---|
552 | SetCurrentScreen(CreateScreen("Tewi Console", 0));
|
---|
553 | BeginThread(thread_stuff, NULL, 0, parg);
|
---|
554 | ThreadSwitch();
|
---|
555 | ExitThread(EXIT_THREAD, 0);
|
---|
556 | return 0;
|
---|
557 | }
|
---|
558 |
|
---|
559 | #ifdef __NETWARE__
|
---|
560 | void __WATCOM_Prelude(void){return;}
|
---|
561 | #endif
|
---|
562 |
|
---|
563 | void thread_stuff(void* pargs) {
|
---|
564 | int st;
|
---|
565 | int argc = ((struct arg_struct*)pargs)->argc;
|
---|
566 | char** argv = ((struct arg_struct*)pargs)->argv;
|
---|
567 | #endif
|
---|
568 | #ifdef SERVICE
|
---|
569 | SERVICE_TABLE_ENTRY table[] = {{"Tewi HTTPd", servmain}, {NULL, NULL}};
|
---|
570 | logfile = stderr;
|
---|
571 | if(!StartServiceCtrlDispatcher(table)) {
|
---|
572 | printf("Failed to start the service dispatcher\n");
|
---|
573 | return 1;
|
---|
574 | }
|
---|
575 | #else
|
---|
576 | logfile = stderr;
|
---|
577 | #ifdef _PSP
|
---|
578 | pspDebugScreenInit();
|
---|
579 | pspDebugScreenSetXY(0, 0);
|
---|
580 | printf("PSP Bootstrap, Tewi/%s\n", tw_get_version());
|
---|
581 | int thid = sceKernelCreateThread("update_thread", psp_callback_thread, 0x11, 0xfa0, 0, NULL);
|
---|
582 | if(thid >= 0) {
|
---|
583 | sceKernelStartThread(thid, 0, NULL);
|
---|
584 | } else {
|
---|
585 | printf("Failed to start thread\n");
|
---|
586 | while(running) sceKernelDelayThread(50 * 1000);
|
---|
587 | sceKernelExitGame();
|
---|
588 | }
|
---|
589 | sceCtrlSetSamplingCycle(0);
|
---|
590 | sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
|
---|
591 | sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON);
|
---|
592 | sceUtilityLoadNetModule(PSP_NET_MODULE_INET);
|
---|
593 | if(pspSdkInetInit()) {
|
---|
594 | printf("Could not init the network\n");
|
---|
595 | while(running) sceKernelDelayThread(50 * 1000);
|
---|
596 | sceKernelExitGame();
|
---|
597 | } else {
|
---|
598 | printf("Network initialization successful\n");
|
---|
599 | }
|
---|
600 | if(sceWlanGetSwitchState() != 1) {
|
---|
601 | printf("Turn the Wi-Fi switch on\n");
|
---|
602 | while(sceWlanGetSwitchState() != 1) {
|
---|
603 | sceKernelDelayThread(1000 * 1000);
|
---|
604 | }
|
---|
605 | } else {
|
---|
606 | printf("Wi-Fi is turned on\n");
|
---|
607 | }
|
---|
608 | int i;
|
---|
609 | int choice[100];
|
---|
610 | int incr = 0;
|
---|
611 | int last = 0;
|
---|
612 | int cur = 0;
|
---|
613 | for(i = 1; i < 100; i++) {
|
---|
614 | choice[i - 1] = 0;
|
---|
615 | netData name;
|
---|
616 | netData data;
|
---|
617 | if(sceUtilityCheckNetParam(i) != 0) continue;
|
---|
618 | choice[incr++] = i;
|
---|
619 | pspDebugScreenSetXY(0, 1 + 3 + incr - 1);
|
---|
620 | if(incr == 1) printf("> ");
|
---|
621 | pspDebugScreenSetXY(2, 1 + 3 + incr - 1);
|
---|
622 | sceUtilityGetNetParam(i, 0, &name);
|
---|
623 | sceUtilityGetNetParam(i, 1, &data);
|
---|
624 | printf("SSID=%s", data.asString);
|
---|
625 | sceUtilityGetNetParam(i, 4, &data);
|
---|
626 | if(data.asString[0]) {
|
---|
627 | sceUtilityGetNetParam(i, 5, &data);
|
---|
628 | printf(" IPADDR=%s\n", data.asString);
|
---|
629 | } else {
|
---|
630 | printf(" DHCP\n");
|
---|
631 | }
|
---|
632 | }
|
---|
633 | int press = 0;
|
---|
634 | while(1) {
|
---|
635 | if(!running) {
|
---|
636 | sceKernelExitGame();
|
---|
637 | }
|
---|
638 | SceCtrlData c;
|
---|
639 | sceCtrlReadBufferPositive(&c, 1);
|
---|
640 | press = 0;
|
---|
641 | if(c.Buttons & PSP_CTRL_DOWN) {
|
---|
642 | if(cur < incr - 1) {
|
---|
643 | cur++;
|
---|
644 | }
|
---|
645 | press = 1;
|
---|
646 | } else if(c.Buttons & PSP_CTRL_UP) {
|
---|
647 | if(cur > 0) {
|
---|
648 | cur--;
|
---|
649 | }
|
---|
650 | press = -1;
|
---|
651 | } else if(c.Buttons & PSP_CTRL_START) {
|
---|
652 | break;
|
---|
653 | }
|
---|
654 | if(last != cur) {
|
---|
655 | pspDebugScreenSetXY(0, 1 + 3 + last);
|
---|
656 | printf(" ");
|
---|
657 | pspDebugScreenSetXY(0, 1 + 3 + cur);
|
---|
658 | printf("> ");
|
---|
659 | last = cur;
|
---|
660 | }
|
---|
661 | if(press != 0) {
|
---|
662 | while(1) {
|
---|
663 | SceCtrlData c;
|
---|
664 | sceCtrlReadBufferPositive(&c, 1);
|
---|
665 | if(press == 1) {
|
---|
666 | if(!(c.Buttons & PSP_CTRL_DOWN)) break;
|
---|
667 | } else if(press == -1) {
|
---|
668 | if(!(c.Buttons & PSP_CTRL_UP)) break;
|
---|
669 | }
|
---|
670 | }
|
---|
671 | }
|
---|
672 | }
|
---|
673 | pspDebugScreenSetXY(0, 1 + 3 + incr + 1);
|
---|
674 | int err = sceNetApctlConnect(choice[cur]);
|
---|
675 | if(err != 0) {
|
---|
676 | printf("Apctl initialization failure\n");
|
---|
677 | while(running) sceKernelDelayThread(50 * 1000);
|
---|
678 | sceKernelExitGame();
|
---|
679 | } else {
|
---|
680 | printf("Apctl initialization successful\n");
|
---|
681 | }
|
---|
682 | printf("Apctl connecting\n");
|
---|
683 | while(1) {
|
---|
684 | int state;
|
---|
685 | err = sceNetApctlGetState(&state);
|
---|
686 | if(err != 0) {
|
---|
687 | printf("Apctl getting status failure\n");
|
---|
688 | while(running) sceKernelDelayThread(50 * 1000);
|
---|
689 | sceKernelExitGame();
|
---|
690 | }
|
---|
691 | if(state == 4) {
|
---|
692 | break;
|
---|
693 | }
|
---|
694 | sceKernelDelayThread(50 * 1000);
|
---|
695 | }
|
---|
696 | union SceNetApctlInfo info;
|
---|
697 | if(sceNetApctlGetInfo(8, &info) != 0) {
|
---|
698 | printf("Got an unknown IP\n");
|
---|
699 | while(running) sceKernelDelayThread(50 * 1000);
|
---|
700 | sceKernelExitGame();
|
---|
701 | }
|
---|
702 | printf("Connected, My IP is %s\n", info.ip);
|
---|
703 | #elif defined(__PPU__)
|
---|
704 | int i;
|
---|
705 | ctx = init_screen();
|
---|
706 | int w, h;
|
---|
707 | get_resolution(&w, &h);
|
---|
708 | tt_width = w / 6;
|
---|
709 | tt_height = h / 8;
|
---|
710 | tvram = malloc((w / 6) * (h / 8));
|
---|
711 | for(i = 0; i < BUFFERS; i++) make_buffer(&buffers[i], i);
|
---|
712 | flip(ctx, BUFFERS - 1);
|
---|
713 | sys_ppu_thread_t id;
|
---|
714 | sysThreadCreate(&id, text_thread, NULL, 1500, 0x1000, THREAD_JOINABLE, "TextThread");
|
---|
715 | printf("PS3 Bootstrap, Tewi/%s\n", tw_get_version());
|
---|
716 | show_png();
|
---|
717 | netInitialize();
|
---|
718 | #elif defined(__ps2sdk__)
|
---|
719 | SifInitRpc(0);
|
---|
720 | while(!SifIopReset("", 0))
|
---|
721 | ;
|
---|
722 | while(!SifIopSync())
|
---|
723 | ;
|
---|
724 | init_scr();
|
---|
725 | scr_printf("PS2 Bootstrap, Tewi/%s\n", tw_get_version());
|
---|
726 | SleepThread();
|
---|
727 | #endif
|
---|
728 | st = startup(argc, argv);
|
---|
729 | if(st != -1) {
|
---|
730 | #ifdef _PSP
|
---|
731 | printf("Error code %d\n", st);
|
---|
732 | while(running) sceKernelDelayThread(50 * 1000);
|
---|
733 | sceKernelExitGame();
|
---|
734 | #else
|
---|
735 | #ifdef __PPU__
|
---|
736 | printf("Error code %d\n", st);
|
---|
737 | while(1)
|
---|
738 | ;
|
---|
739 | #endif
|
---|
740 | #ifdef __NETWARE__
|
---|
741 | return;
|
---|
742 | #else
|
---|
743 | return st;
|
---|
744 | #endif
|
---|
745 | #endif
|
---|
746 | }
|
---|
747 | tw_server_loop();
|
---|
748 | #endif
|
---|
749 | #ifdef _PSP
|
---|
750 | sceKernelExitGame();
|
---|
751 | #endif
|
---|
752 | #ifdef __NETWARE__
|
---|
753 | return;
|
---|
754 | #else
|
---|
755 | return 0;
|
---|
756 | #endif
|
---|
757 | }
|
---|
758 | #endif
|
---|
759 |
|
---|
760 | int startup(int argc, char** argv) {
|
---|
761 | int i;
|
---|
762 | char* r;
|
---|
763 | #if defined(__MINGW32__) || defined(_MSC_VER) || (defined(__WATCOMC__) && !defined(__OS2__) && !defined(__NETWARE__)) || defined(__BORLANDC__)
|
---|
764 | char* confpath = cm_strdup(PREFIX "/etc/tewi.conf");
|
---|
765 | char* regpath = get_registry("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Tewi HTTPd", "InstallDir");
|
---|
766 | if(regpath != NULL) {
|
---|
767 | free(confpath);
|
---|
768 | confpath = cm_strcat(regpath, "/etc/tewi.conf");
|
---|
769 | free(regpath);
|
---|
770 | }
|
---|
771 | #else
|
---|
772 | const char* confpath = PREFIX "/etc/tewi.conf";
|
---|
773 | #endif
|
---|
774 | if(argv != NULL) {
|
---|
775 | for(i = 1; i < argc; i++) {
|
---|
776 | if(argv[i][0] == '-') {
|
---|
777 | if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) {
|
---|
778 | if(!cm_do_log) {
|
---|
779 | cm_do_log = true;
|
---|
780 | #ifndef NO_SSL
|
---|
781 | cm_log("", "This is Tewi HTTPd, version %s, using %s", tw_get_version(), OPENSSL_VERSION_TEXT);
|
---|
782 | #else
|
---|
783 | cm_log("", "This is Tewi HTTPd, version %s", tw_get_version());
|
---|
784 | #endif
|
---|
785 | } else {
|
---|
786 | cm_do_log = true;
|
---|
787 | }
|
---|
788 | } else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0) {
|
---|
789 | i++;
|
---|
790 | if(argv[i] == NULL) {
|
---|
791 | STDERR_LOG("Missing argument\n");
|
---|
792 | return 1;
|
---|
793 | }
|
---|
794 | confpath = argv[i];
|
---|
795 | #ifndef _PSP
|
---|
796 | } else if(strcmp(argv[i], "--logfile") == 0 || strcmp(argv[i], "-l") == 0) {
|
---|
797 | i++;
|
---|
798 | if(argv[i] == NULL) {
|
---|
799 | STDERR_LOG("Missing argument\n");
|
---|
800 | return 1;
|
---|
801 | }
|
---|
802 | if(logfile != NULL && logfile != stderr) {
|
---|
803 | fclose(logfile);
|
---|
804 | }
|
---|
805 | logfile = fopen(argv[i], "a");
|
---|
806 | if(logfile == NULL) {
|
---|
807 | STDERR_LOG("Failed to open logfile\n");
|
---|
808 | return 1;
|
---|
809 | }
|
---|
810 | #endif
|
---|
811 | } else if(strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-V") == 0) {
|
---|
812 | printf("Tewi HTTPd Tewi/%s\n", tw_get_version());
|
---|
813 | printf("Under public domain.\n");
|
---|
814 | printf("Original by 2024 Nishi\n");
|
---|
815 | printf("\n");
|
---|
816 | printf("Usage: %s [--config|-C config] [--verbose|-v] [--version|-V]\n", argv[0]);
|
---|
817 | printf("--config | -C config : Specify config\n");
|
---|
818 | #ifndef _PSP
|
---|
819 | printf("--logfile | -l logfile : Specify logfile\n");
|
---|
820 | #endif
|
---|
821 | printf("--verbose | -v : Verbose mode\n");
|
---|
822 | printf("--version | -V : Version information\n");
|
---|
823 | return 0;
|
---|
824 | } else {
|
---|
825 | STDERR_LOG("Unknown option: %s\n", argv[i]);
|
---|
826 | return 1;
|
---|
827 | }
|
---|
828 | }
|
---|
829 | }
|
---|
830 | }
|
---|
831 | tw_config_init();
|
---|
832 | if(tw_config_read(confpath) != 0) {
|
---|
833 | STDERR_LOG("Could not read the config\n");
|
---|
834 | return 1;
|
---|
835 | }
|
---|
836 | if(tw_server_init() != 0) {
|
---|
837 | STDERR_LOG("Could not initialize the server\n");
|
---|
838 | return 1;
|
---|
839 | }
|
---|
840 | sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension);
|
---|
841 | r = cm_strcat(tw_server, " running...");
|
---|
842 | cm_force_log(r);
|
---|
843 | free(r);
|
---|
844 | #if !defined(__MINGW32__) && !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__WATCOMC__)
|
---|
845 | signal(SIGCHLD, SIG_IGN);
|
---|
846 | signal(SIGPIPE, SIG_IGN);
|
---|
847 | #elif !defined(BUILD_GUI) && !defined(__OS2__) && !defined(__NETWARE__)
|
---|
848 | SetConsoleTitle(tw_server);
|
---|
849 | #endif
|
---|
850 | return -1;
|
---|
851 | }
|
---|