< prev index next >

src/java.desktop/unix/classes/sun/awt/X11/XAtom.java

Print this page




  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 package sun.awt.X11;
  27 
  28 /**
  29  * XAtom is a class that allows you to create and modify X Window properties.
  30  * An X Atom is an identifier for a property that you can set on any X Window.
  31  * Standard X Atom are defined by X11 and these atoms are defined in this class
  32  * for convenience. Common X Atoms like <code>XA_WM_NAME</code> are used to communicate with the
  33  * Window manager to let it know the Window name. The use and protocol for these
  34  * atoms are defined in the Inter client communications converntions manual.
  35  * User specified XAtoms are defined by specifying a name that gets Interned
  36  * by the XServer and an <code>XAtom</code> object is returned. An <code>XAtom</code> can also be created
  37  * by using a pre-exisiting atom like <code>XA_WM_CLASS</code>. A <code>display</code> has to be specified
  38  * in order to create an <code>XAtom</code>. <p> <p>
  39  *
  40  * Once an <code>XAtom</code> instance is created, you can call get and set property methods to
  41  * set the values for a particular window. <p> <p>
  42  *
  43  *
  44  * Example usage : To set the window name for a top level: <p>
  45  * <code>
  46  * XAtom xa = new XAtom(display,XAtom.XA_WM_NAME); <p>
  47  * xa.setProperty(window,"Hello World");<p></code>
  48  *<p>
  49  *<p>
  50  * To get the cut buffer :<p>
  51  * <p><code>
  52  * XAtom xa = new XAtom(display,XAtom.XA_CUT_BUFFER0);<p>
  53  * String selection = xa.getProperty(root_window);<p></code>



  54  * @author  Bino George
  55  * @since       1.5
  56  */
  57 
  58 import sun.misc.Unsafe;
  59 import java.util.HashMap;
  60 
  61 public final class XAtom {
  62 
  63     // Order of lock:  XAWTLock -> XAtom.class
  64 
  65     /* Predefined Atoms - automatically extracted from XAtom.h */
  66     private static Unsafe unsafe = XlibWrapper.unsafe;
  67     private static XAtom[] emptyList = new XAtom[0];
  68 
  69     public static final long XA_PRIMARY=1;
  70     public static final long XA_SECONDARY=2;
  71     public static final long XA_ARC=3;
  72     public static final long XA_ATOM=4;
  73     public static final long XA_BITMAP=5;


 220     long display;
 221 
 222 
 223     /**  This constructor will create and intern a new XAtom that is specified
 224      *  by the supplied name.
 225      *
 226      * @param display X display to use
 227      * @param name name of the XAtom to create.
 228      * @since 1.5
 229      */
 230 
 231     private XAtom(long display, String name) {
 232         this(display, name, true);
 233     }
 234 
 235     public XAtom(String name, boolean autoIntern) {
 236         this(XToolkit.getDisplay(), name, autoIntern);
 237     }
 238 
 239     /**  This constructor will create an instance of XAtom that is specified
 240      *  by the predefined XAtom specified by u <code> latom </code>
 241      *
 242      * @param display X display to use.
 243      * @param atom a predefined XAtom.
 244      * @since 1.5
 245      */
 246     public XAtom(long display, long atom) {
 247         this.atom = atom;
 248         this.display = display;
 249         register();
 250     }
 251 
 252     /**  This constructor will create the instance,
 253      *  and if <code>autoIntern</code> is true intern a new XAtom that is specified
 254      *  by the supplied name.
 255      *
 256      * @param display X display to use
 257      * @param name name of the XAtom to create.
 258      * @since 1.5
 259      */
 260 
 261     private XAtom(long display, String name, boolean autoIntern) {
 262         this.name = name;
 263         this.display = display;
 264         if (autoIntern) {
 265             XToolkit.awtLock();
 266             try {
 267                 atom = XlibWrapper.InternAtom(display,name,0);
 268             } finally {
 269                 XToolkit.awtUnlock();
 270             }
 271         }
 272         register();
 273     }


 433             int status = getter.execute();
 434             if (status != XConstants.Success || getter.getData() == 0) {
 435                 return false;
 436             }
 437             if (getter.getActualType() != atom
 438                 || getter.getActualFormat() != 32
 439                 || getter.getNumberOfItems() != length
 440                 )
 441                 {
 442                     return false;
 443                 }
 444             XlibWrapper.memcpy(data_ptr, getter.getData(), length*getAtomSize());
 445             return true;
 446         } finally {
 447             getter.dispose();
 448         }
 449     }
 450 
 451     /**
 452      * Gets uninterpreted set of data from property and stores them in data_ptr.
 453      * Property type is <code>type</code>, property is current atom.
 454      * Property format is 32. Property 'delete' is false.
 455      * Returns boolean if requested type, format, length match returned values
 456      * and returned data pointer is not null.
 457      */
 458     public boolean getAtomData(long window, long type, long data_ptr, int length) {
 459         if (atom == 0) {
 460             throw new IllegalStateException("Atom should be initialized");
 461         }
 462         checkWindow(window);
 463         WindowPropertyGetter getter =
 464             new WindowPropertyGetter(window, this, 0, (long)length,
 465                                      false, type);
 466         try {
 467             int status = getter.execute();
 468             if (status != XConstants.Success || getter.getData() == 0) {
 469                 return false;
 470             }
 471             if (getter.getActualType() != type
 472                 || getter.getActualFormat() != 32
 473                 || getter.getNumberOfItems() != length


 488      * Property format is 32. Mode is PropModeReplace. length is a number
 489      * of items pointer by data_ptr.
 490      */
 491     public void setAtomData(long window, long data_ptr, int length) {
 492         if (atom == 0) {
 493             throw new IllegalStateException("Atom should be initialized");
 494         }
 495         checkWindow(window);
 496         XToolkit.awtLock();
 497         try {
 498             XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,
 499                 atom, atom, 32, XConstants.PropModeReplace,
 500                 data_ptr, length);
 501         } finally {
 502             XToolkit.awtUnlock();
 503         }
 504     }
 505 
 506     /**
 507      * Sets uninterpreted set of data into property from data_ptr.
 508      * Property type is <code>type</code>, property is current atom.
 509      * Property format is 32. Mode is PropModeReplace. length is a number
 510      * of items pointer by data_ptr.
 511      */
 512     public void setAtomData(long window, long type, long data_ptr, int length) {
 513         if (atom == 0) {
 514             throw new IllegalStateException("Atom should be initialized");
 515         }
 516         checkWindow(window);
 517         XToolkit.awtLock();
 518         try {
 519             XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,
 520                 atom, type, 32, XConstants.PropModeReplace,
 521                 data_ptr, length);
 522         } finally {
 523             XToolkit.awtUnlock();
 524         }
 525     }
 526 
 527     /**
 528      * Sets uninterpreted set of data into property from data_ptr.
 529      * Property type is <code>type</code>, property is current atom.
 530      * Property format is 8. Mode is PropModeReplace. length is a number
 531      * of bytes pointer by data_ptr.
 532      */
 533     public void setAtomData8(long window, long type, long data_ptr, int length) {
 534         if (atom == 0) {
 535             throw new IllegalStateException("Atom should be initialized");
 536         }
 537         checkWindow(window);
 538         XToolkit.awtLock();
 539         try {
 540             XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,
 541                 atom, type, 8, XConstants.PropModeReplace,
 542                 data_ptr, length);
 543         } finally {
 544             XToolkit.awtUnlock();
 545         }
 546     }
 547 
 548     /**
 549      * Deletes property specified by this item on the window.


 767     }
 768 
 769     void checkWindow(long window) {
 770         if (window == 0) {
 771             throw new IllegalArgumentException("Window must not be zero");
 772         }
 773     }
 774 
 775     public boolean equals(Object o) {
 776         if (!(o instanceof XAtom)) {
 777             return false;
 778         }
 779         XAtom ot = (XAtom)o;
 780         return (atom == ot.atom && display == ot.display);
 781     }
 782     public int hashCode() {
 783         return (int)((atom ^ display)& 0xFFFFL);
 784     }
 785 
 786     /**
 787      * Sets property on the <code>window</code> to the value <code>window_value</window>
 788      * Property is assumed to be of type WINDOW/32
 789      */
 790     public void setWindowProperty(long window, long window_value) {
 791         if (atom == 0) {
 792             throw new IllegalStateException("Atom should be initialized");
 793         }
 794         checkWindow(window);
 795         XToolkit.awtLock();
 796         try {
 797             Native.putWindow(XlibWrapper.larg1, window_value);
 798             XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,
 799                                     atom, XA_WINDOW, 32, XConstants.PropModeReplace,
 800                                     XlibWrapper.larg1, 1);
 801         } finally {
 802             XToolkit.awtUnlock();
 803         }
 804     }
 805     public void setWindowProperty(XBaseWindow window, XBaseWindow window_value) {
 806         setWindowProperty(window.getWindow(), window_value.getWindow());
 807     }
 808 
 809     /**
 810      * Gets property on the <code>window</code>. Property is assumed to be
 811      * of type WINDOW/32.
 812      */
 813     public long getWindowProperty(long window) {
 814         if (atom == 0) {
 815             throw new IllegalStateException("Atom should be initialized");
 816         }
 817         checkWindow(window);
 818         WindowPropertyGetter getter =
 819             new WindowPropertyGetter(window, this, 0, 1,
 820                                      false, XA_WINDOW);
 821         try {
 822             int status = getter.execute();
 823             if (status != XConstants.Success || getter.getData() == 0) {
 824                 return 0;
 825             }
 826             if (getter.getActualType() != XA_WINDOW || getter.getActualFormat() != 32) {
 827                 return 0;
 828             }
 829             return Native.getWindow(getter.getData());
 830         } finally {


  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 package sun.awt.X11;
  27 
  28 /**
  29  * XAtom is a class that allows you to create and modify X Window properties.
  30  * An X Atom is an identifier for a property that you can set on any X Window.
  31  * Standard X Atom are defined by X11 and these atoms are defined in this class
  32  * for convenience. Common X Atoms like {@code XA_WM_NAME} are used to communicate with the
  33  * Window manager to let it know the Window name. The use and protocol for these
  34  * atoms are defined in the Inter client communications converntions manual.
  35  * User specified XAtoms are defined by specifying a name that gets Interned
  36  * by the XServer and an {@code XAtom} object is returned. An {@code XAtom} can also be created
  37  * by using a pre-exisiting atom like {@code XA_WM_CLASS}. A {@code display} has to be specified
  38  * in order to create an {@code XAtom}. <p> <p>
  39  *
  40  * Once an {@code XAtom} instance is created, you can call get and set property methods to
  41  * set the values for a particular window. <p> <p>
  42  *
  43  *
  44  * Example usage : To set the window name for a top level: <p>
  45  * <pre>{@code
  46  * XAtom xa = new XAtom(display,XAtom.XA_WM_NAME);
  47  * xa.setProperty(window,"Hello World");
  48  * }</pre>
  49  * <p>
  50  * <p>
  51  * To get the cut buffer:
  52  * <pre>{@code
  53  * XAtom xa = new XAtom(display,XAtom.XA_CUT_BUFFER0);
  54  * String selection = xa.getProperty(root_window);
  55  * }</pre>
  56  *
  57  * @author  Bino George
  58  * @since       1.5
  59  */
  60 
  61 import sun.misc.Unsafe;
  62 import java.util.HashMap;
  63 
  64 public final class XAtom {
  65 
  66     // Order of lock:  XAWTLock -> XAtom.class
  67 
  68     /* Predefined Atoms - automatically extracted from XAtom.h */
  69     private static Unsafe unsafe = XlibWrapper.unsafe;
  70     private static XAtom[] emptyList = new XAtom[0];
  71 
  72     public static final long XA_PRIMARY=1;
  73     public static final long XA_SECONDARY=2;
  74     public static final long XA_ARC=3;
  75     public static final long XA_ATOM=4;
  76     public static final long XA_BITMAP=5;


 223     long display;
 224 
 225 
 226     /**  This constructor will create and intern a new XAtom that is specified
 227      *  by the supplied name.
 228      *
 229      * @param display X display to use
 230      * @param name name of the XAtom to create.
 231      * @since 1.5
 232      */
 233 
 234     private XAtom(long display, String name) {
 235         this(display, name, true);
 236     }
 237 
 238     public XAtom(String name, boolean autoIntern) {
 239         this(XToolkit.getDisplay(), name, autoIntern);
 240     }
 241 
 242     /**  This constructor will create an instance of XAtom that is specified
 243      *  by the predefined XAtom specified by u {@code latom}
 244      *
 245      * @param display X display to use.
 246      * @param atom a predefined XAtom.
 247      * @since 1.5
 248      */
 249     public XAtom(long display, long atom) {
 250         this.atom = atom;
 251         this.display = display;
 252         register();
 253     }
 254 
 255     /**  This constructor will create the instance,
 256      *  and if {@code autoIntern} is true intern a new XAtom that is specified
 257      *  by the supplied name.
 258      *
 259      * @param display X display to use
 260      * @param name name of the XAtom to create.
 261      * @since 1.5
 262      */
 263 
 264     private XAtom(long display, String name, boolean autoIntern) {
 265         this.name = name;
 266         this.display = display;
 267         if (autoIntern) {
 268             XToolkit.awtLock();
 269             try {
 270                 atom = XlibWrapper.InternAtom(display,name,0);
 271             } finally {
 272                 XToolkit.awtUnlock();
 273             }
 274         }
 275         register();
 276     }


 436             int status = getter.execute();
 437             if (status != XConstants.Success || getter.getData() == 0) {
 438                 return false;
 439             }
 440             if (getter.getActualType() != atom
 441                 || getter.getActualFormat() != 32
 442                 || getter.getNumberOfItems() != length
 443                 )
 444                 {
 445                     return false;
 446                 }
 447             XlibWrapper.memcpy(data_ptr, getter.getData(), length*getAtomSize());
 448             return true;
 449         } finally {
 450             getter.dispose();
 451         }
 452     }
 453 
 454     /**
 455      * Gets uninterpreted set of data from property and stores them in data_ptr.
 456      * Property type is {@code type}, property is current atom.
 457      * Property format is 32. Property 'delete' is false.
 458      * Returns boolean if requested type, format, length match returned values
 459      * and returned data pointer is not null.
 460      */
 461     public boolean getAtomData(long window, long type, long data_ptr, int length) {
 462         if (atom == 0) {
 463             throw new IllegalStateException("Atom should be initialized");
 464         }
 465         checkWindow(window);
 466         WindowPropertyGetter getter =
 467             new WindowPropertyGetter(window, this, 0, (long)length,
 468                                      false, type);
 469         try {
 470             int status = getter.execute();
 471             if (status != XConstants.Success || getter.getData() == 0) {
 472                 return false;
 473             }
 474             if (getter.getActualType() != type
 475                 || getter.getActualFormat() != 32
 476                 || getter.getNumberOfItems() != length


 491      * Property format is 32. Mode is PropModeReplace. length is a number
 492      * of items pointer by data_ptr.
 493      */
 494     public void setAtomData(long window, long data_ptr, int length) {
 495         if (atom == 0) {
 496             throw new IllegalStateException("Atom should be initialized");
 497         }
 498         checkWindow(window);
 499         XToolkit.awtLock();
 500         try {
 501             XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,
 502                 atom, atom, 32, XConstants.PropModeReplace,
 503                 data_ptr, length);
 504         } finally {
 505             XToolkit.awtUnlock();
 506         }
 507     }
 508 
 509     /**
 510      * Sets uninterpreted set of data into property from data_ptr.
 511      * Property type is {@code type}, property is current atom.
 512      * Property format is 32. Mode is PropModeReplace. length is a number
 513      * of items pointer by data_ptr.
 514      */
 515     public void setAtomData(long window, long type, long data_ptr, int length) {
 516         if (atom == 0) {
 517             throw new IllegalStateException("Atom should be initialized");
 518         }
 519         checkWindow(window);
 520         XToolkit.awtLock();
 521         try {
 522             XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,
 523                 atom, type, 32, XConstants.PropModeReplace,
 524                 data_ptr, length);
 525         } finally {
 526             XToolkit.awtUnlock();
 527         }
 528     }
 529 
 530     /**
 531      * Sets uninterpreted set of data into property from data_ptr.
 532      * Property type is {@code type}, property is current atom.
 533      * Property format is 8. Mode is PropModeReplace. length is a number
 534      * of bytes pointer by data_ptr.
 535      */
 536     public void setAtomData8(long window, long type, long data_ptr, int length) {
 537         if (atom == 0) {
 538             throw new IllegalStateException("Atom should be initialized");
 539         }
 540         checkWindow(window);
 541         XToolkit.awtLock();
 542         try {
 543             XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,
 544                 atom, type, 8, XConstants.PropModeReplace,
 545                 data_ptr, length);
 546         } finally {
 547             XToolkit.awtUnlock();
 548         }
 549     }
 550 
 551     /**
 552      * Deletes property specified by this item on the window.


 770     }
 771 
 772     void checkWindow(long window) {
 773         if (window == 0) {
 774             throw new IllegalArgumentException("Window must not be zero");
 775         }
 776     }
 777 
 778     public boolean equals(Object o) {
 779         if (!(o instanceof XAtom)) {
 780             return false;
 781         }
 782         XAtom ot = (XAtom)o;
 783         return (atom == ot.atom && display == ot.display);
 784     }
 785     public int hashCode() {
 786         return (int)((atom ^ display)& 0xFFFFL);
 787     }
 788 
 789     /**
 790      * Sets property on the {@code window} to the value {@code window_value}
 791      * Property is assumed to be of type WINDOW/32
 792      */
 793     public void setWindowProperty(long window, long window_value) {
 794         if (atom == 0) {
 795             throw new IllegalStateException("Atom should be initialized");
 796         }
 797         checkWindow(window);
 798         XToolkit.awtLock();
 799         try {
 800             Native.putWindow(XlibWrapper.larg1, window_value);
 801             XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,
 802                                     atom, XA_WINDOW, 32, XConstants.PropModeReplace,
 803                                     XlibWrapper.larg1, 1);
 804         } finally {
 805             XToolkit.awtUnlock();
 806         }
 807     }
 808     public void setWindowProperty(XBaseWindow window, XBaseWindow window_value) {
 809         setWindowProperty(window.getWindow(), window_value.getWindow());
 810     }
 811 
 812     /**
 813      * Gets property on the {@code window}. Property is assumed to be
 814      * of type WINDOW/32.
 815      */
 816     public long getWindowProperty(long window) {
 817         if (atom == 0) {
 818             throw new IllegalStateException("Atom should be initialized");
 819         }
 820         checkWindow(window);
 821         WindowPropertyGetter getter =
 822             new WindowPropertyGetter(window, this, 0, 1,
 823                                      false, XA_WINDOW);
 824         try {
 825             int status = getter.execute();
 826             if (status != XConstants.Success || getter.getData() == 0) {
 827                 return 0;
 828             }
 829             if (getter.getActualType() != XA_WINDOW || getter.getActualFormat() != 32) {
 830                 return 0;
 831             }
 832             return Native.getWindow(getter.getData());
 833         } finally {
< prev index next >