Subversion Repositories Tewi

Rev

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