Add memtest support.
[syslinux-debian/hramrach.git] / com32 / lua / src / vesa.c
blob06649e11f6d1f6d3debe2a4c8cc974fefa745b5e
1 #include <stdlib.h>
2 #include <string.h>
4 #include "lua.h"
5 #include "lauxlib.h"
6 #include "lualib.h"
7 #include "../../include/console.h"
8 #include "../../lib/sys/vesa/vesa.h"
9 #include "../../lib/sys/vesa/video.h"
11 int vesacon_load_background(const char *filename);
13 static int vesa_getmodes(lua_State *L)
15 com32sys_t rm;
16 uint16_t mode, *mode_ptr;
17 struct vesa_general_info *gi;
18 struct vesa_mode_info *mi;
19 int nmode = 1;
20 int rv = -1;
22 gi = lmalloc(sizeof *gi);
23 if (!gi)
24 return -1;
26 mi = lmalloc(sizeof *mi);
27 if (!mi)
28 goto out;
30 memset(&rm, 0, sizeof rm);
31 memset(gi, 0, sizeof *gi);
33 gi->signature = VBE2_MAGIC; /* Get VBE2 extended data */
34 rm.eax.w[0] = 0x4F00; /* Get SVGA general information */
35 rm.edi.w[0] = OFFS(gi);
36 rm.es = SEG(gi);
37 __intcall(0x10, &rm, &rm);
39 if ( rm.eax.w[0] != 0x004F )
40 goto out; /* Function call failed */
41 if ( gi->signature != VESA_MAGIC ) {
42 rv = -2; /* No magic */
43 goto out;
45 if ( gi->version < 0x0102 ) {
46 rv = -3; /* VESA 1.2+ required */
47 goto out;
50 lua_newtable(L); /* list of modes */
52 /* Copy general info */
53 memcpy(&__vesa_info.gi, gi, sizeof *gi);
55 /* Search for a 640x480 mode with a suitable color and memory model... */
57 mode_ptr = GET_PTR(gi->video_mode_ptr);
59 while ((mode = *mode_ptr++) != 0xFFFF) {
60 mode &= 0x1FF; /* The rest are attributes of sorts */
62 printf("Found mode: 0x%04x (%dx%dx%d)\n", mode, mi->h_res, mi->v_res, mi->bpp);
64 memset(mi, 0, sizeof *mi);
65 rm.eax.w[0] = 0x4F01; /* Get SVGA mode information */
66 rm.ecx.w[0] = mode;
67 rm.edi.w[0] = OFFS(mi);
68 rm.es = SEG(mi);
69 __intcall(0x10, &rm, &rm);
71 /* Must be a supported mode */
72 if ( rm.eax.w[0] != 0x004f )
73 continue;
75 lua_pushnumber(L, nmode++);
76 lua_newtable(L); /* mode info */
78 lua_pushstring(L, "mode");
79 lua_pushnumber(L, mode);
80 lua_settable(L,-3);
82 lua_pushstring(L, "hres");
83 lua_pushnumber(L, mi->h_res);
84 lua_settable(L,-3);
86 lua_pushstring(L, "vres");
87 lua_pushnumber(L, mi->v_res);
88 lua_settable(L,-3);
90 lua_pushstring(L, "bpp");
91 lua_pushnumber(L, mi->bpp);
92 lua_settable(L,-3);
94 lua_settable(L, -3); /* add to mode list */
98 rv = 1;
99 out:
100 lfree(mi);
101 lfree(gi);
102 return rv;
106 static int vesa_setmode(lua_State *L)
108 /* Preventing GCC to complain about unused L*/
109 L=L;
110 openconsole(&dev_rawcon_r, &dev_vesaserial_w);
112 return 0;
116 static int vesa_load_background(lua_State *L)
118 const char *filename = luaL_checkstring(L, 1);
120 vesacon_load_background(filename);
122 return 0;
125 static const luaL_reg vesalib[] = {
126 {"getmodes", vesa_getmodes},
127 {"setmode", vesa_setmode},
128 {"load_background", vesa_load_background},
129 {NULL, NULL}
132 /* This defines a function that opens up your library. */
134 LUALIB_API int luaopen_vesa (lua_State *L) {
135 luaL_openlib(L, LUA_VESALIBNAME, vesalib, 0);
136 return 1;