Subversion Repositories Tewi

Rev

Rev 311 | Rev 315 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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