src/solaris/native/sun/awt/awt_GraphicsEnv.c

Print this page


   1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 741         strlen(getenv("_AWT_IGNORE_XKB")) > 0) {
 742         if (XkbIgnoreExtension(True)) {
 743             printf("Ignoring XKB.\n");
 744         }
 745     }
 746 
 747     dpy = awt_display = XOpenDisplay(NULL);
 748 #ifdef NETSCAPE
 749     sigprocmask(SIG_SETMASK, &oldset, NULL);
 750 #endif
 751     if (!dpy) {
 752         jio_snprintf(errmsg,
 753                      sizeof(errmsg),
 754                      "Can't connect to X11 window server using '%s' as the value of the DISPLAY variable.",
 755                      (getenv("DISPLAY") == NULL) ? ":0.0" : getenv("DISPLAY"));
 756         JNU_ThrowByName(env, "java/awt/AWTError", errmsg);
 757         return NULL;
 758     }
 759 
 760     XSetIOErrorHandler(xioerror_handler);


 761 
 762     /* set awt_numScreens, and whether or not we're using Xinerama */
 763     xineramaInit();
 764 
 765     if (!usingXinerama) {
 766         awt_numScreens =  XScreenCount(awt_display);
 767     }
 768 
 769     DTRACE_PRINTLN1("allocating %i screens\n", awt_numScreens);
 770     /* Allocate screen data structure array */
 771     x11Screens = calloc(awt_numScreens, sizeof(AwtScreenData));
 772     if (x11Screens == NULL) {
 773         JNU_ThrowOutOfMemoryError((JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2),
 774                                   NULL);
 775         return NULL;
 776     }
 777 
 778     for (i = 0; i < awt_numScreens; i++) {
 779         if (usingXinerama) {
 780             /* All Xinerama screens use the same X11 root for now */


 887 
 888 /*
 889  * Class:     sun_awt_X11GraphicsDevice
 890  * Method:    getDisplay
 891  * Signature: ()J
 892  */
 893 JNIEXPORT jlong JNICALL
 894 Java_sun_awt_X11GraphicsDevice_getDisplay(JNIEnv *env, jobject this)
 895 {
 896 #ifdef HEADLESS
 897     return NULL;
 898 #else
 899     return ptr_to_jlong(awt_display);
 900 #endif /* !HEADLESS */
 901 }
 902 
 903 #ifdef MITSHM
 904 
 905 static jint canUseShmExt = UNSET_MITSHM;
 906 static jint canUseShmExtPixmaps = UNSET_MITSHM;
 907 static jboolean xshmAttachFailed = JNI_FALSE;
 908 
 909 int J2DXErrHandler(Display *display, XErrorEvent *xerr) {
 910     int ret = 0;
 911     if (xerr->minor_code == X_ShmAttach) {
 912         xshmAttachFailed = JNI_TRUE;
 913     } else {
 914         ret = (*xerror_saved_handler)(display, xerr);
 915     }
 916     return ret;
 917 }
 918 jboolean isXShmAttachFailed() {
 919     return xshmAttachFailed;
 920 }
 921 void resetXShmAttachFailed() {
 922     xshmAttachFailed = JNI_FALSE;
 923 }
 924 
 925 void TryInitMITShm(JNIEnv *env, jint *shmExt, jint *shmPixmaps) {
 926     XShmSegmentInfo shminfo;
 927     int XShmMajor, XShmMinor;
 928     int a, b, c;
 929 
 930     AWT_LOCK();
 931     if (canUseShmExt != UNSET_MITSHM) {
 932         *shmExt = canUseShmExt;
 933         *shmPixmaps = canUseShmExtPixmaps;
 934         AWT_UNLOCK();
 935         return;
 936     }
 937 
 938     *shmExt = canUseShmExt = CANT_USE_MITSHM;
 939     *shmPixmaps = canUseShmExtPixmaps = CANT_USE_MITSHM;
 940 
 941     /**
 942      * XShmQueryExtension returns False in remote server case.
 943      * Unfortunately it also returns True in ssh case, so


 946     if (XShmQueryExtension(awt_display)) {
 947         shminfo.shmid = shmget(IPC_PRIVATE, 0x10000, IPC_CREAT|0777);
 948         if (shminfo.shmid < 0) {
 949             AWT_UNLOCK();
 950             J2dRlsTraceLn1(J2D_TRACE_ERROR,
 951                            "TryInitMITShm: shmget has failed: %s",
 952                            strerror(errno));
 953             return;
 954         }
 955         shminfo.shmaddr = (char *) shmat(shminfo.shmid, 0, 0);
 956         if (shminfo.shmaddr == ((char *) -1)) {
 957             shmctl(shminfo.shmid, IPC_RMID, 0);
 958             AWT_UNLOCK();
 959             J2dRlsTraceLn1(J2D_TRACE_ERROR,
 960                            "TryInitMITShm: shmat has failed: %s",
 961                            strerror(errno));
 962             return;
 963         }
 964         shminfo.readOnly = True;
 965 
 966         resetXShmAttachFailed();
 967         /**
 968          * The J2DXErrHandler handler will set xshmAttachFailed
 969          * to JNI_TRUE if any Shm error has occured.
 970          */
 971         EXEC_WITH_XERROR_HANDLER(J2DXErrHandler,
 972                                  XShmAttach(awt_display, &shminfo));





 973 
 974         /**
 975          * Get rid of the id now to reduce chances of leaking
 976          * system resources.
 977          */
 978         shmctl(shminfo.shmid, IPC_RMID, 0);
 979 
 980         if (isXShmAttachFailed() == JNI_FALSE) {
 981             canUseShmExt = CAN_USE_MITSHM;
 982             /* check if we can use shared pixmaps */
 983             XShmQueryVersion(awt_display, &XShmMajor, &XShmMinor,
 984                              (Bool*)&canUseShmExtPixmaps);
 985             canUseShmExtPixmaps = canUseShmExtPixmaps &&
 986                 (XShmPixmapFormat(awt_display) == ZPixmap);
 987             XShmDetach(awt_display, &shminfo);
 988         }
 989         shmdt(shminfo.shmaddr);
 990         *shmExt = canUseShmExt;
 991         *shmPixmaps = canUseShmExtPixmaps;
 992     }
 993     AWT_UNLOCK();
 994 }
 995 #endif /* MITSHM */
 996 
 997 /*
 998  * Class:     sun_awt_X11GraphicsEnvironment
 999  * Method:    checkShmExt
1000  * Signature: ()I


   1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 741         strlen(getenv("_AWT_IGNORE_XKB")) > 0) {
 742         if (XkbIgnoreExtension(True)) {
 743             printf("Ignoring XKB.\n");
 744         }
 745     }
 746 
 747     dpy = awt_display = XOpenDisplay(NULL);
 748 #ifdef NETSCAPE
 749     sigprocmask(SIG_SETMASK, &oldset, NULL);
 750 #endif
 751     if (!dpy) {
 752         jio_snprintf(errmsg,
 753                      sizeof(errmsg),
 754                      "Can't connect to X11 window server using '%s' as the value of the DISPLAY variable.",
 755                      (getenv("DISPLAY") == NULL) ? ":0.0" : getenv("DISPLAY"));
 756         JNU_ThrowByName(env, "java/awt/AWTError", errmsg);
 757         return NULL;
 758     }
 759 
 760     XSetIOErrorHandler(xioerror_handler);
 761     JNU_CallStaticMethodByName(env, NULL, "sun/awt/X11/XErrorHandlerUtil", "init", "(J)V",
 762         ptr_to_jlong(awt_display));
 763 
 764     /* set awt_numScreens, and whether or not we're using Xinerama */
 765     xineramaInit();
 766 
 767     if (!usingXinerama) {
 768         awt_numScreens =  XScreenCount(awt_display);
 769     }
 770 
 771     DTRACE_PRINTLN1("allocating %i screens\n", awt_numScreens);
 772     /* Allocate screen data structure array */
 773     x11Screens = calloc(awt_numScreens, sizeof(AwtScreenData));
 774     if (x11Screens == NULL) {
 775         JNU_ThrowOutOfMemoryError((JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2),
 776                                   NULL);
 777         return NULL;
 778     }
 779 
 780     for (i = 0; i < awt_numScreens; i++) {
 781         if (usingXinerama) {
 782             /* All Xinerama screens use the same X11 root for now */


 889 
 890 /*
 891  * Class:     sun_awt_X11GraphicsDevice
 892  * Method:    getDisplay
 893  * Signature: ()J
 894  */
 895 JNIEXPORT jlong JNICALL
 896 Java_sun_awt_X11GraphicsDevice_getDisplay(JNIEnv *env, jobject this)
 897 {
 898 #ifdef HEADLESS
 899     return NULL;
 900 #else
 901     return ptr_to_jlong(awt_display);
 902 #endif /* !HEADLESS */
 903 }
 904 
 905 #ifdef MITSHM
 906 
 907 static jint canUseShmExt = UNSET_MITSHM;
 908 static jint canUseShmExtPixmaps = UNSET_MITSHM;

















 909 
 910 void TryInitMITShm(JNIEnv *env, jint *shmExt, jint *shmPixmaps) {
 911     XShmSegmentInfo shminfo;
 912     int XShmMajor, XShmMinor;
 913     int a, b, c;
 914 
 915     AWT_LOCK();
 916     if (canUseShmExt != UNSET_MITSHM) {
 917         *shmExt = canUseShmExt;
 918         *shmPixmaps = canUseShmExtPixmaps;
 919         AWT_UNLOCK();
 920         return;
 921     }
 922 
 923     *shmExt = canUseShmExt = CANT_USE_MITSHM;
 924     *shmPixmaps = canUseShmExtPixmaps = CANT_USE_MITSHM;
 925 
 926     /**
 927      * XShmQueryExtension returns False in remote server case.
 928      * Unfortunately it also returns True in ssh case, so


 931     if (XShmQueryExtension(awt_display)) {
 932         shminfo.shmid = shmget(IPC_PRIVATE, 0x10000, IPC_CREAT|0777);
 933         if (shminfo.shmid < 0) {
 934             AWT_UNLOCK();
 935             J2dRlsTraceLn1(J2D_TRACE_ERROR,
 936                            "TryInitMITShm: shmget has failed: %s",
 937                            strerror(errno));
 938             return;
 939         }
 940         shminfo.shmaddr = (char *) shmat(shminfo.shmid, 0, 0);
 941         if (shminfo.shmaddr == ((char *) -1)) {
 942             shmctl(shminfo.shmid, IPC_RMID, 0);
 943             AWT_UNLOCK();
 944             J2dRlsTraceLn1(J2D_TRACE_ERROR,
 945                            "TryInitMITShm: shmat has failed: %s",
 946                            strerror(errno));
 947             return;
 948         }
 949         shminfo.readOnly = True;
 950 

 951         /**
 952          * XShmAttachHandler will set its internal flag to JNI_TRUE, if any Shm error occurs.

 953          */
 954         jobject xShmAttachHandler = JNU_CallStaticMethodByName(env, NULL, "sun/awt/X11/XErrorHandler$XShmAttachHandler",
 955             "getInstance", "()Lsun/awt/X11/XErrorHandler$XShmAttachHandler;").l;
 956         JNU_CallMethodByName(env, NULL, xShmAttachHandler, "setErrorOccurredFlag", "(Z)V", JNI_FALSE);
 957         JNU_CallStaticMethodByName(env, NULL, "sun/awt/X11/XErrorHandlerUtil", "WITH_XERROR_HANDLER",
 958             "(Lsun/awt/X11/XErrorHandler;)V", xShmAttachHandler);
 959         XShmAttach(awt_display, &shminfo);
 960         JNU_CallStaticMethodByName(env, NULL, "sun/awt/X11/XErrorHandlerUtil", "RESTORE_XERROR_HANDLER", "()V");
 961 
 962         /**
 963          * Get rid of the id now to reduce chances of leaking
 964          * system resources.
 965          */
 966         shmctl(shminfo.shmid, IPC_RMID, 0);
 967 
 968         if (JNU_CallMethodByName(env, NULL, xShmAttachHandler, "getErrorOccurredFlag", "()Z").z == JNI_FALSE) {
 969             canUseShmExt = CAN_USE_MITSHM;
 970             /* check if we can use shared pixmaps */
 971             XShmQueryVersion(awt_display, &XShmMajor, &XShmMinor,
 972                              (Bool*)&canUseShmExtPixmaps);
 973             canUseShmExtPixmaps = canUseShmExtPixmaps &&
 974                 (XShmPixmapFormat(awt_display) == ZPixmap);
 975             XShmDetach(awt_display, &shminfo);
 976         }
 977         shmdt(shminfo.shmaddr);
 978         *shmExt = canUseShmExt;
 979         *shmPixmaps = canUseShmExtPixmaps;
 980     }
 981     AWT_UNLOCK();
 982 }
 983 #endif /* MITSHM */
 984 
 985 /*
 986  * Class:     sun_awt_X11GraphicsEnvironment
 987  * Method:    checkShmExt
 988  * Signature: ()I