]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/android/display.cpp
Use default member initializers and defaulted default constructors
[libs/gui.git] / source / graphics / android / display.cpp
1 #include "display.h"
2 #include "display_private.h"
3 #include <msp/core/application.h>
4 #include <msp/core/mainthread.h>
5
6 using namespace std;
7
8 namespace Msp {
9 namespace Graphics {
10
11 Display::Display(const string &)
12 {
13         Android::MainThread *thread = reinterpret_cast<Android::MainThread *>(Application::get_data());
14         if(!thread->is_starting_up())
15                 throw logic_error("Display must be created during startup");
16
17         priv = new Private;
18
19         thread->signal_native_window_created.connect(sigc::mem_fun(priv, &PlatformDisplayPrivate::native_window_created));
20         thread->signal_native_window_resized.connect(sigc::mem_fun(priv, &PlatformDisplayPrivate::native_window_resized));
21         thread->signal_native_window_destroyed.connect(sigc::mem_fun(priv, &PlatformDisplayPrivate::native_window_destroyed));
22         thread->signal_input_queue_created.connect(sigc::mem_fun(priv, &PlatformDisplayPrivate::input_queue_created));
23         thread->signal_input_queue_destroyed.connect(sigc::mem_fun(priv, &PlatformDisplayPrivate::input_queue_destroyed));
24         thread->resume_startup();
25 }
26
27 Display::~Display()
28 {
29         delete priv;
30 }
31
32 void Display::set_mode(const VideoMode &, bool)
33 {
34         throw runtime_error("video mode switching not supported");
35 }
36
37 bool Display::process_events()
38 {
39         MutexLock lock(priv->event_mutex);
40         if(!priv->input_queue)
41                 return false;
42
43         Window::Event event;
44         if(!priv->events.empty())
45         {
46                 event = priv->events.front();
47                 priv->events.pop_front();
48         }
49         else if(AInputQueue_getEvent(priv->input_queue, &event.aevent)>=0)
50         {
51                 if(AInputQueue_preDispatchEvent(priv->input_queue, event.aevent))
52                         return true;
53
54                 event.type = INPUT_EVENT;
55         }
56         else
57                 return false;
58
59         bool handled = false;
60         if(!priv->windows.empty())
61                 handled = priv->windows.begin()->second->event(event);
62
63         if(event.type==INPUT_EVENT)
64                 AInputQueue_finishEvent(priv->input_queue, event.aevent, handled);
65         else if(event.type==WINDOW_CREATED)
66                 priv->window_mutex.lock();
67         else if(event.type==WINDOW_DESTROYED)
68                 priv->window_mutex.unlock();
69
70         return true;
71 }
72
73 void Display::check_error()
74 {
75 }
76
77
78 void PlatformDisplayPrivate::push_event(Msp::Graphics::AndroidEventType type)
79 {
80         MutexLock lock(event_mutex);
81         Window::Event event;
82         event.type = type;
83         events.push_back(event);
84 }
85
86 void PlatformDisplayPrivate::native_window_created(ANativeWindow *window)
87 {
88         native_window = window;
89         push_event(WINDOW_CREATED);
90 }
91
92 void PlatformDisplayPrivate::native_window_resized(ANativeWindow *)
93 {
94         push_event(WINDOW_RESIZED);
95 }
96
97 void PlatformDisplayPrivate::native_window_destroyed(ANativeWindow *)
98 {
99         push_event(WINDOW_DESTROYED);
100         MutexLock lock(window_mutex);
101         native_window = 0;
102 }
103
104 void PlatformDisplayPrivate::input_queue_created(AInputQueue *queue)
105 {
106         MutexLock lock(event_mutex);
107         input_queue = queue;
108 }
109
110 void PlatformDisplayPrivate::input_queue_destroyed(AInputQueue *)
111 {
112         MutexLock lock(event_mutex);
113         input_queue = 0;
114 }
115
116 } // namespace Graphics
117 } // namespace Msp