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