summaryrefslogtreecommitdiffstats
path: root/src/nativewindow/classes/jogamp/nativewindow/x11/X11Util.java
blob: 117184a712ec490f6726ed37ab5e9536e457f15f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
 * Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved.
 * Copyright (c) 2010 JogAmp Community. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 */

package jogamp.nativewindow.x11;

import com.jogamp.common.util.LongObjectHashMap;
import jogamp.nativewindow.Debug;
import jogamp.nativewindow.NWJNILibLoader;

import javax.media.nativewindow.*;

import java.nio.Buffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import java.security.AccessController;
import java.util.ArrayList;
import java.util.List;
import javax.media.nativewindow.util.Point;

/**
 * Contains a thread safe X11 utility to retrieve display connections.
 */
public class X11Util {
    /**
     * 2011/06/14 libX11 1.4.2 and libxcb 1.7 bug 20708 - Multithreading Issues w/ OpenGL, ..
     *            https://bugs.freedesktop.org/show_bug.cgi?id=20708
     *            https://jogamp.org/bugzilla/show_bug.cgi?id=502
     *            Affects: Ubuntu 11.04, OpenSuSE 11, ..
     * If the property <b>nativewindow.x11.mt-bug</b> is set to true, extensive X11 locking 
     * is being applied, avoiding X11 multithreading capabilities. 
     */
    public static final boolean MULTITHREADING_BUG = Debug.getBooleanProperty("nativewindow.x11.mt-bug", true, AccessController.getContext());
    public static final boolean XINITTHREADS_ALWAYS_ENABLED = true;

    private static final boolean DEBUG = Debug.debug("X11Util");
    private static final boolean TRACE_DISPLAY_LIFECYCLE = Debug.getBooleanProperty("nativewindow.debug.X11Util.TraceDisplayLifecycle", true, AccessController.getContext());

    private static volatile String nullDisplayName = null;
    private static boolean requiresX11Lock = false;
    private static boolean isInit = false;

    private static int setX11ErrorHandlerRecCount = 0;
    private static Object setX11ErrorHandlerLock = new Object();

    
    public static synchronized void initSingleton(final boolean firstX11ActionOnProcess) {
        if(!isInit) {
            NWJNILibLoader.loadNativeWindow("x11");

            /**
             * Always issue XInitThreads() since we have independent
             * off-thread created Display connections able to utilize multithreading, 
             * ie NEWT (jogamp.newt.x11.X11Display.createNativeImpl()) !!
             */
            initialize0( XINITTHREADS_ALWAYS_ENABLED ? true : firstX11ActionOnProcess );

            requiresX11Lock = !firstX11ActionOnProcess || MULTITHREADING_BUG;
            
            if(DEBUG) {
                System.out.println("X11Util firstX11ActionOnProcess: "+firstX11ActionOnProcess+
                                   ", XINITTHREADS_ALWAYS_ENABLED "+XINITTHREADS_ALWAYS_ENABLED+
                                   ", MULTITHREADING_BUG "+MULTITHREADING_BUG+
                                   ", requiresX11Lock "+requiresX11Lock); 
            }
            isInit = true;
        }
    }

    public static void setX11ErrorHandler(boolean onoff, boolean quiet) {
        synchronized(setX11ErrorHandlerLock) {
            if(onoff) {
                if(0==setX11ErrorHandlerRecCount) {
                    setX11ErrorHandler0(true, quiet);
                }
                setX11ErrorHandlerRecCount++;
            } else {
                if(0 >= setX11ErrorHandlerRecCount) {
                    throw new InternalError();
                }
                setX11ErrorHandlerRecCount--;
                if(0==setX11ErrorHandlerRecCount) {
                    setX11ErrorHandler0(false, false);
                }
            }
        }
    }

    public static boolean requiresToolkitLock() {
        return requiresX11Lock;
    }

    public static void lockDefaultToolkit(long dpyHandle) {
        NativeWindowFactory.getDefaultToolkitLock().lock();
        if(requiresX11Lock) {
            X11Util.XLockDisplay(dpyHandle);
        }
    }

    public static void unlockDefaultToolkit(long dpyHandle) {
        if(requiresX11Lock) {
            X11Util.XUnlockDisplay(dpyHandle);
        }
        NativeWindowFactory.getDefaultToolkitLock().unlock();
    }

    public static String getNullDisplayName() {
        if(null==nullDisplayName) { // volatile: ok
            synchronized(X11Util.class) {
                if(null==nullDisplayName) {
                    NativeWindowFactory.getDefaultToolkitLock().lock();
                    long dpy = X11Lib.XOpenDisplay(null);
                    try {
                        nullDisplayName = X11Lib.XDisplayString(dpy);
                    } finally {
                        X11Lib.XCloseDisplay(dpy);
                        NativeWindowFactory.getDefaultToolkitLock().unlock();
                    }
                    if(DEBUG) {
                        System.out.println("X11 Display(NULL) <"+nullDisplayName+">");
                    }
                }
            }
        }
        return nullDisplayName;
    }

    private X11Util() {}

    // not exactly thread safe, but good enough for our purpose,
    // which is to tag a NamedDisplay uncloseable after creation.
    private static Object globalLock = new Object(); 
    private static LongObjectHashMap globalNamedDisplayMap = new LongObjectHashMap();
    private static List openDisplayList = new ArrayList();
    private static List pendingDisplayList = new ArrayList();

    public static class NamedDisplay {
        String name;
        long   handle;
        int    refCount;
        boolean unCloseable;
        Throwable creationStack;

        protected NamedDisplay(String name, long handle) {
            this.name=name;
            this.handle=handle;
            this.refCount=1;
            this.unCloseable=false;
            if(DEBUG) {
                this.creationStack=new Throwable("NamedDisplay Created at:");
            } else {
                this.creationStack=null;
            }
        }

        public final String getName() { return name; }
        public final long   getHandle() { return handle; }
        public final int    getRefCount() { return refCount; }

        public final void setUncloseable(boolean v) { unCloseable = v; }
        public final boolean isUncloseable() { return unCloseable; }

        public final Throwable getCreationStack() { return creationStack; }

        @Override
        public Object clone() throws CloneNotSupportedException {
          return super.clone();
        }

        @Override
        public String toString() {
            return "NamedX11Display["+name+", 0x"+Long.toHexString(handle)+", refCount "+refCount+", unCloseable "+unCloseable+"]";
        }
    }

    /** Returns the number of unclosed X11 Displays.
     * @param realXCloseOpenAndPendingDisplays if true, {@link #closePendingDisplayConnections()} is called.
      */
    public static int shutdown(boolean realXCloseOpenAndPendingDisplays, boolean verbose) {
        int num=0;
        if(DEBUG||verbose||pendingDisplayList.size() > 0) {
            String msg = "X11Util.Display: Shutdown (close open / pending Displays: "+realXCloseOpenAndPendingDisplays+
                         ", open (no close attempt): "+globalNamedDisplayMap.size()+"/"+openDisplayList.size()+
                         ", open (no close attempt and uncloseable): "+pendingDisplayList.size()+")" ;
            if(DEBUG) {
                Exception e = new Exception(msg);
                e.printStackTrace();
            } else {
                System.err.println(msg);
            }
            if( openDisplayList.size() > 0) {
                X11Util.dumpOpenDisplayConnections();
            }
            if( pendingDisplayList.size() > 0 ) {
                X11Util.dumpPendingDisplayConnections();
            }
        }

        synchronized(globalLock) {
            if(realXCloseOpenAndPendingDisplays) {
                closePendingDisplayConnections();    
            }
            openDisplayList.clear();
            pendingDisplayList.clear();
            globalNamedDisplayMap.clear();
        }
        return num;
    }

    /**
     * Closing pending Display connections in reverse order.
     *
     * @return number of closed Display connections
     */
    public static int closePendingDisplayConnections() {
        int num=0;
        synchronized(globalLock) {
            if(DEBUG) {
                System.err.println("X11Util: Closing Pending X11 Display Connections: "+pendingDisplayList.size());
            }
            for(int i=pendingDisplayList.size()-1; i>=0; i--) {
                NamedDisplay ndpy = (NamedDisplay) pendingDisplayList.get(i);
                if(DEBUG) {
                    System.err.println("X11Util.closePendingDisplayConnections(): Closing ["+i+"]: "+ndpy);
                }
                XCloseDisplay(ndpy.getHandle());
                num++;
            }
        }
        return num;
    }

    public static int getOpenDisplayConnectionNumber() {
        synchronized(globalLock) {
            return openDisplayList.size();
        }
    }
        
    public static void dumpOpenDisplayConnections() {
        synchronized(globalLock) {
            System.err.println("X11Util: Open X11 Display Connections: "+openDisplayList.size());
            for(int i=0; i<pendingDisplayList.size(); i++) {
                NamedDisplay ndpy = (NamedDisplay) openDisplayList.get(i);
                System.err.println("X11Util: ["+i+"]: "+ndpy);
                if(null!=ndpy) {
                    Throwable t = ndpy.getCreationStack();
                    if(null!=t) {
                        t.printStackTrace();
                    }
                }
            }
        }
    }

    public static int getPendingDisplayConnectionNumber() {
        synchronized(globalLock) {
            return pendingDisplayList.size();
        }
    }

    public static void dumpPendingDisplayConnections() {
        synchronized(globalLock) {
            System.err.println("X11Util: Pending X11 Display Connections: "+pendingDisplayList.size());
            for(int i=0; i<pendingDisplayList.size(); i++) {
                NamedDisplay ndpy = (NamedDisplay) pendingDisplayList.get(i);
                System.err.println("X11Util: ["+i+"]: "+ndpy);
                if(null!=ndpy) {
                    Throwable t = ndpy.getCreationStack();
                    if(null!=t) {
                        t.printStackTrace();
                    }
                }
            }
        }
    }

    public static boolean markDisplayUncloseable(long handle) {
        NamedDisplay ndpy;
        synchronized(globalLock) {
            ndpy = (NamedDisplay) globalNamedDisplayMap.get(handle);
        }
        if( null != ndpy ) {
            ndpy.setUncloseable(true);
            return true;
        }
        return false;
    }

    /** Returns this created named display. */
    public static long createDisplay(String name) {
        name = validateDisplayName(name);
        long dpy = XOpenDisplay(name);
        if(0==dpy) {
            throw new NativeWindowException("X11Util.Display: Unable to create a display("+name+") connection. Thread "+Thread.currentThread().getName());
        }
        // if you like to debug and synchronize X11 commands ..
        // setSynchronizeDisplay(dpy, true);
        NamedDisplay namedDpy = new NamedDisplay(name, dpy);
        synchronized(globalLock) {
            globalNamedDisplayMap.put(dpy, namedDpy);
            openDisplayList.add(namedDpy);
            pendingDisplayList.add(namedDpy);
        }
        if(DEBUG) {
            Exception e = new Exception("X11Util.Display: Created new "+namedDpy+". Thread "+Thread.currentThread().getName());
            e.printStackTrace();
        }
        return namedDpy.getHandle();
    }

    public static void closeDisplay(long handle) {
        NamedDisplay namedDpy;

        synchronized(globalLock) {
            namedDpy = (NamedDisplay) globalNamedDisplayMap.remove(handle);
            if(namedDpy!=null) {
                if(!openDisplayList.remove(namedDpy)) { throw new RuntimeException("Internal: "+namedDpy); }
            }
        }
        if(null==namedDpy) {
            X11Util.dumpPendingDisplayConnections();
            throw new RuntimeException("X11Util.Display: Display(0x"+Long.toHexString(handle)+") with given handle is not mapped. Thread "+Thread.currentThread().getName());
        }
        if(namedDpy.getHandle()!=handle) {
            X11Util.dumpPendingDisplayConnections();
            throw new RuntimeException("X11Util.Display: Display(0x"+Long.toHexString(handle)+") Mapping error: "+namedDpy+". Thread "+Thread.currentThread().getName());
        }

        if(DEBUG) {
            Exception e = new Exception("X11Util.Display: Closing new "+namedDpy+". Thread "+Thread.currentThread().getName());
            e.printStackTrace();
        }

        if(!namedDpy.isUncloseable()) {
            synchronized(globalLock) {
                if(!pendingDisplayList.remove(namedDpy)) { throw new RuntimeException("Internal: "+namedDpy); }
            }
            XCloseDisplay(namedDpy.getHandle());
        }
    }

    public static NamedDisplay getNamedDisplay(long handle) {
        synchronized(globalLock) {
            return (NamedDisplay) globalNamedDisplayMap.get(handle);
        }
    }

    /** 
     * @return If name is null, it returns the previous queried NULL display name,
     * otherwise the name. */
    public static String validateDisplayName(String name) {
        return ( null == name || AbstractGraphicsDevice.DEFAULT_CONNECTION.equals(name) ) ? getNullDisplayName() : name ;
    }

    public static String validateDisplayName(String name, long handle) {
        if( ( null==name || AbstractGraphicsDevice.DEFAULT_CONNECTION.equals(name) ) && 0!=handle) {
            name = XDisplayString(handle);
        }
        return validateDisplayName(name);
    }

    /*******************************
     **
     ** Locked X11Lib wrapped functions
     **
     *******************************/

    public static long XOpenDisplay(String arg0) {
        NativeWindowFactory.getDefaultToolkitLock().lock();
        try {
            long handle = X11Lib.XOpenDisplay(arg0);
            if(TRACE_DISPLAY_LIFECYCLE) {
                Throwable t = new Throwable(Thread.currentThread()+" - X11Util.XOpenDisplay("+arg0+") 0x"+Long.toHexString(handle));
                t.printStackTrace();
            }
            return handle;
        } finally {
            NativeWindowFactory.getDefaultToolkitLock().unlock();
        }
    }

    public static int XCloseDisplay(long display) {
        NativeWindowFactory.getDefaultToolkitLock().lock();
        try {
            if(TRACE_DISPLAY_LIFECYCLE) {
                Throwable t = new Throwable(Thread.currentThread()+" - X11Util.XCloseDisplay() 0x"+Long.toHexString(display));
                t.printStackTrace();
            }
            return X11Lib.XCloseDisplay(display);
        } finally {
            NativeWindowFactory.getDefaultToolkitLock().unlock();
        }
    }

    public static int XFree(Buffer arg0)  {
        NativeWindowFactory.getDefaultToolkitLock().lock();
        try {
            return X11Lib.XFree(arg0);
        } finally {
            NativeWindowFactory.getDefaultToolkitLock().unlock();
        }
    }

    public static int XSync(long display, boolean discard) {
        lockDefaultToolkit(display);
        try {
            return X11Lib.XSync(display, discard);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static void XSynchronize(long display, boolean onoff) {
        lockDefaultToolkit(display);
        try {
            X11Lib.XSynchronize(display, onoff);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static boolean XineramaEnabled(long display) {
        lockDefaultToolkit(display);
        try {
            return X11Lib.XineramaEnabled(display);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static int DefaultScreen(long display) {
        lockDefaultToolkit(display);
        try {
            return X11Lib.DefaultScreen(display);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static long RootWindow(long display, int screen_number) {
        lockDefaultToolkit(display);
        try {
            return X11Lib.RootWindow(display, screen_number);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static long XCreatePixmap(long display, long arg1, int arg2, int arg3, int arg4) {
        lockDefaultToolkit(display);
        try {
            return X11Lib.XCreatePixmap(display, arg1, arg2, arg3, arg4);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static String XDisplayString(long display) {
        lockDefaultToolkit(display);
        try {
            return X11Lib.XDisplayString(display);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static int XFlush(long display) {
        lockDefaultToolkit(display);
        try {
            return X11Lib.XFlush(display);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static int XFreePixmap(long display, long arg1) {
        lockDefaultToolkit(display);
        try {
            return X11Lib.XFreePixmap(display, arg1);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static long DefaultVisualID(long display, int screen) {
        lockDefaultToolkit(display);
        try {
            return X11Lib.DefaultVisualID(display, screen);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static long CreateDummyWindow(long display, int screen_index, long visualID, int width, int height) {
        lockDefaultToolkit(display);
        try {
            return X11Lib.CreateDummyWindow(display, screen_index, visualID, width, height);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static void DestroyDummyWindow(long display, long window) {
        lockDefaultToolkit(display);
        try {
            X11Lib.DestroyDummyWindow(display, window);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static Point GetRelativeLocation(long display, int screen_index, long src_win, long dest_win, int src_x, int src_y) {
        lockDefaultToolkit(display);
        try {
            return X11Lib.GetRelativeLocation(display, screen_index, src_win, dest_win, src_x, src_y);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static XVisualInfo[] XGetVisualInfo(long display, long arg1, XVisualInfo arg2, int[] arg3, int arg3_offset) {
        lockDefaultToolkit(display);
        try {
            return X11Lib.XGetVisualInfo(display, arg1, arg2, arg3, arg3_offset);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static boolean XF86VidModeGetGammaRamp(long display, int screen, int size, ShortBuffer red_array, ShortBuffer green_array, ShortBuffer blue_array)  {
        lockDefaultToolkit(display);
        try {
            return X11Lib.XF86VidModeGetGammaRamp(display, screen, size, red_array, green_array, blue_array);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static boolean XF86VidModeGetGammaRamp(long display, int screen, int size, short[] red_array, int red_array_offset, short[] green_array, int green_array_offset, short[] blue_array, int blue_array_offset)  {
        lockDefaultToolkit(display);
        try {
            return X11Lib.XF86VidModeGetGammaRamp(display, screen, size, red_array, red_array_offset, green_array, green_array_offset, blue_array, blue_array_offset);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static boolean XF86VidModeGetGammaRampSize(long display, int screen, IntBuffer size)  {
        lockDefaultToolkit(display);
        try {
            return X11Lib.XF86VidModeGetGammaRampSize(display, screen, size);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static boolean XF86VidModeGetGammaRampSize(long display, int screen, int[] size, int size_offset)  {
        lockDefaultToolkit(display);
        try {
            return X11Lib.XF86VidModeGetGammaRampSize(display, screen, size, size_offset);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static boolean XF86VidModeSetGammaRamp(long display, int screen, int size, ShortBuffer red_array, ShortBuffer green_array, ShortBuffer blue_array)  {
        lockDefaultToolkit(display);
        try {
            return X11Lib.XF86VidModeSetGammaRamp(display, screen, size, red_array, green_array, blue_array);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static boolean XF86VidModeSetGammaRamp(long display, int screen, int size, short[] red_array, int red_array_offset, short[] green_array, int green_array_offset, short[] blue_array, int blue_array_offset)  {
        lockDefaultToolkit(display);
        try {
            return X11Lib.XF86VidModeSetGammaRamp(display, screen, size, red_array, red_array_offset, green_array, green_array_offset, blue_array, blue_array_offset);
        } finally {
            unlockDefaultToolkit(display);
        }
    }

    public static void XLockDisplay(long handle) {
        if(ToolkitLock.TRACE_LOCK) {
            System.out.println("+++ X11 Display Lock get 0x"+Long.toHexString(handle));
        }
        X11Lib.XLockDisplay(handle);
    }

    public static void XUnlockDisplay(long handle) {
        if(ToolkitLock.TRACE_LOCK) {
            System.out.println("--- X11 Display Lock rel 0x"+Long.toHexString(handle));
        }
        X11Lib.XUnlockDisplay(handle);
    }

    private static native void initialize0(boolean firstUIActionOnProcess);
    private static native void setX11ErrorHandler0(boolean onoff, boolean quiet);
}