1 /*
2  * Copyright 2016 The Emscripten Authors.  All rights reserved.
3  * Emscripten is available under two separate licenses, the MIT license and the
4  * University of Illinois/NCSA Open Source License.  Both these licenses can be
5  * found in the LICENSE file.
6  */
7 
8 #include <stdio.h>
9 #include <emscripten.h>
10 #include <string.h>
11 #include <emscripten/html5.h>
12 
report_result(int result)13 void report_result(int result)
14 {
15   if (result == 0) {
16     printf("Test successful!\n");
17   } else {
18     printf("Test failed!\n");
19   }
20 #ifdef REPORT_RESULT
21   REPORT_RESULT(result);
22 #endif
23 }
24 
emscripten_result_to_string(EMSCRIPTEN_RESULT result)25 const char *emscripten_result_to_string(EMSCRIPTEN_RESULT result) {
26   if (result == EMSCRIPTEN_RESULT_SUCCESS) return "EMSCRIPTEN_RESULT_SUCCESS";
27   if (result == EMSCRIPTEN_RESULT_DEFERRED) return "EMSCRIPTEN_RESULT_DEFERRED";
28   if (result == EMSCRIPTEN_RESULT_NOT_SUPPORTED) return "EMSCRIPTEN_RESULT_NOT_SUPPORTED";
29   if (result == EMSCRIPTEN_RESULT_FAILED_NOT_DEFERRED) return "EMSCRIPTEN_RESULT_FAILED_NOT_DEFERRED";
30   if (result == EMSCRIPTEN_RESULT_INVALID_TARGET) return "EMSCRIPTEN_RESULT_INVALID_TARGET";
31   if (result == EMSCRIPTEN_RESULT_UNKNOWN_TARGET) return "EMSCRIPTEN_RESULT_UNKNOWN_TARGET";
32   if (result == EMSCRIPTEN_RESULT_INVALID_PARAM) return "EMSCRIPTEN_RESULT_INVALID_PARAM";
33   if (result == EMSCRIPTEN_RESULT_FAILED) return "EMSCRIPTEN_RESULT_FAILED";
34   if (result == EMSCRIPTEN_RESULT_NO_DATA) return "EMSCRIPTEN_RESULT_NO_DATA";
35   return "Unknown EMSCRIPTEN_RESULT!";
36 }
37 
38 #define TEST_RESULT(x) if (ret != EMSCRIPTEN_RESULT_SUCCESS) printf("%s returned %s.\n", #x, emscripten_result_to_string(ret));
39 
40 int gotClick = 0;
41 
click_callback(int eventType,const EmscriptenMouseEvent * e,void * userData)42 EM_BOOL click_callback(int eventType, const EmscriptenMouseEvent *e, void *userData)
43 {
44   if (e->screenX != 0 && e->screenY != 0 && e->clientX != 0 && e->clientY != 0 && e->canvasX != 0 && e->canvasY != 0 && e->targetX != 0 && e->targetY != 0)
45   {
46     if (eventType == EMSCRIPTEN_EVENT_CLICK && !gotClick) {
47       gotClick = 1;
48       printf("Request pointer lock...\n");
49       EMSCRIPTEN_RESULT ret = emscripten_request_pointerlock(0, 0);
50       TEST_RESULT(ret);
51       if (ret != EMSCRIPTEN_RESULT_SUCCESS) {
52         printf("ERROR! emscripten_request_pointerlock() failure\n");
53         report_result(1);
54       }
55     }
56   }
57 
58   return 0;
59 }
60 
pointerlockchange_callback(int eventType,const EmscriptenPointerlockChangeEvent * e,void * userData)61 EM_BOOL pointerlockchange_callback(int eventType, const EmscriptenPointerlockChangeEvent *e, void *userData) {
62   printf("ERROR! received 'pointerlockchange' event\n");
63   report_result(1);
64 
65   return 0;
66 }
67 
pointerlockerror_callback(int eventType,const void * reserved,void * userData)68 EM_BOOL pointerlockerror_callback(int eventType, const void *reserved, void *userData) {
69   if (eventType != EMSCRIPTEN_EVENT_POINTERLOCKERROR) {
70     printf("ERROR! invalid event type for 'pointerlockerror' callback\n");
71     report_result(1);
72     return 0;
73   }
74 
75   printf("SUCCESS! received 'pointerlockerror' event\n");
76   report_result(0);
77 
78   return 0;
79 }
80 
main()81 int main()
82 {
83   printf("'pointerlockerror' event test:\n");
84   printf("Reject the pointer lock request after clicking on canvas.\n");
85 
86   // Make the canvas area stand out from the background.
87   emscripten_set_canvas_element_size( "#canvas", 400, 300 );
88   EM_ASM(Module['canvas'].style.backgroundColor = 'black';);
89 
90   EMSCRIPTEN_RESULT ret = emscripten_set_click_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, 1, click_callback);
91   TEST_RESULT(emscripten_set_click_callback);
92   ret = emscripten_set_pointerlockchange_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, 1, pointerlockchange_callback);
93   TEST_RESULT(emscripten_set_pointerlockchange_callback);
94   ret = emscripten_set_pointerlockerror_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, 1, pointerlockerror_callback);
95   TEST_RESULT(emscripten_set_pointerlockerror_callback);
96 
97   /* For the events to function, one must either call emscripten_set_main_loop or enable Module.noExitRuntime by some other means.
98      Otherwise the application will exit after leaving main(), and the atexit handlers will clean up all event hooks (by design). */
99   EM_ASM(noExitRuntime = true);
100   return 0;
101 }
102