1 /*
   2  * Copyright (c) 2013, 2015, 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 ? gdk_visual_get_depth(visual) : 0),
 123 
 124                                      monitor_geometry.x,
 125                                      monitor_geometry.y,
 126                                      monitor_geometry.width,
 127                                      monitor_geometry.height,
 128 
 129                                      monitor_geometry.x,
 130                                      monitor_geometry.y,
 131                                      monitor_geometry.width,
 132                                      monitor_geometry.height,
 133 
 134                                      working_monitor_geometry.x,
 135                                      working_monitor_geometry.y,
 136                                      working_monitor_geometry.width,
 137                                      working_monitor_geometry.height,
 138 
 139                                      (jint)gdk_screen_get_resolution(screen),
 140                                      (jint)gdk_screen_get_resolution(screen),
 141                                      1.0f, 1.0f, 1.0f, 1.0f);
 142     JNI_EXCEPTION_TO_CPP(env);
 143     return jScreen;
 144 }
 145 
 146 jobject createJavaScreen(JNIEnv* env, gint monitor_idx) {
 147     GdkScreen *default_gdk_screen = gdk_screen_get_default();
 148     try {
 149         return createJavaScreen(env, default_gdk_screen, monitor_idx);
 150     } catch (jni_exception&) {
 151         return NULL;
 152     }
 153 }
 154 
 155 jobjectArray rebuild_screens(JNIEnv* env) {
 156     GdkScreen *default_gdk_screen = gdk_screen_get_default();
 157     gint n_monitors = gdk_screen_get_n_monitors(default_gdk_screen);
 158 
 159     jobjectArray jscreens = env->NewObjectArray(n_monitors, jScreenCls, NULL);
 160     JNI_EXCEPTION_TO_CPP(env)
 161     LOG1("Available monitors: %d\n", n_monitors)
 162 
 163     int i;
 164     for (i=0; i < n_monitors; i++) {
 165         env->SetObjectArrayElement(jscreens, i, createJavaScreen(env, default_gdk_screen, i));
 166         JNI_EXCEPTION_TO_CPP(env)
 167     }
 168 
 169     return jscreens;
 170 }
 171 
 172 
 173 glong getScreenPtrForLocation(gint x, gint y) {
 174     //Note: we are relying on the fact that javafx_screen_id == gdk_monitor_id
 175     return gdk_screen_get_monitor_at_point(gdk_screen_get_default(), x, y);
 176 }
 177 
 178 void screen_settings_changed(GdkScreen* screen, gpointer user_data) {
 179     (void)screen;
 180     (void)user_data;
 181 
 182     mainEnv->CallStaticVoidMethod(jScreenCls, jScreenNotifySettingsChanged);
 183     LOG_EXCEPTION(mainEnv);
 184 }