support app icon by reusing x11 sdl code
[sdl_omap.git] / src / video / omapdss / sdlif.c
index 96388f2..b5cadbe 100644 (file)
@@ -13,6 +13,7 @@
 #include "../SDL_pixels_c.h"
 #include "../../events/SDL_events_c.h"
 
+#include "SDL_x11reuse.h"
 #include "linux/xenv.h"
 #include "osdl.h"
 
@@ -31,6 +32,7 @@ static void omap_free(SDL_VideoDevice *device)
 
 static int omap_VideoInit(SDL_VideoDevice *this, SDL_PixelFormat *vformat)
 {
+       struct SDL_PrivateVideoData *pdata = this->hidden;
        const char *tmp;
        int w, h, ret;
 
@@ -40,21 +42,23 @@ static int omap_VideoInit(SDL_VideoDevice *this, SDL_PixelFormat *vformat)
        vformat->BitsPerPixel = 16;
 
        omapsdl_input_init();
-       omapsdl_config(this->hidden);
+       omapsdl_config(pdata);
 
        tmp = getenv("SDL_OMAP_DEFAULT_MODE");
        if (tmp != NULL && sscanf(tmp, "%dx%d", &w, &h) == 2) {
                this->info.current_w = w;
                this->info.current_h = h;
        }
-       else if (osdl_video_detect_screen(this->hidden) == 0) {
-               this->info.current_w = this->hidden->phys_w;
-               this->info.current_h = this->hidden->phys_h;
+       else if (osdl_video_detect_screen(pdata) == 0) {
+               this->info.current_w = pdata->phys_w;
+               this->info.current_h = pdata->phys_h;
        }
 
        this->handles_any_size = 1;
        this->info.hw_available = 1;
 
+       pdata->x11reuse_context = x11reuse_init();
+
        return 0;
 }
 
@@ -133,7 +137,10 @@ static SDL_Surface *omap_SetVideoMode(SDL_VideoDevice *this, SDL_Surface *curren
                }
        }
 
-       doublebuf = (flags & SDL_DOUBLEBUF) ? 1 : 0;
+       /* always use doublebuf, when SDL_DOUBLEBUF is not set,
+        * we'll have to blit manually on UpdateRects() */
+       doublebuf = 1;
+
        fbmem = osdl_video_set_mode(pdata,
                pdata->border_l, pdata->border_r, pdata->border_t, pdata->border_b,
                width, height, bpp, &doublebuf, this->wm_title);
@@ -143,11 +150,24 @@ static SDL_Surface *omap_SetVideoMode(SDL_VideoDevice *this, SDL_Surface *curren
                    pdata->border_l, pdata->border_r, pdata->border_t, pdata->border_b);
                return NULL;
        }
-       if ((flags & SDL_DOUBLEBUF) && !doublebuf) {
-               log("doublebuffering could not be set\n");
-               flags &= ~SDL_DOUBLEBUF;
+       pdata->front_buffer = osdl_video_get_active_buffer(pdata);
+       if (pdata->front_buffer == NULL) {
+               err("osdl_video_get_active_buffer failed\n");
+               return NULL;
+       }
+
+       if (!doublebuf) {
+               if (flags & SDL_DOUBLEBUF) {
+                       log("doublebuffering could not be set\n");
+                       flags &= ~SDL_DOUBLEBUF;
+               }
+               /* XXX: could just malloc a back buffer here instead */
+               pdata->cfg_force_directbuf = 1;
        }
 
+       if (!(flags & SDL_DOUBLEBUF) && pdata->cfg_force_directbuf)
+               fbmem = pdata->front_buffer;
+
        flags |= SDL_FULLSCREEN | SDL_HWSURFACE;
        unhandled_flags = flags & ~(SDL_FULLSCREEN | SDL_HWSURFACE | SDL_DOUBLEBUF);
        if (unhandled_flags != 0) {
@@ -169,6 +189,17 @@ static SDL_Surface *omap_SetVideoMode(SDL_VideoDevice *this, SDL_Surface *curren
                pdata->ts_ymul = (v_height << 16) / pdata->layer_h;
        }
 
+       if (pdata->delayed_icon != NULL) {
+               this->SetIcon(this, pdata->delayed_icon,
+                       pdata->delayed_icon_mask);
+               SDL_FreeSurface(pdata->delayed_icon);
+               pdata->delayed_icon = NULL;
+               if (pdata->delayed_icon_mask) {
+                       free(pdata->delayed_icon_mask);
+                       pdata->delayed_icon_mask = NULL;
+               }
+       }
+
        return current;
 }
 
@@ -187,10 +218,27 @@ static void omap_UnlockHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
 static int omap_FlipHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
 {
        struct SDL_PrivateVideoData *pdata = this->hidden;
+       static int warned;
 
        trace("%p", surface);
 
-       surface->pixels = osdl_video_flip(pdata);
+       if (surface != this->screen) {
+               if (!warned) {
+                       err("flip surface %p which is not screen %p?\n",
+                               surface, this->screen);
+                       warned = 1;
+               }
+               return;
+       }
+
+       if (surface->flags & SDL_DOUBLEBUF)
+               surface->pixels = osdl_video_flip(pdata);
+       else {
+               if (surface->pixels != pdata->front_buffer)
+                       memcpy(surface->pixels, pdata->front_buffer,
+                               surface->pitch * surface->h);
+       }
+
        pdata->app_uses_flip = 1;
 
        return 0;
@@ -217,15 +265,53 @@ static int omap_SetColors(SDL_VideoDevice *this, int firstcolor, int ncolors, SD
 static void omap_UpdateRects(SDL_VideoDevice *this, int nrects, SDL_Rect *rects)
 {
        struct SDL_PrivateVideoData *pdata = this->hidden;
+       SDL_Surface *screen = this->screen;
+       int fullscreen_blit = 0;
+       int i, Bpp, x, y, w, h;
+       char *src, *dst;
 
        trace("%d, %p", nrects, rects);
 
-       /* for doublebuf forcing on apps */
-       if (nrects == 1 && rects->x == 0 && rects->y == 0
-           && !pdata->app_uses_flip && (this->screen->flags & SDL_DOUBLEBUF)
-           && rects->w == this->screen->w && rects->h == this->screen->h)
-       {
-               this->screen->pixels = osdl_video_flip(pdata);
+       fullscreen_blit =
+               nrects == 1 && rects->x == 0 && rects->y == 0
+                   && (rects->w == screen->w || rects->w == 0)
+                   && (rects->h == screen->h || rects->h == 0);
+
+       if (screen->flags & SDL_DOUBLEBUF) {
+               if (fullscreen_blit && !pdata->app_uses_flip)
+                       screen->pixels = osdl_video_flip(pdata);
+               return;
+       }
+
+       src = screen->pixels;
+       dst = pdata->front_buffer;
+       if (src == dst)
+               return;
+
+       if (fullscreen_blit) {
+               memcpy(dst, src, screen->pitch * screen->h);
+               return;
+       }
+
+       for (i = 0, Bpp = screen->format->BytesPerPixel; i < nrects; i++) {
+               /* this supposedly has no clipping, but we'll do it anyway */
+               x = rects[i].x, y = rects[i].y, w = rects[i].w, h = rects[i].h;
+               if (x < 0)
+                       w += x, x = 0;
+               else if (x + w > screen->w)
+                       w = screen->w - x;
+               if (w <= 0)
+                       continue;
+
+               if (y < 0)
+                       h += y, y = 0;
+               else if (y + h > screen->h)
+                       h = screen->h - y;
+
+               for (; h > 0; y++, h--)
+                       memcpy(dst + y * screen->pitch + x * Bpp,
+                              src + y * screen->pitch + x * Bpp,
+                              w * Bpp);
        }
 }
 
@@ -370,6 +456,55 @@ static void omap_PumpEvents(SDL_VideoDevice *this)
                read_tslib ? ts_event_cb : NULL, this);
 }
 
+static void omap_SetCaption(SDL_VideoDevice *this, const char *title, const char *icon)
+{
+       void *display = NULL, *window = NULL;
+       int screen = 0;
+       int ret;
+
+       ret = osdl_video_get_window(&display, &screen, &window);
+       if (ret == 0) {
+               x11reuse_SetCaption(this->hidden->x11reuse_context,
+                       display, screen, window, title, icon);
+       }
+}
+
+static void omap_SetIcon(SDL_VideoDevice *this, SDL_Surface *icon, Uint8 *mask)
+{
+       struct SDL_PrivateVideoData *pdata = this->hidden;
+       void *display = NULL, *window = NULL;
+       int screen = 0;
+       int ret;
+
+       ret = osdl_video_get_window(&display, &screen, &window);
+       if (ret == 0) {
+               x11reuse_SetIcon(pdata->x11reuse_context,
+                       display, screen, window, icon, mask);
+       }
+       else {
+               SDL_Surface *old_icon = pdata->delayed_icon;
+               void *old_icon_mask = pdata->delayed_icon_mask;
+               int mask_size = ((icon->w + 7) / 8) * icon->h;
+
+               pdata->delayed_icon = SDL_ConvertSurface(icon,
+                                       icon->format, icon->flags);
+               if (pdata->delayed_icon != NULL) {
+                       memcpy(pdata->delayed_icon->pixels, icon->pixels,
+                               icon->pitch * icon->h);
+               }
+               if (mask != NULL) {
+                       pdata->delayed_icon_mask = malloc(mask_size);
+                       if (pdata->delayed_icon_mask)
+                               memcpy(pdata->delayed_icon_mask, mask, mask_size);
+               }
+
+               if (old_icon != NULL)
+                       SDL_FreeSurface(old_icon);
+               if (old_icon_mask)
+                       free(old_icon_mask);
+       }
+}
+
 static SDL_VideoDevice *omap_create(int devindex)
 {
        SDL_VideoDevice *this;
@@ -393,6 +528,8 @@ static SDL_VideoDevice *omap_create(int devindex)
        this->VideoQuit = omap_VideoQuit;
        this->InitOSKeymap = omap_InitOSKeymap;
        this->PumpEvents = omap_PumpEvents;
+       this->SetCaption = omap_SetCaption;
+       this->SetIcon = omap_SetIcon;
        this->free = omap_free;
 
        return this;