1 /*
2  * Copyright (c) 2002-2008 LWJGL Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'LWJGL' nor the names of
17  *   its contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.lwjgl.opengl;
33 
34 import java.awt.GraphicsConfiguration;
35 import java.awt.GraphicsDevice;
36 import java.awt.Canvas;
37 import java.lang.reflect.Method;
38 import java.security.AccessController;
39 import java.security.PrivilegedExceptionAction;
40 
41 import org.lwjgl.LWJGLException;
42 import org.lwjgl.LWJGLUtil;
43 
44 /**
45  *
46  * @author elias_naur <elias_naur@users.sourceforge.net>
47  * @version $Revision$
48  * $Id$
49  */
50 final class LinuxCanvasImplementation implements AWTCanvasImplementation {
getScreenFromDevice(final GraphicsDevice device)51 	static int getScreenFromDevice(final GraphicsDevice device) throws LWJGLException {
52 		try {
53 			Method getScreen_method = AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() {
54 				public Method run() throws Exception {
55 					return device.getClass().getMethod("getScreen");
56 				}
57 			});
58 			Integer screen = (Integer)getScreen_method.invoke(device);
59 			return screen;
60 		} catch (Exception e) {
61 			throw new LWJGLException(e);
62 		}
63 	}
64 
getVisualIDFromConfiguration(final GraphicsConfiguration configuration)65 	private static int getVisualIDFromConfiguration(final GraphicsConfiguration configuration) throws LWJGLException {
66 		try {
67 			Method getVisual_method = AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() {
68 				public Method run() throws Exception {
69 					return configuration.getClass().getMethod("getVisual");
70 				}
71 			});
72 			Integer visual = (Integer)getVisual_method.invoke(configuration);
73 			return visual;
74 		} catch (Exception e) {
75 			throw new LWJGLException(e);
76 		}
77 	}
78 
createPeerInfo(Canvas component, PixelFormat pixel_format, ContextAttribs attribs)79 	public PeerInfo createPeerInfo(Canvas component, PixelFormat pixel_format, ContextAttribs attribs) throws LWJGLException {
80 		return new LinuxAWTGLCanvasPeerInfo(component);
81 	}
82 
83 	/**
84 	 * Find a proper GraphicsConfiguration from the given GraphicsDevice and PixelFormat.
85 	 *
86 	 * @return The GraphicsConfiguration corresponding to a visual that matches the pixel format.
87 	 */
findConfiguration(GraphicsDevice device, PixelFormat pixel_format)88 	public GraphicsConfiguration findConfiguration(GraphicsDevice device, PixelFormat pixel_format) throws LWJGLException {
89 		try {
90 			int screen = getScreenFromDevice(device);
91 			int visual_id_matching_format = findVisualIDFromFormat(screen, pixel_format);
92 			GraphicsConfiguration[] configurations = device.getConfigurations();
93 			for ( GraphicsConfiguration configuration : configurations ) {
94 				int visual_id = getVisualIDFromConfiguration(configuration);
95 				if ( visual_id == visual_id_matching_format )
96 					return configuration;
97 			}
98 		} catch (LWJGLException e) {
99 			LWJGLUtil.log("Got exception while trying to determine configuration: " + e);
100 		}
101 		return null; // In case we failed to locate the visual, or if we're not on a SUN JDK
102 	}
103 
findVisualIDFromFormat(int screen, PixelFormat pixel_format)104 	private static int findVisualIDFromFormat(int screen, PixelFormat pixel_format) throws LWJGLException {
105 		try {
106 			LinuxDisplay.lockAWT();
107 			try {
108 				GLContext.loadOpenGLLibrary();
109 				try {
110 					LinuxDisplay.incDisplay();
111 					return nFindVisualIDFromFormat(LinuxDisplay.getDisplay(), screen, pixel_format);
112 				} finally {
113 					LinuxDisplay.decDisplay();
114 				}
115 			} finally {
116 				GLContext.unloadOpenGLLibrary();
117 			}
118 		} finally {
119 			LinuxDisplay.unlockAWT();
120 		}
121 	}
nFindVisualIDFromFormat(long display, int screen, PixelFormat pixel_format)122 	private static native int nFindVisualIDFromFormat(long display, int screen, PixelFormat pixel_format) throws LWJGLException;
123 }
124