1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 /** \file
18  * \ingroup GHOST
19  */
20 
21 #include <iostream>
22 
23 #include "GHOST_C-api.h"
24 #include "GHOST_XrContext.h"
25 #include "GHOST_Xr_intern.h"
26 
GHOST_XrEventPollNext(XrInstance instance,XrEventDataBuffer & r_event_data)27 static bool GHOST_XrEventPollNext(XrInstance instance, XrEventDataBuffer &r_event_data)
28 {
29   /* (Re-)initialize as required by specification. */
30   r_event_data.type = XR_TYPE_EVENT_DATA_BUFFER;
31   r_event_data.next = nullptr;
32 
33   return (xrPollEvent(instance, &r_event_data) == XR_SUCCESS);
34 }
35 
GHOST_XrEventsHandle(GHOST_XrContextHandle xr_contexthandle)36 GHOST_TSuccess GHOST_XrEventsHandle(GHOST_XrContextHandle xr_contexthandle)
37 {
38   if (xr_contexthandle == nullptr) {
39     return GHOST_kFailure;
40   }
41 
42   GHOST_XrContext &xr_context = *(GHOST_XrContext *)xr_contexthandle;
43   XrEventDataBuffer event_buffer; /* Structure big enough to hold all possible events. */
44 
45   while (GHOST_XrEventPollNext(xr_context.getInstance(), event_buffer)) {
46     XrEventDataBaseHeader *event = (XrEventDataBaseHeader *)&event_buffer;
47 
48     switch (event->type) {
49       case XR_TYPE_EVENT_DATA_SESSION_STATE_CHANGED:
50         xr_context.handleSessionStateChange((XrEventDataSessionStateChanged &)*event);
51         return GHOST_kSuccess;
52       case XR_TYPE_EVENT_DATA_INSTANCE_LOSS_PENDING:
53         GHOST_XrContextDestroy(xr_contexthandle);
54         return GHOST_kSuccess;
55       default:
56         if (xr_context.isDebugMode()) {
57           printf("Unhandled event: %i\n", event->type);
58         }
59         return GHOST_kFailure;
60     }
61   }
62 
63   return GHOST_kFailure;
64 }
65