Added initial support for displaying on Wayland compositors.
[vuplus_xbmc] / xbmc / windowing / egl / wayland / Display.cpp
1 /*
2  *      Copyright (C) 2011-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20 #include <sstream>
21 #include <iostream>
22 #include <stdexcept>
23
24 #include <cstdlib>
25
26 #include <wayland-client.h>
27
28 #include "windowing/DllWaylandClient.h"
29 #include "windowing/WaylandProtocol.h"
30 #include "Display.h"
31
32 namespace xw = xbmc::wayland;
33
34 xw::Display::Display(IDllWaylandClient &clientLibrary) :
35   m_clientLibrary(clientLibrary),
36   m_display(m_clientLibrary.wl_display_connect(NULL))
37 {
38   /* wl_display_connect won't throw when it fails, but it does
39    * return NULL on failure. If this object would be incomplete
40    * then that is a fatal error for the backend and we should
41    * throw a runtime_error for the main connection manager to handle
42    */
43   if (!m_display)
44   {
45     std::stringstream ss;
46     ss << "Failed to connect to "
47        << getenv("WAYLAND_DISPLAY");
48     throw std::runtime_error(ss.str());
49   }
50 }
51
52 xw::Display::~Display()
53 {
54   m_clientLibrary.wl_display_flush(m_display);
55   m_clientLibrary.wl_display_disconnect(m_display);
56 }
57
58 struct wl_display *
59 xw::Display::GetWlDisplay()
60 {
61   return m_display;
62 }
63
64 EGLNativeDisplayType *
65 xw::Display::GetEGLNativeDisplay()
66 {
67   return &m_display;
68 }
69
70 /* Create a sync callback object. This can be wrapped in an
71  * xbmc::wayland::Callback object to call an arbitrary function
72  * as soon as the display has finished processing all commands.
73  * 
74  * This does not block until a synchronization is complete -
75  * consider using a function like WaitForSynchronize to do that */
76 struct wl_callback *
77 xw::Display::Sync()
78 {
79   struct wl_callback *callback =
80       protocol::CreateWaylandObject<struct wl_callback *,
81                                     struct wl_display *> (m_clientLibrary,
82                                                           m_display,
83                                                           m_clientLibrary.Get_wl_callback_interface());
84   protocol::CallMethodOnWaylandObject(m_clientLibrary,
85                                       m_display,
86                                       WL_DISPLAY_SYNC,
87                                       callback);
88   return callback;
89 }