1 /*
   2  * Copyright (c) 2013, 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 "glass_screen.h"
  27 #include "glass_general.h"
  28 
  29 #include <X11/Xatom.h>
  30 #include <gdk/gdk.h>
  31 #include <gdk/gdkx.h>
  32 
  33 static guint get_current_desktop(GdkScreen *screen) {
  34     Display* display = gdk_x11_display_get_xdisplay(gdk_display_get_default());
  35     Atom currentDesktopAtom = XInternAtom(display, "_NET_CURRENT_DESKTOP", True);
  36     guint ret = 0;
  37 
  38     Atom type;
  39     int format;
  40     gulong num, left;
  41     unsigned long *data = NULL;
  42 
  43     if (currentDesktopAtom == None) {
  44         return 0;
  45     }
  46 
  47     int result = XGetWindowProperty(display,
  48                                     GDK_WINDOW_XID(gdk_screen_get_root_window(screen)),
  49                                     currentDesktopAtom, 0, G_MAXLONG, False, XA_CARDINAL,
  50                                     &type, &format, &num, &left, (unsigned char **)&data);
  51 
  52     if ((result == Success) && (data != NULL)) {
  53         if (type == XA_CARDINAL && format == 32) {
  54             ret = data[0];
  55         }
  56 
  57         XFree(data);
  58     }
  59 
  60     return ret;
  61 
  62 }
  63 
  64 static GdkRectangle get_screen_workarea(GdkScreen *screen) {
  65     Display* display = gdk_x11_display_get_xdisplay(gdk_display_get_default());
  66     GdkRectangle ret = { 0, 0, gdk_screen_get_width(screen), gdk_screen_get_height(screen)};
  67 
  68     Atom workareaAtom = XInternAtom(display, "_NET_WORKAREA", True);
  69 
  70     Atom type;
  71     int format;
  72     gulong num, left;
  73     unsigned long *data = NULL;
  74 
  75     if (workareaAtom == None) {
  76         return ret;
  77     }
  78 
  79     int result = XGetWindowProperty(display,
  80                                     GDK_WINDOW_XID(gdk_screen_get_root_window(screen)),
  81                                     workareaAtom, 0, G_MAXLONG, False, AnyPropertyType,
  82                                     &type, &format, &num, &left, (unsigned char **)&data);
  83 
  84     if ((result == Success) && (data != NULL)) {
  85         if (type != None && format == 32) {
  86             guint current_desktop = get_current_desktop(screen);
  87             if (current_desktop < num / 4) {
  88                 ret.x = data[current_desktop * 4];
  89                 ret.y = data[current_desktop * 4 + 1];
  90                 ret.width = data[current_desktop * 4 + 2];
  91                 ret.height = data[current_desktop * 4 + 3];
  92             }
  93         }
  94 
  95         XFree(data);
  96     }
  97 
  98     return ret;
  99 
 100 }
 101 
 102 static jobject createJavaScreen(JNIEnv* env, GdkScreen* screen, gint monitor_idx)
 103 {
 104     GdkRectangle workArea = get_screen_workarea(screen);
 105     LOG4("Work Area: x:%d, y:%d, w:%d, h:%d\n", workArea.x, workArea.y, workArea.width, workArea.height);
 106 
 107     GdkRectangle monitor_geometry;
 108     gdk_screen_get_monitor_geometry(screen, monitor_idx, &monitor_geometry);
 109     LOG1("convert monitor[%d] -> glass Screen\n", monitor_idx)
 110     LOG4("[x: %d y: %d w: %d h: %d]\n",
 111          monitor_geometry.x, monitor_geometry.y,
 112          monitor_geometry.width, monitor_geometry.height)
 113 
 114     GdkVisual* visual = gdk_screen_get_system_visual(screen);
 115 
 116     GdkRectangle working_monitor_geometry;
 117     gdk_rectangle_intersect(&workArea, &monitor_geometry, &working_monitor_geometry);
 118 
 119     jobject jScreen = env->NewObject(jScreenCls, jScreenInit,
 120                                      (jlong)monitor_idx,
 121 
 122                                      visual ? visual->depth : 0,
 123 
 124                                      monitor_geometry.x,
 125                                      monitor_geometry.y,
 126                                      monitor_geometry.width,
 127                                      monitor_geometry.height,
 128 
 129                                      working_monitor_geometry.x,
 130                                      working_monitor_geometry.y,
 131                                      working_monitor_geometry.width,
 132                                      working_monitor_geometry.height,
 133 
 134                                      (jint)gdk_screen_get_resolution(screen),
 135                                      (jint)gdk_screen_get_resolution(screen),
 136                                      1.0f);
 137     JNI_EXCEPTION_TO_CPP(env);
 138     return jScreen;
 139 }
 140 
 141 jobject createJavaScreen(JNIEnv* env, gint monitor_idx) {
 142     GdkScreen *default_gdk_screen = gdk_screen_get_default();
 143     try {
 144         return createJavaScreen(env, default_gdk_screen, monitor_idx);
 145     } catch (jni_exception&) {
 146         return NULL;
 147     }
 148 }
 149 
 150 jobjectArray rebuild_screens(JNIEnv* env) {
 151     GdkScreen *default_gdk_screen = gdk_screen_get_default();
 152     gint n_monitors = gdk_screen_get_n_monitors(default_gdk_screen);
 153 
 154     jobjectArray jscreens = env->NewObjectArray(n_monitors, jScreenCls, NULL);
 155     JNI_EXCEPTION_TO_CPP(env)
 156     LOG1("Available monitors: %d\n", n_monitors)
 157 
 158     int i;
 159     for (i=0; i < n_monitors; i++) {
 160         env->SetObjectArrayElement(jscreens, i, createJavaScreen(env, default_gdk_screen, i));
 161         JNI_EXCEPTION_TO_CPP(env)
 162     }
 163 
 164     return jscreens;
 165 }
 166 
 167 
 168 glong getScreenPtrForLocation(gint x, gint y) {
 169     //Note: we are relying on the fact that javafx_screen_id == gdk_monitor_id
 170     return gdk_screen_get_monitor_at_point(gdk_screen_get_default(), x, y);
 171 }
 172 
 173 void screen_settings_changed(GdkScreen* screen, gpointer user_data) {
 174     (void)screen;
 175     (void)user_data;
 176 
 177     mainEnv->CallStaticVoidMethod(jScreenCls, jScreenNotifySettingsChanged);
 178     LOG_EXCEPTION(mainEnv);
 179 }