Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/awt/X11/XAtom.java
32288 views
/*1* Copyright (c) 2002, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.awt.X11;2627/**28* XAtom is a class that allows you to create and modify X Window properties.29* An X Atom is an identifier for a property that you can set on any X Window.30* Standard X Atom are defined by X11 and these atoms are defined in this class31* for convenience. Common X Atoms like <code>XA_WM_NAME</code> are used to communicate with the32* Window manager to let it know the Window name. The use and protocol for these33* atoms are defined in the Inter client communications converntions manual.34* User specified XAtoms are defined by specifying a name that gets Interned35* by the XServer and an <code>XAtom</code> object is returned. An <code>XAtom</code> can also be created36* by using a pre-exisiting atom like <code>XA_WM_CLASS</code>. A <code>display</code> has to be specified37* in order to create an <code>XAtom</code>. <p> <p>38*39* Once an <code>XAtom</code> instance is created, you can call get and set property methods to40* set the values for a particular window. <p> <p>41*42*43* Example usage : To set the window name for a top level: <p>44* <code>45* XAtom xa = new XAtom(display,XAtom.XA_WM_NAME); <p>46* xa.setProperty(window,"Hello World");<p></code>47*<p>48*<p>49* To get the cut buffer :<p>50* <p><code>51* XAtom xa = new XAtom(display,XAtom.XA_CUT_BUFFER0);<p>52* String selection = xa.getProperty(root_window);<p></code>53* @author Bino George54* @since JDK1.555*/5657import sun.misc.Unsafe;58import java.util.HashMap;5960public final class XAtom {6162// Order of lock: XAWTLock -> XAtom.class6364/* Predefined Atoms - automatically extracted from XAtom.h */65private static Unsafe unsafe = XlibWrapper.unsafe;66private static XAtom[] emptyList = new XAtom[0];6768public static final long XA_PRIMARY=1;69public static final long XA_SECONDARY=2;70public static final long XA_ARC=3;71public static final long XA_ATOM=4;72public static final long XA_BITMAP=5;73public static final long XA_CARDINAL=6;74public static final long XA_COLORMAP=7;75public static final long XA_CURSOR=8;76public static final long XA_CUT_BUFFER0=9;77public static final long XA_CUT_BUFFER1=10;78public static final long XA_CUT_BUFFER2=11;79public static final long XA_CUT_BUFFER3=12;80public static final long XA_CUT_BUFFER4=13;81public static final long XA_CUT_BUFFER5=14;82public static final long XA_CUT_BUFFER6=15;83public static final long XA_CUT_BUFFER7=16;84public static final long XA_DRAWABLE=17;85public static final long XA_FONT=18;86public static final long XA_INTEGER=19;87public static final long XA_PIXMAP=20;88public static final long XA_POINT=21;89public static final long XA_RECTANGLE=22;90public static final long XA_RESOURCE_MANAGER=23;91public static final long XA_RGB_COLOR_MAP=24;92public static final long XA_RGB_BEST_MAP=25;93public static final long XA_RGB_BLUE_MAP=26;94public static final long XA_RGB_DEFAULT_MAP=27;95public static final long XA_RGB_GRAY_MAP=28;96public static final long XA_RGB_GREEN_MAP=29;97public static final long XA_RGB_RED_MAP=30;98public static final long XA_STRING=31;99public static final long XA_VISUALID=32;100public static final long XA_WINDOW=33;101public static final long XA_WM_COMMAND=34;102public static final long XA_WM_HINTS=35;103public static final long XA_WM_CLIENT_MACHINE=36;104public static final long XA_WM_ICON_NAME=37;105public static final long XA_WM_ICON_SIZE=38;106public static final long XA_WM_NAME=39;107public static final long XA_WM_NORMAL_HINTS=40;108public static final long XA_WM_SIZE_HINTS=41;109public static final long XA_WM_ZOOM_HINTS=42;110public static final long XA_MIN_SPACE=43;111public static final long XA_NORM_SPACE=44;112public static final long XA_MAX_SPACE=45;113public static final long XA_END_SPACE=46;114public static final long XA_SUPERSCRIPT_X=47;115public static final long XA_SUPERSCRIPT_Y=48;116public static final long XA_SUBSCRIPT_X=49;117public static final long XA_SUBSCRIPT_Y=50;118public static final long XA_UNDERLINE_POSITION=51;119public static final long XA_UNDERLINE_THICKNESS=52 ;120public static final long XA_STRIKEOUT_ASCENT=53;121public static final long XA_STRIKEOUT_DESCENT=54;122public static final long XA_ITALIC_ANGLE=55;123public static final long XA_X_HEIGHT=56;124public static final long XA_QUAD_WIDTH=57;125public static final long XA_WEIGHT=58;126public static final long XA_POINT_SIZE=59;127public static final long XA_RESOLUTION=60;128public static final long XA_COPYRIGHT=61;129public static final long XA_NOTICE=62;130public static final long XA_FONT_NAME=63;131public static final long XA_FAMILY_NAME=64;132public static final long XA_FULL_NAME=65;133public static final long XA_CAP_HEIGHT=66;134public static final long XA_WM_CLASS=67;135public static final long XA_WM_TRANSIENT_FOR=68;136public static final long XA_LAST_PREDEFINED=68;137static HashMap<Long, XAtom> atomToAtom = new HashMap<Long, XAtom>();138static HashMap<String, XAtom> nameToAtom = new HashMap<String, XAtom>();139static void register(XAtom at) {140if (at == null) {141return;142}143synchronized (XAtom.class) {144if (at.atom != 0) {145atomToAtom.put(Long.valueOf(at.atom), at);146}147if (at.name != null) {148nameToAtom.put(at.name, at);149}150}151}152static XAtom lookup(long atom) {153synchronized (XAtom.class) {154return atomToAtom.get(Long.valueOf(atom));155}156}157static XAtom lookup(String name) {158synchronized (XAtom.class) {159return nameToAtom.get(name);160}161}162/*163* [das]Suggestion:164* 1.Make XAtom immutable.165* 2.Replace public ctors with factory methods (e.g. get() below).166*/167static XAtom get(long atom) {168XAtom xatom = lookup(atom);169if (xatom == null) {170xatom = new XAtom(XToolkit.getDisplay(), atom);171}172return xatom;173}174public static XAtom get(String name) {175XAtom xatom = lookup(name);176if (xatom == null) {177xatom = new XAtom(XToolkit.getDisplay(), name);178}179return xatom;180}181public final String getName() {182if (name == null) {183XToolkit.awtLock();184try {185this.name = XlibWrapper.XGetAtomName(display, atom);186} finally {187XToolkit.awtUnlock();188}189register();190}191return name;192}193static String asString(long atom) {194XAtom at = lookup(atom);195if (at == null) {196return Long.toString(atom);197} else {198return at.toString();199}200}201void register() {202register(this);203}204public String toString() {205if (name != null) {206return name + ":" + atom;207} else {208return Long.toString(atom);209}210}211212/* interned value of Atom */213long atom = 0;214215/* name of atom */216String name;217218/* display for X connection */219long display;220221222/** This constructor will create and intern a new XAtom that is specified223* by the supplied name.224*225* @param display X display to use226* @param name name of the XAtom to create.227* @since 1.5228*/229230private XAtom(long display, String name) {231this(display, name, true);232}233234public XAtom(String name, boolean autoIntern) {235this(XToolkit.getDisplay(), name, autoIntern);236}237238/** This constructor will create an instance of XAtom that is specified239* by the predefined XAtom specified by u <code> latom </code>240*241* @param display X display to use.242* @param atom a predefined XAtom.243* @since 1.5244*/245public XAtom(long display, long atom) {246this.atom = atom;247this.display = display;248register();249}250251/** This constructor will create the instance,252* and if <code>autoIntern</code> is true intern a new XAtom that is specified253* by the supplied name.254*255* @param display X display to use256* @param name name of the XAtom to create.257* @since 1.5258*/259260private XAtom(long display, String name, boolean autoIntern) {261this.name = name;262this.display = display;263if (autoIntern) {264XToolkit.awtLock();265try {266atom = XlibWrapper.InternAtom(display,name,0);267} finally {268XToolkit.awtUnlock();269}270}271register();272}273274/**275* Creates uninitialized instance of276*/277public XAtom() {278}279280/** Sets the window property for the specified window281* @param window window id to use282* @param str value to set to.283* @since 1.5284*/285public void setProperty(long window, String str) {286if (atom == 0) {287throw new IllegalStateException("Atom should be initialized");288}289checkWindow(window);290XToolkit.awtLock();291try {292XlibWrapper.SetProperty(display,window,atom,str);293} finally {294XToolkit.awtUnlock();295}296}297298/**299* Sets UTF8_STRING type property. Explicitly converts str to UTF-8 byte sequence.300*/301public void setPropertyUTF8(long window, String str) {302XAtom XA_UTF8_STRING = XAtom.get("UTF8_STRING"); /* like STRING but encoding is UTF-8 */303if (atom == 0) {304throw new IllegalStateException("Atom should be initialized");305}306checkWindow(window);307byte[] bdata = null;308try {309bdata = str.getBytes("UTF-8");310} catch (java.io.UnsupportedEncodingException uee) {311uee.printStackTrace();312}313if (bdata != null) {314setAtomData(window, XA_UTF8_STRING.atom, bdata);315}316}317318/**319* Sets STRING/8 type property. Explicitly converts str to Latin-1 byte sequence.320*/321public void setProperty8(long window, String str) {322if (atom == 0) {323throw new IllegalStateException("Atom should be initialized");324}325checkWindow(window);326byte[] bdata = null;327try {328bdata = str.getBytes("ISO-8859-1");329} catch (java.io.UnsupportedEncodingException uee) {330uee.printStackTrace();331}332if (bdata != null) {333setAtomData(window, XA_STRING, bdata);334}335}336337338/** Gets the window property for the specified window339* @param window window id to use340* @param str value to set to.341* @return string with the property.342* @since 1.5343*/344public String getProperty(long window) {345if (atom == 0) {346throw new IllegalStateException("Atom should be initialized");347}348checkWindow(window);349XToolkit.awtLock();350try {351return XlibWrapper.GetProperty(display,window,atom);352} finally {353XToolkit.awtUnlock();354}355}356357358/*359* Auxiliary function that returns the value of 'property' of type360* 'property_type' on window 'window'. Format of the property must be 32.361*/362public long get32Property(long window, long property_type) {363if (atom == 0) {364throw new IllegalStateException("Atom should be initialized");365}366checkWindow(window);367WindowPropertyGetter getter =368new WindowPropertyGetter(window, this, 0, 1,369false, property_type);370try {371int status = getter.execute();372if (status != XConstants.Success || getter.getData() == 0) {373return 0;374}375if (getter.getActualType() != property_type || getter.getActualFormat() != 32) {376return 0;377}378return Native.getCard32(getter.getData());379} finally {380getter.dispose();381}382}383384/**385* Returns value of property of type CARDINAL/32 of this window386*/387public long getCard32Property(XBaseWindow window) {388return get32Property(window.getWindow(), XA_CARDINAL);389}390391/**392* Sets property of type CARDINAL on the window393*/394public void setCard32Property(long window, long value) {395if (atom == 0) {396throw new IllegalStateException("Atom should be initialized");397}398checkWindow(window);399XToolkit.awtLock();400try {401Native.putCard32(XlibWrapper.larg1, value);402XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,403atom, XA_CARDINAL, 32, XConstants.PropModeReplace,404XlibWrapper.larg1, 1);405} finally {406XToolkit.awtUnlock();407}408}409410/**411* Sets property of type CARDINAL/32 on the window412*/413public void setCard32Property(XBaseWindow window, long value) {414setCard32Property(window.getWindow(), value);415}416417/**418* Gets uninterpreted set of data from property and stores them in data_ptr.419* Property type is the same as current atom, property is current atom.420* Property format is 32. Property 'delete' is false.421* Returns boolean if requested type, format, length match returned values422* and returned data pointer is not null.423*/424public boolean getAtomData(long window, long data_ptr, int length) {425if (atom == 0) {426throw new IllegalStateException("Atom should be initialized");427}428checkWindow(window);429WindowPropertyGetter getter =430new WindowPropertyGetter(window, this, 0, (long)length,431false, this);432try {433int status = getter.execute();434if (status != XConstants.Success || getter.getData() == 0) {435return false;436}437if (getter.getActualType() != atom438|| getter.getActualFormat() != 32439|| getter.getNumberOfItems() != length440)441{442return false;443}444XlibWrapper.memcpy(data_ptr, getter.getData(), length*getAtomSize());445return true;446} finally {447getter.dispose();448}449}450451/**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 values456* and returned data pointer is not null.457*/458public boolean getAtomData(long window, long type, long data_ptr, int length) {459if (atom == 0) {460throw new IllegalStateException("Atom should be initialized");461}462checkWindow(window);463WindowPropertyGetter getter =464new WindowPropertyGetter(window, this, 0, (long)length,465false, type);466try {467int status = getter.execute();468if (status != XConstants.Success || getter.getData() == 0) {469return false;470}471if (getter.getActualType() != type472|| getter.getActualFormat() != 32473|| getter.getNumberOfItems() != length474)475{476return false;477}478XlibWrapper.memcpy(data_ptr, getter.getData(), length*getAtomSize());479return true;480} finally {481getter.dispose();482}483}484485/**486* Sets uninterpreted set of data into property from data_ptr.487* Property type is the same as current atom, property is current atom.488* Property format is 32. Mode is PropModeReplace. length is a number489* of items pointer by data_ptr.490*/491public void setAtomData(long window, long data_ptr, int length) {492if (atom == 0) {493throw new IllegalStateException("Atom should be initialized");494}495checkWindow(window);496XToolkit.awtLock();497try {498XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,499atom, atom, 32, XConstants.PropModeReplace,500data_ptr, length);501} finally {502XToolkit.awtUnlock();503}504}505506/**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 number510* of items pointer by data_ptr.511*/512public void setAtomData(long window, long type, long data_ptr, int length) {513if (atom == 0) {514throw new IllegalStateException("Atom should be initialized");515}516checkWindow(window);517XToolkit.awtLock();518try {519XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,520atom, type, 32, XConstants.PropModeReplace,521data_ptr, length);522} finally {523XToolkit.awtUnlock();524}525}526527/**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 number531* of bytes pointer by data_ptr.532*/533public void setAtomData8(long window, long type, long data_ptr, int length) {534if (atom == 0) {535throw new IllegalStateException("Atom should be initialized");536}537checkWindow(window);538XToolkit.awtLock();539try {540XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,541atom, type, 8, XConstants.PropModeReplace,542data_ptr, length);543} finally {544XToolkit.awtUnlock();545}546}547548/**549* Deletes property specified by this item on the window.550*/551public void DeleteProperty(long window) {552if (atom == 0) {553throw new IllegalStateException("Atom should be initialized");554}555checkWindow(window);556XToolkit.awtLock();557try {558XlibWrapper.XDeleteProperty(XToolkit.getDisplay(), window, atom);559} finally {560XToolkit.awtUnlock();561}562}563564/**565* Deletes property specified by this item on the window.566*/567public void DeleteProperty(XBaseWindow window) {568if (atom == 0) {569throw new IllegalStateException("Atom should be initialized");570}571checkWindow(window.getWindow());572XToolkit.awtLock();573try {574XlibWrapper.XDeleteProperty(XToolkit.getDisplay(),575window.getWindow(), atom);576} finally {577XToolkit.awtUnlock();578}579}580581public void setAtomData(long window, long property_type, byte[] data) {582long bdata = Native.toData(data);583try {584setAtomData8(window, property_type, bdata, data.length);585} finally {586unsafe.freeMemory(bdata);587}588}589590/*591* Auxiliary function that returns the value of 'property' of type592* 'property_type' on window 'window'. Format of the property must be 8.593*/594public byte[] getByteArrayProperty(long window, long property_type) {595if (atom == 0) {596throw new IllegalStateException("Atom should be initialized");597}598checkWindow(window);599WindowPropertyGetter getter =600new WindowPropertyGetter(window, this, 0, 0xFFFF,601false, property_type);602try {603int status = getter.execute();604if (status != XConstants.Success || getter.getData() == 0) {605return null;606}607if (getter.getActualType() != property_type || getter.getActualFormat() != 8) {608return null;609}610byte[] res = XlibWrapper.getStringBytes(getter.getData());611return res;612} finally {613getter.dispose();614}615}616617/**618* Interns the XAtom619*/620public void intern(boolean onlyIfExists) {621XToolkit.awtLock();622try {623atom = XlibWrapper.InternAtom(display,name, onlyIfExists?1:0);624} finally {625XToolkit.awtUnlock();626}627register();628}629630public boolean isInterned() {631if (atom == 0) {632XToolkit.awtLock();633try {634atom = XlibWrapper.InternAtom(display, name, 1);635} finally {636XToolkit.awtUnlock();637}638if (atom == 0) {639return false;640} else {641register();642return true;643}644} else {645return true;646}647}648649public void setValues(long display, String name, long atom) {650this.display = display;651this.atom = atom;652this.name = name;653register();654}655656static int getAtomSize() {657return Native.getLongSize();658}659660/*661* Returns the value of property ATOM[]/32 as array of XAtom objects662* @return array of atoms, array of length 0 if the atom list is empty663* or has different format664*/665XAtom[] getAtomListProperty(long window) {666if (atom == 0) {667throw new IllegalStateException("Atom should be initialized");668}669checkWindow(window);670671WindowPropertyGetter getter =672new WindowPropertyGetter(window, this, 0, 0xFFFF,673false, XA_ATOM);674try {675int status = getter.execute();676if (status != XConstants.Success || getter.getData() == 0) {677return emptyList;678}679if (getter.getActualType() != XA_ATOM || getter.getActualFormat() != 32) {680return emptyList;681}682683int count = (int)getter.getNumberOfItems();684if (count == 0) {685return emptyList;686}687long list_atoms = getter.getData();688XAtom[] res = new XAtom[count];689for (int index = 0; index < count; index++) {690res[index] = XAtom.get(XAtom.getAtom(list_atoms+index*getAtomSize()));691}692return res;693} finally {694getter.dispose();695}696}697698/*699* Returns the value of property of type ATOM[]/32 as XAtomList700* @return list of atoms, empty list if the atom list is empty701* or has different format702*/703XAtomList getAtomListPropertyList(long window) {704return new XAtomList(getAtomListProperty(window));705}706XAtomList getAtomListPropertyList(XBaseWindow window) {707return getAtomListPropertyList(window.getWindow());708}709XAtom[] getAtomListProperty(XBaseWindow window) {710return getAtomListProperty(window.getWindow());711}712713/**714* Sets property value of type ATOM list to the list of atoms.715*/716void setAtomListProperty(long window, XAtom[] atoms) {717long data = toData(atoms);718setAtomData(window, XAtom.XA_ATOM, data, atoms.length);719unsafe.freeMemory(data);720}721722/**723* Sets property value of type ATOM list to the list of atoms specified by XAtomList724*/725void setAtomListProperty(long window, XAtomList atoms) {726long data = atoms.getAtomsData();727setAtomData(window, XAtom.XA_ATOM, data, atoms.size());728unsafe.freeMemory(data);729}730/**731* Sets property value of type ATOM list to the list of atoms.732*/733public void setAtomListProperty(XBaseWindow window, XAtom[] atoms) {734setAtomListProperty(window.getWindow(), atoms);735}736737/**738* Sets property value of type ATOM list to the list of atoms specified by XAtomList739*/740public void setAtomListProperty(XBaseWindow window, XAtomList atoms) {741setAtomListProperty(window.getWindow(), atoms);742}743744long getAtom() {745return atom;746}747748void putAtom(long ptr) {749Native.putLong(ptr, atom);750}751752static long getAtom(long ptr) {753return Native.getLong(ptr);754}755/**756* Allocated memory to hold the list of native atom data and returns unsafe pointer to it757* Caller should free the memory by himself.758*/759static long toData(XAtom[] atoms) {760long data = unsafe.allocateMemory(getAtomSize() * atoms.length);761for (int i = 0; i < atoms.length; i++ ) {762if (atoms[i] != null) {763atoms[i].putAtom(data + i * getAtomSize());764}765}766return data;767}768769void checkWindow(long window) {770if (window == 0) {771throw new IllegalArgumentException("Window must not be zero");772}773}774775public boolean equals(Object o) {776if (!(o instanceof XAtom)) {777return false;778}779XAtom ot = (XAtom)o;780return (atom == ot.atom && display == ot.display);781}782public int hashCode() {783return (int)((atom ^ display)& 0xFFFFL);784}785786/**787* Sets property on the <code>window</code> to the value <code>window_value</window>788* Property is assumed to be of type WINDOW/32789*/790public void setWindowProperty(long window, long window_value) {791if (atom == 0) {792throw new IllegalStateException("Atom should be initialized");793}794checkWindow(window);795XToolkit.awtLock();796try {797Native.putWindow(XlibWrapper.larg1, window_value);798XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,799atom, XA_WINDOW, 32, XConstants.PropModeReplace,800XlibWrapper.larg1, 1);801} finally {802XToolkit.awtUnlock();803}804}805public void setWindowProperty(XBaseWindow window, XBaseWindow window_value) {806setWindowProperty(window.getWindow(), window_value.getWindow());807}808809/**810* Gets property on the <code>window</code>. Property is assumed to be811* of type WINDOW/32.812*/813public long getWindowProperty(long window) {814if (atom == 0) {815throw new IllegalStateException("Atom should be initialized");816}817checkWindow(window);818WindowPropertyGetter getter =819new WindowPropertyGetter(window, this, 0, 1,820false, XA_WINDOW);821try {822int status = getter.execute();823if (status != XConstants.Success || getter.getData() == 0) {824return 0;825}826if (getter.getActualType() != XA_WINDOW || getter.getActualFormat() != 32) {827return 0;828}829return Native.getWindow(getter.getData());830} finally {831getter.dispose();832}833}834}835836837