IDEA-119858 IDEA with bundled JRE 7 uses discrete graphics card on Mac
authorDenis Fokin <Denis.Fokin@jetbrains.com>
Wed, 10 Dec 2014 10:03:32 +0000 (13:03 +0300)
committerDenis Fokin <Denis.Fokin@jetbrains.com>
Wed, 10 Dec 2014 10:06:16 +0000 (13:06 +0300)
bin/mac/libMacNativeKit64.dylib [new file with mode: 0644]
platform/platform-impl/src/com/intellij/openapi/wm/impl/IdeFrameImpl.java
platform/platform-impl/src/org/jetbrains/io/PowerSupplyKit.java [new file with mode: 0644]
platform/platform-impl/src/org/jetbrains/io/PowerSupplyKitCallback.java [new file with mode: 0644]
platform/util/resources/misc/registry.properties

diff --git a/bin/mac/libMacNativeKit64.dylib b/bin/mac/libMacNativeKit64.dylib
new file mode 100644 (file)
index 0000000..21c75b1
Binary files /dev/null and b/bin/mac/libMacNativeKit64.dylib differ
index fe288c82e5a6edaf2d9134456392b9cb7a34ce99..fe9e12d6bd6d0c7f398583050584e9366dff355a 100644 (file)
@@ -21,6 +21,10 @@ import com.intellij.ide.DataManager;
 import com.intellij.ide.impl.ProjectUtil;
 import com.intellij.ide.ui.UISettings;
 import com.intellij.ide.util.PropertiesComponent;
+import com.intellij.notification.Notification;
+import com.intellij.notification.NotificationListener;
+import com.intellij.notification.NotificationType;
+import com.intellij.notification.Notifications;
 import com.intellij.notification.impl.IdeNotificationArea;
 import com.intellij.openapi.Disposable;
 import com.intellij.openapi.MnemonicHelper;
@@ -41,6 +45,7 @@ import com.intellij.openapi.util.ActionCallback;
 import com.intellij.openapi.util.Disposer;
 import com.intellij.openapi.util.Key;
 import com.intellij.openapi.util.SystemInfo;
+import com.intellij.openapi.util.registry.Registry;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.wm.IdeFrame;
 import com.intellij.openapi.wm.IdeRootPaneNorthExtension;
@@ -56,8 +61,11 @@ import com.intellij.ui.mac.MacMainFrameDecorator;
 import com.intellij.util.ui.UIUtil;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
+import org.jetbrains.io.PowerSupplyKit;
+import org.jetbrains.io.PowerSupplyKitCallback;
 
 import javax.swing.*;
+import javax.swing.event.HyperlinkEvent;
 import java.awt.*;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
@@ -141,6 +149,57 @@ public class IdeFrameImpl extends JFrame implements IdeFrameEx, DataProvider {
 
   }
 
+  @Override
+  public void addNotify() {
+    super.addNotify();
+    checkPowerSupply();
+  }
+
+  private void checkPowerSupply() {
+
+    if (!shouldCheckDiscreteCard) return;
+
+    new Thread() {
+      @Override
+      public void run() {
+          PowerSupplyKit.startListenPowerSupply(new PowerSupplyKitCallback() {
+          @Override
+          public void call() {
+
+            final NotificationType type = NotificationType.INFORMATION;
+
+            final NotificationListener listener = new NotificationListener() {
+              @Override
+              public void hyperlinkUpdate(@NotNull Notification notification, @NotNull HyperlinkEvent event) {
+                notification.expire();
+              }
+            };
+
+            final String message =  "We have noticed that your computer power cord is disconnected and you are using" +
+                       " a discrete video card on  you MacBook Pro. You can switch " +
+                       " to the integrated video card. This significantly extend your battery life." +
+                       " <a href=\"doNotShow\">Do no show</a> this message anymore";
+
+            final Notification notification = new Notification("POWER_SUPPLY_GROUP_ID", "Discrete video card warning", message, type, listener);
+
+            ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
+              @Override
+              public void run() {
+                ApplicationManager.getApplication().getMessageBus().syncPublisher(Notifications.TOPIC).notify(notification);
+              }
+            });
+          }
+
+        });
+      }
+    }.start();
+
+    shouldCheckDiscreteCard = false;
+  }
+
+  private static boolean shouldCheckDiscreteCard = Registry.is("check.power.supply.for.mbp") && SystemInfo.isMacIntel64 && PowerSupplyKit.hasDiscreteCard();
+
+
   private void updateBorder() {
     int state = getExtendedState();
     if (!WindowManager.getInstance().isFullScreenSupportedInCurrentOS() || !SystemInfo.isWindows || myRootPane == null) {
diff --git a/platform/platform-impl/src/org/jetbrains/io/PowerSupplyKit.java b/platform/platform-impl/src/org/jetbrains/io/PowerSupplyKit.java
new file mode 100644 (file)
index 0000000..1bd338f
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2000-2014 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jetbrains.io;
+
+import com.intellij.util.lang.UrlClassLoader;
+
+
+public class PowerSupplyKit {
+
+    static {
+        UrlClassLoader.loadPlatformLibrary("MacNativeKit");
+    }
+
+    public static native void startListenPowerSupply (PowerSupplyKitCallback callback);
+    private static native String[] getInfo ();
+
+    public static boolean hasDiscreteCard() {
+        String [] models =  getInfo();
+
+        if (models.length > 1) {
+            return true;
+            //for (String model : models) {
+            //    if (model.contains("Radeon")) return true;
+            //}
+        }
+
+        return false;
+    }
+}
diff --git a/platform/platform-impl/src/org/jetbrains/io/PowerSupplyKitCallback.java b/platform/platform-impl/src/org/jetbrains/io/PowerSupplyKitCallback.java
new file mode 100644 (file)
index 0000000..4d138f8
--- /dev/null
@@ -0,0 +1,5 @@
+package org.jetbrains.io;
+
+public interface PowerSupplyKitCallback {
+    void call();
+}
index 936e2ab223751e68c5faa150bb893301c46cfae7..3ecf21d7ff54889b19beb59a1c0dfc0d39b7abec 100644 (file)
@@ -500,4 +500,7 @@ editor.config.stop.at.project.root=true
 editor.config.stop.at.project.root.description=Stops searching for .editorconfig at project root (requires project reopening)
 
 JDK8042508.bug.fixed=false
-JDK8042508.bug.fixed.description=Disable check for type variable until javac bug is fixed
\ No newline at end of file
+JDK8042508.bug.fixed.description=Disable check for type variable until javac bug is fixed
+
+check.power.supply.for.mbp=false
+check.power.supply.for.mbp.description=Check for discrete video card and power supply on MBPs
\ No newline at end of file