Path: blob/master/src/java.desktop/unix/classes/sun/awt/X11/XAtom.java
41159 views
/*1* Copyright (c) 2002, 2014, 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} 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} object is returned. An {@code XAtom} can also be created36* by using a pre-exisiting atom like {@code XA_WM_CLASS}. A {@code display} has to be specified37* in order to create an {@code XAtom}. <p> <p>38*39* Once an {@code XAtom} 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* <pre>{@code45* XAtom xa = new XAtom(display,XAtom.XA_WM_NAME);46* xa.setProperty(window,"Hello World");47* }</pre>48* <p>49* <p>50* To get the cut buffer:51* <pre>{@code52* XAtom xa = new XAtom(display,XAtom.XA_CUT_BUFFER0);53* String selection = xa.getProperty(root_window);54* }</pre>55*56* @author Bino George57* @since 1.558*/5960import jdk.internal.misc.Unsafe;61import java.util.HashMap;6263public final class XAtom {6465// Order of lock: XAWTLock -> XAtom.class6667/* Predefined Atoms - automatically extracted from XAtom.h */68private static Unsafe unsafe = XlibWrapper.unsafe;69private static XAtom[] emptyList = new XAtom[0];7071public static final long XA_PRIMARY=1;72public static final long XA_SECONDARY=2;73public static final long XA_ARC=3;74public static final long XA_ATOM=4;75public static final long XA_BITMAP=5;76public static final long XA_CARDINAL=6;77public static final long XA_COLORMAP=7;78public static final long XA_CURSOR=8;79public static final long XA_CUT_BUFFER0=9;80public static final long XA_CUT_BUFFER1=10;81public static final long XA_CUT_BUFFER2=11;82public static final long XA_CUT_BUFFER3=12;83public static final long XA_CUT_BUFFER4=13;84public static final long XA_CUT_BUFFER5=14;85public static final long XA_CUT_BUFFER6=15;86public static final long XA_CUT_BUFFER7=16;87public static final long XA_DRAWABLE=17;88public static final long XA_FONT=18;89public static final long XA_INTEGER=19;90public static final long XA_PIXMAP=20;91public static final long XA_POINT=21;92public static final long XA_RECTANGLE=22;93public static final long XA_RESOURCE_MANAGER=23;94public static final long XA_RGB_COLOR_MAP=24;95public static final long XA_RGB_BEST_MAP=25;96public static final long XA_RGB_BLUE_MAP=26;97public static final long XA_RGB_DEFAULT_MAP=27;98public static final long XA_RGB_GRAY_MAP=28;99public static final long XA_RGB_GREEN_MAP=29;100public static final long XA_RGB_RED_MAP=30;101public static final long XA_STRING=31;102public static final long XA_VISUALID=32;103public static final long XA_WINDOW=33;104public static final long XA_WM_COMMAND=34;105public static final long XA_WM_HINTS=35;106public static final long XA_WM_CLIENT_MACHINE=36;107public static final long XA_WM_ICON_NAME=37;108public static final long XA_WM_ICON_SIZE=38;109public static final long XA_WM_NAME=39;110public static final long XA_WM_NORMAL_HINTS=40;111public static final long XA_WM_SIZE_HINTS=41;112public static final long XA_WM_ZOOM_HINTS=42;113public static final long XA_MIN_SPACE=43;114public static final long XA_NORM_SPACE=44;115public static final long XA_MAX_SPACE=45;116public static final long XA_END_SPACE=46;117public static final long XA_SUPERSCRIPT_X=47;118public static final long XA_SUPERSCRIPT_Y=48;119public static final long XA_SUBSCRIPT_X=49;120public static final long XA_SUBSCRIPT_Y=50;121public static final long XA_UNDERLINE_POSITION=51;122public static final long XA_UNDERLINE_THICKNESS=52 ;123public static final long XA_STRIKEOUT_ASCENT=53;124public static final long XA_STRIKEOUT_DESCENT=54;125public static final long XA_ITALIC_ANGLE=55;126public static final long XA_X_HEIGHT=56;127public static final long XA_QUAD_WIDTH=57;128public static final long XA_WEIGHT=58;129public static final long XA_POINT_SIZE=59;130public static final long XA_RESOLUTION=60;131public static final long XA_COPYRIGHT=61;132public static final long XA_NOTICE=62;133public static final long XA_FONT_NAME=63;134public static final long XA_FAMILY_NAME=64;135public static final long XA_FULL_NAME=65;136public static final long XA_CAP_HEIGHT=66;137public static final long XA_WM_CLASS=67;138public static final long XA_WM_TRANSIENT_FOR=68;139public static final long XA_LAST_PREDEFINED=68;140static HashMap<Long, XAtom> atomToAtom = new HashMap<Long, XAtom>();141static HashMap<String, XAtom> nameToAtom = new HashMap<String, XAtom>();142static void register(XAtom at) {143if (at == null) {144return;145}146synchronized (XAtom.class) {147if (at.atom != 0) {148atomToAtom.put(Long.valueOf(at.atom), at);149}150if (at.name != null) {151nameToAtom.put(at.name, at);152}153}154}155static XAtom lookup(long atom) {156synchronized (XAtom.class) {157return atomToAtom.get(Long.valueOf(atom));158}159}160static XAtom lookup(String name) {161synchronized (XAtom.class) {162return nameToAtom.get(name);163}164}165/*166* [das]Suggestion:167* 1.Make XAtom immutable.168* 2.Replace public ctors with factory methods (e.g. get() below).169*/170static XAtom get(long atom) {171XAtom xatom = lookup(atom);172if (xatom == null) {173xatom = new XAtom(XToolkit.getDisplay(), atom);174}175return xatom;176}177public static XAtom get(String name) {178XAtom xatom = lookup(name);179if (xatom == null) {180xatom = new XAtom(XToolkit.getDisplay(), name);181}182return xatom;183}184public String getName() {185if (name == null) {186XToolkit.awtLock();187try {188this.name = XlibWrapper.XGetAtomName(display, atom);189} finally {190XToolkit.awtUnlock();191}192register();193}194return name;195}196static String asString(long atom) {197XAtom at = lookup(atom);198if (at == null) {199return Long.toString(atom);200} else {201return at.toString();202}203}204void register() {205register(this);206}207public String toString() {208if (name != null) {209return name + ":" + atom;210} else {211return Long.toString(atom);212}213}214215/* interned value of Atom */216long atom = 0;217218/* name of atom */219String name;220221/* display for X connection */222long display;223224225/** This constructor will create and intern a new XAtom that is specified226* by the supplied name.227*228* @param display X display to use229* @param name name of the XAtom to create.230* @since 1.5231*/232233private XAtom(long display, String name) {234this(display, name, true);235}236237public XAtom(String name, boolean autoIntern) {238this(XToolkit.getDisplay(), name, autoIntern);239}240241/** This constructor will create an instance of XAtom that is specified242* by the predefined XAtom specified by u {@code latom}243*244* @param display X display to use.245* @param atom a predefined XAtom.246* @since 1.5247*/248public XAtom(long display, long atom) {249this.atom = atom;250this.display = display;251register();252}253254/** This constructor will create the instance,255* and if {@code autoIntern} is true intern a new XAtom that is specified256* by the supplied name.257*258* @param display X display to use259* @param name name of the XAtom to create.260* @since 1.5261*/262263private XAtom(long display, String name, boolean autoIntern) {264this.name = name;265this.display = display;266if (autoIntern) {267XToolkit.awtLock();268try {269atom = XlibWrapper.InternAtom(display,name,0);270} finally {271XToolkit.awtUnlock();272}273}274register();275}276277/**278* Creates uninitialized instance of279*/280public XAtom() {281}282283/** Sets the window property for the specified window284* @param window window id to use285* @param str value to set to.286* @since 1.5287*/288public void setProperty(long window, String str) {289if (atom == 0) {290throw new IllegalStateException("Atom should be initialized");291}292checkWindow(window);293XToolkit.awtLock();294try {295XlibWrapper.SetProperty(display,window,atom,str);296} finally {297XToolkit.awtUnlock();298}299}300301/**302* Sets UTF8_STRING type property. Explicitly converts str to UTF-8 byte sequence.303*/304public void setPropertyUTF8(long window, String str) {305XAtom XA_UTF8_STRING = XAtom.get("UTF8_STRING"); /* like STRING but encoding is UTF-8 */306if (atom == 0) {307throw new IllegalStateException("Atom should be initialized");308}309checkWindow(window);310byte[] bdata = null;311try {312bdata = str.getBytes("UTF-8");313} catch (java.io.UnsupportedEncodingException uee) {314uee.printStackTrace();315}316if (bdata != null) {317setAtomData(window, XA_UTF8_STRING.atom, bdata);318}319}320321/**322* Sets STRING/8 type property. Explicitly converts str to Latin-1 byte sequence.323*/324public void setProperty8(long window, String str) {325if (atom == 0) {326throw new IllegalStateException("Atom should be initialized");327}328checkWindow(window);329byte[] bdata = null;330try {331bdata = str.getBytes("ISO-8859-1");332} catch (java.io.UnsupportedEncodingException uee) {333uee.printStackTrace();334}335if (bdata != null) {336setAtomData(window, XA_STRING, bdata);337}338}339340341/** Gets the window property for the specified window342* @param window window id to use343* @return string with the property.344* @since 1.5345*/346public String getProperty(long window) {347if (atom == 0) {348throw new IllegalStateException("Atom should be initialized");349}350checkWindow(window);351XToolkit.awtLock();352try {353return XlibWrapper.GetProperty(display,window,atom);354} finally {355XToolkit.awtUnlock();356}357}358359360/*361* Auxiliary function that returns the value of 'property' of type362* 'property_type' on window 'window'. Format of the property must be 32.363*/364public long get32Property(long window, long property_type) {365if (atom == 0) {366throw new IllegalStateException("Atom should be initialized");367}368checkWindow(window);369WindowPropertyGetter getter =370new WindowPropertyGetter(window, this, 0, 1,371false, property_type);372try {373int status = getter.execute();374if (status != XConstants.Success || getter.getData() == 0) {375return 0;376}377if (getter.getActualType() != property_type || getter.getActualFormat() != 32) {378return 0;379}380return Native.getCard32(getter.getData());381} finally {382getter.dispose();383}384}385386/**387* Returns value of property of type CARDINAL/32 of this window388*/389public long getCard32Property(XBaseWindow window) {390return get32Property(window.getWindow(), XA_CARDINAL);391}392393/**394* Sets property of type CARDINAL on the window395*/396public void setCard32Property(long window, long value) {397if (atom == 0) {398throw new IllegalStateException("Atom should be initialized");399}400checkWindow(window);401XToolkit.awtLock();402try {403Native.putCard32(XlibWrapper.larg1, value);404XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,405atom, XA_CARDINAL, 32, XConstants.PropModeReplace,406XlibWrapper.larg1, 1);407} finally {408XToolkit.awtUnlock();409}410}411412/**413* Sets property of type CARDINAL/32 on the window414*/415public void setCard32Property(XBaseWindow window, long value) {416setCard32Property(window.getWindow(), value);417}418419/**420* Gets uninterpreted set of data from property and stores them in data_ptr.421* Property type is the same as current atom, property is current atom.422* Property format is 32. Property 'delete' is false.423* Returns boolean if requested type, format, length match returned values424* and returned data pointer is not null.425*/426public boolean getAtomData(long window, long data_ptr, int length) {427if (atom == 0) {428throw new IllegalStateException("Atom should be initialized");429}430checkWindow(window);431WindowPropertyGetter getter =432new WindowPropertyGetter(window, this, 0, (long)length,433false, this);434try {435int status = getter.execute();436if (status != XConstants.Success || getter.getData() == 0) {437return false;438}439if (getter.getActualType() != atom440|| getter.getActualFormat() != 32441|| getter.getNumberOfItems() != length442)443{444return false;445}446XlibWrapper.memcpy(data_ptr, getter.getData(), length*getAtomSize());447return true;448} finally {449getter.dispose();450}451}452453/**454* Gets uninterpreted set of data from property and stores them in data_ptr.455* Property type is {@code type}, property is current atom.456* Property format is 32. Property 'delete' is false.457* Returns boolean if requested type, format, length match returned values458* and returned data pointer is not null.459*/460public boolean getAtomData(long window, long type, long data_ptr, int length) {461if (atom == 0) {462throw new IllegalStateException("Atom should be initialized");463}464checkWindow(window);465WindowPropertyGetter getter =466new WindowPropertyGetter(window, this, 0, (long)length,467false, type);468try {469int status = getter.execute();470if (status != XConstants.Success || getter.getData() == 0) {471return false;472}473if (getter.getActualType() != type474|| getter.getActualFormat() != 32475|| getter.getNumberOfItems() != length476)477{478return false;479}480XlibWrapper.memcpy(data_ptr, getter.getData(), length*getAtomSize());481return true;482} finally {483getter.dispose();484}485}486487/**488* Sets uninterpreted set of data into property from data_ptr.489* Property type is the same as current atom, property is current atom.490* Property format is 32. Mode is PropModeReplace. length is a number491* of items pointer by data_ptr.492*/493public void setAtomData(long window, long data_ptr, int length) {494if (atom == 0) {495throw new IllegalStateException("Atom should be initialized");496}497checkWindow(window);498XToolkit.awtLock();499try {500XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,501atom, atom, 32, XConstants.PropModeReplace,502data_ptr, length);503} finally {504XToolkit.awtUnlock();505}506}507508/**509* Sets uninterpreted set of data into property from data_ptr.510* Property type is {@code type}, property is current atom.511* Property format is 32. Mode is PropModeReplace. length is a number512* of items pointer by data_ptr.513*/514public void setAtomData(long window, long type, long data_ptr, int length) {515if (atom == 0) {516throw new IllegalStateException("Atom should be initialized");517}518checkWindow(window);519XToolkit.awtLock();520try {521XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,522atom, type, 32, XConstants.PropModeReplace,523data_ptr, length);524} finally {525XToolkit.awtUnlock();526}527}528529/**530* Sets uninterpreted set of data into property from data_ptr.531* Property type is {@code type}, property is current atom.532* Property format is 8. Mode is PropModeReplace. length is a number533* of bytes pointer by data_ptr.534*/535public void setAtomData8(long window, long type, long data_ptr, int length) {536if (atom == 0) {537throw new IllegalStateException("Atom should be initialized");538}539checkWindow(window);540XToolkit.awtLock();541try {542XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,543atom, type, 8, XConstants.PropModeReplace,544data_ptr, length);545} finally {546XToolkit.awtUnlock();547}548}549550/**551* Deletes property specified by this item on the window.552*/553public void DeleteProperty(long window) {554if (atom == 0) {555throw new IllegalStateException("Atom should be initialized");556}557checkWindow(window);558XToolkit.awtLock();559try {560XlibWrapper.XDeleteProperty(XToolkit.getDisplay(), window, atom);561} finally {562XToolkit.awtUnlock();563}564}565566/**567* Deletes property specified by this item on the window.568*/569public void DeleteProperty(XBaseWindow window) {570if (atom == 0) {571throw new IllegalStateException("Atom should be initialized");572}573checkWindow(window.getWindow());574XToolkit.awtLock();575try {576XlibWrapper.XDeleteProperty(XToolkit.getDisplay(),577window.getWindow(), atom);578} finally {579XToolkit.awtUnlock();580}581}582583public void setAtomData(long window, long property_type, byte[] data) {584long bdata = Native.toData(data);585try {586setAtomData8(window, property_type, bdata, data.length);587} finally {588unsafe.freeMemory(bdata);589}590}591592/*593* Auxiliary function that returns the value of 'property' of type594* 'property_type' on window 'window'. Format of the property must be 8.595*/596public byte[] getByteArrayProperty(long window, long property_type) {597if (atom == 0) {598throw new IllegalStateException("Atom should be initialized");599}600checkWindow(window);601WindowPropertyGetter getter =602new WindowPropertyGetter(window, this, 0, 0xFFFF,603false, property_type);604try {605int status = getter.execute();606if (status != XConstants.Success || getter.getData() == 0) {607return null;608}609if (getter.getActualType() != property_type || getter.getActualFormat() != 8) {610return null;611}612byte[] res = XlibWrapper.getStringBytes(getter.getData());613return res;614} finally {615getter.dispose();616}617}618619/**620* Interns the XAtom621*/622public void intern(boolean onlyIfExists) {623XToolkit.awtLock();624try {625atom = XlibWrapper.InternAtom(display,name, onlyIfExists?1:0);626} finally {627XToolkit.awtUnlock();628}629register();630}631632public boolean isInterned() {633if (atom == 0) {634XToolkit.awtLock();635try {636atom = XlibWrapper.InternAtom(display, name, 1);637} finally {638XToolkit.awtUnlock();639}640if (atom == 0) {641return false;642} else {643register();644return true;645}646} else {647return true;648}649}650651public void setValues(long display, String name, long atom) {652this.display = display;653this.atom = atom;654this.name = name;655register();656}657658static int getAtomSize() {659return Native.getLongSize();660}661662/*663* Returns the value of property ATOM[]/32 as array of XAtom objects664* @return array of atoms, array of length 0 if the atom list is empty665* or has different format666*/667XAtom[] getAtomListProperty(long window) {668if (atom == 0) {669throw new IllegalStateException("Atom should be initialized");670}671checkWindow(window);672673WindowPropertyGetter getter =674new WindowPropertyGetter(window, this, 0, 0xFFFF,675false, XA_ATOM);676try {677int status = getter.execute();678if (status != XConstants.Success || getter.getData() == 0) {679return emptyList;680}681if (getter.getActualType() != XA_ATOM || getter.getActualFormat() != 32) {682return emptyList;683}684685int count = getter.getNumberOfItems();686if (count == 0) {687return emptyList;688}689long list_atoms = getter.getData();690XAtom[] res = new XAtom[count];691for (int index = 0; index < count; index++) {692res[index] = XAtom.get(XAtom.getAtom(list_atoms+index*getAtomSize()));693}694return res;695} finally {696getter.dispose();697}698}699700/*701* Returns the value of property of type ATOM[]/32 as XAtomList702* @return list of atoms, empty list if the atom list is empty703* or has different format704*/705XAtomList getAtomListPropertyList(long window) {706return new XAtomList(getAtomListProperty(window));707}708XAtomList getAtomListPropertyList(XBaseWindow window) {709return getAtomListPropertyList(window.getWindow());710}711XAtom[] getAtomListProperty(XBaseWindow window) {712return getAtomListProperty(window.getWindow());713}714715/**716* Sets property value of type ATOM list to the list of atoms.717*/718void setAtomListProperty(long window, XAtom[] atoms) {719long data = toData(atoms);720setAtomData(window, XAtom.XA_ATOM, data, atoms.length);721unsafe.freeMemory(data);722}723724/**725* Sets property value of type ATOM list to the list of atoms specified by XAtomList726*/727void setAtomListProperty(long window, XAtomList atoms) {728long data = atoms.getAtomsData();729setAtomData(window, XAtom.XA_ATOM, data, atoms.size());730unsafe.freeMemory(data);731}732/**733* Sets property value of type ATOM list to the list of atoms.734*/735public void setAtomListProperty(XBaseWindow window, XAtom[] atoms) {736setAtomListProperty(window.getWindow(), atoms);737}738739/**740* Sets property value of type ATOM list to the list of atoms specified by XAtomList741*/742public void setAtomListProperty(XBaseWindow window, XAtomList atoms) {743setAtomListProperty(window.getWindow(), atoms);744}745746long getAtom() {747return atom;748}749750void putAtom(long ptr) {751Native.putLong(ptr, atom);752}753754static long getAtom(long ptr) {755return Native.getLong(ptr);756}757/**758* Allocated memory to hold the list of native atom data and returns unsafe pointer to it759* Caller should free the memory by himself.760*/761static long toData(XAtom[] atoms) {762long data = unsafe.allocateMemory(getAtomSize() * atoms.length);763for (int i = 0; i < atoms.length; i++ ) {764if (atoms[i] != null) {765atoms[i].putAtom(data + i * getAtomSize());766}767}768return data;769}770771void checkWindow(long window) {772if (window == 0) {773throw new IllegalArgumentException("Window must not be zero");774}775}776777public boolean equals(Object o) {778if (!(o instanceof XAtom)) {779return false;780}781XAtom ot = (XAtom)o;782return (atom == ot.atom && display == ot.display);783}784public int hashCode() {785return (int)((atom ^ display)& 0xFFFFL);786}787788/**789* Sets property on the {@code window} to the value {@code window_value}790* Property is assumed to be of type WINDOW/32791*/792public void setWindowProperty(long window, long window_value) {793if (atom == 0) {794throw new IllegalStateException("Atom should be initialized");795}796checkWindow(window);797XToolkit.awtLock();798try {799Native.putWindow(XlibWrapper.larg1, window_value);800XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,801atom, XA_WINDOW, 32, XConstants.PropModeReplace,802XlibWrapper.larg1, 1);803} finally {804XToolkit.awtUnlock();805}806}807public void setWindowProperty(XBaseWindow window, XBaseWindow window_value) {808setWindowProperty(window.getWindow(), window_value.getWindow());809}810811/**812* Gets property on the {@code window}. Property is assumed to be813* of type WINDOW/32.814*/815public long getWindowProperty(long window) {816if (atom == 0) {817throw new IllegalStateException("Atom should be initialized");818}819checkWindow(window);820WindowPropertyGetter getter =821new WindowPropertyGetter(window, this, 0, 1,822false, XA_WINDOW);823try {824int status = getter.execute();825if (status != XConstants.Success || getter.getData() == 0) {826return 0;827}828if (getter.getActualType() != XA_WINDOW || getter.getActualFormat() != 32) {829return 0;830}831return Native.getWindow(getter.getData());832} finally {833getter.dispose();834}835}836}837838839