Subversion Repositories pspware

Rev

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

Rev Author Line No. Line
140 nevyn 1
#include <stdlib.h>
345 Oobles 2
#include <string.h>
140 nevyn 3
#include <pspkernel.h>
279 shine 4
 
141 shine 5
#include <pspusb.h>
6
#include <pspusbstor.h>
213 shine 7
#include <psppower.h>
232 shine 8
#include <pspdebug.h>
9
#include <unistd.h>
335 shine 10
#include <sys/stat.h>
140 nevyn 11
#include "luaplayer.h"
12
 
345 Oobles 13
#define SIO_IOCTL_SET_BAUD_RATE 1
14
 
141 shine 15
static int usbActivated = 0;
232 shine 16
static SceUID sio_fd = -1;
17
static const char* sioNotInitialized = "SIO not initialized.";
140 nevyn 18
 
283 shine 19
SceUID irda_fd = -1;
20
 
140 nevyn 21
static int lua_getCurrentDirectory(lua_State *L)
22
{
23
        char path[256];
24
        getcwd(path, 256);
25
        lua_pushstring(L, path);
26
 
27
        return 1;
28
}
29
 
30
static int lua_setCurrentDirectory(lua_State *L)
31
{
32
        const char *path = luaL_checkstring(L, 1);
33
        if(!path) return luaL_error(L, "Argument error: System.currentDirectory(file) takes a filename as string as argument.");
34
 
35
        lua_getCurrentDirectory(L);
36
        chdir(path);
37
 
38
        return 1;
39
}
40
 
41
static int lua_curdir(lua_State *L) {
42
        int argc = lua_gettop(L);
43
        if(argc == 0) return lua_getCurrentDirectory(L);
44
        if(argc == 1) return lua_setCurrentDirectory(L);
45
        return luaL_error(L, "Argument error: System.currentDirectory([file]) takes zero or one argument.");
46
}
47
 
48
 
147 nevyn 49
// Move g_dir to the stack and MEET CERTAIN DOOM! If the SceIoDirent is found on the STACK instead of among the globals, the PSP WILL CRASH IN A VERY WEIRD WAY. You have been WARNED.
50
SceIoDirent g_dir;
140 nevyn 51
 
52
static int lua_dir(lua_State *L)
53
{
54
        int argc = lua_gettop(L);
55
        if (argc != 0 && argc != 1) return luaL_error(L, "Argument error: System.listDirectory([path]) takes zero or one argument.");
147 nevyn 56
 
140 nevyn 57
 
58
        const char *path = "";
59
        if (argc == 0) {
60
                path = "";
61
        } else {
62
                path = luaL_checkstring(L, 1);
63
        }
64
        int fd = sceIoDopen(path);
147 nevyn 65
 
140 nevyn 66
        if (fd < 0) {
67
                lua_pushnil(L);  /* return nil */
68
                return 1;
69
        }
70
        lua_newtable(L);
71
        int i = 1;
72
        while (sceIoDread(fd, &g_dir) > 0) {
73
                lua_pushnumber(L, i++);  /* push key for file entry */
74
 
75
                lua_newtable(L);
147 nevyn 76
                        lua_pushstring(L, "name");
77
                        lua_pushstring(L, g_dir.d_name);
78
                        lua_settable(L, -3);
79
 
80
                        lua_pushstring(L, "size");
81
                        lua_pushnumber(L, g_dir.d_stat.st_size);
82
                        lua_settable(L, -3);
83
 
84
                        lua_pushstring(L, "directory");
349 shine 85
                        lua_pushboolean(L, FIO_S_ISDIR(g_dir.d_stat.st_mode));
147 nevyn 86
                        lua_settable(L, -3);
140 nevyn 87
 
88
                lua_settable(L, -3);
89
        }
90
 
91
        sceIoDclose(fd);
92
 
93
        return 1;  /* table is already on top */
94
}
95
 
335 shine 96
static int lua_createDir(lua_State *L)
97
{
98
        const char *path = luaL_checkstring(L, 1);
99
        if(!path) return luaL_error(L, "Argument error: System.createDirectory(directory) takes a directory name as string as argument.");
100
 
101
        mkdir(path, 0777);
102
 
103
        return 0;
104
}
105
 
106
static int lua_removeDir(lua_State *L)
107
{
108
        const char *path = luaL_checkstring(L, 1);
109
        if(!path) return luaL_error(L, "Argument error: System.removeDirectory(directory) takes a directory name as string as argument.");
110
 
111
        rmdir(path);
112
 
113
        return 0;
114
}
115
 
116
static int lua_removeFile(lua_State *L)
117
{
118
        const char *path = luaL_checkstring(L, 1);
119
        if(!path) return luaL_error(L, "Argument error: System.removeFile(filename) takes a filena as string as argument.");
120
 
121
        remove(path);
122
 
123
        return 0;
124
}
125
 
141 shine 126
static int LoadStartModule(char *path)
127
{
345 Oobles 128
        u32 loadResult;
129
        u32 startResult;
130
        int status;
141 shine 131
 
345 Oobles 132
        loadResult = sceKernelLoadModule(path, 0, NULL);
133
        if (loadResult & 0x80000000)
147 nevyn 134
                return -1;
345 Oobles 135
        else
147 nevyn 136
                startResult =
345 Oobles 137
                sceKernelStartModule(loadResult, 0, NULL, &status, NULL);
141 shine 138
 
345 Oobles 139
        if (loadResult != startResult)
140
                return -2;
141 shine 141
 
345 Oobles 142
        return 0;
141 shine 143
}
144
 
345 Oobles 145
static char modulePath[256];
146
 
147
static void setModulePath()
148
{
149
        getcwd( modulePath, 256 );
150
}
151
 
350 Oobles 152
extern "C" {
153
        extern SceUID psploadlib( const char * name, char * init );
154
        extern void **findFunction( SceUID id, const char * library, const char * name );
155
        extern int init( lua_State *L);
156
}
157
 
158
#define LOADMODULE_ARGERROR "Argument error: System.loadModule(module, init) takes a module name and init method as string arguments."
159
 
345 Oobles 160
static int lua_loadModule(lua_State *L)
161
{
162
        const char *name = luaL_checkstring(L, 1);
350 Oobles 163
        const char *init = luaL_checkstring(L, 2);
345 Oobles 164
 
350 Oobles 165
        SceUID uid = psploadlib( name, NULL );
166
        if ( uid >= 0 ) {
167
                lua_CFunction f = (lua_CFunction) *(findFunction( uid, name, init ));
168
                if (f != NULL)
169
                {
170
                        lua_pushlightuserdata(L,(void*)uid);
171
                        lua_pushcclosure(L,f,1);
172
                        return 1;
173
                }
345 Oobles 174
        }
175
 
350 Oobles 176
        lua_pushnil(L);
177
        lua_pushstring(L, LOADMODULE_ARGERROR );
178
        lua_pushstring(L, (uid >= 0) ? "init" : "open");
345 Oobles 179
 
350 Oobles 180
        return 3;
345 Oobles 181
}
182
 
141 shine 183
static int lua_usbActivate(lua_State *L)
184
{
185
        if (lua_gettop(L) != 0) return luaL_error(L, "wrong number of arguments");
186
        if (usbActivated) return 0;
187
 
188
        static int modulesLoaded = 0;
189
        if (!modulesLoaded) {
190
                //start necessary drivers 
191
                LoadStartModule("flash0:/kd/semawm.prx");
192
                LoadStartModule("flash0:/kd/usbstor.prx");
193
                LoadStartModule("flash0:/kd/usbstormgr.prx");
194
                LoadStartModule("flash0:/kd/usbstorms.prx");
195
                LoadStartModule("flash0:/kd/usbstorboot.prx");
196
 
197
                //setup USB drivers 
198
                int retVal = sceUsbStart(PSP_USBBUS_DRIVERNAME, 0, 0);
199
                if (retVal != 0) {
200
                        printf("Error starting USB Bus driver (0x%08X)\n", retVal);
201
                        sceKernelSleepThread();
202
                }
203
                retVal = sceUsbStart(PSP_USBSTOR_DRIVERNAME, 0, 0);
204
                if (retVal != 0) {
205
                        printf("Error starting USB Mass Storage driver (0x%08X)\n", retVal);
206
                        sceKernelSleepThread();
207
                }
208
                retVal = sceUsbstorBootSetCapacity(0x800000);
209
                if (retVal != 0) {
210
                        printf("Error setting capacity with USB Mass Storage driver (0x%08X)\n", retVal);
211
                        sceKernelSleepThread();
212
                }
213
                retVal = 0;
214
                modulesLoaded = 1;
215
        }
216
        sceUsbActivate(0x1c8);
217
        usbActivated = 1;
218
        return 0;
219
}
220
 
221
static int lua_usbDeactivate(lua_State *L)
222
{
223
        if (lua_gettop(L) != 0) return luaL_error(L, "wrong number of arguments");
224
        if (!usbActivated) return 0;
225
 
345 Oobles 226
        sceUsbDeactivate( 0 );  // what value here?
141 shine 227
        usbActivated = 0;
228
        return 0;
229
}
230
 
213 shine 231
// battery functions
232
 
233
static int lua_powerIsPowerOnline(lua_State *L)
234
{
235
        if (lua_gettop(L) != 0) return luaL_error(L, "wrong number of arguments");
236
        lua_pushboolean(L, scePowerIsPowerOnline());
237
        return 1;
238
}
239
 
240
static int lua_powerIsBatteryExist(lua_State *L)
241
{
242
        if (lua_gettop(L) != 0) return luaL_error(L, "wrong number of arguments");
243
        lua_pushboolean(L, scePowerIsBatteryExist());
244
        return 1;
245
}
246
 
247
static int lua_powerIsBatteryCharging(lua_State *L)
248
{
249
        if (lua_gettop(L) != 0) return luaL_error(L, "wrong number of arguments");
250
        lua_pushboolean(L, scePowerIsBatteryCharging());
251
        return 1;
252
}
253
 
254
static int lua_powerGetBatteryChargingStatus(lua_State *L)
255
{
256
        if (lua_gettop(L) != 0) return luaL_error(L, "wrong number of arguments");
257
        lua_pushnumber(L, scePowerGetBatteryChargingStatus());
258
        return 1;
259
}
260
 
261
static int lua_powerIsLowBattery(lua_State *L)
262
{
263
        if (lua_gettop(L) != 0) return luaL_error(L, "wrong number of arguments");
264
        lua_pushboolean(L, scePowerIsLowBattery());
265
        return 1;
266
}
267
 
268
static int lua_powerGetBatteryLifePercent(lua_State *L)
269
{
270
        if (lua_gettop(L) != 0) return luaL_error(L, "wrong number of arguments");
271
        lua_pushnumber(L, scePowerGetBatteryLifePercent());
272
        return 1;
273
}
274
 
275
static int lua_powerGetBatteryLifeTime(lua_State *L)
276
{
277
        if (lua_gettop(L) != 0) return luaL_error(L, "wrong number of arguments");
278
        lua_pushnumber(L, scePowerGetBatteryLifeTime());
279
        return 1;
280
}
281
 
282
static int lua_powerGetBatteryTemp(lua_State *L)
283
{
284
        if (lua_gettop(L) != 0) return luaL_error(L, "wrong number of arguments");
285
        lua_pushnumber(L, scePowerGetBatteryTemp());
286
        return 1;
287
}
288
 
289
static int lua_powerGetBatteryVolt(lua_State *L)
290
{
291
        if (lua_gettop(L) != 0) return luaL_error(L, "wrong number of arguments");
292
        lua_pushnumber(L, scePowerGetBatteryVolt());
293
        return 1;
294
}
295
 
335 shine 296
static int lua_powerTick(lua_State *L)
297
{
298
        if (lua_gettop(L) != 0) return luaL_error(L, "wrong number of arguments");
299
        scePowerTick(0);
300
        return 0;
301
}
302
 
215 shine 303
static int lua_md5sum(lua_State *L)
304
{
305
        size_t size;
306
        const char *string = luaL_checklstring(L, 1, &size);
307
        if (!string) return luaL_error(L, "Argument error: System.md5sum(string) takes a string as argument.");
308
 
309
        u8 digest[16];
310
        sceKernelUtilsMd5Digest((u8*)string, size, digest);
311
        int i;
312
        char result[33];
313
        for (i = 0; i < 16; i++) sprintf(result + 2 * i, "%02x", digest[i]);
314
        lua_pushstring(L, result);
315
 
316
        return 1;
317
}
318
 
232 shine 319
static int lua_sioInit(lua_State *L)
320
{
321
        if (lua_gettop(L) != 1) return luaL_error(L, "baud rate expected.");
322
        int baudRate = luaL_checkint(L, 1);
323
        if (sio_fd < 0) sio_fd = sceIoOpen("sio:", PSP_O_RDWR, 0);
324
        if (sio_fd < 0) return luaL_error(L, "failed create SIO handle.");
325
        sceIoIoctl(sio_fd, SIO_IOCTL_SET_BAUD_RATE, &baudRate, sizeof(baudRate), NULL, 0);
326
 
327
        return 0;
328
}
329
 
330
static int lua_sioWrite(lua_State *L)
331
{
332
        if (sio_fd < 0) return luaL_error(L, sioNotInitialized);
333
        size_t size;
334
        const char *string = luaL_checklstring(L, 1, &size);
335
        if (!string) return luaL_error(L, "Argument error: System.sioWrite(string) takes a string as argument.");
336
        sceIoWrite(sio_fd, string, size);
337
 
338
        return 0;
339
}
340
 
341
static int lua_sioRead(lua_State *L)
342
{
343
        if (sio_fd < 0) return luaL_error(L, sioNotInitialized);
344
        if (lua_gettop(L) != 0) return luaL_error(L, "no arguments expected.");
345
        char data[256];
346
        int count = sceIoRead(sio_fd, data, 256);
347
        if (count > 0) {
348
                lua_pushlstring(L, data, count);
349
        } else {
350
                lua_pushstring(L, "");
351
        }
352
 
353
        return 1;
354
}
355
 
283 shine 356
static int lua_irdaInit(lua_State *L)
357
{
358
        if (lua_gettop(L) != 0) return luaL_error(L, "no arguments expected.");
359
        if (irda_fd < 0) irda_fd = sceIoOpen("irda0:", PSP_O_RDWR, 0);
360
        if (irda_fd < 0) return luaL_error(L, "failed create IRDA handle.");
361
 
362
        return 0;
363
}
364
 
365
static int lua_irdaWrite(lua_State *L)
366
{
367
        if (irda_fd < 0) return luaL_error(L, "irda not initialized");
368
        size_t size;
369
        const char *string = luaL_checklstring(L, 1, &size);
370
        if (!string) return luaL_error(L, "Argument error: System.sioWrite(string) takes a string as argument.");
371
        sceIoWrite(irda_fd, string, size);
372
 
373
        return 0;
374
}
375
 
376
static int lua_irdaRead(lua_State *L)
377
{
378
        if (irda_fd < 0) return luaL_error(L, "irda not initialized");
379
        if (lua_gettop(L) != 0) return luaL_error(L, "no arguments expected.");
380
        char data[256];
381
        int count = sceIoRead(irda_fd, &data, 256);
382
        if (count > 0) {
383
                lua_pushlstring(L, data, count);
384
        } else {
385
                lua_pushstring(L, "");
386
        }
387
 
388
        return 1;
389
}
390
 
234 shine 391
static int lua_sleep(lua_State *L)
392
{
393
        if (lua_gettop(L) != 1) return luaL_error(L, "milliseconds expected.");
394
        int milliseconds = luaL_checkint(L, 1);
395
        sceKernelDelayThread(milliseconds * 1000);
396
        return 0;
397
}
398
 
291 shine 399
static int lua_getFreeMemory(lua_State *L)
400
{
401
        if (lua_gettop(L) != 0) return luaL_error(L, "no arguments expected.");
402
 
403
        // for a realistic number, try to allocate 1 mb blocks until out of memory
404
        void* buf[64];
405
        int i = 0;
406
        for (i = 0; i < 64; i++) {
407
                buf[i] = malloc(1024 * 1024);
408
                if (!buf[i]) break;
409
        }
410
        int result = i;
411
        for (; i >= 0; i--) {
412
                free(buf[i]);
413
        }
414
        lua_pushnumber(L, result * 1024 * 1024);
415
        return 1;
416
}
417
 
140 nevyn 418
static const luaL_reg System_functions[] = {
213 shine 419
  {"currentDirectory",              lua_curdir},
420
  {"listDirectory",                 lua_dir},
335 shine 421
  {"createDirectory",               lua_createDir},
422
  {"removeDirectory",               lua_removeDir},
423
  {"removeFile",                    lua_removeFile},
213 shine 424
  {"usbDiskModeActivate",           lua_usbActivate},
425
  {"usbDiskModeDeactivate",         lua_usbDeactivate},
426
  {"powerIsPowerOnline",            lua_powerIsPowerOnline},
427
  {"powerIsBatteryExist",           lua_powerIsBatteryExist},
428
  {"powerIsBatteryCharging",        lua_powerIsBatteryCharging},
429
  {"powerGetBatteryChargingStatus", lua_powerGetBatteryChargingStatus},
430
  {"powerIsLowBattery",             lua_powerIsLowBattery},
431
  {"powerGetBatteryLifePercent",    lua_powerGetBatteryLifePercent},
432
  {"powerGetBatteryLifeTime",       lua_powerGetBatteryLifeTime},
433
  {"powerGetBatteryTemp",           lua_powerGetBatteryTemp},
434
  {"powerGetBatteryVolt",           lua_powerGetBatteryVolt},
335 shine 435
  {"powerTick",                     lua_powerTick},
215 shine 436
  {"md5sum",                        lua_md5sum},
232 shine 437
  {"sioInit",                       lua_sioInit},
438
  {"sioRead",                       lua_sioRead},
439
  {"sioWrite",                      lua_sioWrite},
283 shine 440
  {"irdaInit",                      lua_irdaInit},
441
  {"irdaRead",                      lua_irdaRead},
442
  {"irdaWrite",                     lua_irdaWrite},
234 shine 443
  {"sleep",                         lua_sleep},
291 shine 444
  {"getFreeMemory",                 lua_getFreeMemory},
140 nevyn 445
  {0, 0}
446
};
447
void luaSystem_init(lua_State *L) {
345 Oobles 448
        setModulePath();
140 nevyn 449
        luaL_openlib(L, "System", System_functions, 0);
350 Oobles 450
        lua_register(L, "loadlib", lua_loadModule);
140 nevyn 451
}
452