src/java.desktop/unix/native/libawt_xawt/awt/awt_GraphicsEnv.c
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 54856 4fcc1f1d7dd8
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
   117    short width;
   117    short width;
   118    short height;
   118    short height;
   119 } XineramaScreenInfo;
   119 } XineramaScreenInfo;
   120 
   120 
   121 typedef XineramaScreenInfo* XineramaQueryScreensFunc(Display*, int*);
   121 typedef XineramaScreenInfo* XineramaQueryScreensFunc(Display*, int*);
   122 
   122 static XineramaQueryScreensFunc* XineramaQueryScreens = NULL;
   123 Bool usingXinerama = False;
   123 Bool usingXinerama = False;
   124 XRectangle fbrects[MAXFRAMEBUFFERS];
       
   125 
   124 
   126 JNIEXPORT void JNICALL
   125 JNIEXPORT void JNICALL
   127 Java_sun_awt_X11GraphicsConfig_initIDs (JNIEnv *env, jclass cls)
   126 Java_sun_awt_X11GraphicsConfig_initIDs (JNIEnv *env, jclass cls)
   128 {
   127 {
   129     x11GraphicsConfigIDs.aData = NULL;
   128     x11GraphicsConfigIDs.aData = NULL;
   165                                 mask, vinfo, &visualsMatched);
   164                                 mask, vinfo, &visualsMatched);
   166     if (visualList) {
   165     if (visualList) {
   167         int id = -1;
   166         int id = -1;
   168         VisualID defaultVisual = XVisualIDFromVisual(DefaultVisual(awt_display, vinfo->screen));
   167         VisualID defaultVisual = XVisualIDFromVisual(DefaultVisual(awt_display, vinfo->screen));
   169         defaultConfig = ZALLOC(_AwtGraphicsConfigData);
   168         defaultConfig = ZALLOC(_AwtGraphicsConfigData);
       
   169         if (defaultConfig == NULL) {
       
   170             XFree(visualList);
       
   171             return NULL;
       
   172         }
   170         for (i = 0; i < visualsMatched; i++) {
   173         for (i = 0; i < visualsMatched; i++) {
   171             memcpy(&defaultConfig->awt_visInfo, &visualList[i], sizeof(XVisualInfo));
   174             memcpy(&defaultConfig->awt_visInfo, &visualList[i], sizeof(XVisualInfo));
   172             defaultConfig->awt_depth = visualList[i].depth;
   175             defaultConfig->awt_depth = visualList[i].depth;
   173 
   176 
   174             /* we can't use awtJNI_CreateColorData here, because it'll pull,
   177             /* we can't use awtJNI_CreateColorData here, because it'll pull,
   446             /* Skip the non-supported 12-bit TrueColor visual */
   449             /* Skip the non-supported 12-bit TrueColor visual */
   447             continue;
   450             continue;
   448         } else {
   451         } else {
   449             ind = nConfig++;
   452             ind = nConfig++;
   450         }
   453         }
   451         graphicsConfigs [ind] = ZALLOC (_AwtGraphicsConfigData);
   454         graphicsConfigs[ind] = ZALLOC (_AwtGraphicsConfigData);
   452         graphicsConfigs [ind]->awt_depth = pVITrue [i].depth;
   455         if (graphicsConfigs[ind] == NULL) {
       
   456             JNU_ThrowOutOfMemoryError(env, "allocation in getAllConfigs failed");
       
   457             goto cleanup;
       
   458         }
       
   459         graphicsConfigs[ind]->awt_depth = pVITrue [i].depth;
   453         memcpy (&graphicsConfigs [ind]->awt_visInfo, &pVITrue [i],
   460         memcpy (&graphicsConfigs [ind]->awt_visInfo, &pVITrue [i],
   454                 sizeof (XVisualInfo));
   461                 sizeof (XVisualInfo));
   455         if (xrenderFindVisualFormat != NULL) {
   462         if (xrenderFindVisualFormat != NULL) {
   456             XRenderPictFormat *format = xrenderFindVisualFormat (awt_display,
   463             XRenderPictFormat *format = xrenderFindVisualFormat (awt_display,
   457                                                                  pVITrue [i].visual);
   464                                                                  pVITrue [i].visual);
   481             XVisualIDFromVisual(defaultConfig->awt_visInfo.visual)) {
   488             XVisualIDFromVisual(defaultConfig->awt_visInfo.visual)) {
   482             continue;
   489             continue;
   483         } else {
   490         } else {
   484             ind = nConfig++;
   491             ind = nConfig++;
   485         }
   492         }
   486         graphicsConfigs [ind] = ZALLOC (_AwtGraphicsConfigData);
   493         graphicsConfigs[ind] = ZALLOC (_AwtGraphicsConfigData);
   487         graphicsConfigs [ind]->awt_depth = pVI8p [i].depth;
   494         if (graphicsConfigs[ind] == NULL) {
       
   495             JNU_ThrowOutOfMemoryError(env, "allocation in getAllConfigs failed");
       
   496             goto cleanup;
       
   497         }
       
   498         graphicsConfigs[ind]->awt_depth = pVI8p [i].depth;
   488         memcpy (&graphicsConfigs [ind]->awt_visInfo, &pVI8p [i],
   499         memcpy (&graphicsConfigs [ind]->awt_visInfo, &pVI8p [i],
   489                 sizeof (XVisualInfo));
   500                 sizeof (XVisualInfo));
   490     }
   501     }
   491 
   502 
   492     for (i = 0; i < n12p; i++) {
   503     for (i = 0; i < n12p; i++) {
   494             XVisualIDFromVisual(defaultConfig->awt_visInfo.visual)) {
   505             XVisualIDFromVisual(defaultConfig->awt_visInfo.visual)) {
   495             continue;
   506             continue;
   496         } else {
   507         } else {
   497             ind = nConfig++;
   508             ind = nConfig++;
   498         }
   509         }
   499         graphicsConfigs [ind] = ZALLOC (_AwtGraphicsConfigData);
   510         graphicsConfigs[ind] = ZALLOC (_AwtGraphicsConfigData);
   500         graphicsConfigs [ind]->awt_depth = pVI12p [i].depth;
   511         if (graphicsConfigs[ind] == NULL) {
       
   512             JNU_ThrowOutOfMemoryError(env, "allocation in getAllConfigs failed");
       
   513             goto cleanup;
       
   514         }
       
   515         graphicsConfigs[ind]->awt_depth = pVI12p [i].depth;
   501         memcpy (&graphicsConfigs [ind]->awt_visInfo, &pVI12p [i],
   516         memcpy (&graphicsConfigs [ind]->awt_visInfo, &pVI12p [i],
   502                 sizeof (XVisualInfo));
   517                 sizeof (XVisualInfo));
   503     }
   518     }
   504 
   519 
   505     for (i = 0; i < n8s; i++) {
   520     for (i = 0; i < n8s; i++) {
   507             XVisualIDFromVisual(defaultConfig->awt_visInfo.visual)) {
   522             XVisualIDFromVisual(defaultConfig->awt_visInfo.visual)) {
   508             continue;
   523             continue;
   509         } else {
   524         } else {
   510             ind = nConfig++;
   525             ind = nConfig++;
   511         }
   526         }
   512         graphicsConfigs [ind] = ZALLOC (_AwtGraphicsConfigData);
   527         graphicsConfigs[ind] = ZALLOC (_AwtGraphicsConfigData);
   513         graphicsConfigs [ind]->awt_depth = pVI8s [i].depth;
   528         if (graphicsConfigs[ind] == NULL) {
       
   529             JNU_ThrowOutOfMemoryError(env, "allocation in getAllConfigs failed");
       
   530             goto cleanup;
       
   531         }
       
   532         graphicsConfigs[ind]->awt_depth = pVI8s [i].depth;
   514         memcpy (&graphicsConfigs [ind]->awt_visInfo, &pVI8s [i],
   533         memcpy (&graphicsConfigs [ind]->awt_visInfo, &pVI8s [i],
   515                 sizeof (XVisualInfo));
   534                 sizeof (XVisualInfo));
   516     }
   535     }
   517 
   536 
   518     for (i = 0; i < n8gs; i++) {
   537     for (i = 0; i < n8gs; i++) {
   520             XVisualIDFromVisual(defaultConfig->awt_visInfo.visual)) {
   539             XVisualIDFromVisual(defaultConfig->awt_visInfo.visual)) {
   521             continue;
   540             continue;
   522         } else {
   541         } else {
   523             ind = nConfig++;
   542             ind = nConfig++;
   524         }
   543         }
   525         graphicsConfigs [ind] = ZALLOC (_AwtGraphicsConfigData);
   544         graphicsConfigs[ind] = ZALLOC (_AwtGraphicsConfigData);
   526         graphicsConfigs [ind]->awt_depth = pVI8gs [i].depth;
   545         if (graphicsConfigs[ind] == NULL) {
       
   546             JNU_ThrowOutOfMemoryError(env, "allocation in getAllConfigs failed");
       
   547             goto cleanup;
       
   548         }
       
   549         graphicsConfigs[ind]->awt_depth = pVI8gs [i].depth;
   527         memcpy (&graphicsConfigs [ind]->awt_visInfo, &pVI8gs [i],
   550         memcpy (&graphicsConfigs [ind]->awt_visInfo, &pVI8gs [i],
   528                 sizeof (XVisualInfo));
   551                 sizeof (XVisualInfo));
   529     }
   552     }
   530 
   553 
   531     for (i = 0; i < n8sg; i++) {
   554     for (i = 0; i < n8sg; i++) {
   533             XVisualIDFromVisual(defaultConfig->awt_visInfo.visual)) {
   556             XVisualIDFromVisual(defaultConfig->awt_visInfo.visual)) {
   534             continue;
   557             continue;
   535         } else {
   558         } else {
   536             ind = nConfig++;
   559             ind = nConfig++;
   537         }
   560         }
   538         graphicsConfigs [ind] = ZALLOC (_AwtGraphicsConfigData);
   561         graphicsConfigs[ind] = ZALLOC (_AwtGraphicsConfigData);
   539         graphicsConfigs [ind]->awt_depth = pVI8sg [i].depth;
   562         if (graphicsConfigs[ind] == NULL) {
       
   563             JNU_ThrowOutOfMemoryError(env, "allocation in getAllConfigs failed");
       
   564             goto cleanup;
       
   565         }
       
   566         graphicsConfigs[ind]->awt_depth = pVI8sg [i].depth;
   540         memcpy (&graphicsConfigs [ind]->awt_visInfo, &pVI8sg [i],
   567         memcpy (&graphicsConfigs [ind]->awt_visInfo, &pVI8sg [i],
   541                 sizeof (XVisualInfo));
   568                 sizeof (XVisualInfo));
   542     }
   569     }
   543 
   570 
   544     for (i = 0; i < n1sg; i++) {
   571     for (i = 0; i < n1sg; i++) {
   546             XVisualIDFromVisual(defaultConfig->awt_visInfo.visual)) {
   573             XVisualIDFromVisual(defaultConfig->awt_visInfo.visual)) {
   547             continue;
   574             continue;
   548         } else {
   575         } else {
   549             ind = nConfig++;
   576             ind = nConfig++;
   550         }
   577         }
   551         graphicsConfigs [ind] = ZALLOC (_AwtGraphicsConfigData);
   578         graphicsConfigs[ind] = ZALLOC (_AwtGraphicsConfigData);
   552         graphicsConfigs [ind]->awt_depth = pVI1sg [i].depth;
   579         if (graphicsConfigs[ind] == NULL) {
       
   580             JNU_ThrowOutOfMemoryError(env, "allocation in getAllConfigs failed");
       
   581             goto cleanup;
       
   582         }
       
   583         graphicsConfigs[ind]->awt_depth = pVI1sg [i].depth;
   553         memcpy (&graphicsConfigs [ind]->awt_visInfo, &pVI1sg [i],
   584         memcpy (&graphicsConfigs [ind]->awt_visInfo, &pVI1sg [i],
   554                 sizeof (XVisualInfo));
   585                 sizeof (XVisualInfo));
   555     }
   586     }
   556 
   587 
       
   588     screenDataPtr->numConfigs = nConfig;
       
   589     screenDataPtr->configs = graphicsConfigs;
       
   590 
       
   591 cleanup:
   557     if (n8p != 0)
   592     if (n8p != 0)
   558        XFree (pVI8p);
   593        XFree (pVI8p);
   559     if (n12p != 0)
   594     if (n12p != 0)
   560        XFree (pVI12p);
   595        XFree (pVI12p);
   561     if (n8s != 0)
   596     if (n8s != 0)
   565     if (n8sg != 0)
   600     if (n8sg != 0)
   566        XFree (pVI8sg);
   601        XFree (pVI8sg);
   567     if (n1sg != 0)
   602     if (n1sg != 0)
   568        XFree (pVI1sg);
   603        XFree (pVI1sg);
   569 
   604 
   570     screenDataPtr->numConfigs = nConfig;
       
   571     screenDataPtr->configs = graphicsConfigs;
       
   572 
       
   573     AWT_UNLOCK ();
   605     AWT_UNLOCK ();
   574 }
   606 }
   575 
   607 
   576 #ifndef HEADLESS
   608 #ifndef HEADLESS
   577 
   609 
   584     Bool gotXinExt = False;
   616     Bool gotXinExt = False;
   585     void* libHandle = NULL;
   617     void* libHandle = NULL;
   586     int32_t locNumScr = 0;
   618     int32_t locNumScr = 0;
   587     XineramaScreenInfo *xinInfo;
   619     XineramaScreenInfo *xinInfo;
   588     char* XineramaQueryScreensName = "XineramaQueryScreens";
   620     char* XineramaQueryScreensName = "XineramaQueryScreens";
   589     XineramaQueryScreensFunc* XineramaQueryScreens = NULL;
       
   590 
   621 
   591     gotXinExt = XQueryExtension(awt_display, XinExtName, &major_opcode,
   622     gotXinExt = XQueryExtension(awt_display, XinExtName, &major_opcode,
   592                                 &first_event, &first_error);
   623                                 &first_event, &first_error);
   593 
   624 
   594     if (!gotXinExt) {
   625     if (!gotXinExt) {
   610     }
   641     }
   611     if (libHandle != NULL) {
   642     if (libHandle != NULL) {
   612         XineramaQueryScreens = (XineramaQueryScreensFunc*)
   643         XineramaQueryScreens = (XineramaQueryScreensFunc*)
   613             dlsym(libHandle, XineramaQueryScreensName);
   644             dlsym(libHandle, XineramaQueryScreensName);
   614 
   645 
   615         if (XineramaQueryScreens != NULL) {
   646         if (XineramaQueryScreens == NULL) {
       
   647             DTRACE_PRINTLN("couldn't load XineramaQueryScreens symbol");
       
   648             dlclose(libHandle);
       
   649         } else {
   616             DTRACE_PRINTLN("calling XineramaQueryScreens func");
   650             DTRACE_PRINTLN("calling XineramaQueryScreens func");
   617             xinInfo = (*XineramaQueryScreens)(awt_display, &locNumScr);
   651             xinInfo = (*XineramaQueryScreens)(awt_display, &locNumScr);
   618             if (xinInfo != NULL && locNumScr > XScreenCount(awt_display)) {
   652             if (xinInfo != NULL) {
   619                 int32_t idx;
   653                 if (locNumScr > XScreenCount(awt_display)) {
   620                 DTRACE_PRINTLN("Enabling Xinerama support");
   654                     DTRACE_PRINTLN("Enabling Xinerama support");
   621                 usingXinerama = True;
   655                     usingXinerama = True;
   622                 /* set global number of screens */
   656                     /* set global number of screens */
   623                 DTRACE_PRINTLN1(" num screens = %i\n", locNumScr);
   657                     DTRACE_PRINTLN1(" num screens = %i\n", locNumScr);
   624                 awt_numScreens = locNumScr;
   658                     awt_numScreens = locNumScr;
   625 
   659                 } else {
   626                 /* stuff values into fbrects */
   660                     DTRACE_PRINTLN("XineramaQueryScreens <= XScreenCount");
   627                 for (idx = 0; idx < awt_numScreens; idx++) {
       
   628                     DASSERT(xinInfo[idx].screen_number == idx);
       
   629 
       
   630                     fbrects[idx].width = xinInfo[idx].width;
       
   631                     fbrects[idx].height = xinInfo[idx].height;
       
   632                     fbrects[idx].x = xinInfo[idx].x_org;
       
   633                     fbrects[idx].y = xinInfo[idx].y_org;
       
   634                 }
   661                 }
       
   662                 XFree(xinInfo);
   635             } else {
   663             } else {
   636                 DTRACE_PRINTLN((xinInfo == NULL) ?
   664                 DTRACE_PRINTLN("calling XineramaQueryScreens didn't work");
   637                                "calling XineramaQueryScreens didn't work" :
       
   638                                "XineramaQueryScreens <= XScreenCount"
       
   639                                );
       
   640             }
   665             }
   641         } else {
   666         }
   642             DTRACE_PRINTLN("couldn't load XineramaQueryScreens symbol");
       
   643         }
       
   644         dlclose(libHandle);
       
   645     } else {
   667     } else {
   646         DTRACE_PRINTLN1("\ncouldn't open shared library: %s\n", dlerror());
   668         DTRACE_PRINTLN1("\ncouldn't open shared library: %s\n", dlerror());
   647     }
   669     }
   648 }
   670 }
   649 #endif /* HEADLESS */
   671 #endif /* HEADLESS */
  1301 #else
  1323 #else
  1302     jclass clazz;
  1324     jclass clazz;
  1303     jmethodID mid;
  1325     jmethodID mid;
  1304     jobject bounds = NULL;
  1326     jobject bounds = NULL;
  1305     AwtGraphicsConfigDataPtr adata;
  1327     AwtGraphicsConfigDataPtr adata;
       
  1328     int32_t locNumScr = 0;
       
  1329     XineramaScreenInfo *xinInfo;
  1306 
  1330 
  1307     adata = (AwtGraphicsConfigDataPtr)
  1331     adata = (AwtGraphicsConfigDataPtr)
  1308         JNU_GetLongFieldAsPtr(env, this, x11GraphicsConfigIDs.aData);
  1332         JNU_GetLongFieldAsPtr(env, this, x11GraphicsConfigIDs.aData);
  1309 
  1333 
  1310     clazz = (*env)->FindClass(env, "java/awt/Rectangle");
  1334     clazz = (*env)->FindClass(env, "java/awt/Rectangle");
  1311     CHECK_NULL_RETURN(clazz, NULL);
  1335     CHECK_NULL_RETURN(clazz, NULL);
  1312     mid = (*env)->GetMethodID(env, clazz, "<init>", "(IIII)V");
  1336     mid = (*env)->GetMethodID(env, clazz, "<init>", "(IIII)V");
  1313     if (mid != NULL) {
  1337     if (mid != NULL) {
  1314         if (usingXinerama) {
  1338         if (usingXinerama) {
  1315             if (0 <= screen && screen < awt_numScreens) {
  1339             if (0 <= screen && screen < awt_numScreens) {
  1316                 bounds = (*env)->NewObject(env, clazz, mid, fbrects[screen].x,
  1340                 AWT_LOCK();
  1317                                                             fbrects[screen].y,
  1341                 xinInfo = (*XineramaQueryScreens)(awt_display, &locNumScr);
  1318                                                             fbrects[screen].width,
  1342                 AWT_UNLOCK();
  1319                                                             fbrects[screen].height);
  1343                 if (xinInfo != NULL && locNumScr > 0) {
       
  1344                     if (screen >= locNumScr) {
       
  1345                         screen = 0; // fallback to the main screen
       
  1346                     }
       
  1347                     DASSERT(xinInfo[screen].screen_number == screen);
       
  1348                     bounds = (*env)->NewObject(env, clazz, mid,
       
  1349                                                xinInfo[screen].x_org,
       
  1350                                                xinInfo[screen].y_org,
       
  1351                                                xinInfo[screen].width,
       
  1352                                                xinInfo[screen].height);
       
  1353                     XFree(xinInfo);
       
  1354                 }
  1320             } else {
  1355             } else {
  1321                 jclass exceptionClass = (*env)->FindClass(env, "java/lang/IllegalArgumentException");
  1356                 jclass exceptionClass = (*env)->FindClass(env, "java/lang/IllegalArgumentException");
  1322                 if (exceptionClass != NULL) {
  1357                 if (exceptionClass != NULL) {
  1323                     (*env)->ThrowNew(env, exceptionClass, "Illegal screen index");
  1358                     (*env)->ThrowNew(env, exceptionClass, "Illegal screen index");
  1324                 }
  1359                 }
  1325             }
  1360             }
  1326         } else {
  1361         }
       
  1362         if (!bounds) {
       
  1363             // Xinerama cannot provide correct bounds, will try X11
  1327             XWindowAttributes xwa;
  1364             XWindowAttributes xwa;
  1328             memset(&xwa, 0, sizeof(xwa));
  1365             memset(&xwa, 0, sizeof(xwa));
  1329 
  1366 
  1330             AWT_LOCK ();
  1367             AWT_LOCK ();
  1331             XGetWindowAttributes(awt_display,
  1368             XGetWindowAttributes(awt_display,