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