1 /*
   2  * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "awt.h"
  27 #include <sun_awt_Win32GraphicsConfig.h>
  28 #include "awt_Win32GraphicsConfig.h"
  29 #include "awt_Canvas.h"
  30 #include "awt_Win32GraphicsDevice.h"
  31 #include "Devices.h"
  32 
  33 //Info for building a ColorModel
  34 #include "java_awt_image_DataBuffer.h"
  35 
  36 
  37 //Local utility functions
  38 static int shiftToMask(int numBits, int shift);
  39 
  40 /*
  41  * AwtWin32GraphicsConfig fields
  42  */
  43 
  44 jfieldID AwtWin32GraphicsConfig::win32GCVisualID;
  45 
  46 /*
  47  * Class:     sun_awt_Win32GraphicsConfig
  48  * Method:    initIDs
  49  * Signature: ()V
  50  */
  51 
  52 JNIEXPORT void JNICALL
  53 Java_sun_awt_Win32GraphicsConfig_initIDs
  54     (JNIEnv *env, jclass thisCls)
  55 {
  56     TRY;
  57     AwtWin32GraphicsConfig::win32GCVisualID = env->GetFieldID(thisCls,
  58          "visual", "I");
  59     DASSERT(AwtWin32GraphicsConfig::win32GCVisualID);
  60         CATCH_BAD_ALLOC;
  61 }
  62 
  63 /*
  64  *  shiftToMask:
  65  *  This function converts between cXXXBits and cXXXShift
  66  *  fields in the Windows GDI PIXELFORMATDESCRIPTOR and the mask
  67  *  fields passed to the DirectColorModel constructor.
  68  */
  69 inline int shiftToMask(int numBits, int shift) {
  70     int mask = 0xFFFFFFFF;
  71 
  72     //Shift in numBits 0s
  73     mask = mask << numBits;
  74     mask = ~mask;
  75     //shift left by value of shift
  76     mask = mask << shift;
  77     return mask;
  78 }
  79 
  80 /*
  81  * Class:     sun_awt_Win32GraphicsConfig
  82  * Method:    getBounds
  83  * Signature: ()Ljava/awt/Rectangle
  84  */
  85 JNIEXPORT jobject JNICALL
  86     Java_sun_awt_Win32GraphicsConfig_getBounds(JNIEnv *env, jobject thisobj,
  87                                                jint screen)
  88 {
  89     jclass clazz;
  90     jmethodID mid;
  91     jobject bounds = NULL;
  92 
  93     clazz = env->FindClass("java/awt/Rectangle");
  94     CHECK_NULL_RETURN(clazz, NULL);
  95     mid = env->GetMethodID(clazz, "<init>", "(IIII)V");
  96     if (mid != 0) {
  97         RECT rRW = {0, 0, 0, 0};
  98         Devices::InstanceAccess devices;
  99         AwtWin32GraphicsDevice *device = devices->GetDevice(screen);
 100 
 101         if (TRUE == MonitorBounds(AwtWin32GraphicsDevice::GetMonitor(screen), &rRW)) {
 102 
 103             int x = (device == NULL) ? rRW.left : device->ScaleDownX(rRW.left);
 104             int y = (device == NULL) ? rRW.top  : device->ScaleDownY(rRW.top);
 105             int w = (device == NULL) ? rRW.right - rRW.left
 106                                      : device->ScaleDownX(rRW.right - rRW.left);
 107             int h = (device == NULL) ? rRW.bottom - rRW.top
 108                                      : device->ScaleDownY(rRW.bottom - rRW.top);
 109 
 110             bounds = env->NewObject(clazz, mid, x, y, w, h);
 111 
 112         }
 113         else {
 114             // 4910760 - don't return a null bounds, return the bounds of the
 115             // primary screen
 116             int w = ::GetSystemMetrics(SM_CXSCREEN);
 117             int h = ::GetSystemMetrics(SM_CYSCREEN);
 118 
 119             bounds = env->NewObject(clazz, mid,
 120                                     0, 0,
 121                                     device == NULL ? w : device->ScaleDownX(w),
 122                                     device == NULL ? h : device->ScaleDownY(h));
 123         }
 124         if (safe_ExceptionOccurred(env)) {
 125            return 0;
 126         }
 127     }
 128     return bounds;
 129 }