aboutsummaryrefslogtreecommitdiff
path: root/src/solaris/classes/sun/awt/X11/XlibWrapper.java
blob: 02a7c4b2aa4a762efb4ad9dd62db6b5bc182f046 (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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/*
 * Copyright (c) 2002, 2009, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package sun.awt.X11;

import java.security.AccessController;
import java.security.PrivilegedAction;
import sun.security.action.GetPropertyAction;
import sun.misc.*;

final public class XlibWrapper
{
    static Unsafe unsafe = Unsafe.getUnsafe();
    // strange constants
    static final int MAXSIZE = 32767;
    static final int MINSIZE = 1;

    // define a private constructor here to prevent this class and all
    // its descendants from being created
    private XlibWrapper()
    {
    }

/*
   Display *XOpenDisplay(display_name)
   char *display_name;

*/
    public final static String eventToString[]=
    {"<none:0>", "<none:1>", "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease",
     "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", "FocusOut",
     "KeymapNotify", "Expose", "GraphicsExpose", "NoExpose", "VisibilityNotify",
     "CreateNotify", "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",
     "ReparentNotify", "ConfigureNotify", "ConfigureRequest", "GravityNotify",
     "ResizeRequest", "CirculateNotify", "CirculateRequest", "PropertyNotify",
     "SelectionClear", "SelectionRequest", "SelectionNotify", "ColormapNotify",
     "ClientMessage", "MappingNotify", "LASTEvent"};

    static native void XFree(long ptr);
    static native void memcpy(long dest_ptr, long src_ptr, long length);
    static native long getAddress(Object o);
    static native void copyIntArray(long dest_ptr, Object array, int size_bytes);
    static native void copyLongArray(long dest_ptr, Object array, int size_bytes);

    /**
     * Gets byte string from str_ptr and copies it into byte array
     * String should be NULL terminated
     */
    static native byte[] getStringBytes(long str_ptr);

    static  native long XOpenDisplay(long display);

    static  native void XCloseDisplay(long display);

    static  native long XDisplayString(long display);

    static  native void XSetCloseDownMode(long display, int close_mode);

    static  native long DefaultScreen(long display);

    static native long ScreenOfDisplay(long display, long screen_number);

    static native int DoesBackingStore(long screen);

    static native  long DisplayWidth(long display, long screen);
    static native  long DisplayWidthMM(long display, long screen);

    static native long DisplayHeight(long display, long screen);
    static native long DisplayHeightMM(long display, long screen);

    static  native long RootWindow(long display, long screen_number);
    static native int ScreenCount(long display);


/*
   Window XCreateWindow(display, parent, x, y, width, height,
   border_width, depth,
   class, visual, valuemask, attributes)
   Display *display;
   Window parent;
   int x, y;
   unsigned int width, height;
   unsigned int border_width;
   int depth;
   unsigned int class;
   Visual *visual
   unsigned long valuemask;
   XSetWindowAttributes *attributes;
*/

    static native long XCreateWindow(long display, long parent, int x,int  y, int width, int height, int border_width, int depth, long wclass, long visual, long valuemask, long attributes);

    static native void XDestroyWindow(long display, long window);

    static native int XGrabPointer(long display, long grab_window,
                                   int owner_events, int event_mask, int pointer_mode,
                                   int keyboard_mode, long confine_to, long cursor, long time);

    static native void XUngrabPointer(long display, long time);

    static native int XGrabKeyboard(long display, long grab_window,
                                    int owner_events, int pointer_mode,
                                    int keyboard_mode, long time);

    static native void XUngrabKeyboard(long display, long time);

    static native void XGrabServer(long display);
    static native void XUngrabServer(long display);

/*

void XSetWMProperties(display, w, window_name, icon_name,
argv, argc, normal_hints, wm_hints, class_hints)
Display *display;
Window w;
XTextProperty *window_name;
XTextProperty *icon_name;
char **argv;
int argc;
XSizeHints *normal_hints;
XWMHints *wm_hints;
XClassHint *class_hints;
*/

/*

XMapWindow(display, w)
Display *display;
Window w;
*/

    static  native void XMapWindow(long display, long window);
    static  native void XMapRaised(long display, long window);
    static  native void XRaiseWindow(long display, long window);

    static native void XLowerWindow(long display, long window);
    static native void XRestackWindows(long display, long windows, int length);
    static native void XConfigureWindow(long display, long window,
            long value_mask, long values);
    static native void XSetInputFocus(long display, long window);
    static native void XSetInputFocus2(long display, long window, long time);
    static native long XGetInputFocus(long display);

/*

XUnmapWindow(display, w)
Display *display;
Window w;
*/

    static  native void XUnmapWindow(long display, long window);




/*
  XSelectInput(display, w, event_mask)
  Display *display;
  Window w;
  long event_mask;

*/
    static  native void XSelectInput(long display, long window, long event_mask);

    /*
       XNextEvent(display, event_return)
       Display *display;
       XEvent *event_return;

    */

    static  native void XNextEvent(long display,long ptr);

    /*
     XMaskEvent(display, event_mask, event_return)
           Display *display;
           long event_mask;
           XEvent *event_return;
     */
    static native void XMaskEvent(long display, long event_mask, long event_return);

    static native void XWindowEvent(long display, long window, long event_mask, long event_return);

    /*
      Bool XFilterEvent(event, w)
           XEvent *event;
           Window w;
    */
    static native boolean XFilterEvent(long ptr, long window);

/*
     Bool XSupportsLocale()
*/
static native boolean XSupportsLocale();

/*
     char *XSetLocaleModifiers(modifier_list)
           char *modifier_list;
*/
static native String XSetLocaleModifiers(String modifier_list);


    static  native int XTranslateCoordinates(
                                             long display, long src_w, long dest_w,
                                             long src_x, long src_y,
                                             long dest_x_return, long dest_y_return,
                                             long child_return);

    /*
       XPeekEvent(display, event_return)
       Display *display;
       XEvent *event_return;

    */

    static  native void XPeekEvent(long display,long ptr);

/*
  XFlush(display)
  Display *display;
*/

    static native void XFlush(long display);

/*
  XSync(display, discard)
  Display *display;
  Bool discard;
*/

    static native void XSync(long display,int discard);


/*    XMoveResizeWindow(display, w, x, y, width, height)
      Display *display;
      Window w;
      int x, y;
      unsigned int width, height;
*/
    static native void XMoveResizeWindow(long display, long window, int x, int y, int width, int height);
    static native void XResizeWindow(long display, long window, int width, int height);
    static native void XMoveWindow(long display, long window, int x, int y);

    /*
     Bool XQueryPointer(display, w, root_return, child_return,
     root_x_return, root_y_return,
                          win_x_return, win_y_return,
     mask_return)
           Display *display;
           Window w;
           Window *root_return, *child_return;
           int *root_x_return, *root_y_return;
           int *win_x_return, *win_y_return;
           unsigned int *mask_return;
*/

 static native boolean  XQueryPointer (long display, long window, long root_return, long child_return, long root_x_return, long root_y_return, long win_x_return, long win_y_return, long mask_return);

/*    XFreeCursor(display, cursor)
           Display *display;
           Cursor cursor;
*/

 static native void XFreeCursor(long display, long cursor);

/*
   XSetWindowBackground(display, w, background_pixel)
   Display *display;
   Window w;
   unsigned long background_pixel;
*/

    static native void XSetWindowBackground(long display, long window, long background_pixel);

    static native int XEventsQueued(long display, int mode);

/*
  Atom XInternAtom(display, atom_name, only_if_exists)
  Display *display;
  char *atom_name;
  Bool only_if_exists;
*/

    static native int XInternAtoms(long display, String[] names, boolean only_if_exists, long atoms);

    static native void SetProperty(long display, long window, long atom, String str);
    static native String GetProperty(long display ,long window, long atom);
    static native long InternAtom(long display, String string, int only_if_exists);
    static native int XGetWindowProperty(long display, long window, long atom,
                                         long long_offset, long long_length,
                                         long delete, long req_type, long actualy_type,
                                         long actualy_format, long nitems_ptr,
                                         long bytes_after, long data_ptr);
    native static void XChangePropertyImpl(long display, long window, long atom,
                                           long type, int format, int mode, long data,
                                           int nelements);
    static void XChangeProperty(long display, long window, long atom,
                                long type, int format, int mode, long data,
                                int nelements) {
        // TODO: handling of XChangePropertyImpl return value, if not Success - don't cache
        if (XPropertyCache.isCachingSupported() &&
            XToolkit.windowToXWindow(window) != null &&
            WindowPropertyGetter.isCacheableProperty(XAtom.get(atom)) &&
            mode == XConstants.PropModeReplace)
        {
            int length = (format / 8) * nelements;
            XPropertyCache.storeCache(
                new XPropertyCache.PropertyCacheEntry(format,
                                                      nelements,
                                                      0,
                                                      data,
                                                      length),
                window,
                XAtom.get(atom));
        }
        XChangePropertyImpl(display, window, atom, type, format, mode, data, nelements);
    }

    static native void XChangePropertyS(long display, long window, long atom,
                                       long type, int format, int mode, String value);
    static native void XDeleteProperty(long display, long window, long atom);

    static native void XSetTransientFor(long display, long window, long transient_for_window);
    static native void XSetWMHints(long display, long window, long wmhints);
    static native void XGetWMHints(long display, long window, long wmhints);
    static native long XAllocWMHints();
    static native int XGetPointerMapping(long display, long map, int buttonNumber);
    static native String XGetDefault(long display, String program, String option);
    static native long getScreenOfWindow(long display, long window);
    static native long XScreenNumberOfScreen(long screen);
    static native int XIconifyWindow(long display, long window, long screenNumber);
    static native String ServerVendor(long display);
    static native int VendorRelease(long display);
    static native boolean IsXsunKPBehavior(long display);
    static native boolean IsSunKeyboard(long display);
    static native boolean IsKanaKeyboard(long display);

    static native void XBell(long display, int percent);

 /*
          Cursor XCreateFontCursor(display, shape)
           Display *display;
           unsigned int shape;

           we always pass int as shape param.
           perhaps later we will need to change type of shape to long.
*/

    static native int XCreateFontCursor(long display, int shape);

/*
     Pixmap XCreateBitmapFromData(display, d, data, width,
     height)
           Display *display;
           Drawable d;
           char *data;
           unsigned int width, height;
*/

    static native long XCreateBitmapFromData(long display, long drawable, long data, int width, int height);

 /*
      XFreePixmap(display, pixmap)
           Display *display;
           Pixmap pixmap;
  */

   static native void XFreePixmap(long display, long pixmap);

  /*
     Cursor XCreatePixmapCursor(display, source, mask,
     foreground_color, background_color, x, y)
           Display *display;
           Pixmap source;
           Pixmap mask;
           XColor *foreground_color;
           XColor *background_color;
           unsigned int x, y;
    */
   static native long XCreatePixmapCursor(long display, long source, long mask, long fore, long back, int x, int y);


    /*
         Status XQueryBestCursor(display, d, width, height,
     width_return, height_return)
           Display *display;
           Drawable d;
           unsigned int width, height;
           unsigned int *width_return, *height_return;

    */

    static native boolean XQueryBestCursor(long display, long drawable, int width, int height, long width_return, long height_return);


    /*
     Status XAllocColor(display, colormap, screen_in_out)
           Display *display;
           Colormap colormap;
           XColor *screen_in_out;
  */

    static native boolean XAllocColor( long display, long colormap, long screen_in_out);


    static native long SetToolkitErrorHandler();
    static native void XSetErrorHandler(long handler);
    static native int CallErrorHandler(long handler, long display, long event_ptr);

 /*
      XChangeWindowAttributes(display, w, valuemask, attributes)
           Display *display;
           Window w;
           unsigned long valuemask;
           XSetWindowAttributes *attributes;
  */

    static native void XChangeWindowAttributes(long display, long window, long valuemask, long attributes);
    static native int XGetWindowAttributes(long display, long window, long attr_ptr);
    static native int XGetGeometry(long display, long drawable, long root_return, long x_return, long y_return,
                                   long width_return, long height_return, long border_width_return, long depth_return);

    static native int XGetWMNormalHints(long display, long window, long hints, long supplied_return);
    static native void XSetWMNormalHints(long display, long window, long hints);
    static native void XSetMinMaxHints(long display, long window, int x, int y, int width, int height, long flags);
    static native long XAllocSizeHints();

    static native int XSendEvent(long display, long window, boolean propagate, long event_mask, long event);
    static native void XPutBackEvent(long display, long event);
    static native int XQueryTree(long display, long window, long root_return, long parent_return, long children_return, long nchildren_return);
    static native long XGetVisualInfo(long display, long vinfo_mask, long vinfo_template, long nitems_return);
    static native void XReparentWindow(long display, long window, long parent, int x, int y);

    static native void XConvertSelection(long display, long selection,
                                         long target, long property,
                                         long requestor, long time);

    static native void XSetSelectionOwner(long display, long selection,
                                          long owner, long time);

    static native long XGetSelectionOwner(long display, long selection);

    static native String XGetAtomName(long display, long atom);

    static native long XMaxRequestSize(long display);


    static native long XCreatePixmap(long display, long drawable, int width, int height, int depth);
    static native long XCreateImage(long display, long visual_ptr, int depth, int format,
                                    int offset, long data, int width, int height, int bitmap_pad,
                                    int bytes_per_line);
    static native void XDestroyImage(long image);
    static native void XPutImage(long display, long drawable, long gc, long image,
                                 int src_x, int src_y, int dest_x, int dest_y,
                                 int width, int height);
    static native long XCreateGC(long display, long drawable, long valuemask, long values);
    static native void XFreeGC(long display, long gc);
    static native void XSetWindowBackgroundPixmap(long display, long window, long pixmap);
    static native void XClearWindow(long display, long window);
    static native int XGetIconSizes(long display, long window, long ret_sizes, long ret_count);
    static native int XdbeQueryExtension(long display, long major_version_return,
                                         long minor_version_return);
    static native boolean XQueryExtension(long display, String name, long mop_return,
                                         long feve_return, long err_return);
    static native boolean IsKeypadKey(long keysym);
    static native long XdbeAllocateBackBufferName(long display, long window, int swap_action);
    static native int XdbeDeallocateBackBufferName(long display, long buffer);
    static native int XdbeBeginIdiom(long display);
    static native int XdbeEndIdiom(long display);
    static native int XdbeSwapBuffers(long display, long swap_info, int num_windows);

    static native void XQueryKeymap(long display, long vector);
    static native long XKeycodeToKeysym(long display, int keycode, int index);

    static native int XKeysymToKeycode(long display, long keysym);

    // xkb-related
    static native int XkbGetEffectiveGroup(long display);
    static native long XkbKeycodeToKeysym(long display, int keycode, int group, int level);
    static native void XkbSelectEvents(long display, long device, long bits_to_change, long values_for_bits);
    static native void XkbSelectEventDetails(long display, long device, long event_type,
                                              long bits_to_change, long values_for_bits);
    static native boolean XkbQueryExtension(long display, long opcode_rtrn, long event_rtrn,
              long error_rtrn, long major_in_out, long minor_in_out);
    static native boolean XkbLibraryVersion(long lib_major_in_out, long lib_minor_in_out);
    static native long XkbGetMap(long display, long which, long device_spec);
    static native long XkbGetUpdatedMap(long display, long which, long xkb);
    static native void XkbFreeKeyboard(long xkb, long which, boolean free_all);
    static native boolean XkbTranslateKeyCode(long xkb, int keycode, long mods, long mods_rtrn, long keysym_rtrn);
    static native void XkbSetDetectableAutoRepeat(long display, boolean detectable);


    static native void XConvertCase(long keysym,
                                    long keysym_lowercase,
                                    long keysym_uppercase);

    static native long XGetModifierMapping(long display);
    static native void XFreeModifiermap(long keymap);
    static native void XRefreshKeyboardMapping(long event);


    static native void XChangeActivePointerGrab(long display, int mask,
                                                long cursor, long time);

    /*
      int (*XSynchronize(Display *display, Bool onoff))();
          display   Specifies the connection to the X server.
          onoff     Specifies a Boolean value that indicates whether to enable or disable synchronization.
     */
    public static native int XSynchronize(long display, boolean onoff);

    /**
     * Extracts an X event that can be processed in a secondary loop.
     * Should only be called on the toolkit thread.
     * Returns false if this secondary event was terminated.
     */
    static native boolean XNextSecondaryLoopEvent(long display, long ptr);
    /**
     * Terminates the topmost secondary loop (if any).
     * Should never be called on the toolkit thread.
     */
    static native void ExitSecondaryLoop();

    /**
     * Calls XTextPropertyToStringList on the specified byte array and returns
     * the array of strings.
     */
    static native String[] XTextPropertyToStringList(byte[] bytes, long encoding_atom);

    /**
     * XSHAPE extension support.
     */
    static native boolean XShapeQueryExtension(long display, long event_base_return, long error_base_return);
    static native void SetRectangularShape(long display, long window,
            int lox, int loy, int hix, int hiy,
            sun.java2d.pipe.Region region);
    /** Each int in the bitmap array is one pixel with a 32-bit color:
     *  R, G, B, and Alpha.
     */
    static native void SetBitmapShape(long display, long window,
             int width, int height, int[] bitmap);

    static native void SetZOrder(long display, long window, long above);

/* Global memory area used for X lib parameter passing */

    final static long lbuffer = unsafe.allocateMemory(64);  // array to hold 8 longs
    final static long ibuffer = unsafe.allocateMemory(32);  // array to hold 8 ints

    static final long larg1 = lbuffer;
    static final long larg2 = larg1+8;
    static final long larg3 = larg2+8;
    static final long larg4 = larg3+8;
    static final long larg5 = larg4+8;
    static final long larg6 = larg5+8;
    static final long larg7 = larg6+8;
    static final long larg8 = larg7+8;

    static final long iarg1 = ibuffer;
    static final long iarg2 = iarg1+4;
    static final long iarg3 = iarg2+4;
    static final long iarg4 = iarg3+4;
    static final long iarg5 = iarg4+4;
    static final long iarg6 = iarg5+4;
    static final long iarg7 = iarg6+4;
    static final long iarg8 = iarg7+4;


    static int dataModel;
    static final boolean isBuildInternal;

    static {
        String dataModelProp = AccessController.doPrivileged(
            new GetPropertyAction("sun.arch.data.model"));
        try {
            dataModel = Integer.parseInt(dataModelProp);
        } catch (Exception e) {
            dataModel = 32;
        }

        isBuildInternal = getBuildInternal();

//      System.loadLibrary("mawt");
    }

    static int getDataModel() {
        return dataModel;
    }

    static String hintsToString(long flags) {
        StringBuffer buf = new StringBuffer();
        if ((flags & XUtilConstants.PMaxSize) != 0) {
            buf.append("PMaxSize ");
        }
        if ((flags & XUtilConstants.PMinSize) != 0) {
            buf.append("PMinSize ");
        }
        if ((flags & XUtilConstants.USSize) != 0) {
            buf.append("USSize ");
        }
        if ((flags & XUtilConstants.USPosition) != 0) {
            buf.append("USPosition ");
        }
        if ((flags & XUtilConstants.PPosition) != 0) {
            buf.append("PPosition ");
        }
        if ((flags & XUtilConstants.PSize) != 0) {
            buf.append("PSize ");
        }
        if ((flags & XUtilConstants.PWinGravity) != 0) {
            buf.append("PWinGravity ");
        }
        return buf.toString();
    }
    static String getEventToString( int type ) {
        if( (type >= 0) && (type < eventToString.length)) {
            return eventToString[type];
        }else if( type == XToolkit.getXKBBaseEventCode() ) {
            //XXX TODO various xkb types
            return "XkbEvent";
        }
        return eventToString[0];
    }

    private static boolean getBuildInternal() {
        String javaVersion = AccessController.doPrivileged(
                                 new GetPropertyAction("java.version"));
        return javaVersion != null && javaVersion.contains("internal");
    }

    static native void PrintXErrorEvent(long display, long event_ptr);
}