From 15a826571c8f4809023c2f3ca014705feb5492bf Mon Sep 17 00:00:00 2001
From: steve <romulox_x@yahoo.com>
Date: Thu, 22 Oct 2015 17:31:09 -0700
Subject: Added setting for CADisplayLink on iOS, so you no longer need to
 recompile to change it

---
 platform/iphone/app_delegate.mm             |  2 +
 platform/iphone/gl_view.h                   |  9 ++--
 platform/iphone/gl_view.mm                  | 75 +++++++++++++++--------------
 platform/iphone/globals/global_defaults.cpp |  1 +
 4 files changed, 45 insertions(+), 42 deletions(-)
 mode change 100644 => 100755 platform/iphone/globals/global_defaults.cpp

diff --git a/platform/iphone/app_delegate.mm b/platform/iphone/app_delegate.mm
index d5764b2b5c..647bf1a2d1 100644
--- a/platform/iphone/app_delegate.mm
+++ b/platform/iphone/app_delegate.mm
@@ -236,6 +236,8 @@ static int frame_count = 0;
 	view_controller.view = glView;
 	window.rootViewController = view_controller;
 
+	glView.useCADisplayLink = bool(GLOBAL_DEF("display.iOS/use_cadisplaylink",true)) ? YES : NO;
+	printf("cadisaplylink: %d", glView.useCADisplayLink);
 	glView.animationInterval = 1.0 / kRenderingFrequency;
 	[glView startAnimation];
 	
diff --git a/platform/iphone/gl_view.h b/platform/iphone/gl_view.h
index cda75394db..ca2d1d25ae 100755
--- a/platform/iphone/gl_view.h
+++ b/platform/iphone/gl_view.h
@@ -34,8 +34,6 @@
 #import <MediaPlayer/MediaPlayer.h>
 #import <AVFoundation/AVFoundation.h>
 
-#define USE_CADISPLAYLINK      0   //iOS version 3.1+ is required
-
 @protocol GLViewDelegate;
 
 @interface GLView : UIView<UIKeyInput>
@@ -53,13 +51,13 @@
 	// OpenGL name for the depth buffer that is attached to viewFramebuffer, if it exists (0 if it does not exist)
 	GLuint depthRenderbuffer;
 	
-#if USE_CADISPLAYLINK
+	BOOL useCADisplayLink;
 	// CADisplayLink available on 3.1+ synchronizes the animation timer & drawing with the refresh rate of the display, only supports animation intervals of 1/60 1/30 & 1/15
 	CADisplayLink *displayLink;
-#else
+
 	// An animation timer that, when animation is started, will periodically call -drawView at the given rate.
+	// Only used if CADisplayLink is not
 	NSTimer *animationTimer;
-#endif
 	
 	NSTimeInterval animationInterval;
 	
@@ -104,6 +102,7 @@
 - (void)audioRouteChangeListenerCallback:(NSNotification*)notification;
 
 @property NSTimeInterval animationInterval;
+@property(nonatomic, assign) BOOL useCADisplayLink;
 
 @end
 
diff --git a/platform/iphone/gl_view.mm b/platform/iphone/gl_view.mm
index 4d5d1b81e3..279fbdafa8 100755
--- a/platform/iphone/gl_view.mm
+++ b/platform/iphone/gl_view.mm
@@ -334,13 +334,15 @@ static void clear_touches() {
 	delegateSetup = ![delegate respondsToSelector:@selector(setupView:)];
 }
 
+@synthesize useCADisplayLink;
+
 // If our view is resized, we'll be asked to layout subviews.
 // This is the perfect opportunity to also update the framebuffer so that it is
 // the same size as our display area.
 
 -(void)layoutSubviews
 {
-	printf("HERE\n");
+	//printf("HERE\n");
 	[EAGLContext setCurrentContext:context];
 	[self destroyFramebuffer];
 	[self createFramebuffer];
@@ -418,19 +420,21 @@ static void clear_touches() {
 		return;
 	active = TRUE;
 	printf("start animation!\n");
-#if USE_CADISPLAYLINK
-	// Approximate frame rate
-	// assumes device refreshes at 60 fps
-	int frameInterval = (int) floor(animationInterval * 60.0f);
-
-	displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawView)];
-	[displayLink setFrameInterval:frameInterval];
-
-	// Setup DisplayLink in main thread
-	[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
-#else
-	animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(drawView) userInfo:nil repeats:YES];
-#endif
+	if (useCADisplayLink) {
+
+		// Approximate frame rate
+		// assumes device refreshes at 60 fps
+		int frameInterval = (int) floor(animationInterval * 60.0f);
+
+		displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawView)];
+		[displayLink setFrameInterval:frameInterval];
+
+		// Setup DisplayLink in main thread
+		[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
+	}
+	else {
+		animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(drawView) userInfo:nil repeats:YES];
+	}
 
 	if (video_playing)
 	{
@@ -444,13 +448,16 @@ static void clear_touches() {
 		return;
 	active = FALSE;
 	printf("******** stop animation!\n");
-#if USE_CADISPLAYLINK
-	[displayLink invalidate];
-	displayLink = nil;
-#else
-	[animationTimer invalidate];
-	animationTimer = nil;
-#endif
+
+	if (useCADisplayLink) {
+		[displayLink invalidate];
+		displayLink = nil;
+	}
+	else {
+		[animationTimer invalidate];
+		animationTimer = nil;
+	}
+
 	clear_touches();
 
 	if (video_playing)
@@ -462,13 +469,7 @@ static void clear_touches() {
 - (void)setAnimationInterval:(NSTimeInterval)interval
 {
 	animationInterval = interval;
-	
-#if USE_CADISPLAYLINK
-	if(displayLink)
-#else
-	if(animationTimer)
-#endif
-	{
+	if ( (useCADisplayLink && displayLink) || ( !useCADisplayLink && animationTimer ) ) {
 		[self stopAnimation];
 		[self startAnimation];
 	}
@@ -477,16 +478,16 @@ static void clear_touches() {
 // Updates the OpenGL view when the timer fires
 - (void)drawView
 {
-#if USE_CADISPLAYLINK
-	// Pause the CADisplayLink to avoid recursion
-	[displayLink setPaused: YES];
+	if (useCADisplayLink) {
+		// Pause the CADisplayLink to avoid recursion
+		[displayLink setPaused: YES];
 
-	// Process all input events
-	while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, TRUE) == kCFRunLoopRunHandledSource);
+		// Process all input events
+		while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, TRUE) == kCFRunLoopRunHandledSource);
 
-	// We are good to go, resume the CADisplayLink
-	[displayLink setPaused: NO];
-#endif
+		// We are good to go, resume the CADisplayLink
+		[displayLink setPaused: NO];
+	}
 
 	if (!active) {
 		printf("draw view not active!\n");
@@ -632,7 +633,7 @@ static void clear_touches() {
 		case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
 			NSLog(@"AVAudioSessionRouteChangeReasonOldDeviceUnavailable");
 			NSLog(@"Headphone/Line was pulled. Resuming video play....");
-			if (_is_video_playing) {
+			if (_is_video_playing()) {
 
 				dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5f * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
 							[_instance.avPlayer play]; // NOTE: change this line according your current player implementation
diff --git a/platform/iphone/globals/global_defaults.cpp b/platform/iphone/globals/global_defaults.cpp
old mode 100644
new mode 100755
index a4929c57dc..18a51a5b4e
--- a/platform/iphone/globals/global_defaults.cpp
+++ b/platform/iphone/globals/global_defaults.cpp
@@ -9,4 +9,5 @@ void register_iphone_global_defaults() {
 	GLOBAL_DEF("rasterizer.iOS/fp16_framebuffer",false);
 	GLOBAL_DEF("display.iOS/driver","GLES2");
 	Globals::get_singleton()->set_custom_property_info("display.iOS/driver",PropertyInfo(Variant::STRING,"display.iOS/driver",PROPERTY_HINT_ENUM,"GLES1,GLES2"));
+	GLOBAL_DEF("display.iOS/use_cadisplaylink",true);
 }
-- 
cgit v1.2.3


From 746ef7cbd692af74faeefdbb0e45e8ab5b1b11d4 Mon Sep 17 00:00:00 2001
From: Phobos Tro <phobos_tro@yahoo.com>
Date: Sun, 25 Oct 2015 22:15:56 +0800
Subject: Fixing memory leaks

---
 platform/x11/context_gl_x11.cpp | 10 ++++++++--
 platform/x11/os_x11.cpp         | 39 ++++++++++++++++++++++++---------------
 platform/x11/os_x11.h           |  1 +
 3 files changed, 33 insertions(+), 17 deletions(-)

diff --git a/platform/x11/context_gl_x11.cpp b/platform/x11/context_gl_x11.cpp
index dc0dc063b9..3db1f6da25 100644
--- a/platform/x11/context_gl_x11.cpp
+++ b/platform/x11/context_gl_x11.cpp
@@ -46,7 +46,6 @@ struct ContextGL_X11_Private {
 	::GLXContext glx_context;
 };
 
-
 void ContextGL_X11::release_current() {
 
 	glXMakeCurrent(x11_display, None, NULL);
@@ -56,10 +55,12 @@ void ContextGL_X11::make_current() {
 
 	glXMakeCurrent(x11_display, x11_window, p->glx_context);
 }
+
 void ContextGL_X11::swap_buffers() {
 
 	glXSwapBuffers(x11_display,x11_window);
 }
+
 /*
 static GLWrapperFuncPtr wrapper_get_proc_address(const char* p_function) {
 
@@ -154,6 +155,9 @@ Error ContextGL_X11::initialize() {
 */
 	//glXMakeCurrent(x11_display, None, NULL);
 
+	XFree( vi );
+	XFree( fbc );
+
 	return OK;
 }
 
@@ -164,12 +168,12 @@ int ContextGL_X11::get_window_width() {
 	
 	return xwa.width;
 }
+
 int ContextGL_X11::get_window_height() {
 	XWindowAttributes xwa;
 	XGetWindowAttributes(x11_display,x11_window,&xwa);
 	
 	return xwa.height;
-
 }
 
 
@@ -189,6 +193,8 @@ ContextGL_X11::ContextGL_X11(::Display *p_x11_display,::Window &p_x11_window,con
 
 
 ContextGL_X11::~ContextGL_X11() {
+	release_current();
+	glXDestroyContext( x11_display, p->glx_context );
 
 	memdelete( p );
 }
diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp
index 85928f2815..d538b4c7b6 100644
--- a/platform/x11/os_x11.cpp
+++ b/platform/x11/os_x11.cpp
@@ -153,6 +153,7 @@ void OS_X11::initialize(const VideoMode& p_desired,int p_video_driver,int p_audi
 			
 			XFree (xim_styles);
 		}
+		XFree( imvalret );
 	}
 
 	/*
@@ -351,6 +352,7 @@ void OS_X11::initialize(const VideoMode& p_desired,int p_video_driver,int p_audi
 	for(int i=0;i<CURSOR_MAX;i++) {
 
 		cursors[i]=None;
+		img[i]=NULL;
 	}
 
 	current_cursor=CURSOR_ARROW;
@@ -379,16 +381,15 @@ void OS_X11::initialize(const VideoMode& p_desired,int p_video_driver,int p_audi
 				"question_arrow"
 			};
 
-			XcursorImage *img = XcursorLibraryLoadImage(cursor_file[i],cursor_theme,cursor_size);
-			if (img) {
-				cursors[i]=XcursorImageLoadCursor(x11_display,img);
+			img[i] = XcursorLibraryLoadImage(cursor_file[i],cursor_theme,cursor_size);
+			if (img[i]) {
+				cursors[i]=XcursorImageLoadCursor(x11_display,img[i]);
 				//print_line("found cursor: "+String(cursor_file[i])+" id "+itos(cursors[i]));
 			} else {
 				if (OS::is_stdout_verbose())
 					print_line("failed cursor: "+String(cursor_file[i]));
 			}
 		}
-
 	}
 
 
@@ -399,9 +400,9 @@ void OS_X11::initialize(const VideoMode& p_desired,int p_video_driver,int p_audi
 		 XColor col;
 		 Cursor cursor;
 
-		 cursormask = XCreatePixmap(x11_display, RootWindow(x11_display,DefaultScreen(x11_display)), 1, 1, 1);
-		 xgc.function = GXclear;
-		 gc = XCreateGC(x11_display, cursormask, GCFunction, &xgc);
+		cursormask = XCreatePixmap(x11_display, RootWindow(x11_display,DefaultScreen(x11_display)), 1, 1, 1);
+		xgc.function = GXclear;
+		gc = XCreateGC(x11_display, cursormask, GCFunction, &xgc);
 		XFillRectangle(x11_display, cursormask, gc, 0, 0, 1, 1);
 		col.pixel = 0;
 		col.red = 0;
@@ -438,13 +439,8 @@ void OS_X11::initialize(const VideoMode& p_desired,int p_video_driver,int p_audi
 
 	_ensure_data_dir();
 
-	net_wm_icon = XInternAtom(x11_display, "_NET_WM_ICON", False);
-
-
-
-	//printf("got map notify\n");
-		
 }
+
 void OS_X11::finalize() {
 
 	if(main_loop)
@@ -476,14 +472,26 @@ void OS_X11::finalize() {
 
 	memdelete(input);
 
+	XUnmapWindow( x11_display, x11_window );
+	XDestroyWindow( x11_display, x11_window );
+
 #if defined(OPENGL_ENABLED) || defined(LEGACYGL_ENABLED)
 	memdelete(context_gl);
 #endif
-	
+	for(int i=0;i<CURSOR_MAX;i++) {
+		if( cursors[i] != None )
+			XFreeCursor( x11_display, cursors[i] );		
+		if( img[i] != NULL )
+			XcursorImageDestroy( img[i] );
+	};	
+
+	XDestroyIC( xic );
+	XCloseIM( xim );
 
 	XCloseDisplay(x11_display);
 	if (xmbstring)
 		memfree(xmbstring);
+
 		
 	args.clear();
 }
@@ -1861,7 +1869,6 @@ void OS_X11::set_cursor_shape(CursorShape p_shape) {
 			XDefineCursor(x11_display,x11_window,cursors[CURSOR_ARROW]);
 	}
 
-
 	current_cursor=p_shape;
 }
 
@@ -1894,6 +1901,8 @@ void OS_X11::alert(const String& p_alert,const String& p_title) {
 }
 
 void OS_X11::set_icon(const Image& p_icon) {
+	net_wm_icon = XInternAtom(x11_display, "_NET_WM_ICON", False);
+
 	if (!p_icon.empty()) {
 		Image img=p_icon;
 		img.convert(Image::FORMAT_RGBA);
diff --git a/platform/x11/os_x11.h b/platform/x11/os_x11.h
index 1566062b9e..1258dff7a9 100644
--- a/platform/x11/os_x11.h
+++ b/platform/x11/os_x11.h
@@ -119,6 +119,7 @@ class OS_X11 : public OS_Unix {
 
 	const char *cursor_theme;
 	int cursor_size;
+	XcursorImage *img[CURSOR_MAX];
 	Cursor cursors[CURSOR_MAX];
 	Cursor null_cursor;
 	CursorShape current_cursor;
-- 
cgit v1.2.3


From 2d2ab92680cdc30fa1bfb192adaa89ef50dcb857 Mon Sep 17 00:00:00 2001
From: Phobos Tro <phobos_tro@yahoo.com>
Date: Mon, 26 Oct 2015 01:36:27 +0800
Subject: Made the Atom net_wm_icon local

---
 platform/x11/os_x11.cpp | 29 +++++------------------------
 platform/x11/os_x11.h   |  5 -----
 2 files changed, 5 insertions(+), 29 deletions(-)

diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp
index d538b4c7b6..34bd1184a1 100644
--- a/platform/x11/os_x11.cpp
+++ b/platform/x11/os_x11.cpp
@@ -73,20 +73,18 @@
 #undef CursorShape
 
 int OS_X11::get_video_driver_count() const {
-
 	return 1;
 }
-const char * OS_X11::get_video_driver_name(int p_driver) const {
 
+const char * OS_X11::get_video_driver_name(int p_driver) const {
 	return "GLES2";
 }
-OS::VideoMode OS_X11::get_default_video_mode() const {
 
+OS::VideoMode OS_X11::get_default_video_mode() const {
 	return OS::VideoMode(800,600,false);
 }
 
 int OS_X11::get_audio_driver_count() const {
-
     return AudioDriverManagerSW::get_driver_count();
 }
 
@@ -257,7 +255,6 @@ void OS_X11::initialize(const VideoMode& p_desired,int p_video_driver,int p_audi
 	}
 #endif
 
-
 	AudioDriverManagerSW::get_driver(p_audio_driver)->set_singleton();
 
 	audio_driver_index=p_audio_driver;
@@ -438,7 +435,6 @@ void OS_X11::initialize(const VideoMode& p_desired,int p_video_driver,int p_audi
 	probe_joystick();
 
 	_ensure_data_dir();
-
 }
 
 void OS_X11::finalize() {
@@ -567,37 +563,29 @@ void OS_X11::warp_mouse_pos(const Point2& p_to) {
 }
 
 OS::MouseMode OS_X11::get_mouse_mode() const {
-
 	return mouse_mode;
 }
 
-
-
 int OS_X11::get_mouse_button_state() const {
 	return last_button_state;
 }
 
 Point2 OS_X11::get_mouse_pos() const {
-
 	return last_mouse_pos;
 }
 
 void OS_X11::set_window_title(const String& p_title) {
-
 	XStoreName(x11_display,x11_window,p_title.utf8().get_data());
 }
 
 void OS_X11::set_video_mode(const VideoMode& p_video_mode,int p_screen) {
-
-
 }
-OS::VideoMode OS_X11::get_video_mode(int p_screen) const {
 
+OS::VideoMode OS_X11::get_video_mode(int p_screen) const {
 	return current_videomode;
 }
-void OS_X11::get_fullscreen_mode_list(List<VideoMode> *p_list,int p_screen) const {
-
 
+void OS_X11::get_fullscreen_mode_list(List<VideoMode> *p_list,int p_screen) const {
 }
 
 //#ifdef NEW_WM_API
@@ -680,7 +668,6 @@ void OS_X11::set_current_screen(int p_screen) {
 }
 
 Point2 OS_X11::get_screen_position(int p_screen) const {
-
 	// Using Xinerama Extension
 	int event_base, error_base;
 	const Bool ext_okay = XineramaQueryExtension(x11_display, &event_base, &error_base);
@@ -715,7 +702,6 @@ Size2 OS_X11::get_screen_size(int p_screen) const {
 	XFree(xsi);
 	return size;
 }
-	
 
 Point2 OS_X11::get_window_position() const {
 	int x,y;
@@ -904,7 +890,6 @@ void OS_X11::set_window_maximized(bool p_enabled) {
 	current_videomode.height = xwa.height;
 */
 	maximized = p_enabled;
-
 }
 
 bool OS_X11::is_window_maximized() const {
@@ -1000,7 +985,6 @@ unsigned int OS_X11::get_mouse_button_state(unsigned int p_x11_state) {
 }
 	
 void OS_X11::handle_key_event(XKeyEvent *p_event, bool p_echo) {
-
 			
 	// X11 functions don't know what const is
 	XKeyEvent *xkeyevent = p_event;
@@ -1166,8 +1150,6 @@ void OS_X11::handle_key_event(XKeyEvent *p_event, bool p_echo) {
 
 	//printf("key: %x\n",event.key.scancode);
 	input->parse_input_event( event);
-
-	
 }
 
 void OS_X11::process_xevents() {
@@ -1901,7 +1883,7 @@ void OS_X11::alert(const String& p_alert,const String& p_title) {
 }
 
 void OS_X11::set_icon(const Image& p_icon) {
-	net_wm_icon = XInternAtom(x11_display, "_NET_WM_ICON", False);
+	Atom net_wm_icon = XInternAtom(x11_display, "_NET_WM_ICON", False);
 
 	if (!p_icon.empty()) {
 		Image img=p_icon;
@@ -1935,7 +1917,6 @@ void OS_X11::set_icon(const Image& p_icon) {
 	    XDeleteProperty(x11_display, x11_window, net_wm_icon);
 	}
 	XFlush(x11_display);
-
 }
 
 
diff --git a/platform/x11/os_x11.h b/platform/x11/os_x11.h
index 1258dff7a9..ed61df8f0e 100644
--- a/platform/x11/os_x11.h
+++ b/platform/x11/os_x11.h
@@ -156,12 +156,7 @@ class OS_X11 : public OS_Unix {
 			};
 		};
 	};
-
-	Atom net_wm_icon;
-
-
 	int joystick_count;
-
 	Joystick joysticks[JOYSTICKS_MAX];
 
 	int audio_driver_index;
-- 
cgit v1.2.3