Subversion Repositories Tewi

Rev

Rev 213 | Rev 219 | 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 215 2024-10-02 19:24:43Z nishi $ */
2
 
16 nishi 3
#define SOURCE
4
 
43 nishi 5
#include "../config.h"
6
 
215 nishi 7
#ifdef __BORLANDC__
8
 
9
#pragma resource "tewi_bcc.res"
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
 
215 nishi 33
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__)
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__)
215 nishi 70
#elif defined(_MSC_VER) || defined(__BORLANDC__)
213 nishi 71
void STDERR_LOG(const char* format, ...) {
212 nishi 72
	va_list args;
73
	va_start(args, format);
74
	vfprintf(stderr, format, args);
75
	va_end(args);
76
}
183 nishi 77
#else
78
#define STDERR_LOG(...) fprintf(stderr, __VA_ARGS__)
182 nishi 79
#endif
80
 
3 nishi 81
extern bool cm_do_log;
18 nishi 82
extern struct tw_config config;
62 nishi 83
extern FILE* logfile;
3 nishi 84
 
18 nishi 85
char tw_server[2048];
86
 
62 nishi 87
int startup(int argc, char** argv);
88
 
212 nishi 89
#if defined(__MINGW32__) || defined(_MSC_VER)
206 nishi 90
char* get_registry(const char* main, const char* sub) {
208 nishi 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 nishi 95
	if(err == ERROR_SUCCESS) {
208 nishi 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 nishi 103
		return value;
104
	} else {
105
		free(value);
106
		return NULL;
107
	}
108
}
109
#endif
110
 
62 nishi 111
#ifdef SERVICE
112
SERVICE_STATUS status;
113
SERVICE_STATUS_HANDLE status_handle;
114
 
70 nishi 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 nishi 121
	}
122
	SetServiceStatus(status_handle, &status);
123
}
124
 
70 nishi 125
void WINAPI servmain(DWORD argc, LPSTR* argv) {
206 nishi 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 nishi 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 nishi 147
	if(st != -1) {
62 nishi 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 nishi 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 nishi 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 nishi 359
	for(i = 0; str[i] != 0; i++) {
191 nishi 360
		tvram[tt_y * tt_width + tt_x] = str[i];
193 nishi 361
		if(str[i] == '\n') {
191 nishi 362
			tt_x = 0;
363
			tt_y++;
193 nishi 364
		} else {
191 nishi 365
			tt_x++;
193 nishi 366
			if(tt_x == tt_width) {
191 nishi 367
				tt_x = 0;
368
				tt_y++;
369
			}
370
		}
193 nishi 371
		if(tt_y == tt_height) {
191 nishi 372
			tt_y--;
373
			int x, y;
193 nishi 374
			for(y = 0; y < tt_height - 1; y++) {
375
				for(x = 0; x < tt_width; x++) {
191 nishi 376
					tvram[y * tt_width + x] = tvram[(y + 1) * tt_width + x];
377
				}
378
			}
193 nishi 379
			for(x = 0; x < tt_width; x++) {
197 nishi 380
				tvram[(tt_height - 1) * tt_width + x] = 0x20;
191 nishi 381
			}
382
		}
383
	}
384
}
385
 
193 nishi 386
void tt_putchar(struct rsx_buffer* buffer, int x, int y, uint8_t c) {
191 nishi 387
	int i, j;
197 nishi 388
	if(c == 0) return;
191 nishi 389
	if(c < 0x20) c = 0x20;
390
	if(c >= 0x7f) c = 0x20;
197 nishi 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 nishi 395
			if(l & (1 << 7)) {
197 nishi 396
				col = 0xffffff;
191 nishi 397
			}
398
			l = l << 1;
197 nishi 399
			buffer->ptr[(y * 8 + i) * buffer->width + x * 6 + j] = col;
191 nishi 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 nishi 414
#define BUFFERS 1
191 nishi 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 nishi 439
			if(tmpl[i] == 's') {
191 nishi 440
				char* tmp = log;
441
				log = cm_strcat(tmp, va_arg(va, char*));
442
				free(tmp);
193 nishi 443
			} else if(tmpl[i] == 'd') {
191 nishi 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 nishi 449
			} else if(tmpl[i] == '%') {
191 nishi 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 nishi 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 nishi 525
#endif
526
 
3 nishi 527
int main(int argc, char** argv) {
212 nishi 528
	int st;
62 nishi 529
	logfile = stderr;
530
#ifdef SERVICE
531
	SERVICE_TABLE_ENTRY table[] = {{"Tewi HTTPd", servmain}, {NULL, NULL}};
532
	StartServiceCtrlDispatcher(table);
533
#else
182 nishi 534
#ifdef _PSP
535
	pspDebugScreenInit();
536
	pspDebugScreenSetXY(0, 0);
183 nishi 537
	printf("PSP Bootstrap, Tewi/%s\n", tw_get_version());
538
	int thid = sceKernelCreateThread("update_thread", psp_callback_thread, 0x11, 0xfa0, 0, NULL);
539
	if(thid >= 0) {
540
		sceKernelStartThread(thid, 0, NULL);
541
	} else {
542
		printf("Failed to start thread\n");
543
		while(running) sceKernelDelayThread(50 * 1000);
544
		sceKernelExitGame();
545
	}
546
	sceCtrlSetSamplingCycle(0);
547
	sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
548
	sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON);
549
	sceUtilityLoadNetModule(PSP_NET_MODULE_INET);
550
	if(pspSdkInetInit()) {
551
		printf("Could not init the network\n");
552
		while(running) sceKernelDelayThread(50 * 1000);
553
		sceKernelExitGame();
554
	} else {
555
		printf("Network initialization successful\n");
556
	}
557
	if(sceWlanGetSwitchState() != 1) {
558
		printf("Turn the Wi-Fi switch on\n");
559
		while(sceWlanGetSwitchState() != 1) {
560
			sceKernelDelayThread(1000 * 1000);
561
		}
562
	} else {
563
		printf("Wi-Fi is turned on\n");
564
	}
565
	int i;
566
	int choice[100];
567
	int incr = 0;
568
	int last = 0;
569
	int cur = 0;
570
	for(i = 1; i < 100; i++) {
571
		choice[i - 1] = 0;
572
		netData name;
573
		netData data;
574
		if(sceUtilityCheckNetParam(i) != 0) continue;
575
		choice[incr++] = i;
576
		pspDebugScreenSetXY(0, 1 + 3 + incr - 1);
577
		if(incr == 1) printf("> ");
578
		pspDebugScreenSetXY(2, 1 + 3 + incr - 1);
579
		sceUtilityGetNetParam(i, 0, &name);
580
		sceUtilityGetNetParam(i, 1, &data);
581
		printf("SSID=%s", data.asString);
582
		sceUtilityGetNetParam(i, 4, &data);
583
		if(data.asString[0]) {
584
			sceUtilityGetNetParam(i, 5, &data);
585
			printf(" IPADDR=%s\n", data.asString);
586
		} else {
587
			printf(" DHCP\n");
588
		}
589
	}
590
	int press = 0;
591
	while(1) {
592
		if(!running) {
593
			sceKernelExitGame();
594
		}
595
		SceCtrlData c;
596
		sceCtrlReadBufferPositive(&c, 1);
597
		press = 0;
598
		if(c.Buttons & PSP_CTRL_DOWN) {
599
			if(cur < incr - 1) {
600
				cur++;
601
			}
602
			press = 1;
603
		} else if(c.Buttons & PSP_CTRL_UP) {
604
			if(cur > 0) {
605
				cur--;
606
			}
607
			press = -1;
608
		} else if(c.Buttons & PSP_CTRL_START) {
609
			break;
610
		}
611
		if(last != cur) {
612
			pspDebugScreenSetXY(0, 1 + 3 + last);
613
			printf("  ");
614
			pspDebugScreenSetXY(0, 1 + 3 + cur);
615
			printf("> ");
616
			last = cur;
617
		}
618
		if(press != 0) {
619
			while(1) {
620
				SceCtrlData c;
621
				sceCtrlReadBufferPositive(&c, 1);
622
				if(press == 1) {
623
					if(!(c.Buttons & PSP_CTRL_DOWN)) break;
624
				} else if(press == -1) {
625
					if(!(c.Buttons & PSP_CTRL_UP)) break;
626
				}
627
			}
628
		}
629
	}
630
	pspDebugScreenSetXY(0, 1 + 3 + incr + 1);
631
	int err = sceNetApctlConnect(choice[cur]);
632
	if(err != 0) {
633
		printf("Apctl initialization failure\n");
634
		while(running) sceKernelDelayThread(50 * 1000);
635
		sceKernelExitGame();
636
	} else {
637
		printf("Apctl initialization successful\n");
638
	}
639
	printf("Apctl connecting\n");
640
	while(1) {
641
		int state;
642
		err = sceNetApctlGetState(&state);
643
		if(err != 0) {
644
			printf("Apctl getting status failure\n");
645
			while(running) sceKernelDelayThread(50 * 1000);
646
			sceKernelExitGame();
647
		}
648
		if(state == 4) {
649
			break;
650
		}
651
		sceKernelDelayThread(50 * 1000);
652
	}
653
	union SceNetApctlInfo info;
654
	if(sceNetApctlGetInfo(8, &info) != 0) {
655
		printf("Got an unknown IP\n");
656
		while(running) sceKernelDelayThread(50 * 1000);
657
		sceKernelExitGame();
658
	}
659
	printf("Connected, My IP is %s\n", info.ip);
187 nishi 660
#elif defined(__PPU__)
191 nishi 661
	int i;
662
	ctx = init_screen();
663
	int w, h;
664
	get_resolution(&w, &h);
665
	tt_width = w / 6;
666
	tt_height = h / 8;
667
	tvram = malloc((w / 6) * (h / 8));
668
	for(i = 0; i < BUFFERS; i++) make_buffer(&buffers[i], i);
669
	flip(ctx, BUFFERS - 1);
670
	sys_ppu_thread_t id;
671
	sysThreadCreate(&id, text_thread, NULL, 1500, 0x1000, THREAD_JOINABLE, "TextThread");
187 nishi 672
	printf("PS3 Bootstrap, Tewi/%s\n", tw_get_version());
197 nishi 673
	show_png();
187 nishi 674
	netInitialize();
189 nishi 675
#elif defined(__ps2sdk__)
676
	SifInitRpc(0);
200 nishi 677
	while(!SifIopReset("", 0))
678
		;
679
	while(!SifIopSync())
680
		;
189 nishi 681
	init_scr();
682
	scr_printf("PS2 Bootstrap, Tewi/%s\n", tw_get_version());
200 nishi 683
	SleepThread();
182 nishi 684
#endif
212 nishi 685
	st = startup(argc, argv);
183 nishi 686
	if(st != -1) {
687
#ifdef _PSP
688
		printf("Error code %d\n", st);
689
		while(running) sceKernelDelayThread(50 * 1000);
690
		sceKernelExitGame();
691
#else
191 nishi 692
#ifdef __PPU__
693
		printf("Error code %d\n", st);
694
		while(1)
695
			;
696
#endif
183 nishi 697
		return st;
698
#endif
699
	}
62 nishi 700
	tw_server_loop();
701
#endif
183 nishi 702
#ifdef _PSP
703
	sceKernelExitGame();
704
#endif
168 nishi 705
	return 0;
62 nishi 706
}
707
 
70 nishi 708
int startup(int argc, char** argv) {
3 nishi 709
	int i;
212 nishi 710
	char* r;
711
#if defined(__MINGW32__) || defined(_MSC_VER)
206 nishi 712
	char* confpath = cm_strdup(PREFIX "/etc/tewi.conf");
713
	char* regpath = get_registry("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Tewi HTTPd", "InstallDir");
714
	if(regpath != NULL) {
715
		free(confpath);
716
		confpath = cm_strcat(regpath, "/etc/tewi.conf");
717
		free(regpath);
718
	}
719
#else
18 nishi 720
	const char* confpath = PREFIX "/etc/tewi.conf";
206 nishi 721
#endif
70 nishi 722
	if(argv != NULL) {
62 nishi 723
		for(i = 1; i < argc; i++) {
724
			if(argv[i][0] == '-') {
725
				if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) {
726
					if(!cm_do_log) {
727
						cm_do_log = true;
70 nishi 728
#ifndef NO_SSL
62 nishi 729
						cm_log("", "This is Tewi HTTPd, version %s, using %s", tw_get_version(), OPENSSL_VERSION_TEXT);
70 nishi 730
#else
62 nishi 731
						cm_log("", "This is Tewi HTTPd, version %s", tw_get_version());
70 nishi 732
#endif
62 nishi 733
					} else {
734
						cm_do_log = true;
735
					}
736
				} else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0) {
737
					i++;
738
					if(argv[i] == NULL) {
183 nishi 739
						STDERR_LOG("Missing argument\n");
62 nishi 740
						return 1;
741
					}
742
					confpath = argv[i];
182 nishi 743
#ifndef _PSP
117 nishi 744
				} else if(strcmp(argv[i], "--logfile") == 0 || strcmp(argv[i], "-l") == 0) {
745
					i++;
746
					if(argv[i] == NULL) {
183 nishi 747
						STDERR_LOG("Missing argument\n");
117 nishi 748
						return 1;
749
					}
750
					if(logfile != NULL && logfile != stderr) {
751
						fclose(logfile);
752
					}
753
					logfile = fopen(argv[i], "a");
754
					if(logfile == NULL) {
183 nishi 755
						STDERR_LOG("Failed to open logfile\n");
117 nishi 756
						return 1;
757
					}
182 nishi 758
#endif
62 nishi 759
				} else if(strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-V") == 0) {
760
					printf("Tewi HTTPd Tewi/%s\n", tw_get_version());
761
					printf("Under public domain.\n");
762
					printf("Original by 2024 Nishi\n");
763
					printf("\n");
764
					printf("Usage: %s [--config|-C config] [--verbose|-v] [--version|-V]\n", argv[0]);
119 nishi 765
					printf("--config  | -C config      : Specify config\n");
182 nishi 766
#ifndef _PSP
119 nishi 767
					printf("--logfile | -l logfile     : Specify logfile\n");
182 nishi 768
#endif
119 nishi 769
					printf("--verbose | -v             : Verbose mode\n");
770
					printf("--version | -V             : Version information\n");
62 nishi 771
					return 0;
3 nishi 772
				} else {
183 nishi 773
					STDERR_LOG("Unknown option: %s\n", argv[i]);
4 nishi 774
					return 1;
775
				}
3 nishi 776
			}
777
		}
778
	}
6 nishi 779
	tw_config_init();
18 nishi 780
	if(tw_config_read(confpath) != 0) {
183 nishi 781
		STDERR_LOG("Could not read the config\n");
4 nishi 782
		return 1;
783
	}
8 nishi 784
	if(tw_server_init() != 0) {
183 nishi 785
		STDERR_LOG("Could not initialize the server\n");
8 nishi 786
		return 1;
787
	}
18 nishi 788
	sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension);
212 nishi 789
	r = cm_strcat(tw_server, " running...");
62 nishi 790
	cm_force_log(r);
791
	free(r);
215 nishi 792
#if !defined(__MINGW32__) && !defined(_MSC_VER) && !defined(__BORLANDC__)
16 nishi 793
	signal(SIGCHLD, SIG_IGN);
90 nishi 794
	signal(SIGPIPE, SIG_IGN);
50 nishi 795
#else
796
	SetConsoleTitle(tw_server);
16 nishi 797
#endif
62 nishi 798
	return -1;
3 nishi 799
}