001: /*
002: * Copyright 2002-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.X11;
027:
028: import java.security.AccessController;
029: import java.security.PrivilegedAction;
030: import sun.misc.*;
031:
032: public class XlibWrapper implements XConstants, XUtilConstants,
033: XProtocolConstants, XCursorFontConstants {
034: static Unsafe unsafe = Unsafe.getUnsafe();
035: // strange constants
036: static final int MAXSIZE = 32767;
037: static final int MINSIZE = 1;
038:
039: // define a private constructor here to prevent this class and all
040: // its descendants from being created
041: private XlibWrapper() {
042: }
043:
044: /*
045: Display *XOpenDisplay(display_name)
046: char *display_name;
047:
048: */
049: public final static String eventToString[] = { "<none:0>",
050: "<none:1>", "KeyPress", "KeyRelease", "ButtonPress",
051: "ButtonRelease", "MotionNotify", "EnterNotify",
052: "LeaveNotify", "FocusIn", "FocusOut", "KeymapNotify",
053: "Expose", "GraphicsExpose", "NoExpose", "VisibilityNotify",
054: "CreateNotify", "DestroyNotify", "UnmapNotify",
055: "MapNotify", "MapRequest", "ReparentNotify",
056: "ConfigureNotify", "ConfigureRequest", "GravityNotify",
057: "ResizeRequest", "CirculateNotify", "CirculateRequest",
058: "PropertyNotify", "SelectionClear", "SelectionRequest",
059: "SelectionNotify", "ColormapNotify", "ClientMessage",
060: "MappingNotify", "LASTEvent" };
061:
062: static native void XFree(long ptr);
063:
064: static native void memcpy(long dest_ptr, long src_ptr, long length);
065:
066: static native long getAddress(Object o);
067:
068: static native void copyIntArray(long dest_ptr, Object array,
069: int size_bytes);
070:
071: static native void copyLongArray(long dest_ptr, Object array,
072: int size_bytes);
073:
074: /**
075: * Gets byte string from str_ptr and copies it into byte array
076: * String should be NULL terminated
077: */
078: static native byte[] getStringBytes(long str_ptr);
079:
080: static native long XOpenDisplay(long display);
081:
082: static native void XCloseDisplay(long display);
083:
084: static native long XDisplayString(long display);
085:
086: static native void XSetCloseDownMode(long display, int close_mode);
087:
088: static native long DefaultScreen(long display);
089:
090: static native long ScreenOfDisplay(long display, long screen_number);
091:
092: static native int DoesBackingStore(long screen);
093:
094: static native long DisplayWidth(long display, long screen);
095:
096: static native long DisplayWidthMM(long display, long screen);
097:
098: static native long DisplayHeight(long display, long screen);
099:
100: static native long DisplayHeightMM(long display, long screen);
101:
102: static native long RootWindow(long display, long screen_number);
103:
104: static native int ScreenCount(long display);
105:
106: /*
107: Window XCreateWindow(display, parent, x, y, width, height,
108: border_width, depth,
109: class, visual, valuemask, attributes)
110: Display *display;
111: Window parent;
112: int x, y;
113: unsigned int width, height;
114: unsigned int border_width;
115: int depth;
116: unsigned int class;
117: Visual *visual
118: unsigned long valuemask;
119: XSetWindowAttributes *attributes;
120: */
121:
122: static native long XCreateWindow(long display, long parent, int x,
123: int y, int width, int height, int border_width, int depth,
124: long wclass, long visual, long valuemask, long attributes);
125:
126: static native void XDestroyWindow(long display, long window);
127:
128: static native int XGrabPointer(long display, long grab_window,
129: int owner_events, int event_mask, int pointer_mode,
130: int keyboard_mode, long confine_to, long cursor, long time);
131:
132: static native void XUngrabPointer(long display, long time);
133:
134: static native int XGrabKeyboard(long display, long grab_window,
135: int owner_events, int pointer_mode, int keyboard_mode,
136: long time);
137:
138: static native void XUngrabKeyboard(long display, long time);
139:
140: static native void XGrabServer(long display);
141:
142: static native void XUngrabServer(long display);
143:
144: /*
145:
146: void XSetWMProperties(display, w, window_name, icon_name,
147: argv, argc, normal_hints, wm_hints, class_hints)
148: Display *display;
149: Window w;
150: XTextProperty *window_name;
151: XTextProperty *icon_name;
152: char **argv;
153: int argc;
154: XSizeHints *normal_hints;
155: XWMHints *wm_hints;
156: XClassHint *class_hints;
157: */
158:
159: /*
160:
161: XMapWindow(display, w)
162: Display *display;
163: Window w;
164: */
165:
166: static native void XMapWindow(long display, long window);
167:
168: static native void XMapRaised(long display, long window);
169:
170: static native void XRaiseWindow(long display, long window);
171:
172: static native void XLowerWindow(long display, long window);
173:
174: static native void XRestackWindows(long display, long windows,
175: int length);
176:
177: static native void XSetInputFocus(long display, long window);
178:
179: static native void XSetInputFocus2(long display, long window,
180: long time);
181:
182: static native long XGetInputFocus(long display);
183:
184: /*
185:
186: XUnmapWindow(display, w)
187: Display *display;
188: Window w;
189: */
190:
191: static native void XUnmapWindow(long display, long window);
192:
193: /*
194: XSelectInput(display, w, event_mask)
195: Display *display;
196: Window w;
197: long event_mask;
198:
199: */
200: static native void XSelectInput(long display, long window,
201: long event_mask);
202:
203: /*
204: XNextEvent(display, event_return)
205: Display *display;
206: XEvent *event_return;
207:
208: */
209:
210: static native void XNextEvent(long display, long ptr);
211:
212: /*
213: XMaskEvent(display, event_mask, event_return)
214: Display *display;
215: long event_mask;
216: XEvent *event_return;
217: */
218: static native void XMaskEvent(long display, long event_mask,
219: long event_return);
220:
221: static native void XWindowEvent(long display, long window,
222: long event_mask, long event_return);
223:
224: /*
225: Bool XFilterEvent(event, w)
226: XEvent *event;
227: Window w;
228: */
229: static native boolean XFilterEvent(long ptr, long window);
230:
231: /*
232: Bool XSupportsLocale()
233: */
234: static native boolean XSupportsLocale();
235:
236: /*
237: char *XSetLocaleModifiers(modifier_list)
238: char *modifier_list;
239: */
240: static native String XSetLocaleModifiers(String modifier_list);
241:
242: static native int XTranslateCoordinates(long display, long src_w,
243: long dest_w, long src_x, long src_y, long dest_x_return,
244: long dest_y_return, long child_return);
245:
246: /*
247: XPeekEvent(display, event_return)
248: Display *display;
249: XEvent *event_return;
250:
251: */
252:
253: static native void XPeekEvent(long display, long ptr);
254:
255: /*
256: XFlush(display)
257: Display *display;
258: */
259:
260: static native void XFlush(long display);
261:
262: /*
263: XSync(display, discard)
264: Display *display;
265: Bool discard;
266: */
267:
268: static native void XSync(long display, int discard);
269:
270: /* XMoveResizeWindow(display, w, x, y, width, height)
271: Display *display;
272: Window w;
273: int x, y;
274: unsigned int width, height;
275: */
276: static native void XMoveResizeWindow(long display, long window,
277: int x, int y, int width, int height);
278:
279: static native void XResizeWindow(long display, long window,
280: int width, int height);
281:
282: static native void XMoveWindow(long display, long window, int x,
283: int y);
284:
285: /*
286: Bool XQueryPointer(display, w, root_return, child_return,
287: root_x_return, root_y_return,
288: win_x_return, win_y_return,
289: mask_return)
290: Display *display;
291: Window w;
292: Window *root_return, *child_return;
293: int *root_x_return, *root_y_return;
294: int *win_x_return, *win_y_return;
295: unsigned int *mask_return;
296: */
297:
298: static native boolean XQueryPointer(long display, long window,
299: long root_return, long child_return, long root_x_return,
300: long root_y_return, long win_x_return, long win_y_return,
301: long mask_return);
302:
303: /* XFreeCursor(display, cursor)
304: Display *display;
305: Cursor cursor;
306: */
307:
308: static native void XFreeCursor(long display, long cursor);
309:
310: /*
311: XSetWindowBackground(display, w, background_pixel)
312: Display *display;
313: Window w;
314: unsigned long background_pixel;
315: */
316:
317: static native void XSetWindowBackground(long display, long window,
318: long background_pixel);
319:
320: static native int XEventsQueued(long display, int mode);
321:
322: /*
323: Atom XInternAtom(display, atom_name, only_if_exists)
324: Display *display;
325: char *atom_name;
326: Bool only_if_exists;
327: */
328:
329: static native int XInternAtoms(long display, String[] names,
330: boolean only_if_exists, long atoms);
331:
332: static native void SetProperty(long display, long window,
333: long atom, String str);
334:
335: static native String GetProperty(long display, long window,
336: long atom);
337:
338: static native long InternAtom(long display, String string,
339: int only_if_exists);
340:
341: static native int XGetWindowProperty(long display, long window,
342: long atom, long long_offset, long long_length, long delete,
343: long req_type, long actualy_type, long actualy_format,
344: long nitems_ptr, long bytes_after, long data_ptr);
345:
346: native static void XChangePropertyImpl(long display, long window,
347: long atom, long type, int format, int mode, long data,
348: int nelements);
349:
350: static void XChangeProperty(long display, long window, long atom,
351: long type, int format, int mode, long data, int nelements) {
352: // TODO: handling of XChangePropertyImpl return value, if not Success - don't cache
353: if (XPropertyCache.isCachingSupported()
354: && XToolkit.windowToXWindow(window) != null
355: && WindowPropertyGetter.isCacheableProperty(XAtom
356: .get(atom)) && mode == PropModeReplace) {
357: int length = (format / 8) * nelements;
358: XPropertyCache.storeCache(
359: new XPropertyCache.PropertyCacheEntry(format,
360: nelements, 0, data, length), window, XAtom
361: .get(atom));
362: }
363: XChangePropertyImpl(display, window, atom, type, format, mode,
364: data, nelements);
365: }
366:
367: static native void XChangePropertyS(long display, long window,
368: long atom, long type, int format, int mode, String value);
369:
370: static native void XDeleteProperty(long display, long window,
371: long atom);
372:
373: static native void XSetTransientFor(long display, long window,
374: long transient_for_window);
375:
376: static native void XSetWMHints(long display, long window,
377: long wmhints);
378:
379: static native void XGetWMHints(long display, long window,
380: long wmhints);
381:
382: static native long XAllocWMHints();
383:
384: static native int XGetPointerMapping(long display, long map,
385: int buttonNumber);
386:
387: static native String XGetDefault(long display, String program,
388: String option);
389:
390: static native long getScreenOfWindow(long display, long window);
391:
392: static native long XScreenNumberOfScreen(long screen);
393:
394: static native int XIconifyWindow(long display, long window,
395: long screenNumber);
396:
397: static native String ServerVendor(long display);
398:
399: static native int VendorRelease(long display);
400:
401: static native void XBell(long display, int percent);
402:
403: /*
404: Cursor XCreateFontCursor(display, shape)
405: Display *display;
406: unsigned int shape;
407:
408: we always pass int as shape param.
409: perhaps later we will need to change type of shape to long.
410: */
411:
412: static native int XCreateFontCursor(long display, int shape);
413:
414: /*
415: Pixmap XCreateBitmapFromData(display, d, data, width,
416: height)
417: Display *display;
418: Drawable d;
419: char *data;
420: unsigned int width, height;
421: */
422:
423: static native long XCreateBitmapFromData(long display,
424: long drawable, long data, int width, int height);
425:
426: /*
427: XFreePixmap(display, pixmap)
428: Display *display;
429: Pixmap pixmap;
430: */
431:
432: static native void XFreePixmap(long display, long pixmap);
433:
434: /*
435: Cursor XCreatePixmapCursor(display, source, mask,
436: foreground_color, background_color, x, y)
437: Display *display;
438: Pixmap source;
439: Pixmap mask;
440: XColor *foreground_color;
441: XColor *background_color;
442: unsigned int x, y;
443: */
444: static native long XCreatePixmapCursor(long display, long source,
445: long mask, long fore, long back, int x, int y);
446:
447: /*
448: Status XQueryBestCursor(display, d, width, height,
449: width_return, height_return)
450: Display *display;
451: Drawable d;
452: unsigned int width, height;
453: unsigned int *width_return, *height_return;
454:
455: */
456:
457: static native boolean XQueryBestCursor(long display, long drawable,
458: int width, int height, long width_return, long height_return);
459:
460: /*
461: Status XAllocColor(display, colormap, screen_in_out)
462: Display *display;
463: Colormap colormap;
464: XColor *screen_in_out;
465: */
466:
467: static native boolean XAllocColor(long display, long colormap,
468: long screen_in_out);
469:
470: static native long SetToolkitErrorHandler();
471:
472: static native void XSetErrorHandler(long handler);
473:
474: static native int CallErrorHandler(long handler, long display,
475: long event_ptr);
476:
477: /*
478: XChangeWindowAttributes(display, w, valuemask, attributes)
479: Display *display;
480: Window w;
481: unsigned long valuemask;
482: XSetWindowAttributes *attributes;
483: */
484:
485: static native void XChangeWindowAttributes(long display,
486: long window, long valuemask, long attributes);
487:
488: static native int XGetWindowAttributes(long display, long window,
489: long attr_ptr);
490:
491: static native int XGetGeometry(long display, long drawable,
492: long root_return, long x_return, long y_return,
493: long width_return, long height_return,
494: long border_width_return, long depth_return);
495:
496: static native int XGetWMNormalHints(long display, long window,
497: long hints, long supplied_return);
498:
499: static native void XSetWMNormalHints(long display, long window,
500: long hints);
501:
502: static native void XSetMinMaxHints(long display, long window,
503: int x, int y, int width, int height, long flags);
504:
505: static native long XAllocSizeHints();
506:
507: static native int XSendEvent(long display, long window,
508: boolean propagate, long event_mask, long event);
509:
510: static native void XPutBackEvent(long display, long event);
511:
512: static native int XQueryTree(long display, long window,
513: long root_return, long parent_return, long children_return,
514: long nchildren_return);
515:
516: static native long XGetVisualInfo(long display, long vinfo_mask,
517: long vinfo_template, long nitems_return);
518:
519: static native void XReparentWindow(long display, long window,
520: long parent, int x, int y);
521:
522: static native void XConvertSelection(long display, long selection,
523: long target, long property, long requestor, long time);
524:
525: static native void XSetSelectionOwner(long display, long selection,
526: long owner, long time);
527:
528: static native long XGetSelectionOwner(long display, long selection);
529:
530: static native String XGetAtomName(long display, long atom);
531:
532: static native long XMaxRequestSize(long display);
533:
534: static native long XCreatePixmap(long display, long drawable,
535: int width, int height, int depth);
536:
537: static native long XCreateImage(long display, long visual_ptr,
538: int depth, int format, int offset, long data, int width,
539: int height, int bitmap_pad, int bytes_per_line);
540:
541: static native void XDestroyImage(long image);
542:
543: static native void XPutImage(long display, long drawable, long gc,
544: long image, int src_x, int src_y, int dest_x, int dest_y,
545: int width, int height);
546:
547: static native long XCreateGC(long display, long drawable,
548: long valuemask, long values);
549:
550: static native void XFreeGC(long display, long gc);
551:
552: static native void XSetWindowBackgroundPixmap(long display,
553: long window, long pixmap);
554:
555: static native void XClearWindow(long display, long window);
556:
557: static native int XGetIconSizes(long display, long window,
558: long ret_sizes, long ret_count);
559:
560: static native int XdbeQueryExtension(long display,
561: long major_version_return, long minor_version_return);
562:
563: static native boolean XQueryExtension(long display, String name,
564: long mop_return, long feve_return, long err_return);
565:
566: static native boolean IsKeypadKey(long keysym);
567:
568: static native long XdbeAllocateBackBufferName(long display,
569: long window, int swap_action);
570:
571: static native int XdbeDeallocateBackBufferName(long display,
572: long buffer);
573:
574: static native int XdbeBeginIdiom(long display);
575:
576: static native int XdbeEndIdiom(long display);
577:
578: static native int XdbeSwapBuffers(long display, long swap_info,
579: int num_windows);
580:
581: static native long XKeycodeToKeysym(long display, int keycode,
582: int index);
583:
584: static native int XKeysymToKeycode(long display, long keysym);
585:
586: static native void XConvertCase(long keysym, long keysym_lowercase,
587: long keysym_uppercase);
588:
589: static native long XGetModifierMapping(long display);
590:
591: static native void XFreeModifiermap(long keymap);
592:
593: static native void XChangeActivePointerGrab(long display, int mask,
594: long cursor, long time);
595:
596: /*
597: int (*XSynchronize(Display *display, Bool onoff))();
598: display Specifies the connection to the X server.
599: onoff Specifies a Boolean value that indicates whether to enable or disable synchronization.
600: */
601: public static native int XSynchronize(long display, boolean onoff);
602:
603: /**
604: * Extracts an X event that can be processed in a secondary loop.
605: * Should only be called on the toolkit thread.
606: * Returns false if this secondary event was terminated.
607: */
608: static native boolean XNextSecondaryLoopEvent(long display, long ptr);
609:
610: /**
611: * Terminates the topmost secondary loop (if any).
612: * Should never be called on the toolkit thread.
613: */
614: static native void ExitSecondaryLoop();
615:
616: /**
617: * Calls XTextPropertyToStringList on the specified byte array and returns
618: * the array of strings.
619: */
620: static native String[] XTextPropertyToStringList(byte[] bytes,
621: long encoding_atom);
622:
623: /* Global memory area used for X lib parameter passing */
624:
625: final static long lbuffer = unsafe.allocateMemory(64); // array to hold 8 longs
626: final static long ibuffer = unsafe.allocateMemory(32); // array to hold 8 ints
627:
628: static final long larg1 = lbuffer;
629: static final long larg2 = larg1 + 8;
630: static final long larg3 = larg2 + 8;
631: static final long larg4 = larg3 + 8;
632: static final long larg5 = larg4 + 8;
633: static final long larg6 = larg5 + 8;
634: static final long larg7 = larg6 + 8;
635: static final long larg8 = larg7 + 8;
636:
637: static final long iarg1 = ibuffer;
638: static final long iarg2 = iarg1 + 4;
639: static final long iarg3 = iarg2 + 4;
640: static final long iarg4 = iarg3 + 4;
641: static final long iarg5 = iarg4 + 4;
642: static final long iarg6 = iarg5 + 4;
643: static final long iarg7 = iarg6 + 4;
644: static final long iarg8 = iarg7 + 4;
645:
646: static int dataModel;
647: static final boolean isBuildInternal;
648:
649: static {
650: String dataModelProp = (String) AccessController
651: .doPrivileged(new PrivilegedAction() {
652: public Object run() {
653: return System
654: .getProperty("sun.arch.data.model");
655: }
656: });
657: try {
658: dataModel = Integer.parseInt(dataModelProp);
659: } catch (Exception e) {
660: dataModel = 32;
661: }
662:
663: isBuildInternal = getBuildInternal();
664:
665: // System.loadLibrary("mawt");
666: }
667:
668: static int getDataModel() {
669: return dataModel;
670: }
671:
672: static String hintsToString(long flags) {
673: StringBuffer buf = new StringBuffer();
674: if ((flags & PMaxSize) != 0) {
675: buf.append("PMaxSize ");
676: }
677: if ((flags & PMinSize) != 0) {
678: buf.append("PMinSize ");
679: }
680: if ((flags & USSize) != 0) {
681: buf.append("USSize ");
682: }
683: if ((flags & USPosition) != 0) {
684: buf.append("USPosition ");
685: }
686: if ((flags & PPosition) != 0) {
687: buf.append("PPosition ");
688: }
689: if ((flags & PSize) != 0) {
690: buf.append("PSize ");
691: }
692: if ((flags & PWinGravity) != 0) {
693: buf.append("PWinGravity ");
694: }
695: return buf.toString();
696: }
697:
698: private static boolean getBuildInternal() {
699: String javaVersion = XToolkit.getSystemProperty("java.version");
700: return javaVersion != null && javaVersion.contains("internal");
701: }
702: }
|