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
1001 */
1002 JNIEXPORT jint JNICALL
1003 Java_sun_awt_X11GraphicsEnvironment_checkShmExt(JNIEnv *env, jobject this)
1004 {
1005
1006 int shmExt = NOEXT_MITSHM, shmPixmaps;
1007 #ifdef MITSHM
1008 TryInitMITShm(env, &shmExt, &shmPixmaps);
1009 #endif
1010 return shmExt;
1011 }
1012
1013 /*
|
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 jboolean xShmAttachResult = TryXShmAttach(env, awt_display, &shminfo);
952 /**
953 * Get rid of the id now to reduce chances of leaking
954 * system resources.
955 */
956 shmctl(shminfo.shmid, IPC_RMID, 0);
957
958 if (xShmAttachResult == JNI_TRUE) {
959 canUseShmExt = CAN_USE_MITSHM;
960 /* check if we can use shared pixmaps */
961 XShmQueryVersion(awt_display, &XShmMajor, &XShmMinor,
962 (Bool*)&canUseShmExtPixmaps);
963 canUseShmExtPixmaps = canUseShmExtPixmaps &&
964 (XShmPixmapFormat(awt_display) == ZPixmap);
965 XShmDetach(awt_display, &shminfo);
966 }
967 shmdt(shminfo.shmaddr);
968 *shmExt = canUseShmExt;
969 *shmPixmaps = canUseShmExtPixmaps;
970 }
971 AWT_UNLOCK();
972 }
973
974 /*
975 * Must be called with the acquired AWT lock.
976 */
977 jboolean TryXShmAttach(JNIEnv *env, Display *display, XShmSegmentInfo *shminfo) {
978 /*
979 * XShmAttachHandler will set its internal flag to JNI_TRUE, if any Shm error occurs.
980 */
981 jboolean errorOccurredFlag = JNI_FALSE;
982 EXEC_WITH_XERROR_HANDLER(env, "sun/awt/X11/XErrorHandler$XShmAttachHandler",
983 "()Lsun/awt/X11/XErrorHandler$XShmAttachHandler;", JNI_TRUE, errorOccurredFlag,
984 XShmAttach(display, shminfo));
985 return errorOccurredFlag == JNI_FALSE ? JNI_TRUE : JNI_FALSE;
986 }
987 #endif /* MITSHM */
988
989 /*
990 * Class: sun_awt_X11GraphicsEnvironment
991 * Method: checkShmExt
992 * Signature: ()I
993 */
994 JNIEXPORT jint JNICALL
995 Java_sun_awt_X11GraphicsEnvironment_checkShmExt(JNIEnv *env, jobject this)
996 {
997
998 int shmExt = NOEXT_MITSHM, shmPixmaps;
999 #ifdef MITSHM
1000 TryInitMITShm(env, &shmExt, &shmPixmaps);
1001 #endif
1002 return shmExt;
1003 }
1004
1005 /*
|