JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
X11GraphicsDevice.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010-2024 JogAmp Community. All rights reserved.
3 * Copyright (c) 2008-2009 Sun Microsystems, Inc. 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 * - Redistribution of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistribution 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 Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any kind. ALL
21 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
22 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
23 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
24 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
25 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
27 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
28 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
29 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
30 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
31 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 */
33
34package com.jogamp.nativewindow.x11;
35
36import jogamp.nativewindow.x11.X11Lib;
37import jogamp.nativewindow.x11.X11Util;
38
39import com.jogamp.nativewindow.DefaultGraphicsDevice;
40import com.jogamp.nativewindow.NativeWindowException;
41import com.jogamp.nativewindow.NativeWindowFactory;
42import com.jogamp.nativewindow.ToolkitLock;
43
44/** Encapsulates a graphics device on X11 platforms.
45 */
46
47public class X11GraphicsDevice extends DefaultGraphicsDevice implements Cloneable {
48 final boolean isXineramaEnabled;
49
50 /** Constructs a new X11GraphicsDevice corresponding to the given connection and default
51 * {@link com.jogamp.nativewindow.ToolkitLock} via {@link NativeWindowFactory#getDefaultToolkitLock(String)}.<br>
52 * Note that this is not an open connection, ie no native display handle exist.
53 * This constructor exist to setup a default device connection.
54 * @see DefaultGraphicsDevice#DefaultGraphicsDevice(String, String, int)
55 */
56 public X11GraphicsDevice(final String connection, final int unitID) {
58 setHandleOwnership(false);
59 isXineramaEnabled = false;
60 }
61
62 /** Constructs a new X11GraphicsDevice corresponding to the given native display handle and default
63 * {@link com.jogamp.nativewindow.ToolkitLock} via {@link NativeWindowFactory#getDefaultToolkitLock(String)}.
64 * @see DefaultGraphicsDevice#DefaultGraphicsDevice(String, String, int, long)
65 */
66 public X11GraphicsDevice(final long display, final int unitID, final boolean owner) {
68 }
69
70 /**
71 * @param display the Display connection
72 * @param locker custom {@link com.jogamp.nativewindow.ToolkitLock}, eg to force null locking w/ private connection
73 * @see DefaultGraphicsDevice#DefaultGraphicsDevice(String, String, int, long, ToolkitLock)
74 */
75 public X11GraphicsDevice(final long display, final int unitID, final ToolkitLock locker, final boolean owner) {
76 super(NativeWindowFactory.TYPE_X11, X11Lib.XDisplayString(display), unitID, display, locker);
77 if(0==display) {
78 throw new NativeWindowException("null display");
79 }
80 setHandleOwnership(owner);
81 isXineramaEnabled = X11Util.XineramaIsEnabled(this);
82 }
83
84 /**
85 * Constructs a new X11GraphicsDevice corresponding to the given display connection.
86 * <p>
87 * The constructor opens the native connection and takes ownership.
88 * </p>
89 * @param displayConnection the semantic display connection name
90 * @param locker custom {@link com.jogamp.nativewindow.ToolkitLock}, eg to force null locking w/ private connection
91 * @see DefaultGraphicsDevice#DefaultGraphicsDevice(String, String, int, long, ToolkitLock)
92 */
93 public X11GraphicsDevice(final String displayConnection, final int unitID, final ToolkitLock locker) {
94 super(NativeWindowFactory.TYPE_X11, displayConnection, unitID, 0, locker);
95 setHandleOwnership(true);
96 open();
97 isXineramaEnabled = X11Util.XineramaIsEnabled(this);
98 }
99
100 private static int getDefaultScreenImpl(final long dpy) {
101 return X11Lib.DefaultScreen(dpy);
102 }
103
104 /**
105 * Returns the default screen number as referenced by the display connection, i.e. 'somewhere:0.1' -> 1
106 * <p>
107 * Implementation uses the XLib macro <code>DefaultScreen(display)</code>.
108 * </p>
109 */
110 public int getDefaultScreen() {
111 final long display = getHandle();
112 if(0==display) {
113 throw new NativeWindowException("null display");
114 }
115 final int ds = getDefaultScreenImpl(display);
116 if(DEBUG) {
117 System.err.println(Thread.currentThread().getName() + " - X11GraphicsDevice.getDefaultDisplay() of "+this+": "+ds+", count "+X11Lib.ScreenCount(display));
118 }
119 return ds;
120 }
121
122 public int getDefaultVisualID() {
123 final long display = getHandle();
124 if(0==display) {
125 throw new NativeWindowException("null display");
126 }
127 return X11Lib.DefaultVisualID(display, getDefaultScreenImpl(display));
128 }
129
130 public final boolean isXineramaEnabled() {
131 return isXineramaEnabled;
132 }
133
134 @Override
135 public Object clone() {
136 return super.clone();
137 }
138
139 @Override
140 public boolean open() {
141 if(isHandleOwner() && 0 == handle) {
142 if(DEBUG) {
143 System.err.println(Thread.currentThread().getName() + " - X11GraphicsDevice.open(): "+this);
144 }
145 handle = X11Util.openDisplay(connection);
146 if(0 == handle) {
147 throw new NativeWindowException("X11GraphicsDevice.open() failed: "+this);
148 }
149 return true;
150 }
151 return false;
152 }
153
154 @Override
155 public boolean close() {
156 if(isHandleOwner() && 0 != handle) {
157 if(DEBUG) {
158 System.err.println(Thread.currentThread().getName() + " - X11GraphicsDevice.close(): "+this);
159 }
160 X11Util.closeDisplay(handle);
161 }
162 return super.close();
163 }
164
165 private boolean setHandleOwnership(final boolean v) {
166 final Boolean o = (Boolean)getHandleOwnership();
167 super.setHandleOwnership(v ? Boolean.valueOf(true) : null);
168 return null != o ? o.booleanValue() : false;
169 }
170}
final long getHandle()
Returns the native handle of the underlying native device, if such thing exist.
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
Provides a pluggable mechanism for arbitrary window toolkits to adapt their components to the NativeW...
static ToolkitLock getDefaultToolkitLock()
Provides the system default ToolkitLock for the default system windowing type.
static final String TYPE_X11
X11 type, as retrieved with getNativeWindowType(boolean).
Encapsulates a graphics device on X11 platforms.
int getDefaultScreen()
Returns the default screen number as referenced by the display connection, i.e.
boolean close()
Optionally closing the device if handle is not null.
X11GraphicsDevice(final String displayConnection, final int unitID, final ToolkitLock locker)
Constructs a new X11GraphicsDevice corresponding to the given display connection.
boolean open()
Optionally [re]opening the device if handle is null.
X11GraphicsDevice(final long display, final int unitID, final boolean owner)
Constructs a new X11GraphicsDevice corresponding to the given native display handle and default com....
X11GraphicsDevice(final long display, final int unitID, final ToolkitLock locker, final boolean owner)
X11GraphicsDevice(final String connection, final int unitID)
Constructs a new X11GraphicsDevice corresponding to the given connection and default com....
Marker for a singleton global recursive blocking lock implementation, optionally locking a native win...