# HG changeset patch
# User Andrew Janke <andrew@apjanke.net>
# Date 1581503272 18000
#      Wed Feb 12 05:27:52 2020 -0500
# Branch appnap-disable-on-newer-macos-take-2
# Node ID ded680c06d605817c12859ee474e15f2af318ea1
# Parent  138c3dcf8d4ae85752aefb78e941f58f6337016c
octave-qobject: Disable app nap workaround on macOS 10.14+, where API change breaks the build (bug #57754)

This version of the fix uses a configure test to detect the objc_msgSend signature, instead of testing against specific macOS versions.

diff --git a/configure.ac b/configure.ac
--- a/configure.ac
+++ b/configure.ac
@@ -1742,16 +1742,20 @@ if test $have_framework_carbon = yes; th
   AC_SUBST(CARBON_LIBS)
 fi
 
 ## Check for the CGDisplayBitsPerPixel function.
 if test $have_framework_carbon = yes; then
   OCTAVE_CARBON_CGDISPLAYBITSPERPIXEL
 fi
 
+### Check for other macOS stuff.
+
+OCTAVE_CHECK_OLD_STYLE_OBJCMSGSEND
+
 ### Check for OpenGL and helper libraries used by OpenGL graphics renderer.
 
 check_opengl=yes
 AC_ARG_WITH([opengl],
   [AS_HELP_STRING([--without-opengl],
     [don't use OpenGL libraries, disable OpenGL graphics])],
   [if test x"$withval" = xno; then
      check_opengl=no
diff --git a/libgui/src/octave-qobject.cc b/libgui/src/octave-qobject.cc
--- a/libgui/src/octave-qobject.cc
+++ b/libgui/src/octave-qobject.cc
@@ -42,34 +42,33 @@
 #include "octave-qobject.h"
 #include "qt-application.h"
 #include "qt-interpreter-events.h"
 #include "resource-manager.h"
 #include "shortcut-manager.h"
 
 // Bug #55940 (Disable App Nap on Mac)
 #if defined (Q_OS_MAC)
-#ifdef __MAC_OS_X_VERSION_MAX_ALLOWED
-#if __MAC_OS_X_VERSION_MAX_ALLOWED < 101400
+#if HAVE_OBJCMSGSEND_OLDSTYLE
 #  include <objc/runtime.h>
 #  include <objc/message.h>
 #endif
 #endif
-#endif
 
 #include "oct-env.h"
 #include "version.h"
 
 #include "ovl.h"
 
 
 // Bug #55940 (Disable App Nap on Mac)
 #if defined (Q_OS_MAC)
-#ifdef __MAC_OS_X_VERSION_MAX_ALLOWED
-#if __MAC_OS_X_VERSION_MAX_ALLOWED < 101400
+// Only do this with the pre-10.14 objc_msgSend signature; apjanke hasn't
+// been able to figure out how to use the new signature.
+#if HAVE_OBJCMSGSEND_OLDSTYLE
 static void disable_app_nap (void)
 {
   Class process_info_class;
   SEL process_info_selector;
   SEL begin_activity_with_options_selector;
   id process_info;
   id reason_string;
   id osx_latencycritical_activity;
@@ -111,17 +110,16 @@ static void disable_app_nap (void)
   osx_latencycritical_activity = objc_msgSend (process_info,
       begin_activity_with_options_selector,
       NSActivityUserInitiatedAllowingIdleSystemSleep
       | NSActivityLatencyCritical,
       reason_string);
 }
 #endif
 #endif
-#endif
 
 namespace octave
 {
   // Disable all Qt messages by default.
 
   static void
 #if defined (QTMESSAGEHANDLER_ACCEPTS_QMESSAGELOGCONTEXT)
   message_handler (QtMsgType, const QMessageLogContext &, const QString &)
@@ -204,24 +202,22 @@ namespace octave
 
     // Register octave_value_list for connecting thread crossing signals.
 
     qRegisterMetaType<octave_value_list> ("octave_value_list");
 
 
 // Bug #55940 (Disable App Nap on Mac)
 #if defined (Q_OS_MAC)
-#ifdef __MAC_OS_X_VERSION_MAX_ALLOWED
-#if __MAC_OS_X_VERSION_MAX_ALLOWED < 101400
+#if HAVE_OBJCMSGSEND_OLDSTYLE
     // Mac App Nap feature causes pause() and sleep() to misbehave.
     // Disable it for the entire program run.
     disable_app_nap ();
 #endif
 #endif
-#endif
 
     // Force left-to-right alignment (see bug #46204)
     m_qapplication->setLayoutDirection (Qt::LeftToRight);
 
     connect (m_interpreter_qobj, SIGNAL (octave_finished_signal (int)),
              this, SLOT (handle_octave_finished (int)));
 
     connect (m_main_thread, SIGNAL (finished (void)),
diff --git a/m4/acinclude.m4 b/m4/acinclude.m4
--- a/m4/acinclude.m4
+++ b/m4/acinclude.m4
@@ -67,16 +67,43 @@ AC_DEFUN([OCTAVE_CARBON_CGDISPLAYBITSPER
     AC_LANG_POP(C++)
   ])
   if test $octave_cv_func_carbon_cgdisplaybitsperpixel = yes; then
     AC_DEFINE(HAVE_CARBON_CGDISPLAYBITSPERPIXEL, 1,
       [Define to 1 if Carbon Framework has CGDisplayBitsPerPixel.])
   fi
 ])
 dnl
+dnl Check if the old (pre-macOS-10.14) objc_msgSend signature works
+dnl
+AC_DEFUN([OCTAVE_CHECK_OLD_STYLE_OBJCMSGSEND], [
+  AC_CACHE_CHECK([whether the old-style objc_msgSend signature works],
+    [octave_cv_func_objcmsgsend_oldstyle],
+    [AC_LANG_PUSH(C++)
+    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
+        #include <objc/runtime.h>
+        #include <objc/message.h>
+        ]], [[
+        id test_string;
+        test_string = objc_msgSend (reinterpret_cast<id> (objc_getClass ("NSString")),
+                                      sel_getUid ("alloc"));
+        test_string = objc_msgSend (test_string,
+                                      sel_getUid ("initWithUTF8String:"),
+                                      "Hello, world!");
+      ]])],
+      octave_cv_func_objcmsgsend_oldstyle=yes,
+      octave_cv_func_objcmsgsend_oldstyle=no)
+    AC_LANG_POP(C++)
+  ])
+  if test $octave_cv_func_objcmsgsend_oldstyle = yes; then
+    AC_DEFINE(HAVE_OBJCMSGSEND_OLDSTYLE, 1,
+      [Define to 1 if Objective-C objc_msgSend signature is the old pre-10.14 style.])
+  fi
+])
+dnl
 dnl Check if C compiler handles FLAG command line option.  If two
 dnl arguments are specified, execute the second arg as shell commands.
 dnl Otherwise, add FLAG to CFLAGS if the compiler accepts the flag.
 dnl
 AC_DEFUN([OCTAVE_CC_FLAG], [
   ac_safe=`echo "$1" | $SED 'y% ./+-:=%___p___%'`
   AC_MSG_CHECKING([whether ${CC-cc} accepts $1])
   AC_CACHE_VAL([octave_cv_cc_flag_$ac_safe],
