1 /* 2 * Copyright (c) 2003, 2016, 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 23 * questions. 24 */ 25 26 27 /** 28 * Ported from awt_wm.c, SCCS v1.11, author Valeriy Ushakov 29 * Author: Denis Mikhalkin 30 */ 31 package sun.awt.X11; 32 33 import sun.awt.IconInfo; 34 import jdk.internal.misc.Unsafe; 35 import java.awt.Insets; 36 import java.awt.Frame; 37 import java.awt.Rectangle; 38 import java.util.Collection; 39 import java.util.HashMap; 40 import java.util.LinkedList; 41 import java.util.regex.Matcher; 42 import java.util.regex.Pattern; 43 import sun.util.logging.PlatformLogger; 44 45 46 /** 47 * Class incapsulating knowledge about window managers in general 48 * Descendants should provide some information about specific window manager. 49 */ 50 final class XWM 51 { 52 53 private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XWM"); 54 private static final PlatformLogger insLog = PlatformLogger.getLogger("sun.awt.X11.insets.XWM"); 55 private static final PlatformLogger stateLog = PlatformLogger.getLogger("sun.awt.X11.states.XWM"); 56 57 static final XAtom XA_MWM_HINTS = new XAtom(); 58 59 private static Unsafe unsafe = XlibWrapper.unsafe; 60 61 62 /* Good old ICCCM */ 63 static XAtom XA_WM_STATE = new XAtom(); 64 65 66 XAtom XA_UTF8_STRING = XAtom.get("UTF8_STRING"); /* like STRING but encoding is UTF-8 */ 67 68 /* Currently we only care about max_v and max_h in _NET_WM_STATE */ 69 static final int AWT_NET_N_KNOWN_STATES=2; 70 71 /* Enlightenment */ 72 static final XAtom XA_E_FRAME_SIZE = new XAtom(); 73 74 /* KWin (KDE2) */ 75 static final XAtom XA_KDE_NET_WM_FRAME_STRUT = new XAtom(); 76 77 /* KWM (KDE 1.x) OBSOLETE??? */ 78 static final XAtom XA_KWM_WIN_ICONIFIED = new XAtom(); 79 static final XAtom XA_KWM_WIN_MAXIMIZED = new XAtom(); 80 81 /* OpenLook */ 82 static final XAtom XA_OL_DECOR_DEL = new XAtom(); 83 static final XAtom XA_OL_DECOR_HEADER = new XAtom(); 84 static final XAtom XA_OL_DECOR_RESIZE = new XAtom(); 85 static final XAtom XA_OL_DECOR_PIN = new XAtom(); 86 static final XAtom XA_OL_DECOR_CLOSE = new XAtom(); 87 88 /* EWMH */ 89 static final XAtom XA_NET_FRAME_EXTENTS = new XAtom(); 90 static final XAtom XA_NET_REQUEST_FRAME_EXTENTS = new XAtom(); 91 92 static final int 93 UNDETERMINED_WM = 1, 94 NO_WM = 2, 95 OTHER_WM = 3, 96 OPENLOOK_WM = 4, 97 MOTIF_WM = 5, 98 CDE_WM = 6, 99 ENLIGHTEN_WM = 7, 100 KDE2_WM = 8, 101 SAWFISH_WM = 9, 102 ICE_WM = 10, 103 METACITY_WM = 11, 104 COMPIZ_WM = 12, 105 LG3D_WM = 13, 106 CWM_WM = 14, 107 MUTTER_WM = 15, 108 UNITY_COMPIZ_WM = 16; 109 public String toString() { 110 switch (WMID) { 111 case NO_WM: 112 return "NO WM"; 113 case OTHER_WM: 114 return "Other WM"; 115 case OPENLOOK_WM: 116 return "OPENLOOK"; 117 case MOTIF_WM: 118 return "MWM"; 119 case CDE_WM: 120 return "DTWM"; 121 case ENLIGHTEN_WM: 122 return "Enlightenment"; 123 case KDE2_WM: 124 return "KWM2"; 125 case SAWFISH_WM: 126 return "Sawfish"; 127 case ICE_WM: 128 return "IceWM"; 129 case METACITY_WM: 130 return "Metacity"; 131 case COMPIZ_WM: 132 return "Compiz"; 133 case UNITY_COMPIZ_WM: 134 return "Unity Compiz"; 135 case LG3D_WM: 136 return "LookingGlass"; 137 case CWM_WM: 138 return "CWM"; 139 case MUTTER_WM: 140 return "Mutter"; 141 case UNDETERMINED_WM: 142 default: 143 return "Undetermined WM"; 144 } 145 } 146 147 148 int WMID; 149 static final Insets zeroInsets = new Insets(0, 0, 0, 0); 150 static final Insets defaultInsets = new Insets(25, 5, 5, 5); 151 152 XWM(int WMID) { 153 this.WMID = WMID; 154 initializeProtocols(); 155 if (log.isLoggable(PlatformLogger.Level.FINE)) { 156 log.fine("Window manager: " + toString()); 157 } 158 } 159 int getID() { 160 return WMID; 161 } 162 163 164 static Insets normalize(Insets insets) { 165 if (insets.top > 64 || insets.top < 0) { 166 insets.top = 28; 167 } 168 if (insets.left > 32 || insets.left < 0) { 169 insets.left = 6; 170 } 171 if (insets.right > 32 || insets.right < 0) { 172 insets.right = 6; 173 } 174 if (insets.bottom > 32 || insets.bottom < 0) { 175 insets.bottom = 6; 176 } 177 return insets; 178 } 179 180 static XNETProtocol g_net_protocol = null; 181 static XWINProtocol g_win_protocol = null; 182 static boolean isNetWMName(String name) { 183 if (g_net_protocol != null) { 184 return g_net_protocol.isWMName(name); 185 } else { 186 return false; 187 } 188 } 189 190 static void initAtoms() { 191 final Object[][] atomInitList ={ 192 { XA_WM_STATE, "WM_STATE" }, 193 194 { XA_KDE_NET_WM_FRAME_STRUT, "_KDE_NET_WM_FRAME_STRUT" }, 195 196 { XA_E_FRAME_SIZE, "_E_FRAME_SIZE" }, 197 198 { XA_KWM_WIN_ICONIFIED, "KWM_WIN_ICONIFIED" }, 199 { XA_KWM_WIN_MAXIMIZED, "KWM_WIN_MAXIMIZED" }, 200 201 { XA_OL_DECOR_DEL, "_OL_DECOR_DEL" }, 202 { XA_OL_DECOR_HEADER, "_OL_DECOR_HEADER" }, 203 { XA_OL_DECOR_RESIZE, "_OL_DECOR_RESIZE" }, 204 { XA_OL_DECOR_PIN, "_OL_DECOR_PIN" }, 205 { XA_OL_DECOR_CLOSE, "_OL_DECOR_CLOSE" }, 206 { XA_MWM_HINTS, "_MOTIF_WM_HINTS" }, 207 { XA_NET_FRAME_EXTENTS, "_NET_FRAME_EXTENTS" }, 208 { XA_NET_REQUEST_FRAME_EXTENTS, "_NET_REQUEST_FRAME_EXTENTS" }, 209 }; 210 211 String[] names = new String[atomInitList.length]; 212 for (int index = 0; index < names.length; index++) { 213 names[index] = (String)atomInitList[index][1]; 214 } 215 216 int atomSize = XAtom.getAtomSize(); 217 long atoms = unsafe.allocateMemory(names.length*atomSize); 218 XToolkit.awtLock(); 219 try { 220 int status = XlibWrapper.XInternAtoms(XToolkit.getDisplay(), names, false, atoms); 221 if (status == 0) { 222 return; 223 } 224 for (int atom = 0, atomPtr = 0; atom < names.length; atom++, atomPtr += atomSize) { 225 ((XAtom)(atomInitList[atom][0])).setValues(XToolkit.getDisplay(), names[atom], XAtom.getAtom(atoms + atomPtr)); 226 } 227 } finally { 228 XToolkit.awtUnlock(); 229 unsafe.freeMemory(atoms); 230 } 231 } 232 233 /* 234 * MUST BE CALLED UNDER AWTLOCK. 235 * 236 * If *any* window manager is running? 237 * 238 * According to ICCCM 2.0 section 4.3. 239 * WM will acquire ownership of a selection named WM_Sn, where n is 240 * the screen number. 241 * 242 * No selection owner, but, perhaps it is not ICCCM compliant WM 243 * (e.g. CDE/Sawfish). 244 * Try selecting for SubstructureRedirect, that only one client 245 * can select for, and if the request fails, than some other WM is 246 * already running. 247 * 248 * We also treat eXcursion as NO_WM. 249 */ 250 private static boolean isNoWM() { 251 /* 252 * Quick checks for specific servers. 253 */ 254 String vendor_string = XlibWrapper.ServerVendor(XToolkit.getDisplay()); 255 if (vendor_string.indexOf("eXcursion") != -1) { 256 /* 257 * Use NO_WM since in all other aspects eXcursion is like not 258 * having a window manager running. I.e. it does not reparent 259 * top level shells. 260 */ 261 if (insLog.isLoggable(PlatformLogger.Level.FINER)) { 262 insLog.finer("eXcursion means NO_WM"); 263 } 264 return true; 265 } 266 267 XSetWindowAttributes substruct = new XSetWindowAttributes(); 268 try { 269 /* 270 * Let's check an owner of WM_Sn selection for the default screen. 271 */ 272 final long default_screen_number = 273 XlibWrapper.DefaultScreen(XToolkit.getDisplay()); 274 final String selection_name = "WM_S" + default_screen_number; 275 276 long selection_owner = 277 XlibWrapper.XGetSelectionOwner(XToolkit.getDisplay(), 278 XAtom.get(selection_name).getAtom()); 279 if (insLog.isLoggable(PlatformLogger.Level.FINER)) { 280 insLog.finer("selection owner of " + selection_name 281 + " is " + selection_owner); 282 } 283 284 if (selection_owner != XConstants.None) { 285 return false; 286 } 287 288 winmgr_running = false; 289 substruct.set_event_mask(XConstants.SubstructureRedirectMask); 290 291 XErrorHandlerUtil.WITH_XERROR_HANDLER(detectWMHandler); 292 XlibWrapper.XChangeWindowAttributes(XToolkit.getDisplay(), 293 XToolkit.getDefaultRootWindow(), 294 XConstants.CWEventMask, 295 substruct.pData); 296 XErrorHandlerUtil.RESTORE_XERROR_HANDLER(); 297 298 /* 299 * If no WM is running then our selection for SubstructureRedirect 300 * succeeded and needs to be undone (hey we are *not* a WM ;-). 301 */ 302 if (!winmgr_running) { 303 substruct.set_event_mask(0); 304 XlibWrapper.XChangeWindowAttributes(XToolkit.getDisplay(), 305 XToolkit.getDefaultRootWindow(), 306 XConstants.CWEventMask, 307 substruct.pData); 308 if (insLog.isLoggable(PlatformLogger.Level.FINER)) { 309 insLog.finer("It looks like there is no WM thus NO_WM"); 310 } 311 } 312 313 return !winmgr_running; 314 } finally { 315 substruct.dispose(); 316 } 317 } 318 319 static XAtom XA_ENLIGHTENMENT_COMMS = new XAtom("ENLIGHTENMENT_COMMS", false); 320 /* 321 * Helper function for isEnlightenment(). 322 * Enlightenment uses STRING property for its comms window id. Gaaa! 323 * The property is ENLIGHTENMENT_COMMS, STRING/8 and the string format 324 * is "WINID %8x". Gee, I haven't been using scanf for *ages*... :-) 325 */ 326 static long getECommsWindowIDProperty(long window) { 327 328 if (!XA_ENLIGHTENMENT_COMMS.isInterned()) { 329 return 0; 330 } 331 332 WindowPropertyGetter getter = 333 new WindowPropertyGetter(window, XA_ENLIGHTENMENT_COMMS, 0, 14, false, 334 XAtom.XA_STRING); 335 try { 336 int status = getter.execute(XErrorHandler.IgnoreBadWindowHandler.getInstance()); 337 if (status != XConstants.Success || getter.getData() == 0) { 338 return 0; 339 } 340 341 if (getter.getActualType() != XAtom.XA_STRING 342 || getter.getActualFormat() != 8 343 || getter.getNumberOfItems() != 14 || getter.getBytesAfter() != 0) 344 { 345 return 0; 346 } 347 348 // Convert data to String, ASCII 349 byte[] bytes = XlibWrapper.getStringBytes(getter.getData()); 350 String id = new String(bytes); 351 352 if (log.isLoggable(PlatformLogger.Level.FINER)) { 353 log.finer("ENLIGHTENMENT_COMMS is " + id); 354 } 355 356 // Parse WINID 357 Pattern winIdPat = Pattern.compile("WINID\\s+(\\p{XDigit}{0,8})"); 358 try { 359 Matcher match = winIdPat.matcher(id); 360 if (match.matches()) { 361 if (log.isLoggable(PlatformLogger.Level.FINEST)) { 362 log.finest("Match group count: " + match.groupCount()); 363 } 364 String longId = match.group(1); 365 if (log.isLoggable(PlatformLogger.Level.FINEST)) { 366 log.finest("Match group 1 " + longId); 367 } 368 long winid = Long.parseLong(longId, 16); 369 if (log.isLoggable(PlatformLogger.Level.FINER)) { 370 log.finer("Enlightenment communication window " + winid); 371 } 372 return winid; 373 } else { 374 log.finer("ENLIGHTENMENT_COMMS has wrong format"); 375 return 0; 376 } 377 } catch (Exception e) { 378 if (log.isLoggable(PlatformLogger.Level.FINER)) { 379 e.printStackTrace(); 380 } 381 return 0; 382 } 383 } finally { 384 getter.dispose(); 385 } 386 } 387 388 /* 389 * Is Enlightenment WM running? Congruent to awt_wm_checkAnchor, but 390 * uses STRING property peculiar to Enlightenment. 391 */ 392 static boolean isEnlightenment() { 393 394 long root_xref = getECommsWindowIDProperty(XToolkit.getDefaultRootWindow()); 395 if (root_xref == 0) { 396 return false; 397 } 398 399 long self_xref = getECommsWindowIDProperty(root_xref); 400 if (self_xref != root_xref) { 401 return false; 402 } 403 404 return true; 405 } 406 407 /* 408 * Is CDE running? 409 * 410 * XXX: This is hairy... CDE is MWM as well. It seems we simply test 411 * for default setup and will be bitten if user changes things... 412 * 413 * Check for _DT_SM_WINDOW_INFO(_DT_SM_WINDOW_INFO) on root. Take the 414 * second element of the property and check for presence of 415 * _DT_SM_STATE_INFO(_DT_SM_STATE_INFO) on that window. 416 * 417 * XXX: Any header that defines this structures??? 418 */ 419 static final XAtom XA_DT_SM_WINDOW_INFO = new XAtom("_DT_SM_WINDOW_INFO", false); 420 static final XAtom XA_DT_SM_STATE_INFO = new XAtom("_DT_SM_STATE_INFO", false); 421 static boolean isCDE() { 422 423 if (!XA_DT_SM_WINDOW_INFO.isInterned()) { 424 if (log.isLoggable(PlatformLogger.Level.FINER)) { 425 log.finer("{0} is not interned", XA_DT_SM_WINDOW_INFO); 426 } 427 return false; 428 } 429 430 WindowPropertyGetter getter = 431 new WindowPropertyGetter(XToolkit.getDefaultRootWindow(), 432 XA_DT_SM_WINDOW_INFO, 0, 2, 433 false, XA_DT_SM_WINDOW_INFO); 434 try { 435 int status = getter.execute(); 436 if (status != XConstants.Success || getter.getData() == 0) { 437 log.finer("Getting of _DT_SM_WINDOW_INFO is not successfull"); 438 return false; 439 } 440 if (getter.getActualType() != XA_DT_SM_WINDOW_INFO.getAtom() 441 || getter.getActualFormat() != 32 442 || getter.getNumberOfItems() != 2 || getter.getBytesAfter() != 0) 443 { 444 log.finer("Wrong format of _DT_SM_WINDOW_INFO"); 445 return false; 446 } 447 448 long wmwin = Native.getWindow(getter.getData(), 1); //unsafe.getInt(getter.getData()+4); 449 450 if (wmwin == 0) { 451 log.fine("WARNING: DT_SM_WINDOW_INFO exists but returns zero windows"); 452 return false; 453 } 454 455 /* Now check that this window has _DT_SM_STATE_INFO (ignore contents) */ 456 if (!XA_DT_SM_STATE_INFO.isInterned()) { 457 if (log.isLoggable(PlatformLogger.Level.FINER)) { 458 log.finer("{0} is not interned", XA_DT_SM_STATE_INFO); 459 } 460 return false; 461 } 462 WindowPropertyGetter getter2 = 463 new WindowPropertyGetter(wmwin, XA_DT_SM_STATE_INFO, 0, 1, 464 false, XA_DT_SM_STATE_INFO); 465 try { 466 status = getter2.execute(XErrorHandler.IgnoreBadWindowHandler.getInstance()); 467 468 469 if (status != XConstants.Success || getter2.getData() == 0) { 470 log.finer("Getting of _DT_SM_STATE_INFO is not successfull"); 471 return false; 472 } 473 if (getter2.getActualType() != XA_DT_SM_STATE_INFO.getAtom() 474 || getter2.getActualFormat() != 32) 475 { 476 log.finer("Wrong format of _DT_SM_STATE_INFO"); 477 return false; 478 } 479 480 return true; 481 } finally { 482 getter2.dispose(); 483 } 484 } finally { 485 getter.dispose(); 486 } 487 } 488 489 /* 490 * Is MWM running? (Note that CDE will test positive as well). 491 * 492 * Check for _MOTIF_WM_INFO(_MOTIF_WM_INFO) on root. Take the 493 * second element of the property and check for presence of 494 * _DT_SM_STATE_INFO(_DT_SM_STATE_INFO) on that window. 495 */ 496 static final XAtom XA_MOTIF_WM_INFO = new XAtom("_MOTIF_WM_INFO", false); 497 static final XAtom XA_DT_WORKSPACE_CURRENT = new XAtom("_DT_WORKSPACE_CURRENT", false); 498 static boolean isMotif() { 499 500 if (!(XA_MOTIF_WM_INFO.isInterned()/* && XA_DT_WORKSPACE_CURRENT.isInterned()*/) ) { 501 return false; 502 } 503 504 WindowPropertyGetter getter = 505 new WindowPropertyGetter(XToolkit.getDefaultRootWindow(), 506 XA_MOTIF_WM_INFO, 0, 507 MWMConstants.PROP_MOTIF_WM_INFO_ELEMENTS, 508 false, XA_MOTIF_WM_INFO); 509 try { 510 int status = getter.execute(); 511 512 if (status != XConstants.Success || getter.getData() == 0) { 513 return false; 514 } 515 516 if (getter.getActualType() != XA_MOTIF_WM_INFO.getAtom() 517 || getter.getActualFormat() != 32 518 || getter.getNumberOfItems() != MWMConstants.PROP_MOTIF_WM_INFO_ELEMENTS 519 || getter.getBytesAfter() != 0) 520 { 521 return false; 522 } 523 524 long wmwin = Native.getLong(getter.getData(), 1); 525 if (wmwin != 0) { 526 if (XA_DT_WORKSPACE_CURRENT.isInterned()) { 527 /* Now check that this window has _DT_WORKSPACE_CURRENT */ 528 XAtom[] curws = XA_DT_WORKSPACE_CURRENT.getAtomListProperty(wmwin); 529 if (curws.length == 0) { 530 return false; 531 } 532 return true; 533 } else { 534 // No DT_WORKSPACE, however in our tests MWM sometimes can be without desktop - 535 // and that is still MWM. So simply check for the validity of this window 536 // (through WM_STATE property). 537 WindowPropertyGetter state_getter = 538 new WindowPropertyGetter(wmwin, 539 XA_WM_STATE, 540 0, 1, false, 541 XA_WM_STATE); 542 try { 543 if (state_getter.execute() == XConstants.Success && 544 state_getter.getData() != 0 && 545 state_getter.getActualType() == XA_WM_STATE.getAtom()) 546 { 547 return true; 548 } 549 } finally { 550 state_getter.dispose(); 551 } 552 } 553 } 554 } finally { 555 getter.dispose(); 556 } 557 return false; 558 } 559 560 /* 561 * Is Sawfish running? 562 */ 563 static boolean isSawfish() { 564 return isNetWMName("Sawfish"); 565 } 566 567 /* 568 * Is KDE2 (KWin) running? 569 */ 570 static boolean isKDE2() { 571 return isNetWMName("KWin"); 572 } 573 574 static boolean isCompiz() { 575 return isNetWMName("compiz"); 576 } 577 578 static boolean isUnityCompiz() { 579 return isNetWMName("Compiz"); 580 } 581 582 static boolean isLookingGlass() { 583 return isNetWMName("LG3D"); 584 } 585 586 static boolean isCWM() { 587 return isNetWMName("CWM"); 588 } 589 590 /* 591 * Is Metacity running? 592 */ 593 static boolean isMetacity() { 594 return isNetWMName("Metacity"); 595 // || ( 596 // XA_NET_SUPPORTING_WM_CHECK. 597 // getIntProperty(XToolkit.getDefaultRootWindow(), XA_NET_SUPPORTING_WM_CHECK. 598 // getIntProperty(XToolkit.getDefaultRootWindow(), XAtom.XA_CARDINAL)) == 0); 599 } 600 601 static boolean isMutter() { 602 return isNetWMName("Mutter") || isNetWMName("GNOME Shell"); 603 } 604 605 static int awtWMNonReparenting = -1; 606 static boolean isNonReparentingWM() { 607 if (awtWMNonReparenting == -1) { 608 awtWMNonReparenting = (XToolkit.getEnv("_JAVA_AWT_WM_NONREPARENTING") != null) ? 1 : 0; 609 } 610 return (awtWMNonReparenting == 1 || XWM.getWMID() == XWM.COMPIZ_WM 611 || XWM.getWMID() == XWM.LG3D_WM || XWM.getWMID() == XWM.CWM_WM); 612 } 613 614 /* 615 * Prepare IceWM check. 616 * 617 * The only way to detect IceWM, seems to be by setting 618 * _ICEWM_WINOPTHINT(_ICEWM_WINOPTHINT/8) on root and checking if it 619 * was immediately deleted by IceWM. 620 * 621 * But messing with PropertyNotify here is way too much trouble, so 622 * approximate the check by setting the property in this function and 623 * checking if it still exists later on. 624 * 625 * Gaa, dirty dances... 626 */ 627 static final XAtom XA_ICEWM_WINOPTHINT = new XAtom("_ICEWM_WINOPTHINT", false); 628 static final char opt[] = { 629 'A','W','T','_','I','C','E','W','M','_','T','E','S','T','\0', 630 'a','l','l','W','o','r','k','s','p','a','c','e','s','\0', 631 '0','\0' 632 }; 633 static boolean prepareIsIceWM() { 634 /* 635 * Choose something innocuous: "AWT_ICEWM_TEST allWorkspaces 0". 636 * IceWM expects "class\0option\0arg\0" with zero bytes as delimiters. 637 */ 638 639 if (!XA_ICEWM_WINOPTHINT.isInterned()) { 640 if (log.isLoggable(PlatformLogger.Level.FINER)) { 641 log.finer("{0} is not interned", XA_ICEWM_WINOPTHINT); 642 } 643 return false; 644 } 645 646 XToolkit.awtLock(); 647 try { 648 XErrorHandlerUtil.WITH_XERROR_HANDLER(XErrorHandler.VerifyChangePropertyHandler.getInstance()); 649 XlibWrapper.XChangePropertyS(XToolkit.getDisplay(), XToolkit.getDefaultRootWindow(), 650 XA_ICEWM_WINOPTHINT.getAtom(), 651 XA_ICEWM_WINOPTHINT.getAtom(), 652 8, XConstants.PropModeReplace, 653 new String(opt)); 654 XErrorHandlerUtil.RESTORE_XERROR_HANDLER(); 655 656 if ((XErrorHandlerUtil.saved_error != null) && 657 (XErrorHandlerUtil.saved_error.get_error_code() != XConstants.Success)) { 658 log.finer("Erorr getting XA_ICEWM_WINOPTHINT property"); 659 return false; 660 } 661 log.finer("Prepared for IceWM detection"); 662 return true; 663 } finally { 664 XToolkit.awtUnlock(); 665 } 666 } 667 668 /* 669 * Is IceWM running? 670 * 671 * Note well: Only call this if awt_wm_prepareIsIceWM succeeded, or a 672 * false positive will be reported. 673 */ 674 static boolean isIceWM() { 675 if (!XA_ICEWM_WINOPTHINT.isInterned()) { 676 if (log.isLoggable(PlatformLogger.Level.FINER)) { 677 log.finer("{0} is not interned", XA_ICEWM_WINOPTHINT); 678 } 679 return false; 680 } 681 682 WindowPropertyGetter getter = 683 new WindowPropertyGetter(XToolkit.getDefaultRootWindow(), 684 XA_ICEWM_WINOPTHINT, 0, 0xFFFF, 685 true, XA_ICEWM_WINOPTHINT); 686 try { 687 int status = getter.execute(); 688 boolean res = (status == XConstants.Success && getter.getActualType() != 0); 689 if (log.isLoggable(PlatformLogger.Level.FINER)) { 690 log.finer("Status getting XA_ICEWM_WINOPTHINT: " + !res); 691 } 692 return !res || isNetWMName("IceWM"); 693 } finally { 694 getter.dispose(); 695 } 696 } 697 698 /* 699 * Is OpenLook WM running? 700 * 701 * This one is pretty lame, but the only property peculiar to OLWM is 702 * _SUN_WM_PROTOCOLS(ATOM[]). Fortunately, olwm deletes it on exit. 703 */ 704 static final XAtom XA_SUN_WM_PROTOCOLS = new XAtom("_SUN_WM_PROTOCOLS", false); 705 static boolean isOpenLook() { 706 if (!XA_SUN_WM_PROTOCOLS.isInterned()) { 707 return false; 708 } 709 710 XAtom[] list = XA_SUN_WM_PROTOCOLS.getAtomListProperty(XToolkit.getDefaultRootWindow()); 711 return (list.length != 0); 712 } 713 714 /* 715 * Temporary error handler that checks if selecting for 716 * SubstructureRedirect failed. 717 */ 718 private static boolean winmgr_running = false; 719 private static XErrorHandler detectWMHandler = new XErrorHandler.XBaseErrorHandler() { 720 @Override 721 public int handleError(long display, XErrorEvent err) { 722 if ((err.get_request_code() == XProtocolConstants.X_ChangeWindowAttributes) && 723 (err.get_error_code() == XConstants.BadAccess)) 724 { 725 winmgr_running = true; 726 return 0; 727 } 728 return super.handleError(display, err); 729 } 730 }; 731 732 /* 733 * Make an educated guess about running window manager. 734 * XXX: ideally, we should detect wm restart. 735 */ 736 static int awt_wmgr = XWM.UNDETERMINED_WM; 737 static XWM wm; 738 static XWM getWM() { 739 if (wm == null) { 740 wm = new XWM(awt_wmgr = getWMID()/*XWM.OTHER_WM*/); 741 } 742 return wm; 743 } 744 static int getWMID() { 745 if (insLog.isLoggable(PlatformLogger.Level.FINEST)) { 746 insLog.finest("awt_wmgr = " + awt_wmgr); 747 } 748 /* 749 * Ideally, we should support cases when a different WM is started 750 * during a Java app lifetime. 751 */ 752 753 if (awt_wmgr != XWM.UNDETERMINED_WM) { 754 return awt_wmgr; 755 } 756 757 XSetWindowAttributes substruct = new XSetWindowAttributes(); 758 XToolkit.awtLock(); 759 try { 760 if (isNoWM()) { 761 awt_wmgr = XWM.NO_WM; 762 return awt_wmgr; 763 } 764 765 // Initialize _NET protocol - used to detect Window Manager. 766 // Later, WM will initialize its own version of protocol 767 XNETProtocol l_net_protocol = g_net_protocol = new XNETProtocol(); 768 l_net_protocol.detect(); 769 if (log.isLoggable(PlatformLogger.Level.FINE) && l_net_protocol.active()) { 770 log.fine("_NET_WM_NAME is " + l_net_protocol.getWMName()); 771 } 772 XWINProtocol win = g_win_protocol = new XWINProtocol(); 773 win.detect(); 774 775 /* actual check for IceWM to follow below */ 776 boolean doIsIceWM = prepareIsIceWM(); /* and let IceWM to act */ 777 778 /* 779 * Ok, some WM is out there. Check which one by testing for 780 * "distinguishing" atoms. 781 */ 782 if (isEnlightenment()) { 783 awt_wmgr = XWM.ENLIGHTEN_WM; 784 } else if (isMetacity()) { 785 awt_wmgr = XWM.METACITY_WM; 786 } else if (isMutter()) { 787 awt_wmgr = XWM.MUTTER_WM; 788 } else if (isSawfish()) { 789 awt_wmgr = XWM.SAWFISH_WM; 790 } else if (isKDE2()) { 791 awt_wmgr =XWM.KDE2_WM; 792 } else if (isCompiz()) { 793 awt_wmgr = XWM.COMPIZ_WM; 794 } else if (isLookingGlass()) { 795 awt_wmgr = LG3D_WM; 796 } else if (isCWM()) { 797 awt_wmgr = CWM_WM; 798 } else if (doIsIceWM && isIceWM()) { 799 awt_wmgr = XWM.ICE_WM; 800 } else if (isUnityCompiz()) { 801 awt_wmgr = XWM.UNITY_COMPIZ_WM; 802 } 803 /* 804 * We don't check for legacy WM when we already know that WM 805 * supports WIN or _NET wm spec. 806 */ 807 else if (l_net_protocol.active()) { 808 awt_wmgr = XWM.OTHER_WM; 809 } else if (win.active()) { 810 awt_wmgr = XWM.OTHER_WM; 811 } 812 /* 813 * Check for legacy WMs. 814 */ 815 else if (isCDE()) { /* XXX: must come before isMotif */ 816 awt_wmgr = XWM.CDE_WM; 817 } else if (isMotif()) { 818 awt_wmgr = XWM.MOTIF_WM; 819 } else if (isOpenLook()) { 820 awt_wmgr = XWM.OPENLOOK_WM; 821 } else { 822 awt_wmgr = XWM.OTHER_WM; 823 } 824 825 return awt_wmgr; 826 } finally { 827 XToolkit.awtUnlock(); 828 substruct.dispose(); 829 } 830 } 831 832 833 /*****************************************************************************\ 834 * 835 * Size and decoration hints ... 836 * 837 \*****************************************************************************/ 838 839 840 /* 841 * Remove size hints specified by the mask. 842 * XXX: Why do we need this in the first place??? 843 */ 844 static void removeSizeHints(XDecoratedPeer window, long mask) { 845 mask &= XUtilConstants.PMaxSize | XUtilConstants.PMinSize; 846 847 XToolkit.awtLock(); 848 try { 849 XSizeHints hints = window.getHints(); 850 if ((hints.get_flags() & mask) == 0) { 851 return; 852 } 853 854 hints.set_flags(hints.get_flags() & ~mask); 855 if (insLog.isLoggable(PlatformLogger.Level.FINER)) { 856 insLog.finer("Setting hints, flags " + XlibWrapper.hintsToString(hints.get_flags())); 857 } 858 XlibWrapper.XSetWMNormalHints(XToolkit.getDisplay(), 859 window.getWindow(), 860 hints.pData); 861 } finally { 862 XToolkit.awtUnlock(); 863 } 864 } 865 866 /* 867 * If MWM_DECOR_ALL bit is set, then the rest of the bit-mask is taken 868 * to be subtracted from the decorations. Normalize decoration spec 869 * so that we can map motif decor to something else bit-by-bit in the 870 * rest of the code. 871 */ 872 static int normalizeMotifDecor(int decorations) { 873 if ((decorations & MWMConstants.MWM_DECOR_ALL) == 0) { 874 return decorations; 875 } 876 int d = MWMConstants.MWM_DECOR_BORDER | MWMConstants.MWM_DECOR_RESIZEH 877 | MWMConstants.MWM_DECOR_TITLE 878 | MWMConstants.MWM_DECOR_MENU | MWMConstants.MWM_DECOR_MINIMIZE 879 | MWMConstants.MWM_DECOR_MAXIMIZE; 880 d &= ~decorations; 881 return d; 882 } 883 884 /* 885 * If MWM_FUNC_ALL bit is set, then the rest of the bit-mask is taken 886 * to be subtracted from the functions. Normalize function spec 887 * so that we can map motif func to something else bit-by-bit in the 888 * rest of the code. 889 */ 890 static int normalizeMotifFunc(int functions) { 891 if ((functions & MWMConstants.MWM_FUNC_ALL) == 0) { 892 return functions; 893 } 894 int f = MWMConstants.MWM_FUNC_RESIZE | 895 MWMConstants.MWM_FUNC_MOVE | 896 MWMConstants.MWM_FUNC_MAXIMIZE | 897 MWMConstants.MWM_FUNC_MINIMIZE | 898 MWMConstants.MWM_FUNC_CLOSE; 899 f &= ~functions; 900 return f; 901 } 902 903 /* 904 * Infer OL properties from MWM decorations. 905 * Use _OL_DECOR_DEL(ATOM[]) to remove unwanted ones. 906 */ 907 static void setOLDecor(XWindow window, boolean resizable, int decorations) { 908 if (window == null) { 909 return; 910 } 911 912 XAtomList decorDel = new XAtomList(); 913 decorations = normalizeMotifDecor(decorations); 914 if (insLog.isLoggable(PlatformLogger.Level.FINER)) { 915 insLog.finer("Setting OL_DECOR to " + Integer.toBinaryString(decorations)); 916 } 917 if ((decorations & MWMConstants.MWM_DECOR_TITLE) == 0) { 918 decorDel.add(XA_OL_DECOR_HEADER); 919 } 920 if ((decorations & (MWMConstants.MWM_DECOR_RESIZEH | MWMConstants.MWM_DECOR_MAXIMIZE)) == 0) { 921 decorDel.add(XA_OL_DECOR_RESIZE); 922 } 923 if ((decorations & (MWMConstants.MWM_DECOR_MENU | 924 MWMConstants.MWM_DECOR_MAXIMIZE | 925 MWMConstants.MWM_DECOR_MINIMIZE)) == 0) 926 { 927 decorDel.add(XA_OL_DECOR_CLOSE); 928 } 929 if (decorDel.size() == 0) { 930 insLog.finer("Deleting OL_DECOR"); 931 XA_OL_DECOR_DEL.DeleteProperty(window); 932 } else { 933 if (insLog.isLoggable(PlatformLogger.Level.FINER)) { 934 insLog.finer("Setting OL_DECOR to " + decorDel); 935 } 936 XA_OL_DECOR_DEL.setAtomListProperty(window, decorDel); 937 } 938 } 939 940 /* 941 * Set MWM decorations. Set MWM functions depending on resizability. 942 */ 943 static void setMotifDecor(XWindow window, boolean resizable, int decorations, int functions) { 944 /* Apparently some WMs don't implement MWM_*_ALL semantic correctly */ 945 if ((decorations & MWMConstants.MWM_DECOR_ALL) != 0 946 && (decorations != MWMConstants.MWM_DECOR_ALL)) 947 { 948 decorations = normalizeMotifDecor(decorations); 949 } 950 if ((functions & MWMConstants.MWM_FUNC_ALL) != 0 951 && (functions != MWMConstants.MWM_FUNC_ALL)) 952 { 953 functions = normalizeMotifFunc(functions); 954 } 955 956 PropMwmHints hints = window.getMWMHints(); 957 hints.set_flags(hints.get_flags() | 958 MWMConstants.MWM_HINTS_FUNCTIONS | 959 MWMConstants.MWM_HINTS_DECORATIONS); 960 hints.set_functions(functions); 961 hints.set_decorations(decorations); 962 963 if (stateLog.isLoggable(PlatformLogger.Level.FINER)) { 964 stateLog.finer("Setting MWM_HINTS to " + hints); 965 } 966 window.setMWMHints(hints); 967 } 968 969 /* 970 * Under some window managers if shell is already mapped, we MUST 971 * unmap and later remap in order to effect the changes we make in the 972 * window manager decorations. 973 * 974 * N.B. This unmapping / remapping of the shell exposes a bug in 975 * X/Motif or the Motif Window Manager. When you attempt to map a 976 * widget which is positioned (partially) off-screen, the window is 977 * relocated to be entirely on screen. Good idea. But if both the x 978 * and the y coordinates are less than the origin (0,0), the first 979 * (re)map will move the window to the origin, and any subsequent 980 * (re)map will relocate the window at some other point on the screen. 981 * I have written a short Motif test program to discover this bug. 982 * This should occur infrequently and it does not cause any real 983 * problem. So for now we'll let it be. 984 */ 985 static boolean needRemap(XDecoratedPeer window) { 986 // Don't remap EmbeddedFrame, 987 // e.g. for TrayIcon it causes problems. 988 return !window.isEmbedded(); 989 } 990 991 /* 992 * Set decoration hints on the shell to wdata->decor adjusted 993 * appropriately if not resizable. 994 */ 995 static void setShellDecor(XDecoratedPeer window) { 996 int decorations = window.getDecorations(); 997 int functions = window.getFunctions(); 998 boolean resizable = window.isResizable(); 999 1000 if (!resizable) { 1001 if ((decorations & MWMConstants.MWM_DECOR_ALL) != 0) { 1002 decorations |= MWMConstants.MWM_DECOR_RESIZEH | MWMConstants.MWM_DECOR_MAXIMIZE; 1003 } else { 1004 decorations &= ~(MWMConstants.MWM_DECOR_RESIZEH | MWMConstants.MWM_DECOR_MAXIMIZE); 1005 } 1006 } 1007 setMotifDecor(window, resizable, decorations, functions); 1008 setOLDecor(window, resizable, decorations); 1009 1010 /* Some WMs need remap to redecorate the window */ 1011 if (window.isShowing() && needRemap(window)) { 1012 /* 1013 * Do the re/mapping at the Xlib level. Since we essentially 1014 * work around a WM bug we don't want this hack to be exposed 1015 * to Intrinsics (i.e. don't mess with grabs, callbacks etc). 1016 */ 1017 window.xSetVisible(false); 1018 XToolkit.XSync(); 1019 window.xSetVisible(true); 1020 } 1021 } 1022 1023 /* 1024 * Make specified shell resizable. 1025 */ 1026 static void setShellResizable(XDecoratedPeer window) { 1027 if (insLog.isLoggable(PlatformLogger.Level.FINE)) { 1028 insLog.fine("Setting shell resizable " + window); 1029 } 1030 XToolkit.awtLock(); 1031 try { 1032 Rectangle shellBounds = window.getShellBounds(); 1033 shellBounds.translate(-window.currentInsets.left, -window.currentInsets.top); 1034 window.updateSizeHints(window.getDimensions()); 1035 requestWMExtents(window.getWindow()); 1036 XlibWrapper.XMoveResizeWindow(XToolkit.getDisplay(), 1037 window.getShell(), 1038 window.scaleUp(shellBounds.x), 1039 window.scaleUp(shellBounds.y), 1040 window.scaleUp(shellBounds.width), 1041 window.scaleUp(shellBounds.height)); 1042 /* REMINDER: will need to revisit when setExtendedStateBounds is added */ 1043 //Fix for 4320050: Minimum size for java.awt.Frame is not being enforced. 1044 //We need to update frame's minimum size, not to reset it 1045 removeSizeHints(window, XUtilConstants.PMaxSize); 1046 window.updateMinimumSize(); 1047 1048 /* Restore decorations */ 1049 setShellDecor(window); 1050 } finally { 1051 XToolkit.awtUnlock(); 1052 } 1053 } 1054 1055 /* 1056 * Make specified shell non-resizable. 1057 * If justChangeSize is false, update decorations as well. 1058 * @param shellBounds bounds of the shell window 1059 */ 1060 static void setShellNotResizable(XDecoratedPeer window, WindowDimensions newDimensions, Rectangle shellBounds, 1061 boolean justChangeSize) 1062 { 1063 if (insLog.isLoggable(PlatformLogger.Level.FINE)) { 1064 insLog.fine("Setting non-resizable shell " + window + ", dimensions " + newDimensions + 1065 ", shellBounds " + shellBounds +", just change size: " + justChangeSize); 1066 } 1067 XToolkit.awtLock(); 1068 try { 1069 /* Fix min/max size hints at the specified values */ 1070 if (!shellBounds.isEmpty()) { 1071 window.updateSizeHints(newDimensions); 1072 requestWMExtents(window.getWindow()); 1073 XToolkit.XSync(); 1074 XlibWrapper.XMoveResizeWindow(XToolkit.getDisplay(), 1075 window.getShell(), 1076 window.scaleUp(shellBounds.x), 1077 window.scaleUp(shellBounds.y), 1078 window.scaleUp(shellBounds.width), 1079 window.scaleUp(shellBounds.height)); 1080 } 1081 if (!justChangeSize) { /* update decorations */ 1082 setShellDecor(window); 1083 } 1084 } finally { 1085 XToolkit.awtUnlock(); 1086 } 1087 } 1088 1089 /*****************************************************************************\ 1090 * Protocols support 1091 */ 1092 private HashMap<Class<?>, Collection<?>> protocolsMap = new HashMap<Class<?>, Collection<?>>(); 1093 /** 1094 * Returns all protocols supporting given protocol interface 1095 */ 1096 <T> Collection<T> getProtocols(Class<T> protocolInterface) { 1097 @SuppressWarnings("unchecked") 1098 Collection<T> res = (Collection<T>) protocolsMap.get(protocolInterface); 1099 if (res != null) { 1100 return res; 1101 } else { 1102 return new LinkedList<T>(); 1103 } 1104 } 1105 1106 private <T> void addProtocol(Class<T> protocolInterface, T protocol) { 1107 Collection<T> protocols = getProtocols(protocolInterface); 1108 protocols.add(protocol); 1109 protocolsMap.put(protocolInterface, protocols); 1110 } 1111 1112 boolean supportsDynamicLayout() { 1113 int wm = getWMID(); 1114 switch (wm) { 1115 case XWM.ENLIGHTEN_WM: 1116 case XWM.KDE2_WM: 1117 case XWM.SAWFISH_WM: 1118 case XWM.ICE_WM: 1119 case XWM.METACITY_WM: 1120 return true; 1121 case XWM.OPENLOOK_WM: 1122 case XWM.MOTIF_WM: 1123 case XWM.CDE_WM: 1124 return false; 1125 default: 1126 return false; 1127 } 1128 } 1129 1130 1131 /** 1132 * Check if state is supported. 1133 * Note that a compound state is always reported as not supported. 1134 * Note also that MAXIMIZED_BOTH is considered not a compound state. 1135 * Therefore, a compound state is just ICONIFIED | anything else. 1136 * 1137 */ 1138 @SuppressWarnings("fallthrough") 1139 boolean supportsExtendedState(int state) { 1140 switch (state) { 1141 case Frame.MAXIMIZED_VERT: 1142 case Frame.MAXIMIZED_HORIZ: 1143 /* 1144 * WMs that talk NET/WIN protocol, but do not support 1145 * unidirectional maximization. 1146 */ 1147 if (getWMID() == METACITY_WM) { 1148 /* "This is a deliberate policy decision." -hp */ 1149 return false; 1150 } 1151 /* FALLTROUGH */ 1152 case Frame.MAXIMIZED_BOTH: 1153 for (XStateProtocol proto : getProtocols(XStateProtocol.class)) { 1154 if (proto.supportsState(state)) { 1155 return true; 1156 } 1157 } 1158 /* FALLTROUGH */ 1159 default: 1160 return false; 1161 } 1162 } 1163 1164 /*****************************************************************************\ 1165 * 1166 * Reading state from different protocols 1167 * 1168 \*****************************************************************************/ 1169 1170 1171 int getExtendedState(XWindowPeer window) { 1172 int state = 0; 1173 for (XStateProtocol proto : getProtocols(XStateProtocol.class)) { 1174 state |= proto.getState(window); 1175 } 1176 if (state != 0) { 1177 return state; 1178 } else { 1179 return Frame.NORMAL; 1180 } 1181 } 1182 1183 /*****************************************************************************\ 1184 * 1185 * Notice window state change when WM changes a property on the window ... 1186 * 1187 \*****************************************************************************/ 1188 1189 1190 /* 1191 * Check if property change is a window state protocol message. 1192 */ 1193 boolean isStateChange(XDecoratedPeer window, XPropertyEvent e) { 1194 if (!window.isShowing()) { 1195 stateLog.finer("Window is not showing"); 1196 return false; 1197 } 1198 1199 int wm_state = window.getWMState(); 1200 if (wm_state == XUtilConstants.WithdrawnState) { 1201 stateLog.finer("WithdrawnState"); 1202 return false; 1203 } else { 1204 if (stateLog.isLoggable(PlatformLogger.Level.FINER)) { 1205 stateLog.finer("Window WM_STATE is " + wm_state); 1206 } 1207 } 1208 boolean is_state_change = false; 1209 if (e.get_atom() == XA_WM_STATE.getAtom()) { 1210 is_state_change = true; 1211 } 1212 1213 for (XStateProtocol proto : getProtocols(XStateProtocol.class)) { 1214 is_state_change |= proto.isStateChange(e); 1215 if (stateLog.isLoggable(PlatformLogger.Level.FINEST)) { 1216 stateLog.finest(proto + ": is state changed = " + is_state_change); 1217 } 1218 } 1219 return is_state_change; 1220 } 1221 1222 /* 1223 * Returns current state (including extended) of a given window. 1224 */ 1225 int getState(XDecoratedPeer window) { 1226 int res = 0; 1227 final int wm_state = window.getWMState(); 1228 if (wm_state == XUtilConstants.IconicState) { 1229 res = Frame.ICONIFIED; 1230 } else { 1231 res = Frame.NORMAL; 1232 } 1233 res |= getExtendedState(window); 1234 return res; 1235 } 1236 1237 /*****************************************************************************\ 1238 * 1239 * Setting/changing window state ... 1240 * 1241 \*****************************************************************************/ 1242 1243 /** 1244 * Moves window to the specified layer, layer is one of the constants defined 1245 * in XLayerProtocol 1246 */ 1247 void setLayer(XWindowPeer window, int layer) { 1248 for (XLayerProtocol proto : getProtocols(XLayerProtocol.class)) { 1249 if (proto.supportsLayer(layer)) { 1250 proto.setLayer(window, layer); 1251 } 1252 } 1253 XToolkit.XSync(); 1254 } 1255 1256 void setExtendedState(XWindowPeer window, int state) { 1257 for (XStateProtocol proto : getProtocols(XStateProtocol.class)) { 1258 if (proto.supportsState(state)) { 1259 proto.setState(window, state); 1260 break; 1261 } 1262 } 1263 1264 if (!window.isShowing()) { 1265 /* 1266 * Purge KWM bits. 1267 * Not really tested with KWM, only with WindowMaker. 1268 */ 1269 XToolkit.awtLock(); 1270 try { 1271 XlibWrapper.XDeleteProperty(XToolkit.getDisplay(), 1272 window.getWindow(), 1273 XA_KWM_WIN_ICONIFIED.getAtom()); 1274 XlibWrapper.XDeleteProperty(XToolkit.getDisplay(), 1275 window.getWindow(), 1276 XA_KWM_WIN_MAXIMIZED.getAtom()); 1277 } 1278 finally { 1279 XToolkit.awtUnlock(); 1280 } 1281 } 1282 XToolkit.XSync(); 1283 } 1284 1285 1286 /* 1287 * Work around for 4775545. 1288 * 1289 * If WM exits while the top-level is shaded, the shaded hint remains 1290 * on the top-level properties. When WM restarts and sees the shaded 1291 * window it can reparent it into a "pre-shaded" decoration frame 1292 * (Metacity does), and our insets logic will go crazy, b/c it will 1293 * see a huge nagative bottom inset. There's no clean solution for 1294 * this, so let's just be weasels and drop the shaded hint if we 1295 * detect that WM exited. NB: we are in for a race condition with WM 1296 * restart here. NB2: e.g. WindowMaker saves the state in a private 1297 * property that this code knows nothing about, so this workaround is 1298 * not effective; other WMs might play similar tricks. 1299 */ 1300 void unshadeKludge(XDecoratedPeer window) { 1301 assert(window.isShowing()); 1302 1303 for (XStateProtocol proto : getProtocols(XStateProtocol.class)) { 1304 proto.unshadeKludge(window); 1305 } 1306 XToolkit.XSync(); 1307 } 1308 1309 static boolean inited = false; 1310 static void init() { 1311 if (inited) { 1312 return; 1313 } 1314 1315 initAtoms(); 1316 getWM(); 1317 inited = true; 1318 } 1319 1320 void initializeProtocols() { 1321 XNETProtocol net_protocol = g_net_protocol; 1322 if (net_protocol != null) { 1323 if (!net_protocol.active()) { 1324 net_protocol = null; 1325 } else { 1326 if (net_protocol.doStateProtocol()) { 1327 addProtocol(XStateProtocol.class, net_protocol); 1328 } 1329 if (net_protocol.doLayerProtocol()) { 1330 addProtocol(XLayerProtocol.class, net_protocol); 1331 } 1332 } 1333 } 1334 1335 XWINProtocol win = g_win_protocol; 1336 if (win != null) { 1337 if (win.active()) { 1338 if (win.doStateProtocol()) { 1339 addProtocol(XStateProtocol.class, win); 1340 } 1341 if (win.doLayerProtocol()) { 1342 addProtocol(XLayerProtocol.class, win); 1343 } 1344 } 1345 } 1346 } 1347 1348 HashMap<Class<?>, Insets> storedInsets = new HashMap<>(); 1349 Insets guessInsets(XDecoratedPeer window) { 1350 Insets res = storedInsets.get(window.getClass()); 1351 if (res == null) { 1352 switch (WMID) { 1353 case ENLIGHTEN_WM: 1354 res = new Insets(19, 4, 4, 4); 1355 break; 1356 case CDE_WM: 1357 res = new Insets(28, 6, 6, 6); 1358 break; 1359 case NO_WM: 1360 case LG3D_WM: 1361 res = zeroInsets; 1362 break; 1363 case UNITY_COMPIZ_WM: 1364 res = new Insets(28, 1, 1, 1); 1365 break; 1366 case MOTIF_WM: 1367 case OPENLOOK_WM: 1368 default: 1369 res = defaultInsets; 1370 } 1371 } 1372 if (insLog.isLoggable(PlatformLogger.Level.FINEST)) { 1373 insLog.finest("WM guessed insets: " + res); 1374 } 1375 return res; 1376 } 1377 /* 1378 * Some buggy WMs ignore window gravity when processing 1379 * ConfigureRequest and position window as if the gravity is Static. 1380 * We work around this in MWindowPeer.pReshape(). 1381 * 1382 * Starting with 1.5 we have introduced an Environment variable 1383 * _JAVA_AWT_WM_STATIC_GRAVITY that can be set to indicate to Java 1384 * explicitly that the WM has this behaviour, example is FVWM. 1385 */ 1386 1387 static int awtWMStaticGravity = -1; 1388 static boolean configureGravityBuggy() { 1389 1390 if (awtWMStaticGravity == -1) { 1391 awtWMStaticGravity = (XToolkit.getEnv("_JAVA_AWT_WM_STATIC_GRAVITY") != null) ? 1 : 0; 1392 } 1393 1394 if (awtWMStaticGravity == 1) { 1395 return true; 1396 } 1397 1398 switch(getWMID()) { 1399 case XWM.ICE_WM: 1400 /* 1401 * See bug #228981 at IceWM's SourceForge pages. 1402 * Latest stable version 1.0.8-6 still has this problem. 1403 */ 1404 /** 1405 * Version 1.2.2 doesn't have this problem 1406 */ 1407 // Detect IceWM version 1408 if (g_net_protocol != null) { 1409 String wm_name = g_net_protocol.getWMName(); 1410 Pattern pat = Pattern.compile("^IceWM (\\d+)\\.(\\d+)\\.(\\d+).*$"); 1411 try { 1412 Matcher match = pat.matcher(wm_name); 1413 if (match.matches()) { 1414 int v1 = Integer.parseInt(match.group(1)); 1415 int v2 = Integer.parseInt(match.group(2)); 1416 int v3 = Integer.parseInt(match.group(3)); 1417 return !(v1 > 1 || (v1 == 1 && (v2 > 2 || (v2 == 2 && v3 >=2)))); 1418 } 1419 } catch (Exception e) { 1420 return true; 1421 } 1422 } 1423 return true; 1424 case XWM.ENLIGHTEN_WM: 1425 /* At least E16 is buggy. */ 1426 return true; 1427 default: 1428 return false; 1429 } 1430 } 1431 1432 /* 1433 * @return if WM implements the insets property - returns insets with values 1434 * specified in that property, null otherwise. 1435 */ 1436 public static Insets getInsetsFromExtents(long window) { 1437 if (window == XConstants.None) { 1438 return null; 1439 } 1440 XNETProtocol net_protocol = getWM().getNETProtocol(); 1441 if (net_protocol != null && net_protocol.active()) { 1442 Insets insets = getInsetsFromProp(window, XA_NET_FRAME_EXTENTS); 1443 if (insLog.isLoggable(PlatformLogger.Level.FINE)) { 1444 insLog.fine("_NET_FRAME_EXTENTS: {0}", insets); 1445 } 1446 1447 if (insets != null) { 1448 return insets; 1449 } 1450 } 1451 switch(getWMID()) { 1452 case XWM.KDE2_WM: 1453 return getInsetsFromProp(window, XA_KDE_NET_WM_FRAME_STRUT); 1454 case XWM.ENLIGHTEN_WM: 1455 return getInsetsFromProp(window, XA_E_FRAME_SIZE); 1456 default: 1457 return null; 1458 } 1459 } 1460 1461 /** 1462 * Helper function reads property of type CARDINAL[4] = { left, right, top, bottom } 1463 * and converts it to Insets object. 1464 */ 1465 public static Insets getInsetsFromProp(long window, XAtom atom) { 1466 if (window == XConstants.None) { 1467 return null; 1468 } 1469 1470 WindowPropertyGetter getter = 1471 new WindowPropertyGetter(window, atom, 1472 0, 4, false, XAtom.XA_CARDINAL); 1473 try { 1474 if (getter.execute() != XConstants.Success 1475 || getter.getData() == 0 1476 || getter.getActualType() != XAtom.XA_CARDINAL 1477 || getter.getActualFormat() != 32) 1478 { 1479 return null; 1480 } else { 1481 return new Insets((int)Native.getCard32(getter.getData(), 2), // top 1482 (int)Native.getCard32(getter.getData(), 0), // left 1483 (int)Native.getCard32(getter.getData(), 3), // bottom 1484 (int)Native.getCard32(getter.getData(), 1)); // right 1485 } 1486 } finally { 1487 getter.dispose(); 1488 } 1489 } 1490 1491 /** 1492 * Asks WM to fill Frame Extents (insets) for the window. 1493 */ 1494 public static void requestWMExtents(long window) { 1495 if (window == XConstants.None) { // not initialized 1496 return; 1497 } 1498 1499 log.fine("Requesting FRAME_EXTENTS"); 1500 1501 XClientMessageEvent msg = new XClientMessageEvent(); 1502 msg.zero(); 1503 msg.set_type(XConstants.ClientMessage); 1504 msg.set_display(XToolkit.getDisplay()); 1505 msg.set_window(window); 1506 msg.set_format(32); 1507 XToolkit.awtLock(); 1508 try { 1509 XNETProtocol net_protocol = getWM().getNETProtocol(); 1510 if (net_protocol != null && net_protocol.active()) { 1511 msg.set_message_type(XA_NET_REQUEST_FRAME_EXTENTS.getAtom()); 1512 XlibWrapper.XSendEvent(XToolkit.getDisplay(), XToolkit.getDefaultRootWindow(), 1513 false, 1514 XConstants.SubstructureRedirectMask | XConstants.SubstructureNotifyMask, 1515 msg.getPData()); 1516 } 1517 if (getWMID() == XWM.KDE2_WM) { 1518 msg.set_message_type(XA_KDE_NET_WM_FRAME_STRUT.getAtom()); 1519 XlibWrapper.XSendEvent(XToolkit.getDisplay(), XToolkit.getDefaultRootWindow(), 1520 false, 1521 XConstants.SubstructureRedirectMask | XConstants.SubstructureNotifyMask, 1522 msg.getPData()); 1523 } 1524 // XXX: should we wait for response? XIfEvent() would be useful here :) 1525 } finally { 1526 XToolkit.awtUnlock(); 1527 msg.dispose(); 1528 } 1529 } 1530 1531 /* syncTopLEvelPos() is necessary to insure that the window manager has in 1532 * fact moved us to our final position relative to the reParented WM window. 1533 * We have noted a timing window which our shell has not been moved so we 1534 * screw up the insets thinking they are 0,0. Wait (for a limited period of 1535 * time to let the WM hava a chance to move us. 1536 * @param window window ID of the shell, assuming it is the window 1537 * which will NOT have zero coordinates after the complete 1538 * reparenting 1539 */ 1540 boolean syncTopLevelPos(long window, XWindowAttributes attrs) { 1541 int tries = 0; 1542 XToolkit.awtLock(); 1543 try { 1544 do { 1545 XlibWrapper.XGetWindowAttributes(XToolkit.getDisplay(), window, attrs.pData); 1546 if (attrs.get_x() != 0 || attrs.get_y() != 0) { 1547 return true; 1548 } 1549 tries++; 1550 XToolkit.XSync(); 1551 } while (tries < 50); 1552 } 1553 finally { 1554 XToolkit.awtUnlock(); 1555 } 1556 return false; 1557 } 1558 1559 Insets getInsets(XDecoratedPeer win, long window, long parent) { 1560 /* 1561 * Unfortunately the concept of "insets" borrowed to AWT 1562 * from Win32 is *absolutely*, *unbelievably* foreign to 1563 * X11. Few WMs provide the size of frame decor 1564 * (i.e. insets) in a property they set on the client 1565 * window, so we check if we can get away with just 1566 * peeking at it. [Future versions of wm-spec might add a 1567 * standardized hint for this]. 1568 * 1569 * Otherwise we do some special casing. Actually the 1570 * fallback code ("default" case) seems to cover most of 1571 * the existing WMs (modulo Reparent/Configure order 1572 * perhaps?). 1573 * 1574 * Fallback code tries to account for the two most common cases: 1575 * 1576 * . single reparenting 1577 * parent window is the WM frame 1578 * [twm, olwm, sawfish] 1579 * 1580 * . double reparenting 1581 * parent is a lining exactly the size of the client 1582 * grandpa is the WM frame 1583 * [mwm, e!, kwin, fvwm2 ... ] 1584 */ 1585 Insets correctWM = XWM.getInsetsFromExtents(window); 1586 if (insLog.isLoggable(PlatformLogger.Level.FINER)) { 1587 insLog.finer("Got insets from property: {0}", correctWM); 1588 } 1589 1590 if (correctWM == null) { 1591 correctWM = new Insets(0,0,0,0); 1592 1593 correctWM.top = -1; 1594 correctWM.left = -1; 1595 1596 XWindowAttributes lwinAttr = new XWindowAttributes(); 1597 XWindowAttributes pattr = new XWindowAttributes(); 1598 try { 1599 switch (XWM.getWMID()) { 1600 /* should've been done in awt_wm_getInsetsFromProp */ 1601 case XWM.ENLIGHTEN_WM: { 1602 /* enlightenment does double reparenting */ 1603 syncTopLevelPos(parent, lwinAttr); 1604 correctWM.left = lwinAttr.get_x(); 1605 correctWM.top = lwinAttr.get_y(); 1606 /* 1607 * Now get the actual dimensions of the parent window 1608 * resolve the difference. We can't rely on the left 1609 * to be equal to right or bottom... Enlightment 1610 * breaks that assumption. 1611 */ 1612 XlibWrapper.XGetWindowAttributes(XToolkit.getDisplay(), 1613 XlibUtil.getParentWindow(parent), 1614 pattr.pData); 1615 correctWM.right = pattr.get_width() - 1616 (lwinAttr.get_width() + correctWM.left); 1617 correctWM.bottom = pattr.get_height() - 1618 (lwinAttr.get_height() + correctWM.top); 1619 1620 break; 1621 } 1622 case XWM.ICE_WM: // for 1.2.2. 1623 case XWM.KDE2_WM: /* should've been done in getInsetsFromProp */ 1624 case XWM.CDE_WM: 1625 case XWM.MOTIF_WM: { 1626 /* these are double reparenting too */ 1627 if (syncTopLevelPos(parent, lwinAttr)) { 1628 correctWM.top = lwinAttr.get_y(); 1629 correctWM.left = lwinAttr.get_x(); 1630 correctWM.right = correctWM.left; 1631 correctWM.bottom = correctWM.left; 1632 } else { 1633 return null; 1634 } 1635 break; 1636 } 1637 case XWM.SAWFISH_WM: 1638 case XWM.OPENLOOK_WM: { 1639 /* single reparenting */ 1640 syncTopLevelPos(window, lwinAttr); 1641 correctWM.top = lwinAttr.get_y(); 1642 correctWM.left = lwinAttr.get_x(); 1643 correctWM.right = correctWM.left; 1644 correctWM.bottom = correctWM.left; 1645 break; 1646 } 1647 case XWM.OTHER_WM: 1648 default: { /* this is very similar to the E! case above */ 1649 if (insLog.isLoggable(PlatformLogger.Level.FINEST)) { 1650 insLog.finest("Getting correct insets for OTHER_WM/default, parent: {0}", parent); 1651 } 1652 syncTopLevelPos(parent, lwinAttr); 1653 int status = XlibWrapper.XGetWindowAttributes(XToolkit.getDisplay(), 1654 window, lwinAttr.pData); 1655 status = XlibWrapper.XGetWindowAttributes(XToolkit.getDisplay(), 1656 parent, pattr.pData); 1657 if (lwinAttr.get_root() == parent) { 1658 insLog.finest("our parent is root so insets should be zero"); 1659 correctWM = new Insets(0, 0, 0, 0); 1660 break; 1661 } 1662 1663 /* 1664 * Check for double-reparenting WM. 1665 * 1666 * If the parent is exactly the same size as the 1667 * top-level assume taht it's the "lining" window and 1668 * that the grandparent is the actual frame (NB: we 1669 * have already handled undecorated windows). 1670 * 1671 * XXX: what about timing issues that syncTopLevelPos 1672 * is supposed to work around? 1673 */ 1674 if (lwinAttr.get_x() == 0 && lwinAttr.get_y() == 0 1675 && lwinAttr.get_width()+2*lwinAttr.get_border_width() == pattr.get_width() 1676 && lwinAttr.get_height()+2*lwinAttr.get_border_width() == pattr.get_height()) 1677 { 1678 if (insLog.isLoggable(PlatformLogger.Level.FINEST)) { 1679 insLog.finest("Double reparenting detected, pattr({2})={0}, lwinAttr({3})={1}", 1680 lwinAttr, pattr, parent, window); 1681 } 1682 lwinAttr.set_x(pattr.get_x()); 1683 lwinAttr.set_y(pattr.get_y()); 1684 lwinAttr.set_border_width(lwinAttr.get_border_width()+pattr.get_border_width()); 1685 1686 final long grand_parent = XlibUtil.getParentWindow(parent); 1687 1688 if (grand_parent == lwinAttr.get_root()) { 1689 // This is not double-reparenting in a 1690 // general sense - we simply don't have 1691 // correct insets - return null to try to 1692 // get insets later 1693 return null; 1694 } else { 1695 parent = grand_parent; 1696 XlibWrapper.XGetWindowAttributes(XToolkit.getDisplay(), 1697 parent, 1698 pattr.pData); 1699 } 1700 } 1701 /* 1702 * XXX: To be absolutely correct, we'd need to take 1703 * parent's border-width into account too, but the 1704 * rest of the code is happily unaware about border 1705 * widths and inner/outer distinction, so for the time 1706 * being, just ignore it. 1707 */ 1708 if (insLog.isLoggable(PlatformLogger.Level.FINEST)) { 1709 insLog.finest("Attrs before calculation: pattr({2})={0}, lwinAttr({3})={1}", 1710 lwinAttr, pattr, parent, window); 1711 } 1712 correctWM = new Insets(lwinAttr.get_y() + lwinAttr.get_border_width(), 1713 lwinAttr.get_x() + lwinAttr.get_border_width(), 1714 pattr.get_height() - (lwinAttr.get_y() + lwinAttr.get_height() + 2*lwinAttr.get_border_width()), 1715 pattr.get_width() - (lwinAttr.get_x() + lwinAttr.get_width() + 2*lwinAttr.get_border_width())); 1716 break; 1717 } /* default */ 1718 } /* switch (runningWM) */ 1719 } finally { 1720 lwinAttr.dispose(); 1721 pattr.dispose(); 1722 } 1723 } 1724 1725 correctWM.top = win.scaleUp(correctWM.top); 1726 correctWM.bottom = win.scaleUp(correctWM.bottom); 1727 correctWM.left = win.scaleUp(correctWM.left); 1728 correctWM.right = win.scaleUp(correctWM.right); 1729 1730 if (storedInsets.get(win.getClass()) == null) { 1731 storedInsets.put(win.getClass(), correctWM); 1732 } 1733 return correctWM; 1734 } 1735 boolean isDesktopWindow( long w ) { 1736 if (g_net_protocol != null) { 1737 XAtomList wtype = XAtom.get("_NET_WM_WINDOW_TYPE").getAtomListPropertyList( w ); 1738 return wtype.contains( XAtom.get("_NET_WM_WINDOW_TYPE_DESKTOP") ); 1739 } else { 1740 return false; 1741 } 1742 } 1743 1744 public XNETProtocol getNETProtocol() { 1745 return g_net_protocol; 1746 } 1747 1748 /** 1749 * Sets _NET_WN_ICON property on the window using the arrays of 1750 * raster-data for icons. If icons is null, removes _NET_WM_ICON 1751 * property. 1752 * This method invokes XNETProtocol.setWMIcon() for WMs that 1753 * support NET protocol. 1754 * 1755 * @return true if hint was modified successfully, false otherwise 1756 */ 1757 public boolean setNetWMIcon(XWindowPeer window, java.util.List<IconInfo> icons) { 1758 if (g_net_protocol != null && g_net_protocol.active()) { 1759 g_net_protocol.setWMIcons(window, icons); 1760 return getWMID() != ICE_WM; 1761 } 1762 return false; 1763 } 1764 }