SDL_androidvideo.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2013 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. #include "SDL_config.h"
  19. #if SDL_VIDEO_DRIVER_ANDROID
  20. /* Android SDL video driver implementation
  21. */
  22. #include "SDL_video.h"
  23. #include "SDL_mouse.h"
  24. #include "../SDL_sysvideo.h"
  25. #include "../SDL_pixels_c.h"
  26. #include "../../events/SDL_events_c.h"
  27. #include "../../events/SDL_windowevents_c.h"
  28. #include "SDL_androidvideo.h"
  29. #include "SDL_androidclipboard.h"
  30. #include "SDL_androidevents.h"
  31. #include "SDL_androidkeyboard.h"
  32. #include "SDL_androidwindow.h"
  33. #define ANDROID_VID_DRIVER_NAME "Android"
  34. /* Initialization/Query functions */
  35. static int Android_VideoInit(_THIS);
  36. static void Android_VideoQuit(_THIS);
  37. #include "../SDL_egl.h"
  38. /* GL functions (SDL_androidgl.c) */
  39. extern SDL_GLContext Android_GLES_CreateContext(_THIS, SDL_Window * window);
  40. extern int Android_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context);
  41. extern void Android_GLES_SwapWindow(_THIS, SDL_Window * window);
  42. extern int Android_GLES_LoadLibrary(_THIS, const char *path);
  43. #define Android_GLES_GetProcAddress SDL_EGL_GetProcAddress
  44. #define Android_GLES_UnloadLibrary SDL_EGL_UnloadLibrary
  45. #define Android_GLES_SetSwapInterval SDL_EGL_SetSwapInterval
  46. #define Android_GLES_GetSwapInterval SDL_EGL_GetSwapInterval
  47. #define Android_GLES_DeleteContext SDL_EGL_DeleteContext
  48. /* Android driver bootstrap functions */
  49. /* These are filled in with real values in Android_SetScreenResolution on init (before SDL_main()) */
  50. int Android_ScreenWidth = 0;
  51. int Android_ScreenHeight = 0;
  52. Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
  53. SDL_sem *Android_PauseSem = NULL, *Android_ResumeSem = NULL;
  54. /* Currently only one window */
  55. SDL_Window *Android_Window = NULL;
  56. static int
  57. Android_Available(void)
  58. {
  59. return 1;
  60. }
  61. static void
  62. Android_DeleteDevice(SDL_VideoDevice * device)
  63. {
  64. SDL_free(device);
  65. }
  66. static SDL_VideoDevice *
  67. Android_CreateDevice(int devindex)
  68. {
  69. SDL_VideoDevice *device;
  70. SDL_VideoData *data;
  71. /* Initialize all variables that we clean on shutdown */
  72. device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
  73. if (!device) {
  74. SDL_OutOfMemory();
  75. return NULL;
  76. }
  77. data = (SDL_VideoData*) SDL_calloc(1, sizeof(SDL_VideoData));
  78. if (!data) {
  79. SDL_OutOfMemory();
  80. SDL_free(device);
  81. return NULL;
  82. }
  83. device->driverdata = data;
  84. /* Set the function pointers */
  85. device->VideoInit = Android_VideoInit;
  86. device->VideoQuit = Android_VideoQuit;
  87. device->PumpEvents = Android_PumpEvents;
  88. device->CreateWindow = Android_CreateWindow;
  89. device->SetWindowTitle = Android_SetWindowTitle;
  90. device->DestroyWindow = Android_DestroyWindow;
  91. device->free = Android_DeleteDevice;
  92. /* GL pointers */
  93. device->GL_LoadLibrary = Android_GLES_LoadLibrary;
  94. device->GL_GetProcAddress = Android_GLES_GetProcAddress;
  95. device->GL_UnloadLibrary = Android_GLES_UnloadLibrary;
  96. device->GL_CreateContext = Android_GLES_CreateContext;
  97. device->GL_MakeCurrent = Android_GLES_MakeCurrent;
  98. device->GL_SetSwapInterval = Android_GLES_SetSwapInterval;
  99. device->GL_GetSwapInterval = Android_GLES_GetSwapInterval;
  100. device->GL_SwapWindow = Android_GLES_SwapWindow;
  101. device->GL_DeleteContext = Android_GLES_DeleteContext;
  102. /* Text input */
  103. device->StartTextInput = Android_StartTextInput;
  104. device->StopTextInput = Android_StopTextInput;
  105. device->SetTextInputRect = Android_SetTextInputRect;
  106. /* Screen keyboard */
  107. device->HasScreenKeyboardSupport = Android_HasScreenKeyboardSupport;
  108. device->IsScreenKeyboardShown = Android_IsScreenKeyboardShown;
  109. /* Clipboard */
  110. device->SetClipboardText = Android_SetClipboardText;
  111. device->GetClipboardText = Android_GetClipboardText;
  112. device->HasClipboardText = Android_HasClipboardText;
  113. return device;
  114. }
  115. VideoBootStrap Android_bootstrap = {
  116. ANDROID_VID_DRIVER_NAME, "SDL Android video driver",
  117. Android_Available, Android_CreateDevice
  118. };
  119. int
  120. Android_VideoInit(_THIS)
  121. {
  122. SDL_DisplayMode mode;
  123. mode.format = Android_ScreenFormat;
  124. mode.w = Android_ScreenWidth;
  125. mode.h = Android_ScreenHeight;
  126. mode.refresh_rate = 0;
  127. mode.driverdata = NULL;
  128. if (SDL_AddBasicVideoDisplay(&mode) < 0) {
  129. return -1;
  130. }
  131. SDL_AddDisplayMode(&_this->displays[0], &mode);
  132. Android_InitKeyboard();
  133. /* We're done! */
  134. return 0;
  135. }
  136. void
  137. Android_VideoQuit(_THIS)
  138. {
  139. }
  140. /* This function gets called before VideoInit() */
  141. void
  142. Android_SetScreenResolution(int width, int height, Uint32 format)
  143. {
  144. Android_ScreenWidth = width;
  145. Android_ScreenHeight = height;
  146. Android_ScreenFormat = format;
  147. if (Android_Window) {
  148. SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height);
  149. }
  150. }
  151. #endif /* SDL_VIDEO_DRIVER_ANDROID */
  152. /* vi: set ts=4 sw=4 expandtab: */