Couriersud to Sam
authorSam Lantinga <slouken@libsdl.org>
Mon, 16 Aug 2010 09:04:55 -0700
changeset 4636 b196d2758026
parent 4635 0a07d002f10b
child 4637 8c229370ddba
Couriersud to Sam Hi Sam, 20100815_1.diff contains updates for the directfb driver: - more documentation, mainly on software OpenGL in README.directfb - Revised error handling leading to leaner code - Improved/fixed OpenGL handling of multiple contexts. - Made the built-in simple window manager handle OpenGL windows. - Rewrote pixelformat mapping - this was quite ugly before. Well, all software GL, but working :-)
README.DirectFB
src/video/directfb/SDL_DirectFB_WM.c
src/video/directfb/SDL_DirectFB_WM.h
src/video/directfb/SDL_DirectFB_dyn.c
src/video/directfb/SDL_DirectFB_events.c
src/video/directfb/SDL_DirectFB_modes.c
src/video/directfb/SDL_DirectFB_mouse.c
src/video/directfb/SDL_DirectFB_opengl.c
src/video/directfb/SDL_DirectFB_opengl.h
src/video/directfb/SDL_DirectFB_render.c
src/video/directfb/SDL_DirectFB_video.c
src/video/directfb/SDL_DirectFB_video.h
src/video/directfb/SDL_DirectFB_window.c
src/video/directfb/SDL_DirectFB_window.h
--- a/README.DirectFB	Sat Aug 14 12:28:43 2010 -0700
+++ b/README.DirectFB	Mon Aug 16 09:04:55 2010 -0700
@@ -65,10 +65,26 @@
 OPENGL Support
 ==============
 
-As of this writing 20070810 you need to pull Mesa from git and do the following:
+The following instructions will give you *software* opengl. However this
+works at least on all directfb supported platforms.
+
+As of this writing 20100802 you need to pull Mesa from git and do the following:
 
 ------------------------
+git checkout 2c9fdaf7292423c157fc79b5ce43f0f199dd753a
 cd mesa 
+git clone git://anongit.freedesktop.org/git/mesa/mesa
+------------------------
+
+Edit configs/linux-directfb so that the Directories-section looks like
+------------------------
+# Directories
+SRC_DIRS     = mesa glu 
+GLU_DIRS     = sgi
+DRIVER_DIRS  = directfb
+PROGRAM_DIRS = 
+------------------------
+
 make linux-directfb
 make
 
@@ -87,3 +103,4 @@
 export LD_PRELOAD=/usr/local/dfb_GL/libGL.so.7
 
 ./testgl
+
--- a/src/video/directfb/SDL_DirectFB_WM.c	Sat Aug 14 12:28:43 2010 -0700
+++ b/src/video/directfb/SDL_DirectFB_WM.c	Mon Aug 16 09:04:55 2010 -0700
@@ -27,6 +27,8 @@
 
 #include "SDL_DirectFB_video.h"
 
+#include "../../events/SDL_windowevents_c.h"
+
 #define COLOR_EXPAND(col) col.r, col.g, col.b, col.a
 
 static DFB_Theme theme_std = {
@@ -52,7 +54,7 @@
 };
 
 static void
-DrTriangle(IDirectFBSurface * s, int down, int x, int y, int w)
+DrawTriangle(IDirectFBSurface * s, int down, int x, int y, int w)
 {
     int x1, x2, x3;
     int y1, y2, y3;
@@ -76,7 +78,33 @@
 }
 
 static void
-DrCaption(IDirectFBSurface * s, int x, int y, char *text)
+LoadFont(_THIS, SDL_Window * window)
+{
+    SDL_DFB_DEVICEDATA(_this);
+    SDL_DFB_WINDOWDATA(window);
+
+	if (windata->font != NULL) {
+		SDL_DFB_RELEASE(windata->font);
+	    windata->font = NULL;
+	    SDL_DFB_CHECK(windata->window_surface->SetFont(windata->window_surface, windata->font));
+	}
+	
+	if (windata->theme.font != NULL)
+	{
+        DFBFontDescription fdesc;
+
+		SDL_zero(fdesc);
+	    fdesc.flags = DFDESC_HEIGHT;
+	    fdesc.height = windata->theme.font_size;
+	    SDL_DFB_CHECK(devdata->
+	                  dfb->CreateFont(devdata->dfb, windata->theme.font,
+	                                  &fdesc, &windata->font));
+	    SDL_DFB_CHECK(windata->window_surface->SetFont(windata->window_surface, windata->font));
+	}
+}
+
+static void
+DrawCraption(_THIS, IDirectFBSurface * s, int x, int y, char *text)
 {
     DFBSurfaceTextFlags flags;
 
@@ -86,7 +114,7 @@
 }
 
 void
-DirectFB_WM_RedrawLayout(SDL_Window * window)
+DirectFB_WM_RedrawLayout(_THIS, SDL_Window * window)
 {
     SDL_DFB_WINDOWDATA(window);
     IDirectFBSurface *s = windata->window_surface;
@@ -99,6 +127,7 @@
     if (!windata->is_managed || (window->flags & SDL_WINDOW_FULLSCREEN))
         return;
 
+	LoadFont(_this, window);
     //s->SetDrawingFlags(s, DSDRAW_BLEND);
     s->SetColor(s, COLOR_EXPAND(t->frame_color));
     /* top */
@@ -122,16 +151,16 @@
     x = windata->size.w - t->right_size - w + d;
     y = t->top_size + d;
     s->SetColor(s, COLOR_EXPAND(t->close_color));
-    DrTriangle(s, 1, x, y, w - 2 * d);
+    DrawTriangle(s, 1, x, y, w - 2 * d);
     /* Max Button */
     s->SetColor(s, COLOR_EXPAND(t->max_color));
-    DrTriangle(s, window->flags & SDL_WINDOW_MAXIMIZED ? 1 : 0, x - w,
+    DrawTriangle(s, window->flags & SDL_WINDOW_MAXIMIZED ? 1 : 0, x - w,
                y, w - 2 * d);
 
     /* Caption */
     if (window->title) {
-        s->SetColor(s, COLOR_EXPAND(t->font_color));
-        DrCaption(s, (x - w) / 2, t->top_size + d, window->title);
+	    s->SetColor(s, COLOR_EXPAND(t->font_color));
+        DrawCraption(_this, s, (x - w) / 2, t->top_size + d, window->title);
     }
     /* Icon */
     if (windata->icon) {
@@ -152,26 +181,25 @@
 DirectFB_WM_GetClientSize(_THIS, SDL_Window * window, int *cw, int *ch)
 {
     SDL_DFB_WINDOWDATA(window);
-    DFBResult ret;
 
-    ret = windata->window->GetSize(windata->window, cw, ch);
+    SDL_DFB_CHECK(windata->window->GetSize(windata->window, cw, ch));
     *cw -= windata->theme.left_size + windata->theme.right_size;
     *ch -=
         windata->theme.top_size + windata->theme.caption_size +
         windata->theme.bottom_size;
-    return ret;
+    return DFB_OK;
 }
 
 void
-DirectFB_WM_AdjustWindowLayout(SDL_Window * window)
+DirectFB_WM_AdjustWindowLayout(SDL_Window * window, int flags, int w, int h)
 {
     SDL_DFB_WINDOWDATA(window);
 
     if (!windata->is_managed)
         windata->theme = theme_none;
-    else if (window->flags & SDL_WINDOW_FULLSCREEN) {
+    else if (flags & SDL_WINDOW_FULLSCREEN) {
         windata->theme = theme_none;
-    } else if (window->flags & SDL_WINDOW_MAXIMIZED) {
+    } else if (flags & SDL_WINDOW_MAXIMIZED) {
         windata->theme = theme_std;
         windata->theme.left_size = 0;
         windata->theme.right_size = 0;
@@ -183,12 +211,12 @@
 
     windata->client.x = windata->theme.left_size;
     windata->client.y = windata->theme.top_size + windata->theme.caption_size;
-    windata->client.w = window->w;
-    windata->client.h = window->h;
+    windata->client.w = w;
+    windata->client.h = h;
     windata->size.w =
-        window->w + windata->theme.left_size + windata->theme.right_size;
+        w + windata->theme.left_size + windata->theme.right_size;
     windata->size.h =
-        window->h + windata->theme.top_size +
+        h + windata->theme.top_size +
         windata->theme.caption_size + windata->theme.bottom_size;
 }
 
@@ -198,19 +226,16 @@
     SDL_DFB_WINDOWDATA(window);
     SDL_VideoDisplay *display = window->display;
 
-    windata->window->GetPosition(windata->window,
-                                 &windata->restore.x, &windata->restore.y);
-    windata->window->GetSize(windata->window, &windata->restore.w,
-                             &windata->restore.h);
+    SDL_DFB_CHECK(windata->window->GetPosition(windata->window,
+                                 &windata->restore.x, &windata->restore.y));
+    SDL_DFB_CHECK(windata->window->GetSize(windata->window, &windata->restore.w,
+                             &windata->restore.h));
 
-    /* Do this already here */
-    window->flags |= SDL_WINDOW_MAXIMIZED;
-    DirectFB_WM_AdjustWindowLayout(window);
+    DirectFB_WM_AdjustWindowLayout(window, window->flags | SDL_WINDOW_MAXIMIZED, display->current_mode.w, display->current_mode.h) ;
 
-    windata->window->MoveTo(windata->window, 0, 0);
-    windata->window->Resize(windata->window,
-                            display->current_mode.w, display->current_mode.h);
-    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
+    SDL_DFB_CHECK(windata->window->MoveTo(windata->window, 0, 0));
+    SDL_DFB_CHECK(windata->window->Resize(windata->window,
+                            display->current_mode.w, display->current_mode.h));
 }
 
 void
@@ -218,15 +243,13 @@
 {
     SDL_DFB_WINDOWDATA(window);
 
-    /* Do this already here */
-    //window->flags &= ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED);
+    DirectFB_WM_AdjustWindowLayout(window, window->flags & ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED), 
+    	windata->restore.w, windata->restore.h);
 
-    DirectFB_WM_AdjustWindowLayout(window);
-    windata->window->MoveTo(windata->window, windata->restore.x,
-                            windata->restore.y);
-    windata->window->Resize(windata->window, windata->restore.w,
-                            windata->restore.h);
-    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
+    SDL_DFB_CHECK(windata->window->Resize(windata->window, windata->restore.w,
+                            windata->restore.h));
+    SDL_DFB_CHECK(windata->window->MoveTo(windata->window, windata->restore.x,
+                            windata->restore.y));
 }
 
 enum
@@ -291,7 +314,9 @@
 int
 DirectFB_WM_ProcessEvent(_THIS, SDL_Window * window, DFBWindowEvent * evt)
 {
+    SDL_DFB_DEVICEDATA(_this);
     SDL_DFB_WINDOWDATA(window);
+	DFB_WindowData *gwindata = ((devdata->grabbed_window) ? (DFB_WindowData *) ((devdata->grabbed_window)->driverdata) : NULL);
 
     if (!windata->is_managed)
         return 0;
@@ -304,19 +329,26 @@
             case WM_POS_NONE:
                 return 0;
             case WM_POS_CLOSE:
+		        wm_grab = WM_POS_NONE;
                 SDL_SendWindowEvent(window, SDL_WINDOWEVENT_CLOSE, 0,
                                     0);
                 return 1;
             case WM_POS_MAX:
+		        wm_grab = WM_POS_NONE;
                 if (window->flags & SDL_WINDOW_MAXIMIZED) {
-                    DirectFB_WM_RestoreWindow(_this, window);
+                	SDL_RestoreWindow(window);
                 } else {
-                    DirectFB_WM_MaximizeWindow(_this, window);
+                    SDL_MaximizeWindow(window);
                 }
                 return 1;
+            case WM_POS_CAPTION:
+                DirectFB_RaiseWindow(_this, window);
+                /* fall through */
             default:
                 wm_grab = pos;
-                windata->window->GrabPointer(windata->window);
+                if (gwindata != NULL)
+	                SDL_DFB_CHECK(gwindata->window->UngrabPointer(gwindata->window));
+                SDL_DFB_CHECK(windata->window->GrabPointer(windata->window));
                 wm_lastx = evt->cx;
                 wm_lasty = evt->cy;
             }
@@ -333,20 +365,22 @@
             int cw, ch;
 
             if (wm_grab & WM_POS_CAPTION)
-                windata->window->Move(windata->window, dx, dy);
-            if (wm_grab & WM_POS_RIGHT) {
-                windata->window->GetSize(windata->window, &cw, &ch);
-                windata->window->Resize(windata->window, cw + dx, ch);
-            }
-            if (wm_grab & WM_POS_BOTTOM) {
-                windata->window->GetSize(windata->window, &cw, &ch);
-                windata->window->Resize(windata->window, cw, ch + dy);
+                SDL_DFB_CHECK(windata->window->Move(windata->window, dx, dy));
+	        if (wm_grab & (WM_POS_RIGHT | WM_POS_BOTTOM)) {
+	            if ((wm_grab & (WM_POS_BOTTOM | WM_POS_RIGHT)) == WM_POS_BOTTOM)
+	            	dx = 0;
+	            else if ((wm_grab & (WM_POS_BOTTOM | WM_POS_RIGHT)) == WM_POS_RIGHT)
+	            	dy = 0;
+                SDL_DFB_CHECK(windata->window->GetSize(windata->window, &cw, &ch));
+                SDL_DFB_CHECK(windata->window->Resize(windata->window, cw + dx, ch + dy));
             }
             wm_lastx = evt->cx;
             wm_lasty = evt->cy;
             return 1;
         }
-        windata->window->UngrabPointer(windata->window);
+        SDL_DFB_CHECK(windata->window->UngrabPointer(windata->window));
+        if (gwindata != NULL)
+            SDL_DFB_CHECK(gwindata->window->GrabPointer(gwindata->window));
         wm_grab = WM_POS_NONE;
         break;
     case DWET_KEYDOWN:
--- a/src/video/directfb/SDL_DirectFB_WM.h	Sat Aug 14 12:28:43 2010 -0700
+++ b/src/video/directfb/SDL_DirectFB_WM.h	Mon Aug 16 09:04:55 2010 -0700
@@ -41,10 +41,10 @@
     DFBColor max_color;
 };
 
-extern void DirectFB_WM_AdjustWindowLayout(SDL_Window * window);
+extern void DirectFB_WM_AdjustWindowLayout(SDL_Window * window, int flags, int w, int h);
 extern void DirectFB_WM_MaximizeWindow(_THIS, SDL_Window * window);
 extern void DirectFB_WM_RestoreWindow(_THIS, SDL_Window * window);
-extern void DirectFB_WM_RedrawLayout(SDL_Window * window);
+extern void DirectFB_WM_RedrawLayout(_THIS, SDL_Window * window);
 
 extern int DirectFB_WM_ProcessEvent(_THIS, SDL_Window * window,
                                     DFBWindowEvent * evt);
--- a/src/video/directfb/SDL_DirectFB_dyn.c	Sat Aug 14 12:28:43 2010 -0700
+++ b/src/video/directfb/SDL_DirectFB_dyn.c	Mon Aug 16 09:04:55 2010 -0700
@@ -31,7 +31,8 @@
 #define DFB_SYM(ret, name, args, al, func) ret (*name) args;
 static struct _SDL_DirectFB_Symbols
 {
-    DFB_SYMS const unsigned int *directfb_major_version;
+    DFB_SYMS 
+    const unsigned int *directfb_major_version;
     const unsigned int *directfb_minor_version;
     const unsigned int *directfb_micro_version;
 } SDL_DirectFB_Symbols;
--- a/src/video/directfb/SDL_DirectFB_events.c	Sat Aug 14 12:28:43 2010 -0700
+++ b/src/video/directfb/SDL_DirectFB_events.c	Mon Aug 16 09:04:55 2010 -0700
@@ -33,21 +33,28 @@
 #include "SDL_DirectFB_events.h"
 
 #if USE_MULTI_API
-#define SDL_SendMouseMotion_ex(id, relative, x, y, p) SDL_SendMouseMotion(id, relative, x, y, p)
-#define SDL_SendMouseButton_ex(id, state, button) SDL_SendMouseButton(id, state, button)
+#define SDL_SendMouseMotion_ex(w, id, relative, x, y, p) SDL_SendMouseMotion(id, relative, x, y, p)
+#define SDL_SendMouseButton_ex(w, id, state, button) SDL_SendMouseButton(id, state, button)
 #define SDL_SendKeyboardKey_ex(id, state, scancode) SDL_SendKeyboardKey(id, state, scancode)
 #define SDL_SendKeyboardText_ex(id, text) SDL_SendKeyboardText(id, text)
 #else
-#define SDL_SendMouseMotion_ex(id, relative, x, y, p) SDL_SendMouseMotion(relative, x, y)
-#define SDL_SendMouseButton_ex(id, state, button) SDL_SendMouseButton(state, button)
+#define SDL_SendMouseMotion_ex(w, id, relative, x, y, p) SDL_SendMouseMotion(w, relative, x, y)
+#define SDL_SendMouseButton_ex(w, id, state, button) SDL_SendMouseButton(w, state, button)
 #define SDL_SendKeyboardKey_ex(id, state, scancode) SDL_SendKeyboardKey(state, scancode)
 #define SDL_SendKeyboardText_ex(id, text) SDL_SendKeyboardText(text)
 #endif
 
+typedef struct _cb_data cb_data;
+struct _cb_data 
+{
+	DFB_DeviceData *devdata;	
+	int sys_ids;
+	int sys_kbd;
+};
 
 /* The translation tables from a DirectFB keycode to a SDL keysym */
 static SDLKey oskeymap[256];
-static int sys_ids;
+
 
 static SDL_keysym *DirectFB_TranslateKey(_THIS, DFBWindowEvent * evt,
                                          SDL_keysym * keysym);
@@ -69,14 +76,11 @@
 
     SDL_VideoDisplay *display = window->display;
     DFB_DisplayData *dispdata = (DFB_DisplayData *) display->driverdata;
-    int ret;
 
+	/* FIXME: should we handle the error */
     if (dispdata->vidIDinuse)
-        SDL_DFB_CHECKERR(dispdata->vidlayer->SwitchContext(dispdata->vidlayer,
+        SDL_DFB_CHECK(dispdata->vidlayer->SwitchContext(dispdata->vidlayer,
                                                            DFB_TRUE));
-
-  error:
-    return;
 #endif
 
 }
@@ -172,9 +176,9 @@
         case DWET_BUTTONDOWN:
             if (ClientXY(p, &evt->x, &evt->y)) {
                 if (!devdata->use_linux_input) {
-                    SDL_SendMouseMotion_ex(devdata->mouse_id[0], 0, evt->x,
+                    SDL_SendMouseMotion_ex(p->sdl_window, devdata->mouse_id[0], 0, evt->x,
                                         evt->y, 0);
-                    SDL_SendMouseButton_ex(devdata->mouse_id[0],
+                    SDL_SendMouseButton_ex(p->sdl_window, devdata->mouse_id[0],
                                         SDL_PRESSED,
                                         DirectFB_TranslateButton
                                         (evt->button));
@@ -186,9 +190,9 @@
         case DWET_BUTTONUP:
             if (ClientXY(p, &evt->x, &evt->y)) {
                 if (!devdata->use_linux_input) {
-                    SDL_SendMouseMotion_ex(devdata->mouse_id[0], 0, evt->x,
+                    SDL_SendMouseMotion_ex(p->sdl_window, devdata->mouse_id[0], 0, evt->x,
                                         evt->y, 0);
-                    SDL_SendMouseButton_ex(devdata->mouse_id[0],
+                    SDL_SendMouseButton_ex(p->sdl_window, devdata->mouse_id[0],
                                         SDL_RELEASED,
                                         DirectFB_TranslateButton
                                         (evt->button));
@@ -202,7 +206,7 @@
                 SDL_Window *window = p->sdl_window;
                 if (!devdata->use_linux_input) {
                     if (!(flags & SDL_WINDOW_INPUT_GRABBED))
-                        SDL_SendMouseMotion_ex(devdata->mouse_id[0], 0,
+                        SDL_SendMouseMotion_ex(p->sdl_window, devdata->mouse_id[0], 0,
                                             evt->x, evt->y, 0);
                 } else {
                     /* relative movements are not exact! 
@@ -215,7 +219,7 @@
                     }
                 }
                 if (!(window->flags & SDL_WINDOW_MOUSE_FOCUS))
-                    SDL_SendWindowEvent(p->window, SDL_WINDOWEVENT_ENTER, 0,
+                    SDL_SendWindowEvent(p->sdl_window, SDL_WINDOWEVENT_ENTER, 0,
                                         0);
             }
             break;
@@ -293,7 +297,7 @@
 }
 
 static void
-ProcessInputEvent(_THIS, SDL_Window *grabbed_window, DFBInputEvent * ievt)
+ProcessInputEvent(_THIS, DFBInputEvent * ievt)
 {
     SDL_DFB_DEVICEDATA(_this);
     SDL_keysym keysym;
@@ -302,17 +306,16 @@
 
     if (!devdata->use_linux_input) {
         if (ievt->type == DIET_AXISMOTION) {
-            if ((grabbed_window != NULL) && (ievt->flags & DIEF_AXISREL)) {
+            if ((devdata->grabbed_window != NULL) && (ievt->flags & DIEF_AXISREL)) {
                 if (ievt->axis == DIAI_X)
-                    SDL_SendMouseMotion_ex(ievt->device_id, 1,
+                    SDL_SendMouseMotion_ex(devdata->grabbed_window, ievt->device_id, 1,
                                         ievt->axisrel, 0, 0);
                 else if (ievt->axis == DIAI_Y)
-                    SDL_SendMouseMotion_ex(ievt->device_id, 1, 0,
+                    SDL_SendMouseMotion_ex(devdata->grabbed_window, ievt->device_id, 1, 0,
                                         ievt->axisrel, 0);
             }
         }
     } else {
-#if USE_MULTI_API
         static int last_x, last_y;
 
         switch (ievt->type) {
@@ -323,30 +326,34 @@
                 else if (ievt->axis == DIAI_Y)
                     last_y = ievt->axisabs;
                 if (!(ievt->flags & DIEF_FOLLOW)) {
+#if USE_MULTI_API
                     SDL_Mouse *mouse = SDL_GetMouse(ievt->device_id);
                     SDL_Window *window = SDL_GetWindowFromID(mouse->focus);
+#else
+                    SDL_Window *window = devdata->grabbed_window;
+#endif
                     if (window) {
                         DFB_WindowData *windata =
                             (DFB_WindowData *) window->driverdata;
                         int x, y;
 
                         windata->window->GetPosition(windata->window, &x, &y);
-                        SDL_SendMouseMotion_ex(ievt->device_id, 0,
+                        SDL_SendMouseMotion_ex(window, ievt->device_id, 0,
                                             last_x - (x +
                                                       windata->client.x),
                                             last_y - (y +
                                                       windata->client.y), 0);
                     } else {
-                        SDL_SendMouseMotion_ex(ievt->device_id, 0, last_x,
+                        SDL_SendMouseMotion_ex(window, ievt->device_id, 0, last_x,
                                             last_y, 0);
                     }
                 }
             } else if (ievt->flags & DIEF_AXISREL) {
                 if (ievt->axis == DIAI_X)
-                    SDL_SendMouseMotion_ex(ievt->device_id, 1,
+                    SDL_SendMouseMotion_ex(devdata->grabbed_window, ievt->device_id, 1,
                                         ievt->axisrel, 0, 0);
                 else if (ievt->axis == DIAI_Y)
-                    SDL_SendMouseMotion_ex(ievt->device_id, 1, 0,
+                    SDL_SendMouseMotion_ex(devdata->grabbed_window, ievt->device_id, 1, 0,
                                         ievt->axisrel, 0);
             }
             break;
@@ -365,28 +372,27 @@
         case DIET_KEYRELEASE:
             kbd_idx = KbdIndex(_this, ievt->device_id);
             DirectFB_TranslateKeyInputEvent(_this, kbd_idx, ievt, &keysym);
-            SDL_SendKeyboardKey(kbd_idx, SDL_RELEASED, keysym.scancode);
+            SDL_SendKeyboardKey_ex(kbd_idx, SDL_RELEASED, keysym.scancode);
             break;
         case DIET_BUTTONPRESS:
             if (ievt->buttons & DIBM_LEFT)
-                SDL_SendMouseButton(ievt->device_id, SDL_PRESSED, 1);
+                SDL_SendMouseButton_ex(devdata->grabbed_window, ievt->device_id, SDL_PRESSED, 1);
             if (ievt->buttons & DIBM_MIDDLE)
-                SDL_SendMouseButton(ievt->device_id, SDL_PRESSED, 2);
+                SDL_SendMouseButton_ex(devdata->grabbed_window, ievt->device_id, SDL_PRESSED, 2);
             if (ievt->buttons & DIBM_RIGHT)
-                SDL_SendMouseButton(ievt->device_id, SDL_PRESSED, 3);
+                SDL_SendMouseButton_ex(devdata->grabbed_window, ievt->device_id, SDL_PRESSED, 3);
             break;
         case DIET_BUTTONRELEASE:
             if (!(ievt->buttons & DIBM_LEFT))
-                SDL_SendMouseButton(ievt->device_id, SDL_RELEASED, 1);
+                SDL_SendMouseButton_ex(devdata->grabbed_window, ievt->device_id, SDL_RELEASED, 1);
             if (!(ievt->buttons & DIBM_MIDDLE))
-                SDL_SendMouseButton(ievt->device_id, SDL_RELEASED, 2);
+                SDL_SendMouseButton_ex(devdata->grabbed_window, ievt->device_id, SDL_RELEASED, 2);
             if (!(ievt->buttons & DIBM_RIGHT))
-                SDL_SendMouseButton(ievt->device_id, SDL_RELEASED, 3);
+                SDL_SendMouseButton_ex(devdata->grabbed_window, ievt->device_id, SDL_RELEASED, 3);
             break;
         default:
             break;              /* please gcc */
         }
-#endif
     }
 }
 
@@ -396,18 +402,11 @@
     SDL_DFB_DEVICEDATA(_this);
     DFB_WindowData *p;
     DFBInputEvent ievt;
-    SDL_Window *grabbed_window;
-
-    grabbed_window = NULL;
 
     for (p = devdata->firstwin; p != NULL; p = p->next) {
         DFBWindowEvent evt;
         SDL_Window *w = p->sdl_window;
 
-        if (w->flags & SDL_WINDOW_INPUT_GRABBED) {
-            grabbed_window = w;
-        }
-
         while (p->eventbuffer->GetEvent(p->eventbuffer,
                                         DFB_EVENT(&evt)) == DFB_OK) {
             if (!DirectFB_WM_ProcessEvent(_this, w, &evt))
@@ -418,7 +417,7 @@
     /* Now get relative events in case we need them */
     while (devdata->events->GetEvent(devdata->events,
                                      DFB_EVENT(&ievt)) == DFB_OK) {
-        ProcessInputEvent(_this, grabbed_window, &ievt);
+        ProcessInputEvent(_this, &ievt);
     }
 }
 
@@ -623,105 +622,87 @@
 }
 
 static DFBEnumerationResult
-input_device_cb(DFBInputDeviceID device_id,
+EnumKeyboards(DFBInputDeviceID device_id,
                 DFBInputDeviceDescription desc, void *callbackdata)
 {
-    DFB_DeviceData *devdata = callbackdata;
+	cb_data *cb = callbackdata;
+    DFB_DeviceData *devdata = cb->devdata;
 #if USE_MULTI_API
     SDL_Keyboard keyboard;
 #endif
     SDLKey keymap[SDL_NUM_SCANCODES];
 
-    if ((desc.caps & DIDTF_KEYBOARD) && device_id == DIDID_KEYBOARD) {
+	if (!cb->sys_kbd) {
+		if (cb->sys_ids) {
+		    if (device_id >= 0x10)
+		        return DFENUM_OK;
+		} else {
+		    if (device_id < 0x10)
+		        return DFENUM_OK;
+		}
+	} else {
+		if (device_id != DIDID_KEYBOARD)
+		    return DFENUM_OK;
+	}
+	
+    if ((desc.caps & DIDTF_KEYBOARD)) {
 #if USE_MULTI_API
         SDL_zero(keyboard);
-        SDL_AddKeyboard(&keyboard, 0);
-#endif
-        devdata->keyboard[0].id = device_id;
-        devdata->keyboard[0].is_generic = 0;
-        if (!strncmp("X11", desc.name, 3))
-            devdata->keyboard[0].is_generic = 1;
-
-        SDL_GetDefaultKeymap(keymap);
-#if USE_MULTI_API
-        SDL_SetKeymap(0, 0, keymap, SDL_NUM_SCANCODES);
-#else
-        SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES);
+        SDL_AddKeyboard(&keyboard, devdata->num_keyboard);
 #endif
-        devdata->num_keyboard++;
-
-        return DFENUM_CANCEL;
-    }
-    return DFENUM_OK;
-}
-
-#if USE_MULTI_API
-static DFBEnumerationResult
-EnumKeyboards(DFBInputDeviceID device_id,
-              DFBInputDeviceDescription desc, void *callbackdata)
-{
-    DFB_DeviceData *devdata = callbackdata;
-    SDL_Keyboard keyboard;
-    SDLKey keymap[SDL_NUM_SCANCODES];
-
-    if (sys_ids) {
-        if (device_id >= 0x10)
-            return DFENUM_OK;
-    } else {
-        if (device_id < 0x10)
-            return DFENUM_OK;
-    }
-    if ((desc.caps & DIDTF_KEYBOARD)) {
-        SDL_zero(keyboard);
-        SDL_AddKeyboard(&keyboard, devdata->num_keyboard);
         devdata->keyboard[devdata->num_keyboard].id = device_id;
         devdata->keyboard[devdata->num_keyboard].is_generic = 0;
         if (!strncmp("X11", desc.name, 3))
             devdata->keyboard[devdata->num_keyboard].is_generic = 1;
 
         SDL_GetDefaultKeymap(keymap);
+#if USE_MULTI_API
         SDL_SetKeymap(devdata->num_keyboard, 0, keymap, SDL_NUM_SCANCODES);
+#else
+        SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES);
+#endif
         devdata->num_keyboard++;
+
+		if (cb->sys_kbd)
+	        return DFENUM_CANCEL;
     }
     return DFENUM_OK;
 }
-#endif
 
 void
 DirectFB_InitKeyboard(_THIS)
 {
     SDL_DFB_DEVICEDATA(_this);
-    int ret;
-
+	cb_data cb;    
+	
     DirectFB_InitOSKeymap(_this, &oskeymap[0], SDL_arraysize(oskeymap));
 
     devdata->num_keyboard = 0;
-#if USE_MULTI_API
+    cb.devdata = devdata;
+    
     if (devdata->use_linux_input) {
-        sys_ids = 0;
+    	cb.sys_kbd = 0;
+        cb.sys_ids = 0;
         SDL_DFB_CHECK(devdata->dfb->
-                      EnumInputDevices(devdata->dfb, EnumKeyboards, devdata));
+                      EnumInputDevices(devdata->dfb, EnumKeyboards, &cb));
         if (devdata->num_keyboard == 0) {
-            sys_ids = 1;
+            cb.sys_ids = 1;
             SDL_DFB_CHECK(devdata->dfb->EnumInputDevices(devdata->dfb,
                                                          EnumKeyboards,
-                                                         devdata));
+                                                         &cb));
         }
-    } else
-#else
-    {
+    } else {
+    	cb.sys_kbd = 1;
         SDL_DFB_CHECK(devdata->dfb->EnumInputDevices(devdata->dfb,
-                                                     input_device_cb,
-                                                     devdata));
+                                                     EnumKeyboards,
+                                                     &cb));
     }
-#endif
 }
 
 void
 DirectFB_QuitKeyboard(_THIS)
 {
-    SDL_DFB_DEVICEDATA(_this);
-    int ret;
+    //SDL_DFB_DEVICEDATA(_this);
 
     SDL_KeyboardQuit();
 
--- a/src/video/directfb/SDL_DirectFB_modes.c	Sat Aug 14 12:28:43 2010 -0700
+++ b/src/video/directfb/SDL_DirectFB_modes.c	Mon Aug 16 09:04:55 2010 -0700
@@ -40,112 +40,101 @@
     SDL_DisplayMode *modelist;
 };
 
-static int
-DFBToSDLPixelFormat(DFBSurfacePixelFormat pixelformat, Uint32 * fmt)
+static const struct {
+    DFBSurfacePixelFormat dfb;
+    Uint32 sdl;
+} pixelformat_tab[] = 
 {
-    switch (pixelformat) {
-    case DSPF_ALUT44:
-        *fmt = SDL_PIXELFORMAT_INDEX4LSB;
-        break;
-    case DSPF_LUT8:
-        *fmt = SDL_PIXELFORMAT_INDEX8;
-        break;
-    case DSPF_RGB332:
-        *fmt = SDL_PIXELFORMAT_RGB332;
-        break;
-    case DSPF_ARGB4444:
-        *fmt = SDL_PIXELFORMAT_ARGB4444;
-        break;
-    case SDL_PIXELFORMAT_ARGB1555:
-        *fmt = SDL_PIXELFORMAT_ARGB1555;
-        break;
-    case DSPF_RGB16:
-        *fmt = SDL_PIXELFORMAT_RGB565;
-        break;
-    case DSPF_RGB24:
-        *fmt = SDL_PIXELFORMAT_RGB24;
-        break;
-    case DSPF_RGB32:
-        *fmt = SDL_PIXELFORMAT_RGB888;
-        break;
-    case DSPF_ARGB:
-        *fmt = SDL_PIXELFORMAT_ARGB8888;
-        break;
-    case DSPF_YV12:
-        *fmt = SDL_PIXELFORMAT_YV12;
-        break;                  /* Planar mode: Y + V + U  (3 planes) */
-    case DSPF_I420:
-        *fmt = SDL_PIXELFORMAT_IYUV;
-        break;                  /* Planar mode: Y + U + V  (3 planes) */
-    case DSPF_YUY2:
-        *fmt = SDL_PIXELFORMAT_YUY2;
-        break;                  /* Packed mode: Y0+U0+Y1+V0 (1 plane) */
-    case DSPF_UYVY:
-        *fmt = SDL_PIXELFORMAT_UYVY;
-        break;                  /* Packed mode: U0+Y0+V0+Y1 (1 plane) */
-    default:
-        return -1;
-    }
-    return 0;
+    { DSPF_LUT8, SDL_PIXELFORMAT_INDEX8 },              /* 8 bit LUT (8 bit color and alpha lookup from palette) */
+    { DSPF_RGB332, SDL_PIXELFORMAT_RGB332 },            /* 8 bit RGB (1 byte, red 3@5, green 3@2, blue 2@0) */
+    { DSPF_ARGB4444, SDL_PIXELFORMAT_ARGB4444 },        /* 16 bit ARGB (2 byte, alpha 4@12, red 4@8, green 4@4, blue 4@0) */
+    { DSPF_ARGB1555, SDL_PIXELFORMAT_ARGB1555 },        /* 16 bit ARGB (2 byte, alpha 1@15, red 5@10, green 5@5, blue 5@0) */
+    { DSPF_RGB16, SDL_PIXELFORMAT_RGB565 },             /* 16 bit RGB (2 byte, red 5@11, green 6@5, blue 5@0) */
+    { DSPF_RGB24, SDL_PIXELFORMAT_RGB24 },              /* 24 bit RGB (3 byte, red 8@16, green 8@8, blue 8@0) */
+    { DSPF_RGB32, SDL_PIXELFORMAT_RGB888 },             /* 24 bit RGB (4 byte, nothing@24, red 8@16, green 8@8, blue 8@0) */
+    { DSPF_ARGB, SDL_PIXELFORMAT_ARGB8888 },            /* 32 bit ARGB (4 byte, alpha 8@24, red 8@16, green 8@8, blue 8@0) */
+    { DSPF_RGB444, SDL_PIXELFORMAT_RGB444 },            /* 16 bit RGB (2 byte, nothing @12, red 4@8, green 4@4, blue 4@0) */
+    { DSPF_YV12, SDL_PIXELFORMAT_YV12 },                /* 12 bit YUV (8 bit Y plane followed by 8 bit quarter size V/U planes) */
+    { DSPF_I420,SDL_PIXELFORMAT_IYUV },                 /* 12 bit YUV (8 bit Y plane followed by 8 bit quarter size U/V planes) */
+    { DSPF_YUY2, SDL_PIXELFORMAT_YUY2 },                /* 16 bit YUV (4 byte/ 2 pixel, macropixel contains CbYCrY [31:0]) */
+    { DSPF_UYVY, SDL_PIXELFORMAT_UYVY },                /* 16 bit YUV (4 byte/ 2 pixel, macropixel contains YCbYCr [31:0]) */
+    { DSPF_RGB555, SDL_PIXELFORMAT_RGB555 },            /* 16 bit RGB (2 byte, nothing @15, red 5@10, green 5@5, blue 5@0) */
+
+#if (DFB_VERSION_ATLEAST(1,2,0))
+    { DSPF_BGR555, SDL_PIXELFORMAT_BGR555 },            /* 16 bit BGR (2 byte, nothing @15, blue 5@10, green 5@5, red 5@0) */
+#else
+    { DSPF_UNKNOWN, SDL_PIXELFORMAT_BGR555 },
+#endif
+
+    /* Pfff ... nonmatching formats follow */    
+    
+    { DSPF_ALUT44, SDL_PIXELFORMAT_UNKNOWN },           /* 8 bit ALUT (1 byte, alpha 4@4, color lookup 4@0) */
+ 	{ DSPF_A8, SDL_PIXELFORMAT_UNKNOWN },               /* 	8 bit alpha (1 byte, alpha 8@0), e.g. anti-aliased glyphs */
+ 	{ DSPF_AiRGB, SDL_PIXELFORMAT_UNKNOWN },            /* 	32 bit ARGB (4 byte, inv. alpha 8@24, red 8@16, green 8@8, blue 8@0) */
+ 	{ DSPF_A1, SDL_PIXELFORMAT_UNKNOWN },               /* 	1 bit alpha (1 byte/ 8 pixel, most significant bit used first) */
+ 	{ DSPF_NV12, SDL_PIXELFORMAT_UNKNOWN },             /* 	12 bit YUV (8 bit Y plane followed by one 16 bit quarter size CbCr [15:0] plane) */
+ 	{ DSPF_NV16, SDL_PIXELFORMAT_UNKNOWN },             /* 	16 bit YUV (8 bit Y plane followed by one 16 bit half width CbCr [15:0] plane) */
+ 	{ DSPF_ARGB2554, SDL_PIXELFORMAT_UNKNOWN },         /* 	16 bit ARGB (2 byte, alpha 2@14, red 5@9, green 5@4, blue 4@0) */
+ 	{ DSPF_NV21, SDL_PIXELFORMAT_UNKNOWN },             /* 	12 bit YUV (8 bit Y plane followed by one 16 bit quarter size CrCb [15:0] plane) */
+ 	{ DSPF_AYUV, SDL_PIXELFORMAT_UNKNOWN },             /* 	32 bit AYUV (4 byte, alpha 8@24, Y 8@16, Cb 8@8, Cr 8@0) */
+ 	{ DSPF_A4, SDL_PIXELFORMAT_UNKNOWN },               /* 	4 bit alpha (1 byte/ 2 pixel, more significant nibble used first) */
+ 	{ DSPF_ARGB1666, SDL_PIXELFORMAT_UNKNOWN },         /* 	1 bit alpha (3 byte/ alpha 1@18, red 6@16, green 6@6, blue 6@0) */
+ 	{ DSPF_ARGB6666, SDL_PIXELFORMAT_UNKNOWN },         /* 	6 bit alpha (3 byte/ alpha 6@18, red 6@16, green 6@6, blue 6@0) */
+ 	{ DSPF_RGB18, SDL_PIXELFORMAT_UNKNOWN },            /* 	6 bit RGB (3 byte/ red 6@16, green 6@6, blue 6@0) */
+ 	{ DSPF_LUT2, SDL_PIXELFORMAT_UNKNOWN },             /* 	2 bit LUT (1 byte/ 4 pixel, 2 bit color and alpha lookup from palette) */
+
+#if (DFB_VERSION_ATLEAST(1,3,0))
+ 	{ DSPF_RGBA4444, SDL_PIXELFORMAT_UNKNOWN },         /* 16 bit RGBA (2 byte, red 4@12, green 4@8, blue 4@4, alpha 4@0) */
+#endif
+
+#if (DFB_VERSION_ATLEAST(1,4,0))
+ 	{ DSPF_RGBA5551, SDL_PIXELFORMAT_UNKNOWN },         /* 	16 bit RGBA (2 byte, red 5@11, green 5@6, blue 5@1, alpha 1@0) */
+ 	{ DSPF_YUV444P, SDL_PIXELFORMAT_UNKNOWN },          /* 	24 bit full YUV planar (8 bit Y plane followed by an 8 bit Cb and an 8 bit Cr plane) */
+ 	{ DSPF_ARGB8565, SDL_PIXELFORMAT_UNKNOWN },         /* 	24 bit ARGB (3 byte, alpha 8@16, red 5@11, green 6@5, blue 5@0) */
+ 	{ DSPF_AVYU, SDL_PIXELFORMAT_UNKNOWN },             /* 	32 bit AVYU 4:4:4 (4 byte, alpha 8@24, Cr 8@16, Y 8@8, Cb 8@0) */
+ 	{ DSPF_VYU, SDL_PIXELFORMAT_UNKNOWN },              /* 	24 bit VYU 4:4:4 (3 byte, Cr 8@16, Y 8@8, Cb 8@0)  */
+#endif
+ 	
+    { DSPF_UNKNOWN, SDL_PIXELFORMAT_INDEX1LSB },
+    { DSPF_UNKNOWN, SDL_PIXELFORMAT_INDEX1MSB },
+    { DSPF_UNKNOWN, SDL_PIXELFORMAT_INDEX4LSB }, 
+    { DSPF_UNKNOWN, SDL_PIXELFORMAT_INDEX4MSB },
+    { DSPF_UNKNOWN, SDL_PIXELFORMAT_BGR24 },
+    { DSPF_UNKNOWN, SDL_PIXELFORMAT_BGR888 },
+    { DSPF_UNKNOWN, SDL_PIXELFORMAT_RGBA8888 },
+    { DSPF_UNKNOWN, SDL_PIXELFORMAT_ABGR8888 },
+    { DSPF_UNKNOWN, SDL_PIXELFORMAT_BGRA8888 },
+    { DSPF_UNKNOWN, SDL_PIXELFORMAT_ARGB2101010 },
+    { DSPF_UNKNOWN, SDL_PIXELFORMAT_ABGR4444 },
+    { DSPF_UNKNOWN, SDL_PIXELFORMAT_ABGR1555 },
+    { DSPF_UNKNOWN, SDL_PIXELFORMAT_BGR565 },
+    { DSPF_UNKNOWN, SDL_PIXELFORMAT_YVYU },                        /**< Packed mode: Y0+V0+Y1+U0 (1 pla	*/
+};
+
+static Uint32
+DFBToSDLPixelFormat(DFBSurfacePixelFormat pixelformat)
+{
+    int i;
+    
+    for (i=0; pixelformat_tab[i].dfb != DSPF_UNKNOWN; i++)
+        if (pixelformat_tab[i].dfb == pixelformat)
+        {
+            return pixelformat_tab[i].sdl;
+        }
+    return SDL_PIXELFORMAT_UNKNOWN;
 }
 
 static DFBSurfacePixelFormat
 SDLToDFBPixelFormat(Uint32 format)
 {
-    switch (format) {
-    case SDL_PIXELFORMAT_INDEX4LSB:
-        return DSPF_ALUT44;
-    case SDL_PIXELFORMAT_INDEX8:
-        return DSPF_LUT8;
-    case SDL_PIXELFORMAT_RGB332:
-        return DSPF_RGB332;
-    case SDL_PIXELFORMAT_RGB555:
-        return DSPF_ARGB1555;
-    case SDL_PIXELFORMAT_ARGB4444:
-        return DSPF_ARGB4444;
-    case SDL_PIXELFORMAT_ARGB1555:
-        return DSPF_ARGB1555;
-    case SDL_PIXELFORMAT_RGB565:
-        return DSPF_RGB16;
-    case SDL_PIXELFORMAT_RGB24:
-        return DSPF_RGB24;
-    case SDL_PIXELFORMAT_RGB888:
-        return DSPF_RGB32;
-    case SDL_PIXELFORMAT_ARGB8888:
-        return DSPF_ARGB;
-    case SDL_PIXELFORMAT_YV12:
-        return DSPF_YV12;       /* Planar mode: Y + V + U  (3 planes) */
-    case SDL_PIXELFORMAT_IYUV:
-        return DSPF_I420;       /* Planar mode: Y + U + V  (3 planes) */
-    case SDL_PIXELFORMAT_YUY2:
-        return DSPF_YUY2;       /* Packed mode: Y0+U0+Y1+V0 (1 plane) */
-    case SDL_PIXELFORMAT_UYVY:
-        return DSPF_UYVY;       /* Packed mode: U0+Y0+V0+Y1 (1 plane) */
-    case SDL_PIXELFORMAT_YVYU:
-        return DSPF_UNKNOWN;    /* Packed mode: Y0+V0+Y1+U0 (1 plane) */
-    case SDL_PIXELFORMAT_INDEX1LSB:
-        return DSPF_UNKNOWN;
-    case SDL_PIXELFORMAT_INDEX1MSB:
-        return DSPF_UNKNOWN;
-    case SDL_PIXELFORMAT_INDEX4MSB:
-        return DSPF_UNKNOWN;
-    case SDL_PIXELFORMAT_RGB444:
-        return DSPF_UNKNOWN;
-    case SDL_PIXELFORMAT_BGR24:
-        return DSPF_UNKNOWN;
-    case SDL_PIXELFORMAT_BGR888:
-        return DSPF_UNKNOWN;
-    case SDL_PIXELFORMAT_RGBA8888:
-        return DSPF_UNKNOWN;
-    case SDL_PIXELFORMAT_ABGR8888:
-        return DSPF_UNKNOWN;
-    case SDL_PIXELFORMAT_BGRA8888:
-        return DSPF_UNKNOWN;
-    case SDL_PIXELFORMAT_ARGB2101010:
-        return DSPF_UNKNOWN;
-    default:
-        return DSPF_UNKNOWN;
-    }
+    int i;
+    
+    for (i=0; pixelformat_tab[i].dfb != DSPF_UNKNOWN; i++)
+        if (pixelformat_tab[i].sdl == format)
+        {
+            return pixelformat_tab[i].dfb;
+        }
+    return  DSPF_UNKNOWN;
 }
 
 static DFBEnumerationResult
@@ -164,7 +153,6 @@
         modedata->modelist[modedata->nummodes++] = mode;
     }
 
-    SDL_DFB_DEBUG("w %d h %d bpp %d\n", width, height, bpp);
     return DFENUM_OK;
 }
 
@@ -202,7 +190,6 @@
     SDL_DFB_DEVICEDATA(_this);
     DFBDisplayLayerConfig config;
     DFBDisplayLayerConfigFlags failed;
-    int ret;
 
     SDL_DFB_CHECKERR(data->layer->SetCooperativeLevel(data->layer,
                                                       DLSCL_ADMINISTRATIVE));
@@ -221,7 +208,7 @@
     if (failed == 0)
         SDL_AddDisplayMode(display, mode);
     else
-        SDL_DFB_DEBUG("Mode %d x %d not available: %x\n", mode->w,
+        SDL_DFB_ERR("Mode %d x %d not available: %x\n", mode->w,
                       mode->h, failed);
 
     return;
@@ -295,19 +282,21 @@
             dlc.pixelformat = DSPF_ARGB;
             dlc.options = DLOP_ALPHACHANNEL;
 
-            ret = layer->SetConfiguration(layer, &dlc);
-            if (ret) {
+            ret = SDL_DFB_CHECK(layer->SetConfiguration(layer, &dlc));
+            if (ret != DFB_OK) {
                 /* try AiRGB if the previous failed */
                 dlc.pixelformat = DSPF_AiRGB;
-                ret = layer->SetConfiguration(layer, &dlc);
+                SDL_DFB_CHECKERR(layer->SetConfiguration(layer, &dlc));
             }
         }
 
         /* Query layer configuration to determine the current mode and pixelformat */
         dlc.flags = DLCONF_ALL;
-        layer->GetConfiguration(layer, &dlc);
+        SDL_DFB_CHECKERR(layer->GetConfiguration(layer, &dlc));
 
-        if (DFBToSDLPixelFormat(dlc.pixelformat, &mode.format) != 0) {
+        mode.format = DFBToSDLPixelFormat(dlc.pixelformat);
+        
+        if (mode.format == SDL_PIXELFORMAT_UNKNOWN) {
             SDL_DFB_ERR("Unknown dfb pixelformat %x !\n", dlc.pixelformat);
             goto error;
         }
@@ -363,7 +352,6 @@
     SDL_DisplayMode mode;
     struct modes_callback_t data;
     int i;
-    int ret;
 
     data.nummodes = 0;
     /* Enumerate the available fullscreen modes */
@@ -403,7 +391,6 @@
     DFB_DisplayData *data = (DFB_DisplayData *) display->driverdata;
     DFBDisplayLayerConfig config, rconfig;
     DFBDisplayLayerConfigFlags fail = 0;
-    DFBResult ret;
 
     SDL_DFB_CHECKERR(data->layer->SetCooperativeLevel(data->layer,
                                                       DLSCL_ADMINISTRATIVE));
@@ -469,7 +456,6 @@
 {
     //DFB_DeviceData *devdata = (DFB_DeviceData *) _this->driverdata;
     SDL_DisplayMode tmode;
-    DFBResult ret;
     int i;
 
     for (i = 0; i < _this->num_displays; ++i) {
--- a/src/video/directfb/SDL_DirectFB_mouse.c	Sat Aug 14 12:28:43 2010 -0700
+++ b/src/video/directfb/SDL_DirectFB_mouse.c	Mon Aug 16 09:04:55 2010 -0700
@@ -253,7 +253,7 @@
 void
 DirectFB_QuitMouse(_THIS)
 {
-    SDL_DFB_DEVICEDATA(_this);
+    //SDL_DFB_DEVICEDATA(_this);
 }
 
 
--- a/src/video/directfb/SDL_DirectFB_opengl.c	Sat Aug 14 12:28:43 2010 -0700
+++ b/src/video/directfb/SDL_DirectFB_opengl.c	Mon Aug 16 09:04:55 2010 -0700
@@ -30,6 +30,10 @@
     int gl_active;              /* to stop switching drivers while we have a valid context */
     int initialized;
     DirectFB_GLContext *firstgl;        /* linked list */
+    
+    /* OpenGL */
+    void (*glFinish) (void);
+    void (*glFlush) (void);
 };
 
 #define OPENGL_REQUIRS_DLOPEN
@@ -94,7 +98,7 @@
 int
 DirectFB_GL_LoadLibrary(_THIS, const char *path)
 {
-    SDL_DFB_DEVICEDATA(_this);
+    //SDL_DFB_DEVICEDATA(_this);
 
     void *handle = NULL;
 
@@ -122,9 +126,6 @@
 
     SDL_DFB_DEBUG("Loaded library: %s\n", path);
 
-    /* Unload the old driver and reset the pointers */
-    DirectFB_GL_UnloadLibrary(_this);
-
     _this->gl_config.dll_handle = handle;
     _this->gl_config.driver_loaded = 1;
     if (path) {
@@ -134,8 +135,8 @@
         *_this->gl_config.driver_path = '\0';
     }
 
-    devdata->glFinish = DirectFB_GL_GetProcAddress(_this, "glFinish");
-    devdata->glFlush = DirectFB_GL_GetProcAddress(_this, "glFlush");
+    _this->gl_data->glFinish = DirectFB_GL_GetProcAddress(_this, "glFinish");
+    _this->gl_data->glFlush = DirectFB_GL_GetProcAddress(_this, "glFlush");
 
     return 0;
 }
@@ -143,6 +144,7 @@
 static void
 DirectFB_GL_UnloadLibrary(_THIS)
 {
+ #if 0
     int ret;
 
     if (_this->gl_config.driver_loaded) {
@@ -153,6 +155,10 @@
         _this->gl_config.dll_handle = NULL;
         _this->gl_config.driver_loaded = 0;
     }
+#endif
+    /* Free OpenGL memory */
+    SDL_free(_this->gl_data);
+    _this->gl_data = NULL;
 }
 
 void *
@@ -167,11 +173,11 @@
 SDL_GLContext
 DirectFB_GL_CreateContext(_THIS, SDL_Window * window)
 {
+    //SDL_DFB_DEVICEDATA(_this);
     SDL_DFB_WINDOWDATA(window);
     DirectFB_GLContext *context;
-    int ret;
 
-    SDL_DFB_CALLOC(context, 1, sizeof(*context));
+    SDL_DFB_CALLOC(context, 1, sizeof(DirectFB_GLContext));
 
     SDL_DFB_CHECKERR(windata->surface->GetGL(windata->surface,
                                              &context->context));
@@ -179,11 +185,14 @@
     if (!context->context)
         return NULL;
 
-    SDL_DFB_CHECKERR(context->context->Unlock(context->context));
-
+    context->is_locked = 0;
+    context->sdl_window = window;
+    
     context->next = _this->gl_data->firstgl;
     _this->gl_data->firstgl = context;
 
+    SDL_DFB_CHECK(context->context->Unlock(context->context));
+
     if (DirectFB_GL_MakeCurrent(_this, window, context) < 0) {
         DirectFB_GL_DeleteContext(_this, context);
         return NULL;
@@ -198,28 +207,24 @@
 int
 DirectFB_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
 {
-    SDL_DFB_WINDOWDATA(window);
+    //SDL_DFB_WINDOWDATA(window);
     DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
     DirectFB_GLContext *p;
 
-    int ret;
-
     for (p = _this->gl_data->firstgl; p; p = p->next)
-        p->context->Unlock(p->context);
-
-    if (windata) {
-        windata->gl_context = NULL;
-        /* Everything is unlocked, check for a resize */
-        DirectFB_AdjustWindowSurface(window);
+    {
+       if (p->is_locked) {
+         SDL_DFB_CHECKERR(p->context->Unlock(p->context));
+         p->is_locked = 0;
+       }
+        
     }
 
     if (ctx != NULL) {
         SDL_DFB_CHECKERR(ctx->context->Lock(ctx->context));
+        ctx->is_locked = 1;
     }
 
-    if (windata)
-        windata->gl_context = ctx;
-
     return 0;
   error:
     return -1;
@@ -242,28 +247,36 @@
 void
 DirectFB_GL_SwapWindow(_THIS, SDL_Window * window)
 {
-    SDL_DFB_DEVICEDATA(_this);
+    //SDL_DFB_DEVICEDATA(_this);
     SDL_DFB_WINDOWDATA(window);
-    int ret;
     DFBRegion region;
+    DirectFB_GLContext *p;
 
     region.x1 = 0;
     region.y1 = 0;
     region.x2 = window->w;
     region.y2 = window->h;
 
+#if 0
     if (devdata->glFinish)
         devdata->glFinish();
     else if (devdata->glFlush)
         devdata->glFlush();
+#endif
 
-    if (1 || windata->gl_context) {
-        /* SDL_DFB_CHECKERR(windata->gl_context->context->Unlock(windata->gl_context->context)); */
-        SDL_DFB_CHECKERR(windata->surface->Flip(windata->surface, &region,
-                                                DSFLIP_ONSYNC));
-        /* SDL_DFB_CHECKERR(windata->gl_context->context->Lock(windata->gl_context->context)); */
+  	for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
+        if (p->sdl_window == window && p->is_locked)
+        {
+            SDL_DFB_CHECKERR(p->context->Unlock(p->context));
+            p->is_locked = 0;
+        }            
 
-    }
+    SDL_DFB_CHECKERR(windata->window_surface->Flip(windata->window_surface,NULL,  DSFLIP_PIPELINE |DSFLIP_BLIT | DSFLIP_ONSYNC ));
+
+    //if (windata->gl_context) {
+        //SDL_DFB_CHECKERR(windata->surface->Flip(windata->surface,NULL, DSFLIP_ONSYNC)); 
+        //SDL_DFB_CHECKERR(windata->gl_context->context->Lock(windata->gl_context->context));
+    //}
 
     return;
   error:
@@ -276,19 +289,58 @@
     DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
     DirectFB_GLContext *p;
 
-    ctx->context->Unlock(ctx->context);
-    ctx->context->Release(ctx->context);
+    if (ctx->is_locked)
+        SDL_DFB_CHECK(ctx->context->Unlock(ctx->context));
+    SDL_DFB_RELEASE(ctx->context);
 
-    p = _this->gl_data->firstgl;
-    while (p && p->next != ctx)
-        p = p->next;
+    for (p = _this->gl_data->firstgl; p && p->next != ctx; p = p->next)
+        ;
     if (p)
         p->next = ctx->next;
     else
         _this->gl_data->firstgl = ctx->next;
 
     SDL_DFB_FREE(ctx);
+}
 
+void
+DirectFB_GL_FreeWindowContexts(_THIS, SDL_Window * window)
+{
+    DirectFB_GLContext *p;
+    
+	for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
+	    if (p->sdl_window == window)
+	    {
+	    	if (p->is_locked)
+	        	SDL_DFB_CHECK(p->context->Unlock(p->context));
+	        SDL_DFB_RELEASE(p->context);
+	    }
+}
+
+void
+DirectFB_GL_ReAllocWindowContexts(_THIS, SDL_Window * window)
+{
+    DirectFB_GLContext *p;
+
+	for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
+	    if (p->sdl_window == window)
+	    {
+            SDL_DFB_WINDOWDATA(window);
+			SDL_DFB_CHECK(windata->surface->GetGL(windata->surface,
+	                                         &p->context));
+    		if (p->is_locked)
+            	SDL_DFB_CHECK(p->context->Lock(p->context));
+	        }
+}
+
+void
+DirectFB_GL_DestroyWindowContexts(_THIS, SDL_Window * window)
+{
+    DirectFB_GLContext *p;
+
+	for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
+		if (p->sdl_window == window)
+			DirectFB_GL_DeleteContext(_this, p);
 }
 
 #endif
--- a/src/video/directfb/SDL_DirectFB_opengl.h	Sat Aug 14 12:28:43 2010 -0700
+++ b/src/video/directfb/SDL_DirectFB_opengl.h	Mon Aug 16 09:04:55 2010 -0700
@@ -32,6 +32,9 @@
 {
     IDirectFBGL *context;
     DirectFB_GLContext *next;
+    
+    SDL_Window *sdl_window;
+    int is_locked;
 };
 
 /* OpenGL functions */
@@ -48,6 +51,10 @@
 extern void DirectFB_GL_SwapWindow(_THIS, SDL_Window * window);
 extern void DirectFB_GL_DeleteContext(_THIS, SDL_GLContext context);
 
+extern void DirectFB_GL_FreeWindowContexts(_THIS, SDL_Window * window);
+extern void DirectFB_GL_ReAllocWindowContexts(_THIS, SDL_Window * window);
+extern void DirectFB_GL_DestroyWindowContexts(_THIS, SDL_Window * window);
+
 #endif /* SDL_DIRECTFB_OPENGL */
 
 #endif /* _SDL_directfb_opengl_h */
--- a/src/video/directfb/SDL_DirectFB_render.c	Sat Aug 14 12:28:43 2010 -0700
+++ b/src/video/directfb/SDL_DirectFB_render.c	Mon Aug 16 09:04:55 2010 -0700
@@ -72,6 +72,7 @@
 static void DirectFB_DirtyTexture(SDL_Renderer * renderer,
                                   SDL_Texture * texture, int numrects,
                                   const SDL_Rect * rects);
+static int DirectFB_SetDrawBlendMode(SDL_Renderer * renderer);
 static int DirectFB_RenderDrawPoints(SDL_Renderer * renderer,
                                 const SDL_Point * points, int count);
 static int DirectFB_RenderDrawLines(SDL_Renderer * renderer,
@@ -194,20 +195,20 @@
                                            /**< No blending */
             data->blitFlags = DSBLIT_NOFX;
             data->drawFlags = DSDRAW_NOFX;
-            destsurf->SetSrcBlendFunction(destsurf, DSBF_ONE);
-            destsurf->SetDstBlendFunction(destsurf, DSBF_ZERO);
+            SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_ONE));
+            SDL_DFB_CHECK(destsurf->SetDstBlendFunction(destsurf, DSBF_ZERO));
             break;
         case SDL_BLENDMODE_MASK:
             data->blitFlags = DSBLIT_BLEND_ALPHACHANNEL;
             data->drawFlags = DSDRAW_BLEND;
-            destsurf->SetSrcBlendFunction(destsurf, DSBF_SRCALPHA);
-            destsurf->SetDstBlendFunction(destsurf, DSBF_INVSRCALPHA);
+            SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_SRCALPHA));
+            SDL_DFB_CHECK(destsurf->SetDstBlendFunction(destsurf, DSBF_INVSRCALPHA));
             break;
         case SDL_BLENDMODE_BLEND:
             data->blitFlags = DSBLIT_BLEND_ALPHACHANNEL;
             data->drawFlags = DSDRAW_BLEND;
-            destsurf->SetSrcBlendFunction(destsurf, DSBF_SRCALPHA);
-            destsurf->SetDstBlendFunction(destsurf, DSBF_INVSRCALPHA);
+            SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_SRCALPHA));
+            SDL_DFB_CHECK(destsurf->SetDstBlendFunction(destsurf, DSBF_INVSRCALPHA));
             break;
         case SDL_BLENDMODE_ADD:
             data->blitFlags = DSBLIT_BLEND_ALPHACHANNEL;
@@ -216,16 +217,16 @@
             // It will be cheaper to copy the surface to
             // a temporay surface and premultiply 
             if (source && TextureHasAlpha(source))
-                destsurf->SetSrcBlendFunction(destsurf, DSBF_SRCALPHA);
+                SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_SRCALPHA));
             else
-                destsurf->SetSrcBlendFunction(destsurf, DSBF_ONE);
-            destsurf->SetDstBlendFunction(destsurf, DSBF_ONE);
+                SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_ONE));
+            SDL_DFB_CHECK(destsurf->SetDstBlendFunction(destsurf, DSBF_ONE));
             break;
         case SDL_BLENDMODE_MOD:
             data->blitFlags = DSBLIT_BLEND_ALPHACHANNEL;
             data->drawFlags = DSDRAW_BLEND;
-            destsurf->SetSrcBlendFunction(destsurf, DSBF_DESTCOLOR);
-            destsurf->SetDstBlendFunction(destsurf, DSBF_ZERO);
+            SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_DESTCOLOR));
+            SDL_DFB_CHECK(destsurf->SetDstBlendFunction(destsurf, DSBF_ZERO));
             break;
         }
         data->lastBlendMode = blendMode;
@@ -250,7 +251,6 @@
     SDL_DFB_WINDOWSURFACE(data->window);
     IDirectFBPalette *surfpal;
 
-    int ret;
     int i;
     int ncolors;
     DFBColor entries[256];
@@ -283,7 +283,6 @@
     SDL_VideoDisplay *display = window->display;
     SDL_Renderer *renderer = NULL;
     DirectFB_RenderData *data = NULL;
-    DFBResult ret;
     DFBSurfaceCapabilities scaps;
     char *p;
 
@@ -306,10 +305,16 @@
     renderer->DirtyTexture = DirectFB_DirtyTexture;
     renderer->RenderDrawPoints = DirectFB_RenderDrawPoints;
     renderer->RenderDrawLines = DirectFB_RenderDrawLines;
+    /* SetDrawColor - no needed */
+    renderer->SetDrawBlendMode = DirectFB_SetDrawBlendMode;
     renderer->RenderFillRects = DirectFB_RenderFillRects;
     renderer->RenderDrawRects = DirectFB_RenderDrawRects;
+    /* RenderDrawEllipse - no reference implementation yet */
+    /* RenderFillEllipse - no reference implementation yet */
     renderer->RenderCopy = DirectFB_RenderCopy;
     renderer->RenderPresent = DirectFB_RenderPresent;
+    /* RenderReadPixels is difficult to implement */
+    /* RenderWritePixels is difficult to implement */
     renderer->DestroyTexture = DirectFB_DestroyTexture;
     renderer->DestroyRenderer = DirectFB_DestroyRenderer;
     renderer->info = DirectFB_RenderDriver.info;
@@ -324,7 +329,7 @@
     data->flipflags = DSFLIP_PIPELINE | DSFLIP_BLIT;
 
     if (flags & SDL_RENDERER_PRESENTVSYNC) {
-        data->flipflags |= DSFLIP_WAITFORSYNC;
+        data->flipflags |= DSFLIP_WAITFORSYNC | DSFLIP_ONSYNC;
         renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
     } else
         data->flipflags |= DSFLIP_ONSYNC;
@@ -396,11 +401,9 @@
         return DSPF_UNKNOWN;
     case SDL_PIXELFORMAT_INDEX4MSB:
         return DSPF_UNKNOWN;
+#if (DFB_VERSION_ATLEAST(1,2,0))
     case SDL_PIXELFORMAT_RGB444:
-#if (DFB_VERSION_ATLEAST(1,2,0))
         return DSPF_RGB444;
-#else
-        return DSPF_UNKNOWN;
 #endif
     case SDL_PIXELFORMAT_BGR24:
         return DSPF_UNKNOWN;
@@ -427,7 +430,7 @@
     SDL_DFB_WINDOWDATA(window);
 
     if (renddata->size_changed || windata->wm_needs_redraw) {
-        DirectFB_AdjustWindowSurface(window);
+//        DirectFB_AdjustWindowSurface(window);
     }
     return 0;
 }
@@ -451,7 +454,7 @@
     DFB_DisplayData *dispdata = (DFB_DisplayData *) display->driverdata;
     DirectFB_TextureData *data = texture->driverdata;
     DFBDisplayLayerConfig layconf;
-    int ret;
+    DFBResult ret;
 
     if (renddata->isyuvdirect && (dispdata->vidID >= 0)
         && (!dispdata->vidIDinuse)
@@ -472,7 +475,7 @@
                                                        DLSCL_EXCLUSIVE));
 
         if (devdata->use_yuv_underlays) {
-            ret = dispdata->vidlayer->SetLevel(dispdata->vidlayer, -1);
+            ret = SDL_DFB_CHECK(dispdata->vidlayer->SetLevel(dispdata->vidlayer, -1));
             if (ret != DFB_OK)
                 SDL_DFB_DEBUG("Underlay Setlevel not supported\n");
         }
@@ -505,7 +508,6 @@
     SDL_VideoDisplay *display = window->display;
     SDL_DFB_DEVICEDATA(display->device);
     DirectFB_TextureData *data;
-    DFBResult ret;
     DFBSurfaceDescription dsc;
     DFBSurfacePixelFormat pixelformat;
 
@@ -600,8 +602,7 @@
                            int ncolors)
 {
     DirectFB_TextureData *data = (DirectFB_TextureData *) texture->driverdata;
-    DFBResult ret;
-
+ 
     if (SDL_ISPIXELFORMAT_INDEXED(data->format)
         && !SDL_ISPIXELFORMAT_FOURCC(data->format)) {
         DFBColor entries[256];
@@ -631,7 +632,6 @@
                            int firstcolor, int ncolors)
 {
     DirectFB_TextureData *data = (DirectFB_TextureData *) texture->driverdata;
-    DFBResult ret;
 
     if (SDL_ISPIXELFORMAT_INDEXED(data->format)
         && !SDL_ISPIXELFORMAT_FOURCC(data->format)) {
@@ -687,6 +687,23 @@
 }
 
 static int
+DirectFB_SetDrawBlendMode(SDL_Renderer * renderer)
+{
+    switch (renderer->blendMode) {
+    case SDL_BLENDMODE_NONE:
+    case SDL_BLENDMODE_MASK:
+    case SDL_BLENDMODE_BLEND:
+    case SDL_BLENDMODE_ADD:
+    case SDL_BLENDMODE_MOD:
+        return 0;
+    default:
+        SDL_Unsupported();
+        renderer->blendMode = SDL_BLENDMODE_NONE;
+        return -1;
+    }
+}
+
+static int
 DirectFB_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
 {
 #if (DFB_VERSION_ATLEAST(1,2,0))
@@ -720,7 +737,6 @@
                        const SDL_Rect * rect, const void *pixels, int pitch)
 {
     DirectFB_TextureData *data = (DirectFB_TextureData *) texture->driverdata;
-    DFBResult ret;
     Uint8 *dpixels;
     int dpitch;
     Uint8 *src, *dst;
@@ -772,7 +788,6 @@
 {
     DirectFB_TextureData *texturedata =
         (DirectFB_TextureData *) texture->driverdata;
-    DFBResult ret;
 
     if (markDirty) {
         SDL_AddDirtyRect(&texturedata->dirty, rect);
@@ -807,7 +822,7 @@
         (DirectFB_TextureData *) texture->driverdata;
 
     if (texturedata->display) {
-        texturedata->surface->Unlock(texturedata->surface);
+        SDL_DFB_CHECK(texturedata->surface->Unlock(texturedata->surface));
         texturedata->pixels = NULL;
     }
 }
@@ -830,7 +845,6 @@
     DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
     SDL_DFB_WINDOWSURFACE(data->window);
 
-    DFBResult ret;
     Uint8 r, g, b, a;
 
     r = renderer->r;
@@ -866,7 +880,6 @@
 {
     DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
     SDL_DFB_WINDOWSURFACE(data->window);
-    DFBResult ret;
     int i;
 
     PrepareDraw(renderer);
@@ -882,7 +895,6 @@
 {
     DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
     SDL_DFB_WINDOWSURFACE(data->window);
-    DFBResult ret;
     int i;
 
     PrepareDraw(renderer);
@@ -904,7 +916,6 @@
 {
     DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
     SDL_DFB_WINDOWSURFACE(data->window);
-    DFBResult ret;
     int i;
 
     PrepareDraw(renderer);
@@ -923,7 +934,6 @@
 {
     DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
     SDL_DFB_WINDOWSURFACE(data->window);
-    DFBResult ret;
     int i;
 
     PrepareDraw(renderer);
@@ -946,7 +956,6 @@
     DirectFB_TextureData *texturedata =
         (DirectFB_TextureData *) texture->driverdata;
     Uint8 alpha = 0xFF;
-    DFBResult ret;
 
     if (texturedata->display) {
         int px, py;
@@ -960,7 +969,7 @@
                                                       srcrect->x, srcrect->y,
                                                       srcrect->w,
                                                       srcrect->h));
-        windata->window->GetPosition(windata->window, &px, &py);
+        SDL_DFB_CHECK(windata->window->GetPosition(windata->window, &px, &py));
         px += windata->client.x;
         py += windata->client.y;
         SDL_DFB_CHECKERR(dispdata->
@@ -1052,7 +1061,6 @@
     SDL_DFB_WINDOWDATA(window);
 
     DFBRectangle sr;
-    DFBResult ret;
 
     sr.x = 0;
     sr.y = 0;
@@ -1078,8 +1086,9 @@
         DFB_DisplayData *dispdata =
             (DFB_DisplayData *) data->display->driverdata;
         dispdata->vidIDinuse = 0;
-        dispdata->vidlayer->SetCooperativeLevel(dispdata->vidlayer,
-                                                DLSCL_ADMINISTRATIVE);
+        /* FIXME: Shouldn't we reset the cooperative level */
+        SDL_DFB_CHECK(dispdata->vidlayer->SetCooperativeLevel(dispdata->vidlayer,
+                                                DLSCL_ADMINISTRATIVE));
         SDL_DFB_RELEASE(dispdata->vidlayer);
     }
     SDL_FreeDirtyRects(&data->dirty);
--- a/src/video/directfb/SDL_DirectFB_video.c	Sat Aug 14 12:28:43 2010 -0700
+++ b/src/video/directfb/SDL_DirectFB_video.c	Mon Aug 16 09:04:55 2010 -0700
@@ -209,14 +209,11 @@
             DirectFBSetOption("disable-module", "x11input");
     }
 
-#if USE_MULTI_API
-	devdata->use_linux_input = 1;       /* default: on */
+	/* FIXME: Reenable as default once multi kbd/mouse interface is sorted out */
+	devdata->use_linux_input = 0;       /* default: on */
     stemp = SDL_getenv(DFBENV_USE_LINUX_INPUT);
     if (stemp)
         devdata->use_linux_input = atoi(stemp);
-#else
-	devdata->use_linux_input = 0;       /* no way to support this ... */
-#endif
 
     if (!devdata->use_linux_input)
         DirectFBSetOption("disable-module", "linux_input");
@@ -253,6 +250,7 @@
 
     devdata->dfb = dfb;
     devdata->firstwin = NULL;
+    devdata->grabbed_window = NULL;
 
     _this->driverdata = devdata;
 
--- a/src/video/directfb/SDL_DirectFB_video.h	Sat Aug 14 12:28:43 2010 -0700
+++ b/src/video/directfb/SDL_DirectFB_video.h	Mon Aug 16 09:04:55 2010 -0700
@@ -78,49 +78,38 @@
 #define DFBENV_USE_LINUX_INPUT		"SDL_DIRECTFB_LINUX_INPUT"      /* Default: on  */
 #define DFBENV_USE_WM				"SDL_DIRECTFB_WM"       /* Default: off  */
 
-#define SDL_DFB_RELEASE(x) do { if ( (x) != NULL ) { x->Release(x); x = NULL; } } while (0)
+#define SDL_DFB_RELEASE(x) do { if ( (x) != NULL ) { SDL_DFB_CHECK(x->Release(x)); x = NULL; } } while (0)
 #define SDL_DFB_FREE(x) do { if ( (x) != NULL ) { SDL_free(x); x = NULL; } } while (0)
 #define SDL_DFB_UNLOCK(x) do { if ( (x) != NULL ) { x->Unlock(x); } } while (0)
 
 #if DEBUG
-#define SDL_DFB_DEBUG(x...) do { fprintf(LOG_CHANNEL, "%s:", __FUNCTION__); fprintf(LOG_CHANNEL, x); } while (0)
-#define SDL_DFB_DEBUGC(x...) do { fprintf(LOG_CHANNEL, x); } while (0)
-#else
-#define SDL_DFB_DEBUG(x...) do { } while (0)
-#define SDL_DFB_DEBUGC(x...) do { } while (0)
+/* FIXME: do something with DEBUG */
 #endif
 
 #define SDL_DFB_CONTEXT "SDL_DirectFB"
 
-#define SDL_DFB_ERR(x...) 							\
+static inline DFBResult sdl_dfb_check(DFBResult ret, const char *src_file, int src_line, const char *src_code) {
+	if (ret != DFB_OK) {
+		fprintf(LOG_CHANNEL, "%s <%d>:\n\t", src_file, src_line );
+		fprintf(LOG_CHANNEL, "\t%s\n", src_code );
+		fprintf(LOG_CHANNEL, "\t%s\n", DirectFBErrorString (ret) );
+		SDL_SetError( src_code, DirectFBErrorString (ret) );
+	}
+	return ret;
+}
+
+#define SDL_DFB_CHECK(x...) sdl_dfb_check( x, __FILE__, __LINE__, #x )
+
+#define SDL_DFB_CHECKERR(x...) if ( sdl_dfb_check( x, __FILE__, __LINE__, #x ) != DFB_OK ) goto error
+
+#define SDL_DFB_DEBUG(x...) 							\
 	do {											\
 		fprintf(LOG_CHANNEL, "%s: %s <%d>:\n\t",			\
 			SDL_DFB_CONTEXT, __FILE__, __LINE__ );	\
-		fprintf(LOG_CHANNEL, x );						\
+        fprintf(LOG_CHANNEL, x ); \
 	} while (0)
 
-#define SDL_DFB_CHECK(x...) \
-     do {                                                                \
-          ret = x;                                                    \
-          if (ret != DFB_OK) {                                        \
-               fprintf(LOG_CHANNEL, "%s <%d>:\n\t", __FILE__, __LINE__ ); 	      \
-               fprintf(LOG_CHANNEL, "\t%s\n", #x ); \
-               fprintf(LOG_CHANNEL, "\t%s\n", DirectFBErrorString (ret) ); \
-               SDL_SetError( #x, DirectFBErrorString (ret) );         \
-          }                                                           \
-     } while (0)
-
-#define SDL_DFB_CHECKERR(x...) \
-     do {                                                                \
-          ret = x;                                                    \
-          if (ret != DFB_OK) {                                        \
-               fprintf(LOG_CHANNEL, "%s <%d>:\n", __FILE__, __LINE__ ); \
-               fprintf(LOG_CHANNEL, "\t%s\n", #x ); \
-               fprintf(LOG_CHANNEL, "\t%s\n", DirectFBErrorString (ret) ); \
-               SDL_SetError( #x, DirectFBErrorString (ret) );         \
-               goto error; 					      \
-          }                                                           \
-     } while (0)
+#define SDL_DFB_ERR(x...) SDL_DFB_DEBUG( x )
 
 #define SDL_DFB_CALLOC(r, n, s) \
      do {                                                                \
@@ -158,9 +147,8 @@
     int use_linux_input;
     int has_own_wm;
 
-    /* OpenGL */
-    void (*glFinish) (void);
-    void (*glFlush) (void);
+	/* window grab */
+	SDL_Window *grabbed_window;
 
     /* global events */
     IDirectFBEventBuffer *events;
--- a/src/video/directfb/SDL_DirectFB_window.c	Sat Aug 14 12:28:43 2010 -0700
+++ b/src/video/directfb/SDL_DirectFB_window.c	Mon Aug 16 09:04:55 2010 -0700
@@ -24,9 +24,14 @@
 #include "SDL_syswm.h"
 #include "../SDL_sysvideo.h"
 #include "../../events/SDL_keyboard_c.h"
+#include "../../video/SDL_pixels_c.h"
 
 #include "SDL_DirectFB_video.h"
+#if SDL_DIRECTFB_OPENGL
+#include "SDL_DirectFB_opengl.h"
+#endif
 
+static void DirectFB_AdjustWindowSurface(_THIS, SDL_Window * window);
 
 int
 DirectFB_CreateWindow(_THIS, SDL_Window * window)
@@ -36,8 +41,7 @@
     DFB_WindowData *windata = NULL;
     DFBWindowOptions wopts;
     DFBWindowDescription desc;
-    IDirectFBFont *font;
-    int ret, x, y;
+    int x, y;
 
     SDL_DFB_CALLOC(window->driverdata, 1, sizeof(DFB_WindowData));
     windata = (DFB_WindowData *) window->driverdata;
@@ -69,7 +73,7 @@
         y = 0;
     }
 
-    DirectFB_WM_AdjustWindowLayout(window);
+    DirectFB_WM_AdjustWindowLayout(window, window->flags, window->w, window->h);
 
     /* Create Window */
     desc.flags =
@@ -87,7 +91,7 @@
                                                    &windata->window));
 
     /* Set Options */
-    windata->window->GetOptions(windata->window, &wopts);
+    SDL_DFB_CHECK(windata->window->GetOptions(windata->window, &wopts));
 
     if (window->flags & SDL_WINDOW_RESIZABLE)
         wopts |= DWOP_SCALE;
@@ -96,9 +100,9 @@
 
     if (window->flags & SDL_WINDOW_FULLSCREEN) {
         wopts |= DWOP_KEEP_POSITION | DWOP_KEEP_STACKING | DWOP_KEEP_SIZE;
-        windata->window->SetStackingClass(windata->window, DWSC_UPPER);
+        SDL_DFB_CHECK(windata->window->SetStackingClass(windata->window, DWSC_UPPER));
     }
-    windata->window->SetOptions(windata->window, wopts);
+    SDL_DFB_CHECK(windata->window->SetOptions(windata->window, wopts));
 
     /* See what we got */
     SDL_DFB_CHECKERR(DirectFB_WM_GetClientSize
@@ -112,7 +116,7 @@
                      GetSubSurface(windata->window_surface, &windata->client,
                                    &windata->surface));
 
-    windata->window->SetOpacity(windata->window, 0xFF);
+    SDL_DFB_CHECK(windata->window->SetOpacity(windata->window, 0xFF));
 
     /* Create Eventbuffer */
     SDL_DFB_CHECKERR(windata->window->CreateEventBuffer(windata->window,
@@ -123,21 +127,10 @@
 
     /* Create a font */
     /* FIXME: once during Video_Init */
-    if (windata->is_managed) {
-        DFBFontDescription fdesc;
-
-        fdesc.flags = DFDESC_HEIGHT;
-        fdesc.height = windata->theme.font_size;
-        font = NULL;
-        SDL_DFB_CHECK(devdata->
-                      dfb->CreateFont(devdata->dfb, windata->theme.font,
-                                      &fdesc, &font));
-        windata->window_surface->SetFont(windata->window_surface, font);
-        SDL_DFB_RELEASE(font);
-    }
+    windata->font = NULL;
 
     /* Make it the top most window. */
-    windata->window->RaiseToTop(windata->window);
+    SDL_DFB_CHECK(windata->window->RaiseToTop(windata->window));
 
     /* remember parent */
     windata->sdl_window = window;
@@ -149,7 +142,7 @@
     devdata->firstwin = windata;
 
     /* Draw Frame */
-    DirectFB_WM_RedrawLayout(window);
+    DirectFB_WM_RedrawLayout(_this, window);
 
     return 0;
   error:
@@ -172,6 +165,7 @@
 
     if (windata->is_managed) {
         windata->wm_needs_redraw = 1;
+        DirectFB_WM_RedrawLayout(_this, window);
     } else
         SDL_Unsupported();
 }
@@ -182,7 +176,6 @@
     SDL_DFB_DEVICEDATA(_this);
     SDL_DFB_WINDOWDATA(window);
     SDL_Surface *surface = NULL;
-    DFBResult ret;
 
     if (icon) {
         SDL_PixelFormat format;
@@ -216,7 +209,7 @@
             memcpy((char *) dest + i * pitch,
                    (char *) p + i * surface->pitch, 4 * surface->w);
 
-        windata->icon->Unlock(windata->icon);
+        SDL_DFB_CHECK(windata->icon->Unlock(windata->icon));
         SDL_FreeSurface(surface);
     } else {
         SDL_DFB_RELEASE(windata->icon);
@@ -249,16 +242,15 @@
         x = 0;
         y = 0;
     }
-    DirectFB_WM_AdjustWindowLayout(window);
-    windata->window->MoveTo(windata->window, x, y);
+    DirectFB_WM_AdjustWindowLayout(window, window->flags, window->w, window->h);
+    SDL_DFB_CHECK(windata->window->MoveTo(windata->window, x, y));
 }
 
 void
 DirectFB_SetWindowSize(_THIS, SDL_Window * window)
 {
-    SDL_DFB_DEVICEDATA(_this);
+    //SDL_DFB_DEVICEDATA(_this);
     SDL_DFB_WINDOWDATA(window);
-    int ret;
 
     if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
         int cw;
@@ -272,23 +264,23 @@
 
         if (cw != window->w || ch != window->h) {
 
-            DirectFB_WM_AdjustWindowLayout(window);
+		    DirectFB_WM_AdjustWindowLayout(window, window->flags, window->w, window->h);
             SDL_DFB_CHECKERR(windata->window->Resize(windata->window,
                                                      windata->size.w,
                                                      windata->size.h));
         }
 
+        SDL_DFB_CHECKERR(DirectFB_WM_GetClientSize
+                     (_this, window, &window->w, &window->h));
+        DirectFB_AdjustWindowSurface(_this, window);
+
         SDL_DFB_CHECKERR(windata->window->EnableEvents(windata->window,
                                                        DWET_ALL));
 
-        SDL_DFB_CHECKERR(DirectFB_WM_GetClientSize
-                         (_this, window, &window->w, &window->h));
-
-        SDL_OnWindowResized(window);
     }
     return;
   error:
-    windata->window->EnableEvents(windata->window, DWET_ALL);
+    SDL_DFB_CHECK(windata->window->EnableEvents(windata->window, DWET_ALL));
     return;
 }
 
@@ -297,7 +289,7 @@
 {
     SDL_DFB_WINDOWDATA(window);
 
-    windata->window->SetOpacity(windata->window, windata->opacity);
+    SDL_DFB_CHECK(windata->window->SetOpacity(windata->window, windata->opacity));
 
 }
 
@@ -306,8 +298,8 @@
 {
     SDL_DFB_WINDOWDATA(window);
 
-    windata->window->GetOpacity(windata->window, &windata->opacity);
-    windata->window->SetOpacity(windata->window, 0);
+    SDL_DFB_CHECK(windata->window->GetOpacity(windata->window, &windata->opacity));
+    SDL_DFB_CHECK(windata->window->SetOpacity(windata->window, 0));
 }
 
 void
@@ -315,8 +307,8 @@
 {
     SDL_DFB_WINDOWDATA(window);
 
-    windata->window->RaiseToTop(windata->window);
-    windata->window->RequestFocus(windata->window);
+    SDL_DFB_CHECK(windata->window->RaiseToTop(windata->window));
+    SDL_DFB_CHECK(windata->window->RequestFocus(windata->window));
 }
 
 void
@@ -352,14 +344,23 @@
 void
 DirectFB_SetWindowGrab(_THIS, SDL_Window * window)
 {
+    SDL_DFB_DEVICEDATA(_this);
     SDL_DFB_WINDOWDATA(window);
+    DFB_WindowData *gwindata = ((devdata->grabbed_window) ? (DFB_WindowData *) ((devdata->grabbed_window)->driverdata) : NULL);
 
     if ((window->flags & SDL_WINDOW_INPUT_GRABBED)) {
-        windata->window->GrabPointer(windata->window);
-        windata->window->GrabKeyboard(windata->window);
+        if (gwindata != NULL)
+        {
+		    SDL_DFB_CHECK(gwindata->window->UngrabPointer(gwindata->window));
+		    SDL_DFB_CHECK(gwindata->window->UngrabKeyboard(gwindata->window));
+        }
+        SDL_DFB_CHECK(windata->window->GrabPointer(windata->window));
+        SDL_DFB_CHECK(windata->window->GrabKeyboard(windata->window));
+        devdata->grabbed_window = window;
     } else {
-        windata->window->UngrabPointer(windata->window);
-        windata->window->UngrabKeyboard(windata->window);
+        SDL_DFB_CHECK(windata->window->UngrabPointer(windata->window));
+        SDL_DFB_CHECK(windata->window->UngrabKeyboard(windata->window));
+        devdata->grabbed_window = NULL;
     }
 }
 
@@ -370,14 +371,19 @@
     SDL_DFB_WINDOWDATA(window);
     DFB_WindowData *p;
 
-    SDL_DFB_DEBUG("Trace\n");
+    /* Some cleanups */
+    SDL_DFB_CHECK(windata->window->UngrabPointer(windata->window));
+    SDL_DFB_CHECK(windata->window->UngrabKeyboard(windata->window));
 
-    /* Some cleanups */
-    windata->window->UngrabPointer(windata->window);
-    windata->window->UngrabKeyboard(windata->window);
+#if SDL_DIRECTFB_OPENGL
+	DirectFB_GL_DestroyWindowContexts(_this, window);
+#endif
 
-    windata->window_surface->SetFont(windata->window_surface, NULL);
-    SDL_DFB_RELEASE(windata->icon);
+    SDL_DFB_CHECK(windata->window_surface->SetFont(windata->window_surface, NULL));
+    SDL_DFB_CHECK(windata->surface->ReleaseSource(windata->surface));
+    SDL_DFB_CHECK(windata->window_surface->ReleaseSource(windata->window_surface));
+  	SDL_DFB_RELEASE(windata->icon);
+    SDL_DFB_RELEASE(windata->font);
     SDL_DFB_RELEASE(windata->eventbuffer);
     SDL_DFB_RELEASE(windata->surface);
     SDL_DFB_RELEASE(windata->window_surface);
@@ -405,15 +411,14 @@
     return SDL_FALSE;
 }
 
-void
-DirectFB_AdjustWindowSurface(SDL_Window * window)
+static void
+DirectFB_AdjustWindowSurface(_THIS, SDL_Window * window)
 {
     SDL_DFB_WINDOWDATA(window);
     int adjust = windata->wm_needs_redraw;
     int cw, ch;
-    int ret;
 
-    DirectFB_WM_AdjustWindowLayout(window);
+    DirectFB_WM_AdjustWindowLayout(window, window->flags, window->w, window->h);
 
     SDL_DFB_CHECKERR(windata->
                      window_surface->GetSize(windata->window_surface, &cw,
@@ -423,6 +428,10 @@
     }
 
     if (adjust) {
+#if SDL_DIRECTFB_OPENGL
+		DirectFB_GL_FreeWindowContexts(_this, window);
+#endif
+
 #if DFB_VERSION_ATLEAST(1,2,1)
         SDL_DFB_CHECKERR(windata->window->ResizeSurface(windata->window,
                                                         windata->size.w,
@@ -446,8 +455,12 @@
                          GetSubSurface(windata->window_surface,
                                        &windata->client, &windata->surface));
 #endif
-        DirectFB_WM_RedrawLayout(window);
-    }
+        DirectFB_WM_RedrawLayout(_this, window);
+        
+#if SDL_DIRECTFB_OPENGL
+		DirectFB_GL_ReAllocWindowContexts(_this, window);
+#endif
+   }
   error:
     return;
 }
--- a/src/video/directfb/SDL_DirectFB_window.h	Sat Aug 14 12:28:43 2010 -0700
+++ b/src/video/directfb/SDL_DirectFB_window.h	Mon Aug 16 09:04:55 2010 -0700
@@ -34,7 +34,6 @@
     IDirectFBSurface *surface;
     IDirectFBSurface *window_surface;   /* only used with has_own_wm */
     IDirectFBWindow *window;
-    DirectFB_GLContext *gl_context;
     IDirectFBEventBuffer *eventbuffer;
     SDL_Window *sdl_window;
     DFB_WindowData *next;
@@ -46,6 +45,7 @@
     int is_managed;
     int wm_needs_redraw;
     IDirectFBSurface *icon;
+    IDirectFBFont *font;
     DFB_Theme theme;
 };
 
@@ -69,7 +69,7 @@
 extern SDL_bool DirectFB_GetWindowWMInfo(_THIS, SDL_Window * window,
                                          struct SDL_SysWMinfo *info);
 
-extern void DirectFB_AdjustWindowSurface(SDL_Window * window);
+//extern void DirectFB_AdjustWindowSurface(_THIS, SDL_Window * window);
 
 #endif /* _SDL_directfb_window_h */