From 50011b87890b57435eeacfe718627b491619abbc Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@pulkomandy.tk>
Date: Fri, 22 Nov 2013 23:38:56 +0100
Subject: Remove Alsa and ESD detection

These will fail on Haiku, and we don't need them anyway.

diff --git a/configure.in b/configure.in
index 08c8e1e..84d9a55 100644
--- a/configure.in
+++ b/configure.in
@@ -361,7 +361,7 @@ CheckALSA()
 AC_HELP_STRING([--enable-alsa], [support the ALSA audio API [[default=yes]]]),
                   , enable_alsa=yes)
     if test x$enable_audio = xyes -a x$enable_alsa = xyes; then
-        AM_PATH_ALSA(0.9.0, have_alsa=yes, have_alsa=no)
+        have_alsa=no
         # Restore all flags from before the ALSA detection runs
         CFLAGS="$alsa_save_CFLAGS"
         LDFLAGS="$alsa_save_LDFLAGS"
@@ -460,7 +460,7 @@ CheckESD()
 AC_HELP_STRING([--enable-esd], [support the Enlightened Sound Daemon [[default=yes]]]),
                   , enable_esd=yes)
     if test x$enable_audio = xyes -a x$enable_esd = xyes; then
-        AM_PATH_ESD(0.2.8, have_esd=yes, have_esd=no)
+        have_esd=no
         if test x$have_esd = xyes; then
             AC_ARG_ENABLE(esd-shared,
 AC_HELP_STRING([--enable-esd-shared], [dynamically load ESD audio support [[default=yes]]]),
@@ -2963,3 +2963,4 @@ AC_OUTPUT([
     : >build-deps
     if test x"$MAKE" = x; then MAKE=make; fi; $MAKE depend
 ])
+AC_CONFIG_MACRO_DIR([acinclude])
-- 
2.26.0


From ccb8d617d98f5f138838327da70be061237a655f Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@pulkomandy.tk>
Date: Fri, 22 Nov 2013 23:39:26 +0100
Subject: Fix samples vs bytes issues

This was particularly noticeable in Milkytracker (SDL version) and
Hivelytracker, where all the audio timing relies on the buffer being
played during the right time.

diff --git a/src/audio/baudio/SDL_beaudio.cc b/src/audio/baudio/SDL_beaudio.cc
index de635f8..7faf571 100644
--- a/src/audio/baudio/SDL_beaudio.cc
+++ b/src/audio/baudio/SDL_beaudio.cc
@@ -199,11 +199,11 @@ int BE_OpenAudio(_THIS, SDL_AudioSpec *spec)
         return (-1);
     }
 
-    format.buffer_size = spec->samples;
-
 	/* Calculate the final parameters for this audio specification */
 	SDL_CalculateAudioSpec(spec);
 
+    format.buffer_size = spec->samples;
+
 	/* Subscribe to the audio stream (creates a new thread) */
 	{ sigset_t omask;
 		SDL_MaskSignals(&omask);
-- 
2.26.0


From f4bb5a49719622a970e91f0bf1bf499cdde3079c Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@pulkomandy.tk>
Date: Fri, 22 Nov 2013 23:40:12 +0100
Subject: Various fixes to the video code

* Do not center the window each time it is resized
* Do not reset video mode if we didn't enter fullscreen
* remove annoying debug print

diff --git a/src/video/bwindow/SDL_sysvideo.cc b/src/video/bwindow/SDL_sysvideo.cc
index c32b661..a26f18b 100644
--- a/src/video/bwindow/SDL_sysvideo.cc
+++ b/src/video/bwindow/SDL_sysvideo.cc
@@ -406,6 +406,7 @@ static int BE_SetFullScreen(_THIS, SDL_Surface *screen, int fullscreen)
 {
 	// printf("SetFullScreen(%d)\n", fullscreen);
 	BScreen bscreen;
+	static bool firstTime = true;
 
 	// SetFullSscreen() does not work as expected if called in a window
 	// that was never shown. This is probably a bug in the Haiku Game Kit that needs
@@ -425,6 +426,8 @@ static int BE_SetFullScreen(_THIS, SDL_Surface *screen, int fullscreen)
 	int width = screen->w;
 	int height = screen->h;
 	
+	bool wasFullScreen = SDL_Win->IsFullScreen();
+
 	if (fullscreen) {
 		// Set resolution to the closest available one that matches the
 		// current SDL resolution.
@@ -444,7 +447,8 @@ static int BE_SetFullScreen(_THIS, SDL_Surface *screen, int fullscreen)
 	} else {
 		// Reset to the previous known resolution as we are now in window
 		// mode.
-		bscreen.SetMode(&saved_mode);	
+		if(wasFullScreen)
+			bscreen.SetMode(&saved_mode);	
 	}
 	
 	// Effectivelly set/reset full screen mode. If we are already in
@@ -456,14 +460,6 @@ static int BE_SetFullScreen(_THIS, SDL_Surface *screen, int fullscreen)
 		// printf("Going windowed\n"); 
 	SDL_Win->SetFullScreen(fullscreen);
 	
-	// Calculate offsets for centering the window (in window mode) and for
-	// dentering the bitmap (in full screen mode).
-	BRect bounds = bscreen.Frame();
-	bounds.PrintToStream();
-	int32 cx = (bounds.IntegerWidth() - width)/2;
-	int32 cy = (bounds.IntegerHeight() - height)/2;
-	
-	// printf ("cx = %d, cy = %d\n", cx, cy);
 	if (!SDL_Win->IsFullScreen()) {
 		// printf("Doing not fullscreen stuff.\n");
 		// We are not in full screen mode, so we want to change the window
@@ -471,10 +467,18 @@ static int BE_SetFullScreen(_THIS, SDL_Surface *screen, int fullscreen)
 		SDL_Win->ResizeTo(width, height);
 		
 		// And also center the window and reset the drawing offset.
-		SDL_Win->MoveTo(cx, cy);
+		if(firstTime || wasFullScreen) {
+			SDL_Win->CenterOnScreen();
+			firstTime = false;
+		}
 		SDL_Win->SetXYOffset(0, 0);
 	} else {
-		// printf("Doing fullscreen stuff.");
+		// Calculate offsets for centering the bitmap.
+
+		BRect bounds = bscreen.Frame();
+		int cx = (bounds.Width() - width) / 2;
+		int cy = (bounds.Height() - height) / 2;
+
 		// Center the bitmap whenever we are in full screen mode.
 		SDL_Win->SetXYOffset(cx, cy);
 	}
-- 
2.26.0


From 413345646cc0d5d69b65370fa7e2f453826f0444 Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@pulkomandy.tk>
Date: Sat, 23 Nov 2013 11:59:00 +0100
Subject: Actually fix the sample vs bytes problem in audio driver.


diff --git a/src/audio/baudio/SDL_beaudio.cc b/src/audio/baudio/SDL_beaudio.cc
index 7faf571..300bf03 100644
--- a/src/audio/baudio/SDL_beaudio.cc
+++ b/src/audio/baudio/SDL_beaudio.cc
@@ -161,25 +161,30 @@ int BE_OpenAudio(_THIS, SDL_AudioSpec *spec)
     format.byte_order = B_MEDIA_LITTLE_ENDIAN;
     format.frame_rate = (float) spec->freq;
     format.channel_count = spec->channels;  /* !!! FIXME: support > 2? */
+	int samplesize = 1;
     while ((!valid_datatype) && (test_format)) {
         valid_datatype = 1;
         spec->format = test_format;
         switch (test_format) {
             case AUDIO_S8:
                 format.format = media_raw_audio_format::B_AUDIO_CHAR;
+				samplesize = 1;
                 break;
 
             case AUDIO_U8:
                 format.format = media_raw_audio_format::B_AUDIO_UCHAR;
+				samplesize = 1;
                 break;
 
             case AUDIO_S16LSB:
                 format.format = media_raw_audio_format::B_AUDIO_SHORT;
+				samplesize = 2;
                 break;
 
             case AUDIO_S16MSB:
                 format.format = media_raw_audio_format::B_AUDIO_SHORT;
                 format.byte_order = B_MEDIA_BIG_ENDIAN;
+				samplesize = 2;
                 break;
 
             default:
@@ -202,7 +207,7 @@ int BE_OpenAudio(_THIS, SDL_AudioSpec *spec)
 	/* Calculate the final parameters for this audio specification */
 	SDL_CalculateAudioSpec(spec);
 
-    format.buffer_size = spec->samples;
+    format.buffer_size = spec->samples * spec->channels * samplesize;
 
 	/* Subscribe to the audio stream (creates a new thread) */
 	{ sigset_t omask;
-- 
2.26.0


From 58bb99d3d7ffa2ff5670943b2a9288bc6e524b7a Mon Sep 17 00:00:00 2001
From: Chris Roberts <cpr420@gmail.com>
Date: Sun, 22 Dec 2013 18:37:30 -0700
Subject: Remove Haiku from specialized DECLSPEC definition


diff --git a/include/begin_code.h b/include/begin_code.h
index 27e2f7b..d5e2f1f 100644
--- a/include/begin_code.h
+++ b/include/begin_code.h
@@ -41,7 +41,7 @@
  *  Some compilers use a special export keyword
  */
 #ifndef DECLSPEC
-# if defined(__BEOS__) || defined(__HAIKU__)
+# if defined(__BEOS__)
 #  if defined(__GNUC__)
 #   define DECLSPEC
 #  else
-- 
2.26.0


From 6352a72a663b848cca09d9e8c480e789b50c9c3b Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@pulkomandy.tk>
Date: Sun, 5 Jan 2014 17:22:10 +0100
Subject: Change prefix from "be" to "os" for OpenGL includes

Accomodate the new OpenGL kit that doesn't have the "be" symlink.

diff --git a/src/video/bwindow/SDL_BWin.h b/src/video/bwindow/SDL_BWin.h
index f2b19a2..2cb94ee 100644
--- a/src/video/bwindow/SDL_BWin.h
+++ b/src/video/bwindow/SDL_BWin.h
@@ -31,7 +31,7 @@
 #include <be/game/DirectWindow.h>
 #if SDL_VIDEO_OPENGL
 #include "SDL_opengl.h"
-#include <be/opengl/GLView.h>
+#include <os/opengl/GLView.h>
 #endif
 #include <support/UTF8.h>
 
-- 
2.26.0


From 1852e933b85aef624e46092f004db05c0d089a2a Mon Sep 17 00:00:00 2001
From: Jonathan Schleifer <js@webkeks.org>
Date: Fri, 28 Mar 2014 18:04:12 +0100
Subject: Fix compilation on x86_64

Also cleans up and fixes warnings.

diff --git a/src/video/SDL_sysvideo.h b/src/video/SDL_sysvideo.h
index 436450e..b45a8a9 100644
--- a/src/video/SDL_sysvideo.h
+++ b/src/video/SDL_sysvideo.h
@@ -24,6 +24,10 @@
 #ifndef _SDL_sysvideo_h
 #define _SDL_sysvideo_h
 
+#if defined(__BEOS__) || defined(__HAIKU__)
+#include <os/kernel/image.h>
+#endif
+
 #include "SDL_mouse.h"
 #define SDL_PROTOTYPES_ONLY
 #include "SDL_syswm.h"
@@ -296,7 +300,11 @@ struct SDL_VideoDevice {
 		int swap_control;
 		int driver_loaded;
 		char driver_path[256];
+#if defined(__BEOS__) || defined(__HAIKU__)
+		image_id dll_handle;
+#else
 		void* dll_handle;
+#endif
 	} gl_config;
 
 	/* * * */
diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index 46285c9..255cafb 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -222,7 +222,7 @@ int SDL_VideoInit (const char *driver_name, Uint32 flags)
 
 	/* Set some very sane GL defaults */
 	video->gl_config.driver_loaded = 0;
-	video->gl_config.dll_handle = NULL;
+	video->gl_config.dll_handle = 0;
 	video->gl_config.red_size = 3;
 	video->gl_config.green_size = 3;
 	video->gl_config.blue_size = 2;
diff --git a/src/video/bwindow/SDL_sysvideo.cc b/src/video/bwindow/SDL_sysvideo.cc
index a26f18b..5d4077f 100644
--- a/src/video/bwindow/SDL_sysvideo.cc
+++ b/src/video/bwindow/SDL_sysvideo.cc
@@ -644,17 +644,13 @@ static void BE_NormalUpdate(_THIS, int numrects, SDL_Rect *rects)
 int BE_GL_LoadLibrary(_THIS, const char *path)
 {
 	if (path == NULL) {
-		if (_this->gl_config.dll_handle == NULL) {
+		if (_this->gl_config.dll_handle == 0) {
 			image_info info;
 			int32 cookie = 0;
 			while (get_next_image_info(0,&cookie,&info) == B_OK) {
 				void *location = NULL;
-#ifdef __HAIKU__
 				if (get_image_symbol(info.id,"glBegin",B_SYMBOL_TYPE_ANY,&location) == B_OK) { // This is how it actually works in Haiku
-#else
-				if (get_image_symbol((image_id)cookie,"glBegin",B_SYMBOL_TYPE_ANY,&location) == B_OK) { // I don't know if that *did* work in BeOS
-#endif
-					_this->gl_config.dll_handle = (void*)info.id;
+					_this->gl_config.dll_handle = info.id;
 					_this->gl_config.driver_loaded = 1;
 					SDL_strlcpy(_this->gl_config.driver_path, "libGL.so", SDL_arraysize(_this->gl_config.driver_path));
 				}
@@ -666,7 +662,7 @@ int BE_GL_LoadLibrary(_THIS, const char *path)
 			to load BGLView, which should be reloaded from new lib.
 			So for now just "load" linked libGL.so :(
 		*/
-		if (_this->gl_config.dll_handle == NULL) {
+		if (_this->gl_config.dll_handle == 0) {
 			return BE_GL_LoadLibrary(_this, NULL);
 		}
 
@@ -688,10 +684,10 @@ int BE_GL_LoadLibrary(_THIS, const char *path)
 		}*/
 	}
 
-	if (_this->gl_config.dll_handle != NULL) {
+	if (_this->gl_config.dll_handle != 0) {
 		return 0;
 	} else {
-		_this->gl_config.dll_handle = NULL;
+		_this->gl_config.dll_handle = 0;
 		_this->gl_config.driver_loaded = 0;
 		*_this->gl_config.driver_path = '\0';
 		return -1;
@@ -700,10 +696,10 @@ int BE_GL_LoadLibrary(_THIS, const char *path)
 
 void* BE_GL_GetProcAddress(_THIS, const char *proc)
 {
-	if (_this->gl_config.dll_handle != NULL) {
+	if (_this->gl_config.dll_handle != 0) {
 		void *location = NULL;
 		status_t err;
-		if ((err = get_image_symbol((image_id)_this->gl_config.dll_handle, proc, B_SYMBOL_TYPE_ANY, &location)) == B_OK) {
+		if ((err = get_image_symbol(_this->gl_config.dll_handle, proc, B_SYMBOL_TYPE_ANY, &location)) == B_OK) {
 			return location;
 		} else {
 			SDL_SetError("Couldn't find OpenGL symbol");
@@ -835,8 +831,8 @@ void BE_VideoQuit(_THIS)
 	}
 
 #if SDL_VIDEO_OPENGL
-	if (_this->gl_config.dll_handle != NULL)
-		unload_add_on((image_id)_this->gl_config.dll_handle);
+	if (_this->gl_config.dll_handle != 0)
+		unload_add_on(_this->gl_config.dll_handle);
 #endif
 
 	SDL_QuitBeApp();
diff --git a/src/video/bwindow/SDL_sysyuv.cc b/src/video/bwindow/SDL_sysyuv.cc
index 7c71b00..f2fef10 100644
--- a/src/video/bwindow/SDL_sysyuv.cc
+++ b/src/video/bwindow/SDL_sysyuv.cc
@@ -66,7 +66,8 @@ BBitmap * BE_GetOverlayBitmap(BRect bounds, color_space cs) {
 		uint32 aligned_height = r.source.height_alignment + 1;
 		if (height % aligned_height > 0) {
 			fprintf(stderr,"GetOverlayBitmap failed height alignment\n");
-			fprintf(stderr,"- height = %lu, aligned_height = %lu\n",height,aligned_height);
+			fprintf(stderr,"- height = %" B_PRIu32 ", "
+				"aligned_height = %" B_PRIu32 "\n", height, aligned_height);
 			delete bbitmap;
 			return 0;
 		}
-- 
2.26.0


From 1cf66d3efc3a60fb11ad5181b4f5d73d8a14f945 Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@gmail.com>
Date: Mon, 20 Oct 2014 13:33:55 +0200
Subject: Fix problem with GL viewport

Make sure the window has the correct size before the GL view is created.
Otherwise the view doesn't use the resized window size and things render
in an awfully small area in the corner of the window.

Fixes Dragon Memory, for example (also seen in other games using SDL+GL).

diff --git a/src/video/bwindow/SDL_sysvideo.cc b/src/video/bwindow/SDL_sysvideo.cc
index 5d4077f..7e1bd38 100644
--- a/src/video/bwindow/SDL_sysvideo.cc
+++ b/src/video/bwindow/SDL_sysvideo.cc
@@ -507,6 +507,8 @@ SDL_Surface *BE_SetVideoMode(_THIS, SDL_Surface *current,
 	BRect bounds;
 	Uint32 gl_flags = 0;
 
+	SDL_Win->ResizeTo(width, height);
+
 	/* Only RGB works on r5 currently */
 	gl_flags = BGL_RGB;
 	if (_this->gl_config.double_buffer)
-- 
2.26.0


From f3607ec1423ff4eb4b15ea89e956b608b1cbfe23 Mon Sep 17 00:00:00 2001
From: Michael Lotz <mmlr@mlotz.ch>
Date: Tue, 27 Oct 2015 21:39:24 +0000
Subject: Avoid crashing on quit when SDL_Win was never initialized.

This can happen when an unknown screen mode is detected and video init
fails because of it.

diff --git a/src/video/bwindow/SDL_sysvideo.cc b/src/video/bwindow/SDL_sysvideo.cc
index 7e1bd38..818d1a0 100644
--- a/src/video/bwindow/SDL_sysvideo.cc
+++ b/src/video/bwindow/SDL_sysvideo.cc
@@ -807,8 +807,10 @@ void BE_VideoQuit(_THIS)
 {
 	int i, j;
 
-	SDL_Win->Quit();
-	SDL_Win = NULL;
+	if ( SDL_Win != NULL ) {
+		SDL_Win->Quit();
+		SDL_Win = NULL;
+	}
 
 	if ( SDL_BlankCursor != NULL ) {
 		BE_FreeWMCursor(_this, SDL_BlankCursor);
-- 
2.26.0


From f6184d24a887cc0bfcd638bd6afd8e25a64a19af Mon Sep 17 00:00:00 2001
From: Michael Lotz <mmlr@mlotz.ch>
Date: Tue, 27 Oct 2015 21:40:36 +0000
Subject: Add 24 bit color spaces to ColorSpaceToBitsPerPixel().

This allows initializing the native video mode when running with a 24
bit screen color depth.

diff --git a/src/video/bwindow/SDL_sysvideo.cc b/src/video/bwindow/SDL_sysvideo.cc
index 818d1a0..0100f3b 100644
--- a/src/video/bwindow/SDL_sysvideo.cc
+++ b/src/video/bwindow/SDL_sysvideo.cc
@@ -179,6 +179,10 @@ static inline int ColorSpaceToBitsPerPixel(uint32 colorspace)
 	    case B_RGB16_BIG:
 		bitsperpixel = 16;
 		break;
+	    case B_RGB24:
+	    case B_RGB24_BIG:
+		bitsperpixel = 24;
+		break;
 	    case B_RGB32:
 	    case B_RGBA32:
 	    case B_RGB32_BIG:
-- 
2.26.0


From 5623298d212e0f2a8750a8db062413061e5b7f84 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= <revol@free.fr>
Date: Mon, 11 Jul 2016 15:08:06 +0200
Subject: Try to find correct application signature from app resources

Instead of always hardcoding application/x-SDL-executable.

diff --git a/src/main/beos/SDL_BeApp.cc b/src/main/beos/SDL_BeApp.cc
index 8b79377..65a6dbf 100644
--- a/src/main/beos/SDL_BeApp.cc
+++ b/src/main/beos/SDL_BeApp.cc
@@ -24,8 +24,10 @@
 /* Handle the BeApp specific portions of the application */
 
 #include <AppKit.h>
+#include <storage/AppFileInfo.h>
 #include <storage/Path.h>
 #include <storage/Entry.h>
+#include <storage/File.h>
 #include <unistd.h>
 
 #include "SDL_BeApp.h"
@@ -42,7 +44,24 @@ static int StartBeApp(void *unused)
   if(!be_app) {
 	BApplication *App;
 
-	App = new BApplication("application/x-SDL-executable");
+	// default application signature
+	const char *signature = "application/x-SDL-executable";
+	// dig resources for correct signature
+	image_info info;
+	int32 cookie = 0;
+	if (get_next_image_info(B_CURRENT_TEAM, &cookie, &info) == B_OK) {
+		BFile f(info.name, O_RDONLY);
+		if (f.InitCheck() == B_OK) {
+			BAppFileInfo app_info(&f);
+			if (app_info.InitCheck() == B_OK) {
+				char sig[B_MIME_TYPE_LENGTH];
+				if (app_info.GetSignature(sig) == B_OK)
+					signature = strndup(sig, B_MIME_TYPE_LENGTH);
+			}
+		}
+	}
+
+	App = new BApplication(signature);
 
 	App->Run();
 	delete App;
-- 
2.26.0


From 1a6af96dd5a4bb67128d49a1de660f94ae8ef7a2 Mon Sep 17 00:00:00 2001
From: Peter Kosyh <p.kosyh@gmail.com>
Date: Fri, 5 Oct 2018 17:39:06 +0300
Subject: fix black box in top-left corner while win lost mouse focus


diff --git a/src/video/bwindow/SDL_sysevents.cc b/src/video/bwindow/SDL_sysevents.cc
index 9e12750..63afded 100644
--- a/src/video/bwindow/SDL_sysevents.cc
+++ b/src/video/bwindow/SDL_sysevents.cc
@@ -208,7 +208,8 @@ void SDL_BWin::DispatchMessage(BMessage *msg, BHandler *target)
 					// for some reason, SDL_EraseCursor fails for OpenGL
 					if (this->the_view != this->SDL_GLView)
 #endif
-							SDL_EraseCursor(SDL_VideoSurface);
+						{ if (SHOULD_DRAWCURSOR(SDL_cursorstate))
+							SDL_EraseCursor(SDL_VideoSurface); }
 						be_app->SetCursor(B_HAND_CURSOR);
 					}
 				} else {
@@ -218,7 +219,8 @@ void SDL_BWin::DispatchMessage(BMessage *msg, BHandler *target)
 					// for some reason, SDL_EraseCursor fails for OpenGL
 					if (this->the_view != this->SDL_GLView)
 #endif
-							SDL_EraseCursor(SDL_VideoSurface);
+						{ if (SHOULD_DRAWCURSOR(SDL_cursorstate))
+							SDL_EraseCursor(SDL_VideoSurface); }
 						SDL_SetCursor(NULL);
 					}
 
-- 
2.26.0


From e467a7710ab2ce5dbb2a83154114f37bb005edf2 Mon Sep 17 00:00:00 2001
From: Peter Kosyh <p.kosyh@gmail.com>
Date: Sat, 6 Oct 2018 14:17:22 +0300
Subject: libsdl: mouse wheel inverted


diff --git a/src/video/bwindow/SDL_sysevents.cc b/src/video/bwindow/SDL_sysevents.cc
index 63afded..225e882 100644
--- a/src/video/bwindow/SDL_sysevents.cc
+++ b/src/video/bwindow/SDL_sysevents.cc
@@ -307,11 +307,11 @@ void SDL_BWin::DispatchMessage(BMessage *msg, BHandler *target)
 			x = y = 0;
 			if (msg->FindFloat("be:wheel_delta_x", &x) == B_OK && msg->FindFloat("be:wheel_delta_y", &y) == B_OK) {
 				if (x < 0 || y < 0) {
-					SDL_PrivateMouseButton(SDL_PRESSED, SDL_BUTTON_WHEELDOWN, 0, 0);
-					SDL_PrivateMouseButton(SDL_RELEASED, SDL_BUTTON_WHEELDOWN, 0, 0);
-				} else if (x > 0 || y > 0) {
 					SDL_PrivateMouseButton(SDL_PRESSED, SDL_BUTTON_WHEELUP, 0, 0);
 					SDL_PrivateMouseButton(SDL_RELEASED, SDL_BUTTON_WHEELUP, 0, 0);
+				} else if (x > 0 || y > 0) {
+					SDL_PrivateMouseButton(SDL_PRESSED, SDL_BUTTON_WHEELDOWN, 0, 0);
+					SDL_PrivateMouseButton(SDL_RELEASED, SDL_BUTTON_WHEELDOWN, 0, 0);
 				}
 			}
 			break;
-- 
2.26.0


From ef0037b49fd0359c7d5288978c1d484d1197db53 Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Tue, 19 May 2020 18:10:45 +1000
Subject: Disable direct mode for BGLView


diff --git a/src/video/bwindow/SDL_BWin.h b/src/video/bwindow/SDL_BWin.h
index 2cb94ee..d6b6b3c 100644
--- a/src/video/bwindow/SDL_BWin.h
+++ b/src/video/bwindow/SDL_BWin.h
@@ -25,6 +25,8 @@
 
 #include "SDL_config.h"
 
+#define B_GL_DIRECT_WINDOW_DISABLE 1
+
 #include <stdio.h>
 #include <AppKit.h>
 #include <InterfaceKit.h>
@@ -114,7 +116,9 @@ public:
 				SDL_GLView = new BGLView(Bounds(), "SDL GLView",
 					 	B_FOLLOW_ALL_SIDES, (B_WILL_DRAW|B_FRAME_EVENTS),
 					 	gl_flags|BGL_DOUBLE);
+#ifndef B_GL_DIRECT_WINDOW_DISABLE
 				SDL_GLView->EnableDirectMode(true);
+#endif
 			}
 			if ( the_view != SDL_GLView ) {
 				if ( the_view ) {
@@ -140,11 +144,6 @@ public:
 				the_view = SDL_View;
 			}
 		}
-#if SDL_VIDEO_OPENGL
-		if ( the_view == SDL_GLView ) {
-			SDL_GLView->UnlockGL();
-		}
-#endif
 		Unlock();
 		return(retval);
 	}
@@ -216,10 +215,12 @@ public:
 			   the application should respond to the quit request,
 			   or ignore it as desired.
 			 */
+#ifndef B_GL_DIRECT_WINDOW_DISABLE
 #if SDL_VIDEO_OPENGL
 			if ( SDL_GLView != NULL ) {
 				SDL_GLView->EnableDirectMode(false);
 			}
+#endif
 #endif
 			return(false);
 		}
diff --git a/src/video/bwindow/SDL_sysevents.cc b/src/video/bwindow/SDL_sysevents.cc
index 225e882..38ce3c9 100644
--- a/src/video/bwindow/SDL_sysevents.cc
+++ b/src/video/bwindow/SDL_sysevents.cc
@@ -408,10 +408,12 @@ void SDL_BWin::DirectConnected(direct_buffer_info *info) {
 		default:
 			break;
 	}
+#ifndef B_GL_DIRECT_WINDOW_DISABLE
 #if SDL_VIDEO_OPENGL
 	// If it is a BGLView, it is apparently required to
 	// call DirectConnected() on it as well
 	if (this->the_view == this->SDL_GLView)
 		this->SDL_GLView->DirectConnected(info);
-#endif	
+#endif
+#endif
 }
-- 
2.26.0


From 3d47666a1eed13f76c230b68dd957f65e1a3b573 Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Thu, 21 May 2020 13:11:45 +1000
Subject: Add window positon support


diff --git a/src/video/bwindow/SDL_sysvideo.cc b/src/video/bwindow/SDL_sysvideo.cc
index 0100f3b..07f16b5 100644
--- a/src/video/bwindow/SDL_sysvideo.cc
+++ b/src/video/bwindow/SDL_sysvideo.cc
@@ -578,6 +578,30 @@ SDL_Surface *BE_SetVideoMode(_THIS, SDL_Surface *current,
 	/* Set the correct fullscreen mode */
 	BE_SetFullScreen(_this, current, flags & SDL_FULLSCREEN ? 1 : 0);
 
+	if (!(flags & SDL_FULLSCREEN)) {
+		int x, y;
+	    BPoint pos;
+	    const char *window = SDL_getenv("SDL_VIDEO_WINDOW_POS");
+		const char *center = SDL_getenv("SDL_VIDEO_CENTERED");
+
+		if ( window ) {
+			if ( SDL_sscanf(window, "%d,%d", x, y) == 2 ) {
+				pos.x = x;
+				pos.y = y;
+				SDL_Win->MoveTo(pos);
+				return(current);
+			}
+			if ( SDL_strcmp(window, "center") == 0 ) {
+				center = window;
+			}
+		}
+		if ( center ) {
+			pos.x = (bscreen.Frame().Width() - width) / 2;
+			pos.y = (bscreen.Frame().Height() - height) / 2;
+			SDL_Win->MoveTo(pos);
+		}
+	}
+
 	/* We're done */
 	return(current);
 }
-- 
2.26.0


From b6d516bb5cdd05ecb58c77649a1607acf0fde95d Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Thu, 21 May 2020 19:57:35 +1000
Subject: Fix window size


diff --git a/src/video/bwindow/SDL_BWin.h b/src/video/bwindow/SDL_BWin.h
index d6b6b3c..3eb4c55 100644
--- a/src/video/bwindow/SDL_BWin.h
+++ b/src/video/bwindow/SDL_BWin.h
@@ -102,8 +102,8 @@ public:
 	virtual void FrameResized(float width, float height) {
 		if(inhibit_resize)
 			inhibit_resize = false;
-		else 
-			SDL_PrivateResize((int)width, (int)height);
+		else
+			SDL_PrivateResize((int)width+1, (int)height+1);
 	}
 	virtual int CreateView(Uint32 flags, Uint32 gl_flags) {
 		int retval;
@@ -178,6 +178,9 @@ public:
 #endif
 		SDL_View->GetXYOffset(x, y);
 	}
+	virtual void ResizeWindow(int width, int height) {
+		ResizeTo(width-1, height-1);
+	}
 	virtual bool BeginDraw(void) {
 		return(Lock());
 	}
diff --git a/src/video/bwindow/SDL_sysevents.cc b/src/video/bwindow/SDL_sysevents.cc
index 38ce3c9..549af1c 100644
--- a/src/video/bwindow/SDL_sysevents.cc
+++ b/src/video/bwindow/SDL_sysevents.cc
@@ -398,10 +398,10 @@ void SDL_BWin::DirectConnected(direct_buffer_info *info) {
 		case B_DIRECT_START:
 		case B_DIRECT_MODIFY:
 			{
-				int32 width = info->window_bounds.right -
-					info->window_bounds.left;
-				int32 height = info->window_bounds.bottom -
-					info->window_bounds.top;
+				int32 width = (info->window_bounds.right -
+					info->window_bounds.left) + 1;
+				int32 height = (info->window_bounds.bottom -
+					info->window_bounds.top) + 1;
 				SDL_PrivateResize(width, height);
 				break;
 			}
diff --git a/src/video/bwindow/SDL_sysvideo.cc b/src/video/bwindow/SDL_sysvideo.cc
index 07f16b5..163b0a6 100644
--- a/src/video/bwindow/SDL_sysvideo.cc
+++ b/src/video/bwindow/SDL_sysvideo.cc
@@ -468,7 +468,7 @@ static int BE_SetFullScreen(_THIS, SDL_Surface *screen, int fullscreen)
 		// printf("Doing not fullscreen stuff.\n");
 		// We are not in full screen mode, so we want to change the window
 		// size to match the resolution in SDL.
-		SDL_Win->ResizeTo(width, height);
+		SDL_Win->ResizeWindow(width, height);
 		
 		// And also center the window and reset the drawing offset.
 		if(firstTime || wasFullScreen) {
@@ -511,7 +511,7 @@ SDL_Surface *BE_SetVideoMode(_THIS, SDL_Surface *current,
 	BRect bounds;
 	Uint32 gl_flags = 0;
 
-	SDL_Win->ResizeTo(width, height);
+	SDL_Win->ResizeWindow(width, height);
 
 	/* Only RGB works on r5 currently */
 	gl_flags = BGL_RGB;
-- 
2.26.0

