Initial code release
[external/syslinux.git] / com32 / lua / src / vesa.c
1 #include <stdlib.h>
2 #include <string.h>
3
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"
10
11 int vesacon_load_background(const char *filename);
12
13 static int vesa_getmodes(lua_State *L)
14 {
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
21   /* Allocate space in the bounce buffer for these structures */
22   gi = &((struct vesa_info *)__com32.cs_bounce)->gi;
23   mi = &((struct vesa_info *)__com32.cs_bounce)->mi;
24
25   memset(&rm, 0, sizeof rm);
26   memset(gi, 0, sizeof *gi);
27
28   gi->signature = VBE2_MAGIC;   /* Get VBE2 extended data */
29   rm.eax.w[0] = 0x4F00;         /* Get SVGA general information */
30   rm.edi.w[0] = OFFS(gi);
31   rm.es      = SEG(gi);
32   __intcall(0x10, &rm, &rm);
33
34   if ( rm.eax.w[0] != 0x004F )
35     return -1;                   /* Function call failed */
36   if ( gi->signature != VESA_MAGIC )
37     return -2;                   /* No magic */
38   if ( gi->version < 0x0102 )
39     return -3;                   /* VESA 1.2+ required */
40
41   lua_newtable(L);      /* list of modes */
42
43   /* Copy general info */
44   memcpy(&__vesa_info.gi, gi, sizeof *gi);
45
46   /* Search for a 640x480 mode with a suitable color and memory model... */
47
48   mode_ptr = GET_PTR(gi->video_mode_ptr);
49
50   while ((mode = *mode_ptr++) != 0xFFFF) {
51     mode &= 0x1FF;              /* The rest are attributes of sorts */
52
53     printf("Found mode: 0x%04x (%dx%dx%d)\n", mode, mi->h_res, mi->v_res, mi->bpp);
54
55     memset(mi, 0, sizeof *mi);
56     rm.eax.w[0] = 0x4F01;       /* Get SVGA mode information */
57     rm.ecx.w[0] = mode;
58     rm.edi.w[0] = OFFS(mi);
59     rm.es  = SEG(mi);
60     __intcall(0x10, &rm, &rm);
61
62     /* Must be a supported mode */
63     if ( rm.eax.w[0] != 0x004f )
64       continue;
65
66     lua_pushnumber(L, nmode++);
67     lua_newtable(L); /* mode info */
68
69     lua_pushstring(L, "mode");
70     lua_pushnumber(L, mode);
71     lua_settable(L,-3);
72
73     lua_pushstring(L, "hres");
74     lua_pushnumber(L, mi->h_res);
75     lua_settable(L,-3);
76
77     lua_pushstring(L, "vres");
78     lua_pushnumber(L, mi->v_res);
79     lua_settable(L,-3);
80
81     lua_pushstring(L, "bpp");
82     lua_pushnumber(L, mi->bpp);
83     lua_settable(L,-3);
84
85     lua_settable(L, -3); /* add to mode list */
86
87   }
88
89   return 1;
90 }
91
92
93 static int vesa_setmode(lua_State *L)
94 {
95   /* Preventing GCC to complain about unused L*/
96   L=L;
97   openconsole(&dev_rawcon_r, &dev_vesaserial_w);
98
99   return 0;
100 }
101
102
103 static int vesa_load_background(lua_State *L)
104 {
105   const char *filename = luaL_checkstring(L, 1);
106
107   vesacon_load_background(filename);
108
109   return 0;
110 }
111
112 static const luaL_reg vesalib[] = {
113   {"getmodes", vesa_getmodes},
114   {"setmode", vesa_setmode},
115   {"load_background", vesa_load_background},
116   {NULL, NULL}
117 };
118
119 /* This defines a function that opens up your library. */
120
121 LUALIB_API int luaopen_vesa (lua_State *L) {
122   luaL_openlib(L, LUA_VESALIBNAME, vesalib, 0);
123   return 1;
124 }
125