Debian Codegraph @

dokidoki-support/luaglfw.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 //========================================================================
2 // GLFW - An OpenGL framework
3 // File: luaglfw.c
4 // Platform: Lua 5.0
5 // API version: 2.5
6 // WWW: http://glfw.sourceforge.net
7 //------------------------------------------------------------------------
8 // Copyright (c) 2002-2005 Camilla Berglund
9 //
10 // This software is provided 'as-is', without any express or implied
11 // warranty. In no event will the authors be held liable for any damages
12 // arising from the use of this software.
13 //
14 // Permission is granted to anyone to use this software for any purpose,
15 // including commercial applications, and to alter it and redistribute it
16 // freely, subject to the following restrictions:
17 //
18 // 1. The origin of this software must not be misrepresented; you must not
19 // claim that you wrote the original software. If you use this software
20 // in a product, an acknowledgment in the product documentation would
21 // be appreciated but is not required.
22 //
23 // 2. Altered source versions must be plainly marked as such, and must not
24 // be misrepresented as being the original software.
25 //
26 // 3. This notice may not be removed or altered from any source
27 // distribution.
28 //
29 //========================================================================
30
31 #include <lauxlib.h>
•32 #include <GLFW/glfw3.h>
33 #include <errno.h>
34 #include <time.h>
35 #include "luaglfw.h"
36
•37 static GLFWwindow* window;
38
39 //************************************************************************
40 //**** Internal type definitions ****
41 //************************************************************************
42
43 struct lua_constant
44 {
45 char *str;
46 int value;
47 };
48
49
50 //************************************************************************
51 //**** Handy functions ****
52 //************************************************************************
53
54 // Check the number of arguments on the Lua stack
55 static int badArgs( lua_State *L, int n, char const *name )
56 {
57 if( lua_gettop(L) != n )
58 {
59 lua_settop( L, 0 );
60 lua_pushstring( L, "Bad arguments passed to function: " );
61 lua_pushstring( L, name );
62 lua_concat( L, 2 );
63 lua_error( L );
64 return 1;
65 }
66 return 0;
67 }
68
69 // Get a numeric value from a Lua table
70 static void getNumber( lua_State *L, int index, char *key, lua_Number *num )
71 {
72 lua_pushstring( L, key );
73 if( index < 0 ) index --;
74 lua_rawget( L, index );
75 if( !lua_isnil( L, -1 ) )
76 {
77 *num = lua_tonumber( L, -1 );
78 }
79 lua_remove( L, -1 );
80 }
81
82 // Add constants to the table on top of the stack
83 static void addConstants( lua_State *L, struct lua_constant *cn )
84 {
85 while( cn->str )
86 {
87 lua_pushstring( L, cn->str );
88 lua_pushnumber( L, cn->value );
89 lua_rawset( L, -3 );
90 ++ cn;
91 }
92 }
93
94 // Report error for unsupported functions
95 static void unsupportedFunction( lua_State *L, char const *name )
96 {
97 lua_settop( L, 0 );
98 lua_pushstring( L, "Unsupported function: " );
99 lua_pushstring( L, name );
100 lua_concat( L, 2 );
101 lua_error( L );
102 }
103
104 static void pushVideoMode( lua_State *L, const GLFWvidmode *mode)
105 {
106 lua_newtable( L );
107 lua_pushstring( L, "Width" );
•108 lua_pushnumber( L, mode->width );
109 lua_rawset( L, -3 );
110 lua_pushstring( L, "Height" );
•111 lua_pushnumber( L, mode->height );
112 lua_rawset( L, -3 );
113 lua_pushstring( L, "RedBits" );
•114 lua_pushnumber( L, mode->redBits );
115 lua_rawset( L, -3 );
116 lua_pushstring( L, "GreenBits" );
•117 lua_pushnumber( L, mode->greenBits );
118 lua_rawset( L, -3 );
119 lua_pushstring( L, "BlueBits" );
•120 lua_pushnumber( L, mode->blueBits );
121 lua_rawset( L, -3 );
122 }
123
124
125 //************************************************************************
126 //*** Callback wrapper functions ****
127 //************************************************************************
128
129 static lua_State * callback_lua_state = (lua_State *) 0;
130
131 static const char * windowsize_name;
132 static const char * windowclose_name;
133 static const char * windowrefresh_name;
134 static const char * mousebutton_name;
135 static const char * mousepos_name;
136 static const char * key_name;
137 static const char * char_name;
138
139
•140 void luaglfw_windowsizefun( GLFWwindow* window_dummy, int w, int h )
141 {
142 lua_State *L = callback_lua_state;
143 if( L == NULL ) return;
144
145 lua_getglobal( L, windowsize_name );
146 if( lua_isfunction( L, -1 ) )
147 {
148 lua_pushnumber( L, (lua_Number)w );
149 lua_pushnumber( L, (lua_Number)h );
150 lua_pcall( L, 2, 0, 0 );
151 }
152 }
153
•154 void luaglfw_windowclosefun( GLFWwindow* window_dummy )
155 {
156 lua_State *L = callback_lua_state;
157 int do_close = 1;
158
•159 if( L != NULL )
160 {
•161 lua_getglobal( L, windowclose_name );
162 if( lua_isfunction( L, -1 ) )
163 {
164 lua_pcall( L, 0, 1, 0 );
165 do_close = (int) lua_tonumber( L, -1 );
166 lua_pop( L, 1 );
167 }
168 }
•169
170 glfwSetWindowShouldClose(window, do_close);
171 }
172
•173 void luaglfw_windowrefreshfun( GLFWwindow* window_dummy )
174 {
175 lua_State *L = callback_lua_state;
176 if( L == NULL ) return;
177
178 lua_getglobal( L, windowrefresh_name );
179 if( lua_isfunction( L, -1 ) )
180 {
181 lua_pcall( L, 0, 0, 0 );
182 }
183 }
184
•185 void luaglfw_mousebuttonfun( GLFWwindow* window_dummy, int button, int action, int mods )
186 {
187 lua_State *L = callback_lua_state;
188 if( L == NULL ) return;
189
190 lua_getglobal( L, mousebutton_name );
191 if( lua_isfunction( L, -1 ) )
192 {
193 lua_pushnumber( L, (lua_Number)button );
194 lua_pushnumber( L, (lua_Number)action );
195 lua_pcall( L, 2, 0, 0 );
196 }
197 }
198
•199 void luaglfw_mouseposfun( GLFWwindow* window_dummy, double x, double y )
200 {
201 lua_State *L = callback_lua_state;
202 if( L == NULL ) return;
203
204 lua_getglobal( L, mousepos_name );
205 if( lua_isfunction( L, -1 ) )
206 {
•207 lua_pushnumber( L, (lua_Number)(int)x );
208 lua_pushnumber( L, (lua_Number)(int)y );
209 lua_pcall( L, 2, 0, 0 );
210 }
211 }
212
•213 void luaglfw_keyfun( GLFWwindow* window_dummy, int key, int scancode, int action, int mods )
214 {
215 lua_State *L = callback_lua_state;
216 if( L == NULL ) return;
217
•218 // Filter out GLFW_REPEAT actions
219 if (action == GLFW_REPEAT) return;
220
221 lua_getglobal( L, key_name );
222 if( lua_isfunction( L, -1 ) )
223 {
224 lua_pushnumber( L, (lua_Number)key );
225 lua_pushnumber( L, (lua_Number)action );
226 lua_pcall( L, 2, 0, 0 );
227 }
228 }
229
•230 void luaglfw_charfun( GLFWwindow* window_dummy, unsigned int key )
231 {
232 lua_State *L = callback_lua_state;
233 if( L == NULL ) return;
234
235 lua_getglobal( L, char_name );
236 if( lua_isfunction( L, -1 ) )
237 {
238 lua_pushnumber( L, (lua_Number)key );
•239 lua_pushnumber( L, (lua_Number)GLFW_PRESS );
240 lua_pcall( L, 2, 0, 0 );
241 }
242 }
243
244
245
246 //************************************************************************
247 //**** GLFW proxy functions ****
248 //************************************************************************
249
250
251 //========================================================================
252 // Init/termination & version info
253 //========================================================================
254
255 static int glfw_Init( lua_State *L )
256 {
257 int res;
258 lua_settop( L, 0 );
259 res = glfwInit();
260 lua_pushnumber( L, (lua_Number)res );
261 return 1;
262 }
263
264 static int glfw_Terminate( lua_State *L )
265 {
266 glfwTerminate();
•267 window = NULL;
268 return 0;
269 }
270
271 static int glfw_GetVersion( lua_State *L )
272 {
273 int major, minor, rev;
274 glfwGetVersion( &major, &minor, &rev );
275 lua_pushnumber( L, major );
276 lua_pushnumber( L, minor );
277 lua_pushnumber( L, rev );
278 return 3;
279 }
280
281
282 //========================================================================
283 // Window handling
284 //========================================================================
285
•286 #define GLFW2_WINDOW 0x00010001
287 #define GLFW2_FULLSCREEN 0x00010002
288
289 static int glfw_OpenWindow( lua_State *L )
290 {
291 lua_Number w, h, r, g, b, a, depth, stencil, mode;
•292 int res;
293
294 // Check arguments
295 if( badArgs( L, 9, "OpenWindow" ) ) return 0;
296
•297 // Disallow if a window is already open
298 if (window != NULL) return 0;
299
300 // Get all arguments to glfwOpenWindow
301 w = lua_tonumber( L, 1 );
302 h = lua_tonumber( L, 2 );
303 r = lua_tonumber( L, 3 );
304 g = lua_tonumber( L, 4 );
305 b = lua_tonumber( L, 5 );
306 a = lua_tonumber( L, 6 );
307 depth = lua_tonumber( L, 7 );
308 stencil = lua_tonumber( L, 8 );
309 mode = lua_tonumber( L, 9 );
310
•311 // Call glfwCreateWindow
312 lua_settop( L,0 );
•313 glfwWindowHint(GLFW_RED_BITS, (int)r);
314 glfwWindowHint(GLFW_GREEN_BITS, (int)g);
315 glfwWindowHint(GLFW_BLUE_BITS, (int)b);
316 glfwWindowHint(GLFW_ALPHA_BITS, (int)a);
317 glfwWindowHint(GLFW_DEPTH_BITS, (int)depth);
318 glfwWindowHint(GLFW_STENCIL_BITS, (int)stencil);
319
320 GLFWmonitor* monitor = (mode == GLFW2_FULLSCREEN) ? glfwGetPrimaryMonitor() : NULL;
321 window = glfwCreateWindow((int) w, (int)h, "Pax Britannica", monitor, NULL);
322 res = (window == NULL) ? GL_FALSE : GL_TRUE;
323
324 if (window != NULL)
325 glfwMakeContextCurrent(window);
326
327 // Return result
328 lua_pushnumber( L, (lua_Number)res );
329 return 1;
330 }
331
332 static int glfw_OpenWindowHint( lua_State *L )
333 {
334 lua_Number target, hint;
335 if( badArgs( L, 2, "OpenWindowHint" ) ) return 0;
336 target = lua_tonumber( L, 1 );
337 hint = lua_tonumber( L, 2 );
338 lua_settop( L, 0 );
•339 glfwWindowHint( (int)target, (int)hint );
340 return 0;
341 }
342
343 static int glfw_CloseWindow( lua_State *L )
344 {
•345 if (window != NULL)
346 {
347 glfwDestroyWindow(window);
348 window = NULL;
349 }
350
351 return 0;
352 }
353
354 static int glfw_SetWindowTitle( lua_State *L )
355 {
356 const char *str;
357 if( badArgs( L, 1, "SetWindowTitle" ) ) return 0;
•358
359 // Disallow if a window is not open
360 if (window == NULL) return 0;
361
362 str = lua_tostring( L, 1 );
363 lua_remove( L, 1 );
•364 glfwSetWindowTitle(window, str);
365 return 0;
366 }
367
368 static int glfw_SetWindowSize( lua_State *L )
369 {
370 lua_Number w, h;
371 if( badArgs( L, 2, "SetWindowSize" ) ) return 0;
•372
373 // Disallow if a window is not open
374 if (window == NULL) return 0;
375
376 w = lua_tonumber( L, 1 );
377 h = lua_tonumber( L, 2 );
378 lua_settop( L, 0 );
•379 glfwSetWindowSize(window, (int)w, (int)h);
380 return 0;
381 }
382
383 static int glfw_GetWindowSize( lua_State *L )
384 {
385 int w, h;
•386
387 // Disallow if a window is not open
388 if (window == NULL) return 0;
389
390 glfwGetWindowSize(window, &w, &h);
391 lua_settop( L, 0 );
392 lua_pushnumber( L, (lua_Number)w );
393 lua_pushnumber( L, (lua_Number)h );
394 return 2;
395 }
396
397 static int glfw_SetWindowPos( lua_State *L )
398 {
399 lua_Number x, y;
400 if( badArgs( L, 2, "SetWindowPos" ) ) return 0;
•401
402 // Disallow if a window is not open
403 if (window == NULL) return 0;
404
405 x = lua_tonumber( L, 1 );
406 y = lua_tonumber( L, 2 );
407 lua_settop( L, 0 );
•408 glfwSetWindowPos(window, (int)x, (int)y);
409 return 0;
410 }
411
412 static int glfw_IconifyWindow( lua_State *L )
413 {
•414 // Disallow if a window is not open
415 if (window == NULL) return 0;
416
417 glfwIconifyWindow(window);
418 return 0;
419 }
420
421 static int glfw_RestoreWindow( lua_State *L )
422 {
•423 // Disallow if a window is not open
424 if (window == NULL) return 0;
425
426 glfwRestoreWindow(window);
427 return 0;
428 }
429
•430 #define GLFW2_OPENED 0x00020001
431 #define GLFW2_ACCELERATED 0x00020004
432
433 static int glfw_GetWindowParam( lua_State *L )
434 {
435 lua_Number n;
436 if( badArgs( L, 1, "GetWindowParam" ) ) return 0;
•437
438 // Disallow if a window is not open
439 if (window == NULL) return 0;
440
441 n = lua_tonumber( L, 1 );
442 lua_settop( L, 0 );
•443
444 // Handle special cases
445 switch ((int) n)
446 {
447 case GLFW2_OPENED:
448 n = !glfwWindowShouldClose(window);
449 break;
450 case GLFW2_ACCELERATED:
451 n = GL_TRUE;
452 break;
453 default:
454 n = glfwGetWindowAttrib(window, (int)n);
455 break;
456 }
457
458 lua_pushnumber( L, n );
459 return 1;
460 }
461
462
463
464 //========================================================================
465 // Buffer swapping
466 //========================================================================
467
468 static int glfw_SwapBuffers( lua_State *L )
469 {
•470 // Disallow if a window is not open
471 if (window == NULL) return 0;
472
473 glfwSwapBuffers(window);
474 glfwPollEvents();
475 return 0;
476 }
477
478 static int glfw_SwapInterval( lua_State *L )
479 {
480 lua_Number interval;
481 if( badArgs( L, 1, "SwapInterval" ) ) return 0;
482 interval = lua_tonumber( L, 1 );
483 lua_remove( L, 1 );
484 glfwSwapInterval( (int)interval );
485 return 0;
486 }
487
488
489 //========================================================================
490 // Video modes
491 //========================================================================
492
493 static int glfw_GetVideoModes( lua_State *L )
494 {
495 int modecount, i;
•496 const GLFWvidmode* modes = glfwGetVideoModes(glfwGetPrimaryMonitor(), &modecount);
497
498 lua_newtable( L );
499 for( i = 0; i < modecount; i++ )
500 {
501 pushVideoMode( L, &modes[i] );
502 lua_rawseti( L, -2, i + 1 );
503 }
504
505 return 1;
506 }
507
508 static int glfw_GetDesktopMode( lua_State *L )
509 {
•510 pushVideoMode( L, glfwGetVideoMode(glfwGetPrimaryMonitor()) );
511
512 return 1;
513 }
514
515
516 //========================================================================
517 // Event polling/waiting
518 //========================================================================
519
520 static int glfw_PollEvents( lua_State *L )
521 {
522 glfwPollEvents();
523 return 0;
524 }
525
526 static int glfw_WaitEvents( lua_State *L )
527 {
528 glfwWaitEvents();
529 return 0;
530 }
531
532
533 //========================================================================
534 // Key and mouse input
535 //========================================================================
536
537 static int glfw_GetKey( lua_State *L )
538 {
539 int n = 0;
540
541 if( badArgs( L, 1, "GetKey" ) ) return 0;
542
•543 // Disallow if a window is not open
544 if (window == NULL) return 0;
545
546 switch( lua_type( L, 1 ) )
547 {
548 case LUA_TSTRING:
549 n = (lua_tostring(L,1)[0]);
550 break;
551
552 case LUA_TNUMBER:
553 n = lua_tonumber(L,1);
554 break;
555 }
556
557 lua_settop( L, 0 );
•558 n = glfwGetKey(window, n);
559 lua_pushnumber( L, n );
560 return 1;
561 }
562
563 static int glfw_GetMouseButton( lua_State *L )
564 {
565 lua_Number n;
566 if( badArgs( L, 1, "GetMouseButton" ) ) return 0;
•567
568 // Disallow if a window is not open
569 if (window == NULL) return 0;
570
571 n = lua_tonumber( L, 1 );
572 lua_settop( L, 0 );
•573 lua_pushnumber( L, glfwGetMouseButton(window, (int)n) );
574 return 1;
575 }
576
577 static int glfw_GetMousePos( lua_State *L )
578 {
•579 double x, y;
580
581 // Disallow if a window is not open
582 if (window == NULL) return 0;
583
584 glfwGetCursorPos(window, &x, &y);
585 lua_settop( L, 0 );
586 lua_pushnumber( L, (lua_Number)x );
587 lua_pushnumber( L, (lua_Number)y );
588 return 2;
589 }
590
591 static int glfw_SetMousePos( lua_State *L )
592 {
593 lua_Number x, y;
594 if( badArgs( L, 2, "SetMousePos" ) ) return 0;
•595
596 // Disallow if a window is not open
597 if (window == NULL) return 0;
598
599 x = lua_tonumber( L, 1 );
600 y = lua_tonumber( L, 2 );
601 lua_settop( L, 0 );
•602 glfwSetCursorPos(window, (double)x, (double)y);
603 return 0;
604 }
605
606 static int glfw_GetMouseWheel( lua_State *L )
607 {
•608 unsupportedFunction(L, "GetMouseWheel");
609 return 0;
610 }
611
612 static int glfw_SetMouseWheel( lua_State *L )
613 {
•614 unsupportedFunction(L, "SetMouseWheel");
615 return 0;
616 }
617
618
619 //========================================================================
620 // Joystick input
621 //========================================================================
622
•623 #define GLFW2_PRESENT 0x00050001
624 #define GLFW2_AXES 0x00050002
625 #define GLFW2_BUTTONS 0x00050003
626
627 static int glfw_GetJoystickParam( lua_State *L )
628 {
629 lua_Number joy, param;
630 int res;
631 if( badArgs( L, 2, "GetJoystickParam" ) ) return 0;
632 joy = lua_tonumber( L, 1 );
633 param = lua_tonumber( L, 2 );
634 lua_settop( L, 0 );
•635
636 switch((int) param)
637 {
638 case GLFW2_PRESENT:
639 res = glfwJoystickPresent(joy);
640 break;
641 case GLFW2_AXES:
642 glfwGetJoystickAxes(joy, &res);
643 break;
644 case GLFW2_BUTTONS:
645 glfwGetJoystickButtons(joy, &res);
646 break;
647 default:
648 res = 0;
649 }
650
651 lua_pushnumber( L, (lua_Number)res );
652 return 1;
653 }
654
655 static int glfw_GetJoystickPos( lua_State *L )
656 {
657 int joy, numaxes, res, i;
658
659 // Get arguments
660 if( badArgs( L, 2, "GetJoystickPos" ) ) return 0;
661 joy = (int)lua_tonumber( L, 1 );
662 numaxes = (int)lua_tonumber( L, 2 );
663 lua_settop( L, 0 );
664 if( numaxes < 1 ) return 0;
665
666 // Call GLFW funciton
•667 const float* pos = glfwGetJoystickAxes(joy, &res);
668 if (res > numaxes) res = numaxes;
669
670 // Create result array
671 lua_newtable( L );
672 for( i = 0; i < res; ++ i )
673 {
674 lua_pushnumber( L, (lua_Number)(i+1) );
675 lua_pushnumber( L, pos[ i ] );
676 lua_rawset( L, -3 );
677 }
678
679 return 1;
680 }
681
682 static int glfw_GetJoystickButtons( lua_State *L )
683 {
684 int joy, numbuttons, res, i;
685
686 // Get arguments
687 if( badArgs( L, 2, "GetJoystickButtons" ) ) return 0;
688 joy = (int)lua_tonumber( L, 1 );
689 numbuttons = (int)lua_tonumber( L, 2 );
690 lua_settop( L, 0 );
691 if( numbuttons < 1 ) return 0;
692
693 // Call GLFW funciton
•694 const unsigned char* buttons = glfwGetJoystickButtons(joy, &res);
695 if (res > numbuttons) res = numbuttons;
696
697 // Create result array
698 lua_newtable( L );
699 for( i = 0; i < res; ++ i )
700 {
701 lua_pushnumber( L, (lua_Number)(i+1) );
702 lua_pushnumber( L, (lua_Number)buttons[ i ] );
703 lua_rawset( L, -3 );
704 }
705
706 return 1;
707 }
708
709
710 //========================================================================
711 // Timing
712 //========================================================================
713
714 static int glfw_GetTime( lua_State *L )
715 {
716 lua_Number t;
717 t = glfwGetTime();
718 lua_settop( L, 0 );
719 lua_pushnumber( L, t );
720 return 1;
721 }
722
723 static int glfw_SetTime( lua_State *L )
724 {
725 lua_Number t;
726 if ( badArgs( L, 1, "SetTime" ) ) return 0;
727 t = lua_tonumber( L, 1 );
728 lua_settop( L, 0 );
729 glfwSetTime( (double)t );
730 return 0;
731 }
732
•733 #define ONE_BILLION 1000000000
734
735 static int glfw_Sleep( lua_State *L )
736 {
•737 // This function is no longer implemented by glfw, but is required for
738 // pax-britannica, so replace it with a nanosleep implementation.
739 double t;
740 if ( badArgs( L, 1, "Sleep" ) ) return 0;
•741 t = (double) lua_tonumber( L, 1 );
742 lua_settop( L, 0 );
•743
744 // Cap negative sleep durations
745 if (t < 0) t = 0;
746
747 // Perform the sleep
748 struct timespec req;
749 req.tv_sec = (time_t) t;
750 req.tv_nsec = (long) ((t - (time_t) t) * ONE_BILLION);
751
752 if (req.tv_nsec >= ONE_BILLION)
753 {
754 req.tv_sec++;
755 req.tv_nsec = 0;
756 }
757 else if (req.tv_nsec < 0)
758 {
759 req.tv_nsec = 0;
760 }
761
762 while (nanosleep(&req, &req) == -1 && errno == EINTR)
763 ;
764
765 return 0;
766 }
767
768
769 //========================================================================
770 // Threading support (not possible in Lua)
771 //========================================================================
772
773 static int glfw_CreateThread( lua_State *L )
774 {
775 unsupportedFunction( L, "CreateThread" );
776 return 0;
777 }
778
779 static int glfw_DestroyThread( lua_State *L )
780 {
781 unsupportedFunction( L, "DestroyThread" );
782 return 0;
783 }
784
785 static int glfw_WaitThread( lua_State *L )
786 {
787 unsupportedFunction( L, "WaitThread" );
788 return 0;
789 }
790
791 static int glfw_GetThreadID( lua_State *L )
792 {
793 unsupportedFunction( L, "GetThreadID" );
794 return 0;
795 }
796
797 static int glfw_CreateMutex( lua_State *L )
798 {
799 unsupportedFunction( L, "CreateMutex" );
800 return 0;
801 }
802
803 static int glfw_DestroyMutex( lua_State *L )
804 {
805 unsupportedFunction( L, "DestroyMutex" );
806 return 0;
807 }
808
809 static int glfw_LockMutex( lua_State *L )
810 {
811 unsupportedFunction( L, "LockMutex" );
812 return 0;
813 }
814
815 static int glfw_UnlockMutex( lua_State *L )
816 {
817 unsupportedFunction( L, "UnlockMutex" );
818 return 0;
819 }
820
821 static int glfw_CreateCond( lua_State *L )
822 {
823 unsupportedFunction( L, "CreateCond" );
824 return 0;
825 }
826
827 static int glfw_DestroyCond( lua_State *L )
828 {
829 unsupportedFunction( L, "DestroyCond" );
830 return 0;
831 }
832
833 static int glfw_WaitCond( lua_State *L )
834 {
835 unsupportedFunction( L, "WaitCond" );
836 return 0;
837 }
838
839 static int glfw_SignalCond( lua_State *L )
840 {
841 unsupportedFunction( L, "SignalCond" );
842 return 0;
843 }
844
845 static int glfw_BroadcastCond( lua_State *L )
846 {
847 unsupportedFunction( L, "BroadcastCond" );
848 return 0;
849 }
850
851 static int glfw_GetNumberOfProcessors( lua_State *L )
852 {
•853 unsupportedFunction( L, "GetNumberOfProcessors" );
854 return 0;
855 }
856
857
858 //========================================================================
859 // Extension handling
860 //========================================================================
861
862 static int glfw_GetGLVersion( lua_State *L )
863 {
•864 // GLFW3 requires an open window now, so most uses of this will fail anyway
865 unsupportedFunction( L, "GetGLVersion" );
866 return 0;
867 }
868
869 static int glfw_ExtensionSupported( lua_State *L )
870 {
871 const char *str;
872 int res;
873 if( badArgs( L, 1, "ExtensionSupported" ) ) return 0;
874 str = lua_tostring( L, 1 );
875 lua_settop( L, 0 );
876 res = glfwExtensionSupported( str );
877 lua_pushnumber( L, (lua_Number)res );
878 return 1;
879 }
880
881
882 //========================================================================
883 // Enable/Disable
884 //========================================================================
885
886 static int glfw_Enable( lua_State *L )
887 {
•888 unsupportedFunction( L, "Enable" );
889 return 0;
890 }
891
892 static int glfw_Disable( lua_State *L )
893 {
•894 unsupportedFunction( L, "Disable" );
895 return 0;
896 }
897
898
899 //========================================================================
900 // Image/texture I/O support
901 //========================================================================
902
903 static int glfw_ReadImage( lua_State *L )
904 {
905 unsupportedFunction( L, "ReadImage" );
906 return 0;
907 }
908
909 static int glfw_FreeImage( lua_State *L )
910 {
911 unsupportedFunction( L, "FreeImage" );
912 return 0;
913 }
914
915 static int glfw_LoadTexture2D( lua_State *L )
916 {
•917 unsupportedFunction( L, "LoadTexture2D" );
918 return 0;
919 }
920
921
922 //========================================================================
923 // Callback function management
924 //========================================================================
925
926 static int glfw_SetWindowSizeCallback( lua_State *L )
927 {
•928 // Disallow if a window is not open
929 if (window == NULL) return 0;
930
931 GLFWwindowsizefun fun = NULL;
932 if( lua_isstring( L, 1 ) )
933 {
934 windowsize_name = lua_tostring( L, 1 );
935 fun = luaglfw_windowsizefun;
936 }
•937 glfwSetWindowSizeCallback( window, fun );
938 return 0;
939 }
940
941 static int glfw_SetWindowCloseCallback( lua_State *L )
942 {
•943 // Disallow if a window is not open
944 if (window == NULL) return 0;
945
946 GLFWwindowclosefun fun = NULL;
947 if( lua_isstring( L, 1 ) )
948 {
949 windowclose_name = lua_tostring( L, 1 );
950 fun = luaglfw_windowclosefun;
951 }
•952 glfwSetWindowCloseCallback( window, fun );
953 return 0;
954 }
955
956 static int glfw_SetWindowRefreshCallback( lua_State *L )
957 {
•958 // Disallow if a window is not open
959 if (window == NULL) return 0;
960
961 GLFWwindowrefreshfun fun = NULL;
962 if( lua_isstring( L, 1 ) )
963 {
964 windowrefresh_name = lua_tostring( L, 1 );
965 fun = luaglfw_windowrefreshfun;
966 }
•967 glfwSetWindowRefreshCallback( window, fun );
968 return 0;
969 }
970
971 static int glfw_SetMouseButtonCallback( lua_State *L )
972 {
•973 // Disallow if a window is not open
974 if (window == NULL) return 0;
975
976 GLFWmousebuttonfun fun = NULL;
977 if( lua_isstring( L, 1 ) )
978 {
979 mousebutton_name = lua_tostring( L, 1 );
980 fun = luaglfw_mousebuttonfun;
981 }
•982 glfwSetMouseButtonCallback( window, fun );
983 return 0;
984 }
985
986 static int glfw_SetMousePosCallback( lua_State *L )
987 {
•988 // Disallow if a window is not open
989 if (window == NULL) return 0;
990
991 GLFWcursorposfun fun = NULL;
992 if( lua_isstring( L, 1 ) )
993 {
994 mousepos_name = lua_tostring( L, 1 );
995 fun = luaglfw_mouseposfun;
996 }
•997 glfwSetCursorPosCallback( window, fun );
998 return 0;
999 }
1000
1001 static int glfw_SetMouseWheelCallback( lua_State *L )
1002 {
•1003 unsupportedFunction( L, "SetMouseWheelCallback" );
1004 return 0;
1005 }
1006
1007 static int glfw_SetKeyCallback( lua_State *L )
1008 {
•1009 // Disallow if a window is not open
1010 if (window == NULL) return 0;
1011
1012 GLFWkeyfun fun = NULL;
1013 if( lua_isstring( L, 1 ) )
1014 {
1015 key_name = lua_tostring( L, 1 );
1016 fun = luaglfw_keyfun;
1017 }
•1018 glfwSetKeyCallback( window, fun );
1019 return 0;
1020 }
1021
1022 static int glfw_SetCharCallback( lua_State *L )
1023 {
•1024 // Disallow if a window is not open
1025 if (window == NULL) return 0;
1026
1027 GLFWcharfun fun = NULL;
1028 if( lua_isstring( L, 1 ) )
1029 {
1030 char_name = lua_tostring( L, 1 );
1031 fun = luaglfw_charfun;
1032 }
•1033 glfwSetCharCallback( window, fun );
1034 return 0;
1035 }
1036
1037
1038
1039 //************************************************************************
1040 //**** LUA Library Registration ****
1041 //************************************************************************
1042
1043 // GLFW constants are stored in the global Lua table "glfw"
1044 static struct lua_constant glfw_constants[] =
1045 {
1046 // GL constants (GL_TRUE/GL_FALSE)
1047 { "TRUE", GL_TRUE },
1048 { "FALSE", GL_FALSE },
1049
1050 // GLFW version
1051 { "VERSION_MAJOR", GLFW_VERSION_MAJOR },
1052 { "VERSION_MINOR", GLFW_VERSION_MINOR },
1053 { "VERSION_REVISION", GLFW_VERSION_REVISION },
1054
1055 // Key and button state/action definitions
1056 { "PRESS", GLFW_PRESS },
1057 { "RELEASE", GLFW_RELEASE },
1058
1059 // Keyboard key definitions
1060 { "KEY_SPACE", GLFW_KEY_SPACE },
•1061 { "KEY_ESC", GLFW_KEY_ESCAPE },
1062 { "KEY_F1", GLFW_KEY_F1 },
1063 { "KEY_F2", GLFW_KEY_F2 },
1064 { "KEY_F3", GLFW_KEY_F3 },
1065 { "KEY_F4", GLFW_KEY_F4 },
1066 { "KEY_F5", GLFW_KEY_F5 },
1067 { "KEY_F6", GLFW_KEY_F6 },
1068 { "KEY_F7", GLFW_KEY_F7 },
1069 { "KEY_F8", GLFW_KEY_F8 },
1070 { "KEY_F9", GLFW_KEY_F9 },
1071 { "KEY_F10", GLFW_KEY_F10 },
1072 { "KEY_F11", GLFW_KEY_F11 },
1073 { "KEY_F12", GLFW_KEY_F12 },
1074 { "KEY_F13", GLFW_KEY_F13 },
1075 { "KEY_F14", GLFW_KEY_F14 },
1076 { "KEY_F15", GLFW_KEY_F15 },
1077 { "KEY_F16", GLFW_KEY_F16 },
1078 { "KEY_F17", GLFW_KEY_F17 },
1079 { "KEY_F18", GLFW_KEY_F18 },
1080 { "KEY_F19", GLFW_KEY_F19 },
1081 { "KEY_F20", GLFW_KEY_F20 },
1082 { "KEY_F21", GLFW_KEY_F21 },
1083 { "KEY_F22", GLFW_KEY_F22 },
1084 { "KEY_F23", GLFW_KEY_F23 },
1085 { "KEY_F24", GLFW_KEY_F24 },
1086 { "KEY_F25", GLFW_KEY_F25 },
1087 { "KEY_UP", GLFW_KEY_UP },
1088 { "KEY_DOWN", GLFW_KEY_DOWN },
1089 { "KEY_LEFT", GLFW_KEY_LEFT },
1090 { "KEY_RIGHT", GLFW_KEY_RIGHT },
•1091 { "KEY_LSHIFT", GLFW_KEY_LEFT_SHIFT },
1092 { "KEY_RSHIFT", GLFW_KEY_RIGHT_SHIFT },
1093 { "KEY_LCTRL", GLFW_KEY_LEFT_CONTROL },
1094 { "KEY_RCTRL", GLFW_KEY_RIGHT_CONTROL },
1095 { "KEY_LALT", GLFW_KEY_LEFT_ALT },
1096 { "KEY_RALT", GLFW_KEY_RIGHT_ALT },
1097 { "KEY_TAB", GLFW_KEY_TAB },
1098 { "KEY_ENTER", GLFW_KEY_ENTER },
1099 { "KEY_BACKSPACE", GLFW_KEY_BACKSPACE },
1100 { "KEY_INSERT", GLFW_KEY_INSERT },
•1101 { "KEY_DEL", GLFW_KEY_DELETE },
1102 { "KEY_PAGEUP", GLFW_KEY_PAGE_UP },
1103 { "KEY_PAGEDOWN", GLFW_KEY_PAGE_DOWN },
1104 { "KEY_HOME", GLFW_KEY_HOME },
1105 { "KEY_END", GLFW_KEY_END },
1106 { "KEY_KP_0", GLFW_KEY_KP_0 },
1107 { "KEY_KP_1", GLFW_KEY_KP_1 },
1108 { "KEY_KP_2", GLFW_KEY_KP_2 },
1109 { "KEY_KP_3", GLFW_KEY_KP_3 },
1110 { "KEY_KP_4", GLFW_KEY_KP_4 },
1111 { "KEY_KP_5", GLFW_KEY_KP_5 },
1112 { "KEY_KP_6", GLFW_KEY_KP_6 },
1113 { "KEY_KP_7", GLFW_KEY_KP_7 },
1114 { "KEY_KP_8", GLFW_KEY_KP_8 },
1115 { "KEY_KP_9", GLFW_KEY_KP_9 },
1116 { "KEY_KP_DIVIDE", GLFW_KEY_KP_DIVIDE },
1117 { "KEY_KP_MULTIPLY", GLFW_KEY_KP_MULTIPLY },
1118 { "KEY_KP_SUBTRACT", GLFW_KEY_KP_SUBTRACT },
1119 { "KEY_KP_ADD", GLFW_KEY_KP_ADD },
1120 { "KEY_KP_DECIMAL", GLFW_KEY_KP_DECIMAL },
1121 { "KEY_KP_EQUAL", GLFW_KEY_KP_EQUAL },
1122 { "KEY_KP_ENTER", GLFW_KEY_KP_ENTER },
1123 { "KEY_LAST", GLFW_KEY_LAST },
1124
1125 // Mouse button definitions
1126 { "MOUSE_BUTTON_1", GLFW_MOUSE_BUTTON_1 },
1127 { "MOUSE_BUTTON_2", GLFW_MOUSE_BUTTON_2 },
1128 { "MOUSE_BUTTON_3", GLFW_MOUSE_BUTTON_3 },
1129 { "MOUSE_BUTTON_4", GLFW_MOUSE_BUTTON_4 },
1130 { "MOUSE_BUTTON_5", GLFW_MOUSE_BUTTON_5 },
1131 { "MOUSE_BUTTON_6", GLFW_MOUSE_BUTTON_6 },
1132 { "MOUSE_BUTTON_7", GLFW_MOUSE_BUTTON_7 },
1133 { "MOUSE_BUTTON_8", GLFW_MOUSE_BUTTON_8 },
1134 { "MOUSE_BUTTON_LAST", GLFW_MOUSE_BUTTON_LAST },
1135
1136 // Mouse button aliases
1137 { "MOUSE_BUTTON_LEFT", GLFW_MOUSE_BUTTON_LEFT },
1138 { "MOUSE_BUTTON_RIGHT", GLFW_MOUSE_BUTTON_RIGHT },
1139 { "MOUSE_BUTTON_MIDDLE", GLFW_MOUSE_BUTTON_MIDDLE },
1140
1141 // Joystick identifiers
1142 { "JOYSTICK_1", GLFW_JOYSTICK_1 },
1143 { "JOYSTICK_2", GLFW_JOYSTICK_2 },
1144 { "JOYSTICK_3", GLFW_JOYSTICK_3 },
1145 { "JOYSTICK_4", GLFW_JOYSTICK_4 },
1146 { "JOYSTICK_5", GLFW_JOYSTICK_5 },
1147 { "JOYSTICK_6", GLFW_JOYSTICK_6 },
1148 { "JOYSTICK_7", GLFW_JOYSTICK_7 },
1149 { "JOYSTICK_8", GLFW_JOYSTICK_8 },
1150 { "JOYSTICK_9", GLFW_JOYSTICK_9 },
1151 { "JOYSTICK_10", GLFW_JOYSTICK_10 },
1152 { "JOYSTICK_11", GLFW_JOYSTICK_11 },
1153 { "JOYSTICK_12", GLFW_JOYSTICK_12 },
1154 { "JOYSTICK_13", GLFW_JOYSTICK_13 },
1155 { "JOYSTICK_14", GLFW_JOYSTICK_14 },
1156 { "JOYSTICK_15", GLFW_JOYSTICK_15 },
1157 { "JOYSTICK_16", GLFW_JOYSTICK_16 },
1158 { "JOYSTICK_LAST", GLFW_JOYSTICK_LAST },
1159
1160 // glfwOpenWindow modes
•1161 { "WINDOW", GLFW2_WINDOW },
1162 { "FULLSCREEN", GLFW2_FULLSCREEN },
1163
1164 // glfwGetWindowParam tokens
•1165 { "OPENED", GLFW2_OPENED },
1166 { "ACTIVE", GLFW_FOCUSED },
1167 { "ICONIFIED", GLFW_ICONIFIED },
•1168 { "ACCELERATED", GLFW2_ACCELERATED },
1169 { "RED_BITS", GLFW_RED_BITS },
1170 { "GREEN_BITS", GLFW_GREEN_BITS },
1171 { "BLUE_BITS", GLFW_BLUE_BITS },
1172 { "ALPHA_BITS", GLFW_ALPHA_BITS },
1173 { "DEPTH_BITS", GLFW_DEPTH_BITS },
1174 { "STENCIL_BITS", GLFW_STENCIL_BITS },
1175
1176 // Constants for glfwGetWindowParam and glfwOpenWindowHint
1177 { "REFRESH_RATE", GLFW_REFRESH_RATE },
1178 { "ACCUM_RED_BITS", GLFW_ACCUM_RED_BITS },
1179 { "ACCUM_GREEN_BITS", GLFW_ACCUM_GREEN_BITS },
1180 { "ACCUM_BLUE_BITS", GLFW_ACCUM_BLUE_BITS },
1181 { "ACCUM_ALPHA_BITS", GLFW_ACCUM_ALPHA_BITS },
1182 { "AUX_BUFFERS", GLFW_AUX_BUFFERS },
1183 { "STEREO", GLFW_STEREO },
1184
1185 // glfwGetJoystickParam tokens
•1186 { "PRESENT", GLFW2_PRESENT },
1187 { "AXES", GLFW2_AXES },
1188 { "BUTTONS", GLFW2_BUTTONS },
1189
1190 { NULL, 0 }
1191 };
1192
1193
1194 // Library functions to register
1195 static const luaL_reg glfwlib[] = {
1196 { "Init", glfw_Init },
1197 { "Terminate", glfw_Terminate },
1198 { "GetVersion", glfw_GetVersion },
1199 { "OpenWindow", glfw_OpenWindow },
1200 { "OpenWindowHint", glfw_OpenWindowHint },
1201 { "CloseWindow", glfw_CloseWindow },
1202 { "SetWindowTitle", glfw_SetWindowTitle },
1203 { "SetWindowSize", glfw_SetWindowSize },
1204 { "GetWindowSize", glfw_GetWindowSize },
1205 { "SetWindowPos", glfw_SetWindowPos },
1206 { "IconifyWindow", glfw_IconifyWindow },
1207 { "RestoreWindow", glfw_RestoreWindow },
1208 { "GetWindowParam", glfw_GetWindowParam },
1209 { "SwapBuffers", glfw_SwapBuffers },
1210 { "SwapInterval", glfw_SwapInterval },
1211 { "GetVideoModes", glfw_GetVideoModes },
1212 { "GetDesktopMode", glfw_GetDesktopMode },
1213 { "PollEvents", glfw_PollEvents },
1214 { "WaitEvents", glfw_WaitEvents },
1215 { "GetKey", glfw_GetKey },
1216 { "GetMouseButton", glfw_GetMouseButton },
1217 { "GetMousePos", glfw_GetMousePos },
1218 { "SetMousePos", glfw_SetMousePos },
1219 { "GetMouseWheel", glfw_GetMouseWheel },
1220 { "SetMouseWheel", glfw_SetMouseWheel },
1221 { "GetJoystickParam", glfw_GetJoystickParam },
1222 { "GetJoystickPos", glfw_GetJoystickPos },
1223 { "GetJoystickButtons", glfw_GetJoystickButtons },
1224 { "GetTime", glfw_GetTime },
1225 { "SetTime", glfw_SetTime },
1226 { "Sleep", glfw_Sleep },
1227 { "GetGLVersion", glfw_GetGLVersion },
1228 { "ExtensionSupported", glfw_ExtensionSupported },
1229 { "CreateThread", glfw_CreateThread },
1230 { "DestroyThread", glfw_DestroyThread },
1231 { "WaitThread", glfw_WaitThread },
1232 { "GetThreadID", glfw_GetThreadID },
1233 { "CreateMutex", glfw_CreateMutex },
1234 { "DestroyMutex", glfw_DestroyMutex },
1235 { "LockMutex", glfw_LockMutex },
1236 { "UnlockMutex", glfw_UnlockMutex },
1237 { "CreateCond", glfw_CreateCond },
1238 { "DestroyCond", glfw_DestroyCond },
1239 { "WaitCond", glfw_WaitCond },
1240 { "SignalCond", glfw_SignalCond },
1241 { "BroadcastCond", glfw_BroadcastCond },
1242 { "GetNumberOfProcessors", glfw_GetNumberOfProcessors },
1243 { "Enable", glfw_Enable },
1244 { "Disable", glfw_Disable },
1245 { "ReadImage", glfw_ReadImage },
1246 { "FreeImage", glfw_FreeImage },
1247 { "LoadTexture2D", glfw_LoadTexture2D },
1248 { "SetWindowSizeCallback", glfw_SetWindowSizeCallback },
1249 { "SetWindowCloseCallback", glfw_SetWindowCloseCallback },
1250 { "SetWindowRefreshCallback", glfw_SetWindowRefreshCallback },
1251 { "SetMouseButtonCallback", glfw_SetMouseButtonCallback },
1252 { "SetMousePosCallback", glfw_SetMousePosCallback },
1253 { "SetMouseWheelCallback", glfw_SetMouseWheelCallback },
1254 { "SetKeyCallback", glfw_SetKeyCallback },
1255 { "SetCharCallback", glfw_SetCharCallback },
1256 { NULL, NULL }
1257 };
1258
1259
1260
1261 int luaopen_glfw( lua_State *L )
1262 {
1263 // Store GLFW functions in "glfw" table
1264 luaL_openlib( L, "glfw", glfwlib, 0 );
1265
1266 // Store GLFW constants in "glfw" table
1267 lua_pushstring( L, "glfw" );
1268 lua_gettable( L, LUA_GLOBALSINDEX );
1269 addConstants( L, glfw_constants );
1270 lua_settop( L, 0 );
1271
1272 // Remember Lua state for callback functions
1273 callback_lua_state = L;
1274
1275 return 0;
1276 }