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