SDL_mirwindow.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /*
  19. Contributed by Brandon Schaefer, <brandon.schaefer@canonical.com>
  20. */
  21. #include "../../SDL_internal.h"
  22. #if SDL_VIDEO_DRIVER_MIR
  23. #include "../SDL_egl_c.h"
  24. #include "../SDL_sysvideo.h"
  25. #include "SDL_mirevents.h"
  26. #include "SDL_mirwindow.h"
  27. #include "SDL_mirdyn.h"
  28. int
  29. IsSurfaceValid(MIR_Window* mir_window)
  30. {
  31. if (!MIR_mir_surface_is_valid(mir_window->surface)) {
  32. const char* error = MIR_mir_surface_get_error_message(mir_window->surface);
  33. return SDL_SetError("Failed to created a mir surface: %s", error);
  34. }
  35. return 0;
  36. }
  37. MirPixelFormat
  38. FindValidPixelFormat(MIR_Data* mir_data)
  39. {
  40. unsigned int pf_size = 32;
  41. unsigned int valid_formats;
  42. unsigned int f;
  43. MirPixelFormat formats[pf_size];
  44. MIR_mir_connection_get_available_surface_formats(mir_data->connection, formats,
  45. pf_size, &valid_formats);
  46. for (f = 0; f < valid_formats; f++) {
  47. MirPixelFormat cur_pf = formats[f];
  48. if (cur_pf == mir_pixel_format_abgr_8888 ||
  49. cur_pf == mir_pixel_format_xbgr_8888 ||
  50. cur_pf == mir_pixel_format_argb_8888 ||
  51. cur_pf == mir_pixel_format_xrgb_8888) {
  52. return cur_pf;
  53. }
  54. }
  55. return mir_pixel_format_invalid;
  56. }
  57. int
  58. MIR_CreateWindow(_THIS, SDL_Window* window)
  59. {
  60. MIR_Window* mir_window;
  61. MIR_Data* mir_data;
  62. MirSurfaceParameters surfaceparm =
  63. {
  64. .name = "MirSurface",
  65. .width = window->w,
  66. .height = window->h,
  67. .pixel_format = mir_pixel_format_invalid,
  68. .buffer_usage = mir_buffer_usage_hardware,
  69. .output_id = mir_display_output_id_invalid
  70. };
  71. MirEventDelegate delegate = {
  72. MIR_HandleInput,
  73. window
  74. };
  75. mir_window = SDL_calloc(1, sizeof(MIR_Window));
  76. if (!mir_window)
  77. return SDL_OutOfMemory();
  78. mir_data = _this->driverdata;
  79. window->driverdata = mir_window;
  80. if (mir_data->software)
  81. surfaceparm.buffer_usage = mir_buffer_usage_software;
  82. if (window->x == SDL_WINDOWPOS_UNDEFINED)
  83. window->x = 0;
  84. if (window->y == SDL_WINDOWPOS_UNDEFINED)
  85. window->y = 0;
  86. mir_window->mir_data = mir_data;
  87. mir_window->sdl_window = window;
  88. surfaceparm.pixel_format = FindValidPixelFormat(mir_data);
  89. if (surfaceparm.pixel_format == mir_pixel_format_invalid) {
  90. return SDL_SetError("Failed to find a valid pixel format.");
  91. }
  92. mir_window->surface = MIR_mir_connection_create_surface_sync(mir_data->connection, &surfaceparm);
  93. if (!MIR_mir_surface_is_valid(mir_window->surface)) {
  94. const char* error = MIR_mir_surface_get_error_message(mir_window->surface);
  95. return SDL_SetError("Failed to created a mir surface: %s", error);
  96. }
  97. if (window->flags & SDL_WINDOW_OPENGL) {
  98. EGLNativeWindowType egl_native_window =
  99. (EGLNativeWindowType)MIR_mir_surface_get_egl_native_window(mir_window->surface);
  100. mir_window->egl_surface = SDL_EGL_CreateSurface(_this, egl_native_window);
  101. if (mir_window->egl_surface == EGL_NO_SURFACE) {
  102. return SDL_SetError("Failed to created a window surface %p",
  103. _this->egl_data->egl_display);
  104. }
  105. }
  106. else {
  107. mir_window->egl_surface = EGL_NO_SURFACE;
  108. }
  109. MIR_mir_surface_set_event_handler(mir_window->surface, &delegate);
  110. return 0;
  111. }
  112. void
  113. MIR_DestroyWindow(_THIS, SDL_Window* window)
  114. {
  115. MIR_Data* mir_data = _this->driverdata;
  116. MIR_Window* mir_window = window->driverdata;
  117. if (mir_data) {
  118. SDL_EGL_DestroySurface(_this, mir_window->egl_surface);
  119. MIR_mir_surface_release_sync(mir_window->surface);
  120. SDL_free(mir_window);
  121. }
  122. window->driverdata = NULL;
  123. }
  124. SDL_bool
  125. MIR_GetWindowWMInfo(_THIS, SDL_Window* window, SDL_SysWMinfo* info)
  126. {
  127. if (info->version.major == SDL_MAJOR_VERSION &&
  128. info->version.minor == SDL_MINOR_VERSION) {
  129. MIR_Window* mir_window = window->driverdata;
  130. info->subsystem = SDL_SYSWM_MIR;
  131. info->info.mir.connection = mir_window->mir_data->connection;
  132. info->info.mir.surface = mir_window->surface;
  133. return SDL_TRUE;
  134. }
  135. return SDL_FALSE;
  136. }
  137. void
  138. MIR_SetWindowFullscreen(_THIS, SDL_Window* window,
  139. SDL_VideoDisplay* display,
  140. SDL_bool fullscreen)
  141. {
  142. MIR_Window* mir_window = window->driverdata;
  143. if (IsSurfaceValid(mir_window) < 0)
  144. return;
  145. if (fullscreen) {
  146. MIR_mir_surface_set_type(mir_window->surface, mir_surface_state_fullscreen);
  147. } else {
  148. MIR_mir_surface_set_type(mir_window->surface, mir_surface_state_restored);
  149. }
  150. }
  151. void
  152. MIR_MaximizeWindow(_THIS, SDL_Window* window)
  153. {
  154. MIR_Window* mir_window = window->driverdata;
  155. if (IsSurfaceValid(mir_window) < 0)
  156. return;
  157. MIR_mir_surface_set_type(mir_window->surface, mir_surface_state_maximized);
  158. }
  159. void
  160. MIR_MinimizeWindow(_THIS, SDL_Window* window)
  161. {
  162. MIR_Window* mir_window = window->driverdata;
  163. if (IsSurfaceValid(mir_window) < 0)
  164. return;
  165. MIR_mir_surface_set_type(mir_window->surface, mir_surface_state_minimized);
  166. }
  167. void
  168. MIR_RestoreWindow(_THIS, SDL_Window * window)
  169. {
  170. MIR_Window* mir_window = window->driverdata;
  171. if (IsSurfaceValid(mir_window) < 0)
  172. return;
  173. MIR_mir_surface_set_type(mir_window->surface, mir_surface_state_restored);
  174. }
  175. #endif /* SDL_VIDEO_DRIVER_MIR */
  176. /* vi: set ts=4 sw=4 expandtab: */