Subversion Repositories pspware

Rev

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

Rev Author Line No. Line
128 shine 1
/*
2
 * Lua Player for PSP
3
 * ------------------------------------------------------------------------
4
 * Licensed under the BSD license, see LICENSE for details.
5
 *
6
 * Copyright (c) 2005 Frank Buss <fb@frank-buss.de> (aka Shine)
7
 *
8
 * Credits:
9
 *   many thanks to the authors of the PSPSDK from http://forums.ps2dev.org
10
 *   and to the hints and discussions from #pspdev on freenode.net
11
 *
12
 * $Id: main.cpp 290 2005-12-03 08:49:10Z shine $
13
 */
14
 
15
#include <pspkernel.h>
16
#include <pspdebug.h>
17
#include <pspdisplay.h>
136 nevyn 18
#include <pspctrl.h>
141 shine 19
#include <pspsdk.h>
234 shine 20
#include <psputility.h>
21
#include <time.h>
128 shine 22
#include <stdlib.h>
23
#include <string.h>
136 nevyn 24
#include <unistd.h>
128 shine 25
 
26
#include "graphics.h"
136 nevyn 27
#include "sound.h"
128 shine 28
#include "luaplayer.h"
232 shine 29
#include "sio.h"
128 shine 30
 
210 shine 31
/* the boot.lua */
279 shine 32
#include "boot.cpp"
210 shine 33
 
128 shine 34
/* Define the module info section */
35
PSP_MODULE_INFO("LUAPLAYER", 0x1000, 1, 1);
287 shine 36
PSP_MAIN_THREAD_ATTR(0);
37
PSP_MAIN_THREAD_STACK_SIZE_KB(32); /* smaller stack for kernel thread */
128 shine 38
 
287 shine 39
// startup path
40
char path[256];
128 shine 41
 
42
/* Exit callback */
279 shine 43
int exit_callback(int arg1, int arg2, void *common)
128 shine 44
{
208 nevyn 45
 
286 shine 46
        // Unload modules
208 nevyn 47
        unloadMikmod();
48
 
128 shine 49
        sceKernelExitGame();
50
        return 0;
51
}
52
 
53
/* Callback thread */
54
int CallbackThread(SceSize args, void *argp)
55
{
56
        int cbid;
57
 
279 shine 58
        cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
128 shine 59
        sceKernelRegisterExitCallback(cbid);
60
 
61
        sceKernelSleepThreadCB();
62
 
63
        return 0;
64
}
65
 
66
/* Sets up the callback thread and returns its thread id */
67
int SetupCallbacks(void)
68
{
69
        int thid = 0;
70
 
71
        thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
72
        if(thid >= 0)
73
        {
74
                sceKernelStartThread(thid, 0, 0);
75
        }
76
 
77
        return thid;
78
}
79
 
80
int nullOutput(const char *buff, int size)
81
{
82
        return size;
83
}
84
 
85
int debugOutput(const char *buff, int size)
86
{
87
        static int debugInitialized = 0;
136 nevyn 88
        if(!buff) {
89
                debugInitialized = 0;
90
                return 0;
91
        }
128 shine 92
        if (!debugInitialized) {
93
                disableGraphics();
94
                pspDebugScreenInit();
95
                debugInitialized = 1;
96
        }
97
        return pspDebugScreenPrintData(buff, size);
98
}
99
 
287 shine 100
int user_main(SceSize argc, void* argv)
128 shine 101
{
102
        SetupCallbacks();
246 shine 103
        tzset();
128 shine 104
 
136 nevyn 105
        // init modules
128 shine 106
        initGraphics();
136 nevyn 107
        initMikmod();
128 shine 108
 
109
        // install new output handlers  
110
        pspDebugInstallStdoutHandler(debugOutput);
111
        pspDebugInstallStderrHandler(debugOutput);
112
 
208 nevyn 113
        // execute Lua script (according to boot sequence)
136 nevyn 114
        getcwd(path, 256);
212 shine 115
        char* bootStringWith0 = (char*) malloc(size_bootString + 1);
116
        memcpy(bootStringWith0, bootString, size_bootString);
117
        bootString[size_bootString] = 0;
208 nevyn 118
        while(1) { // reload on error
287 shine 119
                chdir(path); // set base path luaplater/
155 shine 120
                clearScreen(0);
121
                flipScreen();
122
                clearScreen(0);
210 shine 123
 
212 shine 124
                if (runScript(bootStringWith0, true))
279 shine 125
                {
208 nevyn 126
                        debugOutput("Error: No script file found.\n", 29);
279 shine 127
                }
128
                debugOutput("\nPress start to restart\n", 26);
290 shine 129
 
136 nevyn 130
                SceCtrlData pad; int i;
131
                sceCtrlReadBufferPositive(&pad, 1);
189 nevyn 132
                for(i = 0; i < 40; i++) sceDisplayWaitVblankStart();
136 nevyn 133
                while(!(pad.Buttons&PSP_CTRL_START)) sceCtrlReadBufferPositive(&pad, 1);
208 nevyn 134
 
136 nevyn 135
                debugOutput(0,0);
136
                initGraphics();
137
        }
212 shine 138
        free(bootStringWith0);
128 shine 139
 
140
        // wait until user ends the program
141
        sceKernelSleepThread();
142
 
143
        return 0;
144
}
287 shine 145
 
146
int main(void)
147
{
148
        getcwd(path, 256);
149
        int err = pspSdkLoadInetModules();
150
        if (err != 0) {
151
                pspDebugScreenInit();
152
                pspDebugScreenPrintf("pspSdkLoadInetModules failed with %x\n", err);
153
                sceKernelDelayThread(5*1000000); // 5 sec to read error
154
        }
155
 
156
        // create user thread, tweek stack size here if necessary
157
        SceUID thid = sceKernelCreateThread("User Mode Thread", user_main,
158
            0x11, // default priority
159
            256 * 1024, // stack size (256KB is regular default)
160
            PSP_THREAD_ATTR_USER, NULL);
161
 
162
        // start user thread, then wait for it to do everything else
163
        sceKernelStartThread(thid, 0, NULL);
164
        sceKernelWaitThreadEnd(thid, NULL);
165
 
166
        sceKernelExitGame();
167
        return 0;
168
}
169
 
170
__attribute__((constructor)) void stdoutInit()
171
{
172
        pspKernelSetKernelPC();
173
        pspSdkInstallNoDeviceCheckPatch();
174
        pspSdkInstallNoPlainModuleCheckPatch();
175
        pspKernelSetKernelPC();
176
        pspKernelSetKernelPC();
177
        pspSdkInstallNoDeviceCheckPatch();
178
        pspDebugInstallKprintfHandler(NULL);
179
        registerSIODriver();
180
 
181
        // ignore startup messages from kernel, but install the tty driver in kernel mode
182
        pspDebugInstallStdoutHandler(nullOutput);
183
}
184