-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdisplay.c
More file actions
80 lines (65 loc) · 1.24 KB
/
Copy pathdisplay.c
File metadata and controls
80 lines (65 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "a.h"
#define DISPLAY "Display"
typedef struct LDisplay LDisplay;
struct LDisplay
{
Display *d;
};
void
pushdisplay(lua_State *L, Display *d)
{
LDisplay *l;
l = (LDisplay*)lua_newuserdata(L, sizeof(LDisplay));
luaL_getmetatable(L, DISPLAY);
lua_setmetatable(L, -2);
l->d = d;
}
Display*
checkdisplay(lua_State *L, int index)
{
LDisplay *l;
l = (LDisplay*)luaL_checkudata(L, index, DISPLAY);
luaL_argcheck(L, l != NULL, index, "Display expected");
return l->d;
}
static int
display__gc(lua_State *L)
{
/* we do not GC the display */
lua_pushboolean(L, 0);
return 1;
}
static int
display__tostring(lua_State *L)
{
void *p;
p = lua_touserdata(L, 1);
lua_pushfstring(L, "display: %p", p);
return 1;
}
static int
display__index(lua_State *L)
{
Display *d;
const char *s;
d = checkdisplay(L, 1);
s = luaL_checkstring(L, 2);
if(strncmp(s, "white", 5) == 0)
pushimage(L, d->white);
else if(strncmp(s, "black", 5) == 0)
pushimage(L, d->black);
else
return 0;
return 1;
}
static const struct luaL_Reg display_funcs[] = {
{ "__gc", display__gc },
{ "__tostring", display__tostring },
{ "__index", display__index },
{ NULL, NULL },
};
void
registerdisplaymeta(lua_State *L)
{
createmetatable(L, DISPLAY, display_funcs);
}