Home | History | Annotate | Download | only in src
      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   int rv = -1;
     21 
     22   gi = lmalloc(sizeof *gi);
     23   if (!gi)
     24       return -1;
     25 
     26   mi = lmalloc(sizeof *mi);
     27   if (!mi)
     28       goto out;
     29 
     30   memset(&rm, 0, sizeof(rm));
     31   memset(gi, 0, sizeof *gi);
     32 
     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);
     38 
     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;
     44   }
     45   if ( gi->version < 0x0102 ) {
     46     rv = -3;                   /* VESA 1.2+ required */
     47     goto out;
     48   }
     49 
     50   lua_newtable(L);      /* list of modes */
     51 
     52   /* Copy general info */
     53   memcpy(&__vesa_info.gi, gi, sizeof *gi);
     54 
     55   /* Search for a 640x480 mode with a suitable color and memory model... */
     56 
     57   mode_ptr = GET_PTR(gi->video_mode_ptr);
     58 
     59   while ((mode = *mode_ptr++) != 0xFFFF) {
     60     mode &= 0x1FF;              /* The rest are attributes of sorts */
     61 
     62     printf("Found mode: 0x%04x (%dx%dx%d)\n", mode, mi->h_res, mi->v_res, mi->bpp);
     63 
     64     memset(&rm, 0, sizeof(rm));
     65     memset(mi, 0, sizeof *mi);
     66     rm.eax.w[0] = 0x4F01;       /* Get SVGA mode information */
     67     rm.ecx.w[0] = mode;
     68     rm.edi.w[0] = OFFS(mi);
     69     rm.es  = SEG(mi);
     70     __intcall(0x10, &rm, &rm);
     71 
     72     /* Must be a supported mode */
     73     if ( rm.eax.w[0] != 0x004f )
     74       continue;
     75 
     76     lua_pushnumber(L, nmode++);
     77     lua_newtable(L); /* mode info */
     78 
     79     lua_pushstring(L, "mode");
     80     lua_pushnumber(L, mode);
     81     lua_settable(L,-3);
     82 
     83     lua_pushstring(L, "hres");
     84     lua_pushnumber(L, mi->h_res);
     85     lua_settable(L,-3);
     86 
     87     lua_pushstring(L, "vres");
     88     lua_pushnumber(L, mi->v_res);
     89     lua_settable(L,-3);
     90 
     91     lua_pushstring(L, "bpp");
     92     lua_pushnumber(L, mi->bpp);
     93     lua_settable(L,-3);
     94 
     95     lua_settable(L, -3); /* add to mode list */
     96 
     97   }
     98 
     99   rv = 1;
    100 out:
    101   lfree(mi);
    102   lfree(gi);
    103   return rv;
    104 }
    105 
    106 
    107 static int vesa_setmode(lua_State *L)
    108 {
    109   /* Preventing GCC to complain about unused L*/
    110   L=L;
    111   openconsole(&dev_rawcon_r, &dev_vesaserial_w);
    112 
    113   return 0;
    114 }
    115 
    116 
    117 static int vesa_load_background(lua_State *L)
    118 {
    119   const char *filename = luaL_checkstring(L, 1);
    120 
    121   vesacon_load_background(filename);
    122 
    123   return 0;
    124 }
    125 
    126 static const luaL_Reg vesalib[] = {
    127   {"getmodes", vesa_getmodes},
    128   {"setmode", vesa_setmode},
    129   {"load_background", vesa_load_background},
    130   {NULL, NULL}
    131 };
    132 
    133 /* This defines a function that opens up your library. */
    134 
    135 LUALIB_API int luaopen_vesa (lua_State *L) {
    136   luaL_newlib(L, vesalib);
    137   return 1;
    138 }
    139 
    140