src/video/android/SDL_androidvideo.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 13 Jan 2011 18:31:15 -0800
changeset 5001 77df56570442
parent 4998 a514bfe6952a
child 5262 b530ef003506
permissions -rw-r--r--
Added "mouse" support for the Android touch screen
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library 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 GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /* Android SDL video driver implementation
    25 */
    26 
    27 #include "SDL_video.h"
    28 #include "SDL_mouse.h"
    29 #include "../SDL_sysvideo.h"
    30 #include "../SDL_pixels_c.h"
    31 #include "../../events/SDL_events_c.h"
    32 #include "../../events/SDL_windowevents_c.h"
    33 
    34 #include "SDL_androidvideo.h"
    35 #include "SDL_androidevents.h"
    36 #include "SDL_androidkeyboard.h"
    37 #include "SDL_androidwindow.h"
    38 
    39 #define ANDROID_VID_DRIVER_NAME "Android"
    40 
    41 /* Initialization/Query functions */
    42 static int Android_VideoInit(_THIS);
    43 static void Android_VideoQuit(_THIS);
    44 
    45 /* GL functions (SDL_androidgl.c) */
    46 extern int Android_GL_LoadLibrary(_THIS, const char *path);
    47 extern void *Android_GL_GetProcAddress(_THIS, const char *proc);
    48 extern void Android_GL_UnloadLibrary(_THIS);
    49 //extern int *Android_GL_GetVisual(_THIS, Display * display, int screen);
    50 extern SDL_GLContext Android_GL_CreateContext(_THIS, SDL_Window * window);
    51 extern int Android_GL_MakeCurrent(_THIS, SDL_Window * window,
    52                               SDL_GLContext context);
    53 extern int Android_GL_SetSwapInterval(_THIS, int interval);
    54 extern int Android_GL_GetSwapInterval(_THIS);
    55 extern void Android_GL_SwapWindow(_THIS, SDL_Window * window);
    56 extern void Android_GL_DeleteContext(_THIS, SDL_GLContext context);
    57 
    58 /* Android driver bootstrap functions */
    59 
    60 
    61 // These are filled in with real values in Android_SetScreenResolution on 
    62 // init (before SDL_main())
    63 int Android_ScreenWidth = 0;
    64 int Android_ScreenHeight = 0;
    65 Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
    66 
    67 /* Currently only one window */
    68 SDL_Window *Android_Window = NULL;
    69 
    70 static int
    71 Android_Available(void)
    72 {
    73     return 1;
    74 }
    75 
    76 static void
    77 Android_DeleteDevice(SDL_VideoDevice * device)
    78 {
    79     SDL_free(device);
    80 }
    81 
    82 static SDL_VideoDevice *
    83 Android_CreateDevice(int devindex)
    84 {
    85     printf("Creating video device\n");
    86     SDL_VideoDevice *device;
    87 
    88     /* Initialize all variables that we clean on shutdown */
    89     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    90     if (!device) {
    91         SDL_OutOfMemory();
    92         if (device) {
    93             SDL_free(device);
    94         }
    95         return (0);
    96     }
    97 
    98     /* Set the function pointers */
    99     device->VideoInit = Android_VideoInit;
   100     device->VideoQuit = Android_VideoQuit;
   101     device->PumpEvents = Android_PumpEvents;
   102 
   103     device->CreateWindow = Android_CreateWindow;
   104     device->SetWindowTitle = Android_SetWindowTitle;
   105     device->DestroyWindow = Android_DestroyWindow;
   106 
   107     device->free = Android_DeleteDevice;
   108 
   109     /* GL pointers */
   110     device->GL_LoadLibrary = Android_GL_LoadLibrary;
   111     device->GL_GetProcAddress = Android_GL_GetProcAddress;
   112     device->GL_UnloadLibrary = Android_GL_UnloadLibrary;
   113     device->GL_CreateContext = Android_GL_CreateContext;
   114     device->GL_MakeCurrent = Android_GL_MakeCurrent;
   115     device->GL_SetSwapInterval = Android_GL_SetSwapInterval;
   116     device->GL_GetSwapInterval = Android_GL_GetSwapInterval;
   117     device->GL_SwapWindow = Android_GL_SwapWindow;
   118     device->GL_DeleteContext = Android_GL_DeleteContext;
   119 
   120     return device;
   121 }
   122 
   123 VideoBootStrap Android_bootstrap = {
   124     ANDROID_VID_DRIVER_NAME, "SDL Android video driver",
   125     Android_Available, Android_CreateDevice
   126 };
   127 
   128 
   129 int
   130 Android_VideoInit(_THIS)
   131 {
   132     SDL_DisplayMode mode;
   133 
   134     mode.format = Android_ScreenFormat;
   135     mode.w = Android_ScreenWidth;
   136     mode.h = Android_ScreenHeight;
   137     mode.refresh_rate = 0;
   138     mode.driverdata = NULL;
   139     if (SDL_AddBasicVideoDisplay(&mode) < 0) {
   140         return -1;
   141     }
   142 
   143     SDL_zero(mode);
   144     SDL_AddDisplayMode(&_this->displays[0], &mode);
   145 
   146     Android_InitKeyboard();
   147 
   148     /* We're done! */
   149     return 0;
   150 }
   151 
   152 void
   153 Android_VideoQuit(_THIS)
   154 {
   155 }
   156 
   157 /* This function gets called before VideoInit() */
   158 void
   159 Android_SetScreenResolution(int width, int height, Uint32 format)
   160 {
   161     Android_ScreenWidth = width;
   162     Android_ScreenHeight = height;   
   163     Android_ScreenFormat = format;
   164 
   165     if (Android_Window) {
   166         SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height);
   167     }
   168 }
   169 
   170 /* vi: set ts=4 sw=4 expandtab: */