OSDN Git Service

965fb5a2342ff354f1d4278a5d0f0f10ac9f3894
[pf3gnuchains/gcc-fork.git] / libjava / classpath / native / jni / gtk-peer / gnu_java_awt_peer_gtk_GdkScreenGraphicsDevice.c
1 /* gnu_java_awt_peer_gtk_GdkScreenGraphicsDevice.c
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4    This file is part of GNU Classpath.
5
6    GNU Classpath is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    GNU Classpath is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GNU Classpath; see the file COPYING.  If not, write to the
18    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19    02110-1301 USA.
20
21    Linking this library statically or dynamically with other modules is
22    making a combined work based on this library.  Thus, the terms and
23    conditions of the GNU General Public License cover the whole
24    combination.
25
26    As a special exception, the copyright holders of this library give you
27    permission to link this library with independent modules to produce an
28    executable, regardless of the license terms of these independent
29    modules, and to copy and distribute the resulting executable under
30    terms of your choice, provided that you also meet, for each linked
31    independent module, the terms and conditions of the license of that
32    module.  An independent module is a module which is not derived from
33    or based on this library.  If you modify this library, you may extend
34    this exception to your version of the library, but you are not
35    obligated to do so.  If you do not wish to do so, delete this
36    exception statement from your version. */
37
38 #include <jcl.h>
39
40 #include <gdk/gdk.h>
41 #include <gdk/gdkx.h>
42
43 #ifdef HAVE_XRANDR
44 #include <X11/Xlib.h>
45 #include <X11/extensions/Xrandr.h>
46 #endif
47
48 #include "gdkdisplay.h"
49
50 #include "gnu_java_awt_peer_gtk_GdkScreenGraphicsDevice.h"
51
52 struct state_table *cp_gtk_native_screen_state_table;
53
54 jclass gdkScreenGraphicsDevice_class;
55
56 JNIEXPORT void JNICALL
57 Java_gnu_java_awt_peer_gtk_GdkScreenGraphicsDevice_initStaticState
58 (JNIEnv *env, jclass klazz)
59 {
60         gdkScreenGraphicsDevice_class = (*env)->NewGlobalRef
61     (env, klazz);
62
63         NSA_SCREEN_INIT(env, gdkScreenGraphicsDevice_class);
64 }
65
66 JNIEXPORT jobject JNICALL
67 Java_gnu_java_awt_peer_gtk_GdkScreenGraphicsDevice_nativeGetFixedDisplayMode
68 (JNIEnv *env, jobject obj, jobject gdkGraphicsEnv __attribute__((unused)))
69 {
70         jclass displayMode_class;
71         jmethodID displayMode_ctor;
72         GdkScreen *screen;
73         jobject fixedDisplayMode = NULL;
74 #ifdef HAVE_XRANDR
75         int temp1, temp2;
76         GdkDisplay *display;
77
78     display = (GdkDisplay *) NSA_GET_DISPLAY_PTR(env, gdkGraphicsEnv);
79
80         gdk_threads_enter();
81
82         if (!XRRQueryExtension(GDK_DISPLAY_XDISPLAY(display), &temp1, &temp2))
83           {
84         displayMode_class = (*env)->FindClass(env, "java/awt/DisplayMode");
85         displayMode_ctor = (*env)->GetMethodID(env,
86                                                displayMode_class,
87                                                "<init>",
88                                                "(IIII)V");
89
90         screen = (GdkScreen *) NSA_GET_SCREEN_PTR(env, obj);
91                 
92                 fixedDisplayMode = (*env)->NewObject(env,
93                                                      displayMode_class,
94                                                      displayMode_ctor,
95                                                      gdk_screen_get_width(screen),
96                                                      gdk_screen_get_height(screen),
97                                                      -1,
98                                                      0);
99           }
100
101         gdk_threads_leave();
102           
103 #else
104
105     displayMode_class = (*env)->FindClass(env, "java/awt/DisplayMode");
106     displayMode_ctor = (*env)->GetMethodID(env,
107                                            displayMode_class,
108                                            "<init>",
109                                            "(IIII)V");
110
111     screen = (GdkScreen *) NSA_GET_SCREEN_PTR(env, obj);
112                 
113     fixedDisplayMode = (*env)->NewObject(env,
114                                              displayMode_class,
115                                              displayMode_ctor,
116                                              gdk_screen_get_width(screen),
117                                              gdk_screen_get_height(screen),
118                                              -1,
119                                              0);
120
121 #endif    
122         return fixedDisplayMode;
123 }
124
125 JNIEXPORT jstring JNICALL
126 Java_gnu_java_awt_peer_gtk_GdkScreenGraphicsDevice_nativeGetIDString
127 (JNIEnv *env, jobject obj)
128 {
129         GdkScreen *screen;
130         gchar* displayName;
131         jstring string;
132
133     screen = (GdkScreen *) NSA_GET_SCREEN_PTR(env, obj);
134         
135         gdk_threads_enter();
136         
137         displayName = gdk_screen_make_display_name(screen);
138         
139         gdk_threads_leave();
140         
141     string = (*env)->NewStringUTF(env, displayName);
142     
143     g_free(displayName);
144     
145     return string;
146 }
147
148 JNIEXPORT jint JNICALL
149 Java_gnu_java_awt_peer_gtk_GdkScreenGraphicsDevice_nativeGetDisplayModeRate
150 (JNIEnv *env, jobject obj __attribute__((unused)), jobject gdkGraphicsEnv __attribute__((unused)))
151 {
152 #ifdef HAVE_XRANDR
153
154         GdkDisplay *display;
155         XRRScreenConfiguration *config;
156         int rate;
157         
158         display = (GdkDisplay *) NSA_GET_DISPLAY_PTR(env, gdkGraphicsEnv);
159         
160         gdk_threads_enter();
161         
162         config = XRRGetScreenInfo (GDK_DISPLAY_XDISPLAY(display), GDK_ROOT_WINDOW());
163
164         rate = (int) XRRConfigCurrentRate (config);
165
166     XRRFreeScreenConfigInfo (config);
167
168         gdk_threads_leave();
169         
170         return rate;
171 #else
172     JCL_ThrowException(env,
173                        "java/lang/InternalError",
174                        "Method should not have been invoked.");
175     
176     return -1;
177 #endif    
178 }
179
180 JNIEXPORT jint JNICALL
181 Java_gnu_java_awt_peer_gtk_GdkScreenGraphicsDevice_nativeGetDisplayModeIndex
182 (JNIEnv *env, jobject obj __attribute__((unused)), jobject gdkGraphicsEnv __attribute__((unused)))
183 {
184 #ifdef HAVE_XRANDR      
185
186         GdkDisplay *display;
187         XRRScreenConfiguration *config;
188         SizeID index;
189         Rotation rotation;
190         
191         display = (GdkDisplay *) NSA_GET_DISPLAY_PTR(env, gdkGraphicsEnv);
192         
193         gdk_threads_enter();
194         
195         config = XRRGetScreenInfo (GDK_DISPLAY_XDISPLAY(display), GDK_ROOT_WINDOW());
196
197         index = XRRConfigCurrentConfiguration (config, &rotation);
198
199     XRRFreeScreenConfigInfo (config);
200
201         gdk_threads_leave();
202         
203         return (int) index;
204
205 #else
206
207     JCL_ThrowException(env,
208                        "java/lang/InternalError",
209                        "Method should not have been invoked.");
210    
211     return -1;
212    
213 #endif  
214 }
215
216 JNIEXPORT jobjectArray JNICALL
217 Java_gnu_java_awt_peer_gtk_GdkScreenGraphicsDevice_nativeGetDisplayModes
218 (JNIEnv *env, jobject obj __attribute__((unused)), jobject gdkGraphicsEnv __attribute__((unused)))
219 {
220 #ifdef HAVE_XRANDR
221         GdkDisplay *display;
222         XRRScreenConfiguration *config;
223         XRRScreenSize *screenSizes;
224         int nsizes = 0, nrates = 0, i = 0;
225         jclass x11DisplayMode_class;
226         jmethodID x11DisplayMode_ctor;
227         jobjectArray array;
228         jobject instance;
229         short *rates;
230         jshortArray shortArray;
231         
232         display = (GdkDisplay *) NSA_GET_DISPLAY_PTR(env, gdkGraphicsEnv);
233         
234         gdk_threads_enter();
235         
236         config = XRRGetScreenInfo (GDK_DISPLAY_XDISPLAY(display), GDK_ROOT_WINDOW());
237         
238         screenSizes = XRRConfigSizes(config, &nsizes);
239         
240         x11DisplayMode_class = (*env)->FindClass(env, "gnu/java/awt/peer/gtk/GdkScreenGraphicsDevice$X11DisplayMode");
241     
242         x11DisplayMode_ctor = (*env)->GetMethodID(env, x11DisplayMode_class, "<init>", "(II[S)V");
243
244         array = (*env)->NewObjectArray(env, nsizes, x11DisplayMode_class, NULL);
245
246         for (; i < nsizes ; i++)
247           {
248                 /* Retrieves refresh rate information. */
249                 rates = XRRConfigRates(config, i, &nrates);
250                 
251                 /* Create a Java int array and put them in. */
252                 shortArray = (*env)->NewIntArray(env, nrates);
253                 (*env)->SetShortArrayRegion(env, shortArray, 0, nrates, (jshort *) rates);
254                 
255                 /* Create a GdkScreenGraphicsDevice.X11DisplayMode instance. */
256         instance = (*env)->NewObject(env,
257                                                                          x11DisplayMode_class,
258                                                                          x11DisplayMode_ctor,
259                                                                          screenSizes[i].width,
260                                                                          screenSizes[i].height,
261                                                                          shortArray);
262                                                                          
263                 /* Put it into the result array. */
264         (*env)->SetObjectArrayElement(env, array, i, instance);
265           }
266           
267         /* Free everything acquired by xlib. */
268         XRRFreeScreenConfigInfo (config);
269
270         gdk_threads_leave();
271         
272         return array;
273 #else
274     JCL_ThrowException(env,
275                        "java/lang/InternalError",
276                        "Method should not have been invoked.");
277     
278     return NULL;
279    
280 #endif  
281 }
282
283 JNIEXPORT void JNICALL
284 Java_gnu_java_awt_peer_gtk_GdkScreenGraphicsDevice_nativeSetDisplayMode
285 (JNIEnv *env, jobject obj __attribute__((unused)), jobject gdkGraphicsEnv __attribute__((unused)), jint index __attribute__((unused)), jshort rate __attribute__((unused)))
286 {
287 #ifdef HAVE_XRANDR
288         GdkDisplay *display;
289         XRRScreenConfiguration *config;
290         Rotation rotation;
291         
292         display = (GdkDisplay *) NSA_GET_DISPLAY_PTR(env, gdkGraphicsEnv);
293         
294         gdk_threads_enter();
295         
296         config = XRRGetScreenInfo (GDK_DISPLAY_XDISPLAY(display), GDK_ROOT_WINDOW());
297
298         /* The rotation is not exposed to the Java API. So we retrieve its current
299          * value and set it to the same when switching resolution.
300          */
301         XRRConfigCurrentConfiguration (config, &rotation);
302         
303         XRRSetScreenConfigAndRate (GDK_DISPLAY_XDISPLAY(display),
304                                            config,
305                                GDK_ROOT_WINDOW(),
306                                index,
307                                rotation,
308                                rate,
309                                CurrentTime);
310         
311         XRRFreeScreenConfigInfo(config);
312         
313         gdk_threads_leave();
314
315 #else
316     JCL_ThrowException(env,
317                        "java/lang/InternalError",
318                        "Method should not have been invoked.");
319 #endif  
320 }
321
322 JNIEXPORT jobject JNICALL
323 Java_gnu_java_awt_peer_gtk_GdkScreenGraphicsDevice_nativeGetBounds
324 (JNIEnv *env, jobject obj)
325 {
326         jclass rectangle_class;
327         jmethodID rectangle_ctor;
328         GdkScreen *screen;
329         GdkWindow *window;
330         int x, y, w, h;
331         jobject instance;
332     
333         rectangle_class = (*env)->FindClass(env, "java/awt/Rectangle");
334     
335     rectangle_ctor = (*env)->GetMethodID 
336     (env, rectangle_class, "<init>", "(IIII)V");
337
338     screen = (GdkScreen *) NSA_GET_SCREEN_PTR(env, obj);
339
340         gdk_threads_enter();
341         
342         window = gdk_screen_get_root_window(screen);
343         
344         gdk_window_get_geometry(window, &x, &y, &w, &h, NULL);
345
346         gdk_threads_leave();
347         
348     instance = (*env)->NewObject(env,
349                                                                  rectangle_class,
350                                                                  rectangle_ctor,
351                                                                  x, y, w, h);
352         
353         return instance;
354 }